Skip to content
TypeScript

fix(shared): long assistant quote chips no longer end in a broken character (#30367)

spoukyii66a2f35711a3
diff --git a/packages/shared/src/assistantCitations.test.ts b/packages/shared/src/assistantCitations.test.ts
index 923ca2555a0..7300de72e9d 100644
--- a/packages/shared/src/assistantCitations.test.ts
+++ b/packages/shared/src/assistantCitations.test.ts
@@ -9,6 +9,7 @@ import {
type AssistantCitation,
} from "@t3tools/contracts";
import {
+ assistantCitationLabel,
assistantCitationsToPlainText,
collectAssistantCitations,
expandAssistantCitationsForProvider,
@@ -229,6 +230,12 @@ describe("assistant citation references", () => {
);
});
+ it("shortens a long quote label without splitting an emoji", () => {
+ expect(assistantCitationLabel({ ...citation, text: `${"a".repeat(63)}😀 and more` })).toBe(
+ `${"a".repeat(63)}…`,
+ );
+ });
+
it("replaces each marker once, including adjacent and repeated citations", () => {
const second = { ...citation, text: "A second quote: $& $1 $$" };
const marker = serializeAssistantCitation(citation);
diff --git a/packages/shared/src/assistantCitations.ts b/packages/shared/src/assistantCitations.ts
index 9af6a297067..64bb2b3904b 100644
--- a/packages/shared/src/assistantCitations.ts
+++ b/packages/shared/src/assistantCitations.ts
@@ -98,7 +98,14 @@ export function parseAssistantCitationHref(href: string): AssistantCitation | nu
export function assistantCitationLabel(citation: AssistantCitation): string {
const preview = (citation.comment?.trim() || citation.text).replace(/\s+/g, " ");
- return preview.length > 64 ? `${preview.slice(0, 64)}…` : preview;
+ if (preview.length <= 64) return preview;
+ let end = 64;
+ const previous = preview.charCodeAt(end - 1);
+ const next = preview.charCodeAt(end);
+ if (previous >= 0xd800 && previous <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
+ end -= 1;
+ }
+ return `${preview.slice(0, end)}…`;
}
export function serializeAssistantCitation(citation: AssistantCitation): string {