import {z} from 'zod' export type GetApiV1EvalRunsRunIdArgs = {run_id: string, include?: (string | null) | undefined}; export type GetApiV1EvalRunsRunIdOptions = {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 conversationSummary = z.object({id: z.string(), channel: z.string(), session_type: z.string(), status: z.string(), assistant_id: z.string().nullable(), assistant_revision_id: z.string().nullable(), subject: z.string().nullable(), created_at: z.string(), last_activity_at: z.string().nullable()}); export const turnView = z.object({id: z.string(), seq: z.number().int(), role: z.string(), content: z.string().nullable(), tool_calls: z.union([z.array(z.record(z.string(), z.unknown())), z.record(z.string(), z.unknown())]).nullable(), tool_call_id: z.string().nullable(), authored_by: z.string(), created_at: z.string()}); export const recordingRef = z.object({id: z.string(), kind: z.string(), status: z.string(), duration_ms: z.number().int().nullable(), manifest_key: z.string(), started_at: z.string(), finalized_at: z.string().nullable()}); export const evalRunDetailResponse = 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(), session: conversationSummary.nullable().optional(), turns: z.array(turnView).nullable().optional(), turns_truncated: z.boolean().optional(), recording: recordingRef.nullable().optional()}); 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 an eval run (optionally with embedded evidence) * * GET /v1/eval-runs/{run_id} */ export const getApiV1EvalRunsRunId = async (args: GetApiV1EvalRunsRunIdArgs, options: GetApiV1EvalRunsRunIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include != null) query.append("include", String(args.include)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/eval-runs/${encodeURIComponent(String(args.run_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(evalRunDetailResponse, await res.json()) };