import PropTypes from 'prop-types' import { Handle, Position, useUpdateNodeInternals } from 'reactflow' import { useEffect, useRef, useState, useContext } from 'react' import { useSelector } from 'react-redux' // material-ui import { useTheme, styled } from '@mui/material/styles' import { Box, Typography, Tooltip, IconButton, Button } from '@mui/material' import { tooltipClasses } from '@mui/material/Tooltip' import { IconArrowsMaximize, IconEdit, IconAlertTriangle } from '@tabler/icons' // project import import { Dropdown } from 'ui-component/dropdown/Dropdown' import { MultiDropdown } from 'ui-component/dropdown/MultiDropdown' import { AsyncDropdown } from 'ui-component/dropdown/AsyncDropdown' import { Input } from 'ui-component/input/Input' import { File } from 'ui-component/file/File' import { SwitchInput } from 'ui-component/switch/Switch' import { flowContext } from 'store/context/ReactFlowContext' import { isValidConnection } from 'utils/genericHelper' import { JsonEditorInput } from 'ui-component/json/JsonEditor' import { TooltipWithParser } from 'ui-component/tooltip/TooltipWithParser' import ToolDialog from 'views/tools/ToolDialog' import FormatPromptValuesDialog from 'ui-component/dialog/FormatPromptValuesDialog' import CredentialInputHandler from './CredentialInputHandler' // utils import { getInputVariables } from 'utils/genericHelper' // const import { FLOWISE_CREDENTIAL_ID } from 'store/constant' const EDITABLE_TOOLS = ['selectedTool'] const CustomWidthTooltip = styled(({ className, ...props }) => )({ [`& .${tooltipClasses.tooltip}`]: { maxWidth: 500 } }) // ===========================|| NodeInputHandler ||=========================== // const NodeInputHandler = ({ inputAnchor, inputParam, data, disabled = false, isAdditionalParams = false }) => { const theme = useTheme() const customization = useSelector((state) => state.customization) const ref = useRef(null) const { reactFlowInstance } = useContext(flowContext) const updateNodeInternals = useUpdateNodeInternals() const [position, setPosition] = useState(0) const [showExpandDialog, setShowExpandDialog] = useState(false) const [expandDialogProps, setExpandDialogProps] = useState({}) const [showAsyncOptionDialog, setAsyncOptionEditDialog] = useState('') const [asyncOptionEditDialogProps, setAsyncOptionEditDialogProps] = useState({}) const [reloadTimestamp, setReloadTimestamp] = useState(Date.now().toString()) const [showFormatPromptValuesDialog, setShowFormatPromptValuesDialog] = useState(false) const [formatPromptValuesDialogProps, setFormatPromptValuesDialogProps] = useState({}) const onExpandDialogClicked = (value, inputParam) => { const dialogProp = { value, inputParam, disabled, confirmButtonName: 'Save', cancelButtonName: 'Cancel' } setExpandDialogProps(dialogProp) setShowExpandDialog(true) } const onFormatPromptValuesClicked = (value, inputParam) => { // Preset values if the field is format prompt values let inputValue = value if (inputParam.name === 'promptValues' && !value) { const obj = {} const templateValue = (data.inputs['template'] ?? '') + (data.inputs['systemMessagePrompt'] ?? '') + (data.inputs['humanMessagePrompt'] ?? '') const inputVariables = getInputVariables(templateValue) for (const inputVariable of inputVariables) { obj[inputVariable] = '' } if (Object.keys(obj).length) inputValue = JSON.stringify(obj) } const dialogProp = { value: inputValue, inputParam, nodes: reactFlowInstance.getNodes(), edges: reactFlowInstance.getEdges(), nodeId: data.id } setFormatPromptValuesDialogProps(dialogProp) setShowFormatPromptValuesDialog(true) } const onExpandDialogSave = (newValue, inputParamName) => { setShowExpandDialog(false) data.inputs[inputParamName] = newValue } const editAsyncOption = (inputParamName, inputValue) => { if (inputParamName === 'selectedTool') { setAsyncOptionEditDialogProps({ title: 'Edit Tool', type: 'EDIT', cancelButtonName: 'Cancel', confirmButtonName: 'Save', toolId: inputValue }) } setAsyncOptionEditDialog(inputParamName) } const addAsyncOption = (inputParamName) => { if (inputParamName === 'selectedTool') { setAsyncOptionEditDialogProps({ title: 'Add New Tool', type: 'ADD', cancelButtonName: 'Cancel', confirmButtonName: 'Add' }) } setAsyncOptionEditDialog(inputParamName) } const onConfirmAsyncOption = (selectedOptionId = '') => { if (!selectedOptionId) { data.inputs[showAsyncOptionDialog] = '' } else { data.inputs[showAsyncOptionDialog] = selectedOptionId setReloadTimestamp(Date.now().toString()) } setAsyncOptionEditDialogProps({}) setAsyncOptionEditDialog('') } useEffect(() => { if (ref.current && ref.current.offsetTop && ref.current.clientHeight) { setPosition(ref.current.offsetTop + ref.current.clientHeight / 2) updateNodeInternals(data.id) } }, [data.id, ref, updateNodeInternals]) useEffect(() => { updateNodeInternals(data.id) }, [data.id, position, updateNodeInternals]) return (
{inputAnchor && ( <> isValidConnection(connection, reactFlowInstance)} style={{ height: 10, width: 10, backgroundColor: data.selected ? theme.palette.primary.main : theme.palette.text.secondary, top: position }} /> {inputAnchor.label} {!inputAnchor.optional &&  *} {inputAnchor.description && } )} {((inputParam && !inputParam.additionalParams) || isAdditionalParams) && ( <> {inputParam.acceptVariable && ( isValidConnection(connection, reactFlowInstance)} style={{ height: 10, width: 10, backgroundColor: data.selected ? theme.palette.primary.main : theme.palette.text.secondary, top: position }} /> )}
{inputParam.label} {!inputParam.optional &&  *} {inputParam.description && }
{inputParam.type === 'string' && inputParam.rows && ( onExpandDialogClicked(data.inputs[inputParam.name] ?? inputParam.default ?? '', inputParam) } > )}
{inputParam.warning && (
{inputParam.warning}
)} {inputParam.type === 'credential' && ( { data.credential = newValue data.inputs[FLOWISE_CREDENTIAL_ID] = newValue // in case data.credential is not updated }} /> )} {inputParam.type === 'file' && ( (data.inputs[inputParam.name] = newValue)} value={data.inputs[inputParam.name] ?? inputParam.default ?? 'Choose a file to upload'} /> )} {inputParam.type === 'boolean' && ( (data.inputs[inputParam.name] = newValue)} value={data.inputs[inputParam.name] ?? inputParam.default ?? false} /> )} {(inputParam.type === 'string' || inputParam.type === 'password' || inputParam.type === 'number') && ( (data.inputs[inputParam.name] = newValue)} value={data.inputs[inputParam.name] ?? inputParam.default ?? ''} nodes={inputParam?.acceptVariable && reactFlowInstance ? reactFlowInstance.getNodes() : []} edges={inputParam?.acceptVariable && reactFlowInstance ? reactFlowInstance.getEdges() : []} nodeId={data.id} showDialog={showExpandDialog} dialogProps={expandDialogProps} onDialogCancel={() => setShowExpandDialog(false)} onDialogConfirm={(newValue, inputParamName) => onExpandDialogSave(newValue, inputParamName)} /> )} {inputParam.type === 'json' && ( <> {!inputParam?.acceptVariable && ( (data.inputs[inputParam.name] = newValue)} value={data.inputs[inputParam.name] ?? inputParam.default ?? ''} isDarkMode={customization.isDarkMode} /> )} {inputParam?.acceptVariable && ( <> setShowFormatPromptValuesDialog(false)} onChange={(newValue) => (data.inputs[inputParam.name] = newValue)} > )} )} {inputParam.type === 'options' && ( (data.inputs[inputParam.name] = newValue)} value={data.inputs[inputParam.name] ?? inputParam.default ?? 'choose an option'} /> )} {inputParam.type === 'multiOptions' && ( (data.inputs[inputParam.name] = newValue)} value={data.inputs[inputParam.name] ?? inputParam.default ?? 'choose an option'} /> )} {inputParam.type === 'asyncOptions' && ( <> {data.inputParams.length === 1 &&
}
(data.inputs[inputParam.name] = newValue)} onCreateNew={() => addAsyncOption(inputParam.name)} /> {EDITABLE_TOOLS.includes(inputParam.name) && data.inputs[inputParam.name] && ( editAsyncOption(inputParam.name, data.inputs[inputParam.name])} > )}
)} )} setAsyncOptionEditDialog('')} onConfirm={onConfirmAsyncOption} >
) } NodeInputHandler.propTypes = { inputAnchor: PropTypes.object, inputParam: PropTypes.object, data: PropTypes.object, disabled: PropTypes.bool, isAdditionalParams: PropTypes.bool } export default NodeInputHandler