import {z} from 'zod' export type GetApiApiJobsArgs = {status?: string | undefined, workflow_id?: string | undefined, output_type?: ('image' | 'video' | 'audio') | undefined, sort_by?: ('create_time' | 'execution_time') | undefined, sort_order?: ('asc' | 'desc') | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiApiJobsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const executionError = z.object({node_id: z.string(), node_type: z.string(), exception_message: z.string(), exception_type: z.string(), traceback: z.array(z.string()), current_inputs: z.record(z.string(), z.unknown()), current_outputs: z.record(z.string(), z.unknown())}); export const jobEntry = z.object({id: z.string(), status: z.enum(["pending", "in_progress", "completed", "failed", "cancelled"]), execution_error: executionError.optional(), create_time: z.number().int(), preview_output: z.record(z.string(), z.unknown()).optional(), outputs_count: z.number().int().optional(), workflow_id: z.string().optional(), execution_start_time: z.number().int().optional(), execution_end_time: z.number().int().optional()}); export const paginationInfo = z.object({offset: z.number().int().gte(0), limit: z.number().int().gte(1), total: z.number().int().gte(0), has_more: z.boolean()}); export const jobsListResponse = z.object({jobs: z.array(jobEntry), pagination: paginationInfo}); 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 }; /** * List jobs with pagination and filtering * * GET /api/jobs */ export const getApiApiJobs = async (args: GetApiApiJobsArgs, options?: GetApiApiJobsOptions) => { const {baseUrl = "https://api.comfy.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.workflow_id != null) query.append("workflow_id", String(args.workflow_id)) if (args.output_type != null) query.append("output_type", String(args.output_type)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/api/jobs${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(jobsListResponse, await res.json()) };