Refactor lorebook.ts to lorebook.svelte.ts for optimization

This commit is contained in:
kwaroran
2024-10-25 18:49:12 +09:00
parent a68b5c9f59
commit 7d43075528
5 changed files with 25 additions and 32 deletions

View File

@@ -2,7 +2,7 @@
import { DBState } from "../../../ts/storage/database.svelte";
import { language } from "../../../lang";
import { DownloadIcon, FolderUpIcon, ImportIcon, PlusIcon } from "lucide-svelte";
import { addLorebook, exportLoreBook, importLoreBook } from "../../../ts/process/lorebook";
import { addLorebook, exportLoreBook, importLoreBook } from "../../../ts/process/lorebook.svelte";
import Check from "../../UI/GUI/CheckInput.svelte";
import NumberInput from "../../UI/GUI/NumberInput.svelte";
import LoreBookList from "./LoreBookList.svelte";

View File

@@ -5,7 +5,7 @@ import { alertInput, alertMd, alertNormal, alertSelect, alertToast } from "../al
import { sayTTS } from "./tts";
import { risuChatParser } from "../parser";
import { sendChat } from ".";
import { loadLoreBookV3Prompt } from "./lorebook";
import { loadLoreBookV3Prompt } from "./lorebook.svelte";
import { runTrigger } from "./triggers";
export async function processMultiCommand(command:string) {

View File

@@ -4,7 +4,7 @@ import { CharEmotion, selectedCharID } from "../stores";
import { ChatTokenizer, tokenize, tokenizeNum } from "../tokenizer";
import { language } from "../../lang";
import { alertError } from "../alert";
import { loadLoreBookV3Prompt } from "./lorebook";
import { loadLoreBookV3Prompt } from "./lorebook.svelte";
import { findCharacterbyId, getAuthorNoteDefaultText, getPersonaPrompt, getUserName, isLastCharPunctuation, trimUntilPunctuation } from "../util";
import { requestChatData } from "./request";
import { stableDiff } from "./stableDiff";

View File

@@ -1,6 +1,6 @@
import { get } from "svelte/store";
import {selectedCharID} from '../stores'
import { getDatabase, setDatabase, type Message, type loreBook } from "../storage/database.svelte";
import { DBState, type Message, type loreBook } from "../storage/database.svelte";
import { tokenize } from "../tokenizer";
import { checkNullish, selectSingleFile } from "../util";
import { alertError, alertNormal } from "../alert";
@@ -10,12 +10,11 @@ import { getModuleLorebooks } from "./modules";
import { CCardLib } from "@risuai/ccardlib";
export function addLorebook(type:number) {
let selectedID = get(selectedCharID)
let db = getDatabase()
const selectedID = get(selectedCharID)
if(type === 0){
db.characters[selectedID].globalLore.push({
DBState.db.characters[selectedID].globalLore.push({
key: '',
comment: `New Lore ${db.characters[selectedID].globalLore.length + 1}`,
comment: `New Lore ${DBState.db.characters[selectedID].globalLore.length + 1}`,
content: '',
mode: 'normal',
insertorder: 100,
@@ -25,10 +24,10 @@ export function addLorebook(type:number) {
})
}
else{
const page = db.characters[selectedID].chatPage
db.characters[selectedID].chats[page].localLore.push({
const page = DBState.db.characters[selectedID].chatPage
DBState.db.characters[selectedID].chats[page].localLore.push({
key: '',
comment: `New Lore ${db.characters[selectedID].chats[page].localLore.length + 1}`,
comment: `New Lore ${DBState.db.characters[selectedID].chats[page].localLore.length + 1}`,
content: '',
mode: 'normal',
insertorder: 100,
@@ -37,7 +36,6 @@ export function addLorebook(type:number) {
selective: false
})
}
setDatabase(db)
}
interface formatedLore{
@@ -53,16 +51,15 @@ const rmRegex = / |\n/g
export async function loadLoreBookPrompt(){
const selectedID = get(selectedCharID)
const db = getDatabase()
const char = db.characters[selectedID]
const char = DBState.db.characters[selectedID]
const page = char.chatPage
const characterLore = char.globalLore ?? []
const chatLore = char.chats[page].localLore ?? []
const moduleLorebook = getModuleLorebooks()
const fullLore = characterLore.concat(chatLore).concat(moduleLorebook)
const currentChat = char.chats[page].message
const loreDepth = char.loreSettings?.scanDepth ?? db.loreBookDepth
const loreToken = char.loreSettings?.tokenBudget ?? db.loreBookToken
const loreDepth = char.loreSettings?.scanDepth ?? DBState.db.loreBookDepth
const loreToken = char.loreSettings?.tokenBudget ?? DBState.db.loreBookToken
const fullWordMatching = char.loreSettings?.fullWordMatching ?? false
let activatiedPrompt: string[] = []
@@ -208,16 +205,15 @@ export async function loadLoreBookPrompt(){
export async function loadLoreBookV3Prompt(){
const selectedID = get(selectedCharID)
const db = getDatabase()
const char = db.characters[selectedID]
const char = DBState.db.characters[selectedID]
const page = char.chatPage
const characterLore = char.globalLore ?? []
const chatLore = char.chats[page].localLore ?? []
const moduleLorebook = getModuleLorebooks()
const fullLore = safeStructuredClone(characterLore.concat(chatLore).concat(moduleLorebook))
const currentChat = char.chats[page].message
const loreDepth = char.loreSettings?.scanDepth ?? db.loreBookDepth
const loreToken = char.loreSettings?.tokenBudget ?? db.loreBookToken
const loreDepth = char.loreSettings?.scanDepth ?? DBState.db.loreBookDepth
const loreToken = char.loreSettings?.tokenBudget ?? DBState.db.loreBookToken
const fullWordMatching = char.loreSettings?.fullWordMatching ?? false
const chatLength = currentChat.length + 1 //includes first message
const recursiveScanning = char.loreSettings?.recursiveScanning ?? false
@@ -530,11 +526,10 @@ export async function loadLoreBookV3Prompt(){
export async function importLoreBook(mode:'global'|'local'|'sglobal'){
const selectedID = get(selectedCharID)
let db = getDatabase()
const page = mode === 'sglobal' ? -1 : db.characters[selectedID].chatPage
const page = mode === 'sglobal' ? -1 : DBState.db.characters[selectedID].chatPage
let lore =
mode === 'global' ? db.characters[selectedID].globalLore :
db.characters[selectedID].chats[page].localLore
mode === 'global' ? DBState.db.characters[selectedID].globalLore :
DBState.db.characters[selectedID].chats[page].localLore
const lorebook = (await selectSingleFile(['json', 'lorebook'])).data
if(!lorebook){
return
@@ -555,12 +550,11 @@ export async function importLoreBook(mode:'global'|'local'|'sglobal'){
lore.push(...convertExternalLorebook(entries))
}
if(mode === 'global'){
db.characters[selectedID].globalLore = lore
DBState.db.characters[selectedID].globalLore = lore
}
else{
db.characters[selectedID].chats[page].localLore = lore
DBState.db.characters[selectedID].chats[page].localLore = lore
}
setDatabase(db)
} catch (error) {
alertError(`${error}`)
}
@@ -612,11 +606,10 @@ export function convertExternalLorebook(entries:{[key:string]:CCLorebook}){
export async function exportLoreBook(mode:'global'|'local'|'sglobal'){
try {
const selectedID = get(selectedCharID)
const db = getDatabase()
const page = mode === 'sglobal' ? -1 : db.characters[selectedID].chatPage
const page = mode === 'sglobal' ? -1 : DBState.db.characters[selectedID].chatPage
const lore =
mode === 'global' ? db.characters[selectedID].globalLore :
db.characters[selectedID].chats[page].localLore
mode === 'global' ? DBState.db.characters[selectedID].globalLore :
DBState.db.characters[selectedID].chats[page].localLore
const stringl = Buffer.from(JSON.stringify({
type: 'risu',
ver: 1,

View File

@@ -4,7 +4,7 @@ import { getCurrentCharacter, getCurrentChat, getDatabase, setCurrentCharacter,
import { AppendableBuffer, downloadFile, isNodeServer, isTauri, readImage, saveAsset } from "../storage/globalApi"
import { selectSingleFile, sleep } from "../util"
import { v4 } from "uuid"
import { convertExternalLorebook } from "./lorebook"
import { convertExternalLorebook } from "./lorebook.svelte"
import { decodeRPack, encodeRPack } from "../rpack/rpack_bg"
import { convertImage } from "../parser"
import { Capacitor } from "@capacitor/core"