From 88e025fb9b4299a0e55aa77f386319605a5c3682 Mon Sep 17 00:00:00 2001 From: waleed Date: Mon, 6 Jul 2026 09:51:45 -0700 Subject: [PATCH] fix(mailer): correct AgentMail webhook message schema field requirements cc, attachments, and html are omitted from AgentMail's webhook payload when empty rather than sent as empty arrays/null, but the zod schema marked them required. Every inbound email without a CC recipient or attachment (the common case) failed schema validation before a task row was ever created, silently breaking Sim Mailer inbox ingestion since the feature shipped. Renames from_ to from to match AgentMail's documented field name. --- apps/sim/app/api/webhooks/agentmail/route.ts | 12 ++++++------ apps/sim/lib/api/contracts/webhooks.ts | 12 ++++++------ apps/sim/lib/mothership/inbox/types.ts | 12 ++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/apps/sim/app/api/webhooks/agentmail/route.ts b/apps/sim/app/api/webhooks/agentmail/route.ts index b31ae2563a9..0dc99567746 100644 --- a/apps/sim/app/api/webhooks/agentmail/route.ts +++ b/apps/sim/app/api/webhooks/agentmail/route.ts @@ -152,8 +152,8 @@ export const POST = withRouteHandler(async (req: Request) => { return NextResponse.json({ ok: true }) } - const fromEmail = extractSenderEmail(message.from_) || '' - logger.info('Webhook received', { fromEmail, from_raw: message.from_, workspaceId: result.id }) + const fromEmail = extractSenderEmail(message.from) || '' + logger.info('Webhook received', { fromEmail, from_raw: message.from, workspaceId: result.id }) if (result.inboxAddress && fromEmail === result.inboxAddress.toLowerCase()) { logger.info('Skipping email from inbox itself', { workspaceId: result.id }) @@ -212,7 +212,7 @@ export const POST = withRouteHandler(async (req: Request) => { const chatId = parentTaskResult[0]?.chatId ?? null - const fromName = extractDisplayName(message.from_) + const fromName = extractDisplayName(message.from) const taskId = generateId() const bodyText = message.text?.substring(0, 50_000) || null @@ -334,8 +334,8 @@ async function createRejectedTask( await db.insert(mothershipInboxTask).values({ id: generateId(), workspaceId, - fromEmail: extractSenderEmail(message.from_) || 'unknown', - fromName: extractDisplayName(message.from_), + fromEmail: extractSenderEmail(message.from) || 'unknown', + fromName: extractDisplayName(message.from), subject: message.subject || '(no subject)', bodyPreview: (message.text || '').substring(0, 200) || null, emailMessageId: message.message_id, @@ -347,7 +347,7 @@ async function createRejectedTask( } /** - * Extract the raw email address from AgentMail's from_ field. + * Extract the raw email address from AgentMail's from field. * Format: "username@domain.com" or "Display Name " */ function extractSenderEmail(from: string): string { diff --git a/apps/sim/lib/api/contracts/webhooks.ts b/apps/sim/lib/api/contracts/webhooks.ts index b28853b9c6b..ca309c6f44e 100644 --- a/apps/sim/lib/api/contracts/webhooks.ts +++ b/apps/sim/lib/api/contracts/webhooks.ts @@ -102,16 +102,16 @@ export const agentMailMessageSchema = z thread_id: z.string(), inbox_id: z.string(), organization_id: z.string().optional(), - from_: z.string(), + from: z.string(), to: z.array(z.string()), - cc: z.array(z.string()), + cc: z.array(z.string()).optional(), bcc: z.array(z.string()).optional(), reply_to: z.array(z.string()).optional(), - subject: z.string(), + subject: z.string().optional(), preview: z.string().optional(), - text: z.string().nullable(), - html: z.string().nullable(), - attachments: z.array(agentMailAttachmentSchema), + text: z.string().nullable().optional(), + html: z.string().nullable().optional(), + attachments: z.array(agentMailAttachmentSchema).optional(), in_reply_to: z.string().optional(), references: z.array(z.string()).optional(), labels: z.array(z.string()).optional(), diff --git a/apps/sim/lib/mothership/inbox/types.ts b/apps/sim/lib/mothership/inbox/types.ts index 02fab8a1e9a..f79685c9cf6 100644 --- a/apps/sim/lib/mothership/inbox/types.ts +++ b/apps/sim/lib/mothership/inbox/types.ts @@ -57,16 +57,16 @@ export interface AgentMailMessage { thread_id: string inbox_id: string organization_id?: string - from_: string + from: string to: string[] - cc: string[] + cc?: string[] bcc?: string[] reply_to?: string[] - subject: string + subject?: string preview?: string - text: string | null - html: string | null - attachments: AgentMailAttachment[] + text?: string | null + html?: string | null + attachments?: AgentMailAttachment[] in_reply_to?: string references?: string[] labels?: string[]