Skip to content

Commit 4eb579c

Browse files
saphidclaude
andcommitted
fix(provider-core): text generation context cuts no longer split an emoji
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 30084f0 commit 4eb579c

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

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

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

3-
import { sanitizeCommitSubject, sanitizeThreadTitle } from "./textGenerationUtils.ts";
3+
import { limitSection, sanitizeCommitSubject, sanitizeThreadTitle } from "./textGenerationUtils.ts";
4+
5+
describe("limitSection", () => {
6+
it("cuts a long section without splitting an emoji", () => {
7+
expect(limitSection(`${"a".repeat(9)}🚀 more`, 10)).toBe(`${"a".repeat(9)}\n\n[truncated]`);
8+
});
9+
});
410

511
describe("sanitizeCommitSubject", () => {
612
// regression: #19181 truncation split grapheme clusters at the subject limit

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export function toJsonSchemaObject(schema: Schema.Top): unknown {
1919

2020
export function limitSection(value: string, maxChars: number): string {
2121
if (value.length <= maxChars) return value;
22-
const truncated = value.slice(0, maxChars);
22+
// Half an emoji serializes to a lone surrogate escape, which provider JSON parsers reject.
23+
const last = value.charCodeAt(maxChars - 1);
24+
const truncated = value.slice(0, last >= 0xd800 && last <= 0xdbff ? maxChars - 1 : maxChars);
2325
return `${truncated}\n\n[truncated]`;
2426
}
2527

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22
import { ChatAttachmentId } from "@t3tools/contracts";
33
import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";
4-
import { formatThreadTitleContext } from "./threadTitleContext.ts";
4+
import { formatThreadTitleContext, limitTitleMessage } from "./threadTitleContext.ts";
55

66
describe("thread title context", () => {
77
it.each(["", "Ultrathink:\n"])("keeps attachments without bootstrap text (%j)", (prefix) => {
@@ -54,4 +54,9 @@ describe("thread title context", () => {
5454
expect(result.message).not.toContain("System instructions");
5555
expect(result.message.match(/USER:/g)).toHaveLength(1);
5656
});
57+
58+
it("cuts both ends of a long message without splitting an emoji", () => {
59+
const result = limitTitleMessage(`${"a".repeat(9)}🚀${"b".repeat(20)}🚀${"c".repeat(9)}`, 41);
60+
expect(result).toBe(`${"a".repeat(9)}\n[Content truncated]\n${"c".repeat(9)}`);
61+
});
5762
});

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ export function limitTitleMessage(text: string, budget: number): string {
1717
if (text.length <= budget) return text;
1818
if (budget <= TRUNCATED.length) return "";
1919
const available = budget - TRUNCATED.length;
20-
const head = Math.ceil(available / 2);
21-
const tail = available - head;
20+
let head = Math.ceil(available / 2);
21+
let tail = available - head;
22+
// Half an emoji serializes to a lone surrogate escape, which provider JSON parsers reject.
23+
const last = text.charCodeAt(head - 1);
24+
if (last >= 0xd800 && last <= 0xdbff) head -= 1;
25+
const first = text.charCodeAt(text.length - tail);
26+
if (tail > 0 && first >= 0xdc00 && first <= 0xdfff) tail -= 1;
2227
return `${text.slice(0, head)}${TRUNCATED}${tail > 0 ? text.slice(-tail) : ""}`;
2328
}
2429

0 commit comments

Comments
 (0)