import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGmailApi, getGmailMessage, } from "@automate.ax/integration-contracts/gmail" import { GMAIL_DRAFT_READ_REQUIREMENT } from "../lib/scopes" import { DRAFT_SCHEMA } from "@automate.ax/integration-contracts/gmail" /** Retrieves and fully parses a Gmail draft and its message. */ export const getGmailDraft = defineAction("Get Gmail draft") .describe("Gets a Gmail draft with its parsed message and attachments.") .account("google", GMAIL_DRAFT_READ_REQUIREMENT) .input( z.object({ /** Immutable Gmail draft ID. */ draftId: z.string().min(1), }), ) .output(DRAFT_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { data: draft } = await getGmailApi(account.secret).users.drafts.get({ format: "raw", id: input.draftId, userId: "me", }) if (!draft.id || !draft.message) { throw new Error("Gmail returned an incomplete draft.") } return { draftId: draft.id, message: await getGmailMessage(draft.message), } })