feat: Add interactive thinking feedback system

- Add /api/thinking-feedback endpoint for real-time AI responses
- Implement getThinkingFeedback() with OpenAI integration
- Add fallback positive feedback for offline scenarios
- Enhanced question-specific AI prompts for exploratory topics
- Remove generic 'what do you know about this topic' questions
- Generate contextual, topic-specific thinking questions
This commit is contained in:
root
2025-06-29 13:11:55 +02:00
parent 70fb60fd68
commit d4e55b6912

View File

@@ -238,6 +238,56 @@ app.post('/api/think-response', async (req, res) => {
}
});
// API endpoint for interactive thinking feedback
app.post('/api/thinking-feedback', async (req, res) => {
const { question, stepNumber, userThought, language = 'en' } = req.body;
if (!question || !userThought || stepNumber === undefined) {
return res.status(400).json({
success: false,
error: 'Question, step number, and user thought are required'
});
}
try {
// Get AI feedback on the child's thinking
const feedback = await getThinkingFeedback(question, stepNumber, userThought, language);
res.json({
success: true,
feedback: feedback,
question: question,
stepNumber: stepNumber,
userThought: userThought,
language: language
});
} catch (error) {
console.error('Thinking feedback error:', error);
// Fallback positive feedback
const fallbackFeedback = {
response: language === 'de'
? "Interessante Gedanken! Du denkst in die richtige Richtung. 👍"
: "Interesting thoughts! You're thinking in the right direction. 👍",
encouragement: language === 'de'
? "Weiter so!"
: "Keep going!",
type: 'positive'
};
res.json({
success: true,
feedback: fallbackFeedback,
question: question,
stepNumber: stepNumber,
userThought: userThought,
language: language,
fallback: true
});
}
});
// Function to calculate the correct answer for simple math expressions
function calculateMathAnswer(question) {
const questionLower = question.toLowerCase().trim();
@@ -785,12 +835,12 @@ async function getThinkingFeedback(question, thought, stepNumber, language) {
const isGerman = language === 'de';
const systemPrompt = isGerman
? "Du bist ein encourager Lernbegleiter für Kinder. Ein Kind hat über eine Frage nachgedacht und seinen Gedanken geteilt. Gib kurzes, ermutigendes Feedback (1-2 Sätze) zu ihrem Denkprozess. Sei positiv, erkenne gute Punkte an, und gib ggf. einen kleinen Hinweis für weitere Überlegungen. Nie die komplette Antwort verraten!"
: "You are an encouraging learning companion for children. A child has thought about a question and shared their thinking. Give brief, encouraging feedback (1-2 sentences) on their thought process. Be positive, acknowledge good points, and optionally give a small hint for further thinking. Never reveal the complete answer!";
? "Du bist ein ermutigender Tutor für Kinder. Ein Kind hat zu einer Denkfrage geantwortet. Gib eine kurze, positive Rückmeldung (1-2 Sätze) zu seinem Gedanken. Sei ermutigend, auch wenn die Antwort nicht perfekt ist. Erkenne gute Ansätze an und gib sanfte Hinweise für Verbesserungen."
: "You are an encouraging tutor for children. A child has responded to a thinking question. Give brief, positive feedback (1-2 sentences) about their thought. Be encouraging even if the answer isn't perfect. Acknowledge good approaches and give gentle hints for improvements.";
const userPrompt = isGerman
? `Frage: "${question}"\nKind's Gedanke: "${thought}"\n\nGib kurzes, ermutigendes Feedback zu diesem Gedanken und hilf beim Weiterdenken.`
: `Question: "${question}"\nChild's thought: "${thought}"\n\nGive brief, encouraging feedback on this thought and help with further thinking.`;
? `Frage: "${question}"\nSchritt ${stepNumber}\nKind dachte: "${thought}"\n\nGib eine kurze, ermutigende Rückmeldung zu diesem Gedanken.`
: `Question: "${question}"\nStep ${stepNumber}\nChild thought: "${thought}"\n\nGive brief, encouraging feedback about this thought.`;
try {
const completion = await openai.chat.completions.create({
@@ -800,15 +850,22 @@ async function getThinkingFeedback(question, thought, stepNumber, language) {
{ role: "user", content: userPrompt }
],
max_tokens: 100,
temperature: 0.8
temperature: 0.7
});
const aiResponse = completion.choices[0]?.message?.content || '';
// Determine feedback type based on content
const responseType = aiResponse.toLowerCase().includes('gut') ||
aiResponse.toLowerCase().includes('great') ||
aiResponse.toLowerCase().includes('good') ||
aiResponse.toLowerCase().includes('richtig')
? 'positive' : 'neutral';
return {
type: 'ai-powered',
text: aiResponse.trim(),
encouragement: getRandomEncouragement(language),
response: aiResponse.trim(),
encouragement: isGerman ? "Weiter so! 🌟" : "Keep going! 🌟",
type: responseType,
source: 'OpenAI GPT-3.5'
};
@@ -817,29 +874,3 @@ async function getThinkingFeedback(question, thought, stepNumber, language) {
throw error;
}
}
// Fallback thinking feedback
function getFallbackThinkingFeedback(language) {
const isGerman = language === 'de';
const responses = isGerman ? [
"Interessanter Gedanke! Das zeigt, dass du nachdenkst.",
"Gute Überlegung! Du bist auf dem richtigen Weg.",
"Das ist ein wichtiger Punkt! Denk weiter in diese Richtung.",
"Super, dass du so genau nachdenkst!"
] : [
"Interesting thought! That shows you're thinking.",
"Good thinking! You're on the right track.",
"That's an important point! Keep thinking in that direction.",
"Great that you're thinking so carefully!"
];
const randomResponse = responses[Math.floor(Math.random() * responses.length)];
return {
type: 'fallback',
text: randomResponse,
encouragement: getRandomEncouragement(language),
source: 'Fallback'
};
}