From a9371f27e8ff437fdadf4ca4c2204a4c132b9991 Mon Sep 17 00:00:00 2001 From: mikudev Date: Sat, 23 Sep 2023 18:46:00 -0300 Subject: [PATCH] Fix history loading from api, fix setting update speed --- .../src/components/settings/SettingsModal.tsx | 11 ++++++-- apps/browser-chat/src/libs/botLoader.tsx | 19 +++++++------- apps/browser-chat/src/libs/useResponses.tsx | 26 ++++++++++--------- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/apps/browser-chat/src/components/settings/SettingsModal.tsx b/apps/browser-chat/src/components/settings/SettingsModal.tsx index b97dbec3..c5f5cfd7 100644 --- a/apps/browser-chat/src/components/settings/SettingsModal.tsx +++ b/apps/browser-chat/src/components/settings/SettingsModal.tsx @@ -72,8 +72,15 @@ export const voices: { ] const SettingsModal = (props: SettingsModalProps): JSX.Element => { - const settings = props.value; - const setSettings = props.onChange; + const [settings, _setRawSettings] = useState(props.value); + const setSettings = (_settings: SettingsState) => { + _setRawSettings(_settings); + props.onChange(_settings); + }; + + useEffect(() => { + _setRawSettings(props.value); + }, [props.value]); return (
diff --git a/apps/browser-chat/src/libs/botLoader.tsx b/apps/browser-chat/src/libs/botLoader.tsx index afb23522..6b978f20 100644 --- a/apps/browser-chat/src/libs/botLoader.tsx +++ b/apps/browser-chat/src/libs/botLoader.tsx @@ -338,22 +338,21 @@ export function useBot(): { ) { const chat = await platformAPI.getChat(_botData.settings.promptCompleterEndpoint.genSettings.chatId); memoryLines = chat.data.chatMessages.map((message) => ({ - id: message.id, + id: message.isBot ? message.id : undefined, type: MikuCore.Commands.CommandType.DIALOG, subject: message.isBot ? decoratedConfig.bot_name : _botData.settings.text.name, text: message.text, })); - if (chat.data.chatMessages.length) { - const lastMessage = chat.data.chatMessages[chat.data.chatMessages.length - 1]; - const firstScenario = res.card?.data.extensions.mikugg.scenarios.find(_scenario => lastMessage.sceneId === _scenario.id); + 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 === lastMessage.emotionId)?.source[0] || firstEmotionGroup?.emotions[0].source[0]; + let firstImage = firstEmotionGroup?.emotions?.find(emotion => emotion?.id === message.emotionId)?.source[0] || firstEmotionGroup?.emotions[0].source[0]; - fillResponse(lastMessage.id, "text", lastMessage.text); - fillResponse(lastMessage.id, "emotion", firstImage); - fillResponse(lastMessage.id, "audio", ''); - fillResponse(lastMessage.id, "scene", lastMessage.sceneId); - } + fillResponse(message.id, "text", message.text); + fillResponse(message.id, "emotion", firstImage); + fillResponse(message.id, "audio", ''); + fillResponse(message.id, "scene", message.sceneId); + }); } if (!isDifferentBot && memoryLines.length) { const memory = botFactory.getInstance()?.getMemory(); diff --git a/apps/browser-chat/src/libs/useResponses.tsx b/apps/browser-chat/src/libs/useResponses.tsx index d40ecc8e..dc7dbfa7 100644 --- a/apps/browser-chat/src/libs/useResponses.tsx +++ b/apps/browser-chat/src/libs/useResponses.tsx @@ -75,6 +75,20 @@ export const InteractiveResponsesContextProvider = ({ if (botConfig && card && bot) { const memory = bot?.getMemory().getMemory() || []; + const firstScenario = card?.data.extensions.mikugg.scenarios.find(scenario => card?.data.extensions.mikugg.start_scenario === scenario.id); + const firstEmotionGroup = card?.data.extensions.mikugg.emotion_groups.find(emotion_group => firstScenario?.emotion_group === emotion_group.id); + let firstEmotion = firstEmotionGroup?.template === 'base-emotions' ? firstEmotionGroup.emotions?.find(emotion => emotion?.id === 'happy') : firstEmotionGroup?.emotions[0]; + let firstImage = firstEmotion?.source[0]; + let firstMessage = card?.data.first_mes || ''; + firstMessage = MikuExtensions.Memory.Strategies.fillTextTemplate(firstMessage, { + bot: card?.data.name || '', + user: botConfigSettings.text.name + }); + + fillResponse('first', "text", firstMessage); + fillResponse('first', "emotion", firstImage || ''); + fillResponse('first', "audio", ''); + if (memory.length) { const ids = memory.map(line => line.id || '').filter(id => id).reverse(); const lastId = ids.length ? ids[ids.length - 1] : null; @@ -86,20 +100,8 @@ export const InteractiveResponsesContextProvider = ({ } } } else { - const firstScenario = card?.data.extensions.mikugg.scenarios.find(scenario => card?.data.extensions.mikugg.start_scenario === scenario.id); - const firstEmotionGroup = card?.data.extensions.mikugg.emotion_groups.find(emotion_group => firstScenario?.emotion_group === emotion_group.id); - let firstEmotion = firstEmotionGroup?.template === 'base-emotions' ? firstEmotionGroup.emotions?.find(emotion => emotion?.id === 'happy') : firstEmotionGroup?.emotions[0]; - let firstImage = firstEmotion?.source[0]; let firstSoundId = firstEmotion?.sound; let firstSound = card?.data.extensions.mikugg.sounds?.find(sound => sound.id === firstSoundId)?.source; - let firstMessage = card?.data.first_mes || ''; - firstMessage = MikuExtensions.Memory.Strategies.fillTextTemplate(firstMessage, { - bot: card?.data.name || '', - user: botConfigSettings.text.name - }); - fillResponse('first', "text", firstMessage); - fillResponse('first', "emotion", firstImage || ''); - fillResponse('first', "audio", firstSound); if (firstSound) { playAudio(assetLinkLoader(firstSound, 'audio'), botConfigSettings.voice.speed); }