[fix] fix requesting zip

This commit is contained in:
kwaroran
2023-11-27 18:57:34 +09:00
parent 6bb3fe3712
commit a7f9fa3a22
5 changed files with 70 additions and 29 deletions

View File

@@ -1,9 +0,0 @@
export function generateRandomSeed(length) {
let result = '';
const characters = '0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}

View File

@@ -1,8 +1,8 @@
import JSZip from "jszip";
export async function processZip(dataArray: Uint8Array): Promise<string> {
const jszip = await import("jszip");
const blob = new Blob([dataArray], { type: "application/zip" });
const zip = new JSZip();
const zip = new jszip.default();
const zipData = await zip.loadAsync(blob);
const imageFile = Object.keys(zipData.files).find(fileName => /\.(jpg|jpeg|png)$/.test(fileName));

View File

@@ -6,8 +6,6 @@ import { globalFetch, readImage } from "../storage/globalApi"
import { CharEmotion } from "../stores"
import type { OpenAIChat } from "."
import { processZip } from "./processzip"
import type { List } from "lodash"
import { generateRandomSeed } from "./generateSeed"
export async function stableDiff(currentChar:character,prompt:string){
const mainPrompt = "assistant is a chat analyzer.\nuser will input a data of situation with key and values before chat, and a chat of a user and character.\nView the status of the chat and change the data.\nif data's key starts with $, it must change it every time.\nif data value is none, it must change it."
let db = get(DataBase)
@@ -190,8 +188,7 @@ export async function stableDiff(currentChar:character,prompt:string){
base64img = Buffer.from(await readImage(db.NAIImgConfig.image)).toString('base64');
}
let randomseed = generateRandomSeed(10);
let seed = parseInt(randomseed, 10);
let seed = Math.floor(Math.random() * 100000000000)
reqlist = {
body: {
"action": "img2img",
@@ -223,7 +220,8 @@ export async function stableDiff(currentChar:character,prompt:string){
},
headers:{
"Authorization": "Bearer " + db.NAIApiKey
}
},
rawResponse: true
}
}else{
reqlist = {
@@ -243,13 +241,15 @@ export async function stableDiff(currentChar:character,prompt:string){
},
headers:{
"Authorization": "Bearer " + db.NAIApiKey
}
},
rawResponse: true
}
}
try {
const da = await globalFetch(db.NAIImgUrl, reqlist)
if(da){
if(da.ok){
let charemotions = get(CharEmotion)
const img = await processZip(da.data);
const emos:[string, string,number][] = [[img, img, Date.now()]]
@@ -257,7 +257,7 @@ export async function stableDiff(currentChar:character,prompt:string){
CharEmotion.set(charemotions)
}
else{
alertError(JSON.stringify(da.data))
alertError(Buffer.from(da.data).toString())
return false
}

View File

@@ -685,16 +685,6 @@ export async function globalFetch(url:string, arg:{
headers: headers,
method: method
})
if(da.headers.get('content-type')?.includes('application/x-zip-compressed')){
const daText = await da.blob()
addFetchLog(daText, da.ok && da.status >= 200 && da.status < 300)
return {
ok: da.ok && da.status >= 200 && da.status < 300,
data: daText,
headers: Object.fromEntries(da.headers)
}
}
const daText = await da.text()
try {
const dat = JSON.parse(daText)