-
-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathllm.js
More file actions
101 lines (94 loc) · 3.33 KB
/
llm.js
File metadata and controls
101 lines (94 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// LLM Provider Configuration
// Centralized configuration for all AI translation providers and models
const llmProviders = {
anthropic: {
name: 'Anthropic (Claude)',
keyPrefix: 'sk-ant-',
storageKey: 'claudeApiKey',
modelStorageKey: 'anthropicModel',
models: [
{ id: 'claude-haiku-4-5-20251001', name: 'Claude Haiku 4.5 ($)' },
{ id: 'claude-sonnet-4-5-20250929', name: 'Claude Sonnet 4.5 ($$)' },
{ id: 'claude-opus-4-5-20251101', name: 'Claude Opus 4.5 ($$$)' }
],
defaultModel: 'claude-sonnet-4-5-20250929'
},
openai: {
name: 'OpenAI (GPT)',
keyPrefix: 'sk-',
storageKey: 'openaiApiKey',
modelStorageKey: 'openaiModel',
models: [
{ id: 'gpt-5.1-2025-11-13', name: 'GPT-5.1 ($$$)' },
{ id: 'gpt-5-mini-2025-08-07', name: 'GPT-5 Mini ($$)' },
{ id: 'gpt-5-nano-2025-08-07', name: 'GPT-5 Nano ($)' }
],
defaultModel: 'gpt-5-mini-2025-08-07'
},
google: {
name: 'Google (Gemini)',
keyPrefix: 'AIza',
storageKey: 'googleApiKey',
modelStorageKey: 'googleModel',
models: [
{ id: 'gemini-3-flash-preview', name: 'Gemini 3 Flash (Preview) ($$)' },
{ id: 'gemini-3-pro-preview', name: 'Gemini 3 Pro (Preview) ($$$)' },
{ id: 'gemini-2.5-flash-lite', name: 'Gemini 2.5 Flash-Lite ($)' },
{ id: 'gemini-2.5-flash', name: 'Gemini 2.5 Flash ($$)' },
{ id: 'gemini-2.5-pro', name: 'Gemini 2.5 Pro ($$$)' }
],
defaultModel: 'gemini-2.5-flash'
}
};
/**
* Get the selected model for a provider
* @param {string} provider - Provider key (anthropic, openai, google)
* @returns {string} - Model ID
*/
function getSelectedModel(provider) {
const config = llmProviders[provider];
if (!config) return null;
return localStorage.getItem(config.modelStorageKey) || config.defaultModel;
}
/**
* Get the selected provider
* @returns {string} - Provider key
*/
function getSelectedProvider() {
return localStorage.getItem('aiProvider') || 'anthropic';
}
/**
* Get API key for a provider
* @param {string} provider - Provider key
* @returns {string|null} - API key or null
*/
function getApiKey(provider) {
const config = llmProviders[provider];
if (!config) return null;
return localStorage.getItem(config.storageKey);
}
/**
* Validate API key format for a provider
* @param {string} provider - Provider key
* @param {string} key - API key to validate
* @returns {boolean} - Whether key format is valid
*/
function validateApiKeyFormat(provider, key) {
const config = llmProviders[provider];
if (!config) return false;
return key.startsWith(config.keyPrefix);
}
/**
* Generate HTML options for model select dropdown
* @param {string} provider - Provider key
* @param {string} selectedModel - Currently selected model ID (optional)
* @returns {string} - HTML string of option elements
*/
function generateModelOptions(provider, selectedModel = null) {
const config = llmProviders[provider];
if (!config) return '';
const selected = selectedModel || getSelectedModel(provider);
return config.models.map(model =>
`<option value="${model.id}"${model.id === selected ? ' selected' : ''}>${model.name}</option>`
).join('\n');
}