`;
return;
}
// Show loading state
outputPlaceholder.classList.add('hidden');
loadingSpinner.classList.remove('hidden');
categorizedOutput.innerHTML = '';
categorizedOutput.appendChild(loadingSpinner);
const prompt = `
Analyze the following block of study notes. Identify the main subjects and the specific topics within each subject.
Organize all the notes into a structured format.
RULES:
- Group all related notes under the most appropriate subject and topic.
- If a line seems to be a topic heading, use it as the topic name.
- Do not summarize or change the content of the notes. Present them as they are.
--- STUDY NOTES ---
${notes}
--- END STUDY NOTES ---
`;
// Define the expected JSON structure for the response
const payload = {
contents: [{ role: "user", parts: [{ text: prompt }] }],
generationConfig: {
responseMimeType: "application/json",
responseSchema: {
type: "ARRAY",
items: {
type: "OBJECT",
properties: {
"subject": { "type": "STRING" },
"topics": {
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"topic_name": { "type": "STRING" },
"notes": {
"type": "ARRAY",
"items": { "type": "STRING" }
}
},
required: ["topic_name", "notes"]
}
}
},
required: ["subject", "topics"]
}
}
}
};
try {
const response = await fetch(API_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
const result = await response.json();
if (result.candidates && result.candidates[0].content && result.candidates[0].content.parts[0].text) {
const jsonText = result.candidates[0].content.parts[0].text;
const parsedJson = JSON.parse(jsonText);
displayCategorizedNotes(parsedJson);
} else {
throw new Error("Invalid response structure from API.");
}
} catch (error) {
console.error("Error categorizing notes:", error);
categorizedOutput.innerHTML = ``;
} finally {
loadingSpinner.classList.add('hidden');
}
};
/**
* Renders the categorized notes in the output area.
* @param {Array
An error occurred. Please try again.
