mindcraft/src/agent/agent.js

93 lines
3.1 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';
import { getQuery, containsQuery } from './queries.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
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 query_name = containsQuery(res);
let command_name = containsCommand(res);
if (query_name || command_name) { // contains query or command
console.log('Query/Command response:', res);
let execute_name = query_name ? query_name : command_name;
let message = res.substring(0, res.indexOf(execute_name)).trim();
if (message)
2023-12-20 16:30:05 -08:00
this.bot.chat(message);
2024-01-03 22:16:50 -08:00
let execute_func = query_name ? getQuery(query_name) : getCommand(command_name);
let execute_res = await execute_func.perform(this);
console.log('Agent executed:', execute_name, 'and got:', execute_res);
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');
}
}