import {z} from 'zod' export type EvalType = 'accuracy' | 'agent_as_judge' | 'performance' | 'reliability'; export type EvalRunInput = {agent_id?: (string | null) | undefined, team_id?: (string | null) | undefined, model_id?: (string | null) | undefined, model_provider?: (string | null) | undefined, eval_type: EvalType, input: string, additional_guidelines?: (string | null) | undefined, additional_context?: (string | null) | undefined, num_iterations?: number | undefined, name?: (string | null) | undefined, expected_output?: (string | null) | undefined, criteria?: (string | null) | undefined, scoring_strategy?: ('numeric' | 'binary' | null) | undefined, threshold?: (number | null) | undefined, warmup_runs?: number | undefined, expected_tool_calls?: (Array | null) | undefined, allow_additional_tool_calls?: boolean | undefined, expected_tool_call_arguments?: ({[key: string]: {[key: string]: unknown} | Array<{[key: string]: unknown}>} | null) | undefined}; export type CreateApiEvalRunsArgs = {db_id?: (string | null) | undefined, table?: (string | null) | undefined, body: EvalRunInput}; export type CreateApiEvalRunsOptions = {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 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 }; /** * Execute Evaluation * * POST /eval-runs */ export const createApiEvalRuns = async (args: CreateApiEvalRunsArgs, options: CreateApiEvalRunsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.db_id != null) query.append("db_id", String(args.db_id)) if (args.table != null) query.append("table", String(args.table)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/eval-runs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(evalSchema, await res.json()) };