Skip to content

Commit

Permalink
Fix crash in api code dialog when overrideConfig is not available for…
Browse files Browse the repository at this point in the history
… a chatflow/agentflow. Also fix input config in api code dialog not updating when nodes change.
  • Loading branch information
0xi4o committed Nov 11, 2024
1 parent 5b49a57 commit 2aed259
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/ui/src/ui-component/extended/OverrideConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const OverrideConfig = ({ dialogProps }) => {
setNodeConfig(result)

if (!overrideConfigStatus) {
setOverrideConfig(overrideConfig)
setOverrideConfig(newOverrideConfig)
} else {
const updatedOverrideConfig = { ...overrideConfig }

Expand Down
41 changes: 39 additions & 2 deletions packages/ui/src/views/chatflows/APICodeDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const APICodeDialog = ({ show, dialogProps, onCancel }) => {
const dispatch = useDispatch()
const chatflow = useSelector((state) => state.canvas.chatflow)
const apiConfig = chatflow?.apiConfig ? JSON.parse(chatflow.apiConfig) : {}
const overrideConfig = apiConfig?.overrideConfig?.config !== undefined ? apiConfig.overrideConfig.config : {}
const overrideConfigStatus = apiConfig?.overrideConfig?.status !== undefined ? apiConfig.overrideConfig.status : false

const codes = ['Embed', 'Python', 'JavaScript', 'cURL', 'Share Chatbot']
const [value, setValue] = useState(0)
Expand All @@ -96,6 +96,9 @@ const APICodeDialog = ({ show, dialogProps, onCancel }) => {
const [checkboxVal, setCheckbox] = useState(false)
const [nodeConfig, setNodeConfig] = useState({})
const [nodeConfigExpanded, setNodeConfigExpanded] = useState({})
const [overrideConfig, setOverrideConfig] = useState(
apiConfig?.overrideConfig?.config !== undefined ? apiConfig.overrideConfig.config : null
)

const getAllAPIKeysApi = useApi(apiKeyApi.getAllAPIKeys)
const updateChatflowApi = useApi(chatflowsApi.updateChatflow)
Expand Down Expand Up @@ -124,9 +127,12 @@ const APICodeDialog = ({ show, dialogProps, onCancel }) => {

const groupByNodeLabel = (nodes) => {
const result = {}
const newOverrideConfig = {}
const seenNodes = new Set()

nodes.forEach((item) => {
const { node, nodeId, label, name, type } = item
seenNodes.add(node)

if (!result[node]) {
result[node] = {
Expand All @@ -135,21 +141,51 @@ const APICodeDialog = ({ show, dialogProps, onCancel }) => {
}
}

if (!newOverrideConfig[node]) {
// If overrideConfigStatus is true, copy existing config for this node
newOverrideConfig[node] = overrideConfigStatus ? [...(overrideConfig[node] || [])] : []
}

if (!result[node].nodeIds.includes(nodeId)) result[node].nodeIds.push(nodeId)

const param = { label, name, type }

if (!result[node].params.some((existingParam) => JSON.stringify(existingParam) === JSON.stringify(param))) {
result[node].params.push(param)
const paramExists = newOverrideConfig[node].some(
(existingParam) => existingParam.label === label && existingParam.name === name && existingParam.type === type
)
if (!paramExists) {
newOverrideConfig[node].push({ ...param, enabled: false })
}
}
})

// Sort the nodeIds array
for (const node in result) {
result[node].nodeIds.sort()
}

setNodeConfig(result)

if (!overrideConfigStatus) {
setOverrideConfig(newOverrideConfig)
} else {
const updatedOverrideConfig = { ...overrideConfig }

Object.keys(updatedOverrideConfig).forEach((node) => {
if (!seenNodes.has(node)) {
delete updatedOverrideConfig[node]
}
})

seenNodes.forEach((node) => {
if (!updatedOverrideConfig[node]) {
updatedOverrideConfig[node] = newOverrideConfig[node]
}
})

setOverrideConfig(updatedOverrideConfig)
}
}

const handleAccordionChange = (nodeLabel) => (event, isExpanded) => {
Expand All @@ -168,6 +204,7 @@ const APICodeDialog = ({ show, dialogProps, onCancel }) => {
if (getConfigApi.data) {
groupByNodeLabel(getConfigApi.data)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [getConfigApi.data])

const handleChange = (event, newValue) => {
Expand Down

0 comments on commit 2aed259

Please sign in to comment.