import {z} from 'zod' export type ComputationGraphInput = {module: string, qualname: string, serialized_value: string, name: string}; export type PythonVersion = '3.12' | '3.13' | '3.14'; export type GraphInputArgument = {kind?: 'graph_input' | undefined, input_name: string}; 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 ComputationNodeOutputArgument = {kind?: 'node_output' | undefined, node_key: string, output_name?: 'result' | undefined}; export type ComputationGraphArgument = GraphInputArgument | SerializableValue | LiteralValue | ComputationNodeOutputArgument; export type ComputationGraphArguments = {positional?: Array | undefined, keyword?: {[key: string]: ComputationGraphArgument} | undefined}; 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 ComputationGraphNode = {kind?: 'python' | undefined, key: string, node_type?: string | undefined, serialized_function: string, serialization_python_version: PythonVersion, arguments?: ComputationGraphArguments | undefined, environment: Environment, resources?: Resources | undefined, depends_on?: Array | undefined}; export type PlatformOperation = 'training.initialize' | 'training.fit' | 'training.prepare_trial' | 'training.reduce' | 'training.publish'; export type PlatformComputationGraphNode = {kind?: 'platform' | undefined, key: string, operation: PlatformOperation, arguments?: ComputationGraphArguments | undefined, depends_on?: Array | undefined}; export type ComputationGraphNodeSpec = ComputationGraphNode | PlatformComputationGraphNode; export type ComputationGraphOutput = {name: string, node_key: string, output_name?: 'result' | undefined}; export type ComputationGraphSpec = {inputs?: Array | undefined, nodes: Array, outputs: Array}; export type ComputationGraphDefinition = {kind?: 'computation_graph' | undefined, graph: ComputationGraphSpec}; export type ID = string | string; export type ReasoningDefinition = {kind?: 'reasoning' | undefined, model_id: ID, random_state?: number | undefined}; export type JobDefinition = ComputationGraphDefinition | ReasoningDefinition; export type CreateJobRequest = {definition: JobDefinition}; export type CreateApiV1JobsArgs = {body: CreateJobRequest}; export type CreateApiV1JobsOptions = {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 Job * * POST /v1/jobs */ export const createApiV1Jobs = async (args: CreateApiV1JobsArgs, options: CreateApiV1JobsOptions) => { 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/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()) };