diff --git a/html/kidsai/script-new.js b/html/kidsai/script-new.js index a7ad501..984ac80 100644 --- a/html/kidsai/script-new.js +++ b/html/kidsai/script-new.js @@ -658,16 +658,22 @@ class KidsAIExplorer { const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking && !isExpressingConfusion && !isAskingForHelp; // Check conversation history for repeated "don't know" responses - const conversationHistory = Array.from(this.conversationContainer.querySelectorAll('.user-message .message-content p')) - .map(p => p.textContent.toLowerCase().trim()) - .slice(-3); // Last 3 user responses + const conversationHistory = Array.from(this.conversationContainer.querySelectorAll('.user-message .message-content')) + .map(el => el.textContent.toLowerCase().trim()) + .slice(-4); // Last 4 user responses + + console.log('🔍 Recent conversation history:', conversationHistory); const recentDontKnowCount = conversationHistory.filter(msg => msg.includes('weiß nicht') || msg.includes('weis nicht') || msg.includes('weiss nicht') || - msg.includes("don't know") || msg.includes('i dont know') || msg.includes('keine ahnung') + msg.includes("don't know") || msg.includes('i dont know') || msg.includes('keine ahnung') || + msg.includes('das weiß ich nicht') || msg.includes('das weiss ich nicht') || + msg.includes('das weis ich nicht') || msg.includes('weiß ich auch nicht') ).length; - // If child has said "don't know" multiple times recently, they need help + console.log(`🔢 Recent "don't know" count: ${recentDontKnowCount}`); + + // If child has said "don't know" 2 or more times recently, they need help const needsExplanationDueToRepeatedDontKnow = recentDontKnowCount >= 2; // Check if child is asking for a definition or explanation diff --git a/html/kidsai/server.js b/html/kidsai/server.js index 118c8d5..e919348 100755 --- a/html/kidsai/server.js +++ b/html/kidsai/server.js @@ -706,6 +706,61 @@ The child is asking for a definition, explanation, or clarification about locati // Create contextual prompt for responding to the user's answer (existing logic) const isGerman = language === 'de'; + // Check if this looks like a repeated "don't know" scenario + const answerLowerChat = answer.toLowerCase().trim(); + const isDontKnowResponse = answerLowerChat.includes("don't know") || answerLowerChat.includes("weiß nicht") || + answerLowerChat.includes("weis nicht") || answerLowerChat.includes("weiss nicht") || + answerLowerChat.includes("keine ahnung") || answerLowerChat.includes("ich weiß es nicht") || + answerLowerChat.includes("das weiß ich nicht") || answerLowerChat.includes("weiß ich auch nicht"); + + console.log(`🔍 Answer analysis - isDontKnow: ${isDontKnowResponse}, answer: "${answer}"`); + + // If this is a "don't know" response and we're in a loop, provide explanation + if (isDontKnowResponse && (context === 'repeated_dont_know' || instructions.includes('mehrmals') || instructions.includes('multiple times'))) { + console.log('🎯 Detected repeated "don\'t know" - providing explanation instead of more questions'); + + const explanationPrompt = isGerman ? + `Das Kind braucht jetzt eine klare Erklärung, da es mehrmals "weiß nicht" gesagt hat. + +URSPRÜNGLICHE FRAGE: "${originalTopic || question}" +CONTEXT: Wiederholte "weiß nicht" Antworten + +Gib eine einfache, verständliche Erklärung für Kinder. Verwende Beispiele aus dem Alltag. Erkläre Schritt für Schritt. Keine weiteren Fragen - das Kind braucht jetzt Wissen!` : + `The child needs a clear explanation now since they've said "don't know" multiple times. + +ORIGINAL QUESTION: "${originalTopic || question}" +CONTEXT: Repeated "don't know" responses + +Give a simple, understandable explanation for children. Use everyday examples. Explain step by step. No more questions - the child needs knowledge now!`; + + try { + const completion = await openai.chat.completions.create({ + model: "gpt-4o-mini", + messages: [ + { + role: "system", + content: explanationPrompt + } + ], + max_tokens: 300, + temperature: 0.6 + }); + + const aiResponse = completion.choices[0]?.message?.content?.trim(); + + if (aiResponse && aiResponse.length > 10) { + console.log('✅ OpenAI explanation for repeated "don\'t know" generated successfully'); + return res.json({ + success: true, + response: aiResponse, + source: 'OpenAI GPT-4o-mini (Repeated Don\'t Know Explanation)' + }); + } + } catch (openaiError) { + console.log('❌ OpenAI error for repeated don\'t know explanation:', openaiError.message); + } + } + const contextualPrompt = isGerman ? `Du bist ein Socratic Lernbegleiter für Kinder. Ein Kind hat gerade auf eine Frage geantwortet.