fix(webui): preserve oversized turn history with record pagination (#6586)
This commit is contained in:
1 parent
5c1083df7f
commit
3c61bcfdf0
34 files changed
+3466
-380
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { toMediaAttachment } from "@/lib/media";
|
||||
import { ThreadProjectionIndex } from "./thread-projection-index";
|
||||
import {
|
||||
mergeToolProgressEvents,
|
||||
mergeToolProgressTraceLines,
|
||||
@@ -51,6 +52,9 @@ export interface ThreadProjectionOptions {
|
||||
/** First sequence of a coalesced batch; event.turn_seq is its last contribution. */
|
||||
startSeq?: number;
|
||||
effects?: ThreadProjectionEffects;
|
||||
index?: ThreadProjectionIndex;
|
||||
/** Scratch replay owns these collections and does not expose intermediate states. */
|
||||
mutableCollections?: boolean;
|
||||
}
|
||||
|
||||
export function turnFieldsFromEvent(
|
||||
@@ -90,7 +94,18 @@ function findStreamingAssistantIndex(
|
||||
closedStreamIds: ReadonlySet<string>,
|
||||
turn: UIMessageTurnFields = {},
|
||||
streamId?: string,
|
||||
lookup?: ThreadProjectionIndex,
|
||||
): number | null {
|
||||
if (streamId && lookup) {
|
||||
let target: number | null = null;
|
||||
for (const index of lookup.answer("")) {
|
||||
const row = prev[index];
|
||||
if (index > lookup.lastUser && row.kind !== "trace" && row.isStreaming
|
||||
&& !closedStreamIds.has(row.id) && matchesTurn(row, turn)
|
||||
&& (target === null || index > target)) target = index;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
for (let i = prev.length - 1; i >= 0; i -= 1) {
|
||||
const m = prev[i];
|
||||
if (m.kind === "trace") continue;
|
||||
@@ -440,9 +455,10 @@ export function clearThreadProjectionActivity(
|
||||
|
||||
export function closeThreadProjectionAnswer(
|
||||
state: ThreadProjectionState,
|
||||
mutableCollections = false,
|
||||
): ThreadProjectionState {
|
||||
if (!state.activeAssistantId) return state;
|
||||
const closedAssistantIds = new Set(state.closedAssistantIds);
|
||||
const closedAssistantIds = mutableCollections ? state.closedAssistantIds : new Set(state.closedAssistantIds);
|
||||
closedAssistantIds.add(state.activeAssistantId);
|
||||
return {
|
||||
...state,
|
||||
@@ -479,7 +495,7 @@ function turnFieldsForProjection(
|
||||
if (!alias) {
|
||||
const suffix = event.projection_id || event.created_at_ms || state.messages.length;
|
||||
alias = `${rawTurnId}:replay:${suffix}`;
|
||||
state.turnAliases.set(rawTurnId, alias);
|
||||
state.turnAliases = new Map(state.turnAliases).set(rawTurnId, alias);
|
||||
}
|
||||
return { ...fields, turnId: alias };
|
||||
}
|
||||
@@ -499,9 +515,11 @@ function detachedProjectionActivitySegment(state: ThreadProjectionState): string
|
||||
function activeAssistantIndex(
|
||||
state: ThreadProjectionState,
|
||||
turn: UIMessageTurnFields,
|
||||
lookup?: ThreadProjectionIndex,
|
||||
): number | null {
|
||||
if (!state.activeAssistantId) return null;
|
||||
const index = state.messages.findIndex((message) => message.id === state.activeAssistantId);
|
||||
const index = lookup ? lookup.position(state.activeAssistantId) ?? -1
|
||||
: state.messages.findIndex((message) => message.id === state.activeAssistantId);
|
||||
if (index < 0) {
|
||||
state.activeAssistantId = null;
|
||||
state.mergeReasoning = false;
|
||||
@@ -529,14 +547,21 @@ function answerStreamIndex(
|
||||
state: ThreadProjectionState,
|
||||
turn: UIMessageTurnFields,
|
||||
streamId?: string,
|
||||
lookup?: ThreadProjectionIndex,
|
||||
): number | null {
|
||||
if (streamId) {
|
||||
if (streamId && lookup) {
|
||||
let target: number | null = null;
|
||||
for (const index of lookup.answer(streamId)) {
|
||||
if (matchesTurn(state.messages[index], turn) && (target === null || index > target)) target = index;
|
||||
}
|
||||
if (target !== null) return target;
|
||||
} else if (streamId) {
|
||||
for (let index = state.messages.length - 1; index >= 0; index--) {
|
||||
const message = state.messages[index];
|
||||
if (message.role === "assistant" && message.streamId === streamId && matchesTurn(message, turn)) return index;
|
||||
}
|
||||
}
|
||||
const active = activeAssistantIndex(state, turn);
|
||||
const active = activeAssistantIndex(state, turn, lookup);
|
||||
if (active !== null && (!streamId || !state.messages[active].streamId || state.messages[active].streamId === streamId)) {
|
||||
return active;
|
||||
}
|
||||
@@ -544,7 +569,7 @@ function answerStreamIndex(
|
||||
if (placeholder !== null && (!streamId || !state.messages[placeholder].streamId || state.messages[placeholder].streamId === streamId)) {
|
||||
return placeholder;
|
||||
}
|
||||
return findStreamingAssistantIndex(state.messages, state.closedAssistantIds, turn, streamId);
|
||||
return findStreamingAssistantIndex(state.messages, state.closedAssistantIds, turn, streamId, lookup);
|
||||
}
|
||||
|
||||
function appendAnswerText(
|
||||
@@ -555,10 +580,10 @@ function appendAnswerText(
|
||||
const turn = turnFieldsForProjection(state, event, "answer");
|
||||
const streamKey = answerStreamKey(turn, event.stream_id);
|
||||
if (streamKey && state.closedAnswerStreamIds.has(streamKey)) return;
|
||||
let targetIndex = answerStreamIndex(state, turn, event.stream_id);
|
||||
let targetIndex = answerStreamIndex(state, turn, event.stream_id, options.index);
|
||||
if (targetIndex !== null && state.closedAssistantIds.has(state.messages[targetIndex].id)) targetIndex = null;
|
||||
if (targetIndex === null) {
|
||||
Object.assign(state, closeThreadProjectionAnswer(state));
|
||||
Object.assign(state, closeThreadProjectionAnswer(state, options.mutableCollections));
|
||||
const id = projectionMessageId(event, options);
|
||||
state.messages = [
|
||||
...state.messages,
|
||||
@@ -583,7 +608,10 @@ function appendAnswerText(
|
||||
...(event.response_sources !== undefined ? { responseSources: event.response_sources } : {}),
|
||||
...(event.response_input_ids !== undefined ? { responseInputIds: event.response_input_ids } : {}),
|
||||
};
|
||||
state.closedAssistantIds.delete(merged.id);
|
||||
if (state.closedAssistantIds.has(merged.id)) {
|
||||
state.closedAssistantIds = new Set(state.closedAssistantIds);
|
||||
state.closedAssistantIds.delete(merged.id);
|
||||
}
|
||||
state.activeAssistantId = merged.id;
|
||||
state.mergeReasoning = false;
|
||||
state.messages = replaceMessageAt(state.messages, targetIndex, merged);
|
||||
@@ -633,9 +661,9 @@ function attachProjectedReasoning(
|
||||
|
||||
if (state.fileEditSegmentId || activeAssistantBeforeContinuation) {
|
||||
if (activeAssistantBeforeContinuation && !state.activeAssistantId) {
|
||||
state.closedAssistantIds.add(activeAssistantBeforeContinuation);
|
||||
state.closedAssistantIds = new Set(state.closedAssistantIds).add(activeAssistantBeforeContinuation);
|
||||
}
|
||||
const closed = closeThreadProjectionAnswer(state);
|
||||
const closed = closeThreadProjectionAnswer(state, options.mutableCollections);
|
||||
Object.assign(state, clearThreadProjectionActivity(closed));
|
||||
}
|
||||
for (let index = state.messages.length - 1; index >= 0; index -= 1) {
|
||||
@@ -721,7 +749,7 @@ function projectToolActivity(
|
||||
event: Extract<ThreadProjectionEvent, { event: "message" }>,
|
||||
options: ThreadProjectionOptions,
|
||||
): void {
|
||||
Object.assign(state, closeThreadProjectionAnswer(state));
|
||||
Object.assign(state, closeThreadProjectionAnswer(state, options.mutableCollections));
|
||||
const structuredEvents = normalizeToolProgressEvents(event.tool_events);
|
||||
const visibleEvents = filterCoveredFileEditToolEvents(state.messages, structuredEvents);
|
||||
const structuredLines = toolTraceLinesFromEvents(visibleEvents);
|
||||
@@ -802,7 +830,7 @@ function projectFileEdits(
|
||||
event: Extract<ThreadProjectionEvent, { event: "file_edit" }>,
|
||||
options: ThreadProjectionOptions,
|
||||
): void {
|
||||
Object.assign(state, closeThreadProjectionAnswer(state));
|
||||
Object.assign(state, closeThreadProjectionAnswer(state, options.mutableCollections));
|
||||
const normalized = mergeFileEdits(undefined, event.edits);
|
||||
if (normalized.length === 0) return;
|
||||
const turn = turnFieldsForProjection(state, event, "activity");
|
||||
@@ -854,13 +882,20 @@ export function projectThreadEvent(
|
||||
event: ThreadProjectionEvent,
|
||||
options: ThreadProjectionOptions,
|
||||
): ThreadProjectionState {
|
||||
const state: ThreadProjectionState = {
|
||||
...previous,
|
||||
closedAssistantIds: new Set(previous.closedAssistantIds),
|
||||
closedAnswerStreamIds: new Set(previous.closedAnswerStreamIds),
|
||||
closedTurnIds: new Set(previous.closedTurnIds),
|
||||
turnAliases: new Map(previous.turnAliases),
|
||||
};
|
||||
if (!options.index) return reduceThreadEvent(previous, event, options);
|
||||
options.index.sync(previous.messages);
|
||||
const effects = options.effects ?? { bodyWrites: new Set<string>(), consumed: new Set<string>() };
|
||||
const state = reduceThreadEvent(previous, event, { ...options, effects });
|
||||
options.index?.sync(state.messages, effects);
|
||||
return state;
|
||||
}
|
||||
|
||||
function reduceThreadEvent(
|
||||
previous: ThreadProjectionState,
|
||||
event: ThreadProjectionEvent,
|
||||
options: ThreadProjectionOptions,
|
||||
): ThreadProjectionState {
|
||||
const state: ThreadProjectionState = { ...previous };
|
||||
|
||||
if (event.event === "model_request") {
|
||||
if (!event.turn_id || state.closedTurnIds.has(event.turn_id)) return state;
|
||||
@@ -973,14 +1008,24 @@ export function projectThreadEvent(
|
||||
const mergeNext = event.resuming === true && event.merge_next === true;
|
||||
const streamKey = answerStreamKey(turn, event.stream_id);
|
||||
const wireClosed = streamKey !== undefined && state.closedAnswerStreamIds.has(streamKey);
|
||||
if (streamKey && !mergeNext) state.closedAnswerStreamIds.add(streamKey);
|
||||
let targetIndex = answerStreamIndex(state, turn, event.stream_id);
|
||||
if (streamKey && !mergeNext && !wireClosed) {
|
||||
if (!options.mutableCollections) state.closedAnswerStreamIds = new Set(state.closedAnswerStreamIds);
|
||||
state.closedAnswerStreamIds.add(streamKey);
|
||||
}
|
||||
let targetIndex = answerStreamIndex(state, turn, event.stream_id, options.index);
|
||||
let text = event.text;
|
||||
// Older completed records can span a hosted tool. Their snapshots already
|
||||
// use rewritten text; live streams now end before the tool starts.
|
||||
if (options.persisted && event.stream_id && typeof text === "string") {
|
||||
let offset = 0;
|
||||
for (let index = 0; index < (targetIndex ?? state.messages.length); index++) {
|
||||
const positions = options.index?.answer(event.stream_id);
|
||||
if (positions) {
|
||||
for (const index of positions) {
|
||||
if (index < (targetIndex ?? state.messages.length) && matchesTurn(state.messages[index], turn)) {
|
||||
offset += state.messages[index].content.length;
|
||||
}
|
||||
}
|
||||
} else for (let index = 0; index < (targetIndex ?? state.messages.length); index++) {
|
||||
const row = state.messages[index];
|
||||
if (row.streamId === event.stream_id && matchesTurn(row, turn)) offset += row.content.length;
|
||||
}
|
||||
@@ -1026,7 +1071,7 @@ export function projectThreadEvent(
|
||||
if (wasClosed || wireClosed) return state;
|
||||
if (targetIndex !== null) state.activeAssistantId = state.messages[targetIndex].id;
|
||||
state.mergeReasoning = mergeNext && state.activeAssistantId !== null;
|
||||
if (!mergeNext) Object.assign(state, closeThreadProjectionAnswer(state));
|
||||
if (!mergeNext) Object.assign(state, closeThreadProjectionAnswer(state, options.mutableCollections));
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -1050,7 +1095,7 @@ export function projectThreadEvent(
|
||||
}
|
||||
|
||||
if (event.event === "context_compaction") {
|
||||
Object.assign(state, closeThreadProjectionAnswer(state));
|
||||
Object.assign(state, closeThreadProjectionAnswer(state, options.mutableCollections));
|
||||
if (event.phase === "succeeded") {
|
||||
state.modelRequests = new Map(Array.from(state.modelRequests, ([turn, request]) =>
|
||||
[turn, { ...request, contextSnapshots: [] }]));
|
||||
@@ -1135,7 +1180,7 @@ export function projectThreadEvent(
|
||||
return state;
|
||||
}
|
||||
const activeId = state.activeAssistantId;
|
||||
Object.assign(state, closeThreadProjectionAnswer(state));
|
||||
Object.assign(state, closeThreadProjectionAnswer(state, options.mutableCollections));
|
||||
if (activeId) {
|
||||
state.messages = state.messages.filter((message) => {
|
||||
if (message.id !== activeId) return true;
|
||||
@@ -1189,23 +1234,28 @@ export function projectThreadEvent(
|
||||
turn.turnId,
|
||||
);
|
||||
state.activeAssistantId = null;
|
||||
state.closedAssistantIds.clear();
|
||||
state.closedAnswerStreamIds.clear();
|
||||
state.closedAssistantIds = new Set();
|
||||
state.closedAnswerStreamIds = new Set();
|
||||
state.mergeReasoning = false;
|
||||
if (event.turn_id) {
|
||||
if (state.turnAliases.has(event.turn_id)) state.turnAliases.delete(event.turn_id);
|
||||
else state.closedTurnIds.add(event.turn_id);
|
||||
if (state.turnAliases.has(event.turn_id)) {
|
||||
state.turnAliases = new Map(state.turnAliases);
|
||||
state.turnAliases.delete(event.turn_id);
|
||||
} else state.closedTurnIds = new Set(state.closedTurnIds).add(event.turn_id);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
export function projectThreadEvents(events: ThreadProjectionEvent[]): UIMessage[] {
|
||||
let state = createThreadProjectionState();
|
||||
const lookup = new ThreadProjectionIndex();
|
||||
events.forEach((event, index) => {
|
||||
state = projectThreadEvent(state, event, {
|
||||
createId: () => `history-${index}`,
|
||||
now: 0,
|
||||
persisted: true,
|
||||
index: lookup,
|
||||
mutableCollections: true,
|
||||
});
|
||||
});
|
||||
return state.messages.map((row) => {
|
||||
|
||||
Reference in new issue
Block a user