import {z} from 'zod' export type ChatMessageRole = 'system' | 'user' | 'assistant' | 'tool'; export type ChatCompletionContentPartTextParam = {text: string, type: 'text'}; export type ImageURL = {detail?: ('auto' | 'low' | 'high' | null) | undefined, url: string}; export type ChatCompletionContentPartImageParam = {image_url: ImageURL, type: 'image_url'}; export type VideoURL = {detail?: ('auto' | 'low' | 'high' | null) | undefined, url: string}; export type ChatCompletionContentPartVideoParam = {video_url: VideoURL, type: 'video_url'}; export type FunctionCall = {name: string, arguments: string}; export type ToolCall = {id?: string | undefined, type?: 'function' | undefined, function: FunctionCall}; export type ChatCompletionMessage = {role: ChatMessageRole, content?: (string | Array | null) | undefined, name?: (string | null) | undefined, tool_calls?: (Array | null) | undefined, tool_call_id?: (string | null) | undefined, reasoning_content?: (string | null) | undefined, reasoning?: (string | null) | undefined}; export type FunctionObject = {name: string, description?: (string | null) | undefined, parameters?: ({[key: string]: unknown} | null) | undefined, strict?: (boolean | null) | undefined, defer_loading?: (boolean | null) | undefined}; export type ChatCompletionTool = {type?: 'function' | undefined, function: FunctionObject}; export type ChatCompletionNamedFunction = {name: string}; export type ChatCompletionNamedToolChoice = {type?: 'function' | undefined, function: ChatCompletionNamedFunction}; export type JsonSchemaResponseFormat = {name: string, description?: (string | null) | undefined, schema: {[key: string]: unknown} | null, strict?: (boolean | null) | undefined}; export type AppModelsChatCompletionResponseFormat = {type: 'text' | 'json_object' | 'json_schema', json_schema?: (JsonSchemaResponseFormat | null) | undefined}; export type ServiceTier = 'auto' | 'default' | 'over-limit' | 'flex' | 'no-limit'; export type ChatCompletionRequest = {model: string, store?: (boolean | null) | undefined, messages: Array, max_tokens?: (number | null) | undefined, max_completion_tokens?: (number | null) | undefined, temperature?: (number | null) | undefined, top_p?: (number | null) | undefined, tools?: (Array | null) | undefined, tool_choice?: ('none' | 'auto' | 'required' | ChatCompletionNamedToolChoice | null) | undefined, reasoning_effort?: ('none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | null) | undefined, n?: (number | null) | undefined, stream?: (boolean | null) | undefined, stream_options?: ({[key: string]: boolean} | null) | undefined, stop?: (Array | null) | undefined, presence_penalty?: (number | null) | undefined, frequency_penalty?: (number | null) | undefined, logit_bias?: ({[key: string]: number} | null) | undefined, logprobs?: (boolean | null) | undefined, top_logprobs?: (number | null) | undefined, user?: (string | null) | undefined, response_format?: (AppModelsChatCompletionResponseFormat | null) | undefined, extra_body?: ({[key: string]: unknown} | null) | undefined, service_tier?: ServiceTier | undefined} | {[key: string]: unknown}; export type CreateApiV1ChatCompletionsArgs = {ai_project_id?: (string | null) | undefined, body: ChatCompletionRequest}; export type CreateApiV1ChatCompletionsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const chatCompletionObject = z.literal("chat.completion"); export const chatMessageRole = z.enum(["system", "user", "assistant", "tool"]); export const functionCall = z.object({name: z.string(), arguments: z.string()}); export const toolCall = z.object({id: z.string().optional(), type: z.literal("function").optional(), function: functionCall}); export const chatCompletionResponseMessage = z.object({role: chatMessageRole, content: z.string().nullable().optional(), tool_calls: z.array(toolCall).nullable().optional()}); export const chatCompletionFinishReason = z.enum(["stop", "length", "tool_calls", "content_filter"]); export const chatCompletionTopLogprob = z.object({token: z.string(), logprob: z.number(), bytes: z.array(z.number().int()).nullable()}); export const chatCompletionTokenLogprob = z.object({token: z.string(), logprob: z.number(), bytes: z.array(z.number().int()).nullable(), top_logprobs: z.array(chatCompletionTopLogprob)}); export const chatCompletionLogprobs = z.object({content: z.array(chatCompletionTokenLogprob).nullable()}); export const chatCompletionChoice = z.object({index: z.number().int(), message: chatCompletionResponseMessage, finish_reason: chatCompletionFinishReason, logprobs: chatCompletionLogprobs.nullable()}); export const promptTokensDetails = z.object({cached_tokens: z.number().int().nullable().optional()}); export const completionTokensDetails = z.object({reasoning_tokens: z.number().int().nullable().optional()}); export const usage = z.object({completion_tokens: z.number().int(), prompt_tokens: z.number().int(), total_tokens: z.number().int(), prompt_tokens_details: promptTokensDetails.nullable().optional(), completion_tokens_details: completionTokensDetails.nullable().optional()}); export const serviceTier = z.enum(["auto", "default", "over-limit", "flex", "no-limit"]); export const chatCompletionResponse = z.object({id: z.string(), object: chatCompletionObject, created: z.number().int(), model: z.string(), choices: z.array(chatCompletionChoice), usage: usage, service_tier: serviceTier}); export const chatCompletionStreamResponseDelta = z.object({content: z.string().nullable().optional(), role: chatMessageRole, tool_calls: z.array(toolCall).nullable().optional(), reasoning_content: z.string().nullable().optional(), reasoning: z.string().nullable().optional()}); export const chatCompletionStreamChoice = z.object({index: z.number().int(), delta: chatCompletionStreamResponseDelta, finish_reason: chatCompletionFinishReason.nullable(), logprobs: chatCompletionLogprobs.nullable()}); export const chatCompletionChunkObject = z.literal("chat.completion.chunk"); export const chatCompletionChunk = z.object({id: z.string(), choices: z.array(chatCompletionStreamChoice), created: z.number().int(), model: z.string(), system_fingerprint: z.string(), object: chatCompletionChunkObject, usage: usage.nullable()}); export const createApiV1ChatCompletionsResponse = z.union([chatCompletionResponse, chatCompletionChunk]); 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 }; /** * Create chat completion * * POST /v1/chat/completions */ export const createApiV1ChatCompletions = async (args: CreateApiV1ChatCompletionsArgs, options?: CreateApiV1ChatCompletionsOptions) => { const {baseUrl = "https://api.tokenfactory.nebius.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.ai_project_id != null) query.append("ai_project_id", String(args.ai_project_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) 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(createApiV1ChatCompletionsResponse, await res.json()) };