import {z} from 'zod' export type AppRoutesV3CompatabilityRunIOType = 'integer' | 'string' | 'fp' | 'dictionary' | 'boolean' | 'none' | 'array' | 'pkl' | 'file'; export type AppRoutesV3CompatabilityRunInput = {type: AppRoutesV3CompatabilityRunIOType, value: unknown, file_name?: string | undefined, file_path?: string | undefined, file_url?: string | undefined}; export type AppRoutesV3CompatabilityRunCreate = {pipeline_id_or_pointer: string, input_data: Array, async_run?: boolean | undefined}; export type CreateApiV3RunsArgs = {body: AppRoutesV3CompatabilityRunCreate}; export type CreateApiV3RunsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const appRoutesV3CompatabilityRunState = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(22), z.literal(16), z.literal(11), z.literal(14), z.literal(15), z.literal(13), z.literal(21), z.literal(10), z.literal(12), z.literal(17), z.literal(18), z.literal(19), z.literal(23), z.literal(20)]); export const runError = z.object({exception: z.string(), traceback: z.string().optional()}); export const appRoutesV3CompatabilityRunIOType = z.enum(["integer", "string", "fp", "dictionary", "boolean", "none", "array", "pkl", "file"]); export const appRoutesV3CompatabilityRunOutputFile = z.object({name: z.string(), path: z.string(), url: z.string(), size: z.number().int()}); export const appRoutesV3CompatabilityRunOutput = z.object({type: appRoutesV3CompatabilityRunIOType, value: z.unknown(), file: appRoutesV3CompatabilityRunOutputFile.optional()}); export const runResult = z.object({run_id: z.string(), outputs: z.array(appRoutesV3CompatabilityRunOutput)}); export const appRoutesV3CompatabilityRunInput = z.object({type: appRoutesV3CompatabilityRunIOType, value: z.unknown(), file_name: z.string().optional(), file_path: z.string().optional(), file_url: z.string().optional()}); export const run = z.object({id: z.string(), created_at: z.string(), pipeline_id: z.string(), environment_id: z.string(), environment_hash: z.string(), state: appRoutesV3CompatabilityRunState, error: runError.optional(), result: runResult.optional(), input_data: z.array(appRoutesV3CompatabilityRunInput).optional()}); 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 }; /** * Submit Run * * POST /v3/runs * * Also accepts auth: APIKeyCookie */ export const createApiV3Runs = async (args: CreateApiV3RunsArgs, options?: CreateApiV3RunsOptions) => { const {baseUrl = "https://www.mystic.ai", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/runs${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(run, await res.json()) };