@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/tools.ts ADDED
@@ -0,0 +1,357 @@
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
+ */
10
+ import type { Bundle, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
11
+ import { line } from '@yaks/query'
12
+ import { argsOf } from '@yaks/graph/call'
13
+ import { Refused } from '@yaks/graph/errors'
14
+ import type { Runs } from 'jsr:@yaks/graph@0.2.36/tools'
15
+ import { human } from '@yaks/id'
16
+ import { shares } from '@yaks/topic/share'
17
+ import { lineage, recompute } from './day.js'
18
+ import { done, merged, ranked, type Spend, spendOf } from './tally.js'
19
+ import { type Line, said, type Use, uses } from './use.js'
20
+ import {
21
+ accountNames,
22
+ after,
23
+ current,
24
+ dayOf,
25
+ type Limit,
26
+ limitsOf,
27
+ midnight,
28
+ mondayOf,
29
+ paceOf,
30
+ rates,
31
+ span,
32
+ weekOf,
33
+ } from './window.js'
34
+ import { dollars, label, percent, tokens } from './words.js'
35
+
36
+ export type { Billed, Use } from './use.js'
37
+
38
+ let answer = (call: Bundle, body: string): Bundle[] => [{
39
+ entity: { eid: '$said' },
40
+ content: { body },
41
+ output: { source: call.entity.eid },
42
+ }]
43
+
44
+ // A day the door already read (`format: date`), or none.
45
+ let dayArg = (v: unknown): string | undefined => v ? String(v) : undefined
46
+
47
+ // The tally of a span of days, and the plan limits beside it.
48
+ export let read = async (
49
+ g: Graph,
50
+ since?: string,
51
+ until?: string,
52
+ now: number = Date.now(),
53
+ ): Promise<
54
+ {
55
+ spend: Spend
56
+ limits: Limit[]
57
+ rate: Map<Eid, number>
58
+ accounts: Map<Eid, string>
59
+ from: string
60
+ to: string
61
+ }
62
+ > => {
63
+ let { limits, accounts } = await plans(g)
64
+ let [from, to] = span(limits, now, since, until)
65
+ // A week's rates are read from the days its week began, which can be
66
+ // before the span.
67
+ let all = await tallies(g, after(from, -7), to)
68
+ let days = all.filter((d) => d.day >= from)
69
+ return {
70
+ spend: merged(days),
71
+ limits,
72
+ rate: rates(limits, all, now),
73
+ accounts,
74
+ from,
75
+ to,
76
+ }
77
+ }
78
+
79
+ // The plan limits, and what each account is called.
80
+ let plans = async (g: Graph) => {
81
+ let providers = await names(g, 'provider')
82
+ let accounts = accountNames(await g.read('.account *'), providers)
83
+ let limits = limitsOf(
84
+ await g.read('.rate_limit *'),
85
+ new Map([...await names(g, 'model'), ...providers, ...accounts]),
86
+ )
87
+ return { limits, accounts }
88
+ }
89
+
90
+ // The tallies of the days from one to another, in order.
91
+ let tallies = async (g: Graph, from: string, to: string): Promise<Spend[]> =>
92
+ (await g.read(
93
+ line`.spend .spend.day>=${from} .spend.day<=${to} *`,
94
+ )).map(spendOf).filter((s): s is Spend => !!s)
95
+ .sort((a, b) => a.day < b.day ? -1 : 1)
96
+
97
+ /** Each topic's share of the week against what its sessions, and every
98
+ * session they started, used of each plan's running week: a topic before the
99
+ * topics below it, then the work no share covers ({@link uses}). Read from
100
+ * the tallies at `now`; where no topic has a share, nothing is read. */
101
+ export let shared = async (
102
+ g: Graph,
103
+ now: number = Date.now(),
104
+ ): Promise<Use[]> => {
105
+ let given = await shares(g)
106
+ if (!given.share.size) return []
107
+ let { limits } = await plans(g)
108
+ let from = limits.flatMap((l) => weekOf(l, now) ?? []).sort()[0]
109
+ let days = from ? await tallies(g, from, dayOf(now)) : []
110
+ let sessions = days.flatMap((d) =>
111
+ Object.values(d.plans).flatMap((p) => Object.keys(p.sessions))
112
+ )
113
+ let rows = await lineage(g, sessions, ['session'])
114
+ let ref = (b: Bundle, comp: string, prop: string): Eid | undefined => {
115
+ let v = (b[comp] as Record<string, unknown> | undefined)?.[prop]
116
+ return v ? String(v) : undefined
117
+ }
118
+ let lines = new Map<Eid, Line>(
119
+ [...rows].map(([eid, b]) => [eid, {
120
+ topic: ref(b, 'session', 'topic'),
121
+ parent: ref(b, 'spawned', 'parent'),
122
+ }]),
123
+ )
124
+ return uses({ shares: given, lines, limits, days, now })
125
+ }
126
+
127
+ // Each row of a kind, by eid, named.
128
+ let names = async (g: Graph, comp: string): Promise<Map<Eid, string>> =>
129
+ new Map(
130
+ (await g.read(line`.${comp} *`)).map((b) => [
131
+ b.entity.eid,
132
+ String((b[comp] as { name?: unknown }).name ?? b.entity.eid),
133
+ ]),
134
+ )
135
+
136
+ // The rows a report names, read once.
137
+ let named = async (g: Graph, eids: Eid[]) => {
138
+ let id = human(g.vocab)
139
+ let rows = await g.get([...new Set(eids)])
140
+ return new Map(
141
+ rows.filter((r) => r).map((r) => [r!.entity.eid, label(r!, id(r!))]),
142
+ )
143
+ }
144
+
145
+ /** The cost per completed task in each UTC week (Monday to Sunday) from the
146
+ * one `since` falls in to `until`'s: the dollars every tally to `until`
147
+ * charged the tasks completed that week, over those of them charged any, as
148
+ * cash and as usage at list price, never one total. Decisions are not counted.
149
+ * The charge is the tally's `tasks` split, an estimate. */
150
+ export let perTask = async (
151
+ g: Graph,
152
+ since: string,
153
+ until: string,
154
+ ): Promise<string> => {
155
+ let from = mondayOf(since)
156
+ let days = (await g.read(line`.spend .spend.day<=${until} *`))
157
+ .map(spendOf).filter((s): s is Spend => !!s)
158
+ let finished = (await g.read(
159
+ line`.task !decision .completed .completed.at>=${
160
+ midnight(from)
161
+ } .completed.at<${midnight(after(until))} .fields=completed.at`,
162
+ )).map((b): [Eid, string] => [
163
+ b.entity.eid,
164
+ mondayOf(dayOf(String((b.completed as { at: string }).at))),
165
+ ])
166
+ let each = (n: number, of: number) => dollars(of ? n / of : 0)
167
+ return [
168
+ 'Cost per completed task (the tasks split, an estimate: each session ' +
169
+ 'divided evenly across the tasks it worked on; a task charged nothing ' +
170
+ 'is left out of the divisor):',
171
+ ...done(merged(days), finished).map((w) => {
172
+ let paid = w.tasks - w.unpaid
173
+ return ` week of ${w.week}${
174
+ after(w.week, 6) > until ? ` (to ${until})` : ''
175
+ }: ${each(w.cash, paid)} cash and ${
176
+ each(w.usage, paid)
177
+ } usage at list price a task, over ${paid} of ${w.tasks} completed (${w.unpaid} charged nothing); in all ${
178
+ dollars(w.cash)
179
+ } cash, ${dollars(w.usage)} usage`
180
+ }),
181
+ ].join('\n')
182
+ }
183
+
184
+ /** A span's report, as text. */
185
+ export let report = async (
186
+ g: Graph,
187
+ o: {
188
+ since?: string
189
+ until?: string
190
+ top?: number
191
+ now?: number
192
+ plans?: boolean
193
+ done?: boolean
194
+ } = {},
195
+ ): Promise<string> => {
196
+ if (o.done) {
197
+ let { limits } = await plans(g)
198
+ let [from, to] = span(limits, o.now ?? Date.now(), o.since, o.until)
199
+ return perTask(g, from, to)
200
+ }
201
+ let { spend: s, limits, rate, accounts, from, to } = await read(
202
+ g,
203
+ o.since,
204
+ o.until,
205
+ o.now,
206
+ )
207
+ let now = o.now ?? Date.now()
208
+ let paced = limits.filter((l) => current(l, now)).map((l) => {
209
+ let pace = paceOf(l, now)
210
+ return `${l.name} ${l.window == 10080 ? 'week' : 'five hours'}: ${
211
+ Math.round(l.used)
212
+ }% used, ${Math.round(pace * 100)}% of it gone${
213
+ l.resets ? `, resets ${l.resets.slice(0, 16).replace('T', ' ')} UTC` : ''
214
+ }`
215
+ })
216
+ if (o.plans) return paced.join('\n') || 'No plan limit has been read.'
217
+ let given = await shared(g, now)
218
+ let top = o.top ?? 10
219
+ let pick = (m: Record<string, number[]>, at: number) =>
220
+ ranked(m, at).slice(0, top)
221
+ let sessions = pick(s.sessions, 1), tasks = pick(s.tasks, 1)
222
+ let models = pick(s.models, 4), entries = pick(s.entries, 1)
223
+ let name = await named(
224
+ g,
225
+ [
226
+ ...sessions,
227
+ ...tasks,
228
+ ...models,
229
+ ...entries,
230
+ ].map(([k]) => k).concat(given.flatMap((u) => u.topic ?? [])),
231
+ )
232
+ let line = (k: string, ns: number[], at: number, toks: number) =>
233
+ ` ${dollars(ns[at]).padStart(10)} ${tokens(toks).padStart(7)} ${
234
+ name.get(k) ?? k
235
+ }`
236
+ let sum = (ns: number[]) => ns.slice(0, 4).reduce((a, b) => a + b, 0)
237
+ return [
238
+ `${from} to ${to}: ${dollars(s.dollars)}, ${s.requests} requests, ${
239
+ tokens(sum(s.tokens))
240
+ } tokens (${tokens(s.tokens[0])} read from cache, ${
241
+ tokens(s.tokens[1])
242
+ } written to it, ${tokens(s.tokens[2])} fresh, ${
243
+ tokens(s.tokens[3])
244
+ } generated)`,
245
+ ...paced,
246
+ ...given.length
247
+ ? [
248
+ '',
249
+ "Shares of each plan's week (a topic's sessions and all they started):",
250
+ ...given.map((u) =>
251
+ `${' '.repeat(u.depth + 1)}${
252
+ said(u, name.get(u.topic ?? '') ?? u.topic)
253
+ }`
254
+ ),
255
+ ]
256
+ : [],
257
+ ...Object.keys(s.plans).length
258
+ ? [
259
+ '',
260
+ 'Accounts:',
261
+ ...ranked(
262
+ Object.fromEntries(
263
+ Object.entries(s.plans).map(([k, p]) => [k, [p.dollars]]),
264
+ ),
265
+ 0,
266
+ ).map(([k, [d]]) =>
267
+ ` ${dollars(d).padStart(10)} ${
268
+ (rate.has(k) ? percent(d * rate.get(k)!) : 'no reading')
269
+ .padStart(7)
270
+ } ${accounts.get(k) ?? k}`
271
+ ),
272
+ ]
273
+ : [],
274
+ '',
275
+ 'Parts of the context:',
276
+ ...pick(s.parts, 4).map(([k, ns]) => line(k, ns, 4, sum(ns))),
277
+ '',
278
+ 'Sessions:',
279
+ ...sessions.map(([k, ns]) => line(k, ns, 1, ns[0])),
280
+ '',
281
+ 'Tasks (a session split across the tasks it worked on):',
282
+ ...tasks.map(([k, ns]) => line(k, ns, 1, ns[0])),
283
+ '',
284
+ 'Models:',
285
+ ...models.map(([k, ns]) => line(k, ns, 4, sum(ns))),
286
+ '',
287
+ 'Entries carried most:',
288
+ ...entries.map(([k, ns]) => line(k, ns, 1, ns[0])),
289
+ ].join('\n')
290
+ }
291
+
292
+ /** Read in each transcript written since `since` that the graph has not
293
+ * read to its end, measure what was written since then again, then tally
294
+ * each day. What `person` typed is signed with them. */
295
+ export let backfill = async (
296
+ g: Graph,
297
+ since: string,
298
+ now: number = Date.now(),
299
+ person?: Eid,
300
+ ): Promise<string> => {
301
+ let session = await import('@yaks/session/service')
302
+ let spawn = await import('@yaks/spawn')
303
+ let read = await session.recounted(g, since, { person, now })
304
+ let runs = await spawn.recounted(g, since)
305
+ return `measured ${read} requests from transcripts and the graph's ` +
306
+ `sessions and ${runs} from managed runs; ${await tallied(g, since, now)}`
307
+ }
308
+
309
+ /** Tally each day from `since` to today again from the requests the graph
310
+ * holds, measuring nothing: what a changed price or plan needs to show
311
+ * before the duty's next pass. Says what it tallied. */
312
+ export let tallied = async (
313
+ g: Graph,
314
+ since: string,
315
+ now: number = Date.now(),
316
+ ): Promise<string> => {
317
+ let days: Spend[] = []
318
+ for (let d = since; d <= dayOf(now); d = after(d)) {
319
+ days.push(await recompute(g, d))
320
+ }
321
+ let all = merged(days)
322
+ return `tallied ${days.length} days from ${since}: ${all.requests} ` +
323
+ `requests, ${dollars(all.dollars)}`
324
+ }
325
+
326
+ /** The tools this package runs; a transcript read in is signed with the
327
+ * config's `person`. */
328
+ export let runs = (host?: { config?: { person?: string } }): Runs => ({
329
+ spend_show: async (call, graph) => {
330
+ let a = argsOf(call)
331
+ return answer(
332
+ call,
333
+ await report(graph, {
334
+ since: dayArg(a.since),
335
+ until: dayArg(a.until),
336
+ top: a.top == null ? undefined : Number(a.top),
337
+ plans: a.plans === true,
338
+ done: a.done === true,
339
+ }),
340
+ )
341
+ },
342
+ spend_tally: async (call, graph) =>
343
+ answer(
344
+ call,
345
+ await tallied(graph, dayArg(argsOf(call).since) ?? dayOf(Date.now())),
346
+ ),
347
+ spend_backfill: async (call, graph) => {
348
+ let since = dayArg(argsOf(call).since)
349
+ if (!since) throw new Refused('say since, the first UTC day to measure')
350
+ let said = host?.config?.person
351
+ let person = said && ((await graph.address([said])).get(said) ?? said)
352
+ return answer(
353
+ call,
354
+ await backfill(graph, since, Date.now(), person || undefined),
355
+ )
356
+ },
357
+ })
package/use.js ADDED
@@ -0,0 +1,172 @@
1
+ /**
2
+ * How much of each topic's share of the week is used: pure, over the shares
3
+ * (@yaks/topic/share), the sessions' lines and the day tallies. A session's
4
+ * dollars are charged to the topic it was opened on, else to the one the
5
+ * session that started it is charged to, and so on up; a topic with no share
6
+ * passes them to the nearest topic above it that has one. On each plan they
7
+ * are read over the plan's running week and turned into a part of that week
8
+ * by the plan's rate ({@link rates}), beside how much of the week is gone.
9
+ * Nothing here is stored: a share is the one number kept.
10
+ *
11
+ * @module
12
+ */ import { above, holder, kept } from "@yaks/topic/share";
13
+ import { billedOn, limitRates, paceOf, weekOf } from "./window.js";
14
+ import { dollars, percent } from "./words.js";
15
+ /**
16
+ * The topic a session's spend is charged to: the share covering the topic it
17
+ * was opened on, else the one its parent is charged to, up its line.
18
+ *
19
+ * ```ts
20
+ * import { charged } from './use.ts'
21
+ * import { equal } from '@yaks/testing'
22
+ *
23
+ * let shares = { share: new Map([['funnel', 0.1]]), up: new Map([['signup', ['funnel']]]) }
24
+ * let to = charged(shares, new Map([
25
+ * ['head', { topic: 'funnel' }],
26
+ * ['oracle', { topic: 'signup' }],
27
+ * ['helper', { parent: 'worker' }],
28
+ * ['worker', { parent: 'head' }],
29
+ * ['other', {}],
30
+ * ]))
31
+ * equal(['head', 'oracle', 'helper', 'other'].map(to), ['funnel', 'funnel', 'funnel', undefined])
32
+ * ```
33
+ */ export let charged = (shares, lines)=>(session)=>{
34
+ let seen = new Set();
35
+ for(let at = session; at && !seen.has(at); at = lines.get(at)?.parent){
36
+ seen.add(at);
37
+ let topic = lines.get(at)?.topic;
38
+ let to = topic && holder(shares, topic);
39
+ if (to) return to;
40
+ }
41
+ };
42
+ /**
43
+ * Each topic's share against what was used of it on each weekly limit, a
44
+ * topic before the topics below it, then the work no share covers. A limit
45
+ * on one model counts that model's requests alone, at its own rate. No
46
+ * share, no rows.
47
+ *
48
+ * ```ts
49
+ * import { uses } from './use.ts'
50
+ * import { empty } from './tally.ts'
51
+ * import { equal } from '@yaks/testing'
52
+ *
53
+ * let week = { provider: 'claude', of: 'c', name: 'claude', window: 10080, used: 30,
54
+ * resets: '2026-10-27T12:00:00Z', at: '2026-10-21T12:00:00Z' }
55
+ * let sessions = { head: 100, helper: 50, other: 150 }
56
+ * let day = { ...empty('2026-10-21'), plans: { c: { dollars: 300, parts: {}, sessions, tasks: {},
57
+ * models: { f: 50 }, metered: { f: { head: 10, other: 40 } }, entries: {} } } }
58
+ * let asked = (limits: typeof week[]) => uses({
59
+ * shares: { share: new Map([['funnel', 0.25]]), up: new Map() },
60
+ * lines: new Map([['head', { topic: 'funnel' }], ['helper', { parent: 'head' }]]),
61
+ * limits,
62
+ * days: [day],
63
+ * now: Date.parse('2026-10-24T00:00:00Z'),
64
+ * })
65
+ * let plan = { plan: 'c', name: 'claude', gone: 0.5 }
66
+ * equal(asked([week]), [
67
+ * { topic: 'funnel', share: 0.25, keeps: 0.25, depth: 0, plans: [{ ...plan, dollars: 150, used: 0.15 }] },
68
+ * { share: 0.75, keeps: 0.75, depth: 0, plans: [{ ...plan, dollars: 150, used: 0.15 }] },
69
+ * ])
70
+ * // The plan's limit on one model is a limit of its own: that model's dollars, at its rate.
71
+ * let cap = { ...week, name: 'fable', model: 'f', used: 100 }
72
+ * equal(asked([week, cap])[0].plans, [
73
+ * { ...plan, dollars: 150, used: 0.15 },
74
+ * { ...plan, name: 'fable', model: 'f', dollars: 10, used: 0.2 },
75
+ * ])
76
+ * ```
77
+ */ export let uses = (o)=>{
78
+ let { shares, limits, days, now } = o;
79
+ if (!shares.share.size) return [];
80
+ let to = charged(shares, o.lines);
81
+ let rate = limitRates(limits, days, now);
82
+ // Each limit's week, with its dollars by the topic they are charged to.
83
+ let weeks = limits.flatMap((l)=>{
84
+ let from = weekOf(l, now);
85
+ if (!from) return [];
86
+ let by = new Map();
87
+ for (let d of days){
88
+ if (d.day < from) continue;
89
+ for (let [session, n] of Object.entries(billedOn(l, d).sessions)){
90
+ by.set(to(session), (by.get(to(session)) ?? 0) + n);
91
+ }
92
+ }
93
+ return [
94
+ {
95
+ l,
96
+ by
97
+ }
98
+ ];
99
+ });
100
+ let billed = (topic)=>weeks.map(({ l, by })=>{
101
+ let n = by.get(topic) ?? 0;
102
+ return {
103
+ plan: l.of,
104
+ ...l.model ? {
105
+ model: l.model
106
+ } : {},
107
+ name: l.name,
108
+ dollars: n,
109
+ ...rate.has(l) ? {
110
+ used: n * rate.get(l) / 100
111
+ } : {},
112
+ gone: paceOf(l, now)
113
+ };
114
+ });
115
+ let keeps = kept(shares);
116
+ let share = (t)=>shares.share.get(t);
117
+ let seen = new Set();
118
+ let under = (up, depth)=>[
119
+ ...shares.share.keys()
120
+ ].filter((t)=>!seen.has(t) && above(shares, t) == up).sort((a, b)=>share(b) - share(a) || (a < b ? -1 : 1)).flatMap((t)=>{
121
+ seen.add(t);
122
+ let row = {
123
+ topic: t,
124
+ share: share(t),
125
+ keeps: keeps.get(t),
126
+ depth,
127
+ plans: billed(t)
128
+ };
129
+ return [
130
+ row,
131
+ ...under(t, depth + 1)
132
+ ];
133
+ });
134
+ let tree = under(undefined, 0);
135
+ let free = 1 - tree.filter((u)=>!u.depth).reduce((n, u)=>n + u.share, 0);
136
+ return [
137
+ ...tree,
138
+ {
139
+ share: free,
140
+ keeps: free,
141
+ depth: 0,
142
+ plans: billed()
143
+ }
144
+ ];
145
+ };
146
+ // A part of a week, as a percent to a tenth.
147
+ let part = (n)=>`${Number((n * 100).toFixed(1))}%`;
148
+ /**
149
+ * A share against its use, as one line that stands alone: `name` is what the
150
+ * topic is called. Every dollar figure says it is usage at list price.
151
+ *
152
+ * ```ts
153
+ * import { said } from './use.ts'
154
+ * import { equal } from '@yaks/testing'
155
+ *
156
+ * let plans = [
157
+ * { plan: 'c', name: 'claude', dollars: 34.1, used: 0.012, gone: 0.3 },
158
+ * { plan: 'x', name: 'codex', dollars: 2, gone: 0.55 },
159
+ * ]
160
+ * equal(said({ topic: 'q', share: 0.4, keeps: 0.25, depth: 0, plans }, 'Q-12 Pilots'),
161
+ * "Q-12 Pilots keeps 25% of each plan's week (its 40% less 15% given below): " +
162
+ * 'claude 1.2% used ($34.10 of usage at list price), 30% of its week gone; ' +
163
+ * 'codex has no reading of this week ($2.00 of usage at list price), 55% of its week gone')
164
+ * equal(said({ share: 0.15, keeps: 0.15, depth: 0, plans: [] }),
165
+ * "Under no share (15% of each plan's week is not allotted): no plan's week has been read")
166
+ * ```
167
+ */ export let said = (u, name = '')=>{
168
+ let head = !u.topic ? `Under no share (${part(u.share)} of each plan's week is not allotted)` : u.keeps == u.share ? `${name} has ${part(u.share)} of each plan's week` : `${name} keeps ${part(u.keeps)} of each plan's week (its ${part(u.share)} less ${part(u.share - u.keeps)} given below)`;
169
+ let plans = u.plans.map((p)=>`${p.name} ${p.used == null ? 'has no reading of this week' : `${percent(p.used * 100)} used`} (${dollars(p.dollars)} of usage at list price), ${Math.round(p.gone * 100)}% of its week gone`);
170
+ return `${head}: ${plans.join('; ') || "no plan's week has been read"}`;
171
+ };
172
+ //# sourceMappingURL=use.js.map
package/use.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use.js","sources":["./use.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;CAWC,GAED,SAAS,KAAK,EAAE,MAAM,EAAE,IAAI,iC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