Skip to content

Commit 5e2a331

Browse files
authored
fix(server): preserve surrogate pairs in generated thread titles (#27483)
1 parent aac4c15 commit 5e2a331

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎packages/provider-core/src/server/textGenerationUtils.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { sanitizeCommitSubject } from "./textGenerationUtils.ts";
3+
import { sanitizeCommitSubject, sanitizeThreadTitle } from "./textGenerationUtils.ts";
44

55
describe("sanitizeCommitSubject", () => {
66
// regression: #19181 truncation split grapheme clusters at the subject limit
@@ -14,3 +14,16 @@ describe("sanitizeCommitSubject", () => {
1414
expect(sanitizeCommitSubject(`${prefix}${character} more`)).toBe(prefix);
1515
});
1616
});
17+
18+
describe("sanitizeThreadTitle", () => {
19+
it.each([
20+
{ name: "a split surrogate pair", prefixLength: 116, character: "🚀", retained: "" },
21+
{ name: "a complete surrogate pair", prefixLength: 115, character: "🚀", retained: "🚀" },
22+
{ name: "a BMP character", prefixLength: 116, character: "界", retained: "界" },
23+
])("truncates without splitting $name", ({ prefixLength, character, retained }) => {
24+
const prefix = "a".repeat(prefixLength);
25+
const title = `${prefix}${character} more`;
26+
27+
expect(sanitizeThreadTitle(title)).toBe(`${prefix}${retained}...`);
28+
});
29+
});

‎packages/provider-core/src/server/textGenerationUtils.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ export function sanitizeThreadTitle(raw: string): string {
7474
return normalized;
7575
}
7676

77-
return `${normalized.slice(0, MAX_THREAD_TITLE_CHARS - 3).trimEnd()}...`;
77+
let end = MAX_THREAD_TITLE_CHARS - 3;
78+
const previous = normalized.charCodeAt(end - 1);
79+
const next = normalized.charCodeAt(end);
80+
if (previous >= 0xd800 && previous <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
81+
end -= 1;
82+
}
83+
return `${normalized.slice(0, end).trimEnd()}...`;
7884
}
7985

8086
function cliLabel(cliName: string): string {

0 commit comments

Comments
 (0)