import {z} from 'zod' export type GetApiV1AutomationsRunsArgs = {automationRunId?: string | undefined, include?: 'logs' | undefined, automationId?: string | undefined, triggerEventId?: string | undefined, triggerInstanceId?: string | undefined, recipientEmail?: string | undefined, status?: ('pending' | 'running' | 'completed' | 'failed' | 'canceled') | undefined, mode?: ('live' | 'test') | undefined, from?: string | undefined, to?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiV1AutomationsRunsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const automationRunsListResponse = z.object({data: z.array(z.object({automationRunId: z.string().min(1).max(64), automationId: z.string().min(1).max(64), automationVersionId: z.string().min(1).max(64).optional(), triggerInstanceId: z.string().min(4).max(64).optional(), mode: z.enum(["live", "test"]), status: z.enum(["pending", "running", "completed", "failed", "canceled"]), recipientEmail: z.string().optional(), startedAt: z.string().optional(), completedAt: z.string().optional(), error: z.string().optional(), logs: z.array(z.object({automationRunId: z.string().min(1).max(64), nodeId: z.string().min(1), nodeName: z.string().min(1), nodeType: z.enum(["trigger", "wait", "filter", "split", "sendEmail"]), status: z.enum(["running", "success", "error", "skipped"]), orderIndex: z.number().int().gte(0), branch: z.enum(["left", "right"]).optional(), durationMs: z.number().int().gte(0).optional(), startedAt: z.string(), completedAt: z.string().optional(), error: z.string().optional()})).optional()})), pagination: z.object({limit: z.number().int().gte(1).lte(100), cursor: z.string().nullable(), hasMore: z.boolean()}).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get automation runs * * GET /v1/automations/runs * * Also accepts auth: apiKeyAuth */ export const getApiV1AutomationsRuns = async (args: GetApiV1AutomationsRunsArgs, options?: GetApiV1AutomationsRunsOptions) => { const {baseUrl = "https://brew.new/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.automationRunId != null) query.append("automationRunId", String(args.automationRunId)) if (args.include != null) query.append("include", String(args.include)) if (args.automationId != null) query.append("automationId", String(args.automationId)) if (args.triggerEventId != null) query.append("triggerEventId", String(args.triggerEventId)) if (args.triggerInstanceId != null) query.append("triggerInstanceId", String(args.triggerInstanceId)) if (args.recipientEmail != null) query.append("recipientEmail", String(args.recipientEmail)) if (args.status != null) query.append("status", String(args.status)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/automations/runs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(automationRunsListResponse, await res.json()) };