Skip to content

Commit 9cf6328

Browse files
committed
fix(discord): keep voice log previews UTF-16 safe
1 parent 1c46fe7 commit 9cf6328

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

extensions/discord/src/voice/log-preview.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ describe("formatVoiceLogPreview", () => {
1010
const preview = formatVoiceLogPreview("x".repeat(501));
1111
expect(preview).toBe(`${"x".repeat(500)}...`);
1212
});
13+
14+
it("does not split emoji when the preview limit lands inside a surrogate pair", () => {
15+
const preview = formatVoiceLogPreview(`${"x".repeat(499)}😀tail`);
16+
expect(preview).toBe(`${"x".repeat(499)}...`);
17+
expect(preview).not.toMatch(/[\uD800-\uDFFF]/u);
18+
});
1319
});
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
2+
13
const DISCORD_VOICE_LOG_PREVIEW_CHARS = 500;
24

35
export function formatVoiceLogPreview(text: string): string {
46
const oneLine = text.replace(/\s+/g, " ").trim();
57
if (oneLine.length <= DISCORD_VOICE_LOG_PREVIEW_CHARS) {
68
return oneLine;
79
}
8-
return `${oneLine.slice(0, DISCORD_VOICE_LOG_PREVIEW_CHARS)}...`;
10+
return `${truncateUtf16Safe(oneLine, DISCORD_VOICE_LOG_PREVIEW_CHARS)}...`;
911
}

0 commit comments

Comments
 (0)