@xiangyixie/dsh-superagent 0.9.3 → 0.11.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/cost.js ADDED
@@ -0,0 +1,267 @@
1
+ /**
2
+ * Cost accounting for recorded token usage.
3
+ *
4
+ * Prices are list prices per 1M tokens, supplied by the operator as
5
+ * `pricing: { "provider/model": { input, output, cacheRead, currency } }`.
6
+ * A bare model id is accepted as a fallback, and an exact `provider/model`
7
+ * key always wins, so usage logged as `dashscope/qwen-flash` can be priced by
8
+ * `"qwen-flash"` without guessing the provider.
9
+ *
10
+ * Money is accumulated as integer micro-units (1e-6 of the currency), never as
11
+ * floating point. Each model line is rounded once to the nearest micro-unit,
12
+ * the total is a plain integer sum, and presentation rounds that integer once
13
+ * more. A model absent from the table is reported as unpriced — it is never
14
+ * silently priced at zero, because "we do not know" and "it was free" are
15
+ * different answers.
16
+ *
17
+ * @module dsh-superagent/cost
18
+ */
19
+
20
+ /** Micro-units per currency unit. */
21
+ const MICRO = 1_000_000
22
+ /** Tokens per price unit: the table is quoted per million tokens. */
23
+ const TOKENS_PER_PRICE = 1_000_000
24
+ /** The smallest amount the report will print instead of "less than". */
25
+ const SMALLEST_SHOWN_MICROS = 100
26
+
27
+ /**
28
+ * Currency symbol for a three-letter code.
29
+ * @param {string} code - ISO currency code.
30
+ * @returns {string} symbol, or the code plus a space when unknown.
31
+ */
32
+ export function currencySymbol(code) {
33
+ if (code === 'CNY' || code === 'RMB') return '¥'
34
+ if (code === 'USD') return '$'
35
+ if (code === 'EUR') return '€'
36
+ return `${code ?? ''} `
37
+ }
38
+
39
+ /**
40
+ * Convert one configured price to integer micro-units per million tokens.
41
+ * @param {unknown} rate - price quoted per 1M tokens.
42
+ * @returns {number | null} micro-units, or null when the value is unusable.
43
+ */
44
+ function rateMicros(rate) {
45
+ if (typeof rate !== 'number' || !Number.isFinite(rate) || rate < 0) return null
46
+ return Math.round(rate * MICRO)
47
+ }
48
+
49
+ /**
50
+ * Validate and normalise one pricing row.
51
+ * @param {unknown} entry - configured `{ input, output, cacheRead?, cache?, currency? }`.
52
+ * @returns {{ input: number, output: number, cacheRead: number, currency: string,
53
+ * inputMicros: number, outputMicros: number, cacheMicros: number } | null}
54
+ */
55
+ function normalizeEntry(entry) {
56
+ if (!entry || typeof entry !== 'object') return null
57
+ const inputMicros = rateMicros(entry.input)
58
+ const outputMicros = rateMicros(entry.output)
59
+ if (inputMicros === null || outputMicros === null) return null
60
+ // A separate cache rate is optional. Without one, cache reads are priced as
61
+ // ordinary input, which over-reports rather than under-reports the spend.
62
+ const configuredCache = rateMicros(entry.cacheRead ?? entry.cache)
63
+ const cacheRead = configuredCache === null ? entry.input : (entry.cacheRead ?? entry.cache)
64
+ return {
65
+ input: entry.input,
66
+ output: entry.output,
67
+ cacheRead,
68
+ currency: typeof entry.currency === 'string' && entry.currency.trim()
69
+ ? entry.currency.trim().toUpperCase()
70
+ : 'USD',
71
+ inputMicros,
72
+ outputMicros,
73
+ cacheMicros: configuredCache ?? inputMicros,
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Find the price row for one usage model key.
79
+ *
80
+ * Lookup order: exact `provider/model`, nested `provider: { model: {...} }`,
81
+ * then the bare model id. The first usable row wins.
82
+ *
83
+ * @param {object} table - configured pricing map.
84
+ * @param {string} model - usage model key, usually `provider/model`.
85
+ * @returns {object | null} normalised price row.
86
+ */
87
+ function priceFor(table, model) {
88
+ const exact = normalizeEntry(table[model])
89
+ if (exact) return exact
90
+ const slash = model.indexOf('/')
91
+ if (slash > 0) {
92
+ const provider = model.slice(0, slash)
93
+ const bare = model.slice(slash + 1)
94
+ const nested = table[provider]
95
+ const nestedEntry = nested && typeof nested === 'object' && !Array.isArray(nested)
96
+ ? normalizeEntry(nested[bare])
97
+ : null
98
+ if (nestedEntry) return nestedEntry
99
+ const bareEntry = normalizeEntry(table[bare])
100
+ if (bareEntry) return bareEntry
101
+ }
102
+ return null
103
+ }
104
+
105
+ /**
106
+ * Billed micro-units for one token count at one price.
107
+ *
108
+ * Integer arithmetic with a single half-up rounding keeps a long bill free of
109
+ * the drift that accumulates when fractional currency is added in floats.
110
+ *
111
+ * @param {number} tokens - token count.
112
+ * @param {number} priceMicros - micro-currency per 1M tokens.
113
+ * @returns {number} micro-units, rounded half-up.
114
+ */
115
+ function amountMicros(tokens, priceMicros) {
116
+ const count = Math.max(0, Math.trunc(Number(tokens) || 0))
117
+ if (count === 0 || priceMicros <= 0) return 0
118
+ const numerator = BigInt(count) * BigInt(priceMicros)
119
+ const denominator = BigInt(TOKENS_PER_PRICE)
120
+ const whole = numerator / denominator
121
+ const rest = numerator % denominator
122
+ const rounded = rest * 2n >= denominator ? whole + 1n : whole
123
+ return Number(rounded)
124
+ }
125
+
126
+ /**
127
+ * Price recorded usage with an explicit per-model table.
128
+ *
129
+ * @param {object | null} usage - summed usage for one run (or subtask).
130
+ * @param {Record<string, object> | undefined} pricing - configured price table.
131
+ * @returns {{
132
+ * currency: string,
133
+ * totalMicros: number,
134
+ * total: number,
135
+ * byModel: Record<string, number>,
136
+ * byModelMicros: Record<string, number>,
137
+ * records: object[],
138
+ * unpriced: string[],
139
+ * complete: boolean,
140
+ * } | null} cost, or null when there is nothing to price.
141
+ */
142
+ export function computeCost(usage, pricing) {
143
+ if (!usage || !usage.modelCalls) return null
144
+ const table = pricing && typeof pricing === 'object' ? pricing : {}
145
+ const byModel = {}
146
+ const byModelMicros = {}
147
+ const records = []
148
+ const unpriced = []
149
+ let totalMicros = 0
150
+ let currency = null
151
+
152
+ for (const [model, entry] of Object.entries(usage.models ?? {})) {
153
+ const record = {
154
+ model,
155
+ calls: entry.calls ?? 0,
156
+ inputTokens: entry.inputTokens ?? 0,
157
+ outputTokens: entry.outputTokens ?? 0,
158
+ cacheReadTokens: entry.cacheReadTokens ?? 0,
159
+ inputPrice: null,
160
+ outputPrice: null,
161
+ cachePrice: null,
162
+ amountMicros: null,
163
+ }
164
+ const price = priceFor(table, model)
165
+ if (!price) {
166
+ // Named, not zero: a caller can see exactly which route has no price.
167
+ unpriced.push(model)
168
+ records.push(record)
169
+ continue
170
+ }
171
+ record.inputPrice = price.input
172
+ record.outputPrice = price.output
173
+ record.cachePrice = price.cacheRead
174
+ const amount = amountMicros(record.inputTokens, price.inputMicros)
175
+ + amountMicros(record.cacheReadTokens, price.cacheMicros)
176
+ + amountMicros(record.outputTokens, price.outputMicros)
177
+ record.amountMicros = amount
178
+ byModelMicros[model] = amount
179
+ byModel[model] = amount / MICRO
180
+ totalMicros += amount
181
+ records.push(record)
182
+ currency = currency ?? price.currency
183
+ }
184
+
185
+ return {
186
+ currency: currency ?? 'USD',
187
+ totalMicros,
188
+ total: totalMicros / MICRO,
189
+ byModel,
190
+ byModelMicros,
191
+ records,
192
+ unpriced,
193
+ // "Complete" means every recorded model has a price. A usage record with no
194
+ // model attribution at all also cannot be called complete.
195
+ complete: unpriced.length === 0 && records.length > 0,
196
+ }
197
+ }
198
+
199
+ /**
200
+ * Price a run and each of its subtasks.
201
+ *
202
+ * Subtask costs cover only the worker children; planner and aggregator calls
203
+ * are part of the run total but belong to no subtask. A subtask without a
204
+ * readable usage log keeps `cost: null` rather than a fabricated zero.
205
+ *
206
+ * @param {object} run - run record with `usage`, `subtasks`.
207
+ * @param {object | undefined} pricing - configured price table.
208
+ * @returns {object | null} the run cost.
209
+ */
210
+ export function applyCost(run, pricing) {
211
+ if (!run || typeof run !== 'object') return null
212
+ run.cost = computeCost(run.usage, pricing)
213
+ for (const subtask of run.subtasks ?? []) {
214
+ subtask.cost = computeCost(subtask?.usage, pricing)
215
+ }
216
+ return run.cost
217
+ }
218
+
219
+ /**
220
+ * Integer micro-units for a cost object, accepting a legacy `total` number.
221
+ * @param {{ totalMicros?: number, total?: number } | number} cost - cost record.
222
+ * @returns {number} micro-units, or NaN.
223
+ */
224
+ function microsOf(cost) {
225
+ if (typeof cost === 'number') {
226
+ return Number.isFinite(cost) ? Math.round(cost * MICRO) : Number.NaN
227
+ }
228
+ if (cost && Number.isFinite(cost.totalMicros)) return Math.trunc(cost.totalMicros)
229
+ if (cost && Number.isFinite(cost.total)) return Math.round(cost.total * MICRO)
230
+ return Number.NaN
231
+ }
232
+
233
+ /**
234
+ * Render integer micro-units with one rounding, half-up, at the shown precision.
235
+ *
236
+ * Four decimals below one currency unit, two above it; a positive amount
237
+ * smaller than the smallest shown value is marked as such instead of printed
238
+ * as zero.
239
+ *
240
+ * @param {number} micros - integer micro-units.
241
+ * @param {string} [currency] - ISO currency code.
242
+ * @returns {string} e.g. `¥0.1016`, `$1.24`, `<¥0.0001`, `—`.
243
+ */
244
+ export function moneyOfMicros(micros, currency = 'USD') {
245
+ const symbol = currencySymbol(currency)
246
+ if (!Number.isFinite(micros)) return '—'
247
+ if (micros === 0) return `${symbol}0`
248
+ const value = Math.abs(micros)
249
+ if (value < SMALLEST_SHOWN_MICROS) return `<${symbol}0.0001`
250
+ const digits = value < MICRO ? 4 : 2
251
+ const step = 10 ** (6 - digits)
252
+ const rounded = Math.floor((value + step / 2) / step) * step
253
+ const whole = Math.floor(rounded / MICRO)
254
+ const fraction = String(rounded % MICRO).padStart(6, '0').slice(0, digits)
255
+ return `${micros < 0 ? '-' : ''}${symbol}${whole}.${fraction}`
256
+ }
257
+
258
+ /**
259
+ * Format a cost record (or a bare amount) for display.
260
+ * @param {{ totalMicros?: number, total?: number, currency?: string } | number | null} cost - cost.
261
+ * @param {string} [currency] - fallback currency code.
262
+ * @returns {string} formatted money, or `—` when unknown.
263
+ */
264
+ export function formatMoney(cost, currency) {
265
+ const code = (cost && typeof cost === 'object' && cost.currency) || currency || 'USD'
266
+ return moneyOfMicros(microsOf(cost), code)
267
+ }