Skip to content

Commit 2fe6c02

Browse files
authored
fix(discord): retain attachment-only reply context (#126204)
1 parent 6afb2ce commit 2fe6c02

5 files changed

Lines changed: 91 additions & 7 deletions

File tree

‎extensions/discord/src/monitor/message-handler.context.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ export async function buildDiscordMessageProcessContext(params: {
480480
);
481481
return isContextAborted(abortSignal)
482482
? []
483-
: await toInboundMediaFactsWithMetadata(referencedReplyMediaList);
483+
: await toInboundMediaFactsWithMetadata(referencedReplyMediaList, {
484+
messageId: replyContext.id,
485+
});
484486
},
485487
}
486488
: undefined,

‎extensions/discord/src/monitor/message-handler.process.session-routing.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,64 @@ describe("processDiscordMessage session routing", () => {
174174
expect(dispatchCtx.MediaPaths).toBeUndefined();
175175
});
176176

177+
it("keeps attachment-only referenced messages as typed reply context", async () => {
178+
const fetchImpl = vi.fn(
179+
async () => new Response(Buffer.from("image"), { headers: { "content-type": "image/png" } }),
180+
);
181+
const ctx = await createBaseContext({
182+
cfg: {
183+
channels: { discord: { contextVisibility: "all" } },
184+
messages: { ackReaction: "👀" },
185+
session: { store: "/tmp/openclaw-discord-process-test-sessions.json" },
186+
},
187+
discordRestFetch: fetchImpl,
188+
message: {
189+
id: "m-attachment-reply",
190+
channelId: "c1",
191+
content: "<@bot> what is this?",
192+
timestamp: new Date().toISOString(),
193+
attachments: [],
194+
messageReference: { type: 0, message_id: "m-attachment-only", channel_id: "c1" },
195+
referencedMessage: {
196+
id: "m-attachment-only",
197+
channelId: "c1",
198+
content: "",
199+
timestamp: new Date().toISOString(),
200+
attachments: [
201+
{
202+
id: "att-only",
203+
url: "https://cdn.discordapp.com/attachments/1/attachment-only.png",
204+
content_type: "image/png",
205+
filename: "attachment-only.png",
206+
},
207+
],
208+
author: {
209+
id: "U2",
210+
username: "bob",
211+
discriminator: "0",
212+
globalName: "Bob",
213+
},
214+
},
215+
},
216+
baseText: "<@bot> what is this?",
217+
messageText: "<@bot> what is this?",
218+
});
219+
220+
await runProcessDiscordMessage(ctx);
221+
222+
const dispatchCtx = requireRecord(getLastDispatchCtx(), "dispatch context");
223+
expect(dispatchCtx.ReplyToId).toBe("m-attachment-only");
224+
expect(dispatchCtx.ReplyToSender).toBe("bob");
225+
expect(dispatchCtx.ReplyToBody).toBeUndefined();
226+
expect(dispatchCtx.media).toEqual([
227+
expect.objectContaining({
228+
contentType: "image/png",
229+
messageId: "m-attachment-only",
230+
}),
231+
]);
232+
expect(fetchImpl).toHaveBeenCalledOnce();
233+
});
234+
177235
it("does not inject the bot's previous message body when users reply to it", async () => {
178236
const fetchImpl = vi.fn(async () => {
179237
throw new Error("self-reply media should not be fetched");

‎extensions/discord/src/monitor/reply-context.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Discord plugin module implements reply context behavior.
22
import type { Guild, Message, User } from "../internal/discord.js";
33
import { resolveTimestampMs } from "./format.js";
4+
import { resolveDiscordMessageStickers } from "./message-forwarded.js";
45
import { resolveDiscordSenderIdentity } from "./sender-identity.js";
56

67
type DiscordReplyContext = {
@@ -11,7 +12,7 @@ type DiscordReplyContext = {
1112
senderName?: string;
1213
senderTag?: string;
1314
memberRoleIds?: string[];
14-
body: string;
15+
body?: string;
1516
timestamp?: number;
1617
};
1718

@@ -26,7 +27,10 @@ export function resolveReplyContext(
2627
const referencedText = resolveDiscordMessageText(referenced, {
2728
includeForwarded: true,
2829
});
29-
if (!referencedText) {
30+
const hasVisibleMedia =
31+
referenced.attachments.length > 0 ||
32+
(!referencedText && resolveDiscordMessageStickers(referenced).length > 0);
33+
if (!referencedText && !hasVisibleMedia) {
3034
return null;
3135
}
3236
const sender = resolveDiscordSenderIdentity({
@@ -44,7 +48,7 @@ export function resolveReplyContext(
4448
const roles = (referenced as { member?: { roles?: string[] } }).member?.roles;
4549
return Array.isArray(roles) ? roles.map((roleId) => roleId) : undefined;
4650
})(),
47-
body: referencedText,
51+
...(referencedText ? { body: referencedText } : {}),
4852
timestamp: resolveTimestampMs(referenced.timestamp),
4953
};
5054
}

‎src/auto-reply/reply/inbound-meta.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,15 +733,32 @@ describe("buildInboundUserContextPrefix", () => {
733733

734734
it("labels reply context as the current message target", () => {
735735
const text = buildInboundUserContextPrefix({
736+
ReplyToId: "message-41",
736737
ReplyToSender: "Quoter",
737738
ReplyToBody: "quoted body",
738739
} as TemplateContext);
739740

740741
const reply = parseReplyPayload(text);
742+
expect(reply["message_id"]).toBe("message-41");
741743
expect(reply["sender_label"]).toBe("Quoter");
742744
expect(reply["body"]).toBe("quoted body");
743745
});
744746

747+
it("renders reply metadata without a body but not without any reply fields", () => {
748+
const text = buildInboundUserContextPrefix({
749+
ReplyToId: "message-42",
750+
ReplyToSender: "Attachment Sender",
751+
} as TemplateContext);
752+
753+
expect(parseReplyPayload(text)).toEqual({
754+
message_id: "message-42",
755+
sender_label: "Attachment Sender",
756+
});
757+
expect(buildInboundUserContextPrefix({} as TemplateContext)).not.toContain(
758+
"Reply target of current user message",
759+
);
760+
});
761+
745762
it("renders hydrated reply chain instead of duplicate one-hop reply target", () => {
746763
const text = buildInboundUserContextPrefix(
747764
{

‎src/auto-reply/reply/inbound-meta.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,19 +706,22 @@ export function buildInboundUserContextPrefix(
706706

707707
const rawReplyToBody = sanitizePromptBody(ctx.ReplyToBody);
708708
const replyToBody = rawReplyToBody ? truncateBodyHeadTail(rawReplyToBody) : rawReplyToBody;
709+
const replyToSender = normalizePromptMetadataString(ctx.ReplyToSender);
710+
const hasReplyTargetMetadata = Boolean(replyToId || replyToSender || replyToBody);
709711
if (replyChainPayload.length > 0 && !chatWindowCoversReplyContext && !currentMessageContext) {
710712
blocks.push(
711713
formatContextJsonBlock(
712714
markInboundContextLabel("Reply chain of current user message (nearest first):"),
713715
replyChainPayload,
714716
),
715717
);
716-
} else if (replyToBody && !chatWindowCoversReplyContext && !currentMessageContext) {
718+
} else if (hasReplyTargetMetadata && !chatWindowCoversReplyContext && !currentMessageContext) {
717719
blocks.push(
718720
formatContextJsonBlock(markInboundContextLabel("Reply target of current user message:"), {
719-
sender_label: normalizePromptMetadataString(ctx.ReplyToSender),
721+
message_id: replyToId,
722+
sender_label: replyToSender,
720723
is_quote: ctx.ReplyToIsQuote === true ? true : undefined,
721-
body: replyToBody,
724+
body: replyToBody || undefined,
722725
}),
723726
);
724727
}

0 commit comments

Comments
 (0)