|
1 | 1 | /** |
2 | | - * Typed import helpers for provider packages |
| 2 | + * Provider Definitions - Single source of truth for all provider metadata |
3 | 3 | * |
4 | | - * These functions provide type-safe dynamic imports for provider packages. |
5 | | - * TypeScript can infer the correct module type from literal string imports, |
6 | | - * giving consuming code full type safety for provider constructors. |
7 | | - */ |
8 | | - |
9 | | -/** |
10 | | - * Dynamically import the Anthropic provider package |
11 | | - */ |
12 | | -export function importAnthropic() { |
13 | | - return import("@ai-sdk/anthropic"); |
14 | | -} |
15 | | - |
16 | | -/** |
17 | | - * Dynamically import the OpenAI provider package |
18 | | - */ |
19 | | -export function importOpenAI() { |
20 | | - return import("@ai-sdk/openai"); |
21 | | -} |
22 | | - |
23 | | -/** |
24 | | - * Dynamically import the Ollama provider package |
25 | | - */ |
26 | | -export function importOllama() { |
27 | | - return import("ollama-ai-provider-v2"); |
28 | | -} |
29 | | - |
30 | | -/** |
31 | | - * Dynamically import the Google provider package |
32 | | - */ |
33 | | -export function importGoogle() { |
34 | | - return import("@ai-sdk/google"); |
35 | | -} |
36 | | - |
37 | | -/** |
38 | | - * Dynamically import the OpenRouter provider package |
39 | | - */ |
40 | | -export function importOpenRouter() { |
41 | | - return import("@openrouter/ai-sdk-provider"); |
42 | | -} |
43 | | - |
44 | | -/** |
45 | | - * Dynamically import the xAI provider package |
46 | | - */ |
47 | | -export function importXAI() { |
48 | | - return import("@ai-sdk/xai"); |
49 | | -} |
50 | | - |
51 | | -/** |
52 | | - * Dynamically import the Amazon Bedrock provider package |
| 4 | + * When adding a new provider: |
| 5 | + * 1. Add entry to PROVIDER_DEFINITIONS below |
| 6 | + * 2. Add SVG icon + import in src/browser/components/ProviderIcon.tsx |
| 7 | + * 3. If provider needs custom logic, add handler in aiService.ts |
| 8 | + * (simple providers using standard pattern are handled automatically) |
| 9 | + * |
| 10 | + * Simple providers (requiresApiKey + standard factory pattern) need NO aiService.ts changes. |
53 | 11 | */ |
54 | | -export function importBedrock() { |
55 | | - return import("@ai-sdk/amazon-bedrock"); |
56 | | -} |
57 | 12 |
|
58 | | -/** |
59 | | - * Dynamically import the Gateway provider from the AI SDK |
60 | | - */ |
61 | | -export function importMuxGateway() { |
62 | | - return import("ai"); |
| 13 | +interface ProviderDefinition { |
| 14 | + /** Display name for UI (proper casing) */ |
| 15 | + displayName: string; |
| 16 | + /** Dynamic import function for lazy loading */ |
| 17 | + import: () => Promise<unknown>; |
| 18 | + /** Name of the factory function exported by the package */ |
| 19 | + factoryName: string; |
| 20 | + /** Whether provider requires an API key (false for local services like Ollama) */ |
| 21 | + requiresApiKey: boolean; |
| 22 | + /** Whether this provider uses stroke-based icon styling instead of fill */ |
| 23 | + strokeBasedIcon?: boolean; |
63 | 24 | } |
64 | 25 |
|
65 | | -/** |
66 | | - * Centralized provider registry mapping provider names to their import functions |
67 | | - * |
68 | | - * This is the single source of truth for supported providers. By mapping to import |
69 | | - * functions rather than package strings, we eliminate duplication while maintaining |
70 | | - * perfect type safety. |
71 | | - * |
72 | | - * When adding a new provider: |
73 | | - * 1. Create an importXxx() function above |
74 | | - * 2. Add entry mapping provider name to the import function |
75 | | - * 3. Implement provider handling in aiService.ts createModel() |
76 | | - * 4. Runtime check will fail if provider in registry but no handler |
77 | | - */ |
78 | | -export const PROVIDER_REGISTRY = { |
79 | | - anthropic: importAnthropic, |
80 | | - openai: importOpenAI, |
81 | | - google: importGoogle, |
82 | | - xai: importXAI, |
83 | | - ollama: importOllama, |
84 | | - openrouter: importOpenRouter, |
85 | | - bedrock: importBedrock, |
86 | | - "mux-gateway": importMuxGateway, |
87 | | -} as const; |
| 26 | +// Order determines display order in UI (Settings, model selectors, etc.) |
| 27 | +export const PROVIDER_DEFINITIONS = { |
| 28 | + "mux-gateway": { |
| 29 | + displayName: "Mux Gateway", |
| 30 | + import: () => import("ai"), |
| 31 | + factoryName: "createGateway", |
| 32 | + requiresApiKey: true, // Uses couponCode |
| 33 | + strokeBasedIcon: true, |
| 34 | + }, |
| 35 | + anthropic: { |
| 36 | + displayName: "Anthropic", |
| 37 | + import: () => import("@ai-sdk/anthropic"), |
| 38 | + factoryName: "createAnthropic", |
| 39 | + requiresApiKey: true, |
| 40 | + }, |
| 41 | + openai: { |
| 42 | + displayName: "OpenAI", |
| 43 | + import: () => import("@ai-sdk/openai"), |
| 44 | + factoryName: "createOpenAI", |
| 45 | + requiresApiKey: true, |
| 46 | + }, |
| 47 | + google: { |
| 48 | + displayName: "Google", |
| 49 | + import: () => import("@ai-sdk/google"), |
| 50 | + factoryName: "createGoogleGenerativeAI", |
| 51 | + requiresApiKey: true, |
| 52 | + }, |
| 53 | + xai: { |
| 54 | + displayName: "xAI", |
| 55 | + import: () => import("@ai-sdk/xai"), |
| 56 | + factoryName: "createXai", |
| 57 | + requiresApiKey: true, |
| 58 | + }, |
| 59 | + deepseek: { |
| 60 | + displayName: "DeepSeek", |
| 61 | + import: () => import("@ai-sdk/deepseek"), |
| 62 | + factoryName: "createDeepSeek", |
| 63 | + requiresApiKey: true, |
| 64 | + }, |
| 65 | + openrouter: { |
| 66 | + displayName: "OpenRouter", |
| 67 | + import: () => import("@openrouter/ai-sdk-provider"), |
| 68 | + factoryName: "createOpenRouter", |
| 69 | + requiresApiKey: true, |
| 70 | + }, |
| 71 | + bedrock: { |
| 72 | + displayName: "Amazon Bedrock", |
| 73 | + import: () => import("@ai-sdk/amazon-bedrock"), |
| 74 | + factoryName: "createAmazonBedrock", |
| 75 | + requiresApiKey: false, // Uses AWS credential chain |
| 76 | + }, |
| 77 | + ollama: { |
| 78 | + displayName: "Ollama", |
| 79 | + import: () => import("ollama-ai-provider-v2"), |
| 80 | + factoryName: "createOllama", |
| 81 | + requiresApiKey: false, // Local service |
| 82 | + }, |
| 83 | +} as const satisfies Record<string, ProviderDefinition>; |
88 | 84 |
|
89 | 85 | /** |
90 | 86 | * Union type of all supported provider names |
91 | 87 | */ |
92 | | -export type ProviderName = keyof typeof PROVIDER_REGISTRY; |
| 88 | +export type ProviderName = keyof typeof PROVIDER_DEFINITIONS; |
93 | 89 |
|
94 | 90 | /** |
95 | 91 | * Array of all supported provider names (for UI lists, iteration, etc.) |
96 | 92 | */ |
97 | | -export const SUPPORTED_PROVIDERS = Object.keys(PROVIDER_REGISTRY) as ProviderName[]; |
| 93 | +export const SUPPORTED_PROVIDERS = Object.keys(PROVIDER_DEFINITIONS) as ProviderName[]; |
98 | 94 |
|
99 | 95 | /** |
100 | 96 | * Display names for providers (proper casing for UI) |
| 97 | + * Derived from PROVIDER_DEFINITIONS - do not edit directly |
| 98 | + */ |
| 99 | +export const PROVIDER_DISPLAY_NAMES: Record<ProviderName, string> = Object.fromEntries( |
| 100 | + Object.entries(PROVIDER_DEFINITIONS).map(([key, def]) => [key, def.displayName]) |
| 101 | +) as Record<ProviderName, string>; |
| 102 | + |
| 103 | +/** |
| 104 | + * Legacy registry for backward compatibility with aiService.ts |
| 105 | + * Maps provider names to their import functions |
101 | 106 | */ |
102 | | -export const PROVIDER_DISPLAY_NAMES: Record<ProviderName, string> = { |
103 | | - anthropic: "Anthropic", |
104 | | - openai: "OpenAI", |
105 | | - google: "Google", |
106 | | - xai: "xAI", |
107 | | - ollama: "Ollama", |
108 | | - openrouter: "OpenRouter", |
109 | | - bedrock: "Amazon Bedrock", |
110 | | - "mux-gateway": "Mux Gateway", |
111 | | -}; |
| 107 | +export const PROVIDER_REGISTRY = Object.fromEntries( |
| 108 | + Object.entries(PROVIDER_DEFINITIONS).map(([key, def]) => [key, def.import]) |
| 109 | +) as { [K in ProviderName]: (typeof PROVIDER_DEFINITIONS)[K]["import"] }; |
112 | 110 |
|
113 | 111 | /** |
114 | 112 | * Type guard to check if a string is a valid provider name |
|
0 commit comments