( input: ExecuteCodeCellInput, )
| 13 | import { DEFAULT_CODE_MODE_LIMITS } from './index.js'; |
| 14 | |
| 15 | export async function executeCodeCellImpl( |
| 16 | input: ExecuteCodeCellInput, |
| 17 | ): Promise<CodeModeExecutionResult> { |
| 18 | const limits = { |
| 19 | maxSourceBytes: input.limits?.maxSourceBytes ?? DEFAULT_CODE_MODE_LIMITS.maxSourceBytes, |
| 20 | maxSandboxTimeMs: input.limits?.maxSandboxTimeMs ?? DEFAULT_CODE_MODE_LIMITS.maxSandboxTimeMs, |
| 21 | maxMemoryBytes: input.limits?.maxMemoryBytes ?? DEFAULT_CODE_MODE_LIMITS.maxMemoryBytes, |
| 22 | maxStackBytes: input.limits?.maxStackBytes ?? DEFAULT_CODE_MODE_LIMITS.maxStackBytes, |
| 23 | maxToolCalls: input.limits?.maxToolCalls ?? DEFAULT_CODE_MODE_LIMITS.maxToolCalls, |
| 24 | maxToolConcurrency: |
| 25 | input.limits?.maxToolConcurrency ?? DEFAULT_CODE_MODE_LIMITS.maxToolConcurrency, |
| 26 | maxToolInputBytes: |
| 27 | input.limits?.maxToolInputBytes ?? DEFAULT_CODE_MODE_LIMITS.maxToolInputBytes, |
| 28 | maxToolOutputBytes: |
| 29 | input.limits?.maxToolOutputBytes ?? DEFAULT_CODE_MODE_LIMITS.maxToolOutputBytes, |
| 30 | maxOutputBytes: input.limits?.maxOutputBytes ?? DEFAULT_CODE_MODE_LIMITS.maxOutputBytes, |
| 31 | }; |
| 32 | const toolCalls: CodeModeToolCall[] = []; |
| 33 | const hostToolOperations = new Set<Promise<unknown>>(); |
| 34 | const fatalAbortController = new AbortController(); |
| 35 | const invocationSignal = input.signal |
| 36 | ? AbortSignal.any([input.signal, fatalAbortController.signal]) |
| 37 | : fatalAbortController.signal; |
| 38 | let fatalToolFailure: { reason: unknown } | undefined; |
| 39 | const tools = Object.create(null) as ToolSet; |
| 40 | for (const { name } of input.tools) { |
| 41 | tools[name] = tool({ |
| 42 | inputSchema: jsonSchema({}), |
| 43 | execute: async (toolInput, options) => { |
| 44 | if (fatalToolFailure) throw fatalToolFailure.reason; |
| 45 | toolCalls.push({ index: toolCalls.length + 1, name }); |
| 46 | const operation = Promise.resolve().then(() => |
| 47 | input.callTool(name, toolInput, options.abortSignal ?? invocationSignal), |
| 48 | ); |
| 49 | hostToolOperations.add(operation); |
| 50 | return operation.then( |
| 51 | (value) => { |
| 52 | hostToolOperations.delete(operation); |
| 53 | return value; |
| 54 | }, |
| 55 | (error) => { |
| 56 | hostToolOperations.delete(operation); |
| 57 | if (input.isFatalToolError?.(error)) { |
| 58 | if (!fatalToolFailure) { |
| 59 | fatalToolFailure = { reason: error }; |
| 60 | fatalAbortController.abort(error); |
| 61 | } |
| 62 | throw error; |
| 63 | } |
| 64 | throw new CodeModeToolError(error instanceof Error ? error.message : String(error), { |
| 65 | toolName: name, |
| 66 | }); |
| 67 | }, |
| 68 | ); |
| 69 | }, |
| 70 | }); |
| 71 | } |
| 72 |
no test coverage detected