Simplify switching profiles. Less prompting.
This commit is contained in:
		
							parent
							
								
									759eb35bc6
								
							
						
					
					
						commit
						49c7570952
					
				| 
						 | 
				
			
			@ -1,10 +1,10 @@
 | 
			
		|||
<script lang="ts">
 | 
			
		||||
  import { createEventDispatcher } from 'svelte'
 | 
			
		||||
  import { getProfile } from './Profiles.svelte'
 | 
			
		||||
  import { addChat, cleanSettingValue, setChatSettingValue } from './Storage.svelte'
 | 
			
		||||
  // import { getProfile } from './Profiles.svelte'
 | 
			
		||||
  import { cleanSettingValue, setChatSettingValue } from './Storage.svelte'
 | 
			
		||||
  import type { Chat, ChatSetting, ChatSettings, ControlAction, FieldControl, SettingPrompt } from './Types.svelte'
 | 
			
		||||
  import { autoGrowInputOnEvent } from './Util.svelte'
 | 
			
		||||
  import { replace } from 'svelte-spa-router'
 | 
			
		||||
  // import { replace } from 'svelte-spa-router'
 | 
			
		||||
  import Fa from 'svelte-fa/src/fa.svelte'
 | 
			
		||||
 | 
			
		||||
  export let setting:ChatSetting
 | 
			
		||||
| 
						 | 
				
			
			@ -37,33 +37,33 @@
 | 
			
		|||
          return !!chatSettings.isDirty && newVal !== oldVal
 | 
			
		||||
        },
 | 
			
		||||
        passed: false
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        prompt: 'Would you like to start a new chat session with this profile?',
 | 
			
		||||
        checkPrompt: (setting, newVal, oldVal) => {
 | 
			
		||||
          return chat.sessionStarted && newVal !== originalProfile &&
 | 
			
		||||
            (getProfile(newVal).characterName !== chatSettings.characterName)
 | 
			
		||||
        },
 | 
			
		||||
        onYes: (setting, newVal, oldVal) => {
 | 
			
		||||
          // start new char session, apply this profile, amd start it
 | 
			
		||||
          setChatSettingValue(chatId, setting, oldVal)
 | 
			
		||||
          const profile = getProfile(newVal)
 | 
			
		||||
          const newChatId = addChat(profile)
 | 
			
		||||
          replace(`/chat/${newChatId}`)
 | 
			
		||||
          return true
 | 
			
		||||
        },
 | 
			
		||||
        onNo: (setting, newVal, oldVal) => true, // Continue on no
 | 
			
		||||
        passed: false
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        prompt: 'Personality change will not correctly apply to existing chat session.\n Continue?',
 | 
			
		||||
        checkPrompt: (setting, newVal, oldVal) => {
 | 
			
		||||
          return chat.sessionStarted && newVal !== originalProfile &&
 | 
			
		||||
            (getProfile(newVal).characterName !== chatSettings.characterName)
 | 
			
		||||
        },
 | 
			
		||||
        onYes: (setting, newVal, oldVal) => true,
 | 
			
		||||
        passed: false
 | 
			
		||||
      }
 | 
			
		||||
      // {
 | 
			
		||||
      //   prompt: 'Would you like to start a new chat session with this profile?',
 | 
			
		||||
      //   checkPrompt: (setting, newVal, oldVal) => {
 | 
			
		||||
      //     return chat.sessionStarted && newVal !== originalProfile &&
 | 
			
		||||
      //       (getProfile(newVal).characterName !== chatSettings.characterName)
 | 
			
		||||
      //   },
 | 
			
		||||
      //   onYes: (setting, newVal, oldVal) => {
 | 
			
		||||
      //     // start new char session, apply this profile, amd start it
 | 
			
		||||
      //     setChatSettingValue(chatId, setting, oldVal)
 | 
			
		||||
      //     const profile = getProfile(newVal)
 | 
			
		||||
      //     const newChatId = addChat(profile)
 | 
			
		||||
      //     replace(`/chat/${newChatId}`)
 | 
			
		||||
      //     return true
 | 
			
		||||
      //   },
 | 
			
		||||
      //   onNo: (setting, newVal, oldVal) => true, // Continue on no
 | 
			
		||||
      //   passed: false
 | 
			
		||||
      // },
 | 
			
		||||
      // {
 | 
			
		||||
      //   prompt: 'Personality change will not correctly apply to existing chat session.\n Continue?',
 | 
			
		||||
      //   checkPrompt: (setting, newVal, oldVal) => {
 | 
			
		||||
      //     return chat.sessionStarted && newVal !== originalProfile &&
 | 
			
		||||
      //       (getProfile(newVal).characterName !== chatSettings.characterName)
 | 
			
		||||
      //   },
 | 
			
		||||
      //   onYes: (setting, newVal, oldVal) => true,
 | 
			
		||||
      //   passed: false
 | 
			
		||||
      // }
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -98,7 +98,7 @@
 | 
			
		|||
      if (val === newVal) return
 | 
			
		||||
      try {
 | 
			
		||||
        if ((typeof setting.afterChange === 'function') && setting.afterChange(chatId, setting, chatSettings[setting.key])) {
 | 
			
		||||
          console.log('Refreshed from setting', setting.key, chatSettings[setting.key], val)
 | 
			
		||||
          // console.log('Refreshed from setting', setting.key, chatSettings[setting.key], val)
 | 
			
		||||
          refreshSettings()
 | 
			
		||||
        }
 | 
			
		||||
      } catch (e) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,7 +15,7 @@
 | 
			
		|||
    addChat
 | 
			
		||||
 | 
			
		||||
  } from './Storage.svelte'
 | 
			
		||||
  import { supportedModels, type Chat, type ChatSetting, type ResponseModels, type SettingSelect, type SelectOption } from './Types.svelte'
 | 
			
		||||
  import { supportedModels, type Chat, type ChatSetting, type ResponseModels, type SettingSelect, type SelectOption, type ChatSettings } from './Types.svelte'
 | 
			
		||||
  import { sizeTextElements } from './Util.svelte'
 | 
			
		||||
  import Fa from 'svelte-fa/src/fa.svelte'
 | 
			
		||||
  import {
 | 
			
		||||
| 
						 | 
				
			
			@ -29,7 +29,7 @@
 | 
			
		|||
    faSquarePlus
 | 
			
		||||
  } from '@fortawesome/free-solid-svg-icons/index'
 | 
			
		||||
  import { exportProfileAsJSON } from './Export.svelte'
 | 
			
		||||
  import { afterUpdate } from 'svelte'
 | 
			
		||||
  import { onMount, afterUpdate } from 'svelte'
 | 
			
		||||
  import ChatSettingField from './ChatSettingField.svelte'
 | 
			
		||||
  import { getModelMaxTokens } from './Stats.svelte'
 | 
			
		||||
  import { replace } from 'svelte-spa-router'
 | 
			
		||||
| 
						 | 
				
			
			@ -55,13 +55,25 @@
 | 
			
		|||
  $: chatSettings = chat.settings
 | 
			
		||||
  $: globalStore = $globalStorage
 | 
			
		||||
 | 
			
		||||
  const originalProfile = chatSettings && chatSettings.profile
 | 
			
		||||
  let originalProfile:string
 | 
			
		||||
  let originalSettings:ChatSettings
 | 
			
		||||
 | 
			
		||||
  onMount(async () => {
 | 
			
		||||
    originalProfile = chatSettings && chatSettings.profile
 | 
			
		||||
    originalSettings = chatSettings && JSON.parse(JSON.stringify(chatSettings))
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  afterUpdate(() => {
 | 
			
		||||
    if (!originalProfile) {
 | 
			
		||||
      originalProfile = chatSettings && chatSettings.profile
 | 
			
		||||
      originalSettings = chatSettings && JSON.parse(JSON.stringify(chatSettings))
 | 
			
		||||
    }
 | 
			
		||||
    sizeTextElements()
 | 
			
		||||
  })
 | 
			
		||||
  
 | 
			
		||||
  const closeSettings = () => {
 | 
			
		||||
    originalProfile = ''
 | 
			
		||||
    originalSettings = {} as ChatSettings
 | 
			
		||||
    showProfileMenu = false
 | 
			
		||||
    $checkStateChange++
 | 
			
		||||
    showSettingsModal = 0
 | 
			
		||||
| 
						 | 
				
			
			@ -207,12 +219,18 @@
 | 
			
		|||
  }
 | 
			
		||||
 | 
			
		||||
  const startNewChat = () => {
 | 
			
		||||
    const differentProfile = originalSettings.profile !== chatSettings.profile
 | 
			
		||||
    // start new
 | 
			
		||||
    const newChatId = addChat(chatSettings)
 | 
			
		||||
    // restore original
 | 
			
		||||
    if (differentProfile) {
 | 
			
		||||
      chat.settings = originalSettings
 | 
			
		||||
      saveChatStore()
 | 
			
		||||
    }
 | 
			
		||||
    // go to new chat
 | 
			
		||||
    replace(`/chat/${newChatId}`)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // excludeFromProfile
 | 
			
		||||
 | 
			
		||||
  const deepEqual = (x:any, y:any) => {
 | 
			
		||||
    const ok = Object.keys; const tx = typeof x; const ty = typeof y
 | 
			
		||||
    return x && y && tx === 'object' && tx === ty
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,19 +1,25 @@
 | 
			
		|||
<script context="module" lang="ts">
 | 
			
		||||
  import { getChatDefaults, getExcludeFromProfile } from './Settings.svelte'
 | 
			
		||||
  import { get, writable } from 'svelte/store'
 | 
			
		||||
  // Profile definitions
 | 
			
		||||
import { addMessage, clearMessages, getChat, getChatSettings, getCustomProfiles, getGlobalSettings, resetChatSettings, saveChatStore, setGlobalSettingValueByKey } from './Storage.svelte'
 | 
			
		||||
  import { addMessage, clearMessages, getChat, getChatSettings, getCustomProfiles, getGlobalSettings, newName, resetChatSettings, saveChatStore, setGlobalSettingValueByKey } from './Storage.svelte'
 | 
			
		||||
  import type { Message, SelectOption, ChatSettings } from './Types.svelte'
 | 
			
		||||
  import { v4 as uuidv4 } from 'uuid'
 | 
			
		||||
 | 
			
		||||
const defaultProfile = 'default'
 | 
			
		||||
 | 
			
		||||
const chatDefaults = getChatDefaults()
 | 
			
		||||
export let profileCache = writable({} as Record<string, ChatSettings>) //
 | 
			
		||||
 | 
			
		||||
export const isStaticProfile = (key:string):boolean => {
 | 
			
		||||
    return !!profiles[key]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getProfiles = ():Record<string, ChatSettings> => {
 | 
			
		||||
export const getProfiles = (forceUpdate:boolean = false):Record<string, ChatSettings> => {
 | 
			
		||||
    const pc = get(profileCache)
 | 
			
		||||
    if (!forceUpdate && Object.keys(pc).length) {
 | 
			
		||||
      return pc
 | 
			
		||||
    }
 | 
			
		||||
    const result = Object.entries(profiles
 | 
			
		||||
    ).reduce((a, [k, v]) => {
 | 
			
		||||
      a[k] = v
 | 
			
		||||
| 
						 | 
				
			
			@ -22,6 +28,13 @@ const getProfiles = ():Record<string, ChatSettings> => {
 | 
			
		|||
    Object.entries(getCustomProfiles()).forEach(([k, v]) => {
 | 
			
		||||
      result[k] = v
 | 
			
		||||
    })
 | 
			
		||||
    Object.entries(result).forEach(([k, v]) => {
 | 
			
		||||
      pc[k] = v
 | 
			
		||||
    })
 | 
			
		||||
    Object.keys(pc).forEach((k) => {
 | 
			
		||||
      if (!(k in result)) delete pc[k]
 | 
			
		||||
    })
 | 
			
		||||
    profileCache.set(pc)
 | 
			
		||||
    return result
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -98,6 +111,11 @@ export const restartProfile = (chatId:number, noApply:boolean = false) => {
 | 
			
		|||
    setGlobalSettingValueByKey('lastProfile', settings.profile)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const newNameForProfile = (name:string) => {
 | 
			
		||||
    const profiles = getProfileSelect()
 | 
			
		||||
    return newName(name, profiles.reduce((a, p) => { a[p.text] = p; return a }, {}))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Apply currently selected profile
 | 
			
		||||
export const applyProfile = (chatId:number, key:string = '', resetChat:boolean = false) => {
 | 
			
		||||
    resetChatSettings(chatId, resetChat) // Fully reset
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 { getProfile, isStaticProfile, restartProfile } from './Profiles.svelte'
 | 
			
		||||
  import { getProfile, getProfiles, isStaticProfile, newNameForProfile, restartProfile } from './Profiles.svelte'
 | 
			
		||||
 | 
			
		||||
  export const chatsStorage = persisted('chats', [] as Chat[])
 | 
			
		||||
  export const globalStorage = persisted('global', {} as GlobalSettings)
 | 
			
		||||
| 
						 | 
				
			
			@ -370,9 +370,6 @@
 | 
			
		|||
      store.profiles = profiles
 | 
			
		||||
    }
 | 
			
		||||
    if (!profile.profile) profile.profile = uuidv4()
 | 
			
		||||
    if (isStaticProfile(profile.profile as any)) {
 | 
			
		||||
      throw new Error('Sorry, you can\'t modify a static profile. You can clone it though!')
 | 
			
		||||
    }
 | 
			
		||||
    const mt = profile.profileName && profile.profileName.trim().toLocaleLowerCase()
 | 
			
		||||
    const sameTitle = Object.values(profiles).find(c => c.profile !== profile.profile &&
 | 
			
		||||
    c.profileName && c.profileName.trim().toLocaleLowerCase() === mt)
 | 
			
		||||
| 
						 | 
				
			
			@ -385,6 +382,12 @@
 | 
			
		|||
    if (!profile.characterName || profile.characterName.length < 3) {
 | 
			
		||||
      throw new Error('Your profile\'s character needs a valid name.')
 | 
			
		||||
    }
 | 
			
		||||
    if (isStaticProfile(profile.profile as any)) {
 | 
			
		||||
      // throw new Error('Sorry, you can\'t modify a static profile. You can clone it though!')
 | 
			
		||||
      // Save static profile as new custom
 | 
			
		||||
      profile.profileName = newNameForProfile(profile.profileName)
 | 
			
		||||
      profile.profile = uuidv4()
 | 
			
		||||
    }
 | 
			
		||||
    const clone = JSON.parse(JSON.stringify(profile)) // Always store a copy
 | 
			
		||||
    Object.keys(getExcludeFromProfile()).forEach(k => {
 | 
			
		||||
      delete clone[k]
 | 
			
		||||
| 
						 | 
				
			
			@ -393,6 +396,18 @@
 | 
			
		|||
    globalStorage.set(store)
 | 
			
		||||
    profile.isDirty = false
 | 
			
		||||
    saveChatStore()
 | 
			
		||||
    getProfiles(true) // force update profile cache
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  export const newName = (name:string, nameMap:Record<string, any>):string => {
 | 
			
		||||
    if (!nameMap[name]) return name
 | 
			
		||||
    let i:number = 1
 | 
			
		||||
    let cname = name + `-${i}`
 | 
			
		||||
    while (nameMap[cname]) {
 | 
			
		||||
      i++
 | 
			
		||||
      cname = name + `-${i}`
 | 
			
		||||
    }
 | 
			
		||||
    return cname
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
</script>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue