From 9e2b354b0a9db217fb1d0df6b7fa5048119fc22a Mon Sep 17 00:00:00 2001 From: Webifi Date: Thu, 1 Jun 2023 22:53:48 -0500 Subject: [PATCH] Fix improper resetting of setting values --- src/lib/Chat.svelte | 8 +++----- src/lib/ChatOptionMenu.svelte | 4 ++-- src/lib/Profiles.svelte | 23 +++++++++++++++-------- src/lib/Storage.svelte | 4 ++-- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/lib/Chat.svelte b/src/lib/Chat.svelte index 9aec8b3..6b4b43b 100644 --- a/src/lib/Chat.svelte +++ b/src/lib/Chat.svelte @@ -22,7 +22,7 @@ } from './Types.svelte' import Prompts from './Prompts.svelte' import Messages from './Messages.svelte' - import { applyProfile, getProfile, prepareSummaryPrompt } from './Profiles.svelte' + import { prepareSummaryPrompt, restartProfile } from './Profiles.svelte' import { afterUpdate, onMount } from 'svelte' import Fa from 'svelte-fa/src/fa.svelte' @@ -64,8 +64,7 @@ clearTimeout(scDelay) setTimeout(() => { if (chat.startSession) { - const profile = getProfile('') // get default profile - applyProfile(chatId, profile.profile as any) + restartProfile(chatId) if (chat.startSession) { chat.startSession = false saveChatStore() @@ -120,8 +119,7 @@ console.log('Speech recognition not supported') } if (chat.startSession) { - const profile = getProfile('') // get default profile - applyProfile(chatId, profile.profile as any) + restartProfile(chatId) if (chat.startSession) { chat.startSession = false saveChatStore() diff --git a/src/lib/ChatOptionMenu.svelte b/src/lib/ChatOptionMenu.svelte index 834b026..eab3d99 100644 --- a/src/lib/ChatOptionMenu.svelte +++ b/src/lib/ChatOptionMenu.svelte @@ -19,7 +19,7 @@ } from '@fortawesome/free-solid-svg-icons/index' import { apiKeyStorage, addChatFromJSON, chatsStorage, checkStateChange, clearChats, clearMessages, copyChat, globalStorage, setGlobalSettingValueByKey, showSetChatSettings, pinMainMenu } from './Storage.svelte' import { exportAsMarkdown, exportChatAsJSON } from './Export.svelte' - import { applyProfile } from './Profiles.svelte' + import { restartProfile } from './Profiles.svelte' import { replace } from 'svelte-spa-router' import { clickOutside } from 'svelte-use-click-outside' @@ -66,7 +66,7 @@ const restartChatSession = () => { close() - applyProfile(chatId, '', true) + restartProfile(chatId) $checkStateChange++ // signal chat page to start profile } diff --git a/src/lib/Profiles.svelte b/src/lib/Profiles.svelte index cd16b73..4964142 100644 --- a/src/lib/Profiles.svelte +++ b/src/lib/Profiles.svelte @@ -71,12 +71,10 @@ export const prepareSummaryPrompt = (chatId:number, promptsSize:number, maxToken .replaceAll('[[MAX_WORDS]]', Math.floor(maxTokens * 0.75).toString()) // ~.75 words per token. May need to reduce } -// Apply currently selected profile -export const applyProfile = (chatId:number, key:string = '', resetChat:boolean = false) => { +// Restart currently loaded profile +export const restartProfile = (chatId:number, noApply:boolean=false) => { const settings = getChatSettings(chatId) - const profile = getProfile(key || settings.profile) - resetChatSettings(chatId, resetChat) // Fully reset - if (!resetChat) return + if (!settings.profile && !noApply) return applyProfile(chatId, '', true) // Clear current messages clearMessages(chatId) // Add the system prompt @@ -88,8 +86,8 @@ export const applyProfile = (chatId:number, key:string = '', resetChat:boolean = addMessage(chatId, systemPromptMessage) // Add trainingPrompts, if any - if (profile.trainingPrompts) { - profile.trainingPrompts.forEach(tp => { + if (settings.trainingPrompts) { + settings.trainingPrompts.forEach(tp => { addMessage(chatId, tp) }) } @@ -97,7 +95,16 @@ export const applyProfile = (chatId:number, key:string = '', resetChat:boolean = getChat(chatId).startSession = settings.autoStartSession saveChatStore() // Mark mark this as last used - setGlobalSettingValueByKey('lastProfile', key) + setGlobalSettingValueByKey('lastProfile', settings.profile) +} + +// Apply currently selected profile +export const applyProfile = (chatId:number, key:string = '', resetChat:boolean = false) => { + const settings = getChatSettings(chatId) + const profile = getProfile(key || settings.profile) + resetChatSettings(chatId, resetChat) // Fully reset + if (!resetChat) return + return restartProfile(chatId, true) } const summaryPrompts = { diff --git a/src/lib/Storage.svelte b/src/lib/Storage.svelte index 95a37e2..09ccffd 100644 --- a/src/lib/Storage.svelte +++ b/src/lib/Storage.svelte @@ -4,7 +4,7 @@ import type { Chat, ChatSettings, GlobalSettings, Message, ChatSetting, GlobalSetting, Usage, Model } from './Types.svelte' import { getChatSettingObjectByKey, getGlobalSettingObjectByKey, getChatDefaults, getExcludeFromProfile } from './Settings.svelte' import { v4 as uuidv4 } from 'uuid' - import { applyProfile, getProfile, isStaticProfile } from './Profiles.svelte' + import { getProfile, isStaticProfile, restartProfile } from './Profiles.svelte' export const chatsStorage = persisted('chats', [] as Chat[]) export const globalStorage = persisted('global', {} as GlobalSettings) @@ -40,7 +40,7 @@ }) chatsStorage.set(chats) // Apply defaults and prepare it to start - applyProfile(chatId, '', true) + restartProfile(chatId) return chatId }