Skip to content

Commit 66a2f35

Browse files
authored
fix(shared): long assistant quote chips no longer end in a broken character (pingdotgg#30367)
1 parent 4821696 commit 66a2f35

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎packages/shared/src/assistantCitations.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type AssistantCitation,
1010
} from "@t3tools/contracts";
1111
import {
12+
assistantCitationLabel,
1213
assistantCitationsToPlainText,
1314
collectAssistantCitations,
1415
expandAssistantCitationsForProvider,
@@ -229,6 +230,12 @@ describe("assistant citation references", () => {
229230
);
230231
});
231232

233+
it("shortens a long quote label without splitting an emoji", () => {
234+
expect(assistantCitationLabel({ ...citation, text: `${"a".repeat(63)}😀 and more` })).toBe(
235+
`${"a".repeat(63)}…`,
236+
);
237+
});
238+
232239
it("replaces each marker once, including adjacent and repeated citations", () => {
233240
const second = { ...citation, text: "A second quote: $& $1 $$" };
234241
const marker = serializeAssistantCitation(citation);

‎packages/shared/src/assistantCitations.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ export function parseAssistantCitationHref(href: string): AssistantCitation | nu
9898

9999
export function assistantCitationLabel(citation: AssistantCitation): string {
100100
const preview = (citation.comment?.trim() || citation.text).replace(/\s+/g, " ");
101-
return preview.length > 64 ? `${preview.slice(0, 64)}…` : preview;
101+
if (preview.length <= 64) return preview;
102+
let end = 64;
103+
const previous = preview.charCodeAt(end - 1);
104+
const next = preview.charCodeAt(end);
105+
if (previous >= 0xd800 && previous <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
106+
end -= 1;
107+
}
108+
return `${preview.slice(0, end)}…`;
102109
}
103110

104111
export function serializeAssistantCitation(citation: AssistantCitation): string {

0 commit comments

Comments
 (0)