import pMap from "p-map" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGmailApi, getGmailMessage, } from "@automate.ax/integration-contracts/gmail" import { GMAIL_READ_REQUIREMENT } from "../lib/scopes" import { MESSAGE_SCHEMA } from "@automate.ax/integration-contracts/gmail" /** Retrieves and fully parses up to 100 Gmail messages in one action. */ export const getGmailMessages = defineAction("Get Gmail messages") .describe("Gets multiple parsed Gmail messages with headers and attachments.") .account("google", GMAIL_READ_REQUIREMENT) .input( z.object({ /** One to 100 immutable Gmail message IDs. */ messageIds: z.string().min(1).array().min(1).max(100), }), ) .output(MESSAGE_SCHEMA.array()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const gmailApi = getGmailApi(account.secret) return await pMap( input.messageIds, async (messageId) => getGmailMessage( ( await gmailApi.users.messages.get({ format: "raw", id: messageId, userId: "me", }) ).data, ), { concurrency: 20 }, ) })