Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

perf: parallelize R2 attachment fetches in email-worker (#252)

Gener19bb604d6b9f
diff --git a/src/email-worker/src/index.test.ts b/src/email-worker/src/index.test.ts
index 86872b439..fabf4663a 100644
--- a/src/email-worker/src/index.test.ts
+++ b/src/email-worker/src/index.test.ts
@@ -589,6 +589,51 @@ describe("POST /send/agent", () => {
expect(storedMime).toContain("Content-Transfer-Encoding: base64")
})
+ it("fetches multiple attachments in parallel and skips missing objects", async () => {
+ mockGetAgent.mockResolvedValue({ id: "agent-1", workspaceId: "ws-1", emailHandle: "jarvis" })
+ const { env, send, put } = agentSendEnv()
+
+ const file1 = new TextEncoder().encode("content1")
+ const file2 = new TextEncoder().encode("content2")
+ const fetchOrder: string[] = []
+
+ ;((env.EMAIL_BUCKET as any).get as ReturnType<typeof vi.fn>) = vi.fn((key: string) => {
+ fetchOrder.push(key)
+ if (key === "emails/drafts/x/missing.txt") return Promise.resolve(null)
+ const content = key === "emails/drafts/x/a.txt" ? file1 : file2
+ return Promise.resolve({ arrayBuffer: () => Promise.resolve(content.buffer) })
+ })
+
+ const res = await handler.fetch(
+ makeAgentSendRequest({
+ agentId: "agent-1",
+ workspaceId: "ws-1",
+ subject: "Multi-attach",
+ htmlBody: "<p>Files</p>",
+ attachmentKeys: [
+ { key: "emails/drafts/x/a.txt", filename: "a.txt", contentType: "text/plain" },
+ { key: "emails/drafts/x/missing.txt", filename: "missing.txt", contentType: "text/plain" },
+ { key: "emails/drafts/x/b.txt", filename: "b.txt", contentType: "text/plain" },
+ ],
+ }),
+ env,
+ )
+
+ expect(res.status).toBe(200)
+ // All R2 fetches were initiated (parallel)
+ expect(fetchOrder).toHaveLength(3)
+ expect(fetchOrder).toContain("emails/drafts/x/a.txt")
+ expect(fetchOrder).toContain("emails/drafts/x/missing.txt")
+ expect(fetchOrder).toContain("emails/drafts/x/b.txt")
+
+ // Only non-null attachments included
+ const sendArg = send.mock.calls[0][0]
+ expect(sendArg.attachments).toHaveLength(2)
+ expect(sendArg.attachments[0].filename).toBe("a.txt")
+ expect(sendArg.attachments[1].filename).toBe("b.txt")
+ })
+
it("returns 404 when agent not found in workspace", async () => {
mockGetAgent.mockResolvedValue(null)
const { env } = agentSendEnv()
diff --git a/src/email-worker/src/index.ts b/src/email-worker/src/index.ts
index afa586144..d277c3b27 100644
--- a/src/email-worker/src/index.ts
+++ b/src/email-worker/src/index.ts
@@ -135,20 +135,21 @@ export default {
const htmlBody = body.htmlBody ?? ""
const attachmentKeys = body.attachmentKeys ?? []
- // Fetch attachment content from R2
- const attachments: { disposition: "attachment"; filename: string; type: string; raw: ArrayBuffer; base64: string }[] = []
- for (const att of attachmentKeys) {
- const obj = await env.EMAIL_BUCKET.get(att.key)
- if (!obj) continue
- const raw = await obj.arrayBuffer()
- attachments.push({
- disposition: "attachment" as const,
- filename: att.filename,
- type: att.contentType,
- raw,
- base64: arrayBufferToBase64(raw),
+ // Fetch attachment content from R2 in parallel
+ const attachments = (await Promise.all(
+ attachmentKeys.map(async (att) => {
+ const obj = await env.EMAIL_BUCKET.get(att.key)
+ if (!obj) return null
+ const raw = await obj.arrayBuffer()
+ return {
+ disposition: "attachment" as const,
+ filename: att.filename,
+ type: att.contentType,
+ raw,
+ base64: arrayBufferToBase64(raw),
+ }
})
- }
+ )).filter((a): a is NonNullable<typeof a> => a !== null)
if (useCustomSmtp && customAccount) {
const secret = env.ENCRYPTION_KEY