feat(project): sync engineering standards and finalize markdown-normalizer v1.2.7

- Update .github/copilot-instructions.md with latest i18n and naming standards
- Add docs/development/issue-reply-guide.md for professional community engagement
- Sync all documentation (MKDocs, READMEs, Docs) to v1.2.7
- Include CI/CD and Agent instruction templates for better automation
This commit is contained in:
fujie
2026-02-24 15:13:52 +08:00
parent 2da934dd92
commit 377534e6c9
20 changed files with 1675 additions and 337 deletions

View File

@@ -0,0 +1,154 @@
# Copilot Engineering Configuration Plan
> This document defines production-grade engineering configuration for plugin development with GitHub Copilot, Gemini CLI, and antigravity development mode.
---
## 1. Goals
- Standardize plugin engineering workflow for AI-assisted development.
- Support dual assistant stack:
- GitHub Copilot (primary in-editor agent)
- Gemini CLI (secondary external execution/research lane)
- Introduce antigravity development mode for safer, reversible, high-velocity iteration.
---
## 2. Scope
This plan applies to:
- Plugin development (`actions`, `filters`, `pipes`, `pipelines`, `tools`)
- Documentation synchronization
- File generation/delivery workflow in OpenWebUI
- Streaming/tool-call rendering compatibility
---
## 3. Engineering Configuration (Copilot-centered)
### 3.1 Source of truth
- Primary standard file: `.github/copilot-instructions.md`
- Agent workflow file: `.agent/workflows/plugin-development.md`
- Runtime guidance docs:
- `docs/development/plugin-guide.md`
- `docs/development/plugin-guide.zh.md`
### 3.2 Required development contract
- Single-file i18n plugin source.
- Bilingual README (`README.md` + `README_CN.md`).
- Safe context extraction (`_get_user_context`, `_get_chat_context`).
- Structured event handling (`status`, `notification`, `execute`).
- No silent failures; logging + user-visible status.
### 3.3 Tool definition contract (Copilot SDK)
- Define tool params explicitly with `pydantic.BaseModel`.
- Use `params_type` in tool registration.
- Preserve defaults by avoiding forced null overrides.
- Keep tool names normalized and deterministic.
---
## 4. Gemini CLI Compatibility Profile
Gemini CLI is treated as a parallel capability channel, not a replacement.
### 4.1 Usage boundary
- Use for:
- rapid drafts
- secondary reasoning
- cross-checking migration plans
- Do not bypass repository conventions or plugin contracts.
### 4.2 Output normalization
All Gemini CLI outputs must be normalized before merge:
- Match repository style and naming rules.
- Preserve OpenWebUI plugin signatures and context methods.
- Convert speculative outputs into explicit, testable implementation points.
### 4.3 Conflict policy
When Copilot and Gemini suggestions differ:
1. Prefer repository standard compliance.
2. Prefer safer fallback behavior.
3. Prefer lower integration risk.
---
## 5. Antigravity Development Mode
Antigravity mode means high-speed delivery with strict reversibility.
### 5.1 Core principles
- Small, isolated edits
- Deterministic interfaces
- Multi-level fallback paths
- Roll-forward and rollback both feasible
### 5.2 Required patterns
- Timeout guards on frontend execution calls.
- Path sandbox validation for all workspace file operations.
- Dual-channel upload fallback (API first, local/DB fallback).
- Progressive status reporting for long-running tasks.
---
## 6. File Creation & Delivery Standard
### 6.1 Create files in controlled workspace
- Write artifacts in current workspace scope.
- Never use paths outside workspace boundary for deliverables.
### 6.2 Publish protocol
Use 3-step delivery:
1. local write
2. publish from workspace
3. present `/api/v1/files/{id}/content` link
### 6.3 Metadata policy
- Set `skip_rag=true` for generated downloadable artifacts where applicable.
- Keep filename generation deterministic (`chat_title -> markdown_title -> user+date`).
---
## 7. Plugin Development Norms for Multi-Agent Stack
- Compatible with GitHub Copilot and Gemini CLI under same coding contract.
- Keep streaming compatible with OpenWebUI native blocks (`<think>`, `<details type="tool_calls">`).
- Escape tool card attributes safely (`&quot;`) for parser stability.
- Preserve async non-blocking behavior.
---
## 8. Documentation Sync Rule
Any meaningful plugin engineering change must sync:
1. plugin code
2. plugin bilingual README
3. docs plugin detail pages (EN/ZH)
4. docs plugin indexes (EN/ZH)
5. root README update badge/date when release is prepared
---
## 9. Acceptance Checklist
- Copilot config and workflow references are valid.
- Gemini CLI outputs can be merged without violating conventions.
- Antigravity safety mechanisms are present.
- File creation and publication flow is reproducible.
- Streaming/tool-card output format remains OpenWebUI-compatible.

View File

@@ -0,0 +1,149 @@
# Copilot 工程化配置设计
> 本文档定义面向插件开发的工程化配置方案:支持 GitHub Copilot、兼容 Gemini CLI并引入“反重力开发”模式。
---
## 1. 目标
- 建立统一、可落地的 AI 协同开发标准。
- 支持双通道助手体系:
- GitHub Copilot编辑器内主通道
- Gemini CLI外部补充通道
- 在高迭代速度下保障可回滚、可审计、可发布。
---
## 2. 适用范围
适用于以下类型开发:
- 插件代码(`actions` / `filters` / `pipes` / `pipelines` / `tools`
- 文档同步与发布准备
- OpenWebUI 文件创建与交付流程
- 流式输出与工具卡片兼容性
---
## 3. Copilot 工程化主配置
### 3.1 规范来源
- 主规范:`.github/copilot-instructions.md`
- 工作流:`.agent/workflows/plugin-development.md`
- 运行时开发指南:
- `docs/development/plugin-guide.md`
- `docs/development/plugin-guide.zh.md`
### 3.2 强制开发契约
- 单文件 i18n 插件源码。
- README 双语(`README.md` + `README_CN.md`)。
- 上下文统一入口(`_get_user_context``_get_chat_context`)。
- 事件标准化(`status``notification``execute`)。
- 禁止静默失败(用户可见状态 + 后端日志)。
### 3.3 Copilot SDK 工具契约
- 参数必须 `pydantic.BaseModel` 显式定义。
- 工具注册必须声明 `params_type`
- 保留默认值语义,避免把未传参数强制覆盖为 `null`
- 工具名需可预测、可规范化。
---
## 4. Gemini CLI 兼容配置
Gemini CLI 作为补充通道,而不是替代主规范。
### 4.1 使用边界
- 适合:草案生成、方案对照、迁移校验。
- 不得绕过仓库规范与插件契约。
### 4.2 输出归一化
Gemini CLI 输出合入前必须完成:
- 命名与结构对齐仓库约束。
- 保留 OpenWebUI 插件标准签名与上下文方法。
- 将“建议性文字”转换为可执行、可验证实现点。
### 4.3 冲突决策
Copilot 与 Gemini 建议冲突时按优先级处理:
1. 规范一致性优先
2. 安全回退优先
3. 低集成风险优先
---
## 5. 反重力开发Antigravity模式
反重力开发 = 高速迭代 + 强回退能力。
### 5.1 核心原则
- 小步、隔离、可逆变更
- 接口稳定、行为可预测
- 多级回退链路
- 支持前滚与回滚
### 5.2 强制模式
- 前端执行调用必须设置超时保护。
- 工作区文件操作必须做路径沙箱校验。
- 上传链路采用 API 优先 + 本地/DB 回退。
- 长任务必须分阶段状态反馈。
---
## 6. 文件创建与交付标准
### 6.1 创建范围
- 交付文件仅在受控 workspace 内创建。
- 禁止将交付产物写到 workspace 边界之外。
### 6.2 三步交付协议
1. 本地写入
2. 从 workspace 发布
3. 返回并展示 `/api/v1/files/{id}/content`
### 6.3 元数据策略
- 需要绕过检索时为产物标记 `skip_rag=true`
- 文件名采用确定性策略:`chat_title -> markdown_title -> user+date`
---
## 7. 多助手并行下的插件开发规范
- 在统一契约下同时兼容 GitHub Copilot 与 Gemini CLI。
- 流式输出保持 OpenWebUI 原生兼容(`<think>``<details type="tool_calls">`)。
- 工具卡片属性严格转义(`&quot;`)保证解析稳定。
- 全链路保持异步非阻塞。
---
## 8. 文档同步规则
插件工程化变更发生时,至少同步:
1. 插件代码
2. 插件双语 README
3. docs 详情页EN/ZH
4. docs 索引页EN/ZH
5. 发布准备阶段同步根 README 日期徽章
---
## 9. 验收清单
- Copilot 配置与工作流引用有效。
- Gemini CLI 输出可无缝合入且不破坏规范。
- 反重力安全机制完整。
- 文件创建/发布流程可复现。
- 流式与工具卡片格式保持 OpenWebUI 兼容。

View File

@@ -24,6 +24,14 @@ Learn how to develop plugins and contribute to OpenWebUI Extensions.
[:octicons-arrow-right-24: Read the Guide](documentation-guide.md)
- :material-robot:{ .lg .middle } **Copilot Engineering Plan**
---
Engineering configuration for GitHub Copilot + Gemini CLI + antigravity development mode.
[:octicons-arrow-right-24: Read the Plan](copilot-engineering-plan.md)
- :material-github:{ .lg .middle } **Contributing**
---
@@ -164,5 +172,6 @@ user_language = __user__.get("language", "en-US")
## Resources
- [Full Development Guide](plugin-guide.md)
- [Copilot Engineering Plan](copilot-engineering-plan.md)
- [Plugin Examples](https://github.com/Fu-Jie/openwebui-extensions/tree/main/plugins)
- [OpenWebUI Documentation](https://docs.openwebui.com/)

View File

@@ -24,6 +24,14 @@
[:octicons-arrow-right-24: 阅读指南](documentation-guide.md)
- :material-robot:{ .lg .middle } **Copilot 工程化配置**
---
面向 GitHub Copilot + Gemini CLI + 反重力开发模式的工程化设计文档。
[:octicons-arrow-right-24: 阅读文档](copilot-engineering-plan.md)
- :material-github:{ .lg .middle } **贡献指南**
---
@@ -164,5 +172,6 @@ user_language = __user__.get("language", "en-US")
## 资源
- [完整开发指南](plugin-guide.md)
- [Copilot 工程化配置](copilot-engineering-plan.md)
- [插件示例](https://github.com/Fu-Jie/openwebui-extensions/tree/main/plugins)
- [OpenWebUI 文档](https://docs.openwebui.com/)

View File

@@ -7,11 +7,13 @@
## 📚 Table of Contents
1. [Quick Start](#1-quick-start)
2. [Core Concepts & SDK Details](#2-core-concepts-sdk-details)
3. [Deep Dive into Plugin Types](#3-deep-dive-into-plugin-types)
4. [Advanced Development Patterns](#4-advanced-development-patterns)
5. [Best Practices & Design Principles](#5-best-practices-design-principles)
6. [Troubleshooting](#6-troubleshooting)
2. [Project Structure & Naming](#2-project-structure--naming)
3. [Core Concepts & SDK Details](#3-core-concepts--sdk-details)
4. [Deep Dive into Plugin Types](#4-deep-dive-into-plugin-types)
5. [Advanced Development Patterns](#5-advanced-development-patterns)
6. [Best Practices & Design Principles](#6-best-practices--design-principles)
7. [Workflow & Process](#7-workflow--process)
8. [Troubleshooting](#8-troubleshooting)
---
@@ -64,9 +66,39 @@ class Action:
---
## 2. Core Concepts & SDK Details
## 2. Project Structure & Naming
### 2.1 ⚠️ Important: Sync vs Async
### 2.1 Language & Code Requirements
- **Single Code File**: `plugins/{type}/{name}/{name}.py`. Never create separate source files for different languages.
- **Built-in i18n**: Must dynamically switch UI, prompts, and logs based on user language.
- **Documentation**: Must include both `README.md` (English) and `README_CN.md` (Chinese).
### 2.2 Docstring Standard
Each plugin file must start with a standardized docstring:
```python
"""
title: Plugin Name
author: Fu-Jie
author_url: https://github.com/Fu-Jie/openwebui-extensions
funding_url: https://github.com/open-webui
version: 0.1.0
icon_url: data:image/svg+xml;base64,<base64-encoded-svg>
requirements: dependency1==1.0.0, dependency2>=2.0.0
description: Brief description of plugin functionality.
"""
```
- **icon_url**: Required for Action plugins. Must be Base64 encoded SVG from [Lucide Icons](https://lucide.dev/icons/).
- **requirements**: Only list dependencies not installed in the OpenWebUI environment.
---
## 3. Core Concepts & SDK Details
### 3.1 ⚠️ Important: Sync vs Async
OpenWebUI plugins run within an `asyncio` event loop.
@@ -75,7 +107,7 @@ OpenWebUI plugins run within an `asyncio` event loop.
- **Pitfall**: Calling synchronous methods directly (e.g., `time.sleep`, `requests.get`) will freeze the entire server
- **Solution**: Wrap synchronous calls using `await asyncio.to_thread(sync_func, ...)`
### 2.2 Core Parameters
### 3.2 Core Parameters
All plugin methods (`inlet`, `outlet`, `pipe`, `action`) support injecting the following special parameters:
@@ -88,30 +120,43 @@ All plugin methods (`inlet`, `outlet`, `pipe`, `action`) support injecting the f
| `__event_emitter__` | `func` | **One-way Notification**. Used to send Toast notifications or status bar updates |
| `__event_call__` | `func` | **Two-way Interaction**. Used to execute JS code, show confirmation dialogs, or input boxes |
### 2.3 Configuration System (Valves)
### 3.3 Configuration System (Valves)
- **`Valves`**: Global admin configuration
- **`UserValves`**: User-level configuration (higher priority, overrides global)
Use Pydantic BaseModel to define configurable parameters. All Valves fields must use **UPPER_SNAKE_CASE**.
```python
class Filter:
from pydantic import BaseModel, Field
class Action:
class Valves(BaseModel):
API_KEY: str = Field(default="", description="Global API Key")
class UserValves(BaseModel):
API_KEY: str = Field(default="", description="User Private API Key")
def inlet(self, body, __user__):
# Prioritize user's Key
user_valves = __user__.get("valves", self.UserValves())
api_key = user_valves.API_KEY or self.valves.API_KEY
SHOW_STATUS: bool = Field(default=True, description="Whether to show operation status updates.")
# ...
```
### 3.4 Context Access
All plugins **must** use `_get_user_context` and `_get_chat_context` methods to safely extract information, rather than accessing `__user__` or `body` directly.
### 3.5 Event Emission & Logging
- **Event Emission**: Implement helper methods `_emit_status` and `_emit_notification`.
- **Frontend Console Debugging**: Highly recommended for real-time data flow viewing. Use `_emit_debug_log` to print structured debug logs in the browser console.
- **Server-side Logging**: Use Python's standard `logging` module. Do not use `print()`.
### 3.6 Database & File Storage
- **Database**: Re-use Open WebUI's internal database connection (`open_webui.internal.db`).
- **File Storage**: Implement multi-level fallback mechanisms (DB -> S3 -> Local -> URL -> API) to ensure compatibility across all storage configurations.
### 3.7 Internationalization (i18n)
Define a `TRANSLATIONS` dictionary and use a robust language detection mechanism (Multi-level Fallback: JS localStorage -> HTTP Accept-Language -> User Profile -> en-US).
---
## 3. Deep Dive into Plugin Types
## 4. Deep Dive into Plugin Types
### 3.1 Action
### 4.1 Action
**Role**: Adds buttons below messages that trigger upon user click.
@@ -136,7 +181,7 @@ async def action(self, body, __event_call__):
await __event_call__({"type": "execute", "data": {"code": js}})
```
### 3.2 Filter
### 4.2 Filter
**Role**: Middleware that intercepts and modifies requests/responses.
@@ -157,7 +202,7 @@ async def inlet(self, body, __metadata__):
return body
```
### 3.3 Pipe
### 4.3 Pipe
**Role**: Custom Model/Agent.
@@ -182,18 +227,27 @@ class Pipe:
return r.iter_lines()
```
### 4.4 Copilot SDK Tool Definition Standards
When developing custom tools for GitHub Copilot SDK, you **must** define a Pydantic `BaseModel` for parameters and explicitly reference it using `params_type` in `define_tool`.
### 4.5 Copilot SDK Streaming & Tool Card Standards
- **Reasoning Streaming**: Must use native `<think>` tags and ensure proper closure (`\n</think>\n`) before outputting main content or tool calls.
- **Native Tool Calls Block**: Output strictly formatted HTML `<details type="tool_calls"...>` blocks. Ensure all double quotes in attributes are escaped as `&quot;`.
---
## 4. Advanced Development Patterns
## 5. Advanced Development Patterns
### 4.1 Pipe & Filter Collaboration
### 5.1 Pipe & Filter Collaboration
Use `__request__.app.state` to share data between plugins:
- **Pipe**: `__request__.app.state.search_results = [...]`
- **Filter (Outlet)**: Read `search_results` and format them as citation links
### 4.2 Async Background Tasks
### 5.2 Async Background Tasks
Execute time-consuming operations without blocking the user response:
@@ -209,7 +263,7 @@ async def background_job(self, chat_id):
pass
```
### 4.3 Calling Built-in LLM
### 5.3 Calling Built-in LLM
```python
from open_webui.utils.chat import generate_chat_completion
@@ -235,13 +289,13 @@ llm_response = await generate_chat_completion(
)
```
### 4.4 JS Render to Markdown (Data URL Embedding)
### 5.4 JS Render to Markdown (Data URL Embedding)
For scenarios requiring complex frontend rendering (e.g., AntV charts, Mermaid diagrams) but wanting **persistent pure Markdown output**, use the Data URL embedding pattern:
#### Workflow
```
```text
┌──────────────────────────────────────────────────────────────┐
│ 1. Python Action │
│ ├── Analyze message content │
@@ -259,116 +313,28 @@ For scenarios requiring complex frontend rendering (e.g., AntV charts, Mermaid d
└──────────────────────────────────────────────────────────────┘
```
#### Python Side (Send JS for Execution)
### 5.5 Agent File Delivery Standards (3-Step Delivery Protocol)
```python
async def action(self, body, __event_call__, __metadata__, ...):
chat_id = self._extract_chat_id(body, __metadata__)
message_id = self._extract_message_id(body, __metadata__)
# Generate JS code
js_code = self._generate_js_code(
chat_id=chat_id,
message_id=message_id,
data=processed_data,
)
# Execute JS
if __event_call__:
await __event_call__({
"type": "execute",
"data": {"code": js_code}
})
```
#### JavaScript Side (Render and Write-back)
```javascript
(async function() {
// 1. Load visualization library
if (typeof VisualizationLib === 'undefined') {
await new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = 'https://cdn.example.com/lib.min.js';
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
// 2. Create offscreen container
const container = document.createElement('div');
container.style.cssText = 'position:absolute;left:-9999px;';
document.body.appendChild(container);
// 3. Render visualization
const instance = new VisualizationLib({ container });
instance.render(data);
// 4. Export to Data URL
const dataUrl = await instance.toDataURL({ type: 'svg', embedResources: true });
// 5. Cleanup
instance.destroy();
document.body.removeChild(container);
// 6. Generate Markdown image
const markdownImage = `![Chart](${dataUrl})`;
// 7. Update message via API
const token = localStorage.getItem("token");
await fetch(`/api/v1/chats/${chatId}/messages/${messageId}/event`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify({
type: "chat:message",
data: { content: originalContent + "\n\n" + markdownImage }
})
});
})();
```
#### Benefits
- **Pure Markdown Output**: Standard Markdown image syntax, no HTML code blocks
- **Self-Contained**: Images embedded as Base64 Data URL, no external dependencies
- **Persistent**: Via API write-back, images remain after page reload
- **Cross-Platform**: Works on any client supporting Markdown images
#### HTML Injection vs JS Render to Markdown
| Feature | HTML Injection | JS Render + Markdown |
|---------|----------------|----------------------|
| Output Format | HTML code block | Markdown image |
| Interactivity | ✅ Buttons, animations | ❌ Static image |
| External Deps | Requires JS libraries | None (self-contained) |
| Persistence | Depends on browser | ✅ Permanent |
| File Export | Needs special handling | ✅ Direct export |
| Use Case | Interactive content | Infographics, chart snapshots |
#### Reference Implementations
- `plugins/actions/infographic/infographic.py` - Production-ready implementation using AntV + Data URL
1. **Write Local**: Create files in the current execution directory (`.`).
2. **Publish**: Call `publish_file_from_workspace(filename='name.ext')`.
3. **Display Link**: Present the returned `download_url` as a Markdown link.
---
## 5. Best Practices & Design Principles
## 6. Best Practices & Design Principles
### 5.1 Naming & Positioning
### 6.1 Naming & Positioning
- **Short & Punchy**: e.g., "FlashCard", "DeepRead". Avoid generic terms like "Text Analysis Assistant"
- **Complementary**: Don't reinvent the wheel; clarify what specific problem your plugin solves
### 5.2 User Experience (UX)
### 6.2 User Experience (UX)
- **Timely Feedback**: Send a `notification` ("Generating...") before time-consuming operations
- **Visual Appeal**: When Action outputs HTML, use modern CSS (rounded corners, shadows, gradients)
- **Smart Guidance**: If text is too short, prompt the user: "Suggest entering more content for better results"
### 5.3 Error Handling
### 6.3 Error Handling
!!! danger "Never fail silently"
Always catch exceptions and inform the user via `__event_emitter__`.
@@ -384,9 +350,39 @@ except Exception as e:
})
```
### 6.4 Long-running Task Notifications
If a foreground task is expected to take more than 3 seconds, implement a user notification mechanism (e.g., sending a notification every 5 seconds).
---
## 6. Troubleshooting
## 7. Workflow & Process
### 7.1 Source-derived Knowledge (from `plugins/`)
- **Input/context safety**: normalize multimodal text extraction, use `_get_user_context` / `_get_chat_context`, and protect frontend language detection with timeout guards.
- **Long task UX**: emit immediate `status/notification`, then staged progress updates; keep full exception detail in backend logs.
- **HTML merge strategy**: use stable wrapper markers (`OPENWEBUI_PLUGIN_OUTPUT`) and support both overwrite and merge modes.
- **Theme consistency**: detect parent/system theme and apply theme-aware rendering/export styles for iframe-based outputs.
- **Render-export-persist loop**: offscreen render (SVG/PNG) -> upload `/api/v1/files/` -> event update + persistence update to avoid refresh loss.
- **DOCX production path**: `TITLE_SOURCE` fallback naming, reasoning-block stripping, native Word math (`latex2mathml + mathml2omml`), and citation/reference anchoring.
- **File retrieval fallback chain**: DB inline -> S3 direct -> local path variants -> public URL -> internal API -> raw fields, with max-byte guards on each stage.
- **Filter singleton discipline**: do not store request-scoped mutable state on `self`; compute from request context each run.
- **Async compression pattern**: `inlet` summary injection + `outlet` background summary generation, with model-threshold override and system-message protection.
- **Workspace/tool hardening**: explicit `params_type` schemas, strict path-boundary validation, and publish flow returning `/api/v1/files/{id}/content` with `skip_rag=true` metadata.
- **MoE refinement pipeline**: detect aggregation prompts, parse segmented responses, and rewrite to synthesis-oriented master prompt with optional reroute model.
### 7.2 Copilot Engineering Configuration
- For repository-wide AI-assisted engineering setup (GitHub Copilot + Gemini CLI + antigravity mode), follow `docs/development/copilot-engineering-plan.md`.
- This plan defines the shared contract for tool parameter schema/routing, file creation/publish protocol, rollback-safe delivery patterns, and streaming/tool-card compatibility.
- **Consistency Maintenance**: Any addition, modification, or removal of a plugin must simultaneously update the plugin code, READMEs, project docs, doc indexes, and the root README.
- **Release Workflow**: Pushing to `main` triggers automatic release. Ensure version numbers are updated and follow SemVer. Use Conventional Commits.
---
## 8. Troubleshooting
??? question "HTML not showing?"
Ensure it's wrapped in a ` ```html ... ``` ` code block.

View File

@@ -4,15 +4,16 @@
## 📚 目录
1. [插件开发快速入门](#1-quick-start)
2. [核心概念与 SDK 详解](#2-core-concepts-sdk-details)
3. [插件类型深度解析](#3-plugin-types)
* [Action (动作)](#31-action)
* [Filter (过滤器)](#32-filter)
* [Pipe (管道)](#33-pipe)
4. [高级开发模式](#4-advanced-patterns)
5. [最佳实践与设计原则](#5-best-practices)
6. [故障排查](#6-troubleshooting)
1. [插件开发快速入门](#1-quick-start)
2. [核心概念与 SDK 详解](#2-core-concepts-sdk-details)
3. [插件类型深度解析](#3-plugin-types)
* [Action (动作)](#31-action)
* [Filter (过滤器)](#32-filter)
* [Pipe (管道)](#33-pipe)
4. [高级开发模式](#4-advanced-patterns)
5. [最佳实践与设计原则](#5-best-practices)
6. [仓库规范openwebui-extensions](#6-repo-standards)
7. [故障排查](#7-troubleshooting)
---
@@ -21,9 +22,10 @@
### 1.1 什么是 OpenWebUI 插件?
OpenWebUI 插件(官方称为 "Functions")是扩展平台功能的主要方式。它们运行在后端 Python 环境中,允许你:
* 🔌 **集成新模型**:通过 Pipe 接入 Claude、Gemini 或自定义 RAG。
* 🎨 **增强交互**:通过 Action 在消息旁添加按钮(如"导出"、"生成图表"
* 🔧 **干预流程**:通过 Filter 在请求前后修改数据(如注入上下文、敏感词过滤)。
* 🔌 **集成新模型**:通过 Pipe 接入 Claude、Gemini 或自定义 RAG
* 🎨 **增强交互**:通过 Action 在消息旁添加按钮(如"导出"、"生成图表")。
* 🔧 **干预流程**:通过 Filter 在请求前后修改数据(如注入上下文、敏感词过滤)。
### 1.2 你的第一个插件 (Hello World)
@@ -69,9 +71,10 @@ class Action:
### 2.1 ⚠️ 重要:同步与异步
OpenWebUI 插件运行在 `asyncio` 事件循环中。
* **原则**:所有 I/O 操作(数据库、文件、网络)必须非阻塞。
* **陷阱**:直接调用同步方法(如 `time.sleep`, `requests.get`)会卡死整个服务器
* **解决**:使用 `await asyncio.to_thread(sync_func, ...)` 包装同步调用
* **原则**:所有 I/O 操作(数据库、文件、网络)必须非阻塞
* **陷阱**:直接调用同步方法(如 `time.sleep`, `requests.get`)会卡死整个服务器
* **解决**:使用 `await asyncio.to_thread(sync_func, ...)` 包装同步调用。
### 2.2 核心参数详解
@@ -88,8 +91,8 @@ OpenWebUI 插件运行在 `asyncio` 事件循环中。
### 2.3 配置系统 (Valves)
* **`Valves`**: 管理员全局配置。
* **`UserValves`**: 用户级配置(优先级更高,可覆盖全局)。
* **`Valves`**: 管理员全局配置。
* **`UserValves`**: 用户级配置(优先级更高,可覆盖全局)。
```python
class Filter:
@@ -113,7 +116,7 @@ class Filter:
**定位**:在消息下方添加按钮,用户点击触发。
**高级用法:前端执行 JavaScript (文件下载示例)**
#### 高级用法:前端执行 JavaScript (文件下载示例)
```python
import base64
@@ -138,11 +141,11 @@ async def action(self, body, __event_call__):
**定位**:中间件,拦截并修改请求/响应。
* **`inlet`**: 请求前。用于注入上下文、修改模型参数。
* **`outlet`**: 响应后。用于格式化输出、保存日志。
* **`stream`**: 流式处理中。用于实时敏感词过滤。
* **`inlet`**: 请求前。用于注入上下文、修改模型参数。
* **`outlet`**: 响应后。用于格式化输出、保存日志。
* **`stream`**: 流式处理中。用于实时敏感词过滤。
**示例:注入环境变量**
#### 示例:注入环境变量
```python
async def inlet(self, body, __metadata__):
@@ -159,7 +162,7 @@ async def inlet(self, body, __metadata__):
**定位**:自定义模型/代理。
**示例:简单的 OpenAI 代理**
#### 示例:简单的 OpenAI 代理
```python
import requests
@@ -180,11 +183,14 @@ class Pipe:
## 4. 高级开发模式 {: #4-advanced-patterns }
### 4.1 Pipe 与 Filter 协同
利用 `__request__.app.state` 在不同插件间共享数据。
* **Pipe**: `__request__.app.state.search_results = [...]`
* **Filter (Outlet)**: 读取 `search_results` 并将其格式化为引用链接附加到回复末尾。
* **Pipe**: `__request__.app.state.search_results = [...]`
* **Filter (Outlet)**: 读取 `search_results` 并将其格式化为引用链接附加到回复末尾。
### 4.2 异步后台任务
不阻塞用户响应,在后台执行耗时操作(如生成总结、存库)。
```python
@@ -205,7 +211,7 @@ async def background_job(self, chat_id):
#### 工作流程
```
```text
┌──────────────────────────────────────────────────────────────┐
│ 1. Python Action │
│ ├── 分析消息内容 │
@@ -297,10 +303,10 @@ async def action(self, body, __event_call__, __metadata__, ...):
#### 优势
- **纯 Markdown 输出**:结果是标准的 Markdown 图片语法,无需 HTML 代码块
- **自包含**:图片以 Base64 Data URL 嵌入,无外部依赖
- **持久化**:通过 API 回写,消息重新加载后图片仍然存在
- **跨平台**:任何支持 Markdown 图片的客户端都能显示
* **纯 Markdown 输出**:结果是标准的 Markdown 图片语法,无需 HTML 代码块
* **自包含**:图片以 Base64 Data URL 嵌入,无外部依赖
* **持久化**:通过 API 回写,消息重新加载后图片仍然存在
* **跨平台**:任何支持 Markdown 图片的客户端都能显示
#### HTML 注入 vs JS 渲染嵌入 Markdown
@@ -315,20 +321,23 @@ async def action(self, body, __event_call__, __metadata__, ...):
#### 参考实现
- `plugins/actions/infographic/infographic.py` - 基于 AntV + Data URL 的生产级实现
* `plugins/actions/infographic/infographic.py` - 基于 AntV + Data URL 的生产级实现
## 5. 最佳实践与设计原则 {: #5-best-practices }
### 5.1 命名与定位
* **简短有力**:如 "闪记卡", "精读"。避免 "文本分析助手" 这种泛词。
* **功能互补**:不要重复造轮子,明确你的插件解决了什么特定问题
* **简短有力**:如 "闪记卡", "精读"。避免 "文本分析助手" 这种泛词
* **功能互补**:不要重复造轮子,明确你的插件解决了什么特定问题。
### 5.2 用户体验 (UX)
* **反馈及时**:耗时操作前先发送 `notification` ("正在生成...")。
* **视觉美观**Action 输出 HTML 时,使用现代化的 CSS圆角、阴影、渐变
* **智能引导**:检测到文本过短时,提示用户"建议输入更多内容以获得更好结果"
* **反馈及时**:耗时操作前先发送 `notification` ("正在生成...")
* **视觉美观**Action 输出 HTML 时,使用现代化的 CSS圆角、阴影、渐变
* **智能引导**:检测到文本过短时,提示用户"建议输入更多内容以获得更好结果"。
### 5.3 错误处理
永远不要让插件静默失败。捕获异常并通过 `__event_emitter__` 告知用户。
```python
@@ -343,8 +352,76 @@ except Exception as e:
---
## 6. 故障排查 {: #6-troubleshooting }
## 6. 仓库规范openwebui-extensions {: #6-repo-standards }
* **HTML 不显示?** 确保包裹在 ` ```html ... ``` ` 代码块中。
* **数据库报错?** 检查是否在 `async` 函数中直接调用了同步的 DB 方法,请使用 `asyncio.to_thread`
* **参数未生效?** 检查 `Valves` 定义是否正确,以及是否被 `UserValves` 覆盖。
### 6.1 单文件 i18n 规范
本仓库要求每个插件采用**单文件源码 + 内置多语言**方案,禁止按语言拆分多个 `.py` 文件。
* 代码路径规范:`plugins/{type}/{name}/{name}.py`
* 文档规范:必须同时提供 `README.md``README_CN.md`
### 6.2 上下文访问规范(必选)
优先通过 `_get_user_context``_get_chat_context` 提取上下文,避免直接硬编码读取 `__user__``body` 字段。
### 6.3 事件与日志规范
* 用状态/通知事件给用户反馈进度。
* 前端调试优先使用 `execute` 注入的控制台日志。
* 后端统一使用 Python `logging`,生产代码避免 `print()`
### 6.4 前端语言探测防卡死
通过 `__event_call__` 获取前端语言时,必须同时满足:
* JS 端 `try...catch` 并保证返回值
* 后端 `asyncio.wait_for(..., timeout=2.0)`
这样可以避免前端异常导致后端永久等待。
### 6.5 Copilot SDK 工具参数定义
开发 Copilot SDK 工具时,应使用 `pydantic.BaseModel` 显式声明参数,并在 `define_tool(...)` 中通过 `params_type` 传入。
### 6.6 Copilot SDK 流式输出格式
* 思考过程使用原生 `<think>...</think>`
* 正文或工具卡片输出前,必须先闭合 `</think>`
* 工具卡片使用 `<details type="tool_calls" ...>` 原生结构。
* `arguments``result` 属性中的双引号必须转义为 `&quot;`
### 6.7 从 `plugins/` 全量提炼的关键开发知识
* **输入与上下文**:统一做多模态文本抽取;优先 `_get_user_context` / `_get_chat_context`;前端语言探测必须 `wait_for` 超时保护。
* **长任务反馈**:执行前立即发送 `status/notification`,过程分阶段汇报,失败时用户提示简洁、后台日志完整。
* **HTML/渲染输出**:使用固定包装器与插入点,支持覆盖与合并两种模式;主题跟随父页面与系统主题。
* **前端导出闭环**:离屏渲染 SVG/PNG -> 上传 `/api/v1/files/` -> 事件更新 + 持久化更新,避免刷新丢失。
* **DOCX 生产模式**`TITLE_SOURCE` 多级回退;导出前剔除 reasoningLaTeX 转 OMML支持引用锚点与参考文献。
* **文件访问回退链**DB 内联 -> S3 -> 本地路径变体 -> 公网 URL -> 内部 API -> 原始字段,并在每层限制字节上限。
* **Filter 单例安全**:禁止在 `self` 保存请求态;上下文压缩采用 inlet/outlet 双阶段 + 异步后台任务。
* **工具与工作区安全**:工具参数用 `params_type` 显式建模;路径解析后必须二次校验在 workspace 根目录内。
* **文件交付标准**:本地生成 -> 发布工具 -> 返回 `/api/v1/files/{id}/content`,并带 `skip_rag=true` 元数据。
* **MoE 聚合优化**:识别聚合提示词后重写为结构化综合分析任务,并可在聚合阶段切换模型。
### 6.8 Copilot 工程化配置GitHub Copilot + Gemini CLI + 反重力开发)
统一工程化配置请参考:
* `docs/development/copilot-engineering-plan.md`
* `docs/development/copilot-engineering-plan.zh.md`
该设计文档规定了:
* 工具参数建模与路由规则
* 文件创建与发布协议
* 可回滚的高迭代交付模式
* 流式输出与工具卡片兼容要求
---
## 7. 故障排查 {: #7-troubleshooting }
* **HTML 不显示?** 确保包裹在 ` ```html ... ``` ` 代码块中。
* **数据库报错?** 检查是否在 `async` 函数中直接调用了同步的 DB 方法,请使用 `asyncio.to_thread`
* **参数未生效?** 检查 `Valves` 定义是否正确,以及是否被 `UserValves` 覆盖。