mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
194 lines
6.4 KiB
TypeScript
194 lines
6.4 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react'
|
|
import produce from 'immer'
|
|
import useVarList from '../_base/hooks/use-var-list'
|
|
import useOutputVarList from '../_base/hooks/use-output-var-list'
|
|
import { BlockEnum, VarType } from '../../types'
|
|
import type { Var, Variable } from '../../types'
|
|
import { useStore } from '../../store'
|
|
import type { CodeNodeType, OutputVar } from './types'
|
|
import { CodeLanguage } from './types'
|
|
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
|
import { fetchNodeDefault } from '@/service/workflow'
|
|
import { useStore as useAppStore } from '@/app/components/app/store'
|
|
import {
|
|
useNodesReadOnly,
|
|
} from '@/app/components/workflow/hooks'
|
|
|
|
const useConfig = (id: string, payload: CodeNodeType) => {
|
|
const { nodesReadOnly: readOnly } = useNodesReadOnly()
|
|
|
|
const appId = useAppStore.getState().appDetail?.id
|
|
|
|
const [allLanguageDefault, setAllLanguageDefault] = useState<Record<CodeLanguage, CodeNodeType> | null>(null)
|
|
useEffect(() => {
|
|
if (appId) {
|
|
(async () => {
|
|
const { config: javaScriptConfig } = await fetchNodeDefault(appId, BlockEnum.Code, { code_language: CodeLanguage.javascript }) as any
|
|
const { config: pythonConfig } = await fetchNodeDefault(appId, BlockEnum.Code, { code_language: CodeLanguage.python3 }) as any
|
|
setAllLanguageDefault({
|
|
[CodeLanguage.javascript]: javaScriptConfig as CodeNodeType,
|
|
[CodeLanguage.python3]: pythonConfig as CodeNodeType,
|
|
} as any)
|
|
})()
|
|
}
|
|
}, [appId])
|
|
|
|
const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
|
|
const { inputs, setInputs } = useNodeCrud<CodeNodeType>(id, payload)
|
|
const { handleVarListChange, handleAddVariable } = useVarList<CodeNodeType>({
|
|
inputs,
|
|
setInputs,
|
|
})
|
|
|
|
const [outputKeyOrders, setOutputKeyOrders] = useState<string[]>([])
|
|
const syncOutputKeyOrders = useCallback((outputs: OutputVar) => {
|
|
setOutputKeyOrders(Object.keys(outputs))
|
|
}, [])
|
|
useEffect(() => {
|
|
if (inputs.code) {
|
|
if (inputs.outputs && Object.keys(inputs.outputs).length > 0)
|
|
syncOutputKeyOrders(inputs.outputs)
|
|
|
|
return
|
|
}
|
|
|
|
const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
|
|
if (isReady) {
|
|
setInputs({
|
|
...inputs,
|
|
...defaultConfig,
|
|
})
|
|
syncOutputKeyOrders(defaultConfig.outputs)
|
|
}
|
|
}, [defaultConfig])
|
|
|
|
const handleCodeChange = useCallback((code: string) => {
|
|
const newInputs = produce(inputs, (draft) => {
|
|
draft.code = code
|
|
})
|
|
setInputs(newInputs)
|
|
}, [inputs, setInputs])
|
|
|
|
const handleCodeLanguageChange = useCallback((codeLanguage: CodeLanguage) => {
|
|
const currDefaultConfig = allLanguageDefault?.[codeLanguage]
|
|
|
|
const newInputs = produce(inputs, (draft) => {
|
|
draft.code_language = codeLanguage
|
|
if (!currDefaultConfig)
|
|
return
|
|
draft.code = currDefaultConfig.code
|
|
draft.variables = currDefaultConfig.variables
|
|
draft.outputs = currDefaultConfig.outputs
|
|
})
|
|
setInputs(newInputs)
|
|
}, [allLanguageDefault, inputs, setInputs])
|
|
|
|
const handleSyncFunctionSignature = useCallback(() => {
|
|
const generateSyncSignatureCode = (code: string) => {
|
|
let mainDefRe
|
|
let newMainDef
|
|
if (inputs.code_language === CodeLanguage.javascript) {
|
|
mainDefRe = /function\s+main\b\s*\([\s\S]*?\)/g
|
|
newMainDef = 'function main({{var_list}})'
|
|
let param_list = inputs.variables?.map(item => item.variable).join(', ') || ''
|
|
param_list = param_list ? `{${param_list}}` : ''
|
|
newMainDef = newMainDef.replace('{{var_list}}', param_list)
|
|
}
|
|
|
|
else if (inputs.code_language === CodeLanguage.python3) {
|
|
mainDefRe = /def\s+main\b\s*\([\s\S]*?\)/g
|
|
const param_list = []
|
|
for (const item of inputs.variables) {
|
|
let param = item.variable
|
|
let param_type = ''
|
|
switch (item.value_type) {
|
|
case VarType.string:
|
|
param_type = ': str'
|
|
break
|
|
case VarType.number:
|
|
param_type = ': float'
|
|
break
|
|
case VarType.object:
|
|
param_type = ': dict'
|
|
break
|
|
case VarType.array:
|
|
param_type = ': list'
|
|
break
|
|
case VarType.arrayNumber:
|
|
param_type = ': list[float]'
|
|
break
|
|
case VarType.arrayString:
|
|
param_type = ': list[str]'
|
|
break
|
|
case VarType.arrayObject:
|
|
param_type = ': list[dict]'
|
|
break
|
|
}
|
|
param += param_type
|
|
param_list.push(`${param}`)
|
|
}
|
|
|
|
newMainDef = `def main(${param_list.join(', ')})`
|
|
}
|
|
else { return code }
|
|
|
|
const newCode = code.replace(mainDefRe, newMainDef)
|
|
return newCode
|
|
}
|
|
|
|
const newInputs = produce(inputs, (draft) => {
|
|
draft.code = generateSyncSignatureCode(draft.code)
|
|
})
|
|
setInputs(newInputs)
|
|
}, [inputs, setInputs])
|
|
|
|
const {
|
|
handleVarsChange,
|
|
handleAddVariable: handleAddOutputVariable,
|
|
handleRemoveVariable,
|
|
isShowRemoveVarConfirm,
|
|
hideRemoveVarConfirm,
|
|
onRemoveVarConfirm,
|
|
} = useOutputVarList<CodeNodeType>({
|
|
id,
|
|
inputs,
|
|
setInputs,
|
|
outputKeyOrders,
|
|
onOutputKeyOrdersChange: setOutputKeyOrders,
|
|
})
|
|
|
|
const filterVar = useCallback((varPayload: Var) => {
|
|
return [VarType.string, VarType.number, VarType.boolean, VarType.secret, VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayObject, VarType.arrayBoolean, VarType.file, VarType.arrayFile].includes(varPayload.type)
|
|
}, [])
|
|
|
|
const handleCodeAndVarsChange = useCallback((code: string, inputVariables: Variable[], outputVariables: OutputVar) => {
|
|
const newInputs = produce(inputs, (draft) => {
|
|
draft.code = code
|
|
draft.variables = inputVariables
|
|
draft.outputs = outputVariables
|
|
})
|
|
setInputs(newInputs)
|
|
syncOutputKeyOrders(outputVariables)
|
|
}, [inputs, setInputs, syncOutputKeyOrders])
|
|
return {
|
|
readOnly,
|
|
inputs,
|
|
outputKeyOrders,
|
|
handleVarListChange,
|
|
handleAddVariable,
|
|
handleRemoveVariable,
|
|
handleSyncFunctionSignature,
|
|
handleCodeChange,
|
|
handleCodeLanguageChange,
|
|
handleVarsChange,
|
|
filterVar,
|
|
handleAddOutputVariable,
|
|
isShowRemoveVarConfirm,
|
|
hideRemoveVarConfirm,
|
|
onRemoveVarConfirm,
|
|
handleCodeAndVarsChange,
|
|
}
|
|
}
|
|
|
|
export default useConfig
|