cooldown, agent process counter, fixed skills

This commit is contained in:
MaxRobinsonTheGreat 2024-10-12 20:40:16 -05:00
parent cb808cb7d0
commit 2321fd5923
4 changed files with 36 additions and 4 deletions

View file

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

View file

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

View file

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

View file

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