fix(matrix): truncate thread starter body on code-point boundaries (#… · openclaw/openclaw@289865b
Bartok9
·
2026-06-27
·
via Recent Commits to openclaw:main
File tree
extensions/matrix/src/matrix/monitor
| Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,23 @@ describe("matrix thread context", () => {
|
23 | 23 | ).toBe("Thread starter body"); |
24 | 24 | }); |
25 | 25 | |
| 26 | +it("truncates long thread starter bodies on code-point boundaries", () => { |
| 27 | +const summary = summarizeMatrixThreadStarterEvent({ |
| 28 | +event_id: "$root", |
| 29 | +sender: "@alice:example.org", |
| 30 | +type: "m.room.message", |
| 31 | +origin_server_ts: Date.now(), |
| 32 | +content: { |
| 33 | +msgtype: "m.text", |
| 34 | +// 496 "a" + astral emoji (surrogate pair at units 496-497) + tail. |
| 35 | +// A raw slice(0, 497) would cut the pair and leave a lone high surrogate. |
| 36 | +body: `${"a".repeat(496)}\u{1F600}bcd`, |
| 37 | +}, |
| 38 | +} as MatrixRawEvent); |
| 39 | +expect(summary).toBe(`${"a".repeat(496)}...`); |
| 40 | +expect(summary && /[\uD800-\uDFFF]/.test(summary)).toBe(false); |
| 41 | +}); |
| 42 | + |
26 | 43 | it("marks media-only thread starter events instead of returning bare filenames", () => { |
27 | 44 | expect( |
28 | 45 | summarizeMatrixThreadStarterEvent({ |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | // Matrix plugin module implements thread context behavior. |
| 2 | +import { sliceUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime"; |
2 | 3 | import type { MatrixClient } from "../sdk.js"; |
3 | 4 | import { summarizeMatrixMessageContextEvent, trimMatrixMaybeString } from "./context-summary.js"; |
4 | 5 | import type { MatrixRawEvent } from "./types.js"; |
@@ -17,7 +18,7 @@ function truncateThreadStarterBody(value: string): string {
|
17 | 18 | if (value.length <= MAX_THREAD_STARTER_BODY_LENGTH) { |
18 | 19 | return value; |
19 | 20 | } |
20 | | -return `${value.slice(0, MAX_THREAD_STARTER_BODY_LENGTH - 3)}...`; |
| 21 | +return `${sliceUtf16Safe(value, 0, MAX_THREAD_STARTER_BODY_LENGTH - 3)}...`; |
21 | 22 | } |
22 | 23 | |
23 | 24 | export function summarizeMatrixThreadStarterEvent(event: MatrixRawEvent): string | undefined { |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。