refactor(webui): unify live and replay event projection (#5819)
* refactor(webui): unify live and replay event projection * chore(webui): mark legacy projection cleanup * fix(webui): rewrite streamed replay media URLs
This commit is contained in:
1 parent
3d4200ff0d
commit
db675a43b6
14 files changed
+1726
-665
No files matched your search
@@ -81,7 +81,12 @@ describe("webui API helpers", () => {
|
||||
"fetch",
|
||||
vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ deleted: true, key: "websocket:chat-1", messages: [] }),
|
||||
json: async () => ({
|
||||
schemaVersion: 3,
|
||||
deleted: true,
|
||||
key: "websocket:chat-1",
|
||||
messages: [],
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -95,7 +100,7 @@ describe("webui API helpers", () => {
|
||||
await fetchWebuiThread("tok", "websocket:chat-1");
|
||||
|
||||
expect(fetch).toHaveBeenCalledWith(
|
||||
"/api/sessions/websocket%3Achat-1/webui-thread",
|
||||
"/api/sessions/websocket%3Achat-1/webui-thread?projection=events",
|
||||
expect.objectContaining({
|
||||
headers: { Authorization: "Bearer tok" },
|
||||
credentials: "same-origin",
|
||||
@@ -111,7 +116,7 @@ describe("webui API helpers", () => {
|
||||
});
|
||||
|
||||
expect(fetch).toHaveBeenCalledWith(
|
||||
"/api/sessions/websocket%3Achat-1/webui-thread?limit=120&before=abc%2B%2F%3D",
|
||||
"/api/sessions/websocket%3Achat-1/webui-thread?limit=120&before=abc%2B%2F%3D&projection=events",
|
||||
expect.objectContaining({
|
||||
headers: { Authorization: "Bearer tok" },
|
||||
credentials: "same-origin",
|
||||
@@ -136,7 +141,7 @@ describe("webui API helpers", () => {
|
||||
})).resolves.toBe(cached);
|
||||
|
||||
expect(fetch).toHaveBeenCalledWith(
|
||||
"/api/sessions/websocket%3Achat-1/webui-thread",
|
||||
"/api/sessions/websocket%3Achat-1/webui-thread?projection=events",
|
||||
expect.objectContaining({
|
||||
headers: {
|
||||
Authorization: "Bearer tok",
|
||||
@@ -147,6 +152,22 @@ describe("webui API helpers", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects malformed canonical thread events at the HTTP boundary", async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
schemaVersion: 3,
|
||||
projection: "events",
|
||||
events: [{ event: "file_edit", chat_id: "chat-1", edits: [{ tool: "edit_file" }] }],
|
||||
}),
|
||||
} as Response);
|
||||
|
||||
await expect(fetchWebuiThread("tok", "websocket:chat-1")).rejects.toThrow(
|
||||
"Invalid WebUI thread projection event: file_edit",
|
||||
);
|
||||
});
|
||||
|
||||
it("fetches deferred trace details with encoded session and ref", async () => {
|
||||
await fetchWebuiThreadTraceDetail("tok", "websocket:chat-1", "9.tr-deadbeefdeadbeef");
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { projectThreadEvents } from "@/lib/thread-event-projection";
|
||||
import type { ThreadProjectionEvent, UIMessage } from "@/lib/types";
|
||||
import projectionFixture from "./fixtures/live-replay-event-projection.json";
|
||||
|
||||
interface ProjectionFixtureCase {
|
||||
name: string;
|
||||
transcript: Array<Record<string, unknown>>;
|
||||
expected: Array<Record<string, unknown>>;
|
||||
}
|
||||
|
||||
const cases = (
|
||||
projectionFixture as unknown as { cases: ProjectionFixtureCase[] }
|
||||
).cases;
|
||||
|
||||
const SEMANTIC_MESSAGE_FIELDS = [
|
||||
"role",
|
||||
"content",
|
||||
"kind",
|
||||
"traces",
|
||||
"toolEvents",
|
||||
"fileEdits",
|
||||
"images",
|
||||
"media",
|
||||
"cliApps",
|
||||
"mcpPresets",
|
||||
"sessionMentions",
|
||||
"reasoning",
|
||||
"latencyMs",
|
||||
"source",
|
||||
"turnId",
|
||||
"turnPhase",
|
||||
"turnSeq",
|
||||
] as const satisfies ReadonlyArray<keyof UIMessage>;
|
||||
|
||||
function fixtureEvents(records: Array<Record<string, unknown>>): ThreadProjectionEvent[] {
|
||||
return records.flatMap((record, index) => {
|
||||
if (record.event === "user") {
|
||||
return [{
|
||||
...record,
|
||||
event: "user_message" as const,
|
||||
starts_turn: true,
|
||||
projection_id: `fixture-${index}`,
|
||||
} as unknown as ThreadProjectionEvent];
|
||||
}
|
||||
return [{
|
||||
...record,
|
||||
projection_id: `fixture-${index}`,
|
||||
} as unknown as ThreadProjectionEvent];
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeProjection(messages: UIMessage[]): Array<Record<string, unknown>> {
|
||||
const segmentAliases = new Map<string, string>();
|
||||
return messages.map((message) => {
|
||||
const row: Record<string, unknown> = {};
|
||||
for (const field of SEMANTIC_MESSAGE_FIELDS) {
|
||||
const value = message[field];
|
||||
if (value !== undefined && value !== null) row[field] = value;
|
||||
}
|
||||
if (message.activitySegmentId) {
|
||||
let alias = segmentAliases.get(message.activitySegmentId);
|
||||
if (!alias) {
|
||||
alias = `segment-${segmentAliases.size + 1}`;
|
||||
segmentAliases.set(message.activitySegmentId, alias);
|
||||
}
|
||||
row.activitySegmentId = alias;
|
||||
}
|
||||
return row;
|
||||
});
|
||||
}
|
||||
|
||||
describe("canonical thread event projection", () => {
|
||||
it.each(cases)("projects persisted $name events", (fixtureCase) => {
|
||||
const messages = projectThreadEvents(fixtureEvents(fixtureCase.transcript));
|
||||
expect(normalizeProjection(messages)).toEqual(fixtureCase.expected);
|
||||
});
|
||||
});
|
||||
@@ -3539,7 +3539,7 @@ describe("useNanobotStream", () => {
|
||||
|
||||
});
|
||||
|
||||
describe("live/replay projection before canonical-event revision migration", () => {
|
||||
describe("live canonical event projection", () => {
|
||||
it.each(PROJECTION_FIXTURE_CASES)("matches the shared $name fixture", (fixtureCase) => {
|
||||
// Keep client-only elapsed-time estimates out of the transport contract.
|
||||
const clock = vi.spyOn(Date, "now").mockReturnValue(0);
|
||||
|
||||
@@ -568,6 +568,75 @@ describe("useSessions", () => {
|
||||
expect(result.current.messages[2]!.content).toBe("summary");
|
||||
});
|
||||
|
||||
it("projects canonical transcript events with the live event reducer", async () => {
|
||||
vi.mocked(api.fetchWebuiThread).mockResolvedValue({
|
||||
schemaVersion: 3,
|
||||
projection: "events",
|
||||
events: [
|
||||
{
|
||||
event: "user_message",
|
||||
chat_id: "chat-event-history",
|
||||
text: "explain",
|
||||
starts_turn: true,
|
||||
projection_id: "history-user",
|
||||
turn_id: "turn-history",
|
||||
turn_phase: "user",
|
||||
turn_seq: 1,
|
||||
},
|
||||
{
|
||||
event: "reasoning_delta",
|
||||
chat_id: "chat-event-history",
|
||||
text: "thinking",
|
||||
projection_id: "history-reasoning",
|
||||
turn_id: "turn-history",
|
||||
turn_phase: "reasoning",
|
||||
turn_seq: 2,
|
||||
},
|
||||
{
|
||||
event: "reasoning_end",
|
||||
chat_id: "chat-event-history",
|
||||
projection_id: "history-reasoning-end",
|
||||
turn_id: "turn-history",
|
||||
turn_phase: "reasoning",
|
||||
turn_seq: 3,
|
||||
},
|
||||
{
|
||||
event: "delta",
|
||||
chat_id: "chat-event-history",
|
||||
text: "answer",
|
||||
projection_id: "history-answer",
|
||||
turn_id: "turn-history",
|
||||
turn_phase: "answer",
|
||||
turn_seq: 4,
|
||||
},
|
||||
{
|
||||
event: "turn_end",
|
||||
chat_id: "chat-event-history",
|
||||
projection_id: "history-end",
|
||||
latency_ms: 25,
|
||||
turn_id: "turn-history",
|
||||
turn_phase: "complete",
|
||||
turn_seq: 5,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const { result } = renderHook(() => useSessionHistory("websocket:chat-event-history"), {
|
||||
wrapper: wrap(fakeClient()),
|
||||
});
|
||||
|
||||
await waitFor(() => expect(result.current.loading).toBe(false));
|
||||
|
||||
expect(result.current.messages).toHaveLength(2);
|
||||
expect(result.current.messages[1]).toMatchObject({
|
||||
role: "assistant",
|
||||
content: "answer",
|
||||
reasoning: "thinking",
|
||||
latencyMs: 25,
|
||||
turnId: "turn-history",
|
||||
});
|
||||
});
|
||||
|
||||
it("shows a cached transcript immediately while revalidating it", async () => {
|
||||
const cached = {
|
||||
schemaVersion: 3,
|
||||
|
||||
Reference in new issue
Block a user