import { useDispatch } from 'react-redux' import { useState, useEffect } from 'react' import PropTypes from 'prop-types' import { useSelector } from 'react-redux' // material-ui import { IconButton, Button, Box, Typography } from '@mui/material' import { IconArrowsMaximize, IconBulb, IconX } from '@tabler/icons-react' import { useTheme } from '@mui/material/styles' // Project import import { StyledButton } from '@/ui-component/button/StyledButton' import { SwitchInput } from '@/ui-component/switch/Switch' import { CodeEditor } from '@/ui-component/editor/CodeEditor' import ExpandTextDialog from '@/ui-component/dialog/ExpandTextDialog' // store import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackbarAction, SET_CHATFLOW } from '@/store/actions' import useNotifier from '@/utils/useNotifier' // API import chatflowsApi from '@/api/chatflows' const sampleFunction = `return $flow.rawOutput + " This is a post processed response!";` const PostProcessing = ({ dialogProps }) => { const dispatch = useDispatch() useNotifier() const theme = useTheme() const customization = useSelector((state) => state.customization) const enqueueSnackbar = (...args) => dispatch(enqueueSnackbarAction(...args)) const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args)) const [postProcessingEnabled, setPostProcessingEnabled] = useState(false) const [postProcessingFunction, setPostProcessingFunction] = useState('') const [chatbotConfig, setChatbotConfig] = useState({}) const [showExpandDialog, setShowExpandDialog] = useState(false) const [expandDialogProps, setExpandDialogProps] = useState({}) const handleChange = (value) => { setPostProcessingEnabled(value) } const onExpandDialogClicked = (value) => { const dialogProps = { value, inputParam: { label: 'Post Processing Function', name: 'postProcessingFunction', type: 'code', placeholder: sampleFunction, hideCodeExecute: true }, languageType: 'js', confirmButtonName: 'Save', cancelButtonName: 'Cancel' } setExpandDialogProps(dialogProps) setShowExpandDialog(true) } const onSave = async () => { try { let value = { postProcessing: { enabled: postProcessingEnabled, customFunction: JSON.stringify(postProcessingFunction) } } chatbotConfig.postProcessing = value.postProcessing const saveResp = await chatflowsApi.updateChatflow(dialogProps.chatflow.id, { chatbotConfig: JSON.stringify(chatbotConfig) }) if (saveResp.data) { enqueueSnackbar({ message: 'Post Processing Settings Saved', options: { key: new Date().getTime() + Math.random(), variant: 'success', action: (key) => ( ) } }) dispatch({ type: SET_CHATFLOW, chatflow: saveResp.data }) } } catch (error) { enqueueSnackbar({ message: `Failed to save Post Processing Settings: ${ typeof error.response.data === 'object' ? error.response.data.message : error.response.data }`, options: { key: new Date().getTime() + Math.random(), variant: 'error', persist: true, action: (key) => ( ) } }) } } useEffect(() => { if (dialogProps.chatflow && dialogProps.chatflow.chatbotConfig) { let chatbotConfig = JSON.parse(dialogProps.chatflow.chatbotConfig) setChatbotConfig(chatbotConfig || {}) if (chatbotConfig.postProcessing) { setPostProcessingEnabled(chatbotConfig.postProcessing.enabled) if (chatbotConfig.postProcessing.customFunction) { setPostProcessingFunction(JSON.parse(chatbotConfig.postProcessing.customFunction)) } } } return () => {} }, [dialogProps]) return ( <> JS Function
onExpandDialogClicked(postProcessingFunction)} >
setPostProcessingFunction(code)} basicSetup={{ highlightActiveLine: false, highlightActiveLineGutter: false }} />
The following variables are available to use in the custom function:{' '}
$flow.rawOutput, $flow.input, $flow.chatflowId, $flow.sessionId, $flow.chatId
Save setShowExpandDialog(false)} onConfirm={(newValue) => { setPostProcessingFunction(newValue) setShowExpandDialog(false) }} > ) } PostProcessing.propTypes = { dialogProps: PropTypes.object } export default PostProcessing