@goodandready/dsh-cost-meter 0.8.12 → 0.8.14

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/lib/index.js CHANGED
@@ -19,6 +19,18 @@ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
19
19
  import { homedir } from 'node:os'
20
20
  import { dirname, join } from 'node:path'
21
21
  import z from '@deepseek-ai/schemastery'
22
+
23
+ // Ensure .volatile() helper exists on Schemastery schema prototype for DSH 0.2
24
+ try {
25
+ const schemaProto = Object.getPrototypeOf(z.boolean())
26
+ if (schemaProto && typeof schemaProto.volatile !== 'function') {
27
+ schemaProto.volatile = function () {
28
+ return typeof this.extra === 'function' ? this.extra('volatile', true) : this
29
+ }
30
+ }
31
+ } catch {
32
+ // safe fallback
33
+ }
22
34
  import { z as zod } from 'zod'
23
35
 
24
36
  export * from './tariff.js'
@@ -97,41 +109,49 @@ const PriceRow = z.object({
97
109
  })
98
110
 
99
111
  export const Config = z.object({
100
- currency: z.string().description('Currency symbol shown in the widget.').default('$'),
101
- usdRate: z.number().description('Units of `currency` per USD. 1 keeps everything in USD.').default(1),
112
+ currency: z.string().description('Currency symbol shown in the widget.').default('$').volatile(),
113
+ usdRate: z.number().description('Units of `currency` per USD. 1 keeps everything in USD.').default(1).volatile(),
102
114
  displayTimeZone: z
103
115
  .string()
104
116
  .description('IANA zone the widget shows tariff hours in.')
105
- .default('Europe/Moscow'),
117
+ .default('Europe/Moscow')
118
+ .volatile(),
106
119
  budgetThreshold: z
107
120
  .number()
108
121
  .description('Optional session budget threshold in USD. 0 disables threshold warnings.')
109
- .default(0),
122
+ .default(0)
123
+ .volatile(),
110
124
  deepseekPeakPrices: z
111
125
  .dict(PriceRow)
112
126
  .description('Override the built-in DeepSeek peak rates, by model id.')
113
- .default({}),
127
+ .default({})
128
+ .volatile(),
114
129
  useOpenRouter: z
115
130
  .boolean()
116
131
  .description('Pull rates and discount windows from the public OpenRouter catalog.')
117
- .default(true),
118
- refreshHours: z.number().description('How often to refetch the catalog.').default(24),
132
+ .default(true)
133
+ .volatile(),
134
+ refreshHours: z.number().description('How often to refetch the catalog.').default(24).volatile(),
119
135
  modelMap: z
120
136
  .dict(z.string())
121
137
  .description('Route override: "provider/model" -> OpenRouter model id.')
122
- .default({}),
138
+ .default({})
139
+ .volatile(),
123
140
  prices: z
124
141
  .dict(PriceRow)
125
142
  .description('Manual rates by "provider/model" (or "*"). Win over the catalog.')
126
- .default({}),
143
+ .default({})
144
+ .volatile(),
127
145
  manualPeakWindowsUtc: z
128
146
  .array(z.string())
129
147
  .description('Peak windows HH:MM-HH:MM UTC, used only with manual prices.')
130
- .default([]),
148
+ .default([])
149
+ .volatile(),
131
150
  manualOffPeakMultiplier: z
132
151
  .number()
133
152
  .description('Multiplier outside the manual peak windows.')
134
- .default(1),
153
+ .default(1)
154
+ .volatile(),
135
155
  })
136
156
 
137
157
  // ---------------------------------------------------------------------------
@@ -360,32 +380,33 @@ export function apply(ctx, config) {
360
380
  }
361
381
  }
362
382
 
363
- // Live config reaction from GUI / settings service
364
- ctx.on('settings/updated', (ns, next) => {
365
- if (ns === NS && next) {
366
- current = { ...config, ...next }
367
- clearResolveCache()
368
- }
369
- })
370
-
371
- ctx.on('settings/document-updated', (ns) => {
372
- if (!ns || ns === NS) {
373
- clearResolveCache()
374
- }
375
- })
376
-
383
+ // Live config reaction from GUI / settings service (DSH 0.2+)
377
384
  ctx.inject(['settings'], (sctx) => {
378
- if (typeof sctx.settings?.describe === 'function') {
385
+ const svc = sctx.settings
386
+ if (typeof svc?.describe !== 'function') return
387
+ const readRow = () => {
379
388
  try {
380
- const desc = sctx.settings.describe(NS)
381
- if (desc?.value && typeof desc.value === 'object') {
382
- current = { ...config, ...desc.value }
383
- clearResolveCache()
384
- }
389
+ const rows = svc.describe({ redactSecrets: true })
390
+ if (Array.isArray(rows)) return rows.find((r) => r?.ns === NS)
391
+ if (rows && typeof rows === 'object' && rows.ns === NS) return rows
392
+ return undefined
385
393
  } catch {
386
- /* namespace might not have descriptor yet */
394
+ return undefined
387
395
  }
388
396
  }
397
+ const row = readRow()
398
+ if (row?.value && typeof row.value === 'object') {
399
+ current = { ...config, ...row.value }
400
+ clearResolveCache()
401
+ }
402
+ ctx.on('settings/document-updated', (ns) => {
403
+ if (ns && ns !== NS) return
404
+ const fresh = readRow()
405
+ if (fresh?.value && typeof fresh.value === 'object') {
406
+ current = { ...config, ...fresh.value }
407
+ }
408
+ clearResolveCache()
409
+ })
389
410
  })
390
411
 
391
412
  // Hook the session projection so the widget can show spend breakdown.
package/lib/tariff.js CHANGED
@@ -30,9 +30,24 @@ export const VENDOR_ALIASES = {
30
30
  'meta': 'meta-llama',
31
31
  }
32
32
 
33
+ /** Recursively unwrap cosmokit / Schemastery Volatile<T> reference objects into plain values. */
34
+ export function unwrapVolatile(value) {
35
+ if (value && typeof value.get === 'function') {
36
+ return unwrapVolatile(value.get())
37
+ }
38
+ if (!value || typeof value !== 'object') return value
39
+ if (Array.isArray(value)) return value.map(unwrapVolatile)
40
+ const out = {}
41
+ for (const [k, v] of Object.entries(value)) {
42
+ out[k] = unwrapVolatile(v)
43
+ }
44
+ return out
45
+ }
46
+
33
47
  /** OpenRouter prices are USD per token; the widget speaks per 1M tokens. */
34
48
  export function perMillion(value) {
35
- const n = Number(value)
49
+ const unwrapped = value && typeof value.get === 'function' ? value.get() : value
50
+ const n = Number(unwrapped)
36
51
  return Number.isFinite(n) ? n * 1e6 : 0
37
52
  }
38
53
 
@@ -61,8 +76,9 @@ export function parseWindow(spec) {
61
76
  }
62
77
 
63
78
  export function overrideBucket(overrideVal, baseVal) {
64
- if (overrideVal !== undefined && overrideVal !== null && overrideVal !== '') {
65
- const n = Number(overrideVal)
79
+ const val = overrideVal && typeof overrideVal.get === 'function' ? overrideVal.get() : overrideVal
80
+ if (val !== undefined && val !== null && val !== '') {
81
+ const n = Number(val)
66
82
  if (Number.isFinite(n)) return n * 1e6
67
83
  }
68
84
  return baseVal
@@ -165,8 +181,9 @@ export function indexCatalog(models) {
165
181
  * Resolve one route's tariff: manual config wins, then DeepSeek's own table,
166
182
  * then the OpenRouter catalog.
167
183
  */
168
- export function resolveTariff(cfg, index, route) {
184
+ export function resolveTariff(rawCfg, index, route) {
169
185
  if (!route) return { schedule: null, source: 'none', matchedId: null }
186
+ const cfg = unwrapVolatile(rawCfg) || {}
170
187
  const key = route.provider ? route.provider + '/' + route.model : route.model
171
188
  const prices = cfg.prices || {}
172
189
  const manual = prices[key] || prices[route.model] || prices['*']
@@ -180,7 +197,8 @@ export function resolveTariff(cfg, index, route) {
180
197
  }
181
198
  const factor = Number(cfg.manualOffPeakMultiplier) || 1
182
199
  const windows = []
183
- for (const spec of cfg.manualPeakWindowsUtc || []) {
200
+ const peakWindows = Array.isArray(cfg.manualPeakWindowsUtc) ? cfg.manualPeakWindowsUtc : []
201
+ for (const spec of peakWindows) {
184
202
  const parsed = parseWindow(spec)
185
203
  if (parsed) windows.push({ start: parsed.start, end: parsed.end, rates: base })
186
204
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goodandready/dsh-cost-meter",
3
- "version": "0.8.12",
3
+ "version": "0.8.14",
4
4
  "description": "DeepSeek Harness plugin: a session cost chip in the conversation header - live token usage by bucket, peak/off-peak tariff, and a countdown to the next tariff switch.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",