[feat] added translator api to plugin
This commit is contained in:
@@ -107,6 +107,7 @@ export function getCurrentPluginMax(prov:string){
|
||||
|
||||
let pluginWorker:Worker = null
|
||||
let providerRes:{success:boolean, content:string} = null
|
||||
let translatorRes:{success:boolean, content:string} = null
|
||||
|
||||
function postMsgPluginWorker(type:string, body:any){
|
||||
const bod = {
|
||||
@@ -116,6 +117,8 @@ function postMsgPluginWorker(type:string, body:any){
|
||||
pluginWorker.postMessage(bod)
|
||||
}
|
||||
|
||||
let pluginTranslator = false
|
||||
|
||||
export async function loadPlugins() {
|
||||
let db = get(DataBase)
|
||||
if(pluginWorker){
|
||||
@@ -127,10 +130,12 @@ export async function loadPlugins() {
|
||||
const da = await fetch("/pluginApi.js")
|
||||
const pluginApiString = await da.text()
|
||||
let pluginjs = `${pluginApiString}\n`
|
||||
let pluginLoadedJs = ''
|
||||
|
||||
for(const plug of db.plugins){
|
||||
pluginjs += `(() => {${plug.script}})()`
|
||||
pluginLoadedJs += `(() => {${plug.script}})()`
|
||||
}
|
||||
pluginjs = pluginjs.replace('//{{placeholder}}',pluginLoadedJs)
|
||||
|
||||
const blob = new Blob([pluginjs], {type: 'application/javascript'});
|
||||
pluginWorker = new Worker(URL.createObjectURL(blob));
|
||||
@@ -167,6 +172,31 @@ export async function loadPlugins() {
|
||||
}
|
||||
break
|
||||
}
|
||||
case "resTrans":{
|
||||
const provres:{success:boolean, content:string} = data.body
|
||||
if(checkNullish(provres.success) || checkNullish(provres.content)){
|
||||
translatorRes = {
|
||||
success: false,
|
||||
content :"plugin didn't respond 'success' or 'content' in response object"
|
||||
}
|
||||
}
|
||||
else if(typeof(provres.content) !== 'string'){
|
||||
translatorRes = {
|
||||
success: false,
|
||||
content :"plugin didn't respond 'content' in response object in string"
|
||||
}
|
||||
}
|
||||
else{
|
||||
translatorRes = {
|
||||
success: !!provres.success,
|
||||
content: provres.content
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
case "useTranslator": {
|
||||
pluginTranslator = true
|
||||
}
|
||||
case "fetch": {
|
||||
postMsgPluginWorker('fetchData',{
|
||||
id: data.body.id,
|
||||
@@ -208,6 +238,33 @@ export async function loadPlugins() {
|
||||
}
|
||||
}
|
||||
|
||||
export async function translatorPlugin(text:string, from:string, to:string) {
|
||||
if(!pluginTranslator){
|
||||
return false
|
||||
}
|
||||
else{
|
||||
try {
|
||||
translatorRes = null
|
||||
postMsgPluginWorker("requestTrans", {text, from, to})
|
||||
while(true){
|
||||
await sleep(50)
|
||||
if(providerRes){
|
||||
break
|
||||
}
|
||||
}
|
||||
return {
|
||||
success: translatorRes.success,
|
||||
content: translatorRes.content
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
content: "unknownError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function pluginProcess(arg:{
|
||||
prompt_chat: OpenAIChat,
|
||||
temperature: number,
|
||||
|
||||
@@ -1,28 +1,34 @@
|
||||
import { Body,fetch,ResponseType } from "@tauri-apps/api/http"
|
||||
import { isTauri } from "../globalApi"
|
||||
import { translatorPlugin } from "../process/plugins"
|
||||
|
||||
let cache={
|
||||
origin: [''],
|
||||
trans: ['']
|
||||
}
|
||||
|
||||
export async function translate(params:string, reverse:boolean) {
|
||||
export async function translate(text:string, reverse:boolean) {
|
||||
if(!isTauri){
|
||||
return params
|
||||
return text
|
||||
}
|
||||
const plug = await translatorPlugin(text, reverse ? 'ko' : 'en', reverse ? 'en' : 'ko')
|
||||
if(plug){
|
||||
return plug.content
|
||||
}
|
||||
if(!reverse){
|
||||
const ind = cache.origin.indexOf(params)
|
||||
const ind = cache.origin.indexOf(text)
|
||||
if(ind !== -1){
|
||||
return cache.trans[ind]
|
||||
}
|
||||
}
|
||||
else{
|
||||
const ind = cache.trans.indexOf(params)
|
||||
const ind = cache.trans.indexOf(text)
|
||||
if(ind !== -1){
|
||||
return cache.origin[ind]
|
||||
}
|
||||
}
|
||||
return googleTrans(params, reverse)
|
||||
|
||||
return googleTrans(text, reverse)
|
||||
}
|
||||
|
||||
async function googleTrans(text:string, reverse:boolean) {
|
||||
@@ -45,5 +51,8 @@ async function googleTrans(text:string, reverse:boolean) {
|
||||
})
|
||||
|
||||
const res = f.data as {sentences:{trans?:string}[]}
|
||||
if(typeof(f.data) === 'string'){
|
||||
return res
|
||||
}
|
||||
return res.sentences.filter((s) => 'trans' in s).map((s) => s.trans).join('');
|
||||
}
|
||||
Reference in New Issue
Block a user