Skip to content

Commit 294907b

Browse files
committed
fix(ai): update google/genai implementation from legacy interactions to models.generateContent to restore AI Assistant functionality
1 parent 5836cb3 commit 294907b

1 file changed

Lines changed: 20 additions & 32 deletions

File tree

routes/ai.js

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,20 @@ const callAI = async (prompt, systemInstruction = "You are a helpful AI study as
3838

3939
let lastError = null;
4040

41-
// Try each model using the new syntax
41+
// Try each model using the correct syntax
4242
for (const modelName of models) {
4343
try {
4444
console.log(`🤖 AI Attempt: ${modelName} via @google/genai`);
4545

46-
const interaction = await client.interactions.create({
46+
const response = await client.models.generateContent({
4747
model: modelName,
48-
input: `${systemInstruction}\n\nStudent Input: ${prompt}`,
48+
contents: `${systemInstruction}\n\nStudent Input: ${prompt}`,
4949
});
5050

51-
// Extract text from the new interaction output structure
52-
if (interaction && interaction.outputs && interaction.outputs.length > 0) {
53-
const text = interaction.outputs[interaction.outputs.length - 1].text;
54-
if (text) {
55-
console.log(`✅ AI Success: ${modelName}`);
56-
return text.trim();
57-
}
51+
// Extract text from the response
52+
if (response && response.text) {
53+
console.log(`✅ AI Success: ${modelName}`);
54+
return response.text.trim();
5855
}
5956
} catch (err) {
6057
lastError = err;
@@ -167,14 +164,12 @@ router.post('/chat', auth, async (req, res) => {
167164
const responseText = await callAI(prompt, "You are a concise, highly knowledgeable, friendly tutor helping a university student. Keep answers under 3 short paragraphs. Use analogies.");
168165
res.json({ reply: responseText.trim() });
169166
} catch (e) {
170-
if (e.message === 'API_KEY_MISSING') {
167+
if (e.message.includes('API_KEY_MISSING')) {
171168
res.json({ reply: "I'm currently in **Demonstration Mode**. Please configure the `GEMINI_API_KEY` to enable my full intelligence." });
169+
} else if (e.message && (e.message.toLowerCase().includes('quota') || e.message.toLowerCase().includes('429'))) {
170+
res.json({ reply: "My Gemini API quota has been exhausted temporarily! 🚀\n\nWhile I wait for the rate limit to reset, I highly recommend checking out your **AI Arsenal** (in the sidebar)! We recently upgraded its tools to run on indestructible local algorithms, so they work beautifully even when I'm offline." });
172171
} else {
173-
if (message.includes("Give me one short")) {
174-
res.json({ reply: "Discipline is choosing between what you want now and what you want most. Open your planner and let's go!" });
175-
} else {
176-
res.json({ reply: "My server API link is resetting to protect quotas! But don't worry—your local AI Arsenal tools are still perfectly functional right now." });
177-
}
172+
res.json({ reply: "My Gemini API is taking a quick nap (rate limit/connection issue). While I wake up, jump into the **AI Arsenal**—it uses backup algorithms so it never goes offline!" });
178173
}
179174
}
180175
} catch (err) {
@@ -553,22 +548,15 @@ router.post('/mind-sweep', auth, async (req, res) => {
553548
"notes": []
554549
}`;
555550

556-
try {
557-
const responseText = await callAI(prompt, "You are an intelligent task extractor and organizer. Respond ONLY with valid JSON.");
558-
const result = extractJson(responseText);
559-
560-
res.json({
561-
todos: result.todos || [],
562-
assignments: result.assignments || [],
563-
notes: result.notes || []
564-
});
565-
} catch (e) {
566-
res.json({
567-
todos: [{ title: `Review Brain Dump: "${text.substring(0, 20)}..."`, priority: 'High', dayPlan: true, category: 'Personal' }],
568-
assignments: [],
569-
notes: [{ title: 'Auto-Archived Mind Sweep', content: text, tags: ["BrainDump", "Fallback"] }]
570-
});
571-
}
551+
const responseText = await callAI(prompt, "You are an intelligent task extractor and organizer. Respond ONLY with valid JSON.");
552+
const result = extractJson(responseText);
553+
554+
// ensure default arrays
555+
res.json({
556+
todos: result.todos || [],
557+
assignments: result.assignments || [],
558+
notes: result.notes || []
559+
});
572560

573561
} catch (err) {
574562
res.status(500).json({ message: "Mind Sweep Error", error: err.message });

0 commit comments

Comments
 (0)