From 2321fd5923e823d718b914237c53e7941f3cbe11 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Sat, 12 Oct 2024 20:40:16 -0500 Subject: [PATCH] cooldown, agent process counter, fixed skills --- src/agent/commands/index.js | 7 +++++++ src/agent/library/skills.js | 3 +++ src/agent/prompter.js | 16 +++++++++++++++- src/process/agent-process.js | 14 +++++++++++--- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/agent/commands/index.js b/src/agent/commands/index.js index 199c5e9..6f0d4c8 100644 --- a/src/agent/commands/index.js +++ b/src/agent/commands/index.js @@ -43,6 +43,13 @@ function parseCommandMessage(message) { for (let i = 0; i < args.length; i++) { let arg = args[i]; + + if (arg.includes('=')) { + // this sanitizes syntaxes like "x=2" and ignores the param name + let split = arg.split('='); + args[i] = split[1]; + } + if ((arg.startsWith('"') && arg.endsWith('"')) || (arg.startsWith("'") && arg.endsWith("'"))) { args[i] = arg.substring(1, arg.length-1); } else if (!isNaN(arg)) { diff --git a/src/agent/library/skills.js b/src/agent/library/skills.js index a7643c7..5395b68 100644 --- a/src/agent/library/skills.js +++ b/src/agent/library/skills.js @@ -269,6 +269,8 @@ export async function attackNearest(bot, mobType, kill=true) { * await skills.attackNearest(bot, "zombie", true); **/ bot.modes.pause('cowardice'); + if (mobType === 'drowned' || mobType === 'cod' || mobType === 'salmon' || mobType === 'tropical_fish' || mobType === 'squid') + bot.modes.pause('self_preservation'); // so it can go underwater. TODO: have an drowning mode so we don't turn off all self_preservation const mob = world.getNearbyEntities(bot, 24).find(entity => entity.name === mobType); if (mob) { return await attackEntity(bot, mob, kill); @@ -1151,6 +1153,7 @@ export async function goToBed(bot) { const bed = bot.blockAt(loc); await bot.sleep(bed); log(bot, `You are in bed.`); + bot.modes.pause('unstuck'); while (bot.isSleeping) { await new Promise(resolve => setTimeout(resolve, 500)); } diff --git a/src/agent/prompter.js b/src/agent/prompter.js index 3666221..6a3a8cb 100644 --- a/src/agent/prompter.js +++ b/src/agent/prompter.js @@ -18,9 +18,12 @@ export class Prompter { this.profile = JSON.parse(readFileSync(fp, 'utf8')); this.convo_examples = null; this.coding_examples = null; - + let name = this.profile.name; let chat = this.profile.model; + this.cooldown = this.profile.cooldown ? this.profile.cooldown : 0; + this.last_prompt_time = 0; + // try to get "max_tokens" parameter, else null let max_tokens = null; if (this.profile.max_tokens) @@ -168,19 +171,30 @@ export class Prompter { return prompt; } + async checkCooldown() { + let elapsed = Date.now() - this.last_prompt_time; + if (elapsed < this.cooldown && this.cooldown > 0) { + await new Promise(r => setTimeout(r, this.cooldown - elapsed)); + } + this.last_prompt_time = Date.now(); + } + async promptConvo(messages) { + await this.checkCooldown(); let prompt = this.profile.conversing; prompt = await this.replaceStrings(prompt, messages, this.convo_examples); return await this.chat_model.sendRequest(messages, prompt); } async promptCoding(messages) { + await this.checkCooldown(); let prompt = this.profile.coding; prompt = await this.replaceStrings(prompt, messages, this.coding_examples); return await this.chat_model.sendRequest(messages, prompt); } async promptMemSaving(to_summarize) { + await this.checkCooldown(); let prompt = this.profile.saving_memory; prompt = await this.replaceStrings(prompt, null, null, to_summarize); return await this.chat_model.sendRequest([], prompt); diff --git a/src/process/agent-process.js b/src/process/agent-process.js index 5409ba5..53fbd07 100644 --- a/src/process/agent-process.js +++ b/src/process/agent-process.js @@ -1,6 +1,8 @@ import { spawn } from 'child_process'; export class AgentProcess { + static runningCount = 0; + start(profile, load_memory=false, init_message=null, count_id=0) { let args = ['src/process/init-agent.js', this.name]; args.push('-p', profile); @@ -14,6 +16,7 @@ export class AgentProcess { stdio: 'inherit', stderr: 'inherit', }); + AgentProcess.runningCount++; let last_restart = Date.now(); agentProcess.on('exit', (code, signal) => { @@ -22,11 +25,16 @@ export class AgentProcess { if (code !== 0) { // agent must run for at least 10 seconds before restarting if (Date.now() - last_restart < 10000) { - console.error('Agent process exited too quickly. Killing entire process. Goodbye.'); - process.exit(1); + console.error(`Agent process ${profile} exited too quickly and will not be restarted.`); + AgentProcess.runningCount--; + if (AgentProcess.runningCount <= 0) { + console.error('All agent processes have ended. Exiting.'); + process.exit(0); + } + return; } console.log('Restarting agent...'); - this.start(profile, true, 'Agent process restarted.'); + this.start(profile, true, 'Agent process restarted.', count_id); last_restart = Date.now(); } });