new formatting for parsing answer

This commit is contained in:
Mateo Körber Belmonte
2025-03-14 17:35:00 +00:00
parent 5fb2cd4aae
commit 70cc49342e
+41 -24
View File
@@ -272,42 +272,52 @@ async function sendAiTranslateRequest() {
// Sort the data by day index to ensure correct order // Sort the data by day index to ensure correct order
data.sort((a, b) => a.day_index - b.day_index); data.sort((a, b) => a.day_index - b.day_index);
// Format the data as described in the prompt // Construct menu items array
let promptText = ''; const menuItems = [];
data.forEach(entry => { data.forEach(entry => {
// Add meat dish // Add meat dish
if (entry.meat_main) { if (entry.meat_main) {
promptText += `${entry.meat_main}\n`; menuItems.push({
main: entry.meat_main,
if (entry.meat_side) { side: entry.meat_side || ''
promptText += `${entry.meat_side}\n\n`; });
} else {
promptText += '\n';
}
} }
// Add vegetarian dish // Add vegetarian dish
if (entry.veggi_main) { if (entry.veggi_main) {
promptText += `${entry.veggi_main}\n`; menuItems.push({
main: entry.veggi_main,
if (entry.veggi_side) { side: entry.veggi_side || ''
promptText += `${entry.veggi_side}\n\n`; });
} else {
promptText += '\n';
}
} }
}); });
// Send to the AI service // Create a structured request with menu type
const requestData = {
menuItems: menuItems,
menuType: 'weekly' // Specify that this is a weekly menu
};
console.log('Sending request data:', requestData);
// Send to the AI service with the structured data
const aiResponse = await fetch('/ai-request', { const aiResponse = await fetch('/ai-request', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify({ prompt: promptText }) body: JSON.stringify(requestData)
}); });
// Check for non-JSON responses
const contentType = aiResponse.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
const errorText = await aiResponse.text();
console.error('Non-JSON response received:', errorText);
throw new Error('Server returned non-JSON response. You might need to log in again or check server logs.');
}
const aiData = await aiResponse.json(); const aiData = await aiResponse.json();
if (aiData.error) { if (aiData.error) {
console.error('AI request failed:', aiData.error); console.error('AI request failed:', aiData.error);
@@ -315,7 +325,7 @@ async function sendAiTranslateRequest() {
return; return;
} }
// Parse the AI response // Process the result as before
let cleanedText = aiData.result let cleanedText = aiData.result
.trim() .trim()
.replace(/\n/g, ''); .replace(/\n/g, '');
@@ -689,6 +699,8 @@ async function displayEnglishTranslations() {
// Sort the data by day index to ensure correct order // Sort the data by day index to ensure correct order
germanData.sort((a, b) => a.day_index - b.day_index); germanData.sort((a, b) => a.day_index - b.day_index);
console.log("German data loaded:", germanData.length, "entries");
// Create array of original dishes for reference // Create array of original dishes for reference
const originalDishes = []; const originalDishes = [];
germanData.forEach(entry => { germanData.forEach(entry => {
@@ -709,16 +721,20 @@ async function displayEnglishTranslations() {
} }
}); });
console.log("Original dishes:", originalDishes.length);
// Now fetch English translations // Now fetch English translations
const englishResponse = await fetch(`/load-week-en?week=${encodeURIComponent(week)}`); const englishResponse = await fetch(`/load-week-en?week=${encodeURIComponent(week)}`);
const englishData = await englishResponse.json(); const englishData = await englishResponse.json();
// Create formatted data for display console.log("English data loaded:", englishData.length, "entries");
// Create formatted data for display - IMPORTANT: clear the array first
const formattedTranslations = []; const formattedTranslations = [];
// Always create translation objects for each German dish, even if no English data exists // Create translation objects for each German dish
germanData.forEach(entry => { germanData.forEach(entry => {
// Find matching English entry by date (may be undefined) // Find matching English entry by date
const englishEntry = englishData.find(en => en.date === entry.date); const englishEntry = englishData.find(en => en.date === entry.date);
// Add meat dish translation (empty strings if no match) // Add meat dish translation (empty strings if no match)
@@ -738,11 +754,12 @@ async function displayEnglishTranslations() {
} }
}); });
console.log("Formatted translations:", formattedTranslations.length);
// Format as JSON string for the existing displayTranslation function // Format as JSON string for the existing displayTranslation function
// This will always contain objects for each dish, even with empty fields
const translationText = JSON.stringify(formattedTranslations); const translationText = JSON.stringify(formattedTranslations);
// Use a custom title for the modal to indicate these are from the database // Pass the data to displayTranslation with the original dishes for reference
displayTranslation(translationText, originalDishes); displayTranslation(translationText, originalDishes);
} catch (error) { } catch (error) {