| 250 | } |
| 251 | |
| 252 | function ensureRuntimeHelpers( |
| 253 | replContext: JavascriptReplStoreContext, |
| 254 | ): void { |
| 255 | const globals = replContext.vmContext as Record<string, unknown> |
| 256 | |
| 257 | const callTool = async (name: string, args?: Record<string, unknown>) => { |
| 258 | if (typeof name !== 'string' || name.trim() === '') { |
| 259 | throw new Error('callTool(name, args) requires a non-empty tool name') |
| 260 | } |
| 261 | const tool = replContext.registeredTools.get(name) |
| 262 | if (!tool) { |
| 263 | throw new Error(`Unknown JavaScript REPL tool: ${name}`) |
| 264 | } |
| 265 | return tool.handler(ensureRecord(args)) |
| 266 | } |
| 267 | |
| 268 | const listTools = () => |
| 269 | [...replContext.registeredTools.values()].map(tool => ({ |
| 270 | name: tool.name, |
| 271 | description: tool.description, |
| 272 | schema: tool.schema, |
| 273 | })) |
| 274 | |
| 275 | globals.console = replContext.console |
| 276 | globals.callTool = callTool |
| 277 | globals.listTools = listTools |
| 278 | globals.codex = { |
| 279 | cwd: process.cwd(), |
| 280 | homeDir: homedir(), |
| 281 | tmpDir: tmpdir(), |
| 282 | tool: callTool, |
| 283 | listTools, |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | function augmentToolsForJavascriptRepl(toolUseContext: ToolUseContext): Tool[] { |
| 288 | const byName = new Map<string, Tool>() |