@xiangyixie/dsh-superagent 0.12.1 → 0.14.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/lib/report.js CHANGED
@@ -12,7 +12,7 @@
12
12
  *
13
13
  * @module dsh-superagent/report
14
14
  */
15
- import { formatMoney, moneyOfMicros } from './cost.js'
15
+ import { addCurrencyMicros, currencyTotals, formatCurrencyTally, formatMoney, moneyOfMicros } from './cost.js'
16
16
  import { ACTIVE_STATUSES } from './store.js'
17
17
  import { callsOf, formatCount, formatDuration } from './watch.js'
18
18
 
@@ -278,10 +278,24 @@ export function prepare(runs, now) {
278
278
  const phases = phasesOf(run, now)
279
279
  const calls = callsOf(run)
280
280
  const priced = run.cost && run.cost.complete !== false
281
- const cost = priced && Number.isFinite(run.cost?.total) ? run.cost.total : null
282
- const costMicros = priced
283
- ? (Number.isFinite(run.cost?.totalMicros) ? run.cost.totalMicros : Math.round((run.cost.total ?? 0) * 1e6))
284
- : null
281
+ // Amounts are kept per currency. A run that used two currency routes has
282
+ // no single figure, so its scalar cost stays null and the row prints the
283
+ // per-currency text instead.
284
+ const byCurrency = {}
285
+ if (priced) {
286
+ if (run.cost.byCurrency && typeof run.cost.byCurrency === 'object') {
287
+ for (const [code, micros] of Object.entries(run.cost.byCurrency)) {
288
+ addCurrencyMicros(byCurrency, code, Number(micros))
289
+ }
290
+ } else if (Number.isFinite(run.cost.totalMicros)) {
291
+ addCurrencyMicros(byCurrency, run.cost.currency, run.cost.totalMicros)
292
+ } else if (Number.isFinite(run.cost.total)) {
293
+ addCurrencyMicros(byCurrency, run.cost.currency, Math.round(run.cost.total * 1e6))
294
+ }
295
+ }
296
+ const money = currencyTotals(byCurrency)
297
+ const cost = priced && !money.mixedCurrency ? money.total : null
298
+ const costMicros = priced && !money.mixedCurrency ? money.totalMicros : null
285
299
  items.push({
286
300
  run,
287
301
  id: String(run.id),
@@ -293,7 +307,9 @@ export function prepare(runs, now) {
293
307
  calls,
294
308
  cost,
295
309
  costMicros,
296
- currency: run.cost?.currency ?? 'USD',
310
+ costText: priced ? formatMoney(run.cost) : null,
311
+ byCurrency,
312
+ currency: money.currency ?? run.cost?.currency ?? 'USD',
297
313
  tokens: run.usage?.totalTokens ?? 0,
298
314
  duration: Math.max(0, end - start),
299
315
  parent: String(run.parentSessionId ?? ''),
@@ -311,34 +327,35 @@ function summarize(items) {
311
327
  const byStatus = {}
312
328
  let calls = 0
313
329
  let tokens = 0
314
- // Integer micro-units: a long list of runs must not drift the cent total.
315
- let costMicros = 0
330
+ // Integer micro-units, kept per currency: a long list of runs must not drift
331
+ // the cent total, and ¥ must never be added to $.
332
+ const byCurrency = {}
316
333
  let priced = 0
317
334
  let unpriced = 0
318
- const currencies = new Set()
319
335
  for (const item of items) {
320
336
  byStatus[item.status] = (byStatus[item.status] ?? 0) + 1
321
337
  calls += item.calls
322
338
  tokens += item.tokens
323
- if (item.costMicros !== null) {
324
- costMicros += item.costMicros
339
+ const codes = Object.keys(item.byCurrency ?? {})
340
+ if (codes.length > 0) {
341
+ for (const code of codes) addCurrencyMicros(byCurrency, code, item.byCurrency[code])
325
342
  priced += 1
326
- currencies.add(item.currency)
327
343
  } else if (item.calls > 0) {
328
344
  unpriced += 1
329
345
  }
330
346
  }
331
347
  const start = items.length > 0 ? Math.min(...items.map(item => item.start)) : 0
332
348
  const end = items.length > 0 ? Math.max(...items.map(item => item.end)) : 0
333
- const currency = currencies.size === 1 ? [...currencies][0] : (currencies.size === 0 ? 'USD' : null)
349
+ const totals = currencyTotals(byCurrency)
334
350
  return {
335
351
  byStatus,
336
352
  calls,
337
353
  tokens,
338
- cost: costMicros / 1e6,
339
- costMicros,
340
- currency,
341
- mixedCurrency: currencies.size > 1,
354
+ cost: totals.total,
355
+ costMicros: totals.totalMicros,
356
+ byCurrency,
357
+ currency: totals.currency,
358
+ mixedCurrency: totals.mixedCurrency,
342
359
  priced,
343
360
  unpriced,
344
361
  start,
@@ -865,10 +882,17 @@ export function buildReport(runs, options = {}) {
865
882
  .filter(item => item.calls > 0)
866
883
  .map(item => ({ t: item.end, value: item.calls }))
867
884
  .sort((a, b) => a.t - b.t)
868
- const costEvents = items
869
- .filter(item => item.cost !== null && item.cost > 0)
870
- .map(item => ({ t: item.end, value: item.cost }))
871
- .sort((a, b) => a.t - b.t)
885
+ const costCurrencies = [...new Set(items.flatMap(item => Object.keys(item.byCurrency ?? {})))]
886
+ .sort()
887
+ // One cumulative curve per currency: a mixed window gets two honest curves
888
+ // instead of one line that adds ¥ to $.
889
+ const costSeries = costCurrencies.map(code => ({
890
+ code,
891
+ events: items
892
+ .filter(item => (item.byCurrency?.[code] ?? 0) > 0)
893
+ .map(item => ({ t: item.end, value: item.byCurrency[code] / 1_000_000 }))
894
+ .sort((a, b) => a.t - b.t),
895
+ }))
872
896
 
873
897
  const problemRuns = items
874
898
  .filter(item => ['failed', 'cancelled', 'interrupted', 'partial'].includes(item.status))
@@ -884,10 +908,10 @@ export function buildReport(runs, options = {}) {
884
908
  'Cost',
885
909
  summary.priced === 0
886
910
  ? 'unpriced'
887
- : `${moneyOfMicros(summary.costMicros, summary.currency ?? 'USD')}${summary.unpriced > 0 ? '+' : ''}`,
911
+ : `${formatCurrencyTally(summary.byCurrency)}${summary.unpriced > 0 ? '+' : ''}`,
888
912
  summary.priced === 0
889
913
  ? 'no configured price — not shown as zero'
890
- : `${summary.priced}/${items.length} runs priced${summary.mixedCurrency ? ' · mixed currencies' : ''}`,
914
+ : `${summary.priced}/${items.length} runs priced${summary.mixedCurrency ? ' · kept per currency' : ''}`,
891
915
  '#0f766e',
892
916
  ),
893
917
  ].join('')
@@ -944,7 +968,7 @@ export function buildReport(runs, options = {}) {
944
968
  `<td><span class="chip" style="background:${color}">${escapeHtml(item.status)}</span><span class="goal" title="${escapeHtml(item.goal)}">${escapeHtml(item.goal)}</span></td>`,
945
969
  `<td class="num">${escapeHtml(item.calls ? String(item.calls) : '—')}</td>`,
946
970
  `<td class="num">${escapeHtml(item.tokens ? formatCount(item.tokens) : '—')}</td>`,
947
- `<td class="num">${escapeHtml(item.costMicros === null ? 'unpriced' : moneyOfMicros(item.costMicros, item.currency))}</td>`,
971
+ `<td class="num">${escapeHtml(item.costText ?? 'unpriced')}</td>`,
948
972
  `<td class="num">${escapeHtml(formatDuration(item.duration))}</td>`,
949
973
  `<td class="id">${escapeHtml(item.parent.replace(/^session-/, '').slice(0, 8) || '—')}</td>`,
950
974
  '</tr>',
@@ -961,10 +985,7 @@ export function buildReport(runs, options = {}) {
961
985
  const tokens = (usage.inputTokens ?? 0) + (usage.cacheReadTokens ?? 0) + (usage.outputTokens ?? 0)
962
986
  const cost = subtask.cost && Object.keys(subtask.cost.byModel ?? {}).length > 0 ? subtask.cost : null
963
987
  const costText = cost
964
- ? `${moneyOfMicros(
965
- Number.isFinite(cost.totalMicros) ? cost.totalMicros : Math.round((cost.total ?? 0) * 1e6),
966
- cost.currency ?? item.currency,
967
- )}${cost.complete ? '' : '+'}`
988
+ ? `${formatMoney(cost, item.currency)}${cost.complete ? '' : '+'}`
968
989
  : 'unpriced'
969
990
  return [
970
991
  '<tr>',
@@ -1023,7 +1044,16 @@ export function buildReport(runs, options = {}) {
1023
1044
  </section>
1024
1045
  <section class="panel" id="cost">
1025
1046
  <div class="panel-head"><h2>Cost</h2><span class="hint">cumulative at each run's end</span></div>
1026
- <figure>${curveSvg(costEvents, { start, end, ticks, color: '#0f766e', title: 'Cumulative cost', formatter: value => money(value, summary.currency ?? 'USD'), empty: 'no priced runs' })}</figure>
1047
+ <figure>${costSeries.length === 0
1048
+ ? curveSvg([], { start, end, ticks, color: '#0f766e', title: 'Cumulative cost', formatter: value => money(value, summary.currency ?? 'USD'), empty: 'no priced runs' })
1049
+ : costSeries.map(series => curveSvg(series.events, {
1050
+ start,
1051
+ end,
1052
+ ticks,
1053
+ color: '#0f766e',
1054
+ title: costSeries.length > 1 ? `Cumulative cost (${series.code})` : 'Cumulative cost',
1055
+ formatter: value => money(value, series.code),
1056
+ })).join('')}</figure>
1027
1057
  <p class="hint">${escapeHtml(pricedNote)}</p>
1028
1058
  </section>
1029
1059
  </div>