Skip to content

Commit b78730f

Browse files
committed
refactor(telegram): own raw log bounds in formatter
1 parent 96a77fd commit b78730f

3 files changed

Lines changed: 21 additions & 14 deletions

File tree

extensions/telegram/src/bot-core.raw-update-log.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Telegram tests cover bot core.raw update log plugin behavior.
22
import { describe, expect, it } from "vitest";
3-
import { stringifyTelegramRawUpdateForLog } from "./raw-update-log.js";
3+
import { formatTelegramRawUpdateForLog } from "./raw-update-log.js";
44

5-
describe("stringifyTelegramRawUpdateForLog", () => {
5+
describe("formatTelegramRawUpdateForLog", () => {
66
it("redacts private Telegram raw update fields before verbose logging", () => {
77
const update = {
88
update_id: 98765,
@@ -45,7 +45,7 @@ describe("stringifyTelegramRawUpdateForLog", () => {
4545
},
4646
};
4747

48-
const rawLog = stringifyTelegramRawUpdateForLog(update);
48+
const rawLog = formatTelegramRawUpdateForLog(update);
4949

5050
expect(rawLog).toContain('"update_id":98765');
5151
expect(rawLog).toContain('"message_id":44');
@@ -138,7 +138,7 @@ describe("stringifyTelegramRawUpdateForLog", () => {
138138
},
139139
};
140140

141-
const rawLog = stringifyTelegramRawUpdateForLog(update);
141+
const rawLog = formatTelegramRawUpdateForLog(update);
142142

143143
expect(rawLog).toContain('"update_id":45678');
144144
expect(rawLog).toContain('"message_id":99');
@@ -176,7 +176,7 @@ describe("stringifyTelegramRawUpdateForLog", () => {
176176

177177
it("truncates long raw update strings without splitting UTF-16 surrogate pairs", () => {
178178
const prefix = "a".repeat(499);
179-
const rawLog = stringifyTelegramRawUpdateForLog({
179+
const rawLog = formatTelegramRawUpdateForLog({
180180
update_id: 123,
181181
diagnostic: `${prefix}\uD83D\uDE80tail`,
182182
});
@@ -188,4 +188,12 @@ describe("stringifyTelegramRawUpdateForLog", () => {
188188
expect(rawLog).not.toContain("\\ud83d");
189189
expect(rawLog).not.toContain("\\ude80");
190190
});
191+
192+
it("truncates the complete raw update log without splitting surrogate pairs", () => {
193+
const keyPrefix = "x".repeat(7997);
194+
const rawLog = formatTelegramRawUpdateForLog({ [`${keyPrefix}😀tail`]: 1 });
195+
196+
expect(rawLog).toBe(`{"${keyPrefix}...`);
197+
expect(rawLog).not.toMatch(/[\uD800-\uDFFF]/u);
198+
});
191199
});

extensions/telegram/src/bot-core.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { resolveTextChunkLimit } from "openclaw/plugin-sdk/reply-chunking";
1818
import { DEFAULT_GROUP_HISTORY_LIMIT, type HistoryEntry } from "openclaw/plugin-sdk/reply-history";
1919
import { danger, logVerbose, shouldLogVerbose } from "openclaw/plugin-sdk/runtime-env";
2020
import { getChildLogger } from "openclaw/plugin-sdk/runtime-env";
21-
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
2221
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
2322
import { createNonExitingRuntime, type RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
2423
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
@@ -57,7 +56,7 @@ import {
5756
} from "./group-history-window.js";
5857
import { TELEGRAM_TEXT_CHUNK_LIMIT } from "./outbound-adapter.js";
5958
import { registerTelegramOutboundGroupHistoryRecorder } from "./outbound-message-context.js";
60-
import { stringifyTelegramRawUpdateForLog } from "./raw-update-log.js";
59+
import { formatTelegramRawUpdateForLog } from "./raw-update-log.js";
6160
import { TELEGRAM_RICH_TEXT_LIMIT } from "./rich-message.js";
6261
import { createTelegramSendChatActionHandler } from "./sendchataction-401-backoff.js";
6362
import { getTelegramSequentialKey } from "./sequential-key.js";
@@ -242,15 +241,11 @@ export function createTelegramBotCore(
242241
bot.use(botRuntime.sequentialize(getTelegramSequentialKey));
243242

244243
const rawUpdateLogger = createSubsystemLogger("gateway/channels/telegram/raw-update");
245-
const MAX_RAW_UPDATE_CHARS = 8000;
246244

247245
bot.use(async (ctx, next) => {
248246
if (shouldLogVerbose()) {
249247
try {
250-
const raw = stringifyTelegramRawUpdateForLog(ctx.update);
251-
const preview =
252-
raw.length > MAX_RAW_UPDATE_CHARS ? `${truncateUtf16Safe(raw, MAX_RAW_UPDATE_CHARS)}...` : raw;
253-
rawUpdateLogger.debug(`telegram update: ${preview}`);
248+
rawUpdateLogger.debug(`telegram update: ${formatTelegramRawUpdateForLog(ctx.update)}`);
254249
} catch (err) {
255250
rawUpdateLogger.debug(`telegram update log failed: ${String(err)}`);
256251
}

extensions/telegram/src/raw-update-log.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
33

44
const MAX_RAW_UPDATE_STRING = 500;
55
const MAX_RAW_UPDATE_ARRAY = 20;
6+
const MAX_RAW_UPDATE_CHARS = 8000;
67
const REDACTED_TELEGRAM_FIELD = "[redacted]";
78
const TELEGRAM_RAW_UPDATE_ALWAYS_REDACT_KEYS = new Set([
89
"added_to_attachment_menu",
@@ -74,7 +75,7 @@ function isTelegramUserObject(value: Record<string, unknown>): boolean {
7475
);
7576
}
7677

77-
export function stringifyTelegramRawUpdateForLog(update: unknown): string {
78+
export function formatTelegramRawUpdateForLog(update: unknown): string {
7879
const seen = new WeakSet<object>();
7980
const transform = (value: unknown, key = "", parentKey?: string): unknown => {
8081
if (shouldRedactTelegramRawUpdateValue(key, parentKey)) {
@@ -109,5 +110,8 @@ export function stringifyTelegramRawUpdateForLog(update: unknown): string {
109110
}
110111
return value;
111112
};
112-
return JSON.stringify(transform(update ?? null));
113+
const raw = JSON.stringify(transform(update ?? null));
114+
return raw.length > MAX_RAW_UPDATE_CHARS
115+
? `${truncateUtf16Safe(raw, MAX_RAW_UPDATE_CHARS)}...`
116+
: raw;
113117
}

0 commit comments

Comments
 (0)