(
scope: TurnScope,
eventSink: DurableSessionEventSink,
code: string,
context: MakaToolContext,
)
| 3065 | } |
| 3066 | |
| 3067 | private async executeCodeModeCell( |
| 3068 | scope: TurnScope, |
| 3069 | eventSink: DurableSessionEventSink, |
| 3070 | code: string, |
| 3071 | context: MakaToolContext, |
| 3072 | ): Promise<unknown> { |
| 3073 | const snapshot = new Map(scope.codeModeTools); |
| 3074 | let nestedOutputBytes = 0; |
| 3075 | let nestedOutputLimitExceeded = false; |
| 3076 | const nestedEventSink: DurableSessionEventSink = { |
| 3077 | push: (event) => { |
| 3078 | if (event.type === 'tool_output_delta') { |
| 3079 | const nextBytes = new TextEncoder().encode(event.chunk).byteLength; |
| 3080 | if ( |
| 3081 | nestedOutputLimitExceeded || |
| 3082 | nestedOutputBytes + nextBytes > DEFAULT_CODE_MODE_LIMITS.maxToolOutputBytes |
| 3083 | ) { |
| 3084 | nestedOutputLimitExceeded = true; |
| 3085 | return; |
| 3086 | } |
| 3087 | nestedOutputBytes += nextBytes; |
| 3088 | } |
| 3089 | eventSink.push(event); |
| 3090 | }, |
| 3091 | pushAndWaitUntilConsumed: (event) => eventSink.pushAndWaitUntilConsumed(event), |
| 3092 | }; |
| 3093 | return executeCodeCell({ |
| 3094 | code, |
| 3095 | signal: context.abortSignal, |
| 3096 | tools: [...snapshot.values()].map((tool) => ({ |
| 3097 | name: tool.name, |
| 3098 | })), |
| 3099 | isFatalToolError: isRuntimeCommitBoundaryError, |
| 3100 | callTool: async (name, input, signal) => { |
| 3101 | const tool = snapshot.get(name); |
| 3102 | if (!tool) throw new Error(`Tool "${name}" is not active or nestable in this cell`); |
| 3103 | const parsedInput = await validateCodeModeToolInput(tool, input); |
| 3104 | const settlement = await scope.toolRuntime.settleToolCallRaw({ |
| 3105 | tool, |
| 3106 | turnId: context.turnId, |
| 3107 | toolCallId: `${context.toolCallId}:nested:${this.newId()}`, |
| 3108 | input: parsedInput, |
| 3109 | abortSignal: signal, |
| 3110 | eventSink: nestedEventSink, |
| 3111 | origin: 'code_mode', |
| 3112 | parentToolCallId: context.toolCallId, |
| 3113 | ...(context.operationId ? { parentOperationId: context.operationId } : {}), |
| 3114 | maxResultBytes: DEFAULT_CODE_MODE_LIMITS.maxToolOutputBytes, |
| 3115 | }); |
| 3116 | if (settlement.providerError !== undefined) { |
| 3117 | throw new Error(settlement.providerError); |
| 3118 | } |
| 3119 | if (nestedOutputLimitExceeded) { |
| 3120 | throw new Error('Code Mode nested output byte limit exceeded'); |
| 3121 | } |
| 3122 | return settlement.result; |
| 3123 | }, |
| 3124 | }); |
no test coverage detected