cletus/bot/lib/ai-helper.js
roberts 643e039bd8
All checks were successful
Deploy Cletus Bot / deploy (push) Successful in 24s
test
2025-05-10 20:11:26 -05:00

49 lines
1.6 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 generating a Minecraft Mineflayer bot task. The task name is: "${taskName}".
You must return only a valid Node.js module that:
- Exports: \`module.exports = async function(bot, db, say) { ... }\`
- Uses only valid Mineflayer API methods.
- Do NOT use \`bot.inventory.find()\`. Use \`bot.inventory.items().find(...)\` instead.
- If accessing blocks, use \`bot.findBlock()\`, \`bot.blockAt()\`, or \`bot.openBlock()\`.
- Only respond with valid JavaScript code. No comments or markdown.
`.trim();
try {
const response = await chatWithAI(prompt, aiConfig);
let taskCode = response.trim();
// Remove wrapping backticks if present
if (taskCode.startsWith('```')) {
taskCode = taskCode.replace(/```[a-z]*\n?/i, '').replace(/```$/, '').trim();
}
if (!taskCode.includes('module.exports')) {
throw new Error('AI did not return a valid module.exports function.');
}
const taskPath = path.join(__dirname, '../bot-tasks', `${taskName}.js`);
fs.writeFileSync(taskPath, taskCode);
console.log(`[AI] Task file generated: ${taskPath}`);
} catch (err) {
console.error('[TASK GENERATION ERROR]', err.message);
}
}
module.exports = { chatWithAI, generateAndSaveTask };