


















@@ -176,6 +176,22 @@ function normalizeToolResultName(
176176return message;
177177}
178178179+function normalizeLegacyToolResultId(
180+message: Extract<AgentMessage, { role: "toolResult" }>,
181+toolCalls: Array<{ id: string; name?: string }>,
182+): Extract<AgentMessage, { role: "toolResult" }> {
183+if (extractToolResultId(message) || toolCalls.length !== 1) {
184+return message;
185+}
186+const [toolCall] = toolCalls;
187+const toolResultName = normalizeOptionalString((message as { toolName?: unknown }).toolName);
188+const toolCallName = normalizeOptionalString(toolCall.name);
189+if (toolResultName && toolCallName && toolResultName !== toolCallName) {
190+return message;
191+}
192+return { ...message, toolCallId: toolCall.id };
193+}
194+179195export { makeMissingToolResult };
180196181197type ToolCallInputRepairReport = {
@@ -221,6 +237,11 @@ function collectFollowingToolResults(
221237index: number,
222238): { ids: Set<string>; displaced: boolean } {
223239const ids = new Set<string>();
240+const assistant = messages[index];
241+const currentToolCalls =
242+assistant && typeof assistant === "object" && assistant.role === "assistant"
243+ ? extractToolCallsFromAssistant(assistant)
244+ : [];
224245let sawNonToolResult = false;
225246let displaced = false;
226247for (let nextIndex = index + 1; nextIndex < messages.length; nextIndex += 1) {
@@ -233,7 +254,8 @@ function collectFollowingToolResults(
233254break;
234255}
235256if (message.role === "toolResult") {
236-const resultIds = extractToolResultIds(message);
257+const normalizedLegacyResult = normalizeLegacyToolResultId(message, currentToolCalls);
258+const resultIds = extractToolResultIds(normalizedLegacyResult);
237259for (const id of resultIds) {
238260ids.add(id);
239261}
@@ -495,14 +517,20 @@ export function repairToolUseResultPairing(
495517}
496518497519if (nextRole === "toolResult") {
498-const toolResult = next as Extract<AgentMessage, { role: "toolResult" }>;
520+const toolResult = normalizeLegacyToolResultId(
521+next as Extract<AgentMessage, { role: "toolResult" }>,
522+toolCalls,
523+);
499524const id = extractToolResultId(toolResult);
500525if (id && toolCallIds.has(id)) {
501526if (seenToolResultIds.has(id)) {
502527droppedDuplicateCount += 1;
503528changed = true;
504529continue;
505530}
531+if (toolResult !== next) {
532+changed = true;
533+}
506534const normalizedToolResult = normalizeToolResultName(
507535toolResult,
508536toolCallNamesById.get(id),
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。