File size: 34,820 Bytes
63a5bc1
 
56287e6
 
 
63a5bc1
fed2199
86f177c
fed2199
86f177c
fed2199
56287e6
 
 
 
fed2199
 
56287e6
 
 
 
 
 
 
 
 
63a5bc1
 
 
56287e6
 
 
 
63a5bc1
 
 
 
 
 
3ca518f
63a5bc1
 
 
 
 
 
 
a918c3e
b97292d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b8632d
7d032f3
 
 
 
 
 
47a380e
2b8632d
47a380e
 
 
56287e6
 
 
 
 
 
 
 
 
 
 
47a380e
 
56287e6
47a380e
fed2199
47a380e
fed2199
 
 
47a380e
fed2199
25876df
fed2199
 
 
 
47a380e
 
 
 
 
 
 
 
9c565fb
fed2199
 
9c565fb
 
 
 
 
 
fed2199
 
 
9c565fb
 
 
fed2199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfcab2f
4f8034e
dfcab2f
 
4f8034e
dfcab2f
 
 
 
 
 
 
47a380e
 
2b8632d
47a380e
 
 
63a5bc1
9e0745a
 
7d032f3
 
 
 
 
 
 
 
 
47a380e
7d032f3
47a380e
 
 
9e0745a
 
 
 
 
 
 
 
 
 
 
 
 
47a380e
b97292d
 
 
 
 
 
 
 
 
 
fed2199
b97292d
fed2199
 
 
b97292d
04bba1d
47a380e
 
b97292d
fed2199
 
 
 
b97292d
 
47a380e
b97292d
 
 
47a380e
b97292d
 
 
 
 
 
47a380e
b97292d
fed2199
 
 
b97292d
fed2199
b97292d
fed2199
 
47a380e
b97292d
fed2199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56287e6
 
 
 
 
b97292d
 
 
56287e6
 
fed2199
 
 
 
 
 
 
56287e6
fed2199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56287e6
 
 
 
 
 
fed2199
 
56287e6
 
 
b97292d
e4e854a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e0745a
4f630fa
 
 
 
e4e854a
9e0745a
e4e854a
4f630fa
 
 
 
e4e854a
04bba1d
 
 
 
 
 
e4e854a
 
 
 
 
e26fd03
9e0745a
 
 
 
 
 
 
 
 
 
 
 
2ecb883
9e0745a
 
b97292d
 
 
 
 
9e0745a
e4e854a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e0745a
4f630fa
 
 
 
e4e854a
9e0745a
e4e854a
4f630fa
 
 
 
e4e854a
 
 
 
 
 
e26fd03
9e0745a
 
 
 
 
 
 
 
 
 
 
 
 
 
b97292d
 
 
 
 
47a380e
 
 
 
 
 
9e0745a
cd16e7b
 
 
 
 
 
 
 
 
 
9e0745a
 
 
 
 
 
 
 
 
 
 
 
 
cd16e7b
1505067
 
 
 
56287e6
 
 
 
 
 
 
 
 
 
 
cd16e7b
 
56287e6
1505067
cd16e7b
fed2199
cd16e7b
fed2199
 
 
cd16e7b
04bba1d
559e227
fed2199
 
9e0745a
fed2199
 
cd16e7b
fed2199
cd16e7b
a918c3e
fed2199
 
cd16e7b
 
 
1505067
 
 
cd16e7b
 
 
 
 
3ca518f
 
 
 
 
 
 
fed2199
 
3ca518f
fed2199
3ca518f
fed2199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ca518f
 
fed2199
 
 
 
 
 
cd16e7b
 
3ca518f
 
 
 
 
 
 
 
 
 
 
 
440ff08
 
 
a918c3e
 
 
 
 
 
 
3ca518f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440ff08
 
 
 
e4e854a
eeb2654
e4e854a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5848fc
4f630fa
 
 
 
e4e854a
d5848fc
e4e854a
4f630fa
 
 
 
e4e854a
 
 
 
 
 
eeb2654
 
 
 
e4e854a
e26fd03
d5848fc
 
 
 
 
 
 
 
 
 
eeb2654
 
2ecb883
3ca518f
cd16e7b
 
3ca518f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd16e7b
 
1505067
 
 
 
 
 
 
 
 
 
 
cd16e7b
 
 
 
 
86f177c
09d70c8
86f177c
09d70c8
 
 
 
 
86f177c
09d70c8
86f177c
09d70c8
 
86f177c
09d70c8
 
 
b97292d
 
 
 
 
 
 
 
 
 
86f177c
b97292d
fed2199
86f177c
fed2199
b97292d
86f177c
b97292d
fed2199
86f177c
fed2199
b97292d
 
09d70c8
b97292d
 
 
 
 
09d70c8
b97292d
 
 
 
 
 
 
 
fed2199
 
b97292d
fed2199
b97292d
fed2199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b97292d
 
fed2199
86f177c
b97292d
 
 
86f177c
b97292d
 
 
 
 
 
 
 
 
09d70c8
 
86f177c
09d70c8
 
 
56287e6
 
 
 
 
 
 
 
 
 
63a5bc1
 
5beb699
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
class OpenRouterService {
  constructor() {
    // Check for local LLM mode
    this.isLocalMode = this.checkLocalMode();
    this.apiUrl = this.isLocalMode ? 'http://localhost:1234/v1/chat/completions' : 'https://openrouter.ai/api/v1/chat/completions';
    this.apiKey = this.getApiKey();
    
    // Single model configuration: Gemma-3-27b for all operations
    this.hintModel = this.isLocalMode ? 'gemma-3-12b' : 'google/gemma-3-27b-it';
    this.primaryModel = this.isLocalMode ? 'gemma-3-12b' : 'google/gemma-3-27b-it';
    this.model = this.primaryModel; // Default model for backward compatibility
    
    console.log('AI Service initialized:', {
      mode: this.isLocalMode ? 'Local LLM' : 'OpenRouter',
      url: this.apiUrl,
      primaryModel: this.primaryModel,
      hintModel: this.hintModel
    });
  }
  
  checkLocalMode() {
    if (typeof window !== 'undefined' && window.location) {
      const urlParams = new URLSearchParams(window.location.search);
      return urlParams.get('local') === 'true';
    }
    return false;
  }

  getApiKey() {
    // Local mode doesn't need API key
    if (this.isLocalMode) {
      return 'local-mode-no-key';
    }
    if (typeof process !== 'undefined' && process.env && process.env.OPENROUTER_API_KEY) {
      return process.env.OPENROUTER_API_KEY;
    }
    if (typeof window !== 'undefined' && window.OPENROUTER_API_KEY) {
      return window.OPENROUTER_API_KEY;
    }
    // console.warn('No API key found in getApiKey()');
    return '';
  }

  setApiKey(key) {
    this.apiKey = key;
  }

  async retryRequest(requestFn, maxRetries = 3, delayMs = 500) {
    for (let attempt = 1; attempt <= maxRetries; attempt++) {
      try {
        return await requestFn();
      } catch (error) {
        console.log(`API request attempt ${attempt}/${maxRetries} failed:`, error.message);
        if (attempt === maxRetries) {
          throw error; // Final attempt failed, throw the error
        }
        // Wait before retrying, with exponential backoff
        const delay = delayMs * Math.pow(2, attempt - 1);
        console.log(`Retrying in ${delay}ms...`);
        await new Promise(resolve => setTimeout(resolve, delay));
      }
    }
  }

  async generateContextualHint(prompt) {
    // Check for API key at runtime
    const currentKey = this.getApiKey();
    if (currentKey && !this.apiKey) {
      this.apiKey = currentKey;
    }
    
    if (!this.apiKey) {
      return 'API key required for hints';
    }

    try {
      const headers = {
        'Content-Type': 'application/json'
      };
      
      // Only add auth headers for OpenRouter
      if (!this.isLocalMode) {
        headers['Authorization'] = `Bearer ${this.apiKey}`;
        headers['HTTP-Referer'] = window.location.origin;
        headers['X-Title'] = 'Cloze Reader';
      }
      
      const response = await fetch(this.apiUrl, {
        method: 'POST',
        headers,
        body: JSON.stringify({
          model: this.hintModel,  // Use Gemma-3-27b for hints
          messages: [{
            role: 'system',
            content: 'You are a helpful assistant that provides hints for word puzzles. Never reveal the answer word directly.'
          }, {
            role: 'user',
            content: prompt
          }],
          max_tokens: 150,
          temperature: 0.7,
          // Try to disable reasoning mode for hints
          response_format: { type: "text" }
        })
      });

      if (!response.ok) {
        throw new Error(`API request failed: ${response.status}`);
      }

      const data = await response.json();
      
      console.log('Hint API response:', JSON.stringify(data, null, 2));
      
      // Check if data and choices exist before accessing
      if (!data || !data.choices || data.choices.length === 0) {
        console.error('Invalid API response structure:', data);
        return 'Unable to generate hint at this time';
      }
      
      // Check if message exists
      if (!data.choices[0].message) {
        console.error('No message in API response');
        return 'Unable to generate hint at this time';
      }
      
      // OSS-20B model returns content in 'reasoning' field when using reasoning mode
      let content = data.choices[0].message.content || '';
      
      // If content is empty, check for reasoning field
      if (!content && data.choices[0].message.reasoning) {
        content = data.choices[0].message.reasoning;
      }
      
      // Still no content? Check reasoning_details
      if (!content && data.choices[0].message.reasoning_details?.length > 0) {
        content = data.choices[0].message.reasoning_details[0].text;
      }
      
      if (!content) {
        console.error('No content found in hint response');
        // Provide a generic hint based on the prompt type
        if (prompt.toLowerCase().includes('synonym')) {
          return 'Think of a word that means something similar';
        } else if (prompt.toLowerCase().includes('definition')) {
          return 'Consider what this word means in context';
        } else if (prompt.toLowerCase().includes('category')) {
          return 'Think about what type or category this word belongs to';
        } else {
          return 'Consider the context around the blank';
        }
      }
      
      content = content.trim();
      
      // For OSS-20B, extract hint from reasoning text if needed
      if (content.includes('The user') || content.includes('We need to')) {
        // This looks like reasoning text, try to extract the actual hint
        // Look for text about synonyms, definitions, or clues
        const hintPatterns = [
          /synonym[s]?.*?(?:is|are|include[s]?|would be)\s+([^.]+)/i,
          /means?\s+([^.]+)/i,
          /refers? to\s+([^.]+)/i,
          /describes?\s+([^.]+)/i,
        ];
        
        for (const pattern of hintPatterns) {
          const match = content.match(pattern);
          if (match) {
            content = match[1];
            break;
          }
        }
        
        // If still has reasoning markers, just return a fallback
        if (content.includes('The user') || content.includes('We need to')) {
          return 'Think about words that mean something similar';
        }
      }
      
      // Clean up AI response artifacts  
      content = content
        .replace(/^\s*["']|["']\s*$/g, '')  // Remove leading/trailing quotes
        .replace(/^\s*[:;]+\s*/, '')        // Remove leading colons and semicolons
        .replace(/\*+/g, '')                // Remove asterisks (markdown bold/italic)
        .replace(/_+/g, '')                 // Remove underscores (markdown)
        .replace(/#+\s*/g, '')              // Remove hash symbols (markdown headers)
        .replace(/\s+/g, ' ')               // Normalize whitespace
        .trim();
      
      return content;
    } catch (error) {
      console.error('Error generating contextual hint:', error);
      return 'Unable to generate hint at this time';
    }
  }


  async selectSignificantWords(passage, count, level = 1) {
    console.log('selectSignificantWords called with count:', count, 'level:', level);
    
    // Check for API key at runtime in case it was loaded after initialization
    const currentKey = this.getApiKey();
    if (currentKey && !this.apiKey) {
      this.apiKey = currentKey;
    }
    
    console.log('API key available:', !!this.apiKey);
    
    if (!this.apiKey) {
      console.error('No API key for word selection');
      throw new Error('API key required for word selection');
    }

    // Define level-based constraints
    let wordLengthConstraint, difficultyGuidance;
    if (level <= 2) {
      wordLengthConstraint = "EXACTLY 4-7 letters (no words longer than 7 letters)";
      difficultyGuidance = "Select EASY vocabulary words - common, everyday words that most readers know. NEVER select words longer than 7 letters.";
    } else if (level <= 4) {
      wordLengthConstraint = "EXACTLY 4-10 letters (no words longer than 10 letters)";
      difficultyGuidance = "Select MEDIUM difficulty words - mix of common and moderately challenging vocabulary. NEVER select words longer than 10 letters.";
    } else {
      wordLengthConstraint = "5-14 letters";
      difficultyGuidance = "Select CHALLENGING words - sophisticated vocabulary that requires strong reading skills";
    }

    try {
      return await this.retryRequest(async () => {
        const response = await fetch(this.apiUrl, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${this.apiKey}`,
            'HTTP-Referer': window.location.origin,
            'X-Title': 'Cloze Reader'
          },
          body: JSON.stringify({
            model: this.primaryModel,  // Use Gemma-3-12b for word selection
            messages: [{
              role: 'system',
              content: 'Select words for a cloze exercise. Return ONLY a JSON array of words, nothing else.'
            }, {
              role: 'user',
              content: `Select ${count} ${level <= 2 ? 'easy' : level <= 4 ? 'medium' : 'challenging'} words (${wordLengthConstraint}) from this passage. Choose meaningful nouns, verbs, or adjectives. Avoid capitalized words and proper nouns. NEVER select concatenated words like "fromthe", "tothe", "andthe", "hewas", "shewas" - these should be two separate words.

Passage: "${passage}"`
            }],
            max_tokens: 200,
            temperature: 0.5,
            // Try to disable reasoning mode for word selection
            response_format: { type: "text" }
          })
        });

        if (!response.ok) {
          throw new Error(`API request failed: ${response.status}`);
        }

        const data = await response.json();
        
        // Check for OpenRouter error response
        if (data.error) {
          console.error('OpenRouter API error for word selection:', data.error);
          throw new Error(`OpenRouter API error: ${data.error.message || JSON.stringify(data.error)}`);
        }
        
        // Log the full response to debug structure
        console.log('Full API response:', JSON.stringify(data, null, 2));
        
        // Check if response has expected structure
        if (!data.choices || !data.choices[0] || !data.choices[0].message) {
          console.error('Invalid word selection API response structure:', data);
          console.error('Choices[0]:', data.choices?.[0]);
          throw new Error('API response missing expected structure');
        }
        
        // OSS-20B model returns content in 'reasoning' field when using reasoning mode
        let content = data.choices[0].message.content || '';
        
        // If content is empty, check for reasoning field
        if (!content && data.choices[0].message.reasoning) {
          content = data.choices[0].message.reasoning;
        }
        
        // Still no content? Check reasoning_details
        if (!content && data.choices[0].message.reasoning_details?.length > 0) {
          content = data.choices[0].message.reasoning_details[0].text;
        }
        
        if (!content) {
          console.error('No content found in API response');
          throw new Error('API response missing content');
        }
        
        content = content.trim();
        
        // Clean up local LLM artifacts
        if (this.isLocalMode) {
          content = this.cleanLocalLLMResponse(content);
        }
        
        // Try to parse as JSON array
        try {
          let words;
          
          // Try to parse JSON first
          try {
            // Check if content contains JSON array anywhere in it
            const jsonMatch = content.match(/\[[\s\S]*?\]/);
            if (jsonMatch) {
              words = JSON.parse(jsonMatch[0]);
            } else {
              words = JSON.parse(content);
            }
          } catch {
            // If not JSON, check if this is reasoning text from OSS-20B
            if (content.includes('pick') || content.includes('Let\'s')) {
              // Extract words from reasoning text
              // Look for quoted words or words after "pick"
              const quotedWords = content.match(/"([^"]+)"/g);
              if (quotedWords) {
                words = quotedWords.map(w => w.replace(/"/g, ''));
              } else {
                // Look for pattern like "Let's pick 'word'" or "pick word"
                const pickMatch = content.match(/pick\s+['"]?(\w+)['"]?/i);
                if (pickMatch) {
                  words = [pickMatch[1]];
                } else {
                  // For local LLM, try comma-separated
                  if (this.isLocalMode && content.includes(',')) {
                    words = content.split(',').map(w => w.trim());
                  } else {
                    // Single word
                    words = [content.trim()];
                  }
                }
              }
            } else if (this.isLocalMode) {
              // For local LLM, try comma-separated
              if (content.includes(',')) {
                words = content.split(',').map(w => w.trim());
              } else {
                // Single word
                words = [content.trim()];
              }
            } else {
              throw new Error('Could not parse words from response');
            }
          }
          
          if (Array.isArray(words)) {
            // Create passage word array with position and capitalization info (matches clozeGameEngine logic)
            const passageWords = passage.split(/\s+/);
            const passageWordMap = new Map();

            passageWords.forEach((word, idx) => {
              const cleanOriginal = word.replace(/[^\w]/g, '');
              const cleanLower = cleanOriginal.toLowerCase();
              const isCapitalized = cleanOriginal.length > 0 && cleanOriginal[0] === cleanOriginal[0].toUpperCase();

              // Only track non-capitalized words after position 10 (matches game engine constraints)
              if (!isCapitalized && idx >= 10) {
                if (!passageWordMap.has(cleanLower)) {
                  passageWordMap.set(cleanLower, []);
                }
                passageWordMap.get(cleanLower).push(idx);
              }
            });

            // Validate word lengths based on level and passage presence
            const validWords = words.filter(word => {
              // First check if the word contains at least one letter
              if (!/[a-zA-Z]/.test(word)) {
                return false;
              }

              const cleanWord = word.replace(/[^a-zA-Z]/g, '');

              // If cleanWord is empty after removing non-letters, reject
              if (cleanWord.length === 0) {
                return false;
              }

              // Reject obvious concatenated words like "fromthe", "tothe", etc.
              if (/^(from|to|and)(the|a)$/i.test(cleanWord)) {
                console.log(`🚫 Rejected concatenated word: "${word}"`);
                return false;
              }

              // Check if word exists as non-capitalized word after position 10 (matches game engine)
              if (!passageWordMap.has(cleanWord.toLowerCase())) {
                return false;
              }

              // Check length constraints
              if (level <= 2) {
                return cleanWord.length >= 4 && cleanWord.length <= 7;
              } else if (level <= 4) {
                return cleanWord.length >= 4 && cleanWord.length <= 10;
              } else {
                return cleanWord.length >= 5 && cleanWord.length <= 14;
              }
            });
            
            if (validWords.length > 0) {
              return validWords.slice(0, count);
            } else {
              console.warn(`No words met requirements for level ${level}`);
              throw new Error(`No valid words for level ${level}`);
            }
          }
        } catch (e) {
          // If not valid JSON, try to extract words from the response
          const matches = content.match(/"([^"]+)"/g);
          if (matches) {
            const words = matches.map(m => m.replace(/"/g, ''));

            // Create passage word array with position and capitalization info (matches clozeGameEngine logic)
            const passageWords = passage.split(/\s+/);
            const passageWordMap = new Map();

            passageWords.forEach((word, idx) => {
              const cleanOriginal = word.replace(/[^\w]/g, '');
              const cleanLower = cleanOriginal.toLowerCase();
              const isCapitalized = cleanOriginal.length > 0 && cleanOriginal[0] === cleanOriginal[0].toUpperCase();

              // Only track non-capitalized words after position 10 (matches game engine constraints)
              if (!isCapitalized && idx >= 10) {
                if (!passageWordMap.has(cleanLower)) {
                  passageWordMap.set(cleanLower, []);
                }
                passageWordMap.get(cleanLower).push(idx);
              }
            });

            // Validate word lengths and passage presence
            const validWords = words.filter(word => {
              // First check if the word contains at least one letter
              if (!/[a-zA-Z]/.test(word)) {
                return false;
              }

              const cleanWord = word.replace(/[^a-zA-Z]/g, '');

              // If cleanWord is empty after removing non-letters, reject
              if (cleanWord.length === 0) {
                return false;
              }

              // Check if word exists as non-capitalized word after position 10 (matches game engine)
              if (!passageWordMap.has(cleanWord.toLowerCase())) {
                return false;
              }

              // Check length constraints
              if (level <= 2) {
                return cleanWord.length >= 4 && cleanWord.length <= 7;
              } else if (level <= 4) {
                return cleanWord.length >= 4 && cleanWord.length <= 10;
              } else {
                return cleanWord.length >= 5 && cleanWord.length <= 14;
              }
            });
            
            if (validWords.length > 0) {
              return validWords.slice(0, count);
            } else {
              throw new Error(`No valid words for level ${level}`);
            }
          }
        }
        
        throw new Error('Failed to parse AI response');
      });
    } catch (error) {
      console.error('Error selecting words with AI:', error);
      throw error;
    }
  }

  async processBothPassages(passage1, book1, passage2, book2, blanksPerPassage, level = 1) {
    // Process both passages in a single API call to avoid rate limits
    const currentKey = this.getApiKey();
    if (currentKey && !this.apiKey) {
      this.apiKey = currentKey;
    }
    
    if (!this.apiKey) {
      throw new Error('API key required for passage processing');
    }

    // Define level-based constraints
    let wordLengthConstraint, difficultyGuidance;
    if (level <= 2) {
      wordLengthConstraint = "EXACTLY 4-7 letters (no words longer than 7 letters)";
      difficultyGuidance = "Select EASY vocabulary words - common, everyday words that most readers know. NEVER select words longer than 7 letters.";
    } else if (level <= 4) {
      wordLengthConstraint = "EXACTLY 4-10 letters (no words longer than 10 letters)";
      difficultyGuidance = "Select MEDIUM difficulty words - mix of common and moderately challenging vocabulary. NEVER select words longer than 10 letters.";
    } else {
      wordLengthConstraint = "5-14 letters";
      difficultyGuidance = "Select CHALLENGING words - sophisticated vocabulary that requires strong reading skills";
    }

    try {
      // Add timeout controller to prevent aborted operations
      const controller = new AbortController();
      const timeoutId = setTimeout(() => controller.abort(), 15000); // 15 second timeout
      
      const headers = {
        'Content-Type': 'application/json'
      };
      
      // Only add auth headers for OpenRouter
      if (!this.isLocalMode) {
        headers['Authorization'] = `Bearer ${this.apiKey}`;
        headers['HTTP-Referer'] = window.location.origin;
        headers['X-Title'] = 'Cloze Reader';
      }
      
      const response = await fetch(this.apiUrl, {
        method: 'POST',
        headers,
        signal: controller.signal,
        body: JSON.stringify({
          model: this.primaryModel,  // Use Gemma-3-12b for batch processing
          messages: [{
            role: 'system',
            content: 'Process passages for cloze exercises. Return ONLY a JSON object.'
          }, {
            role: 'user',
            content: `Select ${blanksPerPassage} ${level <= 2 ? 'easy' : level <= 4 ? 'medium' : 'challenging'} words (${wordLengthConstraint}) from each passage. NEVER select concatenated words like "fromthe", "tothe", "andthe", "hewas", "shewas" - these should be two separate words.

Passage 1 ("${book1.title}" by ${book1.author}):
${passage1}

Passage 2 ("${book2.title}" by ${book2.author}):
${passage2}

Return JSON: {"passage1": {"words": [${blanksPerPassage} words], "context": "one sentence about book"}, "passage2": {"words": [${blanksPerPassage} words], "context": "one sentence about book"}}`
          }],
          max_tokens: 800,
          temperature: 0.5,
          response_format: { type: "text" }
        })
      });

      // Clear timeout on successful response
      clearTimeout(timeoutId);

      if (!response.ok) {
        throw new Error(`API request failed: ${response.status}`);
      }

      const data = await response.json();
      
      // Check for error response
      if (data.error) {
        console.error('OpenRouter API error for batch processing:', data.error);
        throw new Error(`OpenRouter API error: ${data.error.message || JSON.stringify(data.error)}`);
      }
      
      console.log('Batch API response:', JSON.stringify(data, null, 2));
      
      // Check if response has expected structure
      if (!data.choices || !data.choices[0] || !data.choices[0].message) {
        console.error('Invalid batch API response structure:', data);
        console.error('Choices[0]:', data.choices?.[0]);
        throw new Error('API response missing expected structure');
      }
      
      // OSS-20B model returns content in 'reasoning' field when using reasoning mode
      let content = data.choices[0].message.content || '';
      
      // If content is empty, check for reasoning field
      if (!content && data.choices[0].message.reasoning) {
        content = data.choices[0].message.reasoning;
      }
      
      // Still no content? Check reasoning_details
      if (!content && data.choices[0].message.reasoning_details?.length > 0) {
        content = data.choices[0].message.reasoning_details[0].text;
      }
      
      if (!content) {
        console.error('No content found in batch API response');
        throw new Error('API response missing content');
      }
      
      content = content.trim();
      
      try {
        // Try to extract JSON from the response
        // Sometimes the model returns JSON wrapped in markdown code blocks
        const jsonMatch = content.match(/```json\s*([\s\S]*?)\s*```/) || content.match(/```\s*([\s\S]*?)\s*```/);
        let jsonString = jsonMatch ? jsonMatch[1] : content;
        
        // Clean up the JSON string
        jsonString = jsonString
          .replace(/^\s*```json\s*/, '')
          .replace(/\s*```\s*$/, '')
          .trim();
        
        // Try to fix common JSON issues
        // Fix trailing commas in arrays
        jsonString = jsonString.replace(/,(\s*])/g, '$1');
        
        // Check for truncated strings (unterminated quotes)
        const quoteCount = (jsonString.match(/"/g) || []).length;
        if (quoteCount % 2 !== 0) {
          // Add missing closing quote
          jsonString += '"';
        }
        
        // Check if JSON is truncated (missing closing braces)
        const openBraces = (jsonString.match(/{/g) || []).length;
        const closeBraces = (jsonString.match(/}/g) || []).length;
        
        if (openBraces > closeBraces) {
          // Add missing closing braces
          jsonString += '}'.repeat(openBraces - closeBraces);
        }
        
        // Remove any trailing garbage after the last closing brace
        const lastBrace = jsonString.lastIndexOf('}');
        if (lastBrace !== -1 && lastBrace < jsonString.length - 1) {
          jsonString = jsonString.substring(0, lastBrace + 1);
        }
        
        const parsed = JSON.parse(jsonString);
        
        // Validate the structure
        if (!parsed.passage1 || !parsed.passage2) {
          console.error('Parsed response missing expected structure:', parsed);
          throw new Error('Response missing passage1 or passage2');
        }
        
        // Ensure words arrays exist and are arrays
        if (!Array.isArray(parsed.passage1.words)) {
          parsed.passage1.words = [];
        }
        if (!Array.isArray(parsed.passage2.words)) {
          parsed.passage2.words = [];
        }
        
        // Filter out empty strings from words arrays (caused by trailing commas)
        parsed.passage1.words = parsed.passage1.words.filter(word => word && word.trim() !== '');
        parsed.passage2.words = parsed.passage2.words.filter(word => word && word.trim() !== '');
        
        // Validate word lengths based on level and passage presence
        const validateWords = (words, passageText) => {
          // Create passage word array with position and capitalization info (matches clozeGameEngine logic)
          const passageWords = passageText.split(/\s+/);
          const passageWordMap = new Map();

          passageWords.forEach((word, idx) => {
            const cleanOriginal = word.replace(/[^\w]/g, '');
            const cleanLower = cleanOriginal.toLowerCase();
            const isCapitalized = cleanOriginal.length > 0 && cleanOriginal[0] === cleanOriginal[0].toUpperCase();

            // Only track non-capitalized words after position 10 (matches game engine constraints)
            if (!isCapitalized && idx >= 10) {
              if (!passageWordMap.has(cleanLower)) {
                passageWordMap.set(cleanLower, []);
              }
              passageWordMap.get(cleanLower).push(idx);
            }
          });

          return words.filter(word => {
            // First check if the word contains at least one letter
            if (!/[a-zA-Z]/.test(word)) {
              return false;
            }

            const cleanWord = word.replace(/[^a-zA-Z]/g, '');

            // If cleanWord is empty after removing non-letters, reject
            if (cleanWord.length === 0) {
              return false;
            }

            // Check if word exists as non-capitalized word after position 10 (matches game engine)
            if (!passageWordMap.has(cleanWord.toLowerCase())) {
              return false;
            }

            // Check if word appears in all caps in the passage (like "VOLUME")
            if (passageText.includes(word.toUpperCase()) && word === word.toUpperCase()) {
              return false;
            }

            // Check length constraints
            if (level <= 2) {
              return cleanWord.length >= 4 && cleanWord.length <= 7;
            } else if (level <= 4) {
              return cleanWord.length >= 4 && cleanWord.length <= 10;
            } else {
              return cleanWord.length >= 5 && cleanWord.length <= 14;
            }
          });
        };
        
        parsed.passage1.words = validateWords(parsed.passage1.words, passage1);
        parsed.passage2.words = validateWords(parsed.passage2.words, passage2);

        return parsed;
      } catch (e) {
        console.error('Failed to parse batch response:', e);
        console.error('Raw content:', content);
        
        // Try to extract any usable data from the partial response
        try {
          // Extract passage contexts using regex
          const context1Match = content.match(/"context":\s*"([^"]+)"/);
          const context2Match = content.match(/"passage2"[\s\S]*?"context":\s*"([^"]+)"/);
          
          // Extract words arrays using regex
          const words1Match = content.match(/"words":\s*\[([^\]]+)\]/);
          const words2Match = content.match(/"passage2"[\s\S]*?"words":\s*\[([^\]]+)\]/);
          
          const extractWords = (match) => {
            if (!match) return [];
            try {
              return JSON.parse(`[${match[1]}]`);
            } catch {
              return match[1].split(',').map(w => w.trim().replace(/['"]/g, ''));
            }
          };
          
          return {
            passage1: { 
              words: extractWords(words1Match), 
              context: context1Match ? context1Match[1] : `From "${book1.title}" by ${book1.author}` 
            },
            passage2: { 
              words: extractWords(words2Match), 
              context: context2Match ? context2Match[1] : `From "${book2.title}" by ${book2.author}` 
            }
          };
        } catch (extractError) {
          console.error('Failed to extract partial data:', extractError);
          throw new Error('Invalid API response format');
        }
      }
    } catch (error) {
      // Clear timeout in error case too
      if (typeof timeoutId !== 'undefined') {
        clearTimeout(timeoutId);
      }
      
      // Handle specific abort error
      if (error.name === 'AbortError') {
        console.error('Batch processing timed out after 15 seconds');
        throw new Error('Request timed out - falling back to sequential processing');
      }
      
      console.error('Error processing passages:', error);
      throw error;
    }
  }

  async generateContextualization(title, author, passage) {
    console.log('generateContextualization called for:', title, 'by', author);

    // Check for API key at runtime
    const currentKey = this.getApiKey();
    if (currentKey && !this.apiKey) {
      this.apiKey = currentKey;
    }

    console.log('API key available for contextualization:', !!this.apiKey);

    if (!this.apiKey) {
      console.log('No API key, returning fallback contextualization');
      return `A passage from ${author}'s "${title}"`;
    }

    try {
      return await this.retryRequest(async () => {
        const response = await fetch(this.apiUrl, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${this.apiKey}`,
            'HTTP-Referer': window.location.origin,
            'X-Title': 'Cloze Reader'
          },
          body: JSON.stringify({
            model: this.primaryModel,  // Use Gemma-3-27b for contextualization
            messages: [{
              role: 'system',
              content: 'Provide a single contextual insight about the passage: historical context, literary technique, thematic observation, or relevant fact. Be specific and direct. Maximum 25 words. Do not use dashes or em-dashes. Output ONLY the insight itself with no preamble, acknowledgments, or meta-commentary.'
            }, {
              role: 'user',
              content: `From "${title}" by ${author}:\n\n${passage}`
            }],
            max_tokens: 150,
            temperature: 0.7,
            response_format: { type: "text" }
          })
        });

        if (!response.ok) {
          const errorText = await response.text();
          console.error('Contextualization API error:', response.status, errorText);
          throw new Error(`API request failed: ${response.status}`);
        }

        const data = await response.json();
        
        // Check for OpenRouter error response
        if (data.error) {
          console.error('OpenRouter API error for contextualization:', data.error);
          throw new Error(`OpenRouter API error: ${data.error.message || JSON.stringify(data.error)}`);
        }
        
        console.log('Context API response:', JSON.stringify(data, null, 2));
        
        // Check if response has expected structure
        if (!data.choices || !data.choices[0] || !data.choices[0].message) {
          console.error('Invalid contextualization API response structure:', data);
          console.error('Choices[0]:', data.choices?.[0]);
          throw new Error('API response missing expected structure');
        }
        
        // OSS-20B model returns content in 'reasoning' field when using reasoning mode
        let content = data.choices[0].message.content || '';
        
        // If content is empty, check for reasoning field
        if (!content && data.choices[0].message.reasoning) {
          content = data.choices[0].message.reasoning;
        }
        
        // Still no content? Check reasoning_details
        if (!content && data.choices[0].message.reasoning_details?.length > 0) {
          content = data.choices[0].message.reasoning_details[0].text;
        }
        
        if (!content) {
          console.error('No content found in context API response');
          throw new Error('API response missing content');
        }
        
        content = content.trim();

        // Clean up AI response artifacts
        content = content
          .replace(/^\s*["']|["']\s*$/g, '')  // Remove leading/trailing quotes
          .replace(/^\s*[:;.!?]+\s*/, '')     // Remove leading punctuation
          .replace(/\*+/g, '')                // Remove asterisks (markdown bold/italic)
          .replace(/_+/g, '')                 // Remove underscores (markdown)
          .replace(/#+\s*/g, '')              // Remove hash symbols (markdown headers)
          .replace(/\s+/g, ' ')               // Normalize whitespace
          .trim();
        
        console.log('Contextualization received:', content);
        return content;
      });
    } catch (error) {
      console.error('Error getting contextualization:', error);
      return `A passage from ${author}'s "${title}"`;
    }
  }

  cleanLocalLLMResponse(content) {
    // Remove common artifacts from local LLM responses
    return content
      .replace(/\["?/g, '')       // Remove opening bracket and quote
      .replace(/"?\]/g, '')       // Remove closing quote and bracket  
      .replace(/^[>"|']+/g, '')   // Remove leading > or quotes
      .replace(/[>"|']+$/g, '')   // Remove trailing > or quotes
      .replace(/\\n/g, ' ')       // Replace escaped newlines
      .trim();
  }
}

export { OpenRouterService as AIService };