import {z} from 'zod' export type BatchJobStatus = 'QUEUED' | 'RUNNING' | 'SUCCESS' | 'FAILED' | 'TIMEOUT_EXCEEDED' | 'CANCELLATION_REQUESTED' | 'CANCELLED'; export type GetApiV1BatchJobsArgs = {page?: number | undefined, page_size?: number | undefined, model?: (string | null) | undefined, agent_id?: (string | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, created_after?: (string | null) | undefined, created_by_me?: boolean | undefined, status?: (Array | null) | undefined, order_by?: ('created' | '-created') | undefined}; export type GetApiV1BatchJobsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const batchError = z.object({message: z.string(), count: z.number().int().optional()}); export const batchJobStatus = z.enum(["QUEUED", "RUNNING", "SUCCESS", "FAILED", "TIMEOUT_EXCEEDED", "CANCELLATION_REQUESTED", "CANCELLED"]); export const batchJob = z.object({id: z.string(), object: z.literal("batch").optional(), input_files: z.array(z.string()), metadata: z.record(z.string(), z.unknown()).nullable().optional(), endpoint: z.string(), model: z.string().nullable().optional(), agent_id: z.string().nullable().optional(), output_file: z.string().nullable().optional(), error_file: z.string().nullable().optional(), errors: z.array(batchError), outputs: z.array(z.record(z.string(), z.unknown())).nullable().optional(), status: batchJobStatus, created_at: z.number().int(), total_requests: z.number().int(), completed_requests: z.number().int(), succeeded_requests: z.number().int(), failed_requests: z.number().int(), started_at: z.number().int().nullable().optional(), completed_at: z.number().int().nullable().optional()}); export const listBatchJobsResponse = z.object({data: z.array(batchJob).optional(), object: z.literal("list").optional(), total: 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 }; /** * Get Batch Jobs * * GET /v1/batch/jobs */ export const getApiV1BatchJobs = async (args: GetApiV1BatchJobsArgs, options?: GetApiV1BatchJobsOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.model != null) query.append("model", String(args.model)) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.metadata != null) query.append("metadata", JSON.stringify(args.metadata)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_by_me != null) query.append("created_by_me", String(args.created_by_me)) 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 (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/batch/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(listBatchJobsResponse, await res.json()) };