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 AnalyzeRequest = {bundle?: (BundleInput | null) | undefined, files?: ({[key: string]: string} | null) | undefined, setup_values?: {[key: string]: unknown} | undefined, source?: (string | null) | undefined, source_url?: (string | null) | undefined}; export type CreateApiV1WorkspacesWorkspaceBundlesAnalyzeArgs = {workspace: string, body: AnalyzeRequest}; export type CreateApiV1WorkspacesWorkspaceBundlesAnalyzeOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const bundleAgent = z.object({client: z.string().nullable().optional(), description: z.string().max(1000).optional(), instruction: z.string().max(20000).optional(), key: z.string().min(1), mcps: z.array(z.string()).optional(), model: z.string().nullable().optional(), name: z.string().min(1).max(255), reasoning_effort: z.string().nullable().optional(), reasoning_max_tokens: z.number().int().nullable().optional(), skills: z.array(z.string()).optional(), toolsets: z.array(z.string()).optional()}); export const bundleAutomation = z.object({agent: z.string().min(1), cron: z.string().min(1), enabled: z.boolean().optional(), key: z.string().min(1), prompt: z.string().min(1), timezone: z.string().optional(), type: z.literal("cron").optional()}); export const bundleChannel = z.object({agent: z.string().min(1), bindings: z.record(z.string(), z.string()).optional(), enabled: z.boolean().optional(), key: z.string().min(1), name: z.string().min(1), prompt: z.string().min(1).optional(), type: z.enum(["telegram", "generic"]).optional()}); export const bundleMcp = z.object({bindings: z.record(z.string(), z.string()).optional(), catalog: z.string().max(255).nullable().optional(), context_secret: z.string().nullable().optional(), json_spec: z.record(z.string(), z.unknown()).nullable().optional(), key: z.string().min(1), name: z.string().min(1).max(255)}); export const bundleMetadata = z.object({capabilities: z.array(z.string()).optional(), category: z.string().nullable().optional(), developer: z.string().nullable().optional(), icon: z.string().nullable().optional(), privacy_url: z.string().nullable().optional(), terms_url: z.string().nullable().optional(), website: z.string().nullable().optional()}); export const bundlePolicy = z.object({condition: z.string().nullable().optional(), effect: z.enum(["allow", "deny", "cap", "approval", "safety"]), enabled: z.boolean().optional(), key: z.string().min(1), message: z.string().nullable().optional(), params: z.record(z.string(), z.unknown()).optional(), priority: z.number().int().gte(-2147483648).lte(2147483647).optional(), subject: z.string().optional(), target: z.string().min(1)}); export const setupFieldType = z.enum(["secret", "string", "number", "boolean", "select"]); export const setupField = z.object({default: z.unknown(), help: z.string().nullable().optional(), key: z.string().min(1), label: z.string().min(1), max: z.number().nullable().optional(), min: z.number().nullable().optional(), options: z.array(z.string()).nullable().optional(), required: z.boolean().optional(), type: setupFieldType.optional()}); export const bundleSkill = z.object({content: z.string().nullable().optional(), files: z.record(z.string(), z.string()).nullable().optional(), key: z.string().min(1), name: z.string().min(1).max(255), source_type: z.enum(["content", "files", "github"]).optional(), source_url: z.string().nullable().optional()}); export const bundleOutput = z.object({agents: z.array(bundleAgent).optional(), automations: z.array(bundleAutomation).optional(), channels: z.array(bundleChannel).optional(), description: z.string().optional(), display_name: z.string().max(255).nullable().optional(), mcps: z.array(bundleMcp).optional(), metadata: bundleMetadata.optional(), name: z.string().min(1).max(255), policies: z.array(bundlePolicy).optional(), schema_version: z.string().optional(), setup: z.array(setupField).optional(), skills: z.array(bundleSkill).optional()}); export const entityKind = z.enum(["mcp", "skill", "agent", "channel", "automation", "policy"]); export const entityStatus = z.enum(["will_create", "will_update", "unchanged", "orphaned", "changes_not_applied", "already_exists", "unsupported"]); export const previewEntity = z.object({detail: z.string().nullable().optional(), key: z.string(), kind: entityKind, name: z.string(), status: entityStatus}); export const issueSeverity = z.enum(["block", "warn"]); export const previewIssue = z.object({entity_key: z.string().nullable().optional(), message: z.string(), severity: issueSeverity}); export const importPreview = z.object({bundle: bundleOutput, entities: z.array(previewEntity).optional(), installable: z.boolean(), issues: z.array(previewIssue).optional(), plan_digest: z.string().nullable().optional(), setup: z.array(setupField).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 }; /** * Analyze Bundle * * POST /v1/workspaces/{workspace}/bundles/analyze */ export const createApiV1WorkspacesWorkspaceBundlesAnalyze = async (args: CreateApiV1WorkspacesWorkspaceBundlesAnalyzeArgs, options: CreateApiV1WorkspacesWorkspaceBundlesAnalyzeOptions) => { 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/analyze${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(importPreview, await res.json()) };