@natgurlain/dsh-token-usage 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +204 -2
- package/cordis.patch.yml +16 -0
- package/icon.svg +17 -0
- package/lib/cli.js +102 -0
- package/lib/client.js +502 -0
- package/lib/index.js +318 -0
- package/lib/openrouter-Ygu9q4uX.js +855 -0
- package/lib/types/cli.d.ts +17 -0
- package/lib/types/index.d.ts +39 -0
- package/lib/types/ledger.d.ts +28 -0
- package/lib/types/openrouter.d.ts +72 -0
- package/lib/types/query.d.ts +41 -0
- package/lib/types/report.d.ts +82 -0
- package/lib/types/scan.d.ts +77 -0
- package/lib/types/snapshot.d.ts +77 -0
- package/package.json +86 -3
- package/src/cli.ts +94 -0
- package/src/client.js +502 -0
- package/src/index.ts +195 -0
- package/src/ledger.ts +98 -0
- package/src/openrouter.ts +165 -0
- package/src/query.ts +97 -0
- package/src/report.ts +244 -0
- package/src/scan.ts +289 -0
- package/src/snapshot.ts +151 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nat Gurlain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,205 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<img src="icon.svg" width="96" alt="">
|
|
4
|
+
|
|
5
|
+
# dsh-token-usage
|
|
6
|
+
|
|
7
|
+
**Where your DeepSeek Harness tokens go, by day, model, project and session.**<br>
|
|
8
|
+
Counts every model call in every session straight from DSH's own logs: conversations, subagents, retries and compaction.
|
|
9
|
+
|
|
10
|
+
[](https://www.npmjs.com/package/@natgurlain/dsh-token-usage)
|
|
11
|
+
[](https://github.com/natgurlain/dsh-token-usage/actions/workflows/ci.yml)
|
|
12
|
+
[](LICENSE)
|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
[Quick start](#quick-start) · [Usage page](#usage-page) · [Commands](#commands) · [What is counted](#what-is-counted) · [Costs](#costs) · [Configuration](#configuration)
|
|
16
|
+
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
<p align="center">
|
|
22
|
+
<picture>
|
|
23
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/natgurlain/dsh-token-usage/main/docs/usage-dark.png">
|
|
24
|
+
<img src="https://raw.githubusercontent.com/natgurlain/dsh-token-usage/main/docs/usage-light.png" width="720" alt="The Usage page in DSH settings: totals, a stacked bar chart of tokens per day, and a per-model table with OpenRouter-equivalent costs">
|
|
25
|
+
</picture>
|
|
26
|
+
<br><sub>Settings → Usage (sample data)</sub>
|
|
27
|
+
</p>
|
|
28
|
+
|
|
29
|
+
```text
|
|
30
|
+
Today 4.21M · 7 days 18.6M · 30 days 61.3M · all time 142M tokens
|
|
31
|
+
|
|
32
|
+
Token usage, the last 14 days: 29.4M tokens in 812 model calls across 23 sessions.
|
|
33
|
+
Input 1.92M · cache read 26.8M · cache write 0 · output 684k (reasoning 211k)
|
|
34
|
+
|
|
35
|
+
Day Total Input Cache rd Output Calls
|
|
36
|
+
2026-12-04 4.21M 288k 3.83M 94.1k 97 ██████
|
|
37
|
+
2026-12-03 10.9M 702k 9.94M 251k 264 ████████████████
|
|
38
|
+
2026-12-01 3.48M 231k 3.17M 78.0k 88 █████
|
|
39
|
+
2026-11-28 6.02M 395k 5.48M 143k 201 █████████
|
|
40
|
+
2026-11-25 4.79M 304k 4.37M 118k 162 ███████
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
<sub>Open <b>Settings → Usage</b> for the chart, type <code>/usage</code> in any conversation, ask the agent "how many tokens did I use this week?", or run <code>npx @natgurlain/dsh-token-usage</code> in a terminal.</sub>
|
|
44
|
+
|
|
45
|
+
## Why
|
|
46
|
+
|
|
47
|
+
- **Every session, not just this one.** DSH shows the context of the conversation in front of you; this adds up all of them, across every workspace.
|
|
48
|
+
- **Exact, not estimated.** The numbers are the usage each provider reported, as DSH logged it. Nothing is re-tokenized.
|
|
49
|
+
- **Nothing counted twice, nothing missed.** Forked and resumed sessions skip the history they copied; failed and retried calls and compaction summaries count, because they were billed.
|
|
50
|
+
- **Fast after the first run.** An index remembers each log's results and re-reads only logs that changed.
|
|
51
|
+
- **A page to look at.** Settings → Usage charts tokens per day, with totals and a breakdown by model, workspace and session.
|
|
52
|
+
- **What your local models would cost.** Models you run yourself are priced at OpenRouter's list price for the same model.
|
|
53
|
+
- **Read-only.** The plugin never writes to your sessions. Its one outbound request is a public, anonymous fetch of OpenRouter's price list, which you can turn off.
|
|
54
|
+
|
|
55
|
+
## Quick start
|
|
56
|
+
|
|
57
|
+
In the DeepSeek Harness app, open **Plugins → Add plugin**, enter `@natgurlain/dsh-token-usage` and install. For `dsh web`:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
dsh plugin --profile web add @natgurlain/dsh-token-usage
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Restart DSH, then open **Settings → Usage** or type `/usage`.
|
|
64
|
+
|
|
65
|
+
No install needed for the terminal report:
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
npx @natgurlain/dsh-token-usage month by model
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
<details>
|
|
72
|
+
<summary><b>Install from a checkout</b></summary>
|
|
73
|
+
|
|
74
|
+
You need Node.js 24.9+ and pnpm.
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
git clone https://github.com/natgurlain/dsh-token-usage.git && cd dsh-token-usage
|
|
78
|
+
pnpm install && pnpm run build
|
|
79
|
+
dsh plugin --profile web add -w "@natgurlain/dsh-token-usage@link:$PWD"
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
For the desktop app's own profile, run the same `plugin` command with the CLI bundled in the app (`/Applications/DeepSeek Harness.app/Contents/Resources/runtime/cli/bin/dsh` on macOS) and `--profile desktop`.
|
|
83
|
+
|
|
84
|
+
</details>
|
|
85
|
+
|
|
86
|
+
## Usage page
|
|
87
|
+
|
|
88
|
+
**Settings → Usage** in the DSH app shows:
|
|
89
|
+
|
|
90
|
+
- Totals for today, 7, 30 or 90 days, or all time: total tokens, uncached input, cache reads, output and the estimated cost.
|
|
91
|
+
- A stacked bar per day (input, cache read, output). Hover a bar for its numbers. Short periods show the days before them, dimmed.
|
|
92
|
+
- Models, Workspaces and Sessions tabs, each with its share of the period.
|
|
93
|
+
|
|
94
|
+
The page refreshes every minute while it is open. It reads the same index as `/usage`, through a local route of the DSH host (`POST /api/token-usage.report`).
|
|
95
|
+
|
|
96
|
+
## Commands
|
|
97
|
+
|
|
98
|
+
```text
|
|
99
|
+
/usage overview, then the last 14 days by day
|
|
100
|
+
/usage today | yesterday one day
|
|
101
|
+
/usage week | month the last 7 or 30 days, by day
|
|
102
|
+
/usage 90 the last 90 days
|
|
103
|
+
/usage 2026-11 a month; 2026-11-03 a day; 2026-11-01..2026-11-15 a range
|
|
104
|
+
/usage month by model group by day, model, workspace, session or kind
|
|
105
|
+
/usage week sessions here only this conversation's workspace
|
|
106
|
+
/usage all kind nosub leave subagent sessions out
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The agent gets the same report through a read-only `token_usage` tool (`period`, `group_by`, `this_workspace`, `limit`), so "which project burned the most tokens last month?" just works.
|
|
110
|
+
|
|
111
|
+
The terminal command takes the same words, plus `--json` (the report as JSON), `--home <dir>` (another DSH home; default `$DSH_HOME` or `~/.dsh`), `--tz <zone>` and `--openrouter` (estimate costs at OpenRouter list prices).
|
|
112
|
+
|
|
113
|
+
## What is counted
|
|
114
|
+
|
|
115
|
+
DSH logs every model call of a session in `~/.dsh/sessions/<workspace>/<session>/session.v4.jsonl.zstd`, with the usage the provider reported. The plugin reads:
|
|
116
|
+
|
|
117
|
+
| Event | What it is |
|
|
118
|
+
|---|---|
|
|
119
|
+
| `assistant/message` | a completed reply step, with its provider and model |
|
|
120
|
+
| `assistant/attempt` | a failed or retried call that still reported usage (attributed to the session's model at the time) |
|
|
121
|
+
| `compaction/summary` | the call that summarizes a long conversation |
|
|
122
|
+
|
|
123
|
+
**Total** is prompt plus output as the provider reported it: uncached input, cache reads, cache writes and output (reasoning is part of output). Cache reads usually dominate in long agent sessions and are billed far cheaper, which is why they have their own column.
|
|
124
|
+
|
|
125
|
+
Days are your machine's local days (or `timeZone`). Subagent sessions are included and marked. A deleted session's tokens stay counted once the index has seen them.
|
|
126
|
+
|
|
127
|
+
Not counted, because DSH does not log their usage: the small title-generation call at the start of a conversation, and anything outside DSH.
|
|
128
|
+
|
|
129
|
+
## Costs
|
|
130
|
+
|
|
131
|
+
Give prices (USD per million tokens) and every report gets a cost column. Keys are `provider/model` or a bare model name; cache prices default to the input price.
|
|
132
|
+
|
|
133
|
+
```yaml
|
|
134
|
+
- insert:
|
|
135
|
+
- id: token-usage
|
|
136
|
+
name: '@natgurlain/dsh-token-usage'
|
|
137
|
+
config:
|
|
138
|
+
prices:
|
|
139
|
+
deepseek-official/deepseek-flash: { input: 0.14, output: 0.28, cacheRead: 0.014 }
|
|
140
|
+
inkling-2-mini: { input: 0, output: 0 }
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### OpenRouter prices for everything else
|
|
144
|
+
|
|
145
|
+
Models you run on your own GPU have no bill, but it is useful to know what they would cost. For every model without a configured price, the plugin looks up OpenRouter's public model list (`GET https://openrouter.ai/api/v1/models`, no key and nothing about you sent), and uses the list price of the same model:
|
|
146
|
+
|
|
147
|
+
- `inkling-local/qwen4-35b-a3b` is priced as `qwen/qwen4-35b-a3b`.
|
|
148
|
+
- `nearai/deepseek-ai/DeepSeek-V4.1-Flash-1017` is priced as `deepseek/deepseek-v4.1-flash-1017`.
|
|
149
|
+
|
|
150
|
+
A model matches by its exact OpenRouter id, or else by its bare name when exactly one OpenRouter model has it. When the name differs or is ambiguous, map it yourself:
|
|
151
|
+
|
|
152
|
+
```yaml
|
|
153
|
+
openRouter:
|
|
154
|
+
models:
|
|
155
|
+
my-finetune: qwen/qwen4-35b-a3b
|
|
156
|
+
local/twin: b/twin
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
These costs are marked `≈` on the page and noted under reports. They are what the same tokens would cost on OpenRouter, not a bill. Prices you configure always win; set a local model to `{ input: 0, output: 0 }` to count it as free. The list is cached for a day in `~/.dsh/token-usage/openrouter-prices.json`, and the last saved copy is used offline. Set `openRouter.enabled: false` to never fetch it. The terminal command fetches it only with `--openrouter`.
|
|
160
|
+
|
|
161
|
+
Calls with no price at all are left out of the cost and flagged with `*`.
|
|
162
|
+
|
|
163
|
+
## Configuration
|
|
164
|
+
|
|
165
|
+
Set these in `cordis.patch.yml` or in the profile's plugin settings.
|
|
166
|
+
|
|
167
|
+
| Key | Default | |
|
|
168
|
+
|---|---|---|
|
|
169
|
+
| `prices` | `{}` | USD per million tokens by `provider/model` or model: `input`, `output`, optional `cacheRead`, `cacheWrite` |
|
|
170
|
+
| `openRouter.enabled` | `true` | price unpriced models at OpenRouter list prices |
|
|
171
|
+
| `openRouter.refreshHours` | `24` | how long the cached price list is used |
|
|
172
|
+
| `openRouter.models` | `{}` | manual matches, `provider/model` or model to an OpenRouter id |
|
|
173
|
+
| `timeZone` | this machine's | IANA zone for days, like `Europe/Warsaw` |
|
|
174
|
+
| `root` | `~/.dsh/token-usage` | where the index is kept (0600) |
|
|
175
|
+
| `sessionsDir` | `~/.dsh/sessions` | session logs to read |
|
|
176
|
+
|
|
177
|
+
## Not yet
|
|
178
|
+
|
|
179
|
+
- Claude Code and Codex transcripts next to DSH's.
|
|
180
|
+
- Budgets and a warning when a day goes over one.
|
|
181
|
+
|
|
182
|
+
## Development
|
|
183
|
+
|
|
184
|
+
```sh
|
|
185
|
+
pnpm install
|
|
186
|
+
pnpm test # vitest, with synthetic multi-frame zstd session logs
|
|
187
|
+
pnpm run typecheck
|
|
188
|
+
pnpm run build
|
|
189
|
+
node lib/cli.js # the report for your own ~/.dsh
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Releases: bump `package.json`, push a `vX.Y.Z` tag, and the release workflow tests, attests and publishes to npm through trusted publishing.
|
|
193
|
+
|
|
194
|
+
## More DSH plugins
|
|
195
|
+
|
|
196
|
+
| | Plugin | What it does |
|
|
197
|
+
|---|---|---|
|
|
198
|
+
| <img src="https://github.com/natgurlain/dsh-browser-agent/blob/main/packages/browser/bridge-browser/icon.svg?raw=true" width="36" alt=""> | [**dsh-browser-agent**](https://github.com/natgurlain/dsh-browser-agent) | An AI agent for your real browser that asks before it acts |
|
|
199
|
+
| <img src="https://github.com/natgurlain/dsh-dreaming/blob/main/icon.svg?raw=true" width="36" alt=""> | [**dsh-dreaming**](https://github.com/natgurlain/dsh-dreaming) | Long-term memory, consolidated from past sessions while you're away |
|
|
200
|
+
| <img src="https://github.com/natgurlain/dsh-remote/blob/main/icon.svg?raw=true" width="36" alt=""> | [**dsh-remote**](https://github.com/natgurlain/dsh-remote) | Work on a remote machine over SSH; keys and models stay local |
|
|
201
|
+
| <img src="https://github.com/natgurlain/dsh-free-web-search/blob/main/icon.svg?raw=true" width="36" alt=""> | [**dsh-free-web-search**](https://github.com/natgurlain/dsh-free-web-search) | `web_search` without paying for a search API |
|
|
202
|
+
|
|
203
|
+
## License
|
|
204
|
+
|
|
205
|
+
[MIT](LICENSE) © 2026 Nat Gurlain
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Token usage for DeepSeek Harness.
|
|
2
|
+
#
|
|
3
|
+
# `dsh plugin add` registers this package's `dsh.bundle` manifest, so the
|
|
4
|
+
# plugin mounts without an extra command-line option. It only reads DSH's
|
|
5
|
+
# session logs; its incremental index lives under ~/.dsh/token-usage.
|
|
6
|
+
# Type /usage in any conversation, or ask the agent how many tokens you used.
|
|
7
|
+
#
|
|
8
|
+
# Optional prices (USD per million tokens) turn token counts into costs:
|
|
9
|
+
# prices:
|
|
10
|
+
# deepseek/deepseek-chat: { input: 0.27, output: 1.1, cacheRead: 0.07 }
|
|
11
|
+
|
|
12
|
+
- insert:
|
|
13
|
+
- id: token-usage
|
|
14
|
+
name: '@natgurlain/dsh-token-usage'
|
|
15
|
+
config:
|
|
16
|
+
prices: {}
|
package/icon.svg
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Token usage">
|
|
2
|
+
<title>Token usage</title>
|
|
3
|
+
<defs>
|
|
4
|
+
<linearGradient id="bar" x1="0" y1="1" x2="0" y2="0">
|
|
5
|
+
<stop offset="0" stop-color="#0ea5e9"/>
|
|
6
|
+
<stop offset="1" stop-color="#5eead4"/>
|
|
7
|
+
</linearGradient>
|
|
8
|
+
</defs>
|
|
9
|
+
<rect x="2.5" y="2.5" width="59" height="59" rx="14" fill="#0b1220" stroke="#334155" stroke-width="1"/>
|
|
10
|
+
<path d="M13 49h38" stroke="#475569" stroke-width="2" stroke-linecap="round"/>
|
|
11
|
+
<rect x="15" y="34" width="7" height="12" rx="2" fill="url(#bar)" opacity="0.7"/>
|
|
12
|
+
<rect x="25" y="24" width="7" height="22" rx="2" fill="url(#bar)" opacity="0.85"/>
|
|
13
|
+
<rect x="35" y="29" width="7" height="17" rx="2" fill="url(#bar)" opacity="0.75"/>
|
|
14
|
+
<rect x="45" y="15" width="7" height="31" rx="2" fill="url(#bar)"/>
|
|
15
|
+
<circle cx="18.5" cy="21" r="5.5" fill="none" stroke="#e2e8f0" stroke-width="2"/>
|
|
16
|
+
<path d="M16.5 21h4M18.5 19v4" stroke="#e2e8f0" stroke-width="1.6" stroke-linecap="round"/>
|
|
17
|
+
</svg>
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { a as USAGE_HINT, f as parsePeriod, g as UsageLedger, l as buildReport, o as answer, s as parseQuery, t as OpenRouterPrices, u as dayOf } from "./openrouter-Ygu9q4uX.js";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
//#region lib/types/cli.js
|
|
6
|
+
/**
|
|
7
|
+
* `dsh-token-usage`: the `/usage` report in a terminal, without DSH running.
|
|
8
|
+
*
|
|
9
|
+
* ```
|
|
10
|
+
* dsh-token-usage overview and the last 14 days
|
|
11
|
+
* dsh-token-usage month by model the last 30 days, by model
|
|
12
|
+
* dsh-token-usage 2026-11 --json November's records as JSON
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Reads `$DSH_HOME/sessions` (default `~/.dsh/sessions`); `--home <dir>`
|
|
16
|
+
* points elsewhere. The CLI keeps no index; it reads every log each time.
|
|
17
|
+
*
|
|
18
|
+
* @module @natgurlain/dsh-token-usage/cli
|
|
19
|
+
*/
|
|
20
|
+
const HELP = `Usage: dsh-token-usage ${USAGE_HINT} [--json] [--home <dsh home>] [--tz <zone>]
|
|
21
|
+
|
|
22
|
+
Counts the tokens of every DSH session from its logs.
|
|
23
|
+
--json print the report as JSON
|
|
24
|
+
--home <dir> DSH home (default $DSH_HOME or ~/.dsh)
|
|
25
|
+
--tz <zone> time zone for days (default this machine's)
|
|
26
|
+
--openrouter estimate costs at OpenRouter list prices (fetches openrouter.ai/api/v1/models)`;
|
|
27
|
+
async function main(argv) {
|
|
28
|
+
const words = [];
|
|
29
|
+
let json = false;
|
|
30
|
+
let home = process.env.DSH_HOME ?? join(homedir(), ".dsh");
|
|
31
|
+
let timeZone;
|
|
32
|
+
let openRouter = false;
|
|
33
|
+
for (let i = 0; i < argv.length; i++) {
|
|
34
|
+
const arg = argv[i];
|
|
35
|
+
if (arg === "-h" || arg === "--help") {
|
|
36
|
+
console.log(HELP);
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
if (arg === "--json") json = true;
|
|
40
|
+
else if (arg === "--openrouter") openRouter = true;
|
|
41
|
+
else if (arg === "--home" || arg === "--tz") {
|
|
42
|
+
const value = argv[++i];
|
|
43
|
+
if (value === void 0) {
|
|
44
|
+
console.error(`${arg} needs a value`);
|
|
45
|
+
return 2;
|
|
46
|
+
}
|
|
47
|
+
if (arg === "--home") home = resolve(value);
|
|
48
|
+
else timeZone = value;
|
|
49
|
+
} else words.push(arg);
|
|
50
|
+
}
|
|
51
|
+
const query = parseQuery(words);
|
|
52
|
+
if (typeof query === "string") {
|
|
53
|
+
console.error(`${query}\n${HELP}`);
|
|
54
|
+
return 2;
|
|
55
|
+
}
|
|
56
|
+
const records = await new UsageLedger({ sessionsDir: join(home, "sessions") }).records();
|
|
57
|
+
let prices;
|
|
58
|
+
let note;
|
|
59
|
+
if (openRouter) {
|
|
60
|
+
const list = new OpenRouterPrices({ cacheFile: join(home, "token-usage", "openrouter-prices.json") });
|
|
61
|
+
await list.ready();
|
|
62
|
+
const found = list.pricesFor(new Map(records.map((r) => [`${r.provider}/${r.model}`, r])).values());
|
|
63
|
+
prices = found.prices;
|
|
64
|
+
const keys = Object.keys(found.sources);
|
|
65
|
+
note = keys.length === 0 ? "No model matched an OpenRouter price." : `OpenRouter list prices: ${keys.map((key) => `${key} as ${found.sources[key]?.openRouterId ?? "?"}`).join(", ")}.`;
|
|
66
|
+
}
|
|
67
|
+
if (json) {
|
|
68
|
+
const today = dayOf(Date.now(), timeZone);
|
|
69
|
+
const period = parsePeriod(query.period ?? "all", today);
|
|
70
|
+
if (period === void 0) {
|
|
71
|
+
console.error(`Unknown period "${query.period ?? ""}"`);
|
|
72
|
+
return 2;
|
|
73
|
+
}
|
|
74
|
+
const report = buildReport(records, {
|
|
75
|
+
period,
|
|
76
|
+
by: query.by ?? "day",
|
|
77
|
+
...prices === void 0 ? {} : { prices },
|
|
78
|
+
...timeZone === void 0 ? {} : { timeZone },
|
|
79
|
+
...query.noSubagents === true ? { excludeSubagents: true } : {}
|
|
80
|
+
});
|
|
81
|
+
console.log(JSON.stringify(report, null, 2));
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
if (query.here === true) {
|
|
85
|
+
console.error("\"here\" works inside a DSH conversation; in a terminal, use --home or filter with \"by workspace\".");
|
|
86
|
+
return 2;
|
|
87
|
+
}
|
|
88
|
+
console.log(answer(records, query, {
|
|
89
|
+
...timeZone === void 0 ? {} : { timeZone },
|
|
90
|
+
...prices === void 0 ? {} : { prices },
|
|
91
|
+
...note === void 0 ? {} : { priceNote: note }
|
|
92
|
+
}));
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
main(process.argv.slice(2)).then((code) => {
|
|
96
|
+
process.exitCode = code;
|
|
97
|
+
}, (error) => {
|
|
98
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
99
|
+
process.exitCode = 1;
|
|
100
|
+
});
|
|
101
|
+
//#endregion
|
|
102
|
+
export {};
|