import {z} from 'zod' export type SimulationRunStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled' | 'budget_exceeded'; export type ScenarioChannel = 'text' | 'voice'; export type GetApiV1SimulationRunsArgs = {status?: (SimulationRunStatus | null) | undefined, agent_id?: (string | null) | undefined, channel?: (ScenarioChannel | null) | undefined, q?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1SimulationRunsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const scenarioChannel = z.enum(["text", "voice"]); export const simulationRunResponse = z.object({id: z.string(), session_id: z.string(), organization_id: z.string(), agent_id: z.string(), agent_revision_id: z.string().nullable(), persona_id: z.string(), scenario_id: z.string(), suite_id: z.string().nullable().optional(), suite_run_id: z.string().nullable().optional(), suite_item_id: z.string().nullable().optional(), suite_attempt: z.number().int().optional(), channel: scenarioChannel, tool_overrides: z.record(z.string(), z.unknown()).nullable(), sut_snapshot_sha256: z.string().nullable().optional(), status: z.string(), idempotency_key: z.string().nullable().optional(), budget_usd: z.string().nullable().optional(), spent_usd: z.string().nullable().optional(), started_at: z.string().nullable(), finished_at: z.string().nullable(), error_text: z.string().nullable(), stop_reason: z.string().nullable().optional(), persona_turn_count: z.number().int().optional(), agent_turn_count: z.number().int().optional(), transcript_chars: z.number().int().optional(), heartbeat_at: z.string().nullable().optional(), inputs_snapshot: z.record(z.string(), z.unknown()), created_at: z.string(), recording_id: z.string().nullable().optional(), recording_status: z.string().nullable().optional()}); export const cursorPageSimulationRunResponse = z.object({items: z.array(simulationRunResponse), next_cursor: z.string().nullable().optional(), has_more: z.boolean()}); 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 }; /** * List simulation runs * * GET /v1/simulation/runs */ export const getApiV1SimulationRuns = async (args: GetApiV1SimulationRunsArgs, options: GetApiV1SimulationRunsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.channel != null) query.append("channel", String(args.channel)) if (args.q != null) query.append("q", String(args.q)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/simulation/runs${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(cursorPageSimulationRunResponse, await res.json()) };