fix(v2): 余额凭据优先读取宿主 SQLite credential 表(auth.json 兜底) - #40
Merged
Merged
Conversation
opencode 2.x 把认证凭据保存在 opencode.db 的 credential 表,OAuth 刷新不再同步 auth.json——只读 auth.json 会在 token 过期后拿到陈旧 凭据(OpenAI 余额查询 401 Invalid API Key)。 - 新增 src/v2/credentials.ts:bun:sqlite 只读解析 active 凭据, 库不可用时静默回退 auth.json(兼容 v1/迁移遗留) - 轮询前等待凭据库就绪,避免首轮回退到过期快照 - 新增 tests/credentials.test.ts 覆盖解析与路径逻辑
Owner
|
感谢你的修复,合并后我会尽快发布新版本,感谢支持 |
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.
背景
#38 为 V2 增加了「auth.json 凭据复用」,但 opencode 2.x 已把认证凭据迁移到宿主 SQLite(
~/.local/share/opencode/opencode.db的credential表),OAuth 刷新只更新该库,auth.json不再同步。token 过期后余额查询会读到 auth.json 中的旧 access token:OpenAI Codex 请求chatgpt.com/backend-api/wham/usage返回 401,面板显示Invalid API Key(i18nbalErr401)。实测:auth.json 中的 token 于 2026-09-24 20:09 UTC 过期,而宿主中同一账号的 credential 已于 20:07 UTC 刷新并保持有效——旧 token 过期即报错,与插件版本无关。
改动
src/v2/credentials.ts(新增):以bun:sqlite只读打开宿主opencode.db,读取credential表中integration_id = ? AND active = 1的凭据(oauth → access、api → key)。bun:sqlite不可用(Node/CI 测试)或库不存在时静默回退auth.json,保持 1.x / 迁移遗留兼容;库路径遵循OPENCODE_DB与XDG_DATA_HOME。src/v2/commands.ts:findOpencodeKeyV2()的兜底由「直接读 auth.json」改为「SQLite active 凭据优先,auth.json 兜底」(V2 的Provider.Info仍不含 key 字段)。src/v2/index.tsx:余额轮询在解析自动凭据前等待凭据库就绪(模块加载即预热),避免插件启动首轮读到过期快照。tests/credentials.test.ts:覆盖 oauth/api 解析、无效输入、库路径解析与 Node 回退;新增npm run test:credentials。验证
npm run typecheck、npm run build、npm run test:balance、npm run test:credentials全部通过(Node 25)。credential表的 active token 实测 OpenAI Codex 接口返回 200(plan_type=prolite,primary window 68% used)。bun:sqlite在 esbuild 产物中保持运行时动态 import(与opencode-subagent-magazine的src/v2/db.ts相同模式,tsc/CI 不做静态解析);Node 环境加载失败会被捕获,不影响面板其余功能。说明:V1 路径仍读
auth.json,仅调整 V2;后续若上游合并,可在 1.7.x 发布后让 V2 用户自动获得修复。