import * as z from "zod" import { defineAction } from "../../../automation/actions" import { findLabelIds, getGmailApi, } from "@automate.ax/integration-contracts/gmail" import { GMAIL_MODIFY_REQUIREMENT } from "../lib/scopes" import { LABELS_SCHEMA, MESSAGE_IDS_SCHEMA, } from "@automate.ax/integration-contracts/gmail" /** * Adds Gmail labels to one or many messages. * * Accepts label display names or immutable IDs and returns the modified message * IDs for action chaining. */ export const addGmailLabel = defineAction("Add Gmail label") .describe("Adds Gmail labels to one or many messages.") .account("google", GMAIL_MODIFY_REQUIREMENT) .input( z.object({ /** Gmail label display names or immutable IDs. */ labels: LABELS_SCHEMA, /** 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 gmailApi = getGmailApi(account.secret) const { messageIds } = input await gmailApi.users.messages.batchModify({ requestBody: { addLabelIds: await findLabelIds(gmailApi, input.labels), ids: messageIds, }, userId: "me", }) return messageIds })