refactored agent startup that was broken by ai code

This commit is contained in:
MaxRobinsonTheGreat 2024-11-23 13:33:08 -06:00
parent 1acb35801f
commit 68a2dccb76
2 changed files with 68 additions and 109 deletions

View file

@ -18,7 +18,6 @@ export class Agent {
async start(profile_fp, load_mem=false, init_message=null, count_id=0) {
this.last_sender = null;
try {
// Add validation for profile_fp
if (!profile_fp) {
throw new Error('No profile filepath provided');
}
@ -29,98 +28,69 @@ export class Agent {
console.log('Starting agent initialization with profile:', profile_fp);
// Initialize components with more detailed error handling
try {
console.log('Initializing action manager...');
this.actions = new ActionManager(this);
console.log('Initializing prompter...');
this.prompter = new Prompter(this, profile_fp);
this.name = this.prompter.getName();
console.log('Initializing history...');
this.history = new History(this);
console.log('Initializing coder...');
this.coder = new Coder(this);
console.log('Initializing npc controller...');
this.npc = new NPCContoller(this);
console.log('Initializing memory bank...');
this.memory_bank = new MemoryBank();
console.log('Initializing self prompter...');
this.self_prompter = new SelfPrompter(this);
initConversationManager(this);
// After getting the name, register with MindServer via proxy
serverProxy.registerAgent(this.name);
} catch (error) {
throw new Error(`Failed to initialize agent components: ${error.message || error}`);
}
try {
console.log('Initializing examples...');
await this.prompter.initExamples();
} catch (error) {
throw new Error(`Failed to initialize examples: ${error.message || error}`);
}
console.log('Initializing action manager...');
this.actions = new ActionManager(this);
console.log('Initializing prompter...');
this.prompter = new Prompter(this, profile_fp);
this.name = this.prompter.getName();
console.log('Initializing history...');
this.history = new History(this);
console.log('Initializing coder...');
this.coder = new Coder(this);
console.log('Initializing npc controller...');
this.npc = new NPCContoller(this);
console.log('Initializing memory bank...');
this.memory_bank = new MemoryBank();
console.log('Initializing self prompter...');
this.self_prompter = new SelfPrompter(this);
initConversationManager(this);
// After getting the name, register with MindServer via proxy
serverProxy.registerAgent(this.name);
console.log('Initializing examples...');
await this.prompter.initExamples();
console.log('Logging into minecraft...');
try {
this.bot = initBot(this.name);
} catch (error) {
throw new Error(`Failed to initialize Minecraft bot: ${error.message || error}`);
}
this.bot = initBot(this.name);
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
}
save_data = this.history.load();
}
// 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.on('login', () => {
console.log(this.name, 'logged in!');
// Set skin for profile, requires Fabric Tailor. (https://modrinth.com/mod/fabrictailor)
if (this.prompter.profile.skin)
this.bot.chat(`/skin set URL ${this.prompter.profile.skin.model} ${this.prompter.profile.skin.path}`);
else
this.bot.chat(`/skin clear`);
});
this.bot.once('error', (error) => {
const spawnTimeout = setTimeout(() => {
process.exit(0);
}, 30000);
this.bot.once('spawn', async () => {
try {
clearTimeout(spawnTimeout);
console.error('Bot encountered error:', error);
reject(error);
});
addViewer(this.bot, count_id);
this.bot.on('login', () => {
console.log('Logged in!');
// wait for a bit so stats are not undefined
await new Promise((resolve) => setTimeout(resolve, 1000));
// Set skin for profile, requires Fabric Tailor. (https://modrinth.com/mod/fabrictailor)
if (this.prompter.profile.skin)
this.bot.chat(`/skin set URL ${this.prompter.profile.skin.model} ${this.prompter.profile.skin.path}`);
else
this.bot.chat(`/skin clear`);
});
this.bot.once('spawn', async () => {
try {
clearTimeout(spawnTimeout);
addViewer(this.bot, count_id);
// wait for a bit so stats are not undefined
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(`${this.name} spawned.`);
this.clearBotLogs();
this._setupEventHandlers(save_data, init_message);
this.startEvents();
resolve();
} catch (error) {
reject(error);
}
});
console.log(`${this.name} spawned.`);
this.clearBotLogs();
this._setupEventHandlers(save_data, init_message);
this.startEvents();
} catch (error) {
console.error('Error in spawn event:', error);
process.exit(0);
}
});
} catch (error) {
// Ensure we're not losing error details
@ -133,8 +103,7 @@ export class Agent {
}
}
// Split out event handler setup for clarity
_setupEventHandlers(save_data, init_message) {
async _setupEventHandlers(save_data, init_message) {
const ignore_messages = [
"Set own game mode to",
"Set the time to",
@ -177,32 +146,22 @@ export class Agent {
bannedFood: ["rotten_flesh", "spider_eye", "poisonous_potato", "pufferfish", "chicken"]
};
// 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);
await this.self_prompter.start(prompt);
}
else if (save_data?.last_sender) {
this.last_sender = save_data.last_sender;
await this.handleMessage('system', `You have restarted and this message is auto-generated. Continue the conversation with ${this.last_sender}`);
}
else if (init_message) {
await this.handleMessage('system', init_message, 2);
}
else {
const translation = await handleTranslation("Hello world! I am "+this.name);
this.bot.chat(translation);
}
} catch (error) {
console.error('Error handling startup conditions:', error);
throw error;
if (save_data?.self_prompt) {
let prompt = save_data.self_prompt;
// add initial message to history
this.history.add('system', prompt);
await this.self_prompter.start(prompt);
}
else if (save_data?.last_sender) {
this.last_sender = save_data.last_sender;
await this.handleMessage('system', `You have restarted and this message is auto-generated. Continue the conversation with ${this.last_sender}`);
}
else if (init_message) {
await this.handleMessage('system', init_message, 2);
}
else {
const translation = await handleTranslation("Hello world! I am "+this.name);
this.bot.chat(translation);
}
}

View file

@ -199,7 +199,7 @@ async function _scheduleProcessInMessage(sender, recieved, convo) {
function _processInMessageQueue(name) {
const convo = _getConvo(name);
let pack = null;
let pack = {};
let full_message = '';
while (convo.in_queue.length > 0) {
pack = convo.in_queue.shift();