File size: 1,233 Bytes
55a034a
 
 
 
 
 
 
 
 
8725cc4
 
 
55a034a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8725cc4
 
 
55a034a
 
 
 
 
 
 
 
 
 
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
import { supabase } from "@/integrations/supabase/client";

export const generateAIResponse = async (currentWord: string, currentSentence: string[]): Promise<string> => {
  const { data, error } = await supabase.functions.invoke('generate-word', {
    body: { currentWord, currentSentence }
  });

  if (error) {
    console.error('Error generating AI response:', error);
    if (error.message?.includes('rate limit')) {
      throw new Error('The AI service is currently busy. Please try again in a few moments.');
    }
    throw error;
  }

  if (!data.word) {
    throw new Error('No word generated');
  }

  console.log('AI generated word:', data.word);
  return data.word;
};

export const guessWord = async (sentence: string): Promise<string> => {
  const { data, error } = await supabase.functions.invoke('guess-word', {
    body: { sentence }
  });

  if (error) {
    console.error('Error getting AI guess:', error);
    if (error.message?.includes('rate limit')) {
      throw new Error('The AI service is currently busy. Please try again in a few moments.');
    }
    throw error;
  }

  if (!data.guess) {
    throw new Error('No guess generated');
  }

  console.log('AI guessed:', data.guess);
  return data.guess;
};