milwright commited on
Commit
47a380e
Β·
1 Parent(s): 70a50e9

fix: restore missing AI methods for word selection and chat

Browse files

- Add selectSignificantWords method for AI-powered word selection
- Add generateContextualHint method for chat question responses
- Add getEnhancedFallback for graceful degradation
- These methods were missing after deployment simplification

Files changed (1) hide show
  1. src/aiService.js +116 -0
src/aiService.js CHANGED
@@ -19,6 +19,64 @@ class OpenRouterService {
19
  this.apiKey = key;
20
  }
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  async getContextualHint(passage, wordToReplace, context) {
23
  if (!this.apiKey) {
24
  return 'API key required for contextual hints';
@@ -60,6 +118,64 @@ Provide a brief, educational hint that helps understand the word without giving
60
  }
61
  }
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  async getContextualization(title, author, passage) {
64
  if (!this.apiKey) {
65
  return `πŸ“š Practice with classic literature from ${author}'s "${title}"`;
 
19
  this.apiKey = key;
20
  }
21
 
22
+ async generateContextualHint(questionType, word, sentence, bookTitle, wordContext) {
23
+ if (!this.apiKey) {
24
+ return this.getEnhancedFallback(questionType, word, sentence, bookTitle);
25
+ }
26
+
27
+ try {
28
+ const prompts = {
29
+ part_of_speech: `What part of speech is the word "${word}" in this sentence: "${sentence}"? Provide a clear, direct answer.`,
30
+ sentence_role: `What grammatical role does "${word}" play in this sentence: "${sentence}"? Focus on its function.`,
31
+ word_category: `Is "${word}" an abstract or concrete noun? Explain briefly with an example.`,
32
+ synonym: `What's a good synonym for "${word}" that would fit in this sentence: "${sentence}"?`
33
+ };
34
+
35
+ const response = await fetch(this.apiUrl, {
36
+ method: 'POST',
37
+ headers: {
38
+ 'Content-Type': 'application/json',
39
+ 'Authorization': `Bearer ${this.apiKey}`,
40
+ 'HTTP-Referer': window.location.origin,
41
+ 'X-Title': 'Cloze Reader'
42
+ },
43
+ body: JSON.stringify({
44
+ model: this.model,
45
+ messages: [{
46
+ role: 'system',
47
+ content: 'You are a helpful reading tutor. Provide clear, educational answers that help students learn without giving away the answer directly.'
48
+ }, {
49
+ role: 'user',
50
+ content: prompts[questionType] || `Help me understand the word "${word}" in this context: "${sentence}"`
51
+ }],
52
+ max_tokens: 150,
53
+ temperature: 0.7
54
+ })
55
+ });
56
+
57
+ if (!response.ok) {
58
+ throw new Error(`API request failed: ${response.status}`);
59
+ }
60
+
61
+ const data = await response.json();
62
+ return data.choices[0].message.content.trim();
63
+ } catch (error) {
64
+ console.error('Error generating contextual hint:', error);
65
+ return this.getEnhancedFallback(questionType, word, sentence, bookTitle);
66
+ }
67
+ }
68
+
69
+ getEnhancedFallback(questionType, word, sentence, bookTitle) {
70
+ const fallbacks = {
71
+ part_of_speech: `Consider what "${word}" is doing in the sentence. Is it a person, place, thing (noun), an action (verb), or describing something (adjective)?`,
72
+ sentence_role: `Look at how "${word}" connects to other words around it. What is its job in making the sentence complete?`,
73
+ word_category: `Think about whether "${word}" is something you can touch or see (concrete) or an idea/feeling (abstract).`,
74
+ synonym: `What other word could replace "${word}" and keep the same meaning in this sentence?`
75
+ };
76
+
77
+ return fallbacks[questionType] || `Think about what "${word}" means in this classic literature context.`;
78
+ }
79
+
80
  async getContextualHint(passage, wordToReplace, context) {
81
  if (!this.apiKey) {
82
  return 'API key required for contextual hints';
 
118
  }
119
  }
120
 
121
+ async selectSignificantWords(passage, count) {
122
+ if (!this.apiKey) {
123
+ throw new Error('API key required for word selection');
124
+ }
125
+
126
+ try {
127
+ const response = await fetch(this.apiUrl, {
128
+ method: 'POST',
129
+ headers: {
130
+ 'Content-Type': 'application/json',
131
+ 'Authorization': `Bearer ${this.apiKey}`,
132
+ 'HTTP-Referer': window.location.origin,
133
+ 'X-Title': 'Cloze Reader'
134
+ },
135
+ body: JSON.stringify({
136
+ model: this.model,
137
+ messages: [{
138
+ role: 'system',
139
+ content: 'You are an educational assistant that selects meaningful words from passages for cloze reading exercises. Select words that are important for comprehension and appropriate for the difficulty level.'
140
+ }, {
141
+ role: 'user',
142
+ content: `From this passage, select exactly ${count} meaningful words to create blanks for a cloze reading exercise. The words should be distributed throughout the passage and be important for understanding the text. Return ONLY the words as a JSON array, nothing else.
143
+
144
+ Passage: "${passage}"`
145
+ }],
146
+ max_tokens: 100,
147
+ temperature: 0.3
148
+ })
149
+ });
150
+
151
+ if (!response.ok) {
152
+ throw new Error(`API request failed: ${response.status}`);
153
+ }
154
+
155
+ const data = await response.json();
156
+ const content = data.choices[0].message.content.trim();
157
+
158
+ // Try to parse as JSON array
159
+ try {
160
+ const words = JSON.parse(content);
161
+ if (Array.isArray(words)) {
162
+ return words.slice(0, count);
163
+ }
164
+ } catch (e) {
165
+ // If not valid JSON, try to extract words from the response
166
+ const matches = content.match(/"([^"]+)"/g);
167
+ if (matches) {
168
+ return matches.map(m => m.replace(/"/g, '')).slice(0, count);
169
+ }
170
+ }
171
+
172
+ throw new Error('Failed to parse AI response');
173
+ } catch (error) {
174
+ console.error('Error selecting words with AI:', error);
175
+ throw error;
176
+ }
177
+ }
178
+
179
  async getContextualization(title, author, passage) {
180
  if (!this.apiKey) {
181
  return `πŸ“š Practice with classic literature from ${author}'s "${title}"`;