Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix(web): remove a Code node output variable once the removal is confirmed (#42678)
Neo1Noir0a428a52e660
diff --git a/web/app/components/workflow/nodes/_base/components/error-handle/utils.ts b/web/app/components/workflow/nodes/_base/components/error-handle/utils.tsindex 5671e0834e8..b65c6774ea0 100644--- a/web/app/components/workflow/nodes/_base/components/error-handle/utils.ts+++ b/web/app/components/workflow/nodes/_base/components/error-handle/utils.ts@@ -1,3 +1,4 @@+import type { DefaultValueForm } from './types'import type { CodeNodeType } from '@/app/components/workflow/nodes/code/types'import type { CommonNodeType } from '@/app/components/workflow/types'import { BlockEnum, VarType } from '@/app/components/workflow/types'@@ -82,3 +83,40 @@ export const getDefaultValue = (data: CommonNodeType) => {return []}++type RenamedKey = {+ from: string+ to: string+}++/**+ * Rebuilds the fallback values of a node while keeping what the user configured:+ * a value is kept when its variable is still there with the same name and type,+ * and a renamed variable keeps its value under the new name.+ */+export const mergeDefaultValue = (+ data: CommonNodeType,+ previous?: DefaultValueForm[],+ renamedKey?: RenamedKey,+) => {+ const newDefaultValue = getDefaultValue(data)+ if (!previous?.length) return newDefaultValue++ const previousByKey = new Map(+ previous.map((item) => [+ renamedKey && item.key === renamedKey.from ? renamedKey.to : item.key,+ item,+ ]),+ )++ return newDefaultValue.map((item) => {+ const previousItem = previousByKey.get(item.key)++ if (!previousItem || previousItem.type !== item.type) return item++ return {+ ...item,+ value: previousItem.value,+ }+ })+}diff --git a/web/app/components/workflow/nodes/_base/hooks/__tests__/use-output-var-list.spec.ts b/web/app/components/workflow/nodes/_base/hooks/__tests__/use-output-var-list.spec.tsnew file mode 100644index 00000000000..c52a1abe282--- /dev/null+++ b/web/app/components/workflow/nodes/_base/hooks/__tests__/use-output-var-list.spec.ts@@ -0,0 +1,350 @@+import type { CodeNodeType } from '@/app/components/workflow/nodes/code/types'+import { act, renderHook } from '@testing-library/react'+import { ErrorHandleTypeEnum } from '@/app/components/workflow/nodes/_base/components/error-handle/types'+import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'+import { BlockEnum, VarType } from '@/app/components/workflow/types'+import useOutputVarList from '../use-output-var-list'++const mockSetInputs = vi.hoisted(() => vi.fn())+const mockOnOutputKeyOrdersChange = vi.hoisted(() => vi.fn())+const mockHandleOutVarRenameChange = vi.hoisted(() => vi.fn())+const mockIsVarUsedInNodes = vi.hoisted(() => vi.fn())+const mockRemoveUsedVarInNodes = vi.hoisted(() => vi.fn())+const mockDeleteInspectVar = vi.hoisted(() => vi.fn())+const mockRenameInspectVarName = vi.hoisted(() => vi.fn())+const inspectState = vi.hoisted(() => ({+ nodesWithInspectVars: [] as Array<{+ nodeId: string+ vars: Array<{ id: string; name: string }>+ }>,+}))++vi.mock('ahooks', () => ({+ useDebounceFn: (fn: (...args: unknown[]) => void) => ({+ run: fn,+ }),+}))++vi.mock('@/app/components/workflow/hooks/use-workflow', async (importOriginal) => {+ const actual =+ await importOriginal<typeof import('@/app/components/workflow/hooks/use-workflow')>()++ return {+ ...actual,+ useWorkflow: () => ({+ handleOutVarRenameChange: (...args: unknown[]) => mockHandleOutVarRenameChange(...args),+ isVarUsedInNodes: (...args: unknown[]) => mockIsVarUsedInNodes(...args),+ removeUsedVarInNodes: (...args: unknown[]) => mockRemoveUsedVarInNodes(...args),+ }),+ }+})++vi.mock('@/app/components/workflow/hooks/use-inspect-vars-crud', () => ({+ __esModule: true,+ default: () => ({+ deleteInspectVar: (...args: unknown[]) => mockDeleteInspectVar(...args),+ renameInspectVarName: (...args: unknown[]) => mockRenameInspectVarName(...args),+ nodesWithInspectVars: inspectState.nodesWithInspectVars,+ }),+}))++const createPayload = (overrides: Partial<CodeNodeType> = {}): CodeNodeType =>+ ({+ title: 'Code',+ desc: '',+ type: BlockEnum.Code,+ code: 'def main():\n return "ok"',+ code_language: CodeLanguage.python3,+ variables: [],+ outputs: {+ primary: { type: VarType.string, children: null },+ secondary: { type: VarType.number, children: null },+ },+ ...overrides,+ }) as CodeNodeType++const renderUseOutputVarList = (+ payload: CodeNodeType = createPayload(),+ outputKeyOrders: string[] = Object.keys(payload.outputs),+) =>+ renderHook(() =>+ useOutputVarList<CodeNodeType>({+ id: 'code-node',+ inputs: payload,+ setInputs: mockSetInputs,+ outputKeyOrders,+ onOutputKeyOrdersChange: mockOnOutputKeyOrdersChange,+ }),+ )++describe('useOutputVarList', () => {+ beforeEach(() => {+ vi.clearAllMocks()+ mockIsVarUsedInNodes.mockReturnValue(false)+ inspectState.nodesWithInspectVars = [+ {+ nodeId: 'code-node',+ vars: [+ { id: 'inspect-primary', name: 'primary' },+ { id: 'inspect-secondary', name: 'secondary' },+ ],+ },+ ]+ })++ it('removes the row right away when no other node uses the variable', () => {+ const { result } = renderUseOutputVarList()++ act(() => {+ result.current.handleRemoveVariable(0)+ })++ expect(result.current.isShowRemoveVarConfirm).toBe(false)+ expect(mockSetInputs).toHaveBeenCalledWith(+ expect.objectContaining({+ outputs: { secondary: { type: VarType.number, children: null } },+ }),+ )+ expect(mockOnOutputKeyOrdersChange).toHaveBeenCalledWith(['secondary'])+ expect(mockRemoveUsedVarInNodes).not.toHaveBeenCalled()+ expect(mockDeleteInspectVar).toHaveBeenCalledWith('code-node', 'inspect-primary')+ })++ it('removes the row after the confirmation when other nodes use the variable', () => {+ mockIsVarUsedInNodes.mockReturnValue(true)+ const { result } = renderUseOutputVarList()++ act(() => {+ result.current.handleRemoveVariable(0)+ })++ expect(result.current.isShowRemoveVarConfirm).toBe(true)+ // only ask first, the row is still there+ expect(mockSetInputs).not.toHaveBeenCalled()++ act(() => {+ result.current.onRemoveVarConfirm()+ })++ expect(mockRemoveUsedVarInNodes).toHaveBeenCalledWith(['code-node', 'primary'])+ expect(mockSetInputs).toHaveBeenCalledWith(+ expect.objectContaining({+ outputs: { secondary: { type: VarType.number, children: null } },+ }),+ )+ expect(mockOnOutputKeyOrdersChange).toHaveBeenCalledWith(['secondary'])+ expect(mockDeleteInspectVar).toHaveBeenCalledWith('code-node', 'inspect-primary')+ expect(result.current.isShowRemoveVarConfirm).toBe(false)+ })++ it('removes the confirmed row by its own index, not by the shared name', () => {+ mockIsVarUsedInNodes.mockReturnValue(true)+ const { result } = renderUseOutputVarList()++ act(() => {+ result.current.handleRemoveVariable(1)+ })+ act(() => {+ result.current.onRemoveVarConfirm()+ })++ expect(mockRemoveUsedVarInNodes).toHaveBeenCalledWith(['code-node', 'secondary'])+ expect(mockSetInputs).toHaveBeenCalledWith(+ expect.objectContaining({+ outputs: { primary: { type: VarType.string, children: null } },+ }),+ )+ expect(mockOnOutputKeyOrdersChange).toHaveBeenCalledWith(['primary'])+ })++ it('keeps the row when the confirmation is cancelled', () => {+ mockIsVarUsedInNodes.mockReturnValue(true)+ const { result } = renderUseOutputVarList()++ act(() => {+ result.current.handleRemoveVariable(0)+ })+ act(() => {+ result.current.hideRemoveVarConfirm()+ })++ expect(result.current.isShowRemoveVarConfirm).toBe(false)+ expect(mockSetInputs).not.toHaveBeenCalled()+ expect(mockOnOutputKeyOrdersChange).not.toHaveBeenCalled()+ expect(mockRemoveUsedVarInNodes).not.toHaveBeenCalled()+ })++ it('keeps the output entry when another row still shares the removed name', () => {+ mockIsVarUsedInNodes.mockReturnValue(true)+ const payload = createPayload({+ outputs: { primary: { type: VarType.string, children: null } },+ })+ const { result } = renderUseOutputVarList(payload, ['primary', 'primary'])++ act(() => {+ result.current.handleRemoveVariable(0)+ })+ act(() => {+ result.current.onRemoveVarConfirm()+ })++ expect(mockOnOutputKeyOrdersChange).toHaveBeenCalledWith(['primary'])+ expect(mockSetInputs).toHaveBeenCalledWith(+ expect.objectContaining({+ outputs: { primary: { type: VarType.string, children: null } },+ }),+ )+ expect(mockDeleteInspectVar).not.toHaveBeenCalled()+ })++ it('recomputes the default value after a confirmed removal', () => {+ mockIsVarUsedInNodes.mockReturnValue(true)+ const payload = createPayload({ error_strategy: ErrorHandleTypeEnum.defaultValue })+ const { result } = renderUseOutputVarList(payload)++ act(() => {+ result.current.handleRemoveVariable(1)+ })+ act(() => {+ result.current.onRemoveVarConfirm()+ })++ const lastInputs = mockSetInputs.mock.calls.at(-1)![0] as CodeNodeType+ expect(lastInputs.default_value).toEqual([{ key: 'primary', type: VarType.string, value: '' }])+ })++ it('keeps the configured fallbacks of the outputs that stay behind a confirmed removal', () => {+ mockIsVarUsedInNodes.mockReturnValue(true)+ const payload = createPayload({+ error_strategy: ErrorHandleTypeEnum.defaultValue,+ default_value: [+ { key: 'primary', type: VarType.string, value: 'sentinel' },+ { key: 'secondary', type: VarType.number, value: 7 },+ ],+ })+ const { result } = renderUseOutputVarList(payload)++ act(() => {+ result.current.handleRemoveVariable(1)+ })+ act(() => {+ result.current.onRemoveVarConfirm()+ })++ const lastInputs = mockSetInputs.mock.calls.at(-1)![0] as CodeNodeType+ expect(lastInputs.default_value).toEqual([+ { key: 'primary', type: VarType.string, value: 'sentinel' },+ ])+ })++ it('keeps the configured fallbacks of the outputs that stay behind a direct removal', () => {+ const payload = createPayload({+ error_strategy: ErrorHandleTypeEnum.defaultValue,+ default_value: [+ { key: 'primary', type: VarType.string, value: 'sentinel' },+ { key: 'secondary', type: VarType.number, value: 7 },+ ],+ })+ const { result } = renderUseOutputVarList(payload)++ act(() => {+ result.current.handleRemoveVariable(1)+ })++ const lastInputs = mockSetInputs.mock.calls.at(-1)![0] as CodeNodeType+ expect(lastInputs.default_value).toEqual([+ { key: 'primary', type: VarType.string, value: 'sentinel' },+ ])+ })++ it('keeps the configured fallbacks when another output is added', () => {+ const payload = createPayload({+ error_strategy: ErrorHandleTypeEnum.defaultValue,+ default_value: [+ { key: 'primary', type: VarType.string, value: 'sentinel' },+ { key: 'secondary', type: VarType.number, value: 7 },+ ],+ })+ const { result } = renderUseOutputVarList(payload)++ act(() => {+ result.current.handleAddVariable()+ })++ const lastInputs = mockSetInputs.mock.calls.at(-1)![0] as CodeNodeType+ expect(lastInputs.default_value).toEqual([+ { key: 'primary', type: VarType.string, value: 'sentinel' },+ { key: 'secondary', type: VarType.number, value: 7 },+ { key: 'var_3', type: VarType.string, value: '' },+ ])+ })++ it('carries the configured fallback over to the new name of a renamed output', () => {+ const payload = createPayload({+ error_strategy: ErrorHandleTypeEnum.defaultValue,+ default_value: [+ { key: 'primary', type: VarType.string, value: 'sentinel' },+ { key: 'secondary', type: VarType.number, value: 7 },+ ],+ })+ const { result } = renderUseOutputVarList(payload)++ act(() => {+ result.current.handleVarsChange(+ {+ renamed: { type: VarType.string, children: null },+ secondary: { type: VarType.number, children: null },+ },+ 0,+ 'renamed',+ )+ })++ const lastInputs = mockSetInputs.mock.calls.at(-1)![0] as CodeNodeType+ expect(lastInputs.default_value).toEqual([+ { key: 'renamed', type: VarType.string, value: 'sentinel' },+ { key: 'secondary', type: VarType.number, value: 7 },+ ])+ })++ it('only resets the fallback of the output whose type changed', () => {+ const payload = createPayload({+ error_strategy: ErrorHandleTypeEnum.defaultValue,+ default_value: [+ { key: 'primary', type: VarType.string, value: 'sentinel' },+ { key: 'secondary', type: VarType.number, value: 7 },+ ],+ })+ const { result } = renderUseOutputVarList(payload)++ act(() => {+ result.current.handleVarsChange({+ primary: { type: VarType.number, children: null },+ secondary: { type: VarType.number, children: null },+ })+ })++ const lastInputs = mockSetInputs.mock.calls.at(-1)![0] as CodeNodeType+ expect(lastInputs.default_value).toEqual([+ { key: 'primary', type: VarType.number, value: 0 },+ { key: 'secondary', type: VarType.number, value: 7 },+ ])+ })++ it('does not touch the fallbacks when the error strategy is not default-value', () => {+ const payload = createPayload({+ error_strategy: ErrorHandleTypeEnum.failBranch,+ default_value: [{ key: 'primary', type: VarType.string, value: 'sentinel' }],+ })+ const { result } = renderUseOutputVarList(payload)++ act(() => {+ result.current.handleRemoveVariable(1)+ })++ const lastInputs = mockSetInputs.mock.calls.at(-1)![0] as CodeNodeType+ expect(lastInputs.default_value).toEqual([+ { key: 'primary', type: VarType.string, value: 'sentinel' },+ ])+ })+})diff --git a/web/app/components/workflow/nodes/_base/hooks/use-output-var-list.ts b/web/app/components/workflow/nodes/_base/hooks/use-output-var-list.tsindex a0739807c52..c3559c1c9dc 100644--- a/web/app/components/workflow/nodes/_base/hooks/use-output-var-list.ts+++ b/web/app/components/workflow/nodes/_base/hooks/use-output-var-list.ts@@ -4,7 +4,7 @@ import { useDebounceFn } from 'ahooks'import { produce } from 'immer'import { useCallback, useRef, useState } from 'react'import { ErrorHandleTypeEnum } from '@/app/components/workflow/nodes/_base/components/error-handle/types'-import { getDefaultValue } from '@/app/components/workflow/nodes/_base/components/error-handle/utils'+import { mergeDefaultValue } from '@/app/components/workflow/nodes/_base/components/error-handle/utils'import { BlockEnum, VarType } from '@/app/components/workflow/types'import useInspectVarsCrud from '../../../hooks/use-inspect-vars-crud'import { useWorkflow } from '../../../hooks/use-workflow'@@ -42,6 +42,12 @@ function useOutputVarList<T>({)const handleVarsChange = useCallback((newVars: OutputVar, changedIndex?: number, newKey?: string) => {+ // a renamed output keeps its configured fallback under the new name+ const renamedKey =+ changedIndex !== undefined && newKey+ ? { from: outputKeyOrders[changedIndex]!, to: newKey }+ : undefined+const newInputs = produce(inputs, (draft: any) => {draft[varKey] = newVars@@ -50,7 +56,11 @@ function useOutputVarList<T>({(inputs as CodeNodeType).error_strategy === ErrorHandleTypeEnum.defaultValue &&varKey === 'outputs')- draft.default_value = getDefaultValue(draft as any)+ draft.default_value = mergeDefaultValue(+ draft as any,+ (inputs as CodeNodeType).default_value,+ renamedKey,+ )})setInputs(newInputs)@@ -110,7 +120,10 @@ function useOutputVarList<T>({(inputs as CodeNodeType).error_strategy === ErrorHandleTypeEnum.defaultValue &&varKey === 'outputs')- draft.default_value = getDefaultValue(draft as any)+ draft.default_value = mergeDefaultValue(+ draft as any,+ (inputs as CodeNodeType).default_value,+ )})setInputs(newInputs)onOutputKeyOrdersChange([...outputKeyOrders, newKey])@@ -118,26 +131,12 @@ function useOutputVarList<T>({const [isShowRemoveVarConfirm, setIsShowRemoveVarConfirm] = useState(false)const [removedVar, setRemovedVar] = useState<ValueSelector>([])- const removeVarInNode = useCallback(() => {- const varId = nodesWithInspectVars- .find((node) => node.nodeId === id)- ?.vars.find((varItem) => {- return varItem.name === removedVar[1]- })?.id- if (varId) deleteInspectVar(id, varId)- removeUsedVarInNodes(removedVar)- setIsShowRemoveVarConfirm(false)- }, [deleteInspectVar, id, nodesWithInspectVars, removeUsedVarInNodes, removedVar])- const handleRemoveVariable = useCallback(+ const [removedIndex, setRemovedIndex] = useState(-1)++ const removeOutputVariable = useCallback((index: number) => {const key = outputKeyOrders[index]!- if (isVarUsedInNodes([id, key])) {- setIsShowRemoveVarConfirm(true)- setRemovedVar([id, key])- return- }-const newOutputKeyOrders = outputKeyOrders.filter((_, i) => i !== index)const newInputs = produce(inputs, (draft: any) => {// Only delete from outputs when no remaining entry shares this name@@ -148,10 +147,14 @@ function useOutputVarList<T>({(inputs as CodeNodeType).error_strategy === ErrorHandleTypeEnum.defaultValue &&varKey === 'outputs')- draft.default_value = getDefaultValue(draft as any)+ draft.default_value = mergeDefaultValue(+ draft as any,+ (inputs as CodeNodeType).default_value,+ )})setInputs(newInputs)onOutputKeyOrdersChange(newOutputKeyOrders)+if (!newOutputKeyOrders.includes(key!)) {const varId = nodesWithInspectVars.find((node) => node.nodeId === id)@@ -163,7 +166,6 @@ function useOutputVarList<T>({},[outputKeyOrders,- isVarUsedInNodes,id,inputs,setInputs,@@ -174,6 +176,30 @@ function useOutputVarList<T>({],)+ const removeVarInNode = useCallback(() => {+ // The confirmation only covers variables that other nodes still reference,+ // the row itself has to be removed from this node as well.+ removeOutputVariable(removedIndex)+ removeUsedVarInNodes(removedVar)+ setIsShowRemoveVarConfirm(false)+ }, [removeOutputVariable, removedIndex, removeUsedVarInNodes, removedVar])++ const handleRemoveVariable = useCallback(+ (index: number) => {+ const key = outputKeyOrders[index]!++ if (isVarUsedInNodes([id, key])) {+ setRemovedIndex(index)+ setIsShowRemoveVarConfirm(true)+ setRemovedVar([id, key])+ return+ }++ removeOutputVariable(index)+ },+ [outputKeyOrders, isVarUsedInNodes, id, removeOutputVariable],+ )+return {handleVarsChange,handleAddVariable,