refactor name and icon get

This commit is contained in:
kwaroran
2024-07-29 23:36:52 +09:00
parent 47c3fab589
commit 9d5b5da351
13 changed files with 73 additions and 45 deletions

View File

@@ -7,7 +7,7 @@
import { CopyIcon, LanguagesIcon, RefreshCcwIcon } from "lucide-svelte";
import { alertConfirm } from "src/ts/alert";
import { language } from "src/lang";
import { replacePlaceholders } from "../../ts/util";
import { getUserName, replacePlaceholders } from "../../ts/util";
import { onDestroy } from 'svelte';
import { processScript } from "src/ts/process/scripts";
import { get } from "svelte/store";
@@ -71,7 +71,7 @@
}
,{
role: 'user',
content: lastMessages.map(b=>(b.role==='char'? currentChar.name : $DataBase.username)+":"+b.data).reduce((a,b)=>a+','+b)
content: lastMessages.map(b=>(b.role==='char'? currentChar.name : getUserName())+":"+b.data).reduce((a,b)=>a+','+b)
}
]

View File

@@ -5,10 +5,11 @@
import Check from "src/lib/UI/GUI/CheckInput.svelte";
import TextAreaInput from "src/lib/UI/GUI/TextAreaInput.svelte";
import TextInput from "src/lib/UI/GUI/TextInput.svelte";
import { alertConfirm, alertError, alertSelect } from "src/ts/alert";
import { alertConfirm, alertSelect } from "src/ts/alert";
import { getCharImage } from "src/ts/characters";
import { changeUserPersona, exportUserPersona, importUserPersona, saveUserPersona, selectUserImg } from "src/ts/persona";
import { DataBase, setDatabase } from "src/ts/storage/database";
import { CurrentUserIcon } from "src/ts/stores";
import { get } from "svelte/store";
</script>
@@ -67,10 +68,10 @@
<div class="flex w-full items-starts rounded-md bg-darkbg p-4 max-w-full flex-wrap">
<div class="flex flex-col mt-4 mr-4">
<button on:click={() => {selectUserImg()}}>
{#if $DataBase.userIcon === ''}
{#if $CurrentUserIcon === ''}
<div class="rounded-md h-28 w-28 shadow-lg bg-textcolor2 cursor-pointer hover:text-green-500" />
{:else}
{#await getCharImage($DataBase.userIcon, $DataBase.personas[$DataBase.selectedPersona].largePortrait ? 'lgcss' : 'css')}
{#await getCharImage($CurrentUserIcon, $DataBase.personas[$DataBase.selectedPersona].largePortrait ? 'lgcss' : 'css')}
<div class="rounded-md h-28 w-28 shadow-lg bg-textcolor2 cursor-pointer hover:text-green-500" />
{:then im}
<div class="rounded-md h-28 w-28 shadow-lg bg-textcolor2 cursor-pointer hover:text-green-500" style={im} />

View File

@@ -3,7 +3,7 @@ import { DataBase, saveImage, setDatabase, type character, type Chat, defaultSdD
import { alertConfirm, alertError, alertNormal, alertSelect, alertStore, alertWait } from "./alert";
import { language } from "../lang";
import { decode as decodeMsgpack } from "msgpackr";
import { checkNullish, findCharacterbyId, selectMultipleFile, selectSingleFile, sleep } from "./util";
import { checkNullish, findCharacterbyId, getUserName, selectMultipleFile, selectSingleFile, sleep } from "./util";
import { v4 as uuidv4 } from 'uuid';
import { selectedCharID } from "./stores";
import { checkCharOrder, downloadFile, getFileSrc } from "./storage/globalApi";
@@ -197,7 +197,7 @@ export async function exportChat(page:number){
let i = 0
for(const v of chat.message){
alertWait(`Translating... ${i++}/${chat.message.length}`)
const name = v.saying ? findCharacterbyId(v.saying).name : v.role === 'char' ? char.name : anonymous ? '×××' : db.username
const name = v.saying ? findCharacterbyId(v.saying).name : v.role === 'char' ? char.name : anonymous ? '×××' : getUserName()
chatContentHTML += `<div class="chat">
<h2>${name}</h2>
<div>${await htmlChatParse(v.data)}</div>
@@ -268,7 +268,7 @@ export async function exportChat(page:number){
let i = 0
for(const v of chat.message){
alertWait(`Translating... ${i++}/${chat.message.length}`)
const name = v.saying ? findCharacterbyId(v.saying).name : v.role === 'char' ? char.name : anonymous ? '×××' : db.username
const name = v.saying ? findCharacterbyId(v.saying).name : v.role === 'char' ? char.name : anonymous ? '×××' : getUserName()
chatContentHTML += `<tr>
<td>${name}</td>
<td>${await htmlChatParse(v.data)}</td>
@@ -309,7 +309,7 @@ export async function exportChat(page:number){
return `--${findCharacterbyId(v.saying).name}\n${v.data}`
}
else{
return `--${v.role === 'char' ? char.name : db.username}\n${v.data}`
return `--${v.role === 'char' ? char.name : getUserName()}\n${v.data}`
}
}).join('\n\n')
@@ -409,7 +409,7 @@ export async function importChat(){
function formatTavernChat(chat:string, charName:string){
const db = get(DataBase)
return chat.replace(/<([Uu]ser)>|\{\{([Uu]ser)\}\}/g, db.username).replace(/((\{\{)|<)([Cc]har)(=.+)?((\}\})|>)/g, charName)
return chat.replace(/<([Uu]ser)>|\{\{([Uu]ser)\}\}/g, getUserName()).replace(/((\{\{)|<)([Cc]har)(=.+)?((\}\})|>)/g, charName)
}
export function characterFormatUpdate(index:number|character){

View File

@@ -7,7 +7,7 @@ import { get } from 'svelte/store';
import css from '@adobe/css-tools'
import { CurrentCharacter, CurrentChat, SizeStore, selectedCharID } from './stores';
import { calcString } from './process/infunctions';
import { findCharacterbyId, parseKeyValue, sfc32, sleep, uuidtoNumber } from './util';
import { findCharacterbyId, getUserIcon, getUserName, parseKeyValue, sfc32, sleep, uuidtoNumber } from './util';
import { getInlayImage, writeInlayImage } from './process/files/image';
import { getModuleLorebooks } from './process/modules';
import { HypaProcesser } from './process/memory/hypamemory';
@@ -333,7 +333,7 @@ async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|c
)
data = data.replace(/\uE9b4USER\uE9b4/g,
db.userIcon ? (await getFileSrc(db.userIcon)) : ''
getUserIcon() ? (await getFileSrc(getUserIcon())) : ''
)
}
@@ -661,7 +661,7 @@ function basicMatcher (p1:string,matcherArg:matcherArg,vars:{[key:string]:string
if(matcherArg.consistantChar){
return 'username'
}
return db.username
return getUserName()
}
case 'personality':
case 'char_persona':{
@@ -1571,7 +1571,7 @@ const smMatcher = (p1:string,matcherArg:matcherArg) => {
if(matcherArg.consistantChar){
return 'username'
}
return db.username
return getUserName()
}
}
}

View File

@@ -1,6 +1,6 @@
import { get } from "svelte/store"
import { DataBase, saveImage, setDatabase } from "./storage/database"
import { selectSingleFile, sleep } from "./util"
import { getUserName, selectSingleFile, sleep } from "./util"
import { alertError, alertNormal, alertStore } from "./alert"
import { downloadFile, readImage } from "./storage/globalApi"
import { language } from "src/lang"
@@ -17,7 +17,7 @@ export async function selectUserImg() {
const imgp = await saveImage(img)
db.userIcon = imgp
db.personas[db.selectedPersona] = {
name: db.username,
name: getUserName(),
icon: db.userIcon,
personaPrompt: db.personaPrompt
}
@@ -27,7 +27,7 @@ export async function selectUserImg() {
export function saveUserPersona() {
let db = get(DataBase)
db.personas[db.selectedPersona] = {
name: db.username,
name: getUserName(),
icon: db.userIcon,
personaPrompt: db.personaPrompt,
largePortrait: db.personas[db.selectedPersona]?.largePortrait,

View File

@@ -3,6 +3,7 @@ import { HypaProcesser } from '../memory/hypamemory'
import type { OpenAIChat } from "..";
import { stringlizeChat } from "../stringlize";
import { get } from "svelte/store";
import { getUserName } from "src/ts/util";
export async function additionalInformations(char: character,chats:Chat,){
const processer = new HypaProcesser('MiniLM')
@@ -18,7 +19,7 @@ export async function additionalInformations(char: character,chats:Chat,){
if(!name){
if(chat.role === 'user'){
name = db.username
name = getUserName()
}
else{
name = char.name

View File

@@ -5,7 +5,7 @@ import { ChatTokenizer, tokenize, tokenizeNum } from "../tokenizer";
import { language } from "../../lang";
import { alertError } from "../alert";
import { loadLoreBookPrompt, loadLoreBookV3Prompt } from "./lorebook";
import { findCharacterbyId, getAuthorNoteDefaultText, isLastCharPunctuation, trimUntilPunctuation } from "../util";
import { findCharacterbyId, getAuthorNoteDefaultText, getUserName, isLastCharPunctuation, trimUntilPunctuation } from "../util";
import { requestChatData } from "./request";
import { stableDiff } from "./stableDiff";
import { processScript, processScriptFull, risuChatParser } from "./scripts";
@@ -574,7 +574,7 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
}
}
const examples = exampleMessage(currentChar, db.username)
const examples = exampleMessage(currentChar, getUserName())
for(const example of examples){
currentTokens += await tokenizer.tokenizeChat(example)
@@ -634,7 +634,7 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
}
}
else if(msg.role === 'user'){
name = `${db.username}`
name = `${getUserName()}`
}
if(!msg.chatId){
msg.chatId = v4()

View File

@@ -7,6 +7,7 @@ import { HypaProcesser } from "./hypamemory";
import { stringlizeChat } from "../stringlize";
import { globalFetch } from "src/ts/storage/globalApi";
import { runSummarizer } from "../transformers";
import { getUserName } from "src/ts/util";
export async function supaMemory(
chats:OpenAIChat[],
@@ -329,7 +330,7 @@ export async function supaMemory(
if((chunkSize + tokens) > maxChunkSize){
if(stringlizedChat === ''){
if(cont.role !== 'function' && cont.role !== 'system'){
stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : db.username}: ${cont.content}\n\n`
stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : getUserName()}: ${cont.content}\n\n`
spiceLen += 1
currentTokens -= tokens
chunkSize += tokens
@@ -338,7 +339,7 @@ export async function supaMemory(
lastId = cont.memo
break
}
stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : db.username}: ${cont.content}\n\n`
stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : getUserName()}: ${cont.content}\n\n`
spiceLen += 1
currentTokens -= tokens
chunkSize += tokens

View File

@@ -3,7 +3,7 @@ import type { OpenAIChat } from ".."
import { get } from "svelte/store"
import { globalFetch } from "src/ts/storage/globalApi"
import { alertError, alertInput, alertNormal, alertWait } from "src/ts/alert"
import { sleep } from "src/ts/util"
import { getUserName, sleep } from "src/ts/util"
export function stringlizeNAIChat(formated:OpenAIChat[], char:string, continued: boolean){
const db = get(DataBase)
@@ -34,7 +34,7 @@ export function stringlizeNAIChat(formated:OpenAIChat[], char:string, continued:
res += '> '
}
if(db.NAIappendName){
res += db.username + ": "
res += getUserName() + ": "
}
res += form.content
resultString.push(res)

View File

@@ -1,6 +1,7 @@
import { get } from "svelte/store";
import type { OpenAIChat } from ".";
import { DataBase } from "../storage/database";
import { getUserName } from "../util";
export function multiChatReplacer(){
@@ -57,7 +58,7 @@ export function stringlizeChatOba(formated:OpenAIChat[], characterName:string, s
let name = form.name
if(form.role === 'user'){
prefix = appendWhitespace(suggesting ? assistantPrefix : userPrefix, seperator)
name ??= `${db.username}`
name ??= `${getUserName()}`
name += ': '
}
else if(form.role === 'assistant'){
@@ -80,7 +81,7 @@ export function stringlizeChatOba(formated:OpenAIChat[], characterName:string, s
if(!continued){
if(db.ooba.formating.useName){
if (suggesting){
resultString.push(appendWhitespace(assistantPrefix, seperator) + `${db.username}:\n` + db.autoSuggestPrefix)
resultString.push(appendWhitespace(assistantPrefix, seperator) + `${getUserName()}:\n` + db.autoSuggestPrefix)
} else {
resultString.push(assistantPrefix + `${characterName}:`)
}
@@ -173,17 +174,17 @@ export function getUnstringlizerChunks(formated:OpenAIChat[], char:string, mode:
chunks.push(`${char} `)
}
}
if(db.username){
charNames.push(db.username)
if(getUserName()){
charNames.push(getUserName())
if(mode === 'ain'){
chunks.push(`${db.username} `)
chunks.push(`${db.username} `)
chunks.push(`${getUserName()} `)
chunks.push(`${getUserName()} `)
}
else{
chunks.push(`${db.username}:`)
chunks.push(`${db.username}`)
chunks.push(`${db.username}: `)
chunks.push(`${db.username} `)
chunks.push(`${getUserName()}:`)
chunks.push(`${getUserName()}`)
chunks.push(`${getUserName()}: `)
chunks.push(`${getUserName()} `)
}
}
@@ -223,7 +224,7 @@ export function stringlizeAINChat(formated:OpenAIChat[], char:string, continued:
resultString.push(form.content)
}
else if(form.role === 'user'){
resultString.push(...formatToAIN(db.username, form.content))
resultString.push(...formatToAIN(getUserName(), form.content))
}
else if(form.name || form.role === 'assistant'){
resultString.push(...formatToAIN(form.name ?? char, form.content))
@@ -315,7 +316,7 @@ export function unstringlizeAIN(data:string,formated:OpenAIChat[], char:string =
}
}
else{
const role = (cont.character.trim() === db.username ? 'user' : 'char')
const role = (cont.character.trim() === getUserName() ? 'user' : 'char')
result.push([
role,
`${cont.content}`

View File

@@ -3,6 +3,7 @@ import type { OpenAIChat } from '..';
import { get } from 'svelte/store';
import { DataBase } from 'src/ts/storage/database';
import { CurrentCharacter } from 'src/ts/stores';
import { getUserName } from 'src/ts/util';
export const chatTemplates = {
'llama3': "{% set bos_token = '<|begin_of_text|>' %}{% set loop_messages = messages %}{% for message in loop_messages %}{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' %}{% if loop.index0 == 0 %}{% set content = bos_token + content %}{% endif %}{{ content }}{% endfor %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}",
@@ -93,6 +94,6 @@ export const applyChatTemplate = (messages:OpenAIChat[]) => {
"messages": formatedMessages,
"add_generation_prompt": true,
"risu_char": currentChar.name,
"risu_user": db.username
"risu_user": getUserName()
})
}

View File

@@ -2,7 +2,7 @@ import { get, writable, type Writable } from "svelte/store";
import { DataBase, type Chat, type character, type groupChat } from "./storage/database";
import { isEqual } from "lodash";
import type { simpleCharacterArgument } from "./parser";
import { sleep } from "./util";
import { getUserIcon, getUserName, sleep } from "./util";
import { getModules } from "./process/modules";
function updateSize(){
@@ -76,8 +76,8 @@ function trySync(){
CurrentCharacter.set(structuredClone(currentCharacter))
CurrentSimpleCharacter.set(createSimpleCharacter(currentCharacter))
CurrentChat.set(structuredClone(currentChat))
CurrentUsername.set(db.username)
CurrentUserIcon.set(db.userIcon)
CurrentUsername.set(getUserName())
CurrentUserIcon.set(getUserIcon())
CurrentShowMemoryLimit.set(db.showMemoryLimit)
} catch (error) {}
}
@@ -132,10 +132,10 @@ async function preInit(){
DataBase.subscribe((data) => {
updateCurrentCharacter()
if(data.username !== get(CurrentUsername)){
CurrentUsername.set(data.username)
if(getUserName() !== get(CurrentUsername)){
CurrentUsername.set(getUserName())
}
if(data.userIcon !== get(CurrentUserIcon)){
if(getUserIcon() !== get(CurrentUserIcon)){
CurrentUserIcon.set(data.userIcon)
}
if(data.showMemoryLimit !== get(CurrentShowMemoryLimit)){
@@ -189,6 +189,14 @@ async function preInit(){
return
}
if(getUserName() !== get(CurrentUsername)){
CurrentUsername.set(getUserName())
}
if(getUserIcon() !== get(CurrentUserIcon)){
CurrentUserIcon.set(getUserIcon())
}
const variablePointer = get(CurrentVariablePointer)
const currentState = structuredClone(chat?.scriptstate)

View File

@@ -103,10 +103,25 @@ export const replacePlaceholders = (msg:string, name:string) => {
let selectedChar = get(selectedCharID)
let currentChar = db.characters[selectedChar]
return msg .replace(/({{char}})|({{Char}})|(<Char>)|(<char>)/gi, currentChar.name)
.replace(/({{user}})|({{User}})|(<User>)|(<user>)/gi, db.username)
.replace(/({{user}})|({{User}})|(<User>)|(<user>)/gi, getUserName())
.replace(/(\{\{((set)|(get))var::.+?\}\})/gu,'')
}
export function getUserName(){
const db = get(DataBase)
return db.username ?? 'User'
}
export function getUserIcon(){
const db = get(DataBase)
return db.userIcon ?? ''
}
export function getUserPersonaDesc(){
const db = get(DataBase)
return db.personaPrompt ?? ''
}
export function checkIsIos(){
return /(iPad|iPhone|iPod)/g.test(navigator.userAgent)
}