diff --git a/apps/web/src/components/chat/ComposerStashMenu.test.ts b/apps/web/src/components/chat/ComposerStashMenu.test.ts new file mode 100644 index 0000000000000..efabfc93c42dd --- /dev/null +++ b/apps/web/src/components/chat/ComposerStashMenu.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { stashEntrySnippet } from "./ComposerStashMenu"; + +const entry = (prompt: string) => ({ + id: "stash-1", + createdAt: "2026-10-01T00:00:00.000Z", + prompt, + attachments: [], + droppedImageNames: [], +}); + +describe("stashEntrySnippet", () => { + it("cuts a long prompt on a whole character", () => { + expect(stashEntrySnippet(entry(`${"a".repeat(89)}😀 and more`))).toBe(`${"a".repeat(89)}…`); + expect(stashEntrySnippet(entry(`${"a".repeat(88)}😀 and more`))).toBe(`${"a".repeat(88)}😀…`); + }); +}); diff --git a/apps/web/src/components/chat/ComposerStashMenu.tsx b/apps/web/src/components/chat/ComposerStashMenu.tsx index 938c42d32c8c9..15f19f0b3ed3f 100644 --- a/apps/web/src/components/chat/ComposerStashMenu.tsx +++ b/apps/web/src/components/chat/ComposerStashMenu.tsx @@ -13,10 +13,14 @@ function missingImageCount(entry: PromptStashEntry): number { return entry.droppedImageNames.length + (entry.unreadableImageNames?.length ?? 0); } -function stashEntrySnippet(entry: PromptStashEntry): string { +export function stashEntrySnippet(entry: PromptStashEntry): string { const trimmed = assistantCitationsToPlainText(entry.prompt).trim().replace(/\s+/g, " "); if (trimmed.length > 0) { - return trimmed.length > SNIPPET_MAX_CHARS ? `${trimmed.slice(0, SNIPPET_MAX_CHARS)}…` : trimmed; + if (trimmed.length <= SNIPPET_MAX_CHARS) return trimmed; + // Never end on a lone high surrogate, the first half of an emoji. + const last = trimmed.charCodeAt(SNIPPET_MAX_CHARS - 1); + const end = last >= 0xd800 && last <= 0xdbff ? SNIPPET_MAX_CHARS - 1 : SNIPPET_MAX_CHARS; + return `${trimmed.slice(0, end)}…`; } const imageCount = entry.attachments.length + entry.droppedImageNames.length; const fileCount = entry.files?.length ?? 0;