// ReadableStream is available globally in modern browsers and Node.js 16+ import { AxAIAnthropic, type AxAIAnthropicArgs } from './anthropic/api.js'; import type { AxAIAnthropicModel } from './anthropic/types.js'; import { AxAIAzureOpenAI, type AxAIAzureOpenAIArgs, } from './azure-openai/api.js'; import type { AxAIFeatures } from './base.js'; import { AxAICohere, type AxAICohereArgs } from './cohere/api.js'; import type { AxAICohereEmbedModel, AxAICohereModel } from './cohere/types.js'; import { AxAIDeepSeek, type AxAIDeepSeekArgs } from './deepseek/api.js'; import type { AxAIDeepSeekModel } from './deepseek/types.js'; import { AxAIGoogleGemini, type AxAIGoogleGeminiArgs, } from './google-gemini/api.js'; import type { AxAIGoogleGeminiEmbedModel, AxAIGoogleGeminiModel, } from './google-gemini/types.js'; import { AxAIGroq, type AxAIGroqArgs } from './groq/api.js'; import type { AxAIGroqModel } from './groq/types.js'; import { AxAIHuggingFace, type AxAIHuggingFaceArgs, } from './huggingface/api.js'; import type { AxAIHuggingFaceModel } from './huggingface/types.js'; import { AxAIMistral, type AxAIMistralArgs } from './mistral/api.js'; import type { AxAIMistralModel } from './mistral/types.js'; import { AxAIOllama, type AxAIOllamaArgs } from './ollama/api.js'; import { AxAIOpenAI, type AxAIOpenAIArgs } from './openai/api.js'; import type { AxAIOpenAIEmbedModel, AxAIOpenAIModel, } from './openai/chat_types.js'; import { AxAIOpenAIResponses, type AxAIOpenAIResponsesArgs, } from './openai/responses_api_base.js'; import type { AxAIOpenAIResponsesModel } from './openai/responses_types.js'; import { AxAIDreamsRouter, type AxAIDreamsRouterArgs, } from './dreamsrouter/api.js'; import { AxAIOpenRouter, type AxAIOpenRouterArgs, } from './openrouter/api.js'; import { AxAIReka, type AxAIRekaArgs } from './reka/api.js'; import { AxAITogether, type AxAITogetherArgs } from './together/api.js'; import type { AxAIModelList, AxAIService, AxAIServiceMetrics, AxAIServiceOptions, AxChatRequest, AxChatResponse, AxEmbedRequest, AxEmbedResponse, AxLoggerFunction, } from './types.js'; import { AxAIWebLLM, type AxAIWebLLMArgs } from './webllm/api.js'; import type { AxAIWebLLMModel } from './webllm/types.js'; import { AxAIGrok, type AxAIGrokArgs } from './x-grok/api.js'; import type { AxAIGrokModel } from './x-grok/types.js'; export type AxAIArgs = | AxAIOpenAIArgs<'openai', AxAIOpenAIModel, AxAIOpenAIEmbedModel, TModelKey> | AxAIOpenAIResponsesArgs< 'openai-responses', AxAIOpenAIResponsesModel, AxAIOpenAIEmbedModel, TModelKey > | AxAIAzureOpenAIArgs | AxAITogetherArgs | AxAIDreamsRouterArgs | AxAIOpenRouterArgs | AxAIAnthropicArgs | AxAIGroqArgs | AxAIGoogleGeminiArgs | AxAICohereArgs | AxAIHuggingFaceArgs | AxAIMistralArgs | AxAIDeepSeekArgs | AxAIOllamaArgs | AxAIRekaArgs | AxAIGrokArgs | AxAIWebLLMArgs; export type AxAIModels = | AxAIOpenAIModel | AxAIAnthropicModel | AxAIGroqModel | AxAIGoogleGeminiModel | AxAICohereModel | AxAIHuggingFaceModel | AxAIMistralModel | AxAIDeepSeekModel | AxAIGrokModel | AxAIWebLLMModel; export type AxAIEmbedModels = | AxAIOpenAIEmbedModel | AxAIGoogleGeminiEmbedModel | AxAICohereEmbedModel; // Helper to extract both model keys and enum values from model configurations type ExtractModelKeysAndValues = T extends readonly { key: infer K; model: infer M; }[] ? K | M : never; // Helper to infer TModelKey from args - now includes both keys and enum values type InferTModelKey = T extends { models: infer M } ? ExtractModelKeysAndValues : string; /** * Factory function for creating AxAI instances with type safety. * This is the recommended way to create AxAI instances instead of using the constructor. * * @param options - Configuration options for the AI service * @returns A properly typed AxAI instance * * @example * ```typescript * const ai = ai({ * name: 'openai', * apiKey: process.env.OPENAI_APIKEY! * }); * ``` */ export function ai>( options: T ): AxAI> { return AxAI.create(options); } export class AxAI implements AxAIService { private ai: AxAIService; // Static factory method for automatic type inference static create>( options: T ): AxAI> { return new AxAI(options) as any; } /** * @deprecated Use `AxAI.create()` or `ai()` function instead for better type safety. * This constructor will be removed in v15.0.0. * * Migration timeline: * - v13.0.24+: Deprecation warnings (current) * - v14.0.0: Runtime console warnings * - v15.0.0: Complete removal * * @example * ```typescript * // Instead of: new AxAI({ name: 'openai', apiKey: '...' }) * // Use: AxAI.create({ name: 'openai', apiKey: '...' }) * // Or: ai({ name: 'openai', apiKey: '...' }) * ``` */ constructor(options: Readonly>) { switch (options.name) { case 'openai': this.ai = new AxAIOpenAI(options); break; case 'openai-responses': this.ai = new AxAIOpenAIResponses(options); break; case 'azure-openai': this.ai = new AxAIAzureOpenAI(options); break; case 'grok': this.ai = new AxAIGrok(options); break; case 'huggingface': this.ai = new AxAIHuggingFace(options); break; case 'groq': this.ai = new AxAIGroq(options); break; case 'together': this.ai = new AxAITogether(options); break; case 'dreamsrouter': this.ai = new AxAIDreamsRouter(options); break; case 'openrouter': this.ai = new AxAIOpenRouter(options); break; case 'cohere': this.ai = new AxAICohere(options); break; case 'google-gemini': this.ai = new AxAIGoogleGemini(options); break; case 'anthropic': this.ai = new AxAIAnthropic(options); break; case 'mistral': this.ai = new AxAIMistral(options); break; case 'deepseek': this.ai = new AxAIDeepSeek(options); break; case 'ollama': this.ai = new AxAIOllama(options); break; case 'reka': this.ai = new AxAIReka(options); break; case 'webllm': this.ai = new AxAIWebLLM(options); break; default: throw new Error('Unknown AI'); } } getName(): string { return this.ai.getName(); } getId(): string { return this.ai.getId(); } getFeatures(model?: string): AxAIFeatures { return this.ai.getFeatures(model); } getModelList() { return this.ai.getModelList() as AxAIModelList | undefined; } getLastUsedChatModel() { return this.ai.getLastUsedChatModel(); } getLastUsedEmbedModel() { return this.ai.getLastUsedEmbedModel(); } getLastUsedModelConfig() { return this.ai.getLastUsedModelConfig(); } getMetrics(): AxAIServiceMetrics { return this.ai.getMetrics(); } async chat( req: Readonly>, options?: Readonly ): Promise> { return await this.ai.chat(req, options); } async embed( req: Readonly>, options?: Readonly ): Promise { return await this.ai.embed(req, options); } setOptions(options: Readonly): void { this.ai.setOptions(options); } getOptions(): Readonly { return this.ai.getOptions(); } getLogger(): AxLoggerFunction { return this.ai.getLogger(); } }