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): parse Python Code node parameters with commas (#44337)

mikabff2ff450795
diff --git a/web/app/components/workflow/nodes/code/__tests__/code-parser.spec.ts b/web/app/components/workflow/nodes/code/__tests__/code-parser.spec.ts
index b787bea65ca..bdc2d98472a 100644
--- a/web/app/components/workflow/nodes/code/__tests__/code-parser.spec.ts
+++ b/web/app/components/workflow/nodes/code/__tests__/code-parser.spec.ts
@@ -65,6 +65,62 @@ function main(arg1, arg2) {
}
describe('extractFunctionParams', () => {
+ it.each([
+ [
+ 'generic annotations',
+ 'def main(config: dict[str, str], label: str):\n return {"config": config}',
+ ['config', 'label'],
+ ],
+ [
+ 'string defaults',
+ 'def main(label: str = "one,two", count: int = 1):\n return {"label": label}',
+ ['label', 'count'],
+ ],
+ [
+ 'nested default calls',
+ 'def main(value: str = make_default("x,y"), other: str = ""):\n return {"value": value}',
+ ['value', 'other'],
+ ],
+ [
+ 'escaped quotes in string defaults',
+ String.raw`def main(label: str = "one,\"two", count: int = 1):
+ return {"label": label}`,
+ ['label', 'count'],
+ ],
+ [
+ 'triple-quoted string defaults',
+ 'def main(label: str = """one,two""", count: int = 1):\n return {"label": label}',
+ ['label', 'count'],
+ ],
+ [
+ 'signatures inside Python docstrings',
+ '"""Example:\ndef main(old_arg: str, wrong_arg: str):\n pass\n"""\ndef main(config: dict[str, str]):\n return {"config": config}',
+ ['config'],
+ ],
+ [
+ 'closing parentheses inside strings',
+ 'def main(label: str = "close ) here", count: int = 1):\n return {"label": label}',
+ ['label', 'count'],
+ ],
+ ])(
+ 'splits parameters outside brackets and strings in Python %s',
+ (_description, code, expected) => {
+ expect(extractFunctionParams(code, CodeLanguage.python3)).toEqual(expected)
+ },
+ )
+
+ it('splits JavaScript parameters outside template strings', () => {
+ const code = 'function main({ label: label = `one,two`, count }) { return { label } }'
+ expect(extractFunctionParams(code, CodeLanguage.javascript)).toEqual(['label', 'count'])
+ })
+
+ it.each([
+ ['missing main signature', 'print("no main")'],
+ ['unterminated main signature', 'def main(config: dict[str, str]'],
+ ])('returns no parameters for a %s', (_description, code) => {
+ expect(extractFunctionParams(code, CodeLanguage.python3)).toEqual([])
+ })
+
it.each([
[
'Python typed defaults',
diff --git a/web/app/components/workflow/nodes/code/code-parser.ts b/web/app/components/workflow/nodes/code/code-parser.ts
index d3461917ebd..478947ee0d2 100644
--- a/web/app/components/workflow/nodes/code/code-parser.ts
+++ b/web/app/components/workflow/nodes/code/code-parser.ts
@@ -2,6 +2,99 @@ import type { OutputVar } from './types'
import { VarType } from '../../types'
import { CodeLanguage } from './types'
+const splitTopLevelParams = (rawParams: string, language: CodeLanguage) => {
+ const params: string[] = []
+ let start = 0
+ let nesting = 0
+ let quote: string | undefined
+ let escaped = false
+
+ for (let i = 0; i < rawParams.length; i++) {
+ const char = rawParams[i]!
+
+ if (quote) {
+ if (escaped) {
+ escaped = false
+ } else if (char === '\\') {
+ escaped = true
+ } else if (rawParams.startsWith(quote, i)) {
+ i += quote.length - 1
+ quote = undefined
+ }
+ continue
+ }
+
+ if (char === '"' || char === "'" || (language === CodeLanguage.javascript && char === '`')) {
+ quote =
+ language === CodeLanguage.python3 && rawParams.startsWith(char.repeat(3), i)
+ ? char.repeat(3)
+ : char
+ i += quote.length - 1
+ continue
+ }
+
+ if (char === '(' || char === '[' || char === '{') {
+ nesting++
+ } else if (char === ')' || char === ']' || char === '}') {
+ nesting = Math.max(0, nesting - 1)
+ } else if (char === ',' && nesting === 0) {
+ params.push(rawParams.slice(start, i))
+ start = i + 1
+ }
+ }
+
+ params.push(rawParams.slice(start))
+ return params
+}
+
+const getMainParams = (
+ code: string,
+ language: Exclude<CodeLanguage, typeof CodeLanguage.json>,
+ codeForSearch = code,
+) => {
+ const patterns: Record<Exclude<CodeLanguage, typeof CodeLanguage.json>, RegExp> = {
+ [CodeLanguage.python3]: /def\s+main\s*\(/,
+ [CodeLanguage.javascript]: /function\s+main\s*\(/,
+ }
+ const match = patterns[language].exec(codeForSearch)
+ if (!match) return undefined
+
+ const start = match.index + match[0].length
+ let nesting = 1
+ let quote: string | undefined
+ let escaped = false
+
+ for (let i = start; i < code.length; i++) {
+ const char = code[i]!
+
+ if (quote) {
+ if (escaped) {
+ escaped = false
+ } else if (char === '\\') {
+ escaped = true
+ } else if (code.startsWith(quote, i)) {
+ i += quote.length - 1
+ quote = undefined
+ }
+ continue
+ }
+
+ if (char === '"' || char === "'" || (language === CodeLanguage.javascript && char === '`')) {
+ quote =
+ language === CodeLanguage.python3 && code.startsWith(char.repeat(3), i)
+ ? char.repeat(3)
+ : char
+ i += quote.length - 1
+ } else if (char === '(') {
+ nesting++
+ } else if (char === ')' && --nesting === 0) {
+ return code.slice(start, i)
+ }
+ }
+
+ return undefined
+}
+
export const extractFunctionParams = (code: string, language: CodeLanguage) => {
if (language === CodeLanguage.json) return []
@@ -19,22 +112,20 @@ export const extractFunctionParams = (code: string, language: CodeLanguage) => {
(match: string, comment: string | undefined) =>
comment ? comment.replace(/[^\r\n]/g, ' ') : match,
)
+ const codeForSignatureSearch = codeWithoutComments.replace(commentPatterns[language], (match) =>
+ match.replace(/[^\r\n]/g, ' '),
+ )
- const patterns: Record<Exclude<CodeLanguage, typeof CodeLanguage.json>, RegExp> = {
- [CodeLanguage.python3]: /def\s+main\s*\(([\s\S]*?)\)/,
- [CodeLanguage.javascript]: /function\s+main\s*\(([\s\S]*?)\)/,
- }
- const match = patterns[language].exec(codeWithoutComments)
const params: string[] = []
- if (match?.[1]) {
+ const functionParams = getMainParams(codeWithoutComments, language, codeForSignatureSearch)
+ if (functionParams) {
// The JavaScript `main` is invoked as `main(inputs_obj)`, so its parameters are
// usually written as a single destructuring pattern: `function main({ arg1, arg2 })`.
- const rawParams = match[1].trim().replace(/^\{([\s\S]*)\}$/, '$1')
+ const rawParams = functionParams.trim().replace(/^\{([\s\S]*)\}$/, '$1')
params.push(
- ...rawParams
- .split(',')
+ ...splitTopLevelParams(rawParams, language)
.map((p) => p.trim())
.filter(Boolean)
.map((p) => p.split(':')[0]!.trim()),