fix(sync): 适配 OpenCode 文档改版(Endpoints 改为 /inference/go/<vendor>/v1/)

文档 Endpoints 表地址从 https://opencode.ai/zen/go/v1/<path> 改为按供应商分线:
  /inference/go/openai/v1/{chat/completions,responses}
  /inference/go/anthropic/v1/messages
  /inference/go/google/...(Google 原生,网关不支持)
旧的 "/zen/go/v1/" 硬匹配导致一个模型都解析不出,每日同步固定 exit 2。
改为按路径末尾识别三条协议线(新旧格式都兼容),认不出的跳过并在报告里单列。
This commit is contained in:
Jidekin committed 2026-10-21 10:26:51 +08:00
1 parent db0a73031f
commit c2e1226836
1 file changed
+33 -3
+33 -3
View File
@@ -84,18 +84,44 @@ def _norm(s: str) -> str:
# ── OpenCode Go ──────────────────────────────────────────────── # ── OpenCode Go ────────────────────────────────────────────────
# 2026-10-21 文档改版:Endpoints 表里的地址从
# https://opencode.ai/zen/go/v1/<path> (旧)
# 改成按供应商分线:
# https://opencode.ai/inference/go/openai/v1/chat/completions
# https://opencode.ai/inference/go/openai/v1/responses
# https://opencode.ai/inference/go/anthropic/v1/messages
# https://opencode.ai/inference/go/google/v1beta/models/<id> (Google 原生,网关不支持)
# 网关只认三条线,所以按「路径末尾」认协议线,认不出的(Google 原生)跳过并报告。
OC_PROTOCOLS = ("chat/completions", "messages", "responses")
OC_SKIPPED: list[str] = []
def _oc_protocol(url: str) -> str | None:
"""文档 Endpoint → 网关协议线;认不出返回 None。"""
path = url.split("?", 1)[0].rstrip("/")
for proto in OC_PROTOCOLS:
if path.endswith("/" + proto):
return proto
return None
def fetch_opencode() -> dict[str, dict]: def fetch_opencode() -> dict[str, dict]:
"""返回 {model_id: {name, protocol, free}}。""" """返回 {model_id: {name, protocol, free}}。"""
html = _get(OC_DOC_URL) html = _get(OC_DOC_URL)
models: dict[str, dict] = {} models: dict[str, dict] = {}
skipped: list[str] = []
for row in re.findall(r"<tr>(.*?)</tr>", html, re.S): for row in re.findall(r"<tr>(.*?)</tr>", html, re.S):
cells = [_strip_tags(c) for c in re.findall(r"<td[^>]*>(.*?)</td>", row, re.S)] cells = [_strip_tags(c) for c in re.findall(r"<td[^>]*>(.*?)</td>", row, re.S)]
if len(cells) < 4 or "/zen/go/v1/" not in cells[2]: if len(cells) < 4 or "opencode.ai/" not in cells[2]:
continue continue
tail = cells[2].split("/zen/go/v1/", 1)[1].strip("/").split("?")[0] proto = _oc_protocol(cells[2])
models[cells[1]] = {"name": cells[0], "protocol": tail, "free": False} if proto is None:
skipped.append(cells[1])
continue
models[cells[1]] = {"name": cells[0], "protocol": proto, "free": False}
if not models: if not models:
raise RuntimeError("OpenCode 文档没解析出 Endpoints 表(页面可能改版)") raise RuntimeError("OpenCode 文档没解析出 Endpoints 表(页面可能改版)")
OC_SKIPPED[:] = sorted(skipped)
# 免费模型:价格表里 Input/Output 都是 Free 的那些(名字对 id 做归一匹配) # 免费模型:价格表里 Input/Output 都是 Free 的那些(名字对 id 做归一匹配)
by_norm = {_norm(mid): mid for mid in models} by_norm = {_norm(mid): mid for mid in models}
@@ -448,6 +474,7 @@ def diff(cfg: dict, oc_doc: dict, agnes_doc: dict,
"protocol_drift": proto_drift, "protocol_drift": proto_drift,
"free_drift": free_drift, "free_drift": free_drift,
"ctx_drift": oc_ctx_drift, "ctx_drift": oc_ctx_drift,
"skipped": list(OC_SKIPPED),
} }
# Agnes # Agnes
@@ -564,6 +591,9 @@ def render(report: dict) -> str:
if not any(oc[k] for k in ("added", "removed", "protocol_drift", "free_drift", if not any(oc[k] for k in ("added", "removed", "protocol_drift", "free_drift",
"ctx_drift")): "ctx_drift")):
lines.append(" 无漂移 ✓") lines.append(" 无漂移 ✓")
if oc.get("skipped"):
lines.append(" 跳过(文档端点不是三条网关协议线,网关不支持):")
lines += [f" · {m}" for m in oc["skipped"]]
lines.append("") lines.append("")
lines.append(f"== Agnes == 文档 {ag['doc_count']} 个 / 网关 {ag['cfg_count']} 个") lines.append(f"== Agnes == 文档 {ag['doc_count']} 个 / 网关 {ag['cfg_count']} 个")