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
0c709afe
Commit
0c709afe
authored
Mar 13, 2024
by
takatost
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add if-else node
parent
6ef3542c
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
614 additions
and
2 deletions
+614
-2
variable_pool.py
api/core/workflow/entities/variable_pool.py
+1
-1
entities.py
api/core/workflow/nodes/if_else/entities.py
+26
-0
if_else_node.py
api/core/workflow/nodes/if_else/if_else_node.py
+394
-1
if_else_node.py
api/tests/unit_tests/core/workflow/nodes/if_else_node.py
+193
-0
No files found.
api/core/workflow/entities/variable_pool.py
View file @
0c709afe
...
...
@@ -86,6 +86,6 @@ class VariablePool:
ValueType
.
ARRAY_OBJECT
,
ValueType
.
ARRAY_FILE
]:
if
not
isinstance
(
value
,
list
):
raise
ValueError
(
'Invalid value type: array
'
)
raise
ValueError
(
f
'Invalid value type: {target_value_type.value}
'
)
return
value
api/core/workflow/nodes/if_else/entities.py
0 → 100644
View file @
0c709afe
from
typing
import
Literal
,
Optional
from
pydantic
import
BaseModel
from
core.workflow.entities.base_node_data_entities
import
BaseNodeData
class
IfElseNodeData
(
BaseNodeData
):
"""
Answer Node Data.
"""
class
Condition
(
BaseModel
):
"""
Condition entity
"""
variable_selector
:
list
[
str
]
comparison_operator
:
Literal
[
# for string or array
"contains"
,
"not contains"
,
"start with"
,
"end with"
,
"is"
,
"is not"
,
"empty"
,
"not empty"
,
# for number
"="
,
"≠"
,
">"
,
"<"
,
"≥"
,
"≤"
,
"null"
,
"not null"
]
value
:
Optional
[
str
]
=
None
logical_operator
:
Literal
[
"and"
,
"or"
]
=
"and"
conditions
:
list
[
Condition
]
api/core/workflow/nodes/if_else/if_else_node.py
View file @
0c709afe
This diff is collapsed.
Click to expand it.
api/tests/unit_tests/core/workflow/nodes/if_else_node.py
0 → 100644
View file @
0c709afe
from
unittest.mock
import
MagicMock
from
core.workflow.entities.node_entities
import
SystemVariable
from
core.workflow.entities.variable_pool
import
VariablePool
from
core.workflow.nodes.base_node
import
UserFrom
from
core.workflow.nodes.if_else.if_else_node
import
IfElseNode
from
extensions.ext_database
import
db
from
models.workflow
import
WorkflowNodeExecutionStatus
def
test_execute_if_else_result_true
():
node
=
IfElseNode
(
tenant_id
=
'1'
,
app_id
=
'1'
,
workflow_id
=
'1'
,
user_id
=
'1'
,
user_from
=
UserFrom
.
ACCOUNT
,
config
=
{
'id'
:
'if-else'
,
'data'
:
{
'title'
:
'123'
,
'type'
:
'if-else'
,
'logical_operator'
:
'and'
,
'conditions'
:
[
{
'comparison_operator'
:
'contains'
,
'variable_selector'
:
[
'start'
,
'array_contains'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'not contains'
,
'variable_selector'
:
[
'start'
,
'array_not_contains'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'contains'
,
'variable_selector'
:
[
'start'
,
'contains'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'not contains'
,
'variable_selector'
:
[
'start'
,
'not_contains'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'start with'
,
'variable_selector'
:
[
'start'
,
'start_with'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'end with'
,
'variable_selector'
:
[
'start'
,
'end_with'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'is'
,
'variable_selector'
:
[
'start'
,
'is'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'is not'
,
'variable_selector'
:
[
'start'
,
'is_not'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'empty'
,
'variable_selector'
:
[
'start'
,
'empty'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'not empty'
,
'variable_selector'
:
[
'start'
,
'not_empty'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'='
,
'variable_selector'
:
[
'start'
,
'equals'
],
'value'
:
'22'
},
{
'comparison_operator'
:
'≠'
,
'variable_selector'
:
[
'start'
,
'not_equals'
],
'value'
:
'22'
},
{
'comparison_operator'
:
'>'
,
'variable_selector'
:
[
'start'
,
'greater_than'
],
'value'
:
'22'
},
{
'comparison_operator'
:
'<'
,
'variable_selector'
:
[
'start'
,
'less_than'
],
'value'
:
'22'
},
{
'comparison_operator'
:
'≥'
,
'variable_selector'
:
[
'start'
,
'greater_than_or_equal'
],
'value'
:
'22'
},
{
'comparison_operator'
:
'≤'
,
'variable_selector'
:
[
'start'
,
'less_than_or_equal'
],
'value'
:
'22'
},
{
'comparison_operator'
:
'null'
,
'variable_selector'
:
[
'start'
,
'null'
]
},
{
'comparison_operator'
:
'not null'
,
'variable_selector'
:
[
'start'
,
'not_null'
]
},
]
}
}
)
# construct variable pool
pool
=
VariablePool
(
system_variables
=
{
SystemVariable
.
FILES
:
[],
},
user_inputs
=
{})
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'array_contains'
],
value
=
[
'ab'
,
'def'
])
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'array_not_contains'
],
value
=
[
'ac'
,
'def'
])
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'contains'
],
value
=
'cabcde'
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'not_contains'
],
value
=
'zacde'
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'start_with'
],
value
=
'abc'
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'end_with'
],
value
=
'zzab'
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'is'
],
value
=
'ab'
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'is_not'
],
value
=
'aab'
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'empty'
],
value
=
''
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'not_empty'
],
value
=
'aaa'
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'equals'
],
value
=
22
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'not_equals'
],
value
=
23
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'greater_than'
],
value
=
23
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'less_than'
],
value
=
21
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'greater_than_or_equal'
],
value
=
22
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'less_than_or_equal'
],
value
=
21
)
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'not_null'
],
value
=
'1212'
)
# Mock db.session.close()
db
.
session
.
close
=
MagicMock
()
# execute node
result
=
node
.
_run
(
pool
)
assert
result
.
status
==
WorkflowNodeExecutionStatus
.
SUCCEEDED
assert
result
.
outputs
[
'result'
]
is
True
def
test_execute_if_else_result_false
():
node
=
IfElseNode
(
tenant_id
=
'1'
,
app_id
=
'1'
,
workflow_id
=
'1'
,
user_id
=
'1'
,
user_from
=
UserFrom
.
ACCOUNT
,
config
=
{
'id'
:
'if-else'
,
'data'
:
{
'title'
:
'123'
,
'type'
:
'if-else'
,
'logical_operator'
:
'or'
,
'conditions'
:
[
{
'comparison_operator'
:
'contains'
,
'variable_selector'
:
[
'start'
,
'array_contains'
],
'value'
:
'ab'
},
{
'comparison_operator'
:
'not contains'
,
'variable_selector'
:
[
'start'
,
'array_not_contains'
],
'value'
:
'ab'
}
]
}
}
)
# construct variable pool
pool
=
VariablePool
(
system_variables
=
{
SystemVariable
.
FILES
:
[],
},
user_inputs
=
{})
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'array_contains'
],
value
=
[
'1ab'
,
'def'
])
pool
.
append_variable
(
node_id
=
'start'
,
variable_key_list
=
[
'array_not_contains'
],
value
=
[
'ab'
,
'def'
])
# Mock db.session.close()
db
.
session
.
close
=
MagicMock
()
# execute node
result
=
node
.
_run
(
pool
)
assert
result
.
status
==
WorkflowNodeExecutionStatus
.
SUCCEEDED
assert
result
.
outputs
[
'result'
]
is
False
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