cletus/bot/lib/ai-helper.js
roberts cd1253e965
All checks were successful
Deploy Cletus Bot / deploy (push) Successful in 26s
Updated generate and save tasks.
2025-05-11 14:00:34 -05:00

49 lines
1.5 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const axios = require('axios');
const config = require('../config.json');
async function chatWithAI(prompt, aiConfig) {
const res = await axios.post(aiConfig.ollamaUrl, {
model: aiConfig.model,
prompt,
stream: false
});
return res.data.response;
}
async function generateAndSaveTask(taskName, aiConfig) {
const prompt = `
You are a Minecraft Mineflayer bot generator. The task is called "${taskName}".
Return only a valid Node.js module that:
- Exports: \`module.exports = async function(bot, db, say) { ... }\`
- Uses only Mineflayer APIs like \`bot.inventory.items()\`, \`bot.blockAt()\`, etc.
- DO NOT return markdown, explanations, or comments — code only.
The task should accomplish: "${taskName.replace(/-/g, ' ')}"
`.trim();
try {
const response = await chatWithAI(prompt, aiConfig);
let taskCode = response.trim();
// Strip markdown formatting
if (taskCode.startsWith('```')) {
taskCode = taskCode.replace(/```[a-z]*\n?/i, '').replace(/```$/, '').trim();
}
if (!taskCode.startsWith('module.exports')) {
throw new Error('AI response did not begin with module.exports');
}
const taskPath = path.join(__dirname, '../bot-tasks', `${taskName}.js`);
fs.writeFileSync(taskPath, taskCode);
console.log(`[AI] Task file overwritten with AI code: ${taskPath}`);
} catch (err) {
console.error('[TASK GENERATION ERROR]', err.message);
}
}
module.exports = { chatWithAI, generateAndSaveTask };