log-surroundings and handlechat
All checks were successful
Deploy Cletus Bot / deploy (push) Successful in 25s

This commit is contained in:
roberts 2025-05-10 19:37:50 -05:00
parent d8a907dc63
commit 9ae853529d
2 changed files with 31 additions and 24 deletions

View file

@ -38,7 +38,6 @@ module.exports = async function logSurroundings() {
timestamp: now timestamp: now
}; };
// Store this exact block if not seen before
db.get(`SELECT id FROM memory WHERE label = ?`, [label], (err, row) => { db.get(`SELECT id FROM memory WHERE label = ?`, [label], (err, row) => {
if (err || row) return; if (err || row) return;
saveMemory(db, label, memoryData); saveMemory(db, label, memoryData);
@ -48,17 +47,20 @@ module.exports = async function logSurroundings() {
observedTypes.add(block.name); observedTypes.add(block.name);
const typeLabel = `recent:${block.name}`; const typeLabel = `recent:${block.name}`;
await new Promise(resolve => {
getMemory(db, typeLabel, (err, memory) => { getMemory(db, typeLabel, (err, memory) => {
if (err || !memory) { if (err || !memory) {
saveMemory(db, typeLabel, { lastSeen: now }); saveMemory(db, typeLabel, { lastSeen: now });
bot.chat(`I noticed ${block.name.replace(/_/g, ' ')} nearby.`); bot.chat(`I noticed ${block.name.replace(/_/g, ' ')} nearby.`);
} else { } else {
const age = (now - memory.lastSeen) / (1000 * 60); // in minutes const age = (now - memory.lastSeen) / (1000 * 60); // minutes
if (age > COOLDOWN_MINUTES) { if (age > COOLDOWN_MINUTES) {
saveMemory(db, typeLabel, { lastSeen: now }); saveMemory(db, typeLabel, { lastSeen: now });
bot.chat(`I saw another ${block.name.replace(/_/g, ' ')} nearby.`); bot.chat(`I saw another ${block.name.replace(/_/g, ' ')} nearby.`);
} }
} }
resolve();
});
}); });
} }
} }

View file

@ -1,7 +1,7 @@
// states/HandleChat.js // states/HandleChat.js
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const { getLastChat, getBot, getDB } = 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 } = require('../lib/ai-helper');
const config = require('../config.json'); const config = require('../config.json');
@ -15,7 +15,6 @@ module.exports = async function HandleChat() {
console.log(`[STATE] HandleChat: ${message}`); console.log(`[STATE] HandleChat: ${message}`);
logChatMessage(db, username, message); logChatMessage(db, username, message);
// Load available task scripts from the task directory.
const taskDir = path.join(__dirname, '../bot-tasks'); const taskDir = path.join(__dirname, '../bot-tasks');
const availableTasks = fs.readdirSync(taskDir) const availableTasks = fs.readdirSync(taskDir)
.filter(f => f.endsWith('.js')) .filter(f => f.endsWith('.js'))
@ -26,7 +25,6 @@ module.exports = async function HandleChat() {
if (matchedTask) { if (matchedTask) {
try { try {
const task = require(path.join(taskDir, matchedTask)); const task = require(path.join(taskDir, matchedTask));
const { setActiveTask, clearActiveTask } = require('../core/context');
setActiveTask(matchedTask); setActiveTask(matchedTask);
bot.chat(`Okay, Ill try to ${matchedTask.replace(/-/g, ' ')}.`); bot.chat(`Okay, Ill try to ${matchedTask.replace(/-/g, ' ')}.`);
await task(bot, db, bot.chat); await task(bot, db, bot.chat);
@ -35,21 +33,28 @@ module.exports = async function HandleChat() {
} catch (err) { } catch (err) {
console.error(`[TASK ERROR] ${matchedTask}:`, err.message); console.error(`[TASK ERROR] ${matchedTask}:`, err.message);
bot.chat(`I couldn't complete the task "${matchedTask}".`); bot.chat(`I couldn't complete the task "${matchedTask}".`);
clearActiveTask();
return;
} }
} }
if (!matchedTask) { 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}". If this is a command, what task name (like 'find-flower', 'chop-tree') would you execute? Otherwise, reply "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 (availableTasks.includes(aiTask)) { if (aiTask !== 'none' && availableTasks.includes(aiTask)) {
try {
const task = require(path.join(taskDir, aiTask)); const task = require(path.join(taskDir, aiTask));
bot.chat(`Okay, Ill try to ${aiTask.replace(/-/g, ' ')}.`);
setActiveTask(aiTask); setActiveTask(aiTask);
bot.chat(`Okay, Ill try to ${aiTask.replace(/-/g, ' ')}.`);
await task(bot, db, bot.chat); await task(bot, db, bot.chat);
clearActiveTask(); clearActiveTask();
return; return;
} catch (err) {
console.error(`[AI TASK ERROR] ${aiTask}:`, err.message);
bot.chat(`Something went wrong trying to do "${aiTask}".`);
clearActiveTask();
return;
} }
} }