mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-04 06:15:32 +02:00
simplify profiles and events
This commit is contained in:
parent
4213208172
commit
85e7a508c2
7 changed files with 56 additions and 82 deletions
93
agent.js
93
agent.js
|
@ -29,70 +29,61 @@ 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) {
|
for (let i=0; i<5; i++) {
|
||||||
this.coder.queueCode(code);
|
let res = await sendRequest(this.history.getHistory(), this.history.getSystemMessage());
|
||||||
let code_return = await this.coder.execute();
|
this.history.add(this.name, res);
|
||||||
let message = code_return.message;
|
let query_cmd = containsQuery(res);
|
||||||
if (code_return.interrupted)
|
if (query_cmd) { // contains query
|
||||||
return;
|
let message = res.substring(0, res.indexOf(query_cmd)).trim();
|
||||||
if (!code_return.success)
|
if (message)
|
||||||
message += "\n Write code to fix the problem and try again.";
|
this.bot.chat(message);
|
||||||
console.log('code return:', message);
|
let query = getQuery(query_cmd);
|
||||||
this.history.add('system', message);
|
let query_res = query.perform(this);
|
||||||
if (!code_return.success)
|
console.log('Agent used query:', query_cmd, 'and got:', query_res)
|
||||||
await this.handleMessage(triesRemaining-1);
|
this.history.add('system', query_res);
|
||||||
}
|
}
|
||||||
}
|
else if (containsCodeBlock(res)) { // contains code block
|
||||||
|
console.log('Agent is executing code:', res)
|
||||||
|
|
||||||
async handleMessage(triesRemaining=5) {
|
let message = res.substring(0, res.indexOf('```')).trim();
|
||||||
if (triesRemaining == 0) {
|
if (message)
|
||||||
console.log('Quitting response loop.');
|
this.bot.chat(message);
|
||||||
return;
|
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.history.save();
|
||||||
this.bot.emit('finished_executing');
|
this.bot.emit('finished_executing');
|
||||||
}
|
}
|
||||||
|
|
|
@ -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": []
|
||||||
}
|
}
|
|
@ -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": []
|
|
||||||
}
|
|
|
@ -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)
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue