import {z} from 'zod' export type TaskState = 'new' | 'active' | 'retry' | 'expired' | 'terminated' | 'complete' | 'queue_error' | 'blocked' | 'unreachable'; export type GetApiV1TasksArgs = {state?: Array | undefined, queue?: string | undefined, type?: string | undefined, created_since?: string | undefined, limit?: number | undefined, stream?: 'ndjson' | undefined}; export type GetApiV1TasksOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const taskResult = z.object({payload: z.unknown(), completed: z.string()}); export const taskState = z.enum(["new", "active", "retry", "expired", "terminated", "complete", "queue_error", "blocked", "unreachable"]); export const task = z.object({id: z.string(), type: z.string(), queue: z.string().optional(), dependencies: z.array(z.string()).optional(), dependency_results: z.record(z.string(), taskResult).optional(), load_dependencies: z.boolean().optional(), payload: z.string(), deadline: z.string().optional(), max_tries: z.number().int().optional(), result: taskResult.optional(), state: taskState, created: z.string(), tried: z.string().optional(), tries: z.number().int().optional(), last_err: z.string().optional(), signature: z.string().optional()}); export const taskListResponse = z.object({tasks: z.array(task), count: z.number().int()}); 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 tasks (snapshot). * * GET /v1/tasks */ export const getApiV1Tasks = async (args: GetApiV1TasksArgs, options: GetApiV1TasksOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.state ?? []) query.append("state", String(item)) if (args.queue != null) query.append("queue", String(args.queue)) if (args.type != null) query.append("type", String(args.type)) if (args.created_since != null) query.append("created_since", String(args.created_since)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.stream != null) query.append("stream", String(args.stream)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tasks${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(taskListResponse, await res.json()) };