Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
30ea3cb7
Commit
30ea3cb7
authored
Mar 12, 2024
by
Joel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: can run code node
parent
a5147a38
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
81 additions
and
35 deletions
+81
-35
output-var-list.tsx
...kflow/nodes/_base/components/variable/output-var-list.tsx
+26
-15
use-one-step-run.ts
...components/workflow/nodes/_base/hooks/use-one-step-run.ts
+28
-3
use-output-var-list.ts
...ponents/workflow/nodes/_base/hooks/use-output-var-list.ts
+11
-8
panel.tsx
web/app/components/workflow/nodes/code/panel.tsx
+3
-3
types.ts
web/app/components/workflow/nodes/code/types.ts
+11
-4
use-config.ts
web/app/components/workflow/nodes/code/use-config.ts
+2
-2
No files found.
web/app/components/workflow/nodes/_base/components/variable/output-var-list.tsx
View file @
30ea3cb7
...
@@ -2,45 +2,56 @@
...
@@ -2,45 +2,56 @@
import
type
{
FC
}
from
'react'
import
type
{
FC
}
from
'react'
import
React
,
{
useCallback
}
from
'react'
import
React
,
{
useCallback
}
from
'react'
import
produce
from
'immer'
import
produce
from
'immer'
import
type
{
OutputVar
}
from
'../../../code/types'
import
type
{
OutputVar
,
OutputVarType
}
from
'../../../code/types'
import
RemoveButton
from
'../remove-button'
import
RemoveButton
from
'../remove-button'
import
VarTypePicker
from
'./var-type-picker'
import
VarTypePicker
from
'./var-type-picker'
type
Props
=
{
type
Props
=
{
readonly
:
boolean
readonly
:
boolean
list
:
OutputVar
[]
outputs
:
OutputVar
onChange
:
(
list
:
OutputVar
[]
)
=>
void
onChange
:
(
payload
:
OutputVar
)
=>
void
}
}
const
OutputVarList
:
FC
<
Props
>
=
({
const
OutputVarList
:
FC
<
Props
>
=
({
readonly
,
readonly
,
list
,
outputs
,
onChange
,
onChange
,
})
=>
{
})
=>
{
const
list
=
(
Object
.
keys
(
outputs
)).
map
((
key
)
=>
{
return
{
variable
:
key
,
variable_type
:
outputs
[
key
].
type
,
}
})
const
handleVarNameChange
=
useCallback
((
index
:
number
)
=>
{
const
handleVarNameChange
=
useCallback
((
index
:
number
)
=>
{
return
(
e
:
React
.
ChangeEvent
<
HTMLInputElement
>
)
=>
{
return
(
e
:
React
.
ChangeEvent
<
HTMLInputElement
>
)
=>
{
const
newList
=
produce
(
list
,
(
draft
)
=>
{
const
oldKey
=
list
[
index
].
variable
draft
[
index
].
variable
=
e
.
target
.
value
const
newOutputs
=
produce
(
outputs
,
(
draft
)
=>
{
const
newKey
=
e
.
target
.
value
draft
[
newKey
]
=
draft
[
oldKey
]
delete
draft
[
oldKey
]
})
})
onChange
(
new
List
)
onChange
(
new
Outputs
)
}
}
},
[
list
,
onChange
])
},
[
list
,
onChange
])
const
handleVarChange
=
useCallback
((
index
:
number
)
=>
{
const
handleVar
Type
Change
=
useCallback
((
index
:
number
)
=>
{
return
(
value
:
string
)
=>
{
return
(
value
:
string
)
=>
{
const
newList
=
produce
(
list
,
(
draft
)
=>
{
const
key
=
list
[
index
].
variable
draft
[
index
].
variable_type
=
value
const
newOutputs
=
produce
(
outputs
,
(
draft
)
=>
{
draft
[
key
].
type
=
value
as
OutputVarType
})
})
onChange
(
new
List
)
onChange
(
new
Outputs
)
}
}
},
[
list
,
onChange
])
},
[
list
,
onChange
])
const
handleVarRemove
=
useCallback
((
index
:
number
)
=>
{
const
handleVarRemove
=
useCallback
((
index
:
number
)
=>
{
return
()
=>
{
return
()
=>
{
const
newList
=
produce
(
list
,
(
draft
)
=>
{
const
key
=
list
[
index
].
variable
draft
.
splice
(
index
,
1
)
const
newOutputs
=
produce
(
outputs
,
(
draft
)
=>
{
delete
draft
[
key
]
})
})
onChange
(
new
List
)
onChange
(
new
Outputs
)
}
}
},
[
list
,
onChange
])
},
[
list
,
onChange
])
...
@@ -57,7 +68,7 @@ const OutputVarList: FC<Props> = ({
...
@@ -57,7 +68,7 @@ const OutputVarList: FC<Props> = ({
<
VarTypePicker
<
VarTypePicker
readonly=
{
readonly
}
readonly=
{
readonly
}
value=
{
item
.
variable_type
}
value=
{
item
.
variable_type
}
onChange=
{
handleVarChange
(
index
)
}
onChange=
{
handleVar
Type
Change
(
index
)
}
/>
/>
<
RemoveButton
<
RemoveButton
className=
'!p-2 !bg-gray-100 hover:!bg-gray-200'
className=
'!p-2 !bg-gray-100 hover:!bg-gray-200'
...
...
web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts
View file @
30ea3cb7
import
{
useState
}
from
'react'
import
{
useState
}
from
'react'
import
{
useTranslation
}
from
'react-i18next'
import
{
useWorkflow
}
from
'@/app/components/workflow/hooks'
import
{
useWorkflow
}
from
'@/app/components/workflow/hooks'
import
type
{
CommonNodeType
,
InputVar
,
Variable
}
from
'@/app/components/workflow/types'
import
type
{
CommonNodeType
,
InputVar
,
Variable
}
from
'@/app/components/workflow/types'
import
{
InputVarType
,
NodeRunningStatus
}
from
'@/app/components/workflow/types'
import
{
InputVarType
,
NodeRunningStatus
}
from
'@/app/components/workflow/types'
import
{
useStore
as
useAppStore
}
from
'@/app/components/app/store'
import
{
useStore
as
useAppStore
}
from
'@/app/components/app/store'
import
{
singleNodeRun
}
from
'@/service/workflow'
import
{
singleNodeRun
}
from
'@/service/workflow'
import
Toast
from
'@/app/components/base/toast'
type
Params
<
T
>
=
{
type
Params
<
T
>
=
{
id
:
string
id
:
string
...
@@ -13,6 +15,8 @@ type Params<T> = {
...
@@ -13,6 +15,8 @@ type Params<T> = {
}
}
const
useOneStepRun
=
<
T
>
({
id
,
data
,
defaultRunInputData
,
isInvalid
=
()
=>
true
}:
Params
<
T
>
)
=>
{
const
useOneStepRun
=
<
T
>
({
id
,
data
,
defaultRunInputData
,
isInvalid
=
()
=>
true
}:
Params
<
T
>
)
=>
{
const
{
t
}
=
useTranslation
()
const
appId
=
useAppStore
.
getState
().
appDetail
?.
id
const
appId
=
useAppStore
.
getState
().
appDetail
?.
id
const
[
runInputData
,
setRunInputData
]
=
useState
<
Record
<
string
,
any
>>
(
defaultRunInputData
||
{})
const
[
runInputData
,
setRunInputData
]
=
useState
<
Record
<
string
,
any
>>
(
defaultRunInputData
||
{})
...
@@ -38,9 +42,30 @@ const useOneStepRun = <T>({ id, data, defaultRunInputData, isInvalid = () => tru
...
@@ -38,9 +42,30 @@ const useOneStepRun = <T>({ id, data, defaultRunInputData, isInvalid = () => tru
_singleRunningStatus
:
NodeRunningStatus
.
Running
,
_singleRunningStatus
:
NodeRunningStatus
.
Running
,
},
},
})
})
try
{
const
res
=
await
singleNodeRun
(
appId
!
,
id
,
{
inputs
:
runInputData
})
const
res
=
await
singleNodeRun
(
appId
!
,
id
,
{
inputs
:
runInputData
})
console
.
log
(
res
)
}
catch
(
e
)
{
handleNodeDataUpdate
({
id
,
data
:
{
...
data
,
_singleRunningStatus
:
NodeRunningStatus
.
Failed
,
},
})
return
false
}
handleNodeDataUpdate
({
id
,
data
:
{
...
data
,
_singleRunningStatus
:
NodeRunningStatus
.
Succeeded
,
},
})
Toast
.
notify
({
type
:
'success'
,
message
:
t
(
'common.api.success'
),
})
}
}
const
handleStop
=
()
=>
{
const
handleStop
=
()
=>
{
...
...
web/app/components/workflow/nodes/_base/hooks/use-output-var-list.ts
View file @
30ea3cb7
import
{
useCallback
}
from
'react'
import
{
useCallback
}
from
'react'
import
produce
from
'immer'
import
produce
from
'immer'
import
type
{
OutputVar
}
from
'../../code/types'
import
{
type
OutputVar
,
OutputVarType
}
from
'../../code/types'
type
Params
<
T
>
=
{
type
Params
<
T
>
=
{
inputs
:
T
inputs
:
T
setInputs
:
(
newInputs
:
T
)
=>
void
setInputs
:
(
newInputs
:
T
)
=>
void
...
@@ -11,25 +11,28 @@ function useOutputVarList<T>({
...
@@ -11,25 +11,28 @@ function useOutputVarList<T>({
setInputs
,
setInputs
,
varKey
=
'outputs'
,
varKey
=
'outputs'
,
}:
Params
<
T
>
)
{
}:
Params
<
T
>
)
{
const
handleVar
ListChange
=
useCallback
((
newList
:
OutputVar
[]
)
=>
{
const
handleVar
sChange
=
useCallback
((
newVars
:
OutputVar
)
=>
{
const
newInputs
=
produce
(
inputs
,
(
draft
:
any
)
=>
{
const
newInputs
=
produce
(
inputs
,
(
draft
:
any
)
=>
{
draft
[
varKey
]
=
new
List
draft
[
varKey
]
=
new
Vars
})
})
setInputs
(
newInputs
)
setInputs
(
newInputs
)
},
[
inputs
,
setInputs
,
varKey
])
},
[
inputs
,
setInputs
,
varKey
])
const
handleAddVariable
=
useCallback
(()
=>
{
const
handleAddVariable
=
useCallback
(()
=>
{
const
newInputs
=
produce
(
inputs
,
(
draft
:
any
)
=>
{
const
newInputs
=
produce
(
inputs
,
(
draft
:
any
)
=>
{
draft
[
varKey
].
push
({
draft
[
varKey
]
=
{
variable
:
''
,
...
draft
[
varKey
],
variable_type
:
'string'
,
[
`var-
${
Object
.
keys
(
draft
[
varKey
]).
length
+
1
}
`
]:
{
})
type
:
OutputVarType
.
string
,
children
:
null
,
},
}
})
})
setInputs
(
newInputs
)
setInputs
(
newInputs
)
},
[
inputs
,
setInputs
,
varKey
])
},
[
inputs
,
setInputs
,
varKey
])
return
{
return
{
handleVar
List
Change
,
handleVar
s
Change
,
handleAddVariable
,
handleAddVariable
,
}
}
}
}
...
...
web/app/components/workflow/nodes/code/panel.tsx
View file @
30ea3cb7
...
@@ -39,7 +39,7 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
...
@@ -39,7 +39,7 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
handleAddVariable
,
handleAddVariable
,
handleCodeChange
,
handleCodeChange
,
handleCodeLanguageChange
,
handleCodeLanguageChange
,
handle
OutputVarList
Change
,
handle
Vars
Change
,
handleAddOutputVariable
,
handleAddOutputVariable
,
// single run
// single run
isShowSingleRun
,
isShowSingleRun
,
...
@@ -91,8 +91,8 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
...
@@ -91,8 +91,8 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
>
>
<
OutputVarList
<
OutputVarList
readonly=
{
readOnly
}
readonly=
{
readOnly
}
list
=
{
inputs
.
outputs
}
outputs
=
{
inputs
.
outputs
}
onChange=
{
handle
OutputVarList
Change
}
onChange=
{
handle
Vars
Change
}
/>
/>
</
Field
>
</
Field
>
</
div
>
</
div
>
...
...
web/app/components/workflow/nodes/code/types.ts
View file @
30ea3cb7
...
@@ -6,14 +6,21 @@ export enum CodeLanguage {
...
@@ -6,14 +6,21 @@ export enum CodeLanguage {
json
=
'json'
,
json
=
'json'
,
}
}
export
type
OutputVar
=
{
export
enum
OutputVarType
{
variable
:
string
string
=
'string'
,
variable_type
:
string
number
=
'number'
,
boolean
=
'boolean'
,
object
=
'object'
,
}
}
export
type
OutputVar
=
Record
<
string
,
{
type
:
OutputVarType
children
:
null
// support nest in the future,
}
>
export
type
CodeNodeType
=
CommonNodeType
&
{
export
type
CodeNodeType
=
CommonNodeType
&
{
variables
:
Variable
[]
variables
:
Variable
[]
code_language
:
CodeLanguage
code_language
:
CodeLanguage
code
:
string
code
:
string
outputs
:
OutputVar
[]
outputs
:
OutputVar
}
}
web/app/components/workflow/nodes/code/use-config.ts
View file @
30ea3cb7
...
@@ -27,7 +27,7 @@ const useConfig = (id: string, payload: CodeNodeType) => {
...
@@ -27,7 +27,7 @@ const useConfig = (id: string, payload: CodeNodeType) => {
setInputs
(
newInputs
)
setInputs
(
newInputs
)
},
[
inputs
,
setInputs
])
},
[
inputs
,
setInputs
])
const
{
handleVar
ListChange
:
handleOutputVarList
Change
,
handleAddVariable
:
handleAddOutputVariable
}
=
useOutputVarList
<
CodeNodeType
>
({
const
{
handleVar
s
Change
,
handleAddVariable
:
handleAddOutputVariable
}
=
useOutputVarList
<
CodeNodeType
>
({
inputs
,
inputs
,
setInputs
,
setInputs
,
})
})
...
@@ -68,7 +68,7 @@ const useConfig = (id: string, payload: CodeNodeType) => {
...
@@ -68,7 +68,7 @@ const useConfig = (id: string, payload: CodeNodeType) => {
handleAddVariable
,
handleAddVariable
,
handleCodeChange
,
handleCodeChange
,
handleCodeLanguageChange
,
handleCodeLanguageChange
,
handle
OutputVarList
Change
,
handle
Vars
Change
,
handleAddOutputVariable
,
handleAddOutputVariable
,
// single run
// single run
isShowSingleRun
,
isShowSingleRun
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment