GeezerVison is showing github.com — fix(provider-core): text generation context cuts no longer split an e… · pingdotgg/t3code@2ad6712
File treeExpand file treeCollapse file tree Open diff view settings packages/provider-core/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/provider-core/src/server/textGenerationUtils.test.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { describe, expect, it } from "vite-plus/test"; | 2 | 2 |
| 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 | +}); | 4 | 10 |
| 5 | 11 | describe("sanitizeCommitSubject", () => { | 6 | 12 | // regression: #19181 truncation split grapheme clusters at the subject limit | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-core/src/server/textGenerationUtils.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,9 @@ export function toJsonSchemaObject(schema: Schema.Top): unknown { | 19 | 19 |
| 20 | 20 | export function limitSection(value: string, maxChars: number): string { | 21 | 21 | 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); | 23 | 25 | return `${truncated}\n\n[truncated]`; | 24 | 26 | } | 25 | 27 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-core/src/server/threadTitleContext.test.ts +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { describe, expect, it } from "vite-plus/test"; | 2 | 2 | import { ChatAttachmentId } from "@t3tools/contracts"; | 3 | 3 | import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; | 4 | | -import { formatThreadTitleContext } from "./threadTitleContext.ts"; | | 4 | +import { formatThreadTitleContext, limitTitleMessage } from "./threadTitleContext.ts"; | 5 | 5 |
| 6 | 6 | describe("thread title context", () => { | 7 | 7 | it.each(["", "Ultrathink:\n"])("keeps attachments without bootstrap text (%j)", (prefix) => { | @@ -54,4 +54,9 @@ describe("thread title context", () => { | 54 | 54 | expect(result.message).not.toContain("System instructions"); | 55 | 55 | expect(result.message.match(/USER:/g)).toHaveLength(1); | 56 | 56 | }); | | 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 | + }); | 57 | 62 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-core/src/server/threadTitleContext.ts +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,8 +17,13 @@ export function limitTitleMessage(text: string, budget: number): string { | 17 | 17 | if (text.length <= budget) return text; | 18 | 18 | if (budget <= TRUNCATED.length) return ""; | 19 | 19 | 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; | 22 | 27 | return `${text.slice(0, head)}${TRUNCATED}${tail > 0 ? text.slice(-tail) : ""}`; | 23 | 28 | } | 24 | 29 |
| |
|