refactored init code

This commit is contained in:
MaxRobinsonTheGreat 2023-12-10 20:18:20 -06:00
parent 597e7d02b8
commit 663f7f9b1a
4 changed files with 45 additions and 43 deletions

View file

@ -7,45 +7,43 @@ import { containsCodeBlock } from './utils/skill-library.js';
export class Agent { export class Agent {
constructor(name, save_path, restart_memory=false) { constructor(name, save_path, clear_memory=false, autostart=false) {
this.name = name; this.name = name;
this.bot = initBot(name); this.bot = initBot(name);
this.history = new History(this, save_path); this.history = new History(this, save_path);
this.history.loadExamples();
this.coder = new Coder(this); this.coder = new Coder(this);
if (!restart_memory) { if (!clear_memory) {
this.history.load(); this.history.load();
} }
this.bot.on('login', () => { this.bot.on('login', () => {
this.bot.chat('Hello world! I am ' + this.name); this.bot.chat('Hello world! I am ' + this.name);
console.log(`${this.name} logged in.`); 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.'); 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) { async respond(username, message) {
await this.history.add(username, message); await this.history.add(username, message);
for (let i=0; i<5; i++) { for (let i=0; i<5; i++) {

View file

@ -1,13 +1,12 @@
import { spawn } from 'child_process'; import { spawn } from 'child_process';
export class AgentController { export class AgentProcess {
constructor(name) { constructor(name, clear_memory=false, autostart=false) {
this.name = name; let args = ['controller/init-agent.js', name];
} if (clear_memory)
async start(restart_memory=true) { args.push('-c');
let args = ['controller/init-agent.js', this.name]; if (autostart)
if (restart_memory) args.push('-a');
args.push('-r');
const agentProcess = spawn('node', args, { const agentProcess = spawn('node', args, {
stdio: 'inherit', stdio: 'inherit',
@ -25,7 +24,7 @@ export class AgentController {
process.exit(1); process.exit(1);
} }
console.log('Restarting agent...'); console.log('Restarting agent...');
this.start(false); this.start(false, true);
last_restart = Date.now(); last_restart = Date.now();
} }
}); });

View file

@ -3,20 +3,25 @@ import yargs from 'yargs';
const args = process.argv.slice(2); const args = process.argv.slice(2);
if (args.length < 1) { 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); process.exit(1);
} }
const argv = yargs(args) const argv = yargs(args)
.option('restart_memory', { .option('clear_memory', {
alias: 'r', alias: 'c',
type: 'boolean', type: 'boolean',
description: 'restart memory from scratch' 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 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'; const save_path = './bots/'+name+'.json';
let agent = new Agent(name, save_path, restart_memory); new Agent(name, save_path, clear_memory, autostart);
agent.start();

View file

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