[feat] add plugin addCharaJS

This commit is contained in:
kwaroran
2023-11-11 13:26:58 +09:00
parent 8fe2da4aec
commit 55f508bff7
5 changed files with 88 additions and 27 deletions

View File

@@ -173,6 +173,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
body: char body: char
}); });
} }
function addCharaJs(code, position) {
if (position === void 0) { position = 'back'; }
var codeString = code.toString().replace(/.+?\{/, '{');
postMessage({
type: 'addCharaJs',
body: {
code: codeString,
position: position
}
});
}
function handleOnmessage(data) { function handleOnmessage(data) {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function () {
var _a, body, providers, providerfunc, _i, providers_1, provider, _b, error_1; var _a, body, providers, providerfunc, _i, providers_1, provider, _b, error_1;

View File

@@ -119,6 +119,17 @@
body: char body: char
}) })
} }
function addCharaJs(code:Function, position:'front'|'back' = 'back'){
const codeString = code.toString().replace(/.+?\{/, '{')
postMessage({
type: 'addCharaJs',
body: {
code: codeString,
position: position
}
})
}
async function handleOnmessage(data:{type:string,body:any}) { async function handleOnmessage(data:{type:string,body:any}) {
if(!data.type){ if(!data.type){

View File

@@ -10,6 +10,17 @@ import { setDatabase } from '../storage/database'
let worker = new Worker(WorkerUrl, {type: 'module'}) let worker = new Worker(WorkerUrl, {type: 'module'})
let additionalCharaJS:string[] = []
export function addAdditionalCharaJS(code:string, position:'front'|'back' = 'back'){
if(position === 'front'){
additionalCharaJS.unshift(code)
}
else{
additionalCharaJS.push(code)
}
}
let results:{ let results:{
id: string, id: string,
result: any result: any
@@ -208,8 +219,7 @@ addWorkerFunction('setState', async (statename, data) => {
let lastCode = '' let compCode:{[key:string]:string[]} = {}
let lastModeList:string[] = []
export async function runCharacterJS(arg:{ export async function runCharacterJS(arg:{
code: string|null, code: string|null,
@@ -230,30 +240,51 @@ export async function runCharacterJS(arg:{
'onButtonClick': "onButtonClick" 'onButtonClick': "onButtonClick"
} as const } as const
if(lastCode !== arg.code){ let runCodes = [...additionalCharaJS, arg.code]
lastModeList = []
const codesplit = arg.code.split('\n') let r = arg.data
for(let i = 0; i < codesplit.length; i++){
const line = codesplit[i] for(const code of runCodes){
if(line.startsWith('//@use')){ if(!code){
lastModeList.push(line.replace('//@use','').trim()) continue
}
if(!compCode[code]){
let modeList:string[] = []
const codesplit = code.split('\n')
for(let i = 0; i < codesplit.length; i++){
const line = codesplit[i].trim()
if(line.startsWith('//@use')){
modeList.push(line.replace('//@use','').trim())
}
}
compCode[code] = modeList
// compcode length max 100
if(Object.keys(compCode).length > 100){
delete compCode[Object.keys(compCode)[50]]
} }
} }
lastCode = arg.code
}
const runCode = codes[arg.mode] const runCode = codes[arg.mode]
if(!lastModeList.includes(runCode)){ console.log(compCode[code])
return arg.data
}
const result = await runVirtualJS(`${arg.code}\n${runCode}(${JSON.stringify(arg.data)})`)
if(!result){ if(!compCode[code].includes(runCode)){
return arg.data continue
}
const result = await runVirtualJS(`${code}\n${runCode}(${JSON.stringify(r)})`)
if(!result){
continue
}
r = result.toString()
if(runCode === 'onButtonClick'){
return r
}
} }
return r
return result.toString()
} catch (error) { } catch (error) {
if(arg.mode !== 'editprocess'){ if(arg.mode !== 'editprocess'){
return `Error: ${error}` return `Error: ${error}`

View File

@@ -6,6 +6,7 @@ import { checkNullish, selectSingleFile, sleep } from "../util";
import type { OpenAIChat } from "../process"; import type { OpenAIChat } from "../process";
import { globalFetch } from "../storage/globalApi"; import { globalFetch } from "../storage/globalApi";
import { selectedCharID } from "../stores"; import { selectedCharID } from "../stores";
import { addAdditionalCharaJS } from "./embedscript";
export const customProviderStore = writable([] as string[]) export const customProviderStore = writable([] as string[])
@@ -206,6 +207,15 @@ export async function loadPlugins() {
}) })
break break
} }
case 'addCharaJs': {
let c:string = data.body.code
c.trim()
if(c.startsWith('{') && c.endsWith('}')){
c = c.slice(1, -1)
}
addAdditionalCharaJS(c, data.body.position)
break
}
case "getArg":{ case "getArg":{
try { try {
const db = get(DataBase) const db = get(DataBase)

View File

@@ -62,13 +62,11 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
if(db.officialplugins.automark && mode === 'editdisplay'){ if(db.officialplugins.automark && mode === 'editdisplay'){
data = autoMarkPlugin(data) data = autoMarkPlugin(data)
} }
if(char.virtualscript){ data = await runCharacterJS({
data = await runCharacterJS({ code: char.virtualscript ?? null,
code: char.virtualscript, mode,
mode, data,
data, })
})
}
if(scripts.length === 0){ if(scripts.length === 0){
return {data, emoChanged} return {data, emoChanged}
} }