From 76d050bea4cf611c254aaa4891ab046b35b298e4 Mon Sep 17 00:00:00 2001 From: shaohuzhang1 <80892890+shaohuzhang1@users.noreply.github.com> Date: Mon, 7 Apr 2025 14:07:30 +0800 Subject: [PATCH] feat: New condition for determining whether the discriminator is true or not (#2809) --- .../condition_node/compare/__init__.py | 14 ++++++----- .../condition_node/compare/is_not_true.py | 24 +++++++++++++++++++ .../condition_node/compare/is_true.py | 24 +++++++++++++++++++ .../impl/base_condition_node.py | 6 ++++- .../lang/en-US/views/application-workflow.ts | 10 ++++---- .../lang/zh-CN/views/application-workflow.ts | 8 ++++--- .../zh-Hant/views/application-workflow.ts | 10 ++++---- ui/src/workflow/common/data.ts | 8 ++++--- .../workflow/nodes/condition-node/index.vue | 23 ++++++++---------- 9 files changed, 93 insertions(+), 34 deletions(-) create mode 100644 apps/application/flow/step_node/condition_node/compare/is_not_true.py create mode 100644 apps/application/flow/step_node/condition_node/compare/is_true.py diff --git a/apps/application/flow/step_node/condition_node/compare/__init__.py b/apps/application/flow/step_node/condition_node/compare/__init__.py index 02d42a211..c015f6fea 100644 --- a/apps/application/flow/step_node/condition_node/compare/__init__.py +++ b/apps/application/flow/step_node/condition_node/compare/__init__.py @@ -9,20 +9,22 @@ from .contain_compare import * from .equal_compare import * -from .gt_compare import * from .ge_compare import * +from .gt_compare import * +from .is_not_null_compare import * +from .is_not_true import IsNotTrueCompare +from .is_null_compare import * +from .is_true import IsTrueCompare from .le_compare import * -from .lt_compare import * +from .len_equal_compare import * from .len_ge_compare import * from .len_gt_compare import * from .len_le_compare import * from .len_lt_compare import * -from .len_equal_compare import * -from .is_not_null_compare import * -from .is_null_compare import * +from .lt_compare import * from .not_contain_compare import * compare_handle_list = [GECompare(), GTCompare(), ContainCompare(), EqualCompare(), LTCompare(), LECompare(), LenLECompare(), LenGECompare(), LenEqualCompare(), LenGTCompare(), LenLTCompare(), IsNullCompare(), - IsNotNullCompare(), NotContainCompare()] + IsNotNullCompare(), NotContainCompare(), IsTrueCompare(), IsNotTrueCompare()] diff --git a/apps/application/flow/step_node/condition_node/compare/is_not_true.py b/apps/application/flow/step_node/condition_node/compare/is_not_true.py new file mode 100644 index 000000000..f8a29f5a1 --- /dev/null +++ b/apps/application/flow/step_node/condition_node/compare/is_not_true.py @@ -0,0 +1,24 @@ +# coding=utf-8 +""" + @project: MaxKB + @Author:虎 + @file: is_not_true.py + @date:2025/4/7 13:44 + @desc: +""" +from typing import List + +from application.flow.step_node.condition_node.compare import Compare + + +class IsNotTrueCompare(Compare): + + def support(self, node_id, fields: List[str], source_value, compare, target_value): + if compare == 'is_not_true': + return True + + def compare(self, source_value, compare, target_value): + try: + return source_value is False + except Exception as e: + return False diff --git a/apps/application/flow/step_node/condition_node/compare/is_true.py b/apps/application/flow/step_node/condition_node/compare/is_true.py new file mode 100644 index 000000000..166e0993a --- /dev/null +++ b/apps/application/flow/step_node/condition_node/compare/is_true.py @@ -0,0 +1,24 @@ +# coding=utf-8 +""" + @project: MaxKB + @Author:虎 + @file: IsTrue.py + @date:2025/4/7 13:38 + @desc: +""" +from typing import List + +from application.flow.step_node.condition_node.compare import Compare + + +class IsTrueCompare(Compare): + + def support(self, node_id, fields: List[str], source_value, compare, target_value): + if compare == 'is_true': + return True + + def compare(self, source_value, compare, target_value): + try: + return source_value is True + except Exception as e: + return False diff --git a/apps/application/flow/step_node/condition_node/impl/base_condition_node.py b/apps/application/flow/step_node/condition_node/impl/base_condition_node.py index 0c12e8b1a..109029be2 100644 --- a/apps/application/flow/step_node/condition_node/impl/base_condition_node.py +++ b/apps/application/flow/step_node/condition_node/impl/base_condition_node.py @@ -40,7 +40,11 @@ class BaseConditionNode(IConditionNode): value = self.workflow_manage.generate_prompt(value) except Exception as e: pass - field_value = self.workflow_manage.get_reference_field(field_list[0], field_list[1:]) + field_value = None + try: + field_value = self.workflow_manage.get_reference_field(field_list[0], field_list[1:]) + except Exception as e: + pass for compare_handler in compare_handle_list: if compare_handler.support(field_list[0], field_list[1:], field_value, compare, value): return compare_handler.compare(field_value, compare, value) diff --git a/ui/src/locales/lang/en-US/views/application-workflow.ts b/ui/src/locales/lang/en-US/views/application-workflow.ts index 768415301..e4385ea37 100644 --- a/ui/src/locales/lang/en-US/views/application-workflow.ts +++ b/ui/src/locales/lang/en-US/views/application-workflow.ts @@ -22,7 +22,7 @@ export default { copyParam: 'Copy Parameters', debug: 'Run', exit: 'Exit', - exitSave: 'Save & Exit', + exitSave: 'Save & Exit' }, tip: { publicSuccess: 'Published successfully', @@ -37,7 +37,7 @@ export default { cannotCopy: 'Cannot be copied', copyError: 'Node already copied', paramErrorMessage: 'Parameter already exists: ', - saveMessage: 'Current changes have not been saved. Save before exiting?', + saveMessage: 'Current changes have not been saved. Save before exiting?' }, delete: { confirmTitle: 'Confirm to delete this node?', @@ -229,7 +229,7 @@ export default { toolParam: 'Tool Params', mcpServerTip: 'Please enter the JSON format of the MCP server config', mcpToolTip: 'Please select a tool', - configLabel: 'MCP Server Config (Only supports SSE call method)', + configLabel: 'MCP Server Config (Only supports SSE call method)' }, imageGenerateNode: { label: 'Image Generation', @@ -293,7 +293,9 @@ export default { len_ge: 'Length greater than or equal to', len_gt: 'Length greater than', len_le: 'Length less than or equal to', - len_lt: 'Length less than' + len_lt: 'Length less than', + is_true: 'Is true', + is_not_true: 'Is not true' }, FileUploadSetting: {} } diff --git a/ui/src/locales/lang/zh-CN/views/application-workflow.ts b/ui/src/locales/lang/zh-CN/views/application-workflow.ts index 194fed051..4c5a19d76 100644 --- a/ui/src/locales/lang/zh-CN/views/application-workflow.ts +++ b/ui/src/locales/lang/zh-CN/views/application-workflow.ts @@ -22,7 +22,7 @@ export default { copyParam: '复制参数', debug: '调试', exit: '直接退出', - exitSave: '保存并退出', + exitSave: '保存并退出' }, tip: { publicSuccess: '发布成功', @@ -37,7 +37,7 @@ export default { cannotCopy: '不能被复制', copyError: '已复制节点', paramErrorMessage: '参数已存在: ', - saveMessage: '当前的更改尚未保存,是否保存后退出?', + saveMessage: '当前的更改尚未保存,是否保存后退出?' }, delete: { confirmTitle: '确定删除该节点?', @@ -292,7 +292,9 @@ export default { len_ge: '长度大于等于', len_gt: '长度大于', len_le: '长度小于等于', - len_lt: '长度小于' + len_lt: '长度小于', + is_true: '为真', + is_not_true: '不为真' }, FileUploadSetting: {} } diff --git a/ui/src/locales/lang/zh-Hant/views/application-workflow.ts b/ui/src/locales/lang/zh-Hant/views/application-workflow.ts index f1c7893ff..60269c021 100644 --- a/ui/src/locales/lang/zh-Hant/views/application-workflow.ts +++ b/ui/src/locales/lang/zh-Hant/views/application-workflow.ts @@ -22,7 +22,7 @@ export default { copyParam: '複製參數', debug: '調試', exit: '直接退出', - exitSave: '保存並退出', + exitSave: '保存並退出' }, tip: { publicSuccess: '發布成功', @@ -37,7 +37,7 @@ export default { cannotCopy: '不能被複製', copyError: '已複製節點', paramErrorMessage: '參數已存在: ', - saveMessage: '當前修改未保存,是否保存後退出?', + saveMessage: '當前修改未保存,是否保存後退出?' }, delete: { confirmTitle: '確定刪除該節點?', @@ -229,7 +229,7 @@ export default { toolParam: '工具變數', mcpServerTip: '請輸入JSON格式的MCP服務器配置', mcpToolTip: '請選擇工具', - configLabel: 'MCP Server Config (僅支持SSE調用方式)', + configLabel: 'MCP Server Config (僅支持SSE調用方式)' }, imageGenerateNode: { label: '圖片生成', @@ -292,7 +292,9 @@ export default { len_ge: '長度大於等於', len_gt: '長度大於', len_le: '長度小於等於', - len_lt: '長度小於' + len_lt: '長度小於', + is_true: '為真', + is_not_true: '不為真' }, FileUploadSetting: {} } diff --git a/ui/src/workflow/common/data.ts b/ui/src/workflow/common/data.ts index a30c6ffbb..6ce89c243 100644 --- a/ui/src/workflow/common/data.ts +++ b/ui/src/workflow/common/data.ts @@ -271,7 +271,7 @@ export const mcpNode = { properties: { stepName: t('views.applicationWorkflow.nodes.mcpNode.label'), config: { - fields:[ + fields: [ { label: t('common.result'), value: 'result' @@ -424,7 +424,9 @@ export const compareList = [ { value: 'len_ge', label: t('views.applicationWorkflow.compare.len_ge') }, { value: 'len_gt', label: t('views.applicationWorkflow.compare.len_gt') }, { value: 'len_le', label: t('views.applicationWorkflow.compare.len_le') }, - { value: 'len_lt', label: t('views.applicationWorkflow.compare.len_lt') } + { value: 'len_lt', label: t('views.applicationWorkflow.compare.len_lt') }, + { value: 'is_true', label: t('views.applicationWorkflow.compare.is_true') }, + { value: 'is_not_true', label: t('views.applicationWorkflow.compare.is_not_true') } ] export const nodeDict: any = { @@ -446,7 +448,7 @@ export const nodeDict: any = { [WorkflowType.SpeechToTextNode]: speechToTextNode, [WorkflowType.ImageGenerateNode]: imageGenerateNode, [WorkflowType.VariableAssignNode]: variableAssignNode, - [WorkflowType.McpNode]: mcpNode, + [WorkflowType.McpNode]: mcpNode } export function isWorkFlow(type: string | undefined) { return type === 'WORK_FLOW' diff --git a/ui/src/workflow/nodes/condition-node/index.vue b/ui/src/workflow/nodes/condition-node/index.vue index 00f2ffbe7..fdda27de2 100644 --- a/ui/src/workflow/nodes/condition-node/index.vue +++ b/ui/src/workflow/nodes/condition-node/index.vue @@ -25,14 +25,8 @@ size="small" style="width: 60px; margin: 0 8px" > - - + + {{ $t('views.applicationWorkflow.nodes.conditionNode.conditions.label') @@ -56,9 +50,7 @@ ref="nodeCascaderRef" :nodeModel="nodeModel" class="w-full" - :placeholder=" - $t('views.applicationWorkflow.variable.placeholder') - " + :placeholder="$t('views.applicationWorkflow.variable.placeholder')" v-model="condition.field" /> @@ -94,7 +86,11 @@ - {{ $t('views.applicationWorkflow.nodes.conditionNode.addBranch') }} + + {{ $t('views.applicationWorkflow.nodes.conditionNode.addBranch') }}