Discord: include reply context

This commit is contained in:
Shadow
2026-01-02 14:49:16 -06:00
parent f4a1190bdd
commit f3a973dc9e

View File

@@ -172,12 +172,7 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
const botId = client.user?.id;
const wasMentioned =
!isDirectMessage && Boolean(botId && message.mentions.has(botId));
const attachment = message.attachments.first();
const baseText =
message.content?.trim() ||
(attachment ? inferPlaceholder(attachment) : "") ||
message.embeds[0]?.description ||
"";
const baseText = resolveDiscordMessageText(message);
if (isVerbose()) {
logVerbose(
`discord: inbound id=${message.id} guild=${message.guild?.id ?? "dm"} channel=${message.channelId} mention=${wasMentioned ? "yes" : "no"} type=${isDirectMessage ? "dm" : isGroupDm ? "group-dm" : "guild"} content=${baseText ? "yes" : "no"}`,
@@ -357,6 +352,10 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
combinedBody = `${combinedBody}\n[from: ${name} id:${id}]`;
shouldClearHistory = true;
}
const replyContext = await resolveReplyContext(message);
if (replyContext) {
combinedBody = `[Replied message - for context]\n${replyContext}\n\n${combinedBody}`;
}
const ctxPayload = {
Body: combinedBody,
@@ -659,6 +658,43 @@ function inferPlaceholder(attachment: import("discord.js").Attachment): string {
return "<media:document>";
}
function resolveDiscordMessageText(message: Message): string {
const attachment = message.attachments.first();
return (
message.content?.trim() ||
(attachment ? inferPlaceholder(attachment) : "") ||
message.embeds[0]?.description ||
""
);
}
async function resolveReplyContext(message: Message): Promise<string | null> {
if (!message.reference?.messageId) return null;
try {
const referenced = await message.fetchReference();
if (!referenced?.author) return null;
const referencedText = resolveDiscordMessageText(referenced);
if (!referencedText) return null;
const channelType = referenced.channel.type as ChannelType;
const isDirectMessage = channelType === ChannelType.DM;
const fromLabel = isDirectMessage
? buildDirectLabel(referenced)
: referenced.member?.displayName ?? referenced.author.tag;
const body = `${referencedText}\n[discord message id: ${referenced.id} channel: ${referenced.channelId} from: ${referenced.author.tag} id:${referenced.author.id}]`;
return formatAgentEnvelope({
surface: "Discord",
from: fromLabel,
timestamp: referenced.createdTimestamp,
body,
});
} catch (err) {
logVerbose(
`discord: failed to fetch reply context for ${message.id}: ${String(err)}`,
);
return null;
}
}
function buildDirectLabel(message: import("discord.js").Message) {
const username = message.author.tag;
return `${username} id:${message.author.id}`;