diff --git a/agent.js b/agent.js index 0f9ae95..7263ad6 100644 --- a/agent.js +++ b/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++) { diff --git a/controller/agent-controller.js b/controller/agent-process.js similarity index 76% rename from controller/agent-controller.js rename to controller/agent-process.js index 382421a..cfeb159 100644 --- a/controller/agent-controller.js +++ b/controller/agent-process.js @@ -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(); } }); diff --git a/controller/init-agent.js b/controller/init-agent.js index 465ceb2..e632064 100644 --- a/controller/init-agent.js +++ b/controller/init-agent.js @@ -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 [options]'); + console.log('Usage: node init_agent.js [-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); diff --git a/main.js b/main.js index d601ded..9ed8af8 100644 --- a/main.js +++ b/main.js @@ -1,3 +1,3 @@ -import { AgentController } from './controller/agent-controller.js'; +import { AgentProcess } from './controller/agent-process.js'; -new AgentController('andy').start(); \ No newline at end of file +new AgentProcess('andy', true, false); \ No newline at end of file