Spaces:
Running
Running
fix: add runtime API key checking and comprehensive debugging
Browse files- Check for API key at runtime for all methods (keys may load after init)
- Add detailed logging for selectSignificantWords and getContextualization
- Log API responses and errors for better debugging
- This should fix the fallback issues with word selection and contextualization
- src/aiService.js +34 -1
src/aiService.js
CHANGED
@@ -12,6 +12,7 @@ class OpenRouterService {
|
|
12 |
if (typeof window !== 'undefined' && window.OPENROUTER_API_KEY) {
|
13 |
return window.OPENROUTER_API_KEY;
|
14 |
}
|
|
|
15 |
return '';
|
16 |
}
|
17 |
|
@@ -20,6 +21,12 @@ class OpenRouterService {
|
|
20 |
}
|
21 |
|
22 |
async generateContextualHint(questionType, word, sentence, bookTitle, wordContext) {
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
if (!this.apiKey) {
|
24 |
return this.getEnhancedFallback(questionType, word, sentence, bookTitle);
|
25 |
}
|
@@ -119,7 +126,18 @@ Provide a brief, educational hint that helps understand the word without giving
|
|
119 |
}
|
120 |
|
121 |
async selectSignificantWords(passage, count) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
if (!this.apiKey) {
|
|
|
123 |
throw new Error('API key required for word selection');
|
124 |
}
|
125 |
|
@@ -177,7 +195,18 @@ Passage: "${passage}"`
|
|
177 |
}
|
178 |
|
179 |
async getContextualization(title, author, passage) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
if (!this.apiKey) {
|
|
|
181 |
return `π Practice with classic literature from ${author}'s "${title}"`;
|
182 |
}
|
183 |
|
@@ -205,11 +234,15 @@ Passage: "${passage}"`
|
|
205 |
});
|
206 |
|
207 |
if (!response.ok) {
|
|
|
|
|
208 |
throw new Error(`API request failed: ${response.status}`);
|
209 |
}
|
210 |
|
211 |
const data = await response.json();
|
212 |
-
|
|
|
|
|
213 |
} catch (error) {
|
214 |
console.error('Error getting contextualization:', error);
|
215 |
return `π Practice with classic literature from ${author}'s "${title}"`;
|
|
|
12 |
if (typeof window !== 'undefined' && window.OPENROUTER_API_KEY) {
|
13 |
return window.OPENROUTER_API_KEY;
|
14 |
}
|
15 |
+
console.warn('No API key found in getApiKey()');
|
16 |
return '';
|
17 |
}
|
18 |
|
|
|
21 |
}
|
22 |
|
23 |
async generateContextualHint(questionType, word, sentence, bookTitle, wordContext) {
|
24 |
+
// Check for API key at runtime
|
25 |
+
const currentKey = this.getApiKey();
|
26 |
+
if (currentKey && !this.apiKey) {
|
27 |
+
this.apiKey = currentKey;
|
28 |
+
}
|
29 |
+
|
30 |
if (!this.apiKey) {
|
31 |
return this.getEnhancedFallback(questionType, word, sentence, bookTitle);
|
32 |
}
|
|
|
126 |
}
|
127 |
|
128 |
async selectSignificantWords(passage, count) {
|
129 |
+
console.log('selectSignificantWords called with count:', count);
|
130 |
+
|
131 |
+
// Check for API key at runtime in case it was loaded after initialization
|
132 |
+
const currentKey = this.getApiKey();
|
133 |
+
if (currentKey && !this.apiKey) {
|
134 |
+
this.apiKey = currentKey;
|
135 |
+
}
|
136 |
+
|
137 |
+
console.log('API key available:', !!this.apiKey);
|
138 |
+
|
139 |
if (!this.apiKey) {
|
140 |
+
console.error('No API key for word selection');
|
141 |
throw new Error('API key required for word selection');
|
142 |
}
|
143 |
|
|
|
195 |
}
|
196 |
|
197 |
async getContextualization(title, author, passage) {
|
198 |
+
console.log('getContextualization called for:', title, 'by', author);
|
199 |
+
|
200 |
+
// Check for API key at runtime
|
201 |
+
const currentKey = this.getApiKey();
|
202 |
+
if (currentKey && !this.apiKey) {
|
203 |
+
this.apiKey = currentKey;
|
204 |
+
}
|
205 |
+
|
206 |
+
console.log('API key available for contextualization:', !!this.apiKey);
|
207 |
+
|
208 |
if (!this.apiKey) {
|
209 |
+
console.log('No API key, returning fallback contextualization');
|
210 |
return `π Practice with classic literature from ${author}'s "${title}"`;
|
211 |
}
|
212 |
|
|
|
234 |
});
|
235 |
|
236 |
if (!response.ok) {
|
237 |
+
const errorText = await response.text();
|
238 |
+
console.error('Contextualization API error:', response.status, errorText);
|
239 |
throw new Error(`API request failed: ${response.status}`);
|
240 |
}
|
241 |
|
242 |
const data = await response.json();
|
243 |
+
const content = data.choices[0].message.content.trim();
|
244 |
+
console.log('Contextualization received:', content);
|
245 |
+
return content;
|
246 |
} catch (error) {
|
247 |
console.error('Error getting contextualization:', error);
|
248 |
return `π Practice with classic literature from ${author}'s "${title}"`;
|