dsh-cost-meter 1.9.39 → 1.9.41

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/store.js CHANGED
@@ -12,7 +12,7 @@ import { randomUUID } from 'node:crypto'
12
12
  import { matchesRequestFeeModel, validRequestFee } from './web-request-pricing.js'
13
13
  import { repairDailySessionTotals } from './ledger-totals.js'
14
14
  import { startLedgerWrite } from './ledger-writer.js'
15
- import { withLedgerLock, readLedger, writeLedger, applyAccount, mergeLedger, mergeRuntimeConfig, cleanUsageCursors, cleanWriteReceipts, cloneLedgerSnapshot } from './ledger-persistence.js'
15
+ import { withLedgerLock, readLedger, writeLedger, readLedgerConfigBackup, writeLedgerConfigBackup, applyAccount, mergeLedger, mergeRuntimeConfig, cleanUsageCursors, cleanWriteReceipts, cloneLedgerSnapshot } from './ledger-persistence.js'
16
16
  import { normalizePricingHistory, advancePricingHistory, bindPricingHistory, pricingConfigAt } from './billing-history.js'
17
17
  import { join } from 'node:path'
18
18
  import { resolveDshHome } from '@deepseek-ai/dsh-home-paths'
@@ -2251,6 +2251,7 @@ export class Ledger {
2251
2251
  let writeReceipts = []
2252
2252
  let pricingHistory
2253
2253
  let legacyPricingHistory = false
2254
+ let loaded = false, restoredConfig = false
2254
2255
  try {
2255
2256
  const parsed = JSON.parse(readFileSync(path, 'utf8'))
2256
2257
  if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
@@ -2304,6 +2305,7 @@ export class Ledger {
2304
2305
  } else if (Array.isArray(parsed.planRecentCalls)) {
2305
2306
  planHourBuckets = convertRecentCallsToBuckets(parsed.planRecentCalls)
2306
2307
  }
2308
+ loaded = true
2307
2309
  }
2308
2310
  }
2309
2311
  } catch (error) {
@@ -2313,11 +2315,34 @@ export class Ledger {
2313
2315
  console.warn(`[dsh-cost-meter] 账本读取失败,按空账本启动: ${String(error?.message ?? error)}`)
2314
2316
  }
2315
2317
  }
2318
+ if (!loaded) {
2319
+ try {
2320
+ const backup = readLedgerConfigBackup(path)
2321
+ if (backup) {
2322
+ const cfg = backup.config
2323
+ migrations = backup.migrations
2324
+ for (const migration of CONFIG_MIGRATIONS) {
2325
+ if (migrations.includes(migration.id)) continue
2326
+ migration.apply(cfg)
2327
+ migrations.push(migration.id)
2328
+ }
2329
+ config = sanitizeConfig(cfg)
2330
+ restoredConfig = true
2331
+ console.warn(`[dsh-cost-meter] 已从独立备份恢复配置,自定义余额源 ${config.customBalances.length} 条`)
2332
+ }
2333
+ } catch (error) {
2334
+ console.warn(`[dsh-cost-meter] 配置备份不可用,使用默认配置(${error?.code ?? 'invalid format'})`)
2335
+ }
2336
+ } else {
2337
+ // Protect existing installations immediately, even before their next usage write.
2338
+ try { writeLedgerConfigBackup(path, { config: stripSecrets(config), migrations }) }
2339
+ catch (error) { console.warn(`[dsh-cost-meter] 配置备份写入失败(${error?.code ?? 'write error'})`) }
2340
+ }
2316
2341
  const ledger = new Ledger(config, days, path, migrations, { planSamples, planHourBuckets, openrouterPriceHashes, providerPriceHashes, balanceRef, historyReset, usageCursors, writeReceipts, pricingHistory })
2317
2342
  ledger.legacyPricingHistory = legacyPricingHistory
2318
2343
  ledger.quotaStatsNeedRepair = !migrations.includes('quota-token-window-v2')
2319
2344
  if (ledger.quotaStatsNeedRepair) ledger.planSamples = {}
2320
- if (legacyPricingHistory) ledger.scheduleWrite()
2345
+ if (legacyPricingHistory || restoredConfig) ledger.scheduleWrite()
2321
2346
  const repaired = repairDailySessionTotals(ledger.days)
2322
2347
  if (repaired.length) {
2323
2348
  if (repaired.includes(ledger.balanceRef?.date)) ledger.balanceRef = null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cost-meter",
3
- "version": "1.9.39",
3
+ "version": "1.9.41",
4
4
  "description": "DeepSeek Harness 会话费用统计插件:本会话成本、当日费用、历史记录与官方价格同步,支持多厂商多模型价格计费(内置 170+ 模型 ID 价格目录与自动匹配)、主流 Coding Plan 订阅额度查询与显示(14 家,含 WorkBuddy 官方积分、OpenDesign 工作区查询、含小米 MiMo Token Plan 控制台 Cookie 查询、SCNet Token Plan 本地 Credits 计量与火山方舟 Volcano Ark Coding Plan AK/SK 签名查询)、自定义 Provider 余额查询(可配任意 HTTP 端点)与余额进度条、峰谷计价时段显示与切换前弹窗/系统通知提醒(位置/提前量/类型可配),界面中英双语。Session cost tracking plugin for DeepSeek Harness: per-conversation cost, daily totals, history and official price sync, with multi-vendor model pricing (built-in 170+ model-ID catalog with auto-matching), Coding Plan quota queries & display for 14 vendors (incl. WorkBuddy official credits, OpenDesign workspace queries, Xiaomi MiMo Token Plan via console cookie, local credits metering for the SCNet Token Plan and Volcano Ark Coding Plan via AK/SK signature), plan/API dual-track billing with per-1% & full-window Token Plan usage estimates, custom provider balance lookup (configurable HTTP endpoint) and balance progress bar, plus peak/off-peak pricing display with pre-switch popup & system-notification alerts (position / lead time / type configurable), in a bilingual (Chinese/English) UI.",
5
5
  "type": "module",
6
6
  "main": "lib/bootstrap.js",
@@ -184,7 +184,9 @@
184
184
  "docs/update-notes-1.9.36.md",
185
185
  "docs/update-notes-1.9.37.md",
186
186
  "docs/update-notes-1.9.38.md",
187
- "docs/update-notes-1.9.39.md"
187
+ "docs/update-notes-1.9.39.md",
188
+ "docs/update-notes-1.9.40.md",
189
+ "docs/update-notes-1.9.41.md"
188
190
  ],
189
191
  "dependencies": {
190
192
  "acorn": "8.15.0",