diff --git a/src/ts/process/index.ts b/src/ts/process/index.ts index 4a1896b9..1eb3217a 100644 --- a/src/ts/process/index.ts +++ b/src/ts/process/index.ts @@ -8,7 +8,7 @@ import { loadLoreBookPrompt } from "../lorebook"; import { findCharacterbyId, replacePlaceholders } from "../util"; import { requestChatData } from "./request"; import { stableDiff } from "./stableDiff"; -import { processScript } from "./scripts"; +import { processScript, processScriptFull } from "./scripts"; export interface OpenAIChat{ role: 'system'|'user'|'assistant' @@ -282,23 +282,26 @@ export async function sendChat(chatProcessIndex = -1):Promise { }, 'model') let result = '' + let emoChanged = false if(req.type === 'fail'){ alertError(req.result) return false } else{ - result = reformatContent(req.result) + const result2 = processScriptFull(currentChar, reformatContent(req.result), 'editoutput') + result = result2.data + emoChanged = result2.emoChanged db.characters[selectedChar].chats[selectedChat].message.push({ role: 'char', data: result, - saying: processScript(currentChar,currentChar.chaId, 'editoutput') + saying: currentChar.chaId }) setDatabase(db) } - if(currentChar.viewScreen === 'emotion'){ + if(currentChar.viewScreen === 'emotion' && (!emoChanged)){ let currentEmotion = currentChar.emotionImages diff --git a/src/ts/process/scripts.ts b/src/ts/process/scripts.ts index 514985ea..291ba215 100644 --- a/src/ts/process/scripts.ts +++ b/src/ts/process/scripts.ts @@ -1,15 +1,49 @@ +import { get } from "svelte/store"; +import { CharEmotion, selectedCharID } from "../stores"; import type { character } from "../database"; const dreg = /{{data}}/g -export function processScript(char:character, data:string, mode:'editinput'|'editoutput'|'editprocess'){ +type ScriptMode = 'editinput'|'editoutput'|'editprocess' + +export function processScript(char:character, data:string, mode:ScriptMode){ + return processScriptFull(char, data, mode).data +} + +export function processScriptFull(char:character, data:string, mode:ScriptMode){ + let emoChanged = false for (const script of char.customscript){ if(script.type === mode){ const reg = new RegExp(script.in,'g') data = data.replace(reg, (v) => { + if(script.out.startsWith('@@emo ')){ + if(char.viewScreen !== 'emotion'){ + return v + } + const emoName = script.out.substring(6).trim() + let charemotions = get(CharEmotion) + let tempEmotion = charemotions[char.chaId] + if(!tempEmotion){ + tempEmotion = [] + } + if(tempEmotion.length > 4){ + tempEmotion.splice(0, 1) + } + for(const emo of char.emotionImages){ + if(emo[0] === emoName){ + const emos:[string, string,number] = [emo[0], emo[1], Date.now()] + tempEmotion.push(emos) + charemotions[char.chaId] = tempEmotion + CharEmotion.set(charemotions) + emoChanged = true + break + } + } + return v + } return script.out.replace(dreg, v) }) } } - return data + return {data, emoChanged} } \ No newline at end of file