跳到主要内容

dsh-ai-monitor

已验证

dsh-ai-monitor · v0.1.0 · MIT

DeepSeek Harness plugin: query AI Monitor balance / usage from local SQLite — cost-aware agent, auto bookkeeping with the AI Monitor proxy.

安装

dsh plugin add dsh-ai-monitor

dsh --profile default --dump-config 确认 layer 已生效 —— 参见安装指南

源码

标签

作者

说明文档

dsh-ai-monitor — DeepSeek Harness 插件

AI Monitor 与 DeepSeek Harness 的桥接插件:让 dsh agent 在跑任务前能感知 AI 预算, 任务后能汇报账单。只读本机 AI Monitor SQLite,不改动其数据。

安装(npm 发布后)

# 方式一:直接添加到 dsh profile
cd ~/.dsh/profiles/web
npm install dsh-ai-monitor

# 方式二:dsh CLI(若已支持 plugin add)
dsh plugin add dsh-ai-monitor

然后在 dsh 启动时叠加 bundle(或通过 profile 引用)加载插件。

前置要求

  • 本机已安装并运行 AI Monitor,且至少添加了一个账户
  • dsh 已配置好模型(Settings → Models 填入 API key)
  • 数据库路径:~/Library/Application Support/com.ai-monitor.app/ai-monitor.db(可用 DSH_AI_MONITOR_DB 环境变量覆盖)

功能

  • query_ai_balance 工具:查询各 AI 平台账户余额 + 今日 token 用量 + 消耗金额
  • 数据来源:AI Monitor 本地 SQLite
  • 只读模式(readonly: true + WAL 兼容),不锁库、不改数据

配合 AI Monitor 代理使用(推荐)

把 dsh 的 LLM 请求也指向 AI Monitor 本地代理(127.0.0.1:8899),可以实现自动记账闭环

# ~/.dsh/settings.yaml
llm-deepseek:
  baseURL: http://127.0.0.1:8899/v1

这样 dsh 的每次调用都会自动进入 AI Monitor 账本,agent 又能通过本插件查询余额—— 形成一个「消耗可查、余额可知」的完整闭环。

目录结构

ai-monitor-dsh-plugin/
├── package.json          # dsh.bundle manifest(发布用)
├── cordis.patch.yml      # 补丁层:把插件插入 web profile
├── tsconfig.json         # TS 编译配置(src → .)
├── src/index.ts          # 插件源码(TS)
├── index.js              # 编译产物(dsh 实际加载的 JS)
└── node_modules/         # better-sqlite3(原生模块)

本地开发 / 加载

# 1. 安装依赖
cd ai-monitor-dsh-plugin
npm install better-sqlite3
npx tsc                          # 编译 src/index.ts → index.js

# 2. 生成带补丁的启动配置
cat > /tmp/patch-abs.yml << 'EOF'
- insert:
    - id: ai-monitor
      name: '/绝对路径/ai-monitor-dsh-plugin/index.js'
EOF

# 3. 启动(需先停掉已有 dsh)
pkill -f "dsh" ; sleep 2
cd ai-monitor-dsh-plugin
nohup dsh --profile web --patch /tmp/patch-abs.yml > /tmp/dsh-web-plugin.log 2>&1 &

# 4. 验证加载成功(日志应出现)
tail /tmp/dsh-web-plugin.log
# [ai-monitor] plugin loaded. db=...

验证记录(2026-08-14)

  • dsh --patch xxx --dump-config 确认补丁合成进配置树
  • ✅ 带补丁启动,日志出现 [ai-monitor] plugin loaded. db=/Users/chen/Library/Application Support/com.ai-monitor.app/ai-monitor.db
  • ✅ 查询 SQL 独立验证:DeepSeek 余额 ¥8.16、今日 27.7 万 tokens / 约 ¥2.15
  • 端到端验证通过(用户实测):在 dsh Web UI 配置 DeepSeek API key 后,agent 成功调用 query_ai_balance, 返回真实数据——DeepSeek 余额 ¥8.07、今日 281K tokens / 约 ¥2.25,并自动附带数据边界说明

发布到 npm(bundle 形式)

  1. package.jsondsh.bundle.patch 指向 ./cordis.patch.yml(已是)
  2. cordis.patch.yml 中插件 name 应为包名(如 dsh-ai-monitor),不要用绝对路径
  3. 发布:npm publish(确保 files 包含 index.jscordis.patch.ymlREADME.md
  4. GitHub 仓库加 dsh-plugin topic,便于社区发现
  5. 用户安装:dsh plugin add dsh-ai-monitor

后续方向

  • 方案 C(已完成 2026-08-14):dsh 模型请求走 AI Monitor 代理自动记账

    • 配置:~/.dsh/settings.yamlllm-deepseek.baseURL: http://127.0.0.1:8899/v1
    • 原理:dsh 的 deepseek 适配器原生支持 baseURL 覆盖(config → $DEEPSEEK_BASE_URL → 官方默认), API key 不变,协议不变;每次请求动态重读配置,无需重启
    • 验证:ping 请求后 usage_events ID +5、今日 tokens +769/+1187、花费 +¥0.004,代理路由真实生效
    • 注意:8899/v1 默认路由 → DeepSeek;其他平台用对应前缀(moonshot/siliconflow/openrouter/openai)
  • 余额告警工具:agent 检测到余额低于阈值时主动提醒用户充值

  • 成本感知路由:查询结果参与模型选择(余额紧张切便宜模型)

  • 导出工具:把 AI Monitor 月度账单拉到对话里

  • Windows 支持:通过 DSH_AI_MONITOR_DB 环境变量指定数据库路径(已支持,待实测)

数据边界

  • 只统计本机经过 AI Monitor 代理(127.0.0.1:8899)的调用;其他机器/直连不计入
  • 插件只读,不写 AI Monitor 的任何数据