mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-16 21:11:36 +00:00
fix(gateway): keep live chat assistant buffer tail truncation UTF-16 safe (#102484)
* fix(gateway): keep live chat assistant buffer tail truncation UTF-16 safe * test(gateway): cover UTF-16 tail cap through merge path --------- Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { sliceUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
|
||||
// Gateway live chat projector.
|
||||
// Converts streaming assistant events into display-safe live chat text.
|
||||
import { stripInternalRuntimeContext } from "../agents/internal-runtime-context.js";
|
||||
@@ -36,7 +37,7 @@ function capLiveAssistantBuffer(text: string): string {
|
||||
if (text.length <= MAX_LIVE_CHAT_BUFFER_CHARS) {
|
||||
return text;
|
||||
}
|
||||
return text.slice(-MAX_LIVE_CHAT_BUFFER_CHARS);
|
||||
return sliceUtf16Safe(text, -MAX_LIVE_CHAT_BUFFER_CHARS);
|
||||
}
|
||||
|
||||
/** Merges assistant full-text and delta events into a capped live buffer. */
|
||||
|
||||
@@ -93,4 +93,15 @@ describe("server chat stream text merge", () => {
|
||||
expect(result).toHaveLength(MAX_LIVE_CHAT_BUFFER_CHARS);
|
||||
expect(result.endsWith("bbbb")).toBe(true);
|
||||
});
|
||||
|
||||
it("does not start the capped tail with the low half of a surrogate pair", () => {
|
||||
const safeTail = "y".repeat(MAX_LIVE_CHAT_BUFFER_CHARS - 1);
|
||||
const result = resolveMergedAssistantText({
|
||||
previousText: "",
|
||||
nextText: `x🚀${safeTail}`,
|
||||
nextDelta: "",
|
||||
});
|
||||
|
||||
expect(result).toBe(safeTail);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user