mindcraft/src/agent/agent.js

324 lines
13 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-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';
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';
export class Agent {
async start(profile_fp, load_mem=false, init_message=null, count_id=0) {
this.prompter = new Prompter(this, profile_fp);
this.name = this.prompter.getName();
2023-12-21 20:42:01 -07:00
this.history = new History(this);
this.coder = new Coder(this);
2024-03-05 12:05:46 -08:00
this.npc = new NPCContoller(this);
this.memory_bank = new MemoryBank();
this.self_prompter = new SelfPrompter(this);
2023-11-19 15:34:53 -06:00
await this.prompter.initExamples();
2024-01-30 16:43:30 -06:00
console.log('Logging in...');
this.bot = initBot(this.name);
2023-12-04 23:26:21 -08:00
initModes(this);
2023-12-04 23:26:21 -08:00
let save_data = null;
if (load_mem) {
save_data = this.history.load();
}
2024-04-20 22:18:26 -05:00
2024-04-02 19:19:30 -05:00
this.bot.once('spawn', async () => {
addViewer(this.bot, count_id);
2024-04-20 22:18:26 -05:00
// wait for a bit so stats are not undefined
await new Promise((resolve) => setTimeout(resolve, 1000));
2024-04-02 19:19:30 -05:00
console.log(`${this.name} spawned.`);
2024-01-15 11:04:50 -06:00
this.coder.clear();
2023-12-26 14:42:45 -07:00
const ignore_messages = [
"Set own game mode to",
"Set the time to",
"Set the difficulty to",
"Teleported ",
"Set the weather to",
"Gamerule "
];
2024-05-29 21:33:29 -05:00
const eventname = settings.profiles.length > 1 ? 'whisper' : 'chat';
2024-08-25 14:06:57 -05:00
this.bot.on(eventname, async (username, message) => {
2023-12-12 21:35:39 -08:00
if (username === this.name) return;
2023-12-26 14:42:45 -07:00
if (ignore_messages.some((m) => message.startsWith(m))) return;
let translation = await handleEnglishTranslation(message);
2024-08-25 14:06:57 -05:00
console.log('received message from', username, ':', translation);
2024-08-22 15:57:20 -05:00
this.shut_up = false;
2023-12-10 20:18:20 -06:00
2024-08-25 14:06:57 -05:00
this.handleMessage(username, translation);
2023-12-12 21:35:39 -08:00
});
2024-01-14 14:33:25 -06:00
// set the bot to automatically eat food when hungry
this.bot.autoEat.options = {
priority: 'foodPoints',
startAt: 14,
bannedFood: ["rotten_flesh", "spider_eye", "poisonous_potato", "pufferfish", "chicken"]
2024-01-14 14:33:25 -06:00
};
2023-12-26 14:42:45 -07:00
2024-05-10 12:18:47 -05:00
if (save_data && save_data.self_prompt) { // if we're loading memory and self-prompting was on, restart it, ignore init_message
let prompt = save_data.self_prompt;
2024-05-16 20:24:50 -05:00
// add initial message to history
this.history.add('system', prompt);
this.self_prompter.start(prompt);
}
else if (init_message) {
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
this.bot.emit('finished_executing');
}
2024-01-25 13:25:36 -08:00
this.startEvents();
});
2023-12-04 23:26:21 -08:00
}
2024-08-25 14:06:57 -05:00
2024-10-02 00:57:28 -05:00
async cleanChat(message, translate_up_to=-1) {
2024-10-02 01:01:22 -05:00
let to_translate = message;
let remainging = '';
if (translate_up_to != -1) {
to_translate = to_translate.substring(0, translate_up_to);
remainging = message.substring(translate_up_to);
}
message = (await handleTranslation(to_translate)).trim() + " " + remainging;
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', ' ');
return this.bot.chat(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);
}
}
async handleMessage(source, message, max_responses=null) {
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
let self_prompt = source === 'system' || source === this.name;
if (!self_prompt) {
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') {
// all user initiated commands are ignored by the bot except for this one
// 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)
this.cleanChat(execute_res);
return true;
2024-01-13 12:10:15 -06:00
}
}
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);
}
await this.history.add(source, message);
this.history.save();
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-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;
}
if (command_name === '!stopSelfPrompt' && self_prompt) {
this.history.add('system', `Cannot stopSelfPrompt unless requested by user.`);
2024-01-14 14:33:25 -06:00
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-10-02 00:57:28 -05:00
this.cleanChat(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}`;
this.cleanChat(res);
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-01-24 17:24:52 -06:00
this.cleanChat(res);
2023-12-20 16:30:05 -08:00
console.log('Purely conversational response:', res);
break;
}
2024-08-22 15:57:20 -05:00
this.history.save();
2023-12-04 23:26:21 -08:00
}
2023-12-16 12:08:47 -06:00
this.bot.emit('finished_executing');
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-02-05 13:48:02 -06:00
this.coder.cancelResume();
this.coder.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;
this.memory_bank.rememberPlace('last death position', death_pos);
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 inventory = this.bot.inventory.slots;
let death_items = "";
for (let i=0; i<inventory.length; i++) {
if (inventory[i]) {
death_items += `${inventory[i].name} x${inventory[i].count}, `;
}
}
this.history.add('system', `You died at position ${death_pos_text || "unknown"} with the final message: '${message}'. Previous actions were stopped and you have respawned. Notify the user and perform any necessary actions.`);
this.handleMessage('system', `You died at position ${death_pos_text || "unknown"} and droped the items ${death_items} at your death position with the final message: '${message}'. Previous actions were stopped and you have respawned. Notify the user and perform any necessary actions. you can retrieve your items by doing the action !goToDeath.`);
}
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-03-05 12:05:46 -08:00
this.coder.executeResume();
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() {
return !this.coder.executing && !this.coder.generating;
}
2024-05-20 00:52:08 -05:00
cleanKill(msg='Killing agent process...') {
this.history.add('system', msg);
2024-05-20 00:52:08 -05:00
this.bot.chat('Goodbye world.')
this.history.save();
process.exit(1);
}
}