import {z} from 'zod' export type RunPlaybookRequest = {file_ids: Array, review_mode?: ('first-party' | 'third-party') | undefined, representing_party?: string | undefined, check_ids?: Array | undefined, company_id?: string | undefined}; export type CreateApiPlaybooksIdRunArgs = {id: string, wait?: (number | null) | undefined, Prefer?: string | undefined, body: RunPlaybookRequest}; export type CreateApiPlaybooksIdRunOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const playbookCheckResult = z.object({checkId: z.string(), checkTitle: z.string(), evaluationResult: z.string(), documentPosition: z.number().nullable()}).and(z.record(z.string(), z.unknown())); export const apiDocument = z.object({file_id: z.string(), original_file_id: z.string().optional(), filename: z.string(), original_filename: z.string().optional(), signed_url: z.string(), download_url: z.string()}); export const runPlaybookResult = z.object({playbookTitle: z.string(), totalChecks: z.number(), results: z.array(playbookCheckResult), summary: z.object({flags: z.number(), fallbacks: z.number(), passes: z.number(), errors: z.number()}), documents: z.array(apiDocument).optional()}).nullable(); export const runPlaybookResponse = z.object({job_id: z.string(), kind: z.literal("playbooks/run"), status: z.enum(["pending", "running", "succeeded", "failed", "canceled"]), result: runPlaybookResult, error: z.object({code: z.string(), message: z.string()}).nullable(), created_at: z.string(), completed_at: z.string().nullable()}); 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 }; /** * Run a playbook against uploaded files * * POST /playbooks/{id}/run */ export const createApiPlaybooksIdRun = async (args: CreateApiPlaybooksIdRunArgs, options?: CreateApiPlaybooksIdRunOptions) => { const {baseUrl = "https://app.gc.ai/api/external/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.wait != null) query.append("wait", String(args.wait)) if (args.Prefer != null) headers.set("Prefer", String(args.Prefer)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/playbooks/${encodeURIComponent(String(args.id))}/run${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(runPlaybookResponse, await res.json()) };