Merge pull request #3307 from Eradev/staging-3071-v2 Added option to disable the chat logs in terminal v2
Signed| @@ -65,6 +65,8 @@ autheliaAuth: false | ||
| 65 | 65 | # the username and passwords for basic auth are the same as those |
| 66 | 66 | # for the individual accounts |
| 67 | 67 | perUserBasicAuth: false |
| 68 | +# Minimum log level to display in the terminal (DEBUG = 0, INFO = 1, WARN = 2, ERROR = 3) | |
| 69 | +minLogLevel: 0 | |
| 68 | 70 | |
| 69 | 71 | # User session timeout *in seconds* (defaults to 24 hours). |
| 70 | 72 | ## Set to a positive number to expire session after a certain time of inactivity |
| @@ -69,6 +69,7 @@ const hash_derivations = { | ||
| 69 | 69 | // DeepSeek R1 |
| 70 | 70 | 'b6835114b7303ddd78919a82e4d9f7d8c26ed0d7dfc36beeb12d524f6144eab1': |
| 71 | 71 | 'DeepSeek-V2.5' |
| 72 | + , | |
| 72 | 73 | }; |
| 73 | 74 | |
| 74 | 75 | const substr_derivations = { |
| @@ -97,6 +98,6 @@ export async function deriveTemplatesFromChatTemplate(chat_template, hash) { | ||
| 97 | 98 | } |
| 98 | 99 | } |
| 99 | 100 | |
| 100 | 101 | console.logwarn(`Unknown chat template hash: ${hash} for [${chat_template}]`); |
| 101 | 102 | return null; |
| 102 | 103 | } |
| @@ -1,15 +1,14 @@ | ||
| 1 | 1 | import { getRequestHeaders, substituteParams } from '../../../../script.js'; |
| 2 | 2 | import { Popup, POPUP_RESULT, POPUP_TYPE } from '../../../popup.js'; |
| 3 | 3 | import { executeSlashCommands, executeSlashCommandsOnChatInput, executeSlashCommandsWithOptions } from '../../../slash-commands.js'; |
| 4 | -import { SlashCommandParser } from '../../../slash-commands/SlashCommandParser.js'; | |
| 5 | 4 | import { SlashCommandScope } from '../../../slash-commands/SlashCommandScope.js'; |
| 6 | 5 | import { debounceAsync, log, warnSlashCommandParser } from '../index../../slash-commands/SlashCommandParser.js'; |
| 6 | +import { debounceAsync, warn } from '../index.js'; | |
| 7 | 7 | import { QuickReply } from './QuickReply.js'; |
| 8 | 8 | |
| 9 | 9 | export class QuickReplySet { |
| 10 | 10 | /**@type {QuickReplySet[]}*/ static list = []; |
| 11 | 11 | |
| 12 | - | |
| 13 | 12 | static from(props) { |
| 14 | 13 | props.qrList = []; //props.qrList?.map(it=>QuickReply.from(it)); |
| 15 | 14 | const instance = Object.assign(new this(), props); |
| @@ -24,9 +23,6 @@ export class QuickReplySet { | ||
| 24 | 23 | return this.list.find(it=>it.name == name); |
| 25 | 24 | } |
| 26 | 25 | |
| 27 | - | |
| 28 | - | |
| 29 | - | |
| 30 | 26 | /**@type {string}*/ name; |
| 31 | 27 | /**@type {boolean}*/ disableSend = false; |
| 32 | 28 | /**@type {boolean}*/ placeBeforeInput = false; |
| @@ -34,19 +30,12 @@ export class QuickReplySet { | ||
| 34 | 30 | /**@type {string}*/ color = 'transparent'; |
| 35 | 31 | /**@type {boolean}*/ onlyBorderColor = false; |
| 36 | 32 | /**@type {QuickReply[]}*/ qrList = []; |
| 37 | - | |
| 38 | 33 | /**@type {number}*/ idIndex = 0; |
| 39 | - | |
| 40 | 34 | /**@type {boolean}*/ isDeleted = false; |
| 41 | - | |
| 42 | 35 | /**@type {function}*/ save; |
| 43 | - | |
| 44 | 36 | /**@type {HTMLElement}*/ dom; |
| 45 | 37 | /**@type {HTMLElement}*/ settingsDom; |
| 46 | 38 | |
| 47 | - | |
| 48 | - | |
| 49 | - | |
| 50 | 39 | constructor() { |
| 51 | 40 | this.save = debounceAsync(()=>this.performSave(), 200); |
| 52 | 41 | } |
| @@ -55,9 +44,6 @@ export class QuickReplySet { | ||
| 55 | 44 | this.qrList.forEach(qr=>this.hookQuickReply(qr)); |
| 56 | 45 | } |
| 57 | 46 | |
| 58 | - | |
| 59 | - | |
| 60 | - | |
| 61 | 47 | unrender() { |
| 62 | 48 | this.dom?.remove(); |
| 63 | 49 | this.dom = null; |
| @@ -100,9 +86,6 @@ export class QuickReplySet { | ||
| 100 | 86 | } |
| 101 | 87 | } |
| 102 | 88 | |
| 103 | - | |
| 104 | - | |
| 105 | - | |
| 106 | 89 | renderSettings() { |
| 107 | 90 | if (!this.settingsDom) { |
| 108 | 91 | this.settingsDom = document.createElement('div'); { |
| @@ -123,9 +106,6 @@ export class QuickReplySet { | ||
| 123 | 106 | this.settingsDom.append(qr.renderSettings(idx)); |
| 124 | 107 | } |
| 125 | 108 | |
| 126 | - | |
| 127 | - | |
| 128 | - | |
| 129 | 109 | /** |
| 130 | 110 | * |
| 131 | 111 | * @param {QuickReply} qr |
| @@ -138,6 +118,7 @@ export class QuickReplySet { | ||
| 138 | 118 | closure.scope.setMacro('arg::*', ''); |
| 139 | 119 | return (await closure.execute())?.pipe; |
| 140 | 120 | } |
| 121 | + | |
| 141 | 122 | /** |
| 142 | 123 | * |
| 143 | 124 | * @param {QuickReply} qr The QR to execute. |
| @@ -207,6 +188,7 @@ export class QuickReplySet { | ||
| 207 | 188 | document.querySelector('#send_but').click(); |
| 208 | 189 | } |
| 209 | 190 | } |
| 191 | + | |
| 210 | 192 | /** |
| 211 | 193 | * @param {QuickReply} qr |
| 212 | 194 | * @param {string} [message] - optional altered message to be used |
| @@ -220,9 +202,6 @@ export class QuickReplySet { | ||
| 220 | 202 | }); |
| 221 | 203 | } |
| 222 | 204 | |
| 223 | - | |
| 224 | - | |
| 225 | - | |
| 226 | 205 | addQuickReply(data = {}) { |
| 227 | 206 | const id = Math.max(this.idIndex, this.qrList.reduce((max,qr)=>Math.max(max,qr.id),0)) + 1; |
| 228 | 207 | data.id = |
| @@ -239,6 +218,7 @@ export class QuickReplySet { | ||
| 239 | 218 | this.save(); |
| 240 | 219 | return qr; |
| 241 | 220 | } |
| 221 | + | |
| 242 | 222 | addQuickReplyFromText(qrJson) { |
| 243 | 223 | let data; |
| 244 | 224 | if (qrJson) { |
| @@ -371,7 +351,6 @@ export class QuickReplySet { | ||
| 371 | 351 | this.save(); |
| 372 | 352 | } |
| 373 | 353 | |
| 374 | - | |
| 375 | 354 | toJSON() { |
| 376 | 355 | return { |
| 377 | 356 | version: 2, |
| @@ -386,7 +365,6 @@ export class QuickReplySet { | ||
| 386 | 365 | }; |
| 387 | 366 | } |
| 388 | 367 | |
| 389 | - | |
| 390 | 368 | async performSave() { |
| 391 | 369 | const response = await fetch('/api/quick-replies/save', { |
| 392 | 370 | method: 'POST', |
| @@ -1621,14 +1621,14 @@ jQuery(async () => { | ||
| 1621 | 1621 | const attachments = source ? getDataBankAttachmentsForSource(source, false) : getDataBankAttachments(false); |
| 1622 | 1622 | const collectionIds = await ingestDataBankAttachments(String(source)); |
| 1623 | 1623 | const queryResults = await queryMultipleCollections(collectionIds, String(query), count, threshold); |
| 1624 | - | |
| 1624 | + | |
| 1625 | 1625 | // Get URLs |
| 1626 | 1626 | const urls = Object |
| 1627 | 1627 | .keys(queryResults) |
| 1628 | 1628 | .map(x => attachments.find(y => getFileCollectionId(y.url) === x)) |
| 1629 | 1629 | .filter(x => x) |
| 1630 | 1630 | .map(x => x.url); |
| 1631 | - | |
| 1631 | + | |
| 1632 | 1632 | // Gets the actual text content of chunks |
| 1633 | 1633 | const getChunksText = () => { |
| 1634 | 1634 | let textResult = ''; |
| @@ -1638,14 +1638,12 @@ jQuery(async () => { | ||
| 1638 | 1638 | } |
| 1639 | 1639 | return textResult; |
| 1640 | 1640 | }; |
| 1641 | - | |
| 1642 | 1641 | if (args.return === 'chunks') { |
| 1643 | 1642 | return getChunksText(); |
| 1644 | 1643 | } |
| 1645 | 1644 | |
| 1646 | 1645 | // @ts-ignore |
| 1647 | 1646 | return slashCommandReturnHelper.doReturn(args.return ?? 'object', urls, { objectToStringFunc: list => list.join('\n') }); |
| 1648 | - | |
| 1649 | 1647 | }, |
| 1650 | 1648 | aliases: ['databank-search', 'data-bank-search'], |
| 1651 | 1649 | helpString: 'Search the Data Bank for a specific query using vector similarity. Returns a list of file URLs with the most relevant content.', |
| @@ -1660,10 +1658,10 @@ jQuery(async () => { | ||
| 1660 | 1658 | defaultValue: 'object', |
| 1661 | 1659 | enumList: [ |
| 1662 | 1660 | new SlashCommandEnumValue('chunks', 'Return the actual content chunks', enumTypes.enum, '{}'), |
| 1663 | 1661 | ...slashCommandReturnHelper.enumList({ allowObject: true }), |
| 1664 | 1662 | ], |
| 1665 | 1663 | forceEnum: true, |
| 1666 | 1664 | }), |
| 1667 | 1665 | ], |
| 1668 | 1666 | unnamedArgumentList: [ |
| 1669 | 1667 | new SlashCommandArgument('Query to search by.', ARGUMENT_TYPE.STRING, true, false), |
| @@ -1443,9 +1443,7 @@ async function sendWindowAIRequest(messages, signal, stream) { | ||
| 1443 | 1443 | } |
| 1444 | 1444 | |
| 1445 | 1445 | const onStreamResult = (res, err) => { |
| 1446 | 1446 | if (err) {return; |
| 1447 | - return; | |
| 1448 | - } | |
| 1449 | 1447 | |
| 1450 | 1448 | const thisContent = res?.message?.content; |
| 1451 | 1449 | |
| @@ -66,6 +66,7 @@ import { | ||
| 66 | 66 | removeColorFormatting, |
| 67 | 67 | getSeparator, |
| 68 | 68 | safeReadFileSync, |
| 69 | + setupLogLevel, | |
| 69 | 70 | } from './src/util.js'; |
| 70 | 71 | import { UPLOADS_DIRECTORY } from './src/constants.js'; |
| 71 | 72 | import { ensureThumbnailCache } from './src/endpoints/thumbnails.js'; |
| @@ -698,8 +699,9 @@ const preSetupTasks = async function () { | ||
| 698 | 699 | function getAutorunHostname() { |
| 699 | 700 | if (autorunHostname === 'auto') { |
| 700 | 701 | if (enableIPv6 && enableIPv4) { |
| 701 | - if (avoidLocalhost) return '[::1]'; | |
| 702 | + return avoidLocalhost | |
| 702 | - return 'localhost'; | |
| 703 | + ? '[::1]' | |
| 704 | + : 'localhost'; | |
| 703 | 705 | } |
| 704 | 706 | |
| 705 | 707 | if (enableIPv6) { |
| @@ -765,6 +767,8 @@ const postSetupTasks = async function (v6Failed, v4Failed) { | ||
| 765 | 767 | } |
| 766 | 768 | } |
| 767 | 769 | } |
| 770 | + | |
| 771 | + setupLogLevel(); | |
| 768 | 772 | }; |
| 769 | 773 | |
| 770 | 774 | /** |
| @@ -414,3 +414,10 @@ export const VLLM_KEYS = [ | ||
| 414 | 414 | 'guided_decoding_backend', |
| 415 | 415 | 'guided_whitespace_pattern', |
| 416 | 416 | ]; |
| 417 | + | |
| 418 | +export const LOG_LEVELS = { | |
| 419 | + DEBUG: 0, | |
| 420 | + INFO: 1, | |
| 421 | + WARN: 2, | |
| 422 | + ERROR: 3, | |
| 423 | +}; | |
| @@ -32,7 +32,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 32 | 32 | max_tokens: 4096, |
| 33 | 33 | }; |
| 34 | 34 | |
| 35 | 35 | console.logdebug('Multimodal captioning request', body); |
| 36 | 36 | |
| 37 | 37 | const result = await fetch(url, { |
| 38 | 38 | body: JSON.stringify(body), |
| @@ -46,14 +46,14 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 46 | 46 | |
| 47 | 47 | if (!result.ok) { |
| 48 | 48 | const text = await result.text(); |
| 49 | 49 | console.logwarn(`Claude API returned error: ${result.status} ${result.statusText}`, text); |
| 50 | 50 | return response.status(result.status).send({ error: true }); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** @type {any} */ |
| 54 | 54 | const generateResponseJson = await result.json(); |
| 55 | 55 | const caption = generateResponseJson.content[0].text; |
| 56 | 56 | console.logdebug('Claude response:', generateResponseJson); |
| 57 | 57 | |
| 58 | 58 | if (!caption) { |
| 59 | 59 | return response.status(500).send('No caption found'); |
| @@ -176,7 +176,7 @@ router.post('/get', jsonParser, async (request, response) => { | ||
| 176 | 176 | } |
| 177 | 177 | } |
| 178 | 178 | catch (err) { |
| 179 | 179 | console.logerror(err); |
| 180 | 180 | } |
| 181 | 181 | return response.send(output); |
| 182 | 182 | }); |
| @@ -200,7 +200,7 @@ router.post('/download', jsonParser, async (request, response) => { | ||
| 200 | 200 | category = i; |
| 201 | 201 | |
| 202 | 202 | if (category === null) { |
| 203 | 203 | console.debugerror('Bad request: unsupported asset category.'); |
| 204 | 204 | return response.sendStatus(400); |
| 205 | 205 | } |
| 206 | 206 | |
| @@ -212,7 +212,7 @@ router.post('/download', jsonParser, async (request, response) => { | ||
| 212 | 212 | |
| 213 | 213 | const temp_path = path.join(request.user.directories.assets, 'temp', request.body.filename); |
| 214 | 214 | const file_path = path.join(request.user.directories.assets, category, request.body.filename); |
| 215 | 215 | console.debuginfo('Request received to download', url, 'to', file_path); |
| 216 | 216 | |
| 217 | 217 | try { |
| 218 | 218 | // Download to temp |
| @@ -241,13 +241,13 @@ router.post('/download', jsonParser, async (request, response) => { | ||
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | // Move into asset place |
| 244 | 244 | console.debuginfo('Download finished, moving file from', temp_path, 'to', file_path); |
| 245 | 245 | fs.copyFileSync(temp_path, file_path); |
| 246 | 246 | fs.rmSync(temp_path); |
| 247 | 247 | response.sendStatus(200); |
| 248 | 248 | } |
| 249 | 249 | catch (error) { |
| 250 | 250 | console.logerror(error); |
| 251 | 251 | response.sendStatus(500); |
| 252 | 252 | } |
| 253 | 253 | }); |
| @@ -270,7 +270,7 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 270 | 270 | category = i; |
| 271 | 271 | |
| 272 | 272 | if (category === null) { |
| 273 | 273 | console.debugerror('Bad request: unsupported asset category.'); |
| 274 | 274 | return response.sendStatus(400); |
| 275 | 275 | } |
| 276 | 276 | |
| @@ -280,7 +280,7 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 280 | 280 | return response.status(400).send(validation.message); |
| 281 | 281 | |
| 282 | 282 | const file_path = path.join(request.user.directories.assets, category, request.body.filename); |
| 283 | 283 | console.debuginfo('Request received to delete', category, file_path); |
| 284 | 284 | |
| 285 | 285 | try { |
| 286 | 286 | // Delete if previous download failed |
| @@ -288,17 +288,17 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 288 | 288 | fs.unlink(file_path, (err) => { |
| 289 | 289 | if (err) throw err; |
| 290 | 290 | }); |
| 291 | 291 | console.debuginfo('Asset deleted.'); |
| 292 | 292 | } |
| 293 | 293 | else { |
| 294 | 294 | console.debugerror('Asset not found.'); |
| 295 | 295 | response.sendStatus(400); |
| 296 | 296 | } |
| 297 | 297 | // Move into asset place |
| 298 | 298 | response.sendStatus(200); |
| 299 | 299 | } |
| 300 | 300 | catch (error) { |
| 301 | 301 | console.logerror(error); |
| 302 | 302 | response.sendStatus(500); |
| 303 | 303 | } |
| 304 | 304 | }); |
| @@ -314,6 +314,7 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 314 | 314 | */ |
| 315 | 315 | router.post('/character', jsonParser, async (request, response) => { |
| 316 | 316 | if (request.query.name === undefined) return response.sendStatus(400); |
| 317 | + | |
| 317 | 318 | // For backwards compatibility, don't reject invalid character names, just sanitize them |
| 318 | 319 | const name = sanitize(request.query.name.toString()); |
| 319 | 320 | const inputCategory = request.query.category; |
| @@ -325,7 +326,7 @@ router.post('/character', jsonParser, async (request, response) => { | ||
| 325 | 326 | category = i; |
| 326 | 327 | |
| 327 | 328 | if (category === null) { |
| 328 | 329 | console.debugerror('Bad request: unsupported asset category.'); |
| 329 | 330 | return response.sendStatus(400); |
| 330 | 331 | } |
| 331 | 332 | |
| @@ -364,7 +365,7 @@ router.post('/character', jsonParser, async (request, response) => { | ||
| 364 | 365 | return response.send(output); |
| 365 | 366 | } |
| 366 | 367 | catch (err) { |
| 367 | 368 | console.logerror(err); |
| 368 | 369 | return response.sendStatus(500); |
| 369 | 370 | } |
| 370 | 371 | }); |
| @@ -11,14 +11,14 @@ router.post('/list', jsonParser, async (req, res) => { | ||
| 11 | 11 | const key = readSecret(req.user.directories, SECRET_KEYS.AZURE_TTS); |
| 12 | 12 | |
| 13 | 13 | if (!key) { |
| 14 | 14 | console.errorwarn('Azure TTS API Key not set'); |
| 15 | 15 | return res.sendStatus(403); |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | const region = req.body.region; |
| 19 | 19 | |
| 20 | 20 | if (!region) { |
| 21 | 21 | console.errorwarn('Azure TTS region not set'); |
| 22 | 22 | return res.sendStatus(400); |
| 23 | 23 | } |
| 24 | 24 | |
| @@ -32,7 +32,7 @@ router.post('/list', jsonParser, async (req, res) => { | ||
| 32 | 32 | }); |
| 33 | 33 | |
| 34 | 34 | if (!response.ok) { |
| 35 | 35 | console.errorwarn('Azure Request failed', response.status, response.statusText); |
| 36 | 36 | return res.sendStatus(500); |
| 37 | 37 | } |
| 38 | 38 | |
| @@ -49,13 +49,13 @@ router.post('/generate', jsonParser, async (req, res) => { | ||
| 49 | 49 | const key = readSecret(req.user.directories, SECRET_KEYS.AZURE_TTS); |
| 50 | 50 | |
| 51 | 51 | if (!key) { |
| 52 | 52 | console.errorwarn('Azure TTS API Key not set'); |
| 53 | 53 | return res.sendStatus(403); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | const { text, voice, region } = req.body; |
| 57 | 57 | if (!text || !voice || !region) { |
| 58 | 58 | console.errorwarn('Missing required parameters'); |
| 59 | 59 | return res.sendStatus(400); |
| 60 | 60 | } |
| 61 | 61 | |
| @@ -75,7 +75,7 @@ router.post('/generate', jsonParser, async (req, res) => { | ||
| 75 | 75 | }); |
| 76 | 76 | |
| 77 | 77 | if (!response.ok) { |
| 78 | 78 | console.errorwarn('Azure Request failed', response.status, response.statusText); |
| 79 | 79 | return res.sendStatus(500); |
| 80 | 80 | } |
| 81 | 81 | |
| @@ -114,7 +114,7 @@ async function sendClaudeRequest(request, response) { | ||
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | if (!apiKey) { |
| 117 | 117 | console.logwarn(color.red(`Claude API key is missing.\n${divider}`)); |
| 118 | 118 | return response.status(400).send({ error: true }); |
| 119 | 119 | } |
| 120 | 120 | |
| @@ -230,12 +230,12 @@ async function sendScaleRequest(request, response) { | ||
| 230 | 230 | const apiKey = readSecret(request.user.directories, SECRET_KEYS.SCALE); |
| 231 | 231 | |
| 232 | 232 | if (!apiKey) { |
| 233 | 233 | console.logwarn('Scale API key is missing.'); |
| 234 | 234 | return response.status(400).send({ error: true }); |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | const requestPrompt = convertTextCompletionPrompt(request.body.messages); |
| 238 | 238 | console.logdebug('Scale request:', requestPrompt); |
| 239 | 239 | |
| 240 | 240 | try { |
| 241 | 241 | const controller = new AbortController(); |
| @@ -254,18 +254,18 @@ async function sendScaleRequest(request, response) { | ||
| 254 | 254 | }); |
| 255 | 255 | |
| 256 | 256 | if (!generateResponse.ok) { |
| 257 | 257 | console.logwarn(`Scale API returned error: ${generateResponse.status} ${generateResponse.statusText} ${await generateResponse.text()}`); |
| 258 | 258 | return response.status(500).send({ error: true }); |
| 259 | 259 | } |
| 260 | 260 | |
| 261 | 261 | /** @type {any} */ |
| 262 | 262 | const generateResponseJson = await generateResponse.json(); |
| 263 | 263 | console.logdebug('Scale response:', generateResponseJson); |
| 264 | 264 | |
| 265 | 265 | const reply = { choices: [{ 'message': { 'content': generateResponseJson.output } }] }; |
| 266 | 266 | return response.send(reply); |
| 267 | 267 | } catch (error) { |
| 268 | 268 | console.logerror(error); |
| 269 | 269 | if (!response.headersSent) { |
| 270 | 270 | return response.status(500).send({ error: true }); |
| 271 | 271 | } |
| @@ -282,7 +282,7 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 282 | 282 | const apiKey = request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.MAKERSUITE); |
| 283 | 283 | |
| 284 | 284 | if (!request.body.reverse_proxy && !apiKey) { |
| 285 | 285 | console.logwarn('Google AI Studio API key is missing.'); |
| 286 | 286 | return response.status(400).send({ error: true }); |
| 287 | 287 | } |
| 288 | 288 | |
| @@ -340,7 +340,7 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | const body = getGeminiBody(); |
| 343 | 343 | console.logdebug('Google AI Studio request:', body); |
| 344 | 344 | |
| 345 | 345 | try { |
| 346 | 346 | const controller = new AbortController(); |
| @@ -366,14 +366,14 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 366 | 366 | // Pipe remote SSE stream to Express response |
| 367 | 367 | forwardFetchResponse(generateResponse, response); |
| 368 | 368 | } catch (error) { |
| 369 | 369 | console.logerror('Error forwarding streaming response:', error); |
| 370 | 370 | if (!response.headersSent) { |
| 371 | 371 | return response.status(500).send({ error: true }); |
| 372 | 372 | } |
| 373 | 373 | } |
| 374 | 374 | } else { |
| 375 | 375 | if (!generateResponse.ok) { |
| 376 | 376 | console.logwarn(`Google AI Studio API returned error: ${generateResponse.status} ${generateResponse.statusText} ${await generateResponse.text()}`); |
| 377 | 377 | return response.status(generateResponse.status).send({ error: true }); |
| 378 | 378 | } |
| 379 | 379 | |
| @@ -391,12 +391,12 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | const responseContent = candidates[0].content ?? candidates[0].output; |
| 394 | 394 | console.logwarn('Google AI Studio response:', responseContent); |
| 395 | 395 | |
| 396 | 396 | const responseText = typeof responseContent === 'string' ? responseContent : responseContent?.parts?.filter(part => !part.thought)?.map(part => part.text)?.join('\n\n'); |
| 397 | 397 | if (!responseText) { |
| 398 | 398 | let message = 'Google AI Studio Candidate text empty'; |
| 399 | 399 | console.logwarn(message, generateResponseJson); |
| 400 | 400 | return response.send({ error: { message } }); |
| 401 | 401 | } |
| 402 | 402 | |
| @@ -405,7 +405,7 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 405 | 405 | return response.send(reply); |
| 406 | 406 | } |
| 407 | 407 | } catch (error) { |
| 408 | 408 | console.logerror('Error communicating with Google AI Studio API: ', error); |
| 409 | 409 | if (!response.headersSent) { |
| 410 | 410 | return response.status(500).send({ error: true }); |
| 411 | 411 | } |
| @@ -419,8 +419,9 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 419 | 419 | */ |
| 420 | 420 | async function sendAI21Request(request, response) { |
| 421 | 421 | if (!request.body) return response.sendStatus(400); |
| 422 | + | |
| 422 | 423 | const controller = new AbortController(); |
| 423 | 424 | console.logdebug(request.body.messages); |
| 424 | 425 | request.socket.removeAllListeners('close'); |
| 425 | 426 | request.socket.on('close', function () { |
| 426 | 427 | controller.abort(); |
| @@ -446,7 +447,7 @@ async function sendAI21Request(request, response) { | ||
| 446 | 447 | signal: controller.signal, |
| 447 | 448 | }; |
| 448 | 449 | |
| 449 | 450 | console.logdebug('AI21 request:', body); |
| 450 | 451 | |
| 451 | 452 | try { |
| 452 | 453 | const generateResponse = await fetch(API_AI21 + '/chat/completions', options); |
| @@ -455,16 +456,16 @@ async function sendAI21Request(request, response) { | ||
| 455 | 456 | } else { |
| 456 | 457 | if (!generateResponse.ok) { |
| 457 | 458 | const errorText = await generateResponse.text(); |
| 458 | 459 | console.logwarn(`AI21 API returned error: ${generateResponse.status} ${generateResponse.statusText} ${errorText}`); |
| 459 | 460 | const errorJson = tryParse(errorText) ?? { error: true }; |
| 460 | 461 | return response.status(500).send(errorJson); |
| 461 | 462 | } |
| 462 | 463 | const generateResponseJson = await generateResponse.json(); |
| 463 | 464 | console.logdebug('AI21 response:', generateResponseJson); |
| 464 | 465 | return response.send(generateResponseJson); |
| 465 | 466 | } |
| 466 | 467 | } catch (error) { |
| 467 | 468 | console.logerror('Error communicating with AI21 API: ', error); |
| 468 | 469 | if (!response.headersSent) { |
| 469 | 470 | response.send({ error: true }); |
| 470 | 471 | } else { |
| @@ -483,7 +484,7 @@ async function sendMistralAIRequest(request, response) { | ||
| 483 | 484 | const apiKey = request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.MISTRALAI); |
| 484 | 485 | |
| 485 | 486 | if (!apiKey) { |
| 486 | 487 | console.logwarn('MistralAI API key is missing.'); |
| 487 | 488 | return response.status(400).send({ error: true }); |
| 488 | 489 | } |
| 489 | 490 | |
| @@ -524,7 +525,7 @@ async function sendMistralAIRequest(request, response) { | ||
| 524 | 525 | timeout: 0, |
| 525 | 526 | }; |
| 526 | 527 | |
| 527 | 528 | console.logdebug('MisralAI request:', requestBody); |
| 528 | 529 | |
| 529 | 530 | const generateResponse = await fetch(apiUrl + '/chat/completions', config); |
| 530 | 531 | if (request.body.stream) { |
| @@ -532,16 +533,16 @@ async function sendMistralAIRequest(request, response) { | ||
| 532 | 533 | } else { |
| 533 | 534 | if (!generateResponse.ok) { |
| 534 | 535 | const errorText = await generateResponse.text(); |
| 535 | 536 | console.logwarn(`MistralAI API returned error: ${generateResponse.status} ${generateResponse.statusText} ${errorText}`); |
| 536 | 537 | const errorJson = tryParse(errorText) ?? { error: true }; |
| 537 | 538 | return response.status(500).send(errorJson); |
| 538 | 539 | } |
| 539 | 540 | const generateResponseJson = await generateResponse.json(); |
| 540 | 541 | console.logdebug('MistralAI response:', generateResponseJson); |
| 541 | 542 | return response.send(generateResponseJson); |
| 542 | 543 | } |
| 543 | 544 | } catch (error) { |
| 544 | 545 | console.logerror('Error communicating with MistralAI API: ', error); |
| 545 | 546 | if (!response.headersSent) { |
| 546 | 547 | response.send({ error: true }); |
| 547 | 548 | } else { |
| @@ -564,7 +565,7 @@ async function sendCohereRequest(request, response) { | ||
| 564 | 565 | }); |
| 565 | 566 | |
| 566 | 567 | if (!apiKey) { |
| 567 | 568 | console.logwarn('Cohere API key is missing.'); |
| 568 | 569 | return response.status(400).send({ error: true }); |
| 569 | 570 | } |
| 570 | 571 | |
| @@ -603,7 +604,7 @@ async function sendCohereRequest(request, response) { | ||
| 603 | 604 | requestBody.safety_mode = 'OFF'; |
| 604 | 605 | } |
| 605 | 606 | |
| 606 | 607 | console.logdebug('Cohere request:', requestBody); |
| 607 | 608 | |
| 608 | 609 | const config = { |
| 609 | 610 | method: 'POST', |
| @@ -625,16 +626,16 @@ async function sendCohereRequest(request, response) { | ||
| 625 | 626 | const generateResponse = await fetch(apiUrl, config); |
| 626 | 627 | if (!generateResponse.ok) { |
| 627 | 628 | const errorText = await generateResponse.text(); |
| 628 | 629 | console.logwarn(`Cohere API returned error: ${generateResponse.status} ${generateResponse.statusText} ${errorText}`); |
| 629 | 630 | const errorJson = tryParse(errorText) ?? { error: true }; |
| 630 | 631 | return response.status(500).send(errorJson); |
| 631 | 632 | } |
| 632 | 633 | const generateResponseJson = await generateResponse.json(); |
| 633 | 634 | console.logdebug('Cohere response:', generateResponseJson); |
| 634 | 635 | return response.send(generateResponseJson); |
| 635 | 636 | } |
| 636 | 637 | } catch (error) { |
| 637 | 638 | console.logerror('Error communicating with Cohere API: ', error); |
| 638 | 639 | if (!response.headersSent) { |
| 639 | 640 | response.send({ error: true }); |
| 640 | 641 | } else { |
| @@ -653,7 +654,7 @@ async function sendDeepSeekRequest(request, response) { | ||
| 653 | 654 | const apiKey = request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.DEEPSEEK); |
| 654 | 655 | |
| 655 | 656 | if (!apiKey && !request.body.reverse_proxy) { |
| 656 | 657 | console.logwarn('DeepSeek API key is missing.'); |
| 657 | 658 | return response.status(400).send({ error: true }); |
| 658 | 659 | } |
| 659 | 660 | |
| @@ -774,12 +775,12 @@ router.post('/status', jsonParser, async function (request, response_getstatus_o | ||
| 774 | 775 | api_key_openai = request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.DEEPSEEK); |
| 775 | 776 | headers = {}; |
| 776 | 777 | } else { |
| 777 | 778 | console.logwarn('This chat completion source is not supported yet.'); |
| 778 | 779 | return response_getstatus_openai.status(400).send({ error: true }); |
| 779 | 780 | } |
| 780 | 781 | |
| 781 | 782 | if (!api_key_openai && !request.body.reverse_proxy && request.body.chat_completion_source !== CHAT_COMPLETION_SOURCES.CUSTOM) { |
| 782 | 783 | console.logwarn('Chat Completion API key is missing.'); |
| 783 | 784 | return response_getstatus_openai.status(400).send({ error: true }); |
| 784 | 785 | } |
| 785 | 786 | |
| @@ -814,23 +815,23 @@ router.post('/status', jsonParser, async function (request, response_getstatus_o | ||
| 814 | 815 | }; |
| 815 | 816 | }); |
| 816 | 817 | |
| 817 | 818 | console.loginfo('Available OpenRouter models:', models); |
| 818 | 819 | } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.MISTRALAI) { |
| 819 | 820 | const models = data?.data; |
| 820 | 821 | console.loginfo(models); |
| 821 | 822 | } else { |
| 822 | 823 | const models = data?.data; |
| 823 | 824 | |
| 824 | 825 | if (Array.isArray(models)) { |
| 825 | 826 | const modelIds = models.filter(x => x && typeof x === 'object').map(x => x.id).sort(); |
| 826 | 827 | console.loginfo('Available models:', modelIds); |
| 827 | 828 | } else { |
| 828 | 829 | console.logwarn('Chat Completion endpoint did not return a list of models.'); |
| 829 | 830 | } |
| 830 | 831 | } |
| 831 | 832 | } |
| 832 | 833 | else { |
| 833 | 834 | console.logerror('Chat Completion status check failed. Either Access Token is incorrect or API endpoint is down.'); |
| 834 | 835 | response_getstatus_openai.send({ error: true, can_bypass: true, data: { data: [] } }); |
| 835 | 836 | } |
| 836 | 837 | } catch (e) { |
| @@ -863,7 +864,7 @@ router.post('/bias', jsonParser, async function (request, response) { | ||
| 863 | 864 | const tokenizer = getSentencepiceTokenizer(model); |
| 864 | 865 | const instance = await tokenizer?.get(); |
| 865 | 866 | if (!instance) { |
| 866 | 867 | console.warnerror('Tokenizer not initialized:', model); |
| 867 | 868 | return response.send({}); |
| 868 | 869 | } |
| 869 | 870 | encodeFunction = (text) => new Uint32Array(instance.encodeIds(text)); |
| @@ -1025,7 +1026,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1025 | 1026 | mergeObjectWithYaml(headers, request.body.custom_include_headers); |
| 1026 | 1027 | |
| 1027 | 1028 | if (request.body.custom_prompt_post_processing) { |
| 1028 | 1029 | console.loginfo('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing); |
| 1029 | 1030 | request.body.messages = postProcessPrompt( |
| 1030 | 1031 | request.body.messages, |
| 1031 | 1032 | request.body.custom_prompt_post_processing, |
| @@ -1058,12 +1059,12 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1058 | 1059 | headers = {}; |
| 1059 | 1060 | bodyParams = {}; |
| 1060 | 1061 | } else { |
| 1061 | 1062 | console.logwarn('This chat completion source is not supported yet.'); |
| 1062 | 1063 | return response.status(400).send({ error: true }); |
| 1063 | 1064 | } |
| 1064 | 1065 | |
| 1065 | 1066 | if (!apiKey && !request.body.reverse_proxy && request.body.chat_completion_source !== CHAT_COMPLETION_SOURCES.CUSTOM) { |
| 1066 | 1067 | console.logwarn('OpenAI API key is missing.'); |
| 1067 | 1068 | return response.status(400).send({ error: true }); |
| 1068 | 1069 | } |
| 1069 | 1070 | |
| @@ -1123,7 +1124,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1123 | 1124 | signal: controller.signal, |
| 1124 | 1125 | }; |
| 1125 | 1126 | |
| 1126 | 1127 | console.logdebug(requestBody); |
| 1127 | 1128 | |
| 1128 | 1129 | makeRequest(config, response, request); |
| 1129 | 1130 | |
| @@ -1140,7 +1141,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1140 | 1141 | const fetchResponse = await fetch(endpointUrl, config); |
| 1141 | 1142 | |
| 1142 | 1143 | if (request.body.stream) { |
| 1143 | 1144 | console.loginfo('Streaming request in progress'); |
| 1144 | 1145 | forwardFetchResponse(fetchResponse, response); |
| 1145 | 1146 | return; |
| 1146 | 1147 | } |
| @@ -1149,10 +1150,10 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1149 | 1150 | /** @type {any} */ |
| 1150 | 1151 | let json = await fetchResponse.json(); |
| 1151 | 1152 | response.send(json); |
| 1152 | 1153 | console.logdebug(json); |
| 1153 | 1154 | console.logdebug(json?.choices?.[0]?.message); |
| 1154 | 1155 | } else if (fetchResponse.status === 429 && retries > 0) { |
| 1155 | 1156 | console.logwarn(`Out of quota, retrying in ${Math.round(timeout / 1000)}s`); |
| 1156 | 1157 | setTimeout(() => { |
| 1157 | 1158 | timeout *= 2; |
| 1158 | 1159 | makeRequest(config, response, request, retries - 1, timeout); |
| @@ -1161,7 +1162,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1161 | 1162 | await handleErrorResponse(fetchResponse); |
| 1162 | 1163 | } |
| 1163 | 1164 | } catch (error) { |
| 1164 | 1165 | console.logerror('Generation failed', error); |
| 1165 | 1166 | const message = error.code === 'ECONNREFUSED' |
| 1166 | 1167 | ? `Connection refused: ${error.message}` |
| 1167 | 1168 | : error.message || 'Unknown error occurred'; |
| @@ -1183,7 +1184,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1183 | 1184 | |
| 1184 | 1185 | const message = errorResponse.statusText || 'Unknown error occurred'; |
| 1185 | 1186 | const quota_error = errorResponse.status === 429 && errorData?.error?.type === 'insufficient_quota'; |
| 1186 | 1187 | console.logerror('Chat completion request error: ', message, responseText); |
| 1187 | 1188 | |
| 1188 | 1189 | if (!response.headersSent) { |
| 1189 | 1190 | response.send({ error: { message }, quota_error: quota_error }); |
| @@ -22,17 +22,17 @@ router.post('/generate', jsonParser, async function (request, response_generate) | ||
| 22 | 22 | request.socket.on('close', async function () { |
| 23 | 23 | if (request.body.can_abort && !response_generate.writableEnded) { |
| 24 | 24 | try { |
| 25 | 25 | console.loginfo('Aborting Kobold generation...'); |
| 26 | 26 | // send abort signal to koboldcpp |
| 27 | 27 | const abortResponse = await fetch(`${request.body.api_server}/extra/abort`, { |
| 28 | 28 | method: 'POST', |
| 29 | 29 | }); |
| 30 | 30 | |
| 31 | 31 | if (!abortResponse.ok) { |
| 32 | 32 | console.logerror('Error sending abort request to Kobold:', abortResponse.status); |
| 33 | 33 | } |
| 34 | 34 | } catch (error) { |
| 35 | 35 | console.logerror(error); |
| 36 | 36 | } |
| 37 | 37 | } |
| 38 | 38 | controller.abort(); |
| @@ -81,7 +81,7 @@ router.post('/generate', jsonParser, async function (request, response_generate) | ||
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | console.logdebug(this_settings); |
| 85 | 85 | const args = { |
| 86 | 86 | body: JSON.stringify(this_settings), |
| 87 | 87 | headers: Object.assign( |
| @@ -105,7 +105,7 @@ router.post('/generate', jsonParser, async function (request, response_generate) | ||
| 105 | 105 | } else { |
| 106 | 106 | if (!response.ok) { |
| 107 | 107 | const errorText = await response.text(); |
| 108 | 108 | console.logwarn(`Kobold returned error: ${response.status} ${response.statusText} ${errorText}`); |
| 109 | 109 | |
| 110 | 110 | try { |
| 111 | 111 | const errorJson = JSON.parse(errorText); |
| @@ -117,7 +117,7 @@ router.post('/generate', jsonParser, async function (request, response_generate) | ||
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | const data = await response.json(); |
| 120 | 120 | console.logdebug('Endpoint response:', data); |
| 121 | 121 | return response_generate.send(data); |
| 122 | 122 | } |
| 123 | 123 | } catch (error) { |
| @@ -125,19 +125,19 @@ router.post('/generate', jsonParser, async function (request, response_generate) | ||
| 125 | 125 | switch (error?.status) { |
| 126 | 126 | case 403: |
| 127 | 127 | case 503: // retry in case of temporary service issue, possibly caused by a queue failure? |
| 128 | 128 | console.debugwarn(`KoboldAI is busy. Retry attempt ${i + 1} of ${MAX_RETRIES}...`); |
| 129 | 129 | await delay(delayAmount); |
| 130 | 130 | break; |
| 131 | 131 | default: |
| 132 | 132 | if ('status' in error) { |
| 133 | 133 | console.logerror('Status Code from Kobold:', error.status); |
| 134 | 134 | } |
| 135 | 135 | return response_generate.send({ error: true }); |
| 136 | 136 | } |
| 137 | 137 | } |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | console.logerror('Max retries exceeded. Giving up.'); |
| 141 | 141 | return response_generate.send({ error: true }); |
| 142 | 142 | }); |
| 143 | 143 | |
| @@ -193,16 +193,16 @@ router.post('/transcribe-audio', urlencodedParser, async function (request, resp | ||
| 193 | 193 | const server = request.body.server; |
| 194 | 194 | |
| 195 | 195 | if (!server) { |
| 196 | 196 | console.logerror('Server is not set'); |
| 197 | 197 | return response.sendStatus(400); |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | if (!request.file) { |
| 201 | 201 | console.logerror('No audio file found'); |
| 202 | 202 | return response.sendStatus(400); |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | console.logdebug('Transcribing audio with KoboldCpp', server); |
| 206 | 206 | |
| 207 | 207 | const fileBase64 = fs.readFileSync(request.file.path).toString('base64'); |
| 208 | 208 | fs.rmSync(request.file.path); |
| @@ -226,12 +226,12 @@ router.post('/transcribe-audio', urlencodedParser, async function (request, resp | ||
| 226 | 226 | |
| 227 | 227 | if (!result.ok) { |
| 228 | 228 | const text = await result.text(); |
| 229 | 229 | console.logerror('KoboldCpp request failed', result.statusText, text); |
| 230 | 230 | return response.status(500).send(text); |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | const data = await result.json(); |
| 234 | 234 | console.logdebug('KoboldCpp transcription response', data); |
| 235 | 235 | return response.json(data); |
| 236 | 236 | } catch (error) { |
| 237 | 237 | console.error('KoboldCpp transcription failed', error); |
| @@ -13,7 +13,7 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 13 | 13 | const cookie = readSecret(request.user.directories, SECRET_KEYS.SCALE_COOKIE); |
| 14 | 14 | |
| 15 | 15 | if (!cookie) { |
| 16 | 16 | console.logerror('No Scale cookie found'); |
| 17 | 17 | return response.sendStatus(400); |
| 18 | 18 | } |
| 19 | 19 | |
| @@ -62,7 +62,7 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 62 | 62 | }, |
| 63 | 63 | }; |
| 64 | 64 | |
| 65 | 65 | console.logdebug('Scale request:', body); |
| 66 | 66 | |
| 67 | 67 | const result = await fetch('https://dashboard.scale.com/spellbook/api/trpc/v2.variant.run', { |
| 68 | 68 | method: 'POST', |
| @@ -75,7 +75,7 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 75 | 75 | |
| 76 | 76 | if (!result.ok) { |
| 77 | 77 | const text = await result.text(); |
| 78 | 78 | console.logerror('Scale request failed', result.statusText, text); |
| 79 | 79 | return response.status(500).send({ error: { message: result.statusText } }); |
| 80 | 80 | } |
| 81 | 81 | |
| @@ -83,7 +83,7 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 83 | 83 | const data = await result.json(); |
| 84 | 84 | const output = data?.result?.data?.json?.outputs?.[0] || ''; |
| 85 | 85 | |
| 86 | 86 | console.logdebug('Scale response:', data); |
| 87 | 87 | |
| 88 | 88 | if (!output) { |
| 89 | 89 | console.warn('Scale response is empty'); |
| @@ -92,7 +92,7 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 92 | 92 | |
| 93 | 93 | return response.json({ output }); |
| 94 | 94 | } catch (error) { |
| 95 | 95 | console.logerror(error); |
| 96 | 96 | return response.sendStatus(500); |
| 97 | 97 | } |
| 98 | 98 | }); |
| @@ -58,12 +58,12 @@ async function parseOllamaStream(jsonStream, request, response) { | ||
| 58 | 58 | }); |
| 59 | 59 | |
| 60 | 60 | jsonStream.body.on('end', () => { |
| 61 | 61 | console.loginfo('Streaming request finished'); |
| 62 | 62 | response.write('data: [DONE]\n\n'); |
| 63 | 63 | response.end(); |
| 64 | 64 | }); |
| 65 | 65 | } catch (error) { |
| 66 | 66 | console.logerror('Error forwarding streaming response:', error); |
| 67 | 67 | if (!response.headersSent) { |
| 68 | 68 | return response.status(500).send({ error: true }); |
| 69 | 69 | } else { |
| @@ -79,16 +79,16 @@ async function parseOllamaStream(jsonStream, request, response) { | ||
| 79 | 79 | */ |
| 80 | 80 | async function abortKoboldCppRequest(url) { |
| 81 | 81 | try { |
| 82 | 82 | console.loginfo('Aborting Kobold generation...'); |
| 83 | 83 | const abortResponse = await fetch(`${url}/api/extra/abort`, { |
| 84 | 84 | method: 'POST', |
| 85 | 85 | }); |
| 86 | 86 | |
| 87 | 87 | if (!abortResponse.ok) { |
| 88 | 88 | console.logerror('Error sending abort request to Kobold:', abortResponse.status, abortResponse.statusText); |
| 89 | 89 | } |
| 90 | 90 | } catch (error) { |
| 91 | 91 | console.logerror(error); |
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | |
| @@ -101,7 +101,7 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 101 | 101 | request.body.api_server = request.body.api_server.replace('localhost', '127.0.0.1'); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | console.logdebug('Trying to connect to API:', request.body); |
| 105 | 105 | const baseUrl = trimV1(request.body.api_server); |
| 106 | 106 | |
| 107 | 107 | const args = { |
| @@ -123,6 +123,7 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 123 | 123 | case TEXTGEN_TYPES.LLAMACPP: |
| 124 | 124 | case TEXTGEN_TYPES.INFERMATICAI: |
| 125 | 125 | case TEXTGEN_TYPES.OPENROUTER: |
| 126 | + case TEXTGEN_TYPES.FEATHERLESS: | |
| 126 | 127 | url += '/v1/models'; |
| 127 | 128 | break; |
| 128 | 129 | case TEXTGEN_TYPES.DREAMGEN: |
| @@ -140,9 +141,6 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 140 | 141 | case TEXTGEN_TYPES.OLLAMA: |
| 141 | 142 | url += '/api/tags'; |
| 142 | 143 | break; |
| 143 | - case TEXTGEN_TYPES.FEATHERLESS: | |
| 144 | - url += '/v1/models'; | |
| 145 | - break; | |
| 146 | 144 | case TEXTGEN_TYPES.HUGGINGFACE: |
| 147 | 145 | url += '/info'; |
| 148 | 146 | break; |
| @@ -152,7 +150,7 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 152 | 150 | const isPossiblyLmStudio = modelsReply.headers.get('x-powered-by') === 'Express'; |
| 153 | 151 | |
| 154 | 152 | if (!modelsReply.ok) { |
| 155 | 153 | console.logerror('Models endpoint is offline.'); |
| 156 | 154 | return response.sendStatus(400); |
| 157 | 155 | } |
| 158 | 156 | |
| @@ -173,12 +171,12 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 173 | 171 | } |
| 174 | 172 | |
| 175 | 173 | if (!Array.isArray(data.data)) { |
| 176 | 174 | console.logerror('Models response is not an array.'); |
| 177 | 175 | return response.sendStatus(400); |
| 178 | 176 | } |
| 179 | 177 | |
| 180 | 178 | const modelIds = data.data.map(x => x.id); |
| 181 | 179 | console.loginfo('Models available:', modelIds); |
| 182 | 180 | |
| 183 | 181 | // Set result to the first model ID |
| 184 | 182 | result = modelIds[0] || 'Valid'; |
| @@ -191,7 +189,7 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 191 | 189 | if (modelInfoReply.ok) { |
| 192 | 190 | /** @type {any} */ |
| 193 | 191 | const modelInfo = await modelInfoReply.json(); |
| 194 | 192 | console.logdebug('Ooba model info:', modelInfo); |
| 195 | 193 | |
| 196 | 194 | const modelName = modelInfo?.model_name; |
| 197 | 195 | result = modelName || result; |
| @@ -208,7 +206,7 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 208 | 206 | if (modelInfoReply.ok) { |
| 209 | 207 | /** @type {any} */ |
| 210 | 208 | const modelInfo = await modelInfoReply.json(); |
| 211 | 209 | console.logdebug('Tabby model info:', modelInfo); |
| 212 | 210 | |
| 213 | 211 | const modelName = modelInfo?.id; |
| 214 | 212 | result = modelName || result; |
| @@ -255,7 +253,7 @@ router.post('/props', jsonParser, async function (request, response) { | ||
| 255 | 253 | props['chat_template'] = props['chat_template'].slice(0, -1) + '\n'; |
| 256 | 254 | } |
| 257 | 255 | props['chat_template_hash'] = createHash('sha256').update(props['chat_template']).digest('hex'); |
| 258 | 256 | console.logdebug(`Model properties: ${JSON.stringify(props)}`); |
| 259 | 257 | return response.send(props); |
| 260 | 258 | } catch (error) { |
| 261 | 259 | console.error(error); |
| @@ -273,7 +271,7 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 273 | 271 | |
| 274 | 272 | const apiType = request.body.api_type; |
| 275 | 273 | const baseUrl = request.body.api_server; |
| 276 | 274 | console.logdebug(request.body); |
| 277 | 275 | |
| 278 | 276 | const controller = new AbortController(); |
| 279 | 277 | request.socket.removeAllListeners('close'); |
| @@ -399,7 +397,7 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 399 | 397 | if (completionsReply.ok) { |
| 400 | 398 | /** @type {any} */ |
| 401 | 399 | const data = await completionsReply.json(); |
| 402 | 400 | console.logdebug('Endpoint response:', data); |
| 403 | 401 | |
| 404 | 402 | // Map InfermaticAI response to OAI completions format |
| 405 | 403 | if (apiType === TEXTGEN_TYPES.INFERMATICAI) { |
| @@ -411,24 +409,20 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 411 | 409 | const text = await completionsReply.text(); |
| 412 | 410 | const errorBody = { error: true, status: completionsReply.status, response: text }; |
| 413 | 411 | |
| 414 | 412 | ifreturn (!response.headersSent) { |
| 415 | 413 | return? response.send(errorBody); |
| 416 | - } | |
| 414 | + : response.end(); | |
| 417 | - | |
| 418 | - return response.end(); | |
| 419 | 415 | } |
| 420 | 416 | } |
| 421 | 417 | } catch (error) { |
| 422 | 418 | const status = error?.status ?? error?.code ?? 'UNKNOWN'; |
| 423 | 419 | const text = error?.error ?? error?.statusText ?? error?.message ?? 'Unknown error on /generate endpoint'; |
| 424 | 420 | let value = { error: true, status: status, response: text }; |
| 425 | 421 | console.logerror('Endpoint error:', error); |
| 426 | - | |
| 427 | - if (!response.headersSent) { | |
| 428 | - return response.send(value); | |
| 429 | - } | |
| 430 | 422 | |
| 431 | 423 | return !response.end();headersSent |
| 424 | + ? response.send(value) | |
| 425 | + : response.end(); | |
| 432 | 426 | } |
| 433 | 427 | }); |
| 434 | 428 | |
| @@ -451,7 +445,7 @@ ollama.post('/download', jsonParser, async function (request, response) { | ||
| 451 | 445 | }); |
| 452 | 446 | |
| 453 | 447 | if (!fetchResponse.ok) { |
| 454 | 448 | console.logerror('Download error:', fetchResponse.status, fetchResponse.statusText); |
| 455 | 449 | return response.status(fetchResponse.status).send({ error: true }); |
| 456 | 450 | } |
| 457 | 451 | |
| @@ -468,7 +462,7 @@ ollama.post('/caption-image', jsonParser, async function (request, response) { | ||
| 468 | 462 | return response.sendStatus(400); |
| 469 | 463 | } |
| 470 | 464 | |
| 471 | 465 | console.logdebug('Ollama caption request:', request.body); |
| 472 | 466 | const baseUrl = trimV1(request.body.server_url); |
| 473 | 467 | |
| 474 | 468 | const fetchResponse = await fetch(`${baseUrl}/api/generate`, { |
| @@ -483,18 +477,18 @@ ollama.post('/caption-image', jsonParser, async function (request, response) { | ||
| 483 | 477 | }); |
| 484 | 478 | |
| 485 | 479 | if (!fetchResponse.ok) { |
| 486 | 480 | console.logerror('Ollama caption error:', fetchResponse.status, fetchResponse.statusText); |
| 487 | 481 | return response.status(500).send({ error: true }); |
| 488 | 482 | } |
| 489 | 483 | |
| 490 | 484 | /** @type {any} */ |
| 491 | 485 | const data = await fetchResponse.json(); |
| 492 | 486 | console.logdebug('Ollama caption response:', data); |
| 493 | 487 | |
| 494 | 488 | const caption = data?.response || ''; |
| 495 | 489 | |
| 496 | 490 | if (!caption) { |
| 497 | 491 | console.logerror('Ollama caption is empty.'); |
| 498 | 492 | return response.status(500).send({ error: true }); |
| 499 | 493 | } |
| 500 | 494 | |
| @@ -513,7 +507,7 @@ llamacpp.post('/caption-image', jsonParser, async function (request, response) { | ||
| 513 | 507 | return response.sendStatus(400); |
| 514 | 508 | } |
| 515 | 509 | |
| 516 | 510 | console.logdebug('LlamaCpp caption request:', request.body); |
| 517 | 511 | const baseUrl = trimV1(request.body.server_url); |
| 518 | 512 | |
| 519 | 513 | const fetchResponse = await fetch(`${baseUrl}/completion`, { |
| @@ -529,18 +523,18 @@ llamacpp.post('/caption-image', jsonParser, async function (request, response) { | ||
| 529 | 523 | }); |
| 530 | 524 | |
| 531 | 525 | if (!fetchResponse.ok) { |
| 532 | 526 | console.logerror('LlamaCpp caption error:', fetchResponse.status, fetchResponse.statusText); |
| 533 | 527 | return response.status(500).send({ error: true }); |
| 534 | 528 | } |
| 535 | 529 | |
| 536 | 530 | /** @type {any} */ |
| 537 | 531 | const data = await fetchResponse.json(); |
| 538 | 532 | console.logdebug('LlamaCpp caption response:', data); |
| 539 | 533 | |
| 540 | 534 | const caption = data?.content || ''; |
| 541 | 535 | |
| 542 | 536 | if (!caption) { |
| 543 | 537 | console.logerror('LlamaCpp caption is empty.'); |
| 544 | 538 | return response.status(500).send({ error: true }); |
| 545 | 539 | } |
| 546 | 540 | |
| @@ -558,7 +552,7 @@ llamacpp.post('/props', jsonParser, async function (request, response) { | ||
| 558 | 552 | return response.sendStatus(400); |
| 559 | 553 | } |
| 560 | 554 | |
| 561 | 555 | console.logdebug('LlamaCpp props request:', request.body); |
| 562 | 556 | const baseUrl = trimV1(request.body.server_url); |
| 563 | 557 | |
| 564 | 558 | const fetchResponse = await fetch(`${baseUrl}/props`, { |
| @@ -566,12 +560,12 @@ llamacpp.post('/props', jsonParser, async function (request, response) { | ||
| 566 | 560 | }); |
| 567 | 561 | |
| 568 | 562 | if (!fetchResponse.ok) { |
| 569 | 563 | console.logerror('LlamaCpp props error:', fetchResponse.status, fetchResponse.statusText); |
| 570 | 564 | return response.status(500).send({ error: true }); |
| 571 | 565 | } |
| 572 | 566 | |
| 573 | 567 | const data = await fetchResponse.json(); |
| 574 | 568 | console.logdebug('LlamaCpp props response:', data); |
| 575 | 569 | |
| 576 | 570 | return response.send(data); |
| 577 | 571 | |
| @@ -590,7 +584,7 @@ llamacpp.post('/slots', jsonParser, async function (request, response) { | ||
| 590 | 584 | return response.sendStatus(400); |
| 591 | 585 | } |
| 592 | 586 | |
| 593 | 587 | console.logdebug('LlamaCpp slots request:', request.body); |
| 594 | 588 | const baseUrl = trimV1(request.body.server_url); |
| 595 | 589 | |
| 596 | 590 | let fetchResponse; |
| @@ -616,12 +610,12 @@ llamacpp.post('/slots', jsonParser, async function (request, response) { | ||
| 616 | 610 | } |
| 617 | 611 | |
| 618 | 612 | if (!fetchResponse.ok) { |
| 619 | 613 | console.logerror('LlamaCpp slots error:', fetchResponse.status, fetchResponse.statusText); |
| 620 | 614 | return response.status(500).send({ error: true }); |
| 621 | 615 | } |
| 622 | 616 | |
| 623 | 617 | const data = await fetchResponse.json(); |
| 624 | 618 | console.logdebug('LlamaCpp slots response:', data); |
| 625 | 619 | |
| 626 | 620 | return response.send(data); |
| 627 | 621 | |
| @@ -659,14 +653,14 @@ tabby.post('/download', jsonParser, async function (request, response) { | ||
| 659 | 653 | return response.status(403).send({ error: true }); |
| 660 | 654 | } |
| 661 | 655 | } else { |
| 662 | 656 | console.logerror('API Permission error:', permissionResponse.status, permissionResponse.statusText); |
| 663 | 657 | return response.status(permissionResponse.status).send({ error: true }); |
| 664 | 658 | } |
| 665 | 659 | |
| 666 | 660 | const fetchResponse = await fetch(`${baseUrl}/v1/download`, args); |
| 667 | 661 | |
| 668 | 662 | if (!fetchResponse.ok) { |
| 669 | 663 | console.logerror('Download error:', fetchResponse.status, fetchResponse.statusText); |
| 670 | 664 | return response.status(fetchResponse.status).send({ error: true }); |
| 671 | 665 | } |
| 672 | 666 | |
| @@ -27,7 +27,7 @@ router.post('/delete', jsonParser, getFileNameValidationFunction('bg'), function | ||
| 27 | 27 | const fileName = path.join(request.user.directories.backgrounds, sanitize(request.body.bg)); |
| 28 | 28 | |
| 29 | 29 | if (!fs.existsSync(fileName)) { |
| 30 | 30 | console.logerror('BG file not found'); |
| 31 | 31 | return response.sendStatus(400); |
| 32 | 32 | } |
| 33 | 33 | |
| @@ -43,12 +43,12 @@ router.post('/rename', jsonParser, function (request, response) { | ||
| 43 | 43 | const newFileName = path.join(request.user.directories.backgrounds, sanitize(request.body.new_bg)); |
| 44 | 44 | |
| 45 | 45 | if (!fs.existsSync(oldFileName)) { |
| 46 | 46 | console.logerror('BG file not found'); |
| 47 | 47 | return response.sendStatus(400); |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | if (fs.existsSync(newFileName)) { |
| 51 | 51 | console.logerror('New BG file already exists'); |
| 52 | 52 | return response.sendStatus(400); |
| 53 | 53 | } |
| 54 | 54 | |
| @@ -2,10 +2,10 @@ import express from 'express'; | ||
| 2 | 2 | import { jsonParser } from '../express-common.js'; |
| 3 | 3 | import { getPipeline, getRawImage } from '../transformers.js'; |
| 4 | 4 | |
| 5 | -const TASK = 'image-to-text'; | |
| 6 | - | |
| 7 | 5 | export const router = express.Router(); |
| 8 | 6 | |
| 7 | +const TASK = 'image-to-text'; | |
| 8 | + | |
| 9 | 9 | router.post('/', jsonParser, async (req, res) => { |
| 10 | 10 | try { |
| 11 | 11 | const { image } = req.body; |
| @@ -13,14 +13,14 @@ router.post('/', jsonParser, async (req, res) => { | ||
| 13 | 13 | const rawImage = await getRawImage(image); |
| 14 | 14 | |
| 15 | 15 | if (!rawImage) { |
| 16 | 16 | console.logwarn('Failed to parse captioned image'); |
| 17 | 17 | return res.sendStatus(400); |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | const pipe = await getPipeline(TASK); |
| 21 | 21 | const result = await pipe(rawImage); |
| 22 | 22 | const text = result[0].generated_text; |
| 23 | 23 | console.loginfo('Image caption:', text); |
| 24 | 24 | |
| 25 | 25 | return res.json({ caption: text }); |
| 26 | 26 | } catch (error) { |
| @@ -97,7 +97,7 @@ async function writeCharacterData(inputFile, data, outputFile, request, crop = u | ||
| 97 | 97 | writeFileAtomicSync(outputImagePath, outputImage); |
| 98 | 98 | return true; |
| 99 | 99 | } catch (err) { |
| 100 | 100 | console.logerror(err); |
| 101 | 101 | return false; |
| 102 | 102 | } |
| 103 | 103 | } |
| @@ -166,7 +166,7 @@ async function tryReadImage(imgPath, crop) { | ||
| 166 | 166 | } |
| 167 | 167 | // If it's an unsupported type of image (APNG) - just read the file as buffer |
| 168 | 168 | catch (error) { |
| 169 | 169 | console.logerror(`Failed to read image: ${imgPath}`, error); |
| 170 | 170 | return fs.readFileSync(imgPath); |
| 171 | 171 | } |
| 172 | 172 | } |
| @@ -229,12 +229,12 @@ const processCharacter = async (item, directories) => { | ||
| 229 | 229 | return character; |
| 230 | 230 | } |
| 231 | 231 | catch (err) { |
| 232 | 232 | console.logerror(`Could not process character: ${item}`); |
| 233 | 233 | |
| 234 | 234 | if (err instanceof SyntaxError) { |
| 235 | 235 | console.logerror(`${item} does not contain a valid JSON object.`); |
| 236 | 236 | } else { |
| 237 | 237 | console.logerror('An unexpected error occurred: ', err); |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | return { |
| @@ -322,7 +322,7 @@ function readFromV2(char) { | ||
| 322 | 322 | }; |
| 323 | 323 | |
| 324 | 324 | _.forEach(fieldMappings, (v2Path, charField) => { |
| 325 | 325 | //console.loginfo(`Migrating field: ${charField} from ${v2Path}`); |
| 326 | 326 | const v2Value = _.get(char.data, v2Path); |
| 327 | 327 | if (_.isUndefined(v2Value)) { |
| 328 | 328 | let defaultValue = undefined; |
| @@ -337,15 +337,15 @@ function readFromV2(char) { | ||
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | if (!_.isUndefined(defaultValue)) { |
| 340 | 340 | //console.debugwarn(`Spec v2 extension data missing for field: ${charField}, using default value: ${defaultValue}`); |
| 341 | 341 | char[charField] = defaultValue; |
| 342 | 342 | } else { |
| 343 | 343 | console.debugwarn(`Char ${char['name']} has Spec v2 data missing for unknown field: ${charField}`); |
| 344 | 344 | return; |
| 345 | 345 | } |
| 346 | 346 | } |
| 347 | 347 | if (!_.isUndefined(char[charField]) && !_.isUndefined(v2Value) && String(char[charField]) !== String(v2Value)) { |
| 348 | 348 | console.debugwarn(`Char ${char['name']} has Spec v2 data mismatch with Spec v1 for field: ${charField}`, char[charField], v2Value); |
| 349 | 349 | } |
| 350 | 350 | char[charField] = v2Value; |
| 351 | 351 | }); |
| @@ -442,7 +442,7 @@ function charaFormatData(data, directories) { | ||
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | } catch { |
| 445 | 445 | console.debugwarn(`Failed to read world info file: ${data.world}. Character book will not be available.`); |
| 446 | 446 | } |
| 447 | 447 | } |
| 448 | 448 | |
| @@ -452,7 +452,7 @@ function charaFormatData(data, directories) { | ||
| 452 | 452 | // Deep merge the extensions object |
| 453 | 453 | _.set(char, 'data.extensions', deepMerge(char.data.extensions, extensions)); |
| 454 | 454 | } catch { |
| 455 | 455 | console.debugwarn(`Failed to parse extensions JSON: ${data.extensions}`); |
| 456 | 456 | } |
| 457 | 457 | } |
| 458 | 458 | |
| @@ -526,7 +526,7 @@ async function importFromYaml(uploadPath, context, preservedFileName) { | ||
| 526 | 526 | const fileText = fs.readFileSync(uploadPath, 'utf8'); |
| 527 | 527 | fs.rmSync(uploadPath); |
| 528 | 528 | const yamlData = yaml.parse(fileText); |
| 529 | 529 | console.loginfo('Importing from YAML'); |
| 530 | 530 | yamlData.name = sanitize(yamlData.name); |
| 531 | 531 | const fileName = preservedFileName || getPngName(yamlData.name, context.request.user.directories); |
| 532 | 532 | let char = convertToV2({ |
| @@ -559,7 +559,7 @@ async function importFromYaml(uploadPath, context, preservedFileName) { | ||
| 559 | 559 | async function importFromCharX(uploadPath, { request }, preservedFileName) { |
| 560 | 560 | const data = fs.readFileSync(uploadPath).buffer; |
| 561 | 561 | fs.rmSync(uploadPath); |
| 562 | 562 | console.loginfo('Importing from CharX'); |
| 563 | 563 | const cardBuffer = await extractFileFromZipBuffer(data, 'card.json'); |
| 564 | 564 | |
| 565 | 565 | if (!cardBuffer) { |
| @@ -608,7 +608,7 @@ async function importFromJson(uploadPath, { request }, preservedFileName) { | ||
| 608 | 608 | let jsonData = JSON.parse(data); |
| 609 | 609 | |
| 610 | 610 | if (jsonData.spec !== undefined) { |
| 611 | 611 | console.loginfo(`Importing from ${jsonData.spec} json`); |
| 612 | 612 | importRisuSprites(request.user.directories, jsonData); |
| 613 | 613 | unsetFavFlag(jsonData); |
| 614 | 614 | jsonData = readFromV2(jsonData); |
| @@ -618,7 +618,7 @@ async function importFromJson(uploadPath, { request }, preservedFileName) { | ||
| 618 | 618 | const result = await writeCharacterData(defaultAvatarPath, char, pngName, request); |
| 619 | 619 | return result ? pngName : ''; |
| 620 | 620 | } else if (jsonData.name !== undefined) { |
| 621 | 621 | console.loginfo('Importing from v1 json'); |
| 622 | 622 | jsonData.name = sanitize(jsonData.name); |
| 623 | 623 | if (jsonData.creator_notes) { |
| 624 | 624 | jsonData.creator_notes = jsonData.creator_notes.replace('Creator\'s notes go here.', ''); |
| @@ -644,7 +644,7 @@ async function importFromJson(uploadPath, { request }, preservedFileName) { | ||
| 644 | 644 | const result = await writeCharacterData(defaultAvatarPath, charJSON, pngName, request); |
| 645 | 645 | return result ? pngName : ''; |
| 646 | 646 | } else if (jsonData.char_name !== undefined) {//json Pygmalion notepad |
| 647 | 647 | console.loginfo('Importing from gradio json'); |
| 648 | 648 | jsonData.char_name = sanitize(jsonData.char_name); |
| 649 | 649 | if (jsonData.creator_notes) { |
| 650 | 650 | jsonData.creator_notes = jsonData.creator_notes.replace('Creator\'s notes go here.', ''); |
| @@ -691,7 +691,7 @@ async function importFromPng(uploadPath, { request }, preservedFileName) { | ||
| 691 | 691 | const pngName = preservedFileName || getPngName(jsonData.name, request.user.directories); |
| 692 | 692 | |
| 693 | 693 | if (jsonData.spec !== undefined) { |
| 694 | 694 | console.loginfo(`Found a ${jsonData.spec} character file.`); |
| 695 | 695 | importRisuSprites(request.user.directories, jsonData); |
| 696 | 696 | unsetFavFlag(jsonData); |
| 697 | 697 | jsonData = readFromV2(jsonData); |
| @@ -701,7 +701,7 @@ async function importFromPng(uploadPath, { request }, preservedFileName) { | ||
| 701 | 701 | fs.unlinkSync(uploadPath); |
| 702 | 702 | return result ? pngName : ''; |
| 703 | 703 | } else if (jsonData.name !== undefined) { |
| 704 | 704 | console.loginfo('Found a v1 character file.'); |
| 705 | 705 | |
| 706 | 706 | if (jsonData.creator_notes) { |
| 707 | 707 | jsonData.creator_notes = jsonData.creator_notes.replace('Creator\'s notes go here.', ''); |
| @@ -812,13 +812,13 @@ router.post('/rename', jsonParser, validateAvatarUrlMiddleware, async function ( | ||
| 812 | 812 | |
| 813 | 813 | router.post('/edit', urlencodedParser, validateAvatarUrlMiddleware, async function (request, response) { |
| 814 | 814 | if (!request.body) { |
| 815 | 815 | console.errorwarn('Error: no response body detected'); |
| 816 | 816 | response.status(400).send('Error: no response body detected'); |
| 817 | 817 | return; |
| 818 | 818 | } |
| 819 | 819 | |
| 820 | 820 | if (request.body.ch_name === '' || request.body.ch_name === undefined || request.body.ch_name === '.') { |
| 821 | 821 | console.errorwarn('Error: invalid name.'); |
| 822 | 822 | response.status(400).send('Error: invalid name.'); |
| 823 | 823 | return; |
| 824 | 824 | } |
| @@ -860,14 +860,14 @@ router.post('/edit', urlencodedParser, validateAvatarUrlMiddleware, async functi | ||
| 860 | 860 | * @returns {void} |
| 861 | 861 | */ |
| 862 | 862 | router.post('/edit-attribute', jsonParser, validateAvatarUrlMiddleware, async function (request, response) { |
| 863 | 863 | console.logdebug(request.body); |
| 864 | 864 | if (!request.body) { |
| 865 | 865 | console.errorwarn('Error: no response body detected'); |
| 866 | 866 | return response.status(400).send('Error: no response body detected'); |
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | if (request.body.ch_name === '' || request.body.ch_name === undefined || request.body.ch_name === '.') { |
| 870 | 870 | console.errorwarn('Error: invalid name.'); |
| 871 | 871 | return response.status(400).send('Error: invalid name.'); |
| 872 | 872 | } |
| 873 | 873 | |
| @@ -879,7 +879,7 @@ router.post('/edit-attribute', jsonParser, validateAvatarUrlMiddleware, async fu | ||
| 879 | 879 | const char = JSON.parse(charJSON); |
| 880 | 880 | //check if the field exists |
| 881 | 881 | if (char[request.body.field] === undefined && char.data[request.body.field] === undefined) { |
| 882 | 882 | console.errorwarn('Error: invalid field.'); |
| 883 | 883 | response.status(400).send('Error: invalid field.'); |
| 884 | 884 | return; |
| 885 | 885 | } |
| @@ -928,7 +928,7 @@ router.post('/merge-attributes', jsonParser, getFileNameValidationFunction('avat | ||
| 928 | 928 | await writeCharacterData(avatarPath, JSON.stringify(character), targetImg, request); |
| 929 | 929 | response.sendStatus(200); |
| 930 | 930 | } else { |
| 931 | 931 | console.logwarn(validator.lastValidationError); |
| 932 | 932 | response.status(400).send({ message: `Validation failed for ${character.name}`, error: validator.lastValidationError }); |
| 933 | 933 | } |
| 934 | 934 | } catch (exception) { |
| @@ -1050,7 +1050,7 @@ router.post('/chats', jsonParser, validateAvatarUrlMiddleware, async function (r | ||
| 1050 | 1050 | const fileSizeInKB = `${(stats.size / 1024).toFixed(2)}kb`; |
| 1051 | 1051 | |
| 1052 | 1052 | if (stats.size === 0) { |
| 1053 | 1053 | console.logwarn(`Found an empty chat file: ${pathToFile}`); |
| 1054 | 1054 | res({}); |
| 1055 | 1055 | return; |
| 1056 | 1056 | } |
| @@ -1082,7 +1082,7 @@ router.post('/chats', jsonParser, validateAvatarUrlMiddleware, async function (r | ||
| 1082 | 1082 | |
| 1083 | 1083 | res(chatData); |
| 1084 | 1084 | } else { |
| 1085 | 1085 | console.logwarn('Found an invalid or corrupted chat file:', pathToFile); |
| 1086 | 1086 | res({}); |
| 1087 | 1087 | } |
| 1088 | 1088 | } |
| @@ -1095,7 +1095,7 @@ router.post('/chats', jsonParser, validateAvatarUrlMiddleware, async function (r | ||
| 1095 | 1095 | |
| 1096 | 1096 | return response.send(validFiles); |
| 1097 | 1097 | } catch (error) { |
| 1098 | 1098 | console.logerror(error); |
| 1099 | 1099 | return response.send({ error: true }); |
| 1100 | 1100 | } |
| 1101 | 1101 | }); |
| @@ -1152,7 +1152,7 @@ router.post('/import', urlencodedParser, async function (request, response) { | ||
| 1152 | 1152 | const fileName = await importFunction(uploadPath, { request, response }, preservedFileName); |
| 1153 | 1153 | |
| 1154 | 1154 | if (!fileName) { |
| 1155 | 1155 | console.errorwarn('Failed to import character'); |
| 1156 | 1156 | return response.sendStatus(400); |
| 1157 | 1157 | } |
| 1158 | 1158 | |
| @@ -1162,7 +1162,7 @@ router.post('/import', urlencodedParser, async function (request, response) { | ||
| 1162 | 1162 | |
| 1163 | 1163 | response.send({ file_name: fileName }); |
| 1164 | 1164 | } catch (err) { |
| 1165 | 1165 | console.logerror(err); |
| 1166 | 1166 | response.send({ error: true }); |
| 1167 | 1167 | } |
| 1168 | 1168 | }); |
| @@ -1170,14 +1170,13 @@ router.post('/import', urlencodedParser, async function (request, response) { | ||
| 1170 | 1170 | router.post('/duplicate', jsonParser, validateAvatarUrlMiddleware, async function (request, response) { |
| 1171 | 1171 | try { |
| 1172 | 1172 | if (!request.body.avatar_url) { |
| 1173 | 1173 | console.logwarn('avatar URL not found in request body'); |
| 1174 | 1174 | console.logdebug(request.body); |
| 1175 | 1175 | return response.sendStatus(400); |
| 1176 | 1176 | } |
| 1177 | 1177 | let filename = path.join(request.user.directories.characters, sanitize(request.body.avatar_url)); |
| 1178 | 1178 | if (!fs.existsSync(filename)) { |
| 1179 | 1179 | console.logerror('file for dupe not found', filename); |
| 1180 | - console.log(filename); | |
| 1181 | 1180 | return response.sendStatus(404); |
| 1182 | 1181 | } |
| 1183 | 1182 | let suffix = 1; |
| @@ -1205,7 +1204,7 @@ router.post('/duplicate', jsonParser, validateAvatarUrlMiddleware, async functio | ||
| 1205 | 1204 | } |
| 1206 | 1205 | |
| 1207 | 1206 | fs.copyFileSync(filename, newFilename); |
| 1208 | 1207 | console.loginfo(`${filename} was copied to ${newFilename}`); |
| 1209 | 1208 | response.send({ path: path.parse(newFilename).base }); |
| 1210 | 1209 | } |
| 1211 | 1210 | catch (error) { |
| @@ -50,7 +50,7 @@ function backupChat(directory, name, chat) { | ||
| 50 | 50 | |
| 51 | 51 | removeOldBackups(directory, 'chat_', maxTotalChatBackups); |
| 52 | 52 | } catch (err) { |
| 53 | 53 | console.logerror(`Could not backup chat for ${name}`, err); |
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | 56 | |
| @@ -306,8 +306,8 @@ router.post('/save', jsonParser, validateAvatarUrlMiddleware, function (request, | ||
| 306 | 306 | getBackupFunction(request.user.profile.handle)(request.user.directories.backups, directoryName, jsonlData); |
| 307 | 307 | return response.send({ result: 'ok' }); |
| 308 | 308 | } catch (error) { |
| 309 | 309 | responseconsole.senderror(error); |
| 310 | 310 | return consoleresponse.logsend(error); |
| 311 | 311 | } |
| 312 | 312 | }); |
| 313 | 313 | |
| @@ -359,17 +359,17 @@ router.post('/rename', jsonParser, validateAvatarUrlMiddleware, async function ( | ||
| 359 | 359 | const pathToOriginalFile = path.join(pathToFolder, sanitize(request.body.original_file)); |
| 360 | 360 | const pathToRenamedFile = path.join(pathToFolder, sanitize(request.body.renamed_file)); |
| 361 | 361 | const sanitizedFileName = path.parse(pathToRenamedFile).name; |
| 362 | 362 | console.loginfo('Old chat name', pathToOriginalFile); |
| 363 | 363 | console.loginfo('New chat name', pathToRenamedFile); |
| 364 | 364 | |
| 365 | 365 | if (!fs.existsSync(pathToOriginalFile) || fs.existsSync(pathToRenamedFile)) { |
| 366 | 366 | console.logerror('Either Source or Destination files are not available'); |
| 367 | 367 | return response.status(400).send({ error: true }); |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | fs.copyFileSync(pathToOriginalFile, pathToRenamedFile); |
| 371 | 371 | fs.rmSync(pathToOriginalFile); |
| 372 | 372 | console.loginfo('Successfully renamed.'); |
| 373 | 373 | return response.send({ ok: true, sanitizedFileName }); |
| 374 | 374 | }); |
| 375 | 375 | |
| @@ -380,12 +380,12 @@ router.post('/delete', jsonParser, validateAvatarUrlMiddleware, function (reques | ||
| 380 | 380 | const chatFileExists = fs.existsSync(filePath); |
| 381 | 381 | |
| 382 | 382 | if (!chatFileExists) { |
| 383 | 383 | console.logerror(`Chat file not found '${filePath}'`); |
| 384 | 384 | return response.sendStatus(400); |
| 385 | 385 | } |
| 386 | 386 | |
| 387 | 387 | fs.rmSync(filePath); |
| 388 | 388 | console.loginfo('`Deleted chat file: ' + ${filePath}`); |
| 389 | 389 | return response.send('ok'); |
| 390 | 390 | }); |
| 391 | 391 | |
| @@ -402,7 +402,7 @@ router.post('/export', jsonParser, validateAvatarUrlMiddleware, async function ( | ||
| 402 | 402 | const errorMessage = { |
| 403 | 403 | message: `Could not find JSONL file to export. Source chat file: ${filename}.`, |
| 404 | 404 | }; |
| 405 | 405 | console.logerror(errorMessage.message); |
| 406 | 406 | return response.status(404).json(errorMessage); |
| 407 | 407 | } |
| 408 | 408 | try { |
| @@ -415,14 +415,14 @@ router.post('/export', jsonParser, validateAvatarUrlMiddleware, async function ( | ||
| 415 | 415 | result: rawFile, |
| 416 | 416 | }; |
| 417 | 417 | |
| 418 | 418 | console.loginfo(`Chat exported as ${exportfilename}`); |
| 419 | 419 | return response.status(200).json(successMessage); |
| 420 | 420 | } catch (err) { |
| 421 | 421 | console.error(err); |
| 422 | 422 | const errorMessage = { |
| 423 | 423 | message: `Could not read JSONL file to export. Source chat file: ${filename}.`, |
| 424 | 424 | }; |
| 425 | 425 | console.logerror(errorMessage.message); |
| 426 | 426 | return response.status(500).json(errorMessage); |
| 427 | 427 | } |
| 428 | 428 | } |
| @@ -449,12 +449,11 @@ router.post('/export', jsonParser, validateAvatarUrlMiddleware, async function ( | ||
| 449 | 449 | message: `Chat saved to ${exportfilename}`, |
| 450 | 450 | result: buffer, |
| 451 | 451 | }; |
| 452 | 452 | console.loginfo(`Chat exported as ${exportfilename}`); |
| 453 | 453 | return response.status(200).json(successMessage); |
| 454 | 454 | }); |
| 455 | 455 | } catch (err) { |
| 456 | 456 | console.logerror('chat export failed.', err); |
| 457 | - console.log(err); | |
| 458 | 457 | return response.sendStatus(400); |
| 459 | 458 | } |
| 460 | 459 | }); |
| @@ -513,7 +512,7 @@ router.post('/import', urlencodedParser, validateAvatarUrlMiddleware, function ( | ||
| 513 | 512 | } else if (jsonData.type === 'risuChat') { // RisuAI format |
| 514 | 513 | importFunc = importRisuChat; |
| 515 | 514 | } else { // Unknown format |
| 516 | 515 | console.logerror('Incorrect chat format .json'); |
| 517 | 516 | return response.send({ error: true }); |
| 518 | 517 | } |
| 519 | 518 | |
| @@ -541,7 +540,7 @@ router.post('/import', urlencodedParser, validateAvatarUrlMiddleware, function ( | ||
| 541 | 540 | const jsonData = JSON.parse(header); |
| 542 | 541 | |
| 543 | 542 | if (!(jsonData.user_name !== undefined || jsonData.name !== undefined)) { |
| 544 | 543 | console.logerror('Incorrect chat format .jsonl'); |
| 545 | 544 | return response.send({ error: true }); |
| 546 | 545 | } |
| 547 | 546 | |
| @@ -647,7 +646,7 @@ router.post('/search', jsonParser, validateAvatarUrlMiddleware, function (reques | ||
| 647 | 646 | break; |
| 648 | 647 | } |
| 649 | 648 | } catch (error) { |
| 650 | 649 | console.errorwarn(groupFile, 'group file is corrupted:', error); |
| 651 | 650 | } |
| 652 | 651 | } |
| 653 | 652 | |
| @@ -44,9 +44,9 @@ router.post('/', jsonParser, async (req, res) => { | ||
| 44 | 44 | } |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | console.logdebug('Classify input:', text); |
| 48 | 48 | const result = await getResult(text); |
| 49 | 49 | console.logdebug('Classify output:', result); |
| 50 | 50 | |
| 51 | 51 | return res.json({ classification: result }); |
| 52 | 52 | } catch (error) { |
| @@ -71,7 +71,7 @@ export function getDefaultPresets(directories) { | ||
| 71 | 71 | |
| 72 | 72 | return presets; |
| 73 | 73 | } catch (err) { |
| 74 | 74 | console.logwarn('Failed to get default presets', err); |
| 75 | 75 | return []; |
| 76 | 76 | } |
| 77 | 77 | } |
| @@ -92,7 +92,7 @@ export function getDefaultPresetFile(filename) { | ||
| 92 | 92 | const fileContent = fs.readFileSync(contentPath, 'utf8'); |
| 93 | 93 | return JSON.parse(fileContent); |
| 94 | 94 | } catch (err) { |
| 95 | 95 | console.logwarn(`Failed to get default file ${filename}`, err); |
| 96 | 96 | return null; |
| 97 | 97 | } |
| 98 | 98 | } |
| @@ -121,21 +121,21 @@ async function seedContentForUser(contentIndex, directories, forceCategories) { | ||
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | if (!contentItem.folder) { |
| 124 | 124 | console.logwarn(`Content file ${contentItem.filename} has no parent folder`); |
| 125 | 125 | continue; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | const contentPath = path.join(contentItem.folder, contentItem.filename); |
| 129 | 129 | |
| 130 | 130 | if (!fs.existsSync(contentPath)) { |
| 131 | 131 | console.logwarn(`Content file ${contentItem.filename} is missing`); |
| 132 | 132 | continue; |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | const contentTarget = getTargetByType(contentItem.type, directories); |
| 136 | 136 | |
| 137 | 137 | if (!contentTarget) { |
| 138 | 138 | console.logwarn(`Content file ${contentItem.filename} has unknown type ${contentItem.type}`); |
| 139 | 139 | continue; |
| 140 | 140 | } |
| 141 | 141 | |
| @@ -144,12 +144,12 @@ async function seedContentForUser(contentIndex, directories, forceCategories) { | ||
| 144 | 144 | contentLog.push(contentItem.filename); |
| 145 | 145 | |
| 146 | 146 | if (fs.existsSync(targetPath)) { |
| 147 | 147 | console.logwarn(`Content file ${contentItem.filename} already exists in ${contentTarget}`); |
| 148 | 148 | continue; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | fs.cpSync(contentPath, targetPath, { recursive: true, force: false }); |
| 152 | 152 | console.loginfo(`Content file ${contentItem.filename} copied to ${contentTarget}`); |
| 153 | 153 | anyContentAdded = true; |
| 154 | 154 | } |
| 155 | 155 | |
| @@ -182,12 +182,12 @@ export async function checkForNewContent(directoriesList, forceCategories = []) | ||
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | if (anyContentAdded && !contentCheckSkip && forceCategories?.length === 0) { |
| 185 | 185 | console.loginfo(); |
| 186 | 186 | console.loginfo(`${color.blue('If you don\'t want to receive content updates in the future, set')} ${color.yellow('skipContentCheck')} ${color.blue('to true in the config.yaml file.')}`); |
| 187 | 187 | console.loginfo(); |
| 188 | 188 | } |
| 189 | 189 | } catch (err) { |
| 190 | 190 | console.logerror('Content check failed', err); |
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | 193 | |
| @@ -331,7 +331,7 @@ async function downloadChubLorebook(id) { | ||
| 331 | 331 | |
| 332 | 332 | if (!result.ok) { |
| 333 | 333 | const text = await result.text(); |
| 334 | 334 | console.logerror('Chub returned error', result.statusText, text); |
| 335 | 335 | throw new Error('Failed to download lorebook'); |
| 336 | 336 | } |
| 337 | 337 | |
| @@ -355,7 +355,7 @@ async function downloadChubCharacter(id) { | ||
| 355 | 355 | |
| 356 | 356 | if (!result.ok) { |
| 357 | 357 | const text = await result.text(); |
| 358 | 358 | console.logerror('Chub returned error', result.statusText, text); |
| 359 | 359 | throw new Error('Failed to download character'); |
| 360 | 360 | } |
| 361 | 361 | |
| @@ -376,7 +376,7 @@ async function downloadPygmalionCharacter(id) { | ||
| 376 | 376 | |
| 377 | 377 | if (!result.ok) { |
| 378 | 378 | const text = await result.text(); |
| 379 | 379 | console.logerror('Pygsite returned error', result.status, text); |
| 380 | 380 | throw new Error('Failed to download character'); |
| 381 | 381 | } |
| 382 | 382 | |
| @@ -485,7 +485,7 @@ async function downloadJannyCharacter(uuid) { | ||
| 485 | 485 | } |
| 486 | 486 | } |
| 487 | 487 | |
| 488 | 488 | console.logerror('Janny returned error', result.statusText, await result.text()); |
| 489 | 489 | throw new Error('Failed to download character'); |
| 490 | 490 | } |
| 491 | 491 | |
| @@ -577,7 +577,7 @@ async function downloadRisuCharacter(uuid) { | ||
| 577 | 577 | |
| 578 | 578 | if (!result.ok) { |
| 579 | 579 | const text = await result.text(); |
| 580 | 580 | console.logerror('RisuAI returned error', result.statusText, text); |
| 581 | 581 | throw new Error('Failed to download character'); |
| 582 | 582 | } |
| 583 | 583 | |
| @@ -673,11 +673,11 @@ router.post('/importURL', jsonParser, async (request, response) => { | ||
| 673 | 673 | type = chubParsed?.type; |
| 674 | 674 | |
| 675 | 675 | if (chubParsed?.type === 'character') { |
| 676 | 676 | console.loginfo('Downloading chub character:', chubParsed.id); |
| 677 | 677 | result = await downloadChubCharacter(chubParsed.id); |
| 678 | 678 | } |
| 679 | 679 | else if (chubParsed?.type === 'lorebook') { |
| 680 | 680 | console.loginfo('Downloading chub lorebook:', chubParsed.id); |
| 681 | 681 | result = await downloadChubLorebook(chubParsed.id); |
| 682 | 682 | } |
| 683 | 683 | else { |
| @@ -692,7 +692,7 @@ router.post('/importURL', jsonParser, async (request, response) => { | ||
| 692 | 692 | type = 'character'; |
| 693 | 693 | result = await downloadRisuCharacter(uuid); |
| 694 | 694 | } else if (isGeneric) { |
| 695 | 695 | console.loginfo('Downloading from generic url.'); |
| 696 | 696 | type = 'character'; |
| 697 | 697 | result = await downloadGenericPng(url); |
| 698 | 698 | } else { |
| @@ -708,7 +708,7 @@ router.post('/importURL', jsonParser, async (request, response) => { | ||
| 708 | 708 | response.set('X-Custom-Content-Type', type); |
| 709 | 709 | return response.send(result.buffer); |
| 710 | 710 | } catch (error) { |
| 711 | 711 | console.logerror('Importing custom content failed', error); |
| 712 | 712 | return response.sendStatus(500); |
| 713 | 713 | } |
| 714 | 714 | }); |
| @@ -728,22 +728,22 @@ router.post('/importUUID', jsonParser, async (request, response) => { | ||
| 728 | 728 | const uuidType = uuid.includes('lorebook') ? 'lorebook' : 'character'; |
| 729 | 729 | |
| 730 | 730 | if (isPygmalion) { |
| 731 | 731 | console.loginfo('Downloading Pygmalion character:', uuid); |
| 732 | 732 | result = await downloadPygmalionCharacter(uuid); |
| 733 | 733 | } else if (isJannny) { |
| 734 | 734 | console.loginfo('Downloading Janitor character:', uuid.split('_')[0]); |
| 735 | 735 | result = await downloadJannyCharacter(uuid.split('_')[0]); |
| 736 | 736 | } else if (isAICC) { |
| 737 | 737 | const [, author, card] = uuid.split('/'); |
| 738 | 738 | console.loginfo('Downloading AICC character:', `${author}/${card}`); |
| 739 | 739 | result = await downloadAICCCharacter(`${author}/${card}`); |
| 740 | 740 | } else { |
| 741 | 741 | if (uuidType === 'character') { |
| 742 | 742 | console.loginfo('Downloading chub character:', uuid); |
| 743 | 743 | result = await downloadChubCharacter(uuid); |
| 744 | 744 | } |
| 745 | 745 | else if (uuidType === 'lorebook') { |
| 746 | 746 | console.loginfo('Downloading chub lorebook:', uuid); |
| 747 | 747 | result = await downloadChubLorebook(uuid); |
| 748 | 748 | } |
| 749 | 749 | else { |
| @@ -756,7 +756,7 @@ router.post('/importUUID', jsonParser, async (request, response) => { | ||
| 756 | 756 | response.set('X-Custom-Content-Type', uuidType); |
| 757 | 757 | return response.send(result.buffer); |
| 758 | 758 | } catch (error) { |
| 759 | 759 | console.logerror('Importing custom content failed', error); |
| 760 | 760 | return response.sendStatus(500); |
| 761 | 761 | } |
| 762 | 762 | }); |
| @@ -80,7 +80,7 @@ router.post('/install', jsonParser, async (request, response) => { | ||
| 80 | 80 | const { url, global } = request.body; |
| 81 | 81 | |
| 82 | 82 | if (global && !request.user.profile.admin) { |
| 83 | 83 | console.warnerror(`User ${request.user.profile.handle} does not have permission to install global extensions.`); |
| 84 | 84 | return response.status(403).send('Forbidden: No permission to install global extensions.'); |
| 85 | 85 | } |
| 86 | 86 | |
| @@ -92,13 +92,13 @@ router.post('/install', jsonParser, async (request, response) => { | ||
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | await git.clone(url, extensionPath, { '--depth': 1 }); |
| 95 | 95 | console.loginfo(`Extension has been cloned at ${extensionPath}`); |
| 96 | 96 | |
| 97 | 97 | const { version, author, display_name } = await getManifest(extensionPath); |
| 98 | 98 | |
| 99 | 99 | return response.send({ version, author, display_name, extensionPath }); |
| 100 | 100 | } catch (error) { |
| 101 | 101 | console.logerror('Importing custom content failed', error); |
| 102 | 102 | return response.status(500).send(`Server Error: ${error.message}`); |
| 103 | 103 | } |
| 104 | 104 | }); |
| @@ -124,7 +124,7 @@ router.post('/update', jsonParser, async (request, response) => { | ||
| 124 | 124 | const { extensionName, global } = request.body; |
| 125 | 125 | |
| 126 | 126 | if (global && !request.user.profile.admin) { |
| 127 | 127 | console.warnerror(`User ${request.user.profile.handle} does not have permission to update global extensions.`); |
| 128 | 128 | return response.status(403).send('Forbidden: No permission to update global extensions.'); |
| 129 | 129 | } |
| 130 | 130 | |
| @@ -139,9 +139,9 @@ router.post('/update', jsonParser, async (request, response) => { | ||
| 139 | 139 | const currentBranch = await git.cwd(extensionPath).branch(); |
| 140 | 140 | if (!isUpToDate) { |
| 141 | 141 | await git.cwd(extensionPath).pull('origin', currentBranch.current); |
| 142 | 142 | console.loginfo(`Extension has been updated at ${extensionPath}`); |
| 143 | 143 | } else { |
| 144 | 144 | console.loginfo(`Extension is up to date at ${extensionPath}`); |
| 145 | 145 | } |
| 146 | 146 | await git.cwd(extensionPath).fetch('origin'); |
| 147 | 147 | const fullCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); |
| @@ -150,7 +150,7 @@ router.post('/update', jsonParser, async (request, response) => { | ||
| 150 | 150 | return response.send({ shortCommitHash, extensionPath, isUpToDate, remoteUrl }); |
| 151 | 151 | |
| 152 | 152 | } catch (error) { |
| 153 | 153 | console.logerror('Updating custom content failed', error); |
| 154 | 154 | return response.status(500).send(`Server Error: ${error.message}`); |
| 155 | 155 | } |
| 156 | 156 | }); |
| @@ -164,7 +164,7 @@ router.post('/move', jsonParser, async (request, response) => { | ||
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | if (!request.user.profile.admin) { |
| 167 | 167 | console.warnerror(`User ${request.user.profile.handle} does not have permission to move extensions.`); |
| 168 | 168 | return response.status(403).send('Forbidden: No permission to move extensions.'); |
| 169 | 169 | } |
| 170 | 170 | |
| @@ -190,11 +190,11 @@ router.post('/move', jsonParser, async (request, response) => { | ||
| 190 | 190 | |
| 191 | 191 | fs.cpSync(sourcePath, destinationPath, { recursive: true, force: true }); |
| 192 | 192 | fs.rmSync(sourcePath, { recursive: true, force: true }); |
| 193 | 193 | console.loginfo(`Extension has been moved from ${sourcePath} to ${destinationPath}`); |
| 194 | 194 | |
| 195 | 195 | return response.sendStatus(204); |
| 196 | 196 | } catch (error) { |
| 197 | 197 | console.logerror('Moving extension failed', error); |
| 198 | 198 | return response.status(500).send('Internal Server Error. Try again later.'); |
| 199 | 199 | } |
| 200 | 200 | }); |
| @@ -237,13 +237,13 @@ router.post('/version', jsonParser, async (request, response) => { | ||
| 237 | 237 | // get only the working branch |
| 238 | 238 | const currentBranchName = currentBranch.current; |
| 239 | 239 | await git.cwd(extensionPath).fetch('origin'); |
| 240 | 240 | console.logdebug(extensionName, currentBranchName, currentCommitHash); |
| 241 | 241 | const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); |
| 242 | 242 | |
| 243 | 243 | return response.send({ currentBranchName, currentCommitHash, isUpToDate, remoteUrl }); |
| 244 | 244 | |
| 245 | 245 | } catch (error) { |
| 246 | 246 | console.logerror('Getting extension version failed', error); |
| 247 | 247 | return response.status(500).send(`Server Error: ${error.message}`); |
| 248 | 248 | } |
| 249 | 249 | }); |
| @@ -265,7 +265,7 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 265 | 265 | const { extensionName, global } = request.body; |
| 266 | 266 | |
| 267 | 267 | if (global && !request.user.profile.admin) { |
| 268 | 268 | console.warnerror(`User ${request.user.profile.handle} does not have permission to delete global extensions.`); |
| 269 | 269 | return response.status(403).send('Forbidden: No permission to delete global extensions.'); |
| 270 | 270 | } |
| 271 | 271 | |
| @@ -277,12 +277,12 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | await fs.promises.rm(extensionPath, { recursive: true }); |
| 280 | 280 | console.loginfo(`Extension has been deleted at ${extensionPath}`); |
| 281 | 281 | |
| 282 | 282 | return response.send(`Extension has been deleted at ${extensionPath}`); |
| 283 | 283 | |
| 284 | 284 | } catch (error) { |
| 285 | 285 | console.logerror('Deleting custom content failed', error); |
| 286 | 286 | return response.status(500).send(`Server Error: ${error.message}`); |
| 287 | 287 | } |
| 288 | 288 | }); |
| @@ -323,7 +323,7 @@ router.get('/discover', jsonParser, function (request, response) { | ||
| 323 | 323 | |
| 324 | 324 | // Combine all extensions |
| 325 | 325 | const allExtensions = [...builtInExtensions, ...userExtensions, ...globalExtensions]; |
| 326 | 326 | console.loginfo('Extensions available for', request.user.profile.handle, allExtensions); |
| 327 | 327 | |
| 328 | 328 | return response.send(allExtensions); |
| 329 | 329 | }); |
| @@ -21,7 +21,7 @@ router.post('/sanitize-filename', jsonParser, async (request, response) => { | ||
| 21 | 21 | const sanitizedFilename = sanitize(fileName); |
| 22 | 22 | return response.send({ fileName: sanitizedFilename }); |
| 23 | 23 | } catch (error) { |
| 24 | 24 | console.logerror(error); |
| 25 | 25 | return response.sendStatus(500); |
| 26 | 26 | } |
| 27 | 27 | }); |
| @@ -44,10 +44,10 @@ router.post('/upload', jsonParser, async (request, response) => { | ||
| 44 | 44 | const pathToUpload = path.join(request.user.directories.files, request.body.name); |
| 45 | 45 | writeFileSyncAtomic(pathToUpload, request.body.data, 'base64'); |
| 46 | 46 | const url = clientRelativePath(request.user.directories.root, pathToUpload); |
| 47 | 47 | console.loginfo(`Uploaded file: ${url} from ${request.user.profile.handle}`); |
| 48 | 48 | return response.send({ path: url }); |
| 49 | 49 | } catch (error) { |
| 50 | 50 | console.logerror(error); |
| 51 | 51 | return response.sendStatus(500); |
| 52 | 52 | } |
| 53 | 53 | }); |
| @@ -68,10 +68,10 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | fs.rmSync(pathToDelete); |
| 71 | 71 | console.loginfo(`Deleted file: ${request.body.path} from ${request.user.profile.handle}`); |
| 72 | 72 | return response.sendStatus(200); |
| 73 | 73 | } catch (error) { |
| 74 | 74 | console.logerror(error); |
| 75 | 75 | return response.sendStatus(500); |
| 76 | 76 | } |
| 77 | 77 | }); |
| @@ -87,7 +87,7 @@ router.post('/verify', jsonParser, async (request, response) => { | ||
| 87 | 87 | for (const url of request.body.urls) { |
| 88 | 88 | const pathToVerify = path.join(request.user.directories.root, url); |
| 89 | 89 | if (!pathToVerify.startsWith(request.user.directories.files)) { |
| 90 | 90 | console.debugwarn(`File verification: Invalid path: ${pathToVerify}`); |
| 91 | 91 | continue; |
| 92 | 92 | } |
| 93 | 93 | const fileExists = fs.existsSync(pathToVerify); |
| @@ -96,7 +96,7 @@ router.post('/verify', jsonParser, async (request, response) => { | ||
| 96 | 96 | |
| 97 | 97 | return response.send(verified); |
| 98 | 98 | } catch (error) { |
| 99 | 99 | console.logerror(error); |
| 100 | 100 | return response.sendStatus(500); |
| 101 | 101 | } |
| 102 | 102 | }); |
| @@ -34,7 +34,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 34 | 34 | generationConfig: { maxOutputTokens: 1000 }, |
| 35 | 35 | }; |
| 36 | 36 | |
| 37 | 37 | console.logdebug('Multimodal captioning request', model, body); |
| 38 | 38 | |
| 39 | 39 | const result = await fetch(url, { |
| 40 | 40 | body: JSON.stringify(body), |
| @@ -46,13 +46,13 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 46 | 46 | |
| 47 | 47 | if (!result.ok) { |
| 48 | 48 | const error = await result.json(); |
| 49 | 49 | console.logerror(`Google AI Studio API returned error: ${result.status} ${result.statusText}`, error); |
| 50 | 50 | return response.status(result.status).send({ error: true }); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** @type {any} */ |
| 54 | 54 | const data = await result.json(); |
| 55 | 55 | console.loginfo('Multimodal captioning response', data); |
| 56 | 56 | |
| 57 | 57 | const candidates = data?.candidates; |
| 58 | 58 | if (!candidates) { |
| @@ -114,7 +114,7 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 114 | 114 | |
| 115 | 115 | if (group && Array.isArray(group.chats)) { |
| 116 | 116 | for (const chat of group.chats) { |
| 117 | 117 | console.loginfo('Deleting group chat', chat); |
| 118 | 118 | const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`); |
| 119 | 119 | |
| 120 | 120 | if (fs.existsSync(pathToFile)) { |
| @@ -155,7 +155,7 @@ router.post('/cancel-task', jsonParser, async (request, response) => { | ||
| 155 | 155 | }); |
| 156 | 156 | |
| 157 | 157 | const data = await fetchResult.json(); |
| 158 | 158 | console.loginfo(`Cancelled Horde task ${taskId}`); |
| 159 | 159 | return response.send(data); |
| 160 | 160 | } catch (error) { |
| 161 | 161 | console.error(error); |
| @@ -174,7 +174,7 @@ router.post('/task-status', jsonParser, async (request, response) => { | ||
| 174 | 174 | }); |
| 175 | 175 | |
| 176 | 176 | const data = await fetchResult.json(); |
| 177 | 177 | console.loginfo(`Horde task ${taskId} status:`, data); |
| 178 | 178 | return response.send(data); |
| 179 | 179 | } catch (error) { |
| 180 | 180 | console.error(error); |
| @@ -187,7 +187,7 @@ router.post('/generate-text', jsonParser, async (request, response) => { | ||
| 187 | 187 | const url = 'https://aihorde.net/api/v2/generate/text/async'; |
| 188 | 188 | const agent = await getClientAgent(); |
| 189 | 189 | |
| 190 | 190 | console.logdebug(request.body); |
| 191 | 191 | try { |
| 192 | 192 | const result = await fetch(url, { |
| 193 | 193 | method: 'POST', |
| @@ -201,14 +201,14 @@ router.post('/generate-text', jsonParser, async (request, response) => { | ||
| 201 | 201 | |
| 202 | 202 | if (!result.ok) { |
| 203 | 203 | const message = await result.text(); |
| 204 | 204 | console.logerror('Horde returned an error:', message); |
| 205 | 205 | return response.send({ error: { message } }); |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | const data = await result.json(); |
| 209 | 209 | return response.send(data); |
| 210 | 210 | } catch (error) { |
| 211 | 211 | console.logerror(error); |
| 212 | 212 | return response.send({ error: true }); |
| 213 | 213 | } |
| 214 | 214 | }); |
| @@ -254,7 +254,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 254 | 254 | for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { |
| 255 | 255 | await delay(CHECK_INTERVAL); |
| 256 | 256 | const status = await ai_horde.getInterrogationStatus(result.id); |
| 257 | 257 | console.loginfo(status); |
| 258 | 258 | |
| 259 | 259 | if (status.state === HordeAsyncRequestStates.done) { |
| 260 | 260 | |
| @@ -263,7 +263,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 263 | 263 | return response.sendStatus(500); |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | console.logdebug('Image interrogation result:', status); |
| 267 | 267 | const caption = status?.forms[0]?.result?.caption || ''; |
| 268 | 268 | |
| 269 | 269 | if (!caption) { |
| @@ -275,7 +275,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | if (status.state === HordeAsyncRequestStates.faulted || status.state === HordeAsyncRequestStates.cancelled) { |
| 278 | 278 | console.logerror('Image interrogation request is not successful.'); |
| 279 | 279 | return response.sendStatus(503); |
| 280 | 280 | } |
| 281 | 281 | } |
| @@ -315,7 +315,7 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 315 | 315 | try { |
| 316 | 316 | const maxLength = PROMPT_THRESHOLD - String(request.body.negative_prompt).length - 5; |
| 317 | 317 | if (String(request.body.prompt).length > maxLength) { |
| 318 | 318 | console.logwarn('Stable Horde prompt is too long, truncating...'); |
| 319 | 319 | request.body.prompt = String(request.body.prompt).substring(0, maxLength); |
| 320 | 320 | } |
| 321 | 321 | |
| @@ -324,14 +324,14 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 324 | 324 | const sanitized = sanitizeHordeImagePrompt(request.body.prompt); |
| 325 | 325 | |
| 326 | 326 | if (request.body.prompt !== sanitized) { |
| 327 | 327 | console.loginfo('Stable Horde prompt was sanitized.'); |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | request.body.prompt = sanitized; |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | const api_key_horde = readSecret(request.user.directories, SECRET_KEYS.HORDE) || ANONYMOUS_KEY; |
| 334 | 334 | console.logdebug('Stable Horde request:', request.body); |
| 335 | 335 | |
| 336 | 336 | const ai_horde = await getHordeClient(); |
| 337 | 337 | // noinspection JSCheckFunctionSignatures -- see @ts-ignore - use_gfpgan |
| @@ -360,16 +360,16 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 360 | 360 | { token: api_key_horde }); |
| 361 | 361 | |
| 362 | 362 | if (!generation.id) { |
| 363 | 363 | console.errorwarn('Image generation request is not satisfyable:', generation.message || 'unknown error'); |
| 364 | 364 | return response.sendStatus(400); |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | console.loginfo('Horde image generation request:', generation); |
| 368 | 368 | |
| 369 | 369 | const controller = new AbortController(); |
| 370 | 370 | request.socket.removeAllListeners('close'); |
| 371 | 371 | request.socket.on('close', function () { |
| 372 | 372 | console.logwarn('Horde image generation request aborted.'); |
| 373 | 373 | controller.abort(); |
| 374 | 374 | if (generation.id) ai_horde.deleteImageGenerationRequest(generation.id); |
| 375 | 375 | }); |
| @@ -378,7 +378,7 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 378 | 378 | controller.signal.throwIfAborted(); |
| 379 | 379 | await delay(CHECK_INTERVAL); |
| 380 | 380 | const check = await ai_horde.getImageGenerationCheck(generation.id); |
| 381 | 381 | console.loginfo(check); |
| 382 | 382 | |
| 383 | 383 | if (check.done) { |
| 384 | 384 | const result = await ai_horde.getImageGenerationStatus(generation.id); |
| @@ -71,7 +71,7 @@ router.post('/upload', jsonParser, async (request, response) => { | ||
| 71 | 71 | await fs.promises.writeFile(pathToNewFile, new Uint8Array(imageBuffer)); |
| 72 | 72 | response.send({ path: clientRelativePath(request.user.directories.root, pathToNewFile) }); |
| 73 | 73 | } catch (error) { |
| 74 | 74 | console.logerror(error); |
| 75 | 75 | response.status(500).send({ error: 'Failed to save the image' }); |
| 76 | 76 | } |
| 77 | 77 | }); |
| @@ -120,7 +120,7 @@ router.post('/status', jsonParser, async function (req, res) { | ||
| 120 | 120 | const api_key_novel = readSecret(req.user.directories, SECRET_KEYS.NOVEL); |
| 121 | 121 | |
| 122 | 122 | if (!api_key_novel) { |
| 123 | 123 | console.logwarn('NovelAI Access Token is missing.'); |
| 124 | 124 | return res.sendStatus(400); |
| 125 | 125 | } |
| 126 | 126 | |
| @@ -137,15 +137,15 @@ router.post('/status', jsonParser, async function (req, res) { | ||
| 137 | 137 | const data = await response.json(); |
| 138 | 138 | return res.send(data); |
| 139 | 139 | } else if (response.status == 401) { |
| 140 | 140 | console.logerror('NovelAI Access Token is incorrect.'); |
| 141 | 141 | return res.send({ error: true }); |
| 142 | 142 | } |
| 143 | 143 | else { |
| 144 | 144 | console.logwarn('NovelAI returned an error:', response.statusText); |
| 145 | 145 | return res.send({ error: true }); |
| 146 | 146 | } |
| 147 | 147 | } catch (error) { |
| 148 | 148 | console.logerror(error); |
| 149 | 149 | return res.send({ error: true }); |
| 150 | 150 | } |
| 151 | 151 | }); |
| @@ -156,7 +156,7 @@ router.post('/generate', jsonParser, async function (req, res) { | ||
| 156 | 156 | const api_key_novel = readSecret(req.user.directories, SECRET_KEYS.NOVEL); |
| 157 | 157 | |
| 158 | 158 | if (!api_key_novel) { |
| 159 | 159 | console.logwarn('NovelAI Access Token is missing.'); |
| 160 | 160 | return res.sendStatus(400); |
| 161 | 161 | } |
| 162 | 162 | |
| @@ -241,7 +241,7 @@ router.post('/generate', jsonParser, async function (req, res) { | ||
| 241 | 241 | } |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | console.logdebug(util.inspect(data, { depth: 4 })); |
| 245 | 245 | |
| 246 | 246 | const args = { |
| 247 | 247 | body: JSON.stringify(data), |
| @@ -261,7 +261,7 @@ router.post('/generate', jsonParser, async function (req, res) { | ||
| 261 | 261 | if (!response.ok) { |
| 262 | 262 | const text = await response.text(); |
| 263 | 263 | let message = text; |
| 264 | 264 | console.logwarn(`Novel API returned error: ${response.status} ${response.statusText} ${text}`); |
| 265 | 265 | |
| 266 | 266 | try { |
| 267 | 267 | const data = JSON.parse(text); |
| @@ -276,7 +276,7 @@ router.post('/generate', jsonParser, async function (req, res) { | ||
| 276 | 276 | |
| 277 | 277 | /** @type {any} */ |
| 278 | 278 | const data = await response.json(); |
| 279 | 279 | console.loginfo('NovelAI Output', data?.output); |
| 280 | 280 | return res.send(data); |
| 281 | 281 | } |
| 282 | 282 | } catch (error) { |
| @@ -292,12 +292,12 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 292 | 292 | const key = readSecret(request.user.directories, SECRET_KEYS.NOVEL); |
| 293 | 293 | |
| 294 | 294 | if (!key) { |
| 295 | 295 | console.logwarn('NovelAI Access Token is missing.'); |
| 296 | 296 | return response.sendStatus(400); |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | try { |
| 300 | 300 | console.logdebug('NAI Diffusion request:', request.body); |
| 301 | 301 | const generateUrl = `${IMAGE_NOVELAI}/ai/generate-image`; |
| 302 | 302 | const generateResult = await fetch(generateUrl, { |
| 303 | 303 | method: 'POST', |
| @@ -358,7 +358,7 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 358 | 358 | |
| 359 | 359 | if (!generateResult.ok) { |
| 360 | 360 | const text = await generateResult.text(); |
| 361 | 361 | console.logwarn('NovelAI returned an error.', generateResult.statusText, text); |
| 362 | 362 | return response.sendStatus(500); |
| 363 | 363 | } |
| 364 | 364 | |
| @@ -366,7 +366,7 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 366 | 366 | const imageBuffer = await extractFileFromZipBuffer(archiveBuffer, '.png'); |
| 367 | 367 | |
| 368 | 368 | if (!imageBuffer) { |
| 369 | 369 | console.warnerror('NovelAI generated an image, but the PNG file was not found.'); |
| 370 | 370 | return response.sendStatus(500); |
| 371 | 371 | } |
| 372 | 372 | |
| @@ -378,7 +378,7 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 378 | 378 | } |
| 379 | 379 | |
| 380 | 380 | try { |
| 381 | 381 | console.debuginfo('Upscaling image...'); |
| 382 | 382 | const upscaleUrl = `${API_NOVELAI}/ai/upscale`; |
| 383 | 383 | const upscaleResult = await fetch(upscaleUrl, { |
| 384 | 384 | method: 'POST', |
| @@ -413,7 +413,7 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 413 | 413 | return response.send(originalBase64); |
| 414 | 414 | } |
| 415 | 415 | } catch (error) { |
| 416 | 416 | console.logerror(error); |
| 417 | 417 | return response.sendStatus(500); |
| 418 | 418 | } |
| 419 | 419 | }); |
| @@ -422,7 +422,7 @@ router.post('/generate-voice', jsonParser, async (request, response) => { | ||
| 422 | 422 | const token = readSecret(request.user.directories, SECRET_KEYS.NOVEL); |
| 423 | 423 | |
| 424 | 424 | if (!token) { |
| 425 | 425 | console.logerror('NovelAI Access Token is missing.'); |
| 426 | 426 | return response.sendStatus(400); |
| 427 | 427 | } |
| 428 | 428 | |
| @@ -445,7 +445,7 @@ router.post('/generate-voice', jsonParser, async (request, response) => { | ||
| 445 | 445 | |
| 446 | 446 | if (!result.ok) { |
| 447 | 447 | const errorText = await result.text(); |
| 448 | 448 | console.logerror('NovelAI returned an error.', result.statusText, errorText); |
| 449 | 449 | return response.sendStatus(500); |
| 450 | 450 | } |
| 451 | 451 | |
| @@ -63,7 +63,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | if (!key && !request.body.reverse_proxy && ['custom', 'ooba', 'koboldcpp', 'vllm'].includes(request.body.api) === false) { |
| 66 | 66 | console.logwarn('No key found for API', request.body.api); |
| 67 | 67 | return response.sendStatus(400); |
| 68 | 68 | } |
| 69 | 69 | |
| @@ -93,7 +93,7 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 93 | 93 | excludeKeysByYaml(body, request.body.custom_exclude_body); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | console.logdebug('Multimodal captioning request', body); |
| 97 | 97 | |
| 98 | 98 | let apiUrl = ''; |
| 99 | 99 | |
| @@ -158,13 +158,13 @@ router.post('/caption-image', jsonParser, async (request, response) => { | ||
| 158 | 158 | |
| 159 | 159 | if (!result.ok) { |
| 160 | 160 | const text = await result.text(); |
| 161 | 161 | console.logwarn('Multimodal captioning request failed', result.statusText, text); |
| 162 | 162 | return response.status(500).send(text); |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** @type {any} */ |
| 166 | 166 | const data = await result.json(); |
| 167 | 167 | console.loginfo('Multimodal captioning response', data); |
| 168 | 168 | const caption = data?.choices[0]?.message?.content; |
| 169 | 169 | |
| 170 | 170 | if (!caption) { |
| @@ -184,17 +184,17 @@ router.post('/transcribe-audio', urlencodedParser, async (request, response) => | ||
| 184 | 184 | const key = readSecret(request.user.directories, SECRET_KEYS.OPENAI); |
| 185 | 185 | |
| 186 | 186 | if (!key) { |
| 187 | 187 | console.logwarn('No OpenAI key found'); |
| 188 | 188 | return response.sendStatus(400); |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | if (!request.file) { |
| 192 | 192 | console.logwarn('No audio file found'); |
| 193 | 193 | return response.sendStatus(400); |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | const formData = new FormData(); |
| 197 | 197 | console.loginfo('Processing audio file', request.file.path); |
| 198 | 198 | formData.append('file', fs.createReadStream(request.file.path), { filename: 'audio.wav', contentType: 'audio/wav' }); |
| 199 | 199 | formData.append('model', request.body.model); |
| 200 | 200 | |
| @@ -213,13 +213,13 @@ router.post('/transcribe-audio', urlencodedParser, async (request, response) => | ||
| 213 | 213 | |
| 214 | 214 | if (!result.ok) { |
| 215 | 215 | const text = await result.text(); |
| 216 | 216 | console.logwarn('OpenAI request failed', result.statusText, text); |
| 217 | 217 | return response.status(500).send(text); |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | fs.rmSync(request.file.path); |
| 221 | 221 | const data = await result.json(); |
| 222 | 222 | console.logdebug('OpenAI transcription response', data); |
| 223 | 223 | return response.json(data); |
| 224 | 224 | } catch (error) { |
| 225 | 225 | console.error('OpenAI transcription failed', error); |
| @@ -232,7 +232,7 @@ router.post('/generate-voice', jsonParser, async (request, response) => { | ||
| 232 | 232 | const key = readSecret(request.user.directories, SECRET_KEYS.OPENAI); |
| 233 | 233 | |
| 234 | 234 | if (!key) { |
| 235 | 235 | console.logwarn('No OpenAI key found'); |
| 236 | 236 | return response.sendStatus(400); |
| 237 | 237 | } |
| 238 | 238 | |
| @@ -253,7 +253,7 @@ router.post('/generate-voice', jsonParser, async (request, response) => { | ||
| 253 | 253 | |
| 254 | 254 | if (!result.ok) { |
| 255 | 255 | const text = await result.text(); |
| 256 | 256 | console.logwarn('OpenAI request failed', result.statusText, text); |
| 257 | 257 | return response.status(500).send(text); |
| 258 | 258 | } |
| 259 | 259 | |
| @@ -271,11 +271,11 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 271 | 271 | const key = readSecret(request.user.directories, SECRET_KEYS.OPENAI); |
| 272 | 272 | |
| 273 | 273 | if (!key) { |
| 274 | 274 | console.logwarn('No OpenAI key found'); |
| 275 | 275 | return response.sendStatus(400); |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | console.logdebug('OpenAI request', request.body); |
| 279 | 279 | |
| 280 | 280 | const result = await fetch('https://api.openai.com/v1/images/generations', { |
| 281 | 281 | method: 'POST', |
| @@ -288,7 +288,7 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 288 | 288 | |
| 289 | 289 | if (!result.ok) { |
| 290 | 290 | const text = await result.text(); |
| 291 | 291 | console.logwarn('OpenAI request failed', result.statusText, text); |
| 292 | 292 | return response.status(500).send(text); |
| 293 | 293 | } |
| 294 | 294 | |
| @@ -308,7 +308,7 @@ custom.post('/generate-voice', jsonParser, async (request, response) => { | ||
| 308 | 308 | const { input, provider_endpoint, response_format, voice, speed, model } = request.body; |
| 309 | 309 | |
| 310 | 310 | if (!provider_endpoint) { |
| 311 | 311 | console.logwarn('No OpenAI-compatible TTS provider endpoint provided'); |
| 312 | 312 | return response.sendStatus(400); |
| 313 | 313 | } |
| 314 | 314 | |
| @@ -329,7 +329,7 @@ custom.post('/generate-voice', jsonParser, async (request, response) => { | ||
| 329 | 329 | |
| 330 | 330 | if (!result.ok) { |
| 331 | 331 | const text = await result.text(); |
| 332 | 332 | console.logwarn('OpenAI request failed', result.statusText, text); |
| 333 | 333 | return response.status(500).send(text); |
| 334 | 334 | } |
| 335 | 335 | |
| @@ -96,7 +96,7 @@ router.post('/restore', jsonParser, function (request, response) { | ||
| 96 | 96 | |
| 97 | 97 | return response.send(result); |
| 98 | 98 | } catch (error) { |
| 99 | 99 | console.logerror(error); |
| 100 | 100 | return response.sendStatus(500); |
| 101 | 101 | } |
| 102 | 102 | }); |
| @@ -96,25 +96,25 @@ router.post('/serpapi', jsonParser, async (request, response) => { | ||
| 96 | 96 | const key = readSecret(request.user.directories, SECRET_KEYS.SERPAPI); |
| 97 | 97 | |
| 98 | 98 | if (!key) { |
| 99 | 99 | console.logerror('No SerpApi key found'); |
| 100 | 100 | return response.sendStatus(400); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | const { query } = request.body; |
| 104 | 104 | const result = await fetch(`https://serpapi.com/search.json?q=${encodeURIComponent(query)}&api_key=${key}`); |
| 105 | 105 | |
| 106 | 106 | console.logdebug('SerpApi query', query); |
| 107 | 107 | |
| 108 | 108 | if (!result.ok) { |
| 109 | 109 | const text = await result.text(); |
| 110 | 110 | console.logerror('SerpApi request failed', result.statusText, text); |
| 111 | 111 | return response.status(500).send(text); |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | const data = await result.json(); |
| 115 | 115 | return response.json(data); |
| 116 | 116 | } catch (error) { |
| 117 | 117 | console.logerror(error); |
| 118 | 118 | return response.sendStatus(500); |
| 119 | 119 | } |
| 120 | 120 | }); |
| @@ -130,7 +130,7 @@ router.post('/transcript', jsonParser, async (request, response) => { | ||
| 130 | 130 | const json = request.body.json; |
| 131 | 131 | |
| 132 | 132 | if (!id) { |
| 133 | 133 | console.logerror('Id is required for /transcript'); |
| 134 | 134 | return response.sendStatus(400); |
| 135 | 135 | } |
| 136 | 136 | |
| @@ -155,7 +155,7 @@ router.post('/transcript', jsonParser, async (request, response) => { | ||
| 155 | 155 | throw error; |
| 156 | 156 | } |
| 157 | 157 | } catch (error) { |
| 158 | 158 | console.logerror(error); |
| 159 | 159 | return response.sendStatus(500); |
| 160 | 160 | } |
| 161 | 161 | }); |
| @@ -165,17 +165,17 @@ router.post('/searxng', jsonParser, async (request, response) => { | ||
| 165 | 165 | const { baseUrl, query, preferences, categories } = request.body; |
| 166 | 166 | |
| 167 | 167 | if (!baseUrl || !query) { |
| 168 | 168 | console.logerror('Missing required parameters for /searxng'); |
| 169 | 169 | return response.sendStatus(400); |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | console.logdebug('SearXNG query', baseUrl, query); |
| 173 | 173 | |
| 174 | 174 | const mainPageUrl = new URL(baseUrl); |
| 175 | 175 | const mainPageRequest = await fetch(mainPageUrl, { headers: visitHeaders }); |
| 176 | 176 | |
| 177 | 177 | if (!mainPageRequest.ok) { |
| 178 | 178 | console.logerror('SearXNG request failed', mainPageRequest.statusText); |
| 179 | 179 | return response.sendStatus(500); |
| 180 | 180 | } |
| 181 | 181 | |
| @@ -202,14 +202,14 @@ router.post('/searxng', jsonParser, async (request, response) => { | ||
| 202 | 202 | |
| 203 | 203 | if (!searchResult.ok) { |
| 204 | 204 | const text = await searchResult.text(); |
| 205 | 205 | console.logerror('SearXNG request failed', searchResult.statusText, text); |
| 206 | 206 | return response.sendStatus(500); |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | const data = await searchResult.text(); |
| 210 | 210 | return response.send(data); |
| 211 | 211 | } catch (error) { |
| 212 | 212 | console.logerror('SearXNG request failed', error); |
| 213 | 213 | return response.sendStatus(500); |
| 214 | 214 | } |
| 215 | 215 | }); |
| @@ -219,7 +219,7 @@ router.post('/tavily', jsonParser, async (request, response) => { | ||
| 219 | 219 | const apiKey = readSecret(request.user.directories, SECRET_KEYS.TAVILY); |
| 220 | 220 | |
| 221 | 221 | if (!apiKey) { |
| 222 | 222 | console.logerror('No Tavily key found'); |
| 223 | 223 | return response.sendStatus(400); |
| 224 | 224 | } |
| 225 | 225 | |
| @@ -246,18 +246,18 @@ router.post('/tavily', jsonParser, async (request, response) => { | ||
| 246 | 246 | body: JSON.stringify(body), |
| 247 | 247 | }); |
| 248 | 248 | |
| 249 | 249 | console.logdebug('Tavily query', query); |
| 250 | 250 | |
| 251 | 251 | if (!result.ok) { |
| 252 | 252 | const text = await result.text(); |
| 253 | 253 | console.logerror('Tavily request failed', result.statusText, text); |
| 254 | 254 | return response.status(500).send(text); |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | const data = await result.json(); |
| 258 | 258 | return response.json(data); |
| 259 | 259 | } catch (error) { |
| 260 | 260 | console.logerror(error); |
| 261 | 261 | return response.sendStatus(500); |
| 262 | 262 | } |
| 263 | 263 | }); |
| @@ -303,7 +303,7 @@ router.post('/visit', jsonParser, async (request, response) => { | ||
| 303 | 303 | const html = Boolean(request.body.html ?? true); |
| 304 | 304 | |
| 305 | 305 | if (!url) { |
| 306 | 306 | console.logerror('No url provided for /visit'); |
| 307 | 307 | return response.sendStatus(400); |
| 308 | 308 | } |
| 309 | 309 | |
| @@ -330,16 +330,16 @@ router.post('/visit', jsonParser, async (request, response) => { | ||
| 330 | 330 | throw new Error('Invalid hostname'); |
| 331 | 331 | } |
| 332 | 332 | } catch (error) { |
| 333 | 333 | console.logerror('Invalid url provided for /visit', url); |
| 334 | 334 | return response.sendStatus(400); |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | 337 | console.loginfo('Visiting web URL', url); |
| 338 | 338 | |
| 339 | 339 | const result = await fetch(url, { headers: visitHeaders }); |
| 340 | 340 | |
| 341 | 341 | if (!result.ok) { |
| 342 | 342 | console.logerror(`Visit failed ${result.status} ${result.statusText}`); |
| 343 | 343 | return response.sendStatus(500); |
| 344 | 344 | } |
| 345 | 345 | |
| @@ -347,7 +347,7 @@ router.post('/visit', jsonParser, async (request, response) => { | ||
| 347 | 347 | |
| 348 | 348 | if (html) { |
| 349 | 349 | if (!contentType.includes('text/html')) { |
| 350 | 350 | console.logerror(`Visit failed, content-type is ${contentType}, expected text/html`); |
| 351 | 351 | return response.sendStatus(500); |
| 352 | 352 | } |
| 353 | 353 | |
| @@ -359,7 +359,7 @@ router.post('/visit', jsonParser, async (request, response) => { | ||
| 359 | 359 | const buffer = await result.arrayBuffer(); |
| 360 | 360 | return response.send(Buffer.from(buffer)); |
| 361 | 361 | } catch (error) { |
| 362 | 362 | console.logerror(error); |
| 363 | 363 | return response.sendStatus(500); |
| 364 | 364 | } |
| 365 | 365 | }); |
| @@ -151,7 +151,7 @@ export function getAllSecrets(directories) { | ||
| 151 | 151 | const filePath = path.join(directories.root, SECRETS_FILE); |
| 152 | 152 | |
| 153 | 153 | if (!fs.existsSync(filePath)) { |
| 154 | 154 | console.logerror('Secrets file does not exist'); |
| 155 | 155 | return undefined; |
| 156 | 156 | } |
| 157 | 157 | |
| @@ -105,7 +105,7 @@ function readPresetsFromDirectory(directoryPath, options = {}) { | ||
| 105 | 105 | fileNames.push(removeFileExtension ? item.replace(/\.[^/.]+$/, '') : item); |
| 106 | 106 | } catch { |
| 107 | 107 | // skip |
| 108 | 108 | console.logwarn(`${item} is not a valid JSON`); |
| 109 | 109 | } |
| 110 | 110 | }); |
| 111 | 111 | |
| @@ -120,7 +120,7 @@ async function backupSettings() { | ||
| 120 | 120 | backupUserSettings(handle, true); |
| 121 | 121 | } |
| 122 | 122 | } catch (err) { |
| 123 | 123 | console.logerror('Could not backup settings file', err); |
| 124 | 124 | } |
| 125 | 125 | } |
| 126 | 126 | |
| @@ -202,7 +202,7 @@ router.post('/save', jsonParser, function (request, response) { | ||
| 202 | 202 | triggerAutoSave(request.user.profile.handle); |
| 203 | 203 | response.send({ result: 'ok' }); |
| 204 | 204 | } catch (err) { |
| 205 | 205 | console.logerror(err); |
| 206 | 206 | response.send(err); |
| 207 | 207 | } |
| 208 | 208 | }); |
| @@ -292,7 +292,7 @@ router.post('/get-snapshots', jsonParser, async (request, response) => { | ||
| 292 | 292 | |
| 293 | 293 | response.json(result); |
| 294 | 294 | } catch (error) { |
| 295 | 295 | console.logerror(error); |
| 296 | 296 | response.sendStatus(500); |
| 297 | 297 | } |
| 298 | 298 | }); |
| @@ -316,7 +316,7 @@ router.post('/load-snapshot', jsonParser, getFileNameValidationFunction('name'), | ||
| 316 | 316 | |
| 317 | 317 | response.send(content); |
| 318 | 318 | } catch (error) { |
| 319 | 319 | console.logerror(error); |
| 320 | 320 | response.sendStatus(500); |
| 321 | 321 | } |
| 322 | 322 | }); |
| @@ -326,7 +326,7 @@ router.post('/make-snapshot', jsonParser, async (request, response) => { | ||
| 326 | 326 | backupUserSettings(request.user.profile.handle, false); |
| 327 | 327 | response.sendStatus(204); |
| 328 | 328 | } catch (error) { |
| 329 | 329 | console.logerror(error); |
| 330 | 330 | response.sendStatus(500); |
| 331 | 331 | } |
| 332 | 332 | }); |
| @@ -352,7 +352,7 @@ router.post('/restore-snapshot', jsonParser, getFileNameValidationFunction('name | ||
| 352 | 352 | |
| 353 | 353 | response.sendStatus(204); |
| 354 | 354 | } catch (error) { |
| 355 | 355 | console.logerror(error); |
| 356 | 356 | response.sendStatus(500); |
| 357 | 357 | } |
| 358 | 358 | }); |
| @@ -43,8 +43,8 @@ router.post('/recognize', jsonParser, async (req, res) => { | ||
| 43 | 43 | const start = performance.now(); |
| 44 | 44 | const result = await pipe(wav, { language: lang || null, task: 'transcribe' }); |
| 45 | 45 | const end = performance.now(); |
| 46 | 46 | console.loginfo(`Execution duration: ${(end - start) / 1000} seconds`); |
| 47 | 47 | console.loginfo('Transcribed audio:', result.text); |
| 48 | 48 | |
| 49 | 49 | return res.json({ text: result.text }); |
| 50 | 50 | } catch (error) { |
| @@ -64,7 +64,7 @@ router.post('/synthesize', jsonParser, async (req, res) => { | ||
| 64 | 64 | const start = performance.now(); |
| 65 | 65 | const result = await pipe(text, { speaker_embeddings: speaker_embeddings }); |
| 66 | 66 | const end = performance.now(); |
| 67 | 67 | console.logdebug(`Execution duration: ${(end - start) / 1000} seconds`); |
| 68 | 68 | |
| 69 | 69 | const wav = new wavefile.WaveFile(); |
| 70 | 70 | wav.fromScratch(1, result.sampling_rate, '32f', result.audio); |
| @@ -82,14 +82,14 @@ export function importRisuSprites(directories, data) { | ||
| 82 | 82 | return; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | console.loginfo(`RisuAI: Found ${images.length} sprites for ${name}. Writing to disk.`); |
| 86 | 86 | const files = fs.readdirSync(spritesPath); |
| 87 | 87 | |
| 88 | 88 | outer: for (const [label, fileBase64] of images) { |
| 89 | 89 | // Remove existing sprite with the same label |
| 90 | 90 | for (const file of files) { |
| 91 | 91 | if (path.parse(file).name === label) { |
| 92 | 92 | console.logwarn(`RisuAI: The sprite ${label} for ${name} already exists. Skipping.`); |
| 93 | 93 | continue outer; |
| 94 | 94 | } |
| 95 | 95 | } |
| @@ -133,7 +133,7 @@ router.get('/get', jsonParser, function (request, response) { | ||
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | 135 | catch (err) { |
| 136 | 136 | console.logerror(err); |
| 137 | 137 | } |
| 138 | 138 | return response.send(sprites); |
| 139 | 139 | }); |
| @@ -45,7 +45,7 @@ router.post('/ping', jsonParser, async (request, response) => { | ||
| 45 | 45 | |
| 46 | 46 | return response.sendStatus(200); |
| 47 | 47 | } catch (error) { |
| 48 | 48 | console.logerror(error); |
| 49 | 49 | return response.sendStatus(500); |
| 50 | 50 | } |
| 51 | 51 | }); |
| @@ -99,7 +99,7 @@ router.post('/upscalers', jsonParser, async (request, response) => { | ||
| 99 | 99 | |
| 100 | 100 | return response.send(upscalers); |
| 101 | 101 | } catch (error) { |
| 102 | 102 | console.logerror(error); |
| 103 | 103 | return response.sendStatus(500); |
| 104 | 104 | } |
| 105 | 105 | }); |
| @@ -131,7 +131,7 @@ router.post('/vaes', jsonParser, async (request, response) => { | ||
| 131 | 131 | const names = data.map(x => x.model_name); |
| 132 | 132 | return response.send(names); |
| 133 | 133 | } catch (error) { |
| 134 | 134 | console.logerror(error); |
| 135 | 135 | return response.sendStatus(500); |
| 136 | 136 | } |
| 137 | 137 | }); |
| @@ -158,7 +158,7 @@ router.post('/samplers', jsonParser, async (request, response) => { | ||
| 158 | 158 | return response.send(names); |
| 159 | 159 | |
| 160 | 160 | } catch (error) { |
| 161 | 161 | console.logerror(error); |
| 162 | 162 | return response.sendStatus(500); |
| 163 | 163 | } |
| 164 | 164 | }); |
| @@ -184,7 +184,7 @@ router.post('/schedulers', jsonParser, async (request, response) => { | ||
| 184 | 184 | const names = data.map(x => x.name); |
| 185 | 185 | return response.send(names); |
| 186 | 186 | } catch (error) { |
| 187 | 187 | console.logerror(error); |
| 188 | 188 | return response.sendStatus(500); |
| 189 | 189 | } |
| 190 | 190 | }); |
| @@ -210,7 +210,7 @@ router.post('/models', jsonParser, async (request, response) => { | ||
| 210 | 210 | const models = data.map(x => ({ value: x.title, text: x.title })); |
| 211 | 211 | return response.send(models); |
| 212 | 212 | } catch (error) { |
| 213 | 213 | console.logerror(error); |
| 214 | 214 | return response.sendStatus(500); |
| 215 | 215 | } |
| 216 | 216 | }); |
| @@ -230,7 +230,7 @@ router.post('/get-model', jsonParser, async (request, response) => { | ||
| 230 | 230 | const data = await result.json(); |
| 231 | 231 | return response.send(data['sd_model_checkpoint']); |
| 232 | 232 | } catch (error) { |
| 233 | 233 | console.logerror(error); |
| 234 | 234 | return response.sendStatus(500); |
| 235 | 235 | } |
| 236 | 236 | }); |
| @@ -283,13 +283,13 @@ router.post('/set-model', jsonParser, async (request, response) => { | ||
| 283 | 283 | break; |
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | console.loginfo(`Waiting for SD WebUI to finish model loading... Progress: ${progress}; Job count: ${jobCount}`); |
| 287 | 287 | await delay(CHECK_INTERVAL); |
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | return response.sendStatus(200); |
| 291 | 291 | } catch (error) { |
| 292 | 292 | console.logerror(error); |
| 293 | 293 | return response.sendStatus(500); |
| 294 | 294 | } |
| 295 | 295 | }); |
| @@ -309,7 +309,7 @@ router.post('/generate', jsonParser, async (request, response) => { | ||
| 309 | 309 | } |
| 310 | 310 | } |
| 311 | 311 | } catch (error) { |
| 312 | 312 | console.logerror('SD WebUI failed to get options:', error); |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | const controller = new AbortController(); |
| @@ -323,7 +323,7 @@ router.post('/generate', jsonParser, async (request, response) => { | ||
| 323 | 323 | controller.abort(); |
| 324 | 324 | }); |
| 325 | 325 | |
| 326 | 326 | console.logdebug('SD WebUI request:', request.body); |
| 327 | 327 | const txt2imgUrl = new URL(request.body.url); |
| 328 | 328 | txt2imgUrl.pathname = '/sdapi/v1/txt2img'; |
| 329 | 329 | const result = await fetch(txt2imgUrl, { |
| @@ -344,7 +344,7 @@ router.post('/generate', jsonParser, async (request, response) => { | ||
| 344 | 344 | const data = await result.json(); |
| 345 | 345 | return response.send(data); |
| 346 | 346 | } catch (error) { |
| 347 | 347 | console.logerror(error); |
| 348 | 348 | return response.sendStatus(500); |
| 349 | 349 | } |
| 350 | 350 | }); |
| @@ -377,7 +377,7 @@ router.post('/sd-next/upscalers', jsonParser, async (request, response) => { | ||
| 377 | 377 | |
| 378 | 378 | return response.send(names); |
| 379 | 379 | } catch (error) { |
| 380 | 380 | console.logerror(error); |
| 381 | 381 | return response.sendStatus(500); |
| 382 | 382 | } |
| 383 | 383 | }); |
| @@ -395,7 +395,7 @@ comfy.post('/ping', jsonParser, async (request, response) => { | ||
| 395 | 395 | |
| 396 | 396 | return response.sendStatus(200); |
| 397 | 397 | } catch (error) { |
| 398 | 398 | console.logerror(error); |
| 399 | 399 | return response.sendStatus(500); |
| 400 | 400 | } |
| 401 | 401 | }); |
| @@ -413,7 +413,7 @@ comfy.post('/samplers', jsonParser, async (request, response) => { | ||
| 413 | 413 | const data = await result.json(); |
| 414 | 414 | return response.send(data.KSampler.input.required.sampler_name[0]); |
| 415 | 415 | } catch (error) { |
| 416 | 416 | console.logerror(error); |
| 417 | 417 | return response.sendStatus(500); |
| 418 | 418 | } |
| 419 | 419 | }); |
| @@ -441,7 +441,7 @@ comfy.post('/models', jsonParser, async (request, response) => { | ||
| 441 | 441 | |
| 442 | 442 | return response.send(models); |
| 443 | 443 | } catch (error) { |
| 444 | 444 | console.logerror(error); |
| 445 | 445 | return response.sendStatus(500); |
| 446 | 446 | } |
| 447 | 447 | }); |
| @@ -459,7 +459,7 @@ comfy.post('/schedulers', jsonParser, async (request, response) => { | ||
| 459 | 459 | const data = await result.json(); |
| 460 | 460 | return response.send(data.KSampler.input.required.scheduler[0]); |
| 461 | 461 | } catch (error) { |
| 462 | 462 | console.logerror(error); |
| 463 | 463 | return response.sendStatus(500); |
| 464 | 464 | } |
| 465 | 465 | }); |
| @@ -477,7 +477,7 @@ comfy.post('/vaes', jsonParser, async (request, response) => { | ||
| 477 | 477 | const data = await result.json(); |
| 478 | 478 | return response.send(data.VAELoader.input.required.vae_name[0]); |
| 479 | 479 | } catch (error) { |
| 480 | 480 | console.logerror(error); |
| 481 | 481 | return response.sendStatus(500); |
| 482 | 482 | } |
| 483 | 483 | }); |
| @@ -487,7 +487,7 @@ comfy.post('/workflows', jsonParser, async (request, response) => { | ||
| 487 | 487 | const data = getComfyWorkflows(request.user.directories); |
| 488 | 488 | return response.send(data); |
| 489 | 489 | } catch (error) { |
| 490 | 490 | console.logerror(error); |
| 491 | 491 | return response.sendStatus(500); |
| 492 | 492 | } |
| 493 | 493 | }); |
| @@ -501,7 +501,7 @@ comfy.post('/workflow', jsonParser, async (request, response) => { | ||
| 501 | 501 | const data = fs.readFileSync(filePath, { encoding: 'utf-8' }); |
| 502 | 502 | return response.send(JSON.stringify(data)); |
| 503 | 503 | } catch (error) { |
| 504 | 504 | console.logerror(error); |
| 505 | 505 | return response.sendStatus(500); |
| 506 | 506 | } |
| 507 | 507 | }); |
| @@ -513,7 +513,7 @@ comfy.post('/save-workflow', jsonParser, async (request, response) => { | ||
| 513 | 513 | const data = getComfyWorkflows(request.user.directories); |
| 514 | 514 | return response.send(data); |
| 515 | 515 | } catch (error) { |
| 516 | 516 | console.logerror(error); |
| 517 | 517 | return response.sendStatus(500); |
| 518 | 518 | } |
| 519 | 519 | }); |
| @@ -526,7 +526,7 @@ comfy.post('/delete-workflow', jsonParser, async (request, response) => { | ||
| 526 | 526 | } |
| 527 | 527 | return response.sendStatus(200); |
| 528 | 528 | } catch (error) { |
| 529 | 529 | console.logerror(error); |
| 530 | 530 | return response.sendStatus(500); |
| 531 | 531 | } |
| 532 | 532 | }); |
| @@ -591,7 +591,7 @@ comfy.post('/generate', jsonParser, async (request, response) => { | ||
| 591 | 591 | const imgBuffer = await imgResponse.arrayBuffer(); |
| 592 | 592 | return response.send(Buffer.from(imgBuffer).toString('base64')); |
| 593 | 593 | } catch (error) { |
| 594 | 594 | console.logerror('ComfyUI error:', error); |
| 595 | 595 | response.status(500).send(error.message); |
| 596 | 596 | return response; |
| 597 | 597 | } |
| @@ -604,7 +604,7 @@ together.post('/models', jsonParser, async (request, response) => { | ||
| 604 | 604 | const key = readSecret(request.user.directories, SECRET_KEYS.TOGETHERAI); |
| 605 | 605 | |
| 606 | 606 | if (!key) { |
| 607 | 607 | console.logwarn('TogetherAI key not found.'); |
| 608 | 608 | return response.sendStatus(400); |
| 609 | 609 | } |
| 610 | 610 | |
| @@ -616,14 +616,14 @@ together.post('/models', jsonParser, async (request, response) => { | ||
| 616 | 616 | }); |
| 617 | 617 | |
| 618 | 618 | if (!modelsResponse.ok) { |
| 619 | 619 | console.logwarn('TogetherAI returned an error.'); |
| 620 | 620 | return response.sendStatus(500); |
| 621 | 621 | } |
| 622 | 622 | |
| 623 | 623 | const data = await modelsResponse.json(); |
| 624 | 624 | |
| 625 | 625 | if (!Array.isArray(data)) { |
| 626 | 626 | console.logwarn('TogetherAI returned invalid data.'); |
| 627 | 627 | return response.sendStatus(500); |
| 628 | 628 | } |
| 629 | 629 | |
| @@ -633,7 +633,7 @@ together.post('/models', jsonParser, async (request, response) => { | ||
| 633 | 633 | |
| 634 | 634 | return response.send(models); |
| 635 | 635 | } catch (error) { |
| 636 | 636 | console.logerror(error); |
| 637 | 637 | return response.sendStatus(500); |
| 638 | 638 | } |
| 639 | 639 | }); |
| @@ -643,11 +643,11 @@ together.post('/generate', jsonParser, async (request, response) => { | ||
| 643 | 643 | const key = readSecret(request.user.directories, SECRET_KEYS.TOGETHERAI); |
| 644 | 644 | |
| 645 | 645 | if (!key) { |
| 646 | 646 | console.logwarn('TogetherAI key not found.'); |
| 647 | 647 | return response.sendStatus(400); |
| 648 | 648 | } |
| 649 | 649 | |
| 650 | 650 | console.logdebug('TogetherAI request:', request.body); |
| 651 | 651 | |
| 652 | 652 | const result = await fetch('https://api.together.xyz/v1/images/generations', { |
| 653 | 653 | method: 'POST', |
| @@ -669,13 +669,13 @@ together.post('/generate', jsonParser, async (request, response) => { | ||
| 669 | 669 | }); |
| 670 | 670 | |
| 671 | 671 | if (!result.ok) { |
| 672 | 672 | console.logwarn('TogetherAI returned an error.', { body: await result.text() }); |
| 673 | 673 | return response.sendStatus(500); |
| 674 | 674 | } |
| 675 | 675 | |
| 676 | 676 | /** @type {any} */ |
| 677 | 677 | const data = await result.json(); |
| 678 | 678 | console.logdebug('TogetherAI response:', data); |
| 679 | 679 | |
| 680 | 680 | const choice = data?.data?.[0]; |
| 681 | 681 | let b64_json = choice.b64_json; |
| @@ -687,7 +687,7 @@ together.post('/generate', jsonParser, async (request, response) => { | ||
| 687 | 687 | |
| 688 | 688 | return response.send({ format: 'jpg', data: b64_json }); |
| 689 | 689 | } catch (error) { |
| 690 | 690 | console.logerror(error); |
| 691 | 691 | return response.sendStatus(500); |
| 692 | 692 | } |
| 693 | 693 | }); |
| @@ -709,7 +709,7 @@ drawthings.post('/ping', jsonParser, async (request, response) => { | ||
| 709 | 709 | |
| 710 | 710 | return response.sendStatus(200); |
| 711 | 711 | } catch (error) { |
| 712 | 712 | console.logerror(error); |
| 713 | 713 | return response.sendStatus(500); |
| 714 | 714 | } |
| 715 | 715 | }); |
| @@ -728,7 +728,7 @@ drawthings.post('/get-model', jsonParser, async (request, response) => { | ||
| 728 | 728 | |
| 729 | 729 | return response.send(data['model']); |
| 730 | 730 | } catch (error) { |
| 731 | 731 | console.logerror(error); |
| 732 | 732 | return response.sendStatus(500); |
| 733 | 733 | } |
| 734 | 734 | }); |
| @@ -747,14 +747,14 @@ drawthings.post('/get-upscaler', jsonParser, async (request, response) => { | ||
| 747 | 747 | |
| 748 | 748 | return response.send(data['upscaler']); |
| 749 | 749 | } catch (error) { |
| 750 | 750 | console.logerror(error); |
| 751 | 751 | return response.sendStatus(500); |
| 752 | 752 | } |
| 753 | 753 | }); |
| 754 | 754 | |
| 755 | 755 | drawthings.post('/generate', jsonParser, async (request, response) => { |
| 756 | 756 | try { |
| 757 | 757 | console.logdebug('SD DrawThings API request:', request.body); |
| 758 | 758 | |
| 759 | 759 | const url = new URL(request.body.url); |
| 760 | 760 | url.pathname = '/sdapi/v1/txt2img'; |
| @@ -781,7 +781,7 @@ drawthings.post('/generate', jsonParser, async (request, response) => { | ||
| 781 | 781 | const data = await result.json(); |
| 782 | 782 | return response.send(data); |
| 783 | 783 | } catch (error) { |
| 784 | 784 | console.logerror(error); |
| 785 | 785 | return response.sendStatus(500); |
| 786 | 786 | } |
| 787 | 787 | }); |
| @@ -794,21 +794,21 @@ pollinations.post('/models', jsonParser, async (_request, response) => { | ||
| 794 | 794 | const result = await fetch(modelsUrl); |
| 795 | 795 | |
| 796 | 796 | if (!result.ok) { |
| 797 | 797 | console.logwarn('Pollinations returned an error.', result.status, result.statusText); |
| 798 | 798 | throw new Error('Pollinations request failed.'); |
| 799 | 799 | } |
| 800 | 800 | |
| 801 | 801 | const data = await result.json(); |
| 802 | 802 | |
| 803 | 803 | if (!Array.isArray(data)) { |
| 804 | 804 | console.logwarn('Pollinations returned invalid data.'); |
| 805 | 805 | throw new Error('Pollinations request failed.'); |
| 806 | 806 | } |
| 807 | 807 | |
| 808 | 808 | const models = data.map(x => ({ value: x, text: x })); |
| 809 | 809 | return response.send(models); |
| 810 | 810 | } catch (error) { |
| 811 | 811 | console.logerror(error); |
| 812 | 812 | return response.sendStatus(500); |
| 813 | 813 | } |
| 814 | 814 | }); |
| @@ -829,12 +829,12 @@ pollinations.post('/generate', jsonParser, async (request, response) => { | ||
| 829 | 829 | }); |
| 830 | 830 | promptUrl.search = params.toString(); |
| 831 | 831 | |
| 832 | 832 | console.loginfo('Pollinations request URL:', promptUrl.toString()); |
| 833 | 833 | |
| 834 | 834 | const result = await fetch(promptUrl); |
| 835 | 835 | |
| 836 | 836 | if (!result.ok) { |
| 837 | 837 | console.logwarn('Pollinations returned an error.', result.status, result.statusText); |
| 838 | 838 | throw new Error('Pollinations request failed.'); |
| 839 | 839 | } |
| 840 | 840 | |
| @@ -843,7 +843,7 @@ pollinations.post('/generate', jsonParser, async (request, response) => { | ||
| 843 | 843 | |
| 844 | 844 | return response.send({ image: base64 }); |
| 845 | 845 | } catch (error) { |
| 846 | 846 | console.logerror(error); |
| 847 | 847 | return response.sendStatus(500); |
| 848 | 848 | } |
| 849 | 849 | }); |
| @@ -855,13 +855,13 @@ stability.post('/generate', jsonParser, async (request, response) => { | ||
| 855 | 855 | const key = readSecret(request.user.directories, SECRET_KEYS.STABILITY); |
| 856 | 856 | |
| 857 | 857 | if (!key) { |
| 858 | 858 | console.logwarn('Stability AI key not found.'); |
| 859 | 859 | return response.sendStatus(400); |
| 860 | 860 | } |
| 861 | 861 | |
| 862 | 862 | const { payload, model } = request.body; |
| 863 | 863 | |
| 864 | 864 | console.logdebug('Stability AI request:', model, payload); |
| 865 | 865 | |
| 866 | 866 | const formData = new FormData(); |
| 867 | 867 | for (const [key, value] of Object.entries(payload)) { |
| @@ -896,14 +896,14 @@ stability.post('/generate', jsonParser, async (request, response) => { | ||
| 896 | 896 | |
| 897 | 897 | if (!result.ok) { |
| 898 | 898 | const text = await result.text(); |
| 899 | 899 | console.logwarn('Stability AI returned an error.', result.status, result.statusText, text); |
| 900 | 900 | return response.sendStatus(500); |
| 901 | 901 | } |
| 902 | 902 | |
| 903 | 903 | const buffer = await result.arrayBuffer(); |
| 904 | 904 | return response.send(Buffer.from(buffer).toString('base64')); |
| 905 | 905 | } catch (error) { |
| 906 | 906 | console.logerror(error); |
| 907 | 907 | return response.sendStatus(500); |
| 908 | 908 | } |
| 909 | 909 | }); |
| @@ -915,7 +915,7 @@ blockentropy.post('/models', jsonParser, async (request, response) => { | ||
| 915 | 915 | const key = readSecret(request.user.directories, SECRET_KEYS.BLOCKENTROPY); |
| 916 | 916 | |
| 917 | 917 | if (!key) { |
| 918 | 918 | console.logwarn('Block Entropy key not found.'); |
| 919 | 919 | return response.sendStatus(400); |
| 920 | 920 | } |
| 921 | 921 | |
| @@ -927,21 +927,21 @@ blockentropy.post('/models', jsonParser, async (request, response) => { | ||
| 927 | 927 | }); |
| 928 | 928 | |
| 929 | 929 | if (!modelsResponse.ok) { |
| 930 | 930 | console.logwarn('Block Entropy returned an error.'); |
| 931 | 931 | return response.sendStatus(500); |
| 932 | 932 | } |
| 933 | 933 | |
| 934 | 934 | const data = await modelsResponse.json(); |
| 935 | 935 | |
| 936 | 936 | if (!Array.isArray(data)) { |
| 937 | 937 | console.logwarn('Block Entropy returned invalid data.'); |
| 938 | 938 | return response.sendStatus(500); |
| 939 | 939 | } |
| 940 | 940 | const models = data.map(x => ({ value: x.name, text: x.name })); |
| 941 | 941 | return response.send(models); |
| 942 | 942 | |
| 943 | 943 | } catch (error) { |
| 944 | 944 | console.logerror(error); |
| 945 | 945 | return response.sendStatus(500); |
| 946 | 946 | } |
| 947 | 947 | }); |
| @@ -951,11 +951,11 @@ blockentropy.post('/generate', jsonParser, async (request, response) => { | ||
| 951 | 951 | const key = readSecret(request.user.directories, SECRET_KEYS.BLOCKENTROPY); |
| 952 | 952 | |
| 953 | 953 | if (!key) { |
| 954 | 954 | console.logwarn('Block Entropy key not found.'); |
| 955 | 955 | return response.sendStatus(400); |
| 956 | 956 | } |
| 957 | 957 | |
| 958 | 958 | console.logdebug('Block Entropy request:', request.body); |
| 959 | 959 | |
| 960 | 960 | const result = await fetch('https://api.blockentropy.ai/sdapi/v1/txt2img', { |
| 961 | 961 | method: 'POST', |
| @@ -976,16 +976,16 @@ blockentropy.post('/generate', jsonParser, async (request, response) => { | ||
| 976 | 976 | }); |
| 977 | 977 | |
| 978 | 978 | if (!result.ok) { |
| 979 | 979 | console.logwarn('Block Entropy returned an error.'); |
| 980 | 980 | return response.sendStatus(500); |
| 981 | 981 | } |
| 982 | 982 | |
| 983 | 983 | const data = await result.json(); |
| 984 | 984 | console.logdebug('Block Entropy response:', data); |
| 985 | 985 | |
| 986 | 986 | return response.send(data); |
| 987 | 987 | } catch (error) { |
| 988 | 988 | console.logerror(error); |
| 989 | 989 | return response.sendStatus(500); |
| 990 | 990 | } |
| 991 | 991 | }); |
| @@ -998,11 +998,11 @@ huggingface.post('/generate', jsonParser, async (request, response) => { | ||
| 998 | 998 | const key = readSecret(request.user.directories, SECRET_KEYS.HUGGINGFACE); |
| 999 | 999 | |
| 1000 | 1000 | if (!key) { |
| 1001 | 1001 | console.logwarn('Hugging Face key not found.'); |
| 1002 | 1002 | return response.sendStatus(400); |
| 1003 | 1003 | } |
| 1004 | 1004 | |
| 1005 | 1005 | console.logdebug('Hugging Face request:', request.body); |
| 1006 | 1006 | |
| 1007 | 1007 | const result = await fetch(`https://api-inference.huggingface.co/models/${request.body.model}`, { |
| 1008 | 1008 | method: 'POST', |
| @@ -1016,7 +1016,7 @@ huggingface.post('/generate', jsonParser, async (request, response) => { | ||
| 1016 | 1016 | }); |
| 1017 | 1017 | |
| 1018 | 1018 | if (!result.ok) { |
| 1019 | 1019 | console.logwarn('Hugging Face returned an error.'); |
| 1020 | 1020 | return response.sendStatus(500); |
| 1021 | 1021 | } |
| 1022 | 1022 | |
| @@ -1025,7 +1025,7 @@ huggingface.post('/generate', jsonParser, async (request, response) => { | ||
| 1025 | 1025 | image: Buffer.from(buffer).toString('base64'), |
| 1026 | 1026 | }); |
| 1027 | 1027 | } catch (error) { |
| 1028 | 1028 | console.logerror(error); |
| 1029 | 1029 | return response.sendStatus(500); |
| 1030 | 1030 | } |
| 1031 | 1031 | }); |
| @@ -1037,7 +1037,7 @@ nanogpt.post('/models', jsonParser, async (request, response) => { | ||
| 1037 | 1037 | const key = readSecret(request.user.directories, SECRET_KEYS.NANOGPT); |
| 1038 | 1038 | |
| 1039 | 1039 | if (!key) { |
| 1040 | 1040 | console.logwarn('NanoGPT key not found.'); |
| 1041 | 1041 | return response.sendStatus(400); |
| 1042 | 1042 | } |
| 1043 | 1043 | |
| @@ -1050,7 +1050,7 @@ nanogpt.post('/models', jsonParser, async (request, response) => { | ||
| 1050 | 1050 | }); |
| 1051 | 1051 | |
| 1052 | 1052 | if (!modelsResponse.ok) { |
| 1053 | 1053 | console.logwarn('NanoGPT returned an error.'); |
| 1054 | 1054 | return response.sendStatus(500); |
| 1055 | 1055 | } |
| 1056 | 1056 | |
| @@ -1059,7 +1059,7 @@ nanogpt.post('/models', jsonParser, async (request, response) => { | ||
| 1059 | 1059 | const imageModels = data?.models?.image; |
| 1060 | 1060 | |
| 1061 | 1061 | if (!imageModels || typeof imageModels !== 'object') { |
| 1062 | 1062 | console.logwarn('NanoGPT returned invalid data.'); |
| 1063 | 1063 | return response.sendStatus(500); |
| 1064 | 1064 | } |
| 1065 | 1065 | |
| @@ -1067,7 +1067,7 @@ nanogpt.post('/models', jsonParser, async (request, response) => { | ||
| 1067 | 1067 | return response.send(models); |
| 1068 | 1068 | } |
| 1069 | 1069 | catch (error) { |
| 1070 | 1070 | console.logerror(error); |
| 1071 | 1071 | return response.sendStatus(500); |
| 1072 | 1072 | } |
| 1073 | 1073 | }); |
| @@ -1077,11 +1077,11 @@ nanogpt.post('/generate', jsonParser, async (request, response) => { | ||
| 1077 | 1077 | const key = readSecret(request.user.directories, SECRET_KEYS.NANOGPT); |
| 1078 | 1078 | |
| 1079 | 1079 | if (!key) { |
| 1080 | 1080 | console.logwarn('NanoGPT key not found.'); |
| 1081 | 1081 | return response.sendStatus(400); |
| 1082 | 1082 | } |
| 1083 | 1083 | |
| 1084 | 1084 | console.logdebug('NanoGPT request:', request.body); |
| 1085 | 1085 | |
| 1086 | 1086 | const result = await fetch('https://nano-gpt.com/api/generate-image', { |
| 1087 | 1087 | method: 'POST', |
| @@ -1093,7 +1093,7 @@ nanogpt.post('/generate', jsonParser, async (request, response) => { | ||
| 1093 | 1093 | }); |
| 1094 | 1094 | |
| 1095 | 1095 | if (!result.ok) { |
| 1096 | 1096 | console.logwarn('NanoGPT returned an error.'); |
| 1097 | 1097 | return response.sendStatus(500); |
| 1098 | 1098 | } |
| 1099 | 1099 | |
| @@ -1102,14 +1102,14 @@ nanogpt.post('/generate', jsonParser, async (request, response) => { | ||
| 1102 | 1102 | |
| 1103 | 1103 | const image = data?.data?.[0]?.b64_json; |
| 1104 | 1104 | if (!image) { |
| 1105 | 1105 | console.logwarn('NanoGPT returned invalid data.'); |
| 1106 | 1106 | return response.sendStatus(500); |
| 1107 | 1107 | } |
| 1108 | 1108 | |
| 1109 | 1109 | return response.send({ image }); |
| 1110 | 1110 | } |
| 1111 | 1111 | catch (error) { |
| 1112 | 1112 | console.logerror(error); |
| 1113 | 1113 | return response.sendStatus(500); |
| 1114 | 1114 | } |
| 1115 | 1115 | }); |
| @@ -1121,7 +1121,7 @@ bfl.post('/generate', jsonParser, async (request, response) => { | ||
| 1121 | 1121 | const key = readSecret(request.user.directories, SECRET_KEYS.BFL); |
| 1122 | 1122 | |
| 1123 | 1123 | if (!key) { |
| 1124 | 1124 | console.logwarn('BFL key not found.'); |
| 1125 | 1125 | return response.sendStatus(400); |
| 1126 | 1126 | } |
| 1127 | 1127 | |
| @@ -1173,7 +1173,7 @@ bfl.post('/generate', jsonParser, async (request, response) => { | ||
| 1173 | 1173 | delete requestBody.guidance; |
| 1174 | 1174 | } |
| 1175 | 1175 | |
| 1176 | 1176 | console.logdebug('BFL request:', requestBody); |
| 1177 | 1177 | |
| 1178 | 1178 | const result = await fetch(`https://api.bfl.ml/v1/${request.body.model}`, { |
| 1179 | 1179 | method: 'POST', |
| @@ -1185,7 +1185,7 @@ bfl.post('/generate', jsonParser, async (request, response) => { | ||
| 1185 | 1185 | }); |
| 1186 | 1186 | |
| 1187 | 1187 | if (!result.ok) { |
| 1188 | 1188 | console.logwarn('BFL returned an error.'); |
| 1189 | 1189 | return response.sendStatus(500); |
| 1190 | 1190 | } |
| 1191 | 1191 | |
| @@ -1201,7 +1201,7 @@ bfl.post('/generate', jsonParser, async (request, response) => { | ||
| 1201 | 1201 | |
| 1202 | 1202 | if (!statusResult.ok) { |
| 1203 | 1203 | const text = await statusResult.text(); |
| 1204 | 1204 | console.logwarn('BFL returned an error.', text); |
| 1205 | 1205 | return response.sendStatus(500); |
| 1206 | 1206 | } |
| 1207 | 1207 | |
| @@ -1223,7 +1223,7 @@ bfl.post('/generate', jsonParser, async (request, response) => { | ||
| 1223 | 1223 | throw new Error('BFL failed to generate image.', { cause: statusData }); |
| 1224 | 1224 | } |
| 1225 | 1225 | } catch (error) { |
| 1226 | 1226 | console.logerror(error); |
| 1227 | 1227 | return response.sendStatus(500); |
| 1228 | 1228 | } |
| 1229 | 1229 | }); |
| @@ -148,7 +148,7 @@ async function collectAndCreateStats(chatsPath, charactersPath) { | ||
| 148 | 148 | * @param {string} charactersPath Path to the directory containing the character files. |
| 149 | 149 | */ |
| 150 | 150 | export async function recreateStats(handle, chatsPath, charactersPath) { |
| 151 | 151 | console.loginfo('Collecting and creating stats for user:', handle); |
| 152 | 152 | const stats = await collectAndCreateStats(chatsPath, charactersPath); |
| 153 | 153 | STATS.set(handle, stats); |
| 154 | 154 | await saveStatsToFile(); |
| @@ -200,7 +200,7 @@ async function saveStatsToFile() { | ||
| 200 | 200 | await writeFileAtomic(statsFilePath, JSON.stringify(charStats)); |
| 201 | 201 | TIMESTAMPS.set(handle, Date.now()); |
| 202 | 202 | } catch (error) { |
| 203 | 203 | console.logerror('Failed to save stats to file.', error); |
| 204 | 204 | } |
| 205 | 205 | } |
| 206 | 206 | } |
| @@ -89,7 +89,6 @@ async function generateThumbnail(directories, type, file) { | ||
| 89 | 89 | let thumbnailFolder = getThumbnailFolder(directories, type); |
| 90 | 90 | let originalFolder = getOriginalFolder(directories, type); |
| 91 | 91 | if (thumbnailFolder === undefined || originalFolder === undefined) throw new Error('Invalid thumbnail type'); |
| 92 | - | |
| 93 | 92 | const pathToCachedFile = path.join(thumbnailFolder, file); |
| 94 | 93 | const pathToOriginalFile = path.join(originalFolder, file); |
| 95 | 94 | |
| @@ -104,7 +103,7 @@ async function generateThumbnail(directories, type, file) { | ||
| 104 | 103 | const cachedStat = fs.statSync(pathToCachedFile); |
| 105 | 104 | |
| 106 | 105 | if (originalStat.mtimeMs > cachedStat.ctimeMs) { |
| 107 | 106 | //console.logwarn('Original file changed. Regenerating thumbnail...'); |
| 108 | 107 | shouldRegenerate = true; |
| 109 | 108 | } |
| 110 | 109 | } |
| @@ -157,7 +156,7 @@ export async function ensureThumbnailCache() { | ||
| 157 | 156 | return; |
| 158 | 157 | } |
| 159 | 158 | |
| 160 | 159 | console.loginfo('Generating thumbnails cache. Please wait...'); |
| 161 | 160 | |
| 162 | 161 | const bgFiles = fs.readdirSync(directories.backgrounds); |
| 163 | 162 | const tasks = []; |
| @@ -167,7 +166,7 @@ export async function ensureThumbnailCache() { | ||
| 167 | 166 | } |
| 168 | 167 | |
| 169 | 168 | await Promise.all(tasks); |
| 170 | 169 | console.loginfo(`Done! Generated: ${bgFiles.length} preview images`); |
| 171 | 170 | } |
| 172 | 171 | } |
| 173 | 172 | |
| @@ -96,7 +96,7 @@ async function getPathToTokenizer(model, fallbackModel) { | ||
| 96 | 96 | throw new Error('Downloading tokenizers is disabled, the model is not cached'); |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | console.loginfo('Downloading tokenizer model:', model); |
| 100 | 100 | const response = await fetch(model); |
| 101 | 101 | if (!response.ok) { |
| 102 | 102 | throw new Error(`Failed to fetch the model: ${response.status} ${response.statusText}`); |
| @@ -108,7 +108,7 @@ async function getPathToTokenizer(model, fallbackModel) { | ||
| 108 | 108 | } catch (error) { |
| 109 | 109 | const getLastSegment = str => str?.split('/')?.pop() || ''; |
| 110 | 110 | if (fallbackModel) { |
| 111 | 111 | console.logerror(`Could not get a tokenizer from ${getLastSegment(model)}. Reason: ${error.message}. Using a fallback model: ${getLastSegment(fallbackModel)}.`); |
| 112 | 112 | return fallbackModel; |
| 113 | 113 | } |
| 114 | 114 | |
| @@ -156,7 +156,7 @@ class SentencePieceTokenizer { | ||
| 156 | 156 | const pathToModel = await getPathToTokenizer(this.#model, this.#fallbackModel); |
| 157 | 157 | this.#instance = new SentencePieceProcessor(); |
| 158 | 158 | await this.#instance.load(pathToModel); |
| 159 | 159 | console.loginfo('Instantiated the tokenizer for', path.parse(pathToModel).name); |
| 160 | 160 | return this.#instance; |
| 161 | 161 | } catch (error) { |
| 162 | 162 | console.error('Sentencepiece tokenizer failed to load: ' + this.#model, error); |
| @@ -205,7 +205,7 @@ class WebTokenizer { | ||
| 205 | 205 | const pathToModel = await getPathToTokenizer(this.#model, this.#fallbackModel); |
| 206 | 206 | const arrayBuffer = fs.readFileSync(pathToModel).buffer; |
| 207 | 207 | this.#instance = await Tokenizer.fromJSON(arrayBuffer); |
| 208 | 208 | console.loginfo('Instantiated the tokenizer for', path.parse(pathToModel).name); |
| 209 | 209 | return this.#instance; |
| 210 | 210 | } catch (error) { |
| 211 | 211 | console.error('Web tokenizer failed to load: ' + this.#model, error); |
| @@ -484,7 +484,7 @@ export function getTiktokenTokenizer(model) { | ||
| 484 | 484 | } |
| 485 | 485 | |
| 486 | 486 | const tokenizer = tiktoken.encoding_for_model(model); |
| 487 | 487 | console.loginfo('Instantiated the tokenizer for', model); |
| 488 | 488 | tokenizersCache[model] = tokenizer; |
| 489 | 489 | return tokenizer; |
| 490 | 490 | } |
| @@ -531,7 +531,7 @@ function createSentencepieceEncodingHandler(tokenizer) { | ||
| 531 | 531 | const chunks = instance?.encodePieces(text); |
| 532 | 532 | return response.send({ ids, count, chunks }); |
| 533 | 533 | } catch (error) { |
| 534 | 534 | console.logerror(error); |
| 535 | 535 | return response.send({ ids: [], count: 0, chunks: [] }); |
| 536 | 536 | } |
| 537 | 537 | }; |
| @@ -562,7 +562,7 @@ function createSentencepieceDecodingHandler(tokenizer) { | ||
| 562 | 562 | const text = chunks.join(''); |
| 563 | 563 | return response.send({ text, chunks }); |
| 564 | 564 | } catch (error) { |
| 565 | 565 | console.logerror(error); |
| 566 | 566 | return response.send({ text: '', chunks: [] }); |
| 567 | 567 | } |
| 568 | 568 | }; |
| @@ -591,7 +591,7 @@ function createTiktokenEncodingHandler(modelId) { | ||
| 591 | 591 | const chunks = await getTiktokenChunks(tokenizer, tokens); |
| 592 | 592 | return response.send({ ids: tokens, count: tokens.length, chunks }); |
| 593 | 593 | } catch (error) { |
| 594 | 594 | console.logerror(error); |
| 595 | 595 | return response.send({ ids: [], count: 0, chunks: [] }); |
| 596 | 596 | } |
| 597 | 597 | }; |
| @@ -620,7 +620,7 @@ function createTiktokenDecodingHandler(modelId) { | ||
| 620 | 620 | const text = new TextDecoder().decode(textBytes); |
| 621 | 621 | return response.send({ text }); |
| 622 | 622 | } catch (error) { |
| 623 | 623 | console.logerror(error); |
| 624 | 624 | return response.send({ text: '' }); |
| 625 | 625 | } |
| 626 | 626 | }; |
| @@ -650,7 +650,7 @@ function createWebTokenizerEncodingHandler(tokenizer) { | ||
| 650 | 650 | const chunks = getWebTokenizersChunks(instance, tokens); |
| 651 | 651 | return response.send({ ids: tokens, count: tokens.length, chunks }); |
| 652 | 652 | } catch (error) { |
| 653 | 653 | console.logerror(error); |
| 654 | 654 | return response.send({ ids: [], count: 0, chunks: [] }); |
| 655 | 655 | } |
| 656 | 656 | }; |
| @@ -681,7 +681,7 @@ function createWebTokenizerDecodingHandler(tokenizer) { | ||
| 681 | 681 | const text = instance.decode(new Int32Array(ids)); |
| 682 | 682 | return response.send({ text, chunks }); |
| 683 | 683 | } catch (error) { |
| 684 | 684 | console.logerror(error); |
| 685 | 685 | return response.send({ text: '', chunks: [] }); |
| 686 | 686 | } |
| 687 | 687 | }; |
| @@ -781,7 +781,7 @@ router.post('/openai/encode', jsonParser, async function (req, res) { | ||
| 781 | 781 | const handler = createTiktokenEncodingHandler(model); |
| 782 | 782 | return handler(req, res); |
| 783 | 783 | } catch (error) { |
| 784 | 784 | console.logerror(error); |
| 785 | 785 | return res.send({ ids: [], count: 0, chunks: [] }); |
| 786 | 786 | } |
| 787 | 787 | }); |
| @@ -849,7 +849,7 @@ router.post('/openai/decode', jsonParser, async function (req, res) { | ||
| 849 | 849 | const handler = createTiktokenDecodingHandler(model); |
| 850 | 850 | return handler(req, res); |
| 851 | 851 | } catch (error) { |
| 852 | 852 | console.logerror(error); |
| 853 | 853 | return res.send({ text: '' }); |
| 854 | 854 | } |
| 855 | 855 | }); |
| @@ -988,7 +988,7 @@ router.post('/remote/kobold/count', jsonParser, async function (request, respons | ||
| 988 | 988 | const result = await fetch(url, args); |
| 989 | 989 | |
| 990 | 990 | if (!result.ok) { |
| 991 | 991 | console.logwarn(`API returned error: ${result.status} ${result.statusText}`); |
| 992 | 992 | return response.send({ error: true }); |
| 993 | 993 | } |
| 994 | 994 | |
| @@ -997,7 +997,7 @@ router.post('/remote/kobold/count', jsonParser, async function (request, respons | ||
| 997 | 997 | const ids = data['ids'] ?? []; |
| 998 | 998 | return response.send({ count, ids }); |
| 999 | 999 | } catch (error) { |
| 1000 | 1000 | console.logerror(error); |
| 1001 | 1001 | return response.send({ error: true }); |
| 1002 | 1002 | } |
| 1003 | 1003 | }); |
| @@ -1052,7 +1052,7 @@ router.post('/remote/textgenerationwebui/encode', jsonParser, async function (re | ||
| 1052 | 1052 | const result = await fetch(url, args); |
| 1053 | 1053 | |
| 1054 | 1054 | if (!result.ok) { |
| 1055 | 1055 | console.logwarn(`API returned error: ${result.status} ${result.statusText}`); |
| 1056 | 1056 | return response.send({ error: true }); |
| 1057 | 1057 | } |
| 1058 | 1058 | |
| @@ -1062,7 +1062,7 @@ router.post('/remote/textgenerationwebui/encode', jsonParser, async function (re | ||
| 1062 | 1062 | |
| 1063 | 1063 | return response.send({ count, ids }); |
| 1064 | 1064 | } catch (error) { |
| 1065 | 1065 | console.logerror(error); |
| 1066 | 1066 | return response.send({ error: true }); |
| 1067 | 1067 | } |
| 1068 | 1068 | }); |
| @@ -35,7 +35,7 @@ function decodeBuffer(buffer) { | ||
| 35 | 35 | try { |
| 36 | 36 | return iconv.decode(Buffer.from(buffer), 'utf-8'); |
| 37 | 37 | } catch (error) { |
| 38 | 38 | console.logerror('Failed to decode buffer:', error); |
| 39 | 39 | return Buffer.from(buffer).toString('utf-8'); |
| 40 | 40 | } |
| 41 | 41 | } |
| @@ -46,7 +46,7 @@ router.post('/libre', jsonParser, async (request, response) => { | ||
| 46 | 46 | const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL); |
| 47 | 47 | |
| 48 | 48 | if (!url) { |
| 49 | 49 | console.logwarn('LibreTranslate URL is not configured.'); |
| 50 | 50 | return response.sendStatus(400); |
| 51 | 51 | } |
| 52 | 52 | |
| @@ -69,7 +69,7 @@ router.post('/libre', jsonParser, async (request, response) => { | ||
| 69 | 69 | return response.sendStatus(400); |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | console.logdebug('Input text: ' + text); |
| 73 | 73 | |
| 74 | 74 | const result = await fetch(url, { |
| 75 | 75 | method: 'POST', |
| @@ -85,17 +85,17 @@ router.post('/libre', jsonParser, async (request, response) => { | ||
| 85 | 85 | |
| 86 | 86 | if (!result.ok) { |
| 87 | 87 | const error = await result.text(); |
| 88 | 88 | console.logwarn('LibreTranslate error: ', result.statusText, error); |
| 89 | 89 | return response.sendStatus(500); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** @type {any} */ |
| 93 | 93 | const json = await result.json(); |
| 94 | 94 | console.logdebug('Translated text: ' + json.translatedText); |
| 95 | 95 | |
| 96 | 96 | return response.send(json.translatedText); |
| 97 | 97 | } catch (error) { |
| 98 | 98 | console.logerror('Translation error: ' + error.message); |
| 99 | 99 | return response.sendStatus(500); |
| 100 | 100 | } |
| 101 | 101 | }); |
| @@ -109,14 +109,14 @@ router.post('/google', jsonParser, async (request, response) => { | ||
| 109 | 109 | return response.sendStatus(400); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | console.logdebug('Input text: ' + text); |
| 113 | 113 | |
| 114 | 114 | const { generateRequestUrl, normaliseResponse } = getGoogleTranslateClient(); |
| 115 | 115 | const requestUrl = generateRequestUrl(text, { to: lang }); |
| 116 | 116 | const result = await fetch(requestUrl); |
| 117 | 117 | |
| 118 | 118 | if (!result.ok) { |
| 119 | 119 | console.logwarn('Google Translate error: ', result.statusText); |
| 120 | 120 | return response.sendStatus(500); |
| 121 | 121 | } |
| 122 | 122 | |
| @@ -125,10 +125,10 @@ router.post('/google', jsonParser, async (request, response) => { | ||
| 125 | 125 | const translatedText = translateResponse.text; |
| 126 | 126 | |
| 127 | 127 | response.setHeader('Content-Type', 'text/plain; charset=utf-8'); |
| 128 | 128 | console.logdebug('Translated text: ' + translatedText); |
| 129 | 129 | return response.send(translatedText); |
| 130 | 130 | } catch (error) { |
| 131 | 131 | console.logerror('Translation error', error); |
| 132 | 132 | return response.sendStatus(500); |
| 133 | 133 | } |
| 134 | 134 | }); |
| @@ -161,7 +161,7 @@ router.post('/yandex', jsonParser, async (request, response) => { | ||
| 161 | 161 | params.append('lang', lang); |
| 162 | 162 | const ucid = uuidv4().replaceAll('-', ''); |
| 163 | 163 | |
| 164 | 164 | console.logdebug('Input text: ' + inputText); |
| 165 | 165 | |
| 166 | 166 | const result = await fetch(`https://translate.yandex.net/api/v1/tr.json/translate?ucid=${ucid}&srv=android&format=text`, { |
| 167 | 167 | method: 'POST', |
| @@ -173,18 +173,18 @@ router.post('/yandex', jsonParser, async (request, response) => { | ||
| 173 | 173 | |
| 174 | 174 | if (!result.ok) { |
| 175 | 175 | const error = await result.text(); |
| 176 | 176 | console.logwarn('Yandex error: ', result.statusText, error); |
| 177 | 177 | return response.sendStatus(500); |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** @type {any} */ |
| 181 | 181 | const json = await result.json(); |
| 182 | 182 | const translated = json.text.join(); |
| 183 | 183 | console.logdebug('Translated text: ' + translated); |
| 184 | 184 | |
| 185 | 185 | return response.send(translated); |
| 186 | 186 | } catch (error) { |
| 187 | 187 | console.logerror('Translation error: ' + error.message); |
| 188 | 188 | return response.sendStatus(500); |
| 189 | 189 | } |
| 190 | 190 | }); |
| @@ -195,7 +195,7 @@ router.post('/lingva', jsonParser, async (request, response) => { | ||
| 195 | 195 | const baseUrl = secretUrl || LINGVA_DEFAULT; |
| 196 | 196 | |
| 197 | 197 | if (!secretUrl && baseUrl === LINGVA_DEFAULT) { |
| 198 | 198 | console.logwarn('Lingva URL is using default value.', LINGVA_DEFAULT); |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') { |
| @@ -213,22 +213,22 @@ router.post('/lingva', jsonParser, async (request, response) => { | ||
| 213 | 213 | return response.sendStatus(400); |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | console.logdebug('Input text: ' + text); |
| 217 | 217 | |
| 218 | 218 | const url = urlJoin(baseUrl, 'auto', lang, encodeURIComponent(text)); |
| 219 | 219 | const result = await fetch(url); |
| 220 | 220 | |
| 221 | 221 | if (!result.ok) { |
| 222 | 222 | const error = await result.text(); |
| 223 | 223 | console.logwarn('Lingva error: ', result.statusText, error); |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /** @type {any} */ |
| 227 | 227 | const data = await result.json(); |
| 228 | 228 | console.logdebug('Translated text: ' + data.translation); |
| 229 | 229 | return response.send(data.translation); |
| 230 | 230 | } catch (error) { |
| 231 | 231 | console.logerror('Translation error', error); |
| 232 | 232 | return response.sendStatus(500); |
| 233 | 233 | } |
| 234 | 234 | }); |
| @@ -238,7 +238,7 @@ router.post('/deepl', jsonParser, async (request, response) => { | ||
| 238 | 238 | const key = readSecret(request.user.directories, SECRET_KEYS.DEEPL); |
| 239 | 239 | |
| 240 | 240 | if (!key) { |
| 241 | 241 | console.logwarn('DeepL key is not configured.'); |
| 242 | 242 | return response.sendStatus(400); |
| 243 | 243 | } |
| 244 | 244 | |
| @@ -254,7 +254,7 @@ router.post('/deepl', jsonParser, async (request, response) => { | ||
| 254 | 254 | return response.sendStatus(400); |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | console.logdebug('Input text: ' + text); |
| 258 | 258 | |
| 259 | 259 | const params = new URLSearchParams(); |
| 260 | 260 | params.append('text', text); |
| @@ -280,17 +280,17 @@ router.post('/deepl', jsonParser, async (request, response) => { | ||
| 280 | 280 | |
| 281 | 281 | if (!result.ok) { |
| 282 | 282 | const error = await result.text(); |
| 283 | 283 | console.logwarn('DeepL error: ', result.statusText, error); |
| 284 | 284 | return response.sendStatus(500); |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | /** @type {any} */ |
| 288 | 288 | const json = await result.json(); |
| 289 | 289 | console.logdebug('Translated text: ' + json.translations[0].text); |
| 290 | 290 | |
| 291 | 291 | return response.send(json.translations[0].text); |
| 292 | 292 | } catch (error) { |
| 293 | 293 | console.logerror('Translation error: ' + error.message); |
| 294 | 294 | return response.sendStatus(500); |
| 295 | 295 | } |
| 296 | 296 | }); |
| @@ -376,7 +376,7 @@ router.post('/deeplx', jsonParser, async (request, response) => { | ||
| 376 | 376 | return response.sendStatus(400); |
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | console.logdebug('Input text: ' + text); |
| 380 | 380 | |
| 381 | 381 | const result = await fetch(url, { |
| 382 | 382 | method: 'POST', |
| @@ -393,17 +393,17 @@ router.post('/deeplx', jsonParser, async (request, response) => { | ||
| 393 | 393 | |
| 394 | 394 | if (!result.ok) { |
| 395 | 395 | const error = await result.text(); |
| 396 | 396 | console.logwarn('DeepLX error: ', result.statusText, error); |
| 397 | 397 | return response.sendStatus(500); |
| 398 | 398 | } |
| 399 | 399 | |
| 400 | 400 | /** @type {any} */ |
| 401 | 401 | const json = await result.json(); |
| 402 | 402 | console.logdebug('Translated text: ' + json.data); |
| 403 | 403 | |
| 404 | 404 | return response.send(json.data); |
| 405 | 405 | } catch (error) { |
| 406 | 406 | console.logerror('DeepLX translation error: ' + error.message); |
| 407 | 407 | return response.sendStatus(500); |
| 408 | 408 | } |
| 409 | 409 | }); |
| @@ -429,14 +429,14 @@ router.post('/bing', jsonParser, async (request, response) => { | ||
| 429 | 429 | return response.sendStatus(400); |
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | console.logdebug('Input text: ' + text); |
| 433 | 433 | |
| 434 | 434 | const result = await bingTranslate(text, null, lang); |
| 435 | 435 | const translatedText = result?.translation; |
| 436 | 436 | console.logdebug('Translated text: ' + translatedText); |
| 437 | 437 | return response.send(translatedText); |
| 438 | 438 | } catch (error) { |
| 439 | 439 | console.logerror('Translation error', error); |
| 440 | 440 | return response.sendStatus(500); |
| 441 | 441 | } |
| 442 | 442 | }); |
| @@ -53,12 +53,12 @@ router.post('/get', requireAdminMiddleware, jsonParser, async (_request, respons | ||
| 53 | 53 | router.post('/disable', requireAdminMiddleware, jsonParser, async (request, response) => { |
| 54 | 54 | try { |
| 55 | 55 | if (!request.body.handle) { |
| 56 | 56 | console.logwarn('Disable user failed: Missing required fields'); |
| 57 | 57 | return response.status(400).json({ error: 'Missing required fields' }); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | if (request.body.handle === request.user.profile.handle) { |
| 61 | 61 | console.logwarn('Disable user failed: Cannot disable yourself'); |
| 62 | 62 | return response.status(400).json({ error: 'Cannot disable yourself' }); |
| 63 | 63 | } |
| 64 | 64 | |
| @@ -66,7 +66,7 @@ router.post('/disable', requireAdminMiddleware, jsonParser, async (request, resp | ||
| 66 | 66 | const user = await storage.getItem(toKey(request.body.handle)); |
| 67 | 67 | |
| 68 | 68 | if (!user) { |
| 69 | 69 | console.logerror('Disable user failed: User not found'); |
| 70 | 70 | return response.status(404).json({ error: 'User not found' }); |
| 71 | 71 | } |
| 72 | 72 | |
| @@ -82,7 +82,7 @@ router.post('/disable', requireAdminMiddleware, jsonParser, async (request, resp | ||
| 82 | 82 | router.post('/enable', requireAdminMiddleware, jsonParser, async (request, response) => { |
| 83 | 83 | try { |
| 84 | 84 | if (!request.body.handle) { |
| 85 | 85 | console.logwarn('Enable user failed: Missing required fields'); |
| 86 | 86 | return response.status(400).json({ error: 'Missing required fields' }); |
| 87 | 87 | } |
| 88 | 88 | |
| @@ -90,7 +90,7 @@ router.post('/enable', requireAdminMiddleware, jsonParser, async (request, respo | ||
| 90 | 90 | const user = await storage.getItem(toKey(request.body.handle)); |
| 91 | 91 | |
| 92 | 92 | if (!user) { |
| 93 | 93 | console.logerror('Enable user failed: User not found'); |
| 94 | 94 | return response.status(404).json({ error: 'User not found' }); |
| 95 | 95 | } |
| 96 | 96 | |
| @@ -106,7 +106,7 @@ router.post('/enable', requireAdminMiddleware, jsonParser, async (request, respo | ||
| 106 | 106 | router.post('/promote', requireAdminMiddleware, jsonParser, async (request, response) => { |
| 107 | 107 | try { |
| 108 | 108 | if (!request.body.handle) { |
| 109 | 109 | console.logwarn('Promote user failed: Missing required fields'); |
| 110 | 110 | return response.status(400).json({ error: 'Missing required fields' }); |
| 111 | 111 | } |
| 112 | 112 | |
| @@ -114,7 +114,7 @@ router.post('/promote', requireAdminMiddleware, jsonParser, async (request, resp | ||
| 114 | 114 | const user = await storage.getItem(toKey(request.body.handle)); |
| 115 | 115 | |
| 116 | 116 | if (!user) { |
| 117 | 117 | console.logerror('Promote user failed: User not found'); |
| 118 | 118 | return response.status(404).json({ error: 'User not found' }); |
| 119 | 119 | } |
| 120 | 120 | |
| @@ -130,12 +130,12 @@ router.post('/promote', requireAdminMiddleware, jsonParser, async (request, resp | ||
| 130 | 130 | router.post('/demote', requireAdminMiddleware, jsonParser, async (request, response) => { |
| 131 | 131 | try { |
| 132 | 132 | if (!request.body.handle) { |
| 133 | 133 | console.logwarn('Demote user failed: Missing required fields'); |
| 134 | 134 | return response.status(400).json({ error: 'Missing required fields' }); |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | if (request.body.handle === request.user.profile.handle) { |
| 138 | 138 | console.logwarn('Demote user failed: Cannot demote yourself'); |
| 139 | 139 | return response.status(400).json({ error: 'Cannot demote yourself' }); |
| 140 | 140 | } |
| 141 | 141 | |
| @@ -143,7 +143,7 @@ router.post('/demote', requireAdminMiddleware, jsonParser, async (request, respo | ||
| 143 | 143 | const user = await storage.getItem(toKey(request.body.handle)); |
| 144 | 144 | |
| 145 | 145 | if (!user) { |
| 146 | 146 | console.logerror('Demote user failed: User not found'); |
| 147 | 147 | return response.status(404).json({ error: 'User not found' }); |
| 148 | 148 | } |
| 149 | 149 | |
| @@ -159,7 +159,7 @@ router.post('/demote', requireAdminMiddleware, jsonParser, async (request, respo | ||
| 159 | 159 | router.post('/create', requireAdminMiddleware, jsonParser, async (request, response) => { |
| 160 | 160 | try { |
| 161 | 161 | if (!request.body.handle || !request.body.name) { |
| 162 | 162 | console.logwarn('Create user failed: Missing required fields'); |
| 163 | 163 | return response.status(400).json({ error: 'Missing required fields' }); |
| 164 | 164 | } |
| 165 | 165 | |
| @@ -167,12 +167,12 @@ router.post('/create', requireAdminMiddleware, jsonParser, async (request, respo | ||
| 167 | 167 | const handle = lodash.kebabCase(String(request.body.handle).toLowerCase().trim()); |
| 168 | 168 | |
| 169 | 169 | if (!handle) { |
| 170 | 170 | console.logwarn('Create user failed: Invalid handle'); |
| 171 | 171 | return response.status(400).json({ error: 'Invalid handle' }); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | if (handles.some(x => x === handle)) { |
| 175 | 175 | console.logwarn('Create user failed: User with that handle already exists'); |
| 176 | 176 | return response.status(409).json({ error: 'User already exists' }); |
| 177 | 177 | } |
| 178 | 178 | |
| @@ -192,7 +192,7 @@ router.post('/create', requireAdminMiddleware, jsonParser, async (request, respo | ||
| 192 | 192 | await storage.setItem(toKey(handle), newUser); |
| 193 | 193 | |
| 194 | 194 | // Create user directories |
| 195 | 195 | console.loginfo('Creating data directories for', newUser.handle); |
| 196 | 196 | await ensurePublicDirectoriesExist(); |
| 197 | 197 | const directories = getUserDirectories(newUser.handle); |
| 198 | 198 | await checkForNewContent([directories]); |
| @@ -206,17 +206,17 @@ router.post('/create', requireAdminMiddleware, jsonParser, async (request, respo | ||
| 206 | 206 | router.post('/delete', requireAdminMiddleware, jsonParser, async (request, response) => { |
| 207 | 207 | try { |
| 208 | 208 | if (!request.body.handle) { |
| 209 | 209 | console.logwarn('Delete user failed: Missing required fields'); |
| 210 | 210 | return response.status(400).json({ error: 'Missing required fields' }); |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | if (request.body.handle === request.user.profile.handle) { |
| 214 | 214 | console.logwarn('Delete user failed: Cannot delete yourself'); |
| 215 | 215 | return response.status(400).json({ error: 'Cannot delete yourself' }); |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | if (request.body.handle === DEFAULT_USER.handle) { |
| 219 | 219 | console.logwarn('Delete user failed: Cannot delete default user'); |
| 220 | 220 | return response.status(400).json({ error: 'Sorry, but the default user cannot be deleted. It is required as a fallback.' }); |
| 221 | 221 | } |
| 222 | 222 | |
| @@ -224,7 +224,7 @@ router.post('/delete', requireAdminMiddleware, jsonParser, async (request, respo | ||
| 224 | 224 | |
| 225 | 225 | if (request.body.purge) { |
| 226 | 226 | const directories = getUserDirectories(request.body.handle); |
| 227 | 227 | console.loginfo('Deleting data directories for', request.body.handle); |
| 228 | 228 | await fsPromises.rm(directories.root, { recursive: true, force: true }); |
| 229 | 229 | } |
| 230 | 230 | |
| @@ -238,7 +238,7 @@ router.post('/delete', requireAdminMiddleware, jsonParser, async (request, respo | ||
| 238 | 238 | router.post('/slugify', requireAdminMiddleware, jsonParser, async (request, response) => { |
| 239 | 239 | try { |
| 240 | 240 | if (!request.body.text) { |
| 241 | 241 | console.logwarn('Slugify failed: Missing required fields'); |
| 242 | 242 | return response.status(400).json({ error: 'Missing required fields' }); |
| 243 | 243 | } |
| 244 | 244 | |
| @@ -58,18 +58,18 @@ router.get('/me', async (request, response) => { | ||
| 58 | 58 | router.post('/change-avatar', jsonParser, async (request, response) => { |
| 59 | 59 | try { |
| 60 | 60 | if (!request.body.handle) { |
| 61 | 61 | console.logwarn('Change avatar failed: Missing required fields'); |
| 62 | 62 | return response.status(400).json({ error: 'Missing required fields' }); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | if (request.body.handle !== request.user.profile.handle && !request.user.profile.admin) { |
| 66 | 66 | console.logerror('Change avatar failed: Unauthorized'); |
| 67 | 67 | return response.status(403).json({ error: 'Unauthorized' }); |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // Avatar is not a data URL or not an empty string |
| 71 | 71 | if (!request.body.avatar.startsWith('data:image/') && request.body.avatar !== '') { |
| 72 | 72 | console.logwarn('Change avatar failed: Invalid data URL'); |
| 73 | 73 | return response.status(400).json({ error: 'Invalid data URL' }); |
| 74 | 74 | } |
| 75 | 75 | |
| @@ -77,7 +77,7 @@ router.post('/change-avatar', jsonParser, async (request, response) => { | ||
| 77 | 77 | const user = await storage.getItem(toKey(request.body.handle)); |
| 78 | 78 | |
| 79 | 79 | if (!user) { |
| 80 | 80 | console.logerror('Change avatar failed: User not found'); |
| 81 | 81 | return response.status(404).json({ error: 'User not found' }); |
| 82 | 82 | } |
| 83 | 83 | |
| @@ -93,12 +93,12 @@ router.post('/change-avatar', jsonParser, async (request, response) => { | ||
| 93 | 93 | router.post('/change-password', jsonParser, async (request, response) => { |
| 94 | 94 | try { |
| 95 | 95 | if (!request.body.handle) { |
| 96 | 96 | console.logwarn('Change password failed: Missing required fields'); |
| 97 | 97 | return response.status(400).json({ error: 'Missing required fields' }); |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | if (request.body.handle !== request.user.profile.handle && !request.user.profile.admin) { |
| 101 | 101 | console.logerror('Change password failed: Unauthorized'); |
| 102 | 102 | return response.status(403).json({ error: 'Unauthorized' }); |
| 103 | 103 | } |
| 104 | 104 | |
| @@ -106,17 +106,17 @@ router.post('/change-password', jsonParser, async (request, response) => { | ||
| 106 | 106 | const user = await storage.getItem(toKey(request.body.handle)); |
| 107 | 107 | |
| 108 | 108 | if (!user) { |
| 109 | 109 | console.logerror('Change password failed: User not found'); |
| 110 | 110 | return response.status(404).json({ error: 'User not found' }); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | if (!user.enabled) { |
| 114 | 114 | console.logerror('Change password failed: User is disabled'); |
| 115 | 115 | return response.status(403).json({ error: 'User is disabled' }); |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | if (!request.user.profile.admin && user.password && user.password !== getPasswordHash(request.body.oldPassword, user.salt)) { |
| 119 | 119 | console.logerror('Change password failed: Incorrect password'); |
| 120 | 120 | return response.status(403).json({ error: 'Incorrect password' }); |
| 121 | 121 | } |
| 122 | 122 | |
| @@ -142,12 +142,12 @@ router.post('/backup', jsonParser, async (request, response) => { | ||
| 142 | 142 | const handle = request.body.handle; |
| 143 | 143 | |
| 144 | 144 | if (!handle) { |
| 145 | 145 | console.logwarn('Backup failed: Missing required fields'); |
| 146 | 146 | return response.status(400).json({ error: 'Missing required fields' }); |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | if (handle !== request.user.profile.handle && !request.user.profile.admin) { |
| 150 | 150 | console.logerror('Backup failed: Unauthorized'); |
| 151 | 151 | return response.status(403).json({ error: 'Unauthorized' }); |
| 152 | 152 | } |
| 153 | 153 | |
| @@ -163,7 +163,7 @@ router.post('/reset-settings', jsonParser, async (request, response) => { | ||
| 163 | 163 | const password = request.body.password; |
| 164 | 164 | |
| 165 | 165 | if (request.user.profile.password && request.user.profile.password !== getPasswordHash(password, request.user.profile.salt)) { |
| 166 | 166 | console.logwarn('Reset settings failed: Incorrect password'); |
| 167 | 167 | return response.status(403).json({ error: 'Incorrect password' }); |
| 168 | 168 | } |
| 169 | 169 | |
| @@ -181,12 +181,12 @@ router.post('/reset-settings', jsonParser, async (request, response) => { | ||
| 181 | 181 | router.post('/change-name', jsonParser, async (request, response) => { |
| 182 | 182 | try { |
| 183 | 183 | if (!request.body.name || !request.body.handle) { |
| 184 | 184 | console.logwarn('Change name failed: Missing required fields'); |
| 185 | 185 | return response.status(400).json({ error: 'Missing required fields' }); |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | if (request.body.handle !== request.user.profile.handle && !request.user.profile.admin) { |
| 189 | 189 | console.logerror('Change name failed: Unauthorized'); |
| 190 | 190 | return response.status(403).json({ error: 'Unauthorized' }); |
| 191 | 191 | } |
| 192 | 192 | |
| @@ -194,7 +194,7 @@ router.post('/change-name', jsonParser, async (request, response) => { | ||
| 194 | 194 | const user = await storage.getItem(toKey(request.body.handle)); |
| 195 | 195 | |
| 196 | 196 | if (!user) { |
| 197 | 197 | console.logwarn('Change name failed: User not found'); |
| 198 | 198 | return response.status(404).json({ error: 'User not found' }); |
| 199 | 199 | } |
| 200 | 200 | |
| @@ -211,9 +211,9 @@ router.post('/change-name', jsonParser, async (request, response) => { | ||
| 211 | 211 | router.post('/reset-step1', jsonParser, async (request, response) => { |
| 212 | 212 | try { |
| 213 | 213 | const resetCode = String(crypto.randomInt(1000, 9999)); |
| 214 | 214 | console.loginfo(); |
| 215 | 215 | console.loginfo(color.magenta(`${request.user.profile.name}, your account reset code is: `) + color.red(resetCode)); |
| 216 | 216 | console.loginfo(); |
| 217 | 217 | RESET_CACHE.set(request.user.profile.handle, resetCode); |
| 218 | 218 | return response.sendStatus(204); |
| 219 | 219 | } catch (error) { |
| @@ -225,23 +225,23 @@ router.post('/reset-step1', jsonParser, async (request, response) => { | ||
| 225 | 225 | router.post('/reset-step2', jsonParser, async (request, response) => { |
| 226 | 226 | try { |
| 227 | 227 | if (!request.body.code) { |
| 228 | 228 | console.logwarn('Recover step 2 failed: Missing required fields'); |
| 229 | 229 | return response.status(400).json({ error: 'Missing required fields' }); |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | if (request.user.profile.password && request.user.profile.password !== getPasswordHash(request.body.password, request.user.profile.salt)) { |
| 233 | 233 | console.logwarn('Recover step 2 failed: Incorrect password'); |
| 234 | 234 | return response.status(400).json({ error: 'Incorrect password' }); |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | const code = RESET_CACHE.get(request.user.profile.handle); |
| 238 | 238 | |
| 239 | 239 | if (!code || code !== request.body.code) { |
| 240 | 240 | console.logwarn('Recover step 2 failed: Incorrect code'); |
| 241 | 241 | return response.status(400).json({ error: 'Incorrect code' }); |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | console.loginfo('Resetting account data:', request.user.profile.handle); |
| 245 | 245 | await fsPromises.rm(request.user.directories.root, { recursive: true, force: true }); |
| 246 | 246 | |
| 247 | 247 | await ensurePublicDirectoriesExist(); |
| @@ -56,7 +56,7 @@ router.post('/list', async (_request, response) => { | ||
| 56 | 56 | router.post('/login', jsonParser, async (request, response) => { |
| 57 | 57 | try { |
| 58 | 58 | if (!request.body.handle) { |
| 59 | 59 | console.logwarn('Login failed: Missing required fields'); |
| 60 | 60 | return response.status(400).json({ error: 'Missing required fields' }); |
| 61 | 61 | } |
| 62 | 62 | |
| @@ -67,17 +67,17 @@ router.post('/login', jsonParser, async (request, response) => { | ||
| 67 | 67 | const user = await storage.getItem(toKey(request.body.handle)); |
| 68 | 68 | |
| 69 | 69 | if (!user) { |
| 70 | 70 | console.logerror('Login failed: User', request.body.handle, 'not found'); |
| 71 | 71 | return response.status(403).json({ error: 'Incorrect credentials' }); |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | if (!user.enabled) { |
| 75 | 75 | console.logwarn('Login failed: User', user.handle, 'is disabled'); |
| 76 | 76 | return response.status(403).json({ error: 'User is disabled' }); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | if (user.password && user.password !== getPasswordHash(request.body.password, user.salt)) { |
| 80 | 80 | console.logwarn('Login failed: Incorrect password for', user.handle); |
| 81 | 81 | return response.status(403).json({ error: 'Incorrect credentials' }); |
| 82 | 82 | } |
| 83 | 83 | |
| @@ -88,11 +88,11 @@ router.post('/login', jsonParser, async (request, response) => { | ||
| 88 | 88 | |
| 89 | 89 | await loginLimiter.delete(ip); |
| 90 | 90 | request.session.handle = user.handle; |
| 91 | 91 | console.loginfo('Login successful:', user.handle, 'from', ip, 'at', new Date().toLocaleString()); |
| 92 | 92 | return response.json({ handle: user.handle }); |
| 93 | 93 | } catch (error) { |
| 94 | 94 | if (error instanceof RateLimiterRes) { |
| 95 | 95 | console.logerror('Login failed: Rate limited from', getIpFromRequest(request)); |
| 96 | 96 | return response.status(429).send({ error: 'Too many attempts. Try again later or recover your password.' }); |
| 97 | 97 | } |
| 98 | 98 | |
| @@ -104,7 +104,7 @@ router.post('/login', jsonParser, async (request, response) => { | ||
| 104 | 104 | router.post('/recover-step1', jsonParser, async (request, response) => { |
| 105 | 105 | try { |
| 106 | 106 | if (!request.body.handle) { |
| 107 | 107 | console.logwarn('Recover step 1 failed: Missing required fields'); |
| 108 | 108 | return response.status(400).json({ error: 'Missing required fields' }); |
| 109 | 109 | } |
| 110 | 110 | |
| @@ -115,24 +115,24 @@ router.post('/recover-step1', jsonParser, async (request, response) => { | ||
| 115 | 115 | const user = await storage.getItem(toKey(request.body.handle)); |
| 116 | 116 | |
| 117 | 117 | if (!user) { |
| 118 | 118 | console.logerror('Recover step 1 failed: User', request.body.handle, 'not found'); |
| 119 | 119 | return response.status(404).json({ error: 'User not found' }); |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | if (!user.enabled) { |
| 123 | 123 | console.logerror('Recover step 1 failed: User', user.handle, 'is disabled'); |
| 124 | 124 | return response.status(403).json({ error: 'User is disabled' }); |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | const mfaCode = String(crypto.randomInt(1000, 9999)); |
| 128 | 128 | console.loginfo(); |
| 129 | 129 | console.loginfo(color.blue(`${user.name}, your password recovery code is: `) + color.magenta(mfaCode)); |
| 130 | 130 | console.loginfo(); |
| 131 | 131 | MFA_CACHE.set(user.handle, mfaCode); |
| 132 | 132 | return response.sendStatus(204); |
| 133 | 133 | } catch (error) { |
| 134 | 134 | if (error instanceof RateLimiterRes) { |
| 135 | 135 | console.logerror('Recover step 1 failed: Rate limited from', getIpFromRequest(request)); |
| 136 | 136 | return response.status(429).send({ error: 'Too many attempts. Try again later or contact your admin.' }); |
| 137 | 137 | } |
| 138 | 138 | |
| @@ -144,7 +144,7 @@ router.post('/recover-step1', jsonParser, async (request, response) => { | ||
| 144 | 144 | router.post('/recover-step2', jsonParser, async (request, response) => { |
| 145 | 145 | try { |
| 146 | 146 | if (!request.body.handle || !request.body.code) { |
| 147 | 147 | console.logwarn('Recover step 2 failed: Missing required fields'); |
| 148 | 148 | return response.status(400).json({ error: 'Missing required fields' }); |
| 149 | 149 | } |
| 150 | 150 | |
| @@ -153,12 +153,12 @@ router.post('/recover-step2', jsonParser, async (request, response) => { | ||
| 153 | 153 | const ip = getIpFromRequest(request); |
| 154 | 154 | |
| 155 | 155 | if (!user) { |
| 156 | 156 | console.logerror('Recover step 2 failed: User', request.body.handle, 'not found'); |
| 157 | 157 | return response.status(404).json({ error: 'User not found' }); |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | if (!user.enabled) { |
| 161 | 161 | console.logwarn('Recover step 2 failed: User', user.handle, 'is disabled'); |
| 162 | 162 | return response.status(403).json({ error: 'User is disabled' }); |
| 163 | 163 | } |
| 164 | 164 | |
| @@ -166,7 +166,7 @@ router.post('/recover-step2', jsonParser, async (request, response) => { | ||
| 166 | 166 | |
| 167 | 167 | if (request.body.code !== mfaCode) { |
| 168 | 168 | await recoverLimiter.consume(ip); |
| 169 | 169 | console.logwarn('Recover step 2 failed: Incorrect code'); |
| 170 | 170 | return response.status(403).json({ error: 'Incorrect code' }); |
| 171 | 171 | } |
| 172 | 172 | |
| @@ -186,7 +186,7 @@ router.post('/recover-step2', jsonParser, async (request, response) => { | ||
| 186 | 186 | return response.sendStatus(204); |
| 187 | 187 | } catch (error) { |
| 188 | 188 | if (error instanceof RateLimiterRes) { |
| 189 | 189 | console.logerror('Recover step 2 failed: Rate limited from', getIpFromRequest(request)); |
| 190 | 190 | return response.status(429).send({ error: 'Too many attempts. Try again later or contact your admin.' }); |
| 191 | 191 | } |
| 192 | 192 | |
| @@ -360,7 +360,7 @@ async function regenerateCorruptedIndexErrorHandler(req, res, error) { | ||
| 360 | 360 | |
| 361 | 361 | if (exists) { |
| 362 | 362 | const path = index.folderPath; |
| 363 | 363 | console.errorwarn(`Corrupted index detected at ${path}, regenerating...`); |
| 364 | 364 | await index.deleteIndex(); |
| 365 | 365 | return res.redirect(307, req.originalUrl + '?regenerated=true'); |
| 366 | 366 | } |
| @@ -474,7 +474,7 @@ router.post('/purge-all', jsonParser, async (req, res) => { | ||
| 474 | 474 | continue; |
| 475 | 475 | } |
| 476 | 476 | await fs.promises.rm(sourcePath, { recursive: true }); |
| 477 | 477 | console.loginfo(`Deleted vector source store at ${sourcePath}`); |
| 478 | 478 | } |
| 479 | 479 | |
| 480 | 480 | return res.sendStatus(200); |
| @@ -498,7 +498,7 @@ router.post('/purge', jsonParser, async (req, res) => { | ||
| 498 | 498 | continue; |
| 499 | 499 | } |
| 500 | 500 | await fs.promises.rm(sourcePath, { recursive: true }); |
| 501 | 501 | console.loginfo(`Deleted vector index at ${sourcePath}`); |
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | return res.sendStatus(200); |
| @@ -25,7 +25,7 @@ export function readWorldInfoFile(directories, worldInfoName, allowDummy) { | ||
| 25 | 25 | const pathToWorldInfo = path.join(directories.worlds, filename); |
| 26 | 26 | |
| 27 | 27 | if (!fs.existsSync(pathToWorldInfo)) { |
| 28 | 28 | console.logerror(`World info file ${filename} doesn't exist.`); |
| 29 | 29 | return dummyObject; |
| 30 | 30 | } |
| 31 | 31 | |
| @@ -43,9 +43,9 @@ export default function initRequestProxy({ enabled, url, bypass }) { | ||
| 43 | 43 | http.globalAgent = proxyAgent; |
| 44 | 44 | https.globalAgent = proxyAgent; |
| 45 | 45 | |
| 46 | 46 | console.loginfo(); |
| 47 | 47 | console.loginfo(color.green(LOG_HEADER), 'Proxy URL is used:', color.blue(url)); |
| 48 | 48 | console.loginfo(); |
| 49 | 49 | } catch (error) { |
| 50 | 50 | console.error(color.red(LOG_HEADER), 'Failed to initialize request proxy:', error); |
| 51 | 51 | } |
| @@ -13,6 +13,7 @@ import _ from 'lodash'; | ||
| 13 | 13 | import yauzl from 'yauzl'; |
| 14 | 14 | import mime from 'mime-types'; |
| 15 | 15 | import { default as simpleGit } from 'simple-git'; |
| 16 | +import { LOG_LEVELS } from './constants.js'; | |
| 16 | 17 | |
| 17 | 18 | /** |
| 18 | 19 | * Parsed config object. |
| @@ -149,6 +150,7 @@ export function getHexString(length) { | ||
| 149 | 150 | */ |
| 150 | 151 | export function formatBytes(bytes) { |
| 151 | 152 | if (bytes === 0) return '0 B'; |
| 153 | + | |
| 152 | 154 | const k = 1024; |
| 153 | 155 | const sizes = ['B', 'KB', 'MB', 'GB']; |
| 154 | 156 | const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| @@ -163,9 +165,7 @@ export function formatBytes(bytes) { | ||
| 163 | 165 | */ |
| 164 | 166 | export async function extractFileFromZipBuffer(archiveBuffer, fileExtension) { |
| 165 | 167 | return await new Promise((resolve, reject) => yauzl.fromBuffer(Buffer.from(archiveBuffer), { lazyEntries: true }, (err, zipfile) => { |
| 166 | 168 | if (err) {reject(err); |
| 167 | - reject(err); | |
| 168 | - } | |
| 169 | 169 | |
| 170 | 170 | zipfile.readEntry(); |
| 171 | 171 | zipfile.on('entry', (entry) => { |
| @@ -285,10 +285,11 @@ export function deepMerge(target, source) { | ||
| 285 | 285 | if (isObject(target) && isObject(source)) { |
| 286 | 286 | Object.keys(source).forEach(key => { |
| 287 | 287 | if (isObject(source[key])) { |
| 288 | 288 | if (!(key in target)) { |
| 289 | 289 | Object.assign(output, { [key]: source[key] }); |
| 290 | 290 | } else { |
| 291 | 291 | output[key] = deepMerge(target[key], source[key]); |
| 292 | + } | |
| 292 | 293 | } else { |
| 293 | 294 | Object.assign(output, { [key]: source[key] }); |
| 294 | 295 | } |
| @@ -467,6 +468,7 @@ export function forwardFetchResponse(from, to) { | ||
| 467 | 468 | |
| 468 | 469 | to.socket.on('close', function () { |
| 469 | 470 | if (from.body instanceof Readable) from.body.destroy(); // Close the remote stream |
| 471 | + | |
| 470 | 472 | to.end(); // End the Express response |
| 471 | 473 | }); |
| 472 | 474 | |
| @@ -693,6 +695,19 @@ export function isValidUrl(url) { | ||
| 693 | 695 | } |
| 694 | 696 | |
| 695 | 697 | /** |
| 698 | + * Setup the minimum log level | |
| 699 | + */ | |
| 700 | +export function setupLogLevel() { | |
| 701 | + const logLevel = getConfigValue('minLogLevel', LOG_LEVELS.DEBUG); | |
| 702 | + | |
| 703 | + globalThis.console.debug = logLevel <= LOG_LEVELS.DEBUG ? console.debug : () => {}; | |
| 704 | + globalThis.console.log = logLevel <= LOG_LEVELS.INFO ? console.log : () => {}; | |
| 705 | + globalThis.console.info = logLevel <= LOG_LEVELS.INFO ? console.info : () => {}; | |
| 706 | + globalThis.console.warn = logLevel <= LOG_LEVELS.WARN ? console.warn : () => {}; | |
| 707 | + globalThis.console.error = logLevel <= LOG_LEVELS.ERROR ? console.error : () => {}; | |
| 708 | +} | |
| 709 | + | |
| 710 | +/** | |
| 696 | 711 | * MemoryLimitedMap class that limits the memory usage of string values. |
| 697 | 712 | */ |
| 698 | 713 | export class MemoryLimitedMap { |