import {z} from 'zod' export type GetApiCasesJobsArgs = {case_template_id?: string | undefined, status?: (Array<'pending' | 'succeeded' | 'failed' | 'canceled'>) | undefined, order_by?: ('created_at' | 'finished_at') | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiCasesJobsOptions = {baseUrl: string, backendAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCasesJobsResponse = z.object({data: z.object({items: z.array(z.object({case_template_id: z.string(), created_at: z.string(), error_message: z.string().optional(), finished_at: z.string().optional(), id: z.string(), inputs: z.array(z.object({definition: z.object({choices: z.array(z.string()), default_value: z.string(), description: z.string(), label: z.string(), max: z.number().optional(), min: z.number().optional(), unit: z.string().optional(), value_type: z.enum(["number", "smiles", "string", "tuple", "array", "enum", "file"]), variable_name: z.string()}), value: z.string()})), outputs: z.array(z.object({definition: z.object({cell_index: z.number().int().gte(0).optional(), description: z.string(), figure_index: z.number().int().gte(0).optional(), name: z.string(), type: z.enum(["number", "image"]), unit: z.string().optional(), variable_name: z.string().optional()}), value: z.string()})), remotejob: z.object({finished_at: z.string().optional(), id: z.string(), input_path: z.string(), output_path: z.string(), started_at: z.string().optional(), status: z.enum(["waiting", "initializing", "running", "completed", "failed", "oomkilled", "terminating", "canceled"])}), status: z.enum(["pending", "succeeded", "failed", "canceled"]), title: z.string().max(255), updated_at: z.string(), validations: z.array(z.object({definition: z.object({cell_index: z.number().int().gte(0).optional(), method: z.literal("prompt"), name: z.string(), prompt: z.string()}), reason: z.string(), result: z.boolean()}))})), limit: z.number().int().gte(1), offset: z.number().int().gte(0), total: z.number().int().gte(0)})}); 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 the user's case results * * GET /cases/jobs */ export const getApiCasesJobs = async (args: GetApiCasesJobsArgs, options: GetApiCasesJobsOptions) => { const {baseUrl, fetch: fetchFn = fetch, backendAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.case_template_id != null) query.append("case_template_id", String(args.case_template_id)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (backendAuth !== undefined) cookies.push("mtls-prd=" + encodeURIComponent(backendAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/cases/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(getApiCasesJobsResponse, await res.json()) };