11/** 工具阶段:执行模型要求的工具调用(含图、工作流、委托与内置记忆工具),结果写回对话。 */
22import { runSandboxed } from '../core/contracts.js' ;
3- import type {
4- AgentBundle ,
5- ModelMessage ,
6- Run ,
7- ToolBinding ,
8- ToolCall ,
9- ToolResult ,
10- } from '../core/contracts.js' ;
3+ import type { AgentBundle , Run , ToolBinding , ToolCall , ToolResult } from '../core/contracts.js' ;
114import {
125 executeGraph ,
136 graphToolEffects ,
@@ -33,7 +26,7 @@ import type { KernelRuntime } from './runtime.js';
3326import type { ExecutionSlice } from './types.js' ;
3427import { awaitApproval , autoApprove } from './approvals.js' ;
3528import * as clientTools from './client-tools.js' ;
36- import { contextLimit , modelTurnLimit , toolReplyContext } from './model-context .js' ;
29+ import { prepareToolReplies } from './tool-message .js' ;
3730
3831/**
3932 * 执行模型这一轮要求的全部工具调用,把结果按调用顺序写回对话,然后回到模型阶段。
@@ -104,22 +97,8 @@ export async function runToolPhase(
10497 checkAbort ( signal ) ;
10598 if ( yielding ) throw new SchedulingSuspension ( approvals ) ;
10699 if ( approvals . length ) throw new ApprovalSuspension ( approvals ) ;
107- // A later encoding failure must not leave only the first tool replies in the checkpoint.
108- const messages : ModelMessage [ ] = [ ] ;
109- let reserve : ReturnType < typeof toolReplyContext > | undefined ;
110- for ( const outcome of settled ) {
111- if ( outcome . status !== 'fulfilled' ) continue ;
112- const { call, value } = outcome . value ;
113- const content = clientTools . toolMessageContent ( value ) ;
114- reserve ??= toolReplyContext (
115- run . checkpoint ,
116- bundle . agent . instructions ,
117- ( ) => modelTurnLimit ( run , bundle . agent . limits ) ?? contextLimit ( ) ,
118- ) ;
119- reserve ( call . id , content ) ;
120- messages . push ( { role : 'tool' , tool_call_id : call . id , content } ) ;
121- }
122- for ( const message of messages ) run . checkpoint . messages . push ( message ) ;
100+ for ( const message of prepareToolReplies ( run , bundle . agent , settled ) )
101+ run . checkpoint . messages . push ( message ) ;
123102 run . checkpoint . pending = [ ] ;
124103 run . checkpoint . phase = 'model' ;
125104 rt . store . save ( run ) ;
0 commit comments