import {z} from 'zod' export type GatewayCode = 'OK' | 'CANCELLED' | 'UNKNOWN' | 'INVALID_ARGUMENT' | 'DEADLINE_EXCEEDED' | 'NOT_FOUND' | 'ALREADY_EXISTS' | 'PERMISSION_DENIED' | 'UNAUTHENTICATED' | 'RESOURCE_EXHAUSTED' | 'FAILED_PRECONDITION' | 'ABORTED' | 'OUT_OF_RANGE' | 'UNIMPLEMENTED' | 'INTERNAL' | 'UNAVAILABLE' | 'DATA_LOSS'; export type GatewayStatus = {code?: GatewayCode | undefined, message?: string | undefined}; export type GatewayProvider = {id?: string | undefined, config?: {[key: string]: string} | undefined, label?: string | undefined}; export type AssertionAssertionType = 'ASSERTION_TYPE_UNSPECIFIED' | 'ASSERTION_TYPE_LLM' | 'ASSERTION_TYPE_CODE'; export type GatewayEvaluateOptions = {maxConcurrency?: number | undefined, repeat?: number | undefined, delay?: number | undefined}; export type GatewayLLMAssertion = {llmEvaluatorPrompt?: string | undefined, providers: Array, prompts: Array, evaluateOptions?: GatewayEvaluateOptions | undefined}; export type CodeAssertionExecutionOptions = {timeoutMs?: number | undefined, memoryLimitMb?: number | undefined, envVars?: {[key: string]: string} | undefined}; export type GatewayCodeAssertion = {language: string, code: string, expectedOutput?: string | undefined, options?: CodeAssertionExecutionOptions | undefined}; export type GatewayAssertion = {assertionType: AssertionAssertionType, llmAssertion?: GatewayLLMAssertion | undefined, codeAssertion?: GatewayCodeAssertion | undefined, metricName?: string | undefined}; export type GatewayEvaluation = {name?: string | undefined, createTime?: string | undefined, createdBy?: string | undefined, status?: GatewayStatus | undefined, evaluationType: string, description?: string | undefined, providers: Array, assertions: Array, updateTime?: string | undefined}; export type GatewayGatewayTestEvaluationBody = {evaluation: GatewayEvaluation, sampleData: string}; export type CreateApiV1AccountsAccountIdTestevalArgs = {account_id: string, body: GatewayGatewayTestEvaluationBody}; export type CreateApiV1AccountsAccountIdTestevalOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const gatewayPreviewEvaluationResult = z.object({success: z.boolean().optional(), reason: z.string().optional(), score: z.number().optional(), messages: z.array(z.object({})).optional(), metrics: z.record(z.string(), z.number()).optional()}); export const gatewayPreviewEvaluationResponse = z.object({results: z.array(gatewayPreviewEvaluationResult).optional(), totalSamples: z.number().int().optional(), totalRuntimeMs: z.string().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 }; /** * Similar to preview evaluation, but no need to create the evaluation entry first. * * POST /v1/accounts/{account_id}:testeval */ export const createApiV1AccountsAccountIdTesteval = async (args: CreateApiV1AccountsAccountIdTestevalArgs, options: CreateApiV1AccountsAccountIdTestevalOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}:testeval${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(gatewayPreviewEvaluationResponse, await res.json()) };