@xiangyixie/dsh-superagent 0.12.1 → 0.14.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/lib/ledger.js CHANGED
@@ -15,6 +15,7 @@
15
15
  */
16
16
  import fsp from 'node:fs/promises'
17
17
  import path from 'node:path'
18
+ import { addCurrencyMicros, currencyTotals } from './cost.js'
18
19
  import { TERMINAL_STATUSES } from './store.js'
19
20
 
20
21
  const LEDGER_VERSION = 1
@@ -37,7 +38,7 @@ function fingerprintOf(run) {
37
38
  run.status ?? '',
38
39
  run.finishedAt ?? '',
39
40
  run.usage?.modelCalls ?? 0,
40
- run.cost?.totalMicros ?? '',
41
+ run.cost?.totalMicros ?? JSON.stringify(run.cost?.byCurrency ?? ''),
41
42
  ].join('|')
42
43
  }
43
44
 
@@ -128,9 +129,14 @@ export function createLedger(root, options = {}) {
128
129
  calls: run.usage.modelCalls ?? 0,
129
130
  tokens: run.usage.totalTokens
130
131
  ?? (run.usage.inputTokens ?? 0) + (run.usage.cacheReadTokens ?? 0) + (run.usage.outputTokens ?? 0),
131
- currency: cost.currency ?? 'USD',
132
- totalMicros: Number.isFinite(cost.totalMicros) ? cost.totalMicros : Math.round((cost.total ?? 0) * 1e6),
133
- total: Number.isFinite(cost.total) ? cost.total : (cost.totalMicros ?? 0) / 1e6,
132
+ currency: cost.currency ?? (cost.mixedCurrency ? null : 'USD'),
133
+ totalMicros: Number.isFinite(cost.totalMicros)
134
+ ? cost.totalMicros
135
+ : (Number.isFinite(cost.total) ? Math.round(cost.total * 1e6) : null),
136
+ total: Number.isFinite(cost.total) ? cost.total : (Number.isFinite(cost.totalMicros) ? cost.totalMicros / 1e6 : null),
137
+ // A mixed-currency run has no single number; the per-currency amounts
138
+ // travel with the line so a later summary can still add them correctly.
139
+ byCurrency: cost.byCurrency && typeof cost.byCurrency === 'object' ? { ...cost.byCurrency } : {},
134
140
  complete: cost.complete === true,
135
141
  unpriced: Array.isArray(cost.unpriced) ? [...cost.unpriced] : [],
136
142
  models: cost.records.map(model => ({
@@ -208,24 +214,30 @@ export function createLedger(root, options = {}) {
208
214
  async function summarize(range = {}) {
209
215
  const entries = await read(range)
210
216
  const byModel = {}
217
+ const byCurrency = {}
211
218
  let calls = 0
212
219
  let tokens = 0
213
- let totalMicros = 0
214
220
  let priced = 0
215
221
  let unpricedRuns = 0
216
- const currencies = new Set()
217
222
  let start = null
218
223
  let end = null
219
224
  for (const entry of entries) {
220
225
  const totals = lineTotals(entry)
221
226
  calls += Number(entry.calls) || totals.calls
222
227
  tokens += Number(entry.tokens) || totals.tokens
223
- totalMicros += Number(entry.totalMicros) || 0
228
+ // New lines carry the per-currency split. A line written before that
229
+ // field existed has one currency and one total, which is still exact.
230
+ const lineByCurrency = entry.byCurrency && typeof entry.byCurrency === 'object'
231
+ && Object.keys(entry.byCurrency).length > 0
232
+ ? entry.byCurrency
233
+ : (entry.currency ? { [entry.currency]: Number(entry.totalMicros) || 0 } : {})
234
+ for (const [code, micros] of Object.entries(lineByCurrency)) {
235
+ addCurrencyMicros(byCurrency, code, Number(micros) || 0)
236
+ }
224
237
  const models = Array.isArray(entry.models) ? entry.models : []
225
238
  const hasPriced = models.some(model => Number.isFinite(model.amountMicros))
226
239
  if (hasPriced) priced += 1
227
240
  if (entry.complete !== true) unpricedRuns += 1
228
- if (entry.currency) currencies.add(entry.currency)
229
241
  const at = timeOf(entry.at)
230
242
  if (at !== null) {
231
243
  start = start === null ? at : Math.min(start, at)
@@ -250,17 +262,19 @@ export function createLedger(root, options = {}) {
250
262
  }
251
263
  }
252
264
  }
265
+ const totals = currencyTotals(byCurrency)
253
266
  return {
254
267
  runs: entries.length,
255
268
  priced,
256
269
  unpricedRuns,
257
270
  calls,
258
271
  tokens,
259
- totalMicros,
260
- total: totalMicros / 1e6,
272
+ totalMicros: totals.totalMicros,
273
+ total: totals.total,
261
274
  byModel,
262
- currency: currencies.size === 1 ? [...currencies][0] : (currencies.size === 0 ? 'USD' : null),
263
- mixedCurrency: currencies.size > 1,
275
+ byCurrency,
276
+ currency: totals.currency,
277
+ mixedCurrency: totals.mixedCurrency,
264
278
  start,
265
279
  end,
266
280
  }