import {z} from 'zod' export type LocationType = 'local' | 'k8s' | 'managed'; export type GetApiV1WorkflowsDeploymentsArgs = {active_only?: boolean | undefined, is_hardened?: (boolean | null) | undefined, workflow_name?: (string | null) | undefined, created_by?: (string | null) | undefined, owner?: (string | null) | undefined, location_types?: (Array | null) | undefined, search?: (string | null) | undefined, order_by?: ('updated_at' | 'created_at' | null) | undefined, order?: ('asc' | 'desc') | undefined, limit?: (number | null) | undefined, cursor?: (string | null) | undefined, workspace_id?: (string | null) | undefined}; export type GetApiV1WorkflowsDeploymentsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const locationType = z.enum(["local", "k8s", "managed"]); export const deploymentLocation = z.object({location_type: locationType, k8s_cluster: z.string().nullable().optional(), k8s_namespace: z.string().nullable().optional()}); export const deploymentSecretBinding = z.object({env_var_name: z.string(), reference: z.string()}); export const deploymentMistralCloudBackendSpec = z.object({type: z.enum(["mistral_cloud", "koyeb"]).optional(), build_directory: z.string().nullable().optional(), dockerfile_path: z.string().nullable().optional(), secrets: z.array(deploymentSecretBinding).nullable().optional()}); export const deploymentK8sBackendSpec = z.object({type: z.literal("kubernetes").optional(), entrypoint: z.string().nullable().optional(), working_dir: z.string().nullable().optional()}); export const gitCommitAuthor = z.object({name: z.string().nullable().optional(), username: z.string().nullable().optional(), html_url: z.string().nullable().optional()}); export const gitCommitMetadata = z.object({sha: z.string(), message: z.string().nullable().optional(), author: gitCommitAuthor.nullable().optional(), html_url: z.string().nullable().optional()}); export const deploymentWorkerSpecResponse = z.object({github_url: z.string(), type: z.string().optional(), revision: z.string().nullable().optional(), backend_spec: z.discriminatedUnion("type", [deploymentMistralCloudBackendSpec, deploymentK8sBackendSpec]), restarted_at: z.string().nullable().optional(), commit: gitCommitMetadata.nullable().optional()}); export const deploymentResourceConfig = z.object({replicas: z.number().int().nullable().optional()}); export const deploymentBuildState = z.object({phase: z.string().nullable().optional(), commit_sha: z.string().nullable().optional(), image: z.string().nullable().optional(), message: z.string().nullable().optional(), started_at: z.string().nullable().optional(), finished_at: z.string().nullable().optional()}); export const deploymentObservedState = z.object({phase: z.string().nullable().optional(), available_replicas: z.number().int().optional(), ready_replicas: z.number().int().optional(), endpoint: z.string().nullable().optional(), message: z.string().nullable().optional(), last_seen: z.string().nullable().optional(), deployed_revision: z.string().nullable().optional(), generation: z.number().int().nullable().optional(), build_state: deploymentBuildState.nullable().optional()}); export const principalType = z.enum(["api_key", "service_account", "user"]); export const managedDeploymentResponse = z.object({service_id: z.string(), name: z.string(), spec: deploymentWorkerSpecResponse, resources: deploymentResourceConfig, status: deploymentObservedState, created_at: z.string(), updated_at: z.string(), stopped: z.boolean().optional(), rollout_status: z.string().nullable().optional(), created_by: z.string().nullable().optional(), updated_by: z.string().nullable().optional(), deployed_by: z.string().nullable().optional(), deployed_at: z.string().nullable().optional(), is_hardened: z.boolean().optional(), runtime_credential_id: z.string().nullable().optional(), runtime_principal_type: principalType.nullable().optional()}); export const deploymentResponse = z.object({id: z.string(), name: z.string(), is_active: z.boolean(), is_hardened: z.boolean().optional(), created_at: z.string(), last_heartbeat: z.string(), updated_at: z.string(), owner: z.string().nullable().optional(), location: deploymentLocation.nullable().optional(), worker_count: z.number().int().optional(), active_worker_count: z.number().int().optional(), locations: z.array(locationType).optional(), managed: managedDeploymentResponse.nullable().optional()}); export const deploymentListResponse = z.object({deployments: z.array(deploymentResponse), next_cursor: z.string().nullable(), workspace_id: 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 }; /** * List Deployments * * GET /v1/workflows/deployments */ export const getApiV1WorkflowsDeployments = async (args: GetApiV1WorkflowsDeploymentsArgs, options?: GetApiV1WorkflowsDeploymentsOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.active_only != null) query.append("active_only", String(args.active_only)) if (args.is_hardened != null) query.append("is_hardened", String(args.is_hardened)) if (args.workflow_name != null) query.append("workflow_name", String(args.workflow_name)) if (args.created_by != null) query.append("created_by", String(args.created_by)) if (args.owner != null) query.append("owner", String(args.owner)) for (const item of args.location_types ?? []) query.append("location_types", String(item)) if (args.search != null) query.append("search", String(args.search)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workflows/deployments${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(deploymentListResponse, await res.json()) };