PR fixes
| @@ -1,6 +1,7 @@ | |||
| 1 | import { getRequestHeaders, substituteParams } from '../../../../script.js'; | 1 | import { getRequestHeaders, substituteParams } from '../../../../script.js'; |
| 2 | import { Popup, POPUP_RESULT, POPUP_TYPE } from '../../../popup.js'; | 2 | import { Popup, POPUP_RESULT, POPUP_TYPE } from '../../../popup.js'; |
| 3 | import { executeSlashCommandsOnChatInput, executeSlashCommandsWithOptions } from '../../../slash-commands.js'; | 3 | import { executeSlashCommandsOnChatInput, executeSlashCommandsWithOptions } from '../../../slash-commands.js'; |
| 4 | import { SlashCommandScope } from '../../../slash-commands/SlashCommandScope.js'; | ||
| 4 | import { SlashCommandParser } from '../../../slash-commands/SlashCommandParser.js'; | 5 | import { SlashCommandParser } from '../../../slash-commands/SlashCommandParser.js'; |
| 5 | import { debounceAsync, warn } from '../index.js'; | 6 | import { debounceAsync, warn } from '../index.js'; |
| 6 | import { QuickReply } from './QuickReply.js'; | 7 | import { QuickReply } from './QuickReply.js'; |
| @@ -1443,9 +1443,7 @@ async function sendWindowAIRequest(messages, signal, stream) { | |||
| 1443 | } | 1443 | } |
| 1444 | 1444 | ||
| 1445 | const onStreamResult = (res, err) => { | 1445 | const onStreamResult = (res, err) => { |
| 1446 | if (err) { | 1446 | if (err) return; |
| 1447 | return; | ||
| 1448 | } | ||
| 1449 | 1447 | ||
| 1450 | const thisContent = res?.message?.content; | 1448 | const thisContent = res?.message?.content; |
| 1451 | 1449 | ||
| @@ -224,9 +224,7 @@ router.post('/download', jsonParser, async (request, response) => { | |||
| 224 | // Delete if previous download failed | 224 | // Delete if previous download failed |
| 225 | if (fs.existsSync(temp_path)) { | 225 | if (fs.existsSync(temp_path)) { |
| 226 | fs.unlink(temp_path, (err) => { | 226 | fs.unlink(temp_path, (err) => { |
| 227 | if (err) { | 227 | if (err) throw err; |
| 228 | throw err; | ||
| 229 | } | ||
| 230 | }); | 228 | }); |
| 231 | } | 229 | } |
| 232 | const fileStream = fs.createWriteStream(destination, { flags: 'wx' }); | 230 | const fileStream = fs.createWriteStream(destination, { flags: 'wx' }); |
| @@ -288,9 +286,7 @@ router.post('/delete', jsonParser, async (request, response) => { | |||
| 288 | // Delete if previous download failed | 286 | // Delete if previous download failed |
| 289 | if (fs.existsSync(file_path)) { | 287 | if (fs.existsSync(file_path)) { |
| 290 | fs.unlink(file_path, (err) => { | 288 | fs.unlink(file_path, (err) => { |
| 291 | if (err) { | 289 | if (err) throw err; |
| 292 | throw err; | ||
| 293 | } | ||
| 294 | }); | 290 | }); |
| 295 | console.info('Asset deleted.'); | 291 | console.info('Asset deleted.'); |
| 296 | } | 292 | } |
| @@ -317,9 +313,7 @@ router.post('/delete', jsonParser, async (request, response) => { | |||
| 317 | * @returns {void} | 313 | * @returns {void} |
| 318 | */ | 314 | */ |
| 319 | router.post('/character', jsonParser, async (request, response) => { | 315 | router.post('/character', jsonParser, async (request, response) => { |
| 320 | if (request.query.name === undefined) { | 316 | if (request.query.name === undefined) return response.sendStatus(400); |
| 321 | return response.sendStatus(400); | ||
| 322 | } | ||
| 323 | 317 | ||
| 324 | // For backwards compatibility, don't reject invalid character names, just sanitize them | 318 | // For backwards compatibility, don't reject invalid character names, just sanitize them |
| 325 | const name = sanitize(request.query.name.toString()); | 319 | const name = sanitize(request.query.name.toString()); |
| @@ -327,11 +321,9 @@ router.post('/character', jsonParser, async (request, response) => { | |||
| 327 | 321 | ||
| 328 | // Check category | 322 | // Check category |
| 329 | let category = null; | 323 | let category = null; |
| 330 | for (let i of VALID_CATEGORIES) { | 324 | for (let i of VALID_CATEGORIES) |
| 331 | if (i == inputCategory) { | 325 | if (i == inputCategory) |
| 332 | category = i; | 326 | category = i; |
| 333 | } | ||
| 334 | } | ||
| 335 | 327 | ||
| 336 | if (category === null) { | 328 | if (category === null) { |
| 337 | console.error('Bad request: unsupported asset category.'); | 329 | console.error('Bad request: unsupported asset category.'); |
| @@ -348,9 +340,7 @@ router.post('/character', jsonParser, async (request, response) => { | |||
| 348 | if (category == 'live2d') { | 340 | if (category == 'live2d') { |
| 349 | const folders = fs.readdirSync(folderPath, { withFileTypes: true }); | 341 | const folders = fs.readdirSync(folderPath, { withFileTypes: true }); |
| 350 | for (const folderInfo of folders) { | 342 | for (const folderInfo of folders) { |
| 351 | if (!folderInfo.isDirectory()) { | 343 | if (!folderInfo.isDirectory()) continue; |
| 352 | continue; | ||
| 353 | } | ||
| 354 | 344 | ||
| 355 | const modelFolder = folderInfo.name; | 345 | const modelFolder = folderInfo.name; |
| 356 | const live2dModelPath = path.join(folderPath, modelFolder); | 346 | const live2dModelPath = path.join(folderPath, modelFolder); |
| @@ -418,9 +418,7 @@ async function sendMakerSuiteRequest(request, response) { | |||
| 418 | * @param {express.Response} response Express response | 418 | * @param {express.Response} response Express response |
| 419 | */ | 419 | */ |
| 420 | async function sendAI21Request(request, response) { | 420 | async function sendAI21Request(request, response) { |
| 421 | if (!request.body) { | 421 | if (!request.body) return response.sendStatus(400); |
| 422 | return response.sendStatus(400); | ||
| 423 | } | ||
| 424 | 422 | ||
| 425 | const controller = new AbortController(); | 423 | const controller = new AbortController(); |
| 426 | console.debug(request.body.messages); | 424 | console.debug(request.body.messages); |
| @@ -848,7 +846,8 @@ router.post('/status', jsonParser, async function (request, response_getstatus_o | |||
| 848 | }); | 846 | }); |
| 849 | 847 | ||
| 850 | router.post('/bias', jsonParser, async function (request, response) { | 848 | router.post('/bias', jsonParser, async function (request, response) { |
| 851 | if (!request.body || !Array.isArray(request.body)) return response.sendStatus(400); | 849 | if (!request.body || !Array.isArray(request.body)) |
| 850 | return response.sendStatus(400); | ||
| 852 | 851 | ||
| 853 | try { | 852 | try { |
| 854 | const result = {}; | 853 | const result = {}; |
| @@ -202,8 +202,7 @@ router.post('/transcribe-audio', urlencodedParser, async function (request, resp | |||
| 202 | return response.sendStatus(400); | 202 | return response.sendStatus(400); |
| 203 | } | 203 | } |
| 204 | 204 | ||
| 205 | console.info('Transcribing audio with KoboldCpp'); | 205 | console.debug('Transcribing audio with KoboldCpp', server); |
| 206 | console.debug(server); | ||
| 207 | 206 | ||
| 208 | const fileBase64 = fs.readFileSync(request.file.path).toString('base64'); | 207 | const fileBase64 = fs.readFileSync(request.file.path).toString('base64'); |
| 209 | fs.rmSync(request.file.path); | 208 | fs.rmSync(request.file.path); |
| @@ -101,8 +101,7 @@ router.post('/status', jsonParser, async function (request, response) { | |||
| 101 | request.body.api_server = request.body.api_server.replace('localhost', '127.0.0.1'); | 101 | request.body.api_server = request.body.api_server.replace('localhost', '127.0.0.1'); |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | console.info('Trying to connect to API'); | 104 | console.debug('Trying to connect to API', request.body); |
| 105 | console.debug(request.body); | ||
| 106 | const baseUrl = trimV1(request.body.api_server); | 105 | const baseUrl = trimV1(request.body.api_server); |
| 107 | 106 | ||
| 108 | const args = { | 107 | const args = { |
| @@ -229,9 +228,7 @@ router.post('/status', jsonParser, async function (request, response) { | |||
| 229 | }); | 228 | }); |
| 230 | 229 | ||
| 231 | router.post('/props', jsonParser, async function (request, response) { | 230 | router.post('/props', jsonParser, async function (request, response) { |
| 232 | if (!request.body.api_server) { | 231 | if (!request.body.api_server) return response.sendStatus(400); |
| 233 | return response.sendStatus(400); | ||
| 234 | } | ||
| 235 | 232 | ||
| 236 | try { | 233 | try { |
| 237 | const baseUrl = trimV1(request.body.api_server); | 234 | const baseUrl = trimV1(request.body.api_server); |
| @@ -265,9 +262,7 @@ router.post('/props', jsonParser, async function (request, response) { | |||
| 265 | }); | 262 | }); |
| 266 | 263 | ||
| 267 | router.post('/generate', jsonParser, async function (request, response) { | 264 | router.post('/generate', jsonParser, async function (request, response) { |
| 268 | if (!request.body) { | 265 | if (!request.body) return response.sendStatus(400); |
| 269 | return response.sendStatus(400); | ||
| 270 | } | ||
| 271 | 266 | ||
| 272 | try { | 267 | try { |
| 273 | if (request.body.api_server.indexOf('localhost') !== -1) { | 268 | if (request.body.api_server.indexOf('localhost') !== -1) { |
| @@ -435,9 +430,7 @@ const ollama = express.Router(); | |||
| 435 | 430 | ||
| 436 | ollama.post('/download', jsonParser, async function (request, response) { | 431 | ollama.post('/download', jsonParser, async function (request, response) { |
| 437 | try { | 432 | try { |
| 438 | if (!request.body.name || !request.body.api_server) { | 433 | if (!request.body.name || !request.body.api_server) return response.sendStatus(400); |
| 439 | return response.sendStatus(400); | ||
| 440 | } | ||
| 441 | 434 | ||
| 442 | const name = request.body.name; | 435 | const name = request.body.name; |
| 443 | const url = String(request.body.api_server).replace(/\/$/, ''); | 436 | const url = String(request.body.api_server).replace(/\/$/, ''); |
| @@ -20,7 +20,7 @@ router.post('/', jsonParser, async (req, res) => { | |||
| 20 | const pipe = await getPipeline(TASK); | 20 | const pipe = await getPipeline(TASK); |
| 21 | const result = await pipe(rawImage); | 21 | const result = await pipe(rawImage); |
| 22 | const text = result[0].generated_text; | 22 | const text = result[0].generated_text; |
| 23 | console.debug('Image caption:', text); | 23 | console.info('Image caption:', text); |
| 24 | 24 | ||
| 25 | return res.json({ caption: text }); | 25 | return res.json({ caption: text }); |
| 26 | } catch (error) { | 26 | } catch (error) { |
| @@ -52,7 +52,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { | |||
| 52 | 52 | ||
| 53 | /** @type {any} */ | 53 | /** @type {any} */ |
| 54 | const data = await result.json(); | 54 | const data = await result.json(); |
| 55 | console.debug('Multimodal captioning response', data); | 55 | console.info('Multimodal captioning response', data); |
| 56 | 56 | ||
| 57 | const candidates = data?.candidates; | 57 | const candidates = data?.candidates; |
| 58 | if (!candidates) { | 58 | if (!candidates) { |
| @@ -364,12 +364,12 @@ router.post('/generate-image', jsonParser, async (request, response) => { | |||
| 364 | return response.sendStatus(400); | 364 | return response.sendStatus(400); |
| 365 | } | 365 | } |
| 366 | 366 | ||
| 367 | console.debug('Horde image generation request:', generation); | 367 | console.info('Horde image generation request:', generation); |
| 368 | 368 | ||
| 369 | const controller = new AbortController(); | 369 | const controller = new AbortController(); |
| 370 | request.socket.removeAllListeners('close'); | 370 | request.socket.removeAllListeners('close'); |
| 371 | request.socket.on('close', function () { | 371 | request.socket.on('close', function () { |
| 372 | console.info('Horde image generation request aborted.'); | 372 | console.warn('Horde image generation request aborted.'); |
| 373 | controller.abort(); | 373 | controller.abort(); |
| 374 | if (generation.id) ai_horde.deleteImageGenerationRequest(generation.id); | 374 | if (generation.id) ai_horde.deleteImageGenerationRequest(generation.id); |
| 375 | }); | 375 | }); |
| @@ -276,7 +276,7 @@ router.post('/generate', jsonParser, async function (req, res) { | |||
| 276 | 276 | ||
| 277 | /** @type {any} */ | 277 | /** @type {any} */ |
| 278 | const data = await response.json(); | 278 | const data = await response.json(); |
| 279 | console.debug('NovelAI Output', data?.output); | 279 | console.info('NovelAI Output', data?.output); |
| 280 | return res.send(data); | 280 | return res.send(data); |
| 281 | } | 281 | } |
| 282 | } catch (error) { | 282 | } catch (error) { |
| @@ -164,7 +164,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { | |||
| 164 | 164 | ||
| 165 | /** @type {any} */ | 165 | /** @type {any} */ |
| 166 | const data = await result.json(); | 166 | const data = await result.json(); |
| 167 | console.debug('Multimodal captioning response', data); | 167 | console.info('Multimodal captioning response', data); |
| 168 | const caption = data?.choices[0]?.message?.content; | 168 | const caption = data?.choices[0]?.message?.content; |
| 169 | 169 | ||
| 170 | if (!caption) { | 170 | if (!caption) { |
| @@ -43,8 +43,8 @@ router.post('/recognize', jsonParser, async (req, res) => { | |||
| 43 | const start = performance.now(); | 43 | const start = performance.now(); |
| 44 | const result = await pipe(wav, { language: lang || null, task: 'transcribe' }); | 44 | const result = await pipe(wav, { language: lang || null, task: 'transcribe' }); |
| 45 | const end = performance.now(); | 45 | const end = performance.now(); |
| 46 | console.debug(`Execution duration: ${(end - start) / 1000} seconds`); | 46 | console.info(`Execution duration: ${(end - start) / 1000} seconds`); |
| 47 | console.debug('Transcribed audio:', result.text); | 47 | console.info('Transcribed audio:', result.text); |
| 48 | 48 | ||
| 49 | return res.json({ text: result.text }); | 49 | return res.json({ text: result.text }); |
| 50 | } catch (error) { | 50 | } catch (error) { |
| @@ -35,7 +35,7 @@ function decodeBuffer(buffer) { | |||
| 35 | try { | 35 | try { |
| 36 | return iconv.decode(Buffer.from(buffer), 'utf-8'); | 36 | return iconv.decode(Buffer.from(buffer), 'utf-8'); |
| 37 | } catch (error) { | 37 | } catch (error) { |
| 38 | console.warn('Failed to decode buffer:', error); | 38 | console.error('Failed to decode buffer:', error); |
| 39 | return Buffer.from(buffer).toString('utf-8'); | 39 | return Buffer.from(buffer).toString('utf-8'); |
| 40 | } | 40 | } |
| 41 | } | 41 | } |
| @@ -165,9 +165,7 @@ export function formatBytes(bytes) { | |||
| 165 | */ | 165 | */ |
| 166 | export async function extractFileFromZipBuffer(archiveBuffer, fileExtension) { | 166 | export async function extractFileFromZipBuffer(archiveBuffer, fileExtension) { |
| 167 | return await new Promise((resolve, reject) => yauzl.fromBuffer(Buffer.from(archiveBuffer), { lazyEntries: true }, (err, zipfile) => { | 167 | return await new Promise((resolve, reject) => yauzl.fromBuffer(Buffer.from(archiveBuffer), { lazyEntries: true }, (err, zipfile) => { |
| 168 | if (err) { | 168 | if (err) reject(err); |
| 169 | reject(err); | ||
| 170 | } | ||
| 171 | 169 | ||
| 172 | zipfile.readEntry(); | 170 | zipfile.readEntry(); |
| 173 | zipfile.on('entry', (entry) => { | 171 | zipfile.on('entry', (entry) => { |
| @@ -469,9 +467,8 @@ export function forwardFetchResponse(from, to) { | |||
| 469 | from.body.pipe(to); | 467 | from.body.pipe(to); |
| 470 | 468 | ||
| 471 | to.socket.on('close', function () { | 469 | to.socket.on('close', function () { |
| 472 | if (from.body instanceof Readable) { | 470 | if (from.body instanceof Readable) from.body.destroy(); // Close the remote stream |
| 473 | from.body.destroy(); // Close the remote stream | 471 | |
| 474 | } | ||
| 475 | to.end(); // End the Express response | 472 | to.end(); // End the Express response |
| 476 | }); | 473 | }); |
| 477 | 474 | ||