Change first message to chat dependent

This commit is contained in:
kwaroran
2024-09-18 23:25:02 +09:00
parent 6ad841e931
commit 85609e890e
15 changed files with 50 additions and 61 deletions

View File

@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { ParseMarkdown, risuChatParser } from "src/ts/parser"; import { ParseMarkdown, risuChatParser } from "src/ts/parser";
import { DataBase, type Database, type character, type groupChat } from "src/ts/storage/database"; import { DataBase, type Database, type character, type groupChat } from "src/ts/storage/database";
import { CurrentVariablePointer, moduleBackgroundEmbedding, ReloadGUIPointer, selectedCharID } from "src/ts/stores"; import { moduleBackgroundEmbedding, ReloadGUIPointer, selectedCharID } from "src/ts/stores";
import { onDestroy } from "svelte"; import { onDestroy } from "svelte";
let backgroundHTML = '' let backgroundHTML = ''
@@ -43,14 +43,12 @@
{#if backgroundHTML || $moduleBackgroundEmbedding} {#if backgroundHTML || $moduleBackgroundEmbedding}
{#if $selectedCharID > -1} {#if $selectedCharID > -1}
{#key $CurrentVariablePointer} {#key $ReloadGUIPointer}
{#key $ReloadGUIPointer} <div class="absolute top-0 left-0 w-full h-full">
<div class="absolute top-0 left-0 w-full h-full"> {#await ParseMarkdown(risuChatParser((backgroundHTML || '') + ($moduleBackgroundEmbedding || ''), {chara:currentChar}), currentChar, 'back') then md}
{#await ParseMarkdown(risuChatParser((backgroundHTML || '') + ($moduleBackgroundEmbedding || ''), {chara:currentChar}), currentChar, 'back') then md} {@html md}
{@html md} {/await}
{/await} </div>
</div>
{/key}
{/key} {/key}
{/if} {/if}
{/if} {/if}

View File

@@ -5,7 +5,7 @@
import { alertConfirm, alertError, alertRequestData } from "../../ts/alert"; import { alertConfirm, alertError, alertRequestData } from "../../ts/alert";
import { language } from "../../lang"; import { language } from "../../lang";
import { DataBase, type MessageGenerationInfo } from "../../ts/storage/database"; import { DataBase, type MessageGenerationInfo } from "../../ts/storage/database";
import { CurrentCharacter, CurrentChat, CurrentVariablePointer, HideIconStore, ReloadGUIPointer } from "../../ts/stores"; import { CurrentCharacter, CurrentChat, HideIconStore, ReloadGUIPointer } from "../../ts/stores";
import { translateHTML } from "../../ts/translator/translator"; import { translateHTML } from "../../ts/translator/translator";
import { risuChatParser } from "src/ts/process/scripts"; import { risuChatParser } from "src/ts/process/scripts";
import { get, type Unsubscriber } from "svelte/store"; import { get, type Unsubscriber } from "svelte/store";
@@ -146,9 +146,6 @@
const unsubscribers:Unsubscriber[] = [] const unsubscribers:Unsubscriber[] = []
onMount(()=>{ onMount(()=>{
unsubscribers.push(CurrentVariablePointer.subscribe((v) => {
displaya(message)
}))
unsubscribers.push(ReloadGUIPointer.subscribe((v) => { unsubscribers.push(ReloadGUIPointer.subscribe((v) => {
displaya(message) displaya(message)
})) }))
@@ -295,13 +292,11 @@
style:line-height="{($DataBase.lineHeight ?? 1.25) * ($DataBase.zoomsize / 100)}rem" style:line-height="{($DataBase.lineHeight ?? 1.25) * ($DataBase.zoomsize / 100)}rem"
> >
{#key $ReloadGUIPointer} {#key $ReloadGUIPointer}
{#key $CurrentVariablePointer} {#await markParsing(msgDisplay, character, 'normal', idx, translated)}
{#await markParsing(msgDisplay, character, 'normal', idx, translated)} {@html lastParsed}
{@html lastParsed} {:then md}
{:then md} {@html md}
{@html md} {/await}
{/await}
{/key}
{/key} {/key}
</span> </span>
{/if} {/if}

View File

@@ -601,32 +601,34 @@
<Chat <Chat
character={$CurrentSimpleCharacter} character={$CurrentSimpleCharacter}
name={$CurrentCharacter.name} name={$CurrentCharacter.name}
message={$CurrentCharacter.firstMsgIndex === -1 ? $CurrentCharacter.firstMessage : message={$CurrentChat.fmIndex === -1 ? $CurrentCharacter.firstMessage :
$CurrentCharacter.alternateGreetings[$CurrentCharacter.firstMsgIndex]} $CurrentCharacter.alternateGreetings[$CurrentChat.fmIndex]}
img={getCharImage($CurrentCharacter.image, 'css')} img={getCharImage($CurrentCharacter.image, 'css')}
idx={-1} idx={-1}
altGreeting={$CurrentCharacter.alternateGreetings.length > 0} altGreeting={$CurrentCharacter.alternateGreetings.length > 0}
largePortrait={$CurrentCharacter.largePortrait} largePortrait={$CurrentCharacter.largePortrait}
onReroll={() => { onReroll={() => {
const cha = $CurrentCharacter const cha = $CurrentCharacter
const chat = $CurrentChat
if(cha.type !== 'group'){ if(cha.type !== 'group'){
if (cha.firstMsgIndex >= (cha.alternateGreetings.length - 1)){ if (chat.fmIndex >= (cha.alternateGreetings.length - 1)){
cha.firstMsgIndex = -1 chat.fmIndex = -1
} }
else{ else{
cha.firstMsgIndex += 1 chat.fmIndex += 1
} }
} }
$CurrentCharacter = cha $CurrentChat = chat
}} }}
unReroll={() => { unReroll={() => {
const cha = $CurrentCharacter const cha = $CurrentCharacter
const chat = $CurrentChat
if(cha.type !== 'group'){ if(cha.type !== 'group'){
if (cha.firstMsgIndex === -1){ if (chat.fmIndex === -1){
cha.firstMsgIndex = (cha.alternateGreetings.length - 1) chat.fmIndex = (cha.alternateGreetings.length - 1)
} }
else{ else{
cha.firstMsgIndex -= 1 chat.fmIndex -= 1
} }
} }
$CurrentCharacter = cha $CurrentCharacter = cha

View File

@@ -51,15 +51,6 @@
let currentChar:character|groupChat = $CurrentCharacter; let currentChar:character|groupChat = $CurrentCharacter;
let messages:Message[] = [] let messages:Message[] = []
if(currentChar.type !== 'group'){
const firstMsg:string = currentChar.firstMsgIndex === -1 ? currentChar.firstMessage : currentChar.alternateGreetings[currentChar.firstMsgIndex]
messages.push({
role: 'char',
data: await processScript(currentChar,
replacePlaceholders(firstMsg, currentChar.name),
'editprocess')
})
}
messages = [...messages, ...currentChar.chats[currentChar.chatPage].message]; messages = [...messages, ...currentChar.chats[currentChar.chatPage].message];
let lastMessages:Message[] = messages.slice(Math.max(messages.length - 10, 0)); let lastMessages:Message[] = messages.slice(Math.max(messages.length - 10, 0));
if(lastMessages.length === 0) if(lastMessages.length === 0)

View File

@@ -2,7 +2,7 @@
import { alertConfirm, alertError } from "../../ts/alert"; import { alertConfirm, alertError } from "../../ts/alert";
import { language } from "../../lang"; import { language } from "../../lang";
import { DataBase } from "../../ts/storage/database"; import { DataBase } from "../../ts/storage/database";
import { CurrentCharacter, selectedCharID } from "../../ts/stores"; import { CurrentCharacter, ReloadGUIPointer, selectedCharID } from "../../ts/stores";
import { DownloadIcon, EditIcon, FolderUpIcon, PlusIcon, TrashIcon, XIcon } from "lucide-svelte"; import { DownloadIcon, EditIcon, FolderUpIcon, PlusIcon, TrashIcon, XIcon } from "lucide-svelte";
import { exportChat, importChat } from "../../ts/characters"; import { exportChat, importChat } from "../../ts/characters";
import { findCharacterbyId } from "../../ts/util"; import { findCharacterbyId } from "../../ts/util";
@@ -66,7 +66,7 @@
const len = $CurrentCharacter.chats.length const len = $CurrentCharacter.chats.length
let chats = $CurrentCharacter.chats let chats = $CurrentCharacter.chats
chats.unshift({ chats.unshift({
message:[], note:'', name:`New Chat ${len + 1}`, localLore:[] message:[], note:'', name:`New Chat ${len + 1}`, localLore:[], fmIndex: -1
}) })
if(cha.type === 'group'){ if(cha.type === 'group'){
cha.characters.map((c) => { cha.characters.map((c) => {
@@ -78,6 +78,7 @@
}) })
} }
$CurrentCharacter.chats = chats $CurrentCharacter.chats = chats
$ReloadGUIPointer += 1
$CurrentCharacter.chatPage = len $CurrentCharacter.chatPage = len
close() close()
}}> }}>

View File

@@ -2,7 +2,7 @@
import { language } from "../../lang"; import { language } from "../../lang";
import { tokenizeAccurate } from "../../ts/tokenizer"; import { tokenizeAccurate } from "../../ts/tokenizer";
import { DataBase, saveImage as saveAsset, type Database, type character, type groupChat } from "../../ts/storage/database"; import { DataBase, saveImage as saveAsset, type Database, type character, type groupChat } from "../../ts/storage/database";
import { CharConfigSubMenu, MobileGUI, ShowRealmFrameStore, selectedCharID } from "../../ts/stores"; import { CharConfigSubMenu, CurrentChat, MobileGUI, ShowRealmFrameStore, selectedCharID } from "../../ts/stores";
import { PlusIcon, SmileIcon, TrashIcon, UserIcon, ActivityIcon, BookIcon, User, CurlyBraces, Volume2Icon, DownloadIcon, FolderUpIcon } from 'lucide-svelte' import { PlusIcon, SmileIcon, TrashIcon, UserIcon, ActivityIcon, BookIcon, User, CurlyBraces, Volume2Icon, DownloadIcon, FolderUpIcon } from 'lucide-svelte'
import Check from "../UI/GUI/CheckInput.svelte"; import Check from "../UI/GUI/CheckInput.svelte";
import { addCharEmotion, addingEmotion, getCharImage, rmCharEmotion, selectCharImg, makeGroupImage, removeChar, changeCharImage } from "../../ts/characters"; import { addCharEmotion, addingEmotion, getCharImage, rmCharEmotion, selectCharImg, makeGroupImage, removeChar, changeCharImage } from "../../ts/characters";
@@ -966,7 +966,7 @@
<th class="font-medium cursor-pointer w-10"> <th class="font-medium cursor-pointer w-10">
<button class="hover:text-green-500" on:click={() => { <button class="hover:text-green-500" on:click={() => {
if(currentChar.type === 'character'){ if(currentChar.type === 'character'){
currentChar.data.firstMsgIndex = -1 $CurrentChat.fmIndex = -1
let alternateGreetings = currentChar.data.alternateGreetings let alternateGreetings = currentChar.data.alternateGreetings
alternateGreetings.splice(i, 1) alternateGreetings.splice(i, 1)
currentChar.data.alternateGreetings = alternateGreetings currentChar.data.alternateGreetings = alternateGreetings
@@ -1031,7 +1031,7 @@
<th class="font-medium cursor-pointer w-10"> <th class="font-medium cursor-pointer w-10">
<button class="hover:text-green-500" on:click={() => { <button class="hover:text-green-500" on:click={() => {
if(currentChar.type === 'character'){ if(currentChar.type === 'character'){
currentChar.data.firstMsgIndex = -1 $CurrentChat.fmIndex = -1
let additionalAssets = currentChar.data.additionalAssets let additionalAssets = currentChar.data.additionalAssets
additionalAssets.splice(i, 1) additionalAssets.splice(i, 1)
currentChar.data.additionalAssets = additionalAssets currentChar.data.additionalAssets = additionalAssets

View File

@@ -10,7 +10,7 @@
import { findCharacterbyId, parseKeyValue, sleep, sortableOptions } from "src/ts/util"; import { findCharacterbyId, parseKeyValue, sleep, sortableOptions } from "src/ts/util";
import CheckInput from "../UI/GUI/CheckInput.svelte"; import CheckInput from "../UI/GUI/CheckInput.svelte";
import { createMultiuserRoom } from "src/ts/sync/multiuser"; import { createMultiuserRoom } from "src/ts/sync/multiuser";
import { CurrentCharacter, MobileGUI } from "src/ts/stores"; import { CurrentCharacter, MobileGUI, ReloadGUIPointer } from "src/ts/stores";
import Sortable from 'sortablejs/modular/sortable.core.esm.js'; import Sortable from 'sortablejs/modular/sortable.core.esm.js';
import { onDestroy, onMount } from "svelte"; import { onDestroy, onMount } from "svelte";
import { v4 } from "uuid"; import { v4 } from "uuid";
@@ -69,7 +69,7 @@
const len = chara.chats.length const len = chara.chats.length
let chats = chara.chats let chats = chara.chats
chats.unshift({ chats.unshift({
message:[], note:'', name:`New Chat ${len + 1}`, localLore:[] message:[], note:'', name:`New Chat ${len + 1}`, localLore:[], fmIndex: -1
}) })
if(cha.type === 'group'){ if(cha.type === 'group'){
cha.characters.map((c) => { cha.characters.map((c) => {
@@ -82,6 +82,7 @@
} }
chara.chats = chats chara.chats = chats
chara.chatPage = 0 chara.chatPage = 0
$ReloadGUIPointer += 1
}}>New Chat</Button> }}>New Chat</Button>
<div class="flex flex-col w-full mt-2 overflow-y-auto flex-grow" bind:this={ele}> <div class="flex flex-col w-full mt-2 overflow-y-auto flex-grow" bind:this={ele}>
{#key sorted} {#key sorted}
@@ -89,6 +90,8 @@
<button data-risu-idx={i} on:click={() => { <button data-risu-idx={i} on:click={() => {
if(!editMode){ if(!editMode){
chara.chatPage = i chara.chatPage = i
$ReloadGUIPointer += 1
} }
}} class="flex items-center text-textcolor border-solid border-0 border-darkborderc p-2 cursor-pointer rounded-md"class:bg-selected={i === chara.chatPage}> }} class="flex items-center text-textcolor border-solid border-0 border-darkborderc p-2 cursor-pointer rounded-md"class:bg-selected={i === chara.chatPage}>
{#if editMode} {#if editMode}
@@ -158,6 +161,7 @@
const d = await alertConfirm(`${language.removeConfirm}${chat.name}`) const d = await alertConfirm(`${language.removeConfirm}${chat.name}`)
if(d){ if(d){
chara.chatPage = 0 chara.chatPage = 0
$ReloadGUIPointer += 1
let chats = chara.chats let chats = chara.chats
chats.splice(i, 1) chats.splice(i, 1)
chara.chats = chats chara.chats = chats

View File

@@ -249,7 +249,7 @@ export async function exportChat(page:number){
<div class="chat"> <div class="chat">
<h2>${char.name}</h2> <h2>${char.name}</h2>
<div>${await htmlChatParse( <div>${await htmlChatParse(
char.firstMsgIndex === -1 ? char.firstMessage : char.alternateGreetings?.[char.firstMsgIndex ?? 0] chat.fmIndex === -1 ? char.firstMessage : char.alternateGreetings?.[chat.fmIndex ?? 0]
)}</div> )}</div>
</div> </div>
${chatContentHTML} ${chatContentHTML}
@@ -513,6 +513,10 @@ export function characterFormatUpdate(index:number|character, arg:{
db.characters[index] = cha db.characters[index] = cha
setDatabase(db) setDatabase(db)
} }
cha.chats = cha.chats.map((v) => {
v.fmIndex ??= cha.firstMsgIndex ?? -1
return v
})
return cha return cha
} }

View File

@@ -650,7 +650,7 @@ function basicMatcher (p1:string,matcherArg:matcherArg,vars:{[key:string]:string
} }
pointer-- pointer--
} }
return selchar.firstMsgIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[selchar.firstMsgIndex] return chat.fmIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[chat.fmIndex]
} }
case 'previous_user_chat': case 'previous_user_chat':
case 'lastusermessage':{ case 'lastusermessage':{
@@ -664,7 +664,7 @@ function basicMatcher (p1:string,matcherArg:matcherArg,vars:{[key:string]:string
} }
pointer-- pointer--
} }
return selchar.firstMsgIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[selchar.firstMsgIndex] return chat.fmIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[chat.fmIndex]
} }
return '' return ''
} }
@@ -795,7 +795,8 @@ function basicMatcher (p1:string,matcherArg:matcherArg,vars:{[key:string]:string
} }
case 'first_msg_index':{ case 'first_msg_index':{
const selchar = db.characters[get(selectedCharID)] const selchar = db.characters[get(selectedCharID)]
return selchar.firstMsgIndex.toString() const chat = selchar.chats[selchar.chatPage]
return chat.fmIndex.toString()
} }
case 'blank': case 'blank':
case 'none':{ case 'none':{

View File

@@ -612,7 +612,7 @@ export async function sendChat(chatProcessIndex = -1,arg:{
} }
if(nowChatroom.type !== 'group'){ if(nowChatroom.type !== 'group'){
const firstMsg = nowChatroom.firstMsgIndex === -1 ? nowChatroom.firstMessage : nowChatroom.alternateGreetings[nowChatroom.firstMsgIndex] const firstMsg = currentChat.fmIndex === -1 ? nowChatroom.firstMessage : nowChatroom.alternateGreetings[currentChat.fmIndex]
const chat:OpenAIChat = { const chat:OpenAIChat = {
role: 'assistant', role: 'assistant',

View File

@@ -383,7 +383,7 @@ export async function loadLoreBookV3Prompt(){
if(Number.isNaN(int)){ if(Number.isNaN(int)){
return false return false
} }
if(((char.firstMsgIndex ?? -1) + 1) !== int){ if(((char.chats[page].fmIndex ?? -1) + 1) !== int){
activated = false activated = false
} }
} }

View File

@@ -2,7 +2,7 @@ import { getChatVar, hasher, risuChatParser, setChatVar, type simpleCharacterArg
import { LuaEngine, LuaFactory } from "wasmoon"; import { LuaEngine, LuaFactory } from "wasmoon";
import { DataBase, setDatabase, type Chat, type character, type groupChat } from "../storage/database"; import { DataBase, setDatabase, type Chat, type character, type groupChat } from "../storage/database";
import { get } from "svelte/store"; import { get } from "svelte/store";
import { CurrentCharacter, CurrentChat, CurrentVariablePointer, ReloadGUIPointer, selectedCharID } from "../stores"; import { CurrentCharacter, CurrentChat, ReloadGUIPointer, selectedCharID } from "../stores";
import { alertError, alertInput, alertNormal } from "../alert"; import { alertError, alertInput, alertNormal } from "../alert";
import { HypaProcesser } from "./memory/hypamemory"; import { HypaProcesser } from "./memory/hypamemory";
import { generateAIImage } from "./stableDiff"; import { generateAIImage } from "./stableDiff";

View File

@@ -180,7 +180,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
const v = outScript.split(' ', 2)[1] const v = outScript.split(' ', 2)[1]
const selchar = db.characters[get(selectedCharID)] const selchar = db.characters[get(selectedCharID)]
const chat = selchar.chats[selchar.chatPage] const chat = selchar.chats[selchar.chatPage]
let lastChat = selchar.firstMsgIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[selchar.firstMsgIndex] let lastChat = chat.fmIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[chat.fmIndex]
let pointer = chatID - 1 let pointer = chatID - 1
while(pointer >= 0){ while(pointer >= 0){
if(chat.message[pointer].role === chat.message[chatID].role){ if(chat.message[pointer].role === chat.message[chatID].role){

View File

@@ -1073,6 +1073,7 @@ export interface Chat{
modules?:string[] modules?:string[]
id?:string id?:string
bindedPersona?:string bindedPersona?:string
fmIndex?:number
} }
export interface Message{ export interface Message{

View File

@@ -40,7 +40,6 @@ export const CurrentUserIcon = writable('') as Writable<string>
export const CurrentShowMemoryLimit = writable(false) as Writable<boolean> export const CurrentShowMemoryLimit = writable(false) as Writable<boolean>
export const ShowVN = writable(false) export const ShowVN = writable(false)
export const SettingsMenuIndex = writable(-1) export const SettingsMenuIndex = writable(-1)
export const CurrentVariablePointer = writable({} as {[key:string]: string|number|boolean})
export const ReloadGUIPointer = writable(0) export const ReloadGUIPointer = writable(0)
export const OpenRealmStore = writable(false) export const OpenRealmStore = writable(false)
export const ShowRealmFrameStore = writable('') export const ShowRealmFrameStore = writable('')
@@ -231,13 +230,6 @@ async function preInit(){
if(getUserIcon() !== get(CurrentUserIcon)){ if(getUserIcon() !== get(CurrentUserIcon)){
CurrentUserIcon.set(getUserIcon()) CurrentUserIcon.set(getUserIcon())
} }
const variablePointer = get(CurrentVariablePointer)
const currentState = structuredClone(chat?.scriptstate)
if(!isEqual(variablePointer, currentState)){
CurrentVariablePointer.set(currentState)
}
}) })
} }