新增过滤掉正则表达式内容

Fixes #338

新增过滤掉正则表达式内容

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Soulter/AstrBot/issues/338?shareId=XXXX-XXXX-XXXX-XXXX).
This commit is contained in:
Xu Void
2025-02-06 15:28:28 +08:00
parent 7d4c07e4f6
commit 0021cfc4bc
2 changed files with 11 additions and 2 deletions
+7 -1
View File
@@ -30,7 +30,8 @@ DEFAULT_CONFIG = {
"only_llm_result": True,
"interval": "1.5,3.5",
"seg_prompt": "",
"regex": ".*?[。?!~…]+|.+$"
"regex": ".*?[。?!~…]+|.+$",
"filter_regex_content": False
},
"no_permission_reply": True,
},
@@ -229,6 +230,11 @@ CONFIG_METADATA_2 = {
"obvious_hint": True,
"hint": "用于分隔一段消息。默认情况下会根据句号、问号等标点符号分隔。re.findall(r'<regex>', text)",
},
"filter_regex_content": {
"description": "过滤正则表达式内容",
"type": "bool",
"hint": "启用后,分段回复时会过滤掉正则表达式匹配的内容。",
},
},
},
"reply_prefix": {
@@ -25,6 +25,7 @@ class ResultDecorateStage:
self.only_llm_result = ctx.astrbot_config['platform_settings']['segmented_reply']['only_llm_result']
self.seg_prompt = ctx.astrbot_config['platform_settings']['segmented_reply']['seg_prompt']
self.regex = ctx.astrbot_config['platform_settings']['segmented_reply']['regex']
self.filter_regex_content = ctx.astrbot_config['platform_settings']['segmented_reply'].get('filter_regex_content', False)
async def process(self, event: AstrMessageEvent) -> Union[None, AsyncGenerator[None, None]]:
result = event.get_result()
@@ -69,6 +70,8 @@ class ResultDecorateStage:
continue
for seg in split_response:
if seg:
if self.filter_regex_content:
seg = re.sub(self.regex, '', seg)
new_chain.append(Plain(seg))
else:
# 非 Plain 类型的消息段不分段
@@ -126,4 +129,4 @@ class ResultDecorateStage:
# 引用回复
if self.reply_with_quote:
result.chain.insert(0, Reply(id=event.message_obj.message_id))
result.chain.insert(0, Reply(id=event.message_obj.message_id))