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

@@ -94,6 +94,7 @@ Please conduct a deep and comprehensive analysis, focusing on actionable advice.
# =================================================================
HTML_TEMPLATE = """
<!-- OPENWEBUI_PLUGIN_OUTPUT -->
<!DOCTYPE html>
<html lang="{{ user_language }}">
<head>
@@ -292,6 +293,10 @@ class Action:
default=500,
description="Recommended minimum text length for best analysis results.",
)
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()
@@ -348,6 +353,11 @@ class Action:
"actions_html": actions_html,
}
def _remove_existing_html(self, content: str) -> str:
"""Removes existing plugin-generated HTML code blocks from the content."""
pattern = r"```html\s*<!-- OPENWEBUI_PLUGIN_OUTPUT -->[\s\S]*?```"
return re.sub(pattern, "", content).strip()
def _build_html(self, context: dict) -> str:
"""
Build final HTML content using Jinja2 template and context data.
@@ -488,6 +498,10 @@ class Action:
}
final_html_content = self._build_html(context)
if self.valves.CLEAR_PREVIOUS_HTML:
original_content = self._remove_existing_html(original_content)
html_embed_tag = f"```html\n{final_html_content}\n```"
body["messages"][-1]["content"] = f"{original_content}\n\n{html_embed_tag}"

View File

@@ -91,6 +91,7 @@ USER_PROMPT_GENERATE_SUMMARY = """
# =================================================================
HTML_TEMPLATE = """
<!-- OPENWEBUI_PLUGIN_OUTPUT -->
<!DOCTYPE html>
<html lang="{{ user_language }}">
<head>
@@ -287,6 +288,10 @@ class Action:
RECOMMENDED_MIN_LENGTH: int = Field(
default=500, description="建议的最小文本长度,以获得最佳分析效果。"
)
CLEAR_PREVIOUS_HTML: bool = Field(
default=False,
description="是否在追加新结果前清除消息中已有的插件生成 HTML 内容 (通过标记识别)。",
)
def __init__(self):
self.valves = self.Valves()
@@ -337,6 +342,11 @@ class Action:
"actions_html": actions_html,
}
def _remove_existing_html(self, content: str) -> str:
"""移除内容中已有的插件生成 HTML 代码块 (通过标记识别)。"""
pattern = r"```html\s*<!-- OPENWEBUI_PLUGIN_OUTPUT -->[\s\S]*?```"
return re.sub(pattern, "", content).strip()
def _build_html(self, context: dict) -> str:
"""
使用 Jinja2 模板和上下文数据构建最终的HTML内容。
@@ -477,6 +487,10 @@ class Action:
}
final_html_content = self._build_html(context)
if self.valves.CLEAR_PREVIOUS_HTML:
original_content = self._remove_existing_html(original_content)
html_embed_tag = f"```html\n{final_html_content}\n```"
body["messages"][-1]["content"] = f"{original_content}\n\n{html_embed_tag}"