feat(webui): add lightweight session messaging via mentions

This commit is contained in:
chengyongru authored and chengyongru committed 2026-08-19 01:15:56 +08:00
1 parent 2bdb11eeba
commit 0e184965e8
76 files changed
+8297 -658

No files matched your search

+447 -40
View File
@@ -127,7 +127,12 @@ const MCP_PRESETS: McpPresetInfo[] = [
},
];
function session(chatId: string, title: string, preview = ""): ChatSummary {
function session(
chatId: string,
title: string,
preview = "",
mentionName = title,
): ChatSummary {
return {
key: `websocket:${chatId}`,
channel: "websocket",
@@ -136,6 +141,12 @@ function session(chatId: string, title: string, preview = ""): ChatSummary {
updatedAt: null,
title,
preview,
handle: {
id: `handle_${chatId}`,
name: mentionName,
color_slot: 2,
session_key: `websocket:${chatId}`,
},
};
}
@@ -1722,30 +1733,31 @@ describe("ThreadComposer", () => {
const input = screen.getByLabelText("Message input");
fireEvent.change(input, {
target: { value: "普通文字 @收费设计", selectionStart: 10 },
target: { value: "普通文字 #收费设计", selectionStart: 10 },
});
expect(screen.queryByTestId("composer-session-mention-收费设计")).not.toBeInTheDocument();
expect(screen.queryByTestId("composer-session-reference-收费设计")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenLastCalledWith("普通文字 @收费设计", undefined, undefined);
expect(onSend).toHaveBeenLastCalledWith("普通文字 #收费设计", undefined, undefined);
fireEvent.change(input, {
target: { value: "参考 @收费", selectionStart: 6 },
target: { value: "参考 #收费", selectionStart: 6 },
});
expect(screen.getByRole("group", { name: "Nanobot conversations" })).toBeInTheDocument();
expect(screen.getByRole("option", { name: /@收费设计/i })).toBeInTheDocument();
expect(screen.getByRole("option", { name: /^收费设计 #收费设计$/i }))
.toBeInTheDocument();
fireEvent.keyDown(input, { key: "Tab" });
expect(input).toHaveValue("参考 @收费设计 ");
const mention = screen.getByTestId("composer-session-mention-收费设计");
expect(mention).toHaveTextContent("@收费设计");
expect(input).toHaveValue("参考 #收费设计 ");
const mention = screen.getByTestId("composer-session-reference-收费设计");
expect(mention).toHaveTextContent("#收费设计");
expect(mention).toHaveClass("font-normal");
expect(mention).not.toHaveClass("font-[550]");
expect(mention.closest("a")).toBeNull();
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("参考 @收费设计", undefined, {
expect(onSend).toHaveBeenCalledWith("参考 #收费设计", undefined, {
sessionMentions: [{
name: "收费设计",
session_key: "websocket:pricing",
@@ -1754,6 +1766,198 @@ describe("ThreadComposer", () => {
});
});
it("keeps a selected session reference bound across title refreshes", () => {
const onSend = vi.fn();
const target = session("planning", "Plan");
const { rerender } = render(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
sessions={[target]}
/>,
);
const input = screen.getByLabelText("Message input");
fireEvent.change(input, { target: { value: "#Pla", selectionStart: 4 } });
fireEvent.keyDown(input, { key: "Tab" });
rerender(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
sessions={[{ ...target, title: "Renamed plan" }]}
/>,
);
expect(screen.getByTestId("composer-session-reference-Plan"))
.toHaveTextContent("#Plan");
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("#Plan", undefined, {
sessionMentions: [{
name: "Plan",
session_key: "websocket:planning",
title: "Renamed plan",
}],
});
});
it("does not revive structured session identity after its token is removed", () => {
const onSend = vi.fn();
render(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
sessions={[session("pricing", "收费设计", "讨论云存储")]}
/>,
);
const input = screen.getByLabelText("Message input");
fireEvent.change(input, {
target: { value: "#收费", selectionStart: 3 },
});
fireEvent.keyDown(input, { key: "Tab" });
expect(screen.getByTestId("composer-session-reference-收费设计")).toBeInTheDocument();
fireEvent.change(input, { target: { value: "", selectionStart: 0 } });
fireEvent.change(input, {
target: { value: "普通文字 #收费设计", selectionStart: 10 },
});
expect(screen.queryByTestId("composer-session-reference-收费设计")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("普通文字 #收费设计", undefined, undefined);
});
it("does not migrate a structured identity across an atomic select-all replacement", () => {
const onSend = vi.fn();
render(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
sessions={[session("pricing", "收费设计", "讨论云存储")]}
/>,
);
const input = screen.getByLabelText("Message input") as HTMLTextAreaElement;
fireEvent.change(input, { target: { value: "#收费", selectionStart: 3 } });
fireEvent.keyDown(input, { key: "Tab" });
expect(screen.getByTestId("composer-session-reference-收费设计")).toBeInTheDocument();
input.setSelectionRange(0, input.value.length);
fireEvent.select(input);
const replacement = "普通文字 #收费设计";
fireEvent.change(input, {
target: {
value: replacement,
selectionStart: replacement.length,
selectionEnd: replacement.length,
},
});
expect(screen.queryByTestId("composer-session-reference-收费设计")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith(replacement, undefined, undefined);
});
it("keeps same-name session references distinct from capability mentions", () => {
const onSend = vi.fn();
render(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
cliApps={CLI_APPS}
sessions={[session("blender-chat", "blender")]}
/>,
);
const input = screen.getByLabelText("Message input");
fireEvent.change(input, { target: { value: "#blend", selectionStart: 6 } });
fireEvent.keyDown(input, { key: "Enter" });
expect(screen.getByTestId("composer-session-reference-blender")).toBeInTheDocument();
const next = "#blender @blend";
fireEvent.change(input, { target: { value: next, selectionStart: next.length } });
fireEvent.keyDown(input, { key: "Enter" });
expect(screen.getByTestId("composer-session-reference-blender")).toBeInTheDocument();
expect(screen.getByTestId("composer-cli-mention-blender")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("#blender @blender", undefined, {
cliApps: [expect.objectContaining({ name: "blender" })],
sessionMentions: [{
name: "blender",
session_key: "websocket:blender-chat",
title: "blender",
}],
});
});
it("drops structured session semantics when the identity leaves the current catalog", () => {
const onSend = vi.fn();
const target = session("pricing", "pricing", "", "pricing");
const { rerender } = render(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
sessions={[target]}
/>,
);
const input = screen.getByLabelText("Message input");
fireEvent.change(input, { target: { value: "#pricing", selectionStart: 8 } });
fireEvent.keyDown(input, { key: "Enter" });
expect(screen.getByTestId("composer-session-reference-pricing")).toBeInTheDocument();
rerender(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
sessions={[]}
/>,
);
expect(screen.queryByTestId("composer-session-reference-pricing")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("#pricing", undefined, undefined);
});
it("exposes mention suggestions as an aria-activedescendant combobox and ignores IME Enter", () => {
render(
<ThreadComposer
onSend={vi.fn()}
placeholder="Type your message..."
cliApps={CLI_APPS}
/>,
);
const input = screen.getByLabelText("Message input");
fireEvent.change(input, { target: { value: "@", selectionStart: 1 } });
const combobox = screen.getByRole("combobox", { name: "Message input" });
const listbox = screen.getByRole("listbox", { name: "Mentions" });
const firstOption = screen.getByRole("option", { name: /@gimp/i });
expect(combobox).toHaveAttribute("aria-expanded", "true");
expect(combobox).toHaveAttribute("aria-controls", listbox.id);
expect(combobox).toHaveAttribute("aria-activedescendant", firstOption.id);
expect(firstOption).toHaveAttribute("tabindex", "-1");
fireEvent.keyDown(input, { key: "Enter", isComposing: true });
expect(input).toHaveValue("@");
expect(listbox).toBeInTheDocument();
fireEvent.keyDown(input, { key: "ArrowDown" });
const secondOption = screen.getByRole("option", { name: /@blender/i });
expect(combobox).toHaveAttribute("aria-activedescendant", secondOption.id);
});
it("keeps combobox semantics when the mention popup is closed", () => {
render(<ThreadComposer onSend={vi.fn()} placeholder="Type your message..." />);
const input = screen.getByRole("combobox", { name: "Message input" });
expect(input).toHaveAttribute("aria-autocomplete", "list");
expect(input).toHaveAttribute("aria-expanded", "false");
expect(input).not.toHaveAttribute("aria-controls");
expect(input).not.toHaveAttribute("aria-activedescendant");
});
it("turns a dropped sidebar session into the shared structured mention", () => {
const onSend = vi.fn();
render(
@@ -1782,7 +1986,7 @@ describe("ThreadComposer", () => {
expect(input).toHaveValue("Compare notes");
expect(screen.getByTestId("composer-session-drag-preview"))
.toHaveTextContent("@收费设计");
.toHaveTextContent("#收费设计");
fireEvent.dragEnd(document);
expect(screen.queryByTestId("composer-session-drag-preview")).not.toBeInTheDocument();
@@ -1792,13 +1996,13 @@ describe("ThreadComposer", () => {
fireEvent.drop(input, { dataTransfer });
expect(input).toHaveValue("Compare @收费设计 notes");
expect(input).toHaveValue("Compare #收费设计 notes");
expect(screen.queryByTestId("composer-session-drag-preview")).not.toBeInTheDocument();
expect(screen.getByTestId("composer-session-mention-收费设计"))
.toHaveTextContent("@收费设计");
expect(screen.getByTestId("composer-session-reference-收费设计"))
.toHaveTextContent("#收费设计");
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("Compare @收费设计 notes", undefined, {
expect(onSend).toHaveBeenCalledWith("Compare #收费设计 notes", undefined, {
sessionMentions: [{
name: "收费设计",
session_key: "websocket:pricing",
@@ -1829,17 +2033,19 @@ describe("ThreadComposer", () => {
expect(screen.queryByTestId("composer-session-drag-preview")).not.toBeInTheDocument();
});
it("disambiguates duplicate and capability-colliding session names", () => {
it("uses stable handle identities without exposing session titles", () => {
const handles = [
session("a", "First planning title", "", "Plan"),
session("b", "Second planning title", "", "Plan-2"),
session("blender-chat", "3D notes", "", "Blender"),
];
render(
<ThreadComposer
onSend={vi.fn()}
placeholder="Type your message..."
cliApps={CLI_APPS}
mcpPresets={MCP_PRESETS}
sessions={[
...["a", "b"].map((chatId) => session(chatId, "Plan")),
session("blender-chat", "Blender", "3D notes"),
]}
handleSessions={handles}
/>,
);
@@ -1849,18 +2055,130 @@ describe("ThreadComposer", () => {
const palette = screen.getByRole("listbox", { name: "Mentions" });
expect(within(palette).getAllByRole("group").map((group) => (
group.getAttribute("aria-label")
))).toEqual(["CLI apps", "MCP services", "Nanobot conversations"]);
const options = screen.getAllByRole("option", { name: /Plan @Plan/i });
expect(options.map((option) => option.textContent)).toEqual([
expect.stringContaining("@Plan"),
expect.stringContaining("@Plan-chat"),
]);
expect(screen.getByRole("group", { name: "Nanobot conversations" })).toBeInTheDocument();
))).toEqual(["Nanobot conversations", "CLI apps", "MCP services"]);
const firstSession = screen.getByRole("option", { name: /^@Plan$/i });
expect(firstSession).toHaveAttribute("aria-selected", "true");
expect(input).toHaveAttribute("aria-activedescendant", firstSession.id);
expect(screen.getByRole("option", { name: /^@Plan-2$/i }))
.toBeInTheDocument();
expect(screen.getByRole("group", { name: "Nanobot conversations" }))
.toBeInTheDocument();
expect(screen.getByRole("group", { name: "CLI apps" })).toBeInTheDocument();
expect(screen.getByRole("option", { name: /Blender @Blender-chat Reference/i }))
expect(screen.getByRole("option", { name: /^@Blender$/i }))
.toBeInTheDocument();
expect(screen.getByRole("option", { name: /Blender @blender Use/i }))
.toBeInTheDocument();
expect(screen.queryByText("First planning title")).not.toBeInTheDocument();
expect(screen.queryByText("Second planning title")).not.toBeInTheDocument();
});
it("binds every same-name occurrence to one selected namespace across queue replay", () => {
const onSend = vi.fn();
const sameNameSession = session("blender-handle", "Session title", "", "blender");
render(
<ThreadComposer
onSend={onSend}
onStop={vi.fn()}
isStreaming
placeholder="Type your message..."
cliApps={CLI_APPS}
handleSessions={[sameNameSession]}
/>,
);
const input = screen.getByRole("combobox", { name: "Message input" });
fireEvent.change(input, { target: { value: "@blend", selectionStart: 6 } });
fireEvent.keyDown(input, { key: "Enter" });
expect(screen.getByTestId("composer-handle-mention-blender")).toBeInTheDocument();
const withSecondOccurrence = "@blender then @blender";
fireEvent.change(input, {
target: { value: withSecondOccurrence, selectionStart: withSecondOccurrence.length },
});
expect(screen.getAllByTestId("composer-handle-mention-blender")).toHaveLength(2);
input.setSelectionRange("@blender then ".length, withSecondOccurrence.length);
fireEvent.select(input);
fireEvent.change(input, {
target: { value: "@blender then @blend", selectionStart: 20 },
});
const cliOption = screen.getByRole("option", { name: /Blender @blender .* CLI/i });
fireEvent.mouseDown(cliOption);
expect(screen.getAllByTestId("composer-cli-mention-blender")).toHaveLength(2);
expect(screen.queryByTestId("composer-handle-mention-blender")).not.toBeInTheDocument();
fireEvent.keyDown(input, { key: "Enter" });
fireEvent.keyDown(input, { key: "Enter" });
expect(onSend).toHaveBeenCalledWith("@blender then @blender", undefined, {
cliApps: [expect.objectContaining({ name: "blender" })],
continueActiveTurn: true,
});
});
it("does not reinterpret a disappeared handle as a same-name CLI app", () => {
const onSend = vi.fn();
const handle = session("blender-handle", "Session title", "", "blender");
const { rerender } = render(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
cliApps={CLI_APPS}
handleSessions={[handle]}
/>,
);
const input = screen.getByRole("combobox", { name: "Message input" });
fireEvent.change(input, { target: { value: "@blend", selectionStart: 6 } });
fireEvent.keyDown(input, { key: "Tab" });
expect(screen.getByTestId("composer-handle-mention-blender")).toBeInTheDocument();
rerender(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
cliApps={CLI_APPS}
handleSessions={[]}
/>,
);
expect(screen.queryByTestId("composer-handle-mention-blender")).not.toBeInTheDocument();
expect(screen.queryByTestId("composer-cli-mention-blender")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("@blender", undefined, undefined);
});
it("supports a prototype-named MCP through live and queued mention parsing", () => {
const onSend = vi.fn();
const constructorPreset: McpPresetInfo = {
...MCP_PRESETS[0],
name: "constructor",
display_name: "Constructor",
};
render(
<ThreadComposer
onSend={onSend}
onStop={vi.fn()}
isStreaming
placeholder="Type your message..."
mcpPresets={[constructorPreset]}
/>,
);
const input = screen.getByRole("combobox", { name: "Message input" });
fireEvent.change(input, {
target: { value: "use @constructor", selectionStart: 16 },
});
expect(screen.getByTestId("composer-mcp-mention-constructor")).toBeInTheDocument();
fireEvent.keyDown(input, { key: "Enter" });
fireEvent.keyDown(input, { key: "Enter" });
expect(screen.getByText("use @constructor")).toBeInTheDocument();
fireEvent.keyDown(input, { key: "Enter" });
expect(onSend).toHaveBeenCalledWith("use @constructor", undefined, {
mcpPresets: [expect.objectContaining({ name: "constructor" })],
continueActiveTurn: true,
});
});
it("releases the eight-session limit when a mention is removed", () => {
@@ -1878,11 +2196,21 @@ describe("ThreadComposer", () => {
const input = screen.getByLabelText("Message input") as HTMLTextAreaElement;
for (let index = 0; index < 8; index += 1) {
const value = `${input.value}${input.value ? " " : ""}@Topic${index}`;
const value = `${input.value}${input.value ? " " : ""}#Topic${index}`;
input.setSelectionRange(input.value.length, input.value.length);
fireEvent.select(input);
fireEvent.change(input, { target: { value, selectionStart: value.length } });
fireEvent.keyDown(input, { key: "Tab" });
}
const replacement = `${input.value.replace("@Topic0 ", "")} @Topic8`;
const withoutFirst = input.value.replace("#Topic0 ", "");
input.setSelectionRange(0, "#Topic0 ".length);
fireEvent.select(input);
fireEvent.change(input, {
target: { value: withoutFirst, selectionStart: 0 },
});
const replacement = `${withoutFirst}#Topic8`;
input.setSelectionRange(withoutFirst.length, withoutFirst.length);
fireEvent.select(input);
fireEvent.change(input, {
target: { value: replacement, selectionStart: replacement.length },
});
@@ -1896,7 +2224,7 @@ describe("ThreadComposer", () => {
))).toEqual(expect.arrayContaining(["websocket:topic-8"]));
});
it("keeps a selected session stable across refreshes and queued guidance", () => {
it("keeps a selected handle mention when queuing guidance for the active turn", () => {
const onSend = vi.fn();
const target = session("z-target", "Plan", "Original plan");
const { rerender } = render(
@@ -1905,7 +2233,7 @@ describe("ThreadComposer", () => {
onStop={vi.fn()}
isStreaming
placeholder="Type your message..."
sessions={[target]}
handleSessions={[target]}
/>,
);
@@ -1919,22 +2247,26 @@ describe("ThreadComposer", () => {
onStop={vi.fn()}
isStreaming
placeholder="Type your message..."
sessions={[
handleSessions={[
{ ...target, title: "Renamed plan" },
session("a-new", "Plan", target.preview),
session("a-new", "Another title", target.preview, "Other"),
]}
/>,
);
expect(screen.getByTestId("composer-session-mention-Plan")).toHaveTextContent("@Plan");
expect(screen.getByTestId("composer-handle-mention-Plan")).toHaveTextContent("@Plan");
fireEvent.keyDown(input, { key: "Enter" });
expect(
within(screen.getByRole("group", { name: "Queued guidance" })).getByText("@Plan"),
).toBeInTheDocument();
fireEvent.keyDown(input, { key: "Enter" });
fireEvent.click(screen.getByRole("button", { name: "Guide" }));
expect(onSend).toHaveBeenCalledWith("@Plan", undefined, {
sessionMentions: [{
sessionHandles: [{
id: "handle_z-target",
name: "Plan",
session_key: "websocket:z-target",
title: "Plan",
color_slot: 2,
}],
continueActiveTurn: true,
});
@@ -1993,6 +2325,49 @@ describe("ThreadComposer", () => {
expect(input).toHaveValue(`please use $${skillName} `);
});
it("keeps a later session occurrence bound while completing an earlier skill", () => {
const onSend = vi.fn();
const skillName = "arxiv-intelligence-filter";
render(
<ThreadComposer
onSend={onSend}
placeholder="Type your message..."
sessions={[session("plan", "Plan")]}
skills={[{
name: skillName,
description: "Research papers",
source: "builtin",
enabled: true,
available: true,
}]}
/>,
);
const input = screen.getByLabelText("Message input") as HTMLTextAreaElement;
fireEvent.change(input, { target: { value: "#Pla", selectionStart: 4 } });
fireEvent.keyDown(input, { key: "Tab" });
expect(screen.getByTestId("composer-session-reference-Plan")).toBeInTheDocument();
input.setSelectionRange(0, 0);
fireEvent.select(input);
const withSkillQuery = `$arx ${input.value}`;
fireEvent.change(input, {
target: { value: withSkillQuery, selectionStart: 4, selectionEnd: 4 },
});
fireEvent.keyDown(input, { key: "Tab" });
expect(input).toHaveValue(`$${skillName} #Plan `);
expect(screen.getByTestId("composer-session-reference-Plan")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith(`$${skillName} #Plan`, undefined, {
sessionMentions: [{
name: "Plan",
session_key: "websocket:plan",
title: "Plan",
}],
});
});
it("ranks skill name matches ahead of earlier description matches", () => {
render(
<ThreadComposer
@@ -3043,6 +3418,38 @@ describe("ThreadComposer", () => {
});
});
it("migrates queued guidance from the v1 storage key without losing the prompt", async () => {
const legacyKey = "nanobot.webui.composerQueuedGuidance.v1:chat-a";
const currentKey = "nanobot.webui.composerQueuedGuidance.v2:chat-a";
window.localStorage.setItem(legacyKey, JSON.stringify([{
id: "legacy-guidance",
text: "keep this older queued prompt",
sessionMentions: [{
name: "old-handle",
session_key: "websocket:old-handle",
title: "Old handle",
}],
}]));
render(
<ThreadComposer
onSend={vi.fn()}
onStop={vi.fn()}
isStreaming
pendingQueueKey="chat-a"
placeholder="Type your message..."
/>,
);
expect(await screen.findByText("keep this older queued prompt")).toBeInTheDocument();
expect(window.localStorage.getItem(legacyKey)).toBeNull();
expect(JSON.parse(window.localStorage.getItem(currentKey) ?? "[]"))
.toEqual([expect.objectContaining({
id: "legacy-guidance",
text: "keep this older queued prompt",
})]);
});
it("keeps temporary chat guidance in memory only", async () => {
const onSend = vi.fn();
const view = render(
@@ -3062,7 +3469,7 @@ describe("ThreadComposer", () => {
expect(await screen.findByText("do not persist this")).toBeInTheDocument();
expect(
window.localStorage.getItem(
"nanobot.webui.composerQueuedGuidance.v1:temporary-private",
"nanobot.webui.composerQueuedGuidance.v2:temporary-private",
),
).toBeNull();
@@ -3082,7 +3489,7 @@ describe("ThreadComposer", () => {
});
expect(
window.localStorage.getItem(
"nanobot.webui.composerQueuedGuidance.v1:temporary-private",
"nanobot.webui.composerQueuedGuidance.v2:temporary-private",
),
).toBeNull();
});