From 5ea1f87f5356bfb0a75e6cfb9bcc35df86eb2624 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sun, 30 Jun 2024 23:09:28 +0900 Subject: [PATCH] feat: add lua edits --- src/ts/parser.ts | 3 +- src/ts/process/index.ts | 3 + src/ts/process/lua.ts | 130 +++++++++++++++++++++++++++++++++++--- src/ts/process/scripts.ts | 2 + src/ts/stores.ts | 1 + 5 files changed, 129 insertions(+), 10 deletions(-) diff --git a/src/ts/parser.ts b/src/ts/parser.ts index 15e3b1b1..0b516e1c 100644 --- a/src/ts/parser.ts +++ b/src/ts/parser.ts @@ -1,7 +1,7 @@ import DOMPurify from 'isomorphic-dompurify'; import { Marked } from 'marked'; -import { DataBase, setDatabase, type Database, type Message, type character, type customscript, type groupChat } from './storage/database'; +import { DataBase, setDatabase, type Database, type Message, type character, type customscript, type groupChat, type triggerscript } from './storage/database'; import { getFileSrc } from './storage/globalApi'; import { processScriptFull } from './process/scripts'; import { get } from 'svelte/store'; @@ -171,6 +171,7 @@ export interface simpleCharacterArgument{ chaId: string, virtualscript?: string emotionImages?: [string, string][] + triggerscript?: triggerscript[] } diff --git a/src/ts/process/index.ts b/src/ts/process/index.ts index 4da73d91..b0f53d3d 100644 --- a/src/ts/process/index.ts +++ b/src/ts/process/index.ts @@ -27,6 +27,7 @@ import { addRerolls } from "./prereroll"; import { runImageEmbedding } from "./transformers"; import { hanuraiMemory } from "./memory/hanuraiMemory"; import { hypaMemoryV2 } from "./memory/hypav2"; +import { runLuaEditTrigger } from "./lua"; export interface OpenAIChat{ role: 'system'|'user'|'assistant'|'function' @@ -1046,6 +1047,8 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n data: formated }) + formated = await runLuaEditTrigger(currentChar, 'editRequest', formated) + //token rechecking let inputTokens = 0 diff --git a/src/ts/process/lua.ts b/src/ts/process/lua.ts index 8b0f5b59..fad93b38 100644 --- a/src/ts/process/lua.ts +++ b/src/ts/process/lua.ts @@ -1,4 +1,4 @@ -import { getChatVar, risuChatParser, setChatVar } from "../parser"; +import { getChatVar, risuChatParser, setChatVar, type simpleCharacterArgument } from "../parser"; import { LuaEngine, LuaFactory } from "wasmoon"; import type { Chat, character, groupChat } from "../storage/database"; import { get } from "svelte/store"; @@ -10,6 +10,7 @@ import { writeInlayImage } from "./files/image"; import type { OpenAIChat } from "."; import { requestChatData } from "./request"; import { v4 } from "uuid"; +import { getModuleTriggers } from "./modules"; let luaFactory:LuaFactory let luaEngine:LuaEngine @@ -18,17 +19,19 @@ let LuaSafeIds = new Set() let LuaLowLevelIds = new Set() export async function runLua(code:string, arg:{ - char?:character|groupChat, + char?:character|groupChat|simpleCharacterArgument, chat?:Chat setVar?: (key:string, value:string) => void, getVar?: (key:string) => string, lowLevelAccess?: boolean, - mode?: string + mode?: string, + data?: any }){ const char = arg.char ?? get(CurrentCharacter) const setVar = arg.setVar ?? setChatVar const getVar = arg.getVar ?? getChatVar const mode = arg.mode ?? 'manual' + const data = arg.data ?? {} let chat = arg.chat ?? get(CurrentChat) let stopSending = false let lowLevelAccess = arg.lowLevelAccess ?? false @@ -41,12 +44,6 @@ export async function runLua(code:string, arg:{ makeLuaFactory() } luaEngine = await luaFactory.createEngine() - luaEngine.global.set('cbs', (code:string) => { - const parsed = risuChatParser(code, { - chara: char, - }) - return parsed - }) luaEngine.global.set('setChatVar', (id:string,key:string, value:string) => { if(!LuaSafeIds.has(id)){ return @@ -319,6 +316,16 @@ export async function runLua(code:string, arg:{ res = await func(accessKey) } } + case 'editRequest': + case 'editDisplay': + case 'editInput': + case 'editOutput':{ + const func = luaEngine.global.get('callListenMain') + if(func){ + res = await func(mode, accessKey, JSON.stringify(data)) + res = JSON.parse(res) + } + } default:{ const func = luaEngine.global.get(mode) if(func){ @@ -694,6 +701,111 @@ function LLM(id, prompt) return json.decode(LLMMain(id, json.encode(prompt))) end +local editRequestFuncs = {} +local editDisplayFuncs = {} +local editInputFuncs = {} +local editOutputFuncs = {} + +function listenEdit(type, func) + if type == 'editRequest' then + editRequestFuncs[#editRequestFuncs + 1] = func + return + end + + if type == 'editDisplay' then + editDisplayFuncs[#editDisplayFuncs + 1] = func + return + end + + if type == 'editInput' then + editInputFuncs[#editInputFuncs + 1] = func + return + end + + if type == 'editOutput' then + editOutputFuncs[#editOutputFuncs + 1] = func + return + end + + throw('Invalid type') +end + +function callListenMain(type, id, value) + local realValue = json.decode(value) + + if type == 'editRequest' then + for _, func in ipairs(editRequestFuncs) do + realValue = func(id, realValue) + end + end + + if type == 'editDisplay' then + for _, func in ipairs(editDisplayFuncs) do + realValue = func(id, realValue) + print(realValue) + end + end + + if type == 'editInput' then + for _, func in ipairs(editInputFuncs) do + realValue = func(id, realValue) + end + end + + if type == 'editOutput' then + for _, func in ipairs(editOutputFuncs) do + realValue = func(id, realValue) + end + end + + return json.encode(realValue) +end + + ${code} ` +} + +export async function runLuaEditTrigger(char:character|groupChat|simpleCharacterArgument, mode:string, content:T):Promise{ + let data = content + + switch(mode){ + case 'editinput': + mode = 'editInput' + break + case 'editoutput': + mode = 'editOutput' + break + case 'editdisplay': + mode = 'editDisplay' + break + case 'editprocess': + return content + } + + try { + const triggers = char.type === 'group' ? (getModuleTriggers()) : (char.triggerscript.map((v) => { + v.lowLevelAccess = false + return v + }).concat(getModuleTriggers())) + + for(let trigger of triggers){ + if(trigger.effect[0].type === 'triggerlua'){ + const runResult = await runLua(trigger.effect[0].code, { + char: char, + lowLevelAccess: false, + mode: mode, + data: data + }) + console.log(runResult) + data = runResult.res ?? data + } + } + + + return data + } catch (error) { + console.error(error) + return content + } } \ No newline at end of file diff --git a/src/ts/process/scripts.ts b/src/ts/process/scripts.ts index b502e3f1..d125ef8a 100644 --- a/src/ts/process/scripts.ts +++ b/src/ts/process/scripts.ts @@ -9,6 +9,7 @@ import { assetRegex, risuChatParser as risuChatParserOrg, type simpleCharacterAr import { runCharacterJS } from "../plugins/embedscript"; import { getModuleRegexScripts } from "./modules"; import { HypaProcesser } from "./memory/hypamemory"; +import { runLuaEditTrigger } from "./lua"; const dreg = /{{data}}/g const randomness = /\|\|\|/g @@ -65,6 +66,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter mode, data, }) + data = await runLuaEditTrigger(char, mode, data) if(scripts.length === 0){ return {data, emoChanged} } diff --git a/src/ts/stores.ts b/src/ts/stores.ts index 61e15a89..f114731f 100644 --- a/src/ts/stores.ts +++ b/src/ts/stores.ts @@ -52,6 +52,7 @@ function createSimpleCharacter(char:character|groupChat){ additionalAssets: char.additionalAssets, virtualscript: char.virtualscript, emotionImages: char.emotionImages, + triggerscript: char.triggerscript, } return simpleChar