Risuai 0.6.3 first commit

This commit is contained in:
kwaroran
2023-05-07 12:41:45 +09:00
parent 50e5e1d917
commit 2c5c7d2694
98 changed files with 15070 additions and 0 deletions

BIN
public/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

196
public/pluginApi.js Normal file
View File

@@ -0,0 +1,196 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __risuPlugin__ = {
providers: [],
fetchResponseQueue: []
};
var sleep = function (ms) { return new Promise(function (r) { return setTimeout(r, ms); }); };
function risuFetch(url, arg) {
return __awaiter(this, void 0, void 0, function () {
var id, i, q;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
id = "".concat(Date.now(), "_").concat(Math.random());
postMessage({
type: 'fetch',
body: {
id: id,
url: url,
arg: arg
}
});
_a.label = 1;
case 1:
if (!true) return [3 /*break*/, 3];
return [4 /*yield*/, sleep(50)];
case 2:
_a.sent();
for (i = 0; i < __risuPlugin__.fetchResponseQueue.length; i++) {
q = __risuPlugin__.fetchResponseQueue[i];
if (q.id === id) {
__risuPlugin__.fetchResponseQueue.splice(i, 1);
return [2 /*return*/, q.data];
}
}
return [3 /*break*/, 1];
case 3: return [2 /*return*/];
}
});
});
}
function getArg(arg) {
return __awaiter(this, void 0, void 0, function () {
var id, i, q;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
id = "".concat(Date.now(), "_").concat(Math.random());
postMessage({
type: 'getArg',
body: {
id: id,
arg: arg
}
});
_a.label = 1;
case 1:
if (!true) return [3 /*break*/, 3];
return [4 /*yield*/, sleep(50)];
case 2:
_a.sent();
for (i = 0; i < __risuPlugin__.fetchResponseQueue.length; i++) {
q = __risuPlugin__.fetchResponseQueue[i];
if (q.id === id) {
__risuPlugin__.fetchResponseQueue.splice(i, 1);
return [2 /*return*/, q.data];
}
}
return [3 /*break*/, 1];
case 3: return [2 /*return*/];
}
});
});
}
function addProvider(name, func) {
postMessage({
type: 'addProvider',
body: name
});
__risuPlugin__.providers.push({
name: name,
func: func
});
}
function printLog(data) {
postMessage({
type: 'log',
body: data
});
}
function handleOnmessage(data) {
return __awaiter(this, void 0, void 0, function () {
var _a, body, providers, providerfunc, _i, providers_1, provider, _b, error_1;
var _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (!data.type) {
return [2 /*return*/];
}
_a = data.type;
switch (_a) {
case "requestProvider": return [3 /*break*/, 1];
case "fetchData": return [3 /*break*/, 6];
}
return [3 /*break*/, 7];
case 1:
body = data.body;
providers = __risuPlugin__.providers;
providerfunc = null;
for (_i = 0, providers_1 = providers; _i < providers_1.length; _i++) {
provider = providers_1[_i];
if (provider.name === body.key) {
providerfunc = provider.func;
}
}
if (!!providerfunc) return [3 /*break*/, 2];
postMessage({
type: 'resProvider',
body: {
'success': false,
'content': 'unknown provider'
}
});
return [3 /*break*/, 5];
case 2:
_d.trys.push([2, 4, , 5]);
_b = postMessage;
_c = {
type: 'resProvider'
};
return [4 /*yield*/, providerfunc(body.arg)];
case 3:
_b.apply(void 0, [(_c.body = _d.sent(),
_c)]);
return [3 /*break*/, 5];
case 4:
error_1 = _d.sent();
postMessage({
type: 'resProvider',
body: {
'success': false,
'content': "providerError: ".concat(error_1)
}
});
return [3 /*break*/, 5];
case 5: return [3 /*break*/, 7];
case 6:
{
__risuPlugin__.fetchResponseQueue.push(data.body);
return [3 /*break*/, 7];
}
_d.label = 7;
case 7: return [2 /*return*/];
}
});
});
}
onmessage = function (ev) {
handleOnmessage(ev.data);
var data = ev.data;
};

143
public/pluginApi.ts Normal file
View File

@@ -0,0 +1,143 @@
interface risuPlugin{
providers: {name:string, func:(arg:providerArgument) => Promise<{success:boolean,content:string}>}[]
fetchResponseQueue:{id:string,data:any}[]
}
let __risuPlugin__:risuPlugin = {
providers: [],
fetchResponseQueue: []
}
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
interface OpenAIChat{
role: 'system'|'user'|'assistant'
content: string
}
interface providerArgument{
prompt_chat?: OpenAIChat,
temperature?: number,
max_tokens?: number,
presence_penalty?: number
frequency_penalty?: number
bias?: {[key:string]:string}
}
async function risuFetch(url:string, arg:{body:any,headers?:{[key:string]:string}}){
const id = `${Date.now()}_${Math.random()}`
postMessage({
type: 'fetch',
body: {
id: id,
url: url,
arg: arg
}
})
while(true){
await sleep(50)
for(let i=0;i<__risuPlugin__.fetchResponseQueue.length;i++){
const q = __risuPlugin__.fetchResponseQueue[i]
if(q.id === id){
__risuPlugin__.fetchResponseQueue.splice(i, 1)
return q.data as {
ok: boolean;
data: any;
}
}
}
}
}
async function getArg(arg:string){
const id = `${Date.now()}_${Math.random()}`
postMessage({
type: 'getArg',
body: {
id: id,
arg: arg
}
})
while(true){
await sleep(50)
for(let i=0;i<__risuPlugin__.fetchResponseQueue.length;i++){
const q = __risuPlugin__.fetchResponseQueue[i]
if(q.id === id){
__risuPlugin__.fetchResponseQueue.splice(i, 1)
return q.data as (string|number|null)
}
}
}
}
function addProvider(name:string, func:(arg:providerArgument) => Promise<{success:boolean,content:string}>){
postMessage({
type: 'addProvider',
body: name
})
__risuPlugin__.providers.push({
name: name,
func: func
})
}
function printLog(data:any){
postMessage({
type: 'log',
body: data
})
}
async function handleOnmessage(data:{type:string,body:any}) {
if(!data.type){
return
}
switch(data.type){
case "requestProvider":{
const body:{key:string,arg:providerArgument} = data.body
const providers = __risuPlugin__.providers
let providerfunc:((arg:providerArgument) => Promise<{success:boolean,content:string}>)|null= null
for(const provider of providers){
if(provider.name === body.key){
providerfunc = provider.func
}
}
if(!providerfunc){
postMessage({
type: 'resProvider',
body: {
'success': false,
'content': 'unknown provider'
}
})
}
else{
try {
postMessage({
type: 'resProvider',
body: await providerfunc(body.arg)
})
} catch (error) {
postMessage({
type: 'resProvider',
body: {
'success': false,
'content': `providerError: ${error}`
}
})
}
}
break
}
case "fetchData":{
__risuPlugin__.fetchResponseQueue.push(data.body)
break
}
}
}
onmessage = (ev) => {
handleOnmessage(ev.data)
const data:{type:string,body:any} = ev.data
}

BIN
public/sample/rika.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 MiB

BIN
public/sample/yuzu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 MiB

BIN
public/ss2.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

BIN
public/ss3.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

70
public/sw.js Normal file
View File

@@ -0,0 +1,70 @@
// @ts-nocheck
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url)
const path = url.pathname.split('/')
if(path[1] === 'sw'){
try {
switch (path[2]){
case "check":{
event.respondWith(checkCache(url))
break
}
case "img": {
event.respondWith(getImg(url))
break
}
case "register": {
event.respondWith(registerCache(url, event.request.arrayBuffer()))
break
}
case "init":{
event.respondWith(new Response("true"))
}
default: {
event.respondWith(new Response(
path[2]
))
}
}
} catch (error) {
event.respondWith(new Response(`${error}`))
}
}
})
async function checkCache(url){
const cache = await caches.open('risuCache')
return new Response(JSON.stringify({
"able": !!(await cache.match(url))
}))
}
async function getImg(url){
const cache = await caches.open('risuCache')
return await cache.match(url)
}
async function check(){
}
async function registerCache(urlr, buffer){
const cache = await caches.open('risuCache')
const url = new URL(urlr)
let path = url.pathname.split('/')
path[2] = 'img'
url.pathname = path.join('/')
const buf = new Uint8Array(await buffer)
await cache.put(url, new Response(buf, {
headers: {
"cache-control": "max-age=604800",
"content-type": "image/png"
}
}))
return new Response(JSON.stringify({
"done": true
}))
}