@yaks/spend 0.0.0-stage → 0.2.36

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.
Files changed (69) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +159 -2
  3. package/_dist/graph.d.ts +26 -0
  4. package/_dist/graph.d.ts.map +1 -0
  5. package/_dist/mod.d.ts +11 -0
  6. package/_dist/mod.d.ts.map +1 -0
  7. package/_dist/routes.d.ts +10 -0
  8. package/_dist/routes.d.ts.map +1 -0
  9. package/_dist/service.d.ts +41 -0
  10. package/_dist/service.d.ts.map +1 -0
  11. package/_dist/tally.d.ts +111 -0
  12. package/_dist/tally.d.ts.map +1 -0
  13. package/_dist/tools.d.ts +52 -0
  14. package/_dist/tools.d.ts.map +1 -0
  15. package/_dist/use.d.ts +31 -0
  16. package/_dist/use.d.ts.map +1 -0
  17. package/_dist/views.d.ts +27 -0
  18. package/_dist/views.d.ts.map +1 -0
  19. package/_dist/vocab.d.ts +7 -0
  20. package/_dist/vocab.d.ts.map +1 -0
  21. package/_dist/window.d.ts +132 -0
  22. package/_dist/window.d.ts.map +1 -0
  23. package/_dist/words.d.ts +40 -0
  24. package/_dist/words.d.ts.map +1 -0
  25. package/budget.js +131 -0
  26. package/budget.js.map +1 -0
  27. package/budget.ts +128 -0
  28. package/day.js +336 -0
  29. package/day.js.map +1 -0
  30. package/day.ts +364 -0
  31. package/deno.json +1 -0
  32. package/graph.js +59 -0
  33. package/graph.js.map +1 -0
  34. package/graph.ts +61 -0
  35. package/mod.js +11 -0
  36. package/mod.js.map +1 -0
  37. package/mod.ts +34 -0
  38. package/package.json +57 -4
  39. package/page.js +352 -0
  40. package/page.js.map +1 -0
  41. package/page.ts +491 -0
  42. package/routes.js +22 -0
  43. package/routes.js.map +1 -0
  44. package/routes.ts +19 -0
  45. package/service.js +124 -0
  46. package/service.js.map +1 -0
  47. package/service.ts +149 -0
  48. package/tally.js +385 -0
  49. package/tally.js.map +1 -0
  50. package/tally.ts +424 -0
  51. package/tools.js +243 -0
  52. package/tools.js.map +1 -0
  53. package/tools.ts +357 -0
  54. package/use.js +172 -0
  55. package/use.js.map +1 -0
  56. package/use.ts +226 -0
  57. package/views.js +79 -0
  58. package/views.js.map +1 -0
  59. package/views.ts +96 -0
  60. package/vocab.js +17 -0
  61. package/vocab.js.map +1 -0
  62. package/vocab.json +237 -0
  63. package/vocab.ts +18 -0
  64. package/window.js +259 -0
  65. package/window.js.map +1 -0
  66. package/window.ts +336 -0
  67. package/words.js +47 -0
  68. package/words.js.map +1 -0
  69. package/words.ts +77 -0
package/tally.ts ADDED
@@ -0,0 +1,424 @@
1
+ /**
2
+ * A day's model requests, tallied: pure, over the rows a day's read returns
3
+ * (./day.ts). Each request is its entry: `usage`, the `cost` its provider
4
+ * reported or the hook weighed, and the `context_parts` the importer laid out
5
+ * (@yaks/session/parts). The fixed part it carries is spread over the pieces
6
+ * its session's `fixed` names; a request whose context was not recorded is one
7
+ * part, `unrecorded`. Dollars are weighed per part at the model's price, then
8
+ * scaled to what the provider reported where it did. Days add up (`merged`),
9
+ * so a week is its days.
10
+ *
11
+ * @module
12
+ */
13
+ import type { Bundle, Comp, Eid } from 'jsr:@yaks/graph@0.2.36'
14
+ import type { Paid, Price, Usage } from 'jsr:@yaks/model@0.2.36'
15
+ import { dollars, hourly } from '@yaks/model/vocab'
16
+
17
+ /** One day's tally, as `spend{…}` holds it. */
18
+ export type Spend = {
19
+ day: string
20
+ requests: number
21
+ /** [read, write, fresh, generated] */
22
+ tokens: number[]
23
+ dollars: number
24
+ /** by part: [read, write, fresh, generated, dollars] */
25
+ parts: Record<string, number[]>
26
+ /** by session: [tokens, dollars, requests] */
27
+ sessions: Record<Eid, number[]>
28
+ /** by task: [tokens, dollars] */
29
+ tasks: Record<Eid, number[]>
30
+ /** by model: [read, write, fresh, generated, dollars, requests] */
31
+ models: Record<Eid, number[]>
32
+ /** by entry: [tokens, dollars] */
33
+ entries: Record<Eid, number[]>
34
+ /** by the account whose plan the requests billed against: what they ran
35
+ * up on it */
36
+ plans: Record<Eid, Plan>
37
+ /** the models a request was weighed with no price for, and reported no
38
+ * dollars: their dollars are missing, not zero */
39
+ unpriced: Eid[]
40
+ }
41
+
42
+ /** The rows a tally ranks. */
43
+ export type Rows = 'parts' | 'sessions' | 'tasks' | 'models' | 'entries'
44
+
45
+ /** What one plan's requests ran up: their dollars, and each row's. */
46
+ export type Plan =
47
+ & {
48
+ dollars: number
49
+ unpriced?: Eid[]
50
+ /** by model, by session: what a limit on one model's requests counts */
51
+ metered?: Record<Eid, Record<Eid, number>>
52
+ }
53
+ & Record<
54
+ Rows,
55
+ Record<string, number>
56
+ >
57
+
58
+ let ROWS: Rows[] = ['parts', 'sessions', 'tasks', 'models', 'entries']
59
+
60
+ /** What a tally reads beside its requests. */
61
+ export type Known = {
62
+ /** each model's price, by its eid */
63
+ prices: Map<Eid, Price>
64
+ /** each session's fixed part, piece by piece (@yaks/session `fixed`) */
65
+ fixed: Map<Eid, Record<string, number>>
66
+ /** the tasks each session worked on */
67
+ tasks: Map<Eid, Eid[]>
68
+ /** the account whose plan each request billed against, where one did */
69
+ plans?: Map<Eid, Eid>
70
+ }
71
+
72
+ /** How many entries a tally names. */
73
+ export let ENTRIES = 50
74
+
75
+ let comp = (b: Bundle, name: string) => (b[name] ?? {}) as Comp
76
+ let sum = (ns: number[]) => ns.reduce((a, b) => a + b, 0)
77
+
78
+ /** An empty day. */
79
+ export let empty = (day: string): Spend => ({
80
+ day,
81
+ requests: 0,
82
+ tokens: [0, 0, 0, 0],
83
+ dollars: 0,
84
+ parts: {},
85
+ sessions: {},
86
+ tasks: {},
87
+ models: {},
88
+ entries: {},
89
+ plans: {},
90
+ unpriced: [],
91
+ })
92
+
93
+ let unbilled = (): Plan => ({
94
+ dollars: 0,
95
+ unpriced: [],
96
+ metered: {},
97
+ parts: {},
98
+ sessions: {},
99
+ tasks: {},
100
+ models: {},
101
+ entries: {},
102
+ })
103
+
104
+ // Add `n` to what a plan's `model` ran up in `session`.
105
+ let meter = (plan: Plan | undefined, model: Eid, session: Eid, n: number) => {
106
+ if (!plan) return
107
+ let at = (plan.metered ??= {})[model] ??= {}
108
+ at[session] = (at[session] ?? 0) + n
109
+ }
110
+
111
+ // Name a model once in a list.
112
+ let name = (to: Eid[] | undefined, model: Eid): Eid[] =>
113
+ [...new Set([...to ?? [], model])].sort()
114
+
115
+ // Add `dollars` to what a plan's `row` holds under `key`.
116
+ let bill = (plan: Plan | undefined, row: Rows, key: string, n: number) => {
117
+ if (plan) plan[row][key] = (plan[row][key] ?? 0) + n
118
+ }
119
+
120
+ // Add `more` into the tally `to` keeps under `key`.
121
+ let add = (to: Record<string, number[]>, key: string, more: number[]) => {
122
+ let at = to[key] ??= more.map(() => 0)
123
+ more.forEach((n, i) => at[i] += n)
124
+ }
125
+
126
+ // What a request used, as paid, where its context was not recorded.
127
+ let paid = (u: Usage): Paid => {
128
+ let read = u.cached_tokens ?? 0
129
+ let write = u.cache_write_tokens ?? 0
130
+ return [
131
+ read,
132
+ write,
133
+ Math.max(0, (u.input_tokens ?? 0) - read - write),
134
+ u.output_tokens ?? 0,
135
+ ]
136
+ }
137
+
138
+ // The fixed part spread over its pieces, by their sizes.
139
+ let spread = (p: Paid, pieces?: Record<string, number>): [string, Paid][] => {
140
+ let total = sum(Object.values(pieces ?? {}))
141
+ return total
142
+ ? Object.entries(pieces!).map((
143
+ [k, n],
144
+ ) => [k, p.map((t) => t * n / total) as Paid])
145
+ : [['fixed', p]]
146
+ }
147
+
148
+ // A request's parts, as paid.
149
+ let partsOf = (b: Bundle, known: Known): [string, Paid][] => {
150
+ let cp = comp(b, 'context_parts')
151
+ let parts = cp.parts as Record<string, Paid> | undefined
152
+ if (!parts) return [['unrecorded', paid(comp(b, 'usage') as Usage)]]
153
+ let session = String(comp(b, 'entry').session ?? '')
154
+ return Object.entries(parts).flatMap(([k, p]): [string, Paid][] =>
155
+ k == 'fixed' ? spread(p, known.fixed.get(session)) : [[k, p]]
156
+ )
157
+ }
158
+
159
+ /**
160
+ * One day's tally of its requests.
161
+ *
162
+ * ```ts
163
+ * import { tally } from './tally.ts'
164
+ * import { equal } from '@yaks/testing'
165
+ *
166
+ * let ask = {
167
+ * entity: { eid: 'a' },
168
+ * entry: { session: 's' },
169
+ * ask: { to: 'm' },
170
+ * usage: { input_tokens: 2e6, cached_tokens: 1e6, output_tokens: 0 },
171
+ * context_parts: { parts: { fixed: [1e6, 0, 0, 0], input: [0, 0, 1e6, 0] } },
172
+ * }
173
+ * let t = tally('2026-10-20', [ask], {
174
+ * prices: new Map([['m', { input: 4, cached: 0.2, output: 20 }]]),
175
+ * fixed: new Map([['s', { 'tool Read': 3, skills: 1 }]]),
176
+ * tasks: new Map([['s', ['T-1', 'T-2']]]),
177
+ * plans: new Map([['a', 'claude']]),
178
+ * })
179
+ * equal(t.dollars, 4.2)
180
+ * equal([t.plans.claude.dollars, t.plans.claude.tasks], [4.2, { 'T-1': 2.1, 'T-2': 2.1 }])
181
+ * equal(t.plans.claude.metered, { m: { s: 4.2 } })
182
+ * equal(t.parts, {
183
+ * 'tool Read': [750000, 0, 0, 0, 0.15],
184
+ * skills: [250000, 0, 0, 0, 0.05],
185
+ * input: [0, 0, 1000000, 0, 4],
186
+ * })
187
+ * equal(t.tasks, { 'T-1': [1e6, 2.1], 'T-2': [1e6, 2.1] })
188
+ * ```
189
+ *
190
+ * A model with no price, whose request reported no dollars, is named as
191
+ * unpriced, on the day and on its plan:
192
+ *
193
+ * ```ts
194
+ * import { tally } from './tally.ts'
195
+ * import { equal } from '@yaks/testing'
196
+ *
197
+ * let ask = {
198
+ * entity: { eid: 'a' },
199
+ * entry: { session: 's' },
200
+ * ask: { to: 'free' },
201
+ * usage: { input_tokens: 10, output_tokens: 5 },
202
+ * }
203
+ * let t = tally('2026-10-20', [ask], {
204
+ * prices: new Map(),
205
+ * fixed: new Map(),
206
+ * tasks: new Map(),
207
+ * plans: new Map([['a', 'codex']]),
208
+ * })
209
+ * equal([t.dollars, t.unpriced, t.plans.codex.unpriced], [0, ['free'], ['free']])
210
+ * ```
211
+ */
212
+ export let tally = (day: string, rows: Bundle[], known: Known): Spend => {
213
+ let out = empty(day)
214
+ for (let b of rows) {
215
+ let u = comp(b, 'usage') as Usage
216
+ // Weighed at the price of the model that answered, else the one asked;
217
+ // with neither priced, the one that answered is the one to price.
218
+ let ask = comp(b, 'ask')
219
+ let models = [ask.served, ask.to ?? comp(b, 'using').model]
220
+ .filter((m) => m != null).map(String)
221
+ let model = models.find((m) => known.prices.has(m)) ?? models[0] ?? ''
222
+ let price = known.prices.get(model)
223
+ let hour = hourly(u)
224
+ let weigh = (p: Paid) => price ? dollars(price, p, hour) : 0
225
+ let parts = partsOf(b, known)
226
+ let weighed = sum(parts.map(([, p]) => weigh(p)))
227
+ let cost = comp(b, 'cost')
228
+ let reported = cost.reported && typeof cost.dollars == 'number'
229
+ let spent = reported ? cost.dollars as number : weighed
230
+ // Dollars per part: weighed, scaled to what was reported; where nothing
231
+ // weighs (no price), by tokens.
232
+ let tokens = sum(parts.map(([, p]) => sum(p)))
233
+ let share = (p: Paid) =>
234
+ weighed
235
+ ? weigh(p) * spent / weighed
236
+ : tokens
237
+ ? sum(p) * spent / tokens
238
+ : 0
239
+ let at = known.plans?.get(b.entity.eid)
240
+ let plan = at ? out.plans[at] ??= unbilled() : undefined
241
+ let all: Paid = [0, 0, 0, 0]
242
+ for (let [k, p] of parts) {
243
+ add(out.parts, k, [...p, share(p)])
244
+ bill(plan, 'parts', k, share(p))
245
+ p.forEach((n, i) => all[i] += n)
246
+ }
247
+ if (!price && !reported && sum(all)) {
248
+ out.unpriced = name(out.unpriced, model)
249
+ if (plan) plan.unpriced = name(plan.unpriced, model)
250
+ }
251
+ let entries = (comp(b, 'context_parts').entries ?? {}) as Record<Eid, Paid>
252
+ for (let [eid, p] of Object.entries(entries)) {
253
+ add(out.entries, eid, [sum(p), share(p)])
254
+ bill(plan, 'entries', eid, share(p))
255
+ }
256
+ let session = String(comp(b, 'entry').session ?? '')
257
+ let total = sum(all)
258
+ add(out.sessions, session, [total, spent, 1])
259
+ bill(plan, 'sessions', session, spent)
260
+ let tasks = known.tasks.get(session) ?? []
261
+ for (let t of tasks) {
262
+ add(out.tasks, t, [total / tasks.length, spent / tasks.length])
263
+ bill(plan, 'tasks', t, spent / tasks.length)
264
+ }
265
+ add(out.models, model, [...all, spent, 1])
266
+ bill(plan, 'models', model, spent)
267
+ meter(plan, model, session, spent)
268
+ if (plan) plan.dollars += spent
269
+ out.tokens = out.tokens.map((n, i) => n + all[i])
270
+ out.dollars += spent
271
+ out.requests++
272
+ }
273
+ return settled(out)
274
+ }
275
+
276
+ /** A tally's numbers rounded as kept: tokens whole, dollars to the
277
+ * hundredth of a cent, and its costliest entries alone. */
278
+ export let settled = (s: Spend): Spend => {
279
+ let whole = (ns: number[], money: number[]) =>
280
+ ns.map((n, i) =>
281
+ money.includes(i) ? Math.round(n * 1e4) / 1e4 : Math.round(n)
282
+ )
283
+ let each = (m: Record<string, number[]>, money: number[]) =>
284
+ Object.fromEntries(
285
+ Object.entries(m).map(([k, ns]) => [k, whole(ns, money)]),
286
+ )
287
+ let entries = each(
288
+ Object.fromEntries(ranked(s.entries, 1).slice(0, ENTRIES)),
289
+ [1],
290
+ )
291
+ let cents = (n: number) => Math.round(n * 1e4) / 1e4
292
+ let plan = (p: Plan): Plan => ({
293
+ dollars: cents(p.dollars),
294
+ unpriced: p.unpriced ?? [],
295
+ metered: Object.fromEntries(
296
+ Object.entries(p.metered ?? {}).map(([model, by]) => [
297
+ model,
298
+ Object.fromEntries(Object.entries(by).map(([k, n]) => [k, cents(n)])),
299
+ ]),
300
+ ),
301
+ ...Object.fromEntries(ROWS.map((row) => [
302
+ row,
303
+ Object.fromEntries(
304
+ Object.entries(p[row])
305
+ .filter(([k]) => row != 'entries' || k in entries)
306
+ .map(([k, n]) => [k, cents(n)]),
307
+ ),
308
+ ])) as Record<Rows, Record<string, number>>,
309
+ })
310
+ return {
311
+ ...s,
312
+ tokens: whole(s.tokens, []),
313
+ dollars: Math.round(s.dollars * 1e4) / 1e4,
314
+ parts: each(s.parts, [4]),
315
+ sessions: each(s.sessions, [1]),
316
+ tasks: each(s.tasks, [1]),
317
+ models: each(s.models, [4]),
318
+ entries,
319
+ plans: Object.fromEntries(
320
+ Object.entries(s.plans ?? {}).map(([k, p]) => [k, plan(p)]),
321
+ ),
322
+ }
323
+ }
324
+
325
+ /**
326
+ * Days added up, as one tally named by the first.
327
+ *
328
+ * ```ts
329
+ * import { empty, merged } from './tally.ts'
330
+ * import { equal } from '@yaks/testing'
331
+ *
332
+ * let a = { ...empty('2026-10-20'), requests: 2, dollars: 1, parts: { skills: [1, 0, 0, 0, 1] } }
333
+ * let b = { ...empty('2026-10-21'), requests: 1, dollars: 2, parts: { skills: [2, 0, 0, 0, 2] } }
334
+ * let m = merged([a, b])
335
+ * equal([m.day, m.requests, m.dollars, m.parts], ['2026-10-20', 3, 3, { skills: [3, 0, 0, 0, 3] }])
336
+ * ```
337
+ */
338
+ export let merged = (days: Spend[]): Spend => {
339
+ let out = empty(days[0]?.day ?? '')
340
+ for (let d of days) {
341
+ out.requests += d.requests
342
+ for (let m of d.unpriced ?? []) out.unpriced = name(out.unpriced, m)
343
+ out.dollars += d.dollars
344
+ out.tokens = out.tokens.map((n, i) => n + (d.tokens[i] ?? 0))
345
+ for (let k of ROWS) {
346
+ for (let [key, ns] of Object.entries(d[k] ?? {})) add(out[k], key, ns)
347
+ }
348
+ for (let [at, p] of Object.entries(d.plans ?? {})) {
349
+ let plan = out.plans[at] ??= unbilled()
350
+ plan.dollars += p.dollars
351
+ for (let m of p.unpriced ?? []) plan.unpriced = name(plan.unpriced, m)
352
+ for (let [model, by] of Object.entries(p.metered ?? {})) {
353
+ for (let [k, n] of Object.entries(by)) meter(plan, model, k, n)
354
+ }
355
+ for (let row of ROWS) {
356
+ for (let [k, n] of Object.entries(p[row] ?? {})) bill(plan, row, k, n)
357
+ }
358
+ }
359
+ }
360
+ return settled(out)
361
+ }
362
+
363
+ /** A tally's rows, costliest first, by the number at `at`. */
364
+ export let ranked = (
365
+ m: Record<string, number[]>,
366
+ at: number,
367
+ ): [string, number[]][] =>
368
+ Object.entries(m).sort((a, b) => (b[1][at] ?? 0) - (a[1][at] ?? 0))
369
+
370
+ /** The spend a row holds, as a tally. */
371
+ export let spendOf = (b: Bundle | undefined): Spend | undefined => {
372
+ let s = b?.spend as Partial<Spend> | undefined
373
+ return s?.day ? { ...empty(s.day), ...s } as Spend : undefined
374
+ }
375
+
376
+ /** A week's completed tasks and what was charged to them. */
377
+ export type Done = {
378
+ /** the Monday the week began */
379
+ week: string
380
+ /** the tasks completed that week */
381
+ tasks: number
382
+ /** how many of them were charged no dollars */
383
+ unpaid: number
384
+ /** dollars paid by the token */
385
+ cash: number
386
+ /** dollars billed against a plan: usage at list price, not cash */
387
+ usage: number
388
+ }
389
+
390
+ /**
391
+ * The tasks completed each week and the dollars a tally charged them, as cash
392
+ * (paid by the token) and usage at list price (billed against a plan). The
393
+ * dollars are the tally's `tasks` split, an estimate: a session divided evenly
394
+ * across the tasks it worked on. `finished` is each task and the week it was
395
+ * completed in.
396
+ *
397
+ * ```ts
398
+ * import { done, empty } from './tally.ts'
399
+ * import { equal } from '@yaks/testing'
400
+ *
401
+ * let s = { ...empty('2026-11-09'), tasks: { a: [0, 5], b: [0, 1] },
402
+ * plans: { c: { dollars: 4, parts: {}, sessions: {}, tasks: { a: 4 }, models: {}, entries: {} } } }
403
+ * equal(done(s, [['a', '2026-11-09'], ['b', '2026-11-09'], ['z', '2026-11-16']]), [
404
+ * { week: '2026-11-09', tasks: 2, unpaid: 0, cash: 2, usage: 4 },
405
+ * { week: '2026-11-16', tasks: 1, unpaid: 1, cash: 0, usage: 0 },
406
+ * ])
407
+ * ```
408
+ */
409
+ export let done = (s: Spend, finished: [Eid, string][]): Done[] => {
410
+ let weeks = new Map<string, Done>()
411
+ let plans = Object.values(s.plans ?? {})
412
+ for (let [task, week] of finished) {
413
+ let w = weeks.get(week) ??
414
+ weeks.set(week, { week, tasks: 0, unpaid: 0, cash: 0, usage: 0 })
415
+ .get(week)!
416
+ let all = s.tasks[task]?.[1] ?? 0
417
+ let usage = sum(plans.map((p) => p.tasks?.[task] ?? 0))
418
+ w.tasks++
419
+ if (!all) w.unpaid++
420
+ w.usage += usage
421
+ w.cash += Math.max(0, all - usage)
422
+ }
423
+ return [...weeks.values()].sort((a, b) => a.week < b.week ? -1 : 1)
424
+ }
package/tools.js ADDED
@@ -0,0 +1,243 @@
1
+ /**
2
+ * The tools: `spend_show` reports a span of days from their tallies,
3
+ * `spend_tally` tallies days again from the requests the graph holds, and
4
+ * `spend_backfill` measures past requests (@yaks/session, @yaks/spawn
5
+ * `recounted`) first. `shared` reads each topic's share of
6
+ * the week against what its sessions used (./use.ts).
7
+ *
8
+ * @module
9
+ */ import { line } from "@yaks/query";
10
+ import { argsOf } from "@yaks/graph/call";
11
+ import { Refused } from "@yaks/graph/errors";
12
+ import { human } from "@yaks/id";
13
+ import { shares } from "@yaks/topic/share";
14
+ import { lineage, recompute } from "./day.js";
15
+ import { done, merged, ranked, spendOf } from "./tally.js";
16
+ import { said, uses } from "./use.js";
17
+ import { accountNames, after, current, dayOf, limitsOf, midnight, mondayOf, paceOf, rates, span, weekOf } from "./window.js";
18
+ import { dollars, label, percent, tokens } from "./words.js";
19
+ let answer = (call, body)=>[
20
+ {
21
+ entity: {
22
+ eid: '$said'
23
+ },
24
+ content: {
25
+ body
26
+ },
27
+ output: {
28
+ source: call.entity.eid
29
+ }
30
+ }
31
+ ];
32
+ // A day the door already read (`format: date`), or none.
33
+ let dayArg = (v)=>v ? String(v) : undefined;
34
+ // The tally of a span of days, and the plan limits beside it.
35
+ export let read = async (g, since, until, now = Date.now())=>{
36
+ let { limits, accounts } = await plans(g);
37
+ let [from, to] = span(limits, now, since, until);
38
+ // A week's rates are read from the days its week began, which can be
39
+ // before the span.
40
+ let all = await tallies(g, after(from, -7), to);
41
+ let days = all.filter((d)=>d.day >= from);
42
+ return {
43
+ spend: merged(days),
44
+ limits,
45
+ rate: rates(limits, all, now),
46
+ accounts,
47
+ from,
48
+ to
49
+ };
50
+ };
51
+ // The plan limits, and what each account is called.
52
+ let plans = async (g)=>{
53
+ let providers = await names(g, 'provider');
54
+ let accounts = accountNames(await g.read('.account *'), providers);
55
+ let limits = limitsOf(await g.read('.rate_limit *'), new Map([
56
+ ...await names(g, 'model'),
57
+ ...providers,
58
+ ...accounts
59
+ ]));
60
+ return {
61
+ limits,
62
+ accounts
63
+ };
64
+ };
65
+ // The tallies of the days from one to another, in order.
66
+ let tallies = async (g, from, to)=>(await g.read(line`.spend .spend.day>=${from} .spend.day<=${to} *`)).map(spendOf).filter((s)=>!!s).sort((a, b)=>a.day < b.day ? -1 : 1);
67
+ /** Each topic's share of the week against what its sessions, and every
68
+ * session they started, used of each plan's running week: a topic before the
69
+ * topics below it, then the work no share covers ({@link uses}). Read from
70
+ * the tallies at `now`; where no topic has a share, nothing is read. */ export let shared = async (g, now = Date.now())=>{
71
+ let given = await shares(g);
72
+ if (!given.share.size) return [];
73
+ let { limits } = await plans(g);
74
+ let from = limits.flatMap((l)=>weekOf(l, now) ?? []).sort()[0];
75
+ let days = from ? await tallies(g, from, dayOf(now)) : [];
76
+ let sessions = days.flatMap((d)=>Object.values(d.plans).flatMap((p)=>Object.keys(p.sessions)));
77
+ let rows = await lineage(g, sessions, [
78
+ 'session'
79
+ ]);
80
+ let ref = (b, comp, prop)=>{
81
+ let v = b[comp]?.[prop];
82
+ return v ? String(v) : undefined;
83
+ };
84
+ let lines = new Map([
85
+ ...rows
86
+ ].map(([eid, b])=>[
87
+ eid,
88
+ {
89
+ topic: ref(b, 'session', 'topic'),
90
+ parent: ref(b, 'spawned', 'parent')
91
+ }
92
+ ]));
93
+ return uses({
94
+ shares: given,
95
+ lines,
96
+ limits,
97
+ days,
98
+ now
99
+ });
100
+ };
101
+ // Each row of a kind, by eid, named.
102
+ let names = async (g, comp)=>new Map((await g.read(line`.${comp} *`)).map((b)=>[
103
+ b.entity.eid,
104
+ String(b[comp].name ?? b.entity.eid)
105
+ ]));
106
+ // The rows a report names, read once.
107
+ let named = async (g, eids)=>{
108
+ let id = human(g.vocab);
109
+ let rows = await g.get([
110
+ ...new Set(eids)
111
+ ]);
112
+ return new Map(rows.filter((r)=>r).map((r)=>[
113
+ r.entity.eid,
114
+ label(r, id(r))
115
+ ]));
116
+ };
117
+ /** The cost per completed task in each UTC week (Monday to Sunday) from the
118
+ * one `since` falls in to `until`'s: the dollars every tally to `until`
119
+ * charged the tasks completed that week, over those of them charged any, as
120
+ * cash and as usage at list price, never one total. Decisions are not counted.
121
+ * The charge is the tally's `tasks` split, an estimate. */ export let perTask = async (g, since, until)=>{
122
+ let from = mondayOf(since);
123
+ let days = (await g.read(line`.spend .spend.day<=${until} *`)).map(spendOf).filter((s)=>!!s);
124
+ let finished = (await g.read(line`.task !decision .completed .completed.at>=${midnight(from)} .completed.at<${midnight(after(until))} .fields=completed.at`)).map((b)=>[
125
+ b.entity.eid,
126
+ mondayOf(dayOf(String(b.completed.at)))
127
+ ]);
128
+ let each = (n, of)=>dollars(of ? n / of : 0);
129
+ return [
130
+ 'Cost per completed task (the tasks split, an estimate: each session ' + 'divided evenly across the tasks it worked on; a task charged nothing ' + 'is left out of the divisor):',
131
+ ...done(merged(days), finished).map((w)=>{
132
+ let paid = w.tasks - w.unpaid;
133
+ return ` week of ${w.week}${after(w.week, 6) > until ? ` (to ${until})` : ''}: ${each(w.cash, paid)} cash and ${each(w.usage, paid)} usage at list price a task, over ${paid} of ${w.tasks} completed (${w.unpaid} charged nothing); in all ${dollars(w.cash)} cash, ${dollars(w.usage)} usage`;
134
+ })
135
+ ].join('\n');
136
+ };
137
+ /** A span's report, as text. */ export let report = async (g, o = {})=>{
138
+ if (o.done) {
139
+ let { limits } = await plans(g);
140
+ let [from, to] = span(limits, o.now ?? Date.now(), o.since, o.until);
141
+ return perTask(g, from, to);
142
+ }
143
+ let { spend: s, limits, rate, accounts, from, to } = await read(g, o.since, o.until, o.now);
144
+ let now = o.now ?? Date.now();
145
+ let paced = limits.filter((l)=>current(l, now)).map((l)=>{
146
+ let pace = paceOf(l, now);
147
+ return `${l.name} ${l.window == 10080 ? 'week' : 'five hours'}: ${Math.round(l.used)}% used, ${Math.round(pace * 100)}% of it gone${l.resets ? `, resets ${l.resets.slice(0, 16).replace('T', ' ')} UTC` : ''}`;
148
+ });
149
+ if (o.plans) return paced.join('\n') || 'No plan limit has been read.';
150
+ let given = await shared(g, now);
151
+ let top = o.top ?? 10;
152
+ let pick = (m, at)=>ranked(m, at).slice(0, top);
153
+ let sessions = pick(s.sessions, 1), tasks = pick(s.tasks, 1);
154
+ let models = pick(s.models, 4), entries = pick(s.entries, 1);
155
+ let name = await named(g, [
156
+ ...sessions,
157
+ ...tasks,
158
+ ...models,
159
+ ...entries
160
+ ].map(([k])=>k).concat(given.flatMap((u)=>u.topic ?? [])));
161
+ let line = (k, ns, at, toks)=>` ${dollars(ns[at]).padStart(10)} ${tokens(toks).padStart(7)} ${name.get(k) ?? k}`;
162
+ let sum = (ns)=>ns.slice(0, 4).reduce((a, b)=>a + b, 0);
163
+ return [
164
+ `${from} to ${to}: ${dollars(s.dollars)}, ${s.requests} requests, ${tokens(sum(s.tokens))} tokens (${tokens(s.tokens[0])} read from cache, ${tokens(s.tokens[1])} written to it, ${tokens(s.tokens[2])} fresh, ${tokens(s.tokens[3])} generated)`,
165
+ ...paced,
166
+ ...given.length ? [
167
+ '',
168
+ "Shares of each plan's week (a topic's sessions and all they started):",
169
+ ...given.map((u)=>`${' '.repeat(u.depth + 1)}${said(u, name.get(u.topic ?? '') ?? u.topic)}`)
170
+ ] : [],
171
+ ...Object.keys(s.plans).length ? [
172
+ '',
173
+ 'Accounts:',
174
+ ...ranked(Object.fromEntries(Object.entries(s.plans).map(([k, p])=>[
175
+ k,
176
+ [
177
+ p.dollars
178
+ ]
179
+ ])), 0).map(([k, [d]])=>` ${dollars(d).padStart(10)} ${(rate.has(k) ? percent(d * rate.get(k)) : 'no reading').padStart(7)} ${accounts.get(k) ?? k}`)
180
+ ] : [],
181
+ '',
182
+ 'Parts of the context:',
183
+ ...pick(s.parts, 4).map(([k, ns])=>line(k, ns, 4, sum(ns))),
184
+ '',
185
+ 'Sessions:',
186
+ ...sessions.map(([k, ns])=>line(k, ns, 1, ns[0])),
187
+ '',
188
+ 'Tasks (a session split across the tasks it worked on):',
189
+ ...tasks.map(([k, ns])=>line(k, ns, 1, ns[0])),
190
+ '',
191
+ 'Models:',
192
+ ...models.map(([k, ns])=>line(k, ns, 4, sum(ns))),
193
+ '',
194
+ 'Entries carried most:',
195
+ ...entries.map(([k, ns])=>line(k, ns, 1, ns[0]))
196
+ ].join('\n');
197
+ };
198
+ /** Read in each transcript written since `since` that the graph has not
199
+ * read to its end, measure what was written since then again, then tally
200
+ * each day. What `person` typed is signed with them. */ export let backfill = async (g, since, now = Date.now(), person)=>{
201
+ let session = await import("@yaks/session/service");
202
+ let spawn = await import("@yaks/spawn");
203
+ let read = await session.recounted(g, since, {
204
+ person,
205
+ now
206
+ });
207
+ let runs = await spawn.recounted(g, since);
208
+ return `measured ${read} requests from transcripts and the graph's ` + `sessions and ${runs} from managed runs; ${await tallied(g, since, now)}`;
209
+ };
210
+ /** Tally each day from `since` to today again from the requests the graph
211
+ * holds, measuring nothing: what a changed price or plan needs to show
212
+ * before the duty's next pass. Says what it tallied. */ export let tallied = async (g, since, now = Date.now())=>{
213
+ let days = [];
214
+ for(let d = since; d <= dayOf(now); d = after(d)){
215
+ days.push(await recompute(g, d));
216
+ }
217
+ let all = merged(days);
218
+ return `tallied ${days.length} days from ${since}: ${all.requests} ` + `requests, ${dollars(all.dollars)}`;
219
+ };
220
+ /** The tools this package runs; a transcript read in is signed with the
221
+ * config's `person`. */ export let runs = (host)=>({
222
+ spend_show: async (call, graph)=>{
223
+ let a = argsOf(call);
224
+ return answer(call, await report(graph, {
225
+ since: dayArg(a.since),
226
+ until: dayArg(a.until),
227
+ top: a.top == null ? undefined : Number(a.top),
228
+ plans: a.plans === true,
229
+ done: a.done === true
230
+ }));
231
+ },
232
+ spend_tally: async (call, graph)=>answer(call, await tallied(graph, dayArg(argsOf(call).since) ?? dayOf(Date.now()))),
233
+ spend_backfill: async (call, graph)=>{
234
+ let since = dayArg(argsOf(call).since);
235
+ if (!since) throw new Refused('say since, the first UTC day to measure');
236
+ let said = host?.config?.person;
237
+ let person = said && ((await graph.address([
238
+ said
239
+ ])).get(said) ?? said);
240
+ return answer(call, await backfill(graph, since, Date.now(), person || undefined));
241
+ }
242
+ });
243
+ //# sourceMappingURL=tools.js.map
package/tools.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.js","sources":["./tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;CAQC,GAED,SAAS,IAAI,2BAAgC;AAC7C,SAAS,MAAM,gCAAqC;AACpD,SAAS,OAAO,kCAAuC;AAEvD,SAAS,KAAK,wBAA6B;AAC3C,SAAS,MAAM,iCAAsC;AACrD,SAAS,OAAO,EAAE,SAAS,mBAAkB;AAC7C,SAAS,IAAI,EAAE,MAAM,EAAE,MAAM,EAAc,OAAO,qBAAoB;AACtE,SAAoB,IAAI,EAAY,IAAI,mBAAkB;AAC1D,SACE,YAAY,EACZ,KAAK,EACL,OAAO,EACP,KAAK,EAEL,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,KAAK,EACL,IAAI,EACJ,MAAM,sBACY;AACpB,SAAS,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAoB;AAI5D,IAAI,SAAS,CAAC,MAAc,OAA2B;IAAC;MACtD,QAAQ;QAAE,KAAK;MAAQ;MACvB,SAAS;QAAE;MAAK;MAChB,QAAQ;QAAE,QAAQ,KAAK,MAAM,CAAC,GAAG;MAAC;IACpC;GAAE;AAEF,yDAAyD;AACzD,IAAI,SAAS,CAAC,IAAmC,IAAI,OAAO,KAAK;AA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