Back to home

Turbo-Harry

dsh-status-balance

No description

Stars
0
Language
JavaScript
Created
Aug 15, 2026
Updated
Aug 15, 2026

Introduction

dsh-status-balance

DeepSeek Harness 的树外(out-of-tree)双面插件:在 右下角 显示 DeepSeek API 账户余额。

  • host 半身lib/index.js):读取 DEEPSEEK_API_KEY,调 GET https://api.deepseek.com/user/balance,缓存结果;一方面 ctx.emit("balance/updated", …) 推给所有客户端,另一方面注册只读的 /status-balance 拉取端点。
  • client 半身lib/client.js):往 frame 级 shell.overlay 槽注册一个小角标;挂载时先 fetch("/status-balance") 秒出首屏,再订阅 balance/updated 实时刷新。

架构要点(为什么是这样)

  1. 密钥只在 host 侧:余额接口要 Bearer 密钥,浏览器绝不能碰。host 半身零 import(credentialRef 运行时是恒等函数,直接用字符串 "DEEPSEEK_API_KEY")。
  2. 推送 + 拉取双通道:实时更新走 forwarded-event(host ctx.emit("balance/updated")dsh-host-apiproxy 转发循环 → 客户端 ctx.remote.$on,需第 1 步的 allowlist);首屏秒出走只读 HTTP 端点 /status-balance(host 用 ctx.webServer.register 注册,client 挂载时 fetch 一次)。
  3. 右下角 = shell.overlaydsh-client-ui-layout 把它渲染进一个 position:absolute; inset:0; z-index:20 的浮层,角标用绝对定位钉在 right:16px; bottom:16px

安装(4 步)

1. 把 balance/updated 加进转发 allowlist

编辑 @deepseek-ai/dsh-api-remotes/lib/index.js,在 API_REMOTE_FORWARDED_EVENTS 数组里加一行:

const API_REMOTE_FORWARDED_EVENTS = [
	"agent-preset/selected",
	// ...
	"settings/document-updated",
	"balance/updated"     // ← 新增
];

该文件位于(本机): /Users/wuhai/.nvm/versions/node/v22.19.0/lib/node_modules/@deepseek-ai/dsh/node_modules/@deepseek-ai/dsh-api-remotes/lib/index.js

注:这是对第一方包的单点改动,重装 DSH 会被覆盖;客户端 $on 无运行时 allowlist 校验,所以只改这一处 host 文件即可。

2. 把本包装进 web profile

dsh plugin --profile web add "file:/Users/wuhai/Desktop/untitled folder/dsh-status-balance"

(等价于在 ~/.dsh/profiles/webpnpm add file:...。)

3. 注册到 ~/.dsh/profiles/web/cordis.patch.yml

[] 替换为:

- insert:
    - id: status-balance
      name: 'dsh-status-balance'

单行 name 决定 host 行;包内的 dsh.client 字段同时把它登记进浏览器 roster。

4. 重启 web 并刷新

dsh web    # 或 dsh --profile web

打开 http://127.0.0.1:3080 刷新,右下角应出现「余额 ¥98.22」(数字按你的账户)。未配置 key 时显示「未配置 API Key」,网络失败显示「获取失败」。

验证数据层(可选,不启动 DSH)

node scripts/test-balance.mjs

只打印余额 JSON,不打印密钥。

配置

lib/index.js 顶部的常量:

  • POLL_INTERVAL_MS:轮询间隔(默认 30s)
  • API_KEY_REF:凭证引用名(默认 DEEPSEEK_API_KEY

lib/client.js 顶部的常量:

  • LOW_BALANCE_THRESHOLD:低余额提醒阈值(默认 10,单位同币种)
  • LOW_BALANCE_CURRENCY:按哪个币种判断(默认 "CNY"

当对应币种的 total_balance 低于阈值时,角标变为琥珀色并脉冲闪烁,显示「⚠ 余额不足 ¥xx」;首次跌破阈值时还会在页面顶部弹一条一次性提示。鼠标悬停角标显示「余额低于 ¥10,请及时充值」。

密钥存在 ~/.dsh/.credentials.yaml,或走 DEEPSEEK_API_KEY 环境变量(dsh-credentials-local 会继承环境)。