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 AgentCompletion = {agent_config: AgentSpecInput, task?: (string | null) | undefined, history?: ({[key: string]: unknown} | Array<{[key: string]: string}> | null) | undefined, img?: (string | null) | undefined, imgs?: (Array | null) | undefined, tools_enabled?: (Array | null) | undefined}; export type CreateApiV1AgentBatchCompletionsArgs = {'x-api-key': string, body: Array}; export type CreateApiV1AgentBatchCompletionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const agentBatchCompletionResponse = z.object({batch_id: z.string().nullable().optional(), total_requests: z.number().int().nullable().optional(), results: z.unknown(), execution_time: z.number().nullable().optional(), timestamp: z.string().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 Batch Agent Completions * * POST /v1/agent/batch/completions */ export const createApiV1AgentBatchCompletions = async (args: CreateApiV1AgentBatchCompletionsArgs, options?: CreateApiV1AgentBatchCompletionsOptions) => { 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/agent/batch/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(agentBatchCompletionResponse, await res.json()) };