refactor(webui): remove legacy message projection (#5823)
* refactor(webui): defer oversized replay traces as events * fix(webui): type deferred trace index set * refactor(webui): remove legacy message projection * fix(webui): type event protocol boundaries * test(webui): assert canonical thread events
This commit is contained in:
1 parent
756c8bb7ed
commit
0983762fe5
23 files changed
+943
-2839
No files matched your search
+36
-8
@@ -290,6 +290,18 @@ function hasValidProjectionMetadata(value: Record<string, unknown>): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
function isProjectionTraceDetail(value: unknown): boolean {
|
||||
return isRecord(value)
|
||||
&& typeof value.ref === "string"
|
||||
&& /^\d{1,12}\.history-[0-9a-f]{20}$/.test(value.ref)
|
||||
&& typeof value.bytes === "number"
|
||||
&& Number.isFinite(value.bytes)
|
||||
&& value.bytes >= 0
|
||||
&& typeof value.traceCount === "number"
|
||||
&& Number.isInteger(value.traceCount)
|
||||
&& value.traceCount >= 0;
|
||||
}
|
||||
|
||||
function parseThreadProjectionEvent(value: unknown): ThreadProjectionEvent {
|
||||
if (!isRecord(value) || typeof value.event !== "string" || typeof value.chat_id !== "string") {
|
||||
throw new Error("Invalid WebUI thread event");
|
||||
@@ -328,6 +340,10 @@ function parseThreadProjectionEvent(value: unknown): ThreadProjectionEvent {
|
||||
case "message":
|
||||
if (typeof value.text !== "string") break;
|
||||
if (value.tool_events !== undefined && !isRecordArray(value.tool_events)) break;
|
||||
if (value.trace_detail !== undefined && (
|
||||
!["tool_hint", "progress"].includes(String(value.kind))
|
||||
|| !isProjectionTraceDetail(value.trace_detail)
|
||||
)) break;
|
||||
if (value.media_urls !== undefined && (
|
||||
!Array.isArray(value.media_urls) || !value.media_urls.every(isProjectionMedia)
|
||||
)) break;
|
||||
@@ -356,19 +372,32 @@ function parseWebuiThreadPayload(value: unknown): WebuiThreadPersistedPayload {
|
||||
if (!isRecord(value) || typeof value.schemaVersion !== "number") {
|
||||
throw new Error("Invalid WebUI thread response");
|
||||
}
|
||||
if (value.messages !== undefined && !Array.isArray(value.messages)) {
|
||||
throw new Error("Invalid WebUI thread messages");
|
||||
}
|
||||
if (value.events !== undefined && !Array.isArray(value.events)) {
|
||||
if (value.projection !== "events" || !Array.isArray(value.events)) {
|
||||
throw new Error("Invalid WebUI thread events");
|
||||
}
|
||||
const events = value.events?.map(parseThreadProjectionEvent);
|
||||
const events = value.events.map(parseThreadProjectionEvent);
|
||||
return {
|
||||
...value,
|
||||
...(events ? { events } : {}),
|
||||
projection: "events",
|
||||
events,
|
||||
} as unknown as WebuiThreadPersistedPayload;
|
||||
}
|
||||
|
||||
function parseWebuiThreadTraceDetailPayload(value: unknown): WebuiThreadTraceDetailPayload {
|
||||
if (
|
||||
!isRecord(value)
|
||||
|| typeof value.message_id !== "string"
|
||||
|| !/^history-[0-9a-f]{20}$/.test(value.message_id)
|
||||
|| !Array.isArray(value.events)
|
||||
) {
|
||||
throw new Error("Invalid WebUI thread trace detail response");
|
||||
}
|
||||
return {
|
||||
message_id: value.message_id,
|
||||
events: value.events.map(parseThreadProjectionEvent),
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchWebuiThread(
|
||||
token: string,
|
||||
key: string,
|
||||
@@ -381,7 +410,6 @@ export async function fetchWebuiThread(
|
||||
if (options?.limit !== undefined) params.set("limit", String(options.limit));
|
||||
if (options?.direction) params.set("direction", options.direction);
|
||||
if (options?.before) params.set("before", options.before);
|
||||
params.set("projection", "events");
|
||||
const query = params.toString();
|
||||
const suffix = query ? `?${query}` : "";
|
||||
const url = `${resolvedBase}/api/sessions/${encodeURIComponent(key)}/webui-thread${suffix}`;
|
||||
@@ -413,7 +441,7 @@ export async function fetchWebuiThreadTraceDetail(
|
||||
cache: "no-store",
|
||||
});
|
||||
if (!res.ok) throw new ApiError(res.status, `HTTP ${res.status}`);
|
||||
return (await res.json()) as WebuiThreadTraceDetailPayload;
|
||||
return parseWebuiThreadTraceDetailPayload(await res.json());
|
||||
}
|
||||
|
||||
export async function fetchFilePreview(
|
||||
|
||||
+1
-22
@@ -3,27 +3,6 @@ import { isSystemCommandTurnId } from "@/lib/nanobot-client";
|
||||
import { scrubSubagentUiMessages } from "@/lib/subagent-channel-display";
|
||||
import type { UIMessage } from "@/lib/types";
|
||||
|
||||
/**
|
||||
* Older WebUI disk snapshots and historical sessions may still contain
|
||||
* ``kind: "long_task"`` rows from the retired orchestrator UI. Map them to
|
||||
* ordinary trace rows so the thread stays readable without bespoke cards.
|
||||
*/
|
||||
export function normalizeLegacyLongTaskMessages(messages: UIMessage[]): UIMessage[] {
|
||||
return messages.map((m) => {
|
||||
const kind = (m as { kind?: string }).kind;
|
||||
if (kind !== "long_task") return m;
|
||||
const text = (m.content ?? "").trim() || "(legacy thread activity)";
|
||||
return {
|
||||
id: m.id,
|
||||
role: "tool",
|
||||
kind: "trace",
|
||||
content: text,
|
||||
traces: [text],
|
||||
createdAt: m.createdAt,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
interface PreparedMessage {
|
||||
message: UIMessage;
|
||||
hidden: boolean;
|
||||
@@ -36,7 +15,7 @@ const completionMessages = new WeakMap<UIMessage, { completedAt: number; message
|
||||
function prepareMessage(original: UIMessage): PreparedMessage {
|
||||
const cached = preparedMessages.get(original);
|
||||
if (cached) return cached;
|
||||
const message = scrubSubagentUiMessages(normalizeLegacyLongTaskMessages([original]))[0];
|
||||
const message = scrubSubagentUiMessages([original])[0];
|
||||
const prepared = {
|
||||
message,
|
||||
hidden: isSystemCommandTurnId(message.turnId)
|
||||
@@ -643,6 +643,7 @@ function projectToolActivity(
|
||||
: [];
|
||||
if (lines.length === 0) return;
|
||||
|
||||
const deferredDetail = event.trace_detail;
|
||||
const segmentId = ensureProjectionActivitySegment(state);
|
||||
const turn = turnFieldsForProjection(state, event, "activity");
|
||||
const last = state.messages[state.messages.length - 1];
|
||||
@@ -650,6 +651,8 @@ function projectToolActivity(
|
||||
last
|
||||
&& last.kind === "trace"
|
||||
&& !last.isStreaming
|
||||
&& !last.traceDetail
|
||||
&& !deferredDetail
|
||||
&& (!last.activitySegmentId || last.activitySegmentId === segmentId)
|
||||
) {
|
||||
const previousTraces = last.traces?.length
|
||||
@@ -692,6 +695,7 @@ function projectToolActivity(
|
||||
content: lines[lines.length - 1],
|
||||
traces: lines,
|
||||
...(visibleEvents.length ? { toolEvents: visibleEvents } : {}),
|
||||
...(deferredDetail ? { traceDetail: deferredDetail } : {}),
|
||||
activitySegmentId: segmentId,
|
||||
...turn,
|
||||
createdAt: projectionCreatedAt(event, options),
|
||||
|
||||
+12
-15
@@ -71,6 +71,12 @@ export interface ResponseSource {
|
||||
fallback?: boolean;
|
||||
}
|
||||
|
||||
export interface UITraceDetail {
|
||||
ref: string;
|
||||
bytes: number;
|
||||
traceCount: number;
|
||||
}
|
||||
|
||||
export interface RetryStatus extends WireRetryStatus {
|
||||
next_retry_at?: number;
|
||||
turn_id?: string;
|
||||
@@ -91,11 +97,7 @@ export interface UIMessage {
|
||||
* distinguish running, completed, and failed tool phases. */
|
||||
toolEvents?: ToolProgressEvent[];
|
||||
/** Oversized persisted trace content that can be fetched when activity is expanded. */
|
||||
traceDetail?: {
|
||||
ref: string;
|
||||
bytes: number;
|
||||
traceCount: number;
|
||||
};
|
||||
traceDetail?: UITraceDetail;
|
||||
/** Activity rows: explicit file edits emitted by edit tools. */
|
||||
fileEdits?: UIFileEdit[];
|
||||
/** Activity rows created during the same agent phase share one collapsible block. */
|
||||
@@ -1387,6 +1389,8 @@ export type InboundEvent =
|
||||
media?: string[];
|
||||
media_urls?: Array<{ url: string; name?: string }>;
|
||||
tool_events?: ToolProgressEvent[];
|
||||
/** Oversized persisted activity detail, fetched only when the trace is expanded. */
|
||||
trace_detail?: UITraceDetail;
|
||||
/** Present when the frame is an agent breadcrumb (e.g. tool hint,
|
||||
* generic progress line) rather than a conversational reply. */
|
||||
kind?: "tool_hint" | "progress" | "reasoning";
|
||||
@@ -1572,8 +1576,6 @@ export interface OutboundMcpPresetMention {
|
||||
interface WebuiThreadPagePayload {
|
||||
before_cursor?: string | null;
|
||||
has_more_before?: boolean;
|
||||
loaded_message_count?: number;
|
||||
total_known_message_count?: number;
|
||||
user_message_offset?: number;
|
||||
loaded_event_count?: number;
|
||||
}
|
||||
@@ -1584,12 +1586,9 @@ export interface WebuiThreadPersistedPayload {
|
||||
savedAt?: string;
|
||||
/** Cheap server revision used for application-managed conditional revalidation. */
|
||||
revision?: string;
|
||||
/** Legacy server-projected snapshots, retained as a compatibility fallback. */
|
||||
messages?: UIMessage[];
|
||||
/** Canonical transcript events projected by the same reducer as live events. */
|
||||
events?: ThreadProjectionEvent[];
|
||||
projection?: "events";
|
||||
fork_boundary_message_count?: number;
|
||||
events: ThreadProjectionEvent[];
|
||||
projection: "events";
|
||||
fork_boundary_event_index?: number;
|
||||
/** Turn ids backed by an explicit persisted ``turn_end`` event. */
|
||||
completed_turn_ids?: string[];
|
||||
@@ -1602,9 +1601,7 @@ export interface WebuiThreadPersistedPayload {
|
||||
|
||||
export interface WebuiThreadTraceDetailPayload {
|
||||
message_id: string;
|
||||
content: string;
|
||||
traces?: string[];
|
||||
toolEvents?: ToolProgressEvent[];
|
||||
events: ThreadProjectionEvent[];
|
||||
}
|
||||
|
||||
export interface FilePreviewPayload {
|
||||
|
||||
Reference in new issue
Block a user