mirror of
https://github.com/langgenius/dify.git
synced 2026-05-12 03:00:44 -04:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: jimcody1995 <jjimcody@gmail.com> Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: jerryzai <jerryzh8710@protonmail.com> Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com> Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com> Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
87 lines
2.2 KiB
TypeScript
87 lines
2.2 KiB
TypeScript
import type { OutputVar } from './types'
|
|
import { VarType } from '../../types'
|
|
import { CodeLanguage } from './types'
|
|
|
|
export const extractFunctionParams = (code: string, language: CodeLanguage) => {
|
|
if (language === CodeLanguage.json)
|
|
return []
|
|
|
|
const patterns: Record<Exclude<CodeLanguage, CodeLanguage.json>, RegExp> = {
|
|
[CodeLanguage.python3]: /def\s+main\s*\((.*?)\)/,
|
|
[CodeLanguage.javascript]: /function\s+main\s*\((.*?)\)/,
|
|
}
|
|
const match = patterns[language].exec(code)
|
|
const params: string[] = []
|
|
|
|
if (match?.[1]) {
|
|
params.push(...match[1].split(',')
|
|
.map(p => p.trim())
|
|
.filter(Boolean)
|
|
.map(p => p.split(':')[0]!.trim()),
|
|
)
|
|
}
|
|
|
|
return params
|
|
}
|
|
export const extractReturnType = (code: string, language: CodeLanguage): OutputVar => {
|
|
const codeWithoutComments = code.replace(/\/\*\*[\s\S]*?\*\//, '')
|
|
// console.log(codeWithoutComments)
|
|
|
|
const returnIndex = codeWithoutComments.indexOf('return')
|
|
if (returnIndex === -1)
|
|
return {}
|
|
|
|
// Extract the substring starting with 'return'.
|
|
const codeAfterReturn = codeWithoutComments.slice(returnIndex)
|
|
|
|
let bracketCount = 0
|
|
let startIndex = codeAfterReturn.indexOf('{')
|
|
|
|
if (language === CodeLanguage.javascript && startIndex === -1) {
|
|
const parenStart = codeAfterReturn.indexOf('(')
|
|
if (parenStart !== -1)
|
|
startIndex = codeAfterReturn.indexOf('{', parenStart)
|
|
}
|
|
|
|
if (startIndex === -1)
|
|
return {}
|
|
|
|
let endIndex = -1
|
|
|
|
for (let i = startIndex; i < codeAfterReturn.length; i++) {
|
|
if (codeAfterReturn[i] === '{')
|
|
bracketCount++
|
|
if (codeAfterReturn[i] === '}') {
|
|
bracketCount--
|
|
if (bracketCount === 0) {
|
|
endIndex = i + 1
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if (endIndex === -1)
|
|
return {}
|
|
|
|
const returnContent = codeAfterReturn.slice(startIndex + 1, endIndex - 1)
|
|
// console.log(returnContent)
|
|
|
|
const result: OutputVar = {}
|
|
|
|
const keyRegex = /['"]?(\w+)['"]?\s*:(?![^{]*\})/g
|
|
const matches = returnContent.matchAll(keyRegex)
|
|
|
|
for (const match of matches) {
|
|
// console.log(`Found key: "${match[1]}" from match: "${match[0]}"`)
|
|
const key = match[1]
|
|
result[key!] = {
|
|
type: VarType.string,
|
|
children: null,
|
|
}
|
|
}
|
|
|
|
// console.log(result)
|
|
|
|
return result
|
|
}
|