bowenliang123/dsh-context
bowenliang123/dsh-contextDSH 上下文洞察面板:Context 仪表盘 + /context命令 + Context 浏览器,查看 Context的分类组成、内容详情、演进趋势、压缩/注入事件、统计等一站式 Context 全生命周期管理。
在设置页展示 DeepSeek API 余额与最近 24 小时用量,包括估算消费、Token、请求次数和按小时时间线。
English | 简体中文 | Português (Brasil)
安装后,打开 DeepSeek Harness 的设置 → API用量页面,即可查看 DeepSeek API 用量。页面展示账户余额、最近 24 小时(日视图为 14 天)的估算消费金额、Token 数量与 API 请求次数,并以类似 DeepSeek 官方平台用量页的时间线柱状图呈现。
GET /user/balance 接口。DEEPSEEK_API_KEY 凭证(通过 harness 的 credentials 服务)。┌─────────────────────────────── Host(Node.js)───────────────────────────────┐
│ src/index.js │
│ • ctx.on('llm/stream', ...) ← waterfall:将每次真实模型调用的 │
│ provider 上报的 TokenUsage(输入/输出/缓存命中/缓存未命中, │
│ 已是互斥分项,与 DeepSeek 计费口径一致) │
│ 折叠进内存中的按小时 + 按天桶 │
│ • fetchBalance() ← credentials.resolve('DEEPSEEK_API_KEY') │
│ → subprocess curl → https://api.deepseek.com/user/balance │
│ (web.fetch 无法携带 Authorization 头,故用 curl) │
│ • webServer.register('/ds-api-usage/snapshot') ← 供 Client 读取的 JSON 端点 │
└──────────────────────────────────────────────────────────────────────────────┘
│ fetch('/ds-api-usage/snapshot')
▼
┌────────────────────────────── Client(浏览器)───────────────────────────────┐
│ client/bundle.js(web bundle;client/index.js 为动态插件源码) │
│ • slots.inject('settings.section') → 新增设置页(标签经 locale 服务本地化) │
│ • 余额卡片 + 3 张指标卡 + 时间线柱状图 │
│ (消费 / Tokens / 请求;24h 或 14d) │
│ • 每 30 秒自动刷新(静态 bundle 使用原生 setInterval) │
└──────────────────────────────────────────────────────────────────────────────┘
usage chunk(StreamChunk 的 type: 'usage'、TokenUsage),与 harness 自身用于会话统计的 provider 上报数据完全一致。PRICING 表(src/index.js)中按模型计算:
hit 单价miss 单价output 单价peak / offPeak 单价的模型按请求的 UTC 小时(peakHoursUtc 中的窗口:01:00–04:00 与 06:00–10:00 UTC)计费;其余模型使用 flat 单价。$DSH_HOME/storages/ds-api-usage.json(写入去抖:最多每 60 秒一次,插件关闭时冲刷;fail-safe:读写错误绝不会影响统计)。14 天视图可跨 web app 重启存活;删除该文件即可清零。harness 另维护其自有的按会话持久化 token 投影。dsh plugin add 安装(推荐,GitHub 或 npm)直接从本 GitHub 仓库安装:
dsh plugin --profile web add github:Sev7een/ds-api-usage
或发布到 npm 后:
dsh plugin --profile web add dsh-plugin-ds-api-usage
dsh plugin 会在 profile 目录中转发给 pnpm,并将包调和进 profile 的 bundle 列表(dsh.profile.bundles)。包内 cordis.patch.yml(经 package.json 的 dsh.bundle.patch 声明)随后把插件行插入宿主组合;dsh.client 声明则让 web 外壳加载 client/bundle.js 作为设置页。
原版是会话级动态 Cordis 插件,通过 cordis_define / cordis_run 创建(参见 DeepSeek Harness 文档)。code.host 的函数体即 src/index.js 去掉 module.exports 包装;code.client 的函数体即 client/index.js 去掉包装。
注意:动态形态使用沙箱私有的
harness.handle/host.call通道(client/index.js),而静态 bundle 形态(client/bundle.js)通过 HTTP 路由/ds-api-usage/snapshot与 Host 通信。修改协议时请保持两者同步。
在你的 profile 的宿主组合(cordis.patch.yml)中添加:
- insert:
- id: ds-api-usage
name: 'dsh-plugin-ds-api-usage'
或不安装包、以相对路径指向本仓库。本插件属于 Host 平面:它读取 Host 的 credentials、subprocess、timer、webServer 服务,并将客户端设置页注册到根作用域的 settings.section 插槽,因此应放在宿主组合中,而不是某个 agent preset 内。
DEEPSEEK_API_KEY 凭证可通过 credentials 服务解析)curl(用于余额接口)npm run check # 语法检查两个半端
npm test # 离线测试套件:定价解析器(fixtures)+ peak/off-peak 计费逻辑
.github/workflows/update-pricing.yml(每日 cron + 手动触发)会重新解析官方定价页,表有变化时自动开启 PR;npm run update:pricing 可本地执行同样操作(--apply 写入 src/index.js 中的生成块)。请只通过脚本修改表格——__PRICING_BEGIN__ / __PRICING_END__ 标记之间的块为生成内容。locale 服务本地化(命名空间 settings.ds-api-usage),并跟随 harness 的当前语言:提供 harness 的 zh/en 词典及 pt-BR 条目(为将来 harness 支持预留;当前语言缺少的键会回退到 zh)。仓库自带两个工作流——无需任何密钥或 API key,唯一前提是仓库已启用 GitHub Actions(默认;可在 Settings → Actions → General → Allow all actions 确认):
ci.yml — 每次 push 与 pull request 都会运行:npm run check(两端语法检查)与 npm test(基于页面 fixtures 的离线测试套件)。update-pricing.yml — 每天(06:23 UTC cron)及手动触发(Actions → update-pricing → Run workflow)重新解析 DeepSeek 官方定价页;表格变化时自动开启 PR(工作流在默认 GITHUB_TOKEN 上声明了 contents: write 与 pull-requests: write,无需额外配置)。若文档页改版,解析校验会失败并让 job 响亮地失败,而不会开出错误的 PR。首次 push 后,请手动运行一次 Actions → update-pricing → Run workflow 以端到端验证整个流程(价格未变时,no change 是预期的绿色结果)。