Skip to content

Commit 913b53a

Browse files
authored
fix(channels): preserve bundled delivery contracts
Preserves pending Telegram replies, Discord webhook preview policy, forwarded Slack file context, and QQBot target recognition.
1 parent cd009e7 commit 913b53a

13 files changed

Lines changed: 215 additions & 20 deletions

File tree

‎extensions/discord/src/send.message-request.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ export function resolveDiscordMessageFlags(params: {
9797
return flags || undefined;
9898
}
9999

100+
export function resolveDiscordSuppressEmbeds(params: {
101+
configured?: boolean;
102+
override?: boolean;
103+
}): boolean {
104+
return params.override ?? params.configured ?? true;
105+
}
106+
100107
type DiscordMessageRequestParams = {
101108
text: string;
102109
components?: TopLevelComponents[];

‎extensions/discord/src/send.outbound.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
normalizeDiscordPollInput,
3434
normalizeStickerIds,
3535
resolveDiscordMessageFlags,
36+
resolveDiscordSuppressEmbeds,
3637
resolveChannelId,
3738
resolveDiscordChannel,
3839
resolveDiscordSendComponents,
@@ -109,13 +110,6 @@ async function sendDiscordThreadTextChunks(params: {
109110
}
110111
}
111112

112-
function resolveDiscordSuppressEmbeds(params: {
113-
configured?: boolean;
114-
override?: boolean;
115-
}): boolean {
116-
return params.override ?? params.configured ?? true;
117-
}
118-
119113
/** Discord thread names are capped at 100 characters. */
120114
const DISCORD_THREAD_NAME_LIMIT = 100;
121115

‎extensions/discord/src/send.shared.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export {
4848
buildDiscordMessageRequest,
4949
createDiscordMessageNonce,
5050
resolveDiscordMessageFlags,
51+
resolveDiscordSuppressEmbeds,
5152
resolveDiscordSendComponents,
5253
resolveDiscordSendEmbeds,
5354
stripUndefinedFields,

‎extensions/discord/src/send.webhook-activity.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Discord tests cover send.webhook activity plugin behavior.
2+
import { MessageFlags } from "discord-api-types/v10";
23
import { isRecentOutboundMessageIdentity } from "openclaw/plugin-sdk/channel-outbound";
34
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
45

@@ -155,9 +156,72 @@ describe("sendWebhookMessageDiscord activity", () => {
155156
},
156157
body: JSON.stringify({
157158
content: "hello <@123456789012345678>",
159+
flags: MessageFlags.SuppressEmbeds,
158160
}),
159161
signal: expect.any(AbortSignal),
160162
},
161163
]);
162164
});
165+
166+
it.each([
167+
{
168+
name: "the default",
169+
channelSuppressEmbeds: undefined,
170+
accountSuppressEmbeds: undefined,
171+
expectedFlags: MessageFlags.SuppressEmbeds,
172+
},
173+
{
174+
name: "the channel opt-out",
175+
channelSuppressEmbeds: false,
176+
accountSuppressEmbeds: undefined,
177+
expectedFlags: undefined,
178+
},
179+
{
180+
name: "an account opt-out",
181+
channelSuppressEmbeds: true,
182+
accountSuppressEmbeds: false,
183+
expectedFlags: undefined,
184+
},
185+
{
186+
name: "an account opt-in",
187+
channelSuppressEmbeds: false,
188+
accountSuppressEmbeds: true,
189+
expectedFlags: MessageFlags.SuppressEmbeds,
190+
},
191+
])(
192+
"applies $name for webhook link-preview suppression",
193+
async ({ channelSuppressEmbeds, accountSuppressEmbeds, expectedFlags }) => {
194+
const cfg = {
195+
channels: {
196+
discord: {
197+
token: "resolved-token",
198+
...(channelSuppressEmbeds === undefined
199+
? {}
200+
: { suppressEmbeds: channelSuppressEmbeds }),
201+
accounts: {
202+
runtime:
203+
accountSuppressEmbeds === undefined
204+
? {}
205+
: { suppressEmbeds: accountSuppressEmbeds },
206+
},
207+
},
208+
},
209+
};
210+
211+
await sendWebhookMessageDiscord("https://example.com", {
212+
cfg,
213+
webhookId: "wh-1",
214+
webhookToken: "tok-1",
215+
accountId: "runtime",
216+
threadId: "thread-1",
217+
});
218+
219+
const request = firstMockCall(vi.mocked(fetch), "fetch")[1] as RequestInit;
220+
if (typeof request.body !== "string") {
221+
throw new Error("expected webhook request body");
222+
}
223+
const body = JSON.parse(request.body) as { flags?: number };
224+
expect(body.flags).toBe(expectedFlags);
225+
},
226+
);
163227
});

‎extensions/discord/src/send.webhook.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import {
2020
import { rewriteDiscordKnownMentions } from "./mentions.js";
2121
import { DISCORD_REST_TIMEOUT_MS } from "./proxy-request-client.js";
2222
import { createDiscordRetryRunner } from "./retry.js";
23+
import {
24+
resolveDiscordMessageFlags,
25+
resolveDiscordSuppressEmbeds,
26+
} from "./send.message-request.js";
2327
import { createDiscordSendResult } from "./send.receipt.js";
2428
import type { DiscordSendResult } from "./send.types.js";
2529

@@ -121,6 +125,9 @@ export async function sendWebhookMessageDiscord(
121125
accountId: account.accountId,
122126
mentionAliases: account.config.mentionAliases,
123127
});
128+
const flags = resolveDiscordMessageFlags({
129+
suppressEmbeds: resolveDiscordSuppressEmbeds({ configured: account.config.suppressEmbeds }),
130+
});
124131
const threadConversationId = opts.threadId == null ? "" : String(opts.threadId).trim();
125132
if (threadConversationId) {
126133
// Reserve the webhook source before the request so an immediate gateway echo
@@ -156,6 +163,7 @@ export async function sendWebhookMessageDiscord(
156163
content: rewrittenText,
157164
username: normalizeOptionalString(opts.username),
158165
avatar_url: normalizeOptionalString(opts.avatarUrl),
166+
...(flags ? { flags } : {}),
159167
...(messageReference ? { message_reference: messageReference } : {}),
160168
}),
161169
signal: deadline.signal,

‎extensions/qqbot/src/engine/messaging/target-parser.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ describe("normalizeTarget", () => {
2727
["qqbot:Group:GroupOpenId", "qqbot:group:GroupOpenId"],
2828
["C2C:OpenId", "qqbot:c2c:OpenId"],
2929
["qqbot:channel:ChannelId", "qqbot:channel:ChannelId"],
30+
["qqbot:0123456789abcdef0123456789abcdef", "qqbot:c2c:0123456789abcdef0123456789abcdef"],
31+
[
32+
"QQBOT:01234567-89ab-cdef-0123-456789abcdef",
33+
"qqbot:c2c:01234567-89ab-cdef-0123-456789abcdef",
34+
],
3035
])("normalizes %s to %s", (to, normalized) => {
3136
expect(looksLikeQQBotTarget(to)).toBe(true);
3237
expect(normalizeTarget(to)).toBe(normalized);

‎extensions/qqbot/src/engine/messaging/target-parser.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,5 @@ export function normalizeTarget(target: string): string | undefined {
8989
* Return true when the string looks like a QQ Bot target ID.
9090
*/
9191
export function looksLikeQQBotTarget(id: string): boolean {
92-
const unqualifiedId = id.replace(/^qqbot:/i, "");
93-
if (parseTypedTarget(unqualifiedId)) {
94-
return true;
95-
}
96-
if (/^[0-9a-fA-F]{32}$/.test(id)) {
97-
return true;
98-
}
99-
return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(id);
92+
return normalizeTarget(id) !== undefined;
10093
}

‎extensions/slack/src/monitor/media.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,18 @@ describe("resolveSlackAttachmentContent", () => {
11781178
expect(mockFetch).not.toHaveBeenCalled();
11791179
});
11801180

1181+
it("preserves forwarded file identities when downloads fail", async () => {
1182+
const file = { id: "FFORWARD", name: "forwarded-report.pdf" };
1183+
const result = await resolveSlackAttachmentContent({
1184+
attachments: [{ is_share: true, files: [file] }],
1185+
token: "xoxb-test-token",
1186+
maxBytes: 1024 * 1024,
1187+
});
1188+
1189+
expect(result).toEqual({ text: "", media: [], files: [file] });
1190+
expect(mockFetch).not.toHaveBeenCalled();
1191+
});
1192+
11811193
it("skips forwarded image URLs on non-Slack hosts", async () => {
11821194
const result = await resolveSlackAttachmentContent({
11831195
attachments: [{ is_share: true, image_url: "https://example.com/forwarded.jpg" }],

‎extensions/slack/src/monitor/media.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ export async function resolveSlackAttachmentContent(params: {
411411
readIdleTimeoutMs?: number;
412412
totalTimeoutMs?: number;
413413
abortSignal?: AbortSignal;
414-
}): Promise<{ text: string; media: SlackMediaResult[] } | null> {
414+
}): Promise<{ text: string; media: SlackMediaResult[]; files?: SlackFile[] } | null> {
415415
const attachments = params.attachments;
416416
if (!attachments || attachments.length === 0) {
417417
return null;
@@ -426,6 +426,7 @@ export async function resolveSlackAttachmentContent(params: {
426426

427427
const textBlocks: string[] = [];
428428
const allMedia: SlackMediaResult[] = [];
429+
const allFiles = forwardedAttachments.flatMap((attachment) => attachment.files ?? []);
429430
const govSlack = isGovSlackClient(params.client);
430431

431432
for (const att of forwardedAttachments) {
@@ -485,8 +486,12 @@ export async function resolveSlackAttachmentContent(params: {
485486
}
486487

487488
const combinedText = textBlocks.join("\n\n");
488-
if (!combinedText && allMedia.length === 0) {
489+
if (!combinedText && allMedia.length === 0 && allFiles.length === 0) {
489490
return null;
490491
}
491-
return { text: combinedText, media: allMedia };
492+
return {
493+
text: combinedText,
494+
media: allMedia,
495+
...(allFiles.length > 0 ? { files: allFiles } : {}),
496+
};
492497
}

‎extensions/slack/src/monitor/message-handler/prepare-content.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,20 @@ export async function resolveSlackMessageContent(params: {
136136
? effectiveDirectMedia.map((item) => item.placeholder).join(" ")
137137
: undefined;
138138

139-
const fallbackFiles = ownFiles ?? [];
139+
const fallbackFileIds = new Set<string>();
140+
const fallbackFiles = [...(ownFiles ?? []), ...(attachmentContent?.files ?? [])].filter(
141+
(file) => {
142+
const fileId = normalizeOptionalString(file.id);
143+
if (!fileId) {
144+
return true;
145+
}
146+
if (fallbackFileIds.has(fileId)) {
147+
return false;
148+
}
149+
fallbackFileIds.add(fileId);
150+
return true;
151+
},
152+
);
140153
const fileOnlyFallback =
141154
!mediaPlaceholder && fallbackFiles.length > 0
142155
? fallbackFiles

0 commit comments

Comments
 (0)