Skip to main content
N

dsh-token-lock

nuscior/dsh-token-lock

Keeps a cash reserve for chat, so task sessions pause automatically once the account balance drops below it while chat keeps working.

Install

dsh plugin --profile web add github:nuscior/dsh-token-lock

README

dsh-token-lock · 聊天余额缓冲

DeepSeek Harness 插件:给聊天留一笔余额。账户余额掉到预留线以下时,任务自动暂停,聊天继续可用。

它只做一件事:在任务会话每次模型步骤之前检查余额,低于预留线就写一条预定回复、结束这个 turn,有 goal 就把 goal 暂停;聊天会话永远不被它拦。

哪些会话算「任务」

  • 子代理会话(origin = subagent、有 parentSession、delegationDepth > 0);
  • 由定时任务发起的会话(消息里带 source.kind = schedule);
  • 你自己配置的 agent preset(taskPresets,前缀匹配)。

其余都是聊天,不受影响。

余额从哪来

按顺序尝试,拿不到就放行(fail open),不会把任务一直卡住:

  1. DeepSeek 官方 GET https://api.deepseek.com/user/balance,用宿主凭据里的 DEEPSEEK_API_KEY(ctx.credentials);
  2. 读不到时,用 dsh-whale-widget 缓存的 ~/.dsh/.dshw-usage.json 里的 lastBalance;
  3. 都读不到,或数据超过 10 分钟没刷新,就当作未知,不拦。

配置

配置文件在 ~/.dsh/token-lock/config.json,不存在时用默认值:

字段默认说明
chatReserve5给聊天预留多少元;余额低于它时任务暂停
reserveEnabledtrue总开关;关掉等于不拦任何任务
taskPresets[]额外算作任务的 agent preset 名(前缀匹配,比如 maoying 会匹配 maoying-writer)
overrides{}按会话 id 强制指定 "chat" 或 "task"
taskReply见源码拦截时写进任务会话的回复模板,支持 {balance} / {reserve} 占位

使用

  • 输入框下方 dock 里有一个小 chip,显示当前预留开关状态,点一下切换。
  • /reserve 命令:查看状态与配置路径、开关注留。
  • 工具 token_lock:给智能体读取当前状态用的。

注意

  • 只有能读到 DeepSeek 账户余额的环境才有实际效果(需要 DEEPSEEK_API_KEY 凭据,或装了 dsh-whale-widget 作为兜底)。读不到时插件不会拦截任何东西。
  • 它不花自己的钱、不发请求给第三方、不上传日志;只读余额接口,本地写 ~/.dsh/token-lock/。
  • 拦截是按天去重的:同一个任务会话同一天只会收到一条预定回复。

安装

需要 DSH 0.2.2-rc.1 及以上。

  • 插件市场:在 插件 → 插件市场 里搜索 dsh-token-lock。

  • 手动:

    dsh plugin --profile <profile> add github:nuscior/dsh-token-lock
    

License

MIT,见 LICENSE。


English

Keeps a cash reserve for chat. Once the account balance drops to the reserve line, task sessions pause automatically while chat keeps working.

A session counts as a task when it is a subagent session, a scheduled run, or matches one of the taskPresets you configure. Everything else is chat and is never blocked.

Balance is read from GET https://api.deepseek.com/user/balance using the host's DEEPSEEK_API_KEY credential; when that is unavailable it falls back to the balance cached by dsh-whale-widget, and when neither is available (or data is older than 10 minutes) the plugin fails open and blocks nothing.

Configuration lives in ~/.dsh/token-lock/config.json (chatReserve, reserveEnabled, taskPresets, overrides, taskReply). A chip in the composer dock shows the current state and toggles the reserve; /reserve prints status, and the token_lock tool exposes it to agents.

Client bundle plus a small host half; no dependencies, no build step, no third-party network calls beyond the balance endpoint.

Verified on DSH 0.2.2-rc.5; engines.dsh is >=0.2.2-rc.1.

Related plugins