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

Function _handle_exec

src/tools/REPLTool/kernel.py:83–141  ·  view source on GitHub ↗
(message)

Source from the content-addressed store, hash-verified

81
82
83async def _handle_exec(message):
84 global cell_counter
85
86 exec_id = message.get("id")
87 code = message.get("code")
88 if not isinstance(exec_id, str) or not isinstance(code, str):
89 return
90
91 async def _tool(name, args=None):
92 return await _run_tool(exec_id, name, args)
93
94 state_globals["codex"] = SimpleNamespace(tmpDir=TMP_DIR, tool=_tool)
95 state_globals["tmpDir"] = TMP_DIR
96
97 stdout_buf = io.StringIO()
98 stderr_buf = io.StringIO()
99 original_stdout = sys.stdout
100 original_stderr = sys.stderr
101 sys.stdout = stdout_buf
102 sys.stderr = stderr_buf
103
104 try:
105 filename = f"<cell-{cell_counter}>"
106 cell_counter += 1
107 flags = ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
108 code_obj = compile(code, filename, "exec", flags=flags, dont_inherit=True)
109 result = eval(code_obj, state_globals, state_globals)
110 if inspect.isawaitable(result):
111 await result
112
113 output = stdout_buf.getvalue().rstrip()
114 stderr_output = stderr_buf.getvalue().rstrip()
115 if output and stderr_output:
116 output = f"{output}\n{stderr_output}"
117 elif not output:
118 output = stderr_output
119
120 _send(
121 {
122 "type": "exec_result",
123 "id": exec_id,
124 "ok": True,
125 "output": output,
126 "error": None,
127 }
128 )
129 except BaseException as error:
130 _send(
131 {
132 "type": "exec_result",
133 "id": exec_id,
134 "ok": False,
135 "output": "",
136 "error": _format_error(error),
137 }
138 )
139 finally:
140 sys.stdout = original_stdout

Callers 1

_mainFunction · 0.70

Calls 3

_sendFunction · 0.70
_format_errorFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected