import {z} from 'zod' export type ProviderRoutingObject = {order?: Array | undefined, only?: Array | undefined, ignore?: Array | undefined, sort?: ('speed' | 'throughput' | 'latency' | 'price' | 'auto' | 'none' | 'default' | {by: 'speed' | 'throughput' | 'latency' | 'price' | 'auto' | 'none' | 'default', partition?: ('model' | 'none') | undefined}) | undefined, quantizations?: (Array<'int4' | 'fp4' | 'fp6' | 'int8' | 'fp8' | 'fp16' | 'bf16' | 'fp32' | 'unknown'>) | undefined, min_quantization?: ('int4' | 'fp4' | 'fp6' | 'int8' | 'fp8' | 'fp16' | 'bf16' | 'fp32') | undefined, max_price?: ({prompt?: number | undefined, completion?: number | undefined}) | undefined, preferred_max_latency?: (number | {p50?: number | undefined, p75?: number | undefined, p90?: number | undefined, p99?: number | undefined}) | undefined, preferred_min_throughput?: (number | {p50?: number | undefined, p75?: number | undefined, p90?: number | undefined, p99?: number | undefined}) | undefined, allow_fallbacks?: boolean | undefined, require_parameters?: boolean | undefined}; export type VideoUrl = {url: string, detail?: ('auto' | 'low' | 'high') | undefined, start_offset?: number | undefined, end_offset?: number | undefined}; export type MessageContentPart = {type: 'text' | 'image_url' | 'video_url' | 'input_text' | 'input_audio' | 'input_video' | 'video' | 'input_file' | 'file' | 'document' | 'tool_use' | 'tool_result', text?: string | undefined, image_url?: ({url?: string | undefined, detail?: ('low' | 'high' | 'auto') | undefined}) | undefined, input_audio?: ({data?: string | undefined, format?: string | undefined}) | undefined, video_url?: VideoUrl | undefined, start_offset?: number | undefined, end_offset?: number | undefined, cache_control?: ({type?: 'ephemeral' | undefined, ttl?: ('5m' | '1h') | undefined}) | undefined} | {[key: string]: unknown}; export type InlineReasoningEffort = {effort: 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max'}; export type InlineConfigurationUpdate = {reasoning: InlineReasoningEffort}; export type AdvisorConfig = {model: string, instructions?: string | undefined, mode?: ('auto' | 'required') | undefined, max_uses?: 1 | undefined, max_completion_tokens?: number | undefined, forward_transcript?: false | undefined}; export type ChatCompletionRequest = {model?: string | undefined, models?: Array | undefined, billing_mode?: string | undefined, billingMode?: string | undefined, provider?: (string | ProviderRoutingObject) | undefined, caching?: boolean | undefined, stickyprovider?: boolean | undefined, stickyProvider?: boolean | undefined, messages: Array<{role: 'system' | 'user' | 'assistant' | 'developer', content?: (string | Array | null) | undefined, configuration_update?: InlineConfigurationUpdate | undefined}>, stream?: boolean | undefined, service_tier?: ('auto' | 'default' | 'flex' | 'fast' | 'priority') | undefined, temperature?: number | undefined, max_tokens?: number | undefined, top_p?: number | undefined, frequency_penalty?: number | undefined, presence_penalty?: number | undefined, repetition_penalty?: number | undefined, top_k?: number | undefined, top_a?: number | undefined, min_p?: number | undefined, tfs?: number | undefined, eta_cutoff?: number | undefined, epsilon_cutoff?: number | undefined, typical_p?: number | undefined, mirostat_mode?: (0 | 1 | 2) | undefined, mirostat_tau?: number | undefined, mirostat_eta?: number | undefined, min_tokens?: number | undefined, stop?: (string | Array) | undefined, stop_token_ids?: Array | undefined, include_stop_str_in_output?: boolean | undefined, ignore_eos?: boolean | undefined, no_repeat_ngram_size?: number | undefined, custom_token_bans?: Array | undefined, logit_bias?: {[key: string]: number} | undefined, logprobs?: (boolean | number) | undefined, prompt_logprobs?: boolean | undefined, seed?: number | undefined, web_search?: (boolean | {enabled?: boolean | undefined, provider?: ('native' | 'linkup' | 'tavily' | 'brave' | 'sofya' | 'exa' | 'kagi' | 'perplexity' | 'valyu' | 'firecrawl') | undefined, depth?: string | undefined, search_context_size?: ('low' | 'medium' | 'high') | undefined, user_location?: ({type?: 'approximate' | undefined, country?: string | undefined, city?: string | undefined, region?: string | undefined, timezone?: string | undefined}) | undefined}) | undefined, web_search_options?: ({search_context_size?: ('low' | 'medium' | 'high') | undefined, user_location?: ({type?: 'approximate' | undefined, approximate?: ({country?: string | undefined, city?: string | undefined, region?: string | undefined, timezone?: string | undefined}) | undefined}) | undefined}) | undefined, max_tool_calls?: number | undefined, prompt_caching?: ({enabled?: boolean | undefined, ttl?: ('5m' | '1h') | undefined, cut_after_message_index?: number | undefined, stickyProvider?: boolean | undefined}) | undefined, reasoning_effort?: ('none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh') | undefined, reasoning?: ({exclude?: boolean | undefined, effort?: ('none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh') | undefined, delta_field?: 'reasoning_content' | undefined} | {[key: string]: unknown}) | undefined, reasoning_delta_field?: 'reasoning_content' | undefined, reasoning_content_compat?: boolean | undefined, advisor?: AdvisorConfig | undefined}; export type CreateApiV1ChatCompletionsArgs = {'X-Provider'?: string | undefined, 'X-Billing-Mode'?: string | undefined, 'x-x402'?: 'true' | undefined, body: ChatCompletionRequest}; export type CreateApiV1ChatCompletionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const advisorPhaseBreakdown = z.object({executor: z.record(z.string(), z.unknown()).optional(), advisor: z.record(z.string(), z.unknown()).optional(), continuation: z.record(z.string(), z.unknown()).optional(), total: z.record(z.string(), z.unknown()).optional()}); export const advisorResponseMetadata = z.object({id: z.string(), mode: z.enum(["auto", "required"]), executor_model: z.string(), advisor_model: z.string(), requested: z.boolean(), consulted: z.boolean(), successful: z.boolean(), consultation_count: z.union([z.literal(0), z.literal(1)]), max_uses: z.literal(1), status: z.enum(["not_used", "completed", "failed"]), error: z.string().optional(), usage: advisorPhaseBreakdown.optional(), pricing: advisorPhaseBreakdown.optional()}); export const chatCompletionResponse = z.object({id: z.string().optional(), object: z.string().optional(), created: z.number().int().optional(), choices: z.array(z.object({index: z.number().int().optional(), message: z.object({role: z.literal("assistant").optional(), content: z.string().optional(), reasoning: z.string().optional(), reasoning_content: z.string().optional(), reasoning_details: z.array(z.record(z.string(), z.unknown())).optional()}).optional(), finish_reason: z.enum(["stop", "length", "content_filter"]).optional()})).optional(), usage: z.object({prompt_tokens: z.number().int().optional(), prompt_tokens_details: z.object({cached_tokens: z.number().int().optional()}).optional(), cache_creation_input_tokens: z.number().int().optional(), cache_read_input_tokens: z.number().int().optional(), completion_tokens: z.number().int().optional(), completion_tokens_details: z.object({reasoning_tokens: z.number().int().optional()}).and(z.record(z.string(), z.unknown())).optional(), reasoning_tokens: z.number().int().optional(), total_tokens: z.number().int().optional()}).optional(), service_tier: z.string().optional(), advisor: advisorResponseMetadata.optional()}); 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 }; /** * Creates a chat completion for the provided messages. The NanoGPT Advisor extension is available for non-streaming, platform-billed pay-as-you-go API-key requests that do not use client tools, structured output, inline moderation, BYOK, accountless payment, memory, or server-side content enhancements. * * POST /v1/chat/completions */ export const createApiV1ChatCompletions = async (args: CreateApiV1ChatCompletionsArgs, options?: CreateApiV1ChatCompletionsOptions) => { const {baseUrl = "https://api.nano-gpt.com/api", 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 (args['X-Provider'] != null) headers.set("X-Provider", String(args['X-Provider'])) if (args['X-Billing-Mode'] != null) headers.set("X-Billing-Mode", String(args['X-Billing-Mode'])) if (args['x-x402'] != null) headers.set("x-x402", String(args['x-x402'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/chat/completions${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(chatCompletionResponse, await res.json()) };