Skip to content

Commit e72222e

Browse files
committed
fix(discord): keep error body summaries UTF-16 safe
1 parent 632efa2 commit e72222e

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Discord tests cover error body summary behavior.
2+
import { describe, expect, it } from "vitest";
3+
import { summarizeDiscordResponseBody } from "./error-body.js";
4+
5+
function hasLoneSurrogate(value: string): boolean {
6+
for (let index = 0; index < value.length; index += 1) {
7+
const code = value.charCodeAt(index);
8+
if (code >= 0xd800 && code <= 0xdbff) {
9+
const next = value.charCodeAt(index + 1);
10+
if (!(next >= 0xdc00 && next <= 0xdfff)) {
11+
return true;
12+
}
13+
index += 1;
14+
continue;
15+
}
16+
if (code >= 0xdc00 && code <= 0xdfff) {
17+
return true;
18+
}
19+
}
20+
return false;
21+
}
22+
23+
describe("summarizeDiscordResponseBody", () => {
24+
it("keeps truncated summaries on a UTF-16 boundary", () => {
25+
const summary = summarizeDiscordResponseBody(`${"a".repeat(239)}😀tail`);
26+
27+
expect(summary).toHaveLength(239);
28+
expect(hasLoneSurrogate(summary ?? "")).toBe(false);
29+
});
30+
});

extensions/discord/src/error-body.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Discord plugin module implements error body behavior.
2+
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
3+
24
const DISCORD_RESPONSE_BODY_SUMMARY_MAX_CHARS = 240;
35

46
export function summarizeDiscordResponseBody(
@@ -18,7 +20,7 @@ export function summarizeDiscordResponseBody(
1820
if (!summary) {
1921
return opts.emptyText;
2022
}
21-
return summary.slice(0, DISCORD_RESPONSE_BODY_SUMMARY_MAX_CHARS);
23+
return truncateUtf16Safe(summary, DISCORD_RESPONSE_BODY_SUMMARY_MAX_CHARS);
2224
}
2325

2426
export function isDiscordHtmlResponseBody(body: string, contentType?: string | null): boolean {

0 commit comments

Comments
 (0)