feat(models): 前端展示 OpenCode Go 的月度额度与训练策略
老板要求:模型清单里能直接看到「这个模型每月能调多少」「是否拿你的代码训练」。
数据源:官方 models 接口(与 1.0.6 的同步修复同源,无需鉴权)
go.monthly_usage = {go, go-plus, go-teams} # 官方口径:只用这一个模型时各套餐每月额度
go.training = false # 是否用你的请求训练
go.zdr = true # 零数据保留
为什么不写进 gateway.yaml:这是上游随时会变的市场信息,不属于网关静态配置,
也不该让每日同步脚本再背一层 plan/落盘逻辑。改为后端按需拉取 + 6 小时缓存:
- app/gomodels.py(新增):拉取 + 缓存 + 失败保留旧值(5 分钟后重试),
任何异常都不影响 /v1/models(最坏就是前端不显示这一列)。
- app/main.py:_models_payload() 拆成 base + 合并层。
base 仍按配置 mtime 缓存;合并层再按 (base 对象, go 缓存时间戳) 缓存,
两个都没变时返回同一个对象(保持原有缓存身份语义),任一变化自动重拼。
别名条目不重复挂 go(额度/训练口径就是目标模型的,前端按 alias_of 取)。
- 前端 Models.vue:新增「Go 额度」列 —— 默认档额度(如 $100)+ 训练徽章
(不训练=绿 / 会训练=红),悬浮显示三档明细与零数据保留说明;
非 OpenCode 供应商显示「—」。
- locales:中英文各补 8 个键(check:i18n 通过)。
验证:直接调 _models_payload() —— 83 个模型里 28 个带 go 字段、其余为 None;
前端 vite build 通过;api-gateway 重启后 /healthz 200。
This commit is contained in:
1 parent
f3255b4d73
commit
9d679a02cd
5 files changed
+216
-1
No files matched your search
+37
-1
@@ -56,6 +56,8 @@ from app.sessions import SessionStore
|
||||
from app.users import UsersStore, UserError, key_limit_for_role
|
||||
from app.history import HistoryStore, HistoryError, MAX_ITEMS as HISTORY_CAP
|
||||
from app.feedback import FeedbackStore, FeedbackError, MAX_ENTRIES as FEEDBACK_CAP
|
||||
# 2026-11-19: OpenCode Go 套餐元数据(月度额度 / 是否训练 / 零保留),只用于前端展示
|
||||
from app.gomodels import get_go_meta, cache_stamp as gomodels_cache_stamp
|
||||
from app.video_owners import VideoOwnerStore
|
||||
from app import settings as gw_settings
|
||||
from app.agents import AgentStore, AgentsError
|
||||
@@ -1494,7 +1496,7 @@ def _attach_cost(resp: dict, model: str, usage: dict, cfg) -> None:
|
||||
# ─────────────────────────────────────
|
||||
# GET /models + GET /v1/models(P1-7 / P0-3)
|
||||
# ─────────────────────────────────────
|
||||
def _models_payload():
|
||||
def _models_payload_base():
|
||||
cfg = _cfg()
|
||||
# 优化: 目录按配置 mtime 缓存(热重载改配置才重建;静默期每次请求直接命中,省掉
|
||||
# all_models() 的重复遍历与对象构建)。cfg 引用同一 config 对象,
|
||||
@@ -1571,6 +1573,40 @@ def _models_payload():
|
||||
return payload
|
||||
|
||||
|
||||
def _models_payload():
|
||||
"""在按 mtime 缓存的目录上,现拼 OpenCode Go 的套餐元数据。
|
||||
|
||||
2026-11-19(老板要求):前端要显示「这个模型每月能调多少」「是否拿你的代码训练」。
|
||||
这两个字段来自官方 models 接口(有自己的 6 小时缓存,见 app/gomodels.py),
|
||||
所以**不烤进** `_models_cache`(否则配置不变就永远停在第一次的快照)。
|
||||
|
||||
拼好后按 (配置 mtime, go 缓存时间戳) 再缓存一层 —— 两个都没变时返回**同一个对象**
|
||||
(`test_models_payload_cached_and_invalidated_by_mtime` 依赖这个身份语义);
|
||||
任一变化就重拼。拿不到 go 数据时直接原样返回 base。
|
||||
"""
|
||||
base = _models_payload_base()
|
||||
go = get_go_meta()
|
||||
if not go:
|
||||
return base
|
||||
# 缓存键带上 base 的对象 id(并**持有 base 的强引用**,保证 id 不会被复用)——
|
||||
# 这样只要 base 被重建(配置热重载,或测试直接把 _models_cache 置 None 换 config),
|
||||
# 合并层就跟着失效;go 数据刷新(时间戳变化)也会失效。
|
||||
key = (id(base), gomodels_cache_stamp())
|
||||
cached = _state.get("_models_cache_go")
|
||||
if cached is not None and cached[0] == key and cached[2] is base:
|
||||
return cached[1]
|
||||
data = []
|
||||
for item in base.get("data") or []:
|
||||
mid = item.get("id")
|
||||
# 别名(alias_of)不重复挂:它的额度/训练口径就是目标模型的,前端按目标取
|
||||
if mid in go and "alias_of" not in item:
|
||||
item = {**item, "go": go[mid]}
|
||||
data.append(item)
|
||||
payload = {**base, "data": data}
|
||||
_state["_models_cache_go"] = (key, payload, base)
|
||||
return payload
|
||||
|
||||
|
||||
@app.get("/models")
|
||||
async def list_models(request: Request, _auth=Depends(require_auth)):
|
||||
return _models_payload()
|
||||
|
||||
Reference in new issue
Block a user