

























@@ -554,27 +554,31 @@ function assistantHasToolCalls(message: AgentMessage): boolean {
554554return extractToolCallsFromAssistant(message).length > 0;
555555}
556556557-function findLaterMatchingToolResult(params: {
557+function collectLaterMatchingToolResults(params: {
558558messages: AgentMessage[];
559559startIndex: number;
560-toolCallId: string;
561-toolName?: string;
562560toolCalls: Array<{ id: string; name?: string }>;
561+toolNamesById: Map<string, string>;
563562seenToolResultIds: Set<string>;
564-}): Extract<AgentMessage, { role: "toolResult" }> | undefined {
563+}): Map<string, Extract<AgentMessage, { role: "toolResult" }>> {
564+const resultsById = new Map<string, Extract<AgentMessage, { role: "toolResult" }>>();
565+const toolCallIds = new Set(params.toolCalls.map((toolCall) => toolCall.id));
565566for (let index = params.startIndex; index < params.messages.length; index += 1) {
566567const candidate = params.messages[index];
567568if (!candidate || typeof candidate !== "object" || candidate.role !== "toolResult") {
568569continue;
569570}
570571const normalizedLegacyResult = normalizeLegacyToolResultId(candidate, params.toolCalls);
571572const id = extractToolResultId(normalizedLegacyResult);
572-if (!id || id !== params.toolCallId || params.seenToolResultIds.has(id)) {
573+if (!id || !toolCallIds.has(id) || params.seenToolResultIds.has(id) || resultsById.has(id)) {
573574continue;
574575}
575-return normalizeToolResultName(normalizedLegacyResult, params.toolName);
576+resultsById.set(
577+id,
578+normalizeToolResultName(normalizedLegacyResult, params.toolNamesById.get(id)),
579+);
576580}
577-return undefined;
581+return resultsById;
578582}
579583580584export function repairToolUseResultPairing(
@@ -769,20 +773,21 @@ export function repairToolUseResultPairing(
769773changed = true;
770774}
771775776+const laterResultsById = collectLaterMatchingToolResults({
777+ messages,
778+startIndex: j,
779+ toolCalls,
780+toolNamesById: toolCallNamesById,
781+ seenToolResultIds,
782+});
772783for (const call of toolCalls) {
773784const existing = spanResultsById.get(call.id);
774785if (existing) {
775786pushToolResult(existing);
776787} else {
777-const laterResult = findLaterMatchingToolResult({
778- messages,
779-startIndex: j,
780-toolCallId: call.id,
781-toolName: call.name,
782- toolCalls,
783- seenToolResultIds,
784-});
788+const laterResult = laterResultsById.get(call.id);
785789if (laterResult) {
790+laterResultsById.delete(call.id);
786791moved = true;
787792changed = true;
788793pushToolResult(laterResult);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。