feat: make context compaction visible in channels (#5656)
* feat: surface context compaction lifecycle * fix: complete and simplify context compaction notifications
This commit is contained in:
1 parent
455533169d
commit
5229989f28
52 files changed
+1849
-140
No files matched your search
@@ -2,13 +2,15 @@ import { readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { runInNewContext } from "node:vm";
|
||||
|
||||
import { act, render, screen, waitFor } from "@testing-library/react";
|
||||
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { LanguageSwitcher } from "@/components/LanguageSwitcher";
|
||||
import { MessageBubble } from "@/components/MessageBubble";
|
||||
import { ContextCompactionNotice } from "@/components/thread/ContextCompactionNotice";
|
||||
import { ThreadComposer } from "@/components/thread/ThreadComposer";
|
||||
import { resources } from "@/i18n";
|
||||
import { resources, setAppLanguage } from "@/i18n";
|
||||
import {
|
||||
LOCALE_STORAGE_KEY,
|
||||
resolveInitialLocale,
|
||||
@@ -20,6 +22,7 @@ const IMAGE_QUICK_ACTION_KEYS = ["icon", "sticker", "poster", "product", "portra
|
||||
const HERO_GREETING_KEYS = ["workOn", "start", "build", "tackle"];
|
||||
const SLASH_COMMAND_KEYS = [
|
||||
"new",
|
||||
"compact",
|
||||
"stop",
|
||||
"restart",
|
||||
"status",
|
||||
@@ -418,6 +421,30 @@ describe("webui i18n", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.each(supportedLocales)("localizes compaction states in $code", async ({ code }) => {
|
||||
await setAppLanguage(code);
|
||||
const copy = resources[code].common.thread.compaction;
|
||||
const { container, rerender } = render(
|
||||
<ContextCompactionNotice compaction={{ id: "compact-1", phase: "started", announce: true }} />,
|
||||
);
|
||||
for (const phase of ["started", "succeeded", "failed", "cancelled"] as const) {
|
||||
rerender(<ContextCompactionNotice compaction={{
|
||||
id: "compact-1", phase, announce: true,
|
||||
}} />);
|
||||
const notice = container.querySelector("[data-context-compaction]");
|
||||
expect(copy[phase]).toBeTruthy();
|
||||
expect(notice?.textContent).toBe(copy[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",
|
||||
compactReply, createdAt: 1,
|
||||
}} />);
|
||||
expect(screen.getByText(copy[compactReply])).toBeInTheDocument();
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps preboot copy aligned with every registered locale", () => {
|
||||
for (const { code } of supportedLocales) {
|
||||
const result = runPrebootLocale(code);
|
||||
@@ -499,6 +526,36 @@ describe("webui i18n", () => {
|
||||
expect(screen.getByLabelText("メッセージ入力欄")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("localizes a backend-provided compact slash command", async () => {
|
||||
await act(async () => {
|
||||
const { setAppLanguage } = await import("@/i18n");
|
||||
await setAppLanguage("zh-CN");
|
||||
});
|
||||
|
||||
render(
|
||||
<ThreadComposer
|
||||
onSend={vi.fn()}
|
||||
slashCommands={[{
|
||||
command: "/compact",
|
||||
title: "Compact context",
|
||||
description: "Compact this chat's context and continue the conversation.",
|
||||
icon: "archive",
|
||||
lifecycle: "side_channel",
|
||||
acceptsArgs: false,
|
||||
}]}
|
||||
/>,
|
||||
);
|
||||
|
||||
fireEvent.change(screen.getByLabelText("消息输入框"), {
|
||||
target: { value: "/co" },
|
||||
});
|
||||
|
||||
expect(screen.getByRole("listbox", { name: "斜杠命令" })).toBeInTheDocument();
|
||||
expect(screen.getByText("压缩上下文")).toBeInTheDocument();
|
||||
expect(screen.getByText("压缩当前对话的上下文并继续对话。")).toBeInTheDocument();
|
||||
expect(screen.getByText("/compact")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("keeps empty landing resources localized for every registered locale", () => {
|
||||
for (const resource of Object.values(resources)) {
|
||||
const empty = resource.common.thread.empty;
|
||||
|
||||
@@ -2,6 +2,7 @@ import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { MessageBubble } from "@/components/MessageBubble";
|
||||
import { setAppLanguage } from "@/i18n";
|
||||
import { fmtDateTime, formatMessageEndTime } from "@/lib/format";
|
||||
import type {
|
||||
CliAppInfo,
|
||||
@@ -95,6 +96,46 @@ const SLASH_COMMANDS: SlashCommand[] = [
|
||||
];
|
||||
|
||||
describe("MessageBubble", () => {
|
||||
it("copies the localized compact reply instead of the stored English text", async () => {
|
||||
await setAppLanguage("zh-CN");
|
||||
const writeText = vi.fn().mockResolvedValue(undefined);
|
||||
Object.defineProperty(navigator, "clipboard", {
|
||||
configurable: true,
|
||||
value: { writeText },
|
||||
});
|
||||
render(<MessageBubble message={{
|
||||
id: "compact-empty", role: "assistant", content: "Nothing to compact.",
|
||||
compactReply: "empty", createdAt: 1,
|
||||
}} />);
|
||||
expect(screen.getByText("无需压缩上下文")).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: "复制" }));
|
||||
await waitFor(() => expect(writeText).toHaveBeenCalledWith("无需压缩上下文"));
|
||||
});
|
||||
|
||||
it("renders a compacted context notice", () => {
|
||||
const message: UIMessage = {
|
||||
id: "compaction-1",
|
||||
role: "assistant",
|
||||
content: "",
|
||||
kind: "compaction",
|
||||
createdAt: Date.now(),
|
||||
compaction: {
|
||||
id: "compact-1",
|
||||
phase: "succeeded",
|
||||
announce: true,
|
||||
},
|
||||
};
|
||||
|
||||
const { container } = render(<MessageBubble message={message} />);
|
||||
|
||||
const notice = container.querySelector("[data-context-compaction='succeeded']");
|
||||
expect(notice).toHaveAttribute("role", "status");
|
||||
expect(notice).toHaveAttribute("aria-live", "polite");
|
||||
expect(screen.getByText("Context compacted")).toBeInTheDocument();
|
||||
expect(notice).toHaveTextContent(/^Context compacted$/);
|
||||
expect(notice?.querySelector(".lucide-archive")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders user messages as right-aligned pills", () => {
|
||||
const message: UIMessage = {
|
||||
id: "u1",
|
||||
|
||||
@@ -66,6 +66,26 @@ describe("normalizeLegacyLongTaskMessages", () => {
|
||||
});
|
||||
|
||||
describe("projectWebuiThreadMessages", () => {
|
||||
it("marks only fixed replies belonging to a compact command turn", () => {
|
||||
const rows = [
|
||||
message("user", { content: " /COMPACT ", turnId: "compact" }),
|
||||
message("assistant", { content: "Nothing to compact.", turnId: "compact" }),
|
||||
message("assistant", {
|
||||
content: "Unable to compact context. Check the logs and try again.", turnId: "compact",
|
||||
}),
|
||||
message("assistant", { content: "Nothing to compact.", turnId: "unrelated" }),
|
||||
message("assistant", { content: "Nothing to compact." }),
|
||||
message("assistant", { content: "Nothing to compact.", turnId: "compact", isStreaming: true }),
|
||||
message("assistant", { content: "Quoted: Nothing to compact.", turnId: "compact" }),
|
||||
];
|
||||
const projected = projectWebuiThreadMessages(rows);
|
||||
expect(projected.map(({ compactReply }) => compactReply)).toEqual([
|
||||
undefined, "empty", "failed", undefined, undefined, undefined, undefined,
|
||||
]);
|
||||
expect(projected.map(({ content }) => content)).toEqual(rows.map(({ content }) => content));
|
||||
expect(projectWebuiThreadMessages(projected)).toEqual(projected);
|
||||
});
|
||||
|
||||
it("derives replayed completion time from the matching user turn", () => {
|
||||
const firstOutputAt = STARTED_AT + 5_000;
|
||||
const latencyMs = 13_000;
|
||||
|
||||
@@ -1678,4 +1678,32 @@ describe("ThreadMessages", () => {
|
||||
["a3", true],
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps compaction notices out of assistant fork selection", () => {
|
||||
const units = buildDisplayUnits([
|
||||
{ id: "u1", role: "user", content: "one", createdAt: 1 },
|
||||
{ id: "a1", role: "assistant", content: "answer", createdAt: 2 },
|
||||
{
|
||||
id: "compaction-1",
|
||||
role: "assistant",
|
||||
content: "",
|
||||
kind: "compaction",
|
||||
createdAt: 3,
|
||||
compaction: {
|
||||
id: "compact-1",
|
||||
phase: "succeeded",
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
const flags = assistantForkFlags(units);
|
||||
expect(units.map((unit, index) => [
|
||||
unit.type === "message" ? unit.message.id : "activity",
|
||||
flags[index],
|
||||
])).toEqual([
|
||||
["u1", true],
|
||||
["a1", true],
|
||||
["compaction-1", false],
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -197,6 +197,54 @@ async function flushStreamFrame() {
|
||||
}
|
||||
|
||||
describe("useNanobotStream", () => {
|
||||
it.each(["succeeded", "cancelled"] as const)("updates one stable compaction row to %s", (phase) => {
|
||||
const fake = fakeClient();
|
||||
const { result } = renderHook(
|
||||
() => useNanobotStream("chat-compaction", EMPTY_MESSAGES),
|
||||
{ wrapper: wrap(fake.client) },
|
||||
);
|
||||
|
||||
act(() => {
|
||||
fake.emit("chat-compaction", {
|
||||
event: "context_compaction",
|
||||
chat_id: "chat-compaction",
|
||||
compaction_id: "compact-1",
|
||||
phase: "started",
|
||||
});
|
||||
});
|
||||
|
||||
expect(result.current.messages).toHaveLength(1);
|
||||
const createdAt = result.current.messages[0].createdAt;
|
||||
expect(result.current.messages[0]).toMatchObject({
|
||||
id: "compaction-compact-1",
|
||||
kind: "compaction",
|
||||
compaction: {
|
||||
id: "compact-1",
|
||||
phase: "started",
|
||||
announce: true,
|
||||
},
|
||||
});
|
||||
|
||||
act(() => {
|
||||
fake.emit("chat-compaction", {
|
||||
event: "context_compaction",
|
||||
chat_id: "chat-compaction",
|
||||
compaction_id: "compact-1",
|
||||
phase,
|
||||
});
|
||||
});
|
||||
|
||||
expect(result.current.messages).toHaveLength(1);
|
||||
expect(result.current.messages[0]).toMatchObject({
|
||||
id: "compaction-compact-1",
|
||||
createdAt,
|
||||
compaction: {
|
||||
phase,
|
||||
announce: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("batches answer deltas into one animation-frame update", async () => {
|
||||
const fake = fakeClient();
|
||||
const requestFrame = vi.spyOn(window, "requestAnimationFrame");
|
||||
|
||||
Reference in new issue
Block a user