mindcraft/src/agent/agent.js

99 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-12-21 15:11:38 -07:00
import { initBot } from '../utils/mcdata.js';
import { sendRequest } from '../utils/gpt.js';
import { History } from './history.js';
import { Coder } from './coder.js';
2024-01-03 22:16:50 -08:00
import { getCommand, containsCommand } from './commands.js';
2023-12-21 15:11:38 -07:00
import { Events } from './events.js';
export class Agent {
2023-12-21 20:42:01 -07:00
constructor(name, profile=null, init_message=null) {
this.name = name;
this.bot = initBot(name);
2023-12-21 20:42:01 -07:00
this.history = new History(this);
this.coder = new Coder(this);
2023-11-19 15:34:53 -06:00
2023-12-21 20:42:01 -07:00
this.history.load(profile);
2023-12-04 23:26:21 -08:00
2023-12-12 21:35:39 -08:00
this.events = new Events(this, this.history.events)
2023-12-04 23:26:21 -08:00
2023-12-26 14:42:45 -07:00
this.bot.on('login', async () => {
this.bot.chat('Hello world! I am ' + this.name);
console.log(`${this.name} logged in.`);
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 "
];
2023-12-10 20:18:20 -06:00
this.bot.on('chat', (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;
2023-12-12 21:35:39 -08:00
console.log('received message from', username, ':', message);
2023-12-10 20:18:20 -06:00
2023-12-20 16:30:05 -08:00
this.handleMessage(username, message);
2023-12-12 21:35:39 -08:00
});
2023-12-26 14:42:45 -07:00
await this.history.loadExamples();
2023-12-12 21:35:39 -08:00
if (init_message) {
2023-12-20 16:30:05 -08:00
this.handleMessage('system', init_message);
2023-12-12 21:35:39 -08:00
} else {
this.bot.emit('finished_executing');
}
});
2023-12-04 23:26:21 -08:00
}
2023-12-20 16:30:05 -08:00
async handleMessage(source, message) {
2023-12-26 14:42:45 -07:00
if (!!source && !!message)
await this.history.add(source, message);
2023-12-04 23:26:21 -08:00
const user_command_name = containsCommand(message);
if (user_command_name) {
this.bot.chat(`*${source} used ${user_command_name.substring(1)}*`);
let execute_res = await getCommand(user_command_name).perform(this);
if (execute_res)
this.bot.chat(execute_res);
else
this.bot.chat('Finished command.');
return;
}
2023-12-20 16:30:05 -08:00
for (let i=0; i<5; i++) {
let res = await sendRequest(this.history.getHistory(), this.history.getSystemMessage());
this.history.add(this.name, res);
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
2024-01-03 22:16:50 -08:00
console.log('Query/Command response:', res);
let message = res.substring(0, res.indexOf(command_name)).trim();
2024-01-03 22:16:50 -08:00
this.bot.chat(`${message} *used ${command_name.substring(1)}*`);
let execute_res = await getCommand(command_name).perform(this);
2024-01-03 22:16:50 -08:00
console.log('Agent executed:', command_name, 'and got:', execute_res);
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.bot.chat(res);
console.log('Purely conversational response:', res);
break;
}
2023-12-04 23:26:21 -08:00
}
this.history.save();
2023-12-16 12:08:47 -06:00
this.bot.emit('finished_executing');
}
}