fix(telegram): keep draft preview chunks surrogate-safe

This commit is contained in:
杨浩宇0668001029
2026-06-25 09:10:07 -07:00
committed by Ayaan Zaidi
parent 8bc069f76f
commit 4bd68aef65
2 changed files with 16 additions and 1 deletions
@@ -819,6 +819,20 @@ describe("createTelegramDraftStream", () => {
expect(stream.lastDeliveredText?.()).toBe("1234567890");
});
it("does not split surrogate pairs when clamping a first oversized non-final preview", async () => {
const api = createMockDraftApi();
const stream = createDraftStream(api, { maxChars: 10 });
stream.update("123456789😀tail");
await stream.flush();
expect(api.sendMessage).toHaveBeenCalledTimes(1);
const sentText = requireSendMessageCallText(api, 0);
expect(sentText).toBe("123456789");
expect(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/.test(sentText)).toBe(false);
expect(stream.lastDeliveredText?.()).toBe("123456789");
});
it("finalizes overflow that was hidden by a clamped non-final preview", async () => {
const api = createMockDraftApi();
api.sendMessage
+2 -1
View File
@@ -5,6 +5,7 @@ import {
takeMessageIdAfterStop,
} from "openclaw/plugin-sdk/channel-outbound";
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
import { sliceUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
import { buildTelegramThreadParams, type TelegramThreadSpec } from "./bot/helpers.js";
import { renderTelegramHtmlText, telegramHtmlToPlainTextFallback } from "./format.js";
import {
@@ -169,7 +170,7 @@ function findTelegramDraftChunkLength(
high = mid - 1;
}
}
return best;
return sliceUtf16Safe(text, 0, best).length;
}
export function createTelegramDraftStream(params: {