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 IntegrationConnectionsList = Array; export type PoliciesList = Array; export type RevisionConfiguration = {active?: string | undefined, canary?: string | undefined, canaryPercent?: number | undefined, stickySessionTtl?: number | undefined, traffic?: number | undefined}; export type Env = {name?: string | undefined, secret?: boolean | undefined, value?: string | undefined}; export type Port = {name?: string | undefined, protocol?: ('HTTP' | 'TCP' | 'UDP' | 'TLS') | undefined, target: number}; export type Ports = Array; export type FunctionRuntime = {envs?: Array | undefined, generation?: ('mk2' | 'mk3') | undefined, image?: string | undefined, maxScale?: number | undefined, memory?: number | undefined, minScale?: number | undefined, ports?: Ports | undefined, transport?: ('websocket' | 'http-stream') | undefined}; export type TriggerConfigurationTask = Record; export type TriggerConfiguration = {authenticationType?: string | undefined, callbackSecret?: string | undefined, callbackUrl?: string | undefined, path?: string | undefined, retry?: number | undefined, schedule?: string | undefined, tasks?: Array | undefined, timeout?: number | undefined}; export type Trigger = {configuration?: TriggerConfiguration | undefined, enabled?: boolean | undefined, id?: string | undefined, type?: ('http' | 'http-async' | 'cron') | undefined}; export type Triggers = Array; export type FunctionSpec = {enabled?: boolean | undefined, integrationConnections?: IntegrationConnectionsList | undefined, policies?: PoliciesList | undefined, public?: boolean | undefined, region?: string | undefined, revision?: RevisionConfiguration | undefined, runtime?: FunctionRuntime | undefined, triggers?: Triggers | undefined}; export type Status = 'DELETING' | 'TERMINATED' | 'FAILED' | 'DEACTIVATED' | 'DEACTIVATING' | 'UPLOADING' | 'BUILDING' | 'DEPLOYING' | 'DEPLOYED' | 'BUILT'; export type Function = {events?: CoreEvents | undefined, metadata: Metadata, spec: FunctionSpec, status?: Status | undefined}; export type UpdateApiFunctionsFunctionNameArgs = {body: Function}; export type UpdateApiFunctionsFunctionNameOptions = {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 integrationConnectionsList = z.array(z.string()); export const policiesList = z.array(z.string()); export const revisionConfiguration = z.object({active: z.string().optional(), canary: z.string().optional(), canaryPercent: z.number().int().optional(), stickySessionTtl: z.number().int().optional(), traffic: z.number().int().optional()}); export const env = z.object({name: z.string().optional(), secret: z.boolean().optional(), value: z.string().optional()}); export const port = z.object({name: z.string().optional(), protocol: z.enum(["HTTP", "TCP", "UDP", "TLS"]).optional(), target: z.number().int()}); export const ports = z.array(port); export const functionRuntime = z.object({envs: z.array(env).optional(), generation: z.enum(["mk2", "mk3"]).optional(), image: z.string().optional(), maxScale: z.number().int().optional(), memory: z.number().int().optional(), minScale: z.number().int().optional(), ports: ports.optional(), transport: z.enum(["websocket", "http-stream"]).optional()}); export const triggerConfigurationTask = z.object({}); export const triggerConfiguration = z.object({authenticationType: z.string().optional(), callbackSecret: z.string().optional(), callbackUrl: z.string().optional(), path: z.string().optional(), retry: z.number().int().optional(), schedule: z.string().optional(), tasks: z.array(triggerConfigurationTask).optional(), timeout: z.number().int().optional()}); export const trigger = z.object({configuration: triggerConfiguration.optional(), enabled: z.boolean().optional(), id: z.string().optional(), type: z.enum(["http", "http-async", "cron"]).optional()}); export const triggers = z.array(trigger); export const functionSpec = z.object({enabled: z.boolean().optional(), integrationConnections: integrationConnectionsList.optional(), policies: policiesList.optional(), public: z.boolean().optional(), region: z.string().optional(), revision: revisionConfiguration.optional(), runtime: functionRuntime.optional(), triggers: triggers.optional()}); export const status = z.enum(["DELETING", "TERMINATED", "FAILED", "DEACTIVATED", "DEACTIVATING", "UPLOADING", "BUILDING", "DEPLOYING", "DEPLOYED", "BUILT"]); export const function = z.object({events: coreEvents.optional(), metadata: metadata, spec: functionSpec, 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 }; /** * Update MCP server * * PUT /functions/{functionName} * * Also accepts auth: ApiKeyAuth */ export const updateApiFunctionsFunctionName = async (args: UpdateApiFunctionsFunctionNameArgs, options?: UpdateApiFunctionsFunctionNameOptions) => { 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(/\/+$/, '')}/functions/${encodeURIComponent(String(args.functionName))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(function, await res.json()) };