feat(webui): distinguish context compaction reasons (NAN-324) (#6389)
* feat(webui): distinguish context compaction reasons (NAN-324) * fix(webui): describe idle compaction cancellation as interruption * docs: omit compaction notice details from configuration guide * fix(i18n): make compaction notices read naturally * fix(i18n): keep CJK compaction notices concise * fix(i18n): describe idle compaction as unfinished
This commit is contained in:
1 parent
084b6bb394
commit
bc5c58d239
38 files changed
+540
-23
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { decodeNotification } from "../../../packages/client-events/notifications";
|
||||
import { parseWebuiThreadPayload } from "@/lib/api";
|
||||
import { projectThreadEvents } from "@/lib/thread-event-projection";
|
||||
|
||||
const base = { event: "context_compaction", chat_id: "chat", compaction_id: "compact",
|
||||
turn_id: "turn", turn_phase: "activity", turn_seq: 2, created_at_ms: 123 };
|
||||
|
||||
function history(events: unknown[]) {
|
||||
return projectThreadEvents(parseWebuiThreadPayload({ schemaVersion: 3, projection: "events", events }).events);
|
||||
}
|
||||
|
||||
describe("compaction reason wire to history projection", () => {
|
||||
it.each(["idle", "context_pressure", "manual", "provider_native"] as const)(
|
||||
"preserves explicit %s through phase updates and rehydration", (reason) => {
|
||||
const start = { ...base, phase: "started", reason };
|
||||
const finish = { ...base, phase: "succeeded", reason, turn_seq: 3, created_at_ms: 456 };
|
||||
expect(decodeNotification(start)).toEqual(start);
|
||||
const rows = history([start, finish]);
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0]).toMatchObject({ id: "compaction-compact", createdAt: 123,
|
||||
turnId: "turn", turnPhase: "activity", turnSeq: 3,
|
||||
compaction: { id: "compact", phase: "succeeded", reason } });
|
||||
expect(rows[0].compaction?.announce).toBeUndefined();
|
||||
expect(history(JSON.parse(JSON.stringify([start, finish])))).toEqual(rows);
|
||||
expect(history([{ ...finish, reason }])[0].compaction).toMatchObject({ reason, phase: "succeeded" });
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps old records neutral and rejects malformed reasons at the history boundary", () => {
|
||||
expect(history([{ ...base, phase: "succeeded" }])[0].compaction?.reason).toBeUndefined();
|
||||
for (const reason of ["unknown", null, 42]) {
|
||||
expect(() => history([{ ...base, phase: "succeeded", reason }])).toThrow("Invalid WebUI thread projection event");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -434,6 +434,15 @@ describe("webui i18n", () => {
|
||||
expect(notice?.textContent).toBe(copy[phase]);
|
||||
expect(notice).toHaveAttribute("aria-busy", String(phase === "started"));
|
||||
}
|
||||
for (const reason of ["idle", "context_pressure", "manual", "provider_native"] as const) {
|
||||
for (const phase of ["started", "succeeded", "failed", "cancelled"] as const) {
|
||||
rerender(<ContextCompactionNotice compaction={{ id: "compact-1", reason, phase, announce: true }} />);
|
||||
const notice = container.querySelector("[data-context-compaction]");
|
||||
expect(copy.reasons[reason][phase]).toBeTruthy();
|
||||
expect(notice?.textContent).toBe(copy.reasons[reason][phase]);
|
||||
expect(notice).toHaveAttribute("aria-busy", String(phase === "started"));
|
||||
}
|
||||
}
|
||||
for (const compactReply of ["empty", "failed"] as const) {
|
||||
rerender(<MessageBubble message={{
|
||||
id: "reply", role: "assistant", content: "original command reply",
|
||||
|
||||
@@ -22,6 +22,9 @@ describe("shared notification contract", () => {
|
||||
expect(decodeNotification({ event: "delta", text: "hello" })).toBeUndefined();
|
||||
expect(decodeNotification({ ...fixtures[0], phase: "unknown" })).toBeNull();
|
||||
expect(decodeNotification({ ...fixtures[0], compaction_id: "" })).toBeNull();
|
||||
for (const reason of [null, 42, "unknown"]) {
|
||||
expect(decodeNotification({ ...fixtures[0], reason })).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("allows one terminal transition, including terminal-first hydration", () => {
|
||||
|
||||
@@ -65,6 +65,7 @@ function messageEvents(message: UIMessage): ThreadProjectionEvent[] {
|
||||
if (message.kind === "compaction" && message.compaction) {
|
||||
return [{
|
||||
event: "context_compaction",
|
||||
reason: message.compaction.reason,
|
||||
...base,
|
||||
compaction_id: message.compaction.id,
|
||||
phase: message.compaction.phase,
|
||||
|
||||
@@ -349,7 +349,7 @@ describe("useNanobotStream", () => {
|
||||
act(() => fake.emit("sources", { event: "stream_end", chat_id: "sources", response_sources: [] }));
|
||||
expect(result.current.messages[1].responseSources).toEqual([]);
|
||||
});
|
||||
it.each(["succeeded", "cancelled"] as const)("updates one stable compaction row to %s", (phase) => {
|
||||
it.each(["succeeded", "failed", "cancelled"] as const)("updates one stable compaction row to %s", (phase) => {
|
||||
const fake = fakeClient();
|
||||
const { result } = renderHook(
|
||||
() => useNanobotStream("chat-compaction", EMPTY_MESSAGES),
|
||||
@@ -362,6 +362,7 @@ describe("useNanobotStream", () => {
|
||||
chat_id: "chat-compaction",
|
||||
compaction_id: "compact-1",
|
||||
phase: "started",
|
||||
reason: "idle",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -373,6 +374,7 @@ describe("useNanobotStream", () => {
|
||||
compaction: {
|
||||
id: "compact-1",
|
||||
phase: "started",
|
||||
reason: "idle",
|
||||
announce: true,
|
||||
},
|
||||
});
|
||||
@@ -383,6 +385,7 @@ describe("useNanobotStream", () => {
|
||||
chat_id: "chat-compaction",
|
||||
compaction_id: "compact-1",
|
||||
phase,
|
||||
reason: "idle",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -392,6 +395,7 @@ describe("useNanobotStream", () => {
|
||||
createdAt,
|
||||
compaction: {
|
||||
phase,
|
||||
reason: "idle",
|
||||
announce: true,
|
||||
},
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user