diff --git a/packages/server/src/controllers/documentstore/index.ts b/packages/server/src/controllers/documentstore/index.ts index a3385c177e9..36b1402e1d8 100644 --- a/packages/server/src/controllers/documentstore/index.ts +++ b/packages/server/src/controllers/documentstore/index.ts @@ -201,7 +201,8 @@ const processLoader = async (req: Request, res: Response, next: NextFunction) => } const docLoaderId = req.params.loaderId const body = req.body - const apiResponse = await documentStoreService.processLoaderMiddleware(body, docLoaderId) + const isInternalRequest = req.headers['x-request-from'] === 'internal' + const apiResponse = await documentStoreService.processLoaderMiddleware(body, docLoaderId, isInternalRequest) return res.json(apiResponse) } catch (error) { next(error) diff --git a/packages/server/src/services/documentstore/index.ts b/packages/server/src/services/documentstore/index.ts index 9826af68bb8..adea69baed6 100644 --- a/packages/server/src/services/documentstore/index.ts +++ b/packages/server/src/services/documentstore/index.ts @@ -740,7 +740,7 @@ export const processLoader = async ({ appDataSource, componentNodes, data, docLo return getDocumentStoreFileChunks(appDataSource, data.storeId as string, docLoaderId) } -const processLoaderMiddleware = async (data: IDocumentStoreLoaderForPreview, docLoaderId: string) => { +const processLoaderMiddleware = async (data: IDocumentStoreLoaderForPreview, docLoaderId: string, isInternalRequest = false) => { try { const appServer = getRunningExpressApp() const appDataSource = appServer.AppDataSource @@ -761,6 +761,12 @@ const processLoaderMiddleware = async (data: IDocumentStoreLoaderForPreview, doc const job = await upsertQueue.addJob(omit(executeData, OMIT_QUEUE_JOB_DATA)) logger.debug(`[server]: Job added to queue: ${job.id}`) + if (isInternalRequest) { + return { + jobId: job.id + } + } + const queueEvents = upsertQueue.getQueueEvents() const result = await job.waitUntilFinished(queueEvents) diff --git a/packages/server/src/utils/buildChatflow.ts b/packages/server/src/utils/buildChatflow.ts index 739177ea416..0332cd6b128 100644 --- a/packages/server/src/utils/buildChatflow.ts +++ b/packages/server/src/utils/buildChatflow.ts @@ -14,7 +14,8 @@ import { mapMimeTypeToInputField, mapExtToInputField, getFileFromUpload, - removeSpecificFileFromUpload + removeSpecificFileFromUpload, + handleEscapeCharacters } from 'flowise-components' import { StatusCodes } from 'http-status-codes' import { @@ -665,6 +666,7 @@ export const executeFlow = async ({ const postProcessingFunction = JSON.parse(chatflowConfig?.postProcessing?.customFunction) const nodeInstanceFilePath = componentNodes['customFunction'].filePath as string const nodeModule = await import(nodeInstanceFilePath) + //set the outputs.output to EndingNode to prevent json escaping of content... const nodeData = { inputs: { javascriptFunction: postProcessingFunction }, outputs: { output: 'output' } @@ -681,7 +683,13 @@ export const executeFlow = async ({ } const customFuncNodeInstance = new nodeModule.nodeClass() let moderatedResponse = await customFuncNodeInstance.init(nodeData, question, options) - result.text = moderatedResponse + if (typeof moderatedResponse === 'string') { + result.text = handleEscapeCharacters(moderatedResponse, true) + } else if (typeof moderatedResponse === 'object') { + result.text = '```json\n' + JSON.stringify(moderatedResponse, null, 2) + '\n```' + } else { + result.text = moderatedResponse + } resultText = result.text } catch (e) { logger.log('[server]: Post Processing Error:', e) diff --git a/packages/ui/src/ui-component/cards/FollowUpPromptsCard.jsx b/packages/ui/src/ui-component/cards/FollowUpPromptsCard.jsx index 75aceb85b75..0ad96fb3626 100644 --- a/packages/ui/src/ui-component/cards/FollowUpPromptsCard.jsx +++ b/packages/ui/src/ui-component/cards/FollowUpPromptsCard.jsx @@ -12,25 +12,26 @@ const FollowUpPromptsCard = ({ isGrid, followUpPrompts, sx, onPromptClick }) => className={'button-container'} sx={{ width: '100%', maxWidth: isGrid ? 'inherit' : '400px', p: 1.5, display: 'flex', gap: 1, ...sx }} > - {followUpPrompts.map((fp, index) => ( - onPromptClick(fp, e)} - sx={{ - backgroundColor: 'transparent', - border: '1px solid', - boxShadow: '0px 2px 1px -1px rgba(0,0,0,0.2)', - color: '#2196f3', - transition: 'all 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms', - '&:hover': { - backgroundColor: customization.isDarkMode ? 'rgba(0, 0, 0, 0.12)' : 'rgba(0, 0, 0, 0.05)', - border: '1px solid' - } - }} - /> - ))} + {Array.isArray(followUpPrompts) && + followUpPrompts.map((fp, index) => ( + onPromptClick(fp, e)} + sx={{ + backgroundColor: 'transparent', + border: '1px solid', + boxShadow: '0px 2px 1px -1px rgba(0,0,0,0.2)', + color: '#2196f3', + transition: 'all 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms', + '&:hover': { + backgroundColor: customization.isDarkMode ? 'rgba(0, 0, 0, 0.12)' : 'rgba(0, 0, 0, 0.05)', + border: '1px solid' + } + }} + /> + ))} ) } diff --git a/packages/ui/src/ui-component/extended/FileUpload.jsx b/packages/ui/src/ui-component/extended/FileUpload.jsx index d6fc8f02aba..34a6f3e509d 100644 --- a/packages/ui/src/ui-component/extended/FileUpload.jsx +++ b/packages/ui/src/ui-component/extended/FileUpload.jsx @@ -5,7 +5,7 @@ import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackba import parser from 'html-react-parser' // material-ui -import { Button, Box } from '@mui/material' +import { Button, Box, Typography } from '@mui/material' import { IconX, IconBulb } from '@tabler/icons-react' // Project import @@ -22,6 +22,18 @@ const message = `Uploaded files will be parsed as strings and sent to the LLM. I
Refer docs for more details.` +const availableFileTypes = [ + { name: 'CSS', ext: 'text/css' }, + { name: 'CSV', ext: 'text/csv' }, + { name: 'HTML', ext: 'text/html' }, + { name: 'JSON', ext: 'application/json' }, + { name: 'Markdown', ext: 'text/markdown' }, + { name: 'PDF', ext: 'application/pdf' }, + { name: 'SQL', ext: 'application/sql' }, + { name: 'Text File', ext: 'text/plain' }, + { name: 'XML', ext: 'application/xml' } +] + const FileUpload = ({ dialogProps }) => { const dispatch = useDispatch() @@ -31,16 +43,27 @@ const FileUpload = ({ dialogProps }) => { const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args)) const [fullFileUpload, setFullFileUpload] = useState(false) + const [allowedFileTypes, setAllowedFileTypes] = useState([]) const [chatbotConfig, setChatbotConfig] = useState({}) const handleChange = (value) => { setFullFileUpload(value) } + const handleAllowedFileTypesChange = (event) => { + const { checked, value } = event.target + if (checked) { + setAllowedFileTypes((prev) => [...prev, value]) + } else { + setAllowedFileTypes((prev) => prev.filter((item) => item !== value)) + } + } + const onSave = async () => { try { const value = { - status: fullFileUpload + status: fullFileUpload, + allowedUploadFileTypes: allowedFileTypes.join(',') } chatbotConfig.fullFileUpload = value @@ -82,6 +105,9 @@ const FileUpload = ({ dialogProps }) => { } useEffect(() => { + /* backward compatibility - by default, allow all */ + const allowedFileTypes = availableFileTypes.map((fileType) => fileType.ext) + setAllowedFileTypes(allowedFileTypes) if (dialogProps.chatflow) { if (dialogProps.chatflow.chatbotConfig) { try { @@ -90,6 +116,10 @@ const FileUpload = ({ dialogProps }) => { if (chatbotConfig.fullFileUpload) { setFullFileUpload(chatbotConfig.fullFileUpload.status) } + if (chatbotConfig.fullFileUpload?.allowedUploadFileTypes) { + const allowedFileTypes = chatbotConfig.fullFileUpload.allowedUploadFileTypes.split(',') + setAllowedFileTypes(allowedFileTypes) + } } catch (e) { setChatbotConfig({}) } @@ -135,8 +165,44 @@ const FileUpload = ({ dialogProps }) => { - {/* TODO: Allow selection of allowed file types*/} - + + Allow Uploads of Type +
+ {availableFileTypes.map((fileType) => ( +
+ + +
+ ))} +
+ Save diff --git a/packages/ui/src/ui-component/extended/OverrideConfig.jsx b/packages/ui/src/ui-component/extended/OverrideConfig.jsx index 5f398fe01d6..f3c6041dfc7 100644 --- a/packages/ui/src/ui-component/extended/OverrideConfig.jsx +++ b/packages/ui/src/ui-component/extended/OverrideConfig.jsx @@ -116,25 +116,27 @@ const OverrideConfig = ({ dialogProps }) => { } const formatObj = () => { - const obj = { - overrideConfig: { status: overrideConfigStatus } + let apiConfig = JSON.parse(dialogProps.chatflow.apiConfig) + if (apiConfig === null || apiConfig === undefined) { + apiConfig = {} } + let overrideConfig = { status: overrideConfigStatus } if (overrideConfigStatus) { - // loop through each key in nodeOverrides and filter out the enabled ones const filteredNodeOverrides = {} for (const key in nodeOverrides) { filteredNodeOverrides[key] = nodeOverrides[key].filter((node) => node.enabled) } - obj.overrideConfig = { - ...obj.overrideConfig, + overrideConfig = { + ...overrideConfig, nodes: filteredNodeOverrides, variables: variableOverrides.filter((node) => node.enabled) } } + apiConfig.overrideConfig = overrideConfig - return obj + return apiConfig } const onNodeOverrideToggle = (node, property, status) => { @@ -206,7 +208,7 @@ const OverrideConfig = ({ dialogProps }) => { if (!overrideConfigStatus) { setNodeOverrides(newNodeOverrides) } else { - const updatedNodeOverrides = { ...nodeOverrides } + const updatedNodeOverrides = { ...newNodeOverrides } Object.keys(updatedNodeOverrides).forEach((node) => { if (!seenNodes.has(node)) { diff --git a/packages/ui/src/ui-component/extended/RateLimit.jsx b/packages/ui/src/ui-component/extended/RateLimit.jsx index c57b20e79e2..1b3ca3b0105 100644 --- a/packages/ui/src/ui-component/extended/RateLimit.jsx +++ b/packages/ui/src/ui-component/extended/RateLimit.jsx @@ -19,7 +19,7 @@ import chatflowsApi from '@/api/chatflows' // utils import useNotifier from '@/utils/useNotifier' -const RateLimit = () => { +const RateLimit = ({ dialogProps }) => { const dispatch = useDispatch() const chatflow = useSelector((state) => state.canvas.chatflow) const chatflowid = chatflow.id @@ -36,9 +36,11 @@ const RateLimit = () => { const [limitMsg, setLimitMsg] = useState(apiConfig?.rateLimit?.limitMsg ?? '') const formatObj = () => { - const obj = { - rateLimit: { status: rateLimitStatus } + let apiConfig = JSON.parse(dialogProps.chatflow.apiConfig) + if (apiConfig === null || apiConfig === undefined) { + apiConfig = {} } + let obj = { status: rateLimitStatus } if (rateLimitStatus) { const rateLimitValuesBoolean = [!limitMax, !limitDuration, !limitMsg] @@ -46,16 +48,16 @@ const RateLimit = () => { if (rateLimitFilledValues.length >= 1 && rateLimitFilledValues.length <= 2) { throw new Error('Need to fill all rate limit input fields') } else if (rateLimitFilledValues.length === 3) { - obj.rateLimit = { - ...obj.rateLimit, + obj = { + ...obj, limitMax, limitDuration, limitMsg } } } - - return obj + apiConfig.rateLimit = obj + return apiConfig } const handleChange = (value) => { @@ -173,7 +175,8 @@ const RateLimit = () => { } RateLimit.propTypes = { - isSessionMemory: PropTypes.bool + isSessionMemory: PropTypes.bool, + dialogProps: PropTypes.object } export default RateLimit diff --git a/packages/ui/src/ui-component/extended/Security.jsx b/packages/ui/src/ui-component/extended/Security.jsx index b46847fcba0..57fff04babf 100644 --- a/packages/ui/src/ui-component/extended/Security.jsx +++ b/packages/ui/src/ui-component/extended/Security.jsx @@ -12,7 +12,7 @@ const Security = ({ dialogProps }) => { return ( } spacing={4}> - + diff --git a/packages/ui/src/views/chatmessage/ChatMessage.jsx b/packages/ui/src/views/chatmessage/ChatMessage.jsx index 687602144b7..11355599b57 100644 --- a/packages/ui/src/views/chatmessage/ChatMessage.jsx +++ b/packages/ui/src/views/chatmessage/ChatMessage.jsx @@ -200,6 +200,7 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview // full file upload const [fullFileUpload, setFullFileUpload] = useState(false) + const [fullFileUploadAllowedTypes, setFullFileUploadAllowedTypes] = useState('*') // feedback const [chatFeedbackStatus, setChatFeedbackStatus] = useState(false) @@ -693,7 +694,11 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview if (data.followUpPrompts) { const followUpPrompts = JSON.parse(data.followUpPrompts) - setFollowUpPrompts(followUpPrompts) + if (typeof followUpPrompts === 'string') { + setFollowUpPrompts(JSON.parse(followUpPrompts)) + } else { + setFollowUpPrompts(followUpPrompts) + } } } @@ -981,7 +986,9 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview } const getFileUploadAllowedTypes = () => { - if (fullFileUpload) return '*' + if (fullFileUpload) { + return fullFileUploadAllowedTypes === '' ? '*' : fullFileUploadAllowedTypes + } return fileUploadAllowedTypes.includes('*') ? '*' : fileUploadAllowedTypes || '*' } @@ -1118,6 +1125,9 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview if (config.fullFileUpload) { setFullFileUpload(config.fullFileUpload.status) + if (config.fullFileUpload?.allowedUploadFileTypes) { + setFullFileUploadAllowedTypes(config.fullFileUpload?.allowedUploadFileTypes) + } } } } @@ -1198,7 +1208,13 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview if (followUpPromptsStatus && messages.length > 0) { const lastMessage = messages[messages.length - 1] if (lastMessage.type === 'apiMessage' && lastMessage.followUpPrompts) { - setFollowUpPrompts(lastMessage.followUpPrompts) + if (Array.isArray(lastMessage.followUpPrompts)) { + setFollowUpPrompts(lastMessage.followUpPrompts) + } + if (typeof lastMessage.followUpPrompts === 'string') { + const followUpPrompts = JSON.parse(lastMessage.followUpPrompts) + setFollowUpPrompts(followUpPrompts) + } } else if (lastMessage.type === 'userMessage') { setFollowUpPrompts([]) }