From 533a6dee5d0733bdbc576b39d2bdb4306be1e36a Mon Sep 17 00:00:00 2001 From: fujie Date: Sat, 20 Dec 2025 15:07:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8F=92=E4=BB=B6=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9=E4=BB=A5=E6=94=AF=E6=8C=81=E5=9C=A8?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E6=96=B0=E7=BB=93=E6=9E=9C=E5=89=8D=E6=B8=85?= =?UTF-8?q?=E9=99=A4=E6=97=A7=E7=9A=84=20HTML=20=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/actions/ACTION_PLUGIN_TEMPLATE.py | 16 ++++++++++++++++ plugins/actions/ACTION_PLUGIN_TEMPLATE_CN.py | 17 +++++++++++++++++ .../actions/knowledge-card/knowledge_card.py | 15 +++++++++++++++ plugins/actions/knowledge-card/闪记卡.py | 15 +++++++++++++++ .../actions/smart-mind-map/smart_mind_map.py | 13 +++++++++++++ plugins/actions/smart-mind-map/思维导图.py | 16 +++++++++++++++- plugins/actions/summary/summary.py | 14 ++++++++++++++ plugins/actions/summary/精读.py | 14 ++++++++++++++ 8 files changed, 119 insertions(+), 1 deletion(-) diff --git a/plugins/actions/ACTION_PLUGIN_TEMPLATE.py b/plugins/actions/ACTION_PLUGIN_TEMPLATE.py index 2b9eff7..33c51c2 100644 --- a/plugins/actions/ACTION_PLUGIN_TEMPLATE.py +++ b/plugins/actions/ACTION_PLUGIN_TEMPLATE.py @@ -50,6 +50,7 @@ Content to process: # HTML Template for rendering the result in the chat HTML_TEMPLATE = """ + @@ -86,6 +87,10 @@ class Action: default=50, description="Minimum text length required for processing (characters).", ) + CLEAR_PREVIOUS_HTML: bool = Field( + default=False, + description="Whether to clear existing plugin-generated HTML content in the message before appending new results (identified by marker).", + ) # Add other configuration fields as needed # MAX_TEXT_LENGTH: int = Field(default=2000, description="...") @@ -138,6 +143,12 @@ class Action: # pass return llm_output.strip() + def _remove_existing_html(self, content: str) -> str: + """Removes existing plugin-generated HTML code blocks from the content.""" + # Match ```html ... ``` pattern + pattern = r"```html\s*[\s\S]*?```" + return re.sub(pattern, "", content).strip() + async def _emit_status( self, emitter: Optional[Callable[[Any], Awaitable[None]]], @@ -253,6 +264,11 @@ class Action: ) # 9. Inject Result + if self.valves.CLEAR_PREVIOUS_HTML: + body["messages"][-1]["content"] = self._remove_existing_html( + body["messages"][-1]["content"] + ) + html_embed_tag = f"```html\n{final_html}\n```" body["messages"][-1]["content"] += f"\n\n{html_embed_tag}" diff --git a/plugins/actions/ACTION_PLUGIN_TEMPLATE_CN.py b/plugins/actions/ACTION_PLUGIN_TEMPLATE_CN.py index ca70588..1fa974e 100644 --- a/plugins/actions/ACTION_PLUGIN_TEMPLATE_CN.py +++ b/plugins/actions/ACTION_PLUGIN_TEMPLATE_CN.py @@ -50,6 +50,7 @@ USER_PROMPT_TEMPLATE = """ # 用于在聊天中渲染结果的 HTML 模板 HTML_TEMPLATE = """ + @@ -86,6 +87,10 @@ class Action: default=50, description="处理所需的最小文本长度(字符数)。", ) + CLEAR_PREVIOUS_HTML: bool = Field( + default=False, + description="是否在追加新结果前清除消息中已有的插件生成 HTML 内容 (通过标记识别)。", + ) # 根据需要添加其他配置字段 # MAX_TEXT_LENGTH: int = Field(default=2000, description="...") @@ -138,6 +143,13 @@ class Action: # pass return llm_output.strip() + def _remove_existing_html(self, content: str) -> str: + """移除内容中已有的插件生成 HTML 代码块 (通过标记识别)。""" + # 匹配 ```html ... ``` 模式 + # 使用 [\s\S]*? 非贪婪匹配任意字符 + pattern = r"```html\s*[\s\S]*?```" + return re.sub(pattern, "", content).strip() + async def _emit_status( self, emitter: Optional[Callable[[Any], Awaitable[None]]], @@ -253,6 +265,11 @@ class Action: ) # 9. 注入结果 + if self.valves.CLEAR_PREVIOUS_HTML: + body["messages"][-1]["content"] = self._remove_existing_html( + body["messages"][-1]["content"] + ) + html_embed_tag = f"```html\n{final_html}\n```" body["messages"][-1]["content"] += f"\n\n{html_embed_tag}" diff --git a/plugins/actions/knowledge-card/knowledge_card.py b/plugins/actions/knowledge-card/knowledge_card.py index 50ded72..bab5fc6 100644 --- a/plugins/actions/knowledge-card/knowledge_card.py +++ b/plugins/actions/knowledge-card/knowledge_card.py @@ -42,6 +42,10 @@ class Action: default=True, description="Whether to show status updates in the chat interface.", ) + clear_previous_html: bool = Field( + default=False, + description="Whether to clear existing plugin-generated HTML content in the message before appending new results (identified by marker).", + ) def __init__(self): self.valves = self.Valves() @@ -176,6 +180,11 @@ Important Principles: html_card = self.generate_html_card(card_data) # 3. Append to message + if self.valves.clear_previous_html: + body["messages"][-1]["content"] = self._remove_existing_html( + body["messages"][-1]["content"] + ) + html_embed_tag = f"```html\n{html_card}\n```" body["messages"][-1]["content"] += f"\n\n{html_embed_tag}" @@ -206,9 +215,15 @@ Important Principles: ) return body + def _remove_existing_html(self, content: str) -> str: + """Removes existing plugin-generated HTML code blocks from the content.""" + pattern = r"```html\s*[\s\S]*?```" + return re.sub(pattern, "", content).strip() + def generate_html_card(self, data): # Enhanced CSS with premium styling style = """ +