<div align="center">

# 📊 dsh-usage-stats

**DeepSeek Harness 的 API 开支监控：逐请求明细、上期对比与历史图表，一个面板全览。**

*让每一分 token 开支都看得见。*

[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)
[![DSH plugin](https://img.shields.io/badge/dsh-plugin-✅-green)](https://github.com/topics/dsh-plugin)
[![Node](https://img.shields.io/badge/node-%5E22.19%20%7C%7C%20%3E%3D24-brightgreen.svg)](#)

[English](README.md) · [简体中文](README.zh.md) · [Español](README.es.md) · [Português](README.pt.md) · [हिन्दी](README.hi.md)

</div>

---

## 兼容性

| 方面 | 状态 |
|---|---|
| Harness | DeepSeek Harness `0.1.0-rc.8` |
| Node | `^22.19.0 \|\| >=24.0.0` |
| 界面 | Host + Web 客户端（设置页「用量统计」页签）；`/usage` 命令 |

## 你能得到什么

`dsh-usage-stats` 把会话事件流变成完整的 API 开支监控面板：

- **汇总卡片** —— 今日 / 本周 / 本月 / 累计四张卡片，每张带较上期（昨日 / 上周 / 上月）的百分比增减角标。
- **当前对话逐请求明细** —— 每条模型请求的请求时刻、轮次/步骤、模型、输入 / 输出 / 缓存读 / 缓存写 tokens、按请求时价格核算的费用与峰时标记；基于会话投影持久化，重启后依然完整（有界环形缓冲，`requestLog` 配置）。
- **日 / 周 / 月视图** —— 本期 vs 上期对比面板（费用、tokens、调用数、峰时调用数 + 增减百分比）与期内逐日明细。
- **历史面板** —— 逐日费用柱状图（峰时柱着色区分，悬停显示明细）+ 逐日表格（费用 / tokens / 调用 / 峰时，逐模型下钻），默认保留 90 天（持久层按会话保留约一年）。
- **自定义区间** —— 任选起止日期查询：区间合计、逐日图表、逐周明细、模型明细。
- **计价** —— 内置 USD 价目表与 `config.prices` 合并；**峰谷计价**按请求开始时间生效（`peak.hours` 窗口 × `multiplier` 或显式 `peak.prices`），日历分桶跟随 `peak.timezone`。
- **碳足迹与延迟** —— token→碳桥接（tokens × kWh/token × PUE × 区域电网强度）与按模型延迟百分位。

## 快速开始

```sh
# 1. 把 bundle 装进你的 profile（tarball 通道）
pnpm pack
dsh plugin --profile web add ./dsh-usage-stats-<version>.tgz

# 2. 重启并核实行
dsh web --restart
dsh --profile web --dump-config | grep -A2 'id: usage'
```

然后在会话里输入 `/usage`，并在设置页查看「用量统计」页签。

## 安装与卸载

- **npm 通道**（已发布版本）：`dsh plugin --profile web add dsh-usage-stats-alhabor` —— npm registry，由 tag 触发发布。
- **git 通道**（最新 `main`）：`dsh plugin --profile web add "github:PerryLink/dsh-budget#main"` —— `prepare` 脚本仅用生产依赖构建。
- **tarball 通道**：在本仓库执行 `pnpm pack`，然后 `dsh plugin --profile web add ./dsh-usage-stats-<version>.tgz`。
- **卸载**：`dsh plugin --profile web remove dsh-usage-stats`。

## 配置

所有字段都是 Schemastery 校验的 cordis 配置（默认值见 `cordis.patch.yml`）。

| 键 | 默认值 | 含义 |
|---|---|---|
| `prices` | `{}` | 每模型每 1M token 单价，合并覆盖内置 USD 价目表 |
| `defaultPrice` | `{input: 1.0, output: 3.0}` | 两表均无该模型时的回退价格 |
| `peak.enabled` / `timezone` / `hours` / `weekendOffpeak` / `multiplier` / `prices` | `true` / `Asia/Shanghai` / `[[9,12],[14,18]]` / `true` / `{input:2, output:2, cacheRead:2, cacheWrite:2}` / `{}` | 峰谷计价：请求开始时间落在高峰窗口内时按 `peak.prices` 计价（缺失时按 `peak.multiplier` 放大基础价）；仅周一至周五算峰时，周末全天按低谷价（DeepSeek 2026-08-23 新规）；日/月分桶也按 `peak.timezone` 划分 |
| `modelAliases` | `{}` | 模型 id 别名：带日期后缀/旧 id 映射到规范定价 id |
| `currency` | `{code: CNY, rate: 1.0, decimals: 2}` | 展示货币（金额 = 核算值 × rate；直接人民币计价时设 `rate: 1.0` 并在 `prices` 填人民币单价） |
| `outputLanguage` | `zh` | `/usage` 输出语言：`en` / `zh` |
| `historyDays` | `90` | 面板快照保留的按天历史天数（1..365；投影持久层每会话保留约一年） |
| `requestLog.enabled` / `size` | `true` / `200` | 当前对话逐请求明细的开关与每会话环形缓冲容量（10..2000） |
| `carbon.enabled` / `region` / `pue` / `energyKwhPerToken` | `true` / `global` / `1.58` / `0.000007` | 碳桥接（区域：global, us, eu, china, india, uk, france, iceland） |
| `latency.enabled` / `windowSize` | `true` / `200` | 按模型延迟百分位与其窗口 |
| `refreshIntervalMs` | `5000` | 设置页签轮询间隔（保留字段） |

## 工具与界面

| 界面 | 类型 | 说明 |
|---|---|---|
| `/usage` | 命令 | 总览（会话/今日/昨日/本周/上周/本月/上月/累计） |
| `/usage models \| days \| sessions \| week <周一> \| range <起> <止>` | 命令 | 模型明细 / 每日历史 / 会话列表 / 某周 / 自定义区间 |
| 设置 → 插件 → 用量统计 | 设置页签 | 汇总卡片、当前对话明细、日/周/月对比、自定义区间、历史图表 |
| `usage/status`、`usage/range` | Typert Remote | 客户端通道（页签消费这两个方法） |

## 权限与数据

- **权限**：`session:append`（仅命令审计）、`native-code:none`；无出站网络。
- **数据**：展示内容全部来自会话事件流的只读聚合；插件不向会话日志写入任何自定义事件（rc.8 读取路径会拒绝未知事件类型），持久化状态全部走会话投影（projection）机制。
- **失败大声**：非法价格、时区、比例、区域与边界在挂载时即失败。

## 已知限制

- 逐请求明细按会话有界保留（默认最近 200 条）；更早的请求只进入聚合计量。
- 汇总卡片的上期对比在持久历史不足一个完整周期时显示「新增/持平」。
- 内置价目会漂移；用 `config.prices` 覆盖条目。

## 开发

```sh
pnpm install        # node ^22.19 || >=24
pnpm run typecheck  # tsc：src + tests，对照本地 harness checkout
pnpm run typecheck:ci  # tsc：对照已发布的 0.1.0-rc.8 类型（无 paths）
pnpm test           # vitest：80 个测试
pnpm run build      # tsc 声明 + tsdown bundles（lib/）
pnpm run verify:self-contained  # 依赖声明全部来自 registry
pnpm run verify:artifacts       # 构建产物 ESM 面 + typert manifest + 客户端 bundle
pnpm pack           # 发布用 tarball
```

## Topics

`dsh`, `dsh-plugin`, `deepseek-harness`, `deepseek`, `cordis`, `budget`, `cost-tracking`, `carbon-footprint`, `latency-benchmark`, `token-usage`

## Contributors

- [@PerryLink](https://github.com/PerryLink) —— 创建者与维护者：聚合、预算治理、碳足迹与延迟移植、设置页签与五语文档。

## PerryLink DSH Plugin Family

本项目是由 [PerryLink](https://github.com/PerryLink) 维护的 [29 个 DeepSeek Harness 插件](https://github.com/PerryLink)之一。如果这个对你有用，其他插件很可能也会：

| Plugin | One-liner |
|---|---|
| [dsh-auto-review](https://github.com/PerryLink/dsh-auto-review) | 审批链上的第二模型自动审查，默认失败关闭 |
| [dsh-background-agents](https://github.com/PerryLink/dsh-background-agents) | 持久化后台子代理，带 Web UI 侧边栏、消息与打断 |
| **[dsh-budget](https://github.com/PerryLink/dsh-budget)** | DeepSeek Harness 的成本治理：预算、碳排与延迟一屏呈现。 |
| [dsh-checkpoint-rewind](https://github.com/PerryLink/dsh-checkpoint-rewind) | Claude Code /rewind 等价物：快照、会话分叉、一次性恢复 |
| [dsh-claude-move](https://github.com/PerryLink/dsh-claude-move) | 将 Claude Code 会话、记忆、技能与 CLAUDE.md 迁入 DSH |
| [dsh-click](https://github.com/PerryLink/dsh-click) | 跨平台原生桌面控制（DeepSeek Harness），Windows 优先。 |
| [dsh-composer-history](https://github.com/PerryLink/dsh-composer-history) | Web 输入框的终端式输入历史：方向键、Ctrl+R 搜索 |
| [dsh-defend](https://github.com/PerryLink/dsh-defend) | DeepSeek Harness 的提示注入、越狱与密钥泄露防护。 |
| [dsh-doublecheck](https://github.com/PerryLink/dsh-doublecheck) | 工程纪律门禁：需求质询、测试门禁、对抗式审查 |
| [dsh-draw](https://github.com/PerryLink/dsh-draw) | DeepSeek Harness 的统一静态图像生成路由。 |
| [dsh-fast](https://github.com/PerryLink/dsh-fast) | DeepSeek Harness 的只读性能诊断。 |
| [dsh-github](https://github.com/PerryLink/dsh-github) | DSH 的 GitHub PR/issue 集成，每次写入都经审批门 |
| [dsh-library](https://github.com/PerryLink/dsh-library) | DeepSeek Harness 的本地文档知识库。 |
| [dsh-local-ai](https://github.com/PerryLink/dsh-local-ai) | DeepSeek Harness 的本地模型（Ollama）接入。 |
| [dsh-lsp-actions](https://github.com/PerryLink/dsh-lsp-actions) | 经语言服务器的 LSP 诊断、格式化、补全、代码操作与重命名 |
| [dsh-mask](https://github.com/PerryLink/dsh-mask) | DeepSeek Harness 的 PII 脱敏中间件——数据到模型前匿名化，展示层还原。 |
| [dsh-mcp-panel](https://github.com/PerryLink/dsh-mcp-panel) | 只读 MCP 运行时面板：/mcp 命令 + 带状态、工具与错误的设置页 |
| [dsh-memento](https://github.com/PerryLink/dsh-memento) | 带审批门的跨会话记忆：ctx.memory 接缝 + SQLite + memory 工具 |
| [dsh-observe](https://github.com/PerryLink/dsh-observe) | DeepSeek Harness 的 OpenTelemetry 与 Langfuse 可观测导出器。 |
| [dsh-output-styles](https://github.com/PerryLink/dsh-output-styles) | Claude Code outputStyles 等价的运行时样式切换 |
| [dsh-permission-rules](https://github.com/PerryLink/dsh-permission-rules) | Claude Code 风格声明式 allow/deny/ask 权限规则，带审计 |
| [dsh-plugin-guide](https://github.com/PerryLink/dsh-plugin-guide) | 按需 agent 技能形式的插件开发知识库 |
| [dsh-score](https://github.com/PerryLink/dsh-score) | DeepSeek Harness 插件的多指标质量评分。 |
| [dsh-session-pin](https://github.com/PerryLink/dsh-session-pin) | 在 Web 侧边栏置顶会话，顺序持久化 |
| [dsh-session-sync](https://github.com/PerryLink/dsh-session-sync) | DeepSeek Harness 的跨设备会话同步——会话存储的专用 git 镜像。 |
| [dsh-skill-pack-security](https://github.com/PerryLink/dsh-skill-pack-security) | 安全审计技能包：密钥扫描、依赖与供应链审查 |
| [dsh-talk](https://github.com/PerryLink/dsh-talk) | DeepSeek Harness 的语音优先会话闭环：对它说，听它答。 |
| [dsh-test-drive](https://github.com/PerryLink/dsh-test-drive) | DeepSeek Harness 插件的隔离式安装冒烟实测。 |
| [dsh-translate](https://github.com/PerryLink/dsh-translate) | DeepSeek Harness 的厂商参数翻译与确定性 JSON 修复。 |

## License

[Apache License 2.0](LICENSE) © 2026 dsh-budget contributors
