| 55 | |
| 56 | |
| 57 | async def _run_tool(exec_id, tool_name, args): |
| 58 | global tool_counter |
| 59 | |
| 60 | if not isinstance(tool_name, str) or not tool_name: |
| 61 | raise RuntimeError("codex.tool expects a tool name string") |
| 62 | |
| 63 | tool_id = f"{exec_id}-tool-{tool_counter}" |
| 64 | tool_counter += 1 |
| 65 | |
| 66 | arguments_json = "{}" |
| 67 | if isinstance(args, str): |
| 68 | arguments_json = args |
| 69 | elif args is not None: |
| 70 | arguments_json = json.dumps(args) |
| 71 | |
| 72 | loop = asyncio.get_running_loop() |
| 73 | future = loop.create_future() |
| 74 | pending_tool[tool_id] = future |
| 75 | |
| 76 | _send( |
| 77 | { |
| 78 | "type": "run_tool", |
| 79 | "id": tool_id, |
| 80 | "exec_id": exec_id, |
| 81 | "tool_name": tool_name, |
| 82 | "arguments": arguments_json, |
| 83 | } |
| 84 | ) |
| 85 | |
| 86 | result = await future |
| 87 | if not result.get("ok"): |
| 88 | raise RuntimeError(result.get("error") or "tool failed") |
| 89 | return result.get("response") |
| 90 | |
| 91 | |
| 92 | async def _handle_exec(message): |