refactor: move /services/Files/images/parse to TS API

This commit is contained in:
Danny Avila 2025-08-20 00:54:02 -04:00
parent c160e7c7d5
commit 1c7b3b53da
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
4 changed files with 13 additions and 19 deletions

View file

@ -5,10 +5,10 @@ const fetch = require('node-fetch');
const { v4: uuidv4 } = require('uuid'); const { v4: uuidv4 } = require('uuid');
const { ProxyAgent } = require('undici'); const { ProxyAgent } = require('undici');
const { Tool } = require('@langchain/core/tools'); const { Tool } = require('@langchain/core/tools');
const { logger } = require('@librechat/data-schemas');
const { getImageBasename } = require('@librechat/api');
const { FileContext, ContentTypes } = require('librechat-data-provider'); const { FileContext, ContentTypes } = require('librechat-data-provider');
const { getImageBasename } = require('~/server/services/Files/images');
const extractBaseURL = require('~/utils/extractBaseURL'); const extractBaseURL = require('~/utils/extractBaseURL');
const logger = require('~/config/winston');
const displayMessage = const displayMessage =
"DALL-E displayed an image. All generated images are already plainly visible, so don't repeat the descriptions in detail. Do not list download links as they are available in the UI already. The user may download the images by clicking on them, but do not mention anything about downloading to the user."; "DALL-E displayed an image. All generated images are already plainly visible, so don't repeat the descriptions in detail. Do not list download links as they are available in the UI already. The user may download the images by clicking on them, but do not mention anything about downloading to the user.";

View file

@ -1,13 +1,11 @@
const avatar = require('./avatar'); const avatar = require('./avatar');
const convert = require('./convert'); const convert = require('./convert');
const encode = require('./encode'); const encode = require('./encode');
const parse = require('./parse');
const resize = require('./resize'); const resize = require('./resize');
module.exports = { module.exports = {
...convert, ...convert,
...encode, ...encode,
...parse,
...resize, ...resize,
avatar, avatar,
}; };

View file

@ -1 +1,2 @@
export * from './mistral/crud'; export * from './mistral/crud';
export * from './parse';

View file

@ -1,22 +1,22 @@
const URL = require('url').URL; import path from 'path';
const path = require('path'); import { URL } from 'url';
const imageExtensionRegex = /\.(jpg|jpeg|png|gif|bmp|tiff|svg|webp)$/i; const imageExtensionRegex = /\.(jpg|jpeg|png|gif|bmp|tiff|svg|webp)$/i;
/** /**
* Extracts the image basename from a given URL. * Extracts the image basename from a given URL.
* *
* @param {string} urlString - The URL string from which the image basename is to be extracted. * @param urlString - The URL string from which the image basename is to be extracted.
* @returns {string} The basename of the image file from the URL. * @returns The basename of the image file from the URL.
* Returns an empty string if the URL does not contain a valid image basename. * Returns an empty string if the URL does not contain a valid image basename.
*/ */
function getImageBasename(urlString) { export function getImageBasename(urlString: string) {
try { try {
const url = new URL(urlString); const url = new URL(urlString);
const basename = path.basename(url.pathname); const basename = path.basename(url.pathname);
return imageExtensionRegex.test(basename) ? basename : ''; return imageExtensionRegex.test(basename) ? basename : '';
} catch (error) { } catch {
// If URL parsing fails, return an empty string // If URL parsing fails, return an empty string
return ''; return '';
} }
@ -25,21 +25,16 @@ function getImageBasename(urlString) {
/** /**
* Extracts the basename of a file from a given URL. * Extracts the basename of a file from a given URL.
* *
* @param {string} urlString - The URL string from which the file basename is to be extracted. * @param urlString - The URL string from which the file basename is to be extracted.
* @returns {string} The basename of the file from the URL. * @returns The basename of the file from the URL.
* Returns an empty string if the URL parsing fails. * Returns an empty string if the URL parsing fails.
*/ */
function getFileBasename(urlString) { export function getFileBasename(urlString: string) {
try { try {
const url = new URL(urlString); const url = new URL(urlString);
return path.basename(url.pathname); return path.basename(url.pathname);
} catch (error) { } catch {
// If URL parsing fails, return an empty string // If URL parsing fails, return an empty string
return ''; return '';
} }
} }
module.exports = {
getImageBasename,
getFileBasename,
};