lingshu-skill 0.0.0-stage → 3.79.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2571 -0
- package/LICENSE +21 -0
- package/README.md +698 -3
- package/SKILL-lite.md +45 -0
- package/SKILL-tiny.md +37 -0
- package/SKILL.md +379 -0
- package/assets/panel.html +5616 -0
- package/cordis-plugin/lingshu-cron/cordis.patch.yml +3 -0
- package/cordis-plugin/lingshu-cron/index.js +4 -0
- package/cordis-plugin/lingshu-cron/package.json +34 -0
- package/cordis-plugin/lingshu-cron/plugin.js +1040 -0
- package/cordis-plugin/lingshu-guard/plugin.js +473 -0
- package/cordis-plugin/lingshu-im/plugin.js +635 -0
- package/cordis-plugin/lingshu-startup/README.md +123 -0
- package/cordis-plugin/lingshu-startup/cordis.patch.yml +3 -0
- package/cordis-plugin/lingshu-startup/index.js +4 -0
- package/cordis-plugin/lingshu-startup/package.json +25 -0
- package/cordis-plugin/lingshu-startup/plugin.js +352 -0
- package/cordis.patch.yml +3 -0
- package/docs/commands.md +243 -0
- package/docs/mechanics.md +610 -0
- package/docs/models.md +52 -0
- package/docs/npm-/345/217/221/345/270/203/350/257/264/346/230/216.md +49 -0
- package/docs/panel-api.md +198 -0
- package/docs/recovery.md +90 -0
- package/docs/tgz-/345/256/211/350/243/205/350/257/264/346/230/216.md +53 -0
- package/docs//350/277/233/345/261/225/344/270/216/345/276/205/345/212/236.md +802 -0
- package/index.js +102 -0
- package/package.json +52 -6
- package/scripts/_panel_smoke_v299.py +27 -0
- package/scripts/_smoke_v299.py +31 -0
- package/scripts/_sync_trae_cn.py +34 -0
- package/scripts/assembler.py +197 -0
- package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +53 -0
- package/scripts/astrbot_plugin_lingshu/PATCH_NOTES.md +28 -0
- package/scripts/astrbot_plugin_lingshu/README.md +58 -0
- package/scripts/astrbot_plugin_lingshu/_conf_schema.json +92 -0
- package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +2571 -0
- package/scripts/astrbot_plugin_lingshu/engine/README.md +698 -0
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-lite.md +45 -0
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-tiny.md +37 -0
- package/scripts/astrbot_plugin_lingshu/engine/SKILL.md +379 -0
- package/scripts/astrbot_plugin_lingshu/engine/assets/panel.html +5616 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/commands.md +243 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/mechanics.md +610 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/models.md +52 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/panel-api.md +198 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/recovery.md +90 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_panel_smoke_v299.py +27 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_smoke_v299.py +31 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_sync_trae_cn.py +34 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/assembler.py +197 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/behavior_baseline.py +280 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/bench.py +192 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/brain.py +46 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/check_pressure_sync.py +55 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/check_tag.py +215 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/checkpoint.py +165 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/cli.py +4850 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/context_ab.py +260 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/core.py +2025 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/dream.py +1137 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/emotion.py +1180 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/failures.py +107 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/fallback_embed.py +114 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/growth.py +4745 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/growth_parts.py +263 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/insights.py +131 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/make_dsh_tgz.py +92 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_dialogue.py +242 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_embed.py +159 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_lesson.py +160 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_score.py +210 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_topic.py +177 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/memory.py +4352 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/npm_prepack.py +31 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_build.py +115 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_cloud.py +609 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_server.py +2703 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panels.py +591 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/persona.py +2309 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/retriever.py +195 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/safety.py +368 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/session_import.py +736 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/session_rag.py +228 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/session_summary.py +123 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/summarizer.py +252 -0
- package/scripts/astrbot_plugin_lingshu/main.py +703 -0
- package/scripts/astrbot_plugin_lingshu/make_checksums.py +87 -0
- package/scripts/astrbot_plugin_lingshu/metadata.yaml +7 -0
- package/scripts/behavior_baseline.py +280 -0
- package/scripts/bench.py +192 -0
- package/scripts/brain.py +46 -0
- package/scripts/check_pressure_sync.py +55 -0
- package/scripts/check_tag.py +215 -0
- package/scripts/check_version.py +265 -0
- package/scripts/checkpoint.py +165 -0
- package/scripts/cli.py +4850 -0
- package/scripts/context_ab.py +260 -0
- package/scripts/core.py +2025 -0
- package/scripts/dream.py +1137 -0
- package/scripts/emotion.py +1180 -0
- package/scripts/failures.py +107 -0
- package/scripts/fallback_embed.py +114 -0
- package/scripts/growth.py +4745 -0
- package/scripts/growth_parts.py +263 -0
- package/scripts/hooks/pre-commit +18 -0
- package/scripts/hooks/pre-push +20 -0
- package/scripts/insights.py +131 -0
- package/scripts/make_dsh_tgz.py +92 -0
- package/scripts/make_manifest.py +254 -0
- package/scripts/make_plugin_zip.py +88 -0
- package/scripts/mem_dialogue.py +242 -0
- package/scripts/mem_embed.py +159 -0
- package/scripts/mem_lesson.py +160 -0
- package/scripts/mem_score.py +210 -0
- package/scripts/mem_topic.py +177 -0
- package/scripts/memory.py +4352 -0
- package/scripts/npm_prepack.py +31 -0
- package/scripts/panel_build.py +115 -0
- package/scripts/panel_cloud.py +609 -0
- package/scripts/panel_server.py +2703 -0
- package/scripts/panels.py +591 -0
- package/scripts/persona.py +2309 -0
- package/scripts/recreate_push.ps1 +117 -0
- package/scripts/retriever.py +195 -0
- package/scripts/run_tests.py +91 -0
- package/scripts/safety.py +368 -0
- package/scripts/session_import.py +736 -0
- package/scripts/session_rag.py +228 -0
- package/scripts/session_summary.py +123 -0
- package/scripts/summarizer.py +252 -0
- package/scripts/sync_engine.py +139 -0
- package/scripts/update_dsh_copy.ps1 +107 -0
- package/start-panel.bat +60 -0
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""灵枢 云端面板市场:Gitee(主)/ GitHub(镜像)仓库当云端,纯标准库 urllib。
|
|
4
|
+
|
|
5
|
+
云端约定(仓库布局):
|
|
6
|
+
- panels/<面板名>.zip 面板安装包(≤50MB,安装时解压校验 manifest)
|
|
7
|
+
- shots/<面板名>.<ext> 截图(png/jpg/webp,≤2MB),可选但强烈建议
|
|
8
|
+
- index.json 目录清单:{"panels":[{name,title,version,author,
|
|
9
|
+
description,screenshot,size,updated}]}
|
|
10
|
+
|
|
11
|
+
接口要点(调研结论,2026-11):
|
|
12
|
+
- Gitee v5:GET/POST/PUT https://gitee.com/api/v5/repos/{owner}/{repo}/contents/{path}
|
|
13
|
+
新建 POST、更新 PUT(需带原文件 sha),content 一律 base64;access_token 放 body/查询。
|
|
14
|
+
- GitHub:PUT https://api.github.com/repos/{owner}/{repo}/contents/{path}
|
|
15
|
+
新建不带 sha、更新带 sha;下载用 Accept: application/vnd.github.raw(>1MB 时
|
|
16
|
+
contents 接口的 content 字段为空,必须走 raw 头)。
|
|
17
|
+
- token 只存本机 config.json,API 响应一律脱敏;公开仓库浏览不需要 token。
|
|
18
|
+
|
|
19
|
+
离线/未配置/网络失败一律降级:列表返回空并带 error 提示,不影响本机市场。
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import base64
|
|
25
|
+
import hashlib
|
|
26
|
+
import json
|
|
27
|
+
import os
|
|
28
|
+
import time
|
|
29
|
+
import urllib.error
|
|
30
|
+
import urllib.request
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
|
|
33
|
+
import brain
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _resolve_token(value: str) -> str:
|
|
37
|
+
"""token 支持 "env:变量名" 引用:不落盘明文,运行时从环境变量读取。
|
|
38
|
+
|
|
39
|
+
直接填的明文 token 仍兼容(不推荐;备份时会脱敏剥离,见 cli._sanitize_config_json)。
|
|
40
|
+
"""
|
|
41
|
+
v = str(value or "").strip()
|
|
42
|
+
if v.lower().startswith("env:"):
|
|
43
|
+
return os.environ.get(v[4:].strip(), "").strip()
|
|
44
|
+
return v
|
|
45
|
+
|
|
46
|
+
# 截图白名单扩展名与大小上限(≤2MB),zip 上限沿用 panels.ZIP_MAX_TOTAL
|
|
47
|
+
SHOT_WHITELIST = {".png", ".jpg", ".jpeg", ".webp"}
|
|
48
|
+
SHOT_MAX = 2 * 1024 * 1024
|
|
49
|
+
_CLOUD_TIMEOUT = 20 # 秒;Gitee 稳定,GitHub 国内不稳,超时即降级
|
|
50
|
+
|
|
51
|
+
# 官方商店源兜底:用户没配置 panel.cloud 时,默认浏览灵枢官方镜像仓库的
|
|
52
|
+
# index.json(面板/拓展都发布在这里)。用户配置了自己的仓库后以配置优先。
|
|
53
|
+
DEFAULT_GITEE_REPO = "htmcwy_0/lingshu-skill"
|
|
54
|
+
DEFAULT_GITHUB_REPO = "Q2667671398/lingshu-skill"
|
|
55
|
+
DEFAULT_BRANCH = "main"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# ---------------------------------------------------------------- 配置
|
|
59
|
+
|
|
60
|
+
def _cfg(resolve_tokens: bool = True) -> dict:
|
|
61
|
+
"""读云端配置:panel.cloud.{gitee_repo,gitee_token,github_repo,github_token,branch}
|
|
62
|
+
未配置时回退官方商店源(gitee_repo / branch;GitHub 同理)。
|
|
63
|
+
resolve_tokens=False 时保留 "env:VAR" 原样(set_config 回写用,防明文落盘)。"""
|
|
64
|
+
try:
|
|
65
|
+
c = brain.load_config().get("panel.cloud") or {}
|
|
66
|
+
except Exception:
|
|
67
|
+
c = {}
|
|
68
|
+
c = dict(c) if isinstance(c, dict) else {}
|
|
69
|
+
if not c.get("gitee_repo") and not c.get("github_repo"):
|
|
70
|
+
c.setdefault("gitee_repo", DEFAULT_GITEE_REPO)
|
|
71
|
+
c.setdefault("github_repo", DEFAULT_GITHUB_REPO)
|
|
72
|
+
c.setdefault("branch", DEFAULT_BRANCH)
|
|
73
|
+
# token 环境变量引用在读取侧解析,落盘的可以是 "env:VAR" 而非明文
|
|
74
|
+
if resolve_tokens:
|
|
75
|
+
for _k in ("gitee_token", "github_token"):
|
|
76
|
+
if c.get(_k):
|
|
77
|
+
c[_k] = _resolve_token(c[_k])
|
|
78
|
+
return c
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def set_config(gitee_repo: str = "", gitee_token: str = "",
|
|
82
|
+
github_repo: str = "", github_token: str = "",
|
|
83
|
+
branch: str = "") -> dict:
|
|
84
|
+
"""保存云端配置(空串=清除该项)。token 属敏感信息:写 config.json 前校验格式;
|
|
85
|
+
填 "env:变量名" 则不落盘明文,运行时从环境变量读取。"""
|
|
86
|
+
c = _cfg(resolve_tokens=False)
|
|
87
|
+
if gitee_repo:
|
|
88
|
+
if gitee_repo.count("/") != 1:
|
|
89
|
+
raise ValueError("Gitee 仓库要填 owner/repo 形式,例如 yourname/lingshu-panels")
|
|
90
|
+
c["gitee_repo"] = gitee_repo.strip()
|
|
91
|
+
else:
|
|
92
|
+
c.pop("gitee_repo", None)
|
|
93
|
+
if gitee_token:
|
|
94
|
+
c["gitee_token"] = gitee_token.strip()
|
|
95
|
+
else:
|
|
96
|
+
c.pop("gitee_token", None)
|
|
97
|
+
if github_repo:
|
|
98
|
+
if github_repo.count("/") != 1:
|
|
99
|
+
raise ValueError("GitHub 仓库要填 owner/repo 形式")
|
|
100
|
+
c["github_repo"] = github_repo.strip()
|
|
101
|
+
else:
|
|
102
|
+
c.pop("github_repo", None)
|
|
103
|
+
if github_token:
|
|
104
|
+
c["github_token"] = github_token.strip()
|
|
105
|
+
else:
|
|
106
|
+
c.pop("github_token", None)
|
|
107
|
+
if branch:
|
|
108
|
+
c["branch"] = branch.strip()
|
|
109
|
+
with brain._locked(brain.HOME / "config.json"):
|
|
110
|
+
full = brain.load_config()
|
|
111
|
+
full["panel.cloud"] = c
|
|
112
|
+
brain.atomic_json(brain.HOME / "config.json", full)
|
|
113
|
+
return status()
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def status() -> dict:
|
|
117
|
+
"""云端配置状态(token 脱敏:只露前 4 位,让用户能认出是哪个 token)。
|
|
118
|
+
browse 用默认官方源兜底(未配置也能逛商店);publish 必须显式配置 token。"""
|
|
119
|
+
c = _cfg()
|
|
120
|
+
user_cloud = brain.load_config().get("panel.cloud") or {}
|
|
121
|
+
user_repo = bool(user_cloud.get("gitee_repo"))
|
|
122
|
+
|
|
123
|
+
def mask(t: str) -> str:
|
|
124
|
+
t = str(t or "")
|
|
125
|
+
if t.lower().startswith("env:"):
|
|
126
|
+
return t # 环境变量引用不含密文,原样展示便于辨认
|
|
127
|
+
return (t[:4] + "****") if t else ""
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
"gitee_repo": c.get("gitee_repo", ""),
|
|
131
|
+
"gitee_token_mask": mask(user_cloud.get("gitee_token", "")),
|
|
132
|
+
"github_repo": c.get("github_repo", ""),
|
|
133
|
+
"github_token_mask": mask(user_cloud.get("github_token", "")),
|
|
134
|
+
"branch": c.get("branch", "master"),
|
|
135
|
+
"gitee_ready": user_repo, # 用户显式配置了自己的 Gitee 仓库
|
|
136
|
+
"browse_ready": bool(c.get("gitee_repo") or c.get("github_repo")), # 能逛商店(含官方兜底源)
|
|
137
|
+
"using_default_source": not user_repo, # 浏览的是官方兜底源还是自己的配置
|
|
138
|
+
"publish_ready": bool(user_cloud.get("gitee_repo") and user_cloud.get("gitee_token")),
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
# ---------------------------------------------------------------- HTTP 底座
|
|
143
|
+
|
|
144
|
+
def _http(method: str, url: str, *, token: str = "", headers: dict | None = None,
|
|
145
|
+
body: bytes | None = None) -> bytes:
|
|
146
|
+
"""一次 HTTP 请求 → 响应字节。非 2xx 抛 RuntimeError(带状态码与响应摘要)。"""
|
|
147
|
+
h = {"User-Agent": "lingshu-panel-market/1.0"}
|
|
148
|
+
if token:
|
|
149
|
+
h["Authorization"] = f"token {token}"
|
|
150
|
+
if headers:
|
|
151
|
+
h.update(headers)
|
|
152
|
+
req = urllib.request.Request(url, data=body, headers=h, method=method)
|
|
153
|
+
try:
|
|
154
|
+
with urllib.request.urlopen(req, timeout=_CLOUD_TIMEOUT) as resp:
|
|
155
|
+
return resp.read()
|
|
156
|
+
except urllib.error.HTTPError as exc:
|
|
157
|
+
detail = ""
|
|
158
|
+
try:
|
|
159
|
+
detail = exc.read()[:200].decode("utf-8", "replace")
|
|
160
|
+
except Exception:
|
|
161
|
+
pass
|
|
162
|
+
raise RuntimeError(f"HTTP {exc.code}:{detail or exc.reason}") from exc
|
|
163
|
+
except (urllib.error.URLError, OSError, TimeoutError) as exc:
|
|
164
|
+
raise RuntimeError(f"网络请求失败:{exc}") from exc
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _gitee_get(repo: str, path: str, token: str, branch: str) -> bytes | None:
|
|
168
|
+
"""Gitee 取单文件原始字节(contents 接口 → base64 解码)。不存在 → None。"""
|
|
169
|
+
from urllib.parse import quote
|
|
170
|
+
url = (f"https://gitee.com/api/v5/repos/{repo}/contents/{quote(path)}"
|
|
171
|
+
f"?ref={quote(branch)}")
|
|
172
|
+
if token:
|
|
173
|
+
url += f"&access_token={quote(token)}"
|
|
174
|
+
try:
|
|
175
|
+
raw = _http("GET", url)
|
|
176
|
+
except RuntimeError as exc:
|
|
177
|
+
if "HTTP 404" in str(exc):
|
|
178
|
+
return None
|
|
179
|
+
# API 匿名限流(403)等场景:公开仓库退回 raw 页面直连(不走 contents API 配额)
|
|
180
|
+
try:
|
|
181
|
+
return _http("GET", f"https://gitee.com/{repo}/raw/{branch}/{quote(path)}",
|
|
182
|
+
token=token)
|
|
183
|
+
except RuntimeError:
|
|
184
|
+
raise exc
|
|
185
|
+
data = json.loads(raw.decode("utf-8"))
|
|
186
|
+
if data.get("encoding") == "base64" and data.get("content"):
|
|
187
|
+
return base64.b64decode(data["content"])
|
|
188
|
+
# Gitee 大文件 encoding 可能是 none:退回 raw 页面下载
|
|
189
|
+
return _http("GET", f"https://gitee.com/{repo}/raw/{branch}/{quote(path)}",
|
|
190
|
+
token=token)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _commit_author(c: dict) -> dict | None:
|
|
194
|
+
"""从配置取自定义 commit 作者(author_name/author_email)。
|
|
195
|
+
|
|
196
|
+
未配置时返回 None——远端 API 默认用 token 所属账号作为提交者。
|
|
197
|
+
"""
|
|
198
|
+
name = str(c.get("author_name", "")).strip()
|
|
199
|
+
email = str(c.get("author_email", "")).strip()
|
|
200
|
+
if not name:
|
|
201
|
+
return None
|
|
202
|
+
return {"name": name, "email": email or "lingshu@local"}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _gitee_put(repo: str, path: str, token: str, branch: str,
|
|
206
|
+
content: bytes, message: str,
|
|
207
|
+
author: dict | None = None) -> None:
|
|
208
|
+
"""Gitee 新建/更新单文件:先探 sha,有则 PUT 更新,无则 POST 新建。"""
|
|
209
|
+
from urllib.parse import quote
|
|
210
|
+
b64 = base64.b64encode(content).decode("ascii")
|
|
211
|
+
payload = {"access_token": token, "content": b64, "branch": branch,
|
|
212
|
+
"message": message}
|
|
213
|
+
if author:
|
|
214
|
+
payload["author_name"] = author["name"]
|
|
215
|
+
payload["author_email"] = author["email"]
|
|
216
|
+
payload["committer_name"] = author["name"]
|
|
217
|
+
payload["committer_email"] = author["email"]
|
|
218
|
+
old = _gitee_get(repo, path, token, branch)
|
|
219
|
+
if old is not None:
|
|
220
|
+
# 拿 sha 需要再查一次(_gitee_get 返回的是内容本身),单独请求元数据
|
|
221
|
+
url = (f"https://gitee.com/api/v5/repos/{repo}/contents/{quote(path)}"
|
|
222
|
+
f"?ref={quote(branch)}")
|
|
223
|
+
if token:
|
|
224
|
+
url += f"&access_token={quote(token)}"
|
|
225
|
+
sha = json.loads(_http("GET", url).decode("utf-8")).get("sha")
|
|
226
|
+
payload["sha"] = sha
|
|
227
|
+
_http("PUT", f"https://gitee.com/api/v5/repos/{repo}/contents/{quote(path)}",
|
|
228
|
+
headers={"Content-Type": "application/json"},
|
|
229
|
+
body=json.dumps(payload).encode("utf-8"))
|
|
230
|
+
else:
|
|
231
|
+
_http("POST", f"https://gitee.com/api/v5/repos/{repo}/contents/{quote(path)}",
|
|
232
|
+
headers={"Content-Type": "application/json"},
|
|
233
|
+
body=json.dumps(payload).encode("utf-8"))
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _github_put(repo: str, path: str, token: str, branch: str,
|
|
237
|
+
content: bytes, message: str,
|
|
238
|
+
author: dict | None = None) -> None:
|
|
239
|
+
"""GitHub 新建/更新单文件(contents API:不带 sha=新建,带 sha=更新)。"""
|
|
240
|
+
from urllib.parse import quote
|
|
241
|
+
b64 = base64.b64encode(content).decode("ascii")
|
|
242
|
+
payload = {"content": b64, "message": message, "branch": branch}
|
|
243
|
+
if author:
|
|
244
|
+
payload["author"] = {"name": author["name"], "email": author["email"]}
|
|
245
|
+
payload["committer"] = {"name": author["name"], "email": author["email"]}
|
|
246
|
+
url = f"https://api.github.com/repos/{repo}/contents/{quote(path)}"
|
|
247
|
+
# 先探测是否已存在(拿 sha);404/网络抖动都按"新建"处理
|
|
248
|
+
try:
|
|
249
|
+
meta = json.loads(_http("GET", url + f"?ref={quote(branch)}",
|
|
250
|
+
token=token).decode("utf-8"))
|
|
251
|
+
payload["sha"] = meta.get("sha")
|
|
252
|
+
except RuntimeError:
|
|
253
|
+
pass
|
|
254
|
+
_http("PUT", url, headers={"Content-Type": "application/json"},
|
|
255
|
+
body=json.dumps(payload).encode("utf-8"))
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def _github_get(repo: str, path: str, token: str, branch: str) -> bytes | None:
|
|
259
|
+
"""GitHub 取单文件原始字节(Accept raw,>1MB 文件也直接返回二进制)。"""
|
|
260
|
+
from urllib.parse import quote
|
|
261
|
+
url = f"https://api.github.com/repos/{repo}/contents/{quote(path)}?ref={quote(branch)}"
|
|
262
|
+
try:
|
|
263
|
+
return _http("GET", url, token=token,
|
|
264
|
+
headers={"Accept": "application/vnd.github.raw"})
|
|
265
|
+
except RuntimeError as exc:
|
|
266
|
+
if "HTTP 404" in str(exc):
|
|
267
|
+
return None
|
|
268
|
+
raise
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
# ---------------------------------------------------------------- 读取(浏览/下载)
|
|
272
|
+
|
|
273
|
+
def _repo_list(repo: str, branch: str, getter) -> dict:
|
|
274
|
+
"""拉 index.json 并解析;文件不存在 → 空市场,网络错 → 抛错由上层降级。
|
|
275
|
+
|
|
276
|
+
index.json 结构:{"panels": [...], "extensions": [...]}(extensions 可缺省)。
|
|
277
|
+
"""
|
|
278
|
+
raw = getter(repo, "index.json", branch)
|
|
279
|
+
if raw is None:
|
|
280
|
+
return {"panels": [], "extensions": []}
|
|
281
|
+
data = json.loads(raw.decode("utf-8"))
|
|
282
|
+
if isinstance(data, dict):
|
|
283
|
+
return {
|
|
284
|
+
"panels": data.get("panels") if isinstance(data.get("panels"), list) else [],
|
|
285
|
+
"extensions": data.get("extensions") if isinstance(data.get("extensions"), list) else [],
|
|
286
|
+
}
|
|
287
|
+
return {"panels": [], "extensions": []}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def cloud_list(force: bool = False) -> dict:
|
|
291
|
+
"""GET /api/panels/cloud 的返回体:云端目录清单 + 配置状态。
|
|
292
|
+
|
|
293
|
+
优先 Gitee;失败再试 GitHub;都失败 → 返回 error 与提示,前端仍可用本机市场。
|
|
294
|
+
60 秒内存缓存(force=True 强制刷新),避免每次打开标签页都打 API。
|
|
295
|
+
"""
|
|
296
|
+
global _CACHE
|
|
297
|
+
c = _cfg()
|
|
298
|
+
st = status()
|
|
299
|
+
if not force and _CACHE and time.time() - _CACHE[0] < 60:
|
|
300
|
+
return {**_CACHE[1], "status": st, "cached": True}
|
|
301
|
+
gitee_repo = c.get("gitee_repo", "")
|
|
302
|
+
gh_repo = c.get("github_repo", "")
|
|
303
|
+
branch = c.get("branch", "master")
|
|
304
|
+
gt, gh = c.get("gitee_token", ""), c.get("github_token", "")
|
|
305
|
+
errors: list[str] = []
|
|
306
|
+
items: dict = {}
|
|
307
|
+
if gitee_repo:
|
|
308
|
+
try:
|
|
309
|
+
items = _repo_list(gitee_repo, branch,
|
|
310
|
+
lambda r, p, b: _gitee_get(r, p, gt, b))
|
|
311
|
+
except Exception as exc: # noqa: BLE001 —— 网络失败降级,不让市场页挂掉
|
|
312
|
+
errors.append(f"Gitee:{exc}")
|
|
313
|
+
if not items.get("panels") and gh_repo:
|
|
314
|
+
try:
|
|
315
|
+
items = _repo_list(gh_repo, branch,
|
|
316
|
+
lambda r, p, b: _github_get(r, p, gh, b))
|
|
317
|
+
except Exception as exc: # noqa: BLE001
|
|
318
|
+
errors.append(f"GitHub:{exc}")
|
|
319
|
+
errors = [e for e in errors if e]
|
|
320
|
+
panels = items.get("panels", [])
|
|
321
|
+
exts = items.get("extensions", [])
|
|
322
|
+
out = {"panels": panels, "extensions": exts, "status": st, "errors": errors,
|
|
323
|
+
"offline": bool(errors) and not panels and not exts}
|
|
324
|
+
if panels or exts:
|
|
325
|
+
_CACHE = (time.time(), out)
|
|
326
|
+
return out
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
_CACHE: tuple[float, dict] | None = (0.0, {})
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def cloud_download(name: str) -> dict:
|
|
333
|
+
"""从云端下载面板 zip → 存进本机市场目录(当离线缓存)→ 复用 install 校验落位。"""
|
|
334
|
+
# 落盘前统一过 _safe_name(install 内有校验,但 zip 落盘在 install 之前)
|
|
335
|
+
if not _safe_name(name):
|
|
336
|
+
raise ValueError(f"非法面板名:{name!r}")
|
|
337
|
+
raw = _fetch(f"panels/{name}.zip", name)
|
|
338
|
+
_verify_sha256(name, raw) # 清单带 sha256 字段时校验,不匹配拒绝安装
|
|
339
|
+
zpath = panels.market_dir() / f"{name}.zip"
|
|
340
|
+
brain.atomic_bytes(zpath, raw)
|
|
341
|
+
return panels.install(name)
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def _verify_sha256(name: str, raw: bytes) -> None:
|
|
345
|
+
"""下载的 zip 必须与云端 index.json 清单里的 sha256 比对,否则拒绝安装。
|
|
346
|
+
|
|
347
|
+
fail-closed:清单拉取失败、清单无此面板、条目缺 sha256,一律拒绝。
|
|
348
|
+
自建源也要提供带 sha256 的 index.json(见 docs/panel-api.md)。
|
|
349
|
+
"""
|
|
350
|
+
try:
|
|
351
|
+
items = cloud_list(force=False).get("panels", [])
|
|
352
|
+
except Exception as exc: # noqa: BLE001 —— 统一转成拒绝安装的 ValueError
|
|
353
|
+
raise ValueError(f"面板 {name} 的云端校验清单获取失败({exc}),已拒绝安装") from exc
|
|
354
|
+
entry = next((p for p in items if p.get("name") == name), None)
|
|
355
|
+
if not entry:
|
|
356
|
+
raise ValueError(f"面板 {name} 不在云端清单中,已拒绝安装")
|
|
357
|
+
if not entry.get("sha256"):
|
|
358
|
+
raise ValueError(f"面板 {name} 的清单条目缺少 sha256,已拒绝安装")
|
|
359
|
+
if hashlib.sha256(raw).hexdigest().lower() != str(entry["sha256"]).lower():
|
|
360
|
+
raise ValueError(f"面板 {name} 下载校验失败(sha256 不匹配),已拒绝安装")
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def cloud_extension_download(ext_id: str) -> dict:
|
|
364
|
+
"""从云端下载拓展 zip(extensions/<id>.zip)→ 解压到拓展已安装目录。
|
|
365
|
+
|
|
366
|
+
与面板下载分开:面板走 panels.install(有 manifest 校验),
|
|
367
|
+
拓展是宿主适配插件目录,解压即用,只做路径安全校验。
|
|
368
|
+
"""
|
|
369
|
+
# 落盘前统一过 _safe_name:拒绝 `..`、路径分隔符、Windows 盘符冒号(防路径穿越)
|
|
370
|
+
if not _safe_name(ext_id):
|
|
371
|
+
raise ValueError(f"非法拓展名:{ext_id!r}")
|
|
372
|
+
raw = _fetch(f"extensions/{ext_id}.zip", ext_id)
|
|
373
|
+
import io
|
|
374
|
+
import zipfile
|
|
375
|
+
|
|
376
|
+
import panels # noqa: PLC0415 —— 校验常量在 panels 里,函数内局部引入
|
|
377
|
+
root = brain.HOME / "extensions" / "installed"
|
|
378
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
379
|
+
dst = root / ext_id
|
|
380
|
+
if dst.exists():
|
|
381
|
+
raise FileExistsError("该拓展已安装,请先删除 extensions/installed 下同名目录")
|
|
382
|
+
with zipfile.ZipFile(io.BytesIO(raw)) as z:
|
|
383
|
+
names = z.namelist()
|
|
384
|
+
if len(names) > panels.ZIP_MAX_FILES:
|
|
385
|
+
raise ValueError("拓展包文件数超限")
|
|
386
|
+
total = sum(i.file_size for i in z.infolist())
|
|
387
|
+
if total > panels.ZIP_MAX_TOTAL:
|
|
388
|
+
raise ValueError("拓展包超过 50MB")
|
|
389
|
+
dst_real = dst.resolve()
|
|
390
|
+
for n in names: # 防 zip 路径逃逸(../ 绝对路径 + 解析后必须仍在目标目录内)
|
|
391
|
+
if n.startswith("/") or ".." in Path(n).parts:
|
|
392
|
+
raise ValueError("拓展包含非法路径")
|
|
393
|
+
if not (dst_real / n).resolve().is_relative_to(dst_real):
|
|
394
|
+
raise ValueError(f"拓展包路径逃逸:{n!r}")
|
|
395
|
+
z.extractall(dst)
|
|
396
|
+
brain.log_event("panel", f"云端下载拓展 {ext_id} → {dst}")
|
|
397
|
+
return {"ok": True, "id": ext_id, "dir": str(dst)}
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def _fetch(path: str, label: str) -> bytes:
|
|
401
|
+
"""按路径从云端拉文件字节:先 Gitee 后 GitHub,都失败抛 ValueError。"""
|
|
402
|
+
c = _cfg()
|
|
403
|
+
branch = c.get("branch", "master")
|
|
404
|
+
raw = None
|
|
405
|
+
errs = []
|
|
406
|
+
if c.get("gitee_repo"):
|
|
407
|
+
try:
|
|
408
|
+
raw = _gitee_get(c["gitee_repo"], path, c.get("gitee_token", ""), branch)
|
|
409
|
+
except Exception as exc: # noqa: BLE001
|
|
410
|
+
errs.append(f"Gitee:{exc}")
|
|
411
|
+
if raw is None and c.get("github_repo"):
|
|
412
|
+
try:
|
|
413
|
+
raw = _github_get(c["github_repo"], path, c.get("github_token", ""), branch)
|
|
414
|
+
except Exception as exc: # noqa: BLE001
|
|
415
|
+
errs.append(f"GitHub:{exc}")
|
|
416
|
+
if raw is None:
|
|
417
|
+
raise ValueError(f"云端没有该文件或下载失败({label}):{';'.join(errs) or path}")
|
|
418
|
+
return raw
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def _safe_name(name: str) -> bool:
|
|
422
|
+
"""拓展 id/面板名安全校验:复用 panels._NAME_RE(防路径分隔符与越界)。
|
|
423
|
+
|
|
424
|
+
_NAME_RE 白名单本身不含冒号/斜杠,天然拒绝 `..`、路径分隔符与
|
|
425
|
+
Windows 盘符冒号(`C:`);这里显式再挡一层,与 panels._extract_zip_safe
|
|
426
|
+
的三重防护口径保持一致。
|
|
427
|
+
"""
|
|
428
|
+
if not name or name.startswith("/") or "\\" in name or ".." in name \
|
|
429
|
+
or (len(name) > 1 and name[1] == ":"):
|
|
430
|
+
return False
|
|
431
|
+
return bool(panels._NAME_RE.match(name))
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def cloud_shot(name: str, ext: str) -> bytes:
|
|
435
|
+
"""取云端截图字节(前端 <img> 直连本机代理端点,避免跨域/混合内容问题)。"""
|
|
436
|
+
if ext not in SHOT_WHITELIST:
|
|
437
|
+
raise ValueError("截图扩展名不支持")
|
|
438
|
+
c = _cfg()
|
|
439
|
+
branch = c.get("branch", "master")
|
|
440
|
+
path = f"shots/{name}{ext}"
|
|
441
|
+
raw = None
|
|
442
|
+
if c.get("gitee_repo"):
|
|
443
|
+
try:
|
|
444
|
+
raw = _gitee_get(c["gitee_repo"], path, c.get("gitee_token", ""), branch)
|
|
445
|
+
except Exception: # noqa: BLE001
|
|
446
|
+
raw = None
|
|
447
|
+
if raw is None and c.get("github_repo"):
|
|
448
|
+
try:
|
|
449
|
+
raw = _github_get(c["github_repo"], path, c.get("github_token", ""), branch)
|
|
450
|
+
except Exception: # noqa: BLE001
|
|
451
|
+
raw = None
|
|
452
|
+
if raw is None:
|
|
453
|
+
raise FileNotFoundError("云端没有该截图")
|
|
454
|
+
return raw
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
# ---------------------------------------------------------------- 发布
|
|
458
|
+
|
|
459
|
+
def cloud_publish(name: str, screenshot: bytes | None = None,
|
|
460
|
+
shot_ext: str = ".png") -> dict:
|
|
461
|
+
"""把已安装面板发布到云端(Gitee 必配;GitHub 配了就同步镜像)。
|
|
462
|
+
|
|
463
|
+
流程:校验 manifest → 打 zip → 传 panels/<name>.zip →(可选)传截图 →
|
|
464
|
+
读 index.json 合并本面板条目后回写。任一远端失败则该远端报错,另一端照常。
|
|
465
|
+
"""
|
|
466
|
+
import io
|
|
467
|
+
import zipfile
|
|
468
|
+
|
|
469
|
+
import panels
|
|
470
|
+
src = panels._panel_path(panels.panels_dir(), name)
|
|
471
|
+
if not src.exists():
|
|
472
|
+
raise ValueError(f"已安装面板不存在:{name}")
|
|
473
|
+
mf = panels._load_manifest(src) # 损坏面板不允许发布
|
|
474
|
+
if screenshot is not None:
|
|
475
|
+
if len(screenshot) > SHOT_MAX:
|
|
476
|
+
raise ValueError("截图超过 2MB,请压缩后再上传")
|
|
477
|
+
if shot_ext not in SHOT_WHITELIST:
|
|
478
|
+
raise ValueError("截图只支持 png/jpg/webp")
|
|
479
|
+
# 打 zip:内存打包(总量受 ZIP_MAX_TOTAL 保护,超限抛 ValueError)
|
|
480
|
+
buf = io.BytesIO()
|
|
481
|
+
total = 0
|
|
482
|
+
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as z:
|
|
483
|
+
for f in sorted(src.rglob("*")):
|
|
484
|
+
if f.is_file():
|
|
485
|
+
rel = f.relative_to(src).as_posix()
|
|
486
|
+
if rel in ("panel.json",) or not rel.startswith("."):
|
|
487
|
+
total += f.stat().st_size
|
|
488
|
+
if total > panels.ZIP_MAX_TOTAL:
|
|
489
|
+
raise ValueError("面板超过 50MB,不适合云端分发")
|
|
490
|
+
z.write(f, rel)
|
|
491
|
+
zbytes = buf.getvalue()
|
|
492
|
+
now = time.strftime("%Y-%m-%d %H:%M", time.localtime())
|
|
493
|
+
entry = {"name": mf["name"], "title": mf["title"], "version": mf["version"],
|
|
494
|
+
"author": mf.get("author", ""), "description": mf.get("description", ""),
|
|
495
|
+
"screenshot": (f"{name}{shot_ext}" if screenshot is not None else ""),
|
|
496
|
+
"size": len(zbytes), "sha256": hashlib.sha256(zbytes).hexdigest(), "updated": now}
|
|
497
|
+
c = _cfg()
|
|
498
|
+
branch = c.get("branch", "master")
|
|
499
|
+
author = _commit_author(c)
|
|
500
|
+
msg = f"lingshu: publish panel {name} v{mf['version']}"
|
|
501
|
+
pushed, errors = [], []
|
|
502
|
+
if c.get("gitee_repo") and c.get("gitee_token"):
|
|
503
|
+
try:
|
|
504
|
+
_push_one("gitee", c["gitee_repo"], c["gitee_token"], branch,
|
|
505
|
+
name, zbytes, screenshot, shot_ext, entry, msg,
|
|
506
|
+
kind_key="panels", author=author)
|
|
507
|
+
pushed.append("Gitee")
|
|
508
|
+
except Exception as exc: # noqa: BLE001
|
|
509
|
+
errors.append(f"Gitee:{exc}")
|
|
510
|
+
else:
|
|
511
|
+
errors.append("Gitee 未配置仓库或 token(发布需要 Gitee)")
|
|
512
|
+
if c.get("github_repo") and c.get("github_token"):
|
|
513
|
+
try:
|
|
514
|
+
_push_one("github", c["github_repo"], c["github_token"], branch,
|
|
515
|
+
name, zbytes, screenshot, shot_ext, entry, msg,
|
|
516
|
+
kind_key="panels", author=author)
|
|
517
|
+
pushed.append("GitHub")
|
|
518
|
+
except Exception as exc: # noqa: BLE001
|
|
519
|
+
errors.append(f"GitHub:{exc}")
|
|
520
|
+
global _CACHE
|
|
521
|
+
_CACHE = None # 发布后清缓存,下次列表强制刷新
|
|
522
|
+
if not pushed:
|
|
523
|
+
raise RuntimeError(f"发布失败:{';'.join(errors)}")
|
|
524
|
+
return {"ok": True, "pushed": pushed,
|
|
525
|
+
"errors": errors, "entry": entry}
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
def cloud_extension_publish(ext_id: str) -> dict:
|
|
529
|
+
"""把拓展已安装目录发布到云端 extensions/<id>.zip + index.json 的 extensions 数组。
|
|
530
|
+
|
|
531
|
+
结构与面板发布平行:同一个 index.json,两个子清单互不干扰。
|
|
532
|
+
"""
|
|
533
|
+
import io
|
|
534
|
+
import zipfile
|
|
535
|
+
|
|
536
|
+
src = brain.HOME / "extensions" / "installed" / ext_id
|
|
537
|
+
if not src.is_dir() or not _safe_name(ext_id):
|
|
538
|
+
raise ValueError(f"已安装拓展不存在或 id 不合法:{ext_id}")
|
|
539
|
+
meta = panels._extension_meta(src) if hasattr(panels, "_extension_meta") else {}
|
|
540
|
+
buf = io.BytesIO()
|
|
541
|
+
total = 0
|
|
542
|
+
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as z:
|
|
543
|
+
for f in sorted(src.rglob("*")):
|
|
544
|
+
if f.is_file() and not f.name.startswith("."):
|
|
545
|
+
total += f.stat().st_size
|
|
546
|
+
if total > panels.ZIP_MAX_TOTAL:
|
|
547
|
+
raise ValueError("拓展超过 50MB,不适合云端分发")
|
|
548
|
+
z.write(f, f.relative_to(src).as_posix())
|
|
549
|
+
zbytes = buf.getvalue()
|
|
550
|
+
now = time.strftime("%Y-%m-%d %H:%M", time.localtime())
|
|
551
|
+
entry = {"id": ext_id, "title": ext_id,
|
|
552
|
+
"version": meta.get("version", "0.0.0"),
|
|
553
|
+
"author": meta.get("author", ""),
|
|
554
|
+
"size": len(zbytes), "sha256": hashlib.sha256(zbytes).hexdigest(), "updated": now}
|
|
555
|
+
c = _cfg()
|
|
556
|
+
branch = c.get("branch", "master")
|
|
557
|
+
author = _commit_author(c)
|
|
558
|
+
msg = f"lingshu: publish extension {ext_id} v{entry['version']}"
|
|
559
|
+
pushed, errors = [], []
|
|
560
|
+
for kind in ("gitee", "github"):
|
|
561
|
+
repo = c.get(f"{kind}_repo", "")
|
|
562
|
+
token = c.get(f"{kind}_token", "")
|
|
563
|
+
if repo and token:
|
|
564
|
+
try:
|
|
565
|
+
_push_one(kind, repo, token, branch, ext_id, zbytes,
|
|
566
|
+
None, ".png", entry, msg, kind_key="extensions",
|
|
567
|
+
author=author)
|
|
568
|
+
pushed.append("Gitee" if kind == "gitee" else "GitHub")
|
|
569
|
+
except Exception as exc: # noqa: BLE001
|
|
570
|
+
errors.append(f"{'Gitee' if kind == 'gitee' else 'GitHub'}:{exc}")
|
|
571
|
+
global _CACHE
|
|
572
|
+
_CACHE = None
|
|
573
|
+
if not pushed:
|
|
574
|
+
raise RuntimeError(f"发布失败:{';'.join(errors)}")
|
|
575
|
+
return {"ok": True, "pushed": pushed, "errors": errors, "entry": entry}
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
def _push_one(kind: str, repo: str, token: str, branch: str, name: str,
|
|
579
|
+
zbytes: bytes, shot: bytes | None, shot_ext: str,
|
|
580
|
+
entry: dict, msg: str, kind_key: str = "panels",
|
|
581
|
+
author: dict | None = None) -> None:
|
|
582
|
+
"""向一个远端推 zip + 截图 + index.json 条目。
|
|
583
|
+
|
|
584
|
+
kind_key 决定子目录与清单键:panels → panels/xxx.zip + index.json.panels,
|
|
585
|
+
extensions → extensions/xxx.zip + index.json.extensions(同一份 index.json)。
|
|
586
|
+
"""
|
|
587
|
+
put = _gitee_put if kind == "gitee" else _github_put
|
|
588
|
+
get = _gitee_get if kind == "gitee" else _github_get
|
|
589
|
+
put(repo, f"{kind_key}/{name}.zip", token, branch, zbytes, msg, author)
|
|
590
|
+
if shot is not None:
|
|
591
|
+
put(repo, f"shots/{name}{shot_ext}", token, branch, shot, msg, author)
|
|
592
|
+
# index.json 合并回写:读旧清单 → 覆盖同名条目 → 写回(并发竞争可接受,个人市场)
|
|
593
|
+
raw = get(repo, "index.json", token, branch)
|
|
594
|
+
try:
|
|
595
|
+
data = json.loads(raw.decode("utf-8")) if raw else {"panels": [], "extensions": []}
|
|
596
|
+
except Exception: # noqa: BLE001 —— 云端清单坏了就重建
|
|
597
|
+
data = {"panels": [], "extensions": []}
|
|
598
|
+
if not isinstance(data, dict):
|
|
599
|
+
data = {"panels": [], "extensions": []}
|
|
600
|
+
# key 名:面板条目按 name 匹配,拓展条目按 id 匹配
|
|
601
|
+
id_key = "id" if kind_key == "extensions" else "name"
|
|
602
|
+
items = [p for p in (data.get(kind_key) or []) if p.get(id_key) != name]
|
|
603
|
+
items.append(entry)
|
|
604
|
+
items.sort(key=lambda p: str(p.get("updated", "")), reverse=True)
|
|
605
|
+
data[kind_key] = items
|
|
606
|
+
data.setdefault("panels", data.get("panels", []))
|
|
607
|
+
put(repo, "index.json", token, branch,
|
|
608
|
+
json.dumps(data, ensure_ascii=False, indent=2).encode("utf-8"),
|
|
609
|
+
msg, author)
|