import {z} from 'zod' export type AgentCreateRequest = {accessibility_enabled?: boolean | undefined, capture_enabled?: boolean | undefined, code?: string | undefined, from?: ('task' | 'script') | undefined, instructions?: string | undefined, max_cost_microdollars?: number | undefined, max_steps?: number | undefined, model?: string | undefined, name: string, on_failure?: ('fail' | 'ask' | 'repair') | undefined, output_schema?: {[key: string]: unknown} | undefined, pause_timeout_seconds?: number | undefined, recording_enabled?: boolean | undefined, source_run_id?: string | undefined, telemetry_enabled?: boolean | undefined, timeout_seconds?: number | undefined}; export type CreateApiAgentsArgs = {body: AgentCreateRequest}; export type CreateApiAgentsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const agent = z.object({accessibility_enabled: z.boolean(), capture_enabled: z.boolean(), created_at: z.string(), current_revision_id: z.string().optional(), id: z.string(), last_run_at: z.string().optional(), max_cost_microdollars: z.number().int(), max_steps: z.number().int(), model: z.string(), name: z.string(), output_schema: z.record(z.string(), z.unknown()).optional(), pause_timeout_seconds: z.number().int(), recording_enabled: z.boolean(), telemetry_enabled: z.boolean(), timeout_seconds: z.number().int(), updated_at: 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 }; /** * Create an agent * * POST /agents */ export const createApiAgents = async (args: CreateApiAgentsArgs, options?: CreateApiAgentsOptions) => { const {baseUrl = "https://api.axilio.ai/api/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/agents${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(agent, await res.json()) };