import {z} from 'zod' export type CreateApiApiOrgIdPlaygroundScoreArgs = {org_id: string, body: {expectedOutput: unknown, input: unknown, metadata: unknown, output: string, scorerIds: Array}}; export type CreateApiApiOrgIdPlaygroundScoreOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const skipReasonBody = z.enum(["no_reference", "requires_trace"]); export const scoreResultBody = z.union([z.object({boolean: z.boolean().nullable().optional(), categorical: z.string().nullable().optional(), completionTokens: z.number().int().nullable().optional(), latencyMs: z.number().int(), metadata: z.unknown(), modelUsed: z.string().nullable().optional(), numeric: z.number().nullable().optional(), promptTokens: z.number().int().nullable().optional(), reasoning: z.string().nullable().optional(), status: z.literal("scored"), scorerId: z.string(), scorerName: z.string(), scorerVersion: z.number().int()}), z.object({reason: skipReasonBody, status: z.literal("skipped"), scorerId: z.string(), scorerName: z.string(), scorerVersion: z.number().int()}), z.object({error: z.string(), status: z.literal("failed"), scorerId: z.string(), scorerName: z.string(), scorerVersion: z.number().int()})]); export const createApiApiOrgIdPlaygroundScoreResponse = z.object({results: z.array(scoreResultBody)}); 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 /api/{org_id}/playground/score */ export const createApiApiOrgIdPlaygroundScore = async (args: CreateApiApiOrgIdPlaygroundScoreArgs, options: CreateApiApiOrgIdPlaygroundScoreOptions) => { 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))}/playground/score${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(createApiApiOrgIdPlaygroundScoreResponse, await res.json()) };