mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-04 14:25:43 +02:00
added a ton of error handling/logging
This commit is contained in:
parent
cfcd9d0fd8
commit
fd096b5ae3
4 changed files with 178 additions and 104 deletions
|
@ -14,6 +14,8 @@ import settings from '../../settings.js';
|
|||
|
||||
export class Agent {
|
||||
async start(profile_fp, load_mem=false, init_message=null, count_id=0) {
|
||||
try {
|
||||
// Initialize components with error handling
|
||||
this.actions = new ActionManager(this);
|
||||
this.prompter = new Prompter(this, profile_fp);
|
||||
this.name = this.prompter.getName();
|
||||
|
@ -23,23 +25,53 @@ export class Agent {
|
|||
this.memory_bank = new MemoryBank();
|
||||
this.self_prompter = new SelfPrompter(this);
|
||||
|
||||
try {
|
||||
await this.prompter.initExamples();
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize examples:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log('Logging into minecraft...');
|
||||
try {
|
||||
this.bot = initBot(this.name);
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize Minecraft bot:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
initModes(this);
|
||||
|
||||
let save_data = null;
|
||||
if (load_mem) {
|
||||
try {
|
||||
save_data = this.history.load();
|
||||
} catch (error) {
|
||||
console.error('Failed to load history:', error);
|
||||
// Don't throw here, continue without history
|
||||
}
|
||||
}
|
||||
|
||||
// Return a promise that resolves when spawn is complete
|
||||
return new Promise((resolve, reject) => {
|
||||
// Add timeout to prevent hanging
|
||||
const spawnTimeout = setTimeout(() => {
|
||||
reject(new Error('Bot spawn timed out after 30 seconds'));
|
||||
}, 30000);
|
||||
|
||||
this.bot.once('error', (error) => {
|
||||
clearTimeout(timeout);
|
||||
console.error('Bot encountered error:', error);
|
||||
reject(error);
|
||||
});
|
||||
|
||||
this.bot.on('login', () => {
|
||||
console.log('Logged in!');
|
||||
});
|
||||
|
||||
this.bot.once('spawn', async () => {
|
||||
try {
|
||||
clearTimeout(spawnTimeout);
|
||||
addViewer(this.bot, count_id);
|
||||
|
||||
// wait for a bit so stats are not undefined
|
||||
|
@ -48,6 +80,22 @@ export class Agent {
|
|||
console.log(`${this.name} spawned.`);
|
||||
this.clearBotLogs();
|
||||
|
||||
this._setupEventHandlers(save_data, init_message);
|
||||
this.startEvents();
|
||||
resolve();
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to start agent:', error);
|
||||
throw error; // Re-throw to be caught by init-agent.js
|
||||
}
|
||||
}
|
||||
|
||||
// Split out event handler setup for clarity
|
||||
_setupEventHandlers(save_data, init_message) {
|
||||
const ignore_messages = [
|
||||
"Set own game mode to",
|
||||
"Set the time to",
|
||||
|
@ -56,41 +104,52 @@ export class Agent {
|
|||
"Set the weather to",
|
||||
"Gamerule "
|
||||
];
|
||||
|
||||
const eventname = settings.profiles.length > 1 ? 'whisper' : 'chat';
|
||||
this.bot.on(eventname, async (username, message) => {
|
||||
try {
|
||||
if (username === this.name) return;
|
||||
|
||||
if (ignore_messages.some((m) => message.startsWith(m))) return;
|
||||
|
||||
this.shut_up = false;
|
||||
|
||||
this.handleMessage(username, message);
|
||||
await this.handleMessage(username, message);
|
||||
} catch (error) {
|
||||
console.error('Error handling message:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// set the bot to automatically eat food when hungry
|
||||
// Set up auto-eat
|
||||
this.bot.autoEat.options = {
|
||||
priority: 'foodPoints',
|
||||
startAt: 14,
|
||||
bannedFood: ["rotten_flesh", "spider_eye", "poisonous_potato", "pufferfish", "chicken"]
|
||||
};
|
||||
|
||||
if (save_data && save_data.self_prompt) { // if we're loading memory and self-prompting was on, restart it, ignore init_message
|
||||
// Handle startup conditions
|
||||
this._handleStartupConditions(save_data, init_message);
|
||||
}
|
||||
|
||||
async _handleStartupConditions(save_data, init_message) {
|
||||
try {
|
||||
if (save_data?.self_prompt) {
|
||||
let prompt = save_data.self_prompt;
|
||||
// add initial message to history
|
||||
this.history.add('system', prompt);
|
||||
this.self_prompter.start(prompt);
|
||||
await this.self_prompter.start(prompt);
|
||||
}
|
||||
else if (init_message) {
|
||||
this.handleMessage('system', init_message, 2);
|
||||
await this.handleMessage('system', init_message, 2);
|
||||
}
|
||||
else {
|
||||
const translation = await handleTranslation("Hello world! I am "+this.name);
|
||||
this.bot.chat(translation);
|
||||
this.bot.emit('finished_executing');
|
||||
}
|
||||
|
||||
this.startEvents();
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error handling startup conditions:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
requestInterrupt() {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { writeFileSync, readFileSync, mkdirSync } from 'fs';
|
||||
import { writeFileSync, readFileSync, mkdirSync, existsSync } from 'fs';
|
||||
import { NPCData } from './npc/data.js';
|
||||
import settings from '../../settings.js';
|
||||
|
||||
|
@ -78,51 +78,37 @@ export class History {
|
|||
}
|
||||
}
|
||||
|
||||
save() {
|
||||
// save history object to json file
|
||||
let data = {
|
||||
'name': this.name,
|
||||
'memory': this.memory,
|
||||
'turns': this.turns
|
||||
async save() {
|
||||
try {
|
||||
const data = {
|
||||
memory: this.memory,
|
||||
turns: this.turns,
|
||||
self_prompt: this.agent.self_prompter.on ? this.agent.self_prompter.prompt : null
|
||||
};
|
||||
if (this.agent.npc.data !== null)
|
||||
data.npc = this.agent.npc.data.toObject();
|
||||
const modes = this.agent.bot.modes.getJson();
|
||||
if (modes !== null)
|
||||
data.modes = modes;
|
||||
const memory_bank = this.agent.memory_bank.getJson();
|
||||
if (memory_bank !== null)
|
||||
data.memory_bank = memory_bank;
|
||||
if (this.agent.self_prompter.on) {
|
||||
data.self_prompt = this.agent.self_prompter.prompt;
|
||||
writeFileSync(this.memory_fp, JSON.stringify(data, null, 2));
|
||||
console.log('Saved memory to:', this.memory_fp);
|
||||
} catch (error) {
|
||||
console.error('Failed to save history:', error);
|
||||
throw error;
|
||||
}
|
||||
const json_data = JSON.stringify(data, null, 4);
|
||||
writeFileSync(this.memory_fp, json_data, (err) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
console.log("JSON data is saved.");
|
||||
});
|
||||
}
|
||||
|
||||
load() {
|
||||
try {
|
||||
// load history object from json file
|
||||
const data = readFileSync(this.memory_fp, 'utf8');
|
||||
const obj = JSON.parse(data);
|
||||
this.memory = obj.memory;
|
||||
this.agent.npc.data = NPCData.fromObject(obj.npc);
|
||||
if (obj.modes)
|
||||
this.agent.bot.modes.loadJson(obj.modes);
|
||||
if (obj.memory_bank)
|
||||
this.agent.memory_bank.loadJson(obj.memory_bank);
|
||||
this.turns = obj.turns;
|
||||
return obj;
|
||||
} catch (err) {
|
||||
console.error(`Error reading ${this.name}'s memory file: ${err.message}`);
|
||||
}
|
||||
if (!existsSync(this.memory_fp)) {
|
||||
console.log('No memory file found.');
|
||||
return null;
|
||||
}
|
||||
const data = JSON.parse(readFileSync(this.memory_fp, 'utf8'));
|
||||
this.memory = data.memory || '';
|
||||
this.turns = data.turns || [];
|
||||
console.log('Loaded memory:', this.memory);
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('Failed to load history:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.turns = [];
|
||||
|
|
|
@ -102,7 +102,7 @@ export class Prompter {
|
|||
mkdirSync(`./bots/${name}`, { recursive: true });
|
||||
writeFileSync(`./bots/${name}/last_profile.json`, JSON.stringify(this.profile, null, 4), (err) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
throw new Error('Failed to save profile:', err);
|
||||
}
|
||||
console.log("Copy profile saved.");
|
||||
});
|
||||
|
@ -117,15 +117,28 @@ export class Prompter {
|
|||
}
|
||||
|
||||
async initExamples() {
|
||||
// Using Promise.all to implement concurrent processing
|
||||
// Create Examples instances
|
||||
try {
|
||||
this.convo_examples = new Examples(this.embedding_model);
|
||||
this.coding_examples = new Examples(this.embedding_model);
|
||||
// Use Promise.all to load examples concurrently
|
||||
await Promise.all([
|
||||
|
||||
const [convoResult, codingResult] = await Promise.allSettled([
|
||||
this.convo_examples.load(this.profile.conversation_examples),
|
||||
this.coding_examples.load(this.profile.coding_examples),
|
||||
this.coding_examples.load(this.profile.coding_examples)
|
||||
]);
|
||||
|
||||
// Handle potential failures
|
||||
if (convoResult.status === 'rejected') {
|
||||
console.error('Failed to load conversation examples:', convoResult.reason);
|
||||
throw convoResult.reason;
|
||||
}
|
||||
if (codingResult.status === 'rejected') {
|
||||
console.error('Failed to load coding examples:', codingResult.reason);
|
||||
throw codingResult.reason;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize examples:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async replaceStrings(prompt, messages, examples=null, to_summarize=[], last_goals=null) {
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
import { Agent } from '../agent/agent.js';
|
||||
import yargs from 'yargs';
|
||||
|
||||
// Add global unhandled rejection handler
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('Unhandled Rejection at:', promise);
|
||||
console.error('Reason:', reason);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length < 1) {
|
||||
console.log('Usage: node init_agent.js <agent_name> [profile] [load_memory] [init_message]');
|
||||
|
@ -28,6 +35,15 @@ const argv = yargs(args)
|
|||
type: 'number',
|
||||
default: 0,
|
||||
description: 'identifying count for multi-agent scenarios',
|
||||
}).argv
|
||||
}).argv;
|
||||
|
||||
new Agent().start(argv.profile, argv.load_memory, argv.init_message, argv.count_id);
|
||||
// Wrap agent start in async IIFE with proper error handling
|
||||
(async () => {
|
||||
try {
|
||||
const agent = new Agent();
|
||||
await agent.start(argv.profile, argv.load_memory, argv.init_message, argv.count_id);
|
||||
} catch (error) {
|
||||
console.error('Failed to start agent:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
|
|
Loading…
Add table
Reference in a new issue