mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
* fix: add basePath pattern to support login/register and image paths * Fix linter errors * refactor: Update import statements for getBasePath and isEnabled, and add path utility functions with tests - Refactored imports in addImages.js and StableDiffusion.js to use getBasePath from '@librechat/api'. - Consolidated isEnabled and getBasePath imports in validateImageRequest.js. - Introduced new path utility functions in path.ts and corresponding unit tests in path.spec.ts to validate base path extraction logic. * fix: Update domain server base URL in MarkdownComponents and refactor authentication redirection logic - Changed the domain server base URL in MarkdownComponents.tsx to use the API base URL. - Refactored the useAuthRedirect hook to utilize React Router's navigate for redirection instead of window.location, ensuring a smoother SPA experience. - Added unit tests for the useAuthRedirect hook to verify authentication redirection behavior. * test: Mock isEnabled in validateImages.spec.js for improved test isolation - Updated validateImages.spec.js to mock the isEnabled function from @librechat/api, ensuring that tests can run independently of the actual implementation. - Cleared the DOMAIN_CLIENT environment variable before tests to avoid interference with basePath resolution. --------- Co-authored-by: Danny Avila <danny@librechat.ai>
90 lines
3.1 KiB
JavaScript
90 lines
3.1 KiB
JavaScript
const { getBasePath } = require('@librechat/api');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
|
|
/**
|
|
* The `addImages` function corrects any erroneous image URLs in the `responseMessage.text`
|
|
* and appends image observations from `intermediateSteps` if they are not already present.
|
|
*
|
|
* @function
|
|
* @module addImages
|
|
*
|
|
* @param {Array.<Object>} intermediateSteps - An array of objects, each containing an observation.
|
|
* @param {Object} responseMessage - An object containing the text property which might have image URLs.
|
|
*
|
|
* @property {string} intermediateSteps[].observation - The observation string which might contain an image markdown.
|
|
* @property {string} responseMessage.text - The text which might contain image URLs.
|
|
*
|
|
* @example
|
|
*
|
|
* const intermediateSteps = [
|
|
* { observation: '' }
|
|
* ];
|
|
* const responseMessage = { text: 'Some text with ' };
|
|
*
|
|
* addImages(intermediateSteps, responseMessage);
|
|
*
|
|
* logger.debug(responseMessage.text);
|
|
* // Outputs: 'Some text with \n'
|
|
*
|
|
* @returns {void}
|
|
*/
|
|
function addImages(intermediateSteps, responseMessage) {
|
|
if (!intermediateSteps || !responseMessage) {
|
|
return;
|
|
}
|
|
|
|
const basePath = getBasePath();
|
|
|
|
// Correct any erroneous URLs in the responseMessage.text first
|
|
intermediateSteps.forEach((step) => {
|
|
const { observation } = step;
|
|
if (!observation || !observation.includes('![')) {
|
|
return;
|
|
}
|
|
|
|
const match = observation.match(/\/images\/.*\.\w*/);
|
|
if (!match) {
|
|
return;
|
|
}
|
|
const essentialImagePath = match[0];
|
|
const fullImagePath = `${basePath}${essentialImagePath}`;
|
|
|
|
const regex = /!\[.*?\]\((.*?)\)/g;
|
|
let matchErroneous;
|
|
while ((matchErroneous = regex.exec(responseMessage.text)) !== null) {
|
|
if (matchErroneous[1] && !matchErroneous[1].startsWith(`${basePath}/images/`)) {
|
|
// Replace with the full path including base path
|
|
responseMessage.text = responseMessage.text.replace(matchErroneous[1], fullImagePath);
|
|
}
|
|
}
|
|
});
|
|
|
|
// Now, check if the responseMessage already includes the correct image file path and append if not
|
|
intermediateSteps.forEach((step) => {
|
|
const { observation } = step;
|
|
if (!observation || !observation.includes('![')) {
|
|
return;
|
|
}
|
|
const observedImagePath = observation.match(/!\[[^(]*\]\([^)]*\)/g);
|
|
if (observedImagePath) {
|
|
// Fix the image path to include base path if it doesn't already
|
|
let imageMarkdown = observedImagePath[0];
|
|
const urlMatch = imageMarkdown.match(/\(([^)]+)\)/);
|
|
if (
|
|
urlMatch &&
|
|
urlMatch[1] &&
|
|
!urlMatch[1].startsWith(`${basePath}/images/`) &&
|
|
urlMatch[1].startsWith('/images/')
|
|
) {
|
|
imageMarkdown = imageMarkdown.replace(urlMatch[1], `${basePath}${urlMatch[1]}`);
|
|
}
|
|
|
|
if (!responseMessage.text.includes(imageMarkdown)) {
|
|
responseMessage.text += '\n' + imageMarkdown;
|
|
logger.debug('[addImages] added image from intermediateSteps:', imageMarkdown);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = addImages;
|