diff --git a/agent.js b/agent.js index f1c6bdb..0f9ae95 100644 --- a/agent.js +++ b/agent.js @@ -3,7 +3,7 @@ import { sendRequest } from './utils/gpt.js'; import { History } from './utils/history.js'; import { Coder } from './utils/coder.js'; import { getQuery, containsQuery } from './utils/queries.js'; -import { containsCodeBlock } from './utils/skill_library.js'; +import { containsCodeBlock } from './utils/skill-library.js'; export class Agent { @@ -20,6 +20,9 @@ export class Agent { this.bot.on('login', () => { this.bot.chat('Hello world! I am ' + this.name); console.log(`${this.name} logged in.`); + if (!restart_memory) { + this.respond('system', 'Agent process restarted. Notify the user and decide what to do.'); + } }); } diff --git a/controller.js b/controller/agent-controller.js similarity index 54% rename from controller.js rename to controller/agent-controller.js index 9db3a83..382421a 100644 --- a/controller.js +++ b/controller/agent-controller.js @@ -1,25 +1,32 @@ import { spawn } from 'child_process'; -class AgentController { +export class AgentController { constructor(name) { this.name = name; } - async start(restart_memory=false) { - let args = ['init_agent.js', this.name]; + async start(restart_memory=true) { + let args = ['controller/init-agent.js', this.name]; if (restart_memory) args.push('-r'); + const agentProcess = spawn('node', args, { stdio: 'inherit', stderr: 'inherit', }); - + + let last_restart = Date.now(); agentProcess.on('exit', (code, signal) => { console.log(`Agent process exited with code ${code} and signal ${signal}`); - // Restart the agent if it exited due to an error if (code !== 0) { + // agent must run for at least 30 seconds before restarting + if (Date.now() - last_restart < 30 * 1000) { + console.error('Agent process exited too quickly. Killing entire process. Goodbye.'); + process.exit(1); + } console.log('Restarting agent...'); - this.start(); + this.start(false); + last_restart = Date.now(); } }); @@ -27,6 +34,4 @@ class AgentController { console.error('Failed to start agent process:', err); }); } -} - -new AgentController('andy').start(); \ No newline at end of file +} \ No newline at end of file diff --git a/init_agent.js b/controller/init-agent.js similarity index 93% rename from init_agent.js rename to controller/init-agent.js index 43a0615..465ceb2 100644 --- a/init_agent.js +++ b/controller/init-agent.js @@ -1,4 +1,4 @@ -import { Agent } from './agent.js'; +import { Agent } from '../agent.js'; import yargs from 'yargs'; const args = process.argv.slice(2); diff --git a/main.js b/main.js new file mode 100644 index 0000000..d601ded --- /dev/null +++ b/main.js @@ -0,0 +1,3 @@ +import { AgentController } from './controller/agent-controller.js'; + +new AgentController('andy').start(); \ No newline at end of file diff --git a/utils/coder.js b/utils/coder.js index 5bc029c..5ea4341 100644 --- a/utils/coder.js +++ b/utils/coder.js @@ -95,6 +95,7 @@ export class Coder { await execution_file.main(this.agent.bot); // open fire clearTimeout(TIMEOUT); this.executing = false; + this.agent.bot.emit('finished_executing'); let output = this.formatOutput(this.agent.bot); let interrupted = this.agent.bot.interrupt_code; @@ -156,7 +157,8 @@ export class Coder { await new Promise(resolve => setTimeout(resolve, 5 * 1000)); // wait 5 seconds if (this.executing) { console.error(`Failed to stop. Killing process. Goodbye.`); - this.agent.bot.output += `\nForce stop failed! Killing bot.`; + this.agent.bot.output += `\nForce stop failed! Process was killed and will be restarted. Goodbye world.`; + this.bot.chat('Goodbye world.'); let output = this.formatOutput(this.agent.bot); this.agent.history.add('system', output); this.agent.history.save(); diff --git a/utils/history.js b/utils/history.js index b14a909..abaa323 100644 --- a/utils/history.js +++ b/utils/history.js @@ -1,6 +1,6 @@ import { writeFileSync, readFileSync, mkdirSync } from 'fs'; import { getQueryDocs } from './queries.js'; -import { getSkillDocs } from './skill_library.js'; +import { getSkillDocs } from './skill-library.js'; import { sendRequest, embed, cosineSimilarity } from './gpt.js'; diff --git a/utils/skill_library.js b/utils/skill-library.js similarity index 100% rename from utils/skill_library.js rename to utils/skill-library.js