-
Notifications
You must be signed in to change notification settings - Fork 10
feat: include email attachments in agent.generate + tool calls #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sweetmantech
merged 7 commits into
test
from
sweetmantech/myc-4368-inbound-email-include-attachments-in-agentgenerate-tool
Feb 28, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4ee7ae7
feat: include email attachments in agent.generate and tool calls
sweetmantech a957496
fix: resolve TypeScript type error in generateEmailResponse
sweetmantech 0a5fb33
refactor: remove image parts, rely on download URLs for all file types
sweetmantech 1241c86
debug: add logging to respondToInboundEmail flow
sweetmantech 8b958ed
feat: instruct OpenClaw to download attachment URLs before storing
sweetmantech 35b480b
refactor: use Resend SDK type for EmailAttachment instead of custom i…
sweetmantech d5c0df6
chore: remove debug logging from respondToInboundEmail
sweetmantech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
lib/emails/inbound/__tests__/formatAttachmentsText.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { formatAttachmentsText } from "../formatAttachmentsText"; | ||
| import type { EmailAttachment } from "../getEmailAttachments"; | ||
|
|
||
| const makeAttachment = (overrides: Partial<EmailAttachment> = {}): EmailAttachment => | ||
| ({ | ||
| id: "att-1", | ||
| filename: "file.txt", | ||
| size: 1024, | ||
| content_type: "text/plain", | ||
| content_disposition: "attachment", | ||
| download_url: "https://resend.com/dl/att-1", | ||
| expires_at: "2025-01-01T01:00:00Z", | ||
| ...overrides, | ||
| }) as EmailAttachment; | ||
|
|
||
| describe("formatAttachmentsText", () => { | ||
| it("returns empty string for empty array", () => { | ||
| expect(formatAttachmentsText([])).toBe(""); | ||
| }); | ||
|
|
||
| it("formats a single attachment", () => { | ||
| const attachments = [ | ||
| makeAttachment({ | ||
| filename: "logo.svg", | ||
| content_type: "image/svg+xml", | ||
| download_url: "https://resend.com/dl/att-1", | ||
| }), | ||
| ]; | ||
|
|
||
| const result = formatAttachmentsText(attachments); | ||
|
|
||
| expect(result).toBe( | ||
| "\n\nAttached files:\n- logo.svg (image/svg+xml): https://resend.com/dl/att-1", | ||
| ); | ||
| }); | ||
|
|
||
| it("formats multiple attachments", () => { | ||
| const attachments = [ | ||
| makeAttachment({ | ||
| filename: "logo.svg", | ||
| content_type: "image/svg+xml", | ||
| download_url: "https://resend.com/dl/att-1", | ||
| }), | ||
| makeAttachment({ | ||
| id: "att-2", | ||
| filename: "report.pdf", | ||
| content_type: "application/pdf", | ||
| download_url: "https://resend.com/dl/att-2", | ||
| }), | ||
| ]; | ||
|
|
||
| const result = formatAttachmentsText(attachments); | ||
|
|
||
| expect(result).toContain("Attached files:"); | ||
| expect(result).toContain("- logo.svg (image/svg+xml): https://resend.com/dl/att-1"); | ||
| expect(result).toContain("- report.pdf (application/pdf): https://resend.com/dl/att-2"); | ||
| }); | ||
|
|
||
| it("defaults filename to 'attachment' when not provided", () => { | ||
| const attachments = [makeAttachment({ filename: undefined })]; | ||
|
|
||
| const result = formatAttachmentsText(attachments); | ||
|
|
||
| expect(result).toContain("- attachment (text/plain):"); | ||
| }); | ||
| }); |
64 changes: 64 additions & 0 deletions
64
lib/emails/inbound/__tests__/generateEmailResponse.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { generateEmailResponse } from "../generateEmailResponse"; | ||
| import type { ChatRequestBody } from "@/lib/chat/validateChatRequest"; | ||
|
|
||
| import getGeneralAgent from "@/lib/agents/generalAgent/getGeneralAgent"; | ||
| import { getEmailRoomMessages } from "@/lib/emails/inbound/getEmailRoomMessages"; | ||
|
|
||
| vi.mock("@/lib/agents/generalAgent/getGeneralAgent", () => ({ | ||
| default: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/emails/inbound/getEmailRoomMessages", () => ({ | ||
| getEmailRoomMessages: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/emails/getEmailFooter", () => ({ | ||
| getEmailFooter: vi.fn(() => "<footer>footer</footer>"), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/rooms/selectRoomWithArtist", () => ({ | ||
| selectRoomWithArtist: vi.fn(() => ({ artist_name: "Test Artist" })), | ||
| })); | ||
|
|
||
| const mockGenerate = vi.fn(); | ||
|
|
||
| describe("generateEmailResponse", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| vi.mocked(getGeneralAgent).mockResolvedValue({ | ||
| agent: { generate: mockGenerate }, | ||
| } as unknown as Awaited<ReturnType<typeof getGeneralAgent>>); | ||
|
|
||
| mockGenerate.mockResolvedValue({ text: "Hello from assistant" }); | ||
| }); | ||
|
|
||
| it("throws when roomId is missing", async () => { | ||
| const body = { accountId: "acc-1", orgId: null, messages: [] } as ChatRequestBody; | ||
|
|
||
| await expect(generateEmailResponse(body)).rejects.toThrow( | ||
| "roomId is required to generate email response HTML", | ||
| ); | ||
| }); | ||
|
|
||
| it("generates response with text and footer", async () => { | ||
| vi.mocked(getEmailRoomMessages).mockResolvedValue([{ role: "user", content: "Hi there" }]); | ||
|
|
||
| const body: ChatRequestBody = { | ||
| accountId: "acc-1", | ||
| orgId: null, | ||
| messages: [], | ||
| roomId: "room-1", | ||
| }; | ||
|
|
||
| const result = await generateEmailResponse(body); | ||
|
|
||
| expect(mockGenerate).toHaveBeenCalledWith({ | ||
| messages: [{ role: "user", content: "Hi there" }], | ||
| }); | ||
| expect(result.text).toBe("Hello from assistant"); | ||
| expect(result.html).toContain("Hello from assistant"); | ||
| expect(result.html).toContain("<footer>footer</footer>"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { getEmailAttachments } from "../getEmailAttachments"; | ||
|
|
||
| import { getResendClient } from "@/lib/emails/client"; | ||
|
|
||
| vi.mock("@/lib/emails/client", () => ({ | ||
| getResendClient: vi.fn(), | ||
| })); | ||
|
|
||
| const mockList = vi.fn(); | ||
|
|
||
| describe("getEmailAttachments", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(getResendClient).mockReturnValue({ | ||
| emails: { | ||
| receiving: { | ||
| attachments: { | ||
| list: mockList, | ||
| }, | ||
| }, | ||
| }, | ||
| } as ReturnType<typeof getResendClient>); | ||
| }); | ||
|
|
||
| it("returns attachments directly from Resend SDK", async () => { | ||
| mockList.mockResolvedValue({ | ||
| data: { | ||
| data: [ | ||
| { | ||
| id: "att-1", | ||
| filename: "logo.svg", | ||
| content_type: "image/svg+xml", | ||
| download_url: "https://resend.com/dl/att-1", | ||
| size: 1024, | ||
| content_disposition: "attachment", | ||
| expires_at: "2025-01-01T01:00:00Z", | ||
| }, | ||
| { | ||
| id: "att-2", | ||
| filename: "report.pdf", | ||
| content_type: "application/pdf", | ||
| download_url: "https://resend.com/dl/att-2", | ||
| size: 2048, | ||
| content_disposition: "attachment", | ||
| expires_at: "2025-01-01T01:00:00Z", | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| const result = await getEmailAttachments("email-123"); | ||
|
|
||
| expect(mockList).toHaveBeenCalledWith({ emailId: "email-123" }); | ||
| expect(result).toHaveLength(2); | ||
| expect(result[0].id).toBe("att-1"); | ||
| expect(result[0].download_url).toBe("https://resend.com/dl/att-1"); | ||
| expect(result[1].content_type).toBe("application/pdf"); | ||
| }); | ||
|
|
||
| it("returns empty array when no attachments exist", async () => { | ||
| mockList.mockResolvedValue({ data: { data: [] } }); | ||
|
|
||
| const result = await getEmailAttachments("email-123"); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns empty array when data is null", async () => { | ||
| mockList.mockResolvedValue({ data: null }); | ||
|
|
||
| const result = await getEmailAttachments("email-123"); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import type { EmailAttachment } from "./getEmailAttachments"; | ||
|
|
||
| /** | ||
| * Formats attachment info as text to append to the email body. | ||
| * This makes download URLs available to the LLM and any tools | ||
| * (sandbox, legacy tools, etc.) via the prompt text. | ||
| * | ||
| * @param attachments - Array of email attachments with download URLs | ||
| * @returns Formatted text block listing attachments, or empty string if none | ||
| */ | ||
| export function formatAttachmentsText(attachments: EmailAttachment[]): string { | ||
| if (!attachments.length) return ""; | ||
|
|
||
| const lines = attachments.map( | ||
| att => `- ${att.filename || "attachment"} (${att.content_type}): ${att.download_url}`, | ||
| ); | ||
| return `\n\nAttached files:\n${lines.join("\n")}`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { getResendClient } from "@/lib/emails/client"; | ||
| import type { ListAttachmentsResponseSuccess } from "resend"; | ||
|
|
||
| export type EmailAttachment = ListAttachmentsResponseSuccess["data"][number]; | ||
|
|
||
| /** | ||
| * Fetches attachment download URLs for a received email from Resend. | ||
| * Webhooks only include attachment metadata (id, filename, content_type), | ||
| * so this calls the Attachments API to get download URLs. | ||
| * | ||
| * @param emailId - The email ID from the Resend webhook event | ||
| * @returns Array of attachments with download URLs (empty if none) | ||
| */ | ||
| export async function getEmailAttachments(emailId: string): Promise<EmailAttachment[]> { | ||
| const resend = getResendClient(); | ||
| const { data } = await resend.emails.receiving.attachments.list({ emailId }); | ||
|
|
||
| if (!data?.data?.length) return []; | ||
|
|
||
| return data.data; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.