fix(sync): OpenCode 模型改用官方 models 接口,修复文档改版导致的连续两天 exit=2
官方把文档的 `## Endpoints` 表改成「看 console 页 / 用 models 接口」, HTML 里不再有带 opencode.ai/ 地址的模型行,fetch_opencode() 的 <tr>/<td> 解析拿不到任何行 → 自动化日报「模型清单同步」整块失败: [2026-11-17 23:02:18] exit=2 RuntimeError: OpenCode 文档没解析出 Endpoints 表 [2026-11-18 23:02:29] exit=2 同上 改为优先读官方 models 接口(实测无需鉴权): GET https://opencode.ai/inference/go/v1/models (zen/go/v1/models 同源同内容) 每条自带 id/name/type/api{chat-completions,responses,messages,generate-content}/ recommended_api/limit{context,output}/cost.text.*.price/go.limited_regions, 据此推导协议线、免费标记与地区限制;HTML 表解析降级为兜底(接口不可用才走)。 首次落盘: - 删除 opencode/mimo-v2.5、mimo-v2.5-pro(上游已被 V2.6 取代) - 上下文 hy4-flash-free → 1048576/65536;qwen4-max → 1000000/262144 - 跳过 gemini-4-argon(仅 generate-content)、beam-free 与 muse-spark-1.2/1.3(地区限制) 验证:落盘后 28 个模型、/healthz 200、hot_reload 自动生效未重启、重跑「无漂移」; sync_onemin.py 同步通过(49 接口 / 43 配置 / 跳过重名 6)。 顺带把运行时的 data/ban_history.jsonl 加进 .gitignore(含被封 IP,属隐私)。
This commit is contained in:
1 parent
2eee87f5fa
commit
83ecea482f
4 files changed
+190
-22
No files matched your search
+99
-2
@@ -114,6 +114,99 @@ OC_REGION_BLOCKED = {
|
||||
}
|
||||
OC_REGION_SKIPPED: list[str] = []
|
||||
|
||||
# ── 2026-11-19 文档二次改版:Endpoints 表不再逐条列 URL ──────────────
|
||||
# 官方把 `## Endpoints` 改成了「去看 console 页 / 用 models 接口」,HTML 里
|
||||
# 已经没有 `opencode.ai/` 的模型行 → 连续两天 exit=2「没解析出 Endpoints 表」。
|
||||
# 现在官方 models 接口直接给出机器可读的元数据(**无需鉴权**,实测不带 token 也 200):
|
||||
# GET https://opencode.ai/inference/go/v1/models (zen/go/v1/models 同源同内容)
|
||||
# 每条模型带:
|
||||
# id / name / type
|
||||
# api: {chat-completions|responses|messages|generate-content: {base_url, sdk}}
|
||||
# recommended_api: 官方推荐线
|
||||
# limit: {context, output}
|
||||
# cost.text.input.price / cost.text.output.price (都为 0 → 免费)
|
||||
# go.limited_regions: true → 对部分地区不开放(实测正是这三个 contributor / beam-free)
|
||||
# 口径:**以 models 接口为准**,HTML 表解析只作兜底;接口也挂了才报错。
|
||||
OC_MODELS_API = "https://opencode.ai/inference/go/v1/models"
|
||||
OC_MODELS_API_FALLBACK = "https://opencode.ai/zen/go/v1/models"
|
||||
|
||||
# models 接口的 api 键 → 网关协议线(网关只支持这三条线)
|
||||
OC_API_TO_PROTO = {
|
||||
"chat-completions": "chat/completions",
|
||||
"responses": "responses",
|
||||
"messages": "messages",
|
||||
}
|
||||
|
||||
|
||||
def _oc_protocol_from_api(model: dict) -> tuple[str | None, list[str]]:
|
||||
"""从 models 接口的 api / recommended_api 选网关协议线。
|
||||
|
||||
返回 (协议线, 文档声明的全部 api 键);认不出(例如只有 generate-content)返回 (None, keys)。
|
||||
"""
|
||||
keys = list((model.get("api") or {}).keys())
|
||||
supported = [OC_API_TO_PROTO[k] for k in keys if k in OC_API_TO_PROTO]
|
||||
if not supported:
|
||||
return None, keys
|
||||
rec = model.get("recommended_api")
|
||||
if rec in OC_API_TO_PROTO and OC_API_TO_PROTO[rec] in supported:
|
||||
return OC_API_TO_PROTO[rec], keys
|
||||
for proto in OC_PROTOCOLS: # 没有推荐或推荐不支持 → chat/completions 优先
|
||||
if proto in supported:
|
||||
return proto, keys
|
||||
return supported[0], keys
|
||||
|
||||
|
||||
def _oc_is_free(model: dict) -> bool:
|
||||
"""cost.text 的 input / output 单价都为 0 → 免费。"""
|
||||
price = (model.get("cost") or {}).get("text") or {}
|
||||
return ((price.get("input") or {}).get("price") == 0
|
||||
and (price.get("output") or {}).get("price") == 0)
|
||||
|
||||
|
||||
def fetch_opencode() -> dict[str, dict]:
|
||||
"""返回 {model_id: {name, protocol, free}}。
|
||||
|
||||
优先走官方 models 接口(稳定、机器可读);接口不可用时回退到旧的 HTML 表解析。
|
||||
"""
|
||||
payload = None
|
||||
err: Exception | None = None
|
||||
for url in (OC_MODELS_API, OC_MODELS_API_FALLBACK):
|
||||
try:
|
||||
payload = json.loads(_get(url, timeout=60))
|
||||
if isinstance(payload, dict) and payload.get("data"):
|
||||
break
|
||||
payload = None
|
||||
except Exception as exc: # noqa: BLE001
|
||||
err = exc
|
||||
|
||||
if not payload:
|
||||
print(f"[warn] OpenCode models 接口不可用({err}),回退 HTML 解析", file=sys.stderr)
|
||||
return _fetch_opencode_html()
|
||||
|
||||
models: dict[str, dict] = {}
|
||||
skipped: list[str] = []
|
||||
region_skipped: list[str] = []
|
||||
for m in payload["data"]:
|
||||
mid = m.get("id")
|
||||
if not mid or (m.get("type") or "chat") != "chat":
|
||||
continue
|
||||
if (m.get("go") or {}).get("limited_regions") or mid in OC_REGION_BLOCKED:
|
||||
region_skipped.append(mid)
|
||||
continue
|
||||
proto, keys = _oc_protocol_from_api(m)
|
||||
if proto is None:
|
||||
skipped.append(f"{mid}({'/'.join(keys) or '无 api 字段'})")
|
||||
continue
|
||||
models[mid] = {"name": m.get("name") or mid,
|
||||
"protocol": proto,
|
||||
"free": _oc_is_free(m)}
|
||||
|
||||
if not models:
|
||||
raise RuntimeError("OpenCode models 接口返回 0 个可用模型(接口可能改版)")
|
||||
OC_SKIPPED[:] = sorted(skipped)
|
||||
OC_REGION_SKIPPED[:] = sorted(region_skipped)
|
||||
return models
|
||||
|
||||
|
||||
def _oc_protocol(url: str) -> str | None:
|
||||
"""文档 Endpoint → 网关协议线;认不出返回 None。"""
|
||||
@@ -124,8 +217,12 @@ def _oc_protocol(url: str) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def fetch_opencode() -> dict[str, dict]:
|
||||
"""返回 {model_id: {name, protocol, free}}。"""
|
||||
def _fetch_opencode_html() -> dict[str, dict]:
|
||||
"""【兜底】旧版 HTML 表解析。
|
||||
|
||||
2026-11-19 起官方 `## Endpoints` 表已不再逐条列 URL(内容搬到 console 页 +
|
||||
models 接口),这里正常不会走到;保留只为页面万一改回去时还能用。
|
||||
"""
|
||||
html = _get(OC_DOC_URL)
|
||||
models: dict[str, dict] = {}
|
||||
skipped: list[str] = []
|
||||
|
||||
Reference in new issue
Block a user