import {z} from 'zod' export type ExtraMetadataFieldType = 'string' | 'number' | 'boolean'; export type ExtraMetadataField = {description?: (string | null) | undefined, name: string, type: ExtraMetadataFieldType}; export type LlmJudgeScorerParams = {extra_metadata_fields?: (Array | null) | undefined, include_reasoning?: (boolean | null) | undefined, max_tokens?: (number | null) | undefined, model?: (string | null) | undefined, output_parsing?: (string | null) | undefined, provider_id: string, temperature?: (number | null) | undefined, timeout_ms?: (number | null) | undefined}; export type RemoteScorerAuth = {type: 'none'} | {token: string, type: 'bearer'} | {password: string, type: 'basic', username: string} | {header_name: string, token: string, type: 'api_key'}; export type RemoteScorerHeader = {key: string, value: string}; export type RemoteScorerParams = {auth?: (RemoteScorerAuth | null) | undefined, content_type?: (string | null) | undefined, custom_headers?: (Array | null) | undefined, endpoint: string, http_method?: (string | null) | undefined, max_retries?: (number | null) | undefined, timeout_ms?: (number | null) | undefined}; export type ScorerCreateConfig = {outputSchema: unknown, params: LlmJudgeScorerParams, producesScoreConfigId?: (string | null) | undefined, producesScoreConfigVersion?: (number | null) | undefined, template: string, type: 'llm_judge'} | {params: RemoteScorerParams, producesScoreConfigId?: (string | null) | undefined, producesScoreConfigVersion?: (number | null) | undefined, template: string, type: 'remote'}; export type CreateApiApiOrgIdScorersArgs = {org_id: string, body: {description?: (string | null) | undefined, name: string, scorer: ScorerCreateConfig}}; export type CreateApiApiOrgIdScorersOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiOrgIdScorersResponse = z.object({createdAt: z.number().int(), description: z.string().nullable().optional(), entityId: z.string(), id: z.string(), isActive: z.boolean(), name: z.string(), orgId: z.string(), outputSchema: z.unknown(), params: z.unknown(), producesScoreConfigId: z.string().nullable().optional(), producesScoreConfigVersion: z.number().int().nullable().optional(), referenceBased: z.boolean(), scorerType: z.string(), template: z.string(), updatedAt: z.number().int(), variables: z.array(z.string()), version: 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 }; /** * Create scorer (v1) * * POST /api/{org_id}/scorers */ export const createApiApiOrgIdScorers = async (args: CreateApiApiOrgIdScorersArgs, options: CreateApiApiOrgIdScorersOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/scorers${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(createApiApiOrgIdScorersResponse, await res.json()) };