Add V2 plugin
This commit is contained in:
@@ -1,21 +1,16 @@
|
||||
import { get, writable } from "svelte/store";
|
||||
import { language } from "../../lang";
|
||||
import { alertError } from "../alert";
|
||||
import { getDatabase, setDatabaseLite } from "../storage/database.svelte";
|
||||
import { getCurrentCharacter, getDatabase, setDatabaseLite } from "../storage/database.svelte";
|
||||
import { checkNullish, selectSingleFile, sleep } from "../util";
|
||||
import type { OpenAIChat } from "../process/index.svelte";
|
||||
import { globalFetch } from "../globalApi.svelte";
|
||||
import { fetchNative, globalFetch } from "../globalApi.svelte";
|
||||
import { selectedCharID } from "../stores.svelte";
|
||||
import { addAdditionalCharaJS } from "./embedscript";
|
||||
import type { ScriptMode } from "../process/scripts";
|
||||
|
||||
export const customProviderStore = writable([] as string[])
|
||||
|
||||
interface PluginRequest{
|
||||
url: string
|
||||
header?:{[key:string]:string}
|
||||
body: any,
|
||||
res: string
|
||||
}
|
||||
|
||||
interface ProviderPlugin{
|
||||
name:string
|
||||
@@ -23,6 +18,7 @@ interface ProviderPlugin{
|
||||
script:string
|
||||
arguments:{[key:string]:'int'|'string'|string[]}
|
||||
realArg:{[key:string]:number|string}
|
||||
version?:1|2
|
||||
}
|
||||
|
||||
export type RisuPlugin = ProviderPlugin
|
||||
@@ -37,6 +33,7 @@ export async function importPlugin(){
|
||||
const jsFile = Buffer.from(f.data).toString('utf-8').replace(/^\uFEFF/gm, "");
|
||||
const splitedJs = jsFile.split('\n')
|
||||
let name = ''
|
||||
let version:1|2 = 1
|
||||
let displayName:string = undefined
|
||||
let arg:{[key:string]:'int'|'string'|string[]} = {}
|
||||
let realArg:{[key:string]:number|string} = {}
|
||||
@@ -49,15 +46,32 @@ export async function importPlugin(){
|
||||
}
|
||||
name = provied.trim()
|
||||
}
|
||||
if(line.startsWith('//@name')){
|
||||
const provied = line.slice(7)
|
||||
if(provied === ''){
|
||||
alertError('plugin name must be longer than "", did you put it correctly?')
|
||||
return
|
||||
}
|
||||
version = 2
|
||||
name = provied.trim()
|
||||
}
|
||||
if(line.startsWith('//@risu-display-name')){
|
||||
const provied = line.slice('//@risu-display-name'.length + 1)
|
||||
if(provied === ''){
|
||||
alertError('plugin display name must be longer than "", did you put it correctly?')
|
||||
return
|
||||
}
|
||||
name = provied.trim()
|
||||
displayName = provied.trim()
|
||||
}
|
||||
if(line.startsWith('//@risu-arg')){
|
||||
if(line.startsWith('//@display-name')){
|
||||
const provied = line.slice('//@display-name'.length + 1)
|
||||
if(provied === ''){
|
||||
alertError('plugin display name must be longer than "", did you put it correctly?')
|
||||
return
|
||||
}
|
||||
displayName = provied.trim()
|
||||
}
|
||||
if(line.startsWith('//@risu-arg') || line.startsWith('//@arg')){
|
||||
const provied = line.trim().split(' ')
|
||||
if(provied.length < 3){
|
||||
alertError('plugin argument is incorrect, did you put space in argument name?')
|
||||
@@ -90,7 +104,8 @@ export async function importPlugin(){
|
||||
script: jsFile,
|
||||
realArg: realArg,
|
||||
arguments: arg,
|
||||
displayName: displayName
|
||||
displayName: displayName,
|
||||
version: version
|
||||
}
|
||||
|
||||
db.plugins ??= []
|
||||
@@ -124,11 +139,18 @@ let pluginTranslator = false
|
||||
|
||||
export async function loadPlugins() {
|
||||
let db = getDatabase()
|
||||
|
||||
if(pluginWorker){
|
||||
pluginWorker.terminate()
|
||||
pluginWorker = null
|
||||
}
|
||||
if(db.plugins.length > 0){
|
||||
|
||||
const plugins = safeStructuredClone(db.plugins).filter((a:RisuPlugin) => a.version === 1)
|
||||
const pluginV2 = safeStructuredClone(db.plugins).filter((a:RisuPlugin) => a.version === 2)
|
||||
|
||||
await loadV2Plugin(pluginV2)
|
||||
|
||||
if(plugins.length > 0){
|
||||
|
||||
const da = await fetch("/pluginApi.js")
|
||||
const pluginApiString = await da.text()
|
||||
@@ -267,6 +289,140 @@ export async function loadPlugins() {
|
||||
}
|
||||
}
|
||||
|
||||
type PluginV2ProviderArgument = {
|
||||
prompt_chat: OpenAIChat[],
|
||||
frequency_penalty: number
|
||||
min_p: number
|
||||
presence_penalty: number
|
||||
repetition_penalty: number
|
||||
top_k: number
|
||||
top_p: number
|
||||
temperature: number
|
||||
mode: string
|
||||
}
|
||||
|
||||
type EditFunction = (content:string) => string|null|undefined|Promise<string|null|undefined>
|
||||
type ReplacerFunction = (content:OpenAIChat[], type:string) => OpenAIChat[]|Promise<OpenAIChat[]>
|
||||
|
||||
export const pluginV2 = {
|
||||
providers: new Map<string, (arg:PluginV2ProviderArgument) => Promise<{success:boolean,content:string}> >(),
|
||||
editdisplay: new Set<EditFunction>(),
|
||||
editoutput: new Set<EditFunction>(),
|
||||
editprocess: new Set<EditFunction>(),
|
||||
editinput: new Set<EditFunction>(),
|
||||
replacerbeforeRequest: new Set<ReplacerFunction>(),
|
||||
replacerafterRequest: new Set<(content:string, type:string) => string|Promise<string>>(),
|
||||
unload: new Set<() => void|Promise<void>>(),
|
||||
loaded: false
|
||||
}
|
||||
|
||||
export async function loadV2Plugin(plugins:RisuPlugin[]){
|
||||
|
||||
if(pluginV2.loaded){
|
||||
for(const unload of pluginV2.unload){
|
||||
await unload()
|
||||
}
|
||||
|
||||
pluginV2.providers.clear()
|
||||
pluginV2.editdisplay.clear()
|
||||
pluginV2.editoutput.clear()
|
||||
pluginV2.editprocess.clear()
|
||||
pluginV2.editinput.clear()
|
||||
}
|
||||
|
||||
pluginV2.loaded = true
|
||||
|
||||
globalThis.__pluginApis__ = {
|
||||
risuFetch: globalFetch,
|
||||
nativeFetch: fetchNative,
|
||||
getArg: (arg:string) => {
|
||||
const [name, realArg] = arg.split('::')
|
||||
for(const plug of plugins){
|
||||
if(plug.name === name){
|
||||
return plug.realArg[realArg]
|
||||
}
|
||||
}
|
||||
},
|
||||
getChar: () => {
|
||||
return getCurrentCharacter()
|
||||
},
|
||||
setChar: (char:any) => {
|
||||
const db = getDatabase()
|
||||
const charid = get(selectedCharID)
|
||||
db.characters[charid] = char
|
||||
setDatabaseLite(db)
|
||||
},
|
||||
addProvider: (name:string, func:(arg:PluginV2ProviderArgument) => Promise<{success:boolean,content:string}>) => {
|
||||
let provs = get(customProviderStore)
|
||||
provs.push(name)
|
||||
pluginV2.providers.set(name, func)
|
||||
customProviderStore.set(provs)
|
||||
},
|
||||
addRisuScriptHandler: (name:ScriptMode, func:EditFunction) => {
|
||||
if(pluginV2['edit' + name]){
|
||||
pluginV2['edit' + name].add(func)
|
||||
}
|
||||
else{
|
||||
throw (`script handler named ${name} not found`)
|
||||
}
|
||||
},
|
||||
removeRisuScriptHandler: (name:ScriptMode, func:EditFunction) => {
|
||||
if(pluginV2['edit' + name]){
|
||||
pluginV2['edit' + name].delete(func)
|
||||
}
|
||||
else{
|
||||
throw (`script handler named ${name} not found`)
|
||||
}
|
||||
},
|
||||
addRisuReplacer: (name:string, func:ReplacerFunction) => {
|
||||
if(pluginV2['replacer' + name]){
|
||||
pluginV2['replacer' + name].add(func)
|
||||
}
|
||||
else{
|
||||
throw (`replacer handler named ${name} not found`)
|
||||
}
|
||||
},
|
||||
removeRisuReplacer: (name:string, func:ReplacerFunction) => {
|
||||
if(pluginV2['replacer' + name]){
|
||||
pluginV2['replacer' + name].delete(func)
|
||||
}
|
||||
else{
|
||||
throw (`replacer handler named ${name} not found`)
|
||||
}
|
||||
},
|
||||
onUnload: (func:() => void|Promise<void>) => {
|
||||
pluginV2.unload.add(func)
|
||||
}
|
||||
}
|
||||
|
||||
for(const plugin of plugins){
|
||||
const data = plugin.script
|
||||
|
||||
const realScript = `(async () => {
|
||||
const risuFetch = globalThis.__pluginApis__.risuFetch
|
||||
const nativeFetch = globalThis.__pluginApis__.nativeFetch
|
||||
const getArg = globalThis.__pluginApis__.getArg
|
||||
const printLog = globalThis.__pluginApis__.printLog
|
||||
const getChar = globalThis.__pluginApis__.getChar
|
||||
const setChar = globalThis.__pluginApis__.setChar
|
||||
const addProvider = globalThis.__pluginApis__.addProvider
|
||||
const addRisuEventHandler = globalThis.__pluginApis__.addRisuEventHandler
|
||||
const onUnload = globalThis.__pluginApis__.onUnload
|
||||
|
||||
${data}
|
||||
})();`
|
||||
|
||||
try {
|
||||
eval(realScript)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
|
||||
console.log('Loaded V2 Plugin', plugin.name)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export async function translatorPlugin(text:string, from:string, to:string) {
|
||||
if(!pluginTranslator){
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user