The Open Framework for Building Agents
fix(slack): preserve inbound previews with comments (#3781)
Ben Pankow8c8ef08d6654
diff --git a/.changeset/slack-inbound-previews.md b/.changeset/slack-inbound-previews.mdnew file mode 100644index 000000000..619b7c985--- /dev/null+++ b/.changeset/slack-inbound-previews.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Keep inbound Slack link previews alongside the sender's message even when the link has surrounding text. Previously, shorter previews could be dropped when the message included a question or comment.diff --git a/docs/channels/slack.mdx b/docs/channels/slack.mdxindex f91fdd880..efe7b8858 100644--- a/docs/channels/slack.mdx+++ b/docs/channels/slack.mdx@@ -187,7 +187,7 @@ export default slackChannel({The model-visible `<slack_message>` includes `sender_id`, `bot_user_id` when available, and the `is_mentioned` value from `ctx.isBotMentioned()`. Its `<content>` keeps `<@USER_ID>` intact while converting other mrkdwn to Markdown; routing is unchanged.-When a Slack message shares another Slack message, eve extracts the forwarded message body from Slack's message-unfurl attachment and includes it in `<content>`. The original top-level comment, when present, remains alongside the forwarded content.+When a Slack message includes a link preview, eve includes its attachment text in `<content>` alongside the sender's message, including when the link has words around it. Shared Slack messages work the same way: eve extracts the forwarded message body from Slack's message-unfurl attachment and keeps any top-level comment.#### Restrict who can invoke the agentdiff --git a/packages/eve/src/public/channels/slack/inbound-content.test.ts b/packages/eve/src/public/channels/slack/inbound-content.test.tsindex c6bbb6fbb..802397aa5 100644--- a/packages/eve/src/public/channels/slack/inbound-content.test.ts+++ b/packages/eve/src/public/channels/slack/inbound-content.test.ts@@ -413,6 +413,76 @@ describe("resolveSlackInboundMrkdwn", () => {expect(result).toBe("Forwarded feedback");});+ it.each([+ ["bare link", "<https://x/a>"],+ ["short question and link", "what do you make of this? <https://x/a>"],+ [+ "sentence and link",+ "Can you look at this alert and tell me whether it is the adapter again? <https://x/a>",+ ],+ ])("keeps an inbound link preview with a %s", (_name, text) => {+ const result = resolveSlackInboundMrkdwn(text, {+ attachments: [+ { title: "Grafana Alerts", text: "[FIRING:12] parse errors, severity critical" },+ ],+ });++ expect(result).toBe(`${text}\nGrafana Alerts\n[FIRING:12] parse errors, severity critical`);+ });++ it.each([+ ["short", "[FIRING:12] parse errors"],+ ["long", "[FIRING:12] parse errors, severity critical; check the adapter logs"],+ ])("keeps a %s preview without repeating a URL-only rich-text link", (_name, preview) => {+ const text = "Can you check <https://x/a>?";+ const result = resolveSlackInboundMrkdwn(text, {+ blocks: [+ {+ type: "rich_text",+ elements: [+ {+ type: "rich_text_section",+ elements: [+ { type: "text", text: "Can you check " },+ { type: "link", url: "https://x/a" },+ { type: "text", text: "?" },+ ],+ },+ ],+ },+ ],+ attachments: [{ title: "Grafana Alerts", text: preview }],+ });++ expect(result).toBe(`${text}\nGrafana Alerts\n${preview}`);+ });++ it("keeps nonredundant block content and an attachment alongside top-level text", () => {+ const result = resolveSlackInboundMrkdwn("Alert", {+ blocks: [{ type: "section", text: { type: "mrkdwn", text: "Check the dashboard" } }],+ attachments: [{ text: "Latency is high" }],+ });++ expect(result).toBe("Alert\nCheck the dashboard\nLatency is high");+ });++ it("does not repeat top-level blocks when a preview is longer than the comment", () => {+ const text = "Check <https://x/a>";+ const result = resolveSlackInboundMrkdwn(text, {+ blocks: [{ type: "section", text: { type: "mrkdwn", text } }],+ attachments: [+ {+ title: "Grafana Alerts",+ text: "[FIRING:12] parse errors, severity critical; check the adapter logs",+ },+ ],+ });++ expect(result).toBe(+ `${text}\nGrafana Alerts\n[FIRING:12] parse errors, severity critical; check the adapter logs`,+ );+ });+it("keeps a top-level comment alongside a shorter shared Slack message", () => {const result = resolveSlackInboundMrkdwn("This seems related to the navigation feedback we discussed yesterday.",diff --git a/packages/eve/src/public/channels/slack/inbound-content.ts b/packages/eve/src/public/channels/slack/inbound-content.tsindex 7a484c696..ed7c5b711 100644--- a/packages/eve/src/public/channels/slack/inbound-content.ts+++ b/packages/eve/src/public/channels/slack/inbound-content.ts@@ -17,30 +17,29 @@ function resolveSlackInboundMrkdwnUnsafe(text: string, raw: Record<string, unknoif (!trimmedText) return extracted;if (!extracted) return text;- if (normalizeComparableText(extracted) === normalizeComparableText(trimmedText)) {- return text;- }-- const normalizedExtracted = normalizeComparableText(extracted);const normalizedTrimmed = normalizeComparableText(trimmedText);-- if (extracted.length > trimmedText.length && normalizedExtracted.includes(normalizedTrimmed)) {- return extracted;- }-- if (hasSharedMessageAttachment(raw.attachments)) {- if (normalizedTrimmed.includes(normalizedExtracted)) return text;- return `${text}\n${extracted}`;+ const normalizedExtracted = normalizeComparableText(extracted);+ if (normalizedExtracted === normalizedTrimmed) return text;++ const attachmentText = extractLegacyAttachmentLines(raw.attachments).join("\n").trim();+ if (attachmentText) {+ const blockText = extractBlockKitLines(raw.blocks).join("\n").trim();+ const content = [+ text,+ ...(blockText && !normalizedTrimmed.includes(normalizeComparableText(blockText))+ ? [blockText]+ : []),+ ...(!normalizedTrimmed.includes(normalizeComparableText(attachmentText))+ ? [attachmentText]+ : []),+ ];+ return content.join("\n");}- if (extracted.length >= trimmedText.length * 2) {- const hasLegacyAttachments = Array.isArray(raw.attachments) && raw.attachments.length > 0;- if (hasLegacyAttachments && !normalizedExtracted.includes(normalizedTrimmed)) {- return `${text}\n${extracted}`;- }+ if (extracted.length > trimmedText.length && normalizedExtracted.includes(normalizedTrimmed)) {return extracted;}-+ if (extracted.length >= trimmedText.length * 2) return extracted;return text;}@@ -161,18 +160,6 @@ function extractLegacyAttachmentLines(legacyAttachments: unknown): string[] {return lines;}-function hasSharedMessageAttachment(legacyAttachments: unknown): boolean {- if (!Array.isArray(legacyAttachments)) return false;- return legacyAttachments.some(- (attachment) =>- isObject(attachment) &&- (Array.isArray(attachment.message_blocks) ||- attachment.is_share === true ||- attachment.is_msg_unfurl === true ||- attachment.is_reply_unfurl === true),- );-}-function extractSlackMessageUnfurlLines(messageBlocks: unknown): string[] {if (!Array.isArray(messageBlocks)) return [];@@ -375,7 +362,10 @@ export function readSlackTextObject(textObject: unknown): string {}function normalizeComparableText(input: string): string {- return input.replace(/\s+/gu, " ").trim();+ return input+ .replace(/<(https?:\/\/[^|>]+)>/gu, "$1")+ .replace(/\s+/gu, " ")+ .trim();}function formatInboundRichTextLink(url: string, label: string): string {diff --git a/packages/eve/src/public/channels/slack/inbound.test.ts b/packages/eve/src/public/channels/slack/inbound.test.tsindex 4a1297b32..4dfd4beb0 100644--- a/packages/eve/src/public/channels/slack/inbound.test.ts+++ b/packages/eve/src/public/channels/slack/inbound.test.ts@@ -538,6 +538,24 @@ describe("Block Kit inbound markdown", () => {expect(message?.markdown).toContain("CI Bot");});+ it("keeps a short link preview alongside a question in the model-visible markdown", () => {+ const message = parseMessageEvent({+ type: "event_callback",+ event: {+ type: "message",+ user: "U01",+ text: "Can you check this alert? <https://x/a>",+ channel: "C01",+ ts: "1234567890.123458",+ attachments: [{ title: "Grafana Alerts", text: "[FIRING:12] parse errors" }],+ },+ });++ expect(message?.markdown).toContain("Can you check this alert?");+ expect(message?.markdown).toContain("Grafana Alerts");+ expect(message?.markdown).toContain("[FIRING:12] parse errors");+ });+it("keeps plain top-level text when there are no blocks or attachments", () => {const message = parseMessageEvent({type: "event_callback",