diff --git a/bot/states/HandleChat.js b/bot/states/HandleChat.js index 5d8dd2b..e59f6b5 100644 --- a/bot/states/HandleChat.js +++ b/bot/states/HandleChat.js @@ -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 } = require('../lib/ai-helper'); +const { chatWithAI, generateAndSaveTask } = require('../lib/ai-helper'); const config = require('../config.json'); module.exports = async function HandleChat() { @@ -38,21 +38,27 @@ module.exports = async function HandleChat() { } } - const prompt = `Player said: "${message}". If this is a command, what task name (like 'find-flower', 'chop-tree') would you execute? Otherwise, reply 'none'.`; + const prompt = `Player said: "${message}". What task name should I execute (like 'find-flower', 'move-items')? Reply only with the task name or "none".`; const aiResponse = await chatWithAI(prompt, config.ai); const aiTask = aiResponse.trim().toLowerCase().replace(/\s+/g, '-'); - if (aiTask !== 'none' && availableTasks.includes(aiTask)) { + if (aiTask !== 'none') { + const taskPath = path.join(taskDir, `${aiTask}.js`); + + if (!fs.existsSync(taskPath)) { + bot.chat(`I don’t know how to ${aiTask.replace(/-/g, ' ')} yet, learning now...`); + await generateAndSaveTask(aiTask, config.ai); + } + try { - const task = require(path.join(taskDir, aiTask)); + const task = require(taskPath); setActiveTask(aiTask); - bot.chat(`Okay, I’ll try to ${aiTask.replace(/-/g, ' ')}.`); await task(bot, db, bot.chat); clearActiveTask(); return; } catch (err) { - console.error(`[AI TASK ERROR] ${aiTask}:`, err.message); - bot.chat(`Something went wrong trying to do "${aiTask}".`); + console.error(`[TASK ERROR - AI GENERATED] ${aiTask}:`, err.message); + bot.chat(`I tried to learn how to ${aiTask.replace(/-/g, ' ')}, but it didn’t work.`); clearActiveTask(); return; }