import {z} from 'zod' export type GatewayEvaluatorState = 'STATE_UNSPECIFIED' | 'ACTIVE' | 'BUILDING' | 'BUILD_FAILED'; export type GatewayCriterionType = 'TYPE_UNSPECIFIED' | 'CODE_SNIPPETS'; export type GatewayCodeSnippets = {language?: string | undefined, fileContents?: {[key: string]: string} | undefined, entryFile?: string | undefined, entryFunc?: string | undefined}; export type GatewayCriterion = {type?: GatewayCriterionType | undefined, name?: string | undefined, description?: string | undefined, codeSnippets?: GatewayCodeSnippets | undefined}; 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 EvaluatorSourceType = 'TYPE_UNSPECIFIED' | 'TYPE_UPLOAD' | 'TYPE_GITHUB' | 'TYPE_TEMPORARY'; export type EvaluatorSource = {type?: EvaluatorSourceType | undefined, githubRepositoryName?: string | undefined}; export type GatewayEncryptionState = 'ENCRYPTION_STATE_UNSPECIFIED' | 'ENCRYPTION_STATE_PLAINTEXT' | 'ENCRYPTION_STATE_CMEK'; export type GatewayEvaluator = {name?: string | undefined, displayName?: string | undefined, description?: string | undefined, createTime?: string | undefined, createdBy?: string | undefined, updateTime?: string | undefined, state?: GatewayEvaluatorState | undefined, criteria?: Array | undefined, requirements?: string | undefined, entryPoint?: string | undefined, status?: GatewayStatus | undefined, commitHash?: string | undefined, source?: EvaluatorSource | undefined, defaultDataset?: string | undefined, encryptionState?: GatewayEncryptionState | undefined}; export type GatewayGatewayPreviewEvaluatorBody = {evaluator: GatewayEvaluator, sampleData: Array, maxSamples?: number | undefined}; export type CreateApiV1AccountsAccountIdEvaluatorsPreviewEvaluatorArgs = {account_id: string, body: GatewayGatewayPreviewEvaluatorBody}; export type CreateApiV1AccountsAccountIdEvaluatorsPreviewEvaluatorOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const gatewayPreviewEvaluatorSampleResult = z.object({success: z.string().optional(), score: z.number().optional(), perMetricEvals: z.record(z.string(), z.object({})).optional(), reason: z.string().optional()}); export const gatewayPreviewEvaluatorResponse = z.object({results: z.array(gatewayPreviewEvaluatorSampleResult).optional(), totalSamples: z.number().int().optional(), totalRuntimeMs: z.string().optional(), stdout: z.array(z.string()).optional(), stderr: z.array(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 }; /** * POST /v1/accounts/{account_id}/evaluators:previewEvaluator */ export const createApiV1AccountsAccountIdEvaluatorsPreviewEvaluator = async (args: CreateApiV1AccountsAccountIdEvaluatorsPreviewEvaluatorArgs, options: CreateApiV1AccountsAccountIdEvaluatorsPreviewEvaluatorOptions) => { 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))}/evaluators:previewEvaluator${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(gatewayPreviewEvaluatorResponse, await res.json()) };