From d7514eda0e16806090a8ad828e3d89997e8eb2cf Mon Sep 17 00:00:00 2001 From: mikudev Date: Fri, 29 Sep 2023 00:47:59 -0300 Subject: [PATCH] Fix aphrodite response regeneration --- .../interactive-chat/bot-display/BotDisplay.tsx | 16 +++++++++------- apps/browser-chat/src/libs/botLoader.tsx | 8 ++++---- apps/browser-chat/src/libs/platformAPI.ts | 12 +++++++++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/apps/browser-chat/src/components/interactive-chat/bot-display/BotDisplay.tsx b/apps/browser-chat/src/components/interactive-chat/bot-display/BotDisplay.tsx index dd015d6b..c132baa9 100644 --- a/apps/browser-chat/src/components/interactive-chat/bot-display/BotDisplay.tsx +++ b/apps/browser-chat/src/components/interactive-chat/bot-display/BotDisplay.tsx @@ -258,7 +258,6 @@ export const BotDisplay = () => { }); const lastMemoryLine = memoryLines[memoryLines.length - 2]; - const lastResponse = memoryLines[memoryLines.length - 1]; event.preventDefault(); setResponseIds((_responseIds) => { @@ -268,9 +267,12 @@ export const BotDisplay = () => { }); const aphrodite = getAphroditeConfig(); - if (aphrodite.enabled && lastMemoryLine.id && lastResponse.id) { - await platformAPI.deleteChatMessage(aphrodite.chatId, lastResponse?.id || ''); - await platformAPI.deleteChatMessage(aphrodite.chatId, lastMemoryLine?.id || ''); + if (aphrodite.enabled && lastMemoryLine.id) { + const lastCommandId = platformAPI.getLastMessageId(); + if (lastCommandId) { + platformAPI.deleteChatMessage(aphrodite.chatId, lastCommandId || ''); + platformAPI.deleteChatMessage(aphrodite.chatId, lastMemoryLine?.id || ''); + } } const result = botFactory @@ -300,10 +302,10 @@ export const BotDisplay = () => { const text = responsesStore.get(responseId)?.text; const aphrodite = getAphroditeConfig(); if (aphrodite.enabled && memoryLines?.length) { + const lastCommandId = platformAPI.getLastMessageId(); const newresponse = responsesStore.get(responseId); - const lastMemoryLine = memoryLines[memoryLines.length - 1]; - if (lastMemoryLine?.id) { - platformAPI.editChatMessage(aphrodite.chatId, lastMemoryLine.id, newresponse?.text || ''); + if (lastCommandId) { + platformAPI.editChatMessage(aphrodite.chatId, lastCommandId, newresponse?.text || ''); } } diff --git a/apps/browser-chat/src/libs/botLoader.tsx b/apps/browser-chat/src/libs/botLoader.tsx index 40f42efc..6b075e32 100644 --- a/apps/browser-chat/src/libs/botLoader.tsx +++ b/apps/browser-chat/src/libs/botLoader.tsx @@ -343,7 +343,7 @@ export function useBot(): { subject: message.isBot ? decoratedConfig.bot_name : _botData.settings.text.name, text: message.text, })); - const sceneIds = chat.data.chatMessages.filter(message => message.isBot).map((message) => { + const botMessages = chat.data.chatMessages.filter(message => message.isBot).map((message) => { const firstScenario = res.card?.data.extensions.mikugg.scenarios.find(_scenario => message.sceneId === _scenario.id); const firstEmotionGroup = res.card?.data.extensions.mikugg.emotion_groups.find(emotion_group => firstScenario?.emotion_group === emotion_group.id); let firstImage = firstEmotionGroup?.emotions?.find(emotion => emotion?.id === message.emotionId)?.source[0] || firstEmotionGroup?.emotions[0].source[0]; @@ -353,13 +353,13 @@ export function useBot(): { fillResponse(message.id, "audio", ''); fillResponse(message.id, "scene", message.sceneId); - return message.sceneId; + return message; }); - const lastId = sceneIds.length ? sceneIds[sceneIds.length - 1] : null; + const lastBotMessage = botMessages.length ? botMessages[botMessages.length - 1] : null; const emotionInterpreter = decoratedConfig.outputListeners.find(listener => listener.service === MikuExtensions.Services.ServicesNames.SBertEmotionInterpreter); if (emotionInterpreter) { const bot = botFactory.getInstance(); - bot?.changeContext(lastId || res.card.data.extensions.mikugg.start_scenario) + bot?.changeContext(lastBotMessage?.sceneId || res.card.data.extensions.mikugg.start_scenario) } } if (!isDifferentBot && memoryLines.length) { diff --git a/apps/browser-chat/src/libs/platformAPI.ts b/apps/browser-chat/src/libs/platformAPI.ts index 80277152..a1ee415b 100644 --- a/apps/browser-chat/src/libs/platformAPI.ts +++ b/apps/browser-chat/src/libs/platformAPI.ts @@ -38,6 +38,7 @@ export interface ChatMessageInput { class PlatformAPIClient { private readonly client: AxiosInstance; + private lastMessageId: string | null = null; constructor(token?: string) { this.client = axios.create({ baseURL: import.meta.env.VITE_PLATFORM_API || '', withCredentials: true }); @@ -54,6 +55,9 @@ class PlatformAPIClient { async getChat(chatId: string): Promise> { const response: AxiosResponse = await this.client.get(`/chat/${chatId}`); + if (response.data.chatMessages.length) { + this.lastMessageId = response.data.chatMessages[response.data.chatMessages.length - 1].id; + } return response; } @@ -66,7 +70,9 @@ class PlatformAPIClient { firstMessage: ChatMessage, secondMessage: ChatMessage, }>> { - return await this.client.post(`/chat/${chatId}/messages`, { firstMessage, secondMessage }); + const response = await this.client.post(`/chat/${chatId}/messages`, { firstMessage, secondMessage }); + this.lastMessageId = response.data.secondMessage.id; + return response; } async editChatMessage(chatId: string, messageId: string, text: string): Promise> { @@ -76,6 +82,10 @@ class PlatformAPIClient { async deleteChatMessage(chatId: string, messageId: string): Promise> { return await this.client.delete(`/chat/${chatId}/message/${messageId}`); } + + getLastMessageId(): string | null { + return this.lastMessageId; + } } export default new PlatformAPIClient(); \ No newline at end of file