[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
});
}
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) {
return __awaiter(this, void 0, void 0, function () {
var _a, body, providers, providerfunc, _i, providers_1, provider, _b, error_1;

View File

@@ -119,6 +119,17 @@
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}) {
if(!data.type){

View File

@@ -10,6 +10,17 @@ import { setDatabase } from '../storage/database'
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:{
id: string,
result: any
@@ -208,8 +219,7 @@ addWorkerFunction('setState', async (statename, data) => {
let lastCode = ''
let lastModeList:string[] = []
let compCode:{[key:string]:string[]} = {}
export async function runCharacterJS(arg:{
code: string|null,
@@ -230,30 +240,51 @@ export async function runCharacterJS(arg:{
'onButtonClick': "onButtonClick"
} as const
if(lastCode !== arg.code){
lastModeList = []
const codesplit = arg.code.split('\n')
for(let i = 0; i < codesplit.length; i++){
const line = codesplit[i]
if(line.startsWith('//@use')){
lastModeList.push(line.replace('//@use','').trim())
let runCodes = [...additionalCharaJS, arg.code]
let r = arg.data
for(const code of runCodes){
if(!code){
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)){
return arg.data
}
const result = await runVirtualJS(`${arg.code}\n${runCode}(${JSON.stringify(arg.data)})`)
console.log(compCode[code])
if(!result){
return arg.data
if(!compCode[code].includes(runCode)){
continue
}
const result = await runVirtualJS(`${code}\n${runCode}(${JSON.stringify(r)})`)
if(!result){
continue
}
r = result.toString()
if(runCode === 'onButtonClick'){
return r
}
}
return result.toString()
return r
} catch (error) {
if(arg.mode !== 'editprocess'){
return `Error: ${error}`

View File

@@ -6,6 +6,7 @@ import { checkNullish, selectSingleFile, sleep } from "../util";
import type { OpenAIChat } from "../process";
import { globalFetch } from "../storage/globalApi";
import { selectedCharID } from "../stores";
import { addAdditionalCharaJS } from "./embedscript";
export const customProviderStore = writable([] as string[])
@@ -206,6 +207,15 @@ export async function loadPlugins() {
})
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":{
try {
const db = get(DataBase)

View File

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