MCPcopy Create free account
hub / github.com/Noumena-Network/code / relay_exec

Function relay_exec

rust/py_repl_host/src/main.rs:132–194  ·  view source on GitHub ↗
(
    exec_id: &str,
    code: &str,
    timeout_ms: Option<u64>,
    kernel: &mut KernelProcess,
    parent_input: &mut I,
    parent_output: &mut W,
)

Source from the content-addressed store, hash-verified

130}
131
132fn relay_exec<I, W>(
133 exec_id: &str,
134 code: &str,
135 timeout_ms: Option<u64>,
136 kernel: &mut KernelProcess,
137 parent_input: &mut I,
138 parent_output: &mut W,
139) -> Result<(), String>
140where
141 I: Iterator<Item = io::Result<String>>,
142 W: Write,
143{
144 let exec_message = serde_json::json!({
145 "type": "exec",
146 "id": exec_id,
147 "code": code,
148 "timeout_ms": timeout_ms,
149 });
150 write_json_line(&mut kernel.stdin, &exec_message).map_err(|err| err.to_string())?;
151
152 loop {
153 let mut line = String::new();
154 let bytes_read = kernel
155 .stdout
156 .read_line(&mut line)
157 .map_err(|err| err.to_string())?;
158
159 if bytes_read == 0 {
160 return Err(format!(
161 "py_repl rust host lost the Python kernel: {}",
162 format_stderr_tail(&kernel.stderr_tail)
163 ));
164 }
165
166 let trimmed = line.trim();
167 if trimmed.is_empty() {
168 continue;
169 }
170
171 let message_type = extract_type(trimmed);
172 match message_type.as_deref() {
173 Some("run_tool") => {
174 parent_output
175 .write_all(trimmed.as_bytes())
176 .and_then(|_| parent_output.write_all(b"\n"))
177 .and_then(|_| parent_output.flush())
178 .map_err(|err| err.to_string())?;
179
180 let response = wait_for_parent_run_tool_result(parent_input)?;
181 write_json_line(&mut kernel.stdin, &response).map_err(|err| err.to_string())?;
182 }
183 Some("exec_result") => {
184 parent_output
185 .write_all(trimmed.as_bytes())
186 .and_then(|_| parent_output.write_all(b"\n"))
187 .and_then(|_| parent_output.flush())
188 .map_err(|err| err.to_string())?;
189 return Ok(());

Callers 1

mainFunction · 0.85

Calls 4

write_json_lineFunction · 0.85
extract_typeFunction · 0.85
flushMethod · 0.45

Tested by

no test coverage detected