import {z} from 'zod' export type EvalFilterType = 'agent' | 'team' | 'workflow'; export type SortOrder = 'asc' | 'desc'; export type GetApiEvalRunsArgs = {agent_id?: (string | null) | undefined, team_id?: (string | null) | undefined, workflow_id?: (string | null) | undefined, model_id?: (string | null) | undefined, type?: (EvalFilterType | null) | undefined, limit?: (number | null) | undefined, page?: (number | null) | undefined, sort_by?: (string | null) | undefined, sort_order?: (SortOrder | null) | undefined, db_id?: (string | null) | undefined, table?: (string | null) | undefined, eval_types?: (string | null) | undefined}; export type GetApiEvalRunsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const evalType = z.enum(["accuracy", "agent_as_judge", "performance", "reliability"]); export const evalSchema = z.object({id: z.string(), agent_id: z.string().nullable().optional(), model_id: z.string().nullable().optional(), model_provider: z.string().nullable().optional(), team_id: z.string().nullable().optional(), workflow_id: z.string().nullable().optional(), name: z.string().nullable().optional(), evaluated_component_name: z.string().nullable().optional(), eval_type: evalType, eval_data: z.record(z.string(), z.unknown()), eval_input: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const paginationInfo = z.object({page: z.number().int().gte(0).optional(), limit: z.number().int().gte(1).optional(), total_pages: z.number().int().gte(0).optional(), total_count: z.number().int().gte(0).optional(), search_time_ms: z.number().gte(0).optional()}); export const paginatedResponseEvalSchema = z.object({data: z.array(evalSchema), meta: paginationInfo}); 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 Evaluation Runs * * GET /eval-runs */ export const getApiEvalRuns = async (args: GetApiEvalRunsArgs, options: GetApiEvalRunsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.team_id != null) query.append("team_id", String(args.team_id)) if (args.workflow_id != null) query.append("workflow_id", String(args.workflow_id)) if (args.model_id != null) query.append("model_id", String(args.model_id)) if (args.type != null) query.append("type", String(args.type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.db_id != null) query.append("db_id", String(args.db_id)) if (args.table != null) query.append("table", String(args.table)) if (args.eval_types != null) query.append("eval_types", String(args.eval_types)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(paginatedResponseEvalSchema, await res.json()) };