2024-05-02 08:48:26 +02:00
|
|
|
const fs = require('fs').promises;
|
2025-08-28 23:12:58 -04:00
|
|
|
const { logger } = require('@librechat/data-schemas');
|
2024-05-02 08:48:26 +02:00
|
|
|
const { getImporter } = require('./importers');
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Job definition for importing a conversation.
|
2024-06-10 13:00:34 -04:00
|
|
|
* @param {{ filepath, requestUserId }} job - The job object.
|
2024-05-02 08:48:26 +02:00
|
|
|
*/
|
2024-06-10 13:00:34 -04:00
|
|
|
const importConversations = async (job) => {
|
|
|
|
|
const { filepath, requestUserId } = job;
|
2024-05-02 08:48:26 +02:00
|
|
|
try {
|
|
|
|
|
logger.debug(`user: ${requestUserId} | Importing conversation(s) from file...`);
|
2025-10-07 14:47:21 -04:00
|
|
|
|
|
|
|
|
/* error if file is too large */
|
|
|
|
|
const fileInfo = await fs.stat(filepath);
|
|
|
|
|
if (fileInfo.size > process.env.CONVERSATION_IMPORT_MAX_FILE_SIZE_BYTES) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`File size is ${fileInfo.size} bytes. It exceeds the maximum limit of ${process.env.CONVERSATION_IMPORT_MAX_FILE_SIZE_BYTES} bytes.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 08:48:26 +02:00
|
|
|
const fileData = await fs.readFile(filepath, 'utf8');
|
|
|
|
|
const jsonData = JSON.parse(fileData);
|
|
|
|
|
const importer = getImporter(jsonData);
|
|
|
|
|
await importer(jsonData, requestUserId);
|
|
|
|
|
logger.debug(`user: ${requestUserId} | Finished importing conversations`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(`user: ${requestUserId} | Failed to import conversation: `, error);
|
2025-10-07 14:47:21 -04:00
|
|
|
throw error; // throw error all the way up so request does not return success
|
2024-05-02 08:48:26 +02:00
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
await fs.unlink(filepath);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(`user: ${requestUserId} | Failed to delete file: ${filepath}`, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-10 13:00:34 -04:00
|
|
|
module.exports = importConversations;
|