deepseek-harness-cost 0.0.0-stage → 1.4.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jooyan Bagheri
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,267 @@
1
- # Temporary Holding Version
1
+ # deepseek-harness-cost
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ ![npm version](https://img.shields.io/npm/v/deepseek-harness-cost)
4
+ ![license](https://img.shields.io/npm/l/deepseek-harness-cost)
5
+
6
+ A **chat cost meter for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness)**.
7
+ One pill under the composer answers three questions in the currency the chat
8
+ actually spends:
9
+
10
+ 1. **What has this chat cost?** — `¥0.551` on the DeepSeek platform, `$0.0762`
11
+ on OpenCode Go.
12
+ 2. **What is it costing per 1K tokens?** — the realized blended rate, e.g.
13
+ `¥0.0000214/1K` or `$0.00000967/1K`.
14
+ 3. **How much of the subscription allowance is left?** — OpenCode Go only, with
15
+ the 5-hour, weekly and monthly windows and their reset countdowns.
16
+
17
+ A chat that used both providers shows both currencies side by side instead of
18
+ converting one into the other. A chat on a provider with no price card shows no
19
+ money and reports the excluded requests in the panel.
20
+
21
+ ```
22
+ ¥0.551 · ¥0.0000214/1K $0.0762 · $0.00000967/1K · 5h 12% · 7d 9% · 30d 8%
23
+ ```
24
+
25
+ Click the pill for the panel: requests, uncached/cached/cache-write input and
26
+ output tokens, the cache-hit rate, the peak/off-peak split, a per-model
27
+ breakdown, and the allowance detail.
28
+
29
+ ## Why this one
30
+
31
+ - **Provider-reported, not estimated.** The numbers are the usage the provider
32
+ already returned and the Session log already stored — not a local token guess.
33
+ - **Two currencies, never mixed.** CNY and USD are separate sums. There is no
34
+ exchange-rate guess anywhere.
35
+ - **Subscription-aware.** OpenCode Go is metered as a subscription: the pill
36
+ shows the 5-hour, weekly and monthly allowance windows, not just a dollar
37
+ figure.
38
+ - **Whole conversation, not one log.** Subagent and workflow children are
39
+ separate Sessions; the meter walks the Session tree and sums every descendant.
40
+ - **Zero token overhead.** It reads the Session log and two account/price
41
+ endpoints. It never adds a prompt section, a tool, or a model request, so it
42
+ cannot inflate the number it displays.
43
+ - **No dependencies, no build step.** Plain ESM; the browser half borrows React
44
+ from the Host module table.
45
+
46
+ ## Install
47
+
48
+ DeepSeek Harness Desktop or Web, with the plugin manager available.
49
+
50
+ **From the GUI:** open **Plugins**, choose **Add plugin**, and enter:
51
+
52
+ ```
53
+ deepseek-harness-cost
54
+ ```
55
+
56
+ **From a terminal** (CLI installations):
57
+
58
+ ```
59
+ dsh plugin --profile <profile> add deepseek-harness-cost
60
+ ```
61
+
62
+ Pin an exact version when you want reproducible installs:
63
+
64
+ ```
65
+ dsh plugin --profile <profile> add deepseek-harness-cost@1.4.0
66
+ ```
67
+
68
+ The bundle is selected when it installs and the pill appears under the composer
69
+ immediately (the browser half also hot-reloads into an open page).
70
+
71
+ ## Update
72
+
73
+ When a newer version is published, the **Plugins** page shows it next to the
74
+ plugin and offers **Update** — one click replaces the installed version in
75
+ place. Nothing to run in a terminal.
76
+
77
+ Updates are only offered for plugins installed from a registry. If you installed
78
+ a pinned `git+…` spec instead, reinstall it with the new tag or commit.
79
+
80
+ ## Requirements
81
+
82
+ - A DeepSeek Harness profile with the plugin manager (the Desktop or Web
83
+ application profile).
84
+ - For the **allowance** half only: an `OPENCODE_GO_API_KEY` credential in the
85
+ Harness credential store — the same key the OpenCode provider uses.
86
+ - The **cost** half needs nothing: it prices the Session log against published
87
+ list prices.
88
+
89
+ Without the OpenCode key the plugin still shows CNY cost and rate; the allowance
90
+ rows simply report that the key is missing.
91
+
92
+ ## How the numbers are made
93
+
94
+ **Usage.** Every settled model attempt in the Session log carries
95
+ `inputTokens`, `cacheReadTokens`, `cacheWriteTokens` and `outputTokens`. The Host
96
+ half folds those with the same replacement rule the Harness itself uses:
97
+
98
+ - `assistant/message` carries the settled attempt's usage; `assistant/attempt`
99
+ may carry a streaming sample; a later sample for the same `(turn, step)`
100
+ **replaces** the earlier one instead of adding to it.
101
+ - `llm/retry-started` closes the replacement slot, so a retried attempt is
102
+ billed again (it was a second provider request).
103
+ - `request/context` names the provider route a request went to; the assistant
104
+ message's own `source.model` / `source.provider` win when present.
105
+
106
+ **Currency.** The provider route decides the card:
107
+
108
+ | Route | Card | Pill |
109
+ |---|---|---|
110
+ | `deepseek-official`, `deepseek-account` | DeepSeek CNY list prices | `¥total · ¥rate/1K` |
111
+ | `opencode-go` | OpenCode Go USD list prices (live from its models API) | `$total · $rate/1K` + allowance |
112
+ | anything else | none | no money; the panel reports excluded requests |
113
+
114
+ ### Price cards
115
+
116
+ | Card | Cache-miss input | Cache read | Output | Peak |
117
+ |---|---|---|---|---|
118
+ | DeepSeek CNY (per 1M) | ¥1 (`deepseek-flash`) | ¥0.02 | ¥4 | Mon–Fri 09:00–12:00, 14:00–18:00 Beijing, ×2 |
119
+ | OpenCode Go USD (per 1M) | $0.15 (`deepseek-v4.1-flash`) | $0.003 | $0.60 | Mon–Fri 01:00–04:00, 06:00–10:00 UTC, ×2 |
120
+
121
+ The CNY card is static and configurable. The USD card is **fetched from
122
+ `GET https://opencode.ai/zen/go/v1/models`** at startup and refreshed hourly, so
123
+ a price change does not need a plugin release; the built-in table is only the
124
+ offline fallback. Peak windows, peak factor and cache prices all come from that
125
+ reply.
126
+
127
+ ### The per-1K figure
128
+
129
+ `¥0.0000214/1K` is one number: everything the Session paid in that currency,
130
+ divided by every token the provider reported in that currency.
131
+
132
+ ```
133
+ per 1K = total cost ÷ (cache-miss + cache-read + cache-write + output) × 1000
134
+ ```
135
+
136
+ The numerator already contains every rate, so the result is the realized average
137
+ price per token, not a tariff lookup. The panel also shows **per 1M tokens**,
138
+ directly comparable with the cards above, and **cache saved**, which explains why
139
+ a cache-heavy chat runs far below list price.
140
+
141
+ ### The allowance windows
142
+
143
+ OpenCode Go is a subscription: a 5-hour rolling window (20% of the monthly
144
+ limit), a weekly window (50%) and the monthly limit (100%). The Host reads
145
+ `GET https://opencode.ai/zen/go/v1/usage` with the same `OPENCODE_GO_API_KEY` the
146
+ provider uses, caches the reply for 30 seconds, and exposes it on the plugin's
147
+ own Host route. The pill shows the **used** percentage of each window; the panel
148
+ adds the reset countdown.
149
+
150
+ The percentages are authoritative and come straight from OpenCode. The panel
151
+ names the plan and the model's published monthly limit, but it does not turn a
152
+ rounded percentage into a dollar amount that would pretend to more precision than
153
+ the endpoint reports.
154
+
155
+ ## Configure
156
+
157
+ Every option has a working default. To override a rate, a route or the plan, put
158
+ a `config` block on the plugin row in the profile's `cordis.patch.yml` (the same
159
+ row the bundle inserts):
160
+
161
+ ```yaml
162
+ - id: chat-cost
163
+ name: deepseek-harness-cost
164
+ config:
165
+ routes:
166
+ deepseek-official: CNY
167
+ deepseek-account: CNY
168
+ opencode-go: USD
169
+ models:
170
+ deepseek-flash: { cacheHit: 0.02, cacheMiss: 1, cacheWrite: 0, output: 4 }
171
+ deepseek-v4-pro: { cacheHit: 0.15, cacheMiss: 4.5, cacheWrite: 0, output: 13.5 }
172
+ defaultModel: deepseek-flash
173
+ offPeakRatio: 0.5
174
+ peakMode: auto # auto | peak | off-peak
175
+ peak:
176
+ timezoneOffsetMinutes: 480
177
+ days: [1, 2, 3, 4, 5]
178
+ windows: [[9, 12], [14, 18]]
179
+ holidays: []
180
+ usd:
181
+ apiKeyRef: OPENCODE_GO_API_KEY
182
+ baseUrl: https://opencode.ai
183
+ modelsPath: /zen/go/v1/models
184
+ usagePaths: [/zen/go/v1/usage, /inference/go/v1/usage]
185
+ plan: go # go | go-plus | go-teams
186
+ quotaTtlSeconds: 30
187
+ modelsTtlSeconds: 3600
188
+ requestTimeoutMs: 10000
189
+ monthlyLimitUsd: null
190
+ ```
191
+
192
+ | Field | Default | Meaning |
193
+ |---|---|---|
194
+ | `routes` | the three rows above | provider id → `CNY` / `USD`; a route absent here is not priced and is reported as excluded (set a route to `null` to drop a default). |
195
+ | `models` | the two CNY tables | CNY per 1M tokens at peak, per model id. A model without a row is priced with `defaultModel` and marked `≈`. |
196
+ | `defaultModel` | `deepseek-flash` | CNY rates for an unpriced DeepSeek model id. |
197
+ | `offPeakRatio` | `0.5` | CNY off-peak multiplier. |
198
+ | `peakMode` | `auto` | `auto` computes the CNY peak from the clock; `peak` / `off-peak` force one rate. |
199
+ | `peak.*` | Beijing, Mon–Fri | the CNY peak clock and statutory holidays. |
200
+ | `usd.apiKeyRef` | `OPENCODE_GO_API_KEY` | credential reference the allowance and models calls use. |
201
+ | `usd.baseUrl` | `https://opencode.ai` | allowance API origin. |
202
+ | `usd.modelsPath` / `usd.usagePaths` | the published endpoints | origin-relative paths; anything that is not a plain path (`//host`, `@host`) is ignored. |
203
+ | `usd.plan` | `go` | names the plan and its per-model monthly limit in the allowance panel; the percentages do not depend on it. |
204
+ | `usd.monthlyLimitUsd` | `null` | overrides the per-model monthly limit. |
205
+ | `usd.quotaTtlSeconds` / `usd.modelsTtlSeconds` | `30` / `3600` | how long the allowance and price replies are cached. |
206
+
207
+ ## Privacy and network access
208
+
209
+ - Session logs never leave the machine. The meter runs inside the Host process.
210
+ - The only network calls are two `GET`s to the configured OpenCode origin: the
211
+ price sheet (`/zen/go/v1/models`) and the account allowance
212
+ (`/zen/go/v1/usage`). Both carry the `OPENCODE_GO_API_KEY` credential, over
213
+ HTTPS, and nothing else.
214
+ - The credential is resolved on the Host per call and is never stored in the
215
+ projection, logged, put in a URL, or sent to the browser.
216
+ - The browser half only talks to the plugin's own Host route, which sits behind
217
+ the Harness Connection fence: Host/Origin checks plus browser authentication.
218
+ - No telemetry, no analytics, no remote config.
219
+
220
+ ## Compatibility
221
+
222
+ Built and verified against DeepSeek Harness `0.2.2-rc.5`. It uses the public
223
+ Host services (`credentials`, `sessionProjections`, `webServer`, `connection`)
224
+ and the client slot API; it does not patch or monkey-patch the Harness.
225
+
226
+ ## Development
227
+
228
+ The package is plain ESM with no dependencies: no build step, no `node_modules`.
229
+
230
+ ```
231
+ node --check index.js
232
+ node --check client.js
233
+ node test/fold.test.mjs
234
+ node test/real-log.mjs "<session store>/<workspace>/<session>/session.v4.jsonl.zstd"
235
+ node test/audit.mjs --all
236
+ ```
237
+
238
+ `fold.test.mjs` covers the CNY and USD price arithmetic, both peak clocks, the
239
+ replace-don't-double-count rule for streamed usage, route grouping across
240
+ providers, the per-1K identity, the allowance normalization and plan-limit
241
+ mapping, the models-endpoint parser, the client formatters, and the subagent
242
+ combination.
243
+
244
+ `real-log.mjs` folds a real Session log twice — once through the plugin's unit,
245
+ once through an independent reduction — and fails if any bucket or currency
246
+ total disagrees.
247
+
248
+ `audit.mjs` shares no code with the plugin (its own zstd reader, fold, route
249
+ resolution, peak clocks and transcribed price tables), re-prices every Session
250
+ log on the machine, and fails on any difference.
251
+
252
+ Files: `index.js` (Host unit: fold, pricing, allowance route), `client.js`
253
+ (browser pills and panel), `cordis.patch.yml` (the loader row), `locale/*.json`
254
+ (plugin-manager copy).
255
+
256
+ ## Troubleshooting
257
+
258
+ - **No pill under the chat** — the bundle is installed but not enabled: check
259
+ **Plugins** in the sidebar.
260
+ - **Allowance shows “key missing”** — add the `OPENCODE_GO_API_KEY` credential;
261
+ the CNY half keeps working without it.
262
+ - **A provider shows no money** — its route has no price card. Add it under
263
+ `routes` plus a `models` row, or read it as excluded in the panel.
264
+
265
+ ## License
266
+
267
+ MIT © Jooyan Bagheri. Not affiliated with DeepSeek or OpenCode.