import {z} from 'zod' export type WorkflowWritable = {name?: (string | null) | undefined, parent_uuid?: (string | null) | undefined, notes?: (string | null) | undefined, starred?: (boolean | null) | undefined, public?: (boolean | null) | undefined, shared_with_rowan?: (boolean | null) | undefined, object_type?: (string | null) | undefined, object_data?: (Record | null) | undefined, email_when_complete?: (boolean | null) | undefined, max_credits?: (number | null) | undefined, webhook_url?: (string | null) | undefined}; export type CreateApiWorkflowSlugArgs = {slug: string, access_token?: (string | null) | undefined, body: WorkflowWritable}; export type CreateApiWorkflowSlugOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const workflow = z.object({name: z.string().nullable().optional(), parent_uuid: z.string().nullable().optional(), notes: z.string().nullable().optional(), starred: z.boolean().nullable().optional(), public: z.boolean().nullable().optional(), shared_with_rowan: z.boolean().nullable().optional(), object_type: z.string(), object_data: z.object({}).nullable().optional(), email_when_complete: z.boolean().nullable().optional(), max_credits: z.number().int().nullable().optional(), created_at: z.string(), updated_at: z.string().nullable().optional(), started_at: z.string().nullable().optional(), completed_at: z.string().nullable().optional(), queued_at: z.string().nullable().optional(), deleted_at: z.string().nullable().optional(), uuid: z.string(), elapsed: z.number().nullable().optional(), credits_charged: z.number(), object_status: z.number().int().nullable().optional(), object_logfile: z.string(), is_free: z.boolean().optional(), user_uuid: z.string().nullable().optional(), resubmitted_from_uuid: z.string().nullable().optional(), submission_group_uuid: z.string().nullable().optional(), run_by_username: z.string().nullable().optional(), public_until: z.string().nullable().optional(), compute_hardware: z.string().nullable().optional(), execution_backend: z.enum(["modal", "aws"]).nullable().optional(), retry_count: z.number().int().optional(), is_temporarily_public: z.boolean()}); 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 }; /** * Update Workflow * * POST /workflow/{slug} */ export const createApiWorkflowSlug = async (args: CreateApiWorkflowSlugArgs, options: CreateApiWorkflowSlugOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.access_token != null) cookies.push("access_token=" + encodeURIComponent(String(args.access_token))) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/workflow/${encodeURIComponent(String(args.slug))}${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(workflow, await res.json()) };