44from framework .im .sender import ChatSender
55from framework .ioc .container import DependencyContainer
66from framework .workflow .core .block import Block , Output
7+ from framework .workflow .core .dispatch .models .dispatch_rules import RuleGroup
78from framework .workflow .core .dispatch .registry import DispatchRuleRegistry
89
910
11+ def _format_rule_condition (rule_type : str , config : Dict [str , Any ]) -> str :
12+ """格式化单个规则的条件描述"""
13+ if rule_type == "prefix" :
14+ return f"输入以 { config ['prefix' ]} 开头"
15+ elif rule_type == "keyword" :
16+ keywords = config .get ("keywords" , [])
17+ return f"输入包含 { ' 或 ' .join (keywords )} "
18+ elif rule_type == "regex" :
19+ return f"输入匹配正则 { config ['pattern' ]} "
20+ elif rule_type == "fallback" :
21+ return "任意输入"
22+ elif rule_type == "bot_mention" :
23+ return f"@我"
24+ elif rule_type == "chat_type" :
25+ return f"使用 { config ['chat_type' ]} 聊天类型"
26+ return f"使用 { rule_type } 规则"
27+
28+
29+ def _format_rule_group (group : RuleGroup ) -> str :
30+ """格式化规则组的条件描述"""
31+ rule_conditions = []
32+ for rule in group .rules :
33+ rule_conditions .append (
34+ _format_rule_condition (rule .type , rule .config )
35+ )
36+
37+ operator = " 且 " if group .operator == "and" else " 或 "
38+ return operator .join (rule_conditions )
39+
40+
1041class GenerateHelp (Block ):
1142 """生成帮助信息 block"""
1243
@@ -15,30 +46,6 @@ class GenerateHelp(Block):
1546 outputs = {"response" : Output ("response" , "帮助信息" , IMMessage , "帮助信息" )}
1647 container : DependencyContainer
1748
18- def _format_rule_condition (self , rule_type : str , config : Dict [str , Any ]) -> str :
19- """格式化单个规则的条件描述"""
20- if rule_type == "prefix" :
21- return f"输入以 { config ['prefix' ]} 开头"
22- elif rule_type == "keyword" :
23- keywords = config .get ("keywords" , [])
24- return f"输入包含 { ' 或 ' .join (keywords )} "
25- elif rule_type == "regex" :
26- return f"输入匹配正则 { config ['pattern' ]} "
27- elif rule_type == "fallback" :
28- return "任意输入"
29- return f"使用 { rule_type } 规则"
30-
31- def _format_rule_group (self , group : Dict [str , Any ]) -> str :
32- """格式化规则组的条件描述"""
33- rule_conditions = []
34- for rule in group ["rules" ]:
35- rule_conditions .append (
36- self ._format_rule_condition (rule ["type" ], rule ["config" ])
37- )
38-
39- operator = " 且 " if group ["operator" ] == "and" else " 或 "
40- return operator .join (rule_conditions )
41-
4249 def execute (self ) -> Dict [str , Any ]:
4350 # 从容器获取调度规则注册表
4451 registry = self .container .resolve (DispatchRuleRegistry )
@@ -55,7 +62,7 @@ def execute(self) -> Dict[str, Any]:
5562 # 格式化规则组条件
5663 conditions = []
5764 for group in rule .rule_groups :
58- conditions .append (self . _format_rule_group (group . model_dump () ))
65+ conditions .append (_format_rule_group (group ))
5966
6067 # 组合所有条件(规则组之间是 AND 关系)
6168 rule_format = " 并且 " .join (f"({ condition } )" for condition in conditions )
0 commit comments