import {z} from 'zod' export type MCPOAuthConfig = {grant_type?: ('authorization_code' | 'client_credentials') | undefined, client_id?: (string | null) | undefined, client_secret?: (string | null) | undefined, scopes?: (Array | null) | undefined, redirect_uri?: string | undefined, client_name?: string | undefined, client_uri?: (string | null) | undefined, authorization_url?: (string | null) | undefined, token_url?: (string | null) | undefined, access_token?: (string | null) | undefined, refresh_token?: (string | null) | undefined, token_storage_path?: (string | null) | undefined, use_token_cache?: boolean | undefined, open_browser?: boolean | undefined, callback_timeout?: number | undefined} | {[key: string]: unknown}; export type MCPConnection = {type?: (string | null) | undefined, url?: (string | null) | undefined, name?: (string | null) | undefined, tool_configurations?: ({[key: string]: unknown} | null) | undefined, authorization_token?: (string | null) | undefined, api_key?: (string | null) | undefined, api_key_header?: string | undefined, api_key_prefix?: (string | null) | undefined, auth_type?: ('none' | 'api_key' | 'bearer' | 'oauth' | 'custom' | null) | undefined, oauth?: (MCPOAuthConfig | null) | undefined, transport?: (string | null) | undefined, headers?: ({[key: string]: string} | null) | undefined, timeout?: (number | null) | undefined, sse_read_timeout?: (number | null) | undefined, tool_timeout?: (number | null) | undefined, command?: (string | null) | undefined, args?: (Array | null) | undefined, env?: ({[key: string]: string} | null) | undefined} | {[key: string]: unknown}; export type AgentSpecInput = {agent_name?: (string | null) | undefined, description?: (string | null) | undefined, system_prompt?: (string | null) | undefined, marketplace_prompt_id?: (string | null) | undefined, model_name?: (string | null) | undefined, fallback_models?: (Array | null) | undefined, fallback_model_name?: (string | null) | undefined, auto_generate_prompt?: (boolean | null) | undefined, max_tokens?: (number | null) | undefined, temperature?: (number | null) | undefined, role?: (string | null) | undefined, max_loops?: (number | string | null) | undefined, tools_list_dictionary?: (Array<{[key: string]: unknown}> | null) | undefined, selected_tools?: (string | Array | null) | undefined, mcp_url?: (string | MCPConnection | {[key: string]: unknown} | null) | undefined, streaming_on?: (boolean | null) | undefined, llm_args?: ({[key: string]: unknown} | null) | undefined, top_p?: (number | null) | undefined, dynamic_temperature_enabled?: (boolean | null) | undefined, mcp_config?: (MCPConnection | {[key: string]: unknown} | null) | undefined, mcp_configs?: (Array | null) | undefined, mcp_urls?: (Array | null) | undefined, tool_call_summary?: (boolean | null) | undefined, reasoning_effort?: ('minimal' | 'low' | 'medium' | 'high' | 'none' | 'xhigh' | 'ultra' | 'max' | 'None' | null) | undefined, thinking_tokens?: (number | null) | undefined, reasoning_enabled?: (boolean | null) | undefined, publish_to_marketplace?: (boolean | null) | undefined, use_cases?: (Array<{[key: string]: string}> | null) | undefined, tags?: (Array | null) | undefined, capabilities?: (Array | null) | undefined, category?: (string | null) | undefined, is_free?: (boolean | null) | undefined, price_usd?: (number | null) | undefined, handoffs?: (Array | null) | undefined}; export type DirectorSettings = {agent_name?: string | undefined, system_prompt?: string | undefined, planning_system_prompt?: string | undefined, temperature?: number | undefined, top_p?: number | undefined, max_tokens?: number | undefined}; export type SwarmSpec = {name?: (string | null) | undefined, description?: (string | null) | undefined, agents?: (Array | null) | undefined, max_loops?: (number | null) | undefined, swarm_type?: ('AgentRearrange' | 'MixtureOfAgents' | 'SequentialWorkflow' | 'ConcurrentWorkflow' | 'GroupChat' | 'MultiAgentRouter' | 'HierarchicalSwarm' | 'MajorityVoting' | 'CouncilAsAJudge' | 'HeavySwarm' | 'LLMCouncil' | 'DebateWithJudge' | 'RoundRobin' | 'PlannerWorkerSwarm' | null) | undefined, rearrange_flow?: (string | null) | undefined, task?: (string | null) | undefined, img?: (string | null) | undefined, tasks?: (Array | null) | undefined, messages?: (Array<{[key: string]: unknown}> | {[key: string]: unknown} | null) | undefined, stream?: (boolean | null) | undefined, heavy_swarm_question_agent_model_name?: (string | null) | undefined, heavy_swarm_worker_model_name?: (string | null) | undefined, heavy_swarm_variant?: ('default' | 'medium' | 'heavy' | null) | undefined, council_judge_model_name?: (string | null) | undefined, chairman_model?: (string | null) | undefined, multi_agent_collab_prompt?: (boolean | null) | undefined, heavy_swarm_max_loops?: (number | null) | undefined, list_all_agents?: (boolean | null) | undefined, director_model_name?: (string | null) | undefined, director_settings?: (DirectorSettings | null) | undefined}; export type CreateApiV1SwarmCompletionsArgs = {'x-api-key': string, body: SwarmSpec}; export type CreateApiV1SwarmCompletionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const swarmCompletion = z.object({job_id: z.string().nullable(), status: z.string().nullable(), swarm_name: z.string().nullable(), description: z.string().nullable(), swarm_type: z.string().nullable(), output: z.unknown(), number_of_agents: z.number().int().nullable(), execution_time: z.number().nullable(), usage: z.record(z.string(), z.unknown()).nullable(), tasks: z.array(z.string()).nullable().optional(), messages: z.union([z.array(z.record(z.string(), z.unknown())), z.record(z.string(), z.unknown())]).nullable().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 }; /** * Execute Swarm Completion * * POST /v1/swarm/completions */ export const createApiV1SwarmCompletions = async (args: CreateApiV1SwarmCompletionsArgs, options?: CreateApiV1SwarmCompletionsOptions) => { const {baseUrl = "https://api.swarms.world", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/swarm/completions${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(swarmCompletion, await res.json()) };