File size: 3,626 Bytes
55a034a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ce34cb
 
 
55a034a
 
 
 
8725cc4
 
 
 
 
 
 
 
 
 
 
 
 
55a034a
0ce34cb
55a034a
8725cc4
 
 
 
 
 
 
 
 
 
 
0ce34cb
8725cc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55a034a
8725cc4
 
 
 
 
55a034a
8725cc4
 
55a034a
 
 
8725cc4
 
 
 
 
 
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
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
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { Mistral } from "npm:@mistralai/mistralai";

const corsHeaders = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
};

serve(async (req) => {
  if (req.method === 'OPTIONS') {
    return new Response(null, { headers: corsHeaders });
  }

  try {
    const { currentWord, currentSentence } = await req.json();
    console.log('Generating word for:', { currentWord, currentSentence });

    // currentSentence is already a string from the client
    const existingSentence = currentSentence || '';

    const client = new Mistral({
      apiKey: Deno.env.get('MISTRAL_API_KEY'),
    });

    // Add retry logic with exponential backoff
    const maxRetries = 3;
    let retryCount = 0;
    let lastError = null;

    while (retryCount < maxRetries) {
      try {
        const response = await client.chat.complete({
          model: "mistral-large-latest",
          messages: [
            {
              role: "system",
              content: `You are helping in a word game. The secret word is "${currentWord}". 
                    Your task is to find a sentence to describe this word without using it directly. 
                    Answer with a complete, grammatically correct sentence that starts with "${existingSentence}".
                    Do not add quotes or backticks. Just answer with the sentence.`
            }
          ],
          maxTokens: 10,
          temperature: 0.7
        });

        const aiResponse = response.choices[0].message.content.trim();
        console.log('AI full response:', aiResponse);
        
        // Extract the new word by comparing with the existing sentence
        const newWord = aiResponse
          .slice(existingSentence.length)
          .trim()
          .split(' ')[0]
          .replace(/[.,!?]$/, ''); // Remove any punctuation at the end
        
        console.log('Extracted new word:', newWord);

        return new Response(
          JSON.stringify({ word: newWord }),
          { headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
        );
      } catch (error) {
        console.error(`Attempt ${retryCount + 1} failed:`, error);
        lastError = error;
        
        // If it's a rate limit error, wait before retrying
        if (error.message?.includes('rate limit') || error.status === 429) {
          const waitTime = Math.pow(2, retryCount) * 1000; // Exponential backoff: 1s, 2s, 4s
          console.log(`Rate limit hit, waiting ${waitTime}ms before retry`);
          await new Promise(resolve => setTimeout(resolve, waitTime));
          retryCount++;
          continue;
        }
        
        // If it's not a rate limit error, throw immediately
        throw error;
      }
    }

    // If we've exhausted all retries
    throw new Error(`Failed after ${maxRetries} attempts. Last error: ${lastError?.message}`);

  } catch (error) {
    console.error('Error generating word:', error);
    
    // Provide a more user-friendly error message
    const errorMessage = error.message?.includes('rate limit') 
      ? "The AI service is currently busy. Please try again in a few moments."
      : "Sorry, there was an error generating the word. Please try again.";

    return new Response(
      JSON.stringify({ 
        error: errorMessage,
        details: error.message 
      }),
      { 
        status: error.message?.includes('rate limit') ? 429 : 500,
        headers: { ...corsHeaders, 'Content-Type': 'application/json' }
      }
    );
  }
});