import {z} from 'zod' export type BundleAgent = {client?: (string | null) | undefined, description?: string | undefined, instruction?: string | undefined, key: string, mcps?: Array | undefined, model?: (string | null) | undefined, name: string, reasoning_effort?: (string | null) | undefined, reasoning_max_tokens?: (number | null) | undefined, skills?: Array | undefined, toolsets?: Array | undefined}; export type BundleAutomation = {agent: string, cron: string, enabled?: boolean | undefined, key: string, prompt: string, timezone?: string | undefined, type?: 'cron' | undefined}; export type BundleChannel = {agent: string, bindings?: {[key: string]: string} | undefined, enabled?: boolean | undefined, key: string, name: string, prompt?: string | undefined, type?: ('telegram' | 'generic') | undefined}; export type BundleMcp = {bindings?: {[key: string]: string} | undefined, catalog?: (string | null) | undefined, context_secret?: (string | null) | undefined, json_spec?: ({[key: string]: unknown} | null) | undefined, key: string, name: string}; export type BundleMetadata = {capabilities?: Array | undefined, category?: (string | null) | undefined, developer?: (string | null) | undefined, icon?: (string | null) | undefined, privacy_url?: (string | null) | undefined, terms_url?: (string | null) | undefined, website?: (string | null) | undefined}; export type BundlePolicy = {condition?: (string | null) | undefined, effect: 'allow' | 'deny' | 'cap' | 'approval' | 'safety', enabled?: boolean | undefined, key: string, message?: (string | null) | undefined, params?: {[key: string]: unknown} | undefined, priority?: number | undefined, subject?: string | undefined, target: string}; export type SetupFieldType = 'secret' | 'string' | 'number' | 'boolean' | 'select'; export type SetupField = {default: unknown, help?: (string | null) | undefined, key: string, label: string, max?: (number | null) | undefined, min?: (number | null) | undefined, options?: (Array | null) | undefined, required?: boolean | undefined, type?: SetupFieldType | undefined}; export type BundleSkill = {content?: (string | null) | undefined, files?: ({[key: string]: string} | null) | undefined, key: string, name: string, source_type?: ('content' | 'files' | 'github') | undefined, source_url?: (string | null) | undefined}; export type BundleInput = {agents?: Array | undefined, automations?: Array | undefined, channels?: Array | undefined, description?: string | undefined, display_name?: (string | null) | undefined, mcps?: Array | undefined, metadata?: BundleMetadata | undefined, name: string, policies?: Array | undefined, schema_version?: string | undefined, setup?: Array | undefined, skills?: Array | undefined}; export type InstallRequest = {bundle: BundleInput, plan_digest: string, setup_values?: {[key: string]: unknown} | undefined}; export type CreateApiV1WorkspacesWorkspaceBundlesInstallArgs = {workspace: string, body: InstallRequest}; export type CreateApiV1WorkspacesWorkspaceBundlesInstallOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const installAction = z.enum(["created", "updated", "unchanged", "orphaned", "reused", "skipped"]); export const entityKind = z.enum(["mcp", "skill", "agent", "channel", "automation", "policy"]); export const installedEntity = z.object({action: installAction, detail: z.string().nullable().optional(), fingerprint: z.string().nullable().optional(), id: z.string().nullable().optional(), key: z.string(), kind: entityKind, name: z.string()}); export const installResult = z.object({bundle_name: z.string(), entities: z.array(installedEntity).optional(), installed_bundle_id: z.string().nullable().optional(), setup_values: z.record(z.string(), z.unknown()).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 }; /** * Install Bundle * * POST /v1/workspaces/{workspace}/bundles/install */ export const createApiV1WorkspacesWorkspaceBundlesInstall = async (args: CreateApiV1WorkspacesWorkspaceBundlesInstallArgs, options: CreateApiV1WorkspacesWorkspaceBundlesInstallOptions) => { const {baseUrl, 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(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace))}/bundles/install${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(installResult, await res.json()) };