import {z} from 'zod' export type Tool = {name: string, content: string | Record | null}; export type ToolArray = {type?: 'array' | undefined, value: Array}; export type ToolDict = {type?: 'dict' | undefined, value: Record} | {[key: string]: unknown}; export type ToolString = {type?: 'string' | undefined, value: string}; export type AgentInstructions = {agent_name?: (string | null) | undefined, system_prompt: string, tools?: (ToolArray | ToolDict | ToolString) | undefined}; export type WorkflowInstructions = {instructions: Array}; export type FileReference = {file_uuid?: (string | null) | undefined, remote_file_path?: (string | null) | undefined}; export type PromptUpdate = {prompt_uuid: string, content?: (string | null) | undefined, category?: (string | null) | undefined, action?: string | undefined}; export type PromptCreate = {content: string, category?: (string | null) | undefined}; export type EvalUpdateRequest = {name?: (string | null) | undefined, ai_description?: (string | null) | undefined, ai_instructions?: (string | AgentInstructions | WorkflowInstructions | null) | undefined, eval_instructions?: (string | null) | undefined, ground_truth?: (string | FileReference | null) | undefined, prompt_updates?: (Array | null) | undefined, prompt_creates?: (Array | null) | undefined}; export type UpdateApiV2EvalsEvalUuidArgs = {eval_uuid: string, workspace_uuid?: string | undefined, body: EvalUpdateRequest}; export type UpdateApiV2EvalsEvalUuidOptions = {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 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 }; /** * Update Eval * * PUT /v2/evals/{eval_uuid} * * Also accepts auth: AuthBearer */ export const updateApiV2EvalsEvalUuid = async (args: UpdateApiV2EvalsEvalUuidArgs, options?: UpdateApiV2EvalsEvalUuidOptions) => { const {baseUrl = "https://api.aymara.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.workspace_uuid != null) query.append("workspace_uuid", String(args.workspace_uuid)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/evals/${encodeURIComponent(String(args.eval_uuid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(eval, await res.json()) };