refactor/restart improvements

This commit is contained in:
MaxRobinsonTheGreat 2023-12-09 21:40:53 -06:00
parent e2aad3df9c
commit 597e7d02b8
7 changed files with 26 additions and 13 deletions

View file

@ -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.');
}
});
}

View file

@ -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();
}

View file

@ -1,4 +1,4 @@
import { Agent } from './agent.js';
import { Agent } from '../agent.js';
import yargs from 'yargs';
const args = process.argv.slice(2);

3
main.js Normal file
View file

@ -0,0 +1,3 @@
import { AgentController } from './controller/agent-controller.js';
new AgentController('andy').start();

View file

@ -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();

View file

@ -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';