import {z} from 'zod' export type GetApiV1EvalRunsArgs = {session_id?: (string | null) | undefined, evaluator_id?: (string | null) | undefined, agent_id?: (string | null) | undefined, status?: ('pending' | 'running' | 'completed' | 'failed' | 'errored' | null) | undefined, scope?: ('live' | 'simulation' | 'test' | null) | undefined, outcome?: ('passed' | 'failed' | 'no_verdict' | 'errored' | null) | undefined, score_min?: (number | null) | undefined, score_max?: (number | null) | undefined, severity?: ('info' | 'low' | 'medium' | 'high' | 'critical' | null) | undefined, channel?: (string | null) | undefined, created_from?: (string | null) | undefined, created_to?: (string | null) | undefined, is_critical?: (boolean | null) | undefined, q?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1EvalRunsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const evalScoreResponse = z.object({id: z.string(), organization_id: z.string(), eval_run_id: z.string(), value: z.number().nullable(), raw_value: z.number().nullable(), label: z.string().nullable(), passed: z.boolean().nullable(), confidence: z.number().nullable(), rationale: z.string().nullable(), severity: z.string().nullable(), blocking: z.boolean(), evidence_observation_ids: z.array(z.string()).optional(), created_at: z.string()}); export const evalRunResponse = z.object({id: z.string(), organization_id: z.string(), session_id: z.string(), evaluator_id: z.string(), agent_id: z.string().nullable().optional(), channel: z.string().nullable().optional(), session_type: z.string().nullable().optional(), status: z.string(), started_at: z.string().nullable(), finished_at: z.string().nullable(), error_text: z.string().nullable(), prompt_snapshot: z.string().nullable(), threshold_snapshot: z.number().nullable(), config_snapshot: z.record(z.string(), z.unknown()).optional(), severity_snapshot: z.string(), format_snapshot: z.string(), is_critical_snapshot: z.boolean().optional(), created_at: z.string(), scores: z.array(evalScoreResponse).optional()}); export const cursorPageEvalRunResponse = z.object({items: z.array(evalRunResponse), 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 eval runs * * GET /v1/eval-runs */ export const getApiV1EvalRuns = async (args: GetApiV1EvalRunsArgs, options: GetApiV1EvalRunsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.session_id != null) query.append("session_id", String(args.session_id)) if (args.evaluator_id != null) query.append("evaluator_id", String(args.evaluator_id)) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.status != null) query.append("status", String(args.status)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.outcome != null) query.append("outcome", String(args.outcome)) if (args.score_min != null) query.append("score_min", String(args.score_min)) if (args.score_max != null) query.append("score_max", String(args.score_max)) if (args.severity != null) query.append("severity", String(args.severity)) if (args.channel != null) query.append("channel", String(args.channel)) if (args.created_from != null) query.append("created_from", String(args.created_from)) if (args.created_to != null) query.append("created_to", String(args.created_to)) if (args.is_critical != null) query.append("is_critical", String(args.is_critical)) 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/eval-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(cursorPageEvalRunResponse, await res.json()) };