feat: add lua edits
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import DOMPurify from 'isomorphic-dompurify';
|
import DOMPurify from 'isomorphic-dompurify';
|
||||||
import { Marked } from 'marked';
|
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 { getFileSrc } from './storage/globalApi';
|
||||||
import { processScriptFull } from './process/scripts';
|
import { processScriptFull } from './process/scripts';
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
@@ -171,6 +171,7 @@ export interface simpleCharacterArgument{
|
|||||||
chaId: string,
|
chaId: string,
|
||||||
virtualscript?: string
|
virtualscript?: string
|
||||||
emotionImages?: [string, string][]
|
emotionImages?: [string, string][]
|
||||||
|
triggerscript?: triggerscript[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import { addRerolls } from "./prereroll";
|
|||||||
import { runImageEmbedding } from "./transformers";
|
import { runImageEmbedding } from "./transformers";
|
||||||
import { hanuraiMemory } from "./memory/hanuraiMemory";
|
import { hanuraiMemory } from "./memory/hanuraiMemory";
|
||||||
import { hypaMemoryV2 } from "./memory/hypav2";
|
import { hypaMemoryV2 } from "./memory/hypav2";
|
||||||
|
import { runLuaEditTrigger } from "./lua";
|
||||||
|
|
||||||
export interface OpenAIChat{
|
export interface OpenAIChat{
|
||||||
role: 'system'|'user'|'assistant'|'function'
|
role: 'system'|'user'|'assistant'|'function'
|
||||||
@@ -1046,6 +1047,8 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
|
|||||||
data: formated
|
data: formated
|
||||||
})
|
})
|
||||||
|
|
||||||
|
formated = await runLuaEditTrigger(currentChar, 'editRequest', formated)
|
||||||
|
|
||||||
//token rechecking
|
//token rechecking
|
||||||
let inputTokens = 0
|
let inputTokens = 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getChatVar, risuChatParser, setChatVar } from "../parser";
|
import { getChatVar, risuChatParser, setChatVar, type simpleCharacterArgument } from "../parser";
|
||||||
import { LuaEngine, LuaFactory } from "wasmoon";
|
import { LuaEngine, LuaFactory } from "wasmoon";
|
||||||
import type { Chat, character, groupChat } from "../storage/database";
|
import type { Chat, character, groupChat } from "../storage/database";
|
||||||
import { get } from "svelte/store";
|
import { get } from "svelte/store";
|
||||||
@@ -10,6 +10,7 @@ import { writeInlayImage } from "./files/image";
|
|||||||
import type { OpenAIChat } from ".";
|
import type { OpenAIChat } from ".";
|
||||||
import { requestChatData } from "./request";
|
import { requestChatData } from "./request";
|
||||||
import { v4 } from "uuid";
|
import { v4 } from "uuid";
|
||||||
|
import { getModuleTriggers } from "./modules";
|
||||||
|
|
||||||
let luaFactory:LuaFactory
|
let luaFactory:LuaFactory
|
||||||
let luaEngine:LuaEngine
|
let luaEngine:LuaEngine
|
||||||
@@ -18,17 +19,19 @@ let LuaSafeIds = new Set<string>()
|
|||||||
let LuaLowLevelIds = new Set<string>()
|
let LuaLowLevelIds = new Set<string>()
|
||||||
|
|
||||||
export async function runLua(code:string, arg:{
|
export async function runLua(code:string, arg:{
|
||||||
char?:character|groupChat,
|
char?:character|groupChat|simpleCharacterArgument,
|
||||||
chat?:Chat
|
chat?:Chat
|
||||||
setVar?: (key:string, value:string) => void,
|
setVar?: (key:string, value:string) => void,
|
||||||
getVar?: (key:string) => string,
|
getVar?: (key:string) => string,
|
||||||
lowLevelAccess?: boolean,
|
lowLevelAccess?: boolean,
|
||||||
mode?: string
|
mode?: string,
|
||||||
|
data?: any
|
||||||
}){
|
}){
|
||||||
const char = arg.char ?? get(CurrentCharacter)
|
const char = arg.char ?? get(CurrentCharacter)
|
||||||
const setVar = arg.setVar ?? setChatVar
|
const setVar = arg.setVar ?? setChatVar
|
||||||
const getVar = arg.getVar ?? getChatVar
|
const getVar = arg.getVar ?? getChatVar
|
||||||
const mode = arg.mode ?? 'manual'
|
const mode = arg.mode ?? 'manual'
|
||||||
|
const data = arg.data ?? {}
|
||||||
let chat = arg.chat ?? get(CurrentChat)
|
let chat = arg.chat ?? get(CurrentChat)
|
||||||
let stopSending = false
|
let stopSending = false
|
||||||
let lowLevelAccess = arg.lowLevelAccess ?? false
|
let lowLevelAccess = arg.lowLevelAccess ?? false
|
||||||
@@ -41,12 +44,6 @@ export async function runLua(code:string, arg:{
|
|||||||
makeLuaFactory()
|
makeLuaFactory()
|
||||||
}
|
}
|
||||||
luaEngine = await luaFactory.createEngine()
|
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) => {
|
luaEngine.global.set('setChatVar', (id:string,key:string, value:string) => {
|
||||||
if(!LuaSafeIds.has(id)){
|
if(!LuaSafeIds.has(id)){
|
||||||
return
|
return
|
||||||
@@ -319,6 +316,16 @@ export async function runLua(code:string, arg:{
|
|||||||
res = await func(accessKey)
|
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:{
|
default:{
|
||||||
const func = luaEngine.global.get(mode)
|
const func = luaEngine.global.get(mode)
|
||||||
if(func){
|
if(func){
|
||||||
@@ -694,6 +701,111 @@ function LLM(id, prompt)
|
|||||||
return json.decode(LLMMain(id, json.encode(prompt)))
|
return json.decode(LLMMain(id, json.encode(prompt)))
|
||||||
end
|
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}
|
${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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@ import { assetRegex, risuChatParser as risuChatParserOrg, type simpleCharacterAr
|
|||||||
import { runCharacterJS } from "../plugins/embedscript";
|
import { runCharacterJS } from "../plugins/embedscript";
|
||||||
import { getModuleRegexScripts } from "./modules";
|
import { getModuleRegexScripts } from "./modules";
|
||||||
import { HypaProcesser } from "./memory/hypamemory";
|
import { HypaProcesser } from "./memory/hypamemory";
|
||||||
|
import { runLuaEditTrigger } from "./lua";
|
||||||
|
|
||||||
const dreg = /{{data}}/g
|
const dreg = /{{data}}/g
|
||||||
const randomness = /\|\|\|/g
|
const randomness = /\|\|\|/g
|
||||||
@@ -65,6 +66,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
|
|||||||
mode,
|
mode,
|
||||||
data,
|
data,
|
||||||
})
|
})
|
||||||
|
data = await runLuaEditTrigger(char, mode, data)
|
||||||
if(scripts.length === 0){
|
if(scripts.length === 0){
|
||||||
return {data, emoChanged}
|
return {data, emoChanged}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ function createSimpleCharacter(char:character|groupChat){
|
|||||||
additionalAssets: char.additionalAssets,
|
additionalAssets: char.additionalAssets,
|
||||||
virtualscript: char.virtualscript,
|
virtualscript: char.virtualscript,
|
||||||
emotionImages: char.emotionImages,
|
emotionImages: char.emotionImages,
|
||||||
|
triggerscript: char.triggerscript,
|
||||||
}
|
}
|
||||||
|
|
||||||
return simpleChar
|
return simpleChar
|
||||||
|
|||||||
Reference in New Issue
Block a user