also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,34 @@ | ||
| 1 | import type { AIConfig, AIProvider } from './types' | |
| 2 | ||
| 3 | let globalConfig: AIConfig | undefined | |
| 4 | ||
| 5 | export function configureAI(config: Partial<AIConfig>): void { | |
| 6 | globalConfig = { | |
| 7 | default: config.default ?? Object.keys(config.providers ?? {})[0] ?? '', | |
| 8 | providers: config.providers ?? {}, | |
| 9 | } | |
| 10 | } | |
| 11 | ||
| 12 | export function useAI(provider?: string): AIProvider { | |
| 13 | if (!globalConfig) { | |
| 14 | throw new Error('AI not configured. Call configureAI() first.') | |
| 15 | } | |
| 16 | ||
| 17 | const name = provider ?? globalConfig.default | |
| 18 | ||
| 19 | if (!name) { | |
| 20 | throw new Error('No provider specified and no default provider configured.') | |
| 21 | } | |
| 22 | ||
| 23 | const p = globalConfig.providers[name] | |
| 24 | ||
| 25 | if (!p) { | |
| 26 | throw new Error(`AI provider "${name}" not found. Available providers: ${Object.keys(globalConfig.providers).join(', ')}`) | |
| 27 | } | |
| 28 | ||
| 29 | return p | |
| 30 | } | |
| 31 | ||
| 32 | export function resetAI(): void { | |
| 33 | globalConfig = undefined | |
| 34 | } | |