Simplify switching profiles. Less prompting.

This commit is contained in:
Webifi 2023-06-04 15:22:02 -05:00
parent 759eb35bc6
commit 49c7570952
4 changed files with 199 additions and 148 deletions

View File

@ -1,10 +1,10 @@
<script lang="ts"> <script lang="ts">
import { createEventDispatcher } from 'svelte' import { createEventDispatcher } from 'svelte'
import { getProfile } from './Profiles.svelte' // import { getProfile } from './Profiles.svelte'
import { addChat, cleanSettingValue, setChatSettingValue } from './Storage.svelte' import { cleanSettingValue, setChatSettingValue } from './Storage.svelte'
import type { Chat, ChatSetting, ChatSettings, ControlAction, FieldControl, SettingPrompt } from './Types.svelte' import type { Chat, ChatSetting, ChatSettings, ControlAction, FieldControl, SettingPrompt } from './Types.svelte'
import { autoGrowInputOnEvent } from './Util.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' import Fa from 'svelte-fa/src/fa.svelte'
export let setting:ChatSetting export let setting:ChatSetting
@ -37,33 +37,33 @@
return !!chatSettings.isDirty && newVal !== oldVal return !!chatSettings.isDirty && newVal !== oldVal
}, },
passed: false 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 if (val === newVal) return
try { try {
if ((typeof setting.afterChange === 'function') && setting.afterChange(chatId, setting, chatSettings[setting.key])) { 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() refreshSettings()
} }
} catch (e) { } catch (e) {

View File

@ -15,7 +15,7 @@
addChat addChat
} from './Storage.svelte' } 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 { sizeTextElements } from './Util.svelte'
import Fa from 'svelte-fa/src/fa.svelte' import Fa from 'svelte-fa/src/fa.svelte'
import { import {
@ -29,7 +29,7 @@
faSquarePlus faSquarePlus
} from '@fortawesome/free-solid-svg-icons/index' } from '@fortawesome/free-solid-svg-icons/index'
import { exportProfileAsJSON } from './Export.svelte' import { exportProfileAsJSON } from './Export.svelte'
import { afterUpdate } from 'svelte' import { onMount, afterUpdate } from 'svelte'
import ChatSettingField from './ChatSettingField.svelte' import ChatSettingField from './ChatSettingField.svelte'
import { getModelMaxTokens } from './Stats.svelte' import { getModelMaxTokens } from './Stats.svelte'
import { replace } from 'svelte-spa-router' import { replace } from 'svelte-spa-router'
@ -55,13 +55,25 @@
$: chatSettings = chat.settings $: chatSettings = chat.settings
$: globalStore = $globalStorage $: 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(() => { afterUpdate(() => {
if (!originalProfile) {
originalProfile = chatSettings && chatSettings.profile
originalSettings = chatSettings && JSON.parse(JSON.stringify(chatSettings))
}
sizeTextElements() sizeTextElements()
}) })
const closeSettings = () => { const closeSettings = () => {
originalProfile = ''
originalSettings = {} as ChatSettings
showProfileMenu = false showProfileMenu = false
$checkStateChange++ $checkStateChange++
showSettingsModal = 0 showSettingsModal = 0
@ -207,12 +219,18 @@
} }
const startNewChat = () => { const startNewChat = () => {
const differentProfile = originalSettings.profile !== chatSettings.profile
// start new
const newChatId = addChat(chatSettings) const newChatId = addChat(chatSettings)
// restore original
if (differentProfile) {
chat.settings = originalSettings
saveChatStore()
}
// go to new chat
replace(`/chat/${newChatId}`) replace(`/chat/${newChatId}`)
} }
// excludeFromProfile
const deepEqual = (x:any, y:any) => { const deepEqual = (x:any, y:any) => {
const ok = Object.keys; const tx = typeof x; const ty = typeof y const ok = Object.keys; const tx = typeof x; const ty = typeof y
return x && y && tx === 'object' && tx === ty return x && y && tx === 'object' && tx === ty

View File

@ -1,19 +1,25 @@
<script context="module" lang="ts"> <script context="module" lang="ts">
import { getChatDefaults, getExcludeFromProfile } from './Settings.svelte' import { getChatDefaults, getExcludeFromProfile } from './Settings.svelte'
import { get, writable } from 'svelte/store'
// Profile definitions // 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 type { Message, SelectOption, ChatSettings } from './Types.svelte'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
const defaultProfile = 'default' const defaultProfile = 'default'
const chatDefaults = getChatDefaults() const chatDefaults = getChatDefaults()
export let profileCache = writable({} as Record<string, ChatSettings>) //
export const isStaticProfile = (key:string):boolean => { export const isStaticProfile = (key:string):boolean => {
return !!profiles[key] 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 const result = Object.entries(profiles
).reduce((a, [k, v]) => { ).reduce((a, [k, v]) => {
a[k] = v a[k] = v
@ -22,6 +28,13 @@ const getProfiles = ():Record<string, ChatSettings> => {
Object.entries(getCustomProfiles()).forEach(([k, v]) => { Object.entries(getCustomProfiles()).forEach(([k, v]) => {
result[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 return result
} }
@ -98,6 +111,11 @@ export const restartProfile = (chatId:number, noApply:boolean = false) => {
setGlobalSettingValueByKey('lastProfile', settings.profile) 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 // Apply currently selected profile
export const applyProfile = (chatId:number, key:string = '', resetChat:boolean = false) => { export const applyProfile = (chatId:number, key:string = '', resetChat:boolean = false) => {
resetChatSettings(chatId, resetChat) // Fully reset resetChatSettings(chatId, resetChat) // Fully reset

View File

@ -4,7 +4,7 @@
import type { Chat, ChatSettings, GlobalSettings, Message, ChatSetting, GlobalSetting, Usage, Model } from './Types.svelte' import type { Chat, ChatSettings, GlobalSettings, Message, ChatSetting, GlobalSetting, Usage, Model } from './Types.svelte'
import { getChatSettingObjectByKey, getGlobalSettingObjectByKey, getChatDefaults, getExcludeFromProfile } from './Settings.svelte' import { getChatSettingObjectByKey, getGlobalSettingObjectByKey, getChatDefaults, getExcludeFromProfile } from './Settings.svelte'
import { v4 as uuidv4 } from 'uuid' 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 chatsStorage = persisted('chats', [] as Chat[])
export const globalStorage = persisted('global', {} as GlobalSettings) export const globalStorage = persisted('global', {} as GlobalSettings)
@ -370,9 +370,6 @@
store.profiles = profiles store.profiles = profiles
} }
if (!profile.profile) profile.profile = uuidv4() 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 mt = profile.profileName && profile.profileName.trim().toLocaleLowerCase()
const sameTitle = Object.values(profiles).find(c => c.profile !== profile.profile && const sameTitle = Object.values(profiles).find(c => c.profile !== profile.profile &&
c.profileName && c.profileName.trim().toLocaleLowerCase() === mt) c.profileName && c.profileName.trim().toLocaleLowerCase() === mt)
@ -385,6 +382,12 @@
if (!profile.characterName || profile.characterName.length < 3) { if (!profile.characterName || profile.characterName.length < 3) {
throw new Error('Your profile\'s character needs a valid name.') 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 const clone = JSON.parse(JSON.stringify(profile)) // Always store a copy
Object.keys(getExcludeFromProfile()).forEach(k => { Object.keys(getExcludeFromProfile()).forEach(k => {
delete clone[k] delete clone[k]
@ -393,6 +396,18 @@
globalStorage.set(store) globalStorage.set(store)
profile.isDirty = false profile.isDirty = false
saveChatStore() 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> </script>