feat: add lua edits

This commit is contained in:
kwaroran
2024-06-30 23:09:28 +09:00
parent d5d3c9f423
commit 5ea1f87f53
5 changed files with 129 additions and 10 deletions

View File

@@ -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[]
}

View File

@@ -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

View File

@@ -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<string>()
let LuaLowLevelIds = new Set<string>()
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<T extends any>(char:character|groupChat|simpleCharacterArgument, mode:string, content:T):Promise<T>{
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
}
}

View File

@@ -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}
}

View File

@@ -52,6 +52,7 @@ function createSimpleCharacter(char:character|groupChat){
additionalAssets: char.additionalAssets,
virtualscript: char.virtualscript,
emotionImages: char.emotionImages,
triggerscript: char.triggerscript,
}
return simpleChar