mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-12 00:36:03 +00:00
ruff
This commit is contained in:
@@ -16,82 +16,83 @@ from ..safe_eval import safe_eval_with_vars
|
||||
class ConditionNode(WorkflowNode):
|
||||
"""Condition node - branch based on condition"""
|
||||
|
||||
type_name = "condition"
|
||||
category = "control"
|
||||
icon = "🔀"
|
||||
name = "condition"
|
||||
description = "condition"
|
||||
name_zh = "条件分支"
|
||||
name_en = "Condition"
|
||||
description_zh = "根据条件分支工作流"
|
||||
description_en = "Branch workflow based on a condition"
|
||||
type_name = 'condition'
|
||||
category = 'control'
|
||||
icon = '🔀'
|
||||
name = 'condition'
|
||||
description = 'condition'
|
||||
name_zh = '条件分支'
|
||||
name_en = 'Condition'
|
||||
description_zh = '根据条件分支工作流'
|
||||
description_en = 'Branch workflow based on a condition'
|
||||
|
||||
inputs: ClassVar[list[NodePort]] = []
|
||||
outputs: ClassVar[list[NodePort]] = []
|
||||
config_schema: ClassVar[list[NodeConfig]] = []
|
||||
|
||||
async def execute(self, inputs: dict[str, Any], context: ExecutionContext) -> dict[str, Any]:
|
||||
condition_type = self.get_config("condition_type", "expression")
|
||||
input_data = inputs.get("input")
|
||||
condition_type = self.get_config('condition_type', 'expression')
|
||||
input_data = inputs.get('input')
|
||||
|
||||
result = False
|
||||
|
||||
if condition_type == "expression":
|
||||
expression = self.get_config("expression", "false")
|
||||
if condition_type == 'expression':
|
||||
expression = self.get_config('expression', 'false')
|
||||
result = await self._evaluate_expression(expression, input_data, context)
|
||||
elif condition_type == "comparison":
|
||||
elif condition_type == 'comparison':
|
||||
result = await self._evaluate_comparison(input_data, context)
|
||||
elif condition_type == "contains":
|
||||
left = self.get_config("left_value", "")
|
||||
right = self.get_config("right_value", "")
|
||||
elif condition_type == 'contains':
|
||||
left = self.get_config('left_value', '')
|
||||
right = self.get_config('right_value', '')
|
||||
result = right in left
|
||||
elif condition_type == "empty":
|
||||
elif condition_type == 'empty':
|
||||
result = not bool(input_data)
|
||||
elif condition_type == "regex":
|
||||
elif condition_type == 'regex':
|
||||
import re
|
||||
left = self.get_config("left_value", "")
|
||||
pattern = self.get_config("right_value", "")
|
||||
|
||||
left = self.get_config('left_value', '')
|
||||
pattern = self.get_config('right_value', '')
|
||||
result = bool(re.match(pattern, str(left)))
|
||||
|
||||
if result:
|
||||
return {"true": input_data, "false": None}
|
||||
return {'true': input_data, 'false': None}
|
||||
else:
|
||||
return {"true": None, "false": input_data}
|
||||
return {'true': None, 'false': input_data}
|
||||
|
||||
async def _evaluate_expression(self, expression: str, data: Any, context: ExecutionContext) -> bool:
|
||||
try:
|
||||
local_vars = {"input": data, "data": data, "variables": context.variables}
|
||||
local_vars = {'input': data, 'data': data, 'variables': context.variables}
|
||||
return bool(safe_eval_with_vars(expression, local_vars))
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
async def _evaluate_comparison(self, data: Any, context: ExecutionContext) -> bool:
|
||||
left = self.get_config("left_value", "")
|
||||
right = self.get_config("right_value", "")
|
||||
operator = self.get_config("operator", "==")
|
||||
left = self.get_config('left_value', '')
|
||||
right = self.get_config('right_value', '')
|
||||
operator = self.get_config('operator', '==')
|
||||
|
||||
try:
|
||||
left_num = float(left)
|
||||
right_num = float(right)
|
||||
|
||||
if operator == "==":
|
||||
if operator == '==':
|
||||
return left_num == right_num
|
||||
elif operator == "!=":
|
||||
elif operator == '!=':
|
||||
return left_num != right_num
|
||||
elif operator == ">":
|
||||
elif operator == '>':
|
||||
return left_num > right_num
|
||||
elif operator == "<":
|
||||
elif operator == '<':
|
||||
return left_num < right_num
|
||||
elif operator == ">=":
|
||||
elif operator == '>=':
|
||||
return left_num >= right_num
|
||||
elif operator == "<=":
|
||||
elif operator == '<=':
|
||||
return left_num <= right_num
|
||||
except ValueError:
|
||||
if operator == "==":
|
||||
if operator == '==':
|
||||
return left == right
|
||||
elif operator == "!=":
|
||||
elif operator == '!=':
|
||||
return left != right
|
||||
elif operator in (">", "<", ">=", "<="):
|
||||
elif operator in ('>', '<', '>=', '<='):
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user