架构
August 30, 2026 · View on GitHub
完整技术栈、组件职责与上游拾取手册见 project-facilities.md。 本文档保持精简,只记录当前仓库实际存在的目录与数据流。
目录结构(当前实际)
dsh-commandgo-usage/
├── package.json # bundle 包声明(dsh.bundle.patch)
├── cordis.patch.yml # 插件挂载层
├── tsconfig.json / tsconfig.build.json
├── tsdown.config.ts # 构建到 lib/(Host + Browser)
├── build/
│ ├── tsdown.client.ts # 共享 client bundle 构建预设
│ └── web-platform.ts # 浏览器平台模块表
├── README.md / README.en.md / CHANGELOG.md / LICENSE
├── docs/ # 项目文档
├── assets/readme/ # 文档 SVG
├── src/
│ ├── index.ts # Host apply 入口
│ ├── config.ts # Config + Schemastery schema + 默认值
│ ├── credentials.ts # API Key 解析链
│ ├── client.ts # CommandCodeClient:/alpha/* 抓取
│ ├── store.ts # UsageStore 内存快照 + 订阅
│ ├── poller.ts # UsagePoller 定时轮询 + 退避 + 并发控制
│ ├── routes.ts # 只读 JSON 路由(status / turn-cost / health)
│ ├── ui-routes.ts # UI 路由(credential / refresh / plans / plan-preference)
│ ├── session-watcher.ts # session/event 每轮消耗聚合
│ ├── commands.ts # /commandcode-usage 命令
│ ├── plan.ts # 套餐额度表
│ ├── types.ts # 数据契约
│ └── client/ # 浏览器端(挂件、设置页、hooks、api、locales、样式)
└── tests/
├── client.test.ts
└── poller.test.ts
数据流
UsagePoller按配置间隔调用CommandCodeClient抓取/alpha/*。CommandCodeClient归一化 4 个端点并做失败降级/分类,结果写入UsageStore。routes.ts将UsageStore暴露为同源 JSON 路由。- 浏览器端
src/client/通过 JSON 路由读取数据并渲染挂件/设置页。 SessionWatcher监听session/event,聚合每轮消耗并发布到turn-cost.json。ui-routes.ts提供凭据写入、连接测试、手动刷新与 Plan 偏好管理,全部走 Host 侧能力。
生命周期
- Host
apply在ctx.effect中启动 Poller、注册路由、SessionWatcher、命令与 UI routes,返回统一 disposer。 - Browser
apply通过ctx.effect注册 locale 与两个槽位(settings.section、sidebar.footer.action)。 - 卸载时所有注册自动清理;命令通过子 Fiber
ctx.inject(['commands'])注册。
注意:README/docs 中提到的
scripts/dev-loopback.sh当前仓库不存在,如需使用需补齐。