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-11 15:59:52 -06:00
|
|
|
import { containsCommand, executeCommand } from './commands.js';
|
2023-12-21 15:11:38 -07:00
|
|
|
import { Events } from './events.js';
|
2023-11-07 09:44:56 -06:00
|
|
|
|
|
|
|
|
|
|
|
export class Agent {
|
2023-12-21 20:42:01 -07:00
|
|
|
constructor(name, profile=null, init_message=null) {
|
2023-11-07 09:44:56 -06:00
|
|
|
this.name = name;
|
2023-11-07 12:00:55 -06:00
|
|
|
this.bot = initBot(name);
|
2023-12-21 20:42:01 -07:00
|
|
|
this.history = new History(this);
|
2023-11-07 12:00:55 -06:00
|
|
|
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 () => {
|
2023-11-07 09:44:56 -06:00
|
|
|
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-11-07 09:44:56 -06:00
|
|
|
});
|
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
|
|
|
|
2024-01-09 22:50:22 -06:00
|
|
|
const user_command_name = containsCommand(message);
|
|
|
|
if (user_command_name) {
|
|
|
|
this.bot.chat(`*${source} used ${user_command_name.substring(1)}*`);
|
2024-01-11 15:59:52 -06:00
|
|
|
let execute_res = await executeCommand(this, message);
|
2024-01-09 22:50:22 -06:00
|
|
|
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);
|
|
|
|
|
2024-01-09 22:50:22 -06:00
|
|
|
if (command_name) { // contains query or command
|
2024-01-03 22:16:50 -08:00
|
|
|
console.log('Query/Command response:', res);
|
|
|
|
|
2024-01-11 15:59:52 -06:00
|
|
|
let pre_message = res.substring(0, res.indexOf(command_name)).trim();
|
2024-01-03 22:16:50 -08:00
|
|
|
|
2024-01-11 15:59:52 -06:00
|
|
|
this.bot.chat(`${pre_message} *used ${command_name.substring(1)}*`);
|
|
|
|
let execute_res = await executeCommand(this, res);
|
2024-01-03 22:16:50 -08:00
|
|
|
|
2024-01-09 22:50:22 -06: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');
|
2023-11-07 09:44:56 -06:00
|
|
|
}
|
|
|
|
}
|