milwright commited on
Commit
82a4372
·
1 Parent(s): 106bc8b

fix: make AI API calls sequential with delay to prevent rate limiting

Browse files

- Changed from parallel Promise.all to sequential calls
- Added 1 second delay between word selection and contextualization
- This prevents immediate rate limiting when both calls hit OpenRouter simultaneously
- Applied to both initial passage load and second passage load

Files changed (1) hide show
  1. src/clozeGameEngine.js +14 -10
src/clozeGameEngine.js CHANGED
@@ -55,11 +55,13 @@ class ClozeGame {
55
  this.currentBook = book1;
56
  this.originalText = this.passages[0];
57
 
58
- // Run AI calls in parallel for faster loading
59
- const [clozeResult, contextualizationResult] = await Promise.all([
60
- this.createClozeText(),
61
- this.generateContextualization()
62
- ]);
 
 
63
 
64
  return {
65
  title: this.currentBook.title,
@@ -480,11 +482,13 @@ class ClozeGame {
480
  // Clear last results
481
  this.lastResults = null;
482
 
483
- // Generate new cloze text and contextualization for second passage
484
- const [clozeResult, contextualizationResult] = await Promise.all([
485
- this.createClozeText(),
486
- this.generateContextualization()
487
- ]);
 
 
488
 
489
  return {
490
  title: this.currentBook.title,
 
55
  this.currentBook = book1;
56
  this.originalText = this.passages[0];
57
 
58
+ // Run AI calls sequentially to avoid rate limiting
59
+ await this.createClozeText();
60
+
61
+ // Add small delay between API calls to avoid rate limiting
62
+ await new Promise(resolve => setTimeout(resolve, 1000));
63
+
64
+ await this.generateContextualization();
65
 
66
  return {
67
  title: this.currentBook.title,
 
482
  // Clear last results
483
  this.lastResults = null;
484
 
485
+ // Generate new cloze text and contextualization for second passage sequentially
486
+ await this.createClozeText();
487
+
488
+ // Add small delay between API calls to avoid rate limiting
489
+ await new Promise(resolve => setTimeout(resolve, 1000));
490
+
491
+ await this.generateContextualization();
492
 
493
  return {
494
  title: this.currentBook.title,