import {z} from 'zod' export type GetApiApiJobsJobIdArgs = {job_id: string}; export type GetApiApiJobsJobIdOptions = {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 jobDetailResponse = z.object({id: z.string(), status: z.enum(["pending", "in_progress", "completed", "failed", "cancelled"]), workflow: z.record(z.string(), z.unknown()).optional(), execution_error: executionError.optional(), create_time: z.number().int(), update_time: z.number().int(), outputs: z.record(z.string(), z.unknown()).optional(), preview_output: z.record(z.string(), z.unknown()).optional(), outputs_count: z.number().int().optional(), workflow_id: z.string().optional(), execution_status: z.record(z.string(), z.unknown()).optional(), execution_meta: z.record(z.string(), z.unknown()).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 full job details * * GET /api/jobs/{job_id} */ export const getApiApiJobsJobId = async (args: GetApiApiJobsJobIdArgs, options?: GetApiApiJobsJobIdOptions) => { const {baseUrl = "https://api.comfy.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/jobs/${encodeURIComponent(String(args.job_id))}${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(jobDetailResponse, await res.json()) };