Skip to content

Commit 9fb9dad

Browse files
committed
fix(beam): clip mirrored session text without splitting surrogate pairs
1 parent ac532e1 commit 9fb9dad

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎extensions/beam/src/mirror.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ describe("buildBeamMirrorItems", () => {
189189
const reduced = buildBeamMirrorItems([{ type: "userMessage", text: "x".repeat(10_000) }]);
190190
expect(reduced.items[0]?.text.length).toBe(6_000);
191191
});
192+
193+
it("does not split a surrogate pair when clipping message text", () => {
194+
const reduced = buildBeamMirrorItems([
195+
{ type: "userMessage", text: `${"x".repeat(5_999)}🙂tail` },
196+
]);
197+
expect(reduced.items[0]?.text).toBe("x".repeat(5_999));
198+
});
192199
});
193200

194201
describe("fitBeamMirrorUpload", () => {
@@ -248,6 +255,24 @@ describe("createBeamMirrorRunner", () => {
248255
expect(cancel).toHaveBeenCalledOnce();
249256
});
250257

258+
it("does not split a surrogate pair when clipping the session title", async () => {
259+
const sent: SentRequest[] = [];
260+
const catalog = fakeCatalog({
261+
id: "claude",
262+
sessions: [{ threadId: "t-emoji", name: `${"x".repeat(159)}🙂`, recencyAt: NOW - 60_000 }],
263+
});
264+
const runner = createBeamMirrorRunner({
265+
runtime: fakeRuntime(mirrorConfig()),
266+
logger: silentLogger,
267+
fetchFn: captureFetch(sent),
268+
now: () => NOW,
269+
listCatalogs: () => [catalog],
270+
});
271+
await runner.tick();
272+
expect(sent).toHaveLength(1);
273+
expect(sent[0]?.payload.title).toBe("x".repeat(159));
274+
});
275+
251276
it("keeps successful uploads successful when response cancellation rejects", async () => {
252277
const sent: SentRequest[] = [];
253278
const warnings: string[] = [];

‎extensions/beam/src/mirror.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type ActiveSessionCatalog,
1212
} from "openclaw/plugin-sdk/session-catalog-runtime";
1313
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
14+
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
1415
import { BEAM_MAX_BODY_BYTES, BEAM_MAX_ITEM_CHARS, BEAM_MAX_ITEMS } from "./types.js";
1516

1617
const MIRROR_CONFIG_PATH = "plugins.entries.beam.config.mirror";
@@ -134,7 +135,7 @@ export function beamMirrorId(catalogId: string, hostId: string, threadId: string
134135
}
135136

136137
function clipText(text: string): string {
137-
return text.length > BEAM_MAX_ITEM_CHARS ? text.slice(0, BEAM_MAX_ITEM_CHARS) : text;
138+
return truncateUtf16Safe(text, BEAM_MAX_ITEM_CHARS);
138139
}
139140

140141
function droppedSummary(counts: Map<string, number>): string | undefined {
@@ -324,7 +325,7 @@ export function createBeamMirrorRunner(params: {
324325
version: 1,
325326
beamId: beamMirrorId(candidate.catalogId, candidate.hostId, candidate.threadId),
326327
source: candidate.catalogId,
327-
title: candidate.title.slice(0, 160),
328+
title: truncateUtf16Safe(candidate.title, 160),
328329
updatedAt: new Date(candidate.recencyAt || now()).toISOString(),
329330
completed,
330331
items,

0 commit comments

Comments
 (0)