fix webui tool trace dedupe

This commit is contained in:
Xubin Ren
2026-05-19 13:12:19 +08:00
parent c4794b82a9
commit 0a5606b409
5 changed files with 165 additions and 25 deletions
+31 -16
View File
@@ -44,20 +44,35 @@ const VALID_PHASES = new Set(["start", "end", "error"]);
export function toolTraceLinesFromEvents(events: unknown): string[] {
if (!Array.isArray(events)) return [];
const seen = new Set<string>();
return events
.filter((event) => {
if (!event || typeof event !== "object") return false;
const phase = (event as { phase?: unknown }).phase;
if (!(phase && typeof phase === "string" && VALID_PHASES.has(phase))) {
return false;
}
const callId = (event as { call_id?: unknown }).call_id;
if (callId && typeof callId === "string") {
if (seen.has(callId)) return false;
seen.add(callId);
}
return true;
})
.map(formatToolCallTrace)
.filter((trace): trace is string => !!trace);
const lines: string[] = [];
for (const event of events) {
if (!event || typeof event !== "object") continue;
const phase = (event as { phase?: unknown }).phase;
if (!(phase && typeof phase === "string" && VALID_PHASES.has(phase))) continue;
const callId = (event as { call_id?: unknown }).call_id;
if (callId && typeof callId === "string") {
if (seen.has(callId)) continue;
seen.add(callId);
}
const line = formatToolCallTrace(event);
if (!line) continue;
lines.push(line);
}
return lines;
}
export function mergeUniqueToolTraceLines(
previousTraces: string[],
lines: string[],
): { traces: string[]; added: boolean } {
const seen = new Set(previousTraces);
const traces = [...previousTraces];
let added = false;
for (const line of lines) {
if (seen.has(line)) continue;
seen.add(line);
traces.push(line);
added = true;
}
return { traces, added };
}