Skip to content

armgit / dify

Public Imported from GitHub
0

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.

fix(web): keep the value selector of a Code node input when regenerating code (#44075)

Yueliup5e09cfe050b6
diff --git a/web/app/components/workflow/nodes/code/__tests__/panel.spec.tsx b/web/app/components/workflow/nodes/code/__tests__/panel.spec.tsx
index 3ffaa7f4d9e..f2cb36e3a4e 100644
--- a/web/app/components/workflow/nodes/code/__tests__/panel.spec.tsx
+++ b/web/app/components/workflow/nodes/code/__tests__/panel.spec.tsx
@@ -294,6 +294,38 @@ describe('code/panel', () => {
)
})
+ it('keeps the existing binding of a parameter that survives code regeneration', async () => {
+ const user = userEvent.setup()
+ const config = createConfigResult()
+ mockUseConfig.mockReturnValue(config)
+ mockExtractFunctionParams.mockReturnValue(['foo', 'newParam'])
+
+ renderPanel()
+
+ await user.click(screen.getByRole('button', { name: 'generate-code' }))
+
+ expect(config.handleCodeAndVarsChange).toHaveBeenCalledWith(
+ 'generated signature code',
+ [
+ {
+ variable: 'foo',
+ value_selector: ['start', 'foo'],
+ value_type: VarType.string,
+ },
+ {
+ variable: 'newParam',
+ value_selector: [],
+ },
+ ],
+ {
+ result: {
+ type: VarType.string,
+ children: null,
+ },
+ },
+ )
+ })
+
it('removes input actions in readonly mode and passes readonly state to child sections', () => {
mockUseConfig.mockReturnValue(
createConfigResult({
diff --git a/web/app/components/workflow/nodes/code/panel.tsx b/web/app/components/workflow/nodes/code/panel.tsx
index b9f25de2325..ac994c35df1 100644
--- a/web/app/components/workflow/nodes/code/panel.tsx
+++ b/web/app/components/workflow/nodes/code/panel.tsx
@@ -51,11 +51,14 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({ id, data }) => {
const handleGeneratedCode = (value: string) => {
const params = extractFunctionParams(value, inputs.code_language)
+ // Regenerating the code rebuilds the input list from the new signature, but a
+ // parameter that survives the regeneration should keep the variable entry the
+ // user already set up -- its upstream value selector and its value type --
+ // instead of being reset to an unbound variable.
+ const existingVariables = new Map(inputs.variables.map((v) => [v.variable, v]))
const codeNewInput = params.map((p) => {
- return {
- variable: p,
- value_selector: [],
- }
+ const existing = existingVariables.get(p)
+ return existing ? { ...existing } : { variable: p, value_selector: [] }
})
const returnTypes = extractReturnType(value, inputs.code_language)
handleCodeAndVarsChange(value, codeNewInput, returnTypes)