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

View file

@ -1,7 +1,7 @@
// states/HandleChat.js
const path = require('path');
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 { chatWithAI } = require('../lib/ai-helper');
const config = require('../config.json');
@ -15,7 +15,6 @@ module.exports = async function HandleChat() {
console.log(`[STATE] HandleChat: ${message}`);
logChatMessage(db, username, message);
// Load available task scripts from the task directory.
const taskDir = path.join(__dirname, '../bot-tasks');
const availableTasks = fs.readdirSync(taskDir)
.filter(f => f.endsWith('.js'))
@ -26,7 +25,6 @@ module.exports = async function HandleChat() {
if (matchedTask) {
try {
const task = require(path.join(taskDir, matchedTask));
const { setActiveTask, clearActiveTask } = require('../core/context');
setActiveTask(matchedTask);
bot.chat(`Okay, Ill try to ${matchedTask.replace(/-/g, ' ')}.`);
await task(bot, db, bot.chat);
@ -35,21 +33,28 @@ module.exports = async function HandleChat() {
} catch (err) {
console.error(`[TASK ERROR] ${matchedTask}:`, err.message);
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 aiResponse = await chatWithAI(prompt, config.ai);
const aiTask = aiResponse.trim().toLowerCase().replace(/\s+/g, '-');
if (availableTasks.includes(aiTask)) {
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 aiTask = aiResponse.trim().toLowerCase().replace(/\s+/g, '-');
if (aiTask !== 'none' && availableTasks.includes(aiTask)) {
try {
const task = require(path.join(taskDir, aiTask));
bot.chat(`Okay, Ill try to ${aiTask.replace(/-/g, ' ')}.`);
setActiveTask(aiTask);
bot.chat(`Okay, Ill 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}".`);
clearActiveTask();
return;
}
}
@ -57,4 +62,4 @@ module.exports = async function HandleChat() {
const response = await chatWithAI(message, config.ai);
bot.chat(response);
}
};
};