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