mindcraft/agent.js

91 lines
3.3 KiB
JavaScript
Raw Normal View History

import { initBot } from './utils/mcdata.js';
import { sendRequest } from './utils/gpt.js';
import { History } from './utils/history.js';
import { Coder } from './utils/coder.js';
2023-11-13 20:33:34 -08:00
import { getQuery, containsQuery } from './utils/queries.js';
2023-12-09 21:40:53 -06:00
import { containsCodeBlock } from './utils/skill-library.js';
2023-12-12 19:50:52 -08:00
import { Events } from './utils/events.js';
export class Agent {
2023-12-12 21:35:39 -08:00
constructor(name, save_path, load_path=null, init_message=null) {
this.name = name;
this.bot = initBot(name);
2023-11-15 13:18:38 -08:00
this.history = new History(this, save_path);
2023-12-10 20:18:20 -06:00
this.history.loadExamples();
this.coder = new Coder(this);
2023-11-19 15:34:53 -06:00
2023-12-12 21:35:39 -08:00
if (load_path) {
this.history.load(load_path);
2023-11-19 15:34:53 -06:00
}
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
this.bot.on('login', () => {
this.bot.chat('Hello world! I am ' + this.name);
console.log(`${this.name} logged in.`);
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;
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
});
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) {
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);
let query_cmd = containsQuery(res);
if (query_cmd) { // contains query
let message = res.substring(0, res.indexOf(query_cmd)).trim();
if (message)
this.bot.chat(message);
let query = getQuery(query_cmd);
let query_res = query.perform(this);
console.log('Agent used query:', query_cmd, 'and got:', query_res)
this.history.add('system', query_res);
}
else if (containsCodeBlock(res)) { // contains code block
console.log('Agent is executing code:', res)
2023-12-04 23:26:21 -08:00
2023-12-20 16:30:05 -08:00
let message = res.substring(0, res.indexOf('```')).trim();
if (message)
this.bot.chat(message);
let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```'));
2023-12-04 23:26:21 -08:00
2023-12-20 16:30:05 -08:00
if (code) {
this.coder.queueCode(code);
let code_return = await this.coder.execute();
let message = code_return.message;
if (code_return.interrupted && !code_return.timedout)
break;
if (!code_return.success) {
message += "\nWrite code to fix the problem and try again.";
}
console.log('code return:', message);
this.history.add('system', message);
}
}
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');
}
}