This commit is contained in:
parent
ee1b3504ac
commit
9f95804fc9
1 changed files with 24 additions and 2 deletions
|
|
@ -3,7 +3,7 @@ const path = require('path');
|
|||
const fs = require('fs');
|
||||
const { getLastChat, getBot, getDB, setActiveTask, clearActiveTask } = require('../core/context');
|
||||
const { logChatMessage } = require('../memory/chat');
|
||||
const { chatWithAI, generateAndSaveTask } = require('../lib/ai-helper');
|
||||
const { chatWithAI } = require('../lib/ai-helper');
|
||||
const config = require('../config.json');
|
||||
|
||||
module.exports = async function HandleChat() {
|
||||
|
|
@ -48,13 +48,35 @@ module.exports = async function HandleChat() {
|
|||
if (!fs.existsSync(taskPath)) {
|
||||
bot.chat(`I don’t know how to ${aiTask.replace(/-/g, ' ')} yet, learning now...`);
|
||||
|
||||
// Use template to generate the file
|
||||
const templatePath = path.join(__dirname, '../bot-tasks/TaskTemplate.js');
|
||||
if (fs.existsSync(templatePath)) {
|
||||
let template = fs.readFileSync(templatePath, 'utf8');
|
||||
template = template.replace(/task-template/g, aiTask);
|
||||
fs.writeFileSync(taskPath, template);
|
||||
console.log(`[AI] Task file generated from template: ${taskPath}`);
|
||||
|
||||
// Ask AI to overwrite it with real logic
|
||||
const taskPrompt = `
|
||||
You are a Mineflayer bot assistant. Generate a new Node.js module for this task:
|
||||
|
||||
Task name: "${aiTask}"
|
||||
|
||||
Use this exact export:
|
||||
module.exports = async function(bot, db, say) { ... }
|
||||
|
||||
Use valid Mineflayer APIs. Do NOT use bot.inventory.find(). Use bot.inventory.items().
|
||||
This task should do what the player just asked: "${message}"
|
||||
|
||||
Only return valid code. No markdown. No explanation.
|
||||
`.trim();
|
||||
|
||||
const aiCode = await chatWithAI(taskPrompt, config.ai);
|
||||
if (aiCode.includes('module.exports')) {
|
||||
fs.writeFileSync(taskPath, aiCode.trim());
|
||||
console.log(`[AI] Task file overwritten with AI code: ${taskPath}`);
|
||||
} else {
|
||||
console.warn(`[AI] Failed to get valid task code from model. Keeping the template.`);
|
||||
}
|
||||
} else {
|
||||
bot.chat(`Template not found. Cannot learn the task.`);
|
||||
return;
|
||||
|
|
|
|||
Loading…
Reference in a new issue