import * as z from "zod" import { defineAction } from "../../../automation/actions" import { EMAIL_ORIGIN_TOKEN } from "../../../lib/email-origin" import { buildRawMessage, getGmailApi, toDraftIdentifier, } from "@automate.ax/integration-contracts/gmail" import { GMAIL_COMPOSE_REQUIREMENT } from "../lib/scopes" import { COMPOSE_OPTIONS_SCHEMA, DRAFT_IDENTIFIER_SCHEMA, } from "@automate.ax/integration-contracts/gmail" /** * Creates a new Gmail draft. * * Recipients, subject, and body are optional so incomplete drafts remain valid. * Plain text is derived automatically when only HTML is supplied. */ export const draftGmailEmail = defineAction("Draft Gmail email") .describe("Creates an incomplete or ready-to-send Gmail draft.") .account("google", GMAIL_COMPOSE_REQUIREMENT) .input( COMPOSE_OPTIONS_SCHEMA.extend({ /** Optional HTML draft body. */ html: z.string().optional(), /** Optional plain-text draft body. */ text: z.string().optional(), }), ) .output(DRAFT_IDENTIFIER_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input, runtime }) => { const { data: draft } = await getGmailApi( account.secret, ).users.drafts.create({ requestBody: { message: { raw: await buildRawMessage({ ...input, automationOriginToken: runtime[EMAIL_ORIGIN_TOKEN], }), }, }, userId: "me", }) return toDraftIdentifier(draft, "draft") })