Files

1 line
4.7 KiB
JSON

{"repo": "aswathas/ai-brain-cli", "files": {"src/llm/providers/minimax.ts": {"4978896f0cf309cad2f128c301ad5bb0614ac3d7": {"t": 1785674022.6087277, "n": 4431, "h": "8f3032fc3366fed4", "c": "import axios, { AxiosInstance } from 'axios';\nimport { Message, Response, ChatOptions, LLMProvider } from '../../types';\nimport { configStore } from '../../config/store';\n\n/**\n * MiniMax provider - Chinese LLM API with strong reasoning capabilities\n * Uses MiniMax's API endpoint with custom model names\n */\nexport class MiniMaxProvider implements LLMProvider {\n private apiKey: string;\n private baseURL: string;\n private defaultModelValue: string;\n\n constructor() {\n const config = configStore.getProvider('minimax') || { type: 'minimax' };\n // MiniMax uses Anthropic-compatible API at https://api.minimax.io/anthropic\n // API key is the token plan key (sk-cp-...)\n this.apiKey = config.apiKey || process.env.ANTHROPIC_AUTH_TOKEN || '';\n this.baseURL = 'https://api.minimax.io/anthropic/v1';\n // Model names\n this.defaultModelValue = config.defaultModel || 'MiniMax-M2.7';\n }\n\n name(): string {\n return 'minimax';\n }\n\n displayName(): string {\n return 'MiniMax';\n }\n\n supportsAPIKey(): boolean {\n return true;\n }\n\n supportsOAuth(): boolean {\n return false;\n }\n\n defaultModel(): string {\n return this.defaultModelValue;\n }\n\n async chat(messages: Message[], options?: ChatOptions): Promise<Response> {\n if (!this.apiKey) {\n throw new Error('MiniMax API key not configured. Run \"ai-brain config set-api-key minimax\" or set MINIMAX_API_KEY environment variable.');\n }\n\n const model = options?.model || this.defaultModelValue;\n const temperature = options?.temperature ?? 0.7;\n const maxTokens = options?.maxTokens || 4096;\n\n // Format messages\n const chatMessages: { role: string; content: string }[] = [];\n let systemPrompt = '';\n\n for (const msg of messages) {\n if (msg.role === 'system') {\n systemPrompt += (systemPrompt ? '\\n\\n' : '') + msg.content;\n } else {\n chatMessages.push({\n role: msg.role === 'assistant' ? 'assistant' : 'user',\n content: msg.content,\n });\n }\n }\n\n try {\n const client: AxiosInstance = axios.create({\n baseURL: this.baseURL,\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${this.apiKey}`,\n 'anthropic-version': '2023-06-01',\n },\n });\n\n const requestBody: any = {\n model,\n messages: chatMessages,\n temperature,\n max_tokens: maxTokens,\n };\n\n if (systemPrompt) {\n requestBody.system = systemPrompt;\n }\n\n const response = await client.post('/messages', requestBody);\n\n // MiniMax Anthropic-compatible response format\n // Content is an array with objects that have type: \"text\" or \"thinking\"\n const contentArray = response.data.content || [];\n let responseText = '';\n for (const item of contentArray) {\n if (item.type === 'text') {\n responseText = item.text;\n break;\n }\n }\n\n return {\n content: responseText,\n model: response.data.model || model,\n usage: {\n promptTokens: response.data.usage?.input_tokens || 0,\n completionTokens: response.data.usage?.output_tokens || 0,\n totalTokens: (response.data.usage?.input_tokens || 0) + (response.data.usage?.output_tokens || 0),\n },\n done: true,\n };\n } catch (error: any) {\n if (error.response) {\n const status = error.response.status;\n const data = error.response.data;\n\n if (status === 401) {\n throw new Error('Invalid MiniMax API key. Please check your configuration.');\n } else if (status === 429) {\n throw new Error('MiniMax API rate limit exceeded. Please try again later.');\n } else if (status === 400) {\n throw new Error(`MiniMax API error: ${data.error?.message || 'Bad request'}`);\n }\n\n throw new Error(`MiniMax API error (${status}): ${data.error?.message || 'Unknown error'}`);\n }\n\n throw new Error(`MiniMax API request failed: ${error.message}`);\n }\n }\n\n async listModels(): Promise<string[]> {\n return ['MiniMax-Text-01', 'abab6.5s-chat', 'abab6-chat'];\n }\n\n /**\n * Set API key\n */\n setAPIKey(apiKey: string): void {\n this.apiKey = apiKey;\n configStore.setAPIKey('minimax', apiKey);\n }\n\n /**\n * Check if configured\n */\n isConfigured(): boolean {\n return !!this.apiKey;\n }\n}\n\nexport default MiniMaxProvider;"}}}}