Skip to content

Commit 75830f0

Browse files
authored
fix(synology-chat): deliver long messages without webhook rejection (#116645)
* fix(synology-chat): chunk long messages before webhook delivery * test(synology-chat): expose chunk limit in channel mock
1 parent ea3b5df commit 75830f0

5 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎extensions/synology-chat/src/channel.test-mocks.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ vi.mock("openclaw/plugin-sdk/webhook-ingress", async () => {
149149
});
150150

151151
vi.mock("./client.js", () => ({
152+
SYNOLOGY_CHAT_TEXT_CHUNK_LIMIT: 2_000,
152153
sendMessage: vi.fn().mockResolvedValue(true),
153154
sendFileUrl: vi.fn().mockResolvedValue(true),
154155
resolveLegacyWebhookNameToChatUserId: vi.fn().mockResolvedValue(undefined),

‎extensions/synology-chat/src/channel.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@ describe("createSynologyChatPlugin", () => {
452452
});
453453

454454
describe("outbound", () => {
455+
it("declares bounded Markdown chunking for gateway text delivery", () => {
456+
const plugin = synologyChatPlugin;
457+
expect(plugin.outbound.chunkerMode).toBe("markdown");
458+
expect(plugin.outbound.textChunkLimit).toBe(2_000);
459+
expect(plugin.outbound.chunker("x".repeat(2_001), 2_000)).toEqual(["x".repeat(2_000), "x"]);
460+
});
461+
455462
it("declares message adapter durable text and media with receipt proofs", async () => {
456463
const plugin = synologyChatPlugin;
457464
const cfg = {

‎extensions/synology-chat/src/channel.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ import {
3232
normalizeStringEntriesLower,
3333
} from "openclaw/plugin-sdk/string-coerce-runtime";
3434
import {
35+
chunkTextForOutbound,
3536
findCodeRegions,
3637
isInsideCode,
3738
sanitizeAssistantVisibleText,
3839
} from "openclaw/plugin-sdk/text-chunking";
3940
import { listAccountIds, resolveAccount } from "./accounts.js";
4041
import { synologyChatApprovalAuth } from "./approval-auth.js";
41-
import { sendMessage, sendFileUrl } from "./client.js";
42+
import { SYNOLOGY_CHAT_TEXT_CHUNK_LIMIT, sendFileUrl, sendMessage } from "./client.js";
4243
import { SynologyChatChannelConfigSchema } from "./config-schema.js";
4344
import {
4445
collectSynologyGatewayRoutingWarnings,
@@ -187,6 +188,8 @@ type SynologyChatPlugin = Omit<
187188
};
188189
outbound: {
189190
deliveryMode: "gateway";
191+
chunker: NonNullable<ChannelOutboundAdapter["chunker"]>;
192+
chunkerMode: NonNullable<ChannelOutboundAdapter["chunkerMode"]>;
190193
textChunkLimit: number;
191194
sanitizeText: NonNullable<ChannelOutboundAdapter["sanitizeText"]>;
192195
sendText: (ctx: SynologyChannelSendTextContext) => Promise<SynologyChatOutboundResult>;
@@ -455,7 +458,9 @@ function createSynologyChatPlugin(): SynologyChatPlugin {
455458
},
456459
outbound: {
457460
deliveryMode: "gateway" as const,
458-
textChunkLimit: 2000,
461+
chunker: chunkTextForOutbound,
462+
chunkerMode: "markdown" as const,
463+
textChunkLimit: SYNOLOGY_CHAT_TEXT_CHUNK_LIMIT,
459464
sanitizeText: ({ text }) => sanitizeAssistantVisibleText(text),
460465
sendText: sendSynologyChatText,
461466
sendMedia: async (ctx) => {

‎extensions/synology-chat/src/client.loopback.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,47 @@ describe("Synology Chat user_list loopback", () => {
125125
}
126126
});
127127

128+
it("chunks long text before the Synology payload limit for every text adapter", async () => {
129+
const receivedPayloads: Array<{ text: string; user_ids?: number[] }> = [];
130+
const port = await listenLoopback((req, res) => {
131+
let formBody = "";
132+
req.setEncoding("utf8");
133+
req.on("data", (chunk: string) => {
134+
formBody += chunk;
135+
});
136+
req.on("end", () => {
137+
const payload = JSON.parse(new URLSearchParams(formBody).get("payload") ?? "{}") as {
138+
text?: string;
139+
user_ids?: number[];
140+
};
141+
receivedPayloads.push({ text: payload.text ?? "", user_ids: payload.user_ids });
142+
const accepted = (payload.text?.length ?? 0) <= 2_000;
143+
res.writeHead(accepted ? 200 : 413, { "Content-Type": "application/json" });
144+
res.end(JSON.stringify({ success: accepted }));
145+
});
146+
});
147+
const incomingUrl = `http://127.0.0.1:${port}/webapi/entry.cgi`;
148+
const cfg = {
149+
channels: {
150+
"synology-chat": {
151+
enabled: true,
152+
token: "loopback-token",
153+
incomingUrl,
154+
},
155+
},
156+
};
157+
const text = "x".repeat(2_001);
158+
159+
await synologyChatPlugin.outbound.sendText({ cfg, text, to: "42" });
160+
await synologyChatPlugin.message.send?.text?.({ cfg, text, to: "42" });
161+
await sendMessage(incomingUrl, text, "42");
162+
163+
expect(receivedPayloads).toHaveLength(6);
164+
expect(receivedPayloads.map(({ text: chunk }) => chunk).join("")).toBe(text + text + text);
165+
expect(receivedPayloads.every(({ text: chunk }) => chunk.length <= 2_000)).toBe(true);
166+
expect(receivedPayloads.every(({ user_ids }) => user_ids?.[0] === 42)).toBe(true);
167+
});
168+
128169
it("rejects unauthenticated webhook sends without fabricating delivery receipts", async () => {
129170
let rejectedRequests = 0;
130171
const port = await listenLoopback((_req, res) => {

‎extensions/synology-chat/src/client.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import {
1414
resolvePinnedHostnameWithPolicy,
1515
} from "openclaw/plugin-sdk/ssrf-runtime";
1616
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
17+
import { chunkTextForOutbound } from "openclaw/plugin-sdk/text-chunking";
1718
import { z } from "zod";
1819

1920
const MIN_SEND_INTERVAL_MS = 500;
21+
export const SYNOLOGY_CHAT_TEXT_CHUNK_LIMIT = 2_000;
2022
/** user_list JSON can be larger than inbound webhook pre-auth payloads. */
2123
const USER_LIST_RESPONSE_MAX_BYTES = 1 * 1024 * 1024;
2224
/** Wall-clock budget for user_list fetch including response body. */
@@ -98,6 +100,21 @@ export async function sendMessage(
98100
text: string,
99101
userId?: string | number,
100102
allowInsecureSsl = false,
103+
): Promise<boolean> {
104+
const chunks = chunkTextForOutbound(text, SYNOLOGY_CHAT_TEXT_CHUNK_LIMIT);
105+
for (const chunk of chunks.length > 0 ? chunks : [text]) {
106+
if (!(await sendMessageChunk(incomingUrl, chunk, userId, allowInsecureSsl))) {
107+
return false;
108+
}
109+
}
110+
return true;
111+
}
112+
113+
async function sendMessageChunk(
114+
incomingUrl: string,
115+
text: string,
116+
userId?: string | number,
117+
allowInsecureSsl = false,
101118
): Promise<boolean> {
102119
// Synology Chat API requires user_ids (numeric) to specify the recipient
103120
// The @mention is optional but user_ids is mandatory

0 commit comments

Comments
 (0)