Compare commits
21 Commits
v2026.01.1
...
v2026.01.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d8c4e048e | ||
|
|
014a5a9d1f | ||
|
|
a6dd970859 | ||
|
|
aac730f5b1 | ||
|
|
ff95d9328e | ||
|
|
afe1d8cf52 | ||
|
|
67b819f3de | ||
|
|
9b6acb6b95 | ||
|
|
a9a59e1e34 | ||
|
|
5b05397356 | ||
|
|
7a7dbc0cfa | ||
|
|
6ac0ba6efe | ||
|
|
d3d008efb4 | ||
|
|
4f1528128a | ||
|
|
93c4326206 | ||
|
|
0fca7fe524 | ||
|
|
afdcab10c6 | ||
|
|
f8cc5eabe6 | ||
|
|
f304eb7633 | ||
|
|
827204e082 | ||
|
|
641d7ee8c8 |
20
README.md
20
README.md
@@ -10,28 +10,28 @@ A collection of enhancements, plugins, and prompts for [OpenWebUI](https://githu
|
||||
<!-- STATS_START -->
|
||||
## 📊 Community Stats
|
||||
|
||||
> 🕐 Auto-updated: 2026-01-17 17:07
|
||||
> 🕐 Auto-updated: 2026-01-19 18:11
|
||||
|
||||
| 👤 Author | 👥 Followers | ⭐ Points | 🏆 Contributions |
|
||||
|:---:|:---:|:---:|:---:|
|
||||
| [Fu-Jie](https://openwebui.com/u/Fu-Jie) | **118** | **108** | **25** |
|
||||
| [Fu-Jie](https://openwebui.com/u/Fu-Jie) | **133** | **134** | **25** |
|
||||
|
||||
| 📝 Posts | ⬇️ Downloads | 👁️ Views | 👍 Upvotes | 💾 Saves |
|
||||
|:---:|:---:|:---:|:---:|:---:|
|
||||
| **16** | **1622** | **19716** | **94** | **123** |
|
||||
| **16** | **1792** | **21276** | **120** | **135** |
|
||||
|
||||
### 🔥 Top 6 Popular Plugins
|
||||
|
||||
> 🕐 Auto-updated: 2026-01-17 17:07
|
||||
> 🕐 Auto-updated: 2026-01-19 18:11
|
||||
|
||||
| Rank | Plugin | Version | Downloads | Views | Updated |
|
||||
|:---:|------|:---:|:---:|:---:|:---:|
|
||||
| 🥇 | [Smart Mind Map](https://openwebui.com/posts/turn_any_text_into_beautiful_mind_maps_3094c59a) | 0.9.1 | 503 | 4601 | 2026-01-17 |
|
||||
| 🥈 | [📊 Smart Infographic (AntV)](https://openwebui.com/posts/smart_infographic_ad6f0c7f) | 1.4.9 | 217 | 2232 | 2026-01-17 |
|
||||
| 🥉 | [Export to Excel](https://openwebui.com/posts/export_mulit_table_to_excel_244b8f9d) | 0.3.7 | 202 | 747 | 2026-01-07 |
|
||||
| 4️⃣ | [Async Context Compression](https://openwebui.com/posts/async_context_compression_b1655bc8) | 1.1.3 | 171 | 1889 | 2026-01-17 |
|
||||
| 5️⃣ | [Flash Card](https://openwebui.com/posts/flash_card_65a2ea8f) | 0.2.4 | 131 | 2254 | 2026-01-17 |
|
||||
| 6️⃣ | [Export to Word (Enhanced)](https://openwebui.com/posts/export_to_word_enhanced_formatting_fca6a315) | 0.4.3 | 130 | 1234 | 2026-01-17 |
|
||||
| 🥇 | [Smart Mind Map](https://openwebui.com/posts/turn_any_text_into_beautiful_mind_maps_3094c59a) | 0.9.1 | 532 | 4822 | 2026-01-17 |
|
||||
| 🥈 | [📊 Smart Infographic (AntV)](https://openwebui.com/posts/smart_infographic_ad6f0c7f) | 1.4.9 | 260 | 2514 | 2026-01-18 |
|
||||
| 🥉 | [Export to Excel](https://openwebui.com/posts/export_mulit_table_to_excel_244b8f9d) | 0.3.7 | 209 | 800 | 2026-01-07 |
|
||||
| 4️⃣ | [Async Context Compression](https://openwebui.com/posts/async_context_compression_b1655bc8) | 1.1.3 | 180 | 1975 | 2026-01-17 |
|
||||
| 5️⃣ | [Export to Word (Enhanced)](https://openwebui.com/posts/export_to_word_enhanced_formatting_fca6a315) | 0.4.3 | 158 | 1377 | 2026-01-17 |
|
||||
| 6️⃣ | [Flash Card](https://openwebui.com/posts/flash_card_65a2ea8f) | 0.2.4 | 138 | 2329 | 2026-01-17 |
|
||||
|
||||
*See full stats in [Community Stats Report](./docs/community-stats.md)*
|
||||
<!-- STATS_END -->
|
||||
|
||||
20
README_CN.md
20
README_CN.md
@@ -7,28 +7,28 @@ OpenWebUI 增强功能集合。包含个人开发与收集的插件、提示词
|
||||
<!-- STATS_START -->
|
||||
## 📊 社区统计
|
||||
|
||||
> 🕐 自动更新于 2026-01-17 17:07
|
||||
> 🕐 自动更新于 2026-01-19 18:11
|
||||
|
||||
| 👤 作者 | 👥 粉丝 | ⭐ 积分 | 🏆 贡献 |
|
||||
|:---:|:---:|:---:|:---:|
|
||||
| [Fu-Jie](https://openwebui.com/u/Fu-Jie) | **118** | **108** | **25** |
|
||||
| [Fu-Jie](https://openwebui.com/u/Fu-Jie) | **133** | **134** | **25** |
|
||||
|
||||
| 📝 发布 | ⬇️ 下载 | 👁️ 浏览 | 👍 点赞 | 💾 收藏 |
|
||||
|:---:|:---:|:---:|:---:|:---:|
|
||||
| **16** | **1622** | **19716** | **94** | **123** |
|
||||
| **16** | **1792** | **21276** | **120** | **135** |
|
||||
|
||||
### 🔥 热门插件 Top 6
|
||||
|
||||
> 🕐 自动更新于 2026-01-17 17:07
|
||||
> 🕐 自动更新于 2026-01-19 18:11
|
||||
|
||||
| 排名 | 插件 | 版本 | 下载 | 浏览 | 更新日期 |
|
||||
|:---:|------|:---:|:---:|:---:|:---:|
|
||||
| 🥇 | [Smart Mind Map](https://openwebui.com/posts/turn_any_text_into_beautiful_mind_maps_3094c59a) | 0.9.1 | 503 | 4601 | 2026-01-17 |
|
||||
| 🥈 | [📊 Smart Infographic (AntV)](https://openwebui.com/posts/smart_infographic_ad6f0c7f) | 1.4.9 | 217 | 2232 | 2026-01-17 |
|
||||
| 🥉 | [Export to Excel](https://openwebui.com/posts/export_mulit_table_to_excel_244b8f9d) | 0.3.7 | 202 | 747 | 2026-01-07 |
|
||||
| 4️⃣ | [Async Context Compression](https://openwebui.com/posts/async_context_compression_b1655bc8) | 1.1.3 | 171 | 1889 | 2026-01-17 |
|
||||
| 5️⃣ | [Flash Card](https://openwebui.com/posts/flash_card_65a2ea8f) | 0.2.4 | 131 | 2254 | 2026-01-17 |
|
||||
| 6️⃣ | [Export to Word (Enhanced)](https://openwebui.com/posts/export_to_word_enhanced_formatting_fca6a315) | 0.4.3 | 130 | 1234 | 2026-01-17 |
|
||||
| 🥇 | [Smart Mind Map](https://openwebui.com/posts/turn_any_text_into_beautiful_mind_maps_3094c59a) | 0.9.1 | 532 | 4822 | 2026-01-17 |
|
||||
| 🥈 | [📊 Smart Infographic (AntV)](https://openwebui.com/posts/smart_infographic_ad6f0c7f) | 1.4.9 | 260 | 2514 | 2026-01-18 |
|
||||
| 🥉 | [Export to Excel](https://openwebui.com/posts/export_mulit_table_to_excel_244b8f9d) | 0.3.7 | 209 | 800 | 2026-01-07 |
|
||||
| 4️⃣ | [Async Context Compression](https://openwebui.com/posts/async_context_compression_b1655bc8) | 1.1.3 | 180 | 1975 | 2026-01-17 |
|
||||
| 5️⃣ | [Export to Word (Enhanced)](https://openwebui.com/posts/export_to_word_enhanced_formatting_fca6a315) | 0.4.3 | 158 | 1377 | 2026-01-17 |
|
||||
| 6️⃣ | [Flash Card](https://openwebui.com/posts/flash_card_65a2ea8f) | 0.2.4 | 138 | 2329 | 2026-01-17 |
|
||||
|
||||
*完整统计请查看 [社区统计报告](./docs/community-stats.zh.md)*
|
||||
<!-- STATS_END -->
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"label": "downloads",
|
||||
"message": "1.6k",
|
||||
"message": "1.8k",
|
||||
"color": "blue",
|
||||
"namedLogo": "openwebui"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"label": "followers",
|
||||
"message": "118",
|
||||
"message": "133",
|
||||
"color": "blue"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"label": "points",
|
||||
"message": "108",
|
||||
"message": "134",
|
||||
"color": "orange"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"label": "upvotes",
|
||||
"message": "94",
|
||||
"message": "120",
|
||||
"color": "brightgreen"
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"total_posts": 16,
|
||||
"total_downloads": 1622,
|
||||
"total_views": 19716,
|
||||
"total_upvotes": 94,
|
||||
"total_downloads": 1792,
|
||||
"total_views": 21276,
|
||||
"total_upvotes": 120,
|
||||
"total_downvotes": 2,
|
||||
"total_saves": 123,
|
||||
"total_comments": 23,
|
||||
"total_saves": 135,
|
||||
"total_comments": 24,
|
||||
"by_type": {
|
||||
"action": 14,
|
||||
"unknown": 2
|
||||
@@ -18,9 +18,9 @@
|
||||
"version": "0.9.1",
|
||||
"author": "Fu-Jie",
|
||||
"description": "Intelligently analyzes text content and generates interactive mind maps to help users structure and visualize knowledge.",
|
||||
"downloads": 503,
|
||||
"views": 4601,
|
||||
"upvotes": 13,
|
||||
"downloads": 532,
|
||||
"views": 4822,
|
||||
"upvotes": 15,
|
||||
"saves": 28,
|
||||
"comments": 11,
|
||||
"created_at": "2025-12-30",
|
||||
@@ -34,13 +34,13 @@
|
||||
"version": "1.4.9",
|
||||
"author": "Fu-Jie",
|
||||
"description": "AI-powered infographic generator based on AntV Infographic. Supports professional templates, auto-icon matching, and SVG/PNG downloads.",
|
||||
"downloads": 217,
|
||||
"views": 2232,
|
||||
"upvotes": 10,
|
||||
"saves": 15,
|
||||
"comments": 2,
|
||||
"downloads": 260,
|
||||
"views": 2514,
|
||||
"upvotes": 14,
|
||||
"saves": 20,
|
||||
"comments": 3,
|
||||
"created_at": "2025-12-28",
|
||||
"updated_at": "2026-01-17",
|
||||
"updated_at": "2026-01-18",
|
||||
"url": "https://openwebui.com/posts/smart_infographic_ad6f0c7f"
|
||||
},
|
||||
{
|
||||
@@ -50,9 +50,9 @@
|
||||
"version": "0.3.7",
|
||||
"author": "Fu-Jie",
|
||||
"description": "Extracts tables from chat messages and exports them to Excel (.xlsx) files with smart formatting.",
|
||||
"downloads": 202,
|
||||
"views": 747,
|
||||
"upvotes": 3,
|
||||
"downloads": 209,
|
||||
"views": 800,
|
||||
"upvotes": 4,
|
||||
"saves": 5,
|
||||
"comments": 0,
|
||||
"created_at": "2025-05-30",
|
||||
@@ -66,31 +66,15 @@
|
||||
"version": "1.1.3",
|
||||
"author": "Fu-Jie",
|
||||
"description": "Reduces token consumption in long conversations while maintaining coherence through intelligent summarization and message compression.",
|
||||
"downloads": 171,
|
||||
"views": 1889,
|
||||
"upvotes": 7,
|
||||
"saves": 18,
|
||||
"downloads": 180,
|
||||
"views": 1975,
|
||||
"upvotes": 9,
|
||||
"saves": 19,
|
||||
"comments": 0,
|
||||
"created_at": "2025-11-08",
|
||||
"updated_at": "2026-01-17",
|
||||
"url": "https://openwebui.com/posts/async_context_compression_b1655bc8"
|
||||
},
|
||||
{
|
||||
"title": "Flash Card",
|
||||
"slug": "flash_card_65a2ea8f",
|
||||
"type": "action",
|
||||
"version": "0.2.4",
|
||||
"author": "Fu-Jie",
|
||||
"description": "Quickly generates beautiful flashcards from text, extracting key points and categories.",
|
||||
"downloads": 131,
|
||||
"views": 2254,
|
||||
"upvotes": 8,
|
||||
"saves": 10,
|
||||
"comments": 2,
|
||||
"created_at": "2025-12-30",
|
||||
"updated_at": "2026-01-17",
|
||||
"url": "https://openwebui.com/posts/flash_card_65a2ea8f"
|
||||
},
|
||||
{
|
||||
"title": "Export to Word (Enhanced)",
|
||||
"slug": "export_to_word_enhanced_formatting_fca6a315",
|
||||
@@ -98,42 +82,42 @@
|
||||
"version": "0.4.3",
|
||||
"author": "Fu-Jie",
|
||||
"description": "Export current conversation from Markdown to Word (.docx) with Mermaid diagrams rendered client-side (Mermaid.js, SVG+PNG), LaTeX math, real hyperlinks, improved tables, syntax highlighting, and blockquote support.",
|
||||
"downloads": 130,
|
||||
"views": 1234,
|
||||
"upvotes": 6,
|
||||
"saves": 14,
|
||||
"downloads": 158,
|
||||
"views": 1377,
|
||||
"upvotes": 8,
|
||||
"saves": 16,
|
||||
"comments": 0,
|
||||
"created_at": "2026-01-03",
|
||||
"updated_at": "2026-01-17",
|
||||
"url": "https://openwebui.com/posts/export_to_word_enhanced_formatting_fca6a315"
|
||||
},
|
||||
{
|
||||
"title": "导出为 Word (增强版)",
|
||||
"slug": "导出为_word_支持公式流程图表格和代码块_8a6306c0",
|
||||
"title": "Flash Card",
|
||||
"slug": "flash_card_65a2ea8f",
|
||||
"type": "action",
|
||||
"version": "0.4.3",
|
||||
"version": "0.2.4",
|
||||
"author": "Fu-Jie",
|
||||
"description": "将对话导出为 Word (.docx),支持 Mermaid 图表 (客户端渲染 SVG+PNG)、LaTeX 数学公式、真实超链接、增强表格格式、代码高亮和引用块。",
|
||||
"downloads": 58,
|
||||
"views": 1246,
|
||||
"upvotes": 9,
|
||||
"saves": 3,
|
||||
"comments": 1,
|
||||
"created_at": "2026-01-04",
|
||||
"description": "Quickly generates beautiful flashcards from text, extracting key points and categories.",
|
||||
"downloads": 138,
|
||||
"views": 2329,
|
||||
"upvotes": 10,
|
||||
"saves": 10,
|
||||
"comments": 2,
|
||||
"created_at": "2025-12-30",
|
||||
"updated_at": "2026-01-17",
|
||||
"url": "https://openwebui.com/posts/导出为_word_支持公式流程图表格和代码块_8a6306c0"
|
||||
"url": "https://openwebui.com/posts/flash_card_65a2ea8f"
|
||||
},
|
||||
{
|
||||
"title": "Markdown Normalizer",
|
||||
"slug": "markdown_normalizer_baaa8732",
|
||||
"type": "action",
|
||||
"version": "1.2.0",
|
||||
"version": "1.2.3",
|
||||
"author": "Fu-Jie",
|
||||
"description": "A content normalizer filter that fixes common Markdown formatting issues in LLM outputs, such as broken code blocks, LaTeX formulas, and list formatting.",
|
||||
"downloads": 57,
|
||||
"views": 1681,
|
||||
"upvotes": 8,
|
||||
"saves": 14,
|
||||
"downloads": 84,
|
||||
"views": 2100,
|
||||
"upvotes": 10,
|
||||
"saves": 17,
|
||||
"comments": 5,
|
||||
"created_at": "2026-01-12",
|
||||
"updated_at": "2026-01-17",
|
||||
@@ -146,15 +130,31 @@
|
||||
"version": "1.0.0",
|
||||
"author": "Fu-Jie",
|
||||
"description": "A comprehensive thinking lens that dives deep into any content - from context to logic, insights, and action paths.",
|
||||
"downloads": 57,
|
||||
"views": 597,
|
||||
"upvotes": 3,
|
||||
"saves": 5,
|
||||
"downloads": 68,
|
||||
"views": 663,
|
||||
"upvotes": 4,
|
||||
"saves": 6,
|
||||
"comments": 0,
|
||||
"created_at": "2026-01-08",
|
||||
"updated_at": "2026-01-08",
|
||||
"url": "https://openwebui.com/posts/deep_dive_c0b846e4"
|
||||
},
|
||||
{
|
||||
"title": "导出为 Word (增强版)",
|
||||
"slug": "导出为_word_支持公式流程图表格和代码块_8a6306c0",
|
||||
"type": "action",
|
||||
"version": "0.4.3",
|
||||
"author": "Fu-Jie",
|
||||
"description": "将对话导出为 Word (.docx),支持 Mermaid 图表 (客户端渲染 SVG+PNG)、LaTeX 数学公式、真实超链接、增强表格格式、代码高亮和引用块。",
|
||||
"downloads": 63,
|
||||
"views": 1305,
|
||||
"upvotes": 11,
|
||||
"saves": 3,
|
||||
"comments": 1,
|
||||
"created_at": "2026-01-04",
|
||||
"updated_at": "2026-01-17",
|
||||
"url": "https://openwebui.com/posts/导出为_word_支持公式流程图表格和代码块_8a6306c0"
|
||||
},
|
||||
{
|
||||
"title": "📊 智能信息图 (AntV Infographic)",
|
||||
"slug": "智能信息图_e04a48ff",
|
||||
@@ -162,9 +162,9 @@
|
||||
"version": "1.4.9",
|
||||
"author": "Fu-Jie",
|
||||
"description": "基于 AntV Infographic 的智能信息图生成插件。支持多种专业模板,自动图标匹配,并提供 SVG/PNG 下载功能。",
|
||||
"downloads": 41,
|
||||
"views": 650,
|
||||
"upvotes": 4,
|
||||
"downloads": 42,
|
||||
"views": 683,
|
||||
"upvotes": 6,
|
||||
"saves": 0,
|
||||
"comments": 0,
|
||||
"created_at": "2025-12-28",
|
||||
@@ -179,14 +179,30 @@
|
||||
"author": "Fu-Jie",
|
||||
"description": "智能分析文本内容,生成交互式思维导图,帮助用户结构化和可视化知识。",
|
||||
"downloads": 22,
|
||||
"views": 389,
|
||||
"upvotes": 2,
|
||||
"views": 398,
|
||||
"upvotes": 3,
|
||||
"saves": 1,
|
||||
"comments": 0,
|
||||
"created_at": "2025-12-31",
|
||||
"updated_at": "2026-01-17",
|
||||
"url": "https://openwebui.com/posts/智能生成交互式思维导图帮助用户可视化知识_8d4b097b"
|
||||
},
|
||||
{
|
||||
"title": "闪记卡 (Flash Card)",
|
||||
"slug": "闪记卡生成插件_4a31eac3",
|
||||
"type": "action",
|
||||
"version": "0.2.4",
|
||||
"author": "Fu-Jie",
|
||||
"description": "快速将文本提炼为精美的学习记忆卡片,支持核心要点提取与分类。",
|
||||
"downloads": 16,
|
||||
"views": 443,
|
||||
"upvotes": 5,
|
||||
"saves": 1,
|
||||
"comments": 0,
|
||||
"created_at": "2025-12-30",
|
||||
"updated_at": "2026-01-17",
|
||||
"url": "https://openwebui.com/posts/闪记卡生成插件_4a31eac3"
|
||||
},
|
||||
{
|
||||
"title": "异步上下文压缩",
|
||||
"slug": "异步上下文压缩_5c0617cb",
|
||||
@@ -195,30 +211,14 @@
|
||||
"author": "Fu-Jie",
|
||||
"description": "通过智能摘要和消息压缩,降低长对话的 token 消耗,同时保持对话连贯性。",
|
||||
"downloads": 14,
|
||||
"views": 337,
|
||||
"upvotes": 4,
|
||||
"views": 351,
|
||||
"upvotes": 5,
|
||||
"saves": 1,
|
||||
"comments": 0,
|
||||
"created_at": "2025-11-08",
|
||||
"updated_at": "2026-01-17",
|
||||
"url": "https://openwebui.com/posts/异步上下文压缩_5c0617cb"
|
||||
},
|
||||
{
|
||||
"title": "闪记卡 (Flash Card)",
|
||||
"slug": "闪记卡生成插件_4a31eac3",
|
||||
"type": "action",
|
||||
"version": "0.2.4",
|
||||
"author": "Fu-Jie",
|
||||
"description": "快速将文本提炼为精美的学习记忆卡片,支持核心要点提取与分类。",
|
||||
"downloads": 13,
|
||||
"views": 423,
|
||||
"upvotes": 4,
|
||||
"saves": 1,
|
||||
"comments": 0,
|
||||
"created_at": "2025-12-30",
|
||||
"updated_at": "2026-01-17",
|
||||
"url": "https://openwebui.com/posts/闪记卡生成插件_4a31eac3"
|
||||
},
|
||||
{
|
||||
"title": "精读",
|
||||
"slug": "精读_99830b0f",
|
||||
@@ -227,8 +227,8 @@
|
||||
"author": "Fu-Jie",
|
||||
"description": "全方位的思维透镜 —— 从背景全景到逻辑脉络,从深度洞察到行动路径。",
|
||||
"downloads": 6,
|
||||
"views": 254,
|
||||
"upvotes": 2,
|
||||
"views": 259,
|
||||
"upvotes": 3,
|
||||
"saves": 1,
|
||||
"comments": 0,
|
||||
"created_at": "2026-01-08",
|
||||
@@ -243,8 +243,8 @@
|
||||
"author": "",
|
||||
"description": "",
|
||||
"downloads": 0,
|
||||
"views": 43,
|
||||
"upvotes": 0,
|
||||
"views": 59,
|
||||
"upvotes": 1,
|
||||
"saves": 0,
|
||||
"comments": 0,
|
||||
"created_at": "2026-01-14",
|
||||
@@ -259,8 +259,8 @@
|
||||
"author": "",
|
||||
"description": "",
|
||||
"downloads": 0,
|
||||
"views": 1139,
|
||||
"upvotes": 11,
|
||||
"views": 1198,
|
||||
"upvotes": 12,
|
||||
"saves": 7,
|
||||
"comments": 2,
|
||||
"created_at": "2026-01-10",
|
||||
@@ -273,10 +273,10 @@
|
||||
"name": "Fu-Jie",
|
||||
"profile_url": "https://openwebui.com/u/Fu-Jie",
|
||||
"profile_image": "https://community.s3.openwebui.com/uploads/users/b15d1348-4347-42b4-b815-e053342d6cb0/profile_d9510745-4bd4-4f8f-a997-4a21847d9300.webp",
|
||||
"followers": 118,
|
||||
"followers": 133,
|
||||
"following": 2,
|
||||
"total_points": 108,
|
||||
"post_points": 92,
|
||||
"total_points": 134,
|
||||
"post_points": 118,
|
||||
"comment_points": 16,
|
||||
"contributions": 25
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
# 📊 OpenWebUI Community Stats Report
|
||||
|
||||
> 📅 Updated: 2026-01-17 17:07
|
||||
> 📅 Updated: 2026-01-19 18:11
|
||||
|
||||
## 📈 Overview
|
||||
|
||||
| Metric | Value |
|
||||
|------|------|
|
||||
| 📝 Total Posts | 16 |
|
||||
| ⬇️ Total Downloads | 1622 |
|
||||
| 👁️ Total Views | 19716 |
|
||||
| 👍 Total Upvotes | 94 |
|
||||
| 💾 Total Saves | 123 |
|
||||
| 💬 Total Comments | 23 |
|
||||
| ⬇️ Total Downloads | 1792 |
|
||||
| 👁️ Total Views | 21276 |
|
||||
| 👍 Total Upvotes | 120 |
|
||||
| 💾 Total Saves | 135 |
|
||||
| 💬 Total Comments | 24 |
|
||||
|
||||
## 📂 By Type
|
||||
|
||||
@@ -22,19 +22,19 @@
|
||||
|
||||
| Rank | Title | Type | Version | Downloads | Views | Upvotes | Saves | Updated |
|
||||
|:---:|------|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
|
||||
| 1 | [Smart Mind Map](https://openwebui.com/posts/turn_any_text_into_beautiful_mind_maps_3094c59a) | action | 0.9.1 | 503 | 4601 | 13 | 28 | 2026-01-17 |
|
||||
| 2 | [📊 Smart Infographic (AntV)](https://openwebui.com/posts/smart_infographic_ad6f0c7f) | action | 1.4.9 | 217 | 2232 | 10 | 15 | 2026-01-17 |
|
||||
| 3 | [Export to Excel](https://openwebui.com/posts/export_mulit_table_to_excel_244b8f9d) | action | 0.3.7 | 202 | 747 | 3 | 5 | 2026-01-07 |
|
||||
| 4 | [Async Context Compression](https://openwebui.com/posts/async_context_compression_b1655bc8) | action | 1.1.3 | 171 | 1889 | 7 | 18 | 2026-01-17 |
|
||||
| 5 | [Flash Card](https://openwebui.com/posts/flash_card_65a2ea8f) | action | 0.2.4 | 131 | 2254 | 8 | 10 | 2026-01-17 |
|
||||
| 6 | [Export to Word (Enhanced)](https://openwebui.com/posts/export_to_word_enhanced_formatting_fca6a315) | action | 0.4.3 | 130 | 1234 | 6 | 14 | 2026-01-17 |
|
||||
| 7 | [导出为 Word (增强版)](https://openwebui.com/posts/导出为_word_支持公式流程图表格和代码块_8a6306c0) | action | 0.4.3 | 58 | 1246 | 9 | 3 | 2026-01-17 |
|
||||
| 8 | [Markdown Normalizer](https://openwebui.com/posts/markdown_normalizer_baaa8732) | action | 1.2.0 | 57 | 1681 | 8 | 14 | 2026-01-17 |
|
||||
| 9 | [Deep Dive](https://openwebui.com/posts/deep_dive_c0b846e4) | action | 1.0.0 | 57 | 597 | 3 | 5 | 2026-01-08 |
|
||||
| 10 | [📊 智能信息图 (AntV Infographic)](https://openwebui.com/posts/智能信息图_e04a48ff) | action | 1.4.9 | 41 | 650 | 4 | 0 | 2026-01-17 |
|
||||
| 11 | [思维导图](https://openwebui.com/posts/智能生成交互式思维导图帮助用户可视化知识_8d4b097b) | action | 0.9.1 | 22 | 389 | 2 | 1 | 2026-01-17 |
|
||||
| 12 | [异步上下文压缩](https://openwebui.com/posts/异步上下文压缩_5c0617cb) | action | 1.1.3 | 14 | 337 | 4 | 1 | 2026-01-17 |
|
||||
| 13 | [闪记卡 (Flash Card)](https://openwebui.com/posts/闪记卡生成插件_4a31eac3) | action | 0.2.4 | 13 | 423 | 4 | 1 | 2026-01-17 |
|
||||
| 14 | [精读](https://openwebui.com/posts/精读_99830b0f) | action | 1.0.0 | 6 | 254 | 2 | 1 | 2026-01-08 |
|
||||
| 15 | [Review of Claude Haiku 4.5](https://openwebui.com/posts/review_of_claude_haiku_45_41b0db39) | unknown | | 0 | 43 | 0 | 0 | 2026-01-14 |
|
||||
| 16 | [ 🛠️ Debug Open WebUI Plugins in Your Browser](https://openwebui.com/posts/debug_open_webui_plugins_in_your_browser_81bf7960) | unknown | | 0 | 1139 | 11 | 7 | 2026-01-10 |
|
||||
| 1 | [Smart Mind Map](https://openwebui.com/posts/turn_any_text_into_beautiful_mind_maps_3094c59a) | action | 0.9.1 | 532 | 4822 | 15 | 28 | 2026-01-17 |
|
||||
| 2 | [📊 Smart Infographic (AntV)](https://openwebui.com/posts/smart_infographic_ad6f0c7f) | action | 1.4.9 | 260 | 2514 | 14 | 20 | 2026-01-18 |
|
||||
| 3 | [Export to Excel](https://openwebui.com/posts/export_mulit_table_to_excel_244b8f9d) | action | 0.3.7 | 209 | 800 | 4 | 5 | 2026-01-07 |
|
||||
| 4 | [Async Context Compression](https://openwebui.com/posts/async_context_compression_b1655bc8) | action | 1.1.3 | 180 | 1975 | 9 | 19 | 2026-01-17 |
|
||||
| 5 | [Export to Word (Enhanced)](https://openwebui.com/posts/export_to_word_enhanced_formatting_fca6a315) | action | 0.4.3 | 158 | 1377 | 8 | 16 | 2026-01-17 |
|
||||
| 6 | [Flash Card](https://openwebui.com/posts/flash_card_65a2ea8f) | action | 0.2.4 | 138 | 2329 | 10 | 10 | 2026-01-17 |
|
||||
| 7 | [Markdown Normalizer](https://openwebui.com/posts/markdown_normalizer_baaa8732) | action | 1.2.3 | 84 | 2100 | 10 | 17 | 2026-01-17 |
|
||||
| 8 | [Deep Dive](https://openwebui.com/posts/deep_dive_c0b846e4) | action | 1.0.0 | 68 | 663 | 4 | 6 | 2026-01-08 |
|
||||
| 9 | [导出为 Word (增强版)](https://openwebui.com/posts/导出为_word_支持公式流程图表格和代码块_8a6306c0) | action | 0.4.3 | 63 | 1305 | 11 | 3 | 2026-01-17 |
|
||||
| 10 | [📊 智能信息图 (AntV Infographic)](https://openwebui.com/posts/智能信息图_e04a48ff) | action | 1.4.9 | 42 | 683 | 6 | 0 | 2026-01-17 |
|
||||
| 11 | [思维导图](https://openwebui.com/posts/智能生成交互式思维导图帮助用户可视化知识_8d4b097b) | action | 0.9.1 | 22 | 398 | 3 | 1 | 2026-01-17 |
|
||||
| 12 | [闪记卡 (Flash Card)](https://openwebui.com/posts/闪记卡生成插件_4a31eac3) | action | 0.2.4 | 16 | 443 | 5 | 1 | 2026-01-17 |
|
||||
| 13 | [异步上下文压缩](https://openwebui.com/posts/异步上下文压缩_5c0617cb) | action | 1.1.3 | 14 | 351 | 5 | 1 | 2026-01-17 |
|
||||
| 14 | [精读](https://openwebui.com/posts/精读_99830b0f) | action | 1.0.0 | 6 | 259 | 3 | 1 | 2026-01-08 |
|
||||
| 15 | [Review of Claude Haiku 4.5](https://openwebui.com/posts/review_of_claude_haiku_45_41b0db39) | unknown | | 0 | 59 | 1 | 0 | 2026-01-14 |
|
||||
| 16 | [ 🛠️ Debug Open WebUI Plugins in Your Browser](https://openwebui.com/posts/debug_open_webui_plugins_in_your_browser_81bf7960) | unknown | | 0 | 1198 | 12 | 7 | 2026-01-10 |
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
# 📊 OpenWebUI 社区统计报告
|
||||
|
||||
> 📅 更新时间: 2026-01-17 17:07
|
||||
> 📅 更新时间: 2026-01-19 18:11
|
||||
|
||||
## 📈 总览
|
||||
|
||||
| 指标 | 数值 |
|
||||
|------|------|
|
||||
| 📝 发布数量 | 16 |
|
||||
| ⬇️ 总下载量 | 1622 |
|
||||
| 👁️ 总浏览量 | 19716 |
|
||||
| 👍 总点赞数 | 94 |
|
||||
| 💾 总收藏数 | 123 |
|
||||
| 💬 总评论数 | 23 |
|
||||
| ⬇️ 总下载量 | 1792 |
|
||||
| 👁️ 总浏览量 | 21276 |
|
||||
| 👍 总点赞数 | 120 |
|
||||
| 💾 总收藏数 | 135 |
|
||||
| 💬 总评论数 | 24 |
|
||||
|
||||
## 📂 按类型分类
|
||||
|
||||
@@ -22,19 +22,19 @@
|
||||
|
||||
| 排名 | 标题 | 类型 | 版本 | 下载 | 浏览 | 点赞 | 收藏 | 更新日期 |
|
||||
|:---:|------|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
|
||||
| 1 | [Smart Mind Map](https://openwebui.com/posts/turn_any_text_into_beautiful_mind_maps_3094c59a) | action | 0.9.1 | 503 | 4601 | 13 | 28 | 2026-01-17 |
|
||||
| 2 | [📊 Smart Infographic (AntV)](https://openwebui.com/posts/smart_infographic_ad6f0c7f) | action | 1.4.9 | 217 | 2232 | 10 | 15 | 2026-01-17 |
|
||||
| 3 | [Export to Excel](https://openwebui.com/posts/export_mulit_table_to_excel_244b8f9d) | action | 0.3.7 | 202 | 747 | 3 | 5 | 2026-01-07 |
|
||||
| 4 | [Async Context Compression](https://openwebui.com/posts/async_context_compression_b1655bc8) | action | 1.1.3 | 171 | 1889 | 7 | 18 | 2026-01-17 |
|
||||
| 5 | [Flash Card](https://openwebui.com/posts/flash_card_65a2ea8f) | action | 0.2.4 | 131 | 2254 | 8 | 10 | 2026-01-17 |
|
||||
| 6 | [Export to Word (Enhanced)](https://openwebui.com/posts/export_to_word_enhanced_formatting_fca6a315) | action | 0.4.3 | 130 | 1234 | 6 | 14 | 2026-01-17 |
|
||||
| 7 | [导出为 Word (增强版)](https://openwebui.com/posts/导出为_word_支持公式流程图表格和代码块_8a6306c0) | action | 0.4.3 | 58 | 1246 | 9 | 3 | 2026-01-17 |
|
||||
| 8 | [Markdown Normalizer](https://openwebui.com/posts/markdown_normalizer_baaa8732) | action | 1.2.0 | 57 | 1681 | 8 | 14 | 2026-01-17 |
|
||||
| 9 | [Deep Dive](https://openwebui.com/posts/deep_dive_c0b846e4) | action | 1.0.0 | 57 | 597 | 3 | 5 | 2026-01-08 |
|
||||
| 10 | [📊 智能信息图 (AntV Infographic)](https://openwebui.com/posts/智能信息图_e04a48ff) | action | 1.4.9 | 41 | 650 | 4 | 0 | 2026-01-17 |
|
||||
| 11 | [思维导图](https://openwebui.com/posts/智能生成交互式思维导图帮助用户可视化知识_8d4b097b) | action | 0.9.1 | 22 | 389 | 2 | 1 | 2026-01-17 |
|
||||
| 12 | [异步上下文压缩](https://openwebui.com/posts/异步上下文压缩_5c0617cb) | action | 1.1.3 | 14 | 337 | 4 | 1 | 2026-01-17 |
|
||||
| 13 | [闪记卡 (Flash Card)](https://openwebui.com/posts/闪记卡生成插件_4a31eac3) | action | 0.2.4 | 13 | 423 | 4 | 1 | 2026-01-17 |
|
||||
| 14 | [精读](https://openwebui.com/posts/精读_99830b0f) | action | 1.0.0 | 6 | 254 | 2 | 1 | 2026-01-08 |
|
||||
| 15 | [Review of Claude Haiku 4.5](https://openwebui.com/posts/review_of_claude_haiku_45_41b0db39) | unknown | | 0 | 43 | 0 | 0 | 2026-01-14 |
|
||||
| 16 | [ 🛠️ Debug Open WebUI Plugins in Your Browser](https://openwebui.com/posts/debug_open_webui_plugins_in_your_browser_81bf7960) | unknown | | 0 | 1139 | 11 | 7 | 2026-01-10 |
|
||||
| 1 | [Smart Mind Map](https://openwebui.com/posts/turn_any_text_into_beautiful_mind_maps_3094c59a) | action | 0.9.1 | 532 | 4822 | 15 | 28 | 2026-01-17 |
|
||||
| 2 | [📊 Smart Infographic (AntV)](https://openwebui.com/posts/smart_infographic_ad6f0c7f) | action | 1.4.9 | 260 | 2514 | 14 | 20 | 2026-01-18 |
|
||||
| 3 | [Export to Excel](https://openwebui.com/posts/export_mulit_table_to_excel_244b8f9d) | action | 0.3.7 | 209 | 800 | 4 | 5 | 2026-01-07 |
|
||||
| 4 | [Async Context Compression](https://openwebui.com/posts/async_context_compression_b1655bc8) | action | 1.1.3 | 180 | 1975 | 9 | 19 | 2026-01-17 |
|
||||
| 5 | [Export to Word (Enhanced)](https://openwebui.com/posts/export_to_word_enhanced_formatting_fca6a315) | action | 0.4.3 | 158 | 1377 | 8 | 16 | 2026-01-17 |
|
||||
| 6 | [Flash Card](https://openwebui.com/posts/flash_card_65a2ea8f) | action | 0.2.4 | 138 | 2329 | 10 | 10 | 2026-01-17 |
|
||||
| 7 | [Markdown Normalizer](https://openwebui.com/posts/markdown_normalizer_baaa8732) | action | 1.2.3 | 84 | 2100 | 10 | 17 | 2026-01-17 |
|
||||
| 8 | [Deep Dive](https://openwebui.com/posts/deep_dive_c0b846e4) | action | 1.0.0 | 68 | 663 | 4 | 6 | 2026-01-08 |
|
||||
| 9 | [导出为 Word (增强版)](https://openwebui.com/posts/导出为_word_支持公式流程图表格和代码块_8a6306c0) | action | 0.4.3 | 63 | 1305 | 11 | 3 | 2026-01-17 |
|
||||
| 10 | [📊 智能信息图 (AntV Infographic)](https://openwebui.com/posts/智能信息图_e04a48ff) | action | 1.4.9 | 42 | 683 | 6 | 0 | 2026-01-17 |
|
||||
| 11 | [思维导图](https://openwebui.com/posts/智能生成交互式思维导图帮助用户可视化知识_8d4b097b) | action | 0.9.1 | 22 | 398 | 3 | 1 | 2026-01-17 |
|
||||
| 12 | [闪记卡 (Flash Card)](https://openwebui.com/posts/闪记卡生成插件_4a31eac3) | action | 0.2.4 | 16 | 443 | 5 | 1 | 2026-01-17 |
|
||||
| 13 | [异步上下文压缩](https://openwebui.com/posts/异步上下文压缩_5c0617cb) | action | 1.1.3 | 14 | 351 | 5 | 1 | 2026-01-17 |
|
||||
| 14 | [精读](https://openwebui.com/posts/精读_99830b0f) | action | 1.0.0 | 6 | 259 | 3 | 1 | 2026-01-08 |
|
||||
| 15 | [Review of Claude Haiku 4.5](https://openwebui.com/posts/review_of_claude_haiku_45_41b0db39) | unknown | | 0 | 59 | 1 | 0 | 2026-01-14 |
|
||||
| 16 | [ 🛠️ Debug Open WebUI Plugins in Your Browser](https://openwebui.com/posts/debug_open_webui_plugins_in_your_browser_81bf7960) | unknown | | 0 | 1198 | 12 | 7 | 2026-01-10 |
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Async Context Compression
|
||||
|
||||
<span class="category-badge filter">Filter</span>
|
||||
<span class="version-badge">v1.1.3</span>
|
||||
<span class="version-badge">v1.2.0</span>
|
||||
|
||||
Reduces token consumption in long conversations through intelligent summarization while maintaining conversational coherence.
|
||||
|
||||
@@ -34,6 +34,10 @@ This is especially useful for:
|
||||
- :material-check-all: **Open WebUI v0.7.x Compatibility**: Dynamic DB session handling
|
||||
- :material-account-convert: **Improved Compatibility**: Summary role changed to `assistant`
|
||||
- :material-shield-check: **Enhanced Stability**: Resolved race conditions in state management
|
||||
- :material-ruler: **Preflight Context Check**: Validates context fit before sending
|
||||
- :material-format-align-justify: **Structure-Aware Trimming**: Preserves document structure
|
||||
- :material-content-cut: **Native Tool Output Trimming**: Trims verbose tool outputs (Note: Non-native tool outputs are not fully injected into context)
|
||||
- :material-chart-bar: **Detailed Token Logging**: Granular token breakdown
|
||||
|
||||
---
|
||||
|
||||
@@ -64,10 +68,13 @@ graph TD
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
|--------|------|---------|-------------|
|
||||
| `token_threshold` | integer | `4000` | Trigger compression above this token count |
|
||||
| `preserve_recent` | integer | `5` | Number of recent messages to keep uncompressed |
|
||||
| `summary_model` | string | `"auto"` | Model to use for summarization |
|
||||
| `compression_ratio` | float | `0.3` | Target compression ratio |
|
||||
| `compression_threshold_tokens` | integer | `64000` | Trigger compression above this token count |
|
||||
| `max_context_tokens` | integer | `128000` | Hard limit for context |
|
||||
| `keep_first` | integer | `1` | Always keep the first N messages |
|
||||
| `keep_last` | integer | `6` | Always keep the last N messages |
|
||||
| `summary_model` | string | `None` | Model to use for summarization |
|
||||
| `max_summary_tokens` | integer | `16384` | Maximum tokens for the summary |
|
||||
| `enable_tool_output_trimming` | boolean | `false` | Enable trimming of large tool outputs |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Async Context Compression(异步上下文压缩)
|
||||
|
||||
<span class="category-badge filter">Filter</span>
|
||||
<span class="version-badge">v1.1.3</span>
|
||||
<span class="version-badge">v1.2.0</span>
|
||||
|
||||
通过智能摘要减少长对话的 token 消耗,同时保持对话连贯。
|
||||
|
||||
@@ -34,6 +34,10 @@ Async Context Compression 过滤器通过以下方式帮助管理长对话的 to
|
||||
- :material-check-all: **Open WebUI v0.7.x 兼容性**:动态数据库会话处理
|
||||
- :material-account-convert: **兼容性提升**:摘要角色改为 `assistant`
|
||||
- :material-shield-check: **稳定性增强**:解决状态管理竞态条件
|
||||
- :material-ruler: **预检上下文检查**:发送前验证上下文是否超限
|
||||
- :material-format-align-justify: **结构感知裁剪**:保留文档结构的智能裁剪
|
||||
- :material-content-cut: **原生工具输出裁剪**:自动裁剪冗长的工具输出(注意:非原生工具调用输出不会完整注入上下文)
|
||||
- :material-chart-bar: **详细 Token 日志**:提供细粒度的 Token 统计
|
||||
|
||||
---
|
||||
|
||||
@@ -64,10 +68,13 @@ graph TD
|
||||
|
||||
| 选项 | 类型 | 默认值 | 说明 |
|
||||
|--------|------|---------|-------------|
|
||||
| `token_threshold` | integer | `4000` | 超过该 token 数触发压缩 |
|
||||
| `preserve_recent` | integer | `5` | 保留不压缩的最近消息数量 |
|
||||
| `summary_model` | string | `"auto"` | 用于摘要的模型 |
|
||||
| `compression_ratio` | float | `0.3` | 目标压缩比例 |
|
||||
| `compression_threshold_tokens` | integer | `64000` | 超过该 token 数触发压缩 |
|
||||
| `max_context_tokens` | integer | `128000` | 上下文硬性上限 |
|
||||
| `keep_first` | integer | `1` | 始终保留的前 N 条消息 |
|
||||
| `keep_last` | integer | `6` | 始终保留的后 N 条消息 |
|
||||
| `summary_model` | string | `None` | 用于摘要的模型 |
|
||||
| `max_summary_tokens` | integer | `16384` | 摘要的最大 token 数 |
|
||||
| `enable_tool_output_trimming` | boolean | `false` | 启用长工具输出裁剪 |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ Filters act as middleware in the message pipeline:
|
||||
|
||||
Fixes common Markdown formatting issues in LLM outputs, including Mermaid syntax, code blocks, and LaTeX formulas.
|
||||
|
||||
**Version:** 1.1.2
|
||||
**Version:** 1.2.4
|
||||
|
||||
[:octicons-arrow-right-24: Documentation](markdown_normalizer.md)
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ Filter 充当消息管线中的中间件:
|
||||
|
||||
修复 LLM 输出中常见的 Markdown 格式问题,包括 Mermaid 语法、代码块和 LaTeX 公式。
|
||||
|
||||
**版本:** 1.0.1
|
||||
**版本:** 1.2.4
|
||||
|
||||
[:octicons-arrow-right-24: 查看文档](markdown_normalizer.zh.md)
|
||||
|
||||
|
||||
@@ -51,9 +51,21 @@ A content normalizer filter for Open WebUI that fixes common Markdown formatting
|
||||
|
||||
## Changelog
|
||||
|
||||
### v1.2.4
|
||||
|
||||
* **Documentation Updates**: Synchronized version numbers across all documentation and code files.
|
||||
|
||||
### v1.2.3
|
||||
|
||||
* **List Marker Protection Enhancement**: Fixed a bug where list markers (`*`) followed by plain text and emphasis were having their spaces incorrectly stripped (e.g., `* U16 forward` became `*U16 forward`).
|
||||
* **Placeholder Support**: Confirmed that 4 or more underscores (e.g., `____`) are correctly treated as placeholders and not modified by the emphasis fix.
|
||||
|
||||
### v1.2.2
|
||||
|
||||
* **Version Bump**: Documentation and metadata updated for the latest release.
|
||||
* **Code Block Indentation Fix**: Fixed an issue where code blocks nested inside lists were having their indentation incorrectly stripped. Now preserves proper indentation for nested code blocks.
|
||||
* **Underscore Emphasis Support**: Extended emphasis spacing fix to support `__` (double underscore for bold) and `___` (triple underscore for bold+italic) syntax.
|
||||
* **List Marker Protection**: Fixed a bug where list markers (`*`) followed by emphasis markers (`**`) were incorrectly merged (e.g., `* **Yes**` became `***Yes**`). Added safeguard to prevent this.
|
||||
* **Test Suite**: Added comprehensive pytest test suite with 56 test cases covering all major features.
|
||||
|
||||
### v1.2.1
|
||||
|
||||
|
||||
@@ -51,9 +51,21 @@
|
||||
|
||||
## 更新日志
|
||||
|
||||
### v1.2.4
|
||||
|
||||
* **文档更新**: 同步了所有文档和代码文件的版本号。
|
||||
|
||||
### v1.2.3
|
||||
|
||||
* **列表标记保护增强**: 修复了列表标记 (`*`) 后跟普通文本和强调标记时,空格被错误剥离的问题(例如 `* U16 前锋` 变成 `*U16 前锋`)。
|
||||
* **占位符支持**: 确认 4 个或更多下划线(如 `____`)会被正确视为占位符,不会被强调修复逻辑修改。
|
||||
|
||||
### v1.2.2
|
||||
|
||||
* **版本更新**: 文档与元数据已同步到最新版本。
|
||||
* **代码块缩进修复**: 修复了列表中嵌套代码块的缩进被错误剥离的问题。现在会正确保留嵌套代码块的缩进。
|
||||
* **下划线强调语法支持**: 扩展强调空格修复以支持 `__` (双下划线加粗) 和 `___` (三下划线加粗斜体) 语法。
|
||||
* **列表标记保护**: 修复了列表标记 (`*`) 后跟强调标记 (`**`) 被错误合并的 Bug(例如 `* **是**` 变成 `***是**`)。添加了保护逻辑防止此问题。
|
||||
* **测试套件**: 新增完整的 pytest 测试套件,包含 56 个测试用例,覆盖所有主要功能。
|
||||
|
||||
### v1.2.1
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 162 KiB After Width: | Height: | Size: 234 KiB |
@@ -1,9 +1,19 @@
|
||||
# Async Context Compression Filter
|
||||
|
||||
**Author:** [Fu-Jie](https://github.com/Fu-Jie/awesome-openwebui) | **Version:** 1.1.3 | **Project:** [Awesome OpenWebUI](https://github.com/Fu-Jie/awesome-openwebui) | **License:** MIT
|
||||
**Author:** [Fu-Jie](https://github.com/Fu-Jie/awesome-openwebui) | **Version:** 1.2.0 | **Project:** [Awesome OpenWebUI](https://github.com/Fu-Jie/awesome-openwebui) | **License:** MIT
|
||||
|
||||
This filter reduces token consumption in long conversations through intelligent summarization and message compression while keeping conversations coherent.
|
||||
|
||||
## What's new in 1.2.0
|
||||
|
||||
- **Preflight Context Check**: Before sending to the model, validates that total tokens fit within the context window. Automatically trims or drops oldest messages if exceeded.
|
||||
- **Structure-Aware Assistant Trimming**: When context exceeds the limit, long AI responses are intelligently collapsed while preserving their structure (headers H1-H6, first line, last line).
|
||||
- **Native Tool Output Trimming**: Detects and trims native tool outputs (`function_calling: "native"`), extracting only the final answer. Enable via `enable_tool_output_trimming`. **Note**: Non-native tool outputs are not fully injected into context.
|
||||
- **Consolidated Status Notifications**: Unified "Context Usage" and "Context Summary Updated" notifications with appended warnings (e.g., `| ⚠️ High Usage`) for clearer feedback.
|
||||
- **Context Usage Warning**: Emits a warning notification when context usage exceeds 90%.
|
||||
- **Enhanced Header Detection**: Optimized regex (`^#{1,6}\s+`) to avoid false positives like `#hashtag`.
|
||||
- **Detailed Token Logging**: Logs now show token breakdown for System, Head, Summary, and Tail sections with total.
|
||||
|
||||
## What's new in 1.1.3
|
||||
- **Improved Compatibility**: Changed summary injection role from `user` to `assistant` for better compatibility across different LLMs.
|
||||
- **Enhanced Stability**: Fixed a race condition in state management that could cause "inlet state not found" warnings in high-concurrency scenarios.
|
||||
@@ -31,6 +41,10 @@ This filter reduces token consumption in long conversations through intelligent
|
||||
- ✅ Persistent storage via Open WebUI's shared database connection (PostgreSQL, SQLite, etc.).
|
||||
- ✅ Flexible retention policy to keep the first and last N messages.
|
||||
- ✅ Smart injection of historical summaries back into the context.
|
||||
- ✅ Structure-aware trimming that preserves document structure (headers, intro, conclusion).
|
||||
- ✅ Native tool output trimming for cleaner context when using function calling.
|
||||
- ✅ Real-time context usage monitoring with warning notifications (>90%).
|
||||
- ✅ Detailed token logging for precise debugging and optimization.
|
||||
|
||||
---
|
||||
|
||||
@@ -64,6 +78,7 @@ It is recommended to keep this filter early in the chain so it runs before filte
|
||||
| `max_summary_tokens` | `4000` | Maximum tokens for the generated summary. |
|
||||
| `summary_temperature` | `0.3` | Randomness for summary generation. Lower is more deterministic. |
|
||||
| `model_thresholds` | `{}` | Per-model overrides for `compression_threshold_tokens` and `max_context_tokens` (useful for mixed models). |
|
||||
| `enable_tool_output_trimming` | `false` | When enabled and `function_calling: "native"` is active, trims verbose tool outputs to extract only the final answer. |
|
||||
| `debug_mode` | `true` | Log verbose debug info. Set to `false` in production. |
|
||||
| `show_debug_log` | `false` | Print debug logs to browser console (F12). Useful for frontend debugging. |
|
||||
|
||||
|
||||
@@ -1,11 +1,21 @@
|
||||
# 异步上下文压缩过滤器
|
||||
|
||||
**作者:** [Fu-Jie](https://github.com/Fu-Jie/awesome-openwebui) | **版本:** 1.1.3 | **项目:** [Awesome OpenWebUI](https://github.com/Fu-Jie/awesome-openwebui) | **许可证:** MIT
|
||||
**作者:** [Fu-Jie](https://github.com/Fu-Jie/awesome-openwebui) | **版本:** 1.2.0 | **项目:** [Awesome OpenWebUI](https://github.com/Fu-Jie/awesome-openwebui) | **许可证:** MIT
|
||||
|
||||
> **重要提示**:为了确保所有过滤器的可维护性和易用性,每个过滤器都应附带清晰、完整的文档,以确保其功能、配置和使用方法得到充分说明。
|
||||
|
||||
本过滤器通过智能摘要和消息压缩技术,在保持对话连贯性的同时,显著降低长对话的 Token 消耗。
|
||||
|
||||
## 1.2.0 版本更新
|
||||
|
||||
- **预检上下文检查 (Preflight Context Check)**: 在发送给模型之前,验证总 Token 是否符合上下文窗口。如果超出,自动裁剪或丢弃最旧的消息。
|
||||
- **结构感知助手裁剪 (Structure-Aware Assistant Trimming)**: 当上下文超出限制时,智能折叠过长的 AI 回复,同时保留其结构(标题 H1-H6、首行、尾行)。
|
||||
- **原生工具输出裁剪 (Native Tool Output Trimming)**: 检测并裁剪原生工具输出 (`function_calling: "native"`),仅提取最终答案。通过 `enable_tool_output_trimming` 启用。**注意**:非原生工具调用输出不会完整注入上下文。
|
||||
- **统一状态通知**: 统一了“上下文使用情况”和“上下文摘要更新”的通知,并附加警告(例如 `| ⚠️ 高负载`),反馈更清晰。
|
||||
- **上下文使用警告**: 当上下文使用率超过 90% 时发出警告通知。
|
||||
- **增强的标题检测**: 优化了正则表达式 (`^#{1,6}\s+`) 以避免误判(如 `#hashtag`)。
|
||||
- **详细 Token 日志**: 日志现在显示 System、Head、Summary 和 Tail 部分的 Token 细分及总计。
|
||||
|
||||
## 1.1.3 版本更新
|
||||
- **兼容性提升**: 将摘要注入角色从 `user` 改为 `assistant`,以提高在不同 LLM 之间的兼容性。
|
||||
- **稳定性增强**: 修复了状态管理中的竞态条件,解决了高并发场景下可能出现的“无法获取 inlet 状态”警告。
|
||||
@@ -33,6 +43,10 @@
|
||||
- ✅ **持久化存储**: 复用 Open WebUI 共享数据库连接,自动支持 PostgreSQL/SQLite 等。
|
||||
- ✅ **灵活保留策略**: 可配置保留对话头部和尾部消息,确保关键信息连贯。
|
||||
- ✅ **智能注入**: 将历史摘要智能注入到新上下文中。
|
||||
- ✅ **结构感知裁剪**: 智能折叠过长消息,保留文档骨架(标题、首尾)。
|
||||
- ✅ **原生工具输出裁剪**: 支持裁剪冗长的工具调用输出。
|
||||
- ✅ **实时监控**: 实时监控上下文使用情况,超过 90% 发出警告。
|
||||
- ✅ **详细日志**: 提供精确的 Token 统计日志,便于调试。
|
||||
|
||||
详细的工作原理和流程请参考 [工作流程指南](WORKFLOW_GUIDE_CN.md)。
|
||||
|
||||
@@ -100,15 +114,12 @@
|
||||
}
|
||||
```
|
||||
|
||||
#### `debug_mode`
|
||||
|
||||
- **默认值**: `true`
|
||||
- **描述**: 是否在 Open WebUI 的控制台日志中打印详细的调试信息(如 Token 计数、压缩进度、数据库操作等)。生产环境建议设为 `false`。
|
||||
|
||||
#### `show_debug_log`
|
||||
|
||||
- **默认值**: `false`
|
||||
- **描述**: 是否在浏览器控制台 (F12) 打印调试日志。便于前端调试。
|
||||
| 参数 | 默认值 | 描述 |
|
||||
| :----------------------------- | :------- | :-------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `enable_tool_output_trimming` | `false` | 启用时,若 `function_calling: "native"` 激活,将裁剪冗长的工具输出以仅提取最终答案。 |
|
||||
| `debug_mode` | `true` | 是否在 Open WebUI 的控制台日志中打印详细的调试信息(如 Token 计数、压缩进度、数据库操作等)。生产环境建议设为 `false`。 |
|
||||
| `show_debug_log` | `false` | 是否在浏览器控制台 (F12) 打印调试日志。便于前端调试。 |
|
||||
| `show_token_usage_status` | `true` | 是否在对话结束时显示 Token 使用情况的状态通知。 |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -5,10 +5,20 @@ author: Fu-Jie
|
||||
author_url: https://github.com/Fu-Jie/awesome-openwebui
|
||||
funding_url: https://github.com/open-webui
|
||||
description: Reduces token consumption in long conversations while maintaining coherence through intelligent summarization and message compression.
|
||||
version: 1.1.3
|
||||
version: 1.2.0
|
||||
openwebui_id: b1655bc8-6de9-4cad-8cb5-a6f7829a02ce
|
||||
license: MIT
|
||||
|
||||
═══════════════════════════════════════════════════════════════════════════════
|
||||
📌 What's new in 1.2.0
|
||||
═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
✅ Preflight Context Check: Validates context fit before sending to model.
|
||||
✅ Structure-Aware Trimming: Collapses long AI responses while keeping H1-H6, intro, and conclusion.
|
||||
✅ Native Tool Output Trimming: Cleaner context when using function calling. (Note: Non-native tool outputs are not fully injected into context)
|
||||
✅ Context Usage Warning: Notification when usage exceeds 90%.
|
||||
✅ Detailed Token Logging: Granular breakdown of System, Head, Summary, and Tail tokens.
|
||||
|
||||
═══════════════════════════════════════════════════════════════════════════════
|
||||
📌 Overview
|
||||
═══════════════════════════════════════════════════════════════════════════════
|
||||
@@ -21,6 +31,8 @@ Core Features:
|
||||
✅ Persistent storage with database support (PostgreSQL and SQLite)
|
||||
✅ Flexible retention policy (configurable to keep first and last N messages)
|
||||
✅ Smart summary injection to maintain context
|
||||
✅ Structure-aware trimming to preserve document skeleton
|
||||
✅ Native tool output trimming for function calling support
|
||||
|
||||
═══════════════════════════════════════════════════════════════════════════════
|
||||
🔄 Workflow
|
||||
@@ -110,6 +122,10 @@ model_thresholds
|
||||
Description: Threshold override configuration for specific models.
|
||||
Example: {"gpt-4": {"compression_threshold_tokens": 8000, "max_context_tokens": 32000}}
|
||||
|
||||
enable_tool_output_trimming
|
||||
Default: false
|
||||
Description: When enabled and `function_calling: "native"` is active, trims verbose tool outputs to extract only the final answer.
|
||||
|
||||
keep_first
|
||||
Default: 1
|
||||
Description: Always keep the first N messages of the conversation. Set to 0 to disable. The first message often contains important system prompts.
|
||||
@@ -245,6 +261,7 @@ Solution:
|
||||
|
||||
from pydantic import BaseModel, Field, model_validator
|
||||
from typing import Optional, Dict, Any, List, Union, Callable, Awaitable
|
||||
import re
|
||||
import asyncio
|
||||
import json
|
||||
import hashlib
|
||||
@@ -254,6 +271,7 @@ import contextlib
|
||||
# Open WebUI built-in imports
|
||||
from open_webui.utils.chat import generate_chat_completion
|
||||
from open_webui.models.users import Users
|
||||
from open_webui.models.models import Models
|
||||
from fastapi.requests import Request
|
||||
from open_webui.main import app as webui_app
|
||||
|
||||
@@ -370,10 +388,6 @@ class Filter:
|
||||
self.valves = self.Valves()
|
||||
self._owui_db = owui_db
|
||||
self._db_engine = owui_engine
|
||||
self._db_engine = owui_engine
|
||||
self._fallback_session_factory = (
|
||||
sessionmaker(bind=self._db_engine) if self._db_engine else None
|
||||
)
|
||||
self._fallback_session_factory = (
|
||||
sessionmaker(bind=self._db_engine) if self._db_engine else None
|
||||
)
|
||||
@@ -494,7 +508,14 @@ class Filter:
|
||||
default=True, description="Enable detailed logging for debugging."
|
||||
)
|
||||
show_debug_log: bool = Field(
|
||||
default=False, description="Print debug logs to browser console (F12)"
|
||||
default=False, description="Show debug logs in the frontend console"
|
||||
)
|
||||
show_token_usage_status: bool = Field(
|
||||
default=True, description="Show token usage status notification"
|
||||
)
|
||||
enable_tool_output_trimming: bool = Field(
|
||||
default=False,
|
||||
description="Enable trimming of large tool outputs (only works with native function calling).",
|
||||
)
|
||||
|
||||
def _save_summary(self, chat_id: str, summary: str, compressed_count: int):
|
||||
@@ -758,6 +779,8 @@ class Filter:
|
||||
body: dict,
|
||||
__user__: Optional[dict] = None,
|
||||
__metadata__: dict = None,
|
||||
__request__: Request = None,
|
||||
__model__: dict = None,
|
||||
__event_emitter__: Callable[[Any], Awaitable[None]] = None,
|
||||
__event_call__: Callable[[Any], Awaitable[None]] = None,
|
||||
) -> dict:
|
||||
@@ -765,10 +788,211 @@ class Filter:
|
||||
Executed before sending to the LLM.
|
||||
Compression Strategy: Only responsible for injecting existing summaries, no Token calculation.
|
||||
"""
|
||||
|
||||
messages = body.get("messages", [])
|
||||
|
||||
# --- Native Tool Output Trimming (Opt-in, only for native function calling) ---
|
||||
metadata = body.get("metadata", {})
|
||||
is_native_func_calling = metadata.get("function_calling") == "native"
|
||||
|
||||
if self.valves.enable_tool_output_trimming and is_native_func_calling:
|
||||
trimmed_count = 0
|
||||
|
||||
for msg in messages:
|
||||
content = msg.get("content", "")
|
||||
if not isinstance(content, str):
|
||||
continue
|
||||
|
||||
role = msg.get("role")
|
||||
|
||||
# Only process assistant messages with native tool outputs
|
||||
if role == "assistant":
|
||||
# Detect tool output markers in assistant content
|
||||
if "tool_call_id:" in content or (
|
||||
content.startswith('"') and "\\"" in content
|
||||
):
|
||||
# Always trim tool outputs when enabled
|
||||
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 🔍 Native tool output detected in assistant message.",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# Extract the final answer (after last tool call metadata)
|
||||
# Pattern: Matches escaped JSON strings like """...""" followed by newlines
|
||||
# We look for the last occurrence of such a pattern and take everything after it
|
||||
|
||||
# 1. Try matching the specific OpenWebUI tool output format: """..."""
|
||||
# This regex finds the last end-quote of a tool output block
|
||||
tool_output_pattern = r'""".*?"""\s*'
|
||||
|
||||
# Find all matches
|
||||
matches = list(
|
||||
re.finditer(tool_output_pattern, content, re.DOTALL)
|
||||
)
|
||||
|
||||
if matches:
|
||||
# Get the end position of the last match
|
||||
last_match_end = matches[-1].end()
|
||||
|
||||
# Everything after the last tool output is the final answer
|
||||
final_answer = content[last_match_end:].strip()
|
||||
|
||||
if final_answer:
|
||||
msg["content"] = (
|
||||
f"... [Tool outputs trimmed]\n{final_answer}"
|
||||
)
|
||||
trimmed_count += 1
|
||||
else:
|
||||
# Fallback: Try splitting on "Arguments:" if the new format isn't found
|
||||
# (Preserving backward compatibility or different model behaviors)
|
||||
parts = re.split(r"(?:Arguments:\s*\{[^}]+\})\n+", content)
|
||||
if len(parts) > 1:
|
||||
final_answer = parts[-1].strip()
|
||||
if final_answer:
|
||||
msg["content"] = (
|
||||
f"... [Tool outputs trimmed]\n{final_answer}"
|
||||
)
|
||||
trimmed_count += 1
|
||||
|
||||
if trimmed_count > 0 and self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ✂️ Trimmed {trimmed_count} tool output message(s).",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
chat_ctx = self._get_chat_context(body, __metadata__)
|
||||
chat_id = chat_ctx["chat_id"]
|
||||
|
||||
# Extract system prompt for accurate token calculation
|
||||
# 1. For custom models: check DB (Models.get_model_by_id)
|
||||
# 2. For base models: check messages for role='system'
|
||||
system_prompt_content = None
|
||||
|
||||
# Try to get from DB (custom model)
|
||||
try:
|
||||
model_id = body.get("model")
|
||||
if model_id:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 🔍 Attempting DB lookup for model: {model_id}",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# Clean model ID if needed (though get_model_by_id usually expects the full ID)
|
||||
model_obj = Models.get_model_by_id(model_id)
|
||||
|
||||
if model_obj:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ✅ Model found in DB: {model_obj.name} (ID: {model_obj.id})",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
if model_obj.params:
|
||||
try:
|
||||
params = model_obj.params
|
||||
# Handle case where params is a JSON string
|
||||
if isinstance(params, str):
|
||||
params = json.loads(params)
|
||||
|
||||
# Handle dict or Pydantic object
|
||||
if isinstance(params, dict):
|
||||
system_prompt_content = params.get("system")
|
||||
else:
|
||||
# Assume Pydantic model or object
|
||||
system_prompt_content = getattr(params, "system", None)
|
||||
|
||||
if system_prompt_content:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 📝 System prompt found in DB params ({len(system_prompt_content)} chars)",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
else:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ⚠️ 'system' key missing in model params",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
except Exception as e:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ❌ Failed to parse model params: {e}",
|
||||
type="error",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
else:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ⚠️ Model params are empty",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
else:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ❌ Model NOT found in DB",
|
||||
type="warning",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ❌ Error fetching system prompt from DB: {e}",
|
||||
type="error",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
if self.valves.debug_mode:
|
||||
print(f"[Inlet] Error fetching system prompt from DB: {e}")
|
||||
|
||||
# Fall back to checking messages (base model or already included)
|
||||
if not system_prompt_content:
|
||||
for msg in messages:
|
||||
if msg.get("role") == "system":
|
||||
system_prompt_content = msg.get("content", "")
|
||||
break
|
||||
|
||||
# Build system_prompt_msg for token calculation
|
||||
system_prompt_msg = None
|
||||
if system_prompt_content:
|
||||
system_prompt_msg = {"role": "system", "content": system_prompt_content}
|
||||
if self.valves.debug_mode:
|
||||
print(
|
||||
f"[Inlet] Found system prompt ({len(system_prompt_content)} chars). Including in budget."
|
||||
)
|
||||
|
||||
# Log message statistics (Moved here to include extracted system prompt)
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
try:
|
||||
msg_stats = {
|
||||
"user": 0,
|
||||
"assistant": 0,
|
||||
"system": 0,
|
||||
"total": len(messages),
|
||||
}
|
||||
for msg in messages:
|
||||
role = msg.get("role", "unknown")
|
||||
if role in msg_stats:
|
||||
msg_stats[role] += 1
|
||||
|
||||
# If system prompt was extracted from DB/Model but not in messages, count it
|
||||
if system_prompt_content:
|
||||
# Check if it's already counted (i.e., was in messages)
|
||||
is_in_messages = any(m.get("role") == "system" for m in messages)
|
||||
if not is_in_messages:
|
||||
msg_stats["system"] += 1
|
||||
msg_stats["total"] += 1
|
||||
|
||||
stats_str = f"Total: {msg_stats['total']} | User: {msg_stats['user']} | Assistant: {msg_stats['assistant']} | System: {msg_stats['system']}"
|
||||
await self._log(
|
||||
f"[Inlet] Message Stats: {stats_str}", event_call=__event_call__
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"[Inlet] Error logging message stats: {e}")
|
||||
|
||||
if not chat_id:
|
||||
await self._log(
|
||||
"[Inlet] ❌ Missing chat_id in metadata, skipping compression",
|
||||
@@ -787,10 +1011,6 @@ class Filter:
|
||||
# Target is to compress up to the (total - keep_last) message
|
||||
target_compressed_count = max(0, len(messages) - self.valves.keep_last)
|
||||
|
||||
# Record the target compression progress for the original messages, for use in outlet
|
||||
# Target is to compress up to the (total - keep_last) message
|
||||
target_compressed_count = max(0, len(messages) - self.valves.keep_last)
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] Recorded target compression progress: {target_compressed_count}",
|
||||
event_call=__event_call__,
|
||||
@@ -799,6 +1019,14 @@ class Filter:
|
||||
# Load summary record
|
||||
summary_record = await asyncio.to_thread(self._load_summary_record, chat_id)
|
||||
|
||||
# Calculate effective_keep_first to ensure all system messages are protected
|
||||
last_system_index = -1
|
||||
for i, msg in enumerate(messages):
|
||||
if msg.get("role") == "system":
|
||||
last_system_index = i
|
||||
|
||||
effective_keep_first = max(self.valves.keep_first, last_system_index + 1)
|
||||
|
||||
final_messages = []
|
||||
|
||||
if summary_record:
|
||||
@@ -812,8 +1040,8 @@ class Filter:
|
||||
|
||||
# 1. Head messages (Keep First)
|
||||
head_messages = []
|
||||
if self.valves.keep_first > 0:
|
||||
head_messages = messages[: self.valves.keep_first]
|
||||
if effective_keep_first > 0:
|
||||
head_messages = messages[:effective_keep_first]
|
||||
|
||||
# 2. Summary message (Inserted as User message)
|
||||
summary_content = (
|
||||
@@ -826,29 +1054,215 @@ class Filter:
|
||||
|
||||
# 3. Tail messages (Tail) - All messages starting from the last compression point
|
||||
# Note: Must ensure head messages are not duplicated
|
||||
start_index = max(compressed_count, self.valves.keep_first)
|
||||
start_index = max(compressed_count, effective_keep_first)
|
||||
tail_messages = messages[start_index:]
|
||||
|
||||
final_messages = head_messages + [summary_msg] + tail_messages
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
tail_preview = [
|
||||
f"{i + start_index}: [{m.get('role')}] {m.get('content', '')[:30]}..."
|
||||
for i, m in enumerate(tail_messages)
|
||||
]
|
||||
await self._log(
|
||||
f"[Inlet] 📜 Tail Messages (Start Index: {start_index}): {tail_preview}",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# --- Preflight Check & Budgeting (Simplified) ---
|
||||
|
||||
# Assemble candidate messages (for output)
|
||||
candidate_messages = head_messages + [summary_msg] + tail_messages
|
||||
|
||||
# Prepare messages for token calculation (include system prompt if missing)
|
||||
calc_messages = candidate_messages
|
||||
if system_prompt_msg:
|
||||
# Check if system prompt is already in head_messages
|
||||
is_in_head = any(m.get("role") == "system" for m in head_messages)
|
||||
if not is_in_head:
|
||||
calc_messages = [system_prompt_msg] + candidate_messages
|
||||
|
||||
# Get max context limit
|
||||
model = self._clean_model_id(body.get("model"))
|
||||
thresholds = self._get_model_thresholds(model)
|
||||
max_context_tokens = thresholds.get(
|
||||
"max_context_tokens", self.valves.max_context_tokens
|
||||
)
|
||||
|
||||
# Calculate total tokens
|
||||
total_tokens = await asyncio.to_thread(
|
||||
self._calculate_messages_tokens, calc_messages
|
||||
)
|
||||
|
||||
# Preflight Check Log
|
||||
await self._log(
|
||||
f"[Inlet] 🔎 Preflight Check: {total_tokens}t / {max_context_tokens}t ({(total_tokens/max_context_tokens*100):.1f}%)",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# If over budget, reduce history (Keep Last)
|
||||
if total_tokens > max_context_tokens:
|
||||
await self._log(
|
||||
f"[Inlet] ⚠️ Candidate prompt ({total_tokens} Tokens) exceeds limit ({max_context_tokens}). Reducing history...",
|
||||
type="warning",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# Dynamically remove messages from the start of tail_messages
|
||||
# Always try to keep at least the last message (usually user input)
|
||||
while total_tokens > max_context_tokens and len(tail_messages) > 1:
|
||||
# Strategy 1: Structure-Aware Assistant Trimming
|
||||
# Retain: Headers (#), First Line, Last Line. Collapse the rest.
|
||||
target_msg = None
|
||||
target_idx = -1
|
||||
|
||||
# Find the oldest assistant message that is long and not yet trimmed
|
||||
for i, msg in enumerate(tail_messages):
|
||||
# Skip the last message (usually user input, protect it)
|
||||
if i == len(tail_messages) - 1:
|
||||
break
|
||||
|
||||
if msg.get("role") == "assistant":
|
||||
content = str(msg.get("content", ""))
|
||||
is_trimmed = msg.get("metadata", {}).get(
|
||||
"is_trimmed", False
|
||||
)
|
||||
# Only target messages that are reasonably long (> 200 chars)
|
||||
if len(content) > 200 and not is_trimmed:
|
||||
target_msg = msg
|
||||
target_idx = i
|
||||
break
|
||||
|
||||
# If found a suitable assistant message, apply structure-aware trimming
|
||||
if target_msg:
|
||||
content = str(target_msg.get("content", ""))
|
||||
lines = content.split("\n")
|
||||
kept_lines = []
|
||||
|
||||
# Logic: Keep headers, first non-empty line, last non-empty line
|
||||
first_line_found = False
|
||||
last_line_idx = -1
|
||||
|
||||
# Find last non-empty line index
|
||||
for idx in range(len(lines) - 1, -1, -1):
|
||||
if lines[idx].strip():
|
||||
last_line_idx = idx
|
||||
break
|
||||
|
||||
for idx, line in enumerate(lines):
|
||||
stripped = line.strip()
|
||||
if not stripped:
|
||||
continue
|
||||
|
||||
# Keep headers (H1-H6, requires space after #)
|
||||
if re.match(r"^#{1,6}\s+", stripped):
|
||||
kept_lines.append(line)
|
||||
continue
|
||||
|
||||
# Keep first non-empty line
|
||||
if not first_line_found:
|
||||
kept_lines.append(line)
|
||||
first_line_found = True
|
||||
# Add placeholder if there's more content coming
|
||||
if idx < last_line_idx:
|
||||
kept_lines.append("\n... [Content collapsed] ...\n")
|
||||
continue
|
||||
|
||||
# Keep last non-empty line
|
||||
if idx == last_line_idx:
|
||||
kept_lines.append(line)
|
||||
continue
|
||||
|
||||
# Update message content
|
||||
new_content = "\n".join(kept_lines)
|
||||
|
||||
# Safety check: If trimming didn't save much (e.g. mostly headers), force drop
|
||||
if len(new_content) > len(content) * 0.8:
|
||||
# Fallback to drop if structure preservation is too verbose
|
||||
pass
|
||||
else:
|
||||
target_msg["content"] = new_content
|
||||
if "metadata" not in target_msg:
|
||||
target_msg["metadata"] = {}
|
||||
target_msg["metadata"]["is_trimmed"] = True
|
||||
|
||||
# Calculate token reduction
|
||||
old_tokens = self._count_tokens(content)
|
||||
new_tokens = self._count_tokens(target_msg["content"])
|
||||
diff = old_tokens - new_tokens
|
||||
total_tokens -= diff
|
||||
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 📉 Structure-trimmed Assistant message. Saved: {diff} tokens.",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
continue
|
||||
|
||||
# Strategy 2: Fallback - Drop Oldest Message Entirely (FIFO)
|
||||
# (User requested to remove progressive trimming for other cases)
|
||||
dropped = tail_messages.pop(0)
|
||||
dropped_tokens = self._count_tokens(str(dropped.get("content", "")))
|
||||
total_tokens -= dropped_tokens
|
||||
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 🗑️ Dropped message from history to fit context. Role: {dropped.get('role')}, Tokens: {dropped_tokens}",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# Re-assemble
|
||||
candidate_messages = head_messages + [summary_msg] + tail_messages
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] ✂️ History reduced. New total: {total_tokens} Tokens (Tail size: {len(tail_messages)})",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
final_messages = candidate_messages
|
||||
|
||||
# Calculate detailed token stats for logging
|
||||
system_tokens = (
|
||||
self._count_tokens(system_prompt_msg.get("content", ""))
|
||||
if system_prompt_msg
|
||||
else 0
|
||||
)
|
||||
head_tokens = self._calculate_messages_tokens(head_messages)
|
||||
summary_tokens = self._count_tokens(summary_content)
|
||||
tail_tokens = self._calculate_messages_tokens(tail_messages)
|
||||
|
||||
system_info = (
|
||||
f"System({system_tokens}t)" if system_prompt_msg else "System(0t)"
|
||||
)
|
||||
|
||||
total_section_tokens = (
|
||||
system_tokens + head_tokens + summary_tokens + tail_tokens
|
||||
)
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] Applied summary: {system_info} + Head({len(head_messages)} msg, {head_tokens}t) + Summary({summary_tokens}t) + Tail({len(tail_messages)} msg, {tail_tokens}t) = Total({total_section_tokens}t)",
|
||||
type="success",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# Prepare status message (Context Usage format)
|
||||
if max_context_tokens > 0:
|
||||
usage_ratio = total_section_tokens / max_context_tokens
|
||||
status_msg = f"Context Usage (Estimated): {total_section_tokens} / {max_context_tokens} Tokens ({usage_ratio*100:.1f}%)"
|
||||
if usage_ratio > 0.9:
|
||||
status_msg += " | ⚠️ High Usage"
|
||||
else:
|
||||
status_msg = f"Loaded historical summary (Hidden {compressed_count} historical messages)"
|
||||
|
||||
# Send status notification
|
||||
if __event_emitter__:
|
||||
await __event_emitter__(
|
||||
{
|
||||
"type": "status",
|
||||
"data": {
|
||||
"description": f"Loaded historical summary (Hidden {compressed_count} historical messages)",
|
||||
"description": status_msg,
|
||||
"done": True,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] Applied summary: Head({len(head_messages)}) + Summary + Tail({len(tail_messages)})",
|
||||
type="success",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# Emit debug log to frontend (Keep the structured log as well)
|
||||
await self._emit_debug_log(
|
||||
__event_call__,
|
||||
@@ -861,8 +1275,71 @@ class Filter:
|
||||
)
|
||||
else:
|
||||
# No summary, use original messages
|
||||
# But still need to check budget!
|
||||
final_messages = messages
|
||||
|
||||
# Include system prompt in calculation
|
||||
calc_messages = final_messages
|
||||
if system_prompt_msg:
|
||||
is_in_messages = any(m.get("role") == "system" for m in final_messages)
|
||||
if not is_in_messages:
|
||||
calc_messages = [system_prompt_msg] + final_messages
|
||||
|
||||
# Get max context limit
|
||||
model = self._clean_model_id(body.get("model"))
|
||||
thresholds = self._get_model_thresholds(model)
|
||||
max_context_tokens = thresholds.get(
|
||||
"max_context_tokens", self.valves.max_context_tokens
|
||||
)
|
||||
|
||||
total_tokens = await asyncio.to_thread(
|
||||
self._calculate_messages_tokens, calc_messages
|
||||
)
|
||||
|
||||
if total_tokens > max_context_tokens:
|
||||
await self._log(
|
||||
f"[Inlet] ⚠️ Original messages ({total_tokens} Tokens) exceed limit ({max_context_tokens}). Reducing history...",
|
||||
type="warning",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# Dynamically remove messages from the start
|
||||
# We'll respect effective_keep_first to protect system prompts
|
||||
|
||||
start_trim_index = effective_keep_first
|
||||
|
||||
while (
|
||||
total_tokens > max_context_tokens
|
||||
and len(final_messages)
|
||||
> start_trim_index + 1 # Keep at least 1 message after keep_first
|
||||
):
|
||||
dropped = final_messages.pop(start_trim_index)
|
||||
total_tokens -= self._count_tokens(str(dropped.get("content", "")))
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] ✂️ Messages reduced. New total: {total_tokens} Tokens",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# Send status notification (Context Usage format)
|
||||
if __event_emitter__:
|
||||
status_msg = f"Context Usage (Estimated): {total_tokens} / {max_context_tokens} Tokens"
|
||||
if max_context_tokens > 0:
|
||||
usage_ratio = total_tokens / max_context_tokens
|
||||
status_msg += f" ({usage_ratio*100:.1f}%)"
|
||||
if usage_ratio > 0.9:
|
||||
status_msg += " | ⚠️ High Usage"
|
||||
|
||||
await __event_emitter__(
|
||||
{
|
||||
"type": "status",
|
||||
"data": {
|
||||
"description": status_msg,
|
||||
"done": True,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
body["messages"] = final_messages
|
||||
|
||||
await self._log(
|
||||
@@ -1048,11 +1525,23 @@ class Filter:
|
||||
return
|
||||
|
||||
middle_messages = messages[start_index:end_index]
|
||||
tail_preview_msgs = messages[end_index:]
|
||||
|
||||
await self._log(
|
||||
f"[🤖 Async Summary Task] Middle messages to process: {len(middle_messages)}",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
middle_preview = [
|
||||
f"{i + start_index}: [{m.get('role')}] {m.get('content', '')[:20]}..."
|
||||
for i, m in enumerate(middle_messages[:3])
|
||||
]
|
||||
tail_preview = [
|
||||
f"{i + end_index}: [{m.get('role')}] {m.get('content', '')[:20]}..."
|
||||
for i, m in enumerate(tail_preview_msgs)
|
||||
]
|
||||
await self._log(
|
||||
f"[🤖 Async Summary Task] 📊 Boundary Check:\n"
|
||||
f" - Middle (Compressing): {len(middle_messages)} msgs (Indices {start_index}-{end_index-1}) -> Preview: {middle_preview}\n"
|
||||
f" - Tail (Keeping): {len(tail_preview_msgs)} msgs (Indices {end_index}-End) -> Preview: {tail_preview}",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 3. Check Token limit and truncate (Max Context Truncation)
|
||||
# [Optimization] Use the summary model's (if any) threshold to decide how many middle messages can be processed
|
||||
@@ -1186,6 +1675,109 @@ class Filter:
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# --- Token Usage Status Notification ---
|
||||
if self.valves.show_token_usage_status and __event_emitter__:
|
||||
try:
|
||||
# 1. Fetch System Prompt (DB fallback)
|
||||
system_prompt_msg = None
|
||||
model_id = body.get("model")
|
||||
if model_id:
|
||||
try:
|
||||
model_obj = Models.get_model_by_id(model_id)
|
||||
if model_obj and model_obj.params:
|
||||
params = model_obj.params
|
||||
if isinstance(params, str):
|
||||
params = json.loads(params)
|
||||
if isinstance(params, dict):
|
||||
sys_content = params.get("system")
|
||||
else:
|
||||
sys_content = getattr(params, "system", None)
|
||||
|
||||
if sys_content:
|
||||
system_prompt_msg = {
|
||||
"role": "system",
|
||||
"content": sys_content,
|
||||
}
|
||||
except Exception:
|
||||
pass # Ignore DB errors here, best effort
|
||||
|
||||
# 2. Calculate Effective Keep First
|
||||
last_system_index = -1
|
||||
for i, msg in enumerate(messages):
|
||||
if msg.get("role") == "system":
|
||||
last_system_index = i
|
||||
effective_keep_first = max(
|
||||
self.valves.keep_first, last_system_index + 1
|
||||
)
|
||||
|
||||
# 3. Construct Next Context
|
||||
# Head
|
||||
head_msgs = (
|
||||
messages[:effective_keep_first]
|
||||
if effective_keep_first > 0
|
||||
else []
|
||||
)
|
||||
|
||||
# Summary
|
||||
summary_content = (
|
||||
f"【System Prompt: The following is a summary of the historical conversation, provided for context only. Do not reply to the summary content itself; answer the subsequent latest questions directly.】\n\n"
|
||||
f"{new_summary}\n\n"
|
||||
f"---\n"
|
||||
f"Below is the recent conversation:"
|
||||
)
|
||||
summary_msg = {"role": "assistant", "content": summary_content}
|
||||
|
||||
# Tail (using target_compressed_count which is what we just compressed up to)
|
||||
# Note: target_compressed_count is the index *after* the last compressed message?
|
||||
# In _generate_summary_async, target_compressed_count is passed in.
|
||||
# It represents the number of messages to be covered by summary (excluding keep_last).
|
||||
# So tail starts at max(target_compressed_count, effective_keep_first).
|
||||
start_index = max(target_compressed_count, effective_keep_first)
|
||||
tail_msgs = messages[start_index:]
|
||||
|
||||
# Assemble
|
||||
next_context = head_msgs + [summary_msg] + tail_msgs
|
||||
|
||||
# Inject system prompt if needed
|
||||
if system_prompt_msg:
|
||||
is_in_head = any(m.get("role") == "system" for m in head_msgs)
|
||||
if not is_in_head:
|
||||
next_context = [system_prompt_msg] + next_context
|
||||
|
||||
# 4. Calculate Tokens
|
||||
token_count = self._calculate_messages_tokens(next_context)
|
||||
|
||||
# 5. Get Thresholds & Calculate Ratio
|
||||
model = self._clean_model_id(body.get("model"))
|
||||
thresholds = self._get_model_thresholds(model)
|
||||
max_context_tokens = thresholds.get(
|
||||
"max_context_tokens", self.valves.max_context_tokens
|
||||
)
|
||||
|
||||
# 6. Emit Status
|
||||
status_msg = f"Context Summary Updated: {token_count} / {max_context_tokens} Tokens"
|
||||
if max_context_tokens > 0:
|
||||
ratio = (token_count / max_context_tokens) * 100
|
||||
status_msg += f" ({ratio:.1f}%)"
|
||||
if ratio > 90.0:
|
||||
status_msg += " | ⚠️ High Usage"
|
||||
|
||||
await __event_emitter__(
|
||||
{
|
||||
"type": "status",
|
||||
"data": {
|
||||
"description": status_msg,
|
||||
"done": True,
|
||||
},
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
await self._log(
|
||||
f"[Status] Error calculating tokens: {e}",
|
||||
type="error",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
await self._log(
|
||||
f"[🤖 Async Summary Task] ❌ Error: {str(e)}",
|
||||
|
||||
@@ -5,10 +5,20 @@ author: Fu-Jie
|
||||
author_url: https://github.com/Fu-Jie/awesome-openwebui
|
||||
funding_url: https://github.com/open-webui
|
||||
description: 通过智能摘要和消息压缩,降低长对话的 token 消耗,同时保持对话连贯性。
|
||||
version: 1.1.3
|
||||
version: 1.2.0
|
||||
openwebui_id: 5c0617cb-a9e4-4bd6-a440-d276534ebd18
|
||||
license: MIT
|
||||
|
||||
═══════════════════════════════════════════════════════════════════════════════
|
||||
📌 1.2.0 版本更新
|
||||
═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
✅ 预检上下文检查:发送给模型前验证上下文是否适配。
|
||||
✅ 结构感知裁剪:折叠过长的 AI 响应,同时保留标题 (H1-H6)、开头和结尾。
|
||||
✅ 原生工具输出裁剪:使用函数调用时清理上下文,去除冗余输出。(注意:非原生工具调用输出不会完整注入上下文)
|
||||
✅ 上下文使用警告:当使用量超过 90% 时发出通知。
|
||||
✅ 详细 Token 日志:细粒度记录 System、Head、Summary 和 Tail 的 Token 消耗。
|
||||
|
||||
═══════════════════════════════════════════════════════════════════════════════
|
||||
📌 功能概述
|
||||
═══════════════════════════════════════════════════════════════════════════════
|
||||
@@ -248,9 +258,11 @@ import asyncio
|
||||
import json
|
||||
import hashlib
|
||||
import time
|
||||
import re
|
||||
|
||||
# Open WebUI 内置导入
|
||||
from open_webui.utils.chat import generate_chat_completion
|
||||
from open_webui.models.models import Models
|
||||
from open_webui.models.users import Users
|
||||
from fastapi.requests import Request
|
||||
from open_webui.main import app as webui_app
|
||||
@@ -353,6 +365,13 @@ class Filter:
|
||||
show_debug_log: bool = Field(
|
||||
default=False, description="在浏览器控制台打印调试日志 (F12)"
|
||||
)
|
||||
show_token_usage_status: bool = Field(
|
||||
default=True, description="在对话结束时显示 Token 使用情况的状态通知"
|
||||
)
|
||||
enable_tool_output_trimming: bool = Field(
|
||||
default=False,
|
||||
description="启用原生工具输出裁剪 (仅适用于 native function calling),裁剪过长的工具输出以节省 Token。",
|
||||
)
|
||||
|
||||
def _save_summary(self, chat_id: str, summary: str, compressed_count: int):
|
||||
"""保存摘要到数据库"""
|
||||
@@ -614,12 +633,217 @@ class Filter:
|
||||
) -> dict:
|
||||
"""
|
||||
在发送到 LLM 之前执行
|
||||
压缩策略:只负责注入已有的摘要,不进行 Token 计算
|
||||
压缩策略:
|
||||
1. 注入已有摘要
|
||||
2. 预检 Token 预算
|
||||
3. 如果超限,执行结构化裁剪(Structure-Aware Trimming)或丢弃旧消息
|
||||
"""
|
||||
messages = body.get("messages", [])
|
||||
|
||||
# --- 原生工具输出裁剪 (Native Tool Output Trimming) ---
|
||||
# 即使未启用压缩,也始终检查并裁剪过长的工具输出,以节省 Token
|
||||
if self.valves.enable_tool_output_trimming:
|
||||
trimmed_count = 0
|
||||
for msg in messages:
|
||||
content = msg.get("content", "")
|
||||
if not isinstance(content, str):
|
||||
continue
|
||||
|
||||
role = msg.get("role")
|
||||
|
||||
# 仅处理带有原生工具输出的助手消息
|
||||
if role == "assistant":
|
||||
# 检测助手内容中的工具输出标记
|
||||
if "tool_call_id:" in content or (
|
||||
content.startswith('"') and "\\"" in content
|
||||
):
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 🔍 检测到助手消息中的原生工具输出。",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 提取最终答案(在最后一个工具调用元数据之后)
|
||||
# 模式:匹配转义的 JSON 字符串,如 """...""" 后跟换行符
|
||||
# 我们寻找该模式的最后一次出现,并获取其后的所有内容
|
||||
|
||||
# 1. 尝试匹配特定的 OpenWebUI 工具输出格式:"""..."""
|
||||
tool_output_pattern = r'""".*?"""\s*'
|
||||
|
||||
# 查找所有匹配项
|
||||
matches = list(
|
||||
re.finditer(tool_output_pattern, content, re.DOTALL)
|
||||
)
|
||||
|
||||
if matches:
|
||||
# 获取最后一个匹配项的结束位置
|
||||
last_match_end = matches[-1].end()
|
||||
|
||||
# 最后一个工具输出之后的所有内容即为最终答案
|
||||
final_answer = content[last_match_end:].strip()
|
||||
|
||||
if final_answer:
|
||||
msg["content"] = (
|
||||
f"... [Tool outputs trimmed]\n{final_answer}"
|
||||
)
|
||||
trimmed_count += 1
|
||||
else:
|
||||
# 回退:如果找不到新格式,尝试按 "Arguments:" 分割
|
||||
# (保留向后兼容性或适应不同模型行为)
|
||||
parts = re.split(r"(?:Arguments:\s*\{[^}]+\})\n+", content)
|
||||
if len(parts) > 1:
|
||||
final_answer = parts[-1].strip()
|
||||
if final_answer:
|
||||
msg["content"] = (
|
||||
f"... [Tool outputs trimmed]\n{final_answer}"
|
||||
)
|
||||
trimmed_count += 1
|
||||
|
||||
if trimmed_count > 0 and self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ✂️ 已裁剪 {trimmed_count} 条工具输出消息。",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
chat_ctx = self._get_chat_context(body, __metadata__)
|
||||
chat_id = chat_ctx["chat_id"]
|
||||
|
||||
# 提取系统提示词以进行准确的 Token 计算
|
||||
# 1. 对于自定义模型:检查数据库 (Models.get_model_by_id)
|
||||
# 2. 对于基础模型:检查消息中的 role='system'
|
||||
system_prompt_content = None
|
||||
|
||||
# 尝试从数据库获取 (自定义模型)
|
||||
try:
|
||||
model_id = body.get("model")
|
||||
if model_id:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 🔍 尝试从数据库查找模型: {model_id}",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 清理模型 ID
|
||||
model_obj = Models.get_model_by_id(model_id)
|
||||
|
||||
if model_obj:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ✅ 数据库中找到模型: {model_obj.name} (ID: {model_obj.id})",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
if model_obj.params:
|
||||
try:
|
||||
params = model_obj.params
|
||||
# 处理 params 是 JSON 字符串的情况
|
||||
if isinstance(params, str):
|
||||
params = json.loads(params)
|
||||
|
||||
# 处理字典或 Pydantic 对象
|
||||
if isinstance(params, dict):
|
||||
system_prompt_content = params.get("system")
|
||||
else:
|
||||
# 假设是 Pydantic 模型或对象
|
||||
system_prompt_content = getattr(params, "system", None)
|
||||
|
||||
if system_prompt_content:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 📝 在数据库参数中找到系统提示词 ({len(system_prompt_content)} 字符)",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
else:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ⚠️ 模型参数中缺少 'system' 键",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
except Exception as e:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ❌ 解析模型参数失败: {e}",
|
||||
type="error",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
else:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ⚠️ 模型参数为空",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
else:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ❌ 数据库中未找到模型",
|
||||
type="warning",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] ❌ 从数据库获取系统提示词错误: {e}",
|
||||
type="error",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
if self.valves.debug_mode:
|
||||
print(f"[Inlet] 从数据库获取系统提示词错误: {e}")
|
||||
|
||||
# 回退:检查消息列表 (基础模型或已包含)
|
||||
if not system_prompt_content:
|
||||
for msg in messages:
|
||||
if msg.get("role") == "system":
|
||||
system_prompt_content = msg.get("content", "")
|
||||
break
|
||||
|
||||
# 构建 system_prompt_msg 用于 Token 计算
|
||||
system_prompt_msg = None
|
||||
if system_prompt_content:
|
||||
system_prompt_msg = {"role": "system", "content": system_prompt_content}
|
||||
if self.valves.debug_mode:
|
||||
print(
|
||||
f"[Inlet] 找到系统提示词 ({len(system_prompt_content)} 字符)。计入预算。"
|
||||
)
|
||||
|
||||
# 记录消息统计信息 (移至此处以包含提取的系统提示词)
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
try:
|
||||
msg_stats = {
|
||||
"user": 0,
|
||||
"assistant": 0,
|
||||
"system": 0,
|
||||
"total": len(messages),
|
||||
}
|
||||
for msg in messages:
|
||||
role = msg.get("role", "unknown")
|
||||
if role in msg_stats:
|
||||
msg_stats[role] += 1
|
||||
|
||||
# 如果系统提示词是从 DB/Model 提取的但不在消息中,则计数
|
||||
if system_prompt_content:
|
||||
# 检查是否已计数 (即是否在消息中)
|
||||
is_in_messages = any(m.get("role") == "system" for m in messages)
|
||||
if not is_in_messages:
|
||||
msg_stats["system"] += 1
|
||||
msg_stats["total"] += 1
|
||||
|
||||
stats_str = f"Total: {msg_stats['total']} | User: {msg_stats['user']} | Assistant: {msg_stats['assistant']} | System: {msg_stats['system']}"
|
||||
await self._log(
|
||||
f"[Inlet] 消息统计: {stats_str}", event_call=__event_call__
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"[Inlet] 记录消息统计错误: {e}")
|
||||
|
||||
if not chat_id:
|
||||
await self._log(
|
||||
"[Inlet] ❌ metadata 中缺少 chat_id,跳过压缩",
|
||||
type="error",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
return body
|
||||
|
||||
if self.valves.debug_mode or self.valves.show_debug_log:
|
||||
await self._log(
|
||||
f"\n{'='*60}\n[Inlet] Chat ID: {chat_id}\n[Inlet] 收到 {len(messages)} 条消息",
|
||||
@@ -630,10 +854,6 @@ class Filter:
|
||||
# 目标是压缩到倒数第 keep_last 条之前
|
||||
target_compressed_count = max(0, len(messages) - self.valves.keep_last)
|
||||
|
||||
# 记录原始消息的目标压缩进度,供 outlet 使用
|
||||
# 目标是压缩到倒数第 keep_last 条之前
|
||||
target_compressed_count = max(0, len(messages) - self.valves.keep_last)
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] 记录目标压缩进度: {target_compressed_count}",
|
||||
event_call=__event_call__,
|
||||
@@ -642,6 +862,14 @@ class Filter:
|
||||
# 加载摘要记录
|
||||
summary_record = await asyncio.to_thread(self._load_summary_record, chat_id)
|
||||
|
||||
# 计算 effective_keep_first 以确保所有系统消息都被保护
|
||||
last_system_index = -1
|
||||
for i, msg in enumerate(messages):
|
||||
if msg.get("role") == "system":
|
||||
last_system_index = i
|
||||
|
||||
effective_keep_first = max(self.valves.keep_first, last_system_index + 1)
|
||||
|
||||
final_messages = []
|
||||
|
||||
if summary_record:
|
||||
@@ -655,8 +883,8 @@ class Filter:
|
||||
|
||||
# 1. 头部消息 (Keep First)
|
||||
head_messages = []
|
||||
if self.valves.keep_first > 0:
|
||||
head_messages = messages[: self.valves.keep_first]
|
||||
if effective_keep_first > 0:
|
||||
head_messages = messages[:effective_keep_first]
|
||||
|
||||
# 2. 摘要消息 (作为 User 消息插入)
|
||||
summary_content = (
|
||||
@@ -669,29 +897,214 @@ class Filter:
|
||||
|
||||
# 3. 尾部消息 (Tail) - 从上次压缩点开始的所有消息
|
||||
# 注意:这里必须确保不重复包含头部消息
|
||||
start_index = max(compressed_count, self.valves.keep_first)
|
||||
start_index = max(compressed_count, effective_keep_first)
|
||||
tail_messages = messages[start_index:]
|
||||
|
||||
final_messages = head_messages + [summary_msg] + tail_messages
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
tail_preview = [
|
||||
f"{i + start_index}: [{m.get('role')}] {m.get('content', '')[:30]}..."
|
||||
for i, m in enumerate(tail_messages)
|
||||
]
|
||||
await self._log(
|
||||
f"[Inlet] 📜 尾部消息 (起始索引: {start_index}): {tail_preview}",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# --- 预检检查与预算 (Preflight Check & Budgeting) ---
|
||||
|
||||
# 组装候选消息 (用于输出)
|
||||
candidate_messages = head_messages + [summary_msg] + tail_messages
|
||||
|
||||
# 准备用于 Token 计算的消息 (如果缺少则包含系统提示词)
|
||||
calc_messages = candidate_messages
|
||||
if system_prompt_msg:
|
||||
# 检查系统提示词是否已在 head_messages 中
|
||||
is_in_head = any(m.get("role") == "system" for m in head_messages)
|
||||
if not is_in_head:
|
||||
calc_messages = [system_prompt_msg] + candidate_messages
|
||||
|
||||
# 获取最大上下文限制
|
||||
model = self._clean_model_id(body.get("model"))
|
||||
thresholds = self._get_model_thresholds(model)
|
||||
max_context_tokens = thresholds.get(
|
||||
"max_context_tokens", self.valves.max_context_tokens
|
||||
)
|
||||
|
||||
# 计算总 Token
|
||||
total_tokens = await asyncio.to_thread(
|
||||
self._calculate_messages_tokens, calc_messages
|
||||
)
|
||||
|
||||
# 预检检查日志
|
||||
await self._log(
|
||||
f"[Inlet] 🔎 预检检查: {total_tokens}t / {max_context_tokens}t ({(total_tokens/max_context_tokens*100):.1f}%)",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 如果超出预算,缩减历史记录 (Keep Last)
|
||||
if total_tokens > max_context_tokens:
|
||||
await self._log(
|
||||
f"[Inlet] ⚠️ 候选提示词 ({total_tokens} Tokens) 超过上限 ({max_context_tokens})。正在缩减历史记录...",
|
||||
type="warning",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 动态从 tail_messages 的开头移除消息
|
||||
# 始终尝试保留至少最后一条消息 (通常是用户输入)
|
||||
while total_tokens > max_context_tokens and len(tail_messages) > 1:
|
||||
# 策略 1: 结构化助手消息裁剪 (Structure-Aware Assistant Trimming)
|
||||
# 保留: 标题 (#), 第一行, 最后一行。折叠其余部分。
|
||||
target_msg = None
|
||||
target_idx = -1
|
||||
|
||||
# 查找最旧的、较长且尚未裁剪的助手消息
|
||||
for i, msg in enumerate(tail_messages):
|
||||
# 跳过最后一条消息 (通常是用户输入,保护它)
|
||||
if i == len(tail_messages) - 1:
|
||||
break
|
||||
|
||||
if msg.get("role") == "assistant":
|
||||
content = str(msg.get("content", ""))
|
||||
is_trimmed = msg.get("metadata", {}).get(
|
||||
"is_trimmed", False
|
||||
)
|
||||
# 仅针对相当长 (> 200 字符) 的消息
|
||||
if len(content) > 200 and not is_trimmed:
|
||||
target_msg = msg
|
||||
target_idx = i
|
||||
break
|
||||
|
||||
# 如果找到合适的助手消息,应用结构化裁剪
|
||||
if target_msg:
|
||||
content = str(target_msg.get("content", ""))
|
||||
lines = content.split("\n")
|
||||
kept_lines = []
|
||||
|
||||
# 逻辑: 保留标题, 第一行非空行, 最后一行非空行
|
||||
first_line_found = False
|
||||
last_line_idx = -1
|
||||
|
||||
# 查找最后一行非空行的索引
|
||||
for idx in range(len(lines) - 1, -1, -1):
|
||||
if lines[idx].strip():
|
||||
last_line_idx = idx
|
||||
break
|
||||
|
||||
for idx, line in enumerate(lines):
|
||||
stripped = line.strip()
|
||||
if not stripped:
|
||||
continue
|
||||
|
||||
# 保留标题 (H1-H6, 需要 # 后有空格)
|
||||
if re.match(r"^#{1,6}\s+", stripped):
|
||||
kept_lines.append(line)
|
||||
continue
|
||||
|
||||
# 保留第一行非空行
|
||||
if not first_line_found:
|
||||
kept_lines.append(line)
|
||||
first_line_found = True
|
||||
# 如果后面还有内容,添加占位符
|
||||
if idx < last_line_idx:
|
||||
kept_lines.append("\n... [Content collapsed] ...\n")
|
||||
continue
|
||||
|
||||
# 保留最后一行非空行
|
||||
if idx == last_line_idx:
|
||||
kept_lines.append(line)
|
||||
continue
|
||||
|
||||
# 更新消息内容
|
||||
new_content = "\n".join(kept_lines)
|
||||
|
||||
# 安全检查: 如果裁剪没有节省太多 (例如主要是标题),则强制丢弃
|
||||
if len(new_content) > len(content) * 0.8:
|
||||
# 如果结构保留过于冗长,回退到丢弃
|
||||
pass
|
||||
else:
|
||||
target_msg["content"] = new_content
|
||||
if "metadata" not in target_msg:
|
||||
target_msg["metadata"] = {}
|
||||
target_msg["metadata"]["is_trimmed"] = True
|
||||
|
||||
# 计算 Token 减少量
|
||||
old_tokens = self._count_tokens(content)
|
||||
new_tokens = self._count_tokens(target_msg["content"])
|
||||
diff = old_tokens - new_tokens
|
||||
total_tokens -= diff
|
||||
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 📉 结构化裁剪助手消息。节省: {diff} tokens。",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
continue
|
||||
|
||||
# 策略 2: 回退 - 完全丢弃最旧的消息 (FIFO)
|
||||
dropped = tail_messages.pop(0)
|
||||
dropped_tokens = self._count_tokens(str(dropped.get("content", "")))
|
||||
total_tokens -= dropped_tokens
|
||||
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
await self._log(
|
||||
f"[Inlet] 🗑️ 从历史记录中丢弃消息以适应上下文。角色: {dropped.get('role')}, Tokens: {dropped_tokens}",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 重新组装
|
||||
candidate_messages = head_messages + [summary_msg] + tail_messages
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] ✂️ 历史记录已缩减。新总数: {total_tokens} Tokens (尾部大小: {len(tail_messages)})",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
final_messages = candidate_messages
|
||||
|
||||
# 计算详细 Token 统计以用于日志
|
||||
system_tokens = (
|
||||
self._count_tokens(system_prompt_msg.get("content", ""))
|
||||
if system_prompt_msg
|
||||
else 0
|
||||
)
|
||||
head_tokens = self._calculate_messages_tokens(head_messages)
|
||||
summary_tokens = self._count_tokens(summary_content)
|
||||
tail_tokens = self._calculate_messages_tokens(tail_messages)
|
||||
|
||||
system_info = (
|
||||
f"System({system_tokens}t)" if system_prompt_msg else "System(0t)"
|
||||
)
|
||||
|
||||
total_section_tokens = (
|
||||
system_tokens + head_tokens + summary_tokens + tail_tokens
|
||||
)
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] 应用摘要: {system_info} + Head({len(head_messages)} 条, {head_tokens}t) + Summary({summary_tokens}t) + Tail({len(tail_messages)} 条, {tail_tokens}t) = Total({total_section_tokens}t)",
|
||||
type="success",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 准备状态消息 (上下文使用量格式)
|
||||
if max_context_tokens > 0:
|
||||
usage_ratio = total_section_tokens / max_context_tokens
|
||||
status_msg = f"上下文使用量 (预估): {total_section_tokens} / {max_context_tokens} Tokens ({usage_ratio*100:.1f}%)"
|
||||
if usage_ratio > 0.9:
|
||||
status_msg += " | ⚠️ 高负载"
|
||||
else:
|
||||
status_msg = f"已加载历史摘要 (隐藏 {compressed_count} 条历史消息)"
|
||||
|
||||
# 发送状态通知
|
||||
if __event_emitter__:
|
||||
await __event_emitter__(
|
||||
{
|
||||
"type": "status",
|
||||
"data": {
|
||||
"description": f"已加载历史摘要 (隐藏 {compressed_count} 条历史消息)",
|
||||
"description": status_msg,
|
||||
"done": True,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] 应用摘要: Head({len(head_messages)}) + Summary + Tail({len(tail_messages)})",
|
||||
type="success",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# Emit debug log to frontend (Keep the structured log as well)
|
||||
await self._emit_debug_log(
|
||||
__event_call__,
|
||||
@@ -704,8 +1117,73 @@ class Filter:
|
||||
)
|
||||
else:
|
||||
# 没有摘要,使用原始消息
|
||||
# 但仍然需要检查预算!
|
||||
final_messages = messages
|
||||
|
||||
# 包含系统提示词进行计算
|
||||
calc_messages = final_messages
|
||||
if system_prompt_msg:
|
||||
is_in_messages = any(m.get("role") == "system" for m in final_messages)
|
||||
if not is_in_messages:
|
||||
calc_messages = [system_prompt_msg] + final_messages
|
||||
|
||||
# 获取最大上下文限制
|
||||
model = self._clean_model_id(body.get("model"))
|
||||
thresholds = self._get_model_thresholds(model)
|
||||
max_context_tokens = thresholds.get(
|
||||
"max_context_tokens", self.valves.max_context_tokens
|
||||
)
|
||||
|
||||
total_tokens = await asyncio.to_thread(
|
||||
self._calculate_messages_tokens, calc_messages
|
||||
)
|
||||
|
||||
if total_tokens > max_context_tokens:
|
||||
await self._log(
|
||||
f"[Inlet] ⚠️ 原始消息 ({total_tokens} Tokens) 超过上限 ({max_context_tokens})。正在缩减历史记录...",
|
||||
type="warning",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 动态从开头移除消息
|
||||
# 我们将遵守 effective_keep_first 以保护系统提示词
|
||||
|
||||
start_trim_index = effective_keep_first
|
||||
|
||||
while (
|
||||
total_tokens > max_context_tokens
|
||||
and len(final_messages)
|
||||
> start_trim_index + 1 # 保留 keep_first 之后至少 1 条消息
|
||||
):
|
||||
dropped = final_messages.pop(start_trim_index)
|
||||
total_tokens -= self._count_tokens(str(dropped.get("content", "")))
|
||||
|
||||
await self._log(
|
||||
f"[Inlet] ✂️ 消息已缩减。新总数: {total_tokens} Tokens",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 发送状态通知 (上下文使用量格式)
|
||||
if __event_emitter__:
|
||||
status_msg = (
|
||||
f"上下文使用量 (预估): {total_tokens} / {max_context_tokens} Tokens"
|
||||
)
|
||||
if max_context_tokens > 0:
|
||||
usage_ratio = total_tokens / max_context_tokens
|
||||
status_msg += f" ({usage_ratio*100:.1f}%)"
|
||||
if usage_ratio > 0.9:
|
||||
status_msg += " | ⚠️ 高负载"
|
||||
|
||||
await __event_emitter__(
|
||||
{
|
||||
"type": "status",
|
||||
"data": {
|
||||
"description": status_msg,
|
||||
"done": True,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
body["messages"] = final_messages
|
||||
|
||||
await self._log(
|
||||
@@ -882,11 +1360,23 @@ class Filter:
|
||||
return
|
||||
|
||||
middle_messages = messages[start_index:end_index]
|
||||
tail_preview_msgs = messages[end_index:]
|
||||
|
||||
await self._log(
|
||||
f"[🤖 异步摘要任务] 待处理中间消息: {len(middle_messages)} 条",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
if self.valves.show_debug_log and __event_call__:
|
||||
middle_preview = [
|
||||
f"{i + start_index}: [{m.get('role')}] {m.get('content', '')[:20]}..."
|
||||
for i, m in enumerate(middle_messages[:3])
|
||||
]
|
||||
tail_preview = [
|
||||
f"{i + end_index}: [{m.get('role')}] {m.get('content', '')[:20]}..."
|
||||
for i, m in enumerate(tail_preview_msgs)
|
||||
]
|
||||
await self._log(
|
||||
f"[🤖 异步摘要任务] 📊 边界检查:\n"
|
||||
f" - 中间 (压缩): {len(middle_messages)} 条 (索引 {start_index}-{end_index-1}) -> 预览: {middle_preview}\n"
|
||||
f" - 尾部 (保留): {len(tail_preview_msgs)} 条 (索引 {end_index}-End) -> 预览: {tail_preview}",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# 3. 检查 Token 上限并截断 (Max Context Truncation)
|
||||
# [优化] 使用摘要模型(如果有)的阈值来决定能处理多少中间消息
|
||||
@@ -1020,6 +1510,109 @@ class Filter:
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
# --- Token 使用情况状态通知 ---
|
||||
if self.valves.show_token_usage_status and __event_emitter__:
|
||||
try:
|
||||
# 1. 获取系统提示词 (DB 回退)
|
||||
system_prompt_msg = None
|
||||
model_id = body.get("model")
|
||||
if model_id:
|
||||
try:
|
||||
model_obj = Models.get_model_by_id(model_id)
|
||||
if model_obj and model_obj.params:
|
||||
params = model_obj.params
|
||||
if isinstance(params, str):
|
||||
params = json.loads(params)
|
||||
if isinstance(params, dict):
|
||||
sys_content = params.get("system")
|
||||
else:
|
||||
sys_content = getattr(params, "system", None)
|
||||
|
||||
if sys_content:
|
||||
system_prompt_msg = {
|
||||
"role": "system",
|
||||
"content": sys_content,
|
||||
}
|
||||
except Exception:
|
||||
pass # 忽略 DB 错误,尽力而为
|
||||
|
||||
# 2. 计算 Effective Keep First
|
||||
last_system_index = -1
|
||||
for i, msg in enumerate(messages):
|
||||
if msg.get("role") == "system":
|
||||
last_system_index = i
|
||||
effective_keep_first = max(
|
||||
self.valves.keep_first, last_system_index + 1
|
||||
)
|
||||
|
||||
# 3. 构建下一个上下文 (Next Context)
|
||||
# Head
|
||||
head_msgs = (
|
||||
messages[:effective_keep_first]
|
||||
if effective_keep_first > 0
|
||||
else []
|
||||
)
|
||||
|
||||
# Summary
|
||||
summary_content = (
|
||||
f"【系统提示:以下是历史对话的摘要,仅供参考上下文,请勿对摘要内容进行回复,直接回答后续的最新问题】\n\n"
|
||||
f"{new_summary}\n\n"
|
||||
f"---\n"
|
||||
f"以下是最近的对话:"
|
||||
)
|
||||
summary_msg = {"role": "assistant", "content": summary_content}
|
||||
|
||||
# Tail (使用 target_compressed_count,这是我们刚刚压缩到的位置)
|
||||
# 注意:target_compressed_count 是要被摘要覆盖的消息数(不包括 keep_last)
|
||||
# 所以 tail 从 max(target_compressed_count, effective_keep_first) 开始
|
||||
start_index = max(target_compressed_count, effective_keep_first)
|
||||
tail_msgs = messages[start_index:]
|
||||
|
||||
# 组装
|
||||
next_context = head_msgs + [summary_msg] + tail_msgs
|
||||
|
||||
# 如果需要,注入系统提示词
|
||||
if system_prompt_msg:
|
||||
is_in_head = any(m.get("role") == "system" for m in head_msgs)
|
||||
if not is_in_head:
|
||||
next_context = [system_prompt_msg] + next_context
|
||||
|
||||
# 4. 计算 Token
|
||||
token_count = self._calculate_messages_tokens(next_context)
|
||||
|
||||
# 5. 获取阈值并计算比例
|
||||
model = self._clean_model_id(body.get("model"))
|
||||
thresholds = self._get_model_thresholds(model)
|
||||
max_context_tokens = thresholds.get(
|
||||
"max_context_tokens", self.valves.max_context_tokens
|
||||
)
|
||||
|
||||
# 6. 发送状态
|
||||
status_msg = (
|
||||
f"上下文摘要已更新: {token_count} / {max_context_tokens} Tokens"
|
||||
)
|
||||
if max_context_tokens > 0:
|
||||
ratio = (token_count / max_context_tokens) * 100
|
||||
status_msg += f" ({ratio:.1f}%)"
|
||||
if ratio > 90.0:
|
||||
status_msg += " | ⚠️ 高负载"
|
||||
|
||||
await __event_emitter__(
|
||||
{
|
||||
"type": "status",
|
||||
"data": {
|
||||
"description": status_msg,
|
||||
"done": True,
|
||||
},
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
await self._log(
|
||||
f"[Status] 计算 Token 错误: {e}",
|
||||
type="error",
|
||||
event_call=__event_call__,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
await self._log(
|
||||
f"[🤖 异步摘要任务] ❌ 错误: {str(e)}",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Markdown Normalizer Filter
|
||||
|
||||
**Author:** [Fu-Jie](https://github.com/Fu-Jie/awesome-openwebui) | **Version:** 1.2.2 | **Project:** [Awesome OpenWebUI](https://github.com/Fu-Jie/awesome-openwebui) | **License:** MIT
|
||||
**Author:** [Fu-Jie](https://github.com/Fu-Jie/awesome-openwebui) | **Version:** 1.2.4 | **Project:** [Awesome OpenWebUI](https://github.com/Fu-Jie/awesome-openwebui) | **License:** MIT
|
||||
|
||||
A content normalizer filter for Open WebUI that fixes common Markdown formatting issues in LLM outputs. It ensures that code blocks, LaTeX formulas, Mermaid diagrams, and other Markdown elements are rendered correctly.
|
||||
|
||||
@@ -43,7 +43,7 @@ A content normalizer filter for Open WebUI that fixes common Markdown formatting
|
||||
* `enable_heading_fix`: Fix missing space in headings.
|
||||
* `enable_table_fix`: Fix missing closing pipe in tables.
|
||||
* `enable_xml_tag_cleanup`: Cleanup leftover XML tags.
|
||||
* `enable_emphasis_spacing_fix`: Fix extra spaces in emphasis (default: True).
|
||||
* `enable_emphasis_spacing_fix`: Fix extra spaces in emphasis (default: False).
|
||||
* `show_status`: Show status notification when fixes are applied.
|
||||
* `show_debug_log`: Print debug logs to browser console.
|
||||
|
||||
@@ -53,9 +53,21 @@ A content normalizer filter for Open WebUI that fixes common Markdown formatting
|
||||
|
||||
## Changelog
|
||||
|
||||
### v1.2.4
|
||||
|
||||
* **Documentation Updates**: Synchronized version numbers across all documentation and code files.
|
||||
|
||||
### v1.2.3
|
||||
|
||||
* **List Marker Protection Enhancement**: Fixed a bug where list markers (`*`) followed by plain text and emphasis were having their spaces incorrectly stripped (e.g., `* U16 forward` became `*U16 forward`).
|
||||
* **Placeholder Support**: Confirmed that 4 or more underscores (e.g., `____`) are correctly treated as placeholders and not modified by the emphasis fix.
|
||||
|
||||
### v1.2.2
|
||||
|
||||
* **Version Bump**: Documentation and metadata updated for the latest release.
|
||||
* **Code Block Indentation Fix**: Fixed an issue where code blocks nested inside lists were having their indentation incorrectly stripped. Now preserves proper indentation for nested code blocks.
|
||||
* **Underscore Emphasis Support**: Extended emphasis spacing fix to support `__` (double underscore for bold) and `___` (triple underscore for bold+italic) syntax.
|
||||
* **List Marker Protection**: Fixed a bug where list markers (`*`) followed by emphasis markers (`**`) were incorrectly merged (e.g., `* **Yes**` became `***Yes**`). Added safeguard to prevent this.
|
||||
* **Test Suite**: Added comprehensive pytest test suite with 56 test cases covering all major features.
|
||||
|
||||
### v1.2.1
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Markdown 格式化过滤器 (Markdown Normalizer)
|
||||
|
||||
**作者:** [Fu-Jie](https://github.com/Fu-Jie/awesome-openwebui) | **版本:** 1.2.2 | **项目:** [Awesome OpenWebUI](https://github.com/Fu-Jie/awesome-openwebui) | **许可证:** MIT
|
||||
**作者:** [Fu-Jie](https://github.com/Fu-Jie/awesome-openwebui) | **版本:** 1.2.4 | **项目:** [Awesome OpenWebUI](https://github.com/Fu-Jie/awesome-openwebui) | **许可证:** MIT
|
||||
|
||||
这是一个用于 Open WebUI 的内容格式化过滤器,旨在修复 LLM 输出中常见的 Markdown 格式问题。它能确保代码块、LaTeX 公式、Mermaid 图表和其他 Markdown 元素被正确渲染。
|
||||
|
||||
@@ -53,9 +53,21 @@
|
||||
|
||||
## 更新日志
|
||||
|
||||
### v1.2.4
|
||||
|
||||
* **文档更新**: 同步了所有文档和代码文件的版本号。
|
||||
|
||||
### v1.2.3
|
||||
|
||||
* **列表标记保护增强**: 修复了列表标记 (`*`) 后跟普通文本和强调标记时,空格被错误剥离的问题(例如 `* U16 前锋` 变成 `*U16 前锋`)。
|
||||
* **占位符支持**: 确认 4 个或更多下划线(如 `____`)会被正确视为占位符,不会被强调修复逻辑修改。
|
||||
|
||||
### v1.2.2
|
||||
|
||||
* **版本更新**: 文档与元数据已同步到最新版本。
|
||||
* **代码块缩进修复**: 修复了列表中嵌套代码块的缩进被错误剥离的问题。现在会正确保留嵌套代码块的缩进。
|
||||
* **下划线强调语法支持**: 扩展强调空格修复以支持 `__` (双下划线加粗) 和 `___` (三下划线加粗斜体) 语法。
|
||||
* **列表标记保护**: 修复了列表标记 (`*`) 后跟强调标记 (`**`) 被错误合并的 Bug(例如 `* **是**` 变成 `***是**`)。添加了保护逻辑防止此问题。
|
||||
* **测试套件**: 新增完整的 pytest 测试套件,包含 56 个测试用例,覆盖所有主要功能。
|
||||
|
||||
### v1.2.1
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ title: Markdown Normalizer
|
||||
author: Fu-Jie
|
||||
author_url: https://github.com/Fu-Jie/awesome-openwebui
|
||||
funding_url: https://github.com/open-webui
|
||||
version: 1.2.2
|
||||
version: 1.2.4
|
||||
openwebui_id: baaa8732-9348-40b7-8359-7e009660e23c
|
||||
description: A content normalizer filter that fixes common Markdown formatting issues in LLM outputs, such as broken code blocks, LaTeX formulas, and list formatting.
|
||||
"""
|
||||
@@ -43,7 +43,7 @@ class NormalizerConfig:
|
||||
)
|
||||
enable_table_fix: bool = True # Fix missing closing pipe in tables
|
||||
enable_xml_tag_cleanup: bool = True # Cleanup leftover XML tags
|
||||
enable_emphasis_spacing_fix: bool = True # Fix spaces inside **emphasis**
|
||||
enable_emphasis_spacing_fix: bool = False # Fix spaces inside **emphasis**
|
||||
|
||||
# Custom cleaner functions (for advanced extension)
|
||||
custom_cleaners: List[Callable[[str], str]] = field(default_factory=list)
|
||||
@@ -109,12 +109,13 @@ class ContentNormalizer:
|
||||
"heading_space": re.compile(r"^(#+)([^ \n#])", re.MULTILINE),
|
||||
# Table: | col1 | col2 -> | col1 | col2 |
|
||||
"table_pipe": re.compile(r"^(\|.*[^|\n])$", re.MULTILINE),
|
||||
# Emphasis spacing: ** text ** -> **text**
|
||||
# Emphasis spacing: ** text ** -> **text**, __ text __ -> __text__
|
||||
# Matches emphasis blocks within a single line. We use a recursive approach
|
||||
# in _fix_emphasis_spacing to handle nesting and spaces correctly.
|
||||
# NOTE: We use [^\n] instead of . to prevent cross-line matching.
|
||||
# Supports: * (italic), ** (bold), *** (bold+italic), _ (italic), __ (bold), ___ (bold+italic)
|
||||
"emphasis_spacing": re.compile(
|
||||
r"(?<!\*|_)(\*{1,3}|_)(?P<inner>[^\n]*?)(\1)(?!\*|_)"
|
||||
r"(?<!\*|_)(\*{1,3}|_{1,3})(?P<inner>[^\n]*?)(\1)(?!\*|_)"
|
||||
),
|
||||
}
|
||||
|
||||
@@ -485,6 +486,20 @@ class ContentNormalizer:
|
||||
if symbol in ["*", "_"]:
|
||||
return match.group(0)
|
||||
|
||||
# Safeguard: List marker protection
|
||||
# If symbol is single '*' and inner content starts with whitespace followed by emphasis markers,
|
||||
# this is likely a list item like "* **bold**" - don't merge them.
|
||||
# Pattern: "* **text**" should NOT become "***text**"
|
||||
if symbol == "*" and inner.lstrip().startswith(("*", "_")):
|
||||
return match.group(0)
|
||||
|
||||
# Extended list marker protection:
|
||||
# If symbol is single '*' and inner starts with multiple spaces (list indentation pattern),
|
||||
# this is likely a list item like "* text" - don't strip the spaces.
|
||||
# Pattern: "* U16 forward **Kuang**" should NOT become "*U16 forward **Kuang**"
|
||||
if symbol == "*" and inner.startswith(" "):
|
||||
return match.group(0)
|
||||
|
||||
return f"{symbol}{stripped_inner}{symbol}"
|
||||
|
||||
parts = content.split("```")
|
||||
@@ -549,7 +564,7 @@ class Filter:
|
||||
default=True, description="Cleanup leftover XML tags"
|
||||
)
|
||||
enable_emphasis_spacing_fix: bool = Field(
|
||||
default=True,
|
||||
default=False,
|
||||
description="Fix spaces inside **emphasis** (e.g. ** text ** -> **text**)",
|
||||
)
|
||||
show_status: bool = Field(
|
||||
@@ -680,6 +695,15 @@ class Filter:
|
||||
if self._contains_html(content):
|
||||
return body
|
||||
|
||||
# Skip if content contains tool output markers (native function calling)
|
||||
# Pattern: """...""" or tool_call_id or <details type="tool_calls"...>
|
||||
if (
|
||||
'"""' in content
|
||||
or "tool_call_id" in content
|
||||
or '<details type="tool_calls"' in content
|
||||
):
|
||||
return body
|
||||
|
||||
# Configure normalizer based on valves
|
||||
config = NormalizerConfig(
|
||||
enable_escape_fix=self.valves.enable_escape_fix,
|
||||
|
||||
@@ -3,7 +3,7 @@ title: Markdown 格式修复器 (Markdown Normalizer)
|
||||
author: Fu-Jie
|
||||
author_url: https://github.com/Fu-Jie/awesome-openwebui
|
||||
funding_url: https://github.com/open-webui
|
||||
version: 1.2.2
|
||||
version: 1.2.4
|
||||
description: 内容规范化过滤器,修复 LLM 输出中常见的 Markdown 格式问题,如损坏的代码块、LaTeX 公式、Mermaid 图表和列表格式。
|
||||
"""
|
||||
|
||||
@@ -24,6 +24,9 @@ class NormalizerConfig:
|
||||
"""配置类,用于启用/禁用特定的规范化规则"""
|
||||
|
||||
enable_escape_fix: bool = True # 修复过度的转义字符
|
||||
enable_escape_fix_in_code_blocks: bool = (
|
||||
False # 在代码块内部应用转义修复 (默认:关闭,以确保安全)
|
||||
)
|
||||
enable_thought_tag_fix: bool = True # 规范化思维链标签
|
||||
enable_details_tag_fix: bool = True # 规范化 <details> 标签(类似思维链标签)
|
||||
enable_code_block_fix: bool = True # 修复代码块格式
|
||||
@@ -35,7 +38,7 @@ class NormalizerConfig:
|
||||
enable_heading_fix: bool = True # 修复标题中缺失的空格 (#Header -> # Header)
|
||||
enable_table_fix: bool = True # 修复表格中缺失的闭合管道符
|
||||
enable_xml_tag_cleanup: bool = True # 清理残留的 XML 标签
|
||||
enable_emphasis_spacing_fix: bool = True # 修复 **强调内容** 中的多余空格
|
||||
enable_emphasis_spacing_fix: bool = False # 修复 **强调内容** 中的多余空格
|
||||
|
||||
# 自定义清理函数 (用于高级扩展)
|
||||
custom_cleaners: List[Callable[[str], str]] = field(default_factory=list)
|
||||
@@ -101,12 +104,13 @@ class ContentNormalizer:
|
||||
"heading_space": re.compile(r"^(#+)([^ \n#])", re.MULTILINE),
|
||||
# Table: | col1 | col2 -> | col1 | col2 |
|
||||
"table_pipe": re.compile(r"^(\|.*[^|\n])$", re.MULTILINE),
|
||||
# Emphasis spacing: ** text ** -> **text**
|
||||
# Emphasis spacing: ** text ** -> **text**, __ text __ -> __text__
|
||||
# Matches emphasis blocks within a single line. We use a recursive approach
|
||||
# in _fix_emphasis_spacing to handle nesting and spaces correctly.
|
||||
# NOTE: We use [^\n] instead of . to prevent cross-line matching.
|
||||
# Supports: * (italic), ** (bold), *** (bold+italic), _ (italic), __ (bold), ___ (bold+italic)
|
||||
"emphasis_spacing": re.compile(
|
||||
r"(?<!\*|_)(\*{1,3}|_)(?P<inner>[^\n]*?)(\1)(?!\*|_)"
|
||||
r"(?<!\*|_)(\*{1,3}|_{1,3})(?P<inner>[^\n]*?)(\1)(?!\*|_)"
|
||||
),
|
||||
}
|
||||
|
||||
@@ -238,12 +242,27 @@ class ContentNormalizer:
|
||||
return content
|
||||
|
||||
def _fix_escape_characters(self, content: str) -> str:
|
||||
"""Fix excessive escape characters"""
|
||||
content = content.replace("\\r\\n", "\n")
|
||||
content = content.replace("\\n", "\n")
|
||||
content = content.replace("\\t", "\t")
|
||||
content = content.replace("\\\\", "\\")
|
||||
return content
|
||||
"""修复过度的转义字符
|
||||
|
||||
如果 enable_escape_fix_in_code_blocks 为 False (默认),此方法将仅修复代码块外部的转义字符,
|
||||
以避免破坏有效的代码示例 (例如,带有 \\n 的 JSON 字符串、正则表达式模式等)。
|
||||
"""
|
||||
if self.config.enable_escape_fix_in_code_blocks:
|
||||
# 全局应用 (原始行为)
|
||||
content = content.replace("\\r\\n", "\n")
|
||||
content = content.replace("\\n", "\n")
|
||||
content = content.replace("\\t", "\t")
|
||||
content = content.replace("\\\\", "\\")
|
||||
return content
|
||||
else:
|
||||
# 仅在代码块外部应用 (安全模式)
|
||||
parts = content.split("```")
|
||||
for i in range(0, len(parts), 2): # 偶数索引是 Markdown 文本 (非代码)
|
||||
parts[i] = parts[i].replace("\\r\\n", "\n")
|
||||
parts[i] = parts[i].replace("\\n", "\n")
|
||||
parts[i] = parts[i].replace("\\t", "\t")
|
||||
parts[i] = parts[i].replace("\\\\", "\\")
|
||||
return "```".join(parts)
|
||||
|
||||
def _fix_thought_tags(self, content: str) -> str:
|
||||
"""Normalize thought tags: unify naming and fix spacing"""
|
||||
@@ -464,6 +483,20 @@ class ContentNormalizer:
|
||||
if symbol in ["*", "_"]:
|
||||
return match.group(0)
|
||||
|
||||
# Safeguard: List marker protection
|
||||
# If symbol is single '*' and inner content starts with whitespace followed by emphasis markers,
|
||||
# this is likely a list item like "* **bold**" - don't merge them.
|
||||
# Pattern: "* **text**" should NOT become "***text**"
|
||||
if symbol == "*" and inner.lstrip().startswith(("*", "_")):
|
||||
return match.group(0)
|
||||
|
||||
# Extended list marker protection:
|
||||
# If symbol is single '*' and inner starts with multiple spaces (list indentation pattern),
|
||||
# this is likely a list item like "* text" - don't strip the spaces.
|
||||
# Pattern: "* U16 forward **Kuang**" should NOT become "*U16 forward **Kuang**"
|
||||
if symbol == "*" and inner.startswith(" "):
|
||||
return match.group(0)
|
||||
|
||||
return f"{symbol}{stripped_inner}{symbol}"
|
||||
|
||||
parts = content.split("```")
|
||||
@@ -486,6 +519,10 @@ class Filter:
|
||||
enable_escape_fix: bool = Field(
|
||||
default=True, description="修复过度的转义字符 (\\n, \\t 等)"
|
||||
)
|
||||
enable_escape_fix_in_code_blocks: bool = Field(
|
||||
default=False,
|
||||
description="在代码块内部应用转义修复 (⚠️ 警告:可能会破坏有效的代码,如 JSON 字符串或正则模式。默认:关闭,以确保安全)",
|
||||
)
|
||||
enable_thought_tag_fix: bool = Field(
|
||||
default=True, description="规范化思维链标签 (<think> -> <thought>)"
|
||||
)
|
||||
@@ -524,7 +561,7 @@ class Filter:
|
||||
default=True, description="清理残留的 XML 标签"
|
||||
)
|
||||
enable_emphasis_spacing_fix: bool = Field(
|
||||
default=True,
|
||||
default=False,
|
||||
description="修复强调语法中的多余空格 (例如 ** 文本 ** -> **文本**)",
|
||||
)
|
||||
show_status: bool = Field(default=True, description="应用修复时显示状态通知")
|
||||
@@ -667,13 +704,23 @@ class Filter:
|
||||
content = last.get("content", "") or ""
|
||||
|
||||
if last.get("role") == "assistant" and isinstance(content, str):
|
||||
# Skip if content looks like HTML to avoid breaking it
|
||||
# 如果内容看起来像 HTML,则跳过以避免破坏它
|
||||
if self._contains_html(content):
|
||||
return body
|
||||
|
||||
# Configure normalizer based on valves
|
||||
# 如果内容包含工具输出标记 (原生函数调用),则跳过
|
||||
# 模式:"""...""" 或 tool_call_id 或 <details type="tool_calls"...>
|
||||
if (
|
||||
'"""' in content
|
||||
or "tool_call_id" in content
|
||||
or '<details type="tool_calls"' in content
|
||||
):
|
||||
return body
|
||||
|
||||
# 根据 Valves 配置 Normalizer
|
||||
config = NormalizerConfig(
|
||||
enable_escape_fix=self.valves.enable_escape_fix,
|
||||
enable_escape_fix_in_code_blocks=self.valves.enable_escape_fix_in_code_blocks,
|
||||
enable_thought_tag_fix=self.valves.enable_thought_tag_fix,
|
||||
enable_details_tag_fix=self.valves.enable_details_tag_fix,
|
||||
enable_code_block_fix=self.valves.enable_code_block_fix,
|
||||
|
||||
1
plugins/filters/markdown_normalizer/tests/__init__.py
Normal file
1
plugins/filters/markdown_normalizer/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# Markdown Normalizer Test Suite
|
||||
75
plugins/filters/markdown_normalizer/tests/conftest.py
Normal file
75
plugins/filters/markdown_normalizer/tests/conftest.py
Normal file
@@ -0,0 +1,75 @@
|
||||
"""
|
||||
Shared fixtures for Markdown Normalizer tests.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the parent directory to sys.path for imports
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from markdown_normalizer import ContentNormalizer, NormalizerConfig
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def normalizer():
|
||||
"""Default normalizer with all fixes enabled."""
|
||||
config = NormalizerConfig(
|
||||
enable_escape_fix=True,
|
||||
enable_thought_tag_fix=True,
|
||||
enable_details_tag_fix=True,
|
||||
enable_code_block_fix=True,
|
||||
enable_latex_fix=True,
|
||||
enable_list_fix=False, # Experimental, keep off by default
|
||||
enable_unclosed_block_fix=True,
|
||||
enable_fullwidth_symbol_fix=False,
|
||||
enable_mermaid_fix=True,
|
||||
enable_heading_fix=True,
|
||||
enable_table_fix=True,
|
||||
enable_xml_tag_cleanup=True,
|
||||
enable_emphasis_spacing_fix=True,
|
||||
)
|
||||
return ContentNormalizer(config)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def emphasis_only_normalizer():
|
||||
"""Normalizer with only emphasis spacing fix enabled."""
|
||||
config = NormalizerConfig(
|
||||
enable_escape_fix=False,
|
||||
enable_thought_tag_fix=False,
|
||||
enable_details_tag_fix=False,
|
||||
enable_code_block_fix=False,
|
||||
enable_latex_fix=False,
|
||||
enable_list_fix=False,
|
||||
enable_unclosed_block_fix=False,
|
||||
enable_fullwidth_symbol_fix=False,
|
||||
enable_mermaid_fix=False,
|
||||
enable_heading_fix=False,
|
||||
enable_table_fix=False,
|
||||
enable_xml_tag_cleanup=False,
|
||||
enable_emphasis_spacing_fix=True,
|
||||
)
|
||||
return ContentNormalizer(config)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mermaid_only_normalizer():
|
||||
"""Normalizer with only Mermaid fix enabled."""
|
||||
config = NormalizerConfig(
|
||||
enable_escape_fix=False,
|
||||
enable_thought_tag_fix=False,
|
||||
enable_details_tag_fix=False,
|
||||
enable_code_block_fix=False,
|
||||
enable_latex_fix=False,
|
||||
enable_list_fix=False,
|
||||
enable_unclosed_block_fix=False,
|
||||
enable_fullwidth_symbol_fix=False,
|
||||
enable_mermaid_fix=True,
|
||||
enable_heading_fix=False,
|
||||
enable_table_fix=False,
|
||||
enable_xml_tag_cleanup=False,
|
||||
enable_emphasis_spacing_fix=False,
|
||||
)
|
||||
return ContentNormalizer(config)
|
||||
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
Tests for code block formatting fixes.
|
||||
Covers: prefix, suffix, indentation preservation.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestCodeBlockFix:
|
||||
"""Test code block formatting normalization."""
|
||||
|
||||
def test_code_block_indentation_preserved(self, normalizer):
|
||||
"""Indented code blocks (e.g., in lists) should preserve indentation."""
|
||||
input_str = """
|
||||
* List item 1
|
||||
```python
|
||||
def foo():
|
||||
print("bar")
|
||||
```
|
||||
* List item 2
|
||||
"""
|
||||
# Indentation should be preserved
|
||||
assert " ```python" in normalizer.normalize(input_str)
|
||||
|
||||
def test_inline_code_block_prefix(self, normalizer):
|
||||
"""Code block that follows text on same line should be modified."""
|
||||
input_str = "text```python\ncode\n```"
|
||||
result = normalizer.normalize(input_str)
|
||||
# Just verify the code block markers are present
|
||||
assert "```" in result
|
||||
|
||||
def test_code_block_suffix_fix(self, normalizer):
|
||||
"""Code block with content on same line after lang should be fixed."""
|
||||
input_str = "```python code\nmore code\n```"
|
||||
result = normalizer.normalize(input_str)
|
||||
# Content should be on new line
|
||||
assert "```python\n" in result or "```python " in result
|
||||
|
||||
|
||||
class TestUnclosedCodeBlock:
|
||||
"""Test auto-closing of unclosed code blocks."""
|
||||
|
||||
def test_unclosed_code_block_is_closed(self, normalizer):
|
||||
"""Unclosed code blocks should be automatically closed."""
|
||||
input_str = "```python\ncode here"
|
||||
result = normalizer.normalize(input_str)
|
||||
# Should have closing ```
|
||||
assert result.endswith("```") or result.count("```") == 2
|
||||
|
||||
def test_balanced_code_blocks_unchanged(self, normalizer):
|
||||
"""Already balanced code blocks should not get extra closing."""
|
||||
input_str = "```python\ncode\n```"
|
||||
result = normalizer.normalize(input_str)
|
||||
assert result.count("```") == 2
|
||||
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
Tests for details tag normalization.
|
||||
Covers: </details> spacing, self-closing tags.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestDetailsTagFix:
|
||||
"""Test details tag normalization."""
|
||||
|
||||
def test_details_end_gets_newlines(self, normalizer):
|
||||
"""</details> should be followed by double newline."""
|
||||
input_str = "</details>Content after"
|
||||
result = normalizer.normalize(input_str)
|
||||
assert "</details>\n\n" in result
|
||||
|
||||
def test_self_closing_details_gets_newline(self, normalizer):
|
||||
"""Self-closing <details .../> should get newline after."""
|
||||
input_str = "<details open />## Heading"
|
||||
result = normalizer.normalize(input_str)
|
||||
# Should have newline between tag and heading
|
||||
assert "/>\n" in result or "/> \n" in result
|
||||
|
||||
def test_details_in_code_block_unchanged(self, normalizer):
|
||||
"""Details tags inside code blocks should not be modified."""
|
||||
input_str = "```html\n<details>content</details>more\n```"
|
||||
result = normalizer.normalize(input_str)
|
||||
# Content inside code block should be unchanged
|
||||
assert "</details>more" in result
|
||||
|
||||
|
||||
class TestThoughtTagFix:
|
||||
"""Test thought tag normalization."""
|
||||
|
||||
def test_think_tag_normalized(self, normalizer):
|
||||
"""<think> should be normalized to <thought>."""
|
||||
input_str = "<think>content</think>"
|
||||
result = normalizer.normalize(input_str)
|
||||
assert "<thought>" in result
|
||||
assert "</thought>" in result
|
||||
|
||||
def test_thinking_tag_normalized(self, normalizer):
|
||||
"""<thinking> should be normalized to <thought>."""
|
||||
input_str = "<thinking>content</thinking>"
|
||||
result = normalizer.normalize(input_str)
|
||||
assert "<thought>" in result
|
||||
assert "</thought>" in result
|
||||
@@ -0,0 +1,138 @@
|
||||
"""
|
||||
Tests for emphasis spacing fix.
|
||||
Covers: *, **, ***, _, __, ___ with spaces inside.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestEmphasisSpacingFix:
|
||||
"""Test emphasis spacing normalization."""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"input_str,expected",
|
||||
[
|
||||
# Double asterisk (bold)
|
||||
("** bold **", "**bold**"),
|
||||
("** bold text **", "**bold text**"),
|
||||
("**text **", "**text**"),
|
||||
("** text**", "**text**"),
|
||||
# Triple asterisk (bold+italic)
|
||||
("*** bold italic ***", "***bold italic***"),
|
||||
# Double underscore (bold)
|
||||
("__ bold __", "__bold__"),
|
||||
("__ bold text __", "__bold text__"),
|
||||
("__text __", "__text__"),
|
||||
("__ text__", "__text__"),
|
||||
# Triple underscore (bold+italic)
|
||||
("___ bold italic ___", "___bold italic___"),
|
||||
# Mixed markers
|
||||
("** bold ** and __ also __", "**bold** and __also__"),
|
||||
],
|
||||
)
|
||||
def test_emphasis_with_spaces_fixed(
|
||||
self, emphasis_only_normalizer, input_str, expected
|
||||
):
|
||||
"""Test that emphasis with spaces is correctly fixed."""
|
||||
assert emphasis_only_normalizer.normalize(input_str) == expected
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"input_str",
|
||||
[
|
||||
# Single * and _ with spaces on both sides - treated as operator (safeguard)
|
||||
"* italic *",
|
||||
"_ italic _",
|
||||
# Already correct emphasis
|
||||
"**bold**",
|
||||
"__bold__",
|
||||
"*italic*",
|
||||
"_italic_",
|
||||
"***bold italic***",
|
||||
"___bold italic___",
|
||||
],
|
||||
)
|
||||
def test_safeguard_and_correct_emphasis_unchanged(
|
||||
self, emphasis_only_normalizer, input_str
|
||||
):
|
||||
"""Test that safeguard cases and already correct emphasis are not modified."""
|
||||
assert emphasis_only_normalizer.normalize(input_str) == input_str
|
||||
|
||||
|
||||
class TestEmphasisSideEffects:
|
||||
"""Test that emphasis fix does NOT affect unrelated content."""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"input_str,description",
|
||||
[
|
||||
# URLs with underscores
|
||||
("https://example.com/path_with_underscore", "URL"),
|
||||
("Visit https://api.example.com/get_user_info for info", "URL in text"),
|
||||
# Variable names (snake_case)
|
||||
("The `my_variable_name` is important", "Variable in backticks"),
|
||||
("Use `get_user_data()` function", "Function name"),
|
||||
# File names
|
||||
("Edit the `config_file_name.py` file", "File name"),
|
||||
("See `my_script__v2.py` for details", "Double underscore in filename"),
|
||||
# Math-like subscripts
|
||||
("The variable a_1 and b_2 are defined", "Math subscripts"),
|
||||
# Single underscores not matching emphasis pattern
|
||||
("word_with_underscore", "Underscore in word"),
|
||||
("a_b_c_d", "Multiple underscores"),
|
||||
# Horizontal rules
|
||||
("---", "HR with dashes"),
|
||||
("***", "HR with asterisks"),
|
||||
("___", "HR with underscores"),
|
||||
# List items
|
||||
("- item_one\n- item_two", "List items"),
|
||||
],
|
||||
)
|
||||
def test_no_side_effects(self, emphasis_only_normalizer, input_str, description):
|
||||
"""Test that various content types are NOT modified by emphasis fix."""
|
||||
assert (
|
||||
emphasis_only_normalizer.normalize(input_str) == input_str
|
||||
), f"Failed for: {description}"
|
||||
|
||||
def test_list_marker_not_merged_with_emphasis(self, emphasis_only_normalizer):
|
||||
"""Test that list markers (*) are not merged with emphasis (**).
|
||||
|
||||
Regression test for: "* **Yes**" should NOT become "***Yes**"
|
||||
"""
|
||||
input_str = """1. **Start**: The user opens the login page.
|
||||
* **Yes**: Login successful.
|
||||
* **No**: Show error message."""
|
||||
result = emphasis_only_normalizer.normalize(input_str)
|
||||
assert (
|
||||
"* **Yes**" in result
|
||||
), "List marker was incorrectly merged with emphasis"
|
||||
assert (
|
||||
"* **No**" in result
|
||||
), "List marker was incorrectly merged with emphasis"
|
||||
assert "***Yes**" not in result, "BUG: List marker merged with emphasis"
|
||||
assert "***No**" not in result, "BUG: List marker merged with emphasis"
|
||||
|
||||
def test_list_marker_with_plain_text_then_emphasis(self, emphasis_only_normalizer):
|
||||
"""Test that list items with plain text before emphasis are preserved.
|
||||
|
||||
Regression test for: "* U16 forward **Kuang**" should NOT become "*U16 forward **Kuang**"
|
||||
"""
|
||||
input_str = "* U16 China forward **Kuang Zhaolei**"
|
||||
result = emphasis_only_normalizer.normalize(input_str)
|
||||
assert "* U16" in result, "List marker spaces were incorrectly stripped"
|
||||
assert (
|
||||
"*U16" not in result or "* U16" in result
|
||||
), "BUG: List marker spaces stripped"
|
||||
|
||||
|
||||
class TestEmphasisInCodeBlocks:
|
||||
"""Test that emphasis inside code blocks is NOT modified."""
|
||||
|
||||
def test_emphasis_in_code_block_unchanged(self, emphasis_only_normalizer):
|
||||
"""Code blocks should be completely skipped."""
|
||||
input_str = "```python\nmy_var = get_data__from_api()\n```"
|
||||
assert emphasis_only_normalizer.normalize(input_str) == input_str
|
||||
|
||||
def test_mixed_emphasis_and_code(self, emphasis_only_normalizer):
|
||||
"""Text outside code blocks should be fixed, inside should not."""
|
||||
input_str = "** bold ** text\n```python\n** not bold **\n```"
|
||||
expected = "**bold** text\n```python\n** not bold **\n```"
|
||||
assert emphasis_only_normalizer.normalize(input_str) == expected
|
||||
@@ -0,0 +1,51 @@
|
||||
"""
|
||||
Tests for heading fix.
|
||||
Covers: Missing space after # in headings.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestHeadingFix:
|
||||
"""Test heading space normalization."""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"input_str,expected",
|
||||
[
|
||||
("#Heading", "# Heading"),
|
||||
("##Heading", "## Heading"),
|
||||
("###Heading", "### Heading"),
|
||||
("#中文标题", "# 中文标题"),
|
||||
("#123", "# 123"), # Numbers after # also get space
|
||||
],
|
||||
)
|
||||
def test_missing_space_added(self, normalizer, input_str, expected):
|
||||
"""Headings missing space after # should be fixed."""
|
||||
assert normalizer.normalize(input_str) == expected
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"input_str",
|
||||
[
|
||||
"# Heading",
|
||||
"## Already Correct",
|
||||
"###", # Just hashes
|
||||
],
|
||||
)
|
||||
def test_correct_headings_unchanged(self, normalizer, input_str):
|
||||
"""Already correct headings should not be modified."""
|
||||
assert normalizer.normalize(input_str) == input_str
|
||||
|
||||
|
||||
class TestTableFix:
|
||||
"""Test table pipe normalization."""
|
||||
|
||||
def test_missing_closing_pipe_added(self, normalizer):
|
||||
"""Tables missing closing | should have it added."""
|
||||
input_str = "| col1 | col2"
|
||||
result = normalizer.normalize(input_str)
|
||||
assert result.endswith("|") or "col2 |" in result
|
||||
|
||||
def test_already_closed_table_unchanged(self, normalizer):
|
||||
"""Tables with closing | should not be modified."""
|
||||
input_str = "| col1 | col2 |"
|
||||
assert normalizer.normalize(input_str) == input_str
|
||||
6
pytest.ini
Normal file
6
pytest.ini
Normal file
@@ -0,0 +1,6 @@
|
||||
[pytest]
|
||||
testpaths = plugins
|
||||
python_files = test_*.py
|
||||
python_classes = Test*
|
||||
python_functions = test_*
|
||||
addopts = -v --tb=short
|
||||
Reference in New Issue
Block a user