Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/Upsert files extension to input field #3288

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/components/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 31 additions & 2 deletions packages/server/src/utils/buildChatflow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Request } from 'express'
import * as path from 'path'
import {
IFileUpload,
convertSpeechToText,
ICommonObject,
addSingleFileToStorage,
addArrayFilesToStorage,
mapMimeTypeToInputField,
mapExtToInputField,
IServerSideEventStreamer
} from 'flowise-components'
import { StatusCodes } from 'http-status-codes'
Expand Down Expand Up @@ -151,16 +153,43 @@ 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)
}
incomingInput = {
question: req.body.question ?? 'hello',
overrideConfig
}
if (req.body.chatId) {
incomingInput.chatId = req.body.chatId
}
}

/*** Get chatflows and prepare data ***/
Expand Down
33 changes: 29 additions & 4 deletions packages/server/src/utils/upsertVector.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
Loading