import * as z from "zod" import { defineAction } from "../../../automation/actions" import { findLabelId, getGmailApi, getMessageMetadata, } from "@automate.ax/integration-contracts/gmail" import { GMAIL_READ_REQUIREMENT } from "../lib/scopes" /** Checks whether a Gmail message has a label by display name or ID. */ export const checkGmailLabel = defineAction("Check Gmail label") .describe("Checks whether a Gmail message carries a label.") .account("google", GMAIL_READ_REQUIREMENT) .input( z.object({ /** Gmail label display name or immutable ID. */ label: z.string().min(1), /** Immutable Gmail message ID to inspect. */ messageId: z.string().min(1), }), ) .output(z.boolean()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const gmailApi = getGmailApi(account.secret) const [labelId, message] = await Promise.all([ findLabelId(gmailApi, input.label), getMessageMetadata(gmailApi, input.messageId), ]) return message.labelIds?.includes(labelId) ?? false })