import { anthropic } from '@ai-sdk/anthropic'; import { openai } from '@ai-sdk/openai'; import type { LanguageModel } from 'ai'; // Pure factory — no DI. Caller passes provider/model (admin-editable, from // AiConfigService) and the API key (env-driven, ops-owned). Decoupling means // the model can be re-built per request without re-instantiating the caller // service, so admin updates to provider/model take effect immediately. export type AiProviderOpts = { provider: string; model: string; anthropicApiKey?: string; openaiApiKey?: string; }; export function createAiModel(opts: AiProviderOpts): LanguageModel | null { if (opts.provider === 'anthropic') { if (!opts.anthropicApiKey) return null; return anthropic(opts.model); } // Default to openai if (!opts.openaiApiKey) return null; return openai(opts.model); } export function isAiConfigured(opts: AiProviderOpts): boolean { if (opts.provider === 'anthropic') return !!opts.anthropicApiKey; return !!opts.openaiApiKey; }