import {z} from 'zod' export type AgentSessionId = string; export type GetApiV1AgentSessionsAgentSessionIdArgs = {agent_session_id: AgentSessionId}; export type GetApiV1AgentSessionsAgentSessionIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const requestId = z.string(); export const runtimeLocation = z.enum(["local", "cloud"]); export const execution = z.object({location: runtimeLocation, capability_id: z.string().nullable().optional(), capability_version: z.string().nullable().optional()}); export const usage = z.object({billable: z.boolean(), quantity: z.number().gte(0), unit: z.string(), credits: z.number().gte(0).optional()}); export const agentSessionId = z.string(); export const workspaceId = z.string(); export const runtimeKind = z.enum(["local", "cloud", "hybrid"]); export const libraryId = z.string(); export const assetId = z.string(); export const scope = z.object({library_ids: z.array(libraryId), asset_ids: z.array(assetId)}); export const executionPolicy = z.enum(["local_only", "prefer_local", "cloud_allowed", "cloud_required"]); export const workflowReference = z.object({id: z.string(), version: z.string()}); export const agentSession = z.object({id: agentSessionId, workspace_id: workspaceId.nullable().optional(), runtime: runtimeKind, scope: scope, execution_policy: executionPolicy, workflow: workflowReference.nullable().optional(), data_egress_allowed: z.boolean(), created_at: z.string(), expires_at: z.string()}); export const agentSessionResponse = z.object({request_id: requestId, execution: execution, usage: usage, warnings: z.array(z.string()), data: agentSession}); 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 }; /** * Get a constrained Agent session * * GET /v1/agent-sessions/{agent_session_id} * * Also accepts auth: apiKeyAuth */ export const getApiV1AgentSessionsAgentSessionId = async (args: GetApiV1AgentSessionsAgentSessionIdArgs, options?: GetApiV1AgentSessionsAgentSessionIdOptions) => { const {baseUrl = "https://api.cerul.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agent-sessions/${encodeURIComponent(String(args.agent_session_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(agentSessionResponse, await res.json()) };