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,44 +29,21 @@ export class Agent {
if (username === this.name) return; if (username === this.name) return;
console.log('received message from', username, ':', message); console.log('received message from', username, ':', message);
this.history.add(username, message); this.handleMessage(username, message);
this.handleMessage();
}); });
if (init_message) { if (init_message) {
this.history.add('system', init_message); this.handleMessage('system', init_message);
this.handleMessage();
} else { } else {
this.bot.emit('finished_executing'); this.bot.emit('finished_executing');
} }
}); });
} }
async executeCode(code, triesRemaining=5) { async handleMessage(source, message) {
if (code == 'default') await this.history.add(source, message);
code = this.history.default;
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);
}
}
async handleMessage(triesRemaining=5) {
if (triesRemaining == 0) {
console.log('Quitting response loop.');
return;
}
for (let i=0; i<5; i++) {
let res = await sendRequest(this.history.getHistory(), this.history.getSystemMessage()); let res = await sendRequest(this.history.getHistory(), this.history.getSystemMessage());
this.history.add(this.name, res); this.history.add(this.name, res);
let query_cmd = containsQuery(res); let query_cmd = containsQuery(res);
@ -78,7 +55,6 @@ export class Agent {
let query_res = query.perform(this); let query_res = query.perform(this);
console.log('Agent used query:', query_cmd, 'and got:', query_res) console.log('Agent used query:', query_cmd, 'and got:', query_res)
this.history.add('system', query_res); this.history.add('system', query_res);
await this.handleMessage(triesRemaining-1);
} }
else if (containsCodeBlock(res)) { // contains code block else if (containsCodeBlock(res)) { // contains code block
console.log('Agent is executing code:', res) console.log('Agent is executing code:', res)
@ -87,12 +63,27 @@ export class Agent {
if (message) if (message)
this.bot.chat(message); this.bot.chat(message);
let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```')); let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```'));
await this.executeCode(code, triesRemaining);
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 else { // conversation response
this.bot.chat(res); this.bot.chat(res);
console.log('Purely conversational response:', res) console.log('Purely conversational response:', res);
break;
} }
}
this.history.save(); this.history.save();
this.bot.emit('finished_executing'); this.bot.emit('finished_executing');
} }

View file

@ -2,12 +2,6 @@
"name": "andy", "name": "andy",
"bio": "You are playing minecraft and assisting other players in tasks.", "bio": "You are playing minecraft and assisting other players in tasks.",
"memory": "", "memory": "",
"events": [ "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));"
]
],
"turns": [] "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) { constructor(name) {
this.name = 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]; let args = ['controller/init-agent.js', this.name];
args.push('-p', profile); args.push('-p', profile);
if (clear_memory) if (clear_memory)

View file

@ -26,8 +26,7 @@ const argv = yargs(args)
const name = argv._[0]; const name = argv._[0];
const save_path = './bots/'+name+'.json'; const save_path = './bots/'+name+'.json';
const profile = argv.profile; const load_path = !!argv.clear_memory ? './bots/'+argv.profile+'.json' : save_path;
const load_path = !!argv.clear_memory ? './bots/'+profile+'.json' : save_path;
const init_message = !!argv.autostart ? 'Agent process restarted. Notify the user and decide what to do.' : null; 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); 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 interrupted = this.agent.bot.interrupt_code;
let timedout = this.timedout; let timedout = this.timedout;
this.clear(); this.clear();
this.agent.bot.emit("code_terminated");
return {success:true, message: output, interrupted, timedout}; return {success:true, message: output, interrupted, timedout};
} catch (err) { } catch (err) {
this.executing = false; this.executing = false;
@ -110,6 +111,7 @@ export class Coder {
message += '!!Code threw exception!! Error: ' + err; message += '!!Code threw exception!! Error: ' + err;
let interrupted = this.agent.bot.interrupt_code; let interrupted = this.agent.bot.interrupt_code;
await this.stop(); await this.stop();
this.agent.bot.emit("code_terminated");
return {success: false, message, interrupted, timedout: false}; return {success: false, message, interrupted, timedout: false};
} }
} }

View file

@ -29,8 +29,12 @@ export class Events {
}); });
} }
executeCode(agent, code) { async executeCode(agent, code) {
agent.executeCode(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) { sendThought(agent, message) {
@ -38,4 +42,7 @@ export class Events {
agent.handleMessage(); agent.handleMessage();
} }
sendChat(agent, message) {
agent.bot.chat(message);
}
} }