simplify profiles and events

This commit is contained in:
Kolby Nottingham 2023-12-20 16:30:05 -08:00
parent 4213208172
commit 85e7a508c2
7 changed files with 56 additions and 82 deletions

View file

@ -29,70 +29,61 @@ export class Agent {
if (username === this.name) return;
console.log('received message from', username, ':', message);
this.history.add(username, message);
this.handleMessage();
this.handleMessage(username, message);
});
if (init_message) {
this.history.add('system', init_message);
this.handleMessage();
this.handleMessage('system', init_message);
} else {
this.bot.emit('finished_executing');
}
});
}
async executeCode(code, triesRemaining=5) {
if (code == 'default')
code = this.history.default;
async handleMessage(source, message) {
await this.history.add(source, message);
if (code) {
this.coder.queueCode(code);
let code_return = await this.coder.execute();
let message = code_return.message;
if (code_return.interrupted)
return;
if (!code_return.success)
message += "\n Write code to fix the problem and try again.";
console.log('code return:', message);
this.history.add('system', message);
if (!code_return.success)
await this.handleMessage(triesRemaining-1);
}
}
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)
async handleMessage(triesRemaining=5) {
if (triesRemaining == 0) {
console.log('Quitting response loop.');
return;
let message = res.substring(0, res.indexOf('```')).trim();
if (message)
this.bot.chat(message);
let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```'));
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;
}
}
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);
await this.handleMessage(triesRemaining-1);
}
else if (containsCodeBlock(res)) { // contains code block
console.log('Agent is executing code:', res)
let message = res.substring(0, res.indexOf('```')).trim();
if (message)
this.bot.chat(message);
let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```'));
await this.executeCode(code, triesRemaining);
}
else { // conversation response
this.bot.chat(res);
console.log('Purely conversational response:', res)
}
this.history.save();
this.bot.emit('finished_executing');
}

View file

@ -2,12 +2,6 @@
"name": "andy",
"bio": "You are playing minecraft and assisting other players in tasks.",
"memory": "",
"events": [
[
"finished_executing",
"executeCode",
"let blocks = world.getNearbyBlockTypes(bot, 4);\nlet block_type = blocks[Math.floor(Math.random() * blocks.length)];\nawait skills.collectBlock(bot, block_type);\nawait new Promise(r => setTimeout(r, 1000));\nlet players = world.getNearbyPlayerNames(bot);\nlet player_name = players[Math.floor(Math.random() * players.length)];\nawait skills.goToPlayer(bot, player_name);\nawait new Promise(r => setTimeout(r, 1000));"
]
],
"events": [],
"turns": []
}

View file

@ -1,19 +0,0 @@
{
"name": "andy",
"bio": "You are playing minecraft. Your goal is to collect materials and survive for as long as possible. Follow instructions from other players when appropriate.",
"memory": "",
"default": "",
"events": [
[
"finished_executing",
"sendThought",
"I need keep collecting and crafting to survive. I should plan what to do next and execute it."
],
[
"damaged",
"sendThought",
"I may be under attack or need to eat! I will stop what I am doing to check my health and take action."
]
],
"turns": []
}

View file

@ -4,7 +4,7 @@ export class AgentProcess {
constructor(name) {
this.name = name;
}
start(clear_memory=false, autostart=false, profile='survive') {
start(clear_memory=false, autostart=false, profile='assist') {
let args = ['controller/init-agent.js', this.name];
args.push('-p', profile);
if (clear_memory)

View file

@ -26,8 +26,7 @@ const argv = yargs(args)
const name = argv._[0];
const save_path = './bots/'+name+'.json';
const profile = argv.profile;
const load_path = !!argv.clear_memory ? './bots/'+profile+'.json' : save_path;
const load_path = !!argv.clear_memory ? './bots/'+argv.profile+'.json' : save_path;
const init_message = !!argv.autostart ? 'Agent process restarted. Notify the user and decide what to do.' : null;
new Agent(name, save_path, load_path, init_message);

View file

@ -100,6 +100,7 @@ export class Coder {
let interrupted = this.agent.bot.interrupt_code;
let timedout = this.timedout;
this.clear();
this.agent.bot.emit("code_terminated");
return {success:true, message: output, interrupted, timedout};
} catch (err) {
this.executing = false;
@ -110,6 +111,7 @@ export class Coder {
message += '!!Code threw exception!! Error: ' + err;
let interrupted = this.agent.bot.interrupt_code;
await this.stop();
this.agent.bot.emit("code_terminated");
return {success: false, message, interrupted, timedout: false};
}
}

View file

@ -29,8 +29,12 @@ export class Events {
});
}
executeCode(agent, code) {
agent.executeCode(code);
async executeCode(agent, code) {
console.log('responding to event with code.');
agent.coder.queueCode(code);
let code_return = await agent.coder.execute();
console.log('code return:', code_return.message);
agent.history.add('system', code_return.message);
}
sendThought(agent, message) {
@ -38,4 +42,7 @@ export class Events {
agent.handleMessage();
}
sendChat(agent, message) {
agent.bot.chat(message);
}
}