File size: 1,956 Bytes
1a45d5d
55a034a
 
65676ec
ac04e03
1a45d5d
55a034a
1a45d5d
 
ac04e03
65676ec
 
0ce34cb
55a034a
 
 
 
8725cc4
 
 
55a034a
 
 
0ce34cb
 
55a034a
 
 
1a45d5d
55a034a
 
 
65676ec
5835ecd
1a45d5d
5835ecd
1a45d5d
ac04e03
1a45d5d
55a034a
1a45d5d
45444c0
65676ec
 
47fea40
55a034a
 
 
 
8725cc4
 
 
55a034a
 
 
0ce34cb
 
55a034a
 
 
1a45d5d
 
 
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

import { supabase } from "@/integrations/supabase/client";

export const generateAIResponse = async (currentWord: string, currentSentence: string[], language: string = 'en', model?: string): Promise<string> => {
  console.log('Calling generate-word function with:', { currentWord, currentSentence, language });

  const { data, error } = await supabase.functions.invoke('generate-word', {
    body: {
      currentWord,
      currentSentence: currentSentence.join(' '),
      language,
      model
    }
  });

  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) {
    console.error('No word generated in response:', data);
    throw new Error('No word generated');
  }

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

export const guessWord = async (sentence: string, language: string, model?: string): Promise<{ guess: string; model: string }> => {
  console.log('Processing guess for sentence:', sentence);

  const words = sentence.trim().split(/\s+/);

  console.log('Calling guess-word function with sentence:', sentence, 'language:', language);

  const { data, error } = await supabase.functions.invoke('guess-word', {
    body: {
      sentence,
      language,
      model
    }
  });

  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) {
    console.error('No guess generated in response:', data);
    throw new Error('No guess generated');
  }

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