import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGmailApi, getMessageMetadata, } from "@automate.ax/integration-contracts/gmail" import { GMAIL_METADATA_REQUIREMENT, GMAIL_MODIFY_REQUIREMENT } from "./scopes" import { MESSAGE_IDS_SCHEMA } from "@automate.ax/integration-contracts/gmail" /** * Defines one fixed-label Gmail batch mutation. * * @param name - Action display name. * @param description - Action description. * @param labels - Fixed label mutation. * @param labels.addLabelIds - Label IDs to add. * @param labels.removeLabelIds - Label IDs to remove. */ export function defineMessageLabelMutation( name: string, description: string, labels: { addLabelIds?: string[] removeLabelIds?: string[] }, ) { return defineAction(name) .describe(description) .account("google", GMAIL_MODIFY_REQUIREMENT) .input( z.object({ /** One ID or up to 1,000 immutable Gmail message IDs. */ messageIds: MESSAGE_IDS_SCHEMA, }), ) .output(z.string().array()) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const { messageIds } = input await getGmailApi(account.secret).users.messages.batchModify({ requestBody: { ids: messageIds, ...labels, }, userId: "me", }) return messageIds }) } /** * Defines one fixed-label Gmail predicate. * * @param name - Action display name. * @param description - Action description. * @param labelId - Gmail system label ID. */ export function defineMessageLabelPredicate( name: string, description: string, labelId: string, ) { return defineAction(name) .describe(description) .account("google", GMAIL_METADATA_REQUIREMENT) .input( z.object({ /** Immutable Gmail message ID. */ messageId: z.string().min(1), }), ) .output(z.boolean()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { return ( ( await getMessageMetadata(getGmailApi(account.secret), input.messageId) ).labelIds?.includes(labelId) ?? false ) }) }