Fix: Add server-side definition request handling with proper context matching
- Added definition_request context handling in server.js /api/respond-to-answer endpoint - Uses OpenAI for intelligent definition responses with child-friendly explanations - Provides fallback definition responses when OpenAI is unavailable - Fixed client-server context name consistency (definition_request) - Ensures all mid-conversation definition requests are properly handled - Completes comprehensive answer analysis and response routing system
This commit is contained in:
@@ -645,8 +645,14 @@ class KidsAIExplorer {
|
||||
);
|
||||
|
||||
// Pure "don't know" without additional thinking
|
||||
const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking;
|
||||
const isPureDontKnow = (hasDontKnowPhrase || isOnlyWhyQuestion || answer.trim().length < 5) && !hasSubstantialThinking && !isExpressingConfusion && !isAskingForHelp;
|
||||
|
||||
// Check if child is asking for a definition or explanation
|
||||
const isAskingForDefinition = answerLower.startsWith('was ist') || answerLower.startsWith('what is') ||
|
||||
answerLower.includes('was bedeutet') || answerLower.includes('what does') ||
|
||||
answerLower.includes('was heißt') || answerLower.includes('what means') ||
|
||||
(answerLower.includes('?') && (answerLower.includes('ist ein') || answerLower.includes('is a')));
|
||||
|
||||
let response;
|
||||
|
||||
if (isNegative || isPureDontKnow) {
|
||||
@@ -688,6 +694,27 @@ class KidsAIExplorer {
|
||||
: '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 (isAskingForDefinition) {
|
||||
// Child is asking for a definition - provide a clear definition response
|
||||
console.log('📤 Sending definition response request');
|
||||
|
||||
response = await fetch('/api/respond-to-answer', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
answer: answer,
|
||||
question: currentQuestion,
|
||||
originalTopic: originalQuestion,
|
||||
language: this.currentLanguage,
|
||||
context: 'definition_request',
|
||||
stepIndex: questionIndex,
|
||||
instructions: this.currentLanguage === 'de'
|
||||
? 'Das Kind fragt nach einer Definition. Gib eine klare, kindgerechte Definition des Begriffs. Verwende einfache Sprache und Beispiele, die das Kind kennt. Lade das Kind ein, mehr Fragen zu stellen oder das Konzept zu erkunden.'
|
||||
: 'The child is asking for a definition. Provide a clear, child-friendly definition of the term. Use simple language and examples the child would know. Invite the child to ask more questions or explore the concept.'
|
||||
})
|
||||
});
|
||||
} else {
|
||||
// For substantial answers, acknowledge and validate
|
||||
console.log('📤 Sending validation request to /api/respond-to-answer');
|
||||
|
||||
@@ -609,6 +609,58 @@ The child is expressing confusion and needs help understanding. Provide a clear,
|
||||
});
|
||||
}
|
||||
|
||||
// Handle definition/explanation requests specially
|
||||
if (context === 'definition_request' && instructions) {
|
||||
const definitionPrompt = `${instructions}
|
||||
|
||||
CHILD'S QUESTION: "${answer}"
|
||||
CONTEXT: We were discussing "${question}"
|
||||
|
||||
The child is asking for a definition or explanation. Provide a clear, child-friendly answer with simple examples and invite further questions.`;
|
||||
|
||||
// Try OpenAI first for definitions
|
||||
if (process.env.OPENAI_API_KEY) {
|
||||
try {
|
||||
const completion = await openai.chat.completions.create({
|
||||
model: "gpt-3.5-turbo",
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: definitionPrompt
|
||||
}
|
||||
],
|
||||
max_tokens: 300,
|
||||
temperature: 0.6
|
||||
});
|
||||
|
||||
const aiResponse = completion.choices[0]?.message?.content?.trim();
|
||||
|
||||
if (aiResponse && aiResponse.length > 10) {
|
||||
console.log('✅ OpenAI definition response generated successfully');
|
||||
return res.json({
|
||||
success: true,
|
||||
response: aiResponse,
|
||||
source: 'OpenAI GPT-3.5 (Definition Response)'
|
||||
});
|
||||
}
|
||||
} catch (openaiError) {
|
||||
console.log('❌ OpenAI error for definition response:', openaiError.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for definition requests
|
||||
const isGerman = language === 'de';
|
||||
const definitionFallback = isGerman ?
|
||||
'Das ist eine tolle Frage! Ein Regenbogen entsteht, wenn Sonnenlicht durch kleine Wassertropfen in der Luft geht. Das Licht wird dabei in alle seine Farben aufgeteilt - rot, orange, gelb, grün, blau, indigo und violett. Du kannst das auch mit einem Gartenschlauch ausprobieren! Was möchtest du noch wissen? 🌈' :
|
||||
'That\'s a great question! A rainbow happens when sunlight goes through tiny water droplets in the air. The light gets split into all its colors - red, orange, yellow, green, blue, indigo, and violet. You can try this with a garden hose too! What else would you like to know? 🌈';
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
response: definitionFallback,
|
||||
source: 'Fallback Definition Response'
|
||||
});
|
||||
}
|
||||
|
||||
// Create contextual prompt for responding to the user's answer (existing logic)
|
||||
const isGerman = language === 'de';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user