feat(opencode): 支持 OpenCode Go 官方用量窗口查询与自动刷新 - #5
Merged
Conversation
OpenCode Go 上游(https://opencode.ai/zen/go/v1)官方提供用量端点 GET /zen/go/v1/usage(Bearer 复用消费 key),返回 rolling(5h)/weekly/monthly 三窗口的 percent 与 resetsAt。本提交为 openai apikey 账号(base_url 为 opencode.ai/zen/go/v1)接入该用量窗口: - 后端:OpenCodeGoUsageService——手动刷新(每账号 10s 节流)、 周期自动刷新(全局开关 + 账号开关双开启,interval 5-1440 分钟, leader lock + 每轮 20 账号 + 并发 4)、失败指数退避(上限 24h)、 Retry-After 尊重;401→unauthorized、403→failed;snapshot 持久化于 accounts.extra(单账号写入,无分组 CAS、无会话加密) - admin API:settings GET/PUT、/:id/opencode-go-usage GET、 auto-refresh PUT、refresh POST(与 Ollama Cloud usage 同构) - 前端:账号列表行内三窗口进度条(复用 UsageProgressBar)、 编辑弹窗内配置面板(详情+手动刷新+auto-refresh)、全局设置卡片、 en/zh i18n 验证: - 上游端点已用生产真实 Go key 实测 200,响应 {"usage":{"rolling":{"status":"ok","percent":6,...},...}}(2026-08-13) - go build ./... OK;service/handler/repo/dto/cmd 全量回归 OK - 新增单测 20 例(解析 401/403/畸形 JSON、退避、节流等)全绿 - frontend vitest 新增 7 例 + Ollama 回归 4 例全绿;vue-tsc 零错误
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
OpenCode Go($10/月订阅,5h/$12、周/$30、月/$60 三个用量窗口)已接入 sub2api 上游(openai apikey 账号,base_url =
https://opencode.ai/zen/go/v1)。Go 官方于 2026-08-11 上线了用量端点GET /zen/go/v1/usage(PR anomalyco/opencode#16513,复用消费端 Bearer key,未写入官方文档),返回 rolling/weekly/monthly 三窗口的percent与resetsAt。本 PR 为这类账号实现管理端用量窗口,架构对齐既有 Ollama Cloud usage 功能,但按 API 形态大幅裁剪。
变更
后端
OpenCodeGoUsageService:手动刷新(每账号 10s 节流)、周期自动刷新(全局开关 + 账号开关双开启;interval 5–1440 min 默认 15;leader lock + 每轮 20 账号 + 并发 4)、失败指数退避(2^n 上限 24h,±10% 抖动,尊重 Retry-After)unauthorized(保留旧数据);403 →failed(无 Go 订阅);非 2xx/畸形 JSON →failed;解析宽松(顶层可包usage对象,字段缺失不整体失败)accounts.extra(单账号写入,无分组 CAS、无会话加密——API 无需 Cookie)platform=openai && type=apikey && base_urlhost 精确opencode.ai、path/zen/go/v1opencode_go_usage,extra 脱敏前端
UsageProgressBar,状态徽章)验证
{"usage":{"rolling":{"status":"ok","percent":6,"resetsAt":"..."},"weekly":{...},"monthly":{...}}}go build ./...OK;service / handler/admin / repository / dto / cmd 全量回归 OK;gofmt、go vet 干净vue-tsc --noEmit零错误已知限制
percent/resetsAt),解析做了防御但上游结构变动仍需跟进resetsAt直接信上游,不自行计算滚动)