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++) { for (let i = 0; i < args.length; i++) {
let arg = args[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("'"))) { if ((arg.startsWith('"') && arg.endsWith('"')) || (arg.startsWith("'") && arg.endsWith("'"))) {
args[i] = arg.substring(1, arg.length-1); args[i] = arg.substring(1, arg.length-1);
} else if (!isNaN(arg)) { } else if (!isNaN(arg)) {

View file

@ -269,6 +269,8 @@ export async function attackNearest(bot, mobType, kill=true) {
* await skills.attackNearest(bot, "zombie", true); * await skills.attackNearest(bot, "zombie", true);
**/ **/
bot.modes.pause('cowardice'); 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); const mob = world.getNearbyEntities(bot, 24).find(entity => entity.name === mobType);
if (mob) { if (mob) {
return await attackEntity(bot, mob, kill); return await attackEntity(bot, mob, kill);
@ -1151,6 +1153,7 @@ export async function goToBed(bot) {
const bed = bot.blockAt(loc); const bed = bot.blockAt(loc);
await bot.sleep(bed); await bot.sleep(bed);
log(bot, `You are in bed.`); log(bot, `You are in bed.`);
bot.modes.pause('unstuck');
while (bot.isSleeping) { while (bot.isSleeping) {
await new Promise(resolve => setTimeout(resolve, 500)); await new Promise(resolve => setTimeout(resolve, 500));
} }

View file

@ -18,9 +18,12 @@ export class Prompter {
this.profile = JSON.parse(readFileSync(fp, 'utf8')); this.profile = JSON.parse(readFileSync(fp, 'utf8'));
this.convo_examples = null; this.convo_examples = null;
this.coding_examples = null; this.coding_examples = null;
let name = this.profile.name; let name = this.profile.name;
let chat = this.profile.model; 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 // try to get "max_tokens" parameter, else null
let max_tokens = null; let max_tokens = null;
if (this.profile.max_tokens) if (this.profile.max_tokens)
@ -168,19 +171,30 @@ export class Prompter {
return prompt; 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) { async promptConvo(messages) {
await this.checkCooldown();
let prompt = this.profile.conversing; let prompt = this.profile.conversing;
prompt = await this.replaceStrings(prompt, messages, this.convo_examples); prompt = await this.replaceStrings(prompt, messages, this.convo_examples);
return await this.chat_model.sendRequest(messages, prompt); return await this.chat_model.sendRequest(messages, prompt);
} }
async promptCoding(messages) { async promptCoding(messages) {
await this.checkCooldown();
let prompt = this.profile.coding; let prompt = this.profile.coding;
prompt = await this.replaceStrings(prompt, messages, this.coding_examples); prompt = await this.replaceStrings(prompt, messages, this.coding_examples);
return await this.chat_model.sendRequest(messages, prompt); return await this.chat_model.sendRequest(messages, prompt);
} }
async promptMemSaving(to_summarize) { async promptMemSaving(to_summarize) {
await this.checkCooldown();
let prompt = this.profile.saving_memory; let prompt = this.profile.saving_memory;
prompt = await this.replaceStrings(prompt, null, null, to_summarize); prompt = await this.replaceStrings(prompt, null, null, to_summarize);
return await this.chat_model.sendRequest([], prompt); return await this.chat_model.sendRequest([], prompt);

View file

@ -1,6 +1,8 @@
import { spawn } from 'child_process'; import { spawn } from 'child_process';
export class AgentProcess { export class AgentProcess {
static runningCount = 0;
start(profile, load_memory=false, init_message=null, count_id=0) { start(profile, load_memory=false, init_message=null, count_id=0) {
let args = ['src/process/init-agent.js', this.name]; let args = ['src/process/init-agent.js', this.name];
args.push('-p', profile); args.push('-p', profile);
@ -14,6 +16,7 @@ export class AgentProcess {
stdio: 'inherit', stdio: 'inherit',
stderr: 'inherit', stderr: 'inherit',
}); });
AgentProcess.runningCount++;
let last_restart = Date.now(); let last_restart = Date.now();
agentProcess.on('exit', (code, signal) => { agentProcess.on('exit', (code, signal) => {
@ -22,11 +25,16 @@ export class AgentProcess {
if (code !== 0) { if (code !== 0) {
// agent must run for at least 10 seconds before restarting // agent must run for at least 10 seconds before restarting
if (Date.now() - last_restart < 10000) { if (Date.now() - last_restart < 10000) {
console.error('Agent process exited too quickly. Killing entire process. Goodbye.'); console.error(`Agent process ${profile} exited too quickly and will not be restarted.`);
process.exit(1); AgentProcess.runningCount--;
if (AgentProcess.runningCount <= 0) {
console.error('All agent processes have ended. Exiting.');
process.exit(0);
}
return;
} }
console.log('Restarting agent...'); console.log('Restarting agent...');
this.start(profile, true, 'Agent process restarted.'); this.start(profile, true, 'Agent process restarted.', count_id);
last_restart = Date.now(); last_restart = Date.now();
} }
}); });