Rewritten minLogLevel feature
| @@ -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 |
| @@ -59,6 +59,7 @@ const hash_derivations = { | ||
| 59 | 59 | // Tulu-3-8B |
| 60 | 60 | // Tulu-3-70B |
| 61 | 61 | 'Tulu' |
| 62 | + , | |
| 62 | 63 | }; |
| 63 | 64 | |
| 64 | 65 | const substr_derivations = { |
| @@ -87,6 +88,6 @@ export async function deriveTemplatesFromChatTemplate(chat_template, hash) { | ||
| 87 | 88 | } |
| 88 | 89 | } |
| 89 | 90 | |
| 90 | 91 | console.logwarn(`Unknown chat template hash: ${hash} for [${chat_template}]`); |
| 91 | 92 | return null; |
| 92 | 93 | } |
| @@ -1,15 +1,13 @@ | ||
| 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 | 4 | import { SlashCommandParser } from '../../../slash-commands/SlashCommandParser.js'; |
| 5 | 5 | import { SlashCommandScopedebounceAsync, warn } from '../../../slash-commands/SlashCommandScopeindex.js'; |
| 6 | -import { debounceAsync, log, warn } from '../index.js'; | |
| 7 | 6 | import { QuickReply } from './QuickReply.js'; |
| 8 | 7 | |
| 9 | 8 | export class QuickReplySet { |
| 10 | 9 | /**@type {QuickReplySet[]}*/ static list = []; |
| 11 | 10 | |
| 12 | - | |
| 13 | 11 | static from(props) { |
| 14 | 12 | props.qrList = []; //props.qrList?.map(it=>QuickReply.from(it)); |
| 15 | 13 | const instance = Object.assign(new this(), props); |
| @@ -24,9 +22,6 @@ export class QuickReplySet { | ||
| 24 | 22 | return this.list.find(it=>it.name == name); |
| 25 | 23 | } |
| 26 | 24 | |
| 27 | - | |
| 28 | - | |
| 29 | - | |
| 30 | 25 | /**@type {string}*/ name; |
| 31 | 26 | /**@type {boolean}*/ disableSend = false; |
| 32 | 27 | /**@type {boolean}*/ placeBeforeInput = false; |
| @@ -34,19 +29,12 @@ export class QuickReplySet { | ||
| 34 | 29 | /**@type {string}*/ color = 'transparent'; |
| 35 | 30 | /**@type {boolean}*/ onlyBorderColor = false; |
| 36 | 31 | /**@type {QuickReply[]}*/ qrList = []; |
| 37 | - | |
| 38 | 32 | /**@type {number}*/ idIndex = 0; |
| 39 | - | |
| 40 | 33 | /**@type {boolean}*/ isDeleted = false; |
| 41 | - | |
| 42 | 34 | /**@type {function}*/ save; |
| 43 | - | |
| 44 | 35 | /**@type {HTMLElement}*/ dom; |
| 45 | 36 | /**@type {HTMLElement}*/ settingsDom; |
| 46 | 37 | |
| 47 | - | |
| 48 | - | |
| 49 | - | |
| 50 | 38 | constructor() { |
| 51 | 39 | this.save = debounceAsync(()=>this.performSave(), 200); |
| 52 | 40 | } |
| @@ -55,9 +43,6 @@ export class QuickReplySet { | ||
| 55 | 43 | this.qrList.forEach(qr=>this.hookQuickReply(qr)); |
| 56 | 44 | } |
| 57 | 45 | |
| 58 | - | |
| 59 | - | |
| 60 | - | |
| 61 | 46 | unrender() { |
| 62 | 47 | this.dom?.remove(); |
| 63 | 48 | this.dom = null; |
| @@ -100,9 +85,6 @@ export class QuickReplySet { | ||
| 100 | 85 | } |
| 101 | 86 | } |
| 102 | 87 | |
| 103 | - | |
| 104 | - | |
| 105 | - | |
| 106 | 88 | renderSettings() { |
| 107 | 89 | if (!this.settingsDom) { |
| 108 | 90 | this.settingsDom = document.createElement('div'); { |
| @@ -123,9 +105,6 @@ export class QuickReplySet { | ||
| 123 | 105 | this.settingsDom.append(qr.renderSettings(idx)); |
| 124 | 106 | } |
| 125 | 107 | |
| 126 | - | |
| 127 | - | |
| 128 | - | |
| 129 | 108 | /** |
| 130 | 109 | * |
| 131 | 110 | * @param {QuickReply} qr |
| @@ -138,6 +117,7 @@ export class QuickReplySet { | ||
| 138 | 117 | closure.scope.setMacro('arg::*', ''); |
| 139 | 118 | return (await closure.execute())?.pipe; |
| 140 | 119 | } |
| 120 | + | |
| 141 | 121 | /** |
| 142 | 122 | * |
| 143 | 123 | * @param {QuickReply} qr The QR to execute. |
| @@ -207,6 +187,7 @@ export class QuickReplySet { | ||
| 207 | 187 | document.querySelector('#send_but').click(); |
| 208 | 188 | } |
| 209 | 189 | } |
| 190 | + | |
| 210 | 191 | /** |
| 211 | 192 | * @param {QuickReply} qr |
| 212 | 193 | * @param {string} [message] - optional altered message to be used |
| @@ -220,9 +201,6 @@ export class QuickReplySet { | ||
| 220 | 201 | }); |
| 221 | 202 | } |
| 222 | 203 | |
| 223 | - | |
| 224 | - | |
| 225 | - | |
| 226 | 204 | addQuickReply(data = {}) { |
| 227 | 205 | const id = Math.max(this.idIndex, this.qrList.reduce((max,qr)=>Math.max(max,qr.id),0)) + 1; |
| 228 | 206 | data.id = |
| @@ -239,6 +217,7 @@ export class QuickReplySet { | ||
| 239 | 217 | this.save(); |
| 240 | 218 | return qr; |
| 241 | 219 | } |
| 220 | + | |
| 242 | 221 | addQuickReplyFromText(qrJson) { |
| 243 | 222 | let data; |
| 244 | 223 | if (qrJson) { |
| @@ -371,7 +350,6 @@ export class QuickReplySet { | ||
| 371 | 350 | this.save(); |
| 372 | 351 | } |
| 373 | 352 | |
| 374 | - | |
| 375 | 353 | toJSON() { |
| 376 | 354 | return { |
| 377 | 355 | version: 2, |
| @@ -386,7 +364,6 @@ export class QuickReplySet { | ||
| 386 | 364 | }; |
| 387 | 365 | } |
| 388 | 366 | |
| 389 | - | |
| 390 | 367 | async performSave() { |
| 391 | 368 | const response = await fetch('/api/quick-replies/save', { |
| 392 | 369 | 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), |
| @@ -67,6 +67,7 @@ import { | ||
| 67 | 67 | forwardFetchResponse, |
| 68 | 68 | removeColorFormatting, |
| 69 | 69 | getSeparator, |
| 70 | + setupLogLevel, | |
| 70 | 71 | } from './src/util.js'; |
| 71 | 72 | import { UPLOADS_DIRECTORY } from './src/constants.js'; |
| 72 | 73 | import { ensureThumbnailCache } from './src/endpoints/thumbnails.js'; |
| @@ -688,8 +689,9 @@ const preSetupTasks = async function () { | ||
| 688 | 689 | function getAutorunHostname() { |
| 689 | 690 | if (autorunHostname === 'auto') { |
| 690 | 691 | if (enableIPv6 && enableIPv4) { |
| 691 | - if (avoidLocalhost) return '[::1]'; | |
| 692 | + return avoidLocalhost | |
| 692 | - return 'localhost'; | |
| 693 | + ? '[::1]' | |
| 694 | + : 'localhost'; | |
| 693 | 695 | } |
| 694 | 696 | |
| 695 | 697 | if (enableIPv6) { |
| @@ -755,6 +757,8 @@ const postSetupTasks = async function (v6Failed, v4Failed) { | ||
| 755 | 757 | } |
| 756 | 758 | } |
| 757 | 759 | } |
| 760 | + | |
| 761 | + setupLogLevel(); | |
| 758 | 762 | }; |
| 759 | 763 | |
| 760 | 764 | /** |
| @@ -413,3 +413,10 @@ export const VLLM_KEYS = [ | ||
| 413 | 413 | 'guided_decoding_backend', |
| 414 | 414 | 'guided_whitespace_pattern', |
| 415 | 415 | ]; |
| 416 | + | |
| 417 | +export const LOG_LEVELS = { | |
| 418 | + DEBUG: 0, | |
| 419 | + INFO: 1, | |
| 420 | + WARN: 2, | |
| 421 | + ERROR: 3, | |
| 422 | +}; | |
| @@ -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.logerror(`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 |
| @@ -224,7 +224,9 @@ router.post('/download', jsonParser, async (request, response) => { | ||
| 224 | 224 | // Delete if previous download failed |
| 225 | 225 | if (fs.existsSync(temp_path)) { |
| 226 | 226 | fs.unlink(temp_path, (err) => { |
| 227 | 227 | if (err) throw err;{ |
| 228 | + throw err; | |
| 229 | + } | |
| 228 | 230 | }); |
| 229 | 231 | } |
| 230 | 232 | const fileStream = fs.createWriteStream(destination, { flags: 'wx' }); |
| @@ -241,13 +243,13 @@ router.post('/download', jsonParser, async (request, response) => { | ||
| 241 | 243 | } |
| 242 | 244 | |
| 243 | 245 | // Move into asset place |
| 244 | 246 | console.debuginfo('Download finished, moving file from', temp_path, 'to', file_path); |
| 245 | 247 | fs.copyFileSync(temp_path, file_path); |
| 246 | 248 | fs.rmSync(temp_path); |
| 247 | 249 | response.sendStatus(200); |
| 248 | 250 | } |
| 249 | 251 | catch (error) { |
| 250 | 252 | console.logerror(error); |
| 251 | 253 | response.sendStatus(500); |
| 252 | 254 | } |
| 253 | 255 | }); |
| @@ -270,7 +272,7 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 270 | 272 | category = i; |
| 271 | 273 | |
| 272 | 274 | if (category === null) { |
| 273 | 275 | console.debugerror('Bad request: unsupported asset category.'); |
| 274 | 276 | return response.sendStatus(400); |
| 275 | 277 | } |
| 276 | 278 | |
| @@ -280,25 +282,27 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 280 | 282 | return response.status(400).send(validation.message); |
| 281 | 283 | |
| 282 | 284 | const file_path = path.join(request.user.directories.assets, category, request.body.filename); |
| 283 | 285 | console.debuginfo('Request received to delete', category, file_path); |
| 284 | 286 | |
| 285 | 287 | try { |
| 286 | 288 | // Delete if previous download failed |
| 287 | 289 | if (fs.existsSync(file_path)) { |
| 288 | 290 | fs.unlink(file_path, (err) => { |
| 289 | 291 | if (err) throw err;{ |
| 292 | + throw err; | |
| 293 | + } | |
| 290 | 294 | }); |
| 291 | 295 | console.debuginfo('Asset deleted.'); |
| 292 | 296 | } |
| 293 | 297 | else { |
| 294 | 298 | console.debugerror('Asset not found.'); |
| 295 | 299 | response.sendStatus(400); |
| 296 | 300 | } |
| 297 | 301 | // Move into asset place |
| 298 | 302 | response.sendStatus(200); |
| 299 | 303 | } |
| 300 | 304 | catch (error) { |
| 301 | 305 | console.logerror(error); |
| 302 | 306 | response.sendStatus(500); |
| 303 | 307 | } |
| 304 | 308 | }); |
| @@ -313,19 +317,24 @@ router.post('/delete', jsonParser, async (request, response) => { | ||
| 313 | 317 | * @returns {void} |
| 314 | 318 | */ |
| 315 | 319 | router.post('/character', jsonParser, async (request, response) => { |
| 316 | 320 | if (request.query.name === undefined) return response.sendStatus(400);{ |
| 321 | + return response.sendStatus(400); | |
| 322 | + } | |
| 323 | + | |
| 317 | 324 | // For backwards compatibility, don't reject invalid character names, just sanitize them |
| 318 | 325 | const name = sanitize(request.query.name.toString()); |
| 319 | 326 | const inputCategory = request.query.category; |
| 320 | 327 | |
| 321 | 328 | // Check category |
| 322 | 329 | let category = null; |
| 323 | 330 | for (let i of VALID_CATEGORIES) { |
| 324 | 331 | if (i == inputCategory) { |
| 325 | 332 | category = i; |
| 333 | + } | |
| 334 | + } | |
| 326 | 335 | |
| 327 | 336 | if (category === null) { |
| 328 | 337 | console.debugerror('Bad request: unsupported asset category.'); |
| 329 | 338 | return response.sendStatus(400); |
| 330 | 339 | } |
| 331 | 340 | |
| @@ -339,7 +348,9 @@ router.post('/character', jsonParser, async (request, response) => { | ||
| 339 | 348 | if (category == 'live2d') { |
| 340 | 349 | const folders = fs.readdirSync(folderPath, { withFileTypes: true }); |
| 341 | 350 | for (const folderInfo of folders) { |
| 342 | 351 | if (!folderInfo.isDirectory()) continue;{ |
| 352 | + continue; | |
| 353 | + } | |
| 343 | 354 | |
| 344 | 355 | const modelFolder = folderInfo.name; |
| 345 | 356 | const live2dModelPath = path.join(folderPath, modelFolder); |
| @@ -364,7 +375,7 @@ router.post('/character', jsonParser, async (request, response) => { | ||
| 364 | 375 | return response.send(output); |
| 365 | 376 | } |
| 366 | 377 | catch (err) { |
| 367 | 378 | console.logerror(err); |
| 368 | 379 | return response.sendStatus(500); |
| 369 | 380 | } |
| 370 | 381 | }); |
| @@ -230,7 +230,7 @@ async function sendScaleRequest(request, response) { | ||
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | const requestPrompt = convertTextCompletionPrompt(request.body.messages); |
| 233 | 233 | console.logdebug('Scale request:', requestPrompt); |
| 234 | 234 | |
| 235 | 235 | try { |
| 236 | 236 | const controller = new AbortController(); |
| @@ -249,18 +249,18 @@ async function sendScaleRequest(request, response) { | ||
| 249 | 249 | }); |
| 250 | 250 | |
| 251 | 251 | if (!generateResponse.ok) { |
| 252 | 252 | console.logerror(`Scale API returned error: ${generateResponse.status} ${generateResponse.statusText} ${await generateResponse.text()}`); |
| 253 | 253 | return response.status(500).send({ error: true }); |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | /** @type {any} */ |
| 257 | 257 | const generateResponseJson = await generateResponse.json(); |
| 258 | 258 | console.logdebug('Scale response:', generateResponseJson); |
| 259 | 259 | |
| 260 | 260 | const reply = { choices: [{ 'message': { 'content': generateResponseJson.output } }] }; |
| 261 | 261 | return response.send(reply); |
| 262 | 262 | } catch (error) { |
| 263 | 263 | console.logerror(error); |
| 264 | 264 | if (!response.headersSent) { |
| 265 | 265 | return response.status(500).send({ error: true }); |
| 266 | 266 | } |
| @@ -277,7 +277,7 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 277 | 277 | const apiKey = request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.MAKERSUITE); |
| 278 | 278 | |
| 279 | 279 | if (!request.body.reverse_proxy && !apiKey) { |
| 280 | 280 | console.logerror('Google AI Studio API key is missing.'); |
| 281 | 281 | return response.status(400).send({ error: true }); |
| 282 | 282 | } |
| 283 | 283 | |
| @@ -328,7 +328,7 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | const body = getGeminiBody(); |
| 331 | 331 | console.logdebug('Google AI Studio request:', body); |
| 332 | 332 | |
| 333 | 333 | try { |
| 334 | 334 | const controller = new AbortController(); |
| @@ -355,14 +355,14 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 355 | 355 | // Pipe remote SSE stream to Express response |
| 356 | 356 | forwardFetchResponse(generateResponse, response); |
| 357 | 357 | } catch (error) { |
| 358 | 358 | console.logerror('Error forwarding streaming response:', error); |
| 359 | 359 | if (!response.headersSent) { |
| 360 | 360 | return response.status(500).send({ error: true }); |
| 361 | 361 | } |
| 362 | 362 | } |
| 363 | 363 | } else { |
| 364 | 364 | if (!generateResponse.ok) { |
| 365 | 365 | console.logerror(`Google AI Studio API returned error: ${generateResponse.status} ${generateResponse.statusText} ${await generateResponse.text()}`); |
| 366 | 366 | return response.status(generateResponse.status).send({ error: true }); |
| 367 | 367 | } |
| 368 | 368 | |
| @@ -380,7 +380,7 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | const responseContent = candidates[0].content ?? candidates[0].output; |
| 383 | 383 | console.logerror('Google AI Studio response:', responseContent); |
| 384 | 384 | |
| 385 | 385 | if (Array.isArray(responseContent?.parts) && isThinking && !showThoughts) { |
| 386 | 386 | responseContent.parts = responseContent.parts.filter(part => !part.thought); |
| @@ -389,7 +389,7 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 389 | 389 | const responseText = typeof responseContent === 'string' ? responseContent : responseContent?.parts?.map(part => part.text)?.join('\n\n'); |
| 390 | 390 | if (!responseText) { |
| 391 | 391 | let message = 'Google AI Studio Candidate text empty'; |
| 392 | 392 | console.logerror(message, generateResponseJson); |
| 393 | 393 | return response.send({ error: { message } }); |
| 394 | 394 | } |
| 395 | 395 | |
| @@ -398,7 +398,7 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 398 | 398 | return response.send(reply); |
| 399 | 399 | } |
| 400 | 400 | } catch (error) { |
| 401 | 401 | console.logerror('Error communicating with Google AI Studio API: ', error); |
| 402 | 402 | if (!response.headersSent) { |
| 403 | 403 | return response.status(500).send({ error: true }); |
| 404 | 404 | } |
| @@ -411,9 +411,12 @@ async function sendMakerSuiteRequest(request, response) { | ||
| 411 | 411 | * @param {express.Response} response Express response |
| 412 | 412 | */ |
| 413 | 413 | async function sendAI21Request(request, response) { |
| 414 | 414 | if (!request.body) return response.sendStatus(400);{ |
| 415 | + return response.sendStatus(400); | |
| 416 | + } | |
| 417 | + | |
| 415 | 418 | const controller = new AbortController(); |
| 416 | 419 | console.logdebug(request.body.messages); |
| 417 | 420 | request.socket.removeAllListeners('close'); |
| 418 | 421 | request.socket.on('close', function () { |
| 419 | 422 | controller.abort(); |
| @@ -439,7 +442,7 @@ async function sendAI21Request(request, response) { | ||
| 439 | 442 | signal: controller.signal, |
| 440 | 443 | }; |
| 441 | 444 | |
| 442 | 445 | console.logdebug('AI21 request:', body); |
| 443 | 446 | |
| 444 | 447 | try { |
| 445 | 448 | const generateResponse = await fetch(API_AI21 + '/chat/completions', options); |
| @@ -448,16 +451,16 @@ async function sendAI21Request(request, response) { | ||
| 448 | 451 | } else { |
| 449 | 452 | if (!generateResponse.ok) { |
| 450 | 453 | const errorText = await generateResponse.text(); |
| 451 | 454 | console.logerror(`AI21 API returned error: ${generateResponse.status} ${generateResponse.statusText} ${errorText}`); |
| 452 | 455 | const errorJson = tryParse(errorText) ?? { error: true }; |
| 453 | 456 | return response.status(500).send(errorJson); |
| 454 | 457 | } |
| 455 | 458 | const generateResponseJson = await generateResponse.json(); |
| 456 | 459 | console.logdebug('AI21 response:', generateResponseJson); |
| 457 | 460 | return response.send(generateResponseJson); |
| 458 | 461 | } |
| 459 | 462 | } catch (error) { |
| 460 | 463 | console.logerror('Error communicating with AI21 API: ', error); |
| 461 | 464 | if (!response.headersSent) { |
| 462 | 465 | response.send({ error: true }); |
| 463 | 466 | } else { |
| @@ -476,7 +479,7 @@ async function sendMistralAIRequest(request, response) { | ||
| 476 | 479 | const apiKey = request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.MISTRALAI); |
| 477 | 480 | |
| 478 | 481 | if (!apiKey) { |
| 479 | 482 | console.logerror('MistralAI API key is missing.'); |
| 480 | 483 | return response.status(400).send({ error: true }); |
| 481 | 484 | } |
| 482 | 485 | |
| @@ -517,7 +520,7 @@ async function sendMistralAIRequest(request, response) { | ||
| 517 | 520 | timeout: 0, |
| 518 | 521 | }; |
| 519 | 522 | |
| 520 | 523 | console.logdebug('MisralAI request:', requestBody); |
| 521 | 524 | |
| 522 | 525 | const generateResponse = await fetch(apiUrl + '/chat/completions', config); |
| 523 | 526 | if (request.body.stream) { |
| @@ -525,16 +528,16 @@ async function sendMistralAIRequest(request, response) { | ||
| 525 | 528 | } else { |
| 526 | 529 | if (!generateResponse.ok) { |
| 527 | 530 | const errorText = await generateResponse.text(); |
| 528 | 531 | console.logerror(`MistralAI API returned error: ${generateResponse.status} ${generateResponse.statusText} ${errorText}`); |
| 529 | 532 | const errorJson = tryParse(errorText) ?? { error: true }; |
| 530 | 533 | return response.status(500).send(errorJson); |
| 531 | 534 | } |
| 532 | 535 | const generateResponseJson = await generateResponse.json(); |
| 533 | 536 | console.logdebug('MistralAI response:', generateResponseJson); |
| 534 | 537 | return response.send(generateResponseJson); |
| 535 | 538 | } |
| 536 | 539 | } catch (error) { |
| 537 | 540 | console.logerror('Error communicating with MistralAI API: ', error); |
| 538 | 541 | if (!response.headersSent) { |
| 539 | 542 | response.send({ error: true }); |
| 540 | 543 | } else { |
| @@ -557,7 +560,7 @@ async function sendCohereRequest(request, response) { | ||
| 557 | 560 | }); |
| 558 | 561 | |
| 559 | 562 | if (!apiKey) { |
| 560 | 563 | console.logerror('Cohere API key is missing.'); |
| 561 | 564 | return response.status(400).send({ error: true }); |
| 562 | 565 | } |
| 563 | 566 | |
| @@ -596,7 +599,7 @@ async function sendCohereRequest(request, response) { | ||
| 596 | 599 | requestBody.safety_mode = 'OFF'; |
| 597 | 600 | } |
| 598 | 601 | |
| 599 | 602 | console.logdebug('Cohere request:', requestBody); |
| 600 | 603 | |
| 601 | 604 | const config = { |
| 602 | 605 | method: 'POST', |
| @@ -618,16 +621,16 @@ async function sendCohereRequest(request, response) { | ||
| 618 | 621 | const generateResponse = await fetch(apiUrl, config); |
| 619 | 622 | if (!generateResponse.ok) { |
| 620 | 623 | const errorText = await generateResponse.text(); |
| 621 | 624 | console.logerror(`Cohere API returned error: ${generateResponse.status} ${generateResponse.statusText} ${errorText}`); |
| 622 | 625 | const errorJson = tryParse(errorText) ?? { error: true }; |
| 623 | 626 | return response.status(500).send(errorJson); |
| 624 | 627 | } |
| 625 | 628 | const generateResponseJson = await generateResponse.json(); |
| 626 | 629 | console.logdebug('Cohere response:', generateResponseJson); |
| 627 | 630 | return response.send(generateResponseJson); |
| 628 | 631 | } |
| 629 | 632 | } catch (error) { |
| 630 | 633 | console.logerror('Error communicating with Cohere API: ', error); |
| 631 | 634 | if (!response.headersSent) { |
| 632 | 635 | response.send({ error: true }); |
| 633 | 636 | } else { |
| @@ -684,12 +687,12 @@ router.post('/status', jsonParser, async function (request, response_getstatus_o | ||
| 684 | 687 | api_key_openai = readSecret(request.user.directories, SECRET_KEYS.DEEPSEEK); |
| 685 | 688 | headers = {}; |
| 686 | 689 | } else { |
| 687 | 690 | console.logerror('This chat completion source is not supported yet.'); |
| 688 | 691 | return response_getstatus_openai.status(400).send({ error: true }); |
| 689 | 692 | } |
| 690 | 693 | |
| 691 | 694 | if (!api_key_openai && !request.body.reverse_proxy && request.body.chat_completion_source !== CHAT_COMPLETION_SOURCES.CUSTOM) { |
| 692 | 695 | console.logerror('Chat Completion API key is missing.'); |
| 693 | 696 | return response_getstatus_openai.status(400).send({ error: true }); |
| 694 | 697 | } |
| 695 | 698 | |
| @@ -724,23 +727,23 @@ router.post('/status', jsonParser, async function (request, response_getstatus_o | ||
| 724 | 727 | }; |
| 725 | 728 | }); |
| 726 | 729 | |
| 727 | 730 | console.loginfo('Available OpenRouter models:', models); |
| 728 | 731 | } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.MISTRALAI) { |
| 729 | 732 | const models = data?.data; |
| 730 | 733 | console.loginfo(models); |
| 731 | 734 | } else { |
| 732 | 735 | const models = data?.data; |
| 733 | 736 | |
| 734 | 737 | if (Array.isArray(models)) { |
| 735 | 738 | const modelIds = models.filter(x => x && typeof x === 'object').map(x => x.id).sort(); |
| 736 | 739 | console.loginfo('Available models:', modelIds); |
| 737 | 740 | } else { |
| 738 | 741 | console.logwarn('Chat Completion endpoint did not return a list of models.'); |
| 739 | 742 | } |
| 740 | 743 | } |
| 741 | 744 | } |
| 742 | 745 | else { |
| 743 | 746 | console.logerror('Chat Completion status check failed. Either Access Token is incorrect or API endpoint is down.'); |
| 744 | 747 | response_getstatus_openai.send({ error: true, can_bypass: true, data: { data: [] } }); |
| 745 | 748 | } |
| 746 | 749 | } catch (e) { |
| @@ -755,8 +758,7 @@ router.post('/status', jsonParser, async function (request, response_getstatus_o | ||
| 755 | 758 | }); |
| 756 | 759 | |
| 757 | 760 | router.post('/bias', jsonParser, async function (request, response) { |
| 758 | 761 | if (!request.body || !Array.isArray(request.body)) return response.sendStatus(400); |
| 759 | - return response.sendStatus(400); | |
| 760 | 762 | |
| 761 | 763 | try { |
| 762 | 764 | const result = {}; |
| @@ -773,7 +775,7 @@ router.post('/bias', jsonParser, async function (request, response) { | ||
| 773 | 775 | const tokenizer = getSentencepiceTokenizer(model); |
| 774 | 776 | const instance = await tokenizer?.get(); |
| 775 | 777 | if (!instance) { |
| 776 | 778 | console.warnerror('Tokenizer not initialized:', model); |
| 777 | 779 | return response.send({}); |
| 778 | 780 | } |
| 779 | 781 | encodeFunction = (text) => new Uint32Array(instance.encodeIds(text)); |
| @@ -922,7 +924,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 922 | 924 | mergeObjectWithYaml(headers, request.body.custom_include_headers); |
| 923 | 925 | |
| 924 | 926 | if (request.body.custom_prompt_post_processing) { |
| 925 | 927 | console.loginfo('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing); |
| 926 | 928 | request.body.messages = postProcessPrompt( |
| 927 | 929 | request.body.messages, |
| 928 | 930 | request.body.custom_prompt_post_processing, |
| @@ -967,12 +969,12 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 967 | 969 | |
| 968 | 970 | request.body.messages = postProcessPrompt(request.body.messages, 'deepseek', getPromptNames(request)); |
| 969 | 971 | } else { |
| 970 | 972 | console.logerror('This chat completion source is not supported yet.'); |
| 971 | 973 | return response.status(400).send({ error: true }); |
| 972 | 974 | } |
| 973 | 975 | |
| 974 | 976 | if (!apiKey && !request.body.reverse_proxy && request.body.chat_completion_source !== CHAT_COMPLETION_SOURCES.CUSTOM) { |
| 975 | 977 | console.logerror('OpenAI API key is missing.'); |
| 976 | 978 | return response.status(400).send({ error: true }); |
| 977 | 979 | } |
| 978 | 980 | |
| @@ -1032,7 +1034,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1032 | 1034 | signal: controller.signal, |
| 1033 | 1035 | }; |
| 1034 | 1036 | |
| 1035 | 1037 | console.logdebug(requestBody); |
| 1036 | 1038 | |
| 1037 | 1039 | makeRequest(config, response, request); |
| 1038 | 1040 | |
| @@ -1049,7 +1051,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1049 | 1051 | const fetchResponse = await fetch(endpointUrl, config); |
| 1050 | 1052 | |
| 1051 | 1053 | if (request.body.stream) { |
| 1052 | 1054 | console.loginfo('Streaming request in progress'); |
| 1053 | 1055 | forwardFetchResponse(fetchResponse, response); |
| 1054 | 1056 | return; |
| 1055 | 1057 | } |
| @@ -1058,10 +1060,10 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1058 | 1060 | /** @type {any} */ |
| 1059 | 1061 | let json = await fetchResponse.json(); |
| 1060 | 1062 | response.send(json); |
| 1061 | 1063 | console.logdebug(json); |
| 1062 | 1064 | console.logdebug(json?.choices?.[0]?.message); |
| 1063 | 1065 | } else if (fetchResponse.status === 429 && retries > 0) { |
| 1064 | 1066 | console.logwarn(`Out of quota, retrying in ${Math.round(timeout / 1000)}s`); |
| 1065 | 1067 | setTimeout(() => { |
| 1066 | 1068 | timeout *= 2; |
| 1067 | 1069 | makeRequest(config, response, request, retries - 1, timeout); |
| @@ -1070,7 +1072,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1070 | 1072 | await handleErrorResponse(fetchResponse); |
| 1071 | 1073 | } |
| 1072 | 1074 | } catch (error) { |
| 1073 | 1075 | console.logerror('Generation failed', error); |
| 1074 | 1076 | const message = error.code === 'ECONNREFUSED' |
| 1075 | 1077 | ? `Connection refused: ${error.message}` |
| 1076 | 1078 | : error.message || 'Unknown error occurred'; |
| @@ -1092,7 +1094,7 @@ router.post('/generate', jsonParser, function (request, response) { | ||
| 1092 | 1094 | |
| 1093 | 1095 | const message = errorResponse.statusText || 'Unknown error occurred'; |
| 1094 | 1096 | const quota_error = errorResponse.status === 429 && errorData?.error?.type === 'insufficient_quota'; |
| 1095 | 1097 | console.logerror('Chat completion request error: ', message, responseText); |
| 1096 | 1098 | |
| 1097 | 1099 | if (!response.headersSent) { |
| 1098 | 1100 | 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.logerror(`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,17 @@ 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.loginfo('Transcribing audio with KoboldCpp', server); |
| 206 | + console.debug(server); | |
| 206 | 207 | |
| 207 | 208 | const fileBase64 = fs.readFileSync(request.file.path).toString('base64'); |
| 208 | 209 | fs.rmSync(request.file.path); |
| @@ -226,12 +227,12 @@ router.post('/transcribe-audio', urlencodedParser, async function (request, resp | ||
| 226 | 227 | |
| 227 | 228 | if (!result.ok) { |
| 228 | 229 | const text = await result.text(); |
| 229 | 230 | console.logerror('KoboldCpp request failed', result.statusText, text); |
| 230 | 231 | return response.status(500).send(text); |
| 231 | 232 | } |
| 232 | 233 | |
| 233 | 234 | const data = await result.json(); |
| 234 | 235 | console.logdebug('KoboldCpp transcription response', data); |
| 235 | 236 | return response.json(data); |
| 236 | 237 | } catch (error) { |
| 237 | 238 | 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,8 @@ 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.loginfo('Trying to connect to API:', request.body); |
| 105 | + console.debug(request.body); | |
| 105 | 106 | const baseUrl = trimV1(request.body.api_server); |
| 106 | 107 | |
| 107 | 108 | const args = { |
| @@ -123,6 +124,7 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 123 | 124 | case TEXTGEN_TYPES.LLAMACPP: |
| 124 | 125 | case TEXTGEN_TYPES.INFERMATICAI: |
| 125 | 126 | case TEXTGEN_TYPES.OPENROUTER: |
| 127 | + case TEXTGEN_TYPES.FEATHERLESS: | |
| 126 | 128 | url += '/v1/models'; |
| 127 | 129 | break; |
| 128 | 130 | case TEXTGEN_TYPES.DREAMGEN: |
| @@ -140,9 +142,6 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 140 | 142 | case TEXTGEN_TYPES.OLLAMA: |
| 141 | 143 | url += '/api/tags'; |
| 142 | 144 | break; |
| 143 | - case TEXTGEN_TYPES.FEATHERLESS: | |
| 144 | - url += '/v1/models'; | |
| 145 | - break; | |
| 146 | 145 | case TEXTGEN_TYPES.HUGGINGFACE: |
| 147 | 146 | url += '/info'; |
| 148 | 147 | break; |
| @@ -152,7 +151,7 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 152 | 151 | const isPossiblyLmStudio = modelsReply.headers.get('x-powered-by') === 'Express'; |
| 153 | 152 | |
| 154 | 153 | if (!modelsReply.ok) { |
| 155 | 154 | console.logerror('Models endpoint is offline.'); |
| 156 | 155 | return response.sendStatus(400); |
| 157 | 156 | } |
| 158 | 157 | |
| @@ -173,12 +172,12 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 173 | 172 | } |
| 174 | 173 | |
| 175 | 174 | if (!Array.isArray(data.data)) { |
| 176 | 175 | console.logerror('Models response is not an array.'); |
| 177 | 176 | return response.sendStatus(400); |
| 178 | 177 | } |
| 179 | 178 | |
| 180 | 179 | const modelIds = data.data.map(x => x.id); |
| 181 | 180 | console.loginfo('Models available:', modelIds); |
| 182 | 181 | |
| 183 | 182 | // Set result to the first model ID |
| 184 | 183 | result = modelIds[0] || 'Valid'; |
| @@ -191,7 +190,7 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 191 | 190 | if (modelInfoReply.ok) { |
| 192 | 191 | /** @type {any} */ |
| 193 | 192 | const modelInfo = await modelInfoReply.json(); |
| 194 | 193 | console.logdebug('Ooba model info:', modelInfo); |
| 195 | 194 | |
| 196 | 195 | const modelName = modelInfo?.model_name; |
| 197 | 196 | result = modelName || result; |
| @@ -208,7 +207,7 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 208 | 207 | if (modelInfoReply.ok) { |
| 209 | 208 | /** @type {any} */ |
| 210 | 209 | const modelInfo = await modelInfoReply.json(); |
| 211 | 210 | console.logdebug('Tabby model info:', modelInfo); |
| 212 | 211 | |
| 213 | 212 | const modelName = modelInfo?.id; |
| 214 | 213 | result = modelName || result; |
| @@ -230,7 +229,9 @@ router.post('/status', jsonParser, async function (request, response) { | ||
| 230 | 229 | }); |
| 231 | 230 | |
| 232 | 231 | router.post('/props', jsonParser, async function (request, response) { |
| 233 | 232 | if (!request.body.api_server) return response.sendStatus(400);{ |
| 233 | + return response.sendStatus(400); | |
| 234 | + } | |
| 234 | 235 | |
| 235 | 236 | try { |
| 236 | 237 | const baseUrl = trimV1(request.body.api_server); |
| @@ -255,7 +256,7 @@ router.post('/props', jsonParser, async function (request, response) { | ||
| 255 | 256 | props['chat_template'] = props['chat_template'].slice(0, -1) + '\n'; |
| 256 | 257 | } |
| 257 | 258 | props['chat_template_hash'] = createHash('sha256').update(props['chat_template']).digest('hex'); |
| 258 | 259 | console.logdebug(`Model properties: ${JSON.stringify(props)}`); |
| 259 | 260 | return response.send(props); |
| 260 | 261 | } catch (error) { |
| 261 | 262 | console.error(error); |
| @@ -264,7 +265,9 @@ router.post('/props', jsonParser, async function (request, response) { | ||
| 264 | 265 | }); |
| 265 | 266 | |
| 266 | 267 | router.post('/generate', jsonParser, async function (request, response) { |
| 267 | 268 | if (!request.body) return response.sendStatus(400);{ |
| 269 | + return response.sendStatus(400); | |
| 270 | + } | |
| 268 | 271 | |
| 269 | 272 | try { |
| 270 | 273 | if (request.body.api_server.indexOf('localhost') !== -1) { |
| @@ -273,7 +276,7 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 273 | 276 | |
| 274 | 277 | const apiType = request.body.api_type; |
| 275 | 278 | const baseUrl = request.body.api_server; |
| 276 | 279 | console.logdebug(request.body); |
| 277 | 280 | |
| 278 | 281 | const controller = new AbortController(); |
| 279 | 282 | request.socket.removeAllListeners('close'); |
| @@ -399,7 +402,7 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 399 | 402 | if (completionsReply.ok) { |
| 400 | 403 | /** @type {any} */ |
| 401 | 404 | const data = await completionsReply.json(); |
| 402 | 405 | console.logdebug('Endpoint response:', data); |
| 403 | 406 | |
| 404 | 407 | // Map InfermaticAI response to OAI completions format |
| 405 | 408 | if (apiType === TEXTGEN_TYPES.INFERMATICAI) { |
| @@ -411,24 +414,20 @@ router.post('/generate', jsonParser, async function (request, response) { | ||
| 411 | 414 | const text = await completionsReply.text(); |
| 412 | 415 | const errorBody = { error: true, status: completionsReply.status, response: text }; |
| 413 | 416 | |
| 414 | 417 | ifreturn (!response.headersSent) { |
| 415 | 418 | return? response.send(errorBody); |
| 416 | - } | |
| 419 | + : response.end(); | |
| 417 | - | |
| 418 | - return response.end(); | |
| 419 | 420 | } |
| 420 | 421 | } |
| 421 | 422 | } catch (error) { |
| 422 | 423 | const status = error?.status ?? error?.code ?? 'UNKNOWN'; |
| 423 | 424 | const text = error?.error ?? error?.statusText ?? error?.message ?? 'Unknown error on /generate endpoint'; |
| 424 | 425 | let value = { error: true, status: status, response: text }; |
| 425 | 426 | console.logerror('Endpoint error:', error); |
| 426 | 427 | |
| 427 | 428 | ifreturn (!response.headersSent) { |
| 428 | 429 | return? response.send(value); |
| 429 | - } | |
| 430 | + : response.end(); | |
| 430 | - | |
| 431 | - return response.end(); | |
| 432 | 431 | } |
| 433 | 432 | }); |
| 434 | 433 | |
| @@ -436,7 +435,9 @@ const ollama = express.Router(); | ||
| 436 | 435 | |
| 437 | 436 | ollama.post('/download', jsonParser, async function (request, response) { |
| 438 | 437 | try { |
| 439 | 438 | if (!request.body.name || !request.body.api_server) return response.sendStatus(400);{ |
| 439 | + return response.sendStatus(400); | |
| 440 | + } | |
| 440 | 441 | |
| 441 | 442 | const name = request.body.name; |
| 442 | 443 | const url = String(request.body.api_server).replace(/\/$/, ''); |
| @@ -451,7 +452,7 @@ ollama.post('/download', jsonParser, async function (request, response) { | ||
| 451 | 452 | }); |
| 452 | 453 | |
| 453 | 454 | if (!fetchResponse.ok) { |
| 454 | 455 | console.logerror('Download error:', fetchResponse.status, fetchResponse.statusText); |
| 455 | 456 | return response.status(fetchResponse.status).send({ error: true }); |
| 456 | 457 | } |
| 457 | 458 | |
| @@ -468,7 +469,7 @@ ollama.post('/caption-image', jsonParser, async function (request, response) { | ||
| 468 | 469 | return response.sendStatus(400); |
| 469 | 470 | } |
| 470 | 471 | |
| 471 | 472 | console.logdebug('Ollama caption request:', request.body); |
| 472 | 473 | const baseUrl = trimV1(request.body.server_url); |
| 473 | 474 | |
| 474 | 475 | const fetchResponse = await fetch(`${baseUrl}/api/generate`, { |
| @@ -483,18 +484,18 @@ ollama.post('/caption-image', jsonParser, async function (request, response) { | ||
| 483 | 484 | }); |
| 484 | 485 | |
| 485 | 486 | if (!fetchResponse.ok) { |
| 486 | 487 | console.logerror('Ollama caption error:', fetchResponse.status, fetchResponse.statusText); |
| 487 | 488 | return response.status(500).send({ error: true }); |
| 488 | 489 | } |
| 489 | 490 | |
| 490 | 491 | /** @type {any} */ |
| 491 | 492 | const data = await fetchResponse.json(); |
| 492 | 493 | console.logdebug('Ollama caption response:', data); |
| 493 | 494 | |
| 494 | 495 | const caption = data?.response || ''; |
| 495 | 496 | |
| 496 | 497 | if (!caption) { |
| 497 | 498 | console.logerror('Ollama caption is empty.'); |
| 498 | 499 | return response.status(500).send({ error: true }); |
| 499 | 500 | } |
| 500 | 501 | |
| @@ -513,7 +514,7 @@ llamacpp.post('/caption-image', jsonParser, async function (request, response) { | ||
| 513 | 514 | return response.sendStatus(400); |
| 514 | 515 | } |
| 515 | 516 | |
| 516 | 517 | console.logdebug('LlamaCpp caption request:', request.body); |
| 517 | 518 | const baseUrl = trimV1(request.body.server_url); |
| 518 | 519 | |
| 519 | 520 | const fetchResponse = await fetch(`${baseUrl}/completion`, { |
| @@ -529,18 +530,18 @@ llamacpp.post('/caption-image', jsonParser, async function (request, response) { | ||
| 529 | 530 | }); |
| 530 | 531 | |
| 531 | 532 | if (!fetchResponse.ok) { |
| 532 | 533 | console.logerror('LlamaCpp caption error:', fetchResponse.status, fetchResponse.statusText); |
| 533 | 534 | return response.status(500).send({ error: true }); |
| 534 | 535 | } |
| 535 | 536 | |
| 536 | 537 | /** @type {any} */ |
| 537 | 538 | const data = await fetchResponse.json(); |
| 538 | 539 | console.logdebug('LlamaCpp caption response:', data); |
| 539 | 540 | |
| 540 | 541 | const caption = data?.content || ''; |
| 541 | 542 | |
| 542 | 543 | if (!caption) { |
| 543 | 544 | console.logerror('LlamaCpp caption is empty.'); |
| 544 | 545 | return response.status(500).send({ error: true }); |
| 545 | 546 | } |
| 546 | 547 | |
| @@ -558,7 +559,7 @@ llamacpp.post('/props', jsonParser, async function (request, response) { | ||
| 558 | 559 | return response.sendStatus(400); |
| 559 | 560 | } |
| 560 | 561 | |
| 561 | 562 | console.logdebug('LlamaCpp props request:', request.body); |
| 562 | 563 | const baseUrl = trimV1(request.body.server_url); |
| 563 | 564 | |
| 564 | 565 | const fetchResponse = await fetch(`${baseUrl}/props`, { |
| @@ -566,12 +567,12 @@ llamacpp.post('/props', jsonParser, async function (request, response) { | ||
| 566 | 567 | }); |
| 567 | 568 | |
| 568 | 569 | if (!fetchResponse.ok) { |
| 569 | 570 | console.logerror('LlamaCpp props error:', fetchResponse.status, fetchResponse.statusText); |
| 570 | 571 | return response.status(500).send({ error: true }); |
| 571 | 572 | } |
| 572 | 573 | |
| 573 | 574 | const data = await fetchResponse.json(); |
| 574 | 575 | console.logdebug('LlamaCpp props response:', data); |
| 575 | 576 | |
| 576 | 577 | return response.send(data); |
| 577 | 578 | |
| @@ -590,7 +591,7 @@ llamacpp.post('/slots', jsonParser, async function (request, response) { | ||
| 590 | 591 | return response.sendStatus(400); |
| 591 | 592 | } |
| 592 | 593 | |
| 593 | 594 | console.logdebug('LlamaCpp slots request:', request.body); |
| 594 | 595 | const baseUrl = trimV1(request.body.server_url); |
| 595 | 596 | |
| 596 | 597 | let fetchResponse; |
| @@ -616,12 +617,12 @@ llamacpp.post('/slots', jsonParser, async function (request, response) { | ||
| 616 | 617 | } |
| 617 | 618 | |
| 618 | 619 | if (!fetchResponse.ok) { |
| 619 | 620 | console.logerror('LlamaCpp slots error:', fetchResponse.status, fetchResponse.statusText); |
| 620 | 621 | return response.status(500).send({ error: true }); |
| 621 | 622 | } |
| 622 | 623 | |
| 623 | 624 | const data = await fetchResponse.json(); |
| 624 | 625 | console.logdebug('LlamaCpp slots response:', data); |
| 625 | 626 | |
| 626 | 627 | return response.send(data); |
| 627 | 628 | |
| @@ -659,14 +660,14 @@ tabby.post('/download', jsonParser, async function (request, response) { | ||
| 659 | 660 | return response.status(403).send({ error: true }); |
| 660 | 661 | } |
| 661 | 662 | } else { |
| 662 | 663 | console.logerror('API Permission error:', permissionResponse.status, permissionResponse.statusText); |
| 663 | 664 | return response.status(permissionResponse.status).send({ error: true }); |
| 664 | 665 | } |
| 665 | 666 | |
| 666 | 667 | const fetchResponse = await fetch(`${baseUrl}/v1/download`, args); |
| 667 | 668 | |
| 668 | 669 | if (!fetchResponse.ok) { |
| 669 | 670 | console.logerror('Download error:', fetchResponse.status, fetchResponse.statusText); |
| 670 | 671 | return response.status(fetchResponse.status).send({ error: true }); |
| 671 | 672 | } |
| 672 | 673 | |
| @@ -26,7 +26,7 @@ router.post('/delete', jsonParser, function (request, response) { | ||
| 26 | 26 | const fileName = path.join(request.user.directories.backgrounds, sanitize(request.body.bg)); |
| 27 | 27 | |
| 28 | 28 | if (!fs.existsSync(fileName)) { |
| 29 | 29 | console.logerror('BG file not found'); |
| 30 | 30 | return response.sendStatus(400); |
| 31 | 31 | } |
| 32 | 32 | |
| @@ -42,12 +42,12 @@ router.post('/rename', jsonParser, function (request, response) { | ||
| 42 | 42 | const newFileName = path.join(request.user.directories.backgrounds, sanitize(request.body.new_bg)); |
| 43 | 43 | |
| 44 | 44 | if (!fs.existsSync(oldFileName)) { |
| 45 | 45 | console.logerror('BG file not found'); |
| 46 | 46 | return response.sendStatus(400); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | if (fs.existsSync(newFileName)) { |
| 50 | 50 | console.logerror('New BG file already exists'); |
| 51 | 51 | return response.sendStatus(400); |
| 52 | 52 | } |
| 53 | 53 | |
| @@ -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.logerror('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.logdebug('Image caption:', text); |
| 24 | 24 | |
| 25 | 25 | return res.json({ caption: text }); |
| 26 | 26 | } catch (error) { |
| @@ -90,7 +90,7 @@ async function writeCharacterData(inputFile, data, outputFile, request, crop = u | ||
| 90 | 90 | writeFileAtomicSync(outputImagePath, outputImage); |
| 91 | 91 | return true; |
| 92 | 92 | } catch (err) { |
| 93 | 93 | console.logerror(err); |
| 94 | 94 | return false; |
| 95 | 95 | } |
| 96 | 96 | } |
| @@ -159,7 +159,7 @@ async function tryReadImage(imgPath, crop) { | ||
| 159 | 159 | } |
| 160 | 160 | // If it's an unsupported type of image (APNG) - just read the file as buffer |
| 161 | 161 | catch (error) { |
| 162 | 162 | console.logerror(`Failed to read image: ${imgPath}`, error); |
| 163 | 163 | return fs.readFileSync(imgPath); |
| 164 | 164 | } |
| 165 | 165 | } |
| @@ -222,12 +222,12 @@ const processCharacter = async (item, directories) => { | ||
| 222 | 222 | return character; |
| 223 | 223 | } |
| 224 | 224 | catch (err) { |
| 225 | 225 | console.logerror(`Could not process character: ${item}`); |
| 226 | 226 | |
| 227 | 227 | if (err instanceof SyntaxError) { |
| 228 | 228 | console.logerror(`${item} does not contain a valid JSON object.`); |
| 229 | 229 | } else { |
| 230 | 230 | console.logerror('An unexpected error occurred: ', err); |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | return { |
| @@ -315,7 +315,7 @@ function readFromV2(char) { | ||
| 315 | 315 | }; |
| 316 | 316 | |
| 317 | 317 | _.forEach(fieldMappings, (v2Path, charField) => { |
| 318 | 318 | //console.loginfo(`Migrating field: ${charField} from ${v2Path}`); |
| 319 | 319 | const v2Value = _.get(char.data, v2Path); |
| 320 | 320 | if (_.isUndefined(v2Value)) { |
| 321 | 321 | let defaultValue = undefined; |
| @@ -330,15 +330,15 @@ function readFromV2(char) { | ||
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | if (!_.isUndefined(defaultValue)) { |
| 333 | 333 | //console.debugwarn(`Spec v2 extension data missing for field: ${charField}, using default value: ${defaultValue}`); |
| 334 | 334 | char[charField] = defaultValue; |
| 335 | 335 | } else { |
| 336 | 336 | console.debugwarn(`Char ${char['name']} has Spec v2 data missing for unknown field: ${charField}`); |
| 337 | 337 | return; |
| 338 | 338 | } |
| 339 | 339 | } |
| 340 | 340 | if (!_.isUndefined(char[charField]) && !_.isUndefined(v2Value) && String(char[charField]) !== String(v2Value)) { |
| 341 | 341 | console.debugwarn(`Char ${char['name']} has Spec v2 data mismatch with Spec v1 for field: ${charField}`, char[charField], v2Value); |
| 342 | 342 | } |
| 343 | 343 | char[charField] = v2Value; |
| 344 | 344 | }); |
| @@ -435,7 +435,7 @@ function charaFormatData(data, directories) { | ||
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | } catch { |
| 438 | 438 | console.debugwarn(`Failed to read world info file: ${data.world}. Character book will not be available.`); |
| 439 | 439 | } |
| 440 | 440 | } |
| 441 | 441 | |
| @@ -445,7 +445,7 @@ function charaFormatData(data, directories) { | ||
| 445 | 445 | // Deep merge the extensions object |
| 446 | 446 | _.set(char, 'data.extensions', deepMerge(char.data.extensions, extensions)); |
| 447 | 447 | } catch { |
| 448 | 448 | console.debugwarn(`Failed to parse extensions JSON: ${data.extensions}`); |
| 449 | 449 | } |
| 450 | 450 | } |
| 451 | 451 | |
| @@ -519,7 +519,7 @@ async function importFromYaml(uploadPath, context, preservedFileName) { | ||
| 519 | 519 | const fileText = fs.readFileSync(uploadPath, 'utf8'); |
| 520 | 520 | fs.rmSync(uploadPath); |
| 521 | 521 | const yamlData = yaml.parse(fileText); |
| 522 | 522 | console.loginfo('Importing from YAML'); |
| 523 | 523 | yamlData.name = sanitize(yamlData.name); |
| 524 | 524 | const fileName = preservedFileName || getPngName(yamlData.name, context.request.user.directories); |
| 525 | 525 | let char = convertToV2({ |
| @@ -552,7 +552,7 @@ async function importFromYaml(uploadPath, context, preservedFileName) { | ||
| 552 | 552 | async function importFromCharX(uploadPath, { request }, preservedFileName) { |
| 553 | 553 | const data = fs.readFileSync(uploadPath).buffer; |
| 554 | 554 | fs.rmSync(uploadPath); |
| 555 | 555 | console.loginfo('Importing from CharX'); |
| 556 | 556 | const cardBuffer = await extractFileFromZipBuffer(data, 'card.json'); |
| 557 | 557 | |
| 558 | 558 | if (!cardBuffer) { |
| @@ -601,7 +601,7 @@ async function importFromJson(uploadPath, { request }, preservedFileName) { | ||
| 601 | 601 | let jsonData = JSON.parse(data); |
| 602 | 602 | |
| 603 | 603 | if (jsonData.spec !== undefined) { |
| 604 | 604 | console.loginfo(`Importing from ${jsonData.spec} json`); |
| 605 | 605 | importRisuSprites(request.user.directories, jsonData); |
| 606 | 606 | unsetFavFlag(jsonData); |
| 607 | 607 | jsonData = readFromV2(jsonData); |
| @@ -611,7 +611,7 @@ async function importFromJson(uploadPath, { request }, preservedFileName) { | ||
| 611 | 611 | const result = await writeCharacterData(defaultAvatarPath, char, pngName, request); |
| 612 | 612 | return result ? pngName : ''; |
| 613 | 613 | } else if (jsonData.name !== undefined) { |
| 614 | 614 | console.loginfo('Importing from v1 json'); |
| 615 | 615 | jsonData.name = sanitize(jsonData.name); |
| 616 | 616 | if (jsonData.creator_notes) { |
| 617 | 617 | jsonData.creator_notes = jsonData.creator_notes.replace('Creator\'s notes go here.', ''); |
| @@ -637,7 +637,7 @@ async function importFromJson(uploadPath, { request }, preservedFileName) { | ||
| 637 | 637 | const result = await writeCharacterData(defaultAvatarPath, charJSON, pngName, request); |
| 638 | 638 | return result ? pngName : ''; |
| 639 | 639 | } else if (jsonData.char_name !== undefined) {//json Pygmalion notepad |
| 640 | 640 | console.loginfo('Importing from gradio json'); |
| 641 | 641 | jsonData.char_name = sanitize(jsonData.char_name); |
| 642 | 642 | if (jsonData.creator_notes) { |
| 643 | 643 | jsonData.creator_notes = jsonData.creator_notes.replace('Creator\'s notes go here.', ''); |
| @@ -684,7 +684,7 @@ async function importFromPng(uploadPath, { request }, preservedFileName) { | ||
| 684 | 684 | const pngName = preservedFileName || getPngName(jsonData.name, request.user.directories); |
| 685 | 685 | |
| 686 | 686 | if (jsonData.spec !== undefined) { |
| 687 | 687 | console.loginfo(`Found a ${jsonData.spec} character file.`); |
| 688 | 688 | importRisuSprites(request.user.directories, jsonData); |
| 689 | 689 | unsetFavFlag(jsonData); |
| 690 | 690 | jsonData = readFromV2(jsonData); |
| @@ -694,7 +694,7 @@ async function importFromPng(uploadPath, { request }, preservedFileName) { | ||
| 694 | 694 | fs.unlinkSync(uploadPath); |
| 695 | 695 | return result ? pngName : ''; |
| 696 | 696 | } else if (jsonData.name !== undefined) { |
| 697 | 697 | console.loginfo('Found a v1 character file.'); |
| 698 | 698 | |
| 699 | 699 | if (jsonData.creator_notes) { |
| 700 | 700 | jsonData.creator_notes = jsonData.creator_notes.replace('Creator\'s notes go here.', ''); |
| @@ -853,7 +853,7 @@ router.post('/edit', urlencodedParser, async function (request, response) { | ||
| 853 | 853 | * @returns {void} |
| 854 | 854 | */ |
| 855 | 855 | router.post('/edit-attribute', jsonParser, async function (request, response) { |
| 856 | 856 | console.logdebug(request.body); |
| 857 | 857 | if (!request.body) { |
| 858 | 858 | console.error('Error: no response body detected'); |
| 859 | 859 | return response.status(400).send('Error: no response body detected'); |
| @@ -921,7 +921,7 @@ router.post('/merge-attributes', jsonParser, async function (request, response) | ||
| 921 | 921 | await writeCharacterData(avatarPath, JSON.stringify(character), targetImg, request); |
| 922 | 922 | response.sendStatus(200); |
| 923 | 923 | } else { |
| 924 | 924 | console.logerror(validator.lastValidationError); |
| 925 | 925 | response.status(400).send({ message: `Validation failed for ${character.name}`, error: validator.lastValidationError }); |
| 926 | 926 | } |
| 927 | 927 | } catch (exception) { |
| @@ -1043,7 +1043,7 @@ router.post('/chats', jsonParser, async function (request, response) { | ||
| 1043 | 1043 | const fileSizeInKB = `${(stats.size / 1024).toFixed(2)}kb`; |
| 1044 | 1044 | |
| 1045 | 1045 | if (stats.size === 0) { |
| 1046 | 1046 | console.logwarn(`Found an empty chat file: ${pathToFile}`); |
| 1047 | 1047 | res({}); |
| 1048 | 1048 | return; |
| 1049 | 1049 | } |
| @@ -1075,7 +1075,7 @@ router.post('/chats', jsonParser, async function (request, response) { | ||
| 1075 | 1075 | |
| 1076 | 1076 | res(chatData); |
| 1077 | 1077 | } else { |
| 1078 | 1078 | console.logwarn('Found an invalid or corrupted chat file:', pathToFile); |
| 1079 | 1079 | res({}); |
| 1080 | 1080 | } |
| 1081 | 1081 | } |
| @@ -1088,7 +1088,7 @@ router.post('/chats', jsonParser, async function (request, response) { | ||
| 1088 | 1088 | |
| 1089 | 1089 | return response.send(validFiles); |
| 1090 | 1090 | } catch (error) { |
| 1091 | 1091 | console.logerror(error); |
| 1092 | 1092 | return response.send({ error: true }); |
| 1093 | 1093 | } |
| 1094 | 1094 | }); |
| @@ -1155,7 +1155,7 @@ router.post('/import', urlencodedParser, async function (request, response) { | ||
| 1155 | 1155 | |
| 1156 | 1156 | response.send({ file_name: fileName }); |
| 1157 | 1157 | } catch (err) { |
| 1158 | 1158 | console.logerror(err); |
| 1159 | 1159 | response.send({ error: true }); |
| 1160 | 1160 | } |
| 1161 | 1161 | }); |
| @@ -1163,14 +1163,13 @@ router.post('/import', urlencodedParser, async function (request, response) { | ||
| 1163 | 1163 | router.post('/duplicate', jsonParser, async function (request, response) { |
| 1164 | 1164 | try { |
| 1165 | 1165 | if (!request.body.avatar_url) { |
| 1166 | 1166 | console.logerror('avatar URL not found in request body'); |
| 1167 | 1167 | console.logdebug(request.body); |
| 1168 | 1168 | return response.sendStatus(400); |
| 1169 | 1169 | } |
| 1170 | 1170 | let filename = path.join(request.user.directories.characters, sanitize(request.body.avatar_url)); |
| 1171 | 1171 | if (!fs.existsSync(filename)) { |
| 1172 | 1172 | console.logerror('file for dupe not found', filename); |
| 1173 | - console.log(filename); | |
| 1174 | 1173 | return response.sendStatus(404); |
| 1175 | 1174 | } |
| 1176 | 1175 | let suffix = 1; |
| @@ -1198,7 +1197,7 @@ router.post('/duplicate', jsonParser, async function (request, response) { | ||
| 1198 | 1197 | } |
| 1199 | 1198 | |
| 1200 | 1199 | fs.copyFileSync(filename, newFilename); |
| 1201 | 1200 | console.loginfo(`${filename} was copied to ${newFilename}`); |
| 1202 | 1201 | response.send({ path: path.parse(newFilename).base }); |
| 1203 | 1202 | } |
| 1204 | 1203 | catch (error) { |
| @@ -49,7 +49,7 @@ function backupChat(directory, name, chat) { | ||
| 49 | 49 | |
| 50 | 50 | removeOldBackups(directory, 'chat_', maxTotalChatBackups); |
| 51 | 51 | } catch (err) { |
| 52 | 52 | console.logerror(`Could not backup chat for ${name}`, err); |
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 | |
| @@ -277,8 +277,8 @@ router.post('/save', jsonParser, function (request, response) { | ||
| 277 | 277 | getBackupFunction(request.user.profile.handle)(request.user.directories.backups, directoryName, jsonlData); |
| 278 | 278 | return response.send({ result: 'ok' }); |
| 279 | 279 | } catch (error) { |
| 280 | 280 | responseconsole.senderror(error); |
| 281 | 281 | return consoleresponse.logsend(error); |
| 282 | 282 | } |
| 283 | 283 | }); |
| 284 | 284 | |
| @@ -330,17 +330,17 @@ router.post('/rename', jsonParser, async function (request, response) { | ||
| 330 | 330 | const pathToOriginalFile = path.join(pathToFolder, sanitize(request.body.original_file)); |
| 331 | 331 | const pathToRenamedFile = path.join(pathToFolder, sanitize(request.body.renamed_file)); |
| 332 | 332 | const sanitizedFileName = path.parse(pathToRenamedFile).name; |
| 333 | 333 | console.loginfo('Old chat name', pathToOriginalFile); |
| 334 | 334 | console.loginfo('New chat name', pathToRenamedFile); |
| 335 | 335 | |
| 336 | 336 | if (!fs.existsSync(pathToOriginalFile) || fs.existsSync(pathToRenamedFile)) { |
| 337 | 337 | console.logerror('Either Source or Destination files are not available'); |
| 338 | 338 | return response.status(400).send({ error: true }); |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | fs.copyFileSync(pathToOriginalFile, pathToRenamedFile); |
| 342 | 342 | fs.rmSync(pathToOriginalFile); |
| 343 | 343 | console.loginfo('Successfully renamed.'); |
| 344 | 344 | return response.send({ ok: true, sanitizedFileName }); |
| 345 | 345 | }); |
| 346 | 346 | |
| @@ -351,12 +351,12 @@ router.post('/delete', jsonParser, function (request, response) { | ||
| 351 | 351 | const chatFileExists = fs.existsSync(filePath); |
| 352 | 352 | |
| 353 | 353 | if (!chatFileExists) { |
| 354 | 354 | console.logerror(`Chat file not found '${filePath}'`); |
| 355 | 355 | return response.sendStatus(400); |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | fs.rmSync(filePath); |
| 359 | 359 | console.loginfo('`Deleted chat file: ' + ${filePath}`); |
| 360 | 360 | return response.send('ok'); |
| 361 | 361 | }); |
| 362 | 362 | |
| @@ -373,7 +373,7 @@ router.post('/export', jsonParser, async function (request, response) { | ||
| 373 | 373 | const errorMessage = { |
| 374 | 374 | message: `Could not find JSONL file to export. Source chat file: ${filename}.`, |
| 375 | 375 | }; |
| 376 | 376 | console.logerror(errorMessage.message); |
| 377 | 377 | return response.status(404).json(errorMessage); |
| 378 | 378 | } |
| 379 | 379 | try { |
| @@ -386,14 +386,14 @@ router.post('/export', jsonParser, async function (request, response) { | ||
| 386 | 386 | result: rawFile, |
| 387 | 387 | }; |
| 388 | 388 | |
| 389 | 389 | console.loginfo(`Chat exported as ${exportfilename}`); |
| 390 | 390 | return response.status(200).json(successMessage); |
| 391 | 391 | } catch (err) { |
| 392 | 392 | console.error(err); |
| 393 | 393 | const errorMessage = { |
| 394 | 394 | message: `Could not read JSONL file to export. Source chat file: ${filename}.`, |
| 395 | 395 | }; |
| 396 | 396 | console.logerror(errorMessage.message); |
| 397 | 397 | return response.status(500).json(errorMessage); |
| 398 | 398 | } |
| 399 | 399 | } |
| @@ -420,12 +420,11 @@ router.post('/export', jsonParser, async function (request, response) { | ||
| 420 | 420 | message: `Chat saved to ${exportfilename}`, |
| 421 | 421 | result: buffer, |
| 422 | 422 | }; |
| 423 | 423 | console.loginfo(`Chat exported as ${exportfilename}`); |
| 424 | 424 | return response.status(200).json(successMessage); |
| 425 | 425 | }); |
| 426 | 426 | } catch (err) { |
| 427 | 427 | console.logerror('chat export failed.', err); |
| 428 | - console.log(err); | |
| 429 | 428 | return response.sendStatus(400); |
| 430 | 429 | } |
| 431 | 430 | }); |
| @@ -482,7 +481,7 @@ router.post('/import', urlencodedParser, function (request, response) { | ||
| 482 | 481 | } else if (Array.isArray(jsonData.messages)) { // Agnai's format |
| 483 | 482 | importFunc = importAgnaiChat; |
| 484 | 483 | } else { // Unknown format |
| 485 | 484 | console.logerror('Incorrect chat format .json'); |
| 486 | 485 | return response.send({ error: true }); |
| 487 | 486 | } |
| 488 | 487 | |
| @@ -510,7 +509,7 @@ router.post('/import', urlencodedParser, function (request, response) { | ||
| 510 | 509 | const jsonData = JSON.parse(header); |
| 511 | 510 | |
| 512 | 511 | if (!(jsonData.user_name !== undefined || jsonData.name !== undefined)) { |
| 513 | 512 | console.logerror('Incorrect chat format .jsonl'); |
| 514 | 513 | return response.send({ error: true }); |
| 515 | 514 | } |
| 516 | 515 | |
| @@ -616,7 +615,7 @@ router.post('/search', jsonParser, function (request, response) { | ||
| 616 | 615 | break; |
| 617 | 616 | } |
| 618 | 617 | } catch (error) { |
| 619 | 618 | console.errorwarn(groupFile, 'group file is corrupted:', error); |
| 620 | 619 | } |
| 621 | 620 | } |
| 622 | 621 | |
| @@ -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.logdebug('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 |
| @@ -364,12 +364,12 @@ router.post('/generate-image', jsonParser, async (request, response) => { | ||
| 364 | 364 | return response.sendStatus(400); |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | console.logdebug('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.loginfo('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.logerror('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.logerror('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.logerror('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.logerror(`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.logdebug('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logdebug('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.logerror('No OpenAI key found'); |
| 188 | 188 | return response.sendStatus(400); |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | if (!request.file) { |
| 192 | 192 | console.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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 | }); |
| @@ -94,25 +94,25 @@ router.post('/serpapi', jsonParser, async (request, response) => { | ||
| 94 | 94 | const key = readSecret(request.user.directories, SECRET_KEYS.SERPAPI); |
| 95 | 95 | |
| 96 | 96 | if (!key) { |
| 97 | 97 | console.logerror('No SerpApi key found'); |
| 98 | 98 | return response.sendStatus(400); |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | const { query } = request.body; |
| 102 | 102 | const result = await fetch(`https://serpapi.com/search.json?q=${encodeURIComponent(query)}&api_key=${key}`); |
| 103 | 103 | |
| 104 | 104 | console.logdebug('SerpApi query', query); |
| 105 | 105 | |
| 106 | 106 | if (!result.ok) { |
| 107 | 107 | const text = await result.text(); |
| 108 | 108 | console.logerror('SerpApi request failed', result.statusText, text); |
| 109 | 109 | return response.status(500).send(text); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | const data = await result.json(); |
| 113 | 113 | return response.json(data); |
| 114 | 114 | } catch (error) { |
| 115 | 115 | console.logerror(error); |
| 116 | 116 | return response.sendStatus(500); |
| 117 | 117 | } |
| 118 | 118 | }); |
| @@ -128,7 +128,7 @@ router.post('/transcript', jsonParser, async (request, response) => { | ||
| 128 | 128 | const json = request.body.json; |
| 129 | 129 | |
| 130 | 130 | if (!id) { |
| 131 | 131 | console.logerror('Id is required for /transcript'); |
| 132 | 132 | return response.sendStatus(400); |
| 133 | 133 | } |
| 134 | 134 | |
| @@ -153,7 +153,7 @@ router.post('/transcript', jsonParser, async (request, response) => { | ||
| 153 | 153 | throw error; |
| 154 | 154 | } |
| 155 | 155 | } catch (error) { |
| 156 | 156 | console.logerror(error); |
| 157 | 157 | return response.sendStatus(500); |
| 158 | 158 | } |
| 159 | 159 | }); |
| @@ -163,17 +163,17 @@ router.post('/searxng', jsonParser, async (request, response) => { | ||
| 163 | 163 | const { baseUrl, query, preferences } = request.body; |
| 164 | 164 | |
| 165 | 165 | if (!baseUrl || !query) { |
| 166 | 166 | console.logerror('Missing required parameters for /searxng'); |
| 167 | 167 | return response.sendStatus(400); |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | console.logdebug('SearXNG query', baseUrl, query); |
| 171 | 171 | |
| 172 | 172 | const mainPageUrl = new URL(baseUrl); |
| 173 | 173 | const mainPageRequest = await fetch(mainPageUrl, { headers: visitHeaders }); |
| 174 | 174 | |
| 175 | 175 | if (!mainPageRequest.ok) { |
| 176 | 176 | console.logerror('SearXNG request failed', mainPageRequest.statusText); |
| 177 | 177 | return response.sendStatus(500); |
| 178 | 178 | } |
| 179 | 179 | |
| @@ -197,14 +197,14 @@ router.post('/searxng', jsonParser, async (request, response) => { | ||
| 197 | 197 | |
| 198 | 198 | if (!searchResult.ok) { |
| 199 | 199 | const text = await searchResult.text(); |
| 200 | 200 | console.logerror('SearXNG request failed', searchResult.statusText, text); |
| 201 | 201 | return response.sendStatus(500); |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | const data = await searchResult.text(); |
| 205 | 205 | return response.send(data); |
| 206 | 206 | } catch (error) { |
| 207 | 207 | console.logerror('SearXNG request failed', error); |
| 208 | 208 | return response.sendStatus(500); |
| 209 | 209 | } |
| 210 | 210 | }); |
| @@ -214,7 +214,7 @@ router.post('/tavily', jsonParser, async (request, response) => { | ||
| 214 | 214 | const apiKey = readSecret(request.user.directories, SECRET_KEYS.TAVILY); |
| 215 | 215 | |
| 216 | 216 | if (!apiKey) { |
| 217 | 217 | console.logerror('No Tavily key found'); |
| 218 | 218 | return response.sendStatus(400); |
| 219 | 219 | } |
| 220 | 220 | |
| @@ -241,18 +241,18 @@ router.post('/tavily', jsonParser, async (request, response) => { | ||
| 241 | 241 | body: JSON.stringify(body), |
| 242 | 242 | }); |
| 243 | 243 | |
| 244 | 244 | console.logdebug('Tavily query', query); |
| 245 | 245 | |
| 246 | 246 | if (!result.ok) { |
| 247 | 247 | const text = await result.text(); |
| 248 | 248 | console.logerror('Tavily request failed', result.statusText, text); |
| 249 | 249 | return response.status(500).send(text); |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | const data = await result.json(); |
| 253 | 253 | return response.json(data); |
| 254 | 254 | } catch (error) { |
| 255 | 255 | console.logerror(error); |
| 256 | 256 | return response.sendStatus(500); |
| 257 | 257 | } |
| 258 | 258 | }); |
| @@ -262,7 +262,7 @@ router.post('/visit', jsonParser, async (request, response) => { | ||
| 262 | 262 | const url = request.body.url; |
| 263 | 263 | |
| 264 | 264 | if (!url) { |
| 265 | 265 | console.logerror('No url provided for /visit'); |
| 266 | 266 | return response.sendStatus(400); |
| 267 | 267 | } |
| 268 | 268 | |
| @@ -289,29 +289,29 @@ router.post('/visit', jsonParser, async (request, response) => { | ||
| 289 | 289 | throw new Error('Invalid hostname'); |
| 290 | 290 | } |
| 291 | 291 | } catch (error) { |
| 292 | 292 | console.logerror('Invalid url provided for /visit', url); |
| 293 | 293 | return response.sendStatus(400); |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | console.loginfo('Visiting web URL', url); |
| 297 | 297 | |
| 298 | 298 | const result = await fetch(url, { headers: visitHeaders }); |
| 299 | 299 | |
| 300 | 300 | if (!result.ok) { |
| 301 | 301 | console.logerror(`Visit failed ${result.status} ${result.statusText}`); |
| 302 | 302 | return response.sendStatus(500); |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | const contentType = String(result.headers.get('content-type')); |
| 306 | 306 | if (!contentType.includes('text/html')) { |
| 307 | 307 | console.logerror(`Visit failed, content-type is ${contentType}, expected text/html`); |
| 308 | 308 | return response.sendStatus(500); |
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | const text = await result.text(); |
| 312 | 312 | return response.send(text); |
| 313 | 313 | } catch (error) { |
| 314 | 314 | console.logerror(error); |
| 315 | 315 | return response.sendStatus(500); |
| 316 | 316 | } |
| 317 | 317 | }); |
| @@ -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 | |
| @@ -104,7 +104,7 @@ function readPresetsFromDirectory(directoryPath, options = {}) { | ||
| 104 | 104 | fileNames.push(removeFileExtension ? item.replace(/\.[^/.]+$/, '') : item); |
| 105 | 105 | } catch { |
| 106 | 106 | // skip |
| 107 | 107 | console.logwarn(`${item} is not a valid JSON`); |
| 108 | 108 | } |
| 109 | 109 | }); |
| 110 | 110 | |
| @@ -119,7 +119,7 @@ async function backupSettings() { | ||
| 119 | 119 | backupUserSettings(handle, true); |
| 120 | 120 | } |
| 121 | 121 | } catch (err) { |
| 122 | 122 | console.logerror('Could not backup settings file', err); |
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | |
| @@ -201,7 +201,7 @@ router.post('/save', jsonParser, function (request, response) { | ||
| 201 | 201 | triggerAutoSave(request.user.profile.handle); |
| 202 | 202 | response.send({ result: 'ok' }); |
| 203 | 203 | } catch (err) { |
| 204 | 204 | console.logerror(err); |
| 205 | 205 | response.send(err); |
| 206 | 206 | } |
| 207 | 207 | }); |
| @@ -291,7 +291,7 @@ router.post('/get-snapshots', jsonParser, async (request, response) => { | ||
| 291 | 291 | |
| 292 | 292 | response.json(result); |
| 293 | 293 | } catch (error) { |
| 294 | 294 | console.logerror(error); |
| 295 | 295 | response.sendStatus(500); |
| 296 | 296 | } |
| 297 | 297 | }); |
| @@ -315,7 +315,7 @@ router.post('/load-snapshot', jsonParser, async (request, response) => { | ||
| 315 | 315 | |
| 316 | 316 | response.send(content); |
| 317 | 317 | } catch (error) { |
| 318 | 318 | console.logerror(error); |
| 319 | 319 | response.sendStatus(500); |
| 320 | 320 | } |
| 321 | 321 | }); |
| @@ -325,7 +325,7 @@ router.post('/make-snapshot', jsonParser, async (request, response) => { | ||
| 325 | 325 | backupUserSettings(request.user.profile.handle, false); |
| 326 | 326 | response.sendStatus(204); |
| 327 | 327 | } catch (error) { |
| 328 | 328 | console.logerror(error); |
| 329 | 329 | response.sendStatus(500); |
| 330 | 330 | } |
| 331 | 331 | }); |
| @@ -351,7 +351,7 @@ router.post('/restore-snapshot', jsonParser, async (request, response) => { | ||
| 351 | 351 | |
| 352 | 352 | response.sendStatus(204); |
| 353 | 353 | } catch (error) { |
| 354 | 354 | console.logerror(error); |
| 355 | 355 | response.sendStatus(500); |
| 356 | 356 | } |
| 357 | 357 | }); |
| @@ -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.logdebug(`Execution duration: ${(end - start) / 1000} seconds`); |
| 47 | 47 | console.logdebug('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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); |
| @@ -442,7 +442,7 @@ export function getTiktokenTokenizer(model) { | ||
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | const tokenizer = tiktoken.encoding_for_model(model); |
| 445 | 445 | console.loginfo('Instantiated the tokenizer for', model); |
| 446 | 446 | tokenizersCache[model] = tokenizer; |
| 447 | 447 | return tokenizer; |
| 448 | 448 | } |
| @@ -489,7 +489,7 @@ function createSentencepieceEncodingHandler(tokenizer) { | ||
| 489 | 489 | const chunks = instance?.encodePieces(text); |
| 490 | 490 | return response.send({ ids, count, chunks }); |
| 491 | 491 | } catch (error) { |
| 492 | 492 | console.logerror(error); |
| 493 | 493 | return response.send({ ids: [], count: 0, chunks: [] }); |
| 494 | 494 | } |
| 495 | 495 | }; |
| @@ -520,7 +520,7 @@ function createSentencepieceDecodingHandler(tokenizer) { | ||
| 520 | 520 | const text = chunks.join(''); |
| 521 | 521 | return response.send({ text, chunks }); |
| 522 | 522 | } catch (error) { |
| 523 | 523 | console.logerror(error); |
| 524 | 524 | return response.send({ text: '', chunks: [] }); |
| 525 | 525 | } |
| 526 | 526 | }; |
| @@ -549,7 +549,7 @@ function createTiktokenEncodingHandler(modelId) { | ||
| 549 | 549 | const chunks = await getTiktokenChunks(tokenizer, tokens); |
| 550 | 550 | return response.send({ ids: tokens, count: tokens.length, chunks }); |
| 551 | 551 | } catch (error) { |
| 552 | 552 | console.logerror(error); |
| 553 | 553 | return response.send({ ids: [], count: 0, chunks: [] }); |
| 554 | 554 | } |
| 555 | 555 | }; |
| @@ -578,7 +578,7 @@ function createTiktokenDecodingHandler(modelId) { | ||
| 578 | 578 | const text = new TextDecoder().decode(textBytes); |
| 579 | 579 | return response.send({ text }); |
| 580 | 580 | } catch (error) { |
| 581 | 581 | console.logerror(error); |
| 582 | 582 | return response.send({ text: '' }); |
| 583 | 583 | } |
| 584 | 584 | }; |
| @@ -608,7 +608,7 @@ function createWebTokenizerEncodingHandler(tokenizer) { | ||
| 608 | 608 | const chunks = getWebTokenizersChunks(instance, tokens); |
| 609 | 609 | return response.send({ ids: tokens, count: tokens.length, chunks }); |
| 610 | 610 | } catch (error) { |
| 611 | 611 | console.logerror(error); |
| 612 | 612 | return response.send({ ids: [], count: 0, chunks: [] }); |
| 613 | 613 | } |
| 614 | 614 | }; |
| @@ -639,7 +639,7 @@ function createWebTokenizerDecodingHandler(tokenizer) { | ||
| 639 | 639 | const text = instance.decode(new Int32Array(ids)); |
| 640 | 640 | return response.send({ text, chunks }); |
| 641 | 641 | } catch (error) { |
| 642 | 642 | console.logerror(error); |
| 643 | 643 | return response.send({ text: '', chunks: [] }); |
| 644 | 644 | } |
| 645 | 645 | }; |
| @@ -739,7 +739,7 @@ router.post('/openai/encode', jsonParser, async function (req, res) { | ||
| 739 | 739 | const handler = createTiktokenEncodingHandler(model); |
| 740 | 740 | return handler(req, res); |
| 741 | 741 | } catch (error) { |
| 742 | 742 | console.logerror(error); |
| 743 | 743 | return res.send({ ids: [], count: 0, chunks: [] }); |
| 744 | 744 | } |
| 745 | 745 | }); |
| @@ -807,7 +807,7 @@ router.post('/openai/decode', jsonParser, async function (req, res) { | ||
| 807 | 807 | const handler = createTiktokenDecodingHandler(model); |
| 808 | 808 | return handler(req, res); |
| 809 | 809 | } catch (error) { |
| 810 | 810 | console.logerror(error); |
| 811 | 811 | return res.send({ text: '' }); |
| 812 | 812 | } |
| 813 | 813 | }); |
| @@ -946,7 +946,7 @@ router.post('/remote/kobold/count', jsonParser, async function (request, respons | ||
| 946 | 946 | const result = await fetch(url, args); |
| 947 | 947 | |
| 948 | 948 | if (!result.ok) { |
| 949 | 949 | console.logerror(`API returned error: ${result.status} ${result.statusText}`); |
| 950 | 950 | return response.send({ error: true }); |
| 951 | 951 | } |
| 952 | 952 | |
| @@ -955,7 +955,7 @@ router.post('/remote/kobold/count', jsonParser, async function (request, respons | ||
| 955 | 955 | const ids = data['ids'] ?? []; |
| 956 | 956 | return response.send({ count, ids }); |
| 957 | 957 | } catch (error) { |
| 958 | 958 | console.logerror(error); |
| 959 | 959 | return response.send({ error: true }); |
| 960 | 960 | } |
| 961 | 961 | }); |
| @@ -1010,7 +1010,7 @@ router.post('/remote/textgenerationwebui/encode', jsonParser, async function (re | ||
| 1010 | 1010 | const result = await fetch(url, args); |
| 1011 | 1011 | |
| 1012 | 1012 | if (!result.ok) { |
| 1013 | 1013 | console.logerror(`API returned error: ${result.status} ${result.statusText}`); |
| 1014 | 1014 | return response.send({ error: true }); |
| 1015 | 1015 | } |
| 1016 | 1016 | |
| @@ -1020,7 +1020,7 @@ router.post('/remote/textgenerationwebui/encode', jsonParser, async function (re | ||
| 1020 | 1020 | |
| 1021 | 1021 | return response.send({ count, ids }); |
| 1022 | 1022 | } catch (error) { |
| 1023 | 1023 | console.logerror(error); |
| 1024 | 1024 | return response.send({ error: true }); |
| 1025 | 1025 | } |
| 1026 | 1026 | }); |
| @@ -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.logwarn('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('Slugify failed: Missing required fields'); |
| 242 | 242 | return response.status(400).json({ error: 'Missing required fields' }); |
| 243 | 243 | } |
| 244 | 244 | |
| @@ -57,18 +57,18 @@ router.get('/me', async (request, response) => { | ||
| 57 | 57 | router.post('/change-avatar', jsonParser, async (request, response) => { |
| 58 | 58 | try { |
| 59 | 59 | if (!request.body.handle) { |
| 60 | 60 | console.logerror('Change avatar failed: Missing required fields'); |
| 61 | 61 | return response.status(400).json({ error: 'Missing required fields' }); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | if (request.body.handle !== request.user.profile.handle && !request.user.profile.admin) { |
| 65 | 65 | console.logerror('Change avatar failed: Unauthorized'); |
| 66 | 66 | return response.status(403).json({ error: 'Unauthorized' }); |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // Avatar is not a data URL or not an empty string |
| 70 | 70 | if (!request.body.avatar.startsWith('data:image/') && request.body.avatar !== '') { |
| 71 | 71 | console.logerror('Change avatar failed: Invalid data URL'); |
| 72 | 72 | return response.status(400).json({ error: 'Invalid data URL' }); |
| 73 | 73 | } |
| 74 | 74 | |
| @@ -76,7 +76,7 @@ router.post('/change-avatar', jsonParser, async (request, response) => { | ||
| 76 | 76 | const user = await storage.getItem(toKey(request.body.handle)); |
| 77 | 77 | |
| 78 | 78 | if (!user) { |
| 79 | 79 | console.logerror('Change avatar failed: User not found'); |
| 80 | 80 | return response.status(404).json({ error: 'User not found' }); |
| 81 | 81 | } |
| 82 | 82 | |
| @@ -92,12 +92,12 @@ router.post('/change-avatar', jsonParser, async (request, response) => { | ||
| 92 | 92 | router.post('/change-password', jsonParser, async (request, response) => { |
| 93 | 93 | try { |
| 94 | 94 | if (!request.body.handle) { |
| 95 | 95 | console.logerror('Change password failed: Missing required fields'); |
| 96 | 96 | return response.status(400).json({ error: 'Missing required fields' }); |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | if (request.body.handle !== request.user.profile.handle && !request.user.profile.admin) { |
| 100 | 100 | console.logerror('Change password failed: Unauthorized'); |
| 101 | 101 | return response.status(403).json({ error: 'Unauthorized' }); |
| 102 | 102 | } |
| 103 | 103 | |
| @@ -105,17 +105,17 @@ router.post('/change-password', jsonParser, async (request, response) => { | ||
| 105 | 105 | const user = await storage.getItem(toKey(request.body.handle)); |
| 106 | 106 | |
| 107 | 107 | if (!user) { |
| 108 | 108 | console.logerror('Change password failed: User not found'); |
| 109 | 109 | return response.status(404).json({ error: 'User not found' }); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | if (!user.enabled) { |
| 113 | 113 | console.logerror('Change password failed: User is disabled'); |
| 114 | 114 | return response.status(403).json({ error: 'User is disabled' }); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | if (!request.user.profile.admin && user.password && user.password !== getPasswordHash(request.body.oldPassword, user.salt)) { |
| 118 | 118 | console.logerror('Change password failed: Incorrect password'); |
| 119 | 119 | return response.status(403).json({ error: 'Incorrect password' }); |
| 120 | 120 | } |
| 121 | 121 | |
| @@ -141,12 +141,12 @@ router.post('/backup', jsonParser, async (request, response) => { | ||
| 141 | 141 | const handle = request.body.handle; |
| 142 | 142 | |
| 143 | 143 | if (!handle) { |
| 144 | 144 | console.logerror('Backup failed: Missing required fields'); |
| 145 | 145 | return response.status(400).json({ error: 'Missing required fields' }); |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | if (handle !== request.user.profile.handle && !request.user.profile.admin) { |
| 149 | 149 | console.logerror('Backup failed: Unauthorized'); |
| 150 | 150 | return response.status(403).json({ error: 'Unauthorized' }); |
| 151 | 151 | } |
| 152 | 152 | |
| @@ -162,7 +162,7 @@ router.post('/reset-settings', jsonParser, async (request, response) => { | ||
| 162 | 162 | const password = request.body.password; |
| 163 | 163 | |
| 164 | 164 | if (request.user.profile.password && request.user.profile.password !== getPasswordHash(password, request.user.profile.salt)) { |
| 165 | 165 | console.logerror('Reset settings failed: Incorrect password'); |
| 166 | 166 | return response.status(403).json({ error: 'Incorrect password' }); |
| 167 | 167 | } |
| 168 | 168 | |
| @@ -180,12 +180,12 @@ router.post('/reset-settings', jsonParser, async (request, response) => { | ||
| 180 | 180 | router.post('/change-name', jsonParser, async (request, response) => { |
| 181 | 181 | try { |
| 182 | 182 | if (!request.body.name || !request.body.handle) { |
| 183 | 183 | console.logerror('Change name failed: Missing required fields'); |
| 184 | 184 | return response.status(400).json({ error: 'Missing required fields' }); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | if (request.body.handle !== request.user.profile.handle && !request.user.profile.admin) { |
| 188 | 188 | console.logerror('Change name failed: Unauthorized'); |
| 189 | 189 | return response.status(403).json({ error: 'Unauthorized' }); |
| 190 | 190 | } |
| 191 | 191 | |
| @@ -193,7 +193,7 @@ router.post('/change-name', jsonParser, async (request, response) => { | ||
| 193 | 193 | const user = await storage.getItem(toKey(request.body.handle)); |
| 194 | 194 | |
| 195 | 195 | if (!user) { |
| 196 | 196 | console.logerror('Change name failed: User not found'); |
| 197 | 197 | return response.status(404).json({ error: 'User not found' }); |
| 198 | 198 | } |
| 199 | 199 | |
| @@ -210,9 +210,9 @@ router.post('/change-name', jsonParser, async (request, response) => { | ||
| 210 | 210 | router.post('/reset-step1', jsonParser, async (request, response) => { |
| 211 | 211 | try { |
| 212 | 212 | const resetCode = String(crypto.randomInt(1000, 9999)); |
| 213 | 213 | console.loginfo(); |
| 214 | 214 | console.loginfo(color.magenta(`${request.user.profile.name}, your account reset code is: `) + color.red(resetCode)); |
| 215 | 215 | console.loginfo(); |
| 216 | 216 | RESET_CACHE.set(request.user.profile.handle, resetCode); |
| 217 | 217 | return response.sendStatus(204); |
| 218 | 218 | } catch (error) { |
| @@ -224,23 +224,23 @@ router.post('/reset-step1', jsonParser, async (request, response) => { | ||
| 224 | 224 | router.post('/reset-step2', jsonParser, async (request, response) => { |
| 225 | 225 | try { |
| 226 | 226 | if (!request.body.code) { |
| 227 | 227 | console.logerror('Recover step 2 failed: Missing required fields'); |
| 228 | 228 | return response.status(400).json({ error: 'Missing required fields' }); |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | if (request.user.profile.password && request.user.profile.password !== getPasswordHash(request.body.password, request.user.profile.salt)) { |
| 232 | 232 | console.logerror('Recover step 2 failed: Incorrect password'); |
| 233 | 233 | return response.status(400).json({ error: 'Incorrect password' }); |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | const code = RESET_CACHE.get(request.user.profile.handle); |
| 237 | 237 | |
| 238 | 238 | if (!code || code !== request.body.code) { |
| 239 | 239 | console.logerror('Recover step 2 failed: Incorrect code'); |
| 240 | 240 | return response.status(400).json({ error: 'Incorrect code' }); |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | console.loginfo('Resetting account data:', request.user.profile.handle); |
| 244 | 244 | await fsPromises.rm(request.user.directories.root, { recursive: true, force: true }); |
| 245 | 245 | |
| 246 | 246 | 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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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.logerror('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)); |
| @@ -285,10 +287,11 @@ export function deepMerge(target, source) { | ||
| 285 | 287 | if (isObject(target) && isObject(source)) { |
| 286 | 288 | Object.keys(source).forEach(key => { |
| 287 | 289 | if (isObject(source[key])) { |
| 288 | 290 | if (!(key in target)) { |
| 289 | 291 | Object.assign(output, { [key]: source[key] }); |
| 290 | 292 | } else { |
| 291 | 293 | output[key] = deepMerge(target[key], source[key]); |
| 294 | + } | |
| 292 | 295 | } else { |
| 293 | 296 | Object.assign(output, { [key]: source[key] }); |
| 294 | 297 | } |
| @@ -466,7 +469,9 @@ export function forwardFetchResponse(from, to) { | ||
| 466 | 469 | from.body.pipe(to); |
| 467 | 470 | |
| 468 | 471 | to.socket.on('close', function () { |
| 469 | 472 | if (from.body instanceof Readable) from.body.destroy(); // Close the remote stream{ |
| 473 | + from.body.destroy(); // Close the remote stream | |
| 474 | + } | |
| 470 | 475 | to.end(); // End the Express response |
| 471 | 476 | }); |
| 472 | 477 | |
| @@ -693,6 +698,19 @@ export function isValidUrl(url) { | ||
| 693 | 698 | } |
| 694 | 699 | |
| 695 | 700 | /** |
| 701 | + * Setup the minimum log level | |
| 702 | + */ | |
| 703 | +export function setupLogLevel() { | |
| 704 | + const logLevel = getConfigValue('minLogLevel', LOG_LEVELS.DEBUG); | |
| 705 | + | |
| 706 | + globalThis.console.debug = logLevel <= LOG_LEVELS.DEBUG ? console.debug : () => {}; | |
| 707 | + globalThis.console.log = logLevel <= LOG_LEVELS.INFO ? console.log : () => {}; | |
| 708 | + globalThis.console.info = logLevel <= LOG_LEVELS.INFO ? console.info : () => {}; | |
| 709 | + globalThis.console.warn = logLevel <= LOG_LEVELS.WARN ? console.warn : () => {}; | |
| 710 | + globalThis.console.error = logLevel <= LOG_LEVELS.ERROR ? console.error : () => {}; | |
| 711 | +} | |
| 712 | + | |
| 713 | +/** | |
| 696 | 714 | * MemoryLimitedMap class that limits the memory usage of string values. |
| 697 | 715 | */ |
| 698 | 716 | export class MemoryLimitedMap { |