import {z} from 'zod' export type RunRequest = {agent_id: string, message: string | Record | null, agent_history?: (Array | null) | undefined, thread_id?: (string | null) | undefined, hitl_decision?: ('approve' | 'edit' | 'reject' | null) | undefined}; export type CreateApiV1AgentsRunArgs = {body: RunRequest}; export type CreateApiV1AgentsRunOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const responseMeta = z.object({request_id: z.string().nullable().optional()}); export const toolCall = z.object({name: z.string(), arguments: z.object({}).nullable().optional(), id: z.string().nullable().optional()}); export const agentExecutionDetails = z.object({reasoning: z.string().nullable().optional(), toolcalls: z.array(toolCall).optional(), toolresponse: z.array(z.unknown()).optional(), llmresponse: z.string().nullable().optional(), hitl: z.union([z.string(), z.object({})]).nullable().optional()}); export const creditUsage = z.object({agent_name: z.string(), number_of_runs: z.number().int(), credit_consumed: z.number()}); export const usageStats = z.object({credits_charged: z.array(creditUsage).optional(), total_credits_charged: z.number().optional()}); export const additionalMetadata = z.object({thread_id: z.string().nullable().optional()}); export const agentResponse = z.object({request_metadata: responseMeta.optional(), response: z.array(z.record(z.string(), agentExecutionDetails)), usage: usageStats, additional_metadata: additionalMetadata}); 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 }; /** * Run a deployed Agent * * POST /v1/agents/run */ export const createApiV1AgentsRun = async (args: CreateApiV1AgentsRunArgs, options?: CreateApiV1AgentsRunOptions) => { const {baseUrl = "https://api.svahnar.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agents/run${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(agentResponse, await res.json()) };