mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-09-06 18:12:57 +02:00
refactored init code
This commit is contained in:
parent
597e7d02b8
commit
663f7f9b1a
4 changed files with 45 additions and 43 deletions
48
agent.js
48
agent.js
|
@ -7,45 +7,43 @@ import { containsCodeBlock } from './utils/skill-library.js';
|
|||
|
||||
|
||||
export class Agent {
|
||||
constructor(name, save_path, restart_memory=false) {
|
||||
constructor(name, save_path, clear_memory=false, autostart=false) {
|
||||
this.name = name;
|
||||
this.bot = initBot(name);
|
||||
this.history = new History(this, save_path);
|
||||
this.history.loadExamples();
|
||||
this.coder = new Coder(this);
|
||||
|
||||
if (!restart_memory) {
|
||||
if (!clear_memory) {
|
||||
this.history.load();
|
||||
}
|
||||
|
||||
|
||||
this.bot.on('login', () => {
|
||||
this.bot.chat('Hello world! I am ' + this.name);
|
||||
console.log(`${this.name} logged in.`);
|
||||
if (!restart_memory) {
|
||||
|
||||
this.bot.on('chat', (username, message) => {
|
||||
if (username === this.name) return;
|
||||
console.log('received message from', username, ':', message);
|
||||
|
||||
this.respond(username, message);
|
||||
this.history.save();
|
||||
});
|
||||
|
||||
this.bot.on('finished_executing', () => {
|
||||
setTimeout(() => {
|
||||
if (!this.coder.executing) {
|
||||
// return to default behavior
|
||||
}
|
||||
}, 10000);
|
||||
});
|
||||
|
||||
if (autostart)
|
||||
this.respond('system', 'Agent process restarted. Notify the user and decide what to do.');
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
async start() {
|
||||
await this.history.loadExamples();
|
||||
|
||||
this.bot.on('chat', (username, message) => {
|
||||
if (username === this.name) return;
|
||||
console.log('received message from', username, ':', message);
|
||||
|
||||
this.respond(username, message);
|
||||
this.history.save();
|
||||
});
|
||||
|
||||
this.bot.on('finished_executing', () => {
|
||||
setTimeout(() => {
|
||||
if (!this.coder.executing) {
|
||||
// return to default behavior
|
||||
}
|
||||
}, 10000);
|
||||
})
|
||||
}
|
||||
|
||||
async respond(username, message) {
|
||||
await this.history.add(username, message);
|
||||
for (let i=0; i<5; i++) {
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
import { spawn } from 'child_process';
|
||||
|
||||
export class AgentController {
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
}
|
||||
async start(restart_memory=true) {
|
||||
let args = ['controller/init-agent.js', this.name];
|
||||
if (restart_memory)
|
||||
args.push('-r');
|
||||
export class AgentProcess {
|
||||
constructor(name, clear_memory=false, autostart=false) {
|
||||
let args = ['controller/init-agent.js', name];
|
||||
if (clear_memory)
|
||||
args.push('-c');
|
||||
if (autostart)
|
||||
args.push('-a');
|
||||
|
||||
const agentProcess = spawn('node', args, {
|
||||
stdio: 'inherit',
|
||||
|
@ -25,7 +24,7 @@ export class AgentController {
|
|||
process.exit(1);
|
||||
}
|
||||
console.log('Restarting agent...');
|
||||
this.start(false);
|
||||
this.start(false, true);
|
||||
last_restart = Date.now();
|
||||
}
|
||||
});
|
|
@ -3,20 +3,25 @@ import yargs from 'yargs';
|
|||
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length < 1) {
|
||||
console.log('Usage: node init_agent.js <agent_name> [options]');
|
||||
console.log('Usage: node init_agent.js <agent_name> [-c] [-a]');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const argv = yargs(args)
|
||||
.option('restart_memory', {
|
||||
alias: 'r',
|
||||
.option('clear_memory', {
|
||||
alias: 'c',
|
||||
type: 'boolean',
|
||||
description: 'restart memory from scratch'
|
||||
}).argv;
|
||||
})
|
||||
.option('autostart', {
|
||||
alias: 'a',
|
||||
type: 'boolean',
|
||||
description: 'automatically prompt the agent on startup'
|
||||
}).argv
|
||||
|
||||
const name = argv._[0];
|
||||
const restart_memory = !!argv.restart_memory;
|
||||
const clear_memory = !!argv.clear_memory;
|
||||
const autostart = !!argv.autostart;
|
||||
const save_path = './bots/'+name+'.json';
|
||||
|
||||
let agent = new Agent(name, save_path, restart_memory);
|
||||
agent.start();
|
||||
new Agent(name, save_path, clear_memory, autostart);
|
||||
|
|
4
main.js
4
main.js
|
@ -1,3 +1,3 @@
|
|||
import { AgentController } from './controller/agent-controller.js';
|
||||
import { AgentProcess } from './controller/agent-process.js';
|
||||
|
||||
new AgentController('andy').start();
|
||||
new AgentProcess('andy', true, false);
|
Loading…
Add table
Reference in a new issue