From 721be2de5b0bb97a2b33e7acbc15f3f5be63ba9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=9F=B3=E6=9D=B0?= Date: Mon, 4 May 2026 12:25:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20update.sh=E6=94=B9=E4=B8=BAupdate.p?= =?UTF-8?q?y=EF=BC=8C=E5=85=BC=E5=AE=B9Windows/macOS/Linux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用纯Python标准库(urllib/tarfile/shutil)替换bash+curl+tar, 确保在Windows上无需额外依赖即可运行。 Co-Authored-By: Claude Opus 4.6 --- plugins/mipu-api/skills/mipu-api/SKILL.md | 8 +- plugins/mipu-api/skills/mipu-api/update.py | 87 ++++++++++++++++++++++ plugins/mipu-api/skills/mipu-api/update.sh | 54 -------------- 3 files changed, 91 insertions(+), 58 deletions(-) create mode 100644 plugins/mipu-api/skills/mipu-api/update.py delete mode 100755 plugins/mipu-api/skills/mipu-api/update.sh diff --git a/plugins/mipu-api/skills/mipu-api/SKILL.md b/plugins/mipu-api/skills/mipu-api/SKILL.md index 3fb335f..f93f6ad 100644 --- a/plugins/mipu-api/skills/mipu-api/SKILL.md +++ b/plugins/mipu-api/skills/mipu-api/SKILL.md @@ -10,10 +10,10 @@ allowed-tools: "Read,Grep,Glob,Bash" 每次响应用户请求前,**必须先执行文档更新检查**: -1. 使用 `Glob` 查找 skill 目录:搜索 `**/skills/mipu-api/update.sh` 获取脚本绝对路径 +1. 使用 `Glob` 查找 skill 目录:搜索 `**/skills/mipu-api/update.py` 获取脚本绝对路径 2. 使用 `Bash` 执行更新脚本: ```bash - bash + python3 ``` 3. 脚本会自动: - 对比本地 `.version` 与 Gitea 远程最新 Release 版本 @@ -22,7 +22,7 @@ allowed-tools: "Read,Grep,Glob,Bash" - 网络异常 → 静默退出,不影响后续使用 4. 更新完成后继续用本地 `docs/` 回答问题 -**重要**:如果 update.sh 执行超过 5 秒或报错,直接跳过,使用现有本地文档即可。 +**重要**:如果 update.py 执行超过 5 秒或报错,直接跳过,使用现有本地文档即可。 ## 1. 角色 @@ -108,7 +108,7 @@ Base URL: `https://${endpoint}/` ## 7. 行为规则 -1. **只读**:绝不修改任何文件(update.sh 自动更新除外) +1. **只读**:绝不修改任何文件(update.py 自动更新除外) 2. **引用来源**:回答时标注引用的文档文件名 3. **不确定时询问**:agent code、proxy 配置等不明确的参数,主动询问客户 4. **中文优先**:面向中文开发者,正文使用中文,代码和技术术语保留英文 diff --git a/plugins/mipu-api/skills/mipu-api/update.py b/plugins/mipu-api/skills/mipu-api/update.py new file mode 100644 index 0000000..22598e7 --- /dev/null +++ b/plugins/mipu-api/skills/mipu-api/update.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +"""Mipu API docs auto-updater. + +Checks latest release from mipu-open repo and updates local docs. +Works on macOS, Linux, and Windows. +Safe to run in any environment - fails silently on network errors. +""" + +import json +import os +import shutil +import sys +import tarfile +import tempfile +import urllib.request +import urllib.error + +API_URL = "https://git.addhh.com/api/v1/repos/willow/mipu-open/releases/latest" +TIMEOUT = 5 +DOWNLOAD_TIMEOUT = 30 + + +def main(): + script_dir = os.path.dirname(os.path.abspath(__file__)) + docs_dir = os.path.join(script_dir, "docs") + version_file = os.path.join(script_dir, ".version") + + # Read current version + current = "" + if os.path.isfile(version_file): + with open(version_file, "r", encoding="utf-8") as f: + current = f.read().strip() + + # Fetch latest release info + try: + req = urllib.request.Request(API_URL) + with urllib.request.urlopen(req, timeout=TIMEOUT) as resp: + release = json.loads(resp.read().decode("utf-8")) + except Exception: + # Network error — silent fail + return + + remote = release.get("tag_name", "") + if not remote or remote == current: + return + + # Find docs.tar.gz asset URL + asset_url = "" + for asset in release.get("assets", []): + if asset.get("name") == "docs.tar.gz": + asset_url = asset.get("browser_download_url", "") + break + + if not asset_url: + return + + # Download docs.tar.gz to temp file + tmp_fd, tmp_path = tempfile.mkstemp(suffix=".tar.gz") + try: + os.close(tmp_fd) + req = urllib.request.Request(asset_url) + with urllib.request.urlopen(req, timeout=DOWNLOAD_TIMEOUT) as resp: + with open(tmp_path, "wb") as f: + shutil.copyfileobj(resp, f) + + # Remove old docs and extract new + if os.path.isdir(docs_dir): + shutil.rmtree(docs_dir) + + with tarfile.open(tmp_path, "r:gz") as tar: + tar.extractall(path=script_dir) + + # Write new version + with open(version_file, "w", encoding="utf-8") as f: + f.write(remote) + + print(f"Mipu API docs updated: {current or 'none'} -> {remote}") + + except Exception: + return + finally: + if os.path.isfile(tmp_path): + os.unlink(tmp_path) + + +if __name__ == "__main__": + main() diff --git a/plugins/mipu-api/skills/mipu-api/update.sh b/plugins/mipu-api/skills/mipu-api/update.sh deleted file mode 100755 index 5eb49c6..0000000 --- a/plugins/mipu-api/skills/mipu-api/update.sh +++ /dev/null @@ -1,54 +0,0 @@ -#!/bin/bash -# Mipu API docs auto-updater -# Checks latest release from mipu-open repo and updates local docs. -# Safe to run in any environment — fails silently on network errors. - -set -e - -SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" -DOCS_DIR="$SCRIPT_DIR/docs" -VERSION_FILE="$SCRIPT_DIR/.version" -API_URL="https://git.addhh.com/api/v1/repos/willow/mipu-open/releases/latest" - -# Get current version -CURRENT="" -if [ -f "$VERSION_FILE" ]; then - CURRENT=$(cat "$VERSION_FILE" | tr -d '[:space:]') -fi - -# Fetch latest release info (timeout 3s, fail silently) -RELEASE=$(curl -sf --connect-timeout 3 --max-time 5 "$API_URL" 2>/dev/null) || exit 0 - -# Parse remote version -REMOTE=$(echo "$RELEASE" | python3 -c "import sys,json; print(json.load(sys.stdin).get('tag_name',''))" 2>/dev/null) || exit 0 - -# Skip if same version or empty -[ -z "$REMOTE" ] && exit 0 -[ "$REMOTE" = "$CURRENT" ] && exit 0 - -# Find docs.tar.gz download URL -ASSET_URL=$(echo "$RELEASE" | python3 -c " -import sys,json -r=json.load(sys.stdin) -for a in r.get('assets',[]): - if a['name']=='docs.tar.gz': - print(a['browser_download_url']) - break -" 2>/dev/null) || exit 0 - -[ -z "$ASSET_URL" ] && exit 0 - -# Download -TMP=$(mktemp /tmp/mipu-docs.XXXXXX.tar.gz) -curl -sfL --connect-timeout 5 --max-time 30 "$ASSET_URL" -o "$TMP" 2>/dev/null || { rm -f "$TMP"; exit 0; } - -# Replace docs directory -rm -rf "$DOCS_DIR" -mkdir -p "$SCRIPT_DIR" -tar -xzf "$TMP" -C "$SCRIPT_DIR" -rm -f "$TMP" - -# Write new version -echo "$REMOTE" > "$VERSION_FILE" - -echo "Mipu API docs updated: ${CURRENT:-none} → ${REMOTE}"