From 0c7201902c20c3dc197d708d98f7b67e15229f38 Mon Sep 17 00:00:00 2001 From: Fu-Jie <33599649+Fu-Jie@users.noreply.github.com> Date: Sat, 28 Feb 2026 03:50:56 +0800 Subject: [PATCH] feat(github-copilot-sdk): add workspace skills support (v0.9.0) (#51) * feat(github-copilot-sdk): add workspace skills support - Introduce ENABLE_WORKSPACE_SKILLS valve to enable/disable workspace custom tools discovery - Modify _build_session_config() to auto-load tools from .copilot-skills/ directory - Add workspace_skills_example.py template with 3 working example tools - Update README.md and README_CN.md with Workspace Skills guide and usage examples - Create v0.9.0.md and v0.9.0_CN.md release notes - Sync version to all docs files (index.md, index.zh.md, and main docs) - Bump version from 0.8.0 to 0.9.0 across all 7+ locations * docs: establish temp files handling policy (project-based, not /tmp) - Add TEMP_FILES_POLICY.md guideline for all skills and workflows - Update pr-submitter skill to use .temp/ directory instead of /tmp - Update release-prep skill documentation with temp file convention - Add .temp/ and .build/ entries to .gitignore - Create internal policy memo in /memories/repo/ This policy ensures: - All temporary files stay within project workspace (not system /tmp) - Alignment with OpenWebUI workspace isolation principles - Multi-user safety and cleanup traceability - Consistent handling across all skills and development workflows * fix(terminology): rename 'workspace skills' to 'workspace custom tools' for accuracy The term 'Skills' in Anthropic context refers to instruction-based frameworks (SKILL.md files with YAML frontmatter + markdown), not custom tool functions. Our implementation uses @define_tool decorator to define custom tools that the SDK auto-discovers from .copilot-skills/ directory. These are Tools, not Skills. Changes: - Rename ENABLE_WORKSPACE_SKILLS valve -> ENABLE_WORKSPACE_TOOLS - Update all documentation (README, README_CN, docs, release notes) - Fix section headings and descriptions throughout - Ensure consistent terminology across all files This is a terminology-only change; functionality remains identical. * feat(pipes): release v0.9.0 of GitHub Copilot SDK Pipe - Integrated OpenWebUI Skills Bridge and manage_skills tool - Reinforced status bar stability with session_finalized logic - Added persistent SDK config directory support * docs(pipes): add comprehensive guides and v0.9.0 notes for Copilot SDK - Added skill manager and best practices guides - Added publishing tool documentation - Included v0.9.0 release notes and deployment script - Updated usage guides --- .agent/skills/test-copilot-pipe/SKILL.md | 64 + .github/TEMP_FILES_POLICY.md | 121 + .github/skills/pr-submitter/SKILL.md | 31 +- .github/skills/release-prep/SKILL.md | 7 + .gitignore | 2 + .../github-copilot-sdk-publish-file-tool.md | 29 + ...github-copilot-sdk-publish-file-tool.zh.md | 29 + .../pipes/github-copilot-sdk-usage-guide.md | 185 ++ .../github-copilot-sdk-usage-guide.zh.md | 192 ++ docs/plugins/pipes/github-copilot-sdk.md | 107 +- docs/plugins/pipes/github-copilot-sdk.zh.md | 125 +- docs/plugins/pipes/index.md | 2 +- docs/plugins/pipes/index.zh.md | 2 +- .../PUBLISH_FILE_FROM_WORKSPACE.md | 76 + .../PUBLISH_FILE_FROM_WORKSPACE_CN.md | 76 + plugins/pipes/github-copilot-sdk/README.md | 109 +- plugins/pipes/github-copilot-sdk/README_CN.md | 110 +- .../SKILLS_BEST_PRACTICES.md | 209 ++ .../SKILLS_BEST_PRACTICES_CN.md | 209 ++ .../github-copilot-sdk/SKILLS_MANAGER.md | 128 + .../github-copilot-sdk/SKILLS_MANAGER_CN.md | 128 + .../github-copilot-sdk/github_copilot_sdk.py | 2492 ++++++++++++++--- plugins/pipes/github-copilot-sdk/v0.9.0.md | 101 + plugins/pipes/github-copilot-sdk/v0.9.0_CN.md | 101 + .../workspace_skills_example.py | 260 ++ scripts/update_pipe.py | 110 + 26 files changed, 4564 insertions(+), 441 deletions(-) create mode 100644 .agent/skills/test-copilot-pipe/SKILL.md create mode 100644 .github/TEMP_FILES_POLICY.md create mode 100644 docs/plugins/pipes/github-copilot-sdk-publish-file-tool.md create mode 100644 docs/plugins/pipes/github-copilot-sdk-publish-file-tool.zh.md create mode 100644 docs/plugins/pipes/github-copilot-sdk-usage-guide.md create mode 100644 docs/plugins/pipes/github-copilot-sdk-usage-guide.zh.md create mode 100644 plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE.md create mode 100644 plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE_CN.md create mode 100644 plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES.md create mode 100644 plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES_CN.md create mode 100644 plugins/pipes/github-copilot-sdk/SKILLS_MANAGER.md create mode 100644 plugins/pipes/github-copilot-sdk/SKILLS_MANAGER_CN.md create mode 100644 plugins/pipes/github-copilot-sdk/v0.9.0.md create mode 100644 plugins/pipes/github-copilot-sdk/v0.9.0_CN.md create mode 100644 plugins/pipes/github-copilot-sdk/workspace_skills_example.py create mode 100644 scripts/update_pipe.py diff --git a/.agent/skills/test-copilot-pipe/SKILL.md b/.agent/skills/test-copilot-pipe/SKILL.md new file mode 100644 index 0000000..b069887 --- /dev/null +++ b/.agent/skills/test-copilot-pipe/SKILL.md @@ -0,0 +1,64 @@ +--- +name: test-copilot-pipe +description: Automotive deployment and testing of GitHub Copilot SDK Pipe plugin for frontend/backend status stability. +--- + +# 🤖 Skill: Test Copilot Pipe + +This is a **universal testing framework** for publishing the latest `github_copilot_sdk.py` (Pipe) code to a local OpenWebUI instance and verifying it via an automated agent (`browser_subagent`). + +## 🎯 Core Principles + +- **Fixed Infrastructure**: The deployment script and the test entry URL are always static. +- **Dynamic Test Planning**: Specific test prompts and expectations (acceptance criteria) **must** be dynamically planned by you based on the code changes or specific user requests before execution. + +--- + +## 🛠️ Static Environment Info + +| Attribute | Fixed Value | +|------|--------| +| **Deployment Script** | `/Users/fujie/app/python/oui/openwebui-extensions/scripts/update_pipe.py` | +| **Python Path** | `/opt/homebrew/Caskroom/miniconda/base/envs/ai/bin/python3` | +| **Test URL** | `http://localhost:3003/?model=github_copilot_official_sdk_pipe.github_copilot_sdk-gpt-4.1` | + +--- + +## 📋 Standard Workflow + +### Step 1: Analyze Changes & Plan Test (Plan) + +Before triggering the test, you must define the purpose of this test turn. +Example: *Modified tool calling logic -> Test prompt should trigger a specific tool; observe if the tool executes and returns the correct result.* + +### Step 2: Deploy Latest Code (Deploy) + +Use the `run_command` tool to execute the fixed update task: + +```bash +/opt/homebrew/Caskroom/miniconda/base/envs/ai/bin/python3 /Users/fujie/app/python/oui/openwebui-extensions/scripts/update_pipe.py +``` + +> **Mechanism**: `update_pipe.py` automatically loads the API Key from `scripts/.env` in the same directory. +> **Verification**: Look for `✅ Successfully updated... version X.X.X`. If a 401 error occurs, remind the user to generate a new API Key in OpenWebUI and update `.env`. + +### Step 3: Verify via Browser Subagent (Verify) + +Use the `browser_subagent` tool. **You must fill in the `[Dynamic Content]` slots based on Step 1**: + +```text +Task: +1. Access The Fixed URL: http://localhost:3003/?model=github_copilot_official_sdk_pipe.github_copilot_sdk-gpt-4.1 +2. RELIABILITY WAIT: Wait until the page fully loads. Wait until the chat input text area (`#chat-input`) is present in the DOM. +3. ACTION - FAST INPUT: Use the `execute_browser_javascript` tool to instantly inject the query and submit it. Use exactly this script format to ensure stability: + `const input = document.getElementById('chat-input'); input.value = "[YOUR_DYNAMIC_TEST_PROMPT]"; input.dispatchEvent(new Event('input', { bubbles: true })); const e = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', keyCode: 13, which: 13, bubbles: true }); input.dispatchEvent(e);` +4. WAITING: Wait patiently for the streaming response to stop completely. You should wait for the Stop button to disappear, or wait for the system to settle (approximately 10-15 seconds depending on the query). +5. CHECK THE OUTCOME: [List the phenomena you expect to see, e.g., status bar shows specific text, tool card appears, result contains specific keywords, etc.] +6. CAPTURE: Take a screenshot of the settled state to prove the outcome. +7. REPORT: Report the EXACT outcome matching the criteria from step 5. +``` + +### Step 4: Evaluate & Iterate (Evaluate) + +- **PASS**: Screenshot and phenomena match expectations. Report success to the user. +- **FAIL**: Analyze the issue based on screenshots/logs (e.g., race condition reappeared, API error). Modify the code and **re-run the entire skill workflow**. diff --git a/.github/TEMP_FILES_POLICY.md b/.github/TEMP_FILES_POLICY.md new file mode 100644 index 0000000..ccea909 --- /dev/null +++ b/.github/TEMP_FILES_POLICY.md @@ -0,0 +1,121 @@ +# Temporary Files Handling Policy + +**Last Updated**: 2026-02-26 +**Status**: Active Guideline + +## Overview + +All temporary files created during skill execution or development workflows must follow this centralized policy to maintain project cleanliness and workspace isolation alignment. + +## Core Rule + +**Temporary files MUST be stored in the project's `.temp/` directory, NOT in system directories like `/tmp`.** + +## Rationale + +1. **Workspace Isolation**: Aligns with OpenWebUI's workspace-per-user model +2. **Project Cohesion**: All project artifacts (temporary or permanent) stay within project boundaries +3. **Multi-User Safety**: Avoids conflicts between multiple developers using the same system +4. **Cleanup Traceability**: Easy to verify all temp files are cleaned up via single `.temp/` directory +5. **Debugging**: Inspectable before deletion if issues occur + +## Usage Pattern + +### Creating Temp File + +```bash +# Step 1: Ensure temp directory exists +mkdir -p .temp + +# Step 2: Write temp file +cat > .temp/my_temp_file.md << 'EOF' +...content... +EOF + +# Step 3: Use the file in your workflow +# (e.g., pass to gh CLI, process with script, etc.) +``` + +### Cleanup After Use + +```bash +# Remove individual temp files +rm -f .temp/my_temp_file.md + +# Or full cleanup of entire temp directory +rm -rf .temp/ +``` + +## Skills Affected + +| Skill | Implementation | Status | +|-------|----------------|--------| +| `pr-submitter` | PR body file (`.temp/pr_body.md`) | ✅ Updated | +| `release-prep` | Draft notes (if any) | ✅ Policy Added | +| `version-bumper` | Backup files (if any) | ℹ️ Check needed | +| Future skills | TBD | 📋 Must follow policy | + +## .gitignore Configuration + +The following entry in `.gitignore` ensures temp files are never committed: + +``` +# Temporary files +.temp/ +.build/ +``` + +## Examples + +### Example 1: PR Submitter Skill +```bash +# Create PR body in temp directory +mkdir -p .temp +cat > .temp/pr_body.md << 'EOF' +## Summary +New feature implementation +EOF + +# Use with gh CLI +gh pr create --body-file .temp/pr_body.md --title "feat: new feature" + +# Cleanup +rm -f .temp/pr_body.md +``` + +### Example 2: Release Prepare Workflow +```bash +# Create draft changelog +mkdir -p .temp +cat > .temp/changelog_draft.md << 'EOF' +# v1.0.0 Release Notes +EOF + +# Edit, validate, then integrate into real files +# ... + +# Cleanup +rm -f .temp/changelog_draft.md +``` + +## Anti-Patterns (❌ Don't Do This) + +- ❌ Writing temp files to `/tmp` — will be lost/orphaned +- ❌ Writing to root directory or `plugins/` — pollutes repo +- ❌ Not cleaning up temp files — accumulates clutter +- ❌ Committing `.temp/` files to git — defeats the purpose +- ❌ Using absolute paths — breaks workflow portability + +## Enforcement + +1. **Code Review**: PRs should verify no `/tmp` references in scripts +2. **CI/CD**: Setup can validate `.temp/` cleanup via git status before commit +3. **Documentation**: All skill docs must reference this policy (link to this file) +4. **Automated**: Consider adding pre-commit hook to ensure `.temp/` is not staged + +## Questions / Clarifications + +For questions about this policy, refer to: +- `.github/skills/pr-submitter/SKILL.md` — Practical example +- `.github/skills/release-prep/SKILL.md` — Policy integration +- `/memories/repo/temp-file-handling-convention.md` — Internal notes diff --git a/.github/skills/pr-submitter/SKILL.md b/.github/skills/pr-submitter/SKILL.md index c1f3d92..7dafc3b 100644 --- a/.github/skills/pr-submitter/SKILL.md +++ b/.github/skills/pr-submitter/SKILL.md @@ -19,6 +19,17 @@ This skill handles the final step of pushing a feature branch and creating a val ## Workflow +### Step 0 — Initialize Temp Directory (Project-Based) + +For all temporary files, use the project's `.temp/` directory instead of system `/tmp`: + +```bash +# Create temp directory if it doesn't exist +mkdir -p .temp +``` + +**Why**: All temporary files stay within the project workspace, avoiding system `/tmp` pollution and better aligning with OpenWebUI workspace isolation principles. + ### Step 1 — Pre-Flight Checks Run these checks before any push: @@ -46,10 +57,10 @@ Gather: ### Step 3 — Build PR Body File (Shell-Escape-Safe) -**Always write the body to a temp file.** Never embed multi-line markdown with special characters directly in a shell command. +**Always write the body to a temp file in `.temp/` directory.** Never embed multi-line markdown with special characters directly in a shell command. ```bash -cat > /tmp/pr_body.md << 'HEREDOC' +cat > .temp/pr_body.md << 'HEREDOC' ## Summary Brief one-sentence description of what this PR accomplishes. @@ -101,12 +112,12 @@ Before submitting, verify the body file contains expected sections: ```bash # Check key sections exist -grep -q "## Summary" /tmp/pr_body.md && echo "✅ Summary" || echo "❌ Summary missing" -grep -q "## Changes" /tmp/pr_body.md && echo "✅ Changes" || echo "❌ Changes missing" -grep -q "## 变更摘要" /tmp/pr_body.md && echo "✅ CN Section" || echo "❌ CN Section missing" +grep -q "## Summary" .temp/pr_body.md && echo "✅ Summary" || echo "❌ Summary missing" +grep -q "## Changes" .temp/pr_body.md && echo "✅ Changes" || echo "❌ Changes missing" +grep -q "## 变更摘要" .temp/pr_body.md && echo "✅ CN Section" || echo "❌ CN Section missing" # Preview the body -cat /tmp/pr_body.md +cat .temp/pr_body.md ``` Ask the user to confirm the body content before proceeding. @@ -126,7 +137,7 @@ gh pr create \ --base main \ --head $(git branch --show-current) \ --title "" \ - --body-file /tmp/pr_body.md + --body-file .temp/pr_body.md ``` Always use `--body-file`, never `--body` with inline markdown. @@ -151,9 +162,11 @@ gh pr edit --body-file /tmp/pr_body.md ### Step 8 — Cleanup ```bash -rm -f /tmp/pr_body.md +rm -f .temp/pr_body.md ``` +**Note**: The `.temp/` directory itself is preserved for reuse; only the individual PR body file is deleted. To fully clean up: `rm -rf .temp/` + Report final PR URL to the user. --- @@ -167,6 +180,8 @@ Report final PR URL to the user. | Newlines in `--body` | File-based only | | `$variable` expansion | Use `<< 'HEREDOC'` (quoted) | | Double quotes in body | Safe in heredoc file | +| Temp file storage | Use `.temp/` dir, not `/tmp` | +| Cleanup after use | Always delete temp file (keep dir) | --- diff --git a/.github/skills/release-prep/SKILL.md b/.github/skills/release-prep/SKILL.md index a6e06f4..f0364c0 100644 --- a/.github/skills/release-prep/SKILL.md +++ b/.github/skills/release-prep/SKILL.md @@ -20,6 +20,13 @@ This skill covers: It **stops before** `git push` or `gh pr create`. Use the `pr-submitter` skill for those steps. +### Temporary File Convention + +Any temporary files created during release prep (e.g., draft changelogs) must: +- Be written to the project's `.temp/` directory, **NOT** system `/tmp` +- Be cleaned up before commit using `rm -f .temp/file_name` +- Never be committed to git (add `.temp/` to `.gitignore`) + --- ## Workflow diff --git a/.gitignore b/.gitignore index d298475..d77653b 100644 --- a/.gitignore +++ b/.gitignore @@ -136,6 +136,8 @@ logs/ # Temporary files *.tmp *.temp +.temp/ +.build/ # OpenWebUI specific # Add any specific ignores for OpenWebUI plugins if needed diff --git a/docs/plugins/pipes/github-copilot-sdk-publish-file-tool.md b/docs/plugins/pipes/github-copilot-sdk-publish-file-tool.md new file mode 100644 index 0000000..20cc251 --- /dev/null +++ b/docs/plugins/pipes/github-copilot-sdk-publish-file-tool.md @@ -0,0 +1,29 @@ +# GitHub Copilot SDK Pipe - `publish_file_from_workspace` Tool Guide + +## Summary + +`publish_file_from_workspace` is the file delivery tool used by the GitHub Copilot SDK Pipe to publish workspace-generated files into OpenWebUI storage and return stable preview/download links. + +## Input + +- `filename` (required): Relative file path under current workspace. + +## Delivery Modes + +- `artifacts` (default): Return `[Preview]` + `[Download]`, with optional `html_embed` rendering in HTML block. +- `richui`: Return `[Preview]` + `[Download]`; integrated preview is rendered by Rich UI emitter. + +## PDF Rule + +For PDF outputs, always return markdown links only (`[Preview]`, `[Download]` when available). Do not embed PDF using iframe or HTML. + +## Recommended Steps + +1. Generate file in workspace. +2. Publish via `publish_file_from_workspace(filename=...)`. +3. Return links according to embed mode. +4. Apply PDF link-only rule for `.pdf` files. + +## Reference + +- Plugin local guide: `plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE.md` diff --git a/docs/plugins/pipes/github-copilot-sdk-publish-file-tool.zh.md b/docs/plugins/pipes/github-copilot-sdk-publish-file-tool.zh.md new file mode 100644 index 0000000..bbfa80e --- /dev/null +++ b/docs/plugins/pipes/github-copilot-sdk-publish-file-tool.zh.md @@ -0,0 +1,29 @@ +# GitHub Copilot SDK Pipe - `publish_file_from_workspace` 工具指南 + +## 简介 + +`publish_file_from_workspace` 是 GitHub Copilot SDK Pipe 的文件交付工具,用于将工作区生成文件发布到 OpenWebUI 存储,并返回稳定的预览/下载链接。 + +## 输入参数 + +- `filename`(必填):当前工作区下的相对路径文件名。 + +## 交付模式 + +- `artifacts`(默认):返回 `[Preview]` + `[Download]`,可选在 HTML 代码块中渲染 `html_embed`。 +- `richui`:返回 `[Preview]` + `[Download]`,集成预览由 Rich UI 发射器自动渲染。 + +## PDF 规则 + +PDF 必须只返回 Markdown 链接(`[Preview]`、`[Download]` 可用时),禁止 iframe 或 HTML 嵌入。 + +## 推荐流程 + +1. 在工作区生成文件。 +2. 调用 `publish_file_from_workspace(filename=...)` 发布。 +3. 按当前模式返回链接。 +4. `.pdf` 严格执行仅链接规则。 + +## 参考 + +- 插件内完整指南:`plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE_CN.md` diff --git a/docs/plugins/pipes/github-copilot-sdk-usage-guide.md b/docs/plugins/pipes/github-copilot-sdk-usage-guide.md new file mode 100644 index 0000000..cfda39c --- /dev/null +++ b/docs/plugins/pipes/github-copilot-sdk-usage-guide.md @@ -0,0 +1,185 @@ +# GitHub Copilot SDK Pipe Detailed Usage Guide + +**Author:** [Fu-Jie](https://github.com/Fu-Jie/openwebui-extensions) | **Version:** 0.9.0 | **Project:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) + +This guide is for production usage. README remains community-facing and concise; this page focuses on step-by-step operations and troubleshooting. + +--- + +## 1. When to Use This Guide + +Use this manual if you need to: + +- Run GitHub Copilot official models in OpenWebUI +- Use BYOK providers (OpenAI/Anthropic) +- Generate and publish artifacts (Excel/CSV/HTML/PDF) +- Manage Skills with OpenWebUI bridge and `manage_skills` + +--- + +## 2. Pre-flight Checklist + +### 2.1 Required components + +- OpenWebUI is running (recommended `v0.8.0+` for Rich UI) +- Pipe is installed: `plugins/pipes/github-copilot-sdk/github_copilot_sdk.py` +- Recommended companion filter: + - [GitHub Copilot SDK Files Filter](https://openwebui.com/posts/403a62ee-a596-45e7-be65-fab9cc249dd6) + +### 2.2 Authentication (at least one) + +You must configure one of the following: + +1. `GH_TOKEN` for official GitHub Copilot models +2. `BYOK_API_KEY` for OpenAI/Anthropic style providers + +--- + +## 3. Installation Flow + +1. Open OpenWebUI → **Workspace → Functions** +2. Create function and paste `github_copilot_sdk.py` +3. Save and enable +4. In chat model selector, choose: + - `github_copilot_official_sdk_pipe.*` (official) + - or your BYOK model entries + +--- + +## 4. Configuration Baseline + +### 4.1 Minimal admin setup + +- `GH_TOKEN` or `BYOK_API_KEY` +- `ENABLE_OPENWEBUI_TOOLS = True` +- `ENABLE_MCP_SERVER = True` +- `ENABLE_OPENWEBUI_SKILLS = True` +- `SHOW_THINKING = True` + +### 4.2 Recommended production settings + +- `COPILOTSDK_CONFIG_DIR=/app/backend/data/.copilot` + - Persists SDK config/session state across restarts +- `OPENWEBUI_SKILLS_SHARED_DIR=/app/backend/data/cache/copilot-openwebui-skills` + - Shared skill cache directory +- `DEBUG=True` during troubleshooting only + +### 4.3 User-level overrides + +Users can override `GH_TOKEN`, `REASONING_EFFORT`, `BYOK_API_KEY`, `DISABLED_SKILLS`, etc. + +--- + +## 5. Model Access Modes + +## 5.1 Official Copilot mode + +- Set `GH_TOKEN` +- Use official model catalog +- Supports reasoning effort, tools, infinite session features + +## 5.2 BYOK mode + +- Set `BYOK_TYPE`, `BYOK_BASE_URL`, `BYOK_API_KEY` +- Leave `BYOK_MODELS` empty for auto-fetch, or set explicit list +- Best when no Copilot subscription is available + +--- + +## 6. Artifact Publishing Workflow (Critical) + +Use `publish_file_from_workspace` with the sequence: write → publish → return links. + +### 6.1 HTML delivery modes + +- `artifacts` (default) + - Returns `[Preview]` + `[Download]` + - `html_embed` can be rendered in a full-height iframe block +- `richui` + - Returns `[Preview]` + `[Download]` + - Rich UI renders automatically (no iframe block in message) + +### 6.2 PDF delivery rule + +- Output Markdown links only (`[Preview]` + `[Download]` when available) +- **Do not** embed PDF via iframe/html blocks + +### 6.3 Images and other files + +- Images: prefer direct display + download +- Other artifacts (`xlsx/csv/docx`): provide download links + +--- + +## 7. Skills Operations + +> Key rule: `manage_skills` is a **tool**, not a skill; all skills are installed in **one directory**: `OPENWEBUI_SKILLS_SHARED_DIR/shared/`. + +## 7.1 OpenWebUI Skills bridge + +With `ENABLE_OPENWEBUI_SKILLS=True`: + +- Skills from UI sync into SDK directories +- Directory-side updates sync back according to sync policy + +## 7.2 `manage_skills` quick actions + +- `list`, `install`, `create`, `edit`, `show`, `delete` + +## 7.3 Operational tips + +- Use `DISABLED_SKILLS` to reduce noisy skill routing +- Keep skill descriptions explicit for better intent matching + +--- + +## 8. First-run Validation Checklist + +1. Basic chat response works +2. Tool call can be triggered +3. CSV/XLSX can be published and downloaded +4. HTML mode works (`artifacts` or `richui`) +5. PDF returns links only (no embed) +6. `manage_skills list` returns skill inventory + +--- + +## 9. Troubleshooting + +### 9.1 Empty model list + +- Ensure at least one of `GH_TOKEN` / `BYOK_API_KEY` is set +- Validate BYOK base URL and model names + +### 9.2 Tools not executing + +- Check `ENABLE_OPENWEBUI_TOOLS`, `ENABLE_MCP_SERVER`, `ENABLE_OPENAPI_SERVER` +- Confirm session/model has tool access + +### 9.3 Publish succeeded but link unavailable + +- Use the original URLs returned by tool output +- Verify storage/backend access policy +- For PDF, do not attempt iframe embedding + +### 9.4 Status appears stuck + +- Upgrade to latest plugin code +- Enable `DEBUG=True` temporarily +- Verify frontend version compatibility for Rich UI + +--- + +## 10. Practical Prompt Templates + +- “Analyze `sales.csv`, summarize by month, export `monthly_summary.xlsx`, and give me the download link.” +- “Generate an interactive HTML dashboard and publish it with Preview and Download links.” +- “Create a reusable skill named `finance-reporting` from this workflow.” + +--- + +For deeper architecture and examples: + +- [Deep Dive](github-copilot-sdk-deep-dive.md) +- [Advanced Tutorial](github-copilot-sdk-tutorial.md) +- [Main Plugin Doc](github-copilot-sdk.md) diff --git a/docs/plugins/pipes/github-copilot-sdk-usage-guide.zh.md b/docs/plugins/pipes/github-copilot-sdk-usage-guide.zh.md new file mode 100644 index 0000000..48691b1 --- /dev/null +++ b/docs/plugins/pipes/github-copilot-sdk-usage-guide.zh.md @@ -0,0 +1,192 @@ +# GitHub Copilot SDK Pipe 详细使用手册 + +**Author:** [Fu-Jie](https://github.com/Fu-Jie/openwebui-extensions) | **Version:** 0.9.0 | **Project:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) + +本手册面向“实际落地使用”,覆盖从安装、鉴权、模型选择到文件发布、Skills 管理、故障排查的完整流程。README 侧重社区展示,本页专注操作细节。 + +--- + +## 1. 适用场景 + +适合以下需求: + +- 在 OpenWebUI 内使用 GitHub Copilot 官方模型(含流式、工具调用、多轮会话) +- 使用 BYOK(OpenAI/Anthropic)替代官方订阅 +- 让 Agent 生成并发布文件(Excel/CSV/HTML/PDF) +- 使用 OpenWebUI Skills 与 `manage_skills` 做技能工程化管理 + +--- + +## 2. 部署前检查 + +### 2.1 OpenWebUI 与插件文件 + +- 已运行 OpenWebUI(建议 `v0.8.0+` 以获得 Rich UI 体验) +- 已导入 Pipe 文件:`plugins/pipes/github-copilot-sdk/github_copilot_sdk.py` +- 建议同时安装 Files Filter: + - [GitHub Copilot SDK Files Filter](https://openwebui.com/posts/403a62ee-a596-45e7-be65-fab9cc249dd6) + +### 2.2 必要鉴权(至少一种) + +你必须配置下列其中一种凭据,否则模型列表为空: + +1. `GH_TOKEN`(访问 GitHub Copilot 官方模型) +2. `BYOK_API_KEY`(访问 OpenAI/Anthropic 等自有供应商) + +--- + +## 3. 安装与启用 + +1. 进入 OpenWebUI:**工作区 → 函数** +2. 新建函数并粘贴 `github_copilot_sdk.py` 全量内容 +3. 保存并启用 +4. 回到聊天页,在模型列表选择: + - `github_copilot_official_sdk_pipe.*`(官方) + - 或 BYOK 对应模型 + +--- + +## 4. 配置建议(先跑起来再精调) + +### 4.1 管理员最小可用配置 + +- `GH_TOKEN` 或 `BYOK_API_KEY` +- `ENABLE_OPENWEBUI_TOOLS = True` +- `ENABLE_MCP_SERVER = True` +- `ENABLE_OPENWEBUI_SKILLS = True` +- `SHOW_THINKING = True` + +### 4.2 推荐增强项 + +- `COPILOTSDK_CONFIG_DIR=/app/backend/data/.copilot` + - 用于 SDK 配置/会话状态持久化(重启不丢) +- `OPENWEBUI_SKILLS_SHARED_DIR=/app/backend/data/cache/copilot-openwebui-skills` + - 统一 skills 缓存目录 +- `DEBUG=True`(排障阶段) + +### 4.3 用户级覆盖(Profile) + +普通用户可按需覆盖:`GH_TOKEN`、`REASONING_EFFORT`、`BYOK_API_KEY`、`DISABLED_SKILLS` 等。 + +--- + +## 5. 两种模型接入模式 + +## 5.1 官方模式(GitHub Copilot) + +- 配置 `GH_TOKEN` +- 模型来自 Copilot 官方可用列表 +- 支持推理强度、工具调用、无限会话等插件能力 + +## 5.2 BYOK 模式(OpenAI/Anthropic) + +- 配置 `BYOK_TYPE`、`BYOK_BASE_URL`、`BYOK_API_KEY` +- `BYOK_MODELS` 留空可自动拉取,或手动逗号分隔指定 +- 适合无官方订阅、或需要指定厂商模型时使用 + +--- + +## 6. 文件发布完整工作流(重点) + +插件内置 `publish_file_from_workspace`,推荐遵循“写入 → 发布 → 返回链接”。 + +### 6.1 HTML 交付模式 + +- `artifacts`(默认) + - 返回 `[Preview]` + `[Download]` + - 可输出 `html_embed`(iframe)用于完整交互展示 +- `richui` + - 返回 `[Preview]` + `[Download]` + - 由 Rich UI 自动渲染,不在消息中输出 iframe 代码块 + +### 6.2 PDF 交付规则(务必遵守) + +- 仅输出 Markdown 链接(可用时 `[Preview]` + `[Download]`) +- **不要**输出 PDF iframe/embed HTML + +### 6.3 图片与其他文件 + +- 图片:优先直接展示 + 下载 +- 其他文件(xlsx/csv/docx 等):返回下载链接为主 + +--- + +## 7. Skills 使用与管理 + +> 关键原则:`manage_skills` 是 **工具(tool)**,不是 skill;所有 skills 统一安装在 **一个目录**:`OPENWEBUI_SKILLS_SHARED_DIR/shared/`。 + +## 7.1 OpenWebUI Skills 双向桥接 + +当 `ENABLE_OPENWEBUI_SKILLS=True` 时: + +- UI 中创建/编辑的 Skills 会同步到 SDK 目录 +- 目录内技能更新可回写到 OpenWebUI(按同步规则) + +## 7.2 `manage_skills` 常用动作 + +- `list`:列出现有技能 +- `install`:从 GitHub URL / `.zip` / `.tar.gz` 安装 +- `create`:从当前上下文创建技能 +- `edit`:更新技能内容与附加文件 +- `show`:查看 `SKILL.md` 与附属文件 +- `delete`:删除本地目录并清理关联记录 + +### 7.3 生产建议 + +- 用 `DISABLED_SKILLS` 关闭不需要的技能,降低误触发 +- Skill 描述尽量明确(包含 Use when 语义),提高路由准确率 + +--- + +## 8. 首次验收清单 + +完成部署后,建议按顺序验证: + +1. **基础对话**:确认模型能正常响应 +2. **工具调用**:执行一条会触发工具的指令(如文件分析) +3. **文件发布**:生成一个 `csv` 并确认可下载 +4. **HTML 发布**:验证 `artifacts/richui` 至少一种模式 +5. **PDF 发布**:确认仅返回链接,无 iframe +6. **Skills**:执行 `manage_skills list`,确认可见 + +--- + +## 9. 常见问题排查 + +### 9.1 模型列表为空 + +- 检查 `GH_TOKEN` / `BYOK_API_KEY` 是否至少配置一个 +- 检查 BYOK `BASE_URL` 是否可达、模型名是否有效 + +### 9.2 工具似乎不可用 + +- 检查 `ENABLE_OPENWEBUI_TOOLS`、`ENABLE_MCP_SERVER`、`ENABLE_OPENAPI_SERVER` +- 检查当前模型/会话是否有工具权限 + +### 9.3 文件发布成功但无法打开 + +- 检查返回链接是否来自工具原始输出 +- 检查对象存储/本地存储权限与可访问性 +- PDF 场景不要尝试 iframe 嵌入 + +### 9.4 状态栏“卡住” + +- 升级到最新插件代码 +- 打开 `DEBUG=True` 查看事件流 +- 确认前端版本与 Rich UI 能力匹配 + +--- + +## 10. 推荐操作模板(可直接对 AI 说) + +- “读取当前目录下的 `sales.csv`,按月份汇总并导出 `monthly_summary.xlsx`,最后给我下载链接。” +- “生成一个交互式 HTML 仪表盘并发布,给我 Preview 和 Download 链接。” +- “把本次流程固化成一个 skill,命名为 `finance-reporting`,并写入使用说明。” + +--- + +如需架构细节,请结合阅读: + +- [深度解析](github-copilot-sdk-deep-dive.zh.md) +- [进阶实战教程](github-copilot-sdk-tutorial.zh.md) +- [插件主文档](github-copilot-sdk.zh.md) diff --git a/docs/plugins/pipes/github-copilot-sdk.md b/docs/plugins/pipes/github-copilot-sdk.md index 5b182e1..7162b6e 100644 --- a/docs/plugins/pipes/github-copilot-sdk.md +++ b/docs/plugins/pipes/github-copilot-sdk.md @@ -1,6 +1,6 @@ # GitHub Copilot SDK Pipe for OpenWebUI -**Author:** [Fu-Jie](https://github.com/Fu-Jie) | **Version:** 0.8.0 | **Project:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) | **License:** MIT +**Author:** [Fu-Jie](https://github.com/Fu-Jie) | **Version:** 0.9.0 | **Project:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) | **License:** MIT This is an advanced Pipe function for [OpenWebUI](https://github.com/open-webui/open-webui) that integrates the official [GitHub Copilot SDK](https://github.com/github/copilot-sdk). It enables you to use **GitHub Copilot models** (e.g., `gpt-5.2-codex`, `claude-sonnet-4.5`,`gemini-3-pro`, `gpt-5-mini`) **AND** your own models via **BYOK** (OpenAI, Anthropic) directly within OpenWebUI, providing a unified agentic experience with **strict User & Chat-level Workspace Isolation**. @@ -14,37 +14,31 @@ This is an advanced Pipe function for [OpenWebUI](https://github.com/open-webui/ --- -## ✨ v0.8.0 Updates (What's New) +## ✨ v0.9.0: The Skills Revolution & Stability Update -- **🎛️ Conditional Tool Filtering (P1~P4)**: Four-priority tool permission system. **Default ON**: If no tools are selected in Chat UI (P4), all enabled tools are active. **Whitelist Mode**: Once specific tools are checked, the whitelist strictly filters both OpenWebUI tools and MCP servers. Admin-level `config.enable` (P2) allows global server disabling. (v0.8.0) -- **🔧 File Publish Reliability**: Fixed `Error getting file content` across all storage backends (local/S3/GCS/Azure) by using `Storage.upload_file()` directly in the fallback path. HTML files are no longer blocked by `ALLOWED_FILE_EXTENSIONS` (`?process=false` always applied). (v0.8.0) -- **🌐 HTML Direct Access Link**: When `publish_file_from_workspace` publishes an HTML file, the plugin also provides a directly accessible HTML link for instant in-chat preview/opening. (v0.8.0) -- **🔒 Strict File URL Format**: Published file links must be relative paths starting with `/api/v1/files/` (e.g., `/api/v1/files/{id}/content/html`). Do not use `api/...` and do not prepend any domain. (v0.8.0) -- **🛠️ CLI Built-in Tools Always Available**: `available_tools` is now always `None`, ensuring Copilot CLI built-ins (e.g. `bash`, `create_file`) are never silently blocked regardless of MCP configuration. (v0.8.0) -- **📌 Publish Tool Always Injected**: `publish_file_from_workspace` is no longer lost when `ENABLE_OPENWEBUI_TOOLS` is disabled. (v0.8.0) -- **⚠️ Code Interpreter Limitation**: The `code_interpreter` tool runs in a remote, ephemeral environment. A system prompt warning now clarifies that it cannot access local files or persist changes. (v0.8.0) - -### 🐞 Bug Fixes in v0.8.0 - -- Fixed `{"detail":"[ERROR: Error getting file content]"}` when publishing files under object storage backends by replacing fallback manual copy/DB writes with `Storage.upload_file()`. -- Fixed HTML artifact upload being rejected by `ALLOWED_FILE_EXTENSIONS` by always appending `?process=false` on file upload API calls. -- Fixed invalid artifact links generated as `api/...` or domain-prefixed absolute URLs; links are now constrained to `/api/v1/files/...` relative paths. -- Fixed Copilot CLI built-ins being silently unavailable when no server tools were configured/loaded (which resulted in `available_tools=[]`); now `available_tools` remains `None`. -- Fixed `publish_file_from_workspace` disappearing when `ENABLE_OPENWEBUI_TOOLS` was disabled. +- **🧩 Copilot SDK Skills Support**: Native support for Copilot SDK skill directories (`SKILL.md` + resources). +- **🔄 OpenWebUI Skills Bridge**: Full bidirectional sync between OpenWebUI Workspace > Skills and SDK skill directories. +- **🛠️ Deterministic `manage_skills` Tool**: Expert tool for stable install/create/list/edit/delete skill operations. +- **🌊 Reinforced Status Bar**: Multi-layered locking mechanism (`session_finalized` guard) and atomic async delivery to prevent "stuck" indicators. +- **🗂️ Persistent Config Directory**: Added `COPILOTSDK_CONFIG_DIR` for stable session-state persistence across container restarts. --- ## ✨ Key Capabilities -- **🔑 Flexible Auth & BYOK**: Official Copilot subscriptions (PAT) or Bring Your Own Key (OpenAI/Anthropic). -- **🔌 Universal Tool Protocol**: Native support for **MCP (Model Context Protocol)**, OpenAPI, and OpenWebUI built-in tools. -- **🛡️ Sandbox Workspace Isolation**: Strict per-session sandboxing for data privacy and security. -- **♾️ Infinite Session Management**: Smart context window management with automatic compaction for indefinite conversation capability. -- **🧠 Deep Database Integration**: Real-time persistence of TOD·O lists for long-running workflows. -- **🌊 Advanced Streaming**: Full support for thinking process/Chain of Thought visualization. -- **🖼️ Intelligent Multimodal**: Vision capabilities and raw file analysis support (bypasses RAG for direct binary access). -- **📤 Workspace Artifacts (`publish_file_from_workspace`)**: Agents can generate files (Excel, CSV, HTML reports, etc.) and provide **persistent download links** directly in the chat. For HTML files, a direct-access HTML link is also provided. -- **🖼️ Interactive Artifacts**: Automatically renders HTML/JS apps generated by the agent directly in the chat interface. +- **🔑 Unified Intelligence (Official + BYOK)**: Seamlessly switch between official GitHub Copilot models (o1, GPT-4o, Claude 3.5 Sonnet, Gemini 2.0 Flash) and your own models (OpenAI, Anthropic) via **Bring Your Own Key** mode. +- **🛡️ Physical Workspace Isolation**: Every session runs in its own isolated directory sandbox. This ensures absolute data privacy and prevents cross-chat file contamination while allowing the Agent full filesystem access. +- **🔌 Universal Tool Protocol**: + - **Native MCP**: Direct, high-performance connection to Model Context Protocol servers. + - **OpenAPI Bridge**: Connect to any external REST API as an Agent tool. + - **OpenWebUI Native**: Zero-config bridge to your existing OpenWebUI tools and built-ins (Web Search, Memory, etc.). +- **🧩 OpenWebUI Skills Bridge**: Transforms simple OpenWebUI Markdown instructions into powerful SDK skill folders complete with supporting scripts, templates, and data. +- **♾️ Infinite Session Management**: Advanced context window management with automatic "Compaction" (summarization + list persistence). Carry out weeks-long projects without losing the core thread. +- **📊 Interactive Artifacts & Publishing**: + - **Live HTML/JS**: Instantly render and interact with apps, dashboards, or reports generated by the Agent. + - **Persistent Publishing**: Agents can "publish" generated files (Excel, CSV, docs) to OpenWebUI's file storage, providing permanent download links. +- **🌊 UX-First Streaming**: Full support for "Thinking" processes (Chain of Thought), status indicators, and real-time progress bars for long-running tasks. +- **🧠 Deep Database Integration**: Real-time persistence of TOD·O lists and session metadata ensures your workflow state is always visible in the UI. --- @@ -69,9 +63,14 @@ Administrators define the default behavior for all users in the function setting | Valve | Default | Description | | :--- | :--- | :--- | | `GH_TOKEN` | `""` | Global GitHub Token (Requires 'Copilot Requests' permission). | +| `COPILOTSDK_CONFIG_DIR` | `""` | Persistent directory for SDK config and session state (e.g., `/app/backend/data/.copilot`). | | `ENABLE_OPENWEBUI_TOOLS` | `True` | Enable OpenWebUI Tools (includes defined Tools and Built-in Tools). | | `ENABLE_OPENAPI_SERVER` | `True` | Enable OpenAPI Tool Server connection. | | `ENABLE_MCP_SERVER` | `True` | Enable Direct MCP Client connection (Recommended). | +| `ENABLE_OPENWEBUI_SKILLS` | `True` | Enable bidirectional sync with OpenWebUI Workspace > Skills. | +| `OPENWEBUI_SKILLS_SHARED_DIR` | `/app/backend/data/cache/copilot-openwebui-skills` | Shared cache directory for skills. | +| `GITHUB_SKILLS_SOURCE_URL` | `""` | Optional GitHub tree URL for batch skill import (e.g., anthropic/skills). | +| `DISABLED_SKILLS` | `""` | Comma-separated skill names to disable in SDK session. | | `REASONING_EFFORT` | `medium` | Reasoning effort level: low, medium, high. | | `SHOW_THINKING` | `True` | Show model reasoning/thinking process. | | `INFINITE_SESSION` | `True` | Enable Infinite Sessions (automatic context compaction). | @@ -95,10 +94,64 @@ Standard users can override these settings in their individual Profile/Function | `SHOW_THINKING` | Show model reasoning/thinking process. | | `MAX_MULTIPLIER` | Maximum allowed billing multiplier override. | | `EXCLUDE_KEYWORDS` | Exclude models containing these keywords. | +| `ENABLE_OPENWEBUI_SKILLS` | Enable loading all active OpenWebUI skills readable by you into SDK `SKILL.md` directories. | +| `GITHUB_SKILLS_SOURCE_URL` | Optional GitHub tree URL for batch skill import in your own session. | +| `DISABLED_SKILLS` | Comma-separated skill names to disable for your own session. | | `BYOK_API_KEY` | Use your personal OpenAI/Anthropic API Key. | --- +### 📤 Enhanced Publishing & Interactive Components + +The `publish_file_from_workspace` tool now uses a clearer delivery contract for production use: + +- **Artifacts mode (`artifacts`, default)**: Agent returns `[Preview]` + `[Download]` and may output `html_embed` in a ```html block for direct chat rendering. +- **Rich UI mode (`richui`)**: Agent returns `[Preview]` + `[Download]` only; integrated preview is rendered automatically via emitter (no iframe block in message). +- **📄 PDF delivery safety rule**: Always output Markdown links only (`[Preview]` + `[Download]` when available). **Do not embed PDF via iframe/html blocks.** +- **⚡ Stable dual-channel publishing**: Keeps interactive viewing and persistent file download aligned across local/object-storage backends. +- **✅ Status integration**: Emits real-time publishing progress and completion feedback to the OpenWebUI status bar. +- **📘 Publishing Tool Guide (GitHub)**: [publish_file_from_workspace Guide](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE.md) + +--- + +### 🧩 OpenWebUI Skills Bridge & `manage_skills` Tool + +The SDK now features a bidirectional bridge with the OpenWebUI **Workspace > Skills** page: + +- **🔄 Automatic Sync**: Skills created or updated in the OpenWebUI UI are automatically downloaded as `SKILL.md` folders into the SDK's shared cache on every request. +- **🛠️ `manage_skills` Tool**: The Agent can deterministically manage skills using this tool. + - `list`: List all installed skills and their descriptions. + - `install`: Install a skill from a GitHub URL (auto-normalized to archive link) or a direct `.zip`/`.tar.gz`. + - `create`: Create a new skill directory from context, writing `SKILL.md` and any extra resource files (scripts, templates). + - `edit`: Update an existing skill folder. + - `delete`: Atomically delete both the local directory and the linked OpenWebUI DB entry. +- **📁 Full Folder Support**: Unlike the single-markdown storage in OpenWebUI DB, the SDK loads the **entire folder** for each skill. This allows skills to carry binary scripts, data files, or complex templates alongside the core instructions. +- **🌐 Shared Persistent Cache**: Skills are stored in `OPENWEBUI_SKILLS_SHARED_DIR/shared/`, which is persistent across sessions and container restarts. +- **📚 Full Skill Docs (GitHub)**: [manage_skills Tool Guide](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER.md) | [Skills Best Practices](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES.md) + +--- + +### 🌊 Fluid UX & Granular Status Feedback + +Say goodbye to the "stuck" feeling during complex processing: + +- **🔄 Real-Time Status Bubbles**: Maps internal SDK events (`turn_start`, `compaction`, `subagent_started`) directly to the OpenWebUI status bar. +- **🧭 Richer Stage Descriptions**: Status text now explicitly reflects phases such as processing, skill invocation, tool execution, tool completion/failure, publishing, and final completion. +- **⏱️ Long-Task Heartbeat**: During long waits, the status bar emits periodic "still processing" updates (elapsed-time style) to avoid silent stalls. +- **📈 Tool Progress Tracking**: Long-running tool executions provide live progress percentages and descriptive sub-task updates in the status bar. +- **⚡ Immediate Feedback**: Response starts with an instant "Assistant is processing" status, eliminating idle wait time before the first token. + +--- + +### 🛡️ Smart Version Compatibility + +The plugin automatically adapts its feature set based on your OpenWebUI version: + +- **v0.8.0+**: Rich UI, live status bubbles, and integrated HTML preview. +- **Older**: Automatic fallback to standard Markdown blocks for maximum stability. + +--- + ## 🎯 Use Cases (What can you do?) - **📁 Fully Autonomous DevOps**: Agent analyzes code, runs tests, and applies patches within its isolated sandbox. @@ -141,7 +194,7 @@ If neither is configured, the model list will not appear. ## 📋 Troubleshooting & Dependencies - **Agent ignores files?**: Ensure the Files Filter is enabled, otherwise RAG will interfere with raw binaries. -- **No progress bar?**: The bar only appears when the Agent uses the `update_todo` tool. +- **No status updates?**: Status bubbles are emitted for processing/tool phases; TODO progress bars specifically appear when the Agent uses `update_todo`. - **Dependencies**: This Pipe automatically manages `github-copilot-sdk` (Python) and utilizes the bundled binary CLI. No manual install required. --- diff --git a/docs/plugins/pipes/github-copilot-sdk.zh.md b/docs/plugins/pipes/github-copilot-sdk.zh.md index 6a4e6e5..057c59a 100644 --- a/docs/plugins/pipes/github-copilot-sdk.zh.md +++ b/docs/plugins/pipes/github-copilot-sdk.zh.md @@ -1,12 +1,24 @@ # GitHub Copilot SDK 官方管道 -**作者:** [Fu-Jie](https://github.com/Fu-Jie) | **版本:** 0.8.0 | **项目:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) | **许可证:** MIT +**作者:** [Fu-Jie](https://github.com/Fu-Jie/openwebui-extensions) | **版本:** 0.9.0 | **项目:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) | **许可证:** MIT 这是一个用于 [OpenWebUI](https://github.com/open-webui/open-webui) 的高级 Pipe 函数,深度集成了 **GitHub Copilot SDK**。它不仅支持 **GitHub Copilot 官方模型**(如 `gpt-5.2-codex`, `claude-sonnet-4.5`, `gemini-3-pro`, `gpt-5-mini`),还支持 **BYOK (自带 Key)** 模式对接自定义服务商(OpenAI, Anthropic),并具备**严格的用户与会话级工作区隔离**能力,提供统一且安全的 Agent 交互体验。 > [!IMPORTANT] > **核心伴侣组件** -> 如需启用文件处理与数据分析能力,请务必安装 [GitHub Copilot SDK Files Filter](https://openwebui.com/posts/403a62ee-a596-45e7-be65-fab9cc249dd6)。 +> 如需启用文件处理与数据分析能力,请务必安装 [GitHub Copilot SDK Files Filter](https://openwebui.com/posts/403a62ee-a596-45e7-be65-fab9cc24dd6)。 +> +>## ✨ 0.9.0 核心更新:技能革命与稳定性加固 + +- **🧩 Copilot SDK Skills 原生支持**: 技能可作为一等上下文能力被加载和使用。 +- **🔄 OpenWebUI Skills 桥接**: 实现 OpenWebUI **工作区 > Skills** 与 SDK 技能目录的深度双向同步。 +- **🛠️ 确定性 `manage_skills` 工具**: 通过稳定工具契约完成技能的生命周期管理。 +- **🌊 状态栏逻辑加固**: 引入 `session_finalized` 多层锁定机制,彻底解决任务完成后状态栏回弹或卡死的问题。 +- **🗂️ 环境目录持久化**: 增强 `COPILOTSDK_CONFIG_DIR` 逻辑,确保会话状态跨容器重启稳定存在。 +- **🌐 持续化共享缓存(扩展)**: 技能统一存储在 `OPENWEBUI_SKILLS_SHARED_DIR/shared/`,跨会话与容器重启复用。 +- **🎯 智能意图路由(扩展)**: 自动识别技能管理请求并优先路由到 `manage_skills`,确保执行确定性。 +- **🗂️ 环境目录升级**: 新增 `COPILOTSDK_CONFIG_DIR`,并自动回退到 `/app/backend/data/.copilot`,确保 SDK 配置与会话状态在容器重启后稳定持久化。 +- **🧭 CLI 提示词护栏**: 系统提示词明确区分可执行的 **tools** 与不可调用的 **skills**,并要求技能生命周期操作优先走 `manage_skills`,同时强化 CLI/Python 执行规范。 > [!TIP] > **BYOK 模式无需订阅** @@ -14,37 +26,21 @@ --- -## ✨ 0.8.0 更新内容 (What's New) - -- **🎛️ 条件工具过滤 (P1~P4)**: 四优先级工具权限体系。**默认全开**: 若未在 Chat UI (P4) 勾选任何工具,则默认启用所有工具;**白名单模式**: 一旦勾选特定工具,即刻进入严格过滤模式,且 MCP server 同步受控;管理员亦可通过 `config.enable` (P2) 全局禁用工具服务器。(v0.8.0) -- **🔧 文件发布全面修复**: 通过在回退路径直接调用 `Storage.upload_file()`,彻底修复了所有存储后端(local/S3/GCS/Azure)下的 `Error getting file content` 问题;同时上传时自动携带 `?process=false`,HTML 文件不再被 `ALLOWED_FILE_EXTENSIONS` 拦截。(v0.8.0) -- **🌐 HTML 直达链接**: 当 `publish_file_from_workspace` 发布的是 HTML 文件时,插件会额外提供可直接访问的 HTML 链接,便于在聊天中即时预览/打开。(v0.8.0) -- **🔒 文件链接格式严格约束**: 发布链接必须是以 `/api/v1/files/` 开头的相对路径(例如 `/api/v1/files/{id}/content/html`)。禁止使用 `api/...`,也禁止拼接任何域名。(v0.8.0) -- **🛠️ CLI 内置工具始终可用**: `available_tools` 统一设为 `None`,Copilot CLI 内置工具(如 `bash`、`create_file`)无论 MCP 配置如何都不会被静默屏蔽。(v0.8.0) -- **📌 发布工具始终注入**: 即使 `ENABLE_OPENWEBUI_TOOLS` 关闭,`publish_file_from_workspace` 工具也不再丢失。(v0.8.0) -- **⚠️ 代码解释器限制**: `code_interpreter` 工具运行在远程临时环境中。系统提示词现已包含警告,明确指出该工具无法访问本地文件或持久化更改。(v0.8.0) - -### 🐞 v0.8.0 Bug 修复说明 - -- 修复了对象存储后端发布文件时出现的 `{"detail":"[ERROR: Error getting file content]"}`,回退路径从手动复制/写库改为 `Storage.upload_file()`。 -- 修复了 HTML 产物被 `ALLOWED_FILE_EXTENSIONS` 拦截的问题,上传接口统一追加 `?process=false`。 -- 修复了产物链接偶发被生成成 `api/...` 或带域名绝对 URL 的问题,现统一限制为 `/api/v1/files/...` 相对路径。 -- 修复了在未配置/未加载任何 server 工具时(最终出现 `available_tools=[]`)Copilot CLI 内置工具被静默禁用的问题,现统一保持 `available_tools=None`。 -- 修复了 `ENABLE_OPENWEBUI_TOOLS` 关闭时 `publish_file_from_workspace` 工具丢失的问题。 - ---- - ## ✨ 核心能力 (Key Capabilities) -- **🔑 灵活鉴权与 BYOK**: 支持 GitHub Copilot 官方订阅 (PAT) 或自带 Key (OpenAI/Anthropic)。 -- **🔌 通用工具协议**: 原生支持 **MCP (Model Context Protocol)**、OpenAPI 以及 OpenWebUI 内置工具。 -- **🛡️ 物理级工作区隔离**: 强制执行严格的用户特定沙箱,确保数据隐私与文件安全。 -- **♾️ 无限会话管理**: 智能上下文窗口管理与自动压缩算法,支持无限时长的对话交互。 -- **🧠 深度数据库集成**: 实时持久化 TOD·O 列表到 UI 进度条。 -- **🌊 深度推理展示**: 完整支持模型思考过程 (Thinking Process) 的流式渲染。 -- **🖼️ 智能多模态**: 完整支持图像识别与附件上传分析(绕过 RAG 直接访问原始二进制内容)。 -- **📤 工作区产物工具 (`publish_file_from_workspace`)**: Agent 可生成文件(Excel、CSV、HTML 报告等)并直接提供**持久化下载链接**。管理员还可额外获得通过 `/content/html` 接口的**聊天内 HTML 预览**链接。 -- **🖼️ 交互式伪影 (Artifacts)**: 自动渲染 Agent 生成的 HTML/JS 应用程序,直接在聊天界面交互。 +- **🔑 统一智能体验 (官方 + BYOK)**: 自由切换官方模型(o1, GPT-4o, Claude 3.5 Sonnet, Gemini 2.0 Flash)与自定义服务商(OpenAI, Anthropic),支持 **BYOK (自带 Key)** 模式。 +- **🛡️ 物理级工作区隔离**: 每个会话在独立的沙箱目录中运行。确保绝对的数据隐私,防止不同聊天间的文件污染,同时给予 Agent 完整的文件系统操作权限。 +- **🔌 通用工具协议**: + - **原生 MCP**: 高性能直连 Model Context Protocol 服务器。 + - **OpenAPI 桥接**: 将任何外部 REST API 一键转换为 Agent 可调用的工具。 + - **OpenWebUI 原生桥接**: 零配置接入现有的 OpenWebUI 工具及内置功能(网页搜索、记忆等)。 +- **🧩 OpenWebUI Skills 桥接**: 将简单的 OpenWebUI Markdown 指令转化为包含脚本、模板和数据的强大 SDK 技能文件夹。 +- **♾️ 无限会话管理**: 先进的上下文窗口管理,支持自动“压缩”(摘要提取 + TODO 列表持久化)。支持长达数周的项目跟踪而不会丢失核心上下文。 +- **📊 交互式产物与发布**: + - **实时 HTML/JS**: 瞬间渲染并交互 Agent 生成的应用程序、可视化看板或报告。 + - **持久化发布**: Agent 可将生成的产物(Excel, CSV, 文档)发布至 OpenWebUI 文件存储,并在聊天中提供永久下载链接。 +- **🌊 极致交互体验**: 完整支持深度思考过程 (Thinking Process) 流式渲染、状态指示器以及长任务实时进度条。 +- **🧠 深度数据库集成**: TOD·O 列表与会话元数据的实时持久化,确保任务执行状态在 UI 上清晰可见。 --- @@ -69,9 +65,14 @@ | 参数 | 默认值 | 说明 | | :--- | :--- | :--- | | `GH_TOKEN` | `""` | 全局 GitHub Token (需具备 'Copilot Requests' 权限)。 | +| `COPILOTSDK_CONFIG_DIR` | `""` | SDK 配置与会话状态持久化目录 (例如: `/app/backend/data/.copilot`)。 | | `ENABLE_OPENWEBUI_TOOLS` | `True` | 启用 OpenWebUI 工具 (包括定义工具和内置工具)。 | | `ENABLE_OPENAPI_SERVER` | `True` | 启用 OpenAPI 工具服务器连接。 | | `ENABLE_MCP_SERVER` | `True` | 启用直接 MCP 客户端连接 (推荐)。 | +| `ENABLE_OPENWEBUI_SKILLS` | `True` | 开启与 OpenWebUI **工作区 > Skills** 的双向同步桥接。 | +| `OPENWEBUI_SKILLS_SHARED_DIR` | `/app/backend/data/cache/copilot-openwebui-skills` | OpenWebUI skills 转换后的共享缓存目录。 | +| `GITHUB_SKILLS_SOURCE_URL` | `""` | 可选 GitHub tree 地址,用于批量导入 skills(例如 anthropic/skills)。 | +| `DISABLED_SKILLS` | `""` | 逗号分隔的 skill 名称黑名单(如 `docs-writer,webapp-testing`)。 | | `REASONING_EFFORT` | `medium` | 推理强度:low, medium, high。 | | `SHOW_THINKING` | `True` | 显示模型推理/思考过程。 | | `INFINITE_SESSION` | `True` | 启用无限会话 (自动上下文压缩)。 | @@ -79,7 +80,7 @@ | `EXCLUDE_KEYWORDS` | `""` | 排除包含这些关键字的模型 (逗号分隔)。 | | `TIMEOUT` | `300` | 每个流数据块的超时时间 (秒)。 | | `BYOK_TYPE` | `openai` | BYOK 服务商类型:`openai`, `anthropic`。 | -| `BYOK_BASE_URL` | `""` | BYOK 基础 URL (例如: )。 | +| `BYOK_BASE_URL` | `""` | BYOK 基础 URL (例如: | | `BYOK_MODELS` | `""` | BYOK 模型列表 (逗号分隔)。留空则从 API 获取。 | | `CUSTOM_ENV_VARS` | `""` | 自定义环境变量 (JSON 格式)。 | | `DEBUG` | `False` | 开启此项以在前端控制台输出详细调试日志。 | @@ -95,10 +96,34 @@ | `SHOW_THINKING` | 显示模型推理/思考过程。 | | `MAX_MULTIPLIER` | 最大允许的模型计费倍率覆盖。 | | `EXCLUDE_KEYWORDS` | 排除包含这些关键字的模型。 | +| `ENABLE_OPENWEBUI_SKILLS` | 启用将当前用户可读的全部已启用 OpenWebUI skills 转换并加载为 SDK `SKILL.md` 目录。 | +| `GITHUB_SKILLS_SOURCE_URL` | 为当前用户会话设置可选 GitHub tree 地址以批量导入 skills。 | +| `DISABLED_SKILLS` | 为当前用户会话禁用指定 skills(逗号分隔)。 | | `BYOK_API_KEY` | 使用个人的 OpenAI/Anthropic API Key。 | --- +### 🌊 细粒度反馈与流畅体验 (Fluid UX) + +彻底告别复杂任务执行过程中的“卡顿”感: + +- **🔄 实时状态气泡**: 将 SDK 内部事件(如 `turn_start`, `compaction`, `subagent_started`)直接映射为 OpenWebUI 的状态栏信息。 +- **🧭 分阶段状态描述增强**: 状态栏会明确显示处理阶段(处理中、技能触发、工具执行、工具完成/失败、发布中、任务完成)。 +- **⏱️ 长任务心跳提示**: 长时间处理中会周期性显示“仍在处理中(已耗时 X 秒)”,避免用户误判为卡死。 +- **📈 工具执行进度追踪**: 长耗时工具(如代码分析)会在状态栏实时显示进度百分比及当前子任务描述。 +- **⚡ 即时响应反馈**: 从响应开始第一秒即显示“助手正在处理您的请求...”,减少等待空窗感。 + +--- + +### 🛡️ 智能版本兼容 + +插件会自动根据您的 OpenWebUI 版本调整功能集: + +- **v0.8.0+**: 开启 Rich UI、实时状态气泡及集成 HTML 预览。 +- **旧版本**: 自动回退至标准 Markdown 代码块模式,确保最大稳定性。 + +--- + ## 🎯 典型应用场景 (Use Cases) - **📁 全自主仓库维护**: Agent 在隔离工作区内自动分析代码、运行测试并应用补丁。 @@ -136,12 +161,46 @@ 如果两者都未配置,模型列表将不会出现。 +### 4) 配套插件 (强烈推荐) + +为了获得最佳的文件处理体验,请安装 [GitHub Copilot SDK Files Filter](https://openwebui.com/posts/403a62ee-a596-45e7-be65-fab9cc249dd6)。 + +--- + +### 📤 增强型发布工具与交互式组件 + +`publish_file_from_workspace` 现采用更清晰、可落地的交付规范: + +- **Artifacts 模式(`artifacts`,默认)**:返回 `[Preview]` + `[Download]`,并可附带 `html_embed`,在 ```html 代码块中直接渲染。 +- **Rich UI 模式(`richui`)**:仅返回 `[Preview]` + `[Download]`,由发射器自动触发集成式预览(消息中不输出 iframe 代码块)。 +- **📄 PDF 安全交付规则**:仅输出 Markdown 链接(可用时为 `[Preview]` + `[Download]`)。**禁止通过 iframe/html 方式嵌入 PDF。** +- **⚡ 稳定双通道发布**:在本地与对象存储后端下,保持交互预览与持久下载链接一致可用。 +- **✅ 状态集成**:通过 OpenWebUI 状态栏实时反馈发布进度与完成状态。 +- **📘 发布工具指南(GitHub)**:[publish_file_from_workspace 工具指南(中文)](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE_CN.md) + +--- + +### 🧩 OpenWebUI Skills 桥接与 `manage_skills` 工具 + +SDK 现在具备与 OpenWebUI **工作区 > Skills** 的双向同步能力: + +- **🔄 自动同步**: 每次请求时,前端定义的技能会自动作为 `SKILL.md` 文件夹同步至 SDK 共享缓存,Agent 可直接调用。 +- **🛠️ `manage_skills` 工具**: 内置专业工具,赋予 Agent (或用户) 绝对的技能管理权。 + - `list`: 列出所有已安装技能及描述。 + - `install`: 从 GitHub URL (自动转换归档链接) 或直接从 `.zip`/`.tar.gz` 安装。 + - `create`: 从当前会话内容创建新技能目录,支持写入 `SKILL.md` 及辅助资源文件 (脚本、模板)。 + - `edit`: 更新现有技能文件夹。 + - `delete`: 原子化删除本地目录及关联的数据库条目,防止僵尸技能复活。 +- **📁 完整的文件夹支持**: 不同于数据库中单文件存储,SDK 会加载技能的**整个目录**。这使得技能可以携带二进制脚本、数据文件或复杂模板。 +- **🌐 持久化共享缓存**: 技能存储在 `OPENWEBUI_SKILLS_SHARED_DIR/shared/`,跨会话及容器重启持久存在。 +- **📚 技能完整文档(GitHub)**: [manage_skills 工具指南(中文)](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER_CN.md) | [Skills Best Practices(中文)](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES_CN.md) + --- ## 📋 常见问题与依赖 (Troubleshooting) - **Agent 无法识别文件?**: 请确保已安装并启用了 Files Filter 插件,否则原始文件会被 RAG 干扰。 -- **看不到 TODO 进度条?**: 进度条仅在 Agent 使用 `update_todo` 工具(通常是处理复杂任务)时出现。 +- **看不到状态更新或 TODO 进度条?**: 状态气泡会覆盖处理/工具阶段;而 TODO 进度条仅在 Agent 使用 `update_todo` 工具(通常是复杂任务)时出现。 - **依赖安装**: 本管道会自动管理 `github-copilot-sdk` (Python 包) 并优先直接使用内置的二进制 CLI,无需手动干预。 --- diff --git a/docs/plugins/pipes/index.md b/docs/plugins/pipes/index.md index 65d148d..4898c7c 100644 --- a/docs/plugins/pipes/index.md +++ b/docs/plugins/pipes/index.md @@ -15,7 +15,7 @@ Pipes allow you to: ## Available Pipe Plugins -- [GitHub Copilot SDK](github-copilot-sdk.md) (v0.8.0) - Official GitHub Copilot SDK integration. Features **Workspace Isolation**, **Database Persistence**, **Zero-config OpenWebUI Tool Bridge**, **BYOK** support, and **dynamic MCP discovery**. Supports streaming, multimodal, and infinite sessions. [View Deep Dive](github-copilot-sdk-deep-dive.md) | [**View Advanced Tutorial**](github-copilot-sdk-tutorial.md). +- [GitHub Copilot SDK](github-copilot-sdk.md) (v0.9.0) - Official GitHub Copilot SDK integration. Features **Workspace Isolation**, **Zero-config OpenWebUI Tool Bridge**, **BYOK** support, and **dynamic MCP discovery**. **NEW in v0.9.0: OpenWebUI Skills Bridge**, reinforced status bar stability, and persistent SDK config management. [View Deep Dive](github-copilot-sdk-deep-dive.md) | [**View Advanced Tutorial**](github-copilot-sdk-tutorial.md) | [**View Detailed Usage Guide**](github-copilot-sdk-usage-guide.md). - **[Case Study: GitHub 100 Star Growth Analysis](star-prediction-example.md)** - Learn how to use the GitHub Copilot SDK Pipe with Minimax 2.1 to automatically analyze CSV data and generate project growth reports. - **[Case Study: High-Quality Video to GIF Conversion](video-processing-example.md)** - See how the model uses system-level FFmpeg to accelerate, scale, and optimize colors for screen recordings. diff --git a/docs/plugins/pipes/index.zh.md b/docs/plugins/pipes/index.zh.md index 7f57e10..3b266ec 100644 --- a/docs/plugins/pipes/index.zh.md +++ b/docs/plugins/pipes/index.zh.md @@ -15,7 +15,7 @@ Pipes 可以用于: ## 可用的 Pipe 插件 -- [GitHub Copilot SDK](github-copilot-sdk.zh.md) (v0.8.0) - GitHub Copilot SDK 官方集成。具备**工作区安全隔离**、**数据库持久化**、**零配置工具桥接**与**BYOK (自带 Key) 支持**。支持流式输出、打字机思考过程及无限会话。[查看深度架构解析](github-copilot-sdk-deep-dive.zh.md) | [**查看进阶实战教程**](github-copilot-sdk-tutorial.zh.md)。 +- [GitHub Copilot SDK](github-copilot-sdk.zh.md) (v0.9.0) - GitHub Copilot SDK 官方集成。具备**工作区安全隔离**、**零配置工具桥接**与**BYOK (自带 Key) 支持**。**v0.9.0 重量级更新:OpenWebUI Skills 桥接**、状态栏稳定性加固,以及持久化 SDK 配置目录管理(`COPILOTSDK_CONFIG_DIR`)。[查看深度架构解析](github-copilot-sdk-deep-dive.zh.md) | [**查看进阶实战教程**](github-copilot-sdk-tutorial.zh.md) | [**查看详细使用手册**](github-copilot-sdk-usage-guide.zh.md)。 - **[实战案例:GitHub 100 Star 增长预测](star-prediction-example.zh.md)** - 展示如何使用 GitHub Copilot SDK Pipe 结合 Minimax 2.1 模型,自动编写脚本分析 CSV 数据并生成详细的项目增长报告。 - **[实战案例:视频高质量 GIF 转换与加速](video-processing-example.zh.md)** - 演示模型如何通过底层 FFmpeg 工具对录屏进行加速、缩放及双阶段色彩优化处理。 diff --git a/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE.md b/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE.md new file mode 100644 index 0000000..c719626 --- /dev/null +++ b/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE.md @@ -0,0 +1,76 @@ +# 📤 `publish_file_from_workspace` Tool Guide + +This document explains the recommended usage contract of the built-in `publish_file_from_workspace` tool in the GitHub Copilot SDK Pipe. + +## Tool Purpose + +Use this tool when the agent has generated a file in the current workspace and needs to: + +- Save the file into OpenWebUI file storage. +- Return stable links for preview and download. +- Keep rendering behavior consistent across local disk and object storage backends. + +## Required Input + +- `filename`: Relative filename under current workspace. + - ✅ Example: `report.xlsx` + - ✅ Example: `output/summary.html` + - ❌ Avoid temporary paths outside workspace (e.g. `/tmp/...`). + +## Output Contract + +The tool typically returns structured fields used by the pipe to build user-facing links: + +- `filename` +- `download_url` +- `preview_url` (if preview is available) +- metadata used by renderer (including optional `html_embed` for HTML previews) + +## Embed Modes + +### 1) `artifacts` (default) + +- Message should include `[Preview]` + `[Download]` links. +- For HTML-capable content, `html_embed` may be rendered in a ```html block. +- Best for inline interactive previews in chat. + +### 2) `richui` + +- Message should include `[Preview]` + `[Download]` links. +- Integrated preview is emitted by Rich UI renderer automatically. +- Do not output iframe/html preview block in chat body. + +## PDF Safety Rule (Mandatory) + +For PDF files, always output markdown links only: + +- `[Preview](...)` +- `[Download](...)` (if available) + +Do NOT embed PDFs with iframe or raw HTML blocks. + +## Recommended Workflow + +1. Generate file in workspace. +2. Call `publish_file_from_workspace(filename=...)`. +3. Return links according to selected embed mode. +4. Follow PDF safety rule for any `.pdf` output. + +## Practical Example + +### Example A: HTML report (artifacts) + +- Publish `analysis.html`. +- Return links. +- Allow `html_embed` block rendering for direct preview. + +### Example B: PDF report + +- Publish `audit.pdf`. +- Return links only. +- Skip iframe/html embedding entirely. + +## Related Docs + +- [Skills Manager Guide](./SKILLS_MANAGER.md) +- [Skills Best Practices](./SKILLS_BEST_PRACTICES.md) diff --git a/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE_CN.md b/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE_CN.md new file mode 100644 index 0000000..0a5daff --- /dev/null +++ b/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE_CN.md @@ -0,0 +1,76 @@ +# 📤 `publish_file_from_workspace` 工具指南 + +本文档说明 GitHub Copilot SDK Pipe 内置工具 `publish_file_from_workspace` 的推荐使用规范。 + +## 工具用途 + +当 Agent 在当前工作区生成文件后,使用此工具可实现: + +- 将文件发布到 OpenWebUI 文件存储。 +- 返回稳定可用的预览/下载链接。 +- 在本地磁盘与对象存储后端保持一致交付行为。 + +## 必填参数 + +- `filename`:工作区内的相对路径文件名。 + - ✅ 示例:`report.xlsx` + - ✅ 示例:`output/summary.html` + - ❌ 避免工作区外临时路径(如 `/tmp/...`)。 + +## 返回结构(常见字段) + +该工具通常返回用于构建前端链接与渲染的数据: + +- `filename` +- `download_url` +- `preview_url`(如可预览) +- 渲染元数据(HTML 场景可含 `html_embed`) + +## 发布模式 + +### 1) `artifacts`(默认) + +- 消息中返回 `[Preview]` + `[Download]`。 +- 对于 HTML 可预览内容,可在 ```html 代码块中渲染 `html_embed`。 +- 适用于聊天内联交互式预览。 + +### 2) `richui` + +- 消息中返回 `[Preview]` + `[Download]`。 +- 由 Rich UI 渲染器自动输出集成预览。 +- 聊天正文中不输出 iframe/html 预览块。 + +## PDF 安全规则(强制) + +针对 PDF 文件,必须只输出 Markdown 链接: + +- `[Preview](...)` +- `[Download](...)`(可用时) + +禁止使用 iframe 或 HTML 代码块嵌入 PDF。 + +## 推荐流程 + +1. 在工作区生成文件。 +2. 调用 `publish_file_from_workspace(filename=...)`。 +3. 按模式返回链接。 +4. 若为 `.pdf`,严格执行“仅链接”规则。 + +## 示例 + +### 示例 A:HTML 报告(artifacts) + +- 发布 `analysis.html`。 +- 返回链接。 +- 允许渲染 `html_embed` 进行直接预览。 + +### 示例 B:PDF 报告 + +- 发布 `audit.pdf`。 +- 仅返回链接。 +- 完全跳过 iframe/html 嵌入。 + +## 相关文档 + +- [manage_skills 工具指南](./SKILLS_MANAGER_CN.md) +- [Skills 最佳实践](./SKILLS_BEST_PRACTICES_CN.md) diff --git a/plugins/pipes/github-copilot-sdk/README.md b/plugins/pipes/github-copilot-sdk/README.md index 5b182e1..3735d17 100644 --- a/plugins/pipes/github-copilot-sdk/README.md +++ b/plugins/pipes/github-copilot-sdk/README.md @@ -1,6 +1,6 @@ # GitHub Copilot SDK Pipe for OpenWebUI -**Author:** [Fu-Jie](https://github.com/Fu-Jie) | **Version:** 0.8.0 | **Project:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) | **License:** MIT +**Author:** [Fu-Jie](https://github.com/Fu-Jie) | **Version:** 0.9.0 | **Project:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) | **License:** MIT This is an advanced Pipe function for [OpenWebUI](https://github.com/open-webui/open-webui) that integrates the official [GitHub Copilot SDK](https://github.com/github/copilot-sdk). It enables you to use **GitHub Copilot models** (e.g., `gpt-5.2-codex`, `claude-sonnet-4.5`,`gemini-3-pro`, `gpt-5-mini`) **AND** your own models via **BYOK** (OpenAI, Anthropic) directly within OpenWebUI, providing a unified agentic experience with **strict User & Chat-level Workspace Isolation**. @@ -14,37 +14,33 @@ This is an advanced Pipe function for [OpenWebUI](https://github.com/open-webui/ --- -## ✨ v0.8.0 Updates (What's New) +## ✨ v0.9.0: The Skills Revolution & Stability Update -- **🎛️ Conditional Tool Filtering (P1~P4)**: Four-priority tool permission system. **Default ON**: If no tools are selected in Chat UI (P4), all enabled tools are active. **Whitelist Mode**: Once specific tools are checked, the whitelist strictly filters both OpenWebUI tools and MCP servers. Admin-level `config.enable` (P2) allows global server disabling. (v0.8.0) -- **🔧 File Publish Reliability**: Fixed `Error getting file content` across all storage backends (local/S3/GCS/Azure) by using `Storage.upload_file()` directly in the fallback path. HTML files are no longer blocked by `ALLOWED_FILE_EXTENSIONS` (`?process=false` always applied). (v0.8.0) -- **🌐 HTML Direct Access Link**: When `publish_file_from_workspace` publishes an HTML file, the plugin also provides a directly accessible HTML link for instant in-chat preview/opening. (v0.8.0) -- **🔒 Strict File URL Format**: Published file links must be relative paths starting with `/api/v1/files/` (e.g., `/api/v1/files/{id}/content/html`). Do not use `api/...` and do not prepend any domain. (v0.8.0) -- **🛠️ CLI Built-in Tools Always Available**: `available_tools` is now always `None`, ensuring Copilot CLI built-ins (e.g. `bash`, `create_file`) are never silently blocked regardless of MCP configuration. (v0.8.0) -- **📌 Publish Tool Always Injected**: `publish_file_from_workspace` is no longer lost when `ENABLE_OPENWEBUI_TOOLS` is disabled. (v0.8.0) -- **⚠️ Code Interpreter Limitation**: The `code_interpreter` tool runs in a remote, ephemeral environment. A system prompt warning now clarifies that it cannot access local files or persist changes. (v0.8.0) - -### 🐞 Bug Fixes in v0.8.0 - -- Fixed `{"detail":"[ERROR: Error getting file content]"}` when publishing files under object storage backends by replacing fallback manual copy/DB writes with `Storage.upload_file()`. -- Fixed HTML artifact upload being rejected by `ALLOWED_FILE_EXTENSIONS` by always appending `?process=false` on file upload API calls. -- Fixed invalid artifact links generated as `api/...` or domain-prefixed absolute URLs; links are now constrained to `/api/v1/files/...` relative paths. -- Fixed Copilot CLI built-ins being silently unavailable when no server tools were configured/loaded (which resulted in `available_tools=[]`); now `available_tools` remains `None`. -- Fixed `publish_file_from_workspace` disappearing when `ENABLE_OPENWEBUI_TOOLS` was disabled. +- **🧩 Copilot SDK Skills Support**: Native support for Copilot SDK skill directories (`SKILL.md` + resources). Skills can now be loaded as first-class runtime context. +- **🔄 OpenWebUI Skills Bridge**: Full bidirectional sync between OpenWebUI **Workspace > Skills** and SDK skill directories. +- **🛠️ Deterministic `manage_skills` Tool**: Expert tool for stable install/create/list/edit/delete skill operations. +- **🌊 Reinforced Status Bar**: Multi-layered locking mechanism (`session_finalized` guard) and atomic async delivery to prevent "stuck" indicators. +- **⚡ Asynchronous Integrity**: Refactored status emission to route all updates through a centralized helper, ensuring atomic delivery and preventing race conditions in parallel execution streams. +- **💓 Pulse-Lock Refresh**: Implemented a hardware-inspired "pulse" logic that forces a final UI state refresh at the end of each session, ensuring the status bar settling on "Task completed." +- **🗂️ Persistent Config Directory**: Added `COPILOTSDK_CONFIG_DIR` for stable session-state persistence across container restarts. --- ## ✨ Key Capabilities -- **🔑 Flexible Auth & BYOK**: Official Copilot subscriptions (PAT) or Bring Your Own Key (OpenAI/Anthropic). -- **🔌 Universal Tool Protocol**: Native support for **MCP (Model Context Protocol)**, OpenAPI, and OpenWebUI built-in tools. -- **🛡️ Sandbox Workspace Isolation**: Strict per-session sandboxing for data privacy and security. -- **♾️ Infinite Session Management**: Smart context window management with automatic compaction for indefinite conversation capability. -- **🧠 Deep Database Integration**: Real-time persistence of TOD·O lists for long-running workflows. -- **🌊 Advanced Streaming**: Full support for thinking process/Chain of Thought visualization. -- **🖼️ Intelligent Multimodal**: Vision capabilities and raw file analysis support (bypasses RAG for direct binary access). -- **📤 Workspace Artifacts (`publish_file_from_workspace`)**: Agents can generate files (Excel, CSV, HTML reports, etc.) and provide **persistent download links** directly in the chat. For HTML files, a direct-access HTML link is also provided. -- **🖼️ Interactive Artifacts**: Automatically renders HTML/JS apps generated by the agent directly in the chat interface. +- **🔑 Unified Intelligence (Official + BYOK)**: Seamlessly switch between official GitHub Copilot models (o1, GPT-4o, Claude 3.5 Sonnet, Gemini 2.0 Flash) and your own models (OpenAI, Anthropic) via **Bring Your Own Key** mode. +- **🛡️ Physical Workspace Isolation**: Every session runs in its own isolated directory sandbox. This ensures absolute data privacy and prevents cross-chat file contamination while allowing the Agent full filesystem access. +- **🔌 Universal Tool Protocol**: + - **Native MCP**: Direct, high-performance connection to Model Context Protocol servers. + - **OpenAPI Bridge**: Connect to any external REST API as an Agent tool. + - **OpenWebUI Native**: Zero-config bridge to your existing OpenWebUI tools and built-ins (Web Search, Memory, etc.). +- **🧩 OpenWebUI Skills Bridge**: Transforms simple OpenWebUI Markdown instructions into powerful SDK skill folders complete with supporting scripts, templates, and data. +- **♾️ Infinite Session Management**: Advanced context window management with automatic "Compaction" (summarization + list persistence). Carry out weeks-long projects without losing the core thread. +- **📊 Interactive Artifacts & Publishing**: + - **Live HTML/JS**: Instantly render and interact with apps, dashboards, or reports generated by the Agent. + - **Persistent Publishing**: Agents can "publish" generated files (Excel, CSV, docs) to OpenWebUI's file storage, providing permanent download links. +- **🌊 UX-First Streaming**: Full support for "Thinking" processes (Chain of Thought), status indicators, and real-time progress bars for long-running tasks. +- **🧠 Deep Database Integration**: Real-time persistence of TOD·O lists and session metadata ensures your workflow state is always visible in the UI. --- @@ -69,9 +65,14 @@ Administrators define the default behavior for all users in the function setting | Valve | Default | Description | | :--- | :--- | :--- | | `GH_TOKEN` | `""` | Global GitHub Token (Requires 'Copilot Requests' permission). | +| `COPILOTSDK_CONFIG_DIR` | `""` | Persistent directory for SDK config and session state (e.g., `/app/backend/data/.copilot`). | | `ENABLE_OPENWEBUI_TOOLS` | `True` | Enable OpenWebUI Tools (includes defined Tools and Built-in Tools). | | `ENABLE_OPENAPI_SERVER` | `True` | Enable OpenAPI Tool Server connection. | | `ENABLE_MCP_SERVER` | `True` | Enable Direct MCP Client connection (Recommended). | +| `ENABLE_OPENWEBUI_SKILLS` | `True` | Enable bidirectional sync with OpenWebUI Workspace > Skills. | +| `OPENWEBUI_SKILLS_SHARED_DIR` | `/app/backend/data/cache/copilot-openwebui-skills` | Shared cache directory for skills. | +| `GITHUB_SKILLS_SOURCE_URL` | `""` | Optional GitHub tree URL for batch skill import (e.g., anthropic/skills). | +| `DISABLED_SKILLS` | `""` | Comma-separated skill names to disable in SDK session. | | `REASONING_EFFORT` | `medium` | Reasoning effort level: low, medium, high. | | `SHOW_THINKING` | `True` | Show model reasoning/thinking process. | | `INFINITE_SESSION` | `True` | Enable Infinite Sessions (automatic context compaction). | @@ -95,10 +96,64 @@ Standard users can override these settings in their individual Profile/Function | `SHOW_THINKING` | Show model reasoning/thinking process. | | `MAX_MULTIPLIER` | Maximum allowed billing multiplier override. | | `EXCLUDE_KEYWORDS` | Exclude models containing these keywords. | +| `ENABLE_OPENWEBUI_SKILLS` | Enable loading all active OpenWebUI skills readable by you into SDK `SKILL.md` directories. | +| `GITHUB_SKILLS_SOURCE_URL` | Optional GitHub tree URL for batch skill import in your own session. | +| `DISABLED_SKILLS` | Comma-separated skill names to disable for your own session. | | `BYOK_API_KEY` | Use your personal OpenAI/Anthropic API Key. | --- +### 📤 Enhanced Publishing & Interactive Components + +The `publish_file_from_workspace` tool now uses a clearer delivery contract for production use: + +- **Artifacts mode (`artifacts`, default)**: Agent returns `[Preview]` + `[Download]` and may output `html_embed` in a ```html block for direct chat rendering. +- **Rich UI mode (`richui`)**: Agent returns `[Preview]` + `[Download]` only; integrated preview is rendered automatically via emitter (no iframe block in message). +- **📄 PDF delivery safety rule**: Always output Markdown links only (`[Preview]` + `[Download]` when available). **Do not embed PDF via iframe/html blocks.** +- **⚡ Stable dual-channel publishing**: Keeps interactive viewing and persistent file download aligned across local/object-storage backends. +- **✅ Status integration**: Emits real-time publishing progress and completion feedback to the OpenWebUI status bar. +- **📘 Publishing Tool Guide (GitHub)**: [publish_file_from_workspace Guide](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE.md) + +--- + +### 🧩 OpenWebUI Skills Bridge & `manage_skills` Tool + +The SDK now features a bidirectional bridge with the OpenWebUI **Workspace > Skills** page: + +- **🔄 Automatic Sync**: Skills created or updated in the OpenWebUI UI are automatically downloaded as `SKILL.md` folders into the SDK's shared cache on every request. +- **🛠️ `manage_skills` Tool**: The Agent can deterministically manage skills using this tool. + - `list`: List all installed skills and their descriptions. + - `install`: Install a skill from a GitHub URL (auto-normalized to archive link) or a direct `.zip`/`.tar.gz`. + - `create`: Create a new skill directory from context, writing `SKILL.md` and any extra resource files (scripts, templates). + - `edit`: Update an existing skill folder. + - `delete`: Atomically delete both the local directory and the linked OpenWebUI DB entry. +- **📁 Full Folder Support**: Unlike the single-markdown storage in OpenWebUI DB, the SDK loads the **entire folder** for each skill. This allows skills to carry binary scripts, data files, or complex templates alongside the core instructions. +- **🌐 Shared Persistent Cache**: Skills are stored in `OPENWEBUI_SKILLS_SHARED_DIR/shared/`, which is persistent across sessions and container restarts. +- **📚 Full Skill Docs (GitHub)**: [manage_skills Tool Guide](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER.md) | [Skills Best Practices](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES.md) + +--- + +### 🌊 Fluid UX & Granular Status Feedback + +Say goodbye to the "stuck" feeling during complex processing: + +- **🔄 Real-Time Status Bubbles**: Maps internal SDK events (`turn_start`, `compaction`, `subagent_started`) directly to the OpenWebUI status bar. +- **🧭 Richer Stage Descriptions**: Status text now explicitly reflects phases such as processing, skill invocation, tool execution, tool completion/failure, publishing, and final completion. +- **⏱️ Long-Task Heartbeat**: During long waits, the status bar emits periodic "still processing" updates (elapsed-time style) to avoid silent stalls. +- **📈 Tool Progress Tracking**: Long-running tool executions provide live progress percentages and descriptive sub-task updates in the status bar. +- **⚡ Immediate Feedback**: Response starts with an instant "Assistant is processing" status, eliminating idle wait time before the first token. + +--- + +### 🛡️ Smart Version Compatibility + +The plugin automatically adapts its feature set based on your OpenWebUI version: + +- **v0.8.0+**: Rich UI, live status bubbles, and integrated HTML preview. +- **Older**: Automatic fallback to standard Markdown blocks for maximum stability. + +--- + ## 🎯 Use Cases (What can you do?) - **📁 Fully Autonomous DevOps**: Agent analyzes code, runs tests, and applies patches within its isolated sandbox. @@ -141,7 +196,7 @@ If neither is configured, the model list will not appear. ## 📋 Troubleshooting & Dependencies - **Agent ignores files?**: Ensure the Files Filter is enabled, otherwise RAG will interfere with raw binaries. -- **No progress bar?**: The bar only appears when the Agent uses the `update_todo` tool. +- **No status updates?**: Status bubbles are emitted for processing/tool phases; TODO progress bars specifically appear when the Agent uses `update_todo`. - **Dependencies**: This Pipe automatically manages `github-copilot-sdk` (Python) and utilizes the bundled binary CLI. No manual install required. --- diff --git a/plugins/pipes/github-copilot-sdk/README_CN.md b/plugins/pipes/github-copilot-sdk/README_CN.md index 0721ca9..7d1281f 100644 --- a/plugins/pipes/github-copilot-sdk/README_CN.md +++ b/plugins/pipes/github-copilot-sdk/README_CN.md @@ -1,50 +1,43 @@ # GitHub Copilot SDK 官方管道 -**作者:** [Fu-Jie](https://github.com/Fu-Jie/openwebui-extensions) | **版本:** 0.8.0 | **项目:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) | **许可证:** MIT +**作者:** [Fu-Jie](https://github.com/Fu-Jie/openwebui-extensions) | **版本:** 0.9.0 | **项目:** [OpenWebUI Extensions](https://github.com/Fu-Jie/openwebui-extensions) | **许可证:** MIT 这是一个用于 [OpenWebUI](https://github.com/open-webui/open-webui) 的高级 Pipe 函数,深度集成了 **GitHub Copilot SDK**。它不仅支持 **GitHub Copilot 官方模型**(如 `gpt-5.2-codex`, `claude-sonnet-4.5`, `gemini-3-pro`, `gpt-5-mini`),还支持 **BYOK (自带 Key)** 模式对接自定义服务商(OpenAI, Anthropic),并具备**严格的用户与会话级工作区隔离**能力,提供统一且安全的 Agent 交互体验。 > [!IMPORTANT] > **核心伴侣组件** > 如需启用文件处理与数据分析能力,请务必安装 [GitHub Copilot SDK Files Filter](https://openwebui.com/posts/403a62ee-a596-45e7-be65-fab9cc249dd6)。 - > [!TIP] > **BYOK 模式无需订阅** > 如果您使用自带的 API Key (BYOK 模式对接 OpenAI/Anthropic),**您不需要 GitHub Copilot 官方订阅**。只有在访问 GitHub 官方模型时才需要订阅。 --- -## ✨ 0.8.0 更新内容 (What's New) +## ✨ 0.9.0 核心更新:技能革命与稳定性加固 -- **🎛️ 条件工具过滤 (P1~P4)**: 四优先级工具权限体系。**默认全开**: 若未在 Chat UI (P4) 勾选任何工具,则默认启用所有工具;**白名单模式**: 一旦勾选特定工具,即刻进入严格过滤模式,且 MCP server 同步受控;管理员亦可通过 `config.enable` (P2) 全局禁用工具服务器。(v0.8.0) -- **🔧 文件发布全面修复**: 通过在回退路径直接调用 `Storage.upload_file()`,彻底修复了所有存储后端(local/S3/GCS/Azure)下的 `Error getting file content` 问题;同时上传时自动携带 `?process=false`,HTML 文件不再被 `ALLOWED_FILE_EXTENSIONS` 拦截。(v0.8.0) -- **🌐 HTML 直达链接**: 当 `publish_file_from_workspace` 发布的是 HTML 文件时,插件会额外提供可直接访问的 HTML 链接,便于在聊天中即时预览/打开。(v0.8.0) -- **🔒 文件链接格式严格约束**: 发布链接必须是以 `/api/v1/files/` 开头的相对路径(例如 `/api/v1/files/{id}/content/html`)。禁止使用 `api/...`,也禁止拼接任何域名。(v0.8.0) -- **🛠️ CLI 内置工具始终可用**: `available_tools` 统一设为 `None`,Copilot CLI 内置工具(如 `bash`、`create_file`)无论 MCP 配置如何都不会被静默屏蔽。(v0.8.0) -- **📌 发布工具始终注入**: 即使 `ENABLE_OPENWEBUI_TOOLS` 关闭,`publish_file_from_workspace` 工具也不再丢失。(v0.8.0) -- **⚠️ 代码解释器限制**: `code_interpreter` 工具运行在远程临时环境中。系统提示词现已包含警告,明确指出该工具无法访问本地文件或持久化更改。(v0.8.0) - -### 🐞 v0.8.0 Bug 修复说明 - -- 修复了对象存储后端发布文件时出现的 `{"detail":"[ERROR: Error getting file content]"}`,回退路径从手动复制/写库改为 `Storage.upload_file()`。 -- 修复了 HTML 产物被 `ALLOWED_FILE_EXTENSIONS` 拦截的问题,上传接口统一追加 `?process=false`。 -- 修复了产物链接偶发被生成成 `api/...` 或带域名绝对 URL 的问题,现统一限制为 `/api/v1/files/...` 相对路径。 -- 修复了在未配置/未加载任何 server 工具时(最终出现 `available_tools=[]`)Copilot CLI 内置工具被静默禁用的问题,现统一保持 `available_tools=None`。 -- 修复了 `ENABLE_OPENWEBUI_TOOLS` 关闭时 `publish_file_from_workspace` 工具丢失的问题。 +- **🧩 Copilot SDK Skills 原生支持**: 技能可作为一等上下文能力被加载和使用。 +- **🔄 OpenWebUI Skills 桥接**: 实现 OpenWebUI **工作区 > Skills** 与 SDK 技能目录的深度双向同步。 +- **🛠️ 确定性 `manage_skills` 工具**: 通过稳定工具契约完成技能的生命周期管理。 +- **🌊 状态栏逻辑加固**: 引入 `session_finalized` 多层锁定机制,彻底解决任务完成后状态栏回弹或卡死的问题。 +- **🗂️ 环境目录持久化**: 增强 `COPILOTSDK_CONFIG_DIR` 逻辑,确保会话状态跨容器重启稳定存在。 --- ## ✨ 核心能力 (Key Capabilities) -- **🔑 灵活鉴权与 BYOK**: 支持 GitHub Copilot 官方订阅 (PAT) 或自带 Key (OpenAI/Anthropic)。 -- **🔌 通用工具协议**: 原生支持 **MCP (Model Context Protocol)**、OpenAPI 以及 OpenWebUI 内置工具。 -- **🛡️ 物理级工作区隔离**: 强制执行严格的用户特定沙箱,确保数据隐私与文件安全。 -- **♾️ 无限会话管理**: 智能上下文窗口管理与自动压缩算法,支持无限时长的对话交互。 -- **🧠 深度数据库集成**: 实时持久化 TOD·O 列表到 UI 进度条。 -- **🌊 深度推理展示**: 完整支持模型思考过程 (Thinking Process) 的流式渲染。 -- **🖼️ 智能多模态**: 完整支持图像识别与附件上传分析(绕过 RAG 直接访问原始二进制内容)。 -- **📤 工作区产物工具 (`publish_file_from_workspace`)**: Agent 可生成文件(Excel、CSV、HTML 报告等)并直接在聊天中提供**持久化下载链接**。若为 HTML 文件,还会额外提供可直接访问的 HTML 链接。 -- **🖼️ 交互式伪影 (Artifacts)**: 自动渲染 Agent 生成的 HTML/JS 应用程序,直接在聊天界面交互。 +- **🔑 统一智能体验 (官方 + BYOK)**: 自由切换官方模型(o1, GPT-4o, Claude 3.5 Sonnet, Gemini 2.0 Flash)与自定义服务商(OpenAI, Anthropic),支持 **BYOK (自带 Key)** 模式。 +- **🛡️ 物理级工作区隔离**: 每个会话在独立的沙箱目录中运行。确保绝对的数据隐私,防止不同聊天间的文件污染,同时给予 Agent 完整的文件系统操作权限。 +- **🔌 通用工具协议**: + - **原生 MCP**: 高性能直连 Model Context Protocol 服务器。 + - **OpenAPI 桥接**: 将任何外部 REST API 一键转换为 Agent 可调用的工具。 + - **OpenWebUI 原生桥接**: 零配置接入现有的 OpenWebUI 工具及内置功能(网页搜索、记忆等)。 +- **🧩 OpenWebUI Skills 桥接**: 将简单的 OpenWebUI Markdown 指令转化为包含脚本、模板和数据的强大 SDK 技能文件夹。 +- **♾️ 无限会话管理**: 先进的上下文窗口管理,支持自动“压缩”(摘要提取 + TODO 列表持久化)。支持长达数周的项目跟踪而不会丢失核心上下文。 +- **📊 交互式产物与发布**: + - **实时 HTML/JS**: 瞬间渲染并交互 Agent 生成的应用程序、可视化看板或报告。 + - **持久化发布**: Agent 可将生成的产物(Excel, CSV, 文档)发布至 OpenWebUI 文件存储,并在聊天中提供永久下载链接。 +- **🌊 极致交互体验**: 完整支持深度思考过程 (Thinking Process) 流式渲染、状态指示器以及长任务实时进度条。 +- **🧠 深度数据库集成**: TOD·O 列表与会话元数据的实时持久化,确保任务执行状态在 UI 上清晰可见。 --- @@ -69,9 +62,14 @@ | 参数 | 默认值 | 说明 | | :--- | :--- | :--- | | `GH_TOKEN` | `""` | 全局 GitHub Token (需具备 'Copilot Requests' 权限)。 | +| `COPILOTSDK_CONFIG_DIR` | `""` | SDK 配置与会话状态持久化目录 (例如: `/app/backend/data/.copilot`)。 | | `ENABLE_OPENWEBUI_TOOLS` | `True` | 启用 OpenWebUI 工具 (包括定义工具和内置工具)。 | | `ENABLE_OPENAPI_SERVER` | `True` | 启用 OpenAPI 工具服务器连接。 | | `ENABLE_MCP_SERVER` | `True` | 启用直接 MCP 客户端连接 (推荐)。 | +| `ENABLE_OPENWEBUI_SKILLS` | `True` | 开启与 OpenWebUI **工作区 > Skills** 的双向同步桥接。 | +| `OPENWEBUI_SKILLS_SHARED_DIR` | `/app/backend/data/cache/copilot-openwebui-skills` | OpenWebUI skills 转换后的共享缓存目录。 | +| `GITHUB_SKILLS_SOURCE_URL` | `""` | 可选 GitHub tree 地址,用于批量导入 skills(例如 anthropic/skills)。 | +| `DISABLED_SKILLS` | `""` | 逗号分隔的 skill 名称黑名单(如 `docs-writer,webapp-testing`)。 | | `REASONING_EFFORT` | `medium` | 推理强度:low, medium, high。 | | `SHOW_THINKING` | `True` | 显示模型推理/思考过程。 | | `INFINITE_SESSION` | `True` | 启用无限会话 (自动上下文压缩)。 | @@ -91,14 +89,38 @@ | 参数 | 说明 | | :--- | :--- | | `GH_TOKEN` | 使用个人的 GitHub Token。 | -| `REASONING_EFFORT`| 个人偏好的推理强度。 | +| `REASONING_EFFORT` | 个人偏好的推理强度。 | | `SHOW_THINKING` | 显示模型推理/思考过程。 | | `MAX_MULTIPLIER` | 最大允许的模型计费倍率覆盖。 | | `EXCLUDE_KEYWORDS` | 排除包含这些关键字的模型。 | +| `ENABLE_OPENWEBUI_SKILLS` | 启用将当前用户可读的全部已启用 OpenWebUI skills 转换并加载为 SDK `SKILL.md` 目录。 | +| `GITHUB_SKILLS_SOURCE_URL` | 为当前用户会话设置可选 GitHub tree 地址以批量导入 skills。 | +| `DISABLED_SKILLS` | 为当前用户会话禁用指定 skills(逗号分隔)。 | | `BYOK_API_KEY` | 使用个人的 OpenAI/Anthropic API Key。 | --- +### 🌊 细粒度反馈与流畅体验 (Fluid UX) + +彻底告别复杂任务执行过程中的“卡顿”感: + +- **🔄 实时状态气泡**: 将 SDK 内部事件(如 `turn_start`, `compaction`, `subagent_started`)直接映射为 OpenWebUI 的状态栏信息。 +- **🧭 分阶段状态描述增强**: 状态栏会明确显示处理阶段(处理中、技能触发、工具执行、工具完成/失败、发布中、任务完成)。 +- **⏱️ 长任务心跳提示**: 长时间处理中会周期性显示“仍在处理中(已耗时 X 秒)”,避免用户误判为卡死。 +- **📈 工具执行进度追踪**: 长耗时工具(如代码分析)会在状态栏实时显示进度百分比及当前子任务描述。 +- **⚡ 即时响应反馈**: 从响应开始第一秒即显示“助手正在处理您的请求...”,减少等待空窗感。 + +--- + +### 🛡️ 智能版本兼容 + +插件会自动根据您的 OpenWebUI 版本调整功能集: + +- **v0.8.0+**: 开启 Rich UI、实时状态气泡及集成 HTML 预览。 +- **旧版本**: 自动回退至标准 Markdown 代码块模式,确保最大稳定性。 + +--- + ## 🎯 典型应用场景 (Use Cases) - **📁 全自主仓库维护**: Agent 在隔离工作区内自动分析代码、运行测试并应用补丁。 @@ -142,10 +164,40 @@ --- +### 📤 增强型发布工具与交互式组件 + +`publish_file_from_workspace` 现采用更清晰、可落地的交付规范: + +- **Artifacts 模式(`artifacts`,默认)**:返回 `[Preview]` + `[Download]`,并可附带 `html_embed`,在 ```html 代码块中直接渲染。 +- **Rich UI 模式(`richui`)**:仅返回 `[Preview]` + `[Download]`,由发射器自动触发集成式预览(消息中不输出 iframe 代码块)。 +- **📄 PDF 安全交付规则**:仅输出 Markdown 链接(可用时为 `[Preview]` + `[Download]`)。**禁止通过 iframe/html 方式嵌入 PDF。** +- **⚡ 稳定双通道发布**:在本地与对象存储后端下,保持交互预览与持久下载链接一致可用。 +- **✅ 状态集成**:通过 OpenWebUI 状态栏实时反馈发布进度与完成状态。 +- **📘 发布工具指南(GitHub)**:[publish_file_from_workspace 工具指南(中文)](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/PUBLISH_FILE_FROM_WORKSPACE_CN.md) + +--- + +### 🧩 OpenWebUI Skills 桥接与 `manage_skills` 工具 + +SDK 现在具备与 OpenWebUI **工作区 > Skills** 的双向同步能力: + +- **🔄 自动同步**: 每次请求时,前端定义的技能会自动作为 `SKILL.md` 文件夹同步至 SDK 共享缓存,Agent 可直接调用。 +- **🛠️ `manage_skills` 工具**: 内置专业工具,赋予 Agent (或用户) 绝对的技能管理权。 + - `list`: 列出所有已安装技能及描述。 + - `install`: 从 GitHub URL (自动转换归档链接) 或直接从 `.zip`/`.tar.gz` 安装。 + - `create`: 从当前会话内容创建新技能目录,支持写入 `SKILL.md` 及辅助资源文件 (脚本、模板)。 + - `edit`: 更新现有技能文件夹。 + - `delete`: 原子化删除本地目录及关联的数据库条目,防止僵尸技能复活。 +- **📁 完整的文件夹支持**: 不同于数据库中单文件存储,SDK 会加载技能的**整个目录**。这使得技能可以携带二进制脚本、数据文件或复杂模板。 +- **🌐 持久化共享缓存**: 技能存储在 `OPENWEBUI_SKILLS_SHARED_DIR/shared/`,跨会话及容器重启持久存在。 +- **📚 技能完整文档(GitHub)**: [manage_skills 工具指南(中文)](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER_CN.md) | [Skills Best Practices(中文)](https://github.com/Fu-Jie/openwebui-extensions/blob/main/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES_CN.md) + +--- + ## 📋 常见问题与依赖 (Troubleshooting) - **Agent 无法识别文件?**: 请确保已安装并启用了 Files Filter 插件,否则原始文件会被 RAG 干扰。 -- **看不到 TODO 进度条?**: 进度条仅在 Agent 使用 `update_todo` 工具(通常是处理复杂任务)时出现。 +- **看不到状态更新或 TODO 进度条?**: 状态气泡会覆盖处理/工具阶段;而 TODO 进度条仅在 Agent 使用 `update_todo` 工具(通常是复杂任务)时出现。 - **依赖安装**: 本管道会自动管理 `github-copilot-sdk` (Python 包) 并优先直接使用内置的二进制 CLI,无需手动干预。 --- diff --git a/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES.md b/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES.md new file mode 100644 index 0000000..c84447b --- /dev/null +++ b/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES.md @@ -0,0 +1,209 @@ +# Skills Best Practices + +A concise guide to writing, organizing, and maintaining Copilot SDK skills effectively. + +--- + +## Understanding How Skills Work + +Skills are **not command-line tools**. They are context-injected instruction sets: + +1. The Copilot SDK daemon reads every `SKILL.md` file from your `skill_directories` +2. It extracts the YAML `description` field from each skill +3. When the user sends a message, the SDK compares intent against all descriptions +4. If a match is found, the SDK fires `skill.invoked` and **injects the full SKILL.md body** into the conversation as instructions +5. The agent reads those instructions and executes them using `bash`, `create_file`, `view_file`, etc. + +**Key implication**: never run a skill's name as a bash command (e.g., `finance-reporting`). The skill IS the instructions, not an executable. + +--- + +## Writing a Good `description` Field + +The `description` in `SKILL.md` frontmatter is the **primary trigger mechanism**. The SDK uses it like a semantic router. + +### Do ✅ + +- Start with a verb: "Manage…", "Generate…", "Analyze…" +- Include explicit "Use when:" scenarios — this is the most reliable trigger signal +- Cover all the intent variations a user might express + +```yaml +description: Generate a PowerPoint presentation from an outline or topic. + Use when: creating slides, building a deck, making a presentation, exporting to PPTX. +``` + +### Don't ❌ + +- Vague descriptions: "A useful skill for various things" +- Overlapping descriptions with other skills (causes misfires) +- Omitting "Use when:" examples (reduces trigger reliability significantly) + +### Rule of Thumb + +If two people would phrase the same request differently (e.g., "make slides" vs. "create a deck"), both phrasings should appear somewhere in the description. + +--- + +## Structure: What Goes Where + +``` +skill-name/ +├── SKILL.md ← Required. Frontmatter + core instructions +├── .owui_id ← Auto-generated. DO NOT edit or delete +├── references/ ← Optional. Supplementary docs, loaded on demand +│ └── advanced.md +├── scripts/ ← Optional. Helper shell/Python scripts +└── assets/ ← Optional. Templates, sample files, static data +``` + +### When to Use `references/` + +Put content in `references/` when it is: + +- Only needed for edge cases or advanced usage +- Too long to read every time (> ~100 lines) +- Reference material (API specs, format docs, examples) + +Use progressive disclosure: the agent reads `SKILL.md` first, then loads a specific reference file only when the task requires it. + +```markdown +## Advanced Export Options + +See [references/export-options.md](references/export-options.md) for the full list. +``` + +### When to Inline in `SKILL.md` + +Keep content in `SKILL.md` when it is: + +- Needed for every run of the skill +- Short enough not to slow down context injection (< ~150 lines total) +- Core to the skill's main workflow + +--- + +## Naming Conventions + +| Item | Convention | Example | +|---|---|---| +| Skill directory name | `kebab-case` | `export-to-pptx` | +| `name` field in frontmatter | `kebab-case`, matches dir name | `export-to-pptx` | +| Script filenames | `snake_case.py` or `snake_case.sh` | `build_slide.py` | +| Reference filenames | `kebab-case.md` | `advanced-options.md` | + +Avoid spaces and uppercase in skill directory names — the SDK uses the directory name as the skill identifier. + +--- + +## Writing Effective SKILL.md Instructions + +### Open With a Role Statement + +Tell the agent who it is in this skill context: + +```markdown +# Export to PowerPoint + +You are a presentation builder. Your job is to convert the user's content into a well-structured PPTX file using the scripts in this skill directory. +``` + +### Use Imperative Steps + +Write instructions as numbered steps, not prose: + +```markdown +1. Ask the user for the outline if not provided. +2. Run `python3 {scripts_dir}/build_slide.py --title "..." --output "{cwd}/output.pptx"` +3. Confirm success by checking the file exists. +4. Provide the user with the download path. +``` + +### Handle Errors Explicitly + +Tell the agent what to do when things go wrong: + +```markdown +If the script exits with a non-zero code, show the user the stderr output and ask how to proceed. +``` + +### End With a Closing Instruction + +```markdown +After completing the task, summarize what was created and remind the user where to find the file. +``` + +--- + +## Skill Scope + +Each skill should do **one thing well**. Signs a skill is too broad: + +- The description has more than 4–5 "Use when:" entries covering unrelated domains +- The SKILL.md is > 300 lines +- You've added more than 3 reference files + +When a skill grows too large, split it: one parent skill for routing + separate child skills per major function. + +--- + +## Managing the `shared/` Directory + +The `shared/` directory is **bidirectionally synced** with the OpenWebUI database: + +- Skills created via the OpenWebUI UI are automatically imported into `shared/` +- Skills created by the agent in `shared/` are exported back to OpenWebUI at session start + +### Safe operations + +| Operation | Method | +|---|---| +| Install from URL | `python3 {scripts_dir}/install_skill.py --url --dest {shared_dir}` | +| Create new skill | `mkdir -p {shared_dir}// && create SKILL.md` | +| Edit skill | Read → modify → write `SKILL.md` | +| Delete skill | `rm -rf {shared_dir}//` (does NOT delete from OpenWebUI UI — do that separately) | +| List skills | `python3 {scripts_dir}/list_skills.py --path {shared_dir}` | + +### The `.owui_id` file + +Every skill synced with OpenWebUI has a `.owui_id` file containing the database UUID. **Never edit or delete this file** — it is the link between the filesystem and OpenWebUI DB. If deleted, the skill will be treated as new on next sync and may create a duplicate. + +--- + +## Session Lifecycle Awareness + +Skills are loaded **once at session start**. Changes made during a session take effect in the **next session**. + +| When | What happens | +|---|---| +| Session starts | SDK daemon reads all `SKILL.md` files; `_sync_openwebui_skills` runs bidirectional DB↔file sync | +| During a session | New/edited/deleted skill files exist on disk but are NOT yet loaded by the daemon | +| After user starts new session | New skills become available; edited descriptions take effect | + +**Always tell the user** after any create/edit/delete: "This change will take effect when you start a new session." + +--- + +## Anti-Patterns to Avoid + +| Anti-pattern | Why it's bad | Fix | +|---|---|---| +| Running `` as a bash command | Skills are not executables | Read the SKILL.md instructions and act with standard tools | +| Editing `.owui_id` | Breaks DB sync | Never touch it | +| Storing per-session state in `SKILL.md` | SKILL.md is static instructions, not a state file | Use separate workspace files for session state | +| Ultra-broad skill descriptions | Causes false positives on every message | Narrow to specific intent with "Use when:" | +| Putting all logic in one 500-line SKILL.md | Slow context injection, hard to maintain | Split into SKILL.md + `references/*.md` | +| Creating skills in `/tmp` | Not persisted, not found by SDK | Always create in `{shared_dir}/` | + +--- + +## Quick Checklist for a New Skill + +- [ ] Directory name is `kebab-case` and matches the `name` field +- [ ] `description` starts with a verb and has "Use when:" examples +- [ ] SKILL.md opens with a role statement for the agent +- [ ] Instructions use imperative numbered steps +- [ ] Long reference content moved to `references/` +- [ ] Scripts placed in `scripts/` +- [ ] Confirmed: skill does NOT overlap in description with other loaded skills +- [ ] User informed: "new skill takes effect next session" diff --git a/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES_CN.md b/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES_CN.md new file mode 100644 index 0000000..14dda1b --- /dev/null +++ b/plugins/pipes/github-copilot-sdk/SKILLS_BEST_PRACTICES_CN.md @@ -0,0 +1,209 @@ +# Skills 使用最佳实践 + +编写、组织和维护 Copilot SDK Skills 的简明指南。 + +--- + +## 理解 Skills 的工作机制 + +Skills **不是命令行工具**,而是**上下文注入的指令集**: + +1. Copilot SDK 守护进程读取 `skill_directories` 中每个 `SKILL.md` 文件 +2. 提取每个 skill 的 YAML `description` 字段 +3. 用户发消息时,SDK 将用户意图与所有 description 进行语义匹配 +4. 匹配成功后,SDK 触发 `skill.invoked` 事件,将完整的 **SKILL.md 正文注入对话上下文** +5. Agent 读取这些指令,使用 `bash`、`create_file`、`view_file` 等工具来执行 + +**关键理解**:永远不要把 skill 的名字当作 bash 命令来运行(例如 `finance-reporting`)。Skill 本身就是指令,而不是可执行文件。 + +--- + +## 写好 `description` 字段 + +`SKILL.md` frontmatter 中的 `description` 是 **主要触发机制**,SDK 用它做语义路由。 + +### 应该这样做 ✅ + +- 以动词开头:"管理…"、"生成…"、"分析…" +- 明确写出 "Use when:" 场景——这是最可靠的触发信号 +- 覆盖用户可能表达同一需求的多种说法 + +```yaml +description: 根据大纲或主题生成 PowerPoint 演示文稿。 + Use when: 创建幻灯片、制作演讲稿、导出 PPTX 文件、做 PPT。 +``` + +### 不要这样做 ❌ + +- 模糊描述:"一个有用的多功能工具" +- 与其他 skill 描述重叠(会造成误触发) +- 省略 "Use when:" 示例(大幅降低触发准确率) + +### 实用经验 + +如果两个人会用不同方式表达同一需求(如"做个幻灯片"vs"制作一个演讲 deck"),两种说法都应该出现在 description 中。 + +--- + +## 目录结构:什么放在哪里 + +``` +skill-name/ +├── SKILL.md ← 必须。Frontmatter + 核心指令 +├── .owui_id ← 自动生成,禁止编辑或删除 +├── references/ ← 可选。补充文档,按需加载 +│ └── advanced.md +├── scripts/ ← 可选。辅助脚本(Shell/Python) +└── assets/ ← 可选。模板、样例文件、静态数据 +``` + +### 何时用 `references/` + +当内容属于以下情况时放入 `references/`: + +- 仅边缘场景或高级用法才需要 +- 内容太长,每次都注入会浪费上下文(> 约 100 行) +- 纯参考资料(API 规格、格式文档、示例) + +使用"渐进式披露":Agent 先读 `SKILL.md`,仅在任务需要时才加载特定 reference 文件: + +```markdown +## 高级导出选项 + +详见 [references/export-options.md](references/export-options.md)。 +``` + +### 何时内联在 `SKILL.md` + +当内容属于以下情况时留在 `SKILL.md`: + +- 每次运行 skill 都需要 +- 足够短,不会拖慢上下文注入(总计 < 约 150 行) +- 是 skill 主流程的核心内容 + +--- + +## 命名约定 + +| 内容 | 规范 | 示例 | +|---|---|---| +| Skill 目录名 | `kebab-case` | `export-to-pptx` | +| Frontmatter `name` 字段 | `kebab-case`,与目录名一致 | `export-to-pptx` | +| 脚本文件名 | `snake_case.py` 或 `.sh` | `build_slide.py` | +| Reference 文件名 | `kebab-case.md` | `advanced-options.md` | + +Skill 目录名避免空格和大写字母——SDK 使用目录名作为 skill 标识符。 + +--- + +## 编写高效的 SKILL.md 指令 + +### 以角色声明开头 + +告诉 Agent 在这个 skill 上下文中扮演什么角色: + +```markdown +# 导出为 PowerPoint + +你是一个演示文稿构建器。你的任务是使用本 skill 目录中的脚本,将用户的内容转换为结构清晰的 PPTX 文件。 +``` + +### 使用祈使句步骤 + +用编号步骤写指令,而不是大段散文: + +```markdown +1. 如果用户未提供大纲,先询问。 +2. 运行 `python3 {scripts_dir}/build_slide.py --title "..." --output "{cwd}/output.pptx"` +3. 检查文件是否存在,确认成功。 +4. 向用户提供文件的下载路径。 +``` + +### 明确处理错误 + +告诉 Agent 出错时怎么做: + +```markdown +如果脚本以非零状态码退出,将 stderr 输出展示给用户并询问如何处理。 +``` + +### 以收尾指令结束 + +```markdown +任务完成后,总结创建的内容,并提醒用户文件的存放位置。 +``` + +--- + +## Skill 的适用范围 + +每个 skill 应该**只做一件事**。以下迹象说明 skill 太宽泛了: + +- description 包含 4–5 个以上涵盖不同领域的 "Use when:" 条目 +- SKILL.md 超过 300 行 +- 已添加超过 3 个 reference 文件 + +当 skill 变得过大时,进行拆分:一个父 skill 负责路由,各子 skill 负责具体功能。 + +--- + +## 管理 `shared/` 目录 + +`shared/` 目录与 OpenWebUI 数据库**双向同步**: + +- 通过 OpenWebUI UI 创建的 skill 会自动导入 `shared/` +- Agent 在 `shared/` 中创建的 skill 在下次会话启动时导出到 OpenWebUI + +### 安全操作方式 + +| 操作 | 方法 | +|---|---| +| 从 URL 安装 | `python3 {scripts_dir}/install_skill.py --url --dest {shared_dir}` | +| 新建 skill | `mkdir -p {shared_dir}// && 创建 SKILL.md` | +| 编辑 skill | 读取 → 修改 → 写回 `SKILL.md` | +| 删除 skill | `rm -rf {shared_dir}//`(不会删除 OpenWebUI UI 中的记录,需单独删除) | +| 列出 skills | `python3 {scripts_dir}/list_skills.py --path {shared_dir}` | + +### `.owui_id` 文件 + +每个与 OpenWebUI 同步的 skill 都有一个 `.owui_id` 文件,里面存储数据库 UUID。**绝对不要编辑或删除此文件**——它是文件系统与 OpenWebUI 数据库之间的关联纽带。一旦删除,下次同步时该 skill 会被视为新建项,可能产生重复。 + +--- + +## 会话生命周期意识 + +Skills 在**会话开始时加载一次**。在会话期间做的修改,**下次会话才会生效**。 + +| 时间点 | 发生的事 | +|---|---| +| 会话启动 | SDK 守护进程读取所有 `SKILL.md`;`_sync_openwebui_skills` 执行双向 DB↔文件同步 | +| 会话期间 | 新建/编辑/删除的 skill 文件已在磁盘上,但守护进程尚未加载 | +| 用户开启新会话 | 新 skill 生效;修改后的 description 开始触发 | + +**每次创建/编辑/删除后,必须告知用户**:"此更改将在您开启新会话后生效。" + +--- + +## 需要避免的反模式 + +| 反模式 | 问题所在 | 解决方式 | +|---|---|---| +| 把 `` 当 bash 命令运行 | Skill 不是可执行文件 | 阅读 SKILL.md 指令,用标准工具执行 | +| 编辑 `.owui_id` | 破坏数据库同步 | 永远不要碰这个文件 | +| 在 SKILL.md 中存储会话状态 | SKILL.md 是静态指令,不是状态文件 | 使用工作区中的独立文件保存会话状态 | +| description 过于宽泛 | 对每条消息都误触发 | 用 "Use when:" 缩窄到具体意图 | +| 把所有逻辑写进一个 500 行的 SKILL.md | 上下文注入慢,难以维护 | 拆分为 SKILL.md + `references/*.md` | +| 在 `/tmp` 创建 skill | 不持久,SDK 找不到 | 始终在 `{shared_dir}/` 中创建 | + +--- + +## 新建 Skill 快速检查清单 + +- [ ] 目录名为 `kebab-case`,与 `name` 字段一致 +- [ ] `description` 以动词开头,包含 "Use when:" 示例 +- [ ] SKILL.md 以角色声明开头 +- [ ] 指令使用祈使句编号步骤 +- [ ] 过长的参考内容已移至 `references/` +- [ ] 脚本已放入 `scripts/` +- [ ] 确认:description 与其他已加载 skill 无重叠 +- [ ] 已告知用户:"新 skill 在下次会话后生效" diff --git a/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER.md b/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER.md new file mode 100644 index 0000000..729ebd1 --- /dev/null +++ b/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER.md @@ -0,0 +1,128 @@ +# manage_skills Tool Guide + +This document describes the `manage_skills` **tool** in GitHub Copilot SDK Pipe. + +> Important: `manage_skills` is a tool, not a skill. + +--- + +## Core Model + +The plugin uses **one** install/sync location for skills: + +- `OPENWEBUI_SKILLS_SHARED_DIR/shared/` + +There is no separate install target for "manager skill" or per-workspace skill buckets. + +--- + +## Skill Directory Layout + +All skills live under the same directory: + +```text +{OPENWEBUI_SKILLS_SHARED_DIR}/shared/ +├── finance-reporting/ +│ ├── SKILL.md +│ ├── .owui_id +│ ├── scripts/ +│ └── templates/ +├── docs-writer/ +│ ├── SKILL.md +│ └── .owui_id +└── ... +``` + +- `SKILL.md` is required. +- `.owui_id` links local folder to OpenWebUI DB record. +- Extra files (`scripts/`, `templates/`, `references/`) are optional resources. + +--- + +## What `manage_skills` Does + +`manage_skills` provides deterministic skill lifecycle operations: + +- `list` +- `install` +- `create` +- `edit` +- `show` +- `delete` + +Use this tool for all skill CRUD operations instead of ad-hoc shell workflows. + +--- + +## Sync Mechanism (Local Files ↔ OpenWebUI DB) + +The SDK performs **real-time bidirectional sync** between the local filesystem and the OpenWebUI database to ensure consistency. + +### How it works + +1. **Identity Link**: Each local skill folder contains a hidden `.owui_id` file. This is the "glue" that links the folder to a specific record in the OpenWebUI database. +2. **Conflict Resolution**: + - **Content Hash**: The SDK first compares the MD5 hash of the local `SKILL.md` content against the DB record. If they match, no sync occurs. + - **Timestamp Check**: If content differs, it compares the file's `mtime` with the database's `updated_at`. The newer version wins. +3. **Operation Sync**: + - **Manual Edit (Filesystem)**: If you edit `SKILL.md` via VS Code or terminal, the next SDK request will push those changes to the OpenWebUI UI. + - **UI Edit (OpenWebUI)**: If you update instructions in the OpenWebUI workspace, the SDK will pull those changes and overwrite the local `SKILL.md`. + - **Tool Actions**: Actions like `manage_skills(action="create")` or `action="delete"` trigger an immediate atomic sync to the database. + +> **Warning**: Do not manually delete the `.owui_id` file unless you want to "unlink" the skill and force the SDK to re-register it as a new entry. + +--- + +## Typical Flows (Example Queries) + +### 1. Install Skill from GitHub URL + +**User Query:** "Help me install the data-visualizer skill from `https://github.com/user/skills/blob/main/data-visualizer/SKILL.md`" +**Tool Call:** `manage_skills(action="install", url="https://github.com/user/skills/blob/main/data-visualizer/SKILL.md")` +**Result:** + +- Files downloaded to `{OPENWEBUI_SKILLS_SHARED_DIR}/shared/data-visualizer/` +- Skill metadata automatically synced to OpenWebUI Database. + +### 2. Install Multiple Skills from Different URLs at Once + +**User Query:** "Install these three skills: URL1, URL2, URL3" +**Tool Call:** `manage_skills(action="install", url=["URL1", "URL2", "URL3"])` +**Result:** + +- Each URL is downloaded, extracted, and installed sequentially into `shared/`. +- A single DB sync runs after all installs complete. +- If one URL fails, the others still proceed. Failed URLs are listed in `errors`. + +### 3. Install All Skills from One Repository + +**User Query:** "Install everything under `https://github.com/myorg/skill-pack/tree/main/`" +**Tool Call:** `manage_skills(action="install", url="https://github.com/myorg/skill-pack/tree/main/")` +**Result:** + +- All subdirectories containing a `SKILL.md` are discovered and installed in one shot. + +### 4. Create Skill from Current Conversation + +**User Query:** "Remember the Python cleanup logic we just discussed as a new skill called 'py-clean'" +**Tool Call:** `manage_skills(action="create", name="py-clean", content="...")` +**Result:** + +- New directory `{OPENWEBUI_SKILLS_SHARED_DIR}/shared/py-clean/` created. +- `SKILL.md` written and synced to Database. + +--- + +## Recommended Settings + +- `ENABLE_OPENWEBUI_SKILLS=True` +- `OPENWEBUI_SKILLS_SHARED_DIR=/app/backend/data/cache/copilot-openwebui-skills` +- Optional blacklist: `DISABLED_SKILLS=skill-a,skill-b` + +--- + +## Notes + +- Do not run skill names as shell commands. +- Use `manage_skills` for lifecycle control. +- Keep all installed skills in one directory: `.../shared/`. diff --git a/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER_CN.md b/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER_CN.md new file mode 100644 index 0000000..1a81852 --- /dev/null +++ b/plugins/pipes/github-copilot-sdk/SKILLS_MANAGER_CN.md @@ -0,0 +1,128 @@ +# manage_skills 工具指南 + +本文档说明 GitHub Copilot SDK Pipe 中的 `manage_skills` **工具**。 + +> 重点:`manage_skills` 是工具(tool),不是 skill。 + +--- + +## 核心模型 + +插件只使用**一个** skill 安装/同步目录: + +- `OPENWEBUI_SKILLS_SHARED_DIR/shared/` + +不存在额外的“manager skill 目录”或按工作区分裂的安装目录。 + +--- + +## Skill 目录结构 + +所有 skills 统一放在同一个目录下: + +```text +{OPENWEBUI_SKILLS_SHARED_DIR}/shared/ +├── finance-reporting/ +│ ├── SKILL.md +│ ├── .owui_id +│ ├── scripts/ +│ └── templates/ +├── docs-writer/ +│ ├── SKILL.md +│ └── .owui_id +└── ... +``` + +- `SKILL.md` 为必需文件。 +- `.owui_id` 用于关联 OpenWebUI 数据库记录。 +- `scripts/`、`templates/`、`references/` 等为可选资源文件。 + +--- + +## `manage_skills` 可以做什么 + +`manage_skills` 提供确定性的 skill 生命周期操作: + +- `list` +- `install` +- `create` +- `edit` +- `show` +- `delete` + +建议将 skill 的增删改查统一通过该工具完成,避免脆弱的临时 shell 流程。 + +--- + +## 同步机制 (本地文件 ↔ OpenWebUI 数据库) + +SDK 在本地文件系统与 OpenWebUI 数据库之间执行**实时双向同步**,以确保一致性。 + +### 工作原理 + +1. **身份绑定**:每个本地 skill 文件夹内包含一个隐藏的 `.owui_id` 文件。它是将文件夹链接到 OpenWebUI 数据库中特定记录的“粘合剂”。 +2. **冲突处理**: + - **内容哈希**:SDK 首先比较本地 `SKILL.md` 与数据库中指令的内容哈希 (MD5)。若一致,则不执行同步。 + - **时间戳校验**:若内容不同,则比较文件的 `mtime` 与数据库的 `updated_at`。时间较新的一方将覆盖另一方。 +3. **操作同步场景**: + - **手动编辑 (文件系统)**:若你通过 VS Code 或终端修改了 `SKILL.md`,下次 SDK 请求时会自动将改动推送到 OpenWebUI 前端。 + - **界面编辑 (OpenWebUI)**:若你在 OpenWebUI 工作区中修改了指令内容,SDK 会拉取变更并覆写本地的 `SKILL.md`。 + - **工具操作**:调用 `manage_skills(action="create")` 或 `action="delete"` 会立即触发与数据库的原子同步。 + +> **警告**:除非你想“取消关联”并强制 SDK 将该技能注册为新条目,否则请勿手动删除 `.owui_id` 文件。 + +--- + +## 典型工作流 (典型问题示例) + +### 1. 从 GitHub URL 安装 Skill + +**用户提问:** "帮我安装这个数据分析 skill:`https://github.com/user/skills/blob/main/data-visualizer/SKILL.md`" +**工具调用:** `manage_skills(action="install", url="https://github.com/user/skills/blob/main/data-visualizer/SKILL.md")` +**结果:** + +- 文件下载至 `{OPENWEBUI_SKILLS_SHARED_DIR}/shared/data-visualizer/` +- Skill 元数据自动同步至 OpenWebUI 数据库。 + +### 2. 一次安装多个来自不同 URL 的 Skills + +**用户提问:** "帮我安装这三个 skill:URL1、URL2、URL3" +**工具调用:** `manage_skills(action="install", url=["URL1", "URL2", "URL3"])` +**结果:** + +- Agent 将 `url` 传入为列表,SDK 依次下载、解压并安装每个 URL 对应的 skill 到 `shared/` 目录。 +- 所有安装完成后,执行一次统一的数据库同步,避免重复触发。 +- 若某个 URL 失败,其余 URL 的安装仍会继续,失败信息汇总在 `errors` 字段中返回。 + +### 3. 从单个仓库一次安装所有 Skills + +**用户提问:** "安装 `https://github.com/myorg/skill-pack/tree/main/` 下的所有 skill" +**工具调用:** `manage_skills(action="install", url="https://github.com/myorg/skill-pack/tree/main/")` +**结果:** + +- SDK 自动扫描目录下所有包含 `SKILL.md` 的子文件夹,一次性批量安装。 + +### 4. 从当前对话创建新 Skill + +**用户提问:** "把我们刚才讨论的 Python 代码清理逻辑保存为一个名为 'py-clean' 的新 skill" +**工具调用:** `manage_skills(action="create", name="py-clean", content="...")` +**结果:** + +- 在 `{OPENWEBUI_SKILLS_SHARED_DIR}/shared/py-clean/` 创建新目录。 +- 写入 `SKILL.md` 并同步至数据库。 + +--- + +## 推荐配置 + +- `ENABLE_OPENWEBUI_SKILLS=True` +- `OPENWEBUI_SKILLS_SHARED_DIR=/app/backend/data/cache/copilot-openwebui-skills` +- 可选黑名单:`DISABLED_SKILLS=skill-a,skill-b` + +--- + +## 注意事项 + +- 不要把 skill 名称当作 shell 命令执行。 +- skill 生命周期管理请优先使用 `manage_skills` 工具。 +- 所有已安装 skills 统一维护在一个目录:`.../shared/`。 diff --git a/plugins/pipes/github-copilot-sdk/github_copilot_sdk.py b/plugins/pipes/github-copilot-sdk/github_copilot_sdk.py index 29eea52..77f40d3 100644 --- a/plugins/pipes/github-copilot-sdk/github_copilot_sdk.py +++ b/plugins/pipes/github-copilot-sdk/github_copilot_sdk.py @@ -4,8 +4,8 @@ author: Fu-Jie author_url: https://github.com/Fu-Jie/openwebui-extensions funding_url: https://github.com/open-webui openwebui_id: ce96f7b4-12fc-4ac3-9a01-875713e69359 -description: Integrate GitHub Copilot SDK. Supports dynamic models, multi-turn conversation, streaming, multimodal input, infinite sessions, and frontend debug logging. -version: 0.8.0 +description: Integrate GitHub Copilot SDK. Supports dynamic models, multi-turn conversation, streaming, multimodal input, infinite sessions, bidirectional OpenWebUI Skills bridge, and manage_skills tool. +version: 0.9.0 requirements: github-copilot-sdk==0.1.25 """ @@ -18,6 +18,12 @@ import asyncio import logging import shutil import hashlib +import time +import subprocess +import tarfile +import zipfile +import urllib.parse +import urllib.request import aiohttp import contextlib from pathlib import Path @@ -43,11 +49,15 @@ from open_webui.utils.tools import get_tools as get_openwebui_tools, get_builtin from open_webui.models.tools import Tools from open_webui.models.users import Users from open_webui.models.files import Files, FileForm -from open_webui.config import UPLOAD_DIR, DATA_DIR from open_webui.storage.provider import Storage import mimetypes import uuid -import shutil + +# Get OpenWebUI version for capability detection +try: + from open_webui.env import VERSION as open_webui_version +except ImportError: + open_webui_version = "0.0.0" # Open WebUI internal database (re-use shared connection) try: @@ -124,21 +134,38 @@ BASE_GUIDELINES = ( " - `/app/build`: Compiled frontend assets (assets, static, pyodide, index.html).\n" "- **Rich Python Environment**: You can natively import and use any installed OpenWebUI dependencies. You have access to a wealth of libraries (e.g., for data processing, utility functions). However, you **MUST NOT** install new packages in the global environment. If you need additional dependencies, you must create a virtual environment within your designated workspace directory.\n" "- **Tool Availability**: You may have access to various tools (OpenWebUI Built-ins, Custom Tools, OpenAPI Servers, or MCP Servers) depending on the user's current configuration. If tools are visible in your session metadata, use them proactively to enhance your task execution.\n" + "- **Skills vs Tools — CRITICAL DISTINCTION**:\n" + " - **Tools** (`bash`, `create_file`, `view_file`, custom functions, MCP tools, etc.) are **executable functions** you call directly. They take inputs, run code or API calls, and return results.\n" + " - **Skills** are **context-injected Markdown instructions** (from `SKILL.md` files in a skill directory). They are NOT callable functions and NOT shell commands. When the Copilot SDK detects intent, it reads the relevant `SKILL.md` and injects its content into your context automatically — you then follow those instructions using your standard tools.\n" + " - **Skill directory structure**: A skill lives in a subdirectory under the Skills Directory. **Only `SKILL.md` is required** — all other contents are optional resources that the skill may provide:\n" + " - `scripts/` — helper Python or shell scripts; invoke via `bash` / `python3` **only when SKILL.md instructs you to**.\n" + " - `references/` — supplementary Markdown documents (detailed workflows, examples); read with `view_file` as directed.\n" + " - `templates/` — file templates to copy or fill in as part of the skill workflow.\n" + " - Any other supporting files (data, configs, assets) — treat them as resources described in `SKILL.md`.\n" + " - **Rule**: Always start by reading `SKILL.md`. It is the authoritative entry point. Other files in the directory only matter if `SKILL.md` references them.\n" + " - **Deterministic skill management**: For install/list/create/edit/delete/show operations, MUST use the `manage_skills` tool (do not rely on skill auto-trigger).\n" + " - **NEVER run a skill name as a shell command** (e.g., do NOT run `docx` or any skill name via `bash`). The skill name is not a binary. Scripts inside `scripts/` are helpers to be called explicitly as instructed.\n" + " - **How to identify a skill**: Skills appear in your context as injected instruction blocks (usually with a heading matching the skill name). Tools appear in your available-tools list.\n" "\n" "**Formatting & Presentation Directives:**\n" "1. **Markdown Excellence**: Leverage full **Markdown** capabilities (headers, bold, italics, tables, lists) to structure your response professionally for the chat interface.\n" "2. **Advanced Visualization**: Use **Mermaid** for flowcharts/diagrams and **LaTeX** for math. **IMPORTANT**: Always wrap Mermaid code within a standard ` ```mermaid ` code block to ensure it is rendered correctly by the UI.\n" - "3. **Interactive Artifacts (HTML)**: Standalone HTML code blocks (HTML+CSS+JS) will be **AUTOMATICALLY RENDERED** as interactive web pages within the chat. **Dual Delivery Protocol**: For web applications, you MUST perform two actions:\n" + "3. **Interactive Artifacts (HTML)**: **Premium Delivery Protocol**: For web applications, you MUST perform two actions:\n" " - 1. **Persist**: Create the file in the workspace (e.g., `index.html`) for project structure.\n" - " - 2. **Render**: Immediately output the SAME code in a ` ```html ` block so the user can interact with it.\n" - " - **Result**: The user gets both a saved file AND a live app. Never force the user to choose one over the other.\n" + " - 2. **Publish & Embed**: Call `publish_file_from_workspace(filename='your_file.html')`. This will automatically trigger the **Premium Experience** by directly embedding the interactive component using the action-style return.\n" + " - **CRITICAL**: When using this protocol, **DO NOT** output the raw HTML code in a code block. Provide ONLY the **[Preview]** and **[Download]** links returned by the tool. The interactive embed will appear automatically after your message finishes.\n" + " - **Process Visibility**: While raw code is forbidden, you SHOULD provide a **very brief Markdown summary** of the component's structure or key features (e.g., 'Generated login form with validation') before publishing. This keeps the user informed of the 'processing' progress.\n" + " - **Game/App Controls**: If your HTML includes keyboard controls (e.g., arrow keys, spacebar for games), you MUST include `event.preventDefault()` in your `keydown` listeners to prevent the parent browser page from scrolling.\n" "4. **Images & Files**: ALWAYS embed generated images/files directly using `![caption](url)`. Never provide plain text links.\n" "5. **File Delivery Protocol (Dual-Channel Delivery)**:\n" + " - **Definition**: **Artifacts** = content/code-block driven visual output in chat (typically with `html_embed`). **Rich UI** = tool/action returned embedded UI rendered by emitter in a persistent sandboxed iframe.\n" " - **Philosophy**: Visual Artifacts (HTML/Mermaid) and Downloadable Files are **COMPLEMENTARY**. Always aim to provide BOTH: instant visual insight in the chat AND a persistent file for the user to keep.\n" " - **The Rule**: When the user needs to *possess* data (download/export), you MUST publish it. Creating a local file alone is useless because the user cannot access your container.\n" " - **Implicit Requests**: If asked to 'export', 'get link', or 'save', automatically trigger this sequence.\n" - " - **Execution Sequence**: 1. **Write Local**: Create file in `.` (current directory). 2. **Publish**: Call `publish_file_from_workspace(filename='your_file.ext')`. 3. **Link**: Present the result based on file type. **For HTML files**: the tool returns both `download_url` (raw file) and `view_url` (`/content/html` format). You MUST present `view_url` as a **[Preview]** link that opens directly in the browser, AND `download_url` as a **[Download]** link. **For image files** (.png, .jpg, .gif, .svg, etc.): embed directly using `![caption](download_url)` — NEVER use a text link for images. For all other files: present `download_url` as a single download link.\n" - " - **URL Format is STRICT**: File links MUST be relative paths starting with `/api/v1/files/` (for example: `/api/v1/files/{id}/content` or `/api/v1/files/{id}/content/html`). NEVER output `api/...` (missing leading slash). NEVER prepend any domain (such as `https://example.com/...`) even if a domain appears in conversation context.\n" + " - **Execution Sequence**: 1. **Write Local**: Create file. 2. **Publish**: Call `publish_file_from_workspace`. 3. **Response Structure**:\n" + " - **For PDF files**: You MUST output ONLY Markdown links from the tool output (preview + download). **CRITICAL: NEVER output iframe/html_embed for PDF.**\n" + " - **For HTML files**: choose mode by complexity/environment. **Artifacts mode** (`embed_type='artifacts'`): output [Preview]/[Download], then output `html_embed` in a ```html code block. **Rich UI mode** (`embed_type='richui'`): output ONLY [Preview]/[Download]; do NOT output iframe/html block because Rich UI will render automatically via emitter." + " - **URL Format**: You MUST use the **ABSOLUTE URLs** provided in the tool output. NEVER modify them.\n" " - **Bypass RAG**: This protocol automatically handles S3 storage and bypasses RAG, ensuring 100% accurate data delivery.\n" "6. **TODO Visibility**: Every time you call the `update_todo` tool, you **MUST** immediately follow up with a beautifully formatted **Markdown summary** of the current TODO list. Use task checkboxes (`- [ ]`), progress indicators, and clear headings so the user can see the status directly in the chat.\n" "7. **Python Execution Standard**: For ANY task requiring Python logic (not just data analysis), you **MUST NOT** embed multi-line code directly in a shell command (e.g., using `python -c` or `<< 'EOF'`).\n" @@ -160,7 +187,7 @@ ADMIN_EXTENSIONS = ( "You have detected that the current user is an **ADMINISTRATOR**. You are granted additional 'God Mode' perspective:\n" "- **Full OS Interaction**: You can use shell tools to deep-dive into any container process or system configuration.\n" "- **Database Access**: You can connect to the **OpenWebUI Database** directly using credentials found in environment variables (e.g., `DATABASE_URL`).\n" - "- **Copilot SDK & Metadata**: You can inspect your own session state in `/root/.copilot/session-state/` and core configuration in `/root/.copilot/config.json` to debug session persistence.\n" + "- **Copilot SDK & Metadata**: You can inspect your own session state and core configuration in the Copilot SDK config directory to debug session persistence.\n" "- **Environment Secrets**: You are permitted to read and analyze environment variables and system-wide secrets for diagnostic purposes.\n" "**SECURITY NOTE**: Do NOT leak these sensitive internal details to non-admin users if you are ever switched to a lower privilege context.\n" ) @@ -171,13 +198,16 @@ USER_RESTRICTIONS = ( "You have detected that the current user is a **REGULAR USER**. You must adhere to the following security boundaries:\n" "- **NO Environment Access**: You are strictly **FORBIDDEN** from accessing environment variables (e.g., via `env`, `printenv`, or Python's `os.environ`).\n" "- **NO Database Access**: You must **NOT** attempt to connect to or query the OpenWebUI database.\n" - "- **NO System Metadata Access**: Access to `/root/.copilot/` or any system-level configuration files is strictly **PROHIBITED**.\n" + "- **Limited Session Metadata Access (Own Session Only)**: You MAY read Copilot session information for the current user/current chat strictly for troubleshooting. Allowed scope includes session state under the configured `COPILOTSDK_CONFIG_DIR` (default path: `/app/backend/data/.copilot/session-state/{chat_id}/`). Access to other users' sessions or unrelated global metadata is strictly **PROHIBITED**.\n" "- **NO Writing Outside Workspace**: Any attempt to write files to `/root`, `/app`, `/etc`, or other system folders is a **SECURITY VIOLATION**. All generated code, HTML, or data artifacts MUST be saved strictly inside the `Your Isolated Workspace` path provided.\n" - "- **Interactive Delivery**: When creating web/HTML content, you MUST accompanying the file creation with a rendered Artifact (` ```html ` block). Providing *only* a file path for a web app is considered a poor user experience.\n" + "- **Formal Delivery**: Write the file to the workspace and call `publish_file_from_workspace`. You are strictly **FORBIDDEN** from outputting raw HTML code blocks in the conversation.\n" "- **Restricted Shell**: Use shell tools **ONLY** for operations within your isolated workspace sub-directory. You are strictly **PROHIBITED** from exploring or reading other users' workspace directories. Any attempt to probe system secrets or cross-user data will be logged as a security violation.\n" + "- **System Tools Availability**: You MAY use all tools provided by the system for this session to complete tasks, as long as you obey the security boundaries above.\n" "**SECURITY NOTE**: Your priority is to protect the platform's integrity while providing helpful assistance within these boundaries.\n" ) +# Skill management is handled by the `manage_skills` tool. + class Pipe: class Valves(BaseModel): @@ -185,6 +215,10 @@ class Pipe: default="", description="GitHub Fine-grained Token (Requires 'Copilot Requests' permission)", ) + COPILOTSDK_CONFIG_DIR: str = Field( + default="", + description="Persistent directory for Copilot SDK config and session state (e.g. /app/backend/data/.copilot). If empty, auto-detects /app/backend/data/.copilot.", + ) ENABLE_OPENWEBUI_TOOLS: bool = Field( default=True, description="Enable OpenWebUI Tools (includes defined Tools and Built-in Tools).", @@ -197,6 +231,18 @@ class Pipe: default=True, description="Enable Direct MCP Client connection (Recommended).", ) + ENABLE_OPENWEBUI_SKILLS: bool = Field( + default=True, + description="Enable loading OpenWebUI model-attached skills into SDK skill directories.", + ) + OPENWEBUI_SKILLS_SHARED_DIR: str = Field( + default="/app/backend/data/cache/copilot-openwebui-skills", + description="Shared cache directory for OpenWebUI skills converted to SDK SKILL.md format.", + ) + DISABLED_SKILLS: str = Field( + default="", + description="Comma-separated skill names to disable in Copilot SDK session (e.g. docs-writer,webapp-testing).", + ) REASONING_EFFORT: Literal["low", "medium", "high", "xhigh"] = Field( default="medium", description="Reasoning effort level (low, medium, high). Only affects standard Copilot models (not BYOK).", @@ -314,6 +360,14 @@ class Pipe: default=True, description="Enable dynamic MCP server loading (overrides global).", ) + ENABLE_OPENWEBUI_SKILLS: bool = Field( + default=True, + description="Enable loading OpenWebUI model-attached skills into SDK skill directories (user override).", + ) + DISABLED_SKILLS: str = Field( + default="", + description="Comma-separated skill names to disable in Copilot SDK session (user override).", + ) # BYOK User Overrides BYOK_API_KEY: str = Field( @@ -352,96 +406,226 @@ class Pipe: _env_setup_done = False # Track if env setup has been completed _last_update_check = 0 # Timestamp of last CLI update check + def _is_version_at_least(self, target: str) -> bool: + """Check if OpenWebUI version is at least the target version.""" + try: + # Simple numeric comparison for speed and to avoid dependencies + def parse_v(v_str): + # Extract only numbers and dots + clean = re.sub(r"[^0-9.]", "", v_str) + return [int(x) for x in clean.split(".") if x] + + return parse_v(open_webui_version) >= parse_v(target) + except Exception: + return False + TRANSLATIONS = { "en-US": { "status_conn_est": "Connection established, waiting for response...", "status_reasoning_inj": "Reasoning Effort injected: {effort}", + "status_assistant_start": "Assistant is starting to think...", + "status_assistant_processing": "Assistant is processing your request...", + "status_still_working": "Still processing... ({seconds}s elapsed)", + "status_skill_invoked": "Detected and using skill: {skill}", + "status_tool_using": "Using tool: {name}...", + "status_tool_progress": "Tool progress: {name} ({progress}%) {msg}", + "status_tool_done": "Tool completed: {name}", + "status_tool_failed": "Tool failed: {name}", + "status_subagent_start": "Invoking sub-agent: {name}", + "status_compaction_start": "Optimizing session context...", + "status_compaction_complete": "Context optimization complete.", + "status_publishing_file": "Publishing artifact: {filename}", + "status_task_completed": "Task completed.", + "status_session_error": "Processing failed: {error}", + "status_no_skill_invoked": "No skill was invoked in this turn (DEBUG)", "debug_agent_working_in": "Agent working in: {path}", "debug_mcp_servers": "🔌 Connected MCP Servers: {servers}", "publish_success": "File published successfully.", - "publish_hint_html": "Link: [View {filename}]({view_url}) | [Download]({download_url})", + "publish_hint_html": "[View {filename}]({view_url}) | [Download]({download_url})", + "publish_hint_embed": "✨ Premium: Displayed directly via Action.", "publish_hint_default": "Link: [Download {filename}]({download_url})", }, "zh-CN": { "status_conn_est": "已建立连接,等待响应...", "status_reasoning_inj": "已注入推理级别:{effort}", + "status_assistant_start": "助手开始思考...", + "status_assistant_processing": "助手正在处理您的请求...", + "status_still_working": "仍在处理中...(已耗时 {seconds} 秒)", + "status_skill_invoked": "已发现并使用技能:{skill}", + "status_tool_using": "正在使用工具:{name}...", + "status_tool_progress": "工具进度:{name} ({progress}%) {msg}", + "status_tool_done": "工具已完成:{name}", + "status_tool_failed": "工具执行失败:{name}", + "status_subagent_start": "正在调用子代理:{name}", + "status_compaction_start": "正在优化会话上下文...", + "status_compaction_complete": "上下文优化完成。", + "status_publishing_file": "正在发布成果物:{filename}", + "status_task_completed": "任务已完成。", + "status_session_error": "处理失败:{error}", + "status_no_skill_invoked": "本轮未触发任何技能(DEBUG)", "debug_agent_working_in": "Agent 工作目录: {path}", "debug_mcp_servers": "🔌 已连接 MCP 服务器: {servers}", "publish_success": "文件发布成功。", - "publish_hint_html": "链接: [查看 {filename}]({view_url}) | [下载]({download_url})", + "publish_hint_html": "[查看网页]({view_url}) | [下载文件]({download_url})", + "publish_hint_embed": "✨ 高级体验:组件已通过 Action 直接展示。", "publish_hint_default": "链接: [下载 {filename}]({download_url})", }, "zh-HK": { "status_conn_est": "已建立連接,等待響應...", "status_reasoning_inj": "已注入推理級別:{effort}", + "status_assistant_start": "助手開始思考...", + "status_assistant_processing": "助手正在處理您的請求...", + "status_skill_invoked": "已發現並使用技能:{skill}", + "status_tool_using": "正在使用工具:{name}...", + "status_tool_progress": "工具進度:{name} ({progress}%) {msg}", + "status_subagent_start": "正在調用子代理:{name}", + "status_compaction_start": "正在優化會話上下文...", + "status_compaction_complete": "上下文優化完成。", + "status_publishing_file": "正在發布成果物:{filename}", + "status_no_skill_invoked": "本輪未觸發任何技能(DEBUG)", "debug_agent_working_in": "Agent 工作目錄: {path}", "debug_mcp_servers": "🔌 已連接 MCP 伺服器: {servers}", "publish_success": "文件發布成功。", "publish_hint_html": "連結: [查看 {filename}]({view_url}) | [下載]({download_url})", + "publish_hint_embed": "高級體驗:組件已通過 Action 直接展示。", "publish_hint_default": "連結: [下載 {filename}]({download_url})", }, "zh-TW": { "status_conn_est": "已建立連接,等待響應...", "status_reasoning_inj": "已注入推理級別:{effort}", + "status_assistant_start": "助手開始思考...", + "status_assistant_processing": "助手正在處理您的請求...", + "status_skill_invoked": "已發現並使用技能:{skill}", + "status_tool_using": "正在使用工具:{name}...", + "status_tool_progress": "工具進度:{name} ({progress}%) {msg}", + "status_subagent_start": "正在調用子代理:{name}", + "status_compaction_start": "正在優化會話上下文...", + "status_compaction_complete": "上下文優化完成。", + "status_publishing_file": "正在發布成果物:{filename}", + "status_no_skill_invoked": "本輪未觸發任何技能(DEBUG)", "debug_agent_working_in": "Agent 工作目錄: {path}", "debug_mcp_servers": "🔌 已連接 MCP 伺服器: {servers}", "publish_success": "文件發布成功。", "publish_hint_html": "連結: [查看 {filename}]({view_url}) | [下載]({download_url})", + "publish_hint_embed": "高級體驗:組件已通過 Action 直接展示。", "publish_hint_default": "連結: [下載 {filename}]({download_url})", }, "ja-JP": { "status_conn_est": "接続が確立されました。応答を待っています...", "status_reasoning_inj": "推論レベルが注入されました:{effort}", - "debug_agent_working_in": "Agent 作業ディレクトリ: {path}", - "debug_mcp_servers": "🔌 接続済み MCP サーバー: {servers}", + "status_skill_invoked": "スキルが検出され、使用されています:{skill}", + "status_publishing_file": "アーティファクトを公開中:{filename}", + "status_no_skill_invoked": "このターンではスキルは呼び出されませんでした (DEBUG)", + "debug_agent_working_in": "エージェントの作業ディレクトリ: {path}", + "debug_mcp_servers": "🔌 接続された MCP サーバー: {servers}", + "publish_success": "ファイルが正常に公開されました。", + "publish_hint_html": "リンク: [表示 {filename}]({view_url}) | [ダウンロード]({download_url})", + "publish_hint_embed": "プレミアム体験:コンポーネントはアクション経由で直接表示されました。", + "publish_hint_default": "リンク: [ダウンロード {filename}]({download_url})", }, "ko-KR": { "status_conn_est": "연결이 설정되었습니다. 응답을 기다리는 중...", - "status_reasoning_inj": "추론 수준 설정됨: {effort}", - "debug_agent_working_in": "Agent 작업 디렉토리: {path}", + "status_reasoning_inj": "추론 노력이 주입되었습니다: {effort}", + "status_skill_invoked": "스킬이 감지되어 사용 중입니다: {skill}", + "status_publishing_file": "아티팩트 게시 중: {filename}", + "status_no_skill_invoked": "이 턴에는 스킬이 호출되지 않았습니다 (DEBUG)", + "debug_agent_working_in": "에이전트 작업 디렉토리: {path}", "debug_mcp_servers": "🔌 연결된 MCP 서버: {servers}", + "publish_success": "파일이 성공적으로 게시되었습니다.", + "publish_hint_html": "링크: [{filename} 보기]({view_url}) | [다운로드]({download_url})", + "publish_hint_embed": "프리미엄 경험: 구성 요소가 액션을 통해 직접 표시되었습니다.", + "publish_hint_default": "링크: [{filename} 다운로드]({download_url})", }, "fr-FR": { "status_conn_est": "Connexion établie, en attente de réponse...", "status_reasoning_inj": "Effort de raisonnement injecté : {effort}", - "debug_agent_working_in": "Répertoire de travail de l'Agent : {path}", + "status_skill_invoked": "Compétence détectée et utilisée : {skill}", + "status_publishing_file": "Publication de l'artefact : {filename}", + "status_no_skill_invoked": "Aucune compétence invoquée pour ce tour (DEBUG)", + "debug_agent_working_in": "Agent travaillant dans : {path}", "debug_mcp_servers": "🔌 Serveurs MCP connectés : {servers}", + "publish_success": "Fichier publié avec succès.", + "publish_hint_html": "Lien : [Voir {filename}]({view_url}) | [Télécharger]({download_url})", + "publish_hint_embed": "Expérience Premium : Le composant est affiché directement via Action.", + "publish_hint_default": "Lien : [Télécharger {filename}]({download_url})", }, "de-DE": { "status_conn_est": "Verbindung hergestellt, warte auf Antwort...", - "status_reasoning_inj": "Argumentationsaufwand injiziert: {effort}", - "debug_agent_working_in": "Agent-Arbeitsverzeichnis: {path}", + "status_reasoning_inj": "Schlussfolgerungsaufwand injiziert: {effort}", + "status_skill_invoked": "Skill erkannt und verwendet: {skill}", + "status_publishing_file": "Artifact wird veröffentlicht: {filename}", + "status_no_skill_invoked": "In dieser Runde wurde kein Skill aufgerufen (DEBUG)", + "debug_agent_working_in": "Agent arbeitet in: {path}", "debug_mcp_servers": "🔌 Verbundene MCP-Server: {servers}", - }, - "es-ES": { - "status_conn_est": "Conexión establecida, esperando respuesta...", - "status_reasoning_inj": "Nivel de razonamiento inyectado: {effort}", - "debug_agent_working_in": "Directorio de trabajo del Agente: {path}", - "debug_mcp_servers": "🔌 Servidores MCP conectados: {servers}", + "publish_success": "Datei erfolgreich veröffentlicht.", + "publish_hint_html": "Link: [{filename} ansehen]({view_url}) | [Herunterladen]({download_url})", + "publish_hint_embed": "Premium-Erlebnis: Die Komponente wurde direkt per Action angezeigt.", + "publish_hint_default": "Link: [{filename} herunterladen]({download_url})", }, "it-IT": { "status_conn_est": "Connessione stabilita, in attesa di risposta...", - "status_reasoning_inj": "Livello di ragionamento iniettato: {effort}", - "debug_agent_working_in": "Directory di lavoro dell'Agente: {path}", + "status_reasoning_inj": "Sforzo di ragionamento iniettato: {effort}", + "status_skill_invoked": "Skill rilevata e utilizzata: {skill}", + "status_publishing_file": "Pubblicazione dell'artefatto: {filename}", + "status_no_skill_invoked": "Nessuna skill invocata in questo turno (DEBUG)", + "debug_agent_working_in": "Agente al lavoro in: {path}", "debug_mcp_servers": "🔌 Server MCP connessi: {servers}", + "publish_success": "File pubblicato correttamente.", + "publish_hint_html": "Link: [Visualizza {filename}]({view_url}) | [Scarica]({download_url})", + "publish_hint_embed": "Esperienza Premium: il componente è stato visualizzato direttamente tramite Action.", + "publish_hint_default": "Link: [Scarica {filename}]({download_url})", + }, + "es-ES": { + "status_conn_est": "Conexión establecida, esperando respuesta...", + "status_reasoning_inj": "Esfuerzo de razonamiento inyectado: {effort}", + "status_skill_invoked": "Habilidad detectada y utilizada: {skill}", + "status_publishing_file": "Publicando artefacto: {filename}", + "status_no_skill_invoked": "No se invocó ninguna habilidad en este turno (DEBUG)", + "debug_agent_working_in": "Agente trabajando en: {path}", + "debug_mcp_servers": "🔌 Servidores MCP conectados: {servers}", + "publish_success": "Archivo publicado con éxito.", + "publish_hint_html": "Enlace: [Ver {filename}]({view_url}) | [Descargar]({download_url})", + "publish_hint_embed": "Experiencia Premium: el componente se mostró directamente a través de Action.", + "publish_hint_default": "Enlace: [Descargar {filename}]({download_url})", + }, + "vi-VN": { + "status_conn_est": "Đã thiết lập kết nối, đang chờ phản hồi...", + "status_reasoning_inj": "Nỗ lực suy luận đã được đưa vào: {effort}", + "status_skill_invoked": "Kỹ năng đã được phát hiện và sử dụng: {skill}", + "status_publishing_file": "Đang xuất bản thành phẩm: {filename}", + "status_no_skill_invoked": "Không có kỹ năng nào được gọi trong lượt này (DEBUG)", + "debug_agent_working_in": "Agent đang làm việc tại: {path}", + "debug_mcp_servers": "🔌 Các máy chủ MCP đã kết nối: {servers}", + "publish_success": "Tệp đã được xuất bản thành công.", + "publish_hint_html": "Liên kết: [Xem {filename}]({view_url}) | [Tải xuống]({download_url})", + "publish_hint_embed": "Trải nghiệm Cao cấp: Thành phần đã được hiển thị trực tiếp qua Action.", + "publish_hint_default": "Liên kết: [Tải xuống {filename}]({download_url})", + }, + "id-ID": { + "status_conn_est": "Koneksi terjalin, menunggu respons...", + "status_reasoning_inj": "Upaya penalaran dimasukkan: {effort}", + "status_skill_invoked": "Keahlian terdeteksi và digunakan: {skill}", + "status_publishing_file": "Menerbitkan artefak: {filename}", + "status_no_skill_invoked": "Tidak ada keahlian yang dipanggil dalam giliran ini (DEBUG)", + "debug_agent_working_in": "Agen bekerja di: {path}", + "debug_mcp_servers": "🔌 Server MCP Terhubung: {servers}", + "publish_success": "File berhasil diterbitkan.", + "publish_hint_html": "Tautan: [Lihat {filename}]({view_url}) | [Unduh]({download_url})", + "publish_hint_embed": "Pengalaman Premium: Komponen ditampilkan secara langsung melalui Action.", + "publish_hint_default": "Tautan: [Unduh {filename}]({download_url})", }, "ru-RU": { "status_conn_est": "Соединение установлено, ожидание ответа...", "status_reasoning_inj": "Уровень рассуждения внедрен: {effort}", + "status_skill_invoked": "Обнаружен и используется навык: {skill}", + "status_publishing_file": "Публикация файла: {filename}", + "status_no_skill_invoked": "На этом шаге навыки не вызывались (DEBUG)", "debug_agent_working_in": "Рабочий каталог Агента: {path}", "debug_mcp_servers": "🔌 Подключенные серверы MCP: {servers}", - }, - "vi-VN": { - "status_conn_est": "Đã thiết lập kết nối, đang chờ phản hồi...", - "status_reasoning_inj": "Cấp độ suy luận đã được áp dụng: {effort}", - "debug_agent_working_in": "Thư mục làm việc của Agent: {path}", - "debug_mcp_servers": "🔌 Các máy chủ MCP đã kết nối: {servers}", - }, - "id-ID": { - "status_conn_est": "Koneksi terjalin, menunggu respons...", - "status_reasoning_inj": "Tingkat penalaran diterapkan: {effort}", - "debug_agent_working_in": "Direktori kerja Agent: {path}", - "debug_mcp_servers": "🔌 Server MCP yang terhubung: {servers}", + "publish_success": "Файл успешно опубликован.", + "publish_hint_html": "Ссылка: [Просмотр {filename}]({view_url}) | [Скачать]({download_url})", + "publish_hint_embed": "Premium: компонент отображен напрямую через Action.", + "publish_hint_default": "Ссылка: [Скачать {filename}]({download_url})", }, } @@ -703,9 +887,11 @@ class Pipe: self, body: dict = None, __user__=None, + __event_emitter__=None, __event_call__=None, __request__=None, __metadata__=None, + pending_embeds: List[dict] = None, ): """Initialize custom tools based on configuration""" # 1. Determine effective settings (User override > Global) @@ -716,9 +902,20 @@ class Pipe: # 2. Publish tool is always injected, regardless of other settings chat_ctx = self._get_chat_context(body, __metadata__) chat_id = chat_ctx.get("chat_id") - file_tool = self._get_publish_file_tool(__user__, chat_id, __request__) + file_tool = self._get_publish_file_tool( + __user__, + chat_id, + __request__, + __event_emitter__=__event_emitter__, + pending_embeds=pending_embeds, + ) final_tools = [file_tool] if file_tool else [] + # Skill management tool is always injected for deterministic operations + manage_skills_tool = self._get_manage_skills_tool(__user__, chat_id) + if manage_skills_tool: + final_tools.append(manage_skills_tool) + # 3. If all OpenWebUI tool types are disabled, skip loading and return early if not enable_tools and not enable_openapi: return final_tools @@ -732,10 +929,12 @@ class Pipe: openwebui_tools = await self._load_openwebui_tools( body=body, __user__=__user__, + __event_emitter__=__event_emitter__, __event_call__=__event_call__, enable_tools=enable_tools, enable_openapi=enable_openapi, chat_tool_ids=chat_tool_ids, + __metadata__=__metadata__, ) if openwebui_tools: @@ -755,7 +954,14 @@ class Pipe: return final_tools - def _get_publish_file_tool(self, __user__, chat_id, __request__=None): + def _get_publish_file_tool( + self, + __user__, + chat_id, + __request__=None, + __event_emitter__=None, + pending_embeds: List[dict] = None, + ): """ Create a tool to publish files from the workspace to a downloadable URL. """ @@ -776,27 +982,61 @@ class Pipe: # Resolve workspace directory workspace_dir = Path(self._get_workspace_dir(user_id=user_id, chat_id=chat_id)) + # Resolve host from request for absolute URLs + base_url = "" + if __request__ and hasattr(__request__, "base_url"): + base_url = str(__request__.base_url).rstrip("/") + elif __request__ and hasattr(__request__, "url"): + # Fallback for different request implementations + try: + from urllib.parse import urljoin, urlparse + + parsed = urlparse(str(__request__.url)) + base_url = f"{parsed.scheme}://{parsed.netloc}" + except Exception: + pass + # Define parameter schema explicitly for the SDK class PublishFileParams(BaseModel): filename: str = Field( ..., - description="The EXACT name of the file you just created in the current directory (e.g., 'report.csv'). REQUIRED.", + description=( + "The relative path of the file to publish (e.g., 'report.html', 'data/results.csv'). " + "The tool will return internal access URLs starting with `/api/v1/files/`. " + "You MUST use these specific relative paths as is in your response." + ), + ) + embed_type: Literal["artifacts", "richui"] = Field( + default="artifacts", + description=( + "Rendering style for HTML files. For PDF files, embedding is disabled and you MUST only provide preview/download Markdown links from tool output. " + "Use 'artifacts' for HTML (Default: output html_embed iframe inside a ```html code block; no height limit). " + "Use 'richui' for HTML (emitter-based integrated preview). DO NOT output html_embed in richui mode; it is rendered automatically. " + "Only 'artifacts' and 'richui' are supported." + ), ) - async def publish_file_from_workspace(filename: Any) -> dict: + async def publish_file_from_workspace(filename: Any) -> Union[dict, tuple]: """ Publishes a file from the local chat workspace to a downloadable URL. + If the file is HTML, it will also be directly embedded as an interactive component. """ try: # 1. Robust Parameter Extraction + embed_type = "artifacts" # Case A: filename is a Pydantic model (common when using params_type) if hasattr(filename, "model_dump"): # Pydantic v2 - filename = filename.model_dump().get("filename") + data = filename.model_dump() + filename = data.get("filename") + embed_type = data.get("embed_type", "artifacts") elif hasattr(filename, "dict"): # Pydantic v1 - filename = filename.dict().get("filename") + data = filename.dict() + filename = data.get("filename") + embed_type = data.get("embed_type", "artifacts") # Case B: filename is a dict if isinstance(filename, dict): + embed_type = filename.get("embed_type") or embed_type filename = ( filename.get("filename") or filename.get("file") @@ -812,12 +1052,16 @@ class Pipe: data = json.loads(filename) if isinstance(data, dict): + embed_type = data.get("embed_type") or embed_type filename = ( data.get("filename") or data.get("file") or filename ) except: pass + if embed_type not in ("artifacts", "richui"): + embed_type = "artifacts" + # 2. Final String Validation if ( not filename @@ -837,80 +1081,41 @@ class Pipe: target_path = target_path.resolve() if not str(target_path).startswith(str(workspace_dir.resolve())): return { - "error": f"Access denied: File must be within the current chat workspace." + "error": f"Security violation: path traversal detected.", + "filename": filename, } except Exception as e: - return {"error": f"Path validation failed: {e}"} + return { + "error": f"Invalid filename format: {e}", + "filename": filename, + } if not target_path.exists() or not target_path.is_file(): return { - "error": f"File '{filename}' not found in chat workspace. Ensure you saved it to the CURRENT DIRECTORY (.)." + "error": f"File not found in workspace: {filename}", + "workspace": str(workspace_dir), + "hint": "Ensure the file was successfully written using shell commands or create_file tool before publishing.", } - # 3. Upload via API (S3 Compatible) - api_success = False - file_id = None - safe_filename = filename - - token = None - if __request__: - auth_header = __request__.headers.get("Authorization") - if auth_header and auth_header.startswith("Bearer "): - token = auth_header.split(" ")[1] - if not token and "token" in __request__.cookies: - token = __request__.cookies.get("token") - - if token: - try: - import aiohttp - - base_url = str(__request__.base_url).rstrip("/") - # ?process=false skips RAG processing AND the - # ALLOWED_FILE_EXTENSIONS restriction (which blocks html/htm) - upload_url = f"{base_url}/api/v1/files/?process=false" - - async with aiohttp.ClientSession() as session: - with open(target_path, "rb") as f: - data = aiohttp.FormData() - data.add_field("file", f, filename=target_path.name) - import json - - data.add_field( - "metadata", - json.dumps( - { - "source": "copilot_workspace_publish", - "skip_rag": True, - } - ), - ) - - async with session.post( - upload_url, - data=data, - headers={"Authorization": f"Bearer {token}"}, - ) as resp: - if resp.status == 200: - api_result = await resp.json() - file_id = api_result.get("id") - safe_filename = api_result.get( - "filename", target_path.name - ) - api_success = True - except Exception as e: - logger.error(f"API upload failed: {e}") - - # 4. Fallback: Use Storage.upload_file directly (S3/Local/GCS/Azure compatible) - if not api_success: - file_id = str(uuid.uuid4()) + # 3. Handle Storage & File ID + # We check if file already exists in OpenWebUI DB to avoid duplicates + try: safe_filename = target_path.name - storage_filename = f"{file_id}_{safe_filename}" + # deterministic ID based on user + workspace path + filename + file_key = f"{user_id}:{workspace_dir}:{safe_filename}" + file_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, file_key)) + except Exception as e: + file_id = str(uuid.uuid4()) + + existing_file = await asyncio.to_thread(Files.get_file_by_id, file_id) + if not existing_file: def _upload_via_storage(): + # Open file and upload to storage provider (S3 or Local) with open(target_path, "rb") as f: _, stored_path = Storage.upload_file( f, - storage_filename, + f"{file_id}_{safe_filename}", { "OpenWebUI-User-Id": user_id, "OpenWebUI-File-Id": file_id, @@ -936,18 +1141,50 @@ class Pipe: ) await asyncio.to_thread(Files.insert_new_file, user_id, file_form) - # 5. Result - download_url = f"/api/v1/files/{file_id}/content" - is_html = safe_filename.lower().endswith((".html", ".htm")) + # 5. Result Construction + raw_id = str(file_id).split("/")[-1] + rel_download_url = f"/api/v1/files/{raw_id}/content" + download_url = ( + f"{base_url}{rel_download_url}" if base_url else rel_download_url + ) + + is_html = safe_filename.lower().endswith((".html", ".htm")) + is_pdf = safe_filename.lower().endswith(".pdf") - # For HTML files, provide a direct view link (/content/html) for browser preview view_url = None + has_preview = False + + # Capability Check: Rich UI requires OpenWebUI >= 0.8.0 + rich_ui_supported = self._is_version_at_least("0.8.0") + if is_html: view_url = f"{download_url}/html" + has_preview = True + elif is_pdf: + view_url = download_url + # Add download flag to absolute URL carefully + sep = "&" if "?" in download_url else "?" + download_url = f"{download_url}{sep}download=1" + has_preview = True # Localized output msg = self._get_translation(user_lang, "publish_success") - if is_html: + if is_html and rich_ui_supported: + # Specific sequence for HTML + hint = ( + self._get_translation(user_lang, "publish_hint_embed") + + "\n\n" + + self._get_translation( + user_lang, + "publish_hint_html", + filename=safe_filename, + view_url=view_url, + download_url=download_url, + ) + ) + if embed_type == "richui": + hint += "\n\nCRITICAL: You are in 'richui' mode. DO NOT output an HTML code block or iframe in your message. Just output the links above." + elif has_preview: hint = self._get_translation( user_lang, "publish_hint_html", @@ -963,18 +1200,65 @@ class Pipe: download_url=download_url, ) - result = { - "file_id": file_id, + # Fallback for old versions + if is_html and not rich_ui_supported: + hint += f"\n\n**NOTE**: Rich UI embedding is NOT supported in this OpenWebUI version ({open_webui_version}). You SHOULD output the HTML code block manually if the user needs to see the result immediately." + + result_dict = { + "file_id": raw_id, "filename": safe_filename, "download_url": download_url, + "url_type": "internal_relative_path", + "path_specification": "MUST_START_WITH_/api", "message": msg, "hint": hint, + "rich_ui_supported": rich_ui_supported, } - if is_html and view_url: - result["view_url"] = view_url - return result + if has_preview and view_url: + result_dict["view_url"] = view_url + if is_html and embed_type == "artifacts": + # Artifacts mode: standard iframe for the AI to output directly (Infinite height) + iframe_html = ( + f'' + ) + result_dict["html_embed"] = iframe_html + # Note: We do NOT add to pending_embeds. The AI will output this in the message. + elif embed_type == "richui": + # In richui mode, we physically remove html_embed to prevent the AI from outputting it + # The system will handle the rendering via emitter + pass + + # 6. Premium Rich UI Experience for HTML only (Direct Embed via emitter) + # We emit events directly ONLY IF embed_type is 'richui'. + # Note: Emission is now delayed until session.idle to avoid UI flicker and ensure reliability. + if is_html and embed_type == "richui" and rich_ui_supported: + try: + # For Rich UI Integrated view, we pass a clean iframe. + # We use 60vh directly to avoid nested iframe height collapses. + embed_content = ( + f'' + ) + + if pending_embeds is not None: + pending_embeds.append( + { + "filename": safe_filename, + "content": embed_content, + "type": "richui", + } + ) + except Exception as e: + logger.error(f"Failed to prepare Rich UI embed: {e}") + + return result_dict + except Exception as e: - return {"error": str(e)} + logger.error(f"Publish error: {e}") + return {"error": str(e), "filename": filename} return define_tool( name="publish_file_from_workspace", @@ -982,6 +1266,407 @@ class Pipe: params_type=PublishFileParams, )(publish_file_from_workspace) + def _get_manage_skills_tool(self, __user__, chat_id): + """Create a deterministic standalone skill management tool. + + Supports: + - install: install skill(s) from URL (GitHub tree URL / zip / tar.gz) + - list: list installed skills under shared directory + - create: create skill from current context content + - edit/show/delete: local skill CRUD + """ + if isinstance(__user__, (list, tuple)): + user_data = __user__[0] if __user__ else {} + elif isinstance(__user__, dict): + user_data = __user__ + else: + user_data = {} + + user_id = user_data.get("id") or user_data.get("user_id") + if not user_id: + return None + + workspace_dir = self._get_workspace_dir(user_id=user_id, chat_id=chat_id) + shared_dir = self._get_shared_skills_dir(workspace_dir) + + class ManageSkillsParams(BaseModel): + action: Literal["list", "install", "create", "edit", "delete", "show"] = ( + Field( + ..., + description="Operation to perform on skills.", + ) + ) + skill_name: Optional[str] = Field( + default=None, + description="Skill name for create/edit/delete/show operations.", + ) + url: Optional[Union[str, List[str]]] = Field( + default=None, + description=( + "Source URL(s) for install operation. " + "Accepts a single URL string or a list of URLs to install multiple skills at once." + ), + ) + description: Optional[str] = Field( + default=None, + description="Skill description for create/edit.", + ) + content: Optional[str] = Field( + default=None, + description="Skill instruction body (SKILL.md body) for create/edit.", + ) + files: Optional[Dict[str, str]] = Field( + default=None, + description=( + "Extra files to write into the skill folder alongside SKILL.md. " + "Keys are relative filenames (e.g. 'template.md', 'examples/usage.py'), " + "values are their text content. Useful for templates, example scripts, " + "or any resource files the Copilot agent can read from the skill directory." + ), + ) + force: Optional[bool] = Field( + default=False, + description="Force overwrite for install.", + ) + dry_run: Optional[bool] = Field( + default=False, + description="Preview install without writing files.", + ) + output_format: Optional[Literal["text", "json"]] = Field( + default="text", + description="Output format for list action.", + ) + + def _sanitize_skill_name(name: str) -> str: + clean = self._skill_dir_name_from_skill_name(name) + return re.sub(r"\s+", "-", clean) + + def _normalize_github_archive_url(url: str) -> tuple[str, str]: + parsed = urllib.parse.urlparse(url) + path_parts = [p for p in parsed.path.split("/") if p] + # GitHub tree URL: /owner/repo/tree/branch/subpath + if parsed.netloc.endswith("github.com") and "tree" in path_parts: + tree_idx = path_parts.index("tree") + if tree_idx >= 2 and len(path_parts) > tree_idx + 1: + owner = path_parts[0] + repo = path_parts[1] + branch = path_parts[tree_idx + 1] + subpath = "/".join(path_parts[tree_idx + 2 :]) + archive_url = f"https://github.com/{owner}/{repo}/archive/refs/heads/{branch}.zip" + return archive_url, subpath + return url, "" + + def _extract_archive(archive_path: Path, dest_dir: Path) -> Path: + dest_dir.mkdir(parents=True, exist_ok=True) + if archive_path.suffix == ".zip": + with zipfile.ZipFile(archive_path, "r") as zf: + zf.extractall(dest_dir) + elif archive_path.name.endswith(".tar.gz") or archive_path.suffix in { + ".tgz", + ".tar", + }: + with tarfile.open(archive_path, "r:*") as tf: + tf.extractall(dest_dir) + else: + raise ValueError(f"Unsupported archive format: {archive_path.name}") + + children = [p for p in dest_dir.iterdir() if p.is_dir()] + if len(children) == 1: + return children[0] + return dest_dir + + def _discover_skill_dirs(root: Path, subpath: str = "") -> List[Path]: + target = root / subpath if subpath else root + target = target.resolve() + if not target.exists() or not target.is_dir(): + raise ValueError( + f"Skill source path not found in archive: {subpath or str(root)}" + ) + + if (target / "SKILL.md").exists() or (target / "README.md").exists(): + return [target] + + found = [] + for child in target.iterdir(): + if child.is_dir() and ( + (child / "SKILL.md").exists() or (child / "README.md").exists() + ): + found.append(child) + if not found: + raise ValueError("No valid skill found (need SKILL.md or README.md)") + return found + + def _copy_skill_dir(src_dir: Path, dest_root: Path, force: bool = False) -> str: + skill_name = _sanitize_skill_name(src_dir.name) + dest_dir = dest_root / skill_name + if dest_dir.exists(): + if not force: + raise FileExistsError(f"Skill already exists: {skill_name}") + shutil.rmtree(dest_dir) + + shutil.copytree(src_dir, dest_dir) + readme = dest_dir / "README.md" + skill_md = dest_dir / "SKILL.md" + if not skill_md.exists() and readme.exists(): + readme.rename(skill_md) + if not skill_md.exists(): + raise ValueError(f"Installed directory missing SKILL.md: {skill_name}") + return skill_name + + def _list_skills_text(skills: List[dict]) -> str: + if not skills: + return "No skills found" + lines = [] + for s in skills: + lines.append(f"- {s['name']}: {s.get('description', '')}") + return "\n".join(lines) + + async def manage_skills(params: Any) -> dict: + try: + if hasattr(params, "model_dump"): + payload = params.model_dump(exclude_unset=True) + elif isinstance(params, dict): + payload = params + else: + payload = {} + + action = str(payload.get("action", "")).strip().lower() + skill_name = (payload.get("skill_name") or "").strip() + _raw_url = payload.get("url") or "" + if isinstance(_raw_url, list): + source_urls = [u.strip() for u in _raw_url if u and u.strip()] + source_url = source_urls[0] if source_urls else "" + else: + source_url = str(_raw_url).strip() + source_urls = [source_url] if source_url else [] + skill_desc = (payload.get("description") or "").strip() + skill_body = (payload.get("content") or "").strip() + force = bool(payload.get("force", False)) + dry_run = bool(payload.get("dry_run", False)) + output_format = ( + str(payload.get("output_format", "text")).strip().lower() + ) + + if action == "list": + entries = [] + root = Path(shared_dir) + if root.exists(): + for child in sorted( + root.iterdir(), key=lambda p: p.name.lower() + ): + if not child.is_dir(): + continue + skill_md = child / "SKILL.md" + if not skill_md.exists(): + continue + name, desc, _ = self._parse_skill_md_meta( + skill_md.read_text(encoding="utf-8"), child.name + ) + entries.append( + { + "name": name or child.name, + "dir_name": child.name, + "description": desc, + "path": str(skill_md), + } + ) + if output_format == "json": + return {"skills": entries, "count": len(entries)} + return {"count": len(entries), "text": _list_skills_text(entries)} + + if action == "install": + if not source_urls: + return {"error": "Missing required argument: url"} + + all_installed: List[str] = [] + errors: List[str] = [] + + for _url in source_urls: + archive_url, subpath = _normalize_github_archive_url(_url) + tmp_dir = Path(tempfile.mkdtemp(prefix="skill-install-")) + try: + suffix = ".zip" + if archive_url.endswith(".tar.gz"): + suffix = ".tar.gz" + elif archive_url.endswith(".tgz"): + suffix = ".tgz" + archive_path = tmp_dir / f"download{suffix}" + + await asyncio.to_thread( + urllib.request.urlretrieve, + archive_url, + str(archive_path), + ) + extracted_root = _extract_archive( + archive_path, tmp_dir / "extract" + ) + candidates = _discover_skill_dirs(extracted_root, subpath) + + for candidate in candidates: + if dry_run: + all_installed.append( + _sanitize_skill_name(candidate.name) + ) + else: + all_installed.append( + _copy_skill_dir( + candidate, Path(shared_dir), force=force + ) + ) + except Exception as e: + errors.append(f"{_url}: {e}") + finally: + shutil.rmtree(tmp_dir, ignore_errors=True) + + if not dry_run and all_installed: + # Immediately sync new skills to OW DB so frontend + # reflects them without needing a new request. + try: + await asyncio.to_thread( + self._sync_openwebui_skills, workspace_dir, user_id + ) + except Exception: + pass + + return { + "success": len(errors) == 0, + "action": "install", + "dry_run": dry_run, + "installed": all_installed, + "count": len(all_installed), + **({"errors": errors} if errors else {}), + } + + if action in {"create", "edit", "show", "delete"}: + if not skill_name: + return { + "error": "Missing required argument: skill_name for this action" + } + dir_name = self._skill_dir_name_from_skill_name(skill_name) + skill_dir = Path(shared_dir) / dir_name + skill_md = skill_dir / "SKILL.md" + + if action == "show": + if not skill_dir.exists(): + return {"error": f"Skill not found: {dir_name}"} + # Return SKILL.md content plus a listing of all other files + skill_md_content = ( + skill_md.read_text(encoding="utf-8") + if skill_md.exists() + else "" + ) + other_files = [] + for f in sorted(skill_dir.rglob("*")): + if f.is_file() and f.name not in ("SKILL.md", ".owui_id"): + rel = str(f.relative_to(skill_dir)) + other_files.append(rel) + return { + "skill_name": dir_name, + "path": str(skill_dir), + "skill_md": skill_md_content, + "other_files": other_files, + } + + if action == "delete": + if not skill_dir.exists(): + return {"error": f"Skill not found: {dir_name}"} + # Remove from OW DB before deleting local dir, otherwise + # next-request sync will recreate the directory from DB. + owui_id_file = skill_dir / ".owui_id" + if owui_id_file.exists(): + owui_id = owui_id_file.read_text(encoding="utf-8").strip() + if owui_id: + try: + from open_webui.models.skills import Skills + + Skills.delete_skill_by_id(owui_id) + except Exception: + pass + shutil.rmtree(skill_dir) + return { + "success": True, + "action": "delete", + "skill_name": dir_name, + "path": str(skill_dir), + } + + # create / edit + if action == "create" and skill_dir.exists() and not force: + return { + "error": f"Skill already exists: {dir_name}. Use force=true to overwrite." + } + + if action == "edit" and not skill_md.exists(): + return { + "error": f"Skill not found: {dir_name}. Create it first." + } + + existing_content = "" + if skill_md.exists(): + existing_content = skill_md.read_text(encoding="utf-8") + + parsed_name, parsed_desc, parsed_body = self._parse_skill_md_meta( + existing_content, dir_name + ) + + final_name = skill_name or parsed_name or dir_name + final_desc = skill_desc or parsed_desc or final_name + final_body = ( + skill_body or parsed_body or "Describe how to use this skill." + ) + + skill_dir.mkdir(parents=True, exist_ok=True) + final_content = self._build_skill_md_content( + final_name, final_desc, final_body + ) + skill_md.write_text(final_content, encoding="utf-8") + + # Write any extra files into the skill folder. + # These are accessible to the Copilot SDK agent but not synced to OW DB. + extra_files = payload.get("files") or {} + if not isinstance(extra_files, dict): + return { + "error": "Invalid 'files' parameter: must be a dictionary of {filename: content} pairs" + } + + written_files = [] + for rel_path, file_content in extra_files.items(): + # Sanitize: prevent absolute paths or path traversal + rel = Path(rel_path) + if rel.is_absolute() or any(part == ".." for part in rel.parts): + continue + dest = skill_dir / rel + dest.parent.mkdir(parents=True, exist_ok=True) + dest.write_text(file_content, encoding="utf-8") + written_files.append(str(rel)) + + # Immediately sync to OW DB so frontend reflects the change. + try: + await asyncio.to_thread( + self._sync_openwebui_skills, workspace_dir, user_id + ) + except Exception: + pass + + return { + "success": True, + "action": action, + "skill_name": dir_name, + "skill_dir": str(skill_dir), + "skill_md": str(skill_md), + "extra_files_written": written_files, + } + + return {"error": f"Unsupported action: {action}"} + except Exception as e: + return {"error": str(e)} + + return define_tool( + name="manage_skills", + description="Manage skills deterministically: install/list/create/edit/delete/show. Supports creating skill content from current context.", + params_type=ManageSkillsParams, + )(manage_skills) + def _json_schema_to_python_type(self, schema: dict) -> Any: """Convert JSON Schema type to Python type for Pydantic models.""" if not isinstance(schema, dict): @@ -1015,8 +1700,12 @@ class Pipe: return Any - def _convert_openwebui_tool( - self, tool_name: str, tool_dict: dict, __event_call__=None + def _convert_openwebui_tool_to_sdk( + self, + tool_name: str, + tool_dict: dict, + __event_emitter__=None, + __event_call__=None, ): """Convert OpenWebUI tool definition to Copilot SDK tool.""" # Sanitize tool name to match pattern ^[a-zA-Z0-9_-]+$ @@ -1156,11 +1845,36 @@ class Pipe: result = await tool_callable(**payload) - # Special handling for OpenAPI tools which return (data, headers) tuple + # Support v0.8.0+ Action-style returns (tuple with headers) if isinstance(result, tuple) and len(result) == 2: data, headers = result # Basic heuristic to detect response headers (aiohttp headers or dict) if hasattr(headers, "get") and hasattr(headers, "items"): + # If Content-Disposition is 'inline', it's a Direct HTML/Embed result + if ( + "inline" + in str(headers.get("Content-Disposition", "")).lower() + ): + if __event_emitter__: + await __event_emitter__( + { + "type": "embeds", + "data": {"embeds": [data]}, + } + ) + # Return a status dict instead of raw HTML for the LLM's Tool UI block + return { + "status": "success", + "ui_intent": "direct_artifact_embed", + "message": "The interactive component has been displayed directly in the chat interface.", + "preview": ( + str(data)[:100] + "..." + if isinstance(data, str) + else "[Binary Data]" + ), + } + + # Standard tuple return for OpenAPI tools etc. if self.valves.DEBUG: await self._emit_debug_log( f"✅ {sanitized_tool_name} returned tuple, extracting data.", @@ -1290,10 +2004,12 @@ class Pipe: self, body: dict = None, __user__=None, + __event_emitter__=None, __event_call__=None, enable_tools: bool = True, enable_openapi: bool = True, chat_tool_ids: Optional[list] = None, + __metadata__: Optional[dict] = None, ): """Load OpenWebUI tools and convert them to Copilot SDK tools.""" if isinstance(__user__, (list, tuple)): @@ -1526,13 +2242,16 @@ class Pipe: pass # Get builtin tools - # Open all feature gates so filtering is driven solely by - # model.meta.builtinTools (defaults to all-enabled when absent). + # Code interpreter is STRICT opt-in: only enabled when request + # explicitly sets feature code_interpreter=true. Missing means disabled. + code_interpreter_enabled = self._is_code_interpreter_feature_enabled( + body, __metadata__ + ) all_features = { "memory": True, "web_search": True, "image_generation": True, - "code_interpreter": True, + "code_interpreter": code_interpreter_enabled, } builtin_tools = get_builtin_tools( self._build_openwebui_request(user_data), @@ -1617,13 +2336,15 @@ class Pipe: ].get("description") converted_tools = [] - for tool_name, tool_def in tools_dict.items(): + for tool_name, t_dict in tools_dict.items(): try: - converted_tools.append( - self._convert_openwebui_tool( - tool_name, tool_def, __event_call__=__event_call__ - ) + copilot_tool = self._convert_openwebui_tool_to_sdk( + tool_name, + t_dict, + __event_emitter__=__event_emitter__, + __event_call__=__event_call__, ) + converted_tools.append(copilot_tool) except Exception as e: await self._emit_debug_log( f"Failed to load OpenWebUI tool '{tool_name}': {e}", @@ -1860,12 +2581,482 @@ class Pipe: return self._dedupe_preserve_order(model_ids) + def _parse_csv_items(self, value: Optional[str]) -> List[str]: + if not value or not isinstance(value, str): + return [] + items = [item.strip() for item in value.split(",")] + return self._dedupe_preserve_order([item for item in items if item]) + + def _is_manage_skills_intent(self, text: str) -> bool: + """Detect whether the user is asking to manage/install skills. + + When true, route to the deterministic `manage_skills` tool workflow. + """ + if not text or not isinstance(text, str): + return False + + t = text.lower() + + patterns = [ + r"\bskills?-manager\b", + r"\binstall\b.*\bskills?\b", + r"\binstall\b.*github\.com/.*/skills", + r"\bmanage\b.*\bskills?\b", + r"\blist\b.*\bskills?\b", + r"\bdelete\b.*\bskills?\b", + r"\bremove\b.*\bskills?\b", + r"\bedit\b.*\bskills?\b", + r"\bupdate\b.*\bskills?\b", + r"安装.*技能", + r"安装.*skills?", + r"管理.*技能", + r"管理.*skills?", + r"列出.*技能", + r"删除.*技能", + r"编辑.*技能", + r"更新.*技能", + r"skills码", + r"skill\s*code", + ] + + for p in patterns: + if re.search(p, t): + return True + return False + + def _collect_skill_names_for_routing( + self, + resolved_cwd: str, + user_id: str, + enable_openwebui_skills: bool, + ) -> List[str]: + """Collect current skill names from shared directory.""" + skill_names: List[str] = [] + + def _scan_skill_dir(parent_dir: str): + parent = Path(parent_dir) + if not parent.exists() or not parent.is_dir(): + return + for skill_dir in parent.iterdir(): + if not skill_dir.is_dir(): + continue + skill_md = skill_dir / "SKILL.md" + if not skill_md.exists(): + continue + try: + content = skill_md.read_text(encoding="utf-8") + parsed_name, _, _ = self._parse_skill_md_meta( + content, skill_dir.name + ) + skill_names.append(parsed_name or skill_dir.name) + except Exception: + skill_names.append(skill_dir.name) + + if enable_openwebui_skills: + shared_dir = self._sync_openwebui_skills(resolved_cwd, user_id) + else: + shared_dir = self._get_shared_skills_dir(resolved_cwd) + _scan_skill_dir(shared_dir) + + return self._dedupe_preserve_order(skill_names) + + def _skill_dir_name_from_skill_name(self, skill_name: str) -> str: + name = (skill_name or "owui-skill").strip() + name = re.sub(r'[<>:"/\\|?*\x00-\x1f\x7f]+', "_", name) + name = name.strip().strip(".") + if not name: + name = "owui-skill" + return name[:128] + + def _get_copilot_config_dir(self) -> str: + """Get the effective directory for Copilot SDK config/metadata.""" + # 1. Valve override + if getattr(self.valves, "COPILOTSDK_CONFIG_DIR", ""): + return os.path.expanduser(self.valves.COPILOTSDK_CONFIG_DIR) + + # 2. Container persistence (Shared data volume) + if os.path.exists("/app/backend/data"): + path = "/app/backend/data/.copilot" + try: + os.makedirs(path, exist_ok=True) + return path + except Exception as e: + logger.warning(f"Failed to create .copilot dir in data volume: {e}") + + # 3. Fallback to standard path + return os.path.expanduser("~/.copilot") + + def _get_shared_skills_dir(self, resolved_cwd: str) -> str: + """Returns (and creates) the unified shared skills directory. + + Both OpenWebUI page skills and pipe-installed skills live here. + The directory is persistent and shared across all sessions. + """ + shared_base = Path(self.valves.OPENWEBUI_SKILLS_SHARED_DIR or "").expanduser() + if not shared_base.is_absolute(): + shared_base = Path(resolved_cwd) / shared_base + shared_dir = shared_base / "shared" + shared_dir.mkdir(parents=True, exist_ok=True) + return str(shared_dir) + + def _parse_skill_md_meta(self, content: str, fallback_name: str) -> tuple: + """Parse SKILL.md content into (name, description, body). + + Handles files with or without YAML frontmatter. + Strips quotes from frontmatter string values. + """ + fm_match = re.match(r"^---\s*\n(.*?)\n---\s*\n", content, re.DOTALL) + if fm_match: + fm_text = fm_match.group(1) + body = content[fm_match.end() :].strip() + name = fallback_name + description = "" + for line in fm_text.split("\n"): + m = re.match(r"^name:\s*(.+)$", line) + if m: + name = m.group(1).strip().strip("\"'") + m = re.match(r"^description:\s*(.+)$", line) + if m: + description = m.group(1).strip().strip("\"'") + return name, description, body + # No frontmatter: try to extract H1 as name + h1_match = re.search(r"^#\s+(.+)$", content.strip(), re.MULTILINE) + name = h1_match.group(1).strip() if h1_match else fallback_name + return name, "", content.strip() + + def _build_skill_md_content(self, name: str, description: str, body: str) -> str: + """Construct a SKILL.md file string from name, description, and body.""" + desc_line = description or name + if any(c in desc_line for c in ":#\n"): + desc_line = f'"{desc_line}"' + return ( + f"---\n" + f"name: {name}\n" + f"description: {desc_line}\n" + f"---\n\n" + f"# {name}\n\n" + f"{body}\n" + ) + + def _sync_openwebui_skills(self, resolved_cwd: str, user_id: str) -> str: + """Bidirectionally sync skills between OpenWebUI DB and the shared/ directory. + + Sync rules (per skill): + DB → File: if a skill exists in OpenWebUI but has no directory entry, or the + DB is newer than the file → write/update SKILL.md in shared/. + File → DB: if a skill directory has no .owui_id or the file is newer than the + DB entry → create/update the skill in OpenWebUI DB. + + Change detection uses MD5 content hash (skip if identical) then falls back to + timestamp comparison (db.updated_at vs file mtime) to determine direction. + + A `.owui_id` marker file inside each skill directory tracks the OpenWebUI skill ID. + Skills installed via pipe that have no OpenWebUI counterpart are registered in DB. + If a directory has `.owui_id` but the corresponding OpenWebUI skill is gone, + the local directory is removed (UI is source of truth for deletions). + + Returns the shared skills directory path (always, even on sync failure). + """ + shared_dir = Path(self._get_shared_skills_dir(resolved_cwd)) + + try: + from open_webui.models.skills import Skills, SkillForm, SkillMeta + + sync_stats = { + "db_to_file_updates": 0, + "db_to_file_creates": 0, + "file_to_db_updates": 0, + "file_to_db_creates": 0, + "file_to_db_links": 0, + "orphan_dir_deletes": 0, + } + + # ------------------------------------------------------------------ + # Step 1: Load all accessible OpenWebUI skills + # ------------------------------------------------------------------ + owui_by_id: Dict[str, dict] = {} + for skill in Skills.get_skills_by_user_id(user_id, "read") or []: + if not skill or not getattr(skill, "is_active", False): + continue + content = (getattr(skill, "content", "") or "").strip() + sk_id = str(getattr(skill, "id", "") or "") + sk_name = (getattr(skill, "name", "") or sk_id or "owui-skill").strip() + if not sk_id or not sk_name or not content: + continue + owui_by_id[sk_id] = { + "id": sk_id, + "name": sk_name, + "description": (getattr(skill, "description", "") or "") + .replace("\n", " ") + .strip(), + "content": content, + "updated_at": getattr(skill, "updated_at", 0) or 0, + } + + # ------------------------------------------------------------------ + # Step 2: Load directory skills (shared/) and build lookup maps + # ------------------------------------------------------------------ + dir_skills: Dict[str, dict] = {} # dir_name → dict + for skill_dir in shared_dir.iterdir(): + if not skill_dir.is_dir(): + continue + skill_md_path = skill_dir / "SKILL.md" + if not skill_md_path.exists(): + continue + owui_id_file = skill_dir / ".owui_id" + owui_id = ( + owui_id_file.read_text(encoding="utf-8").strip() + if owui_id_file.exists() + else None + ) + try: + file_content = skill_md_path.read_text(encoding="utf-8") + file_mtime = skill_md_path.stat().st_mtime + except Exception: + continue + dir_skills[skill_dir.name] = { + "path": skill_dir, + "owui_id": owui_id, + "mtime": file_mtime, + "content": file_content, + } + + # Reverse map: owui_id → dir_name (for skills already linked) + id_to_dir: Dict[str, str] = { + info["owui_id"]: dn + for dn, info in dir_skills.items() + if info["owui_id"] + } + + # ------------------------------------------------------------------ + # Step 3: DB → File (OpenWebUI skills written to shared/) + # ------------------------------------------------------------------ + for sk_id, sk in owui_by_id.items(): + expected_file_content = self._build_skill_md_content( + sk["name"], sk["description"], sk["content"] + ) + + if sk_id in id_to_dir: + dir_name = id_to_dir[sk_id] + dir_info = dir_skills[dir_name] + existing_hash = hashlib.md5( + dir_info["content"].encode("utf-8", errors="replace") + ).hexdigest() + new_hash = hashlib.md5( + expected_file_content.encode("utf-8", errors="replace") + ).hexdigest() + if ( + existing_hash != new_hash + and sk["updated_at"] > dir_info["mtime"] + ): + # DB is newer — update file + (dir_info["path"] / "SKILL.md").write_text( + expected_file_content, encoding="utf-8" + ) + dir_skills[dir_name]["content"] = expected_file_content + dir_skills[dir_name]["mtime"] = ( + (dir_info["path"] / "SKILL.md").stat().st_mtime + ) + sync_stats["db_to_file_updates"] += 1 + else: + # No directory for this OpenWebUI skill → create one + dir_name = self._skill_dir_name_from_skill_name(sk["name"]) + # Avoid collision with existing dir names + base = dir_name + suffix = 1 + while dir_name in dir_skills: + dir_name = f"{base}-{suffix}" + suffix += 1 + skill_dir = shared_dir / dir_name + skill_dir.mkdir(parents=True, exist_ok=True) + (skill_dir / "SKILL.md").write_text( + expected_file_content, encoding="utf-8" + ) + (skill_dir / ".owui_id").write_text(sk_id, encoding="utf-8") + dir_skills[dir_name] = { + "path": skill_dir, + "owui_id": sk_id, + "mtime": (skill_dir / "SKILL.md").stat().st_mtime, + "content": expected_file_content, + } + id_to_dir[sk_id] = dir_name + sync_stats["db_to_file_creates"] += 1 + + # ------------------------------------------------------------------ + # Step 4: File → DB (directory skills written to OpenWebUI) + # ------------------------------------------------------------------ + owui_by_name: Dict[str, str] = { + info["name"]: sid for sid, info in owui_by_id.items() + } + + for dir_name, dir_info in dir_skills.items(): + owui_id = dir_info["owui_id"] + file_content = dir_info["content"] + file_mtime = dir_info["mtime"] + parsed_name, parsed_desc, parsed_body = self._parse_skill_md_meta( + file_content, dir_name + ) + + if owui_id and owui_id in owui_by_id: + # Skill is linked to DB — check if file is newer and content differs + db_info = owui_by_id[owui_id] + # Re-construct what the file would look like from DB to compare + db_file_content = self._build_skill_md_content( + db_info["name"], db_info["description"], db_info["content"] + ) + file_hash = hashlib.md5( + file_content.encode("utf-8", errors="replace") + ).hexdigest() + db_hash = hashlib.md5( + db_file_content.encode("utf-8", errors="replace") + ).hexdigest() + if file_hash != db_hash and file_mtime > db_info["updated_at"]: + # File is newer — push to DB + Skills.update_skill_by_id( + owui_id, + { + "name": parsed_name, + "description": parsed_desc or parsed_name, + "content": parsed_body or file_content, + }, + ) + sync_stats["file_to_db_updates"] += 1 + elif owui_id and owui_id not in owui_by_id: + # .owui_id points to a removed skill in OpenWebUI UI. + # UI is source of truth — delete local dir. + try: + shutil.rmtree(dir_info["path"], ignore_errors=False) + sync_stats["orphan_dir_deletes"] += 1 + except Exception as e: + logger.warning( + f"[Skills Sync] Failed to remove orphaned skill dir '{dir_info['path']}': {e}" + ) + else: + # No OpenWebUI link — try to match by name, then create new + matched_id = owui_by_name.get(parsed_name) + if matched_id: + # Link to existing skill with same name + (dir_info["path"] / ".owui_id").write_text( + matched_id, encoding="utf-8" + ) + sync_stats["file_to_db_links"] += 1 + db_info = owui_by_id[matched_id] + db_file_content = self._build_skill_md_content( + db_info["name"], db_info["description"], db_info["content"] + ) + file_hash = hashlib.md5( + file_content.encode("utf-8", errors="replace") + ).hexdigest() + db_hash = hashlib.md5( + db_file_content.encode("utf-8", errors="replace") + ).hexdigest() + if file_hash != db_hash and file_mtime > db_info["updated_at"]: + Skills.update_skill_by_id( + matched_id, + { + "name": parsed_name, + "description": parsed_desc or parsed_name, + "content": parsed_body or file_content, + }, + ) + sync_stats["file_to_db_updates"] += 1 + else: + # Truly new skill from file — register in OpenWebUI + new_skill = Skills.insert_new_skill( + user_id=user_id, + form_data=SkillForm( + id=str(uuid.uuid4()), + name=parsed_name, + description=parsed_desc or parsed_name, + content=parsed_body or file_content, + meta=SkillMeta(), + is_active=True, + ), + ) + if new_skill: + new_id = str(getattr(new_skill, "id", "") or "") + (dir_info["path"] / ".owui_id").write_text( + new_id, encoding="utf-8" + ) + sync_stats["file_to_db_creates"] += 1 + + logger.debug(f"[Skills Sync] Summary: {sync_stats}") + + except ImportError: + # Running outside OpenWebUI environment — directory is still usable + pass + except Exception as e: + logger.debug(f"[Copilot] Skills sync failed: {e}", exc_info=True) + + return str(shared_dir) + + def _resolve_session_skill_config( + self, + resolved_cwd: str, + user_id: str, + enable_openwebui_skills: bool, + disabled_skills: Optional[List[str]] = None, + ) -> Dict[str, Any]: + skill_directories: List[str] = [] + + # Unified shared directory — always included. + # When enable_openwebui_skills is True, run bidirectional sync first so + # OpenWebUI page skills and directory skills are kept in sync. + if enable_openwebui_skills: + shared_dir = self._sync_openwebui_skills(resolved_cwd, user_id) + else: + shared_dir = self._get_shared_skills_dir(resolved_cwd) + skill_directories.append(shared_dir) + + config: Dict[str, Any] = {} + if skill_directories: + config["skill_directories"] = self._dedupe_preserve_order(skill_directories) + + if disabled_skills: + normalized_disabled = self._dedupe_preserve_order(disabled_skills) + if normalized_disabled: + config["disabled_skills"] = normalized_disabled + + return config + + def _is_code_interpreter_feature_enabled( + self, body: Optional[dict], __metadata__: Optional[dict] = None + ) -> bool: + """Code interpreter must be explicitly enabled by request feature flags.""" + + def _extract_flag(container: Any) -> Optional[bool]: + if not isinstance(container, dict): + return None + features = container.get("features") + if isinstance(features, dict) and "code_interpreter" in features: + return bool(features.get("code_interpreter")) + return None + + # 1) top-level body.features + flag = _extract_flag(body) + if flag is not None: + return flag + + # 2) body.metadata.features + if isinstance(body, dict): + flag = _extract_flag(body.get("metadata")) + if flag is not None: + return flag + + # 3) injected __metadata__.features + flag = _extract_flag(__metadata__) + if flag is not None: + return flag + + return False + async def _extract_system_prompt( self, body: dict, messages: List[dict], request_model: str, real_model_id: str, + code_interpreter_enabled: bool = False, __event_call__=None, debug_enabled: bool = False, ) -> Tuple[Optional[str], str]: @@ -1969,21 +3160,21 @@ class Pipe: ) break - # Append Code Interpreter Warning - code_interpreter_warning = ( - "\n\n[System Note]\n" - "The `execute_code` tool (builtin category: `code_interpreter`) executes code in a remote, ephemeral environment. " - "It cannot access files in your local workspace or persist changes. " - "Use it only for calculation or logic verification, not for file manipulation." - "\n" - "For links returned by `publish_file_from_workspace`, URL formatting is strict: " - "always use relative paths that start with `/api/v1/files/`. " - "Do not output `api/...` and do not prepend any domain." - ) - if system_prompt_content: - system_prompt_content += code_interpreter_warning - else: - system_prompt_content = code_interpreter_warning.strip() + # Append Code Interpreter Warning only when feature is explicitly enabled + if code_interpreter_enabled: + code_interpreter_warning = ( + "\n\n[System Note]\n" + "The `execute_code` tool (builtin category: `code_interpreter`) executes code in a remote, ephemeral environment. " + "It cannot access files in your local workspace or persist changes. " + "Use it only for calculation or logic verification, not for file manipulation." + "\n" + "always use relative paths that start with `/api/v1/files/`. " + "Do not output `api/...` and do not prepend any domain or protocol (e.g., NEVER use `https://same.ai/api/...`)." + ) + if system_prompt_content: + system_prompt_content += code_interpreter_warning + else: + system_prompt_content = code_interpreter_warning.strip() return system_prompt_content, system_prompt_source @@ -2016,15 +3207,19 @@ class Pipe: return cwd - def _build_client_config( - self, body: dict, user_id: str = None, chat_id: str = None - ) -> dict: + def _build_client_config(self, user_id: str = None, chat_id: str = None) -> dict: """Build CopilotClient config from valves and request body.""" cwd = self._get_workspace_dir(user_id=user_id, chat_id=chat_id) + config_dir = self._get_copilot_config_dir() + + # Set environment variable for SDK/CLI to pick up the new config location + os.environ["COPILOTSDK_CONFIG_DIR"] = config_dir + client_config = {} if os.environ.get("COPILOT_CLI_PATH"): client_config["cli_path"] = os.environ["COPILOT_CLI_PATH"] client_config["cwd"] = cwd + client_config["config_dir"] = config_dir if self.valves.LOG_LEVEL: client_config["log_level"] = self.valves.LOG_LEVEL @@ -2032,14 +3227,55 @@ class Pipe: if self.valves.LOG_LEVEL: client_config["log_level"] = self.valves.LOG_LEVEL + # Setup persistent CLI tool installation directories + agent_env = dict(os.environ) + if os.path.exists("/app/backend/data"): + tools_dir = "/app/backend/data/.copilot_tools" + npm_dir = f"{tools_dir}/npm" + venv_dir = f"{tools_dir}/venv" + + try: + os.makedirs(f"{npm_dir}/bin", exist_ok=True) + + # Setup Python Virtual Environment to strictly protect system python + if not os.path.exists(f"{venv_dir}/bin/activate"): + import subprocess + import sys + + subprocess.run( + [ + sys.executable, + "-m", + "venv", + "--system-site-packages", + venv_dir, + ], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + check=True, + ) + + agent_env["NPM_CONFIG_PREFIX"] = npm_dir + agent_env["VIRTUAL_ENV"] = venv_dir + agent_env.pop("PYTHONUSERBASE", None) + agent_env.pop("PIP_USER", None) + + agent_env["PATH"] = ( + f"{npm_dir}/bin:{venv_dir}/bin:{agent_env.get('PATH', '')}" + ) + except Exception as e: + logger.warning(f"Failed to setup Python venv or tool dirs: {e}") + if self.valves.CUSTOM_ENV_VARS: try: custom_env = json.loads(self.valves.CUSTOM_ENV_VARS) if isinstance(custom_env, dict): - client_config["env"] = custom_env + agent_env.update(custom_env) except: pass + client_config["env"] = agent_env + return client_config def _build_session_config( @@ -2055,11 +3291,26 @@ class Pipe: is_admin: bool = False, user_id: str = None, enable_mcp: bool = True, + enable_openwebui_skills: bool = True, + disabled_skills: Optional[List[str]] = None, chat_tool_ids: Optional[list] = None, __event_call__=None, + manage_skills_intent: bool = False, ): """Build SessionConfig for Copilot SDK.""" from copilot.types import SessionConfig, InfiniteSessionConfig + import time + + try: + # -time.timezone is offset in seconds. UTC+8 is 28800. + is_china_tz = (-time.timezone / 3600) == 8.0 + except Exception: + is_china_tz = False + + if is_china_tz: + pkg_mirror_hint = " (Note: Server is in UTC+8. You MUST append `-i https://pypi.tuna.tsinghua.edu.cn/simple` for pip/uv and `--registry=https://registry.npmmirror.com` for npm to prevent network timeouts.)" + else: + pkg_mirror_hint = " (Note: If network is slow or times out, proactively use a fast regional mirror suitable for the current timezone.)" infinite_session_config = None if self.valves.INFINITE_SESSION: @@ -2074,22 +3325,62 @@ class Pipe: if system_prompt_content: system_parts.append(system_prompt_content.strip()) + if manage_skills_intent: + system_parts.append( + "[Skill Management]\n" + "If the user wants to install, create, delete, edit, or list skills, use the `manage_skills` tool.\n" + "Supported operations: list, install, create, edit, delete, show.\n" + "When installing skills that require CLI tools, you MAY run installation commands.\n" + f"To avoid hanging the session, ALWAYS append `-q` or `--silent` to package managers, and confirm unattended installations (e.g., `npm install -g -q ` or `pip install -q `).{pkg_mirror_hint}\n" + "When running `npm install -g`, it will automatically use prefix `/app/backend/data/.copilot_tools/npm`. No need to set the prefix manually, but you MUST be aware this is the installation target.\n" + "When running `pip install`, it operates within an isolated Python Virtual Environment (`VIRTUAL_ENV=/app/backend/data/.copilot_tools/venv`) that has access to system packages (`--system-site-packages`). This protects the system Python while allowing you to use pre-installed generic libraries. DO NOT attempt to bypass this isolation." + ) + # Calculate final path once to ensure consistency resolved_cwd = self._get_workspace_dir(user_id=user_id, chat_id=chat_id) # Inject explicit path context + config_dir = self._get_copilot_config_dir() path_context = ( f"\n[Session Context]\n" f"- **Your Isolated Workspace**: `{resolved_cwd}`\n" f"- **Active User ID**: `{user_id}`\n" f"- **Active Chat ID**: `{chat_id}`\n" + f"- **Skills Directory**: `{self.valves.OPENWEBUI_SKILLS_SHARED_DIR}/shared/` — contains user-installed skills.\n" + f"- **Config Directory**: `{config_dir}` — system configuration (Restricted).\n" + f"- **CLI Tools Path**: `/app/backend/data/.copilot_tools/` — Global tools installed via npm or pip will automatically go here and be in your $PATH. Python tools are strictly isolated in a venv here.\n" "**CRITICAL INSTRUCTION**: You MUST use the above workspace for ALL file operations.\n" "- DO NOT create files in `/tmp` or any other system directories.\n" "- Always interpret 'current directory' as your Isolated Workspace." ) system_parts.append(path_context) + # Available Native Tools Context + native_tools_context = ( + "\n[Available Native System Tools]\n" + "The host environment is rich. Based on the official OpenWebUI Docker deployment baseline (backend image), the following CLI tools are expected to be preinstalled and globally available in $PATH:\n" + "- **Network/Data**: `curl`, `jq`, `netcat-openbsd`\n" + "- **Media/Doc**: `pandoc` (format conversion), `ffmpeg` (audio/video)\n" + "- **Build/System**: `git`, `gcc`, `make`, `build-essential`, `zstd`, `bash`\n" + "- **Python/Runtime**: `python3`, `pip3`, `uv`\n" + f"- **Package Mgr Guidance**: Prefer `uv pip install ` over plain `pip install` for speed and stability.{pkg_mirror_hint}\n" + "- **Verification Rule**: Before installing any CLI/tool dependency, first check availability with `which ` or a lightweight version probe (e.g. ` --version`).\n" + "- **Python Libs**: The active virtual environment inherits `--system-site-packages`. Advanced libraries like `pandas`, `numpy`, `pillow`, `opencv-python-headless`, `pypdf`, `langchain`, `playwright`, `httpx`, and `beautifulsoup4` are ALREADY installed. Try importing them before attempting to install.\n" + ) + system_parts.append(native_tools_context) + system_parts.append(BASE_GUIDELINES) + + # Dynamic Capability Note: Rich UI (HTML Emitters/Iframes) requires OpenWebUI >= 0.8.0 + if not self._is_version_at_least("0.8.0"): + version_note = ( + f"\n**[CRITICAL VERSION NOTE]**\n" + f"The host OpenWebUI version is `{open_webui_version}`, which is older than 0.8.0.\n" + "- **Rich UI Disabled**: Integration features like `type: embeds` or automated iframe overlays are NOT supported.\n" + "- **Protocol Fallback**: You MUST NOT rely on the 'Premium Delivery Protocol' for visuals. Instead, you SHOULD output the HTML code block manually in your message if you want the user to see the result." + ) + system_parts.append(version_note) + if is_admin: system_parts.append(ADMIN_EXTENSIONS) else: @@ -2162,6 +3453,30 @@ class Pipe: cwd=resolved_cwd, __event_call__=__event_call__ ) + session_params.update( + self._resolve_session_skill_config( + resolved_cwd=resolved_cwd, + user_id=user_id, + enable_openwebui_skills=enable_openwebui_skills, + disabled_skills=disabled_skills, + ) + ) + + try: + skill_dirs_dbg = session_params.get("skill_directories") or [] + if skill_dirs_dbg: + logger.info(f"[Copilot] skill_directories={skill_dirs_dbg}") + for sd in skill_dirs_dbg: + path = Path(sd) + skill_md_count = sum( + 1 for p in path.glob("*/SKILL.md") if p.is_file() + ) + logger.info( + f"[Copilot] skill_dir check: {sd} exists={path.exists()} skill_md_count={skill_md_count}" + ) + except Exception as e: + logger.debug(f"[Copilot] skill directory debug check failed: {e}") + return SessionConfig(**session_params) def _build_session_hooks(self, cwd: str, __event_call__=None): @@ -2173,7 +3488,6 @@ class Pipe: async def on_post_tool_use(input_data, invocation): result = input_data.get("result", "") - tool_name = input_data.get("toolName", "") # Logic to detect and move large files saved to /tmp # Pattern: Saved to: /tmp/copilot_result_xxxx.txt @@ -2650,8 +3964,6 @@ class Pipe: debug_enabled: bool = False, token: str = None, enable_mcp: bool = True, - __event_emitter__=None, - user_lang: str = "en-US", ): """Setup environment variables and resolve Copilot CLI path from SDK bundle.""" @@ -2822,7 +4134,7 @@ class Pipe: self, reasoning_effort: str, __event_call__=None, debug_enabled: bool = False ): """ - Dynamically update ~/.copilot/config.json if REASONING_EFFORT is set. + Dynamically update config.json if REASONING_EFFORT is set. This provides a fallback if API injection is ignored by the server. """ if not reasoning_effort: @@ -2831,8 +4143,8 @@ class Pipe: effort = reasoning_effort try: - # Target standard path ~/.copilot/config.json - config_path = os.path.expanduser("~/.copilot/config.json") + # Target dynamic config path + config_path = os.path.join(self._get_copilot_config_dir(), "config.json") config_dir = os.path.dirname(config_path) # Only proceed if directory exists (avoid creating trash types of files if path is wrong) @@ -2857,7 +4169,7 @@ class Pipe: json.dump(data, f, indent=4) self._emit_debug_log_sync( - f"Dynamically updated ~/.copilot/config.json: reasoning_effort='{effort}'", + f"Dynamically updated config.json: reasoning_effort='{effort}'", __event_call__, debug_enabled=debug_enabled, ) @@ -2880,8 +4192,8 @@ class Pipe: debug_enabled: bool = False, enable_mcp: bool = True, ): - """Sync MCP configuration to ~/.copilot/config.json.""" - path = os.path.expanduser("~/.copilot/config.json") + """Sync MCP configuration to dynamic config.json.""" + path = os.path.join(self._get_copilot_config_dir(), "config.json") # If disabled, we should ensure the config doesn't contain stale MCP info if not enable_mcp: @@ -2906,7 +4218,7 @@ class Pipe: if not mcp: return try: - path = os.path.expanduser("~/.copilot/config.json") + path = os.path.join(self._get_copilot_config_dir(), "config.json") os.makedirs(os.path.dirname(path), exist_ok=True) data = {} if os.path.exists(path): @@ -2982,6 +4294,10 @@ class Pipe: # Determine effective MCP settings effective_mcp = user_valves.ENABLE_MCP_SERVER + effective_openwebui_skills = user_valves.ENABLE_OPENWEBUI_SKILLS + effective_disabled_skills = self._parse_csv_items( + user_valves.DISABLED_SKILLS or self.valves.DISABLED_SKILLS + ) # P4: Chat tool_ids whitelist — extract once, reuse for both OpenAPI and MCP chat_tool_ids = None @@ -2997,8 +4313,6 @@ class Pipe: debug_enabled=effective_debug, token=effective_token, enable_mcp=effective_mcp, - __event_emitter__=__event_emitter__, - user_lang=user_lang, ) cwd = self._get_workspace_dir(user_id=user_id, chat_id=chat_id) @@ -3024,6 +4338,9 @@ class Pipe: # Parse user selected model request_model = body.get("model", "") real_model_id = request_model + code_interpreter_enabled = self._is_code_interpreter_feature_enabled( + body, __metadata__ + ) # Determine effective reasoning effort effective_reasoning_effort = ( @@ -3122,7 +4439,8 @@ class Pipe: messages, request_model, real_model_id, - __event_call__, + code_interpreter_enabled=code_interpreter_enabled, + __event_call__=__event_call__, debug_enabled=effective_debug, ) @@ -3152,21 +4470,21 @@ class Pipe: debug_enabled=effective_debug, ) - # 1. Determine user role and construct guidelines - user_data = ( - __user__[0] if isinstance(__user__, (list, tuple)) else (__user__ or {}) - ) - is_admin = user_data.get("role") == "admin" - - system_parts = [] - if system_prompt_content: - system_parts.append(system_prompt_content.strip()) - system_parts.append(BASE_GUIDELINES) - if is_admin: - system_parts.append(ADMIN_EXTENSIONS) - else: - system_parts.append(USER_RESTRICTIONS) - final_system_msg = "\n".join(system_parts) + # Skill-manager intent diagnostics/routing hint (without disabling other skills). + manage_skills_intent = self._is_manage_skills_intent(last_text) + if manage_skills_intent: + try: + await self._emit_debug_log( + "[Skills] Skill management intent detected. `manage_skills` tool routing enabled.", + __event_call__, + debug_enabled=effective_debug, + ) + except Exception as e: + await self._emit_debug_log( + f"[Skills] Skill-manager intent diagnostics failed: {e}", + __event_call__, + debug_enabled=effective_debug, + ) # Determine prompt strategy # If we have a chat_id, we try to resume session. @@ -3217,9 +4535,12 @@ class Pipe: if not is_byok_model: self._sync_copilot_config(effective_reasoning_effort, __event_call__) + # Shared state for delayed HTML embeds (Premium Experience) + pending_embeds = [] + # Initialize Client client = CopilotClient( - self._build_client_config(body, user_id=user_id, chat_id=chat_id) + self._build_client_config(user_id=user_id, chat_id=chat_id) ) should_stop_client = True try: @@ -3229,12 +4550,13 @@ class Pipe: custom_tools = await self._initialize_custom_tools( body=body, __user__=__user__, + __event_emitter__=__event_emitter__, __event_call__=__event_call__, __request__=__request__, __metadata__=__metadata__, + pending_embeds=pending_embeds, ) if custom_tools: - tool_names = [t.name for t in custom_tools] await self._emit_debug_log( f"Enabled {len(custom_tools)} tools (Custom/Built-in)", __event_call__, @@ -3258,7 +4580,6 @@ class Pipe: # Create or Resume Session session = None - is_new_session = True # Build BYOK Provider Config provider_config = None @@ -3283,6 +4604,9 @@ class Pipe: if chat_id: try: + resolved_cwd = self._get_workspace_dir( + user_id=user_id, chat_id=chat_id + ) # Prepare resume config (Requires github-copilot-sdk >= 0.1.23) resume_params = { "model": real_model_id, @@ -3305,29 +4629,73 @@ class Pipe: # Always None: let CLI built-ins (bash etc.) remain available. resume_params["available_tools"] = None + resume_params.update( + self._resolve_session_skill_config( + resolved_cwd=resolved_cwd, + user_id=user_id, + enable_openwebui_skills=effective_openwebui_skills, + disabled_skills=effective_disabled_skills, + ) + ) + try: + skill_dirs_dbg = resume_params.get("skill_directories") or [] + if skill_dirs_dbg: + logger.info( + f"[Copilot] resume skill_directories={skill_dirs_dbg}" + ) + for sd in skill_dirs_dbg: + path = Path(sd) + skill_md_count = sum( + 1 for p in path.glob("*/SKILL.md") if p.is_file() + ) + logger.info( + f"[Copilot] resume skill_dir check: {sd} exists={path.exists()} skill_md_count={skill_md_count}" + ) + except Exception as e: + logger.debug( + f"[Copilot] resume skill directory debug check failed: {e}" + ) + # Always inject the latest system prompt in 'replace' mode # This handles both custom models and user-defined system messages system_parts = [] if system_prompt_content: system_parts.append(system_prompt_content.strip()) + if manage_skills_intent: + system_parts.append( + "[Skill Routing Hint]\n" + "The user is asking to install/manage skills. Use the `manage_skills` tool first for deterministic operations " + "(list/install/create/edit/delete/show). Do not run skill names as shell commands." + ) + # Calculate and inject path context for resumed session - resolved_cwd = self._get_workspace_dir( - user_id=user_id, chat_id=chat_id - ) path_context = ( f"\n[Session Context]\n" f"- **Your Isolated Workspace**: `{resolved_cwd}`\n" f"- **Active User ID**: `{user_id}`\n" f"- **Active Chat ID**: `{chat_id}`\n" + f"- **Skills Directory**: `{self.valves.OPENWEBUI_SKILLS_SHARED_DIR}/shared/` — contains user skills (`SKILL.md`-based). For management operations, use the `manage_skills` tool.\n" "**CRITICAL INSTRUCTION**: You MUST use the above workspace for ALL file operations.\n" "- DO NOT create files in `/tmp` or any other system directories.\n" + "- Use the `manage_skills` tool for skill install/list/create/edit/delete/show operations.\n" "- If a tool output is too large, save it to a file within your workspace, NOT `/tmp`.\n" "- Always interpret 'current directory' as your Isolated Workspace." ) system_parts.append(path_context) system_parts.append(BASE_GUIDELINES) + + # Dynamic Capability Note: Rich UI (HTML Emitters/Iframes) requires OpenWebUI >= 0.8.0 + if not self._is_version_at_least("0.8.0"): + version_note = ( + f"\n**[CRITICAL VERSION NOTE]**\n" + f"The host OpenWebUI version is `{open_webui_version}`, which is older than 0.8.0.\n" + "- **Rich UI Disabled**: Integration features like `type: embeds` or automated iframe overlays are NOT supported.\n" + "- **Protocol Fallback**: You MUST NOT rely on the 'Premium Delivery Protocol' for visuals. Instead, you SHOULD output the HTML code block manually in your message if you want the user to see the result." + ) + system_parts.append(version_note) + if is_admin: system_parts.append(ADMIN_EXTENSIONS) else: @@ -3368,7 +4736,6 @@ class Pipe: f"Successfully resumed session {chat_id} with model {real_model_id}", __event_call__, ) - is_new_session = False except Exception as e: await self._emit_debug_log( f"Session {chat_id} not found or failed to resume ({str(e)}), creating new.", @@ -3376,7 +4743,6 @@ class Pipe: ) if session is None: - is_new_session = True session_config = self._build_session_config( chat_id, real_model_id, @@ -3389,6 +4755,9 @@ class Pipe: is_admin=is_admin, user_id=user_id, enable_mcp=effective_mcp, + enable_openwebui_skills=effective_openwebui_skills, + disabled_skills=effective_disabled_skills, + manage_skills_intent=manage_skills_intent, chat_tool_ids=chat_tool_ids, __event_call__=__event_call__, ) @@ -3459,6 +4828,7 @@ class Pipe: show_thinking=show_thinking, debug_enabled=effective_debug, user_lang=user_lang, + pending_embeds=pending_embeds, ) else: try: @@ -3505,20 +4875,33 @@ class Pipe: show_thinking: bool = True, debug_enabled: bool = False, user_lang: str = "en-US", + pending_embeds: List[dict] = None, ) -> AsyncGenerator: """ Stream response from Copilot SDK, handling various event types. Follows official SDK patterns for event handling and streaming. """ - from copilot.generated.session_events import SessionEventType - queue = asyncio.Queue() done = asyncio.Event() SENTINEL = object() # Use local state to handle concurrency and tracking - state = {"thinking_started": False, "content_sent": False} + state = { + "thinking_started": False, + "content_sent": False, + "last_status_desc": None, + "idle_reached": False, + "session_finalized": False, + } has_content = False # Track if any content has been yielded active_tools = {} # Map tool_call_id to tool_name + skill_invoked_in_turn = False + stream_start_ts = time.monotonic() + last_wait_status_ts = 0.0 + wait_status_interval = 15.0 + + IDLE_SENTINEL = object() + ERROR_SENTINEL = object() + SENTINEL = object() def get_event_type(event) -> str: """Extract event type as string, handling both enum and string types.""" @@ -3554,8 +4937,109 @@ class Pipe: """ event_type = get_event_type(event) + # --- Status Emission Helper --- + async def _emit_status_helper(description: str, is_done: bool = False): + if not __event_emitter__: + return + try: + # BLOCKING LOCK: If we are in the safe-haven of turn completion, + # discard any stray async status updates from earlier pending tasks. + if state.get( + "session_finalized" + ) and description != self._get_translation( + user_lang, "status_task_completed" + ): + return + + # Optimized emission: we try to minimize context switches + + # 1. Close the OLD one if it's different + if ( + state.get("last_status_desc") + and state["last_status_desc"] != description + ): + try: + await __event_emitter__( + { + "type": "status", + "data": { + "description": state["last_status_desc"], + "done": True, + }, + } + ) + except: + pass + + # CRITICAL: Re-check session_finalized after the inner await above. + # The coroutine may have been suspended at await point #1 while the + # main loop set session_finalized=True and emitted the final done=True. + # Without this re-check, the done=False emission below would fire + # AFTER all finalization, becoming the last statusHistory entry + # and leaving a permanent shimmer on the UI. + if state.get( + "session_finalized" + ) and description != self._get_translation( + user_lang, "status_task_completed" + ): + return + + # 2. Emit the requested status + await __event_emitter__( + { + "type": "status", + "data": {"description": description, "done": is_done}, + } + ) + + # 3. Track the active status + if not is_done: + state["last_status_desc"] = description + elif state.get("last_status_desc") == description: + state["last_status_desc"] = None + except: + pass + + def emit_status(desc: str, is_done: bool = False): + """Sync wrapper to schedule the async status emission.""" + if __event_emitter__ and desc: + # We use a task because this is often called from sync tool handlers + asyncio.create_task(_emit_status_helper(desc, is_done)) + + # === Turn Management Events === + if event_type == "assistant.turn_start": + self._emit_debug_log_sync( + "Assistant Turn Started", + __event_call__, + debug_enabled=debug_enabled, + ) + + initial_status = self._get_translation( + user_lang, "status_assistant_processing" + ) + # Route through emit_status → _emit_status_helper so the session_finalized + # guard is respected. Direct create_task(__event_emitter__) bypasses the guard + # and can fire AFTER finalization, leaving a stale done=False spinner. + emit_status(initial_status) + + elif event_type == "assistant.intent": + intent = safe_get_data_attr(event, "intent") + if intent: + self._emit_debug_log_sync( + f"Assistant Intent: {intent}", + __event_call__, + debug_enabled=debug_enabled, + ) + emit_status(f"{intent}...") + # === Message Delta Events (Primary streaming content) === - if event_type == "assistant.message_delta": + elif event_type == "assistant.message_delta": + # Close any pending thinking status when content starts + if not state["content_sent"]: + state["content_sent"] = True + if state.get("last_status_desc"): + emit_status(state["last_status_desc"], is_done=True) + # Official: event.data.delta_content for Python SDK delta = safe_get_data_attr( event, "delta_content" @@ -3579,6 +5063,10 @@ class Pipe: ) if content: state["content_sent"] = True + # Close current status + if state.get("last_status_desc"): + emit_status(state["last_status_desc"], is_done=True) + if state["thinking_started"]: queue.put_nowait("\n\n") state["thinking_started"] = False @@ -3618,6 +5106,33 @@ class Pipe: if state["thinking_started"]: queue.put_nowait(reasoning) + # === Skill Invocation Events === + elif event_type == "skill.invoked": + nonlocal skill_invoked_in_turn + skill_invoked_in_turn = True + skill_name = ( + safe_get_data_attr(event, "name") + or safe_get_data_attr(event, "skill_name") + or safe_get_data_attr(event, "skill") + or safe_get_data_attr(event, "id") + or "unknown-skill" + ) + skill_status_text = self._get_translation( + user_lang, "status_skill_invoked", skill=skill_name + ) + + self._emit_debug_log_sync( + f"Skill Invoked: {skill_name}", + __event_call__, + debug_enabled=debug_enabled, + ) + + # Make invocation visible in chat stream to avoid "skills loaded but feels unknown" confusion. + queue.put_nowait(f"\n> 🧩 **{skill_status_text}**\n") + + # Also send status bubble when possible. + emit_status(skill_status_text, is_done=True) + # === Tool Execution Events === elif event_type == "tool.execution_start": tool_name = ( @@ -3638,10 +5153,42 @@ class Pipe: except: pass + # Try to detect filename in arguments for better status (e.g., create_file, bash) + tool_status_text = self._get_translation( + user_lang, + "status_tool_using", + name=tool_name, + ) + + # Enhanced filenames detection for common tools + filename_hint = ( + tool_args.get("filename") + or tool_args.get("file") + or tool_args.get("path") + ) + if not filename_hint and tool_name == "bash": + command = tool_args.get("command", "") + # Detect output file from common bash redirect patterns (>>, >, tee, cat >) + # Use alternation group (not char class) to avoid matching '|' pipe symbols + match = re.search(r"(?:>>|>|tee|cat\s*>)\s*([^\s;&|<>]+)", command) + if match: + candidate = match.group(1).strip().split("/")[-1] + # Only use as hint if it looks like a filename (has extension or is not a flag) + if ( + candidate + and not candidate.startswith("-") + and "." in candidate + ): + filename_hint = candidate + + if filename_hint: + tool_status_text += f" ({filename_hint})" + if tool_call_id: active_tools[tool_call_id] = { "name": tool_name, "arguments": tool_args, + "status_text": tool_status_text if __event_emitter__ else None, } # Close thinking tag if open before showing tool @@ -3649,8 +5196,8 @@ class Pipe: queue.put_nowait("\n\n") state["thinking_started"] = False - # Note: We do NOT emit a done="false" card here to avoid card duplication - # (unless we have a way to update text which SSE content stream doesn't) + # Show status bubble for tool usage + emit_status(tool_status_text) self._emit_debug_log_sync( f"Tool Start: {tool_name}", @@ -3662,13 +5209,17 @@ class Pipe: tool_call_id = safe_get_data_attr(event, "tool_call_id", "") tool_info = active_tools.get(tool_call_id) - # Handle both old string format and new dict format - if isinstance(tool_info, str): + tool_name = "tool" + status_text = None + if isinstance(tool_info, dict): + tool_name = tool_info.get("name", "tool") + status_text = tool_info.get("status_text") + elif isinstance(tool_info, str): tool_name = tool_info - elif isinstance(tool_info, dict): - tool_name = tool_info.get("name", "Unknown Tool") - else: - tool_name = "Unknown Tool" + + # Mark tool status as done if it was the last one + if status_text: + emit_status(status_text, is_done=True) # Try to get result content result_content = "" @@ -3685,6 +5236,11 @@ class Pipe: result_content = json.dumps( result_obj, indent=2, ensure_ascii=False ) + elif isinstance(result_obj, str): + result_content = result_obj + result_type = ( + safe_get_data_attr(event, "type", "success") or "success" + ) except Exception as e: self._emit_debug_log_sync( f"Error extracting result: {e}", @@ -3694,128 +5250,143 @@ class Pipe: result_type = "failure" result_content = f"Error: {str(e)}" + # User-friendly completion status (success/failure) after the tool finishes. + # We emit this as done=True so it cleanly replaces transient "Using tool..." states. + if str(result_type).lower() in {"success", "ok", "completed"}: + emit_status( + self._get_translation( + user_lang, "status_tool_done", name=tool_name + ), + is_done=True, + ) + else: + emit_status( + self._get_translation( + user_lang, "status_tool_failed", name=tool_name + ), + is_done=True, + ) + # Display tool result with improved formatting - if result_content: - status_icon = "✅" if result_type == "success" else "❌" + # --- TODO Sync Logic (File + DB) --- + if tool_name == "update_todo" and result_type == "success": + try: + # Extract todo content with fallback strategy + todo_text = "" - # --- TODO Sync Logic (File + DB) --- - if tool_name == "update_todo" and result_type == "success": - try: - # Extract todo content with fallback strategy - todo_text = "" + # 1. Try detailedContent (Best source) + if isinstance(result_obj, dict) and result_obj.get( + "detailedContent" + ): + todo_text = result_obj["detailedContent"] + # 2. Try content (Second best) + elif isinstance(result_obj, dict) and result_obj.get("content"): + todo_text = result_obj["content"] + elif hasattr(result_obj, "content"): + todo_text = result_obj.content - # 1. Try detailedContent (Best source) - if isinstance(result_obj, dict) and result_obj.get( - "detailedContent" - ): - todo_text = result_obj["detailedContent"] - # 2. Try content (Second best) - elif isinstance(result_obj, dict) and result_obj.get( - "content" - ): - todo_text = result_obj["content"] - elif hasattr(result_obj, "content"): - todo_text = result_obj.content - - # 3. Fallback: If content is just a status message, try to recover from arguments - if ( - not todo_text or len(todo_text) < 50 - ): # Threshold to detect "TODO list updated" - if tool_call_id in active_tools: - args = active_tools[tool_call_id].get( - "arguments", {} + # 3. Fallback: If content is just a status message, try to recover from arguments + if ( + not todo_text or len(todo_text) < 50 + ): # Threshold to detect "TODO list updated" + if tool_call_id in active_tools: + args = active_tools[tool_call_id].get("arguments", {}) + if isinstance(args, dict) and "todos" in args: + todo_text = args["todos"] + self._emit_debug_log_sync( + f"Recovered TODO from arguments (Result was too short)", + __event_call__, + debug_enabled=debug_enabled, ) - if isinstance(args, dict) and "todos" in args: - todo_text = args["todos"] - self._emit_debug_log_sync( - f"Recovered TODO from arguments (Result was too short)", - __event_call__, - debug_enabled=debug_enabled, - ) - if todo_text: - # Use the explicit chat_id passed to stream_response - target_chat_id = chat_id or "default" + if todo_text: + # Use the explicit chat_id passed to stream_response + target_chat_id = chat_id or "default" - # 1. Sync to file - ws_dir = self._get_workspace_dir( - user_id=user_id, chat_id=target_chat_id - ) - todo_path = os.path.join(ws_dir, "TODO.md") - with open(todo_path, "w") as f: - f.write(todo_text) + # 1. Sync to file + ws_dir = self._get_workspace_dir( + user_id=user_id, chat_id=target_chat_id + ) + todo_path = os.path.join(ws_dir, "TODO.md") + with open(todo_path, "w") as f: + f.write(todo_text) - # 2. Sync to Database & Emit Status - self._save_todo_to_db( - target_chat_id, - todo_text, - __event_emitter__=__event_emitter__, - __event_call__=__event_call__, - debug_enabled=debug_enabled, - ) + # 2. Sync to Database & Emit Status + self._save_todo_to_db( + target_chat_id, + todo_text, + __event_emitter__=__event_emitter__, + __event_call__=__event_call__, + debug_enabled=debug_enabled, + ) - self._emit_debug_log_sync( - f"Synced TODO to file and DB (Chat: {target_chat_id})", - __event_call__, - debug_enabled=debug_enabled, - ) - except Exception as sync_err: self._emit_debug_log_sync( - f"TODO Sync Failed: {sync_err}", + f"Synced TODO to file and DB (Chat: {target_chat_id})", __event_call__, debug_enabled=debug_enabled, ) - # ------------------------ - - # --- Build native OpenWebUI 0.8.3 tool_calls block --- - # Serialize input args (from execution_start) - tool_args_for_block = {} - if tool_call_id and tool_call_id in active_tools: - tool_args_for_block = active_tools[tool_call_id].get( - "arguments", {} + except Exception as sync_err: + self._emit_debug_log_sync( + f"TODO Sync Failed: {sync_err}", + __event_call__, + debug_enabled=debug_enabled, ) + # ------------------------ - try: - args_json_str = json.dumps( - tool_args_for_block, ensure_ascii=False - ) - except Exception: - args_json_str = "{}" - - def escape_html_attr(s: str) -> str: - if not isinstance(s, str): - return "" - return ( - str(s) - .replace("&", "&") - .replace("<", "<") - .replace(">", ">") - .replace('"', """) - .replace("\n", " ") - .replace("\r", " ") - ) - - # MUST escape both arguments and result with " and to satisfy OpenWebUI's strict regex /="([^"]*)"/ - # OpenWebUI `marked` extension does not match multiline attributes properly without - args_for_attr = ( - escape_html_attr(args_json_str) if args_json_str else "{}" + # --- Build native OpenWebUI 0.8.3 tool_calls block --- + # Serialize input args (from execution_start) + tool_args_for_block = {} + if tool_call_id and tool_call_id in active_tools: + tool_args_for_block = active_tools[tool_call_id].get( + "arguments", {} ) - result_for_attr = escape_html_attr(result_content) + tool_name = active_tools[tool_call_id].get("name", tool_name) - # Emit the unified native tool_calls block: - # OpenWebUI 0.8.3 frontend regex explicitly expects: name="xxx" arguments="..." result="..." done="true" - # CRITICAL:
tag MUST be followed immediately by \n for the frontend Markdown extension to parse it! - tool_block = ( - f'\n
\n' - f"Tool Executed\n" - f"
\n\n" + try: + args_json_str = json.dumps(tool_args_for_block, ensure_ascii=False) + except Exception: + args_json_str = "{}" + + def escape_html_attr(s: str) -> str: + if not isinstance(s, str): + return "" + return ( + str(s) + .replace("&", "&") + .replace("<", "<") + .replace(">", ">") + .replace('"', """) + .replace("\n", " ") + .replace("\r", " ") ) - queue.put_nowait(tool_block) + + # MUST escape both arguments and result with " and to satisfy OpenWebUI's strict regex /="([^"]*)"/ + args_for_attr = ( + escape_html_attr(args_json_str) if args_json_str else "{}" + ) + # Use "Success" if result_content is empty to ensure card renders + result_for_attr = escape_html_attr(result_content or "Success") + + # Emit the unified native tool_calls block: + # OpenWebUI 0.8.3 frontend regex explicitly expects: name="xxx" arguments="..." result="..." done="true" + tool_block = ( + f'\n
\n' + f"Tool Executed\n" + f"
\n\n" + ) + state["content_sent"] = True + queue.put_nowait(tool_block) + + self._emit_debug_log_sync( + f"Tool Complete: {tool_name} - {result_type}", + __event_call__, + debug_enabled=debug_enabled, + ) elif event_type == "tool.execution_progress": # Tool execution progress update (for long-running tools) @@ -3830,9 +5401,16 @@ class Pipe: progress = safe_get_data_attr(event, "progress", 0) message = safe_get_data_attr(event, "message", "") - if message: - progress_display = f"\n> 🔄 **{tool_name}**: {message}\n" - queue.put_nowait(progress_display) + status_text = self._get_translation( + user_lang, + "status_tool_progress", + name=tool_name, + progress=progress, + msg=message, + ) + + # Route through emit_status to respect session_finalized guard + emit_status(status_text) self._emit_debug_log_sync( f"Tool Progress: {tool_name} - {progress}%", @@ -3860,6 +5438,69 @@ class Pipe: debug_enabled=debug_enabled, ) + # === Sub-agent Events === + elif event_type == "subagent.started": + agent_name = safe_get_data_attr(event, "name") or "Agent" + self._emit_debug_log_sync( + f"Sub-agent Started: {agent_name}", + __event_call__, + debug_enabled=debug_enabled, + ) + emit_status( + self._get_translation( + user_lang, "status_subagent_start", name=agent_name + ) + ) + + elif event_type == "subagent.completed": + agent_name = safe_get_data_attr(event, "name") or "Agent" + self._emit_debug_log_sync( + f"Sub-agent Completed: {agent_name}", + __event_call__, + debug_enabled=debug_enabled, + ) + emit_status( + self._get_translation( + user_lang, "status_subagent_start", name=agent_name + ), + is_done=True, + ) + + elif event_type == "subagent.failed": + agent_name = safe_get_data_attr(event, "name") or "Agent" + error = safe_get_data_attr(event, "error") or "Unknown error" + self._emit_debug_log_sync( + f"Sub-agent Failed: {agent_name} - {error}", + __event_call__, + debug_enabled=debug_enabled, + ) + emit_status( + self._get_translation( + user_lang, "status_subagent_start", name=agent_name + ), + is_done=True, + ) + self._emit_debug_log_sync( + f"Sub-agent Failed: {agent_name} - {error}", + __event_call__, + debug_enabled=debug_enabled, + ) + + elif event_type == "assistant.turn_end": + self._emit_debug_log_sync( + "Assistant Turn Ended", + __event_call__, + debug_enabled=debug_enabled, + ) + if state.get("last_status_desc"): + emit_status(state["last_status_desc"], is_done=True) + + # Send the clean Task Completed status + emit_status( + self._get_translation(user_lang, "status_task_completed"), + is_done=True, + ) + # === Usage Statistics Events === elif event_type == "assistant.usage": # Token usage for current assistant turn @@ -3873,27 +5514,44 @@ class Pipe: # Cumulative session usage information pass + elif event_type == "session.compaction_start": + self._emit_debug_log_sync( + "Session Compaction Started", + __event_call__, + debug_enabled=debug_enabled, + ) + emit_status(self._get_translation(user_lang, "status_compaction_start")) + elif event_type == "session.compaction_complete": self._emit_debug_log_sync( "Session Compaction Completed", __event_call__, debug_enabled=debug_enabled, ) + emit_status( + self._get_translation(user_lang, "status_compaction_complete"), + is_done=True, + ) elif event_type == "session.idle": - # Session finished processing - signal completion - done.set() + # Session finished processing - signal to the generator loop to finalize + state["idle_reached"] = True try: - queue.put_nowait(SENTINEL) + queue.put_nowait(IDLE_SENTINEL) except: pass elif event_type == "session.error": error_msg = safe_get_data_attr(event, "message", "Unknown Error") + emit_status( + self._get_translation( + user_lang, "status_session_error", error=error_msg + ), + is_done=True, + ) queue.put_nowait(f"\n[Error: {error_msg}]") - done.set() try: - queue.put_nowait(SENTINEL) + queue.put_nowait(ERROR_SENTINEL) except: pass @@ -3963,7 +5621,153 @@ class Pipe: queue.get(), timeout=float(self.valves.TIMEOUT) ) if chunk is SENTINEL: + done.set() break + + if chunk is IDLE_SENTINEL: + # --- [FINAL STEP] Emit Rich UI Integrated View & Task Completion --- + if __event_emitter__: + try: + # 1b. Clear any tracked last tool/intent status + if state.get("last_status_desc"): + await __event_emitter__( + { + "type": "status", + "data": { + "description": state[ + "last_status_desc" + ], + "done": True, + }, + } + ) + state["last_status_desc"] = None + + # 1c. CRITICAL: Close all tool statuses and REWRITE their description + # In some versions of OpenWebUI, just marking as done doesn't update the summary. + # We explicitly change the text to 'Completed' to force UI refresh. + for _tool_id, _tool_info in active_tools.items(): + if isinstance(_tool_info, dict) and _tool_info.get( + "status_text" + ): + try: + # Append a checkmark to the tool status to force a string change + final_tool_status = f"✅ {_tool_info['status_text'].replace('...', '')}" + await __event_emitter__( + { + "type": "status", + "data": { + "description": final_tool_status, + "done": True, + }, + } + ) + except Exception: + pass + + # 2. Emit Rich UI components (richui type) + if pending_embeds: + for embed in pending_embeds: + if embed.get("type") == "richui": + # Status update + await __event_emitter__( + { + "type": "status", + "data": { + "description": self._get_translation( + user_lang, + "status_publishing_file", + filename=embed["filename"], + ), + "done": True, + }, + } + ) + # Success notification + await __event_emitter__( + { + "type": "notification", + "data": { + "type": "success", + "content": self._get_translation( + user_lang, "publish_success" + ), + }, + } + ) + # Standard OpenWebUI Embed Structure: type: "embeds", data: {"embeds": [content]} + await __event_emitter__( + { + "type": "embeds", + "data": { + "embeds": [embed["content"]] + }, + } + ) + + # 3. LOCK internal status emission for background tasks + # (Stray Task A from tool.execution_complete will now be discarded) + state["session_finalized"] = True + + # 4. [PULSE LOCK] Trigger a UI refresh by pulsing a non-done status + # This forces OpenWebUI's summary line to re-evaluate the description. + # 4. [PULSE LOCK] Trigger a UI refresh by pulsing a non-done status + finalized_msg = "✔️ " + self._get_translation( + user_lang, "status_task_completed" + ) + + await __event_emitter__( + { + "type": "status", + "data": { + "description": finalized_msg, + "done": False, + }, + } + ) + + # Increased window to ensure the 'done: False' is processed before the pipe closes + await asyncio.sleep(0.2) + + # 5. FINAL emit + await __event_emitter__( + { + "type": "status", + "data": { + "description": finalized_msg, + "done": True, + "hidden": False, + }, + } + ) + except Exception as emit_error: + self._emit_debug_log_sync( + f"Final emission error: {emit_error}", + __event_call__, + debug_enabled=debug_enabled, + ) + + done.set() + break + + if chunk is ERROR_SENTINEL: + # Extract error message if possible or use default + if __event_emitter__: + try: + await __event_emitter__( + { + "type": "status", + "data": { + "description": "Error during processing", + "done": True, + }, + } + ) + except: + pass + done.set() + break + if chunk: has_content = True try: @@ -3979,26 +5783,36 @@ class Pipe: except asyncio.TimeoutError: if done.is_set(): break - if __event_emitter__ and debug_enabled: + + now_ts = time.monotonic() + if __event_emitter__ and ( + now_ts - last_wait_status_ts >= wait_status_interval + ): + elapsed = int(now_ts - stream_start_ts) try: asyncio.create_task( __event_emitter__( { "type": "status", "data": { - "description": f"Waiting for response ({self.valves.TIMEOUT}s exceeded)...", - "done": True, + "description": self._get_translation( + user_lang, + "status_still_working", + seconds=elapsed, + ), + "done": False, }, } ) ) - except: + except Exception: pass + last_wait_status_ts = now_ts continue while not queue.empty(): chunk = queue.get_nowait() - if chunk is SENTINEL: + if chunk in (SENTINEL, IDLE_SENTINEL, ERROR_SENTINEL): break if chunk: has_content = True @@ -4028,6 +5842,54 @@ class Pipe: except: pass # Connection already closed finally: + # Final Status Cleanup: Emergency mark all as done if not already + if __event_emitter__: + try: + # Clear any specific tool/intent statuses tracked + if state.get("last_status_desc"): + await __event_emitter__( + { + "type": "status", + "data": { + "description": state["last_status_desc"], + "done": True, + }, + } + ) + + # Clear all active tool statuses before final completion status, + # so Task completed remains the last visible summary in OpenWebUI. + for tool_id, tool_info in active_tools.items(): + if isinstance(tool_info, dict) and tool_info.get("status_text"): + try: + await __event_emitter__( + { + "type": "status", + "data": { + "description": tool_info["status_text"], + "done": True, + }, + } + ) + except: + pass + + # Final final confirmation to prevent any stuck status bubbles + await __event_emitter__( + { + "type": "status", + "data": { + "description": self._get_translation( + user_lang, "status_task_completed" + ), + "done": True, + "hidden": False, + }, + } + ) + except: + pass + unsubscribe() # Cleanup client and session try: diff --git a/plugins/pipes/github-copilot-sdk/v0.9.0.md b/plugins/pipes/github-copilot-sdk/v0.9.0.md new file mode 100644 index 0000000..a72d41c --- /dev/null +++ b/plugins/pipes/github-copilot-sdk/v0.9.0.md @@ -0,0 +1,101 @@ +# 🚀 GitHub Copilot SDK Pipe v0.9.0: Copilot SDK Skills Core Capabilities & Extended Delivery + +**GitHub Copilot SDK Pipe v0.9.0** centers on making **Copilot SDK Skills** a first-class capability (native directory support + bidirectional sync + deterministic `manage_skills` lifecycle), then extends delivery with improved publishing behavior. + +--- + +## ⭐ Release Highlights + +- **🧩 Copilot SDK Skills Support (Core)**: Native support for SDK skill directories (`SKILL.md` + optional resources), so skills are loaded as first-class runtime context. +- **🔄 OpenWebUI Skills Bridge (Extension)**: Bidirectional sync between OpenWebUI and local SDK skill folders. +- **🛠️ `manage_skills` Tool (Extension)**: In-session deterministic skill lifecycle tool for `list/install/create/edit/show/delete`. +- **📤 Publishing Update**: HTML supports `artifacts` / `richui`; PDF is delivered via Markdown links. +- **📁 Folder-based Skills**: A skill can include `SKILL.md` plus templates/scripts/resources. +- **🧱 Environment Refactor**: New persistent config/workspace/toolchain layout under `/app/backend/data` with stricter isolation and better reproducibility. + +--- + +## 📦 Quick Installation + +- **GitHub Copilot SDK (Pipe)**: [Install v0.9.0](https://openwebui.com/posts/ce96f7b4-12fc-4ac3-9a01-875713e69359) +- **GitHub Copilot SDK (Filter)**: [Install v0.1.3](https://openwebui.com/posts/403a62ee-a596-45e7-be65-fab9cc249dd6) + +--- + +## 🚀 What's New in v0.9.0 + +### 1. OpenWebUI Skills ↔ SDK Sync + +All skills are unified under `{OPENWEBUI_SKILLS_SHARED_DIR}/shared/` and synced automatically between DB and files. + +### 2. `manage_skills` Tool + +The built-in tool (not a skill) supports: + +- `list` +- `install` (single URL / multiple URLs) +- `create` +- `edit` +- `show` +- `delete` + +### 3. Publishing Behavior + +- HTML: `artifacts` (code-block embed output) or `richui` (emitter auto-render) mode. +- PDF: Markdown links only (no iframe embed). + +### 4. Environment Refactor (Detailed) + +This release includes a substantial runtime environment redesign: + +- **Persistent SDK config directory** + - Introduced `COPILOTSDK_CONFIG_DIR`. + - Resolution order: Valve override -> `/app/backend/data/.copilot` -> `~/.copilot` fallback. + - Ensures session/config state survives container restarts in standard OpenWebUI deployments. + +- **User + Chat isolated workspace model** + - Workspace root is now `/app/backend/data/copilot_workspace` (container mode). + - Effective path is isolated per user and chat: `/app/backend/data/copilot_workspace/{user_id}/{chat_id}`. + - `user_id`/`chat_id` are sanitized to prevent traversal. + +- **Deterministic CLI tool runtime under data volume** + - Global tool root: `/app/backend/data/.copilot_tools`. + - NPM prefix is forced to `/app/backend/data/.copilot_tools/npm` via `NPM_CONFIG_PREFIX`. + - Python tool execution is isolated in `/app/backend/data/.copilot_tools/venv` via `VIRTUAL_ENV`. + - `PYTHONUSERBASE` and `PIP_USER` are cleared to avoid leaking user-site installs. + - PATH is rewritten to prioritize npm/venv bins for consistent tool resolution. + +- **CLI bootstrap hardening** + - Copilot CLI discovery chain: `COPILOT_CLI_PATH` -> system `PATH` -> SDK bundled binary. + - `COPILOT_AUTO_UPDATE=false` to prevent uncontrolled runtime drift. + - Successful discovery auto-injects CLI bin dir into PATH. + +- **System prompt path context hardening** + - Session context now injects explicit paths: isolated workspace, skills directory, config directory, and tools path. + - This reduces ambiguity and prevents accidental writes outside the approved workspace. + +- **Regular-user security boundary refinement** + - Non-admin users still cannot access env vars/db/global internals. + - They can read **their own current chat session metadata** under `COPILOTSDK_CONFIG_DIR` for troubleshooting. + - Cross-user session access remains forbidden. + +### 5. New Valves + +- `COPILOTSDK_CONFIG_DIR` +- `ENABLE_OPENWEBUI_SKILLS` +- `OPENWEBUI_SKILLS_SHARED_DIR` +- `DISABLED_SKILLS` + +--- + +## 🔄 Migration + +- No breaking changes from v0.8.0. +- `ENABLE_WORKSPACE_TOOLS` replaced by `ENABLE_OPENWEBUI_SKILLS`. + +--- + +## 📚 Detailed Docs + +- README: +- manage_skills Tool Guide: diff --git a/plugins/pipes/github-copilot-sdk/v0.9.0_CN.md b/plugins/pipes/github-copilot-sdk/v0.9.0_CN.md new file mode 100644 index 0000000..897870b --- /dev/null +++ b/plugins/pipes/github-copilot-sdk/v0.9.0_CN.md @@ -0,0 +1,101 @@ +# 🚀 GitHub Copilot SDK Pipe v0.9.0:Copilot SDK Skills 核心能力与扩展交付 + +**GitHub Copilot SDK Pipe v0.9.0** 的核心是将 **Copilot SDK Skills** 提升为一等能力(原生目录支持 + 双向同步 + `manage_skills` 确定性管理),并在此基础上增强发布交付体验。 + +--- + +## ⭐ 版本亮点 + +- **🧩 Copilot SDK Skills 原生支持(核心)**:原生支持 SDK 技能目录(`SKILL.md` + 可选资源文件),技能可作为一等运行时上下文加载。 +- **🔄 OpenWebUI Skills 桥接(扩展)**:OpenWebUI 与本地 SDK 技能目录双向同步。 +- **🛠️ `manage_skills` 工具(扩展)**:会话内以确定性方式完成 `list/install/create/edit/show/delete`。 +- **📤 发布增强**:HTML 支持 `artifacts` / `richui`;PDF 统一 Markdown 链接交付。 +- **📁 文件夹级技能**:一个技能可包含 `SKILL.md` 及模板/脚本/资源文件。 +- **🧱 环境改造**:在 `/app/backend/data` 下重构配置、工作区与工具运行时布局,隔离性和可复现性显著提升。 + +--- + +## 📦 快速安装 + +- **GitHub Copilot SDK (Pipe 插件)**: [安装 v0.9.0](https://openwebui.com/posts/ce96f7b4-12fc-4ac3-9a01-875713e69359) +- **GitHub Copilot SDK (Filter 伴侣插件)**: [安装 v0.1.3](https://openwebui.com/posts/403a62ee-a596-45e7-be65-fab9cc249dd6) + +--- + +## 🚀 v0.9.0 更新内容 + +### 1. OpenWebUI Skills ↔ SDK 双向同步 + +所有技能统一存放于 `{OPENWEBUI_SKILLS_SHARED_DIR}/shared/`,在数据库与文件之间自动同步。 + +### 2. `manage_skills` 工具 + +内置原生工具(不是 skill),支持: + +- `list` +- `install`(单 URL / 多 URL) +- `create` +- `edit` +- `show` +- `delete` + +### 3. 发布行为 + +- HTML:`artifacts`(输出代码块嵌入)或 `richui`(发射器自动渲染)两种模式。 +- PDF:仅以 Markdown 链接交付(不嵌入 iframe)。 + +### 4. 环境改造(详细分析) + +这一版本不只是功能更新,还做了较大规模的运行环境重构: + +- **持久化 SDK 配置目录** + - 新增 `COPILOTSDK_CONFIG_DIR`。 + - 解析优先级:Valve 显式配置 -> `/app/backend/data/.copilot` -> `~/.copilot` 回退。 + - 标准 OpenWebUI 容器场景下,会话/配置状态可跨容器重启保留。 + +- **按用户 + 会话隔离的工作区模型** + - 工作区根目录统一到 `/app/backend/data/copilot_workspace`(容器模式)。 + - 实际目录按用户与 chat 隔离:`/app/backend/data/copilot_workspace/{user_id}/{chat_id}`。 + - `user_id` 与 `chat_id` 会进行清洗,避免路径穿越风险。 + +- **工具运行时固定到 data 卷(可持久)** + - 工具根目录:`/app/backend/data/.copilot_tools`。 + - NPM 安装前缀固定为 `/app/backend/data/.copilot_tools/npm`(通过 `NPM_CONFIG_PREFIX`)。 + - Python 工具执行固定在 `/app/backend/data/.copilot_tools/venv`(通过 `VIRTUAL_ENV`)。 + - 同时清理 `PYTHONUSERBASE` 与 `PIP_USER`,防止 user-site 安装污染运行时。 + - PATH 被重写优先指向 npm/venv bin,提升工具解析一致性。 + +- **CLI 启动链路加固** + - Copilot CLI 发现顺序:`COPILOT_CLI_PATH` -> 系统 `PATH` -> SDK 内置 bundled binary。 + - 强制 `COPILOT_AUTO_UPDATE=false`,避免运行期自动更新导致行为漂移。 + - 发现成功后自动将 CLI 目录注入 PATH。 + +- **系统提示词中的路径上下文强化** + - 会显式注入隔离工作区、技能目录、配置目录、工具目录。 + - 让 Agent 的“可写路径”和“可读路径”更确定,减少越界写入风险。 + +- **普通用户权限边界细化** + - 普通用户仍禁止访问环境变量、数据库和无关系统内部信息。 + - 允许读取**当前用户当前会话**在 `COPILOTSDK_CONFIG_DIR` 下的会话元数据用于排障。 + - 仍严格禁止跨用户会话读取。 + +### 5. 新增 Valves + +- `COPILOTSDK_CONFIG_DIR` +- `ENABLE_OPENWEBUI_SKILLS` +- `OPENWEBUI_SKILLS_SHARED_DIR` +- `DISABLED_SKILLS` + +--- + +## 🔄 升级说明 + +- 从 v0.8.0 升级无破坏性变更。 +- `ENABLE_WORKSPACE_TOOLS` 已由 `ENABLE_OPENWEBUI_SKILLS` 取代。 + +--- + +## 📚 详细文档 + +- README_CN: +- manage_skills 工具指南: diff --git a/plugins/pipes/github-copilot-sdk/workspace_skills_example.py b/plugins/pipes/github-copilot-sdk/workspace_skills_example.py new file mode 100644 index 0000000..4f03516 --- /dev/null +++ b/plugins/pipes/github-copilot-sdk/workspace_skills_example.py @@ -0,0 +1,260 @@ +""" +Workspace Skills Example - Custom Tools for GitHub Copilot SDK + +This file demonstrates how to create custom tools using the @define_tool decorator +for use in your workspace's .copilot-skills/ directory. + +USAGE: +====== +1. Create a .copilot-skills/ directory at the root of your workspace: + ``` + your-workspace/ + └── .copilot-skills/ + ├── custom_search.py (copy and modify this example) + ├── data_processor.py (your custom tools) + └── README.md (optional: document your skills) + ``` + +2. Copy this file (or your modified version) to .copilot-skills/ + +3. Define your tools using @define_tool decorator: + ```python + from pydantic import BaseModel, Field + from copilot import define_tool + + class SearchParams(BaseModel): + query: str = Field(..., description="Search query") + limit: int = Field(default=10, description="Max results") + + @define_tool(description="Search your custom database") + async def search_custom_db(query: str, limit: int = 10) -> dict: + # Your implementation here + return {"results": [...]} + + # Register as tool (tool name will be snake_case of function name) + custom_search = define_tool( + name="search_custom_db", + description="Search your custom database for documents or data", + params_type=SearchParams, + )(search_custom_db) + ``` + +4. The SDK will automatically discover and register your tools from .copilot-skills/ + +5. Use them in your conversation: "Use the search_custom_db tool to find..." + +REQUIREMENTS: +============= +- Python 3.9+ +- github-copilot-sdk (v0.1.25+) +- Any external dependencies your custom tools need +""" + +from pydantic import BaseModel, Field +from copilot import define_tool + + +# ============================================================================ +# Example 1: Simple Math Helper Tool +# ============================================================================ + +@define_tool(description="Perform common mathematical calculations") +async def calculate_math(operation: str, value1: float, value2: float = 0) -> dict: + """ + Performs basic mathematical operations. + + Args: + operation: One of 'add', 'subtract', 'multiply', 'divide', 'power', 'sqrt' + value1: First number + value2: Second number (for binary operations) + + Returns: + Dictionary with 'result' and 'operation' keys + """ + import math + + op_map = { + "add": lambda a, b: a + b, + "subtract": lambda a, b: a - b, + "multiply": lambda a, b: a * b, + "divide": lambda a, b: a / b if b != 0 else None, + "power": lambda a, b: a ** b, + "sqrt": lambda a, _: math.sqrt(a) if a >= 0 else None, + } + + result = None + if operation in op_map: + try: + result = op_map[operation](value1, value2) + except Exception as e: + return {"success": False, "error": str(e)} + else: + return {"success": False, "error": f"Unknown operation: {operation}"} + + return { + "success": True, + "operation": operation, + "value1": value1, + "value2": value2, + "result": result, + } + + +# ============================================================================ +# Example 2: Text Processing Tool with Parameter Model +# ============================================================================ + +class TextProcessParams(BaseModel): + """Parameters for text processing operations.""" + text: str = Field(..., description="The text to process") + operation: str = Field( + default="count_words", + description="Operation: 'count_words', 'to_uppercase', 'to_lowercase', 'reverse', 'count_lines'" + ) + + +@define_tool(description="Process and analyze text content") +async def process_text(text: str, operation: str = "count_words") -> dict: + """ + Processes text with various operations. + + Args: + text: Input text to process + operation: Type of processing to apply + + Returns: + Dictionary with processing results + """ + results = { + "operation": operation, + "input_length": len(text), + "result": None, + } + + if operation == "count_words": + results["result"] = len(text.split()) + elif operation == "to_uppercase": + results["result"] = text.upper() + elif operation == "to_lowercase": + results["result"] = text.lower() + elif operation == "reverse": + results["result"] = text[::-1] + elif operation == "count_lines": + results["result"] = len(text.split("\n")) + else: + results["error"] = f"Unknown operation: {operation}" + + return results + + +# ============================================================================ +# Example 3: Advanced Tool with Complex Return Type +# ============================================================================ + +class DataAnalysisParams(BaseModel): + """Parameters for data analysis.""" + data_points: list = Field(..., description="List of numbers to analyze") + include_stats: bool = Field(default=True, description="Include statistical analysis") + + +@define_tool(description="Analyze numerical data and compute statistics") +async def analyze_data(data_points: list, include_stats: bool = True) -> dict: + """ + Analyzes a list of numerical values. + + Args: + data_points: List of numbers to analyze + include_stats: Whether to include statistical analysis + + Returns: + Dictionary with analysis results + """ + if not data_points or not all(isinstance(x, (int, float)) for x in data_points): + return { + "error": "data_points must be a non-empty list of numbers", + "success": False, + } + + results = { + "success": True, + "count": len(data_points), + "min": min(data_points), + "max": max(data_points), + "sum": sum(data_points), + } + + if include_stats: + import statistics + try: + results["mean"] = statistics.mean(data_points) + results["median"] = statistics.median(data_points) + if len(data_points) > 1: + results["stdev"] = statistics.stdev(data_points) + except Exception as e: + results["stats_error"] = str(e) + + return results + + +# ============================================================================ +# Tool Registration (Optional: explicit naming) +# ============================================================================ +# The SDK will auto-discover tools from @define_tool decorated functions. +# You can optionally register them explicitly by assigning to variables: + +math_tool = define_tool( + name="calculate_math", + description="Perform mathematical calculations (add, subtract, multiply, divide, power, sqrt)", + params_type=BaseModel, # Can be complex if needed +)(calculate_math) + +text_processor = define_tool( + name="process_text", + description="Process and analyze text (count words, case conversion, etc.)", + params_type=TextProcessParams, +)(process_text) + +data_analyzer = define_tool( + name="analyze_data", + description="Analyze numerical data and compute statistics", + params_type=DataAnalysisParams, +)(analyze_data) + + +# ============================================================================ +# Example: Custom Implementation from Scratch +# ============================================================================ +# If you need more control, implement the Tool class directly: +# +# from copilot.types import Tool +# +# async def my_custom_handler(query: str) -> str: +# """Your tool logic here.""" +# return f"Processed: {query}" +# +# my_tool = Tool( +# name="my_custom_tool", +# description="My custom tool description", +# handler=my_custom_handler, +# parameters={} # Add JSON Schema if complex params needed +# ) + + +if __name__ == "__main__": + """Test the example tools locally.""" + import asyncio + + async def main(): + # Test math tool + result1 = await calculate_math("add", 10, 5) + print("Math (10 + 5):", result1) + + # Test text processor + result2 = await process_text("Hello World", "count_words") + print("Text (count words):", result2) + + # Test data analyzer + result3 = await analyze_data([1, 2, 3, 4, 5], include_stats=True) + print("Data Analysis:", result3) + + asyncio.run(main()) diff --git a/scripts/update_pipe.py b/scripts/update_pipe.py new file mode 100644 index 0000000..8e14993 --- /dev/null +++ b/scripts/update_pipe.py @@ -0,0 +1,110 @@ +import requests +import json +import os +import re +from pathlib import Path + +# ─── Configuration ─────────────────────────────────────────────────────────── +SCRIPT_DIR = Path(__file__).parent +ENV_FILE = SCRIPT_DIR / ".env" + +URL = ( + "http://localhost:3003/api/v1/functions/id/github_copilot_official_sdk_pipe/update" +) +FILE_PATH = SCRIPT_DIR.parent / "plugins/pipes/github-copilot-sdk/github_copilot_sdk.py" + + +def _load_api_key() -> str: + """Load API key from .env file in the same directory as this script. + + The .env file should contain a line like: + api_key=sk-xxxxxxxxxxxx + """ + if not ENV_FILE.exists(): + raise FileNotFoundError( + f".env file not found at {ENV_FILE}. " + "Please create it with: api_key=sk-xxxxxxxxxxxx" + ) + + for line in ENV_FILE.read_text(encoding="utf-8").splitlines(): + line = line.strip() + if line.startswith("api_key="): + key = line.split("=", 1)[1].strip() + if key: + return key + + raise ValueError("api_key not found in .env file.") + + +def update_pipe() -> None: + """Push the latest local github_copilot_sdk.py content to OpenWebUI.""" + # 1. Load API key + try: + api_key = _load_api_key() + except (FileNotFoundError, ValueError) as e: + print(f"[ERROR] {e}") + return + + # 2. Read local source file + if not FILE_PATH.exists(): + print(f"[ERROR] Source file not found: {FILE_PATH}") + return + + content = FILE_PATH.read_text(encoding="utf-8") + + # 3. Extract version from docstring + version_match = re.search(r"version:\s*([\d.]+)", content) + version = version_match.group(1) if version_match else "0.9.0" + + # 4. Build payload + payload = { + "id": "github_copilot_official_sdk_pipe", + "name": "GitHub Copilot Official SDK Pipe", + "meta": { + "description": ( + "Integrate GitHub Copilot SDK. Supports dynamic models, " + "multi-turn conversation, streaming, multimodal input, " + "infinite sessions, and frontend debug logging." + ), + "manifest": { + "title": "GitHub Copilot Official SDK Pipe", + "author": "Fu-Jie", + "author_url": "https://github.com/Fu-Jie/openwebui-extensions", + "funding_url": "https://github.com/open-webui", + "openwebui_id": "ce96f7b4-12fc-4ac3-9a01-875713e69359", + "description": ( + "Integrate GitHub Copilot SDK. Supports dynamic models, " + "multi-turn conversation, streaming, multimodal input, " + "infinite sessions, bidirectional OpenWebUI Skills bridge, " + "and manage_skills tool." + ), + "version": version, + "requirements": "github-copilot-sdk==0.1.25", + }, + "type": "pipe", + }, + "content": content, + } + + # 5. Build headers — use long-lived API key instead of short-lived JWT + headers = { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": f"Bearer {api_key}", + } + + # 6. Send update request + print(f"Updating pipe with version {version}...") + try: + response = requests.post(URL, headers=headers, data=json.dumps(payload)) + if response.status_code == 200: + print("✅ Successfully updated GitHub Copilot Official SDK Pipe!") + else: + print(f"❌ Failed to update. Status: {response.status_code}") + print(f" Response: {response.text[:500]}") + except Exception as e: + print(f"❌ Request error: {e}") + + +if __name__ == "__main__": + update_pipe()