-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
70 lines (61 loc) · 2.14 KB
/
config.js
File metadata and controls
70 lines (61 loc) · 2.14 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
// Configuration for DSA Hints Coach
class Config {
constructor() {
this.apiKey = null;
// Use the correct Gemini model - gemini-1.5-flash is the current stable model
this.apiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent';
this.init();
}
async init() {
await this.loadApiKey();
}
async loadApiKey() {
try {
// First try to load from Chrome storage (for user-provided keys)
if (chrome.storage && chrome.storage.sync) {
const result = await chrome.storage.sync.get(['geminiApiKey']);
if (result.geminiApiKey) {
this.apiKey = result.geminiApiKey;
return;
}
}
// Fallback: try to load from environment variable (for development)
// Note: In Chrome extensions, we can't directly access .env files
// This is mainly for development purposes
if (typeof process !== 'undefined' && process.env && process.env.GEMINI_API_KEY) {
this.apiKey = process.env.GEMINI_API_KEY;
return;
}
// If no API key found, return null
this.apiKey = null;
} catch (error) {
console.error('Error loading API key:', error);
this.apiKey = null;
}
}
async saveApiKey(apiKey) {
try {
if (chrome.storage && chrome.storage.sync) {
await chrome.storage.sync.set({ geminiApiKey: apiKey });
}
this.apiKey = apiKey;
return true;
} catch (error) {
console.error('Error saving API key:', error);
return false;
}
}
getApiKey() {
return this.apiKey;
}
getApiUrl() {
return this.apiUrl;
}
hasValidApiKey() {
return this.apiKey && this.apiKey.trim().length > 0;
}
}
// Export for use in other files
if (typeof module !== 'undefined' && module.exports) {
module.exports = Config;
}