This commit is contained in:
parent
37ee4a0ac1
commit
2b5d49d5bc
1 changed files with 13 additions and 7 deletions
|
|
@ -3,7 +3,7 @@ const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const { getLastChat, getBot, getDB, setActiveTask, clearActiveTask } = require('../core/context');
|
const { getLastChat, getBot, getDB, setActiveTask, clearActiveTask } = require('../core/context');
|
||||||
const { logChatMessage } = require('../memory/chat');
|
const { logChatMessage } = require('../memory/chat');
|
||||||
const { chatWithAI } = require('../lib/ai-helper');
|
const { chatWithAI, generateAndSaveTask } = require('../lib/ai-helper');
|
||||||
const config = require('../config.json');
|
const config = require('../config.json');
|
||||||
|
|
||||||
module.exports = async function HandleChat() {
|
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 aiResponse = await chatWithAI(prompt, config.ai);
|
||||||
const aiTask = aiResponse.trim().toLowerCase().replace(/\s+/g, '-');
|
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 {
|
try {
|
||||||
const task = require(path.join(taskDir, aiTask));
|
const task = require(taskPath);
|
||||||
setActiveTask(aiTask);
|
setActiveTask(aiTask);
|
||||||
bot.chat(`Okay, I’ll try to ${aiTask.replace(/-/g, ' ')}.`);
|
|
||||||
await task(bot, db, bot.chat);
|
await task(bot, db, bot.chat);
|
||||||
clearActiveTask();
|
clearActiveTask();
|
||||||
return;
|
return;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`[AI TASK ERROR] ${aiTask}:`, err.message);
|
console.error(`[TASK ERROR - AI GENERATED] ${aiTask}:`, err.message);
|
||||||
bot.chat(`Something went wrong trying to do "${aiTask}".`);
|
bot.chat(`I tried to learn how to ${aiTask.replace(/-/g, ' ')}, but it didn’t work.`);
|
||||||
clearActiveTask();
|
clearActiveTask();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue