import {z} from 'zod' export type GetApiV2EvalsArgs = {workspace_uuid?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV2EvalsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tool = z.object({name: z.string(), content: z.union([z.string(), z.object({})]).nullable()}); export const toolArray = z.object({type: z.literal("array").optional(), value: z.array(tool)}); export const toolDict = z.object({type: z.literal("dict").optional(), value: z.object({})}).and(z.record(z.string(), z.unknown())); export const toolString = z.object({type: z.literal("string").optional(), value: z.string()}); export const agentInstructions = z.object({agent_name: z.string().nullable().optional(), system_prompt: z.string(), tools: z.discriminatedUnion("type", [toolArray, toolDict, toolString]).optional()}); export const workflowInstructions = z.object({instructions: z.array(agentInstructions)}); export const contentType = z.enum(["text", "image", "video"]); export const fileReference = z.object({file_uuid: z.string().nullable().optional(), remote_file_path: z.string().nullable().optional()}); export const exampleType = z.enum(["good", "bad"]); export const promptExample = z.object({content: z.string(), example_uuid: z.string().nullable().optional(), type: exampleType.optional(), explanation: z.string().nullable().optional()}); export const status = z.enum(["created", "processing", "finished", "failed"]); export const eval = z.object({eval_uuid: z.string().nullable().optional(), name: z.string().nullable().optional(), created_by: z.string().nullable().optional(), ai_description: z.string(), ai_instructions: z.union([z.string(), agentInstructions, workflowInstructions]).nullable().optional(), eval_type: z.string(), eval_instructions: z.string().nullable().optional(), language: z.string().nullable().optional(), modality: contentType.optional(), ground_truth: z.union([z.string(), fileReference]).nullable().optional(), num_prompts: z.number().int().nullable().optional(), prompt_examples: z.array(promptExample).nullable().optional(), is_jailbreak: z.boolean().optional(), is_sandbox: z.boolean().optional(), workspace_uuid: z.string().nullable().optional(), status: status.nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const pagedEval = z.object({items: z.array(eval), count: z.number().int()}); 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 Evals * * GET /v2/evals * * Also accepts auth: AuthBearer */ export const getApiV2Evals = async (args: GetApiV2EvalsArgs, options?: GetApiV2EvalsOptions) => { const {baseUrl = "https://api.aymara.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.workspace_uuid != null) query.append("workspace_uuid", String(args.workspace_uuid)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/evals${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(pagedEval, await res.json()) };