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