import {z} from 'zod' export type CoreEvent = {canaryRevision?: string | undefined, message?: string | undefined, revision?: string | undefined, status?: string | undefined, time?: string | undefined, type?: string | undefined}; export type CoreEvents = Array; export type MetadataLabels = {[key: string]: string}; export type Metadata = {createdAt?: string | undefined, updatedAt?: string | undefined, createdBy?: string | undefined, updatedBy?: string | undefined, displayName?: string | undefined, externalId?: string | undefined, labels?: MetadataLabels | undefined, name: string, plan?: string | undefined, url?: string | undefined, workspace?: string | undefined}; export type Flavor = {name?: string | undefined, type?: ('cpu' | 'gpu') | undefined}; export type Flavors = Array; export type IntegrationConnectionsList = Array; export type PoliciesList = Array; export type ModelRuntime = {endpointName?: string | undefined, generation?: ('mk3' | 'mk2') | undefined, model?: string | undefined, organization?: string | undefined, type?: ('hf_private_endpoint' | 'hf_public_endpoint' | 'huggingface' | 'public_model' | 'mcp' | 'openai' | 'anthropic' | 'gemini' | 'mistral' | 'deepseek' | 'cohere' | 'cerebras' | 'xai' | 'vertexai' | 'azure-openai-service' | 'azure-ai-inference' | 'azure-marketplace' | 'groq') | undefined}; export type ModelSpec = {enabled?: boolean | undefined, flavors?: Flavors | undefined, integrationConnections?: IntegrationConnectionsList | undefined, policies?: PoliciesList | undefined, runtime?: ModelRuntime | undefined, sandbox?: boolean | undefined}; export type Status = 'DELETING' | 'TERMINATED' | 'FAILED' | 'DEACTIVATED' | 'DEACTIVATING' | 'UPLOADING' | 'BUILDING' | 'DEPLOYING' | 'DEPLOYED' | 'BUILT'; export type Model = {events?: CoreEvents | undefined, metadata: Metadata, spec: ModelSpec, status?: Status | undefined}; export type CreateApiModelsArgs = {body: Model}; export type CreateApiModelsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const coreEvent = z.object({canaryRevision: z.string().optional(), message: z.string().optional(), revision: z.string().optional(), status: z.string().optional(), time: z.string().optional(), type: z.string().optional()}); export const coreEvents = z.array(coreEvent); export const metadataLabels = z.record(z.string(), z.string()); export const metadata = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), createdBy: z.string().optional(), updatedBy: z.string().optional(), displayName: z.string().optional(), externalId: z.string().optional(), labels: metadataLabels.optional(), name: z.string(), plan: z.string().optional(), url: z.string().optional(), workspace: z.string().optional()}); export const flavor = z.object({name: z.string().optional(), type: z.enum(["cpu", "gpu"]).optional()}); export const flavors = z.array(flavor); export const integrationConnectionsList = z.array(z.string()); export const policiesList = z.array(z.string()); export const modelRuntime = z.object({endpointName: z.string().optional(), generation: z.enum(["mk3", "mk2"]).optional(), model: z.string().optional(), organization: z.string().optional(), type: z.enum(["hf_private_endpoint", "hf_public_endpoint", "huggingface", "public_model", "mcp", "openai", "anthropic", "gemini", "mistral", "deepseek", "cohere", "cerebras", "xai", "vertexai", "azure-openai-service", "azure-ai-inference", "azure-marketplace", "groq"]).optional()}); export const modelSpec = z.object({enabled: z.boolean().optional(), flavors: flavors.optional(), integrationConnections: integrationConnectionsList.optional(), policies: policiesList.optional(), runtime: modelRuntime.optional(), sandbox: z.boolean().optional()}); export const status = z.enum(["DELETING", "TERMINATED", "FAILED", "DEACTIVATED", "DEACTIVATING", "UPLOADING", "BUILDING", "DEPLOYING", "DEPLOYED", "BUILT"]); export const model = z.object({events: coreEvents.optional(), metadata: metadata, spec: modelSpec, status: status.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 }; /** * Create model endpoint * * POST /models * * Also accepts auth: ApiKeyAuth */ export const createApiModels = async (args: CreateApiModelsArgs, options?: CreateApiModelsOptions) => { const {baseUrl = "https://api.blaxel.ai/v0", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/models${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(model, await res.json()) };