import {z} from 'zod' export type JsonScalar = string | number | number | boolean | null; export type JsonValueInput = JsonScalar | Array | {[key: string]: JsonValueInput}; export type NoulCriteria = {false?: string | undefined, true?: string | undefined}; export type NoulQuestion = {criteria?: NoulCriteria | undefined, instructions: string, type: 'noul'}; export type ChoiceQuestion = {criteria: {[key: string]: JsonValueInput}, instructions: string, type: 'choice'}; export type ScoreQuestion = {criteria: Array | {[key: string]: JsonValueInput}, instructions: string, type: 'score'}; export type SystemOneRequest = {model: string, provider?: ({[key: string]: JsonValueInput} | null) | undefined, questions: {[key: string]: NoulQuestion | ChoiceQuestion | ScoreQuestion}, session_id?: (string | null) | undefined, state: string | {[key: string]: JsonValueInput} | Array, store?: boolean | undefined, thinking?: (boolean | null) | undefined, trace?: ({[key: string]: JsonValueInput} | null) | undefined, user?: (string | null) | undefined}; export type CreateApiV1SystemoneArgs = {body: SystemOneRequest}; export type CreateApiV1SystemoneOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const noulAnswer = z.object({confidence: z.number().gte(0).lte(1), noul: z.number().gte(0).lte(1), type: z.literal("noul")}); export const choiceAnswer = z.object({choice: z.string(), confidence: z.number().gte(0).lte(1), probabilities: z.record(z.string(), z.number().gte(0).lte(1)), type: z.literal("choice")}); export const scoreAnswer = z.object({confidence: z.number().gte(0).lte(1), expected_level: z.number(), legend: z.record(z.string(), z.string()), probabilities: z.record(z.string(), z.number().gte(0).lte(1)), score: z.number().int().gte(0), type: z.literal("score")}); export const systemOneUsage = z.object({input_tokens: z.number().int().gte(0), output_tokens: z.number().int().gte(0)}); export const systemOneResponse = z.object({answers: z.record(z.string(), z.discriminatedUnion("type", [noulAnswer, choiceAnswer, scoreAnswer])), model: z.string(), token_usage: z.number().int().gte(0), 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 }; /** * Decide * * POST /v1/systemone * * Also accepts auth: BearerAuth */ export const createApiV1Systemone = async (args: CreateApiV1SystemoneArgs, options?: CreateApiV1SystemoneOptions) => { const {baseUrl = "https://api.fastino.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) 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()) };