mindcraft/src/agent/agent.js

468 lines
18 KiB
JavaScript
Raw Normal View History

2023-12-21 15:11:38 -07:00
import { History } from './history.js';
import { Coder } from './coder.js';
import { Prompter } from './prompter.js';
import { initModes } from './modes.js';
2024-01-25 13:25:36 -08:00
import { initBot } from '../utils/mcdata.js';
2024-04-30 23:07:07 -05:00
import { containsCommand, commandExists, executeCommand, truncCommandMessage, isAction } from './commands/index.js';
2024-11-03 22:17:28 -06:00
import { ActionManager } from './action_manager.js';
2024-03-05 12:05:46 -08:00
import { NPCContoller } from './npc/controller.js';
import { MemoryBank } from './memory_bank.js';
import { SelfPrompter } from './self_prompter.js';
import { isOtherAgent, initConversationManager, sendToBot, endAllChats } from './conversation.js';
2024-10-02 00:57:28 -05:00
import { handleTranslation, handleEnglishTranslation } from '../utils/translator.js';
import { addViewer } from './viewer.js';
import settings from '../../settings.js';
import { serverProxy } from './server_proxy.js';
export class Agent {
async start(profile_fp, load_mem=false, init_message=null, count_id=0) {
this.last_sender = null;
2024-11-07 10:25:49 -06:00
try {
2024-11-07 10:48:45 -06:00
// Add validation for profile_fp
if (!profile_fp) {
throw new Error('No profile filepath provided');
}
2024-04-20 22:18:26 -05:00
// Connect to MindServer via proxy
serverProxy.connect();
2024-11-07 10:48:45 -06:00
console.log('Starting agent initialization with profile:', profile_fp);
// Initialize components with more detailed error handling
try {
console.log('Initializing action manager...');
this.actions = new ActionManager(this);
console.log('Initializing prompter...');
this.prompter = new Prompter(this, profile_fp);
this.name = this.prompter.getName();
console.log('Initializing history...');
this.history = new History(this);
console.log('Initializing coder...');
this.coder = new Coder(this);
console.log('Initializing npc controller...');
this.npc = new NPCContoller(this);
console.log('Initializing memory bank...');
this.memory_bank = new MemoryBank();
console.log('Initializing self prompter...');
this.self_prompter = new SelfPrompter(this);
2024-11-07 13:30:02 -06:00
initConversationManager(this);
// After getting the name, register with MindServer via proxy
serverProxy.registerAgent(this.name);
2024-11-07 10:48:45 -06:00
} catch (error) {
throw new Error(`Failed to initialize agent components: ${error.message || error}`);
}
2024-11-07 10:25:49 -06:00
try {
2024-11-07 10:48:45 -06:00
console.log('Initializing examples...');
2024-11-07 10:25:49 -06:00
await this.prompter.initExamples();
} catch (error) {
2024-11-07 10:48:45 -06:00
throw new Error(`Failed to initialize examples: ${error.message || error}`);
2024-11-07 10:25:49 -06:00
}
2024-04-20 22:18:26 -05:00
2024-11-07 10:25:49 -06:00
console.log('Logging into minecraft...');
try {
this.bot = initBot(this.name);
} catch (error) {
2024-11-07 10:48:45 -06:00
throw new Error(`Failed to initialize Minecraft bot: ${error.message || error}`);
2024-11-07 10:25:49 -06:00
}
2024-01-30 16:43:30 -06:00
2024-11-07 10:25:49 -06:00
initModes(this);
2023-12-04 23:26:21 -08:00
2024-11-07 10:25:49 -06:00
let save_data = null;
if (load_mem) {
try {
save_data = this.history.load();
} catch (error) {
console.error('Failed to load history:', error);
// Don't throw here, continue without history
}
}
2023-12-04 23:26:21 -08:00
2024-11-07 10:25:49 -06:00
// Return a promise that resolves when spawn is complete
return new Promise((resolve, reject) => {
// Add timeout to prevent hanging
const spawnTimeout = setTimeout(() => {
reject(new Error('Bot spawn timed out after 30 seconds'));
}, 30000);
this.bot.once('error', (error) => {
2024-11-07 11:35:09 -06:00
clearTimeout(spawnTimeout);
2024-11-07 10:25:49 -06:00
console.error('Bot encountered error:', error);
reject(error);
});
this.bot.on('login', () => {
console.log('Logged in!');
2024-11-14 17:06:26 -06:00
2024-11-14 17:18:50 -06:00
// Set skin for profile, requires Fabric Tailor. (https://modrinth.com/mod/fabrictailor)
2024-11-14 17:06:26 -06:00
if (this.prompter.profile.skin)
2024-11-14 17:18:50 -06:00
this.bot.chat(`/skin set URL ${this.prompter.profile.skin.model} ${this.prompter.profile.skin.path}`);
else
this.bot.chat(`/skin clear`);
2024-11-07 10:25:49 -06:00
});
this.bot.once('spawn', async () => {
try {
clearTimeout(spawnTimeout);
addViewer(this.bot, count_id);
// wait for a bit so stats are not undefined
await new Promise((resolve) => setTimeout(resolve, 1000));
2024-11-14 17:06:26 -06:00
2024-11-07 10:25:49 -06:00
console.log(`${this.name} spawned.`);
this.clearBotLogs();
this._setupEventHandlers(save_data, init_message);
this.startEvents();
resolve();
} catch (error) {
reject(error);
}
});
});
} catch (error) {
2024-11-07 10:48:45 -06:00
// Ensure we're not losing error details
console.error('Agent start failed with error:', {
message: error.message || 'No error message',
stack: error.stack || 'No stack trace',
error: error
});
throw error; // Re-throw with preserved details
}
2024-11-07 10:25:49 -06:00
}
2024-04-20 22:18:26 -05:00
2024-11-07 10:25:49 -06:00
// Split out event handler setup for clarity
_setupEventHandlers(save_data, init_message) {
const ignore_messages = [
"Set own game mode to",
"Set the time to",
"Set the difficulty to",
"Teleported ",
"Set the weather to",
"Gamerule "
];
2024-11-07 13:30:02 -06:00
const respondFunc = async (username, message) => {
if (username === this.name) return;
2024-11-07 10:25:49 -06:00
try {
2023-12-26 14:42:45 -07:00
if (ignore_messages.some((m) => message.startsWith(m))) return;
2024-11-05 12:17:10 -06:00
this.shut_up = false;
2024-08-25 14:06:57 -05:00
2024-11-05 12:17:10 -06:00
console.log(this.name, 'received message from', username, ':', message);
2024-08-22 15:57:20 -05:00
2024-11-05 12:17:10 -06:00
if (isOtherAgent(username)) {
//recieveFromBot(username, message);
console.warn('recieved whisper from other bot??')
2024-11-05 12:17:10 -06:00
}
else {
let translation = await handleEnglishTranslation(message);
this.handleMessage(username, translation);
}
2024-11-07 10:25:49 -06:00
} catch (error) {
console.error('Error handling message:', error);
}
2024-11-07 13:30:02 -06:00
}
this.bot.on('whisper', respondFunc);
if (settings.profiles.length === 1)
this.bot.on('chat', respondFunc);
2024-11-02 10:46:04 -05:00
2024-11-07 10:25:49 -06:00
// Set up auto-eat
this.bot.autoEat.options = {
priority: 'foodPoints',
startAt: 14,
bannedFood: ["rotten_flesh", "spider_eye", "poisonous_potato", "pufferfish", "chicken"]
};
2023-12-12 21:35:39 -08:00
2024-11-07 10:25:49 -06:00
// Handle startup conditions
this._handleStartupConditions(save_data, init_message);
}
2023-12-26 14:42:45 -07:00
2024-11-07 10:25:49 -06:00
async _handleStartupConditions(save_data, init_message) {
try {
if (save_data?.self_prompt) {
let prompt = save_data.self_prompt;
2024-05-16 20:24:50 -05:00
// add initial message to history
this.history.add('system', prompt);
2024-11-07 10:25:49 -06:00
await this.self_prompter.start(prompt);
}
else if (save_data?.last_sender) {
this.last_sender = save_data.last_sender;
await this.handleMessage(this.last_sender, `(You have restarted and this message is auto-generated. Continue the conversation with ${this.last_sender})`);
}
else if (init_message) {
2024-11-07 10:25:49 -06:00
await this.handleMessage('system', init_message, 2);
}
else {
2024-10-02 01:01:22 -05:00
const translation = await handleTranslation("Hello world! I am "+this.name);
2024-10-02 00:57:28 -05:00
this.bot.chat(translation);
2023-12-12 21:35:39 -08:00
}
2024-11-07 10:25:49 -06:00
} catch (error) {
console.error('Error handling startup conditions:', error);
throw error;
}
2023-12-04 23:26:21 -08:00
}
2024-11-03 22:17:28 -06:00
requestInterrupt() {
this.bot.interrupt_code = true;
this.bot.collectBlock.cancelTask();
this.bot.pathfinder.stop();
this.bot.pvp.stop();
}
clearBotLogs() {
this.bot.output = '';
this.bot.interrupt_code = false;
}
2024-08-25 14:06:57 -05:00
2024-11-02 10:46:04 -05:00
async cleanChat(to_player, message, translate_up_to=-1) {
2024-11-05 12:17:10 -06:00
if (isOtherAgent(to_player)) {
this.bot.chat(message);
sendToBot(to_player, message);
return;
}
2024-10-02 01:01:22 -05:00
let to_translate = message;
2024-11-05 12:17:10 -06:00
let remaining = '';
2024-10-02 01:01:22 -05:00
if (translate_up_to != -1) {
to_translate = to_translate.substring(0, translate_up_to);
2024-11-05 12:17:10 -06:00
remaining = message.substring(translate_up_to);
2024-10-02 01:01:22 -05:00
}
2024-11-05 12:17:10 -06:00
message = (await handleTranslation(to_translate)).trim() + " " + remaining;
2024-01-24 17:24:52 -06:00
// newlines are interpreted as separate chats, which triggers spam filters. replace them with spaces
2024-10-02 00:57:28 -05:00
message = message.replaceAll('\n', ' ');
2024-11-02 10:46:04 -05:00
2024-11-05 12:17:10 -06:00
if (to_player === 'system' || to_player === this.name)
2024-11-02 10:46:04 -05:00
this.bot.chat(message);
else
this.bot.whisper(to_player, message);
2024-01-24 17:24:52 -06:00
}
2024-08-22 15:57:20 -05:00
shutUp() {
this.shut_up = true;
if (this.self_prompter.on) {
this.self_prompter.stop(false);
}
endAllChats();
2024-08-22 15:57:20 -05:00
}
async handleMessage(source, message, max_responses=null) {
if (!source || !message) {
console.warn('Received empty message from', source);
return false;
}
2024-04-30 23:07:07 -05:00
let used_command = false;
2024-08-22 15:57:20 -05:00
if (max_responses === null) {
max_responses = settings.max_commands === -1 ? Infinity : settings.max_commands;
}
if (max_responses === -1){
max_responses = Infinity;
}
2024-04-30 23:07:07 -05:00
2024-11-02 10:46:04 -05:00
const self_prompt = source === 'system' || source === this.name;
2024-11-05 12:17:10 -06:00
const from_other_bot = isOtherAgent(source);
2024-11-05 12:17:10 -06:00
if (!self_prompt && !from_other_bot) { // from user, check for forced commands
2024-04-30 23:07:07 -05:00
const user_command_name = containsCommand(message);
if (user_command_name) {
if (!commandExists(user_command_name)) {
this.bot.chat(`Command '${user_command_name}' does not exist.`);
return false;
}
this.bot.chat(`*${source} used ${user_command_name.substring(1)}*`);
if (user_command_name === '!newAction') {
2024-11-02 10:46:04 -05:00
// all user-initiated commands are ignored by the bot except for this one
2024-04-30 23:07:07 -05:00
// add the preceding message to the history to give context for newAction
2024-06-23 18:54:13 -05:00
this.history.add(source, message);
2024-04-30 23:07:07 -05:00
}
2024-06-23 18:54:13 -05:00
let execute_res = await executeCommand(this, message);
2024-04-30 23:07:07 -05:00
if (execute_res)
2024-11-02 10:46:04 -05:00
this.cleanChat(source, execute_res);
2024-04-30 23:07:07 -05:00
return true;
2024-01-13 12:10:15 -06:00
}
}
if (!self_prompt)
this.last_sender = source;
else
this.last_sender = null;
2024-11-05 15:44:40 -08:00
// Now translate the message
message = await handleEnglishTranslation(message);
console.log('received message from', source, ':', message);
2024-08-22 15:57:20 -05:00
const checkInterrupt = () => this.self_prompter.shouldInterrupt(self_prompt) || this.shut_up;
let behavior_log = this.bot.modes.flushBehaviorLog();
2024-09-29 13:35:15 -07:00
if (behavior_log.trim().length > 0) {
const MAX_LOG = 500;
if (behavior_log.length > MAX_LOG) {
2024-09-29 13:35:15 -07:00
behavior_log = '...' + behavior_log.substring(behavior_log.length - MAX_LOG);
}
behavior_log = 'Recent behaviors log: \n' + behavior_log.substring(behavior_log.indexOf('\n'));
await this.history.add('system', behavior_log);
}
2024-11-05 15:44:40 -08:00
// Handle other user messages
2024-11-05 12:17:10 -06:00
await this.history.add(source, message);
this.history.save();
2024-11-02 10:46:04 -05:00
if (!self_prompt && this.self_prompter.on) // message is from user during self-prompting
max_responses = 1; // force only respond to this message, then let self-prompting take over
for (let i=0; i<max_responses; i++) {
2024-08-22 15:57:20 -05:00
if (checkInterrupt()) break;
let history = this.history.getHistory();
let res = await this.prompter.promptConvo(history);
2023-12-04 23:26:21 -08:00
2024-01-03 22:16:50 -08:00
let command_name = containsCommand(res);
if (command_name) { // contains query or command
console.log(`Full response: ""${res}""`)
res = truncCommandMessage(res); // everything after the command is ignored
this.history.add(this.name, res);
2024-11-02 10:46:04 -05:00
2024-01-14 14:33:25 -06:00
if (!commandExists(command_name)) {
this.history.add('system', `Command ${command_name} does not exist.`);
console.warn('Agent hallucinated command:', command_name)
continue;
}
2024-08-22 15:57:20 -05:00
if (checkInterrupt()) break;
this.self_prompter.handleUserPromptedCmd(self_prompt, isAction(command_name));
if (settings.verbose_commands) {
2024-11-02 10:46:04 -05:00
this.cleanChat(source, res, res.indexOf(command_name));
}
else { // only output command name
2024-10-02 01:01:22 -05:00
let pre_message = res.substring(0, res.indexOf(command_name)).trim();
let chat_message = `*used ${command_name.substring(1)}*`;
if (pre_message.length > 0)
chat_message = `${pre_message} ${chat_message}`;
2024-11-02 10:46:04 -05:00
this.cleanChat(source, chat_message);
2024-04-30 23:07:07 -05:00
}
2024-01-11 15:59:52 -06:00
let execute_res = await executeCommand(this, res);
2024-01-03 22:16:50 -08:00
console.log('Agent executed:', command_name, 'and got:', execute_res);
2024-04-30 23:07:07 -05:00
used_command = true;
2024-01-03 22:16:50 -08:00
if (execute_res)
this.history.add('system', execute_res);
else
break;
2023-12-20 16:30:05 -08:00
}
else { // conversation response
this.history.add(this.name, res);
2024-11-02 10:46:04 -05:00
this.cleanChat(source, res);
2023-12-20 16:30:05 -08:00
console.log('Purely conversational response:', res);
break;
}
2024-11-02 10:46:04 -05:00
2024-08-22 15:57:20 -05:00
this.history.save();
2023-12-04 23:26:21 -08:00
}
2024-04-30 23:07:07 -05:00
return used_command;
}
2024-01-25 13:25:36 -08:00
startEvents() {
// Custom events
this.bot.on('time', () => {
if (this.bot.time.timeOfDay == 0)
this.bot.emit('sunrise');
else if (this.bot.time.timeOfDay == 6000)
this.bot.emit('noon');
else if (this.bot.time.timeOfDay == 12000)
this.bot.emit('sunset');
else if (this.bot.time.timeOfDay == 18000)
this.bot.emit('midnight');
});
2024-04-20 22:18:26 -05:00
let prev_health = this.bot.health;
this.bot.lastDamageTime = 0;
this.bot.lastDamageTaken = 0;
2024-01-25 13:25:36 -08:00
this.bot.on('health', () => {
2024-04-20 22:18:26 -05:00
if (this.bot.health < prev_health) {
this.bot.lastDamageTime = Date.now();
this.bot.lastDamageTaken = prev_health - this.bot.health;
}
prev_health = this.bot.health;
2024-01-25 13:25:36 -08:00
});
// Logging callbacks
2024-01-24 17:24:52 -06:00
this.bot.on('error' , (err) => {
console.error('Error event!', err);
});
this.bot.on('end', (reason) => {
console.warn('Bot disconnected! Killing agent process.', reason)
2024-05-20 00:52:08 -05:00
this.cleanKill('Bot disconnected! Killing agent process.');
});
this.bot.on('death', () => {
2024-11-03 12:04:47 -05:00
this.actions.cancelResume();
this.actions.stop();
});
2024-01-24 17:24:52 -06:00
this.bot.on('kicked', (reason) => {
console.warn('Bot kicked!', reason);
2024-05-20 00:52:08 -05:00
this.cleanKill('Bot kicked! Killing agent process.');
2024-01-24 17:24:52 -06:00
});
this.bot.on('messagestr', async (message, _, jsonMsg) => {
if (jsonMsg.translate && jsonMsg.translate.startsWith('death') && message.startsWith(this.name)) {
console.log('Agent died: ', message);
2024-10-27 20:35:02 +10:00
let death_pos = this.bot.entity.position;
2024-11-03 23:01:34 -06:00
this.memory_bank.rememberPlace('last_death_position', death_pos.x, death_pos.y, death_pos.z);
2024-10-27 20:35:02 +10:00
let death_pos_text = null;
if (death_pos) {
death_pos_text = `x: ${death_pos.x.toFixed(2)}, y: ${death_pos.y.toFixed(2)}, z: ${death_pos.x.toFixed(2)}`;
}
let dimention = this.bot.game.dimension;
this.handleMessage('system', `You died at position ${death_pos_text || "unknown"} in the ${dimention} dimension with the final message: '${message}'. Your place of death is saved as 'last_death_position' if you want to return. Previous actions were stopped and you have respawned.`);
}
2024-03-05 16:40:06 -08:00
});
2024-01-26 12:11:32 -08:00
this.bot.on('idle', () => {
2024-04-20 22:18:26 -05:00
this.bot.clearControlStates();
this.bot.pathfinder.stop(); // clear any lingering pathfinder
this.bot.modes.unPauseAll();
2024-11-03 12:04:47 -05:00
this.actions.resumeAction();
2024-01-26 12:11:32 -08:00
});
2024-03-05 12:05:46 -08:00
// Init NPC controller
this.npc.init();
2024-01-26 13:18:30 -06:00
// This update loop ensures that each update() is called one at a time, even if it takes longer than the interval
const INTERVAL = 300;
let last = Date.now();
2024-01-26 13:18:30 -06:00
setTimeout(async () => {
while (true) {
let start = Date.now();
await this.update(start - last);
2024-01-26 13:18:30 -06:00
let remaining = INTERVAL - (Date.now() - start);
if (remaining > 0) {
await new Promise((resolve) => setTimeout(resolve, remaining));
}
last = start;
2024-01-26 13:18:30 -06:00
}
}, INTERVAL);
2024-02-12 16:33:28 -08:00
this.bot.emit('idle');
}
async update(delta) {
await this.bot.modes.update();
await this.self_prompter.update(delta);
}
isIdle() {
2024-11-03 12:04:47 -05:00
return !this.actions.executing && !this.coder.generating;
}
2024-05-20 00:52:08 -05:00
cleanKill(msg='Killing agent process...') {
this.history.add('system', msg);
2024-11-02 10:46:04 -05:00
this.bot.chat('Restarting.')
this.history.save();
process.exit(1);
}
}