CRITICAL FIX: Add missing repeated 'don't know' detection to chat mode

ROOT CAUSE FOUND:
- Chat mode was missing repeated 'don't know' detection logic entirely
- Only step-by-step mode had this functionality
- Server had undefined variable error when checking instructions
- This caused infinite questioning loops in chat conversations

FIXES APPLIED:
1. Added complete repeated 'don't know' detection to chat mode:
   - Conversation history tracking (last 4 messages)
   - German phrase detection variants
   - Threshold-based triggering (2+ occurrences)
   - Proper context passing to server

2. Fixed server-side undefined variable error:
   - Added null check for instructions parameter
   - Prevents TypeError when checking for repeated scenarios

3. Enhanced context handling:
   - Chat mode now sends 'repeated_dont_know' context
   - Proper instruction differentiation for different scenarios
   - Better debugging logs for both modes

RESULT:
- Both chat AND step-by-step modes now detect repeated frustration
- Children get explanations instead of endless questioning
- Eliminated infinite loop scenarios completely
- Better educational experience with adaptive responses
This commit is contained in:
root
2025-06-30 16:13:06 +02:00
parent 1e8149fdcc
commit 8d794b6318
2 changed files with 34 additions and 8 deletions

View File

@@ -458,11 +458,32 @@ class KidsAIExplorer {
// Pure "don't know" without additional thinking // Pure "don't know" without additional thinking
const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking && !isExpressingConfusion && !isAskingForHelp; const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking && !isExpressingConfusion && !isAskingForHelp;
// Check conversation history for repeated "don't know" responses (Chat Mode)
const conversationHistoryChat = 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 (Chat):', conversationHistoryChat);
const recentDontKnowCountChat = conversationHistoryChat.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('das weiß ich nicht') || msg.includes('das weiss ich nicht') ||
msg.includes('das weis ich nicht') || msg.includes('weiß ich auch nicht')
).length;
console.log(`🔢 Recent "don't know" count (Chat): ${recentDontKnowCountChat}`);
// If child has said "don't know" 2 or more times recently, they need help
const needsExplanationDueToRepeatedDontKnowChat = recentDontKnowCountChat >= 2;
let response; let response;
if (isExpressingConfusion) { if (isExpressingConfusion || needsExplanationDueToRepeatedDontKnowChat) {
// Child is expressing confusion - provide a simple explanation // Child is expressing confusion or repeatedly saying "don't know" - provide a simple explanation
console.log('📤 Sending explanation request for confused child'); console.log('📤 Sending explanation request for confused child or repeated dont know (Chat mode)');
const contextReason = needsExplanationDueToRepeatedDontKnowChat ? 'repeated_dont_know' : 'confusion_explanation';
response = await fetch('/api/respond-to-answer', { response = await fetch('/api/respond-to-answer', {
method: 'POST', method: 'POST',
@@ -474,11 +495,15 @@ class KidsAIExplorer {
question: currentQuestion, question: currentQuestion,
originalTopic: originalQuestion, originalTopic: originalQuestion,
language: this.currentLanguage, language: this.currentLanguage,
context: 'confusion_explanation', context: contextReason,
stepIndex: questionIndex, stepIndex: questionIndex,
instructions: this.currentLanguage === 'de' instructions: needsExplanationDueToRepeatedDontKnowChat
? (this.currentLanguage === 'de'
? 'Das Kind braucht jetzt eine einfache, klare Erklärung. Es hat bereits mehrmals "weiß nicht" gesagt. Gib eine vollständige Antwort mit praktischen Beispielen. Erkläre das Konzept Schritt für Schritt mit alltäglichen Vergleichen. Keine weiteren Fragen - das Kind braucht Wissen!'
: 'The child needs a simple, clear explanation now. They have said "don\'t know" multiple times. Give a complete answer with practical examples. Explain the concept step by step with everyday comparisons. No more questions - the child needs knowledge!')
: (this.currentLanguage === 'de'
? 'Das Kind ist verwirrt und braucht eine einfache Erklärung. Gib eine klare, konkrete Antwort für Kinder. Erkläre das Konzept mit alltäglichen Beispielen. Verwende einfache Wörter und lade zum Ausprobieren ein. Keine weiteren Fragen stellen - erst erklären!' ? 'Das Kind ist verwirrt und braucht eine einfache Erklärung. Gib eine klare, konkrete Antwort für Kinder. Erkläre das Konzept mit alltäglichen Beispielen. Verwende einfache Wörter und lade zum Ausprobieren ein. Keine weiteren Fragen stellen - erst erklären!'
: 'The child is confused and needs a simple explanation. Give a clear, concrete answer for children. Explain the concept with everyday examples. Use simple words and invite experimentation. Don\'t ask more questions - explain first!' : 'The child is confused and needs a simple explanation. Give a clear, concrete answer for children. Explain the concept with everyday examples. Use simple words and invite experimentation. Don\'t ask more questions - explain first!')
}) })
}); });
} else if (isNegative || isPureDontKnow) { } else if (isNegative || isPureDontKnow) {

View File

@@ -716,7 +716,8 @@ The child is asking for a definition, explanation, or clarification about locati
console.log(`🔍 Answer analysis - isDontKnow: ${isDontKnowResponse}, answer: "${answer}"`); 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 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'))) { if (isDontKnowResponse && (context === 'repeated_dont_know' ||
(instructions && (instructions.includes('mehrmals') || instructions.includes('multiple times'))))) {
console.log('🎯 Detected repeated "don\'t know" - providing explanation instead of more questions'); console.log('🎯 Detected repeated "don\'t know" - providing explanation instead of more questions');
const explanationPrompt = isGerman ? const explanationPrompt = isGerman ?