import type { Encodable } from "@automate.ax/codec" import type { Vercel } from "@vercel/sdk" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getVercelApi, getVercelTeamId, vercelAccountOptions } from "../lib/api" import { defineScopedVercelSdkAction } from "../lib/action" import { VERCEL_LOG_DRAIN_WRITE_SCOPE } from "../lib/scopes" type Drains = Vercel["drains"] type CreateDrainBody = NonNullable< Parameters[0]["requestBody"] > const CREATE_VERCEL_DRAIN_INPUT_SCHEMA = z.object({ /** Optional provider delivery configuration. */ delivery: z.custom>().optional(), /** Optional provider filter configuration. */ filter: z.custom>().optional(), /** Display name for the drain. */ name: z.string().min(1), /** Project IDs when `projects` is `some`. */ projectIds: z.string().array().optional(), /** Whether the drain receives events from all or selected projects. */ projects: z.enum(["all", "some"]), /** Optional provider sampling rules. */ sampling: z.custom>().optional(), /** Schema versions keyed by event family. */ schemas: z.record(z.string(), z.object({ version: z.string().min(1) })), /** Optional provider source metadata. */ source: z.custom>().optional(), /** Team ID. OAuth accounts default to the installed team. */ teamId: z.string().min(1).optional(), /** Optional provider transform references. */ transforms: z.custom>().optional(), }) /** Lists Vercel drains. */ export const listVercelDrains = defineScopedVercelSdkAction({ description: "Lists drains in the selected account or team.", handler: (api, request: Parameters[0]) => api.drains.getDrains(request), name: "List Vercel drains", replaySafety: "safe", scope: VERCEL_LOG_DRAIN_WRITE_SCOPE, }) /** Gets one Vercel drain. */ export const getVercelDrain = defineScopedVercelSdkAction({ description: "Gets the configuration and status of one drain.", handler: (api, request: Parameters[0]) => api.drains.getDrain(request), name: "Get Vercel drain", replaySafety: "safe", scope: VERCEL_LOG_DRAIN_WRITE_SCOPE, }) /** Creates a Vercel drain. */ export const createVercelDrain = defineAction("Create Vercel drain") .describe("Creates a drain with filters and a delivery destination.") .account("vercel", vercelAccountOptions(VERCEL_LOG_DRAIN_WRITE_SCOPE)) .input(CREATE_VERCEL_DRAIN_INPUT_SCHEMA) .output(z.custom> & Encodable>()) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => getVercelApi(account).drains.createDrain({ requestBody: { name: input.name, projects: input.projects, schemas: input.schemas, ...(input.delivery && { delivery: input.delivery }), ...(input.filter && { filter: input.filter }), ...(input.projectIds && { projectIds: input.projectIds }), ...(input.sampling && { sampling: input.sampling }), ...(input.source && { source: input.source }), ...(input.transforms && { transforms: input.transforms }), }, ...(getVercelTeamId(account, input.teamId) && { teamId: getVercelTeamId(account, input.teamId), }), }), ) /** Updates one Vercel drain. */ export const updateVercelDrain = defineScopedVercelSdkAction({ description: "Updates the filters, delivery, or state of one drain.", handler: (api, request: Parameters[0]) => api.drains.updateDrain(request), name: "Update Vercel drain", replaySafety: "unsafe", scope: VERCEL_LOG_DRAIN_WRITE_SCOPE, }) /** Deletes one Vercel drain. */ export const deleteVercelDrain = defineScopedVercelSdkAction({ description: "Permanently deletes one drain.", handler: (api, request: Parameters[0]) => api.drains.deleteDrain(request), name: "Delete Vercel drain", replaySafety: "unsafe", scope: VERCEL_LOG_DRAIN_WRITE_SCOPE, }) /** Tests one Vercel drain configuration with sample events. */ export const testVercelDrain = defineScopedVercelSdkAction({ description: "Validates a drain delivery configuration with sample events.", handler: (api, request: Parameters[0]) => api.drains.testDrain(request), name: "Test Vercel drain", replaySafety: "safe", scope: VERCEL_LOG_DRAIN_WRITE_SCOPE, })