import {z} from 'zod' export type Metadata = {[key: string]: string} | null; export type ServiceTier = 'auto' | 'default' | 'flex' | 'scale' | 'priority' | 'fast' | null; export type PromptCacheTTLEnum = '30m'; export type PromptCacheModeEnum = 'implicit' | 'explicit'; export type PromptCacheOptionsParam = {ttl?: PromptCacheTTLEnum | undefined, mode?: PromptCacheModeEnum | undefined}; export type PromptCacheBreakpointParam = {mode: 'explicit'}; export type ChatCompletionRequestMessageContentPartText = {type: 'text', text: string, prompt_cache_breakpoint?: PromptCacheBreakpointParam | undefined, cache_control?: ({ttl?: ('5m' | '1h') | undefined, type: 'ephemeral'}) | undefined}; export type ChatCompletionRequestSystemMessageContentPart = {type: 'text', text: string, prompt_cache_breakpoint?: PromptCacheBreakpointParam | undefined, cache_control?: ({ttl?: ('5m' | '1h') | undefined, type: 'ephemeral'}) | undefined}; export type ChatCompletionRequestUserMessageContentPart = {type: 'text', text: string, prompt_cache_breakpoint?: PromptCacheBreakpointParam | undefined, cache_control?: ({ttl?: ('5m' | '1h') | undefined, type: 'ephemeral'}) | undefined} | {type: 'image_url', image_url: {url: string, detail?: ('auto' | 'low' | 'high') | undefined}, prompt_cache_breakpoint?: PromptCacheBreakpointParam | undefined, cache_control?: ({ttl?: ('5m' | '1h') | undefined, type: 'ephemeral'}) | undefined} | {type: 'input_audio', input_audio: {data: string, format: 'wav' | 'mp3'}, prompt_cache_breakpoint?: PromptCacheBreakpointParam | undefined, cache_control?: ({ttl?: ('5m' | '1h') | undefined, type: 'ephemeral'}) | undefined} | {type: 'file', file: {filename?: string | undefined, file_data?: string | undefined, file_id?: string | undefined}, prompt_cache_breakpoint?: PromptCacheBreakpointParam | undefined, cache_control?: ({ttl?: ('5m' | '1h') | undefined, type: 'ephemeral'}) | undefined}; export type ChatCompletionRequestAssistantMessageContentPart = {type: 'text', text: string, prompt_cache_breakpoint?: PromptCacheBreakpointParam | undefined, cache_control?: ({ttl?: ('5m' | '1h') | undefined, type: 'ephemeral'}) | undefined} | {type: 'refusal', refusal: string}; export type ChatCompletionMessageToolCall = {id: string, type: 'function', function: {name: string, arguments: string}}; export type ChatCompletionMessageCustomToolCall = {id: string, type: 'custom', custom: {name: string, input: string}}; export type ChatCompletionMessageToolCalls = Array; export type ChatCompletionRequestToolMessageContentPart = {type: 'text', text: string, prompt_cache_breakpoint?: PromptCacheBreakpointParam | undefined, cache_control?: ({ttl?: ('5m' | '1h') | undefined, type: 'ephemeral'}) | undefined}; export type ChatCompletionRequestMessage = {content: string | Array, role: 'developer', name?: string | undefined} | {content: string | Array, role: 'system', name?: string | undefined} | {content: string | Array, role: 'user', name?: string | undefined} | {content?: (string | Array | null) | undefined, refusal?: (string | null) | undefined, role: 'assistant', name?: string | undefined, audio?: ({id: string} | null) | undefined, tool_calls?: ChatCompletionMessageToolCalls | undefined, function_call?: ({arguments: string, name: string} | null) | undefined, reasoning_content?: (string | null) | undefined, reasoning_details?: (Array<{[key: string]: unknown}> | null) | undefined} | {role: 'tool', content: string | Array, tool_call_id: string} | {role: 'function', content: string | null, name: string}; export type ModelIdsShared = string | 'gpt-5.6-sol' | 'gpt-5.6-terra' | 'gpt-5.6-luna' | 'gpt-5.5' | 'gpt-5.4' | 'gpt-5.4-mini' | 'gpt-5.4-nano' | 'gpt-5.4-mini-2026-03-17' | 'gpt-5.4-nano-2026-03-17' | 'gpt-5.3-chat-latest' | 'gpt-5.2' | 'gpt-5.2-2025-12-11' | 'gpt-5.2-chat-latest' | 'gpt-5.2-pro' | 'gpt-5.2-pro-2025-12-11' | 'gpt-5.1' | 'gpt-5.1-2025-11-13' | 'gpt-5.1-codex' | 'gpt-5.1-mini' | 'gpt-5.1-chat-latest' | 'gpt-5' | 'gpt-5-mini' | 'gpt-5-nano' | 'gpt-5-2025-08-07' | 'gpt-5-mini-2025-08-07' | 'gpt-5-nano-2025-08-07' | 'gpt-5-chat-latest' | 'gpt-4.1' | 'gpt-4.1-mini' | 'gpt-4.1-nano' | 'gpt-4.1-2025-04-14' | 'gpt-4.1-mini-2025-04-14' | 'gpt-4.1-nano-2025-04-14' | 'o4-mini' | 'o4-mini-2025-04-16' | 'o3' | 'o3-2025-04-16' | 'o3-mini' | 'o3-mini-2025-01-31' | 'o1' | 'o1-2024-12-17' | 'o1-preview' | 'o1-preview-2024-09-12' | 'o1-mini' | 'o1-mini-2024-09-12' | 'gpt-4o' | 'gpt-4o-2024-11-20' | 'gpt-4o-2024-08-06' | 'gpt-4o-2024-05-13' | 'gpt-4o-audio-preview' | 'gpt-4o-audio-preview-2024-10-01' | 'gpt-4o-audio-preview-2024-12-17' | 'gpt-4o-audio-preview-2025-06-03' | 'gpt-4o-mini-audio-preview' | 'gpt-4o-mini-audio-preview-2024-12-17' | 'gpt-4o-search-preview' | 'gpt-4o-mini-search-preview' | 'gpt-4o-search-preview-2025-03-11' | 'gpt-4o-mini-search-preview-2025-03-11' | 'chatgpt-4o-latest' | 'codex-mini-latest' | 'gpt-4o-mini' | 'gpt-4o-mini-2024-07-18' | 'gpt-4-turbo' | 'gpt-4-turbo-2024-04-09' | 'gpt-4-0125-preview' | 'gpt-4-turbo-preview' | 'gpt-4-1106-preview' | 'gpt-4-vision-preview' | 'gpt-4' | 'gpt-4-0314' | 'gpt-4-0613' | 'gpt-4-32k' | 'gpt-4-32k-0314' | 'gpt-4-32k-0613' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-16k' | 'gpt-3.5-turbo-0301' | 'gpt-3.5-turbo-0613' | 'gpt-3.5-turbo-1106' | 'gpt-3.5-turbo-0125' | 'gpt-3.5-turbo-16k-0613'; export type ResponseModalities = Array<'text' | 'audio'> | null; export type Verbosity = 'low' | 'medium' | 'high' | null; export type ReasoningEffort = 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | null; export type WebSearchLocation = {country?: string | undefined, region?: string | undefined, city?: string | undefined, timezone?: string | undefined}; export type WebSearchContextSize = 'low' | 'medium' | 'high'; export type ResponseFormatText = {type: 'text'}; export type ResponseFormatJsonSchemaSchema = {[key: string]: unknown}; export type ResponseFormatJsonSchema = {type: 'json_schema', json_schema: {description?: string | undefined, name: string, schema?: ResponseFormatJsonSchemaSchema | undefined, strict?: (boolean | null) | undefined}}; export type ResponseFormatJsonObject = {type: 'json_object'}; export type VoiceIdsOrCustomVoice = string | 'alloy' | 'ash' | 'ballad' | 'coral' | 'echo' | 'sage' | 'shimmer' | 'verse' | 'marin' | 'cedar' | {id: string}; export type ModerationMode = 'score' | 'block'; export type ModerationConfigParam = {mode: ModerationMode}; export type ModerationPolicyParam = {input?: (ModerationConfigParam | null) | undefined, output?: (ModerationConfigParam | null) | undefined}; export type ModerationParam = {model: string, policy?: (ModerationPolicyParam | null) | undefined}; export type StopConfiguration = string | null | Array | null; export type PredictionContent = {type: 'content', content: string | Array}; export type ChatCompletionStreamOptions = {include_usage?: boolean | undefined, include_obfuscation?: boolean | undefined} | null; export type FunctionParameters = {[key: string]: unknown}; export type FunctionObject = {description?: string | undefined, name: string, parameters?: FunctionParameters | undefined, strict?: (boolean | null) | undefined}; export type ChatCompletionTool = {type: 'function', function: FunctionObject, cache_control?: ({ttl?: ('5m' | '1h') | undefined, type: 'ephemeral'}) | undefined}; export type CustomToolChatCompletions = {type: 'custom', custom: {name: string, description?: string | undefined, format?: ({type: 'text'} | {type: 'grammar', grammar: {definition: string, syntax: 'lark' | 'regex'}}) | undefined}}; export type ChatCompletionAllowedTools = {mode: 'auto' | 'required', tools: Array<{[key: string]: unknown}>}; export type ChatCompletionToolChoiceOption = 'none' | 'auto' | 'required' | {type: 'allowed_tools', allowed_tools: ChatCompletionAllowedTools} | {type: 'function', function: {name: string}} | {type: 'custom', custom: {name: string}}; export type ParallelToolCalls = boolean; export type ChatCompletionFunctionCallOption = {name: string}; export type ChatCompletionFunctions = {description?: string | undefined, name: string, parameters?: FunctionParameters | undefined}; export type CreateChatCompletionRequest = {metadata?: Metadata | undefined, top_logprobs?: (number | null) | undefined, temperature?: (number | null) | undefined, top_p?: (number | null) | undefined, user?: string | undefined, safety_identifier?: (string | null) | undefined, prompt_cache_key?: (string | null) | undefined, service_tier?: ServiceTier | undefined, prompt_cache_retention?: ('in_memory' | '24h' | null) | undefined, prompt_cache_options?: PromptCacheOptionsParam | undefined, messages: Array, model: ModelIdsShared, modalities?: ResponseModalities | undefined, verbosity?: Verbosity | undefined, reasoning_effort?: ReasoningEffort | undefined, max_completion_tokens?: (number | null) | undefined, frequency_penalty?: (number | null) | undefined, presence_penalty?: (number | null) | undefined, web_search_options?: ({user_location?: ({type: 'approximate', approximate: WebSearchLocation} | null) | undefined, search_context_size?: WebSearchContextSize | undefined}) | undefined, response_format?: (ResponseFormatText | ResponseFormatJsonSchema | ResponseFormatJsonObject) | undefined, audio?: ({voice: VoiceIdsOrCustomVoice, format: 'wav' | 'aac' | 'mp3' | 'flac' | 'opus' | 'pcm16'} | null) | undefined, store?: (boolean | null) | undefined, moderation?: (ModerationParam | null) | undefined, stream?: (boolean | null) | undefined, stop?: StopConfiguration | undefined, logit_bias?: ({[key: string]: number} | null) | undefined, logprobs?: (boolean | null) | undefined, max_tokens?: (number | null) | undefined, n?: (number | null) | undefined, prediction?: (PredictionContent | null) | undefined, seed?: (number | null) | undefined, stream_options?: ChatCompletionStreamOptions | undefined, tools?: (Array) | undefined, tool_choice?: ChatCompletionToolChoiceOption | undefined, parallel_tool_calls?: ParallelToolCalls | undefined, function_call?: ('none' | 'auto' | ChatCompletionFunctionCallOption) | undefined, functions?: Array | undefined, top_k?: number | undefined}; export type CreateApiV1ChatCompletionsArgs = {body: CreateChatCompletionRequest}; export type CreateApiV1ChatCompletionsOptions = {baseUrl?: string | undefined, gatewayBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const chatCompletionMessageToolCall = z.object({id: z.string(), type: z.literal("function"), function: z.object({name: z.string(), arguments: z.string()})}); export const chatCompletionMessageCustomToolCall = z.object({id: z.string(), type: z.literal("custom"), custom: z.object({name: z.string(), input: z.string()})}); export const chatCompletionMessageToolCalls = z.array(z.discriminatedUnion("type", [chatCompletionMessageToolCall, chatCompletionMessageCustomToolCall])); export const chatCompletionResponseMessage = z.object({content: z.string().nullable(), refusal: z.string().nullable(), tool_calls: chatCompletionMessageToolCalls.optional(), annotations: z.array(z.object({type: z.literal("url_citation"), url_citation: z.object({end_index: z.number().int(), start_index: z.number().int(), url: z.string(), title: z.string()})})).optional(), role: z.literal("assistant"), function_call: z.object({arguments: z.string(), name: z.string()}).optional(), audio: z.object({id: z.string(), expires_at: z.number().int(), data: z.string(), transcript: z.string()}).nullable().optional(), reasoning_content: z.string().nullable().optional(), reasoning_details: z.array(z.record(z.string(), z.unknown())).nullable().optional()}); export const chatCompletionTokenLogprob = z.object({token: z.string(), logprob: z.number(), bytes: z.array(z.number().int()).nullable(), top_logprobs: z.array(z.object({token: z.string(), logprob: z.number(), bytes: z.array(z.number().int()).nullable()}))}); export const serviceTier = z.enum(["auto", "default", "flex", "scale", "priority", "fast"]).nullable(); export const completionUsage = z.object({completion_tokens: z.number().int(), prompt_tokens: z.number().int(), total_tokens: z.number().int(), completion_tokens_details: z.object({accepted_prediction_tokens: z.number().int().optional(), audio_tokens: z.number().int().optional(), reasoning_tokens: z.number().int().optional(), rejected_prediction_tokens: z.number().int().optional()}).optional(), prompt_tokens_details: z.object({audio_tokens: z.number().int().optional(), cached_tokens: z.number().int().optional(), cache_write_tokens: z.number().int().optional()}).optional()}); export const moderationInputType = z.enum(["text", "image"]); export const moderationResultBody = z.object({type: z.literal("moderation_result"), model: z.string(), flagged: z.boolean(), categories: z.record(z.string(), z.boolean()), category_scores: z.record(z.string(), z.number()), category_applied_input_types: z.record(z.string(), z.array(moderationInputType))}); export const chatCompletionModerationResults = z.object({type: z.literal("moderation_results"), model: z.string(), results: z.array(moderationResultBody)}); export const chatCompletionModerationError = z.object({type: z.literal("error"), code: z.string(), message: z.string()}); export const chatCompletionModeration = z.object({input: z.discriminatedUnion("type", [chatCompletionModerationResults, chatCompletionModerationError]), output: z.discriminatedUnion("type", [chatCompletionModerationResults, chatCompletionModerationError])}); export const createChatCompletionResponse = z.object({id: z.string(), choices: z.array(z.object({finish_reason: z.enum(["stop", "length", "tool_calls", "content_filter", "function_call"]), index: z.number().int(), message: chatCompletionResponseMessage, logprobs: z.object({content: z.array(chatCompletionTokenLogprob).nullable(), refusal: z.array(chatCompletionTokenLogprob).nullable()}).nullable()})), created: z.number().int(), model: z.string(), service_tier: serviceTier.optional(), system_fingerprint: z.string().optional(), object: z.literal("chat.completion"), usage: completionUsage.optional(), moderation: chatCompletionModeration.nullable().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 }; /** * /v1/chat/completions * * POST /v1/chat/completions */ export const createApiV1ChatCompletions = async (args: CreateApiV1ChatCompletionsArgs, options?: CreateApiV1ChatCompletionsOptions) => { const {baseUrl = "https://aihubmix.com", fetch: fetchFn = fetch, gatewayBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (gatewayBearer !== undefined) headers.set('authorization', `Bearer ${gatewayBearer}`) 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(createChatCompletionResponse, await res.json()) };