diff --git a/src/lib/ChatScreens/Chat.svelte b/src/lib/ChatScreens/Chat.svelte index fb6e37bc..57103313 100644 --- a/src/lib/ChatScreens/Chat.svelte +++ b/src/lib/ChatScreens/Chat.svelte @@ -162,7 +162,7 @@ {#if editMode} {:else} - {#await ParseMarkdown(msgDisplay, character) then md} + {#await ParseMarkdown(msgDisplay, character, 'normal', idx) then md} { if($DataBase.clickToEdit && idx > -1){ diff --git a/src/ts/parser.ts b/src/ts/parser.ts index b025a5db..540dead0 100644 --- a/src/ts/parser.ts +++ b/src/ts/parser.ts @@ -2,7 +2,7 @@ import DOMPurify from 'isomorphic-dompurify'; import showdown from 'showdown'; import { DataBase, type character, type groupChat } from './storage/database'; import { getFileSrc } from './storage/globalApi'; -import { processScript } from './process/scripts'; +import { processScript, processScriptFull } from './process/scripts'; import { get } from 'svelte/store'; import css from '@adobe/css-tools' @@ -75,14 +75,15 @@ async function parseAdditionalAssets(data:string, char:character, mode:'normal'| return data } -export async function ParseMarkdown(data:string, char:(character | groupChat) = null, mode:'normal'|'back' = 'normal') { +export async function ParseMarkdown(data:string, char:(character | groupChat) = null, mode:'normal'|'back' = 'normal', chatID=-1) { let firstParsed = '' + const orgDat = data if(char && char.type !== 'group'){ data = await parseAdditionalAssets(data, char, mode) firstParsed = data } if(char){ - data = processScript(char, data, 'editdisplay') + data = processScriptFull(char, data, 'editdisplay', chatID).data } if(firstParsed !== data && char && char.type !== 'group'){ data = await parseAdditionalAssets(data, char, mode) diff --git a/src/ts/process/index.ts b/src/ts/process/index.ts index 2b68bf97..b094f679 100644 --- a/src/ts/process/index.ts +++ b/src/ts/process/index.ts @@ -430,7 +430,7 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n const readed = (await reader.read()) if(readed.value){ result = readed.value - const result2 = processScriptFull(nowChatroom, reformatContent(result), 'editoutput') + const result2 = processScriptFull(nowChatroom, reformatContent(result), 'editoutput', msgIndex) db.characters[selectedChar].chats[selectedChat].message[msgIndex].data = result2.data emoChanged = result2.emoChanged setDatabase(db) @@ -446,7 +446,8 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n else{ const msgs = req.type === 'success' ? [['char',req.result]] as const : req.type === 'multiline' ? req.result : [] for(const msg of msgs){ - const result2 = processScriptFull(nowChatroom, reformatContent(msg[1]), 'editoutput') + const msgIndex = db.characters[selectedChar].chats[selectedChat].message.length + const result2 = processScriptFull(nowChatroom, reformatContent(msg[1]), 'editoutput', msgIndex) result = result2.data emoChanged = result2.emoChanged db.characters[selectedChar].chats[selectedChat].message.push({ diff --git a/src/ts/process/scripts.ts b/src/ts/process/scripts.ts index 827e6af6..10f90547 100644 --- a/src/ts/process/scripts.ts +++ b/src/ts/process/scripts.ts @@ -4,7 +4,7 @@ import { DataBase, setDatabase, type character, type customscript, type groupCha import { downloadFile } from "../storage/globalApi"; import { alertError, alertNormal } from "../alert"; import { language } from "src/lang"; -import { selectSingleFile } from "../util"; +import { findCharacterbyId, selectSingleFile } from "../util"; const dreg = /{{data}}/g const randomness = /\|\|\|/g @@ -52,38 +52,91 @@ export async function importRegex(){ } } -export function processScriptFull(char:character|groupChat, data:string, mode:ScriptMode){ +export function processScriptFull(char:character|groupChat, data:string, mode:ScriptMode, chatID = -1){ let db = get(DataBase) let emoChanged = false const scripts = (db.globalscript ?? []).concat(char.customscript) for (const script of scripts){ if(script.type === mode){ const reg = new RegExp(script.in, script.ableFlag ? script.flag : 'g') - const outScript = script.out - if(outScript.startsWith('@@') && reg.test(data)){ - if(outScript.startsWith('@@emo ')){ - 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 + let outScript = script.out.replaceAll("$n", "\n") + if(outScript.startsWith('@@')){ + if(reg.test(data)){ + if(outScript.startsWith('@@emo ')){ + 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 + } + } + } + if(outScript.startsWith('@@inject') && chatID !== -1){ + const selchar = db.characters[get(selectedCharID)] + selchar.chats[selchar.chatPage].message[chatID].data = data + data = data.replace(reg, "") + } + } + else{ + if(outScript.startsWith('@@repeat_back') && chatID !== -1){ + const selchar = db.characters[get(selectedCharID)] + const chat = selchar.chats[selchar.chatPage] + let lastChat = selchar.firstMsgIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[selchar.firstMsgIndex] + let pointer = chatID - 1 + while(pointer >= 0){ + if(chat.message[pointer].role === chat.message[chatID].role){ + lastChat = chat.message[pointer].data + break + } + pointer-- + } + + const r = lastChat.match(reg) + data = data + r[0] + } } } else{ + if(chatID !== -1){ + const selchar = db.characters[get(selectedCharID)] + const chat = selchar.chats[selchar.chatPage] + outScript = outScript.replace(/{{(.+?)}}/g, (v, p1:string) => { + if(p1 === 'previous_char_chat'){ + let pointer = chatID - 1 + while(pointer >= 0){ + if(chat.message[pointer].role === 'char'){ + return chat.message[pointer].data + } + pointer-- + } + return selchar.firstMsgIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[selchar.firstMsgIndex] + } + if(p1 === 'previous_user_chat'){ + let pointer = chatID - 1 + while(pointer >= 0){ + if(chat.message[pointer].role === 'user'){ + return chat.message[pointer].data + } + pointer-- + } + return selchar.firstMsgIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[selchar.firstMsgIndex] + } + return v + }) + } if(randomness.test(data)){ const list = data.split('|||') data = list[Math.floor(Math.random()*list.length)];