fix(webui): update usage and tool progress during active turns (#6553)
* fix(webui): stream model status and usage during active turns * fix(webui): keep live rounds and active-run status consistent * fix(webui): reuse existing run indicators without extra status text * fix(webui): preserve live activity and batch durable delivery Cancel and drain sibling tools when progress notifications fail, retain balanced checkpoints, and keep model telemetry separate from answer provenance. Group-commit already queued activity at the channel delivery boundary while preserving FIFO, generation ownership, per-envelope admission, and commit-before-fanout semantics. Isolate invalid members without dropping valid neighboring events. Retain tool detail lazy grouping and cover scheduling, cancellation, persistence, encoding failures, and projection regressions. * test(webui): separate standard-library imports in batch regression * test: tighten live execution checks and clarify contracts * refactor: remove redundant live execution comments * refactor: settle tool results through one output path
This commit is contained in:
1 parent
006a535537
commit
8ff04aad2b
52 files changed
+2065
-211
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import type { UIMessage, UIModelRequestSnapshot } from "@/lib/types";
|
||||
|
||||
export type ComposerUsageSnapshot = Pick<UIMessage,
|
||||
"id" | "turnId" | "createdAt" | "completedAt" | "usage" | "roundUsages" | "contextWindowTokens"
|
||||
> & { compacted?: boolean };
|
||||
|
||||
export function reconcileModelRequest(
|
||||
current: UIModelRequestSnapshot | undefined,
|
||||
incoming: UIModelRequestSnapshot,
|
||||
): UIModelRequestSnapshot {
|
||||
if (!current) return incoming;
|
||||
if ((current.ended && !incoming.ended) || current.retiredRunIds.includes(incoming.runId)) return current;
|
||||
if (current.turnSeq !== undefined && incoming.turnSeq !== undefined) {
|
||||
if (incoming.turnSeq < current.turnSeq
|
||||
|| (incoming.turnSeq === current.turnSeq && !(incoming.ended && !current.ended))) return current;
|
||||
} else if (current.runId === incoming.runId) {
|
||||
if (incoming.sequence < current.sequence
|
||||
|| (incoming.sequence === current.sequence && !(incoming.ended && !current.ended))) return current;
|
||||
} else if (!incoming.retiredRunIds.includes(current.runId)) return current;
|
||||
return { ...incoming, retiredRunIds: Array.from(new Set([
|
||||
...current.retiredRunIds, ...incoming.retiredRunIds,
|
||||
...(current.runId !== incoming.runId ? [current.runId] : []),
|
||||
])) };
|
||||
}
|
||||
|
||||
/** Select one authoritative usage snapshot per turn, independent of text rows. */
|
||||
export function composerUsageSnapshots(
|
||||
messages: UIMessage[],
|
||||
snapshots: ReadonlyMap<string, UIModelRequestSnapshot>,
|
||||
): ComposerUsageSnapshot[] {
|
||||
const lastRows = new Map<string, number>();
|
||||
const compactions = new Map<string, number | undefined>();
|
||||
messages.forEach((row, index) => {
|
||||
if (!row.turnId) return;
|
||||
lastRows.set(row.turnId, index);
|
||||
if (row.kind === "compaction" && row.compaction?.phase === "succeeded") {
|
||||
compactions.set(row.turnId, row.compaction.turnSeq);
|
||||
}
|
||||
});
|
||||
const rows = messages.flatMap((row, index): ComposerUsageSnapshot[] => {
|
||||
const snapshot = row.turnId ? snapshots.get(row.turnId) : undefined;
|
||||
if (snapshot) {
|
||||
if (lastRows.get(row.turnId!) !== index) return [];
|
||||
const compactionSeq = compactions.get(row.turnId!);
|
||||
const compacted = compactions.has(row.turnId!) && !(snapshot.usageTurnSeq !== undefined
|
||||
&& compactionSeq !== undefined && snapshot.usageTurnSeq > compactionSeq);
|
||||
return [{ id: row.id, turnId: row.turnId, createdAt: row.createdAt, compacted,
|
||||
completedAt: snapshot.completedAt ?? row.completedAt, usage: snapshot.usage,
|
||||
roundUsages: snapshot.roundUsages, contextWindowTokens: snapshot.contextWindowTokens }];
|
||||
}
|
||||
if (row.kind === "compaction" && row.compaction?.phase === "succeeded") {
|
||||
return [{ id: row.id, createdAt: row.createdAt, compacted: true }];
|
||||
}
|
||||
if (row.role !== "assistant" || row.kind === "trace" || row.isStreaming) return [];
|
||||
return [row];
|
||||
});
|
||||
// Proactive requests can precede any body. They need usage, not placeholder rows.
|
||||
const unanchored = Array.from(snapshots, ([turnId, snapshot]) => ({
|
||||
id: `model-request:${turnId}`, turnId, ...snapshot,
|
||||
})).filter((snapshot) => !lastRows.has(snapshot.turnId))
|
||||
.sort((left, right) => left.createdAt - right.createdAt);
|
||||
if (!unanchored.length) return rows;
|
||||
const result: ComposerUsageSnapshot[] = [];
|
||||
let index = 0;
|
||||
for (const row of rows) {
|
||||
while (index < unanchored.length && unanchored[index].createdAt <= row.createdAt) {
|
||||
result.push(unanchored[index++]);
|
||||
}
|
||||
result.push(row);
|
||||
}
|
||||
return [...result, ...unanchored.slice(index)];
|
||||
}
|
||||
Reference in new issue
Block a user