Skip to content

Commit b3bd973

Browse files
author
hzq
committed
Keep reply preparation with the tool message encoder
1 parent af1b9d4 commit b3bd973

2 files changed

Lines changed: 29 additions & 25 deletions

File tree

‎src/kernel/tool-message.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { chargeQuoted, JsonByteBudget, stringifyBounded } from '../core/bounded-json.js';
2+
import type { AgentDefinition, ModelMessage, Run, ToolCall } from '../core/contracts.js';
23
import { TOOL_RESULT_JSON_BYTES as limit } from '../core/limits.js';
34
import { boundedJson, payloadTooLarge } from '../core/util.js';
5+
import { contextLimit, modelTurnLimit, toolReplyContext } from './model-context.js';
46

57
export class CallerOutput {
68
constructor(readonly output: unknown) {}
@@ -51,3 +53,26 @@ export function toolMessageContent(value: unknown): string {
5153
checkToolString(output);
5254
return output;
5355
}
56+
57+
/** Prepare the whole batch before changing the checkpoint, keeping durable step results independent. */
58+
export function prepareToolReplies(
59+
run: Run,
60+
agent: AgentDefinition,
61+
outcomes: ReadonlyArray<PromiseSettledResult<{ call: ToolCall; value: unknown }>>,
62+
): ModelMessage[] {
63+
const messages: ModelMessage[] = [];
64+
let reserve: ReturnType<typeof toolReplyContext> | undefined;
65+
for (const outcome of outcomes) {
66+
if (outcome.status !== 'fulfilled') continue;
67+
const { call, value } = outcome.value;
68+
const content = toolMessageContent(value);
69+
reserve ??= toolReplyContext(
70+
run.checkpoint,
71+
agent.instructions,
72+
() => modelTurnLimit(run, agent.limits) ?? contextLimit(),
73+
);
74+
reserve(call.id, content);
75+
messages.push({ role: 'tool', tool_call_id: call.id, content });
76+
}
77+
return messages;
78+
}

‎src/kernel/tool-phase.ts‎

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
/** 工具阶段:执行模型要求的工具调用(含图、工作流、委托与内置记忆工具),结果写回对话。 */
22
import { 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';
114
import {
125
executeGraph,
136
graphToolEffects,
@@ -33,7 +26,7 @@ import type { KernelRuntime } from './runtime.js';
3326
import type { ExecutionSlice } from './types.js';
3427
import { awaitApproval, autoApprove } from './approvals.js';
3528
import * 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

Comments
 (0)