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, vercelAccessTokenAccountOptions } from "../lib/api" import { vercelObjectSchema } from "../lib/schemas" const VERCEL_RUNTIME_LOG_SCHEMA = vercelObjectSchema< Awaited> & Encodable >( z.looseObject({ level: z.enum(["debug", "error", "fatal", "info", "trace", "warning"]), message: z.string(), rowId: z.string(), timestampInMs: z.number(), }), ) /** Gets runtime log data for one Vercel deployment. */ export const getVercelRuntimeLogs = defineAction("Get Vercel runtime logs") .describe("Gets runtime log data for one deployment.") .account("vercel", vercelAccessTokenAccountOptions()) .input( z.object({ /** Deployment ID. */ deploymentId: z.string().min(1), /** Project ID. */ projectId: z.string().min(1), /** Team ID for a team-owned deployment. */ teamId: z.string().min(1).optional(), }), ) .output(VERCEL_RUNTIME_LOG_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getVercelApi(account).logs.getRuntimeLogs({ deploymentId: input.deploymentId, projectId: input.projectId, ...(input.teamId && { teamId: input.teamId }), }), ) /** Invalidates Vercel edge-cache entries associated with cache tags. */ export const invalidateVercelEdgeCacheByTags = defineAction( "Invalidate Vercel edge cache by tags", ) .describe("Marks cache entries with the selected tags as stale.") .account("vercel", vercelAccessTokenAccountOptions()) .input( z.object({ /** Project ID or name. */ project: z.string().min(1), /** Cache tags. */ tags: z.string().min(1).array().min(1), /** Production or preview cache. */ target: z.enum(["preview", "production"]).optional(), /** Team ID for a team-owned project. */ teamId: z.string().min(1).optional(), }), ) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getVercelApi(account).edgeCache.invalidateByTags({ projectIdOrName: input.project, requestBody: { tags: input.tags, ...(input.target && { target: input.target }), }, ...(input.teamId && { teamId: input.teamId }), }), ) /** Invalidates optimized Vercel images by source URL. */ export const invalidateVercelEdgeCacheBySourceImages = defineAction( "Invalidate Vercel edge cache by source images", ) .describe("Marks transformed images for the selected sources as stale.") .account("vercel", vercelAccessTokenAccountOptions()) .input( z.object({ /** Project ID or name. */ project: z.string().min(1), /** Source image URLs. */ sourceImages: z.url().array().min(1), /** Team ID for a team-owned project. */ teamId: z.string().min(1).optional(), }), ) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getVercelApi(account).edgeCache.invalidateBySrcImages({ projectIdOrName: input.project, requestBody: { srcImages: input.sourceImages }, ...(input.teamId && { teamId: input.teamId }), }), )