From 2923766264b478df5f9993e8e7e7224151fa97ed Mon Sep 17 00:00:00 2001 From: Erik Jespersen <42016062+Mookse@users.noreply.github.com> Date: Wed, 17 Jul 2024 01:09:36 -0400 Subject: [PATCH] 20240717 @Mookse (#278) --- inc/js/mylife-avatar.mjs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/inc/js/mylife-avatar.mjs b/inc/js/mylife-avatar.mjs index 9f8ac2a..f7f58a4 100644 --- a/inc/js/mylife-avatar.mjs +++ b/inc/js/mylife-avatar.mjs @@ -95,14 +95,12 @@ class Avatar extends EventEmitter { if(!activeBotId) throw new Error('Parameter `activeBotId` required.') const { activeBot, factory } = this - const { id: botId, } = activeBot + const { id: botId, thread_id, } = activeBot if(botId!==activeBotId) throw new Error(`Invalid bot id: ${ activeBotId }, active bot id: ${ botId }`) - // @stub - clean up conversation alterations - Q is immune conversation = conversation - ?? this.getConversation(threadId) - if(!conversation) - throw new Error('No conversation found for thread id and could not be created.') + ?? this.getConversation(threadId ?? thread_id) + ?? await this.createConversation('chat', threadId, activeBotId) conversation.bot_id = activeBot.bot_id // pass in via quickly mutating conversation (or independently if preferred in end), versus llmServices which are global const messages = await mCallLLM(this.#llmServices, conversation, chatMessage, factory, this) conversation.addMessages(messages) @@ -186,8 +184,18 @@ class Avatar extends EventEmitter { * @returns {Conversation} - The conversation object. */ async createConversation(type='chat', threadId, botId=this.activeBotId, saveToConversations=true){ + const bot = await this.bot(botId) + console.log('createConversation::bot', botId, bot.bot_name, bot.type) const thread = await this.#llmServices.thread(threadId) const conversation = new (this.#factory.conversation)({ mbr_id: this.mbr_id, type, }, this.#factory, thread, botId) + bot.thread_id = conversation.thread_id + const { bot_id, bot_name, id, thread_id, } = bot + const updatedBot = await this.factory.updateBot({ + bot_id, // required? + bot_name, // required? + id, + thread_id, + }) if(saveToConversations) this.#conversations.push(conversation) return conversation @@ -1278,6 +1286,7 @@ async function mBot(factory, avatar, bot){ /* update/create */ let originBot = bots.find(oBot=>oBot.id===botId) if(originBot){ /* update bot */ + console.log('mBot::UPDATE', bots, originBot) const options = {} const updatedBot = Object.keys(bot) .reduce((diff, key) => { @@ -1957,29 +1966,28 @@ async function mInit(factory, avatar, bots){ avatar.nickname = avatar.nickname ?? avatar.names?.[0] ?? `${avatar.memberFirstName ?? 'member'}'s avatar` - /* bots */ // @stub - determine by default or activated team - requiredBotTypes.push('library', 'personal-biographer') // default memory team + /* bots */ + requiredBotTypes.push('personal-biographer') // memory team } bots.push(...await factory.bots(avatar.id)) await Promise.all( requiredBotTypes - .map(async botType=>{ - if(!bots.some(bot=>bot.type===botType)){ // create required bot - const bot = await mBot(factory, avatar, { type: botType }) - bots.push(bot) - } + .map(async botType=>{ + if(!bots.some(bot=>bot.type===botType)){ // create required bot + const bot = await mBot(factory, avatar, { type: botType }) + bots.push(bot) + } } )) avatar.activeBotId = avatar.avatar.id // initially set active bot to personal-avatar /* conversations */ await Promise.all( - bots.map(async bot=>{ + bots.map(async bot=>{ const { thread_id, } = bot if(thread_id && !avatar.getConversation(thread_id)) avatar.conversations.push(await avatar.createConversation('chat', thread_id)) }) ) - // need to create new conversation for Q variant, but also need to ensure that no thread_id is saved for Q /* evolver */ if(!factory.isMyLife) avatar.evolver = await (new EvolutionAssistant(avatar))