From 8857d4f9c8fc7c3ec853f28cf38838d8e9459638 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Tue, 27 Oct 2026 11:37:42 +0800 Subject: [PATCH] feat(webui): unify mention candidate display --- webui/browser/inline-references-fixture.tsx | 21 +++- webui/browser/inline-references.spec.ts | 103 +++++++++++++++++- .../src/components/thread/ThreadComposer.tsx | 73 ++++++------- webui/src/tests/thread-composer.test.tsx | 28 ++++- 4 files changed, 180 insertions(+), 45 deletions(-) diff --git a/webui/browser/inline-references-fixture.tsx b/webui/browser/inline-references-fixture.tsx index f249d15d5..aa39c80a5 100644 --- a/webui/browser/inline-references-fixture.tsx +++ b/webui/browser/inline-references-fixture.tsx @@ -4,13 +4,30 @@ import { MessageBubble } from "@/components/MessageBubble"; import { ThreadComposer } from "@/components/thread/ThreadComposer"; import { ThemeProvider, useTheme } from "@/hooks/useTheme"; import { useReadingTypography } from "@/hooks/useReadingTypography"; -import type { ChatSummary, SlashCommand, UIMessage } from "@/lib/types"; +import type { ChatSummary, CliAppInfo, McpPresetInfo, SlashCommand, UIMessage } from "@/lib/types"; const session: ChatSummary = { key: "websocket:typography", channel: "websocket", chatId: "typography", createdAt: null, updatedAt: null, title: "Reference target", preview: "", handle: { id: "handle_12345678901234567890123456789012", name: "typography" }, }; +const longSession: ChatSummary = { + ...session, key: "websocket:long-reference", chatId: "long-reference", title: "A longer conversation title", + handle: { id: "handle_abcdefabcdefabcdefabcdefabcdefab", name: "mimamimamimamima" }, +}; +const logo = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Crect width='20' height='20' fill='navy'/%3E%3C/svg%3E"; +const cliApps: CliAppInfo[] = [{ + name: "drawio", display_name: "Draw.io", category: "diagram", description: "Diagram editor", + requires: "", source: "fixture", entry_point: "drawio", install_supported: true, + installed: true, available: true, status: "installed", skill_installed: true, logo_url: logo, +}]; +const linearPreset: McpPresetInfo = { + name: "linear", display_name: "Linear", category: "productivity", description: "Issue tracker", + docs_url: "", transport: "streamableHttp", requires: "", note: "", install_supported: true, + installed: true, configured: true, available: true, status: "configured", required_fields: [], + connection_summary: "", logo_url: logo, +}; +const mcpPresets = [linearPreset, { ...linearPreset, name: "adobe-express-docs", display_name: "Adobe Express Developer" }]; const commands: SlashCommand[] = [{ command: "/goal", title: "Start a goal", description: "Start a sustained goal.", icon: "activity", lifecycle: "agent_turn_with_args", acceptsArgs: true, @@ -27,7 +44,7 @@ export function InlineReferencesFixture() { return
{[answer, ...sent].map(message => )} - setSent(current => [...current, { id: `sent-${current.length}`, role: "user", content: text, createdAt: current.length + 2, }, ...(options?.userShell ? [{ diff --git a/webui/browser/inline-references.spec.ts b/webui/browser/inline-references.spec.ts index e9ca9c2b0..d565a1361 100644 --- a/webui/browser/inline-references.spec.ts +++ b/webui/browser/inline-references.spec.ts @@ -54,8 +54,109 @@ async function expectMatchingInputLayers(input: Locator) { } } +async function minimumHandleContrast(handle: Locator): Promise { + return handle.evaluate(node => { + const canvas = document.createElement("canvas"); + canvas.width = canvas.height = 1; + const context = canvas.getContext("2d", { willReadFrequently: true })!; + const pixel = (color: string) => { + context.clearRect(0, 0, 1, 1); + context.fillStyle = color; + context.fillRect(0, 0, 1, 1); + return [...context.getImageData(0, 0, 1, 1).data]; + }; + const layers: number[][] = []; + for (let element = node.parentElement; element; element = element.parentElement) { + layers.push(pixel(getComputedStyle(element).backgroundColor)); + } + let background = [255, 255, 255]; + for (const [r, g, b, alpha] of layers.reverse()) { + const opacity = alpha / 255; + background = [r, g, b].map((channel, index) => channel * opacity + background[index] * (1 - opacity)); + } + const luminance = (rgb: number[]) => { + const linear = rgb.map(value => { + const channel = value / 255; + return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4; + }); + return 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2]; + }; + const style = getComputedStyle(node); + const lightness = style.getPropertyValue("--session-handle-lightness").trim(); + const chroma = style.getPropertyValue("--session-handle-chroma").trim(); + const backgroundLuminance = luminance(background); + return Math.min(...Array.from({ length: 360 }, (_, hue) => { + const foreground = luminance(pixel(`oklch(${lightness} ${chroma} ${hue})`).slice(0, 3)); + return (Math.max(foreground, backgroundLuminance) + 0.05) / (Math.min(foreground, backgroundLuminance) + 0.05); + })); + }); +} + +test("keeps a long app name readable in a minimum-width pane", async ({ page }) => { + await page.setViewportSize({ width: 200, height: 844 }); + await page.addInitScript(() => localStorage.setItem("nanobot.locale", "en")); + await page.goto("/browser/fixture.html?case=inline-references"); + const input = page.getByRole("textbox", { name: "Message input", exact: true }); + await input.fill("@adobe"); + const option = page.getByRole("option", { name: /@adobe-express-docs Adobe Express Developer/i }); + const label = option.getByText("Adobe Express Developer", { exact: true }); + await expect(label).toBeVisible(); + expect(await label.evaluate(node => node.clientWidth)).toBeGreaterThanOrEqual(64); + expect(await option.evaluate(node => node.scrollWidth <= node.clientWidth)).toBe(true); + await expect(option.locator("img")).toBeVisible(); + await expect(option.getByText("MCP", { exact: true })).toBeVisible(); +}); + for (const narrow of [false, true]) { for (const theme of ["light", "dark"]) { + test(`mention choices show colored session handles and retain app icons: ${narrow ? "narrow" : "desktop"}, ${theme}`, async ({ page }, info) => { + await page.setViewportSize(narrow ? { width: 320, height: 844 } : { width: 1440, height: 1000 }); + await page.addInitScript(theme => { + localStorage.setItem("nanobot.locale", "en"); + localStorage.setItem("nanobot-webui.theme", theme); + }, theme); + await page.goto("/browser/fixture.html?case=inline-references"); + const input = page.getByRole("textbox", { name: "Message input", exact: true }); + await input.fill("@"); + const conversation = page.getByRole("option", { name: /@typography Reference target/i }); + const app = page.getByRole("option", { name: /@drawio Draw.io/i }); + const mcp = page.getByRole("option", { name: /@linear Linear/i }); + const sessionHandle = conversation.locator(":scope > span").nth(0); + await expect(sessionHandle).toHaveText("@typography"); + await expect(conversation.locator(":scope > span").nth(1)).toHaveText("Reference target"); + await expect(conversation.locator('[data-product-icon="conversation"]')).toHaveCount(0); + const sessionColor = await sessionHandle.evaluate(node => getComputedStyle(node).color); + expect(await minimumHandleContrast(sessionHandle)).toBeGreaterThanOrEqual(4.5); + for (const [option, id, name] of [[app, "@drawio", "Draw.io"], [mcp, "@linear", "Linear"]] as const) { + await expect(option.locator(":scope > span").nth(0)).toHaveText(id); + await expect(option.locator(":scope > span").nth(1).locator("img, svg")).toBeVisible(); + await expect(option.getByText(name, { exact: true })).toBeVisible(); + } + const longHandle = page.getByRole("option", { name: /@mimamimamimamima/i }).getByText("@mimamimamimamima", { exact: true }); + await expect(longHandle).toBeVisible(); + expect(await longHandle.evaluate(node => node.scrollWidth <= node.clientWidth)).toBe(true); + const palette = page.getByRole("listbox", { name: "Mentions" }); + expect(await palette.evaluate(node => node.scrollWidth <= node.clientWidth)).toBe(true); + await page.screenshot({ path: info.outputPath("mention-order.png") }); + await input.fill("@adobe"); + const longApp = page.getByRole("option", { name: /@adobe-express-docs Adobe Express Developer/i }); + const longAppHandle = longApp.getByText("@adobe-express-docs", { exact: true }); + const longAppName = longApp.getByText("Adobe Express Developer", { exact: true }); + await expect(longAppHandle).toBeVisible(); + await expect(longAppName).toBeVisible(); + expect(await longAppHandle.evaluate(node => node.scrollWidth <= node.clientWidth)).toBe(true); + expect(await longAppName.evaluate(node => node.clientWidth)).toBeGreaterThanOrEqual(64); + expect(await longApp.evaluate(node => node.scrollWidth <= node.clientWidth)).toBe(true); + await page.screenshot({ path: info.outputPath("long-mention.png") }); + await input.fill("@typography "); + await expect(page.getByTestId("composer-session-mention-typography")).toHaveCSS("color", sessionColor); + await input.fill("@draw"); + await input.press("Tab"); + await expect(page.getByTestId("composer-cli-mention-drawio")).toHaveText("@\u00a0Draw.io"); + await expect(page.getByTestId("composer-cli-mention-logo-drawio")).toBeVisible(); + await expectMatchingInputLayers(input); + }); + test(`inline references preserve native editing: ${narrow ? "narrow" : "desktop"}, ${theme}`, async ({ page }, info) => { await page.setViewportSize(narrow ? { width: 390, height: 844 } : { width: 1440, height: 1000 }); await page.context().grantPermissions(["clipboard-read", "clipboard-write"]); @@ -175,7 +276,7 @@ for (const narrow of [false, true]) { expect(await output.textContent()).toBe(literal); await expect(output.locator("img, b")).toHaveCount(0); await page.getByRole("button", { name: "Copy code", exact: true }).click(); - await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(literal); + await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(literal.replaceAll("\n", EOL)); expect(errors).toEqual([]); }); } diff --git a/webui/src/components/thread/ThreadComposer.tsx b/webui/src/components/thread/ThreadComposer.tsx index 050026496..3c67a4f2f 100644 --- a/webui/src/components/thread/ThreadComposer.tsx +++ b/webui/src/components/thread/ThreadComposer.tsx @@ -15,7 +15,6 @@ import { MemoryIcon, HelpIcon, HistoryIcon, - ConversationIcon, AttachIcon, RestartIcon, RestrictedAccessIcon, @@ -3178,6 +3177,9 @@ function CliAppMentionPalette({ {group.items.map(({ candidate, index }) => { const selected = index === selectedIndex; const name = candidate.name; + const handleColor = candidate.kind === "session" + ? candidate.mention.id ? sessionHandleColor(candidate.mention.id) : INLINE_TOKEN_HIGHLIGHT_COLOR + : undefined; const typeLabel = candidate.kind === "cli" ? t("thread.composer.mentions.cliBadge") : candidate.kind === "mcp" @@ -3195,7 +3197,7 @@ function CliAppMentionPalette({ role="option" data-palette-index={index} aria-selected={selected} - aria-label={`${candidate.displayName} @${name} ${ariaDescription} ${typeLabel}`} + aria-label={`@${name} ${candidate.displayName} ${ariaDescription} ${typeLabel}`} onMouseEnter={() => onHover(index)} onMouseDown={(e) => { e.preventDefault(); @@ -3204,33 +3206,39 @@ function CliAppMentionPalette({ className={cn( floatingItemClassName, floatingItemFocusClassName, - "touch-target flex min-h-10 w-full items-center gap-2.5 px-2.5 py-1.5 text-left transition-colors", + "touch-target flex min-h-10 w-full flex-wrap items-center gap-x-2.5 gap-y-1 px-2.5 py-1.5 text-left transition-colors", selected - ? "bg-foreground/[0.055] text-foreground" + ? "bg-foreground/[0.055] text-foreground dark:bg-black/[0.12]" : "text-foreground/90 hover:bg-foreground/[0.04]", )} > - - - + + @{name} + + + {candidate.kind !== "session" ? : null} + {candidate.displayName} - - @{name} - + {candidate.kind !== "session" ? ( + + {typeLabel} + + ) : null} - {candidate.kind !== "session" ? ( - - {typeLabel} - - ) : null} ); })} @@ -3245,28 +3253,13 @@ function MentionCandidateLogo({ candidate, selected, }: { - candidate: MentionCandidate; + candidate: Extract; selected: boolean; }) { - const color = candidate.kind === "session" - ? candidate.mention.id - ? sessionHandleColor(candidate.mention.id) - : INLINE_TOKEN_HIGHLIGHT_COLOR - : candidate.brandColor || INLINE_TOKEN_HIGHLIGHT_COLOR; - const rawLogoUrl = candidate.kind === "session" ? null : candidate.logoUrl; - const logoUrls = useMemo(() => logoFallbackUrls(rawLogoUrl), [rawLogoUrl]); + const color = candidate.brandColor || INLINE_TOKEN_HIGHLIGHT_COLOR; + const logoUrls = useMemo(() => logoFallbackUrls(candidate.logoUrl), [candidate.logoUrl]); const { logoUrl, onLogoError, onLogoLoad } = useLogoFallback(logoUrls); - if (candidate.kind === "session") { - return ( - - - - ); - } if (logoUrl) { return ( { expect(options[0]).toHaveTextContent("Recent"); }); + it("shows session handles without an icon and preserves app icons after their identifiers", () => { + render(); + const input = screen.getByLabelText("Message input"); + fireEvent.change(input, { target: { value: "@", selectionStart: 1 } }); + + const conversation = screen.getByRole("option", { name: /@bime Pricing/i }); + const app = screen.getByRole("option", { name: /@gimp GIMP/i }); + const mcp = screen.getByRole("option", { name: /@browserbase Browserbase/i }); + expect(conversation.children[0]).toHaveTextContent("@bime"); + expect(conversation.children[1]).toHaveTextContent("Pricing"); + expect(conversation.querySelector('[data-product-icon="conversation"]')).not.toBeInTheDocument(); + expect(app.children[0]).toHaveTextContent("@gimp"); + expect(app.children[1].querySelector("img")).toHaveAttribute("src", CLI_APPS[0].logo_url); + expect(app.children[1]).toHaveTextContent("GIMP"); + expect(mcp.children[0]).toHaveTextContent("@browserbase"); + expect(mcp.children[1].querySelector("img")).toHaveAttribute("src", MCP_PRESETS[0].logo_url); + expect(mcp.children[1]).toHaveTextContent("Browserbase"); + }); + it("shows right-side source badges so users can distinguish CLI apps from MCP servers", () => { render( { expect(screen.queryByText("CLI Apps")).not.toBeInTheDocument(); expect(screen.queryByText("MCP servers")).not.toBeInTheDocument(); - const gimp = screen.getByRole("option", { name: /GIMP @gimp .* CLI/i }); - const browserbase = screen.getByRole("option", { name: /Browserbase @browserbase .* MCP/i }); + const gimp = screen.getByRole("option", { name: /@gimp GIMP .* CLI/i }); + const browserbase = screen.getByRole("option", { name: /@browserbase Browserbase .* MCP/i }); // Reuse the app's 44px coarse-pointer targets without changing desktop density. expect(gimp).toHaveClass("touch-target"); expect(browserbase).toHaveClass("touch-target");