import {z} from 'zod' export type PythonVersion = '3.12' | '3.13' | '3.14'; export type Environment = {python_version?: PythonVersion | undefined, pypi_dependencies?: Array | undefined, environment_variables?: {[key: string]: string} | undefined}; export type Resources = {cpu?: (number | null) | undefined, memory_mib?: (number | null) | undefined, gpu?: (string | null) | undefined, timeout_seconds?: number | undefined}; export type SerializableValue = {module: string, qualname: string, serialized_value: string, kind?: 'serialized' | undefined}; export type JsonValue = unknown; export type LiteralValue = {kind?: 'literal' | undefined, value: JsonValue}; export type ExecutionValue = SerializableValue | LiteralValue; export type ExecutionArguments = {positional?: Array | undefined, keyword?: {[key: string]: ExecutionValue} | undefined}; export type ExecutionRequest = {serialized_function: string, serialization_python_version: PythonVersion, environment: Environment, resources?: Resources | undefined, arguments?: ExecutionArguments | undefined}; export type CreateApiV1ExecutionJobsArgs = {body: ExecutionRequest}; export type CreateApiV1ExecutionJobsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const iD = z.union([z.string(), z.string()]); export const jobStatus = z.enum(["pending", "running", "completed", "failed", "cancelled"]); export const jobSubmissionResponse = z.object({job_id: iD, status: jobStatus, message: z.string()}); 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 }; /** * Create Execution Job * * POST /v1/execution_jobs */ export const createApiV1ExecutionJobs = async (args: CreateApiV1ExecutionJobsArgs, options: CreateApiV1ExecutionJobsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/execution_jobs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(jobSubmissionResponse, await res.json()) };