/** * model-provider.ts — 模型管理、提供商 SDK 调用、格式转换入口 * * 整体流程: * 用户请求 → 统计输入 token → 代理转换 → SDK 调用 → 流式/非流式响应 → 统计输出 token * * 转换层统一使用 ConvertUtils(service-convert 框架), * 模型管理和 SDK 调用保持与 OpenAI/Anthropic SDK 的直接集成。 */ import Anthropic from '@anthropic-ai/sdk'; import type { Message, MessageParam, Tool } from '@anthropic-ai/sdk/resources/messages/messages'; import OpenAI from 'openai'; export interface ModelRow { id: number; name: string; model_name: string; url: string; max_content_length: number; max_token: number; api_key: string; sort_index: number; api_format: number; model_label_id: number | null; capabilities: string | null; isLock: number; isDisable: number; user_id?: number; created_at: string; } export interface AIProvider { type: 'anthropic' | 'openai-chat' | 'openai-responses'; client: Anthropic | OpenAI; modelName: string; } export interface GenericMessage { role: 'system' | 'user' | 'assistant' | 'tool'; content: string | Array> | null; tool_call_id?: string; tool_calls?: Array<{ id: string; type: 'function'; function: { name: string; arguments: string; }; }>; } export interface OpenAIMessage { role: string; content: string | Array> | null; tool_calls?: Array<{ id: string; type: string; function: { name: string; arguments: string; }; }>; tool_call_id?: string; name?: string; } export interface OpenAITool { type: string; function: { name: string; description?: string; parameters: Record; }; } export interface UserSettings { max_content_length: number; max_token: number; lock_duration?: number; proxy_url?: string; } export interface ChatCallParams { messages: GenericMessage[]; maxOutputTokens: number; temperature?: number; topP?: number; tools?: Array<{ function: { name: string; description?: string; parameters: Record; }; }>; toolChoice?: string; system?: string; } export declare const MAX_RESPONSE_TOKENS = 64000; export declare const REQUEST_TIMEOUT_MS = 10000; export declare const API_FORMAT: { readonly OPENAI_CHAT: 1; readonly ANTHROPIC: 2; readonly OPENAI_RESPONSES: 3; }; export declare function getLockDurationMs(): number; export declare const LOCK_DURATION_MS: number; /** 上游代理 URL,从数据库读取,空则不使用代理,重启后生效 */ export declare const PROXY_URL: string; /** 生成随机字符串 */ export declare function generateRandomString(length?: number): string; /** 估算文本的 token 数 */ export declare function estimateTextTokens(text: string): number; /** 估算消息数组的 token 数 */ export declare function estimateMessagesTokens(messages: GenericMessage[]): number; export declare function getUserSettings(): UserSettings; export declare function getUserEffectiveSettings(): UserSettings; export declare function getEffectiveContentLength(modelContentLength: number): number; export declare function getEffectiveMaxToken(): number; /** 根据用户名查询用户 ID */ export declare function getUserIdByUsername(username: string): number | null; export declare function createModelProvider(model: ModelRow): AIProvider; export declare function isModelLocked(isLock: number): { locked: boolean; expired: boolean; }; export declare function getAllModels(userId?: number): ModelRow[]; export declare function unlockExpiredModels(userId?: number): void; export declare function getAvailableModels(userId?: number): ModelRow[]; export declare function getModelByName(name: string, userId?: number): ModelRow | undefined; /** 判断是否为"不支持多模态"类型的错误 */ export declare function isMultimodalNotSupportedError(err: unknown): boolean; /** * 判断是否为上游网关/代理故障类型的错误 * * 某些 API 网关(如 OpenCode Console Go)在上游服务不可用时, * 返回400而非5xx。这类错误不应锁定模型,应允许故障转移到下一个模型。 */ export declare function isUpstreamGatewayError(err: unknown): boolean; export declare function tryModelsSequentially(requestModelName: string, tryFn: (model: ModelRow) => Promise | T, userId?: number): Promise; export declare function getOrderedModels(requestModelName: string, userId?: number): ModelRow[]; export declare function getCachedResponseId(baseUrl: string, modelName: string): string | undefined; export declare function setCachedResponseId(baseUrl: string, modelName: string, responseId: string): void; export declare function chatResultToChunk(chatResult: Record, modelName: string): OpenAI.Chat.ChatCompletionChunk; export declare function createSingleChunkStream(chunk: T): AsyncIterable; /** OpenAI Chat 消息 → Anthropic Messages 格式 */ export declare function convertToAnthropicMessages(messages: OpenAIMessage[], system?: string): { system?: string; messages: MessageParam[]; }; /** OpenAI Chat 消息 → 通用消息格式 */ export declare function convertToOpenAIChatMessages(messages: OpenAIMessage[]): GenericMessage[]; export declare function convertToAnthropicTools(tools: OpenAITool[]): Tool[]; export declare function convertToOpenAITools(tools: OpenAITool[]): Record[]; export declare function mapToolChoice(toolChoice: unknown, direction: 'to-anthropic' | 'to-openai'): unknown; export declare function mapStopSequences(stop: unknown, direction: 'to-anthropic' | 'to-openai'): unknown; export declare function convertAnthropicToChatCompletion(response: Message, modelName: string): Record; export declare function convertOpenAIChatToAnthropicResponse(chatCompletion: Record, requestBody: Record): Record; export declare function toAnthropicError(message: string, type?: string): Record; export declare function convertAnthropicRequestToCommon(body: Record): ChatCallParams; export declare function callOpenAIChat(provider: AIProvider, params: ChatCallParams): Promise; export declare function callAnthropic(provider: AIProvider, params: ChatCallParams): Promise>; export declare function callOpenAIResponses(provider: AIProvider, params: ChatCallParams, _originalBody: Record): Promise>; export declare function callAnthropicMessages(provider: AIProvider, params: ChatCallParams): Promise>; /** Anthropic 流式创建,用于 /v1/messages 端点流式转发 */ export declare function streamAnthropicMessages(provider: AIProvider, params: ChatCallParams): Promise; /** 构建 OpenAI 模型响应格式 */ export declare function buildOpenAIModel(model: ModelRow): { id: string; object: string; created: number; owned_by: string; name: string; content_length: number; capabilities: any; supports_responses_api: boolean; max_output_tokens: number; }; export declare function parseModelCapabilities(raw?: string | null): string[]; /** * Responses API 请求 → Chat Completion 请求 */ export declare function convertResponsesRequestToChatRequest(body: Record): Record; //# sourceMappingURL=model-provider.d.ts.map