Skip to content

Commit 2f92c17

Browse files
committed
fix(ui): bound realtime Talk conversation text (#117581)
* commit '07822aaefdb249c2678bea03cca6c8db4863a650': fix(ui): preserve Talk transcript surrogate bounds fix(ui): harden Talk transcript marker bounds test(ui): cover bounded realtime Talk entries fix(ui): bound realtime Talk conversation text
2 parents 165c968 + 07822aa commit 2f92c17

2 files changed

Lines changed: 165 additions & 2 deletions

File tree

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

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,137 @@ describe("realtime Talk conversation", () => {
128128
]);
129129
});
130130

131+
it("bounds streamed assistant delta growth while retaining useful context", () => {
132+
let state = createRealtimeTalkConversationState();
133+
const opening = "Opening context stays visible. ";
134+
135+
state = updateRealtimeTalkConversation(state, {
136+
role: "assistant",
137+
text: `${opening}${"a".repeat(7_900)}`,
138+
final: false,
139+
nowMs: 1,
140+
});
141+
state = updateRealtimeTalkConversation(state, {
142+
role: "assistant",
143+
text: "b".repeat(500),
144+
final: false,
145+
nowMs: 2,
146+
});
147+
state = updateRealtimeTalkConversation(state, {
148+
role: "assistant",
149+
text: `${"c".repeat(500)}NEWEST`,
150+
final: false,
151+
nowMs: 3,
152+
});
153+
154+
expect(state.entries[0]?.text.length).toBeLessThanOrEqual(8_000);
155+
expect(state.entries[0]?.text.startsWith(opening)).toBe(true);
156+
expect(state.entries[0]?.text).toContain("\n…\n");
157+
expect(state.entries[0]?.text.split("\n…\n")).toHaveLength(2);
158+
expect(state.entries[0]?.text.endsWith("NEWEST")).toBe(true);
159+
});
160+
161+
it("replaces a bounded assistant stream with the authoritative final transcript", () => {
162+
let state = createRealtimeTalkConversationState();
163+
const opening = "Original opening context. ";
164+
165+
state = updateRealtimeTalkConversation(state, {
166+
role: "assistant",
167+
text: `${opening}${"draft ".repeat(1_600)}`,
168+
final: false,
169+
nowMs: 1,
170+
});
171+
expect(state.entries[0]?.text).toContain("\n…\n");
172+
173+
state = updateRealtimeTalkConversation(state, {
174+
role: "assistant",
175+
text: `${opening}corrected ${"final ".repeat(1_600)}DONE`,
176+
final: true,
177+
nowMs: 2,
178+
});
179+
180+
expect(state.entries[0]?.text.length).toBeLessThanOrEqual(8_000);
181+
expect(state.entries[0]?.text.startsWith(`${opening}corrected `)).toBe(true);
182+
expect(state.entries[0]?.text).not.toContain("draft ");
183+
expect(state.entries[0]?.text.endsWith("DONE")).toBe(true);
184+
expect(state.entries[0]?.isStreaming).toBe(false);
185+
});
186+
187+
it("does not expose dangling surrogates at a bounded transcript edge", () => {
188+
let state = createRealtimeTalkConversationState();
189+
const transcript = `${"a".repeat(8_000)}🚀${"b".repeat(7_740)}`;
190+
191+
state = updateRealtimeTalkConversation(state, {
192+
role: "assistant",
193+
text: transcript,
194+
final: true,
195+
nowMs: 1,
196+
});
197+
198+
const text = state.entries[0]?.text ?? "";
199+
expect(text.length).toBeLessThanOrEqual(8_000);
200+
expect(text).not.toMatch(
201+
/(?:[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF])/,
202+
);
203+
});
204+
205+
it("does not trust a natural truncation marker outside the bounded prefix", () => {
206+
let state = createRealtimeTalkConversationState();
207+
208+
state = updateRealtimeTalkConversation(state, {
209+
role: "assistant",
210+
text: `${"a".repeat(7_998)}\n…\n${"b".repeat(500)}NEWEST`,
211+
final: true,
212+
nowMs: 1,
213+
});
214+
215+
expect(state.entries[0]?.text.length).toBeLessThanOrEqual(8_000);
216+
expect(state.entries[0]?.text.startsWith("a".repeat(256))).toBe(true);
217+
expect(state.entries[0]?.text.endsWith("NEWEST")).toBe(true);
218+
});
219+
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+
243+
it.each(["user", "assistant"] as const)(
244+
"bounds oversized final %s entries while retaining the newest text",
245+
(role) => {
246+
let state = createRealtimeTalkConversationState();
247+
248+
state = updateRealtimeTalkConversation(state, {
249+
role,
250+
text: `Useful opening. ${"x".repeat(9_000)}NEWEST`,
251+
final: true,
252+
nowMs: 1,
253+
});
254+
255+
expect(state.entries[0]?.text.length).toBeLessThanOrEqual(8_000);
256+
expect(state.entries[0]?.text.startsWith("Useful opening. ")).toBe(true);
257+
expect(state.entries[0]?.text.endsWith("NEWEST")).toBe(true);
258+
expect(state.entries[0]?.isStreaming).toBe(false);
259+
},
260+
);
261+
131262
it("keeps alternating realtime turns as separate bubbles", () => {
132263
let state = createRealtimeTalkConversationState();
133264

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Control UI chat module implements realtime talk conversation behavior.
2+
import { sliceUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
3+
24
type RealtimeTalkConversationRole = "user" | "assistant";
35

46
export type RealtimeTalkConversationEntry = {
@@ -25,6 +27,9 @@ type RealtimeTalkTranscriptUpdate = {
2527
};
2628

2729
const MAX_CONVERSATION_ENTRIES = 60;
30+
const MAX_CONVERSATION_ENTRY_CHARS = 8_000;
31+
const CONVERSATION_ENTRY_PREFIX_CHARS = 256;
32+
const CONVERSATION_ENTRY_TRUNCATION_MARKER = "\n…\n";
2833
const USER_FINAL_REWRITE_GRACE_MS = 1_500;
2934

3035
export function createRealtimeTalkConversationState(): RealtimeTalkConversationState {
@@ -96,7 +101,12 @@ function upsertRealtimeConversationEntry(
96101
const id = `rt-${state.nextEntryId}`;
97102
const entries = [
98103
...state.entries,
99-
{ id, role, text: text.trimStart(), isStreaming: !isFinal },
104+
{
105+
id,
106+
role,
107+
text: boundRealtimeConversationText(text.trimStart()),
108+
isStreaming: !isFinal,
109+
},
100110
].slice(-MAX_CONVERSATION_ENTRIES);
101111
return rememberRealtimeConversationEntry(
102112
{ ...state, entries, nextEntryId: state.nextEntryId + 1 },
@@ -115,10 +125,11 @@ function upsertRealtimeConversationEntry(
115125
if (!entry) {
116126
return upsertRealtimeConversationEntry(state, role, null, text, isFinal, nowMs);
117127
}
118-
const updatedText =
128+
const mergedText =
119129
role === "assistant"
120130
? mergeAssistantTranscriptText(entry.text, text, isFinal)
121131
: mergeRealtimeTranscriptText(entry.text, text, isFinal);
132+
const updatedText = boundRealtimeConversationText(mergedText);
122133
const entries =
123134
entry.text === updatedText && entry.isStreaming === !isFinal
124135
? state.entries
@@ -263,6 +274,27 @@ function mergeRealtimeTranscriptText(existing: string, incoming: string, isFinal
263274
return `${existing}${separator}${suffix}`;
264275
}
265276

277+
function boundRealtimeConversationText(text: string): string {
278+
if (text.length <= MAX_CONVERSATION_ENTRY_CHARS) {
279+
return text;
280+
}
281+
// Keep the opening context for late full-final replacement detection and
282+
// the newest tail for the visible conversation. Reuse the original prefix
283+
// so repeated streaming deltas do not move the truncation boundary.
284+
const markerIndex = text.indexOf(CONVERSATION_ENTRY_TRUNCATION_MARKER);
285+
const hasBoundedPrefix =
286+
markerIndex >= CONVERSATION_ENTRY_PREFIX_CHARS - 1 &&
287+
markerIndex <= 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]$/, "");
292+
const tailChars =
293+
MAX_CONVERSATION_ENTRY_CHARS - prefix.length - CONVERSATION_ENTRY_TRUNCATION_MARKER.length;
294+
const tail = sliceUtf16Safe(text, -tailChars);
295+
return `${prefix}${CONVERSATION_ENTRY_TRUNCATION_MARKER}${tail}`;
296+
}
297+
266298
function looksLikeTranscriptReplacement(existing: string, incoming: string): boolean {
267299
const existingWords = transcriptWords(existing);
268300
const incomingWords = transcriptWords(incoming);

0 commit comments

Comments
 (0)