Skip to content

Commit

Permalink
ifEsle node add regex match (langgenius#8007)
Browse files Browse the repository at this point in the history
  • Loading branch information
charli117 authored and mehrajagdish committed Sep 6, 2024
1 parent 94d896a commit 4962195
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/core/workflow/nodes/if_else/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Condition(BaseModel):
variable_selector: list[str]
comparison_operator: Literal[
# for string or array
"contains", "not contains", "start with", "end with", "is", "is not", "empty", "not empty",
"contains", "not contains", "start with", "end with", "is", "is not", "empty", "not empty", "regex match",
# for number
"=", "≠", ">", "<", "≥", "≤", "null", "not null"
]
Expand Down
18 changes: 18 additions & 0 deletions api/core/workflow/nodes/if_else/if_else_node.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from collections.abc import Sequence
from typing import Optional, cast

Expand Down Expand Up @@ -136,6 +137,8 @@ def evaluate_condition(
return self._assert_null(actual_value)
elif comparison_operator == "not null":
return self._assert_not_null(actual_value)
elif comparison_operator == "regex match":
return self._assert_regex_match(actual_value, expected_value)
else:
raise ValueError(f"Invalid comparison operator: {comparison_operator}")

Expand Down Expand Up @@ -285,6 +288,21 @@ def _assert_empty(self, actual_value: Optional[str]) -> bool:
return True
return False

def _assert_regex_match(self, actual_value: Optional[str], expected_value: str) -> bool:
"""
Assert empty
:param actual_value: actual value
:return:
"""
if actual_value is None:
return False

pattern = re.compile(expected_value)
regex_result = pattern.findall(actual_value)
if len(regex_result) > 0:
return True
return False

def _assert_not_empty(self, actual_value: Optional[str]) -> bool:
"""
Assert not empty
Expand Down
1 change: 0 additions & 1 deletion web/app/components/app/configuration/config-var/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
} as InputVar
})()
const updatePromptVariableItem = (payload: InputVar) => {
console.log(payload)
const newPromptVariables = produce(promptVariables, (draft) => {
const { variable, label, type, ...rest } = payload
draft[currIndex] = {
Expand Down
1 change: 1 addition & 0 deletions web/app/components/workflow/nodes/if-else/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export enum ComparisonOperator {
lessThanOrEqual = '≤',
isNull = 'is null',
isNotNull = 'is not null',
regexMatch = 'regex match',
}

export type Condition = {
Expand Down
1 change: 1 addition & 0 deletions web/app/components/workflow/nodes/if-else/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const getOperators = (type?: VarType) => {
ComparisonOperator.isNot,
ComparisonOperator.empty,
ComparisonOperator.notEmpty,
ComparisonOperator.regexMatch,
]
case VarType.number:
return [
Expand Down
1 change: 1 addition & 0 deletions web/i18n/en-US/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ const translation = {
'not empty': 'is not empty',
'null': 'is null',
'not null': 'is not null',
'regex match': 'regex match',
},
enterValue: 'Enter value',
addCondition: 'Add Condition',
Expand Down
1 change: 1 addition & 0 deletions web/i18n/zh-Hans/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ const translation = {
'not empty': '不为空',
'null': '空',
'not null': '不为空',
'regex match': '正则匹配',
},
enterValue: '输入值',
addCondition: '添加条件',
Expand Down

0 comments on commit 4962195

Please sign in to comment.