feat: 插件新增配置项以支持在渲染新结果前清除旧的 HTML 输出

This commit is contained in:
fujie
2025-12-20 15:07:41 +08:00
parent 0ef4d67d09
commit 533a6dee5d
8 changed files with 119 additions and 1 deletions

View File

@@ -50,6 +50,7 @@ USER_PROMPT_TEMPLATE = """
# 用于在聊天中渲染结果的 HTML 模板
HTML_TEMPLATE = """
<!-- OPENWEBUI_PLUGIN_OUTPUT -->
<!DOCTYPE html>
<html lang="{user_language}">
<head>
@@ -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 <!-- OPENWEBUI_PLUGIN_OUTPUT --> ... ``` 模式
# 使用 [\s\S]*? 非贪婪匹配任意字符
pattern = r"```html\s*<!-- OPENWEBUI_PLUGIN_OUTPUT -->[\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}"