From 4fcad0752f6e03d4796cda9838f96604298e02e9 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 11 Oct 2024 21:33:36 +0300 Subject: [PATCH] [chore] Fix type errors --- package-lock.json | 1 - package.json | 1 - src/character-card-parser.js | 4 ++-- src/endpoints/anthropic.js | 2 +- src/endpoints/backends/chat-completions.js | 9 ++++---- src/endpoints/backends/kobold.js | 3 ++- src/endpoints/backends/scale-alt.js | 2 +- src/endpoints/backends/text-completions.js | 17 +++++++++----- src/endpoints/content-manager.js | 2 ++ src/endpoints/google.js | 2 +- src/endpoints/images.js | 2 +- src/endpoints/novelai.js | 6 ++--- src/endpoints/openai.js | 3 +-- src/endpoints/stable-diffusion.js | 26 +++++++++++++++++----- src/endpoints/translate.js | 9 ++++---- src/transformers.mjs | 5 +++-- src/util.js | 20 ++++++++++------- src/vectors/cohere-vectors.js | 1 + src/vectors/extras-vectors.js | 1 + src/vectors/llamacpp-vectors.js | 1 + src/vectors/makersuite-vectors.js | 1 + src/vectors/nomicai-vectors.js | 1 + src/vectors/ollama-vectors.js | 1 + src/vectors/openai-vectors.js | 1 + src/vectors/vllm-vectors.js | 1 + 25 files changed, 78 insertions(+), 44 deletions(-) diff --git a/package-lock.json b/package-lock.json index bc729cf22..cdbaa662a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -71,7 +71,6 @@ "@types/lodash": "^4.17.10", "@types/mime-types": "^2.1.4", "@types/multer": "^1.4.12", - "@types/node-fetch": "^2.6.11", "@types/node-persist": "^3.1.8", "@types/png-chunk-text": "^1.0.3", "@types/png-chunks-encode": "^1.0.2", diff --git a/package.json b/package.json index 22a6f3526..2e702c499 100644 --- a/package.json +++ b/package.json @@ -97,7 +97,6 @@ "@types/lodash": "^4.17.10", "@types/mime-types": "^2.1.4", "@types/multer": "^1.4.12", - "@types/node-fetch": "^2.6.11", "@types/node-persist": "^3.1.8", "@types/png-chunk-text": "^1.0.3", "@types/png-chunks-encode": "^1.0.2", diff --git a/src/character-card-parser.js b/src/character-card-parser.js index a8433f3b7..a9a414c37 100644 --- a/src/character-card-parser.js +++ b/src/character-card-parser.js @@ -13,7 +13,7 @@ import PNGtext from 'png-chunk-text'; * @returns {Buffer} PNG image buffer with metadata */ export const write = (image, data) => { - const chunks = extract(image); + const chunks = extract(new Uint8Array(image)); const tEXtChunks = chunks.filter(chunk => chunk.name === 'tEXt'); // Remove existing tEXt chunks @@ -52,7 +52,7 @@ export const write = (image, data) => { * @returns {string} Character data */ export const read = (image) => { - const chunks = extract(image); + const chunks = extract(new Uint8Array(image)); const textChunks = chunks.filter((chunk) => chunk.name === 'tEXt').map((chunk) => PNGtext.decode(chunk.data)); diff --git a/src/endpoints/anthropic.js b/src/endpoints/anthropic.js index ff0b7b6ad..79f3fc5e9 100644 --- a/src/endpoints/anthropic.js +++ b/src/endpoints/anthropic.js @@ -42,7 +42,6 @@ router.post('/caption-image', jsonParser, async (request, response) => { 'anthropic-version': '2023-06-01', 'x-api-key': request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.CLAUDE), }, - timeout: 0, }); if (!result.ok) { @@ -51,6 +50,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { return response.status(result.status).send({ error: true }); } + /** @type {any} */ const generateResponseJson = await result.json(); const caption = generateResponseJson.content[0].text; console.log('Claude response:', generateResponseJson); diff --git a/src/endpoints/backends/chat-completions.js b/src/endpoints/backends/chat-completions.js index 7bfff462b..0d80ed453 100644 --- a/src/endpoints/backends/chat-completions.js +++ b/src/endpoints/backends/chat-completions.js @@ -152,7 +152,6 @@ async function sendClaudeRequest(request, response) { 'x-api-key': apiKey, ...additionalHeaders, }, - timeout: 0, }); if (request.body.stream) { @@ -165,6 +164,7 @@ async function sendClaudeRequest(request, response) { return response.status(generateResponse.status).send({ error: true }); } + /** @type {any} */ const generateResponseJson = await generateResponse.json(); const responseText = generateResponseJson?.content?.[0]?.text || ''; console.log('Claude response:', generateResponseJson); @@ -212,7 +212,6 @@ async function sendScaleRequest(request, response) { 'Content-Type': 'application/json', 'Authorization': `Basic ${apiKey}`, }, - timeout: 0, }); if (!generateResponse.ok) { @@ -220,6 +219,7 @@ async function sendScaleRequest(request, response) { return response.status(500).send({ error: true }); } + /** @type {any} */ const generateResponseJson = await generateResponse.json(); console.log('Scale response:', generateResponseJson); @@ -335,7 +335,6 @@ async function sendMakerSuiteRequest(request, response) { 'Content-Type': 'application/json', }, signal: controller.signal, - timeout: 0, }); // have to do this because of their busted ass streaming endpoint if (stream) { @@ -354,6 +353,7 @@ async function sendMakerSuiteRequest(request, response) { return response.status(generateResponse.status).send({ error: true }); } + /** @type {any} */ const generateResponseJson = await generateResponse.json(); const candidates = generateResponseJson?.candidates; @@ -676,6 +676,7 @@ router.post('/status', jsonParser, async function (request, response_getstatus_o }); if (response.ok) { + /** @type {any} */ const data = await response.json(); response_getstatus_openai.send(data); @@ -979,7 +980,6 @@ router.post('/generate', jsonParser, function (request, response) { }, body: JSON.stringify(requestBody), signal: controller.signal, - timeout: 0, }; console.log(requestBody); @@ -1005,6 +1005,7 @@ router.post('/generate', jsonParser, function (request, response) { } if (fetchResponse.ok) { + /** @type {any} */ let json = await fetchResponse.json(); response.send(json); console.log(json); diff --git a/src/endpoints/backends/kobold.js b/src/endpoints/backends/kobold.js index e2fa630ff..a6e7fc89e 100644 --- a/src/endpoints/backends/kobold.js +++ b/src/endpoints/backends/kobold.js @@ -96,7 +96,7 @@ router.post('/generate', jsonParser, async function (request, response_generate) for (let i = 0; i < MAX_RETRIES; i++) { try { const url = request.body.streaming ? `${request.body.api_server}/extra/generate/stream` : `${request.body.api_server}/v1/generate`; - const response = await fetch(url, { method: 'POST', timeout: 0, ...args }); + const response = await fetch(url, { method: 'POST', ...args }); if (request.body.streaming) { // Pipe remote SSE stream to Express response @@ -156,6 +156,7 @@ router.post('/status', jsonParser, async function (request, response) { const result = {}; + /** @type {any} */ const [koboldUnitedResponse, koboldExtraResponse, koboldModelResponse] = await Promise.all([ // We catch errors both from the response not having a successful HTTP status and from JSON parsing failing diff --git a/src/endpoints/backends/scale-alt.js b/src/endpoints/backends/scale-alt.js index 0bdbcd336..5e621e17c 100644 --- a/src/endpoints/backends/scale-alt.js +++ b/src/endpoints/backends/scale-alt.js @@ -70,7 +70,6 @@ router.post('/generate', jsonParser, async function (request, response) { 'Content-Type': 'application/json', 'cookie': `_jwt=${cookie}`, }, - timeout: 0, body: JSON.stringify(body), }); @@ -80,6 +79,7 @@ router.post('/generate', jsonParser, async function (request, response) { return response.status(500).send({ error: { message: result.statusText } }); } + /** @type {any} */ const data = await result.json(); const output = data?.result?.data?.json?.outputs?.[0] || ''; diff --git a/src/endpoints/backends/text-completions.js b/src/endpoints/backends/text-completions.js index 6c6942621..7dcea23f1 100644 --- a/src/endpoints/backends/text-completions.js +++ b/src/endpoints/backends/text-completions.js @@ -28,6 +28,10 @@ export const router = express.Router(); */ async function parseOllamaStream(jsonStream, request, response) { try { + if (!jsonStream.body) { + throw new Error('No body in the response'); + } + let partialData = ''; jsonStream.body.on('data', (data) => { const chunk = data.toString(); @@ -153,6 +157,7 @@ router.post('/status', jsonParser, async function (request, response) { return response.status(400); } + /** @type {any} */ let data = await modelsReply.json(); if (request.body.legacy_api) { @@ -190,6 +195,7 @@ router.post('/status', jsonParser, async function (request, response) { const modelInfoReply = await fetch(modelInfoUrl, args); if (modelInfoReply.ok) { + /** @type {any} */ const modelInfo = await modelInfoReply.json(); console.log('Ooba model info:', modelInfo); @@ -206,6 +212,7 @@ router.post('/status', jsonParser, async function (request, response) { const modelInfoReply = await fetch(modelInfoUrl, args); if (modelInfoReply.ok) { + /** @type {any} */ const modelInfo = await modelInfoReply.json(); console.log('Tabby model info:', modelInfo); @@ -359,6 +366,7 @@ router.post('/generate', jsonParser, async function (request, response) { const completionsReply = await fetch(url, args); if (completionsReply.ok) { + /** @type {any} */ const data = await completionsReply.json(); console.log('Endpoint response:', data); @@ -415,7 +423,6 @@ ollama.post('/download', jsonParser, async function (request, response) { name: name, stream: false, }), - timeout: 0, }); if (!fetchResponse.ok) { @@ -448,7 +455,6 @@ ollama.post('/caption-image', jsonParser, async function (request, response) { images: [request.body.image], stream: false, }), - timeout: 0, }); if (!fetchResponse.ok) { @@ -456,6 +462,7 @@ ollama.post('/caption-image', jsonParser, async function (request, response) { return response.status(500).send({ error: true }); } + /** @type {any} */ const data = await fetchResponse.json(); console.log('Ollama caption response:', data); @@ -487,7 +494,6 @@ llamacpp.post('/caption-image', jsonParser, async function (request, response) { const fetchResponse = await fetch(`${baseUrl}/completion`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - timeout: 0, body: JSON.stringify({ prompt: `USER:[img-1]${String(request.body.prompt).trim()}\nASSISTANT:`, image_data: [{ data: request.body.image, id: 1 }], @@ -502,6 +508,7 @@ llamacpp.post('/caption-image', jsonParser, async function (request, response) { return response.status(500).send({ error: true }); } + /** @type {any} */ const data = await fetchResponse.json(); console.log('LlamaCpp caption response:', data); @@ -531,7 +538,6 @@ llamacpp.post('/props', jsonParser, async function (request, response) { const fetchResponse = await fetch(`${baseUrl}/props`, { method: 'GET', - timeout: 0, }); if (!fetchResponse.ok) { @@ -566,7 +572,6 @@ llamacpp.post('/slots', jsonParser, async function (request, response) { if (request.body.action === 'info') { fetchResponse = await fetch(`${baseUrl}/slots`, { method: 'GET', - timeout: 0, }); } else { if (!/^\d+$/.test(request.body.id_slot)) { @@ -579,7 +584,6 @@ llamacpp.post('/slots', jsonParser, async function (request, response) { fetchResponse = await fetch(`${baseUrl}/slots/${request.body.id_slot}?action=${request.body.action}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - timeout: 0, body: JSON.stringify({ filename: request.body.action !== 'erase' ? `${request.body.filename}` : undefined, }), @@ -623,6 +627,7 @@ tabby.post('/download', jsonParser, async function (request, response) { }); if (permissionResponse.ok) { + /** @type {any} */ const permissionJson = await permissionResponse.json(); if (permissionJson['permission'] !== 'admin') { diff --git a/src/endpoints/content-manager.js b/src/endpoints/content-manager.js index 88c2c6cd2..90aaee002 100644 --- a/src/endpoints/content-manager.js +++ b/src/endpoints/content-manager.js @@ -380,6 +380,7 @@ async function downloadPygmalionCharacter(id) { throw new Error('Failed to download character'); } + /** @type {any} */ const jsonData = await result.json(); const characterData = jsonData?.character; @@ -472,6 +473,7 @@ async function downloadJannyCharacter(uuid) { }); if (result.ok) { + /** @type {any} */ const downloadResult = await result.json(); if (downloadResult.status === 'ok') { const imageResult = await fetch(downloadResult.downloadUrl); diff --git a/src/endpoints/google.js b/src/endpoints/google.js index 09f66f599..b0ba87260 100644 --- a/src/endpoints/google.js +++ b/src/endpoints/google.js @@ -40,7 +40,6 @@ router.post('/caption-image', jsonParser, async (request, response) => { headers: { 'Content-Type': 'application/json', }, - timeout: 0, }); if (!result.ok) { @@ -49,6 +48,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { return response.status(result.status).send({ error: true }); } + /** @type {any} */ const data = await result.json(); console.log('Multimodal captioning response', data); diff --git a/src/endpoints/images.js b/src/endpoints/images.js index eeacb90fc..22c1d5b17 100644 --- a/src/endpoints/images.js +++ b/src/endpoints/images.js @@ -68,7 +68,7 @@ router.post('/upload', jsonParser, async (request, response) => { ensureDirectoryExistence(pathToNewFile); const imageBuffer = Buffer.from(base64Data, 'base64'); - await fs.promises.writeFile(pathToNewFile, imageBuffer); + await fs.promises.writeFile(pathToNewFile, new Uint8Array(imageBuffer)); response.send({ path: clientRelativePath(request.user.directories.root, pathToNewFile) }); } catch (error) { console.log(error); diff --git a/src/endpoints/novelai.js b/src/endpoints/novelai.js index a4b770308..53ed8258e 100644 --- a/src/endpoints/novelai.js +++ b/src/endpoints/novelai.js @@ -252,7 +252,7 @@ router.post('/generate', jsonParser, async function (req, res) { try { const baseURL = (req.body.model.includes('kayra') || req.body.model.includes('erato')) ? TEXT_NOVELAI : API_NOVELAI; const url = req.body.streaming ? `${baseURL}/ai/generate-stream` : `${baseURL}/ai/generate`; - const response = await fetch(url, { method: 'POST', timeout: 0, ...args }); + const response = await fetch(url, { method: 'POST', ...args }); if (req.body.streaming) { // Pipe remote SSE stream to Express response @@ -274,6 +274,7 @@ router.post('/generate', jsonParser, async function (req, res) { return res.status(response.status).send({ error: { message } }); } + /** @type {any} */ const data = await response.json(); console.log('NovelAI Output', data?.output); return res.send(data); @@ -416,7 +417,6 @@ router.post('/generate-voice', jsonParser, async (request, response) => { 'Authorization': `Bearer ${token}`, 'Accept': 'audio/mpeg', }, - timeout: 0, }); if (!result.ok) { @@ -426,7 +426,7 @@ router.post('/generate-voice', jsonParser, async (request, response) => { } const chunks = await readAllChunks(result.body); - const buffer = Buffer.concat(chunks); + const buffer = Buffer.concat(chunks.map(chunk => new Uint8Array(chunk))); response.setHeader('Content-Type', 'audio/mpeg'); return response.send(buffer); } diff --git a/src/endpoints/openai.js b/src/endpoints/openai.js index 08d138e52..8c820dc55 100644 --- a/src/endpoints/openai.js +++ b/src/endpoints/openai.js @@ -154,7 +154,6 @@ router.post('/caption-image', jsonParser, async (request, response) => { ...headers, }, body: JSON.stringify(body), - timeout: 0, }); if (!result.ok) { @@ -163,6 +162,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { return response.status(500).send(text); } + /** @type {any} */ const data = await result.json(); console.log('Multimodal captioning response', data); const caption = data?.choices[0]?.message?.content; @@ -284,7 +284,6 @@ router.post('/generate-image', jsonParser, async (request, response) => { Authorization: `Bearer ${key}`, }, body: JSON.stringify(request.body), - timeout: 0, }); if (!result.ok) { diff --git a/src/endpoints/stable-diffusion.js b/src/endpoints/stable-diffusion.js index 811020f9a..dbd9a22fd 100644 --- a/src/endpoints/stable-diffusion.js +++ b/src/endpoints/stable-diffusion.js @@ -65,6 +65,7 @@ router.post('/upscalers', jsonParser, async (request, response) => { throw new Error('SD WebUI returned an error.'); } + /** @type {any} */ const data = await result.json(); const names = data.map(x => x.name); return names; @@ -85,6 +86,7 @@ router.post('/upscalers', jsonParser, async (request, response) => { throw new Error('SD WebUI returned an error.'); } + /** @type {any} */ const data = await result.json(); const names = data.map(x => x.name); return names; @@ -118,6 +120,7 @@ router.post('/vaes', jsonParser, async (request, response) => { throw new Error('SD WebUI returned an error.'); } + /** @type {any} */ const data = await result.json(); const names = data.map(x => x.model_name); return response.send(names); @@ -143,6 +146,7 @@ router.post('/samplers', jsonParser, async (request, response) => { throw new Error('SD WebUI returned an error.'); } + /** @type {any} */ const data = await result.json(); const names = data.map(x => x.name); return response.send(names); @@ -169,6 +173,7 @@ router.post('/schedulers', jsonParser, async (request, response) => { throw new Error('SD WebUI returned an error.'); } + /** @type {any} */ const data = await result.json(); const names = data.map(x => x.name); return response.send(names); @@ -194,6 +199,7 @@ router.post('/models', jsonParser, async (request, response) => { throw new Error('SD WebUI returned an error.'); } + /** @type {any} */ const data = await result.json(); const models = data.map(x => ({ value: x.title, text: x.title })); return response.send(models); @@ -214,6 +220,7 @@ router.post('/get-model', jsonParser, async (request, response) => { 'Authorization': getBasicAuthHeader(request.body.auth), }, }); + /** @type {any} */ const data = await result.json(); return response.send(data['sd_model_checkpoint']); } catch (error) { @@ -233,7 +240,6 @@ router.post('/set-model', jsonParser, async (request, response) => { headers: { 'Authorization': getBasicAuthHeader(request.body.auth), }, - timeout: 0, }); const data = await result.json(); return data; @@ -253,7 +259,6 @@ router.post('/set-model', jsonParser, async (request, response) => { 'Content-Type': 'application/json', 'Authorization': getBasicAuthHeader(request.body.auth), }, - timeout: 0, }); if (!result.ok) { @@ -264,6 +269,7 @@ router.post('/set-model', jsonParser, async (request, response) => { const CHECK_INTERVAL = 2000; for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { + /** @type {any} */ const progressState = await getProgress(); const progress = progressState['progress']; @@ -308,8 +314,6 @@ router.post('/generate', jsonParser, async (request, response) => { 'Content-Type': 'application/json', 'Authorization': getBasicAuthHeader(request.body.auth), }, - timeout: 0, - // @ts-ignore signal: controller.signal, }); @@ -345,6 +349,7 @@ router.post('/sd-next/upscalers', jsonParser, async (request, response) => { // Vlad doesn't provide Latent Upscalers in the API, so we have to hardcode them here const latentUpscalers = ['Latent', 'Latent (antialiased)', 'Latent (bicubic)', 'Latent (bicubic antialiased)', 'Latent (nearest)', 'Latent (nearest-exact)']; + /** @type {any} */ const data = await result.json(); const names = data.map(x => x.name); @@ -387,6 +392,7 @@ comfy.post('/samplers', jsonParser, async (request, response) => { throw new Error('ComfyUI returned an error.'); } + /** @type {any} */ const data = await result.json(); return response.send(data.KSampler.input.required.sampler_name[0]); } catch (error) { @@ -404,6 +410,7 @@ comfy.post('/models', jsonParser, async (request, response) => { if (!result.ok) { throw new Error('ComfyUI returned an error.'); } + /** @type {any} */ const data = await result.json(); return response.send(data.CheckpointLoaderSimple.input.required.ckpt_name[0].map(it => ({ value: it, text: it }))); } catch (error) { @@ -422,6 +429,7 @@ comfy.post('/schedulers', jsonParser, async (request, response) => { throw new Error('ComfyUI returned an error.'); } + /** @type {any} */ const data = await result.json(); return response.send(data.KSampler.input.required.scheduler[0]); } catch (error) { @@ -440,6 +448,7 @@ comfy.post('/vaes', jsonParser, async (request, response) => { throw new Error('ComfyUI returned an error.'); } + /** @type {any} */ const data = await result.json(); return response.send(data.VAELoader.input.required.vae_name[0]); } catch (error) { @@ -521,6 +530,7 @@ comfy.post('/generate', jsonParser, async (request, response) => { throw new Error('ComfyUI returned an error.'); } + /** @type {any} */ const data = await promptResult.json(); const id = data.prompt_id; let item; @@ -531,6 +541,7 @@ comfy.post('/generate', jsonParser, async (request, response) => { if (!result.ok) { throw new Error('ComfyUI returned an error.'); } + /** @type {any} */ const history = await result.json(); item = history[id]; if (item) { @@ -633,6 +644,7 @@ together.post('/generate', jsonParser, async (request, response) => { return response.sendStatus(500); } + /** @type {any} */ const data = await result.json(); console.log('TogetherAI response:', data); @@ -681,6 +693,8 @@ drawthings.post('/get-model', jsonParser, async (request, response) => { const result = await fetch(url, { method: 'GET', }); + + /** @type {any} */ const data = await result.json(); return response.send(data['model']); @@ -698,6 +712,8 @@ drawthings.post('/get-upscaler', jsonParser, async (request, response) => { const result = await fetch(url, { method: 'GET', }); + + /** @type {any} */ const data = await result.json(); return response.send(data['upscaler']); @@ -726,7 +742,6 @@ drawthings.post('/generate', jsonParser, async (request, response) => { 'Content-Type': 'application/json', 'Authorization': auth, }, - timeout: 0, }); if (!result.ok) { @@ -848,7 +863,6 @@ stability.post('/generate', jsonParser, async (request, response) => { 'Accept': 'image/*', }, body: formData, - timeout: 0, }); if (!result.ok) { diff --git a/src/endpoints/translate.js b/src/endpoints/translate.js index 67f98ddc8..57b7a9a5a 100644 --- a/src/endpoints/translate.js +++ b/src/endpoints/translate.js @@ -78,6 +78,7 @@ router.post('/libre', jsonParser, async (request, response) => { return response.sendStatus(result.status); } + /** @type {any} */ const json = await result.json(); console.log('Translated text: ' + json.translatedText); @@ -158,7 +159,6 @@ router.post('/yandex', jsonParser, async (request, response) => { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, - timeout: 0, }); if (!result.ok) { @@ -167,6 +167,7 @@ router.post('/yandex', jsonParser, async (request, response) => { return response.sendStatus(500); } + /** @type {any} */ const json = await result.json(); const translated = json.text.join(); console.log('Translated text: ' + translated); @@ -264,7 +265,6 @@ router.post('/deepl', jsonParser, async (request, response) => { 'Authorization': `DeepL-Auth-Key ${key}`, 'Content-Type': 'application/x-www-form-urlencoded', }, - timeout: 0, }); if (!result.ok) { @@ -273,6 +273,7 @@ router.post('/deepl', jsonParser, async (request, response) => { return response.sendStatus(result.status); } + /** @type {any} */ const json = await result.json(); console.log('Translated text: ' + json.translations[0].text); @@ -317,7 +318,6 @@ router.post('/onering', jsonParser, async (request, response) => { const result = await fetch(fetchUrl, { method: 'GET', - timeout: 0, }); if (!result.ok) { @@ -326,6 +326,7 @@ router.post('/onering', jsonParser, async (request, response) => { return response.sendStatus(result.status); } + /** @type {any} */ const data = await result.json(); console.log('Translated text: ' + data.result); @@ -373,7 +374,6 @@ router.post('/deeplx', jsonParser, async (request, response) => { 'Accept': 'application/json', 'Content-Type': 'application/json', }, - timeout: 0, }); if (!result.ok) { @@ -382,6 +382,7 @@ router.post('/deeplx', jsonParser, async (request, response) => { return response.sendStatus(result.status); } + /** @type {any} */ const json = await result.json(); console.log('Translated text: ' + json.data); diff --git a/src/transformers.mjs b/src/transformers.mjs index 416f2031f..a4981a191 100644 --- a/src/transformers.mjs +++ b/src/transformers.mjs @@ -3,7 +3,7 @@ import fs from 'node:fs'; import process from 'node:process'; import { Buffer } from 'node:buffer'; -import { pipeline, env, RawImage, Pipeline } from 'sillytavern-transformers'; +import { pipeline, env, RawImage } from 'sillytavern-transformers'; import { getConfigValue } from './util.js'; configureTransformers(); @@ -117,7 +117,7 @@ async function migrateCacheToDataDir() { * Gets the transformers.js pipeline for a given task. * @param {import('sillytavern-transformers').PipelineType} task The task to get the pipeline for * @param {string} forceModel The model to use for the pipeline, if any - * @returns {Promise} Pipeline for the task + * @returns {Promise} The transformers.js pipeline */ export async function getPipeline(task, forceModel = '') { await migrateCacheToDataDir(); @@ -137,6 +137,7 @@ export async function getPipeline(task, forceModel = '') { const instance = await pipeline(task, model, { cache_dir: cacheDir, quantized: tasks[task].quantized ?? true, local_files_only: localOnly }); tasks[task].pipeline = instance; tasks[task].currentModel = model; + // @ts-ignore return instance; } diff --git a/src/util.js b/src/util.js index f966739d9..b3a6cca75 100644 --- a/src/util.js +++ b/src/util.js @@ -441,17 +441,21 @@ export function forwardFetchResponse(from, to) { to.statusCode = statusCode; to.statusMessage = statusText; - from.body.pipe(to); + if (from.body && to.socket) { + from.body.pipe(to); - to.socket.on('close', function () { - if (from.body instanceof Readable) from.body.destroy(); // Close the remote stream - to.end(); // End the Express response - }); + to.socket.on('close', function () { + if (from.body instanceof Readable) from.body.destroy(); // Close the remote stream + to.end(); // End the Express response + }); - from.body.on('end', function () { - console.log('Streaming request finished'); + from.body.on('end', function () { + console.log('Streaming request finished'); + to.end(); + }); + } else { to.end(); - }); + } } /** diff --git a/src/vectors/cohere-vectors.js b/src/vectors/cohere-vectors.js index 7a940ac23..01bc3214d 100644 --- a/src/vectors/cohere-vectors.js +++ b/src/vectors/cohere-vectors.js @@ -38,6 +38,7 @@ export async function getCohereBatchVector(texts, isQuery, directories, model) { throw new Error('API request failed'); } + /** @type {any} */ const data = await response.json(); if (!Array.isArray(data?.embeddings?.float)) { console.log('API response was not an array'); diff --git a/src/vectors/extras-vectors.js b/src/vectors/extras-vectors.js index 5a0857ef6..8a0229b5e 100644 --- a/src/vectors/extras-vectors.js +++ b/src/vectors/extras-vectors.js @@ -66,6 +66,7 @@ async function getExtrasVectorImpl(text, apiUrl, apiKey) { throw new Error('Extras request failed'); } + /** @type {any} */ const data = await response.json(); const vector = data.embedding; // `embedding`: number[] (one text item), or number[][] (multiple text items). diff --git a/src/vectors/llamacpp-vectors.js b/src/vectors/llamacpp-vectors.js index 0af91336e..83b823a17 100644 --- a/src/vectors/llamacpp-vectors.js +++ b/src/vectors/llamacpp-vectors.js @@ -30,6 +30,7 @@ export async function getLlamaCppBatchVector(texts, apiUrl, directories) { throw new Error(`LlamaCpp: Failed to get vector for text: ${response.statusText} ${responseText}`); } + /** @type {any} */ const data = await response.json(); if (!Array.isArray(data?.data)) { diff --git a/src/vectors/makersuite-vectors.js b/src/vectors/makersuite-vectors.js index 48c8fd056..1ff4a0d50 100644 --- a/src/vectors/makersuite-vectors.js +++ b/src/vectors/makersuite-vectors.js @@ -52,6 +52,7 @@ export async function getMakerSuiteVector(text, directories) { throw new Error('Google AI Studio request failed'); } + /** @type {any} */ const data = await response.json(); // noinspection JSValidateTypes return data['embedding']['values']; diff --git a/src/vectors/nomicai-vectors.js b/src/vectors/nomicai-vectors.js index 2f832956a..5acedfe8f 100644 --- a/src/vectors/nomicai-vectors.js +++ b/src/vectors/nomicai-vectors.js @@ -51,6 +51,7 @@ export async function getNomicAIBatchVector(texts, source, directories) { throw new Error('API request failed'); } + /** @type {any} */ const data = await response.json(); if (!Array.isArray(data?.embeddings)) { console.log('API response was not an array'); diff --git a/src/vectors/ollama-vectors.js b/src/vectors/ollama-vectors.js index 237d6979d..415fde604 100644 --- a/src/vectors/ollama-vectors.js +++ b/src/vectors/ollama-vectors.js @@ -54,6 +54,7 @@ export async function getOllamaVector(text, apiUrl, model, keep, directories) { throw new Error(`Ollama: Failed to get vector for text: ${response.statusText} ${responseText}`); } + /** @type {any} */ const data = await response.json(); if (!Array.isArray(data?.embedding)) { diff --git a/src/vectors/openai-vectors.js b/src/vectors/openai-vectors.js index f408e505e..76ce7e55b 100644 --- a/src/vectors/openai-vectors.js +++ b/src/vectors/openai-vectors.js @@ -61,6 +61,7 @@ export async function getOpenAIBatchVector(texts, source, directories, model = ' throw new Error('API request failed'); } + /** @type {any} */ const data = await response.json(); if (!Array.isArray(data?.data)) { diff --git a/src/vectors/vllm-vectors.js b/src/vectors/vllm-vectors.js index e20c85a72..436bc57c4 100644 --- a/src/vectors/vllm-vectors.js +++ b/src/vectors/vllm-vectors.js @@ -31,6 +31,7 @@ export async function getVllmBatchVector(texts, apiUrl, model, directories) { throw new Error(`VLLM: Failed to get vector for text: ${response.statusText} ${responseText}`); } + /** @type {any} */ const data = await response.json(); if (!Array.isArray(data?.data)) {