import {z} from 'zod' export type DecisionInputPart = {type: 'input_text', text: string, prompt_cache_breakpoint?: ({mode?: 'explicit' | undefined} | null) | undefined} | {type: 'input_image', image_url: string, detail?: ('low' | 'high' | 'auto' | 'original' | 'null' | null) | undefined, prompt_cache_breakpoint?: ({mode?: 'explicit' | undefined} | null) | undefined} | {type: 'input_json', data: Record | Array>}; export type DecisionMessage = {type?: 'message' | undefined, role: 'user', content: string | Array}; export type DecisionInput = string | Array; export type DecisionQuestion = {type: 'predicate', name?: (string | null) | undefined, instructions: string} | {type: 'choice', name?: (string | null) | undefined, instructions: string, choices: Array<{value: string | boolean, description?: (string | null) | undefined}>} | {type: 'score', name?: (string | null) | undefined, instructions: string, levels: Array<{label: string, description?: (string | null) | undefined}>}; export type DecisionRequest = {model?: string | undefined, input: DecisionInput, questions: Array, safety_identifier?: (string | null) | undefined, prompt_cache_key?: (string | null) | undefined, prompt_cache_options?: ({mode?: ('implicit' | 'explicit' | 'null' | null) | undefined, ttl?: '30m' | undefined} | null) | undefined}; export type CreateApiV1DecisionsArgs = {body: DecisionRequest}; export type CreateApiV1DecisionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const decisionAnswer = z.union([z.object({type: z.literal("predicate"), name: z.string().nullable(), probability: z.number().gte(0).lte(1)}), z.object({type: z.literal("choice"), name: z.string().nullable(), choice: z.union([z.string(), z.boolean()]), confidence: z.number().gte(0).lte(1), probabilities: z.array(z.object({value: z.union([z.string(), z.boolean()]), probability: z.number()}))}), z.object({type: z.literal("score"), name: z.string().nullable(), score: z.number(), confidence: z.number().gte(0).lte(1), probabilities: z.array(z.object({value: z.number().int(), label: z.string(), probability: z.number()}))}), z.object({type: z.literal("refusal"), name: z.string().nullable()})]); export const decisionUsage = z.object({input_tokens: z.number().int(), output_tokens: z.number().int(), total_tokens: z.number().int(), input_tokens_details: z.object({cached_tokens: z.literal(0), cache_write_tokens: z.literal(0)}), output_tokens_details: z.object({reasoning_tokens: z.literal(0)})}); export const requestId = z.string(); export const decisionResponse = z.object({model: z.enum(["drex-v1.5", "drex-v2.0", "drex-max", "drex-max-v1.1", "drex-max-finance"]), answers: z.array(decisionAnswer), usage: decisionUsage, request_id: requestId}); 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 questions (OpenAI Decisions format) * * POST /v1/decisions */ export const createApiV1Decisions = async (args: CreateApiV1DecisionsArgs, options?: CreateApiV1DecisionsOptions) => { const {baseUrl = "https://console.nace.ai", 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/decisions${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(decisionResponse, await res.json()) };