[feat] new regex features

This commit is contained in:
kwaroran
2023-07-07 08:20:35 +09:00
parent d39a7b362f
commit 68570dbbe0
4 changed files with 83 additions and 28 deletions

View File

@@ -162,7 +162,7 @@
{#if editMode} {#if editMode}
<AutoresizeArea bind:value={message} /> <AutoresizeArea bind:value={message} />
{:else} {:else}
{#await ParseMarkdown(msgDisplay, character) then md} {#await ParseMarkdown(msgDisplay, character, 'normal', idx) then md}
<!-- svelte-ignore a11y-click-events-have-key-events --> <!-- svelte-ignore a11y-click-events-have-key-events -->
<span class="text chat chattext prose prose-invert minw-0" on:click={() => { <span class="text chat chattext prose prose-invert minw-0" on:click={() => {
if($DataBase.clickToEdit && idx > -1){ if($DataBase.clickToEdit && idx > -1){

View File

@@ -2,7 +2,7 @@ import DOMPurify from 'isomorphic-dompurify';
import showdown from 'showdown'; import showdown from 'showdown';
import { DataBase, type character, type groupChat } from './storage/database'; import { DataBase, type character, type groupChat } from './storage/database';
import { getFileSrc } from './storage/globalApi'; import { getFileSrc } from './storage/globalApi';
import { processScript } from './process/scripts'; import { processScript, processScriptFull } from './process/scripts';
import { get } from 'svelte/store'; import { get } from 'svelte/store';
import css from '@adobe/css-tools' import css from '@adobe/css-tools'
@@ -75,14 +75,15 @@ async function parseAdditionalAssets(data:string, char:character, mode:'normal'|
return data 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 = '' let firstParsed = ''
const orgDat = data
if(char && char.type !== 'group'){ if(char && char.type !== 'group'){
data = await parseAdditionalAssets(data, char, mode) data = await parseAdditionalAssets(data, char, mode)
firstParsed = data firstParsed = data
} }
if(char){ if(char){
data = processScript(char, data, 'editdisplay') data = processScriptFull(char, data, 'editdisplay', chatID).data
} }
if(firstParsed !== data && char && char.type !== 'group'){ if(firstParsed !== data && char && char.type !== 'group'){
data = await parseAdditionalAssets(data, char, mode) data = await parseAdditionalAssets(data, char, mode)

View File

@@ -430,7 +430,7 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
const readed = (await reader.read()) const readed = (await reader.read())
if(readed.value){ if(readed.value){
result = 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 db.characters[selectedChar].chats[selectedChat].message[msgIndex].data = result2.data
emoChanged = result2.emoChanged emoChanged = result2.emoChanged
setDatabase(db) setDatabase(db)
@@ -446,7 +446,8 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
else{ else{
const msgs = req.type === 'success' ? [['char',req.result]] as const : req.type === 'multiline' ? req.result : [] const msgs = req.type === 'success' ? [['char',req.result]] as const : req.type === 'multiline' ? req.result : []
for(const msg of msgs){ 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 result = result2.data
emoChanged = result2.emoChanged emoChanged = result2.emoChanged
db.characters[selectedChar].chats[selectedChat].message.push({ db.characters[selectedChar].chats[selectedChat].message.push({

View File

@@ -4,7 +4,7 @@ import { DataBase, setDatabase, type character, type customscript, type groupCha
import { downloadFile } from "../storage/globalApi"; import { downloadFile } from "../storage/globalApi";
import { alertError, alertNormal } from "../alert"; import { alertError, alertNormal } from "../alert";
import { language } from "src/lang"; import { language } from "src/lang";
import { selectSingleFile } from "../util"; import { findCharacterbyId, selectSingleFile } from "../util";
const dreg = /{{data}}/g const dreg = /{{data}}/g
const randomness = /\|\|\|/g const randomness = /\|\|\|/g
@@ -52,15 +52,16 @@ 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 db = get(DataBase)
let emoChanged = false let emoChanged = false
const scripts = (db.globalscript ?? []).concat(char.customscript) const scripts = (db.globalscript ?? []).concat(char.customscript)
for (const script of scripts){ for (const script of scripts){
if(script.type === mode){ if(script.type === mode){
const reg = new RegExp(script.in, script.ableFlag ? script.flag : 'g') const reg = new RegExp(script.in, script.ableFlag ? script.flag : 'g')
const outScript = script.out let outScript = script.out.replaceAll("$n", "\n")
if(outScript.startsWith('@@') && reg.test(data)){ if(outScript.startsWith('@@')){
if(reg.test(data)){
if(outScript.startsWith('@@emo ')){ if(outScript.startsWith('@@emo ')){
const emoName = script.out.substring(6).trim() const emoName = script.out.substring(6).trim()
let charemotions = get(CharEmotion) let charemotions = get(CharEmotion)
@@ -82,8 +83,60 @@ export function processScriptFull(char:character|groupChat, data:string, mode:Sc
} }
} }
} }
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{ 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)){ if(randomness.test(data)){
const list = data.split('|||') const list = data.split('|||')
data = list[Math.floor(Math.random()*list.length)]; data = list[Math.floor(Math.random()*list.length)];