import {z} from 'zod' export type NoulCriteria = {false?: (string | null) | undefined, true?: (string | null) | undefined}; export type NoulQuestion = {criteria?: (NoulCriteria | null) | undefined, instructions?: (string | null) | undefined, type: 'noul'}; export type ChoiceQuestion = {criteria: {[key: string]: string | null}, instructions?: (string | null) | undefined, type: 'choice'}; export type ScoreQuestion = {criteria: Array, instructions?: (string | null) | undefined, type: 'score'}; export type JsonValue = unknown; export type SystemOneRequest = {questions: {[key: string]: NoulQuestion | ChoiceQuestion | ScoreQuestion}, state: JsonValue}; export type CreateApiV1SystemoneArgs = {body: SystemOneRequest}; export type CreateApiV1SystemoneOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const noulAnswer = z.object({noul: z.number(), type: z.literal("noul").optional()}); export const choiceAnswer = z.object({choice: z.string(), confidence: z.number(), probabilities: z.record(z.string(), z.number()), type: z.literal("choice").optional()}); export const scoreAnswer = z.object({confidence: z.number(), legend: z.array(z.string()), probabilities: z.record(z.string(), z.number()), score: z.number(), type: z.literal("score").optional()}); export const systemOneUsage = z.object({input_tokens: z.number().int().nullable(), output_tokens: z.number().int().nullable()}); export const systemOneResponse = z.object({answers: z.record(z.string(), z.discriminatedUnion("type", [noulAnswer, choiceAnswer, scoreAnswer])), evaluation_time_ms: z.number(), model: z.string(), request_id: z.string().nullable(), usage: systemOneUsage}); 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 }; /** * evaluate_systemone * * POST /v1/systemone */ export const createApiV1Systemone = async (args: CreateApiV1SystemoneArgs, options?: CreateApiV1SystemoneOptions) => { const {baseUrl = "https://ndi-api.nace.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/systemone${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(systemOneResponse, await res.json()) };