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
@@ -10,6 +10,7 @@ import type {
|
||||
ToolProgressEvent,
|
||||
UIFileEdit,
|
||||
UIMessage,
|
||||
UIModelRequestSnapshot,
|
||||
UITurnPhase,
|
||||
} from "@/lib/types";
|
||||
|
||||
@@ -21,6 +22,7 @@ const FILE_EDIT_TOOL_NAMES = new Set(["write_file", "edit_file", "apply_patch"])
|
||||
|
||||
export interface ThreadProjectionState {
|
||||
messages: UIMessage[];
|
||||
modelRequests: ReadonlyMap<string, UIModelRequestSnapshot>;
|
||||
activeAssistantId: string | null;
|
||||
closedAssistantIds: Set<string>;
|
||||
/** Completed wire identities also fence late deltas for textless ends. */
|
||||
@@ -399,6 +401,7 @@ export function createThreadProjectionState(
|
||||
): ThreadProjectionState {
|
||||
return {
|
||||
messages,
|
||||
modelRequests: new Map(),
|
||||
activeAssistantId: null,
|
||||
closedAssistantIds: new Set(),
|
||||
closedAnswerStreamIds: new Set(),
|
||||
@@ -418,6 +421,7 @@ export function resetThreadProjectionCursor(
|
||||
): ThreadProjectionState {
|
||||
return {
|
||||
...createThreadProjectionState(messages),
|
||||
modelRequests: state.modelRequests,
|
||||
activitySegmentCounter: state.activitySegmentCounter,
|
||||
closedTurnIds: new Set(state.closedTurnIds),
|
||||
turnAliases: new Map(state.turnAliases),
|
||||
@@ -727,9 +731,9 @@ function projectToolActivity(
|
||||
: event.text
|
||||
? [event.text]
|
||||
: [];
|
||||
if (lines.length === 0) return;
|
||||
|
||||
const deferredDetail = event.trace_detail;
|
||||
if (lines.length === 0 && !deferredDetail) return;
|
||||
|
||||
const segmentId = ensureProjectionActivitySegment(state);
|
||||
const turn = turnFieldsForProjection(state, event, "activity");
|
||||
const last = state.messages[state.messages.length - 1];
|
||||
@@ -780,7 +784,7 @@ function projectToolActivity(
|
||||
id,
|
||||
role: "tool",
|
||||
kind: "trace",
|
||||
content: lines[lines.length - 1],
|
||||
content: lines[lines.length - 1] ?? "",
|
||||
traces: lines,
|
||||
...(visibleEvents.length ? { toolEvents: visibleEvents } : {}),
|
||||
...(deferredDetail ? { traceDetail: deferredDetail } : {}),
|
||||
@@ -857,6 +861,35 @@ export function projectThreadEvent(
|
||||
turnAliases: new Map(previous.turnAliases),
|
||||
};
|
||||
|
||||
if (event.event === "model_request") {
|
||||
if (!event.turn_id || state.closedTurnIds.has(event.turn_id)) return state;
|
||||
const previousRequest = state.modelRequests.get(event.turn_id);
|
||||
if (previousRequest?.ended
|
||||
|| previousRequest?.retiredRunIds.includes(event.run_id)
|
||||
|| (previousRequest?.runId === event.run_id && event.sequence <= previousRequest.sequence)
|
||||
|| (previousRequest?.turnSeq !== undefined && event.turn_seq !== undefined
|
||||
&& event.turn_seq <= previousRequest.turnSeq)) return state;
|
||||
const modelRequest: UIModelRequestSnapshot = {
|
||||
...previousRequest,
|
||||
createdAt: previousRequest?.createdAt ?? projectionCreatedAt(event, options),
|
||||
runId: event.run_id,
|
||||
sequence: event.sequence,
|
||||
turnSeq: event.turn_seq,
|
||||
...((event.phase === "completed" || event.phase === "failed")
|
||||
&& (event.usage?.request_count ?? 0) > (
|
||||
previousRequest?.runId === event.run_id ? previousRequest.usage?.request_count ?? 0 : 0
|
||||
) ? { usageTurnSeq: event.turn_seq } : {}),
|
||||
retiredRunIds: previousRequest && previousRequest.runId !== event.run_id
|
||||
? [...previousRequest.retiredRunIds, previousRequest.runId]
|
||||
: previousRequest?.retiredRunIds ?? [],
|
||||
...(event.usage !== undefined ? { usage: event.usage } : {}),
|
||||
...(event.round_usages !== undefined ? { roundUsages: event.round_usages.slice(-8) } : {}),
|
||||
...(event.context_window_tokens !== undefined ? { contextWindowTokens: event.context_window_tokens } : {}),
|
||||
};
|
||||
state.modelRequests = new Map(state.modelRequests).set(event.turn_id, modelRequest);
|
||||
return state;
|
||||
}
|
||||
|
||||
if (event.event === "user_message") {
|
||||
if (event.provenance?.session_message) {
|
||||
const messageId = event.provenance.session_message.message_id?.trim();
|
||||
@@ -1002,6 +1035,7 @@ export function projectThreadEvent(
|
||||
compaction: {
|
||||
id: event.compaction_id,
|
||||
phase: event.phase,
|
||||
turnSeq: event.turn_seq,
|
||||
reason: event.reason,
|
||||
...(!options.persisted ? { announce: true } : {}),
|
||||
},
|
||||
@@ -1089,6 +1123,18 @@ export function projectThreadEvent(
|
||||
state.activitySegmentId = null;
|
||||
state.fileEditSegmentId = null;
|
||||
state.messages = finalizeStreamedTurn(state.messages);
|
||||
const modelRequests = new Map(state.modelRequests);
|
||||
for (const [turnId, snapshot] of modelRequests) {
|
||||
if (turn.turnId && turnId !== turn.turnId) continue;
|
||||
modelRequests.set(turnId, { ...snapshot, ended: true,
|
||||
turnSeq: event.turn_seq ?? snapshot.turnSeq,
|
||||
...(!options.persisted ? { completedAt: options.now } : {}),
|
||||
...(event.usage !== undefined ? { usage: event.usage } : {}),
|
||||
...(event.round_usages !== undefined ? { roundUsages: event.round_usages.slice(-8) } : {}),
|
||||
...(event.context_window_tokens !== undefined ? { contextWindowTokens: event.context_window_tokens } : {}),
|
||||
});
|
||||
}
|
||||
state.modelRequests = modelRequests;
|
||||
const latencyMs = typeof event.latency_ms === "number" && event.latency_ms >= 0
|
||||
? Math.round(event.latency_ms)
|
||||
: undefined;
|
||||
|
||||
Reference in new issue
Block a user