diff --git a/packages/components/src/utils.ts b/packages/components/src/utils.ts index 609ce1931b6..387cca64f1c 100644 --- a/packages/components/src/utils.ts +++ b/packages/components/src/utils.ts @@ -872,6 +872,35 @@ export const getVersion: () => Promise<{ version: string }> = async () => { throw new Error('None of the package.json paths could be parsed') } +/** + * Map Ext to InputField + * @param {string} ext + * @returns {string} + */ +export const mapExtToInputField = (ext: string) => { + switch (ext) { + case '.txt': + return 'txtFile' + case '.pdf': + return 'pdfFile' + case '.json': + return 'jsonFile' + case '.csv': + case '.xls': + case '.xlsx': + return 'csvFile' + case '.jsonl': + return 'jsonlinesFile' + case '.docx': + case '.doc': + return 'docxFile' + case '.yaml': + return 'yamlFile' + default: + return 'txtFile' + } +} + /** * Map MimeType to InputField * @param {string} mimeType diff --git a/packages/server/src/utils/buildChatflow.ts b/packages/server/src/utils/buildChatflow.ts index 7bcf697f400..5ff9ddcafd0 100644 --- a/packages/server/src/utils/buildChatflow.ts +++ b/packages/server/src/utils/buildChatflow.ts @@ -1,4 +1,5 @@ import { Request } from 'express' +import * as path from 'path' import { IFileUpload, convertSpeechToText, @@ -6,6 +7,7 @@ import { addSingleFileToStorage, addArrayFilesToStorage, mapMimeTypeToInputField, + mapExtToInputField, IServerSideEventStreamer } from 'flowise-components' import { StatusCodes } from 'http-status-codes' @@ -151,9 +153,33 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals const storagePath = await addArrayFilesToStorage(file.mimetype, fileBuffer, file.originalname, fileNames, chatflowid) - const fileInputField = mapMimeTypeToInputField(file.mimetype) + const fileInputFieldFromMimeType = mapMimeTypeToInputField(file.mimetype) - overrideConfig[fileInputField] = storagePath + const fileExtension = path.extname(file.originalname) + + const fileInputFieldFromExt = mapExtToInputField(fileExtension) + + let fileInputField = 'txtFile' + + if (fileInputFieldFromExt !== 'txtFile') { + fileInputField = fileInputFieldFromExt + } else if (fileInputFieldFromMimeType !== 'txtFile') { + fileInputField = fileInputFieldFromExt + } + + if (overrideConfig[fileInputField]) { + const existingFileInputField = overrideConfig[fileInputField].replace('FILE-STORAGE::', '') + const existingFileInputFieldArray = JSON.parse(existingFileInputField) + + const newFileInputField = storagePath.replace('FILE-STORAGE::', '') + const newFileInputFieldArray = JSON.parse(newFileInputField) + + const updatedFieldArray = existingFileInputFieldArray.concat(newFileInputFieldArray) + + overrideConfig[fileInputField] = `FILE-STORAGE::${JSON.stringify(updatedFieldArray)}` + } else { + overrideConfig[fileInputField] = storagePath + } fs.unlinkSync(file.path) } @@ -161,6 +187,9 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals question: req.body.question ?? 'hello', overrideConfig } + if (req.body.chatId) { + incomingInput.chatId = req.body.chatId + } } /*** Get chatflows and prepare data ***/ diff --git a/packages/server/src/utils/upsertVector.ts b/packages/server/src/utils/upsertVector.ts index a6f67e9b362..044716e74a8 100644 --- a/packages/server/src/utils/upsertVector.ts +++ b/packages/server/src/utils/upsertVector.ts @@ -1,7 +1,8 @@ import { Request } from 'express' import * as fs from 'fs' +import * as path from 'path' import { cloneDeep, omit } from 'lodash' -import { ICommonObject, IMessage, addArrayFilesToStorage, mapMimeTypeToInputField } from 'flowise-components' +import { ICommonObject, IMessage, addArrayFilesToStorage, mapMimeTypeToInputField, mapExtToInputField } from 'flowise-components' import telemetryService from '../services/telemetry' import logger from '../utils/logger' import { @@ -52,15 +53,39 @@ export const upsertVector = async (req: Request, isInternal: boolean = false) => if (files.length) { const overrideConfig: ICommonObject = { ...req.body } - const fileNames: string[] = [] for (const file of files) { + const fileNames: string[] = [] const fileBuffer = fs.readFileSync(file.path) const storagePath = await addArrayFilesToStorage(file.mimetype, fileBuffer, file.originalname, fileNames, chatflowid) - const fileInputField = mapMimeTypeToInputField(file.mimetype) + const fileInputFieldFromMimeType = mapMimeTypeToInputField(file.mimetype) - overrideConfig[fileInputField] = storagePath + const fileExtension = path.extname(file.originalname) + + const fileInputFieldFromExt = mapExtToInputField(fileExtension) + + let fileInputField = 'txtFile' + + if (fileInputFieldFromExt !== 'txtFile') { + fileInputField = fileInputFieldFromExt + } else if (fileInputFieldFromMimeType !== 'txtFile') { + fileInputField = fileInputFieldFromExt + } + + if (overrideConfig[fileInputField]) { + const existingFileInputField = overrideConfig[fileInputField].replace('FILE-STORAGE::', '') + const existingFileInputFieldArray = JSON.parse(existingFileInputField) + + const newFileInputField = storagePath.replace('FILE-STORAGE::', '') + const newFileInputFieldArray = JSON.parse(newFileInputField) + + const updatedFieldArray = existingFileInputFieldArray.concat(newFileInputFieldArray) + + overrideConfig[fileInputField] = `FILE-STORAGE::${JSON.stringify(updatedFieldArray)}` + } else { + overrideConfig[fileInputField] = storagePath + } fs.unlinkSync(file.path) }