Skip to content

Commit 07822aa

Browse files
committed
fix(ui): preserve Talk transcript surrogate bounds
1 parent 35799b6 commit 07822aa

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎ui/src/pages/chat/realtime-talk-conversation.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,29 @@ describe("realtime Talk conversation", () => {
217217
expect(state.entries[0]?.text.endsWith("NEWEST")).toBe(true);
218218
});
219219

220+
it.each([255, 256])(
221+
"does not retain a lone high surrogate before a natural marker at offset %i",
222+
(markerOffset) => {
223+
let state = createRealtimeTalkConversationState();
224+
const retainedText = "a".repeat(markerOffset - 1);
225+
226+
state = updateRealtimeTalkConversation(state, {
227+
role: "assistant",
228+
text: `${retainedText}\uD800\n…\n${"b".repeat(8_000)}NEWEST`,
229+
final: true,
230+
nowMs: 1,
231+
});
232+
233+
const text = state.entries[0]?.text ?? "";
234+
expect(text.length).toBeLessThanOrEqual(8_000);
235+
expect(text.startsWith(`${retainedText}\n…\n`)).toBe(true);
236+
expect(text.endsWith("NEWEST")).toBe(true);
237+
expect(text).not.toMatch(
238+
/(?:[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF])/,
239+
);
240+
},
241+
);
242+
220243
it.each(["user", "assistant"] as const)(
221244
"bounds oversized final %s entries while retaining the newest text",
222245
(role) => {

‎ui/src/pages/chat/realtime-talk-conversation.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,10 @@ function boundRealtimeConversationText(text: string): string {
285285
const hasBoundedPrefix =
286286
markerIndex >= CONVERSATION_ENTRY_PREFIX_CHARS - 1 &&
287287
markerIndex <= CONVERSATION_ENTRY_PREFIX_CHARS;
288-
const prefix = hasBoundedPrefix
289-
? text.slice(0, markerIndex)
290-
: sliceUtf16Safe(text, 0, CONVERSATION_ENTRY_PREFIX_CHARS);
288+
const prefixEnd = hasBoundedPrefix ? markerIndex : CONVERSATION_ENTRY_PREFIX_CHARS;
289+
// A natural marker can follow malformed provider text ending in a lone high
290+
// surrogate. Keep that code unit out of the retained truncation boundary.
291+
const prefix = sliceUtf16Safe(text, 0, prefixEnd).replace(/[\uD800-\uDBFF]$/, "");
291292
const tailChars =
292293
MAX_CONVERSATION_ENTRY_CHARS - prefix.length - CONVERSATION_ENTRY_TRUNCATION_MARKER.length;
293294
const tail = sliceUtf16Safe(text, -tailChars);

0 commit comments

Comments
 (0)