@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/use.ts ADDED
@@ -0,0 +1,226 @@
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
+ */
13
+ import type { Eid } from 'jsr:@yaks/graph@0.2.36'
14
+ import { above, holder, kept, type Shares } from '@yaks/topic/share'
15
+ import type { Spend } from './tally.ts'
16
+ import { billedOn, limitRates, paceOf, weekOf } from './window.js'
17
+ import type { Limit } from './window.js'
18
+ import { dollars, percent } from './words.js'
19
+
20
+ /** What says where a session's spend is charged: the topic it was opened
21
+ * on, and the session that started it. */
22
+ export type Line = { topic?: Eid; parent?: Eid }
23
+
24
+ /** What some work ran up against one weekly limit in its running week: a
25
+ * plan's, or a plan's limit on one model. */
26
+ export type Billed = {
27
+ plan: Eid
28
+ /** the model the limit meters, where it is a limit on one model */
29
+ model?: Eid
30
+ /** what the limit is called */
31
+ name: string
32
+ /** usage at list price, not cash: the plan is a subscription */
33
+ dollars: number
34
+ /** those dollars as a part of the limit's week, 0 to 1, where the limit has
35
+ * a reading of this week */
36
+ used?: number
37
+ /** how much of the limit's week is gone, 0 to 1 */
38
+ gone: number
39
+ }
40
+
41
+ /** A topic's share against its use; with no `topic`, the work no share
42
+ * covers against the part of the week no topic was given. */
43
+ export type Use = {
44
+ topic?: Eid
45
+ /** its share, 0 to 1 */
46
+ share: number
47
+ /** its share less the shares below it */
48
+ keeps: number
49
+ /** how many shares it sits under */
50
+ depth: number
51
+ plans: Billed[]
52
+ }
53
+
54
+ /**
55
+ * The topic a session's spend is charged to: the share covering the topic it
56
+ * was opened on, else the one its parent is charged to, up its line.
57
+ *
58
+ * ```ts
59
+ * import { charged } from './use.ts'
60
+ * import { equal } from '@yaks/testing'
61
+ *
62
+ * let shares = { share: new Map([['funnel', 0.1]]), up: new Map([['signup', ['funnel']]]) }
63
+ * let to = charged(shares, new Map([
64
+ * ['head', { topic: 'funnel' }],
65
+ * ['oracle', { topic: 'signup' }],
66
+ * ['helper', { parent: 'worker' }],
67
+ * ['worker', { parent: 'head' }],
68
+ * ['other', {}],
69
+ * ]))
70
+ * equal(['head', 'oracle', 'helper', 'other'].map(to), ['funnel', 'funnel', 'funnel', undefined])
71
+ * ```
72
+ */
73
+ export let charged = (shares: Shares, lines: Map<Eid, Line>) =>
74
+ (
75
+ session: Eid,
76
+ ): Eid | undefined => {
77
+ let seen = new Set<Eid>()
78
+ for (
79
+ let at: Eid | undefined = session;
80
+ at && !seen.has(at);
81
+ at = lines.get(at)?.parent
82
+ ) {
83
+ seen.add(at)
84
+ let topic = lines.get(at)?.topic
85
+ let to = topic && holder(shares, topic)
86
+ if (to) return to
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Each topic's share against what was used of it on each weekly limit, a
92
+ * topic before the topics below it, then the work no share covers. A limit
93
+ * on one model counts that model's requests alone, at its own rate. No
94
+ * share, no rows.
95
+ *
96
+ * ```ts
97
+ * import { uses } from './use.ts'
98
+ * import { empty } from './tally.ts'
99
+ * import { equal } from '@yaks/testing'
100
+ *
101
+ * let week = { provider: 'claude', of: 'c', name: 'claude', window: 10080, used: 30,
102
+ * resets: '2026-10-27T12:00:00Z', at: '2026-10-21T12:00:00Z' }
103
+ * let sessions = { head: 100, helper: 50, other: 150 }
104
+ * let day = { ...empty('2026-10-21'), plans: { c: { dollars: 300, parts: {}, sessions, tasks: {},
105
+ * models: { f: 50 }, metered: { f: { head: 10, other: 40 } }, entries: {} } } }
106
+ * let asked = (limits: typeof week[]) => uses({
107
+ * shares: { share: new Map([['funnel', 0.25]]), up: new Map() },
108
+ * lines: new Map([['head', { topic: 'funnel' }], ['helper', { parent: 'head' }]]),
109
+ * limits,
110
+ * days: [day],
111
+ * now: Date.parse('2026-10-24T00:00:00Z'),
112
+ * })
113
+ * let plan = { plan: 'c', name: 'claude', gone: 0.5 }
114
+ * equal(asked([week]), [
115
+ * { topic: 'funnel', share: 0.25, keeps: 0.25, depth: 0, plans: [{ ...plan, dollars: 150, used: 0.15 }] },
116
+ * { share: 0.75, keeps: 0.75, depth: 0, plans: [{ ...plan, dollars: 150, used: 0.15 }] },
117
+ * ])
118
+ * // The plan's limit on one model is a limit of its own: that model's dollars, at its rate.
119
+ * let cap = { ...week, name: 'fable', model: 'f', used: 100 }
120
+ * equal(asked([week, cap])[0].plans, [
121
+ * { ...plan, dollars: 150, used: 0.15 },
122
+ * { ...plan, name: 'fable', model: 'f', dollars: 10, used: 0.2 },
123
+ * ])
124
+ * ```
125
+ */
126
+ export let uses = (o: {
127
+ shares: Shares
128
+ lines: Map<Eid, Line>
129
+ limits: Limit[]
130
+ days: Spend[]
131
+ now: number
132
+ }): Use[] => {
133
+ let { shares, limits, days, now } = o
134
+ if (!shares.share.size) return []
135
+ let to = charged(shares, o.lines)
136
+ let rate = limitRates(limits, days, now)
137
+ // Each limit's week, with its dollars by the topic they are charged to.
138
+ let weeks = limits.flatMap((l) => {
139
+ let from = weekOf(l, now)
140
+ if (!from) return []
141
+ let by = new Map<Eid | undefined, number>()
142
+ for (let d of days) {
143
+ if (d.day < from) continue
144
+ for (let [session, n] of Object.entries(billedOn(l, d).sessions)) {
145
+ by.set(to(session), (by.get(to(session)) ?? 0) + n)
146
+ }
147
+ }
148
+ return [{ l, by }]
149
+ })
150
+ let billed = (topic?: Eid): Billed[] =>
151
+ weeks.map(({ l, by }) => {
152
+ let n = by.get(topic) ?? 0
153
+ return {
154
+ plan: l.of,
155
+ ...l.model ? { model: l.model } : {},
156
+ name: l.name,
157
+ dollars: n,
158
+ ...rate.has(l) ? { used: n * rate.get(l)! / 100 } : {},
159
+ gone: paceOf(l, now),
160
+ }
161
+ })
162
+ let keeps = kept(shares)
163
+ let share = (t: Eid) => shares.share.get(t)!
164
+ let seen = new Set<Eid>()
165
+ let under = (up: Eid | undefined, depth: number): Use[] =>
166
+ [...shares.share.keys()]
167
+ .filter((t) => !seen.has(t) && above(shares, t) == up)
168
+ .sort((a, b) => share(b) - share(a) || (a < b ? -1 : 1))
169
+ .flatMap((t) => {
170
+ seen.add(t)
171
+ let row = {
172
+ topic: t,
173
+ share: share(t),
174
+ keeps: keeps.get(t)!,
175
+ depth,
176
+ plans: billed(t),
177
+ }
178
+ return [row, ...under(t, depth + 1)]
179
+ })
180
+ let tree = under(undefined, 0)
181
+ let free = 1 - tree.filter((u) => !u.depth).reduce((n, u) => n + u.share, 0)
182
+ return [...tree, { share: free, keeps: free, depth: 0, plans: billed() }]
183
+ }
184
+
185
+ // A part of a week, as a percent to a tenth.
186
+ let part = (n: number) => `${Number((n * 100).toFixed(1))}%`
187
+
188
+ /**
189
+ * A share against its use, as one line that stands alone: `name` is what the
190
+ * topic is called. Every dollar figure says it is usage at list price.
191
+ *
192
+ * ```ts
193
+ * import { said } from './use.ts'
194
+ * import { equal } from '@yaks/testing'
195
+ *
196
+ * let plans = [
197
+ * { plan: 'c', name: 'claude', dollars: 34.1, used: 0.012, gone: 0.3 },
198
+ * { plan: 'x', name: 'codex', dollars: 2, gone: 0.55 },
199
+ * ]
200
+ * equal(said({ topic: 'q', share: 0.4, keeps: 0.25, depth: 0, plans }, 'Q-12 Pilots'),
201
+ * "Q-12 Pilots keeps 25% of each plan's week (its 40% less 15% given below): " +
202
+ * 'claude 1.2% used ($34.10 of usage at list price), 30% of its week gone; ' +
203
+ * 'codex has no reading of this week ($2.00 of usage at list price), 55% of its week gone')
204
+ * equal(said({ share: 0.15, keeps: 0.15, depth: 0, plans: [] }),
205
+ * "Under no share (15% of each plan's week is not allotted): no plan's week has been read")
206
+ * ```
207
+ */
208
+ export let said = (u: Use, name = ''): string => {
209
+ let head = !u.topic
210
+ ? `Under no share (${part(u.share)} of each plan's week is not allotted)`
211
+ : u.keeps == u.share
212
+ ? `${name} has ${part(u.share)} of each plan's week`
213
+ : `${name} keeps ${part(u.keeps)} of each plan's week (its ${
214
+ part(u.share)
215
+ } less ${part(u.share - u.keeps)} given below)`
216
+ let plans = u.plans.map((p) =>
217
+ `${p.name} ${
218
+ p.used == null
219
+ ? 'has no reading of this week'
220
+ : `${percent(p.used * 100)} used`
221
+ } (${dollars(p.dollars)} of usage at list price), ${
222
+ Math.round(p.gone * 100)
223
+ }% of its week gone`
224
+ )
225
+ return `${head}: ${plans.join('; ') || "no plan's week has been read"}`
226
+ }
package/views.js ADDED
@@ -0,0 +1,79 @@
1
+ /**
2
+ * How a day's tally reads wherever one is drawn by name: its title, a row in
3
+ * a list, a link. Portable renderers through the host's hyperscript, so a
4
+ * terminal lists the days a browser does. The page that ranks a span's
5
+ * tally, and a day's own page, are ./page.ts; the sidebar offers the page as
6
+ * `Spend`, over the list of days.
7
+ *
8
+ * @module
9
+ */ import { define } from "@yaks/render";
10
+ import { parse } from "@yaks/query/core";
11
+ // @ts-types="@yaks/ui/lucide"
12
+ import Coins from "lucide/dist/esm/icons/coins.mjs";
13
+ import { ranked, spendOf } from "./tally.js";
14
+ import { dollars, tokens } from "./words.js";
15
+ let title = (s)=>s ? `Spend on ${s.day}` : 'Spend';
16
+ /// facts({ day: '2026-10-20', requests: 3, dollars: 1.5, tokens: [9e6, 0, 1e6, 0], parts: { skills: [9e6, 0, 0, 0, 1], input: [0, 0, 1e6, 0, 0.5] }, sessions: {}, tasks: {}, models: {}, entries: {} })
17
+ /// -> ['$1.50', '3 requests', '10M tokens', 'most on skills']
18
+ /** What a day's row says in a line. */ export let facts = (s)=>{
19
+ let [top] = ranked(s.parts, 4);
20
+ return [
21
+ dollars(s.dollars),
22
+ `${s.requests.toLocaleString('en-US')} requests`,
23
+ `${tokens(s.tokens.reduce((a, b)=>a + b, 0))} tokens`,
24
+ ...top ? [
25
+ `most on ${top[0]}`
26
+ ] : []
27
+ ];
28
+ };
29
+ let tile = (e, h, ctx)=>{
30
+ let s = spendOf(e);
31
+ return h('a', {
32
+ class: 'Tile',
33
+ href: ctx.link?.(e.entity.eid)
34
+ }, h('span', {
35
+ class: 'Tile_Text'
36
+ }, h('span', {
37
+ class: 'Tile_Line'
38
+ }, h('span', {
39
+ class: 'Tile_Title'
40
+ }, s?.day ?? '')), ' ', h('span', {
41
+ class: 'Tile_Sub'
42
+ }, s ? facts(s).join(' · ') : '')));
43
+ };
44
+ /** Draws a day's tally by name. */ export let views = define([
45
+ {
46
+ view: 'Title',
47
+ match: parse('.spend'),
48
+ render: (e, h)=>h('span', null, title(spendOf(e)))
49
+ },
50
+ ...[
51
+ 'Tile',
52
+ 'List.Tile'
53
+ ].map((view)=>({
54
+ view,
55
+ match: parse('.spend'),
56
+ render: tile
57
+ })),
58
+ {
59
+ view: 'Inline',
60
+ match: parse('.spend'),
61
+ render: (e, h, ctx)=>h('a', {
62
+ href: ctx.link?.(e.entity.eid)
63
+ }, title(spendOf(e)))
64
+ }
65
+ ]);
66
+ /** The page and a day's page, loaded when first drawn. */ export let inspectViews = ()=>import("./page.js").then((m)=>m.inspectViews);
67
+ /** The page the sidebar offers: where the tokens went, over the days. */ export let destinations = [
68
+ {
69
+ key: 'spend',
70
+ name: 'Spend',
71
+ icon: 'coins',
72
+ query: '.spend * .order=-spend.day',
73
+ view: 'Spend.Page'
74
+ }
75
+ ];
76
+ /** The glyph it wears there. */ export let icons = {
77
+ coins: Coins
78
+ };
79
+ //# sourceMappingURL=views.js.map
package/views.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"views.js","sources":["./views.ts"],"names":[],"mappings":"AAAA;;;;;;;;CAQC,GACD,SAAS,MAAM,4BAA4E;AAC3F,SAAS,KAAK,gCAAqC;AAInD,8BAA8B;AAC9B,OAAO,6CAAuD;AAC9D,SAAS,MAAM,EAAc,OAAO,qBAAoB;AACxD,SAAS,OAAO,EAAE,MAAM,qBAAoB;AAI5C,IAAI,QAAQ,CAAC,IAAc,IAAI,CAAC,SAAS,EAAE,EAAE,GAAG,EAAE,GAAG;AAErD,yMAAyM;AACzM,gEAAgE;AAChE,qCAAqC,GACrC,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,CAAC,IAAI,GAAG,OAAO,EAAE,KAAK,EAAE;EAC5B,OAAO;IACL,QAAQ,EAAE,OAAO;IACjB,GAAG,EAAE,QAAQ,CAAC,cAAc,CAAC,SAAS,SAAS,CAAC;IAChD,GAAG,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,IAAM,IAAI,GAAG,IAAI,OAAO,CAAC;OACpD,MAAM;MAAC,CAAC,QAAQ,EAAE,GAAG,CAAC,EAAE,EAAE;KAAC,GAAG,EAAE;GACpC;AACH,EAAC;AAED,IAAI,OAAO,CAAO,GAAW,GAAY;EACvC,IAAI,IAAI,QAAQ;EAChB,OAAO,EACL,KACA;IAAE,OAAO;IAAQ,MAAM,AAAC,IAAkB,IAAI,GAAG,EAAE,MAAM,CAAC,GAAG;EAAE,GAC/D,EACE,QACA;IAAE,OAAO;EAAY,GACrB,EACE,QACA;IAAE,OAAO;EAAY,GACrB,EAAE,QAAQ;IAAE,OAAO;EAAa,GAAG,GAAG,OAAO,MAE/C,KACA,EAAE,QAAQ;IAAE,OAAO;EAAW,GAAG,IAAI,MAAM,GAAG,IAAI,CAAC,SAAS;AAGlE;AAEA,iCAAiC,GACjC,OAAO,IAAI,QAAkB,OAAO;EAClC;IACE,MAAM;IACN,OAAO,MAAM;IACb,QAAQ,CAAC,GAAG,IAAM,EAAE,QAAQ,MAAM,MAAM,QAAQ;EAClD;KACG;IAAC;IAAQ;GAAY,CAAC,GAAG,CAAC,CAAC,OAAS,CAAC;MACtC;MACA,OAAO,MAAM;MACb,QAAQ;IACV,CAAC;EACD;IACE,MAAM;IACN,OAAO,MAAM;IACb,QAAQ,CAAO,GAAW,GAAY,MACpC,EACE,KACA;QAAE,MAAM,AAAC,IAAkB,IAAI,GAAG,EAAE,MAAM,CAAC,GAAG;MAAE,GAChD,MAAM,QAAQ;EAEpB;CACD,EAAC;AAEF,wDAAwD,GACxD,OAAO,IAAI,eAAe,IACxB,MAAM,CAAC,aAAa,IAAI,CAAC,CAAC,IAAM,EAAE,YAAY,EAAC;AAEjD,uEAAuE,GACvE,OAAO,IAAI,eAAe;EACxB;IACE,KAAK;IACL,MAAM;IACN,MAAM;IACN,OAAO;IACP,MAAM;EACR;CACD,CAAA;AAED,8BAA8B,GAC9B,OAAO,IAAI,QAAQ;EAAE,OAAO;AAAM,EAAC"}
package/views.ts ADDED
@@ -0,0 +1,96 @@
1
+ /**
2
+ * How a day's tally reads wherever one is drawn by name: its title, a row in
3
+ * a list, a link. Portable renderers through the host's hyperscript, so a
4
+ * terminal lists the days a browser does. The page that ranks a span's
5
+ * tally, and a day's own page, are ./page.ts; the sidebar offers the page as
6
+ * `Spend`, over the list of days.
7
+ *
8
+ * @module
9
+ */
10
+ import { define, type H, type Registry, type RenderContext } from '@yaks/render'
11
+ import { parse } from '@yaks/query/core'
12
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
13
+ import type { View } from 'jsr:@yaks/inspect@0.2.36'
14
+ import type { Shown } from 'jsr:@yaks/render@0.2.36/views'
15
+ // @ts-types="@yaks/ui/lucide"
16
+ import Coins from 'lucide/dist/esm/icons/coins.mjs'
17
+ import { ranked, type Spend, spendOf } from './tally.js'
18
+ import { dollars, tokens } from './words.js'
19
+
20
+ type Ctx<Node> = RenderContext<Node> & Partial<Shown<Node>>
21
+
22
+ let title = (s?: Spend) => s ? `Spend on ${s.day}` : 'Spend'
23
+
24
+ /// facts({ day: '2026-10-20', requests: 3, dollars: 1.5, tokens: [9e6, 0, 1e6, 0], parts: { skills: [9e6, 0, 0, 0, 1], input: [0, 0, 1e6, 0, 0.5] }, sessions: {}, tasks: {}, models: {}, entries: {} })
25
+ /// -> ['$1.50', '3 requests', '10M tokens', 'most on skills']
26
+ /** What a day's row says in a line. */
27
+ export let facts = (s: Spend): string[] => {
28
+ let [top] = ranked(s.parts, 4)
29
+ return [
30
+ dollars(s.dollars),
31
+ `${s.requests.toLocaleString('en-US')} requests`,
32
+ `${tokens(s.tokens.reduce((a, b) => a + b, 0))} tokens`,
33
+ ...top ? [`most on ${top[0]}`] : [],
34
+ ]
35
+ }
36
+
37
+ let tile = <Node>(e: Bundle, h: H<Node>, ctx: RenderContext<Node>) => {
38
+ let s = spendOf(e)
39
+ return h(
40
+ 'a',
41
+ { class: 'Tile', href: (ctx as Ctx<Node>).link?.(e.entity.eid) },
42
+ h(
43
+ 'span',
44
+ { class: 'Tile_Text' },
45
+ h(
46
+ 'span',
47
+ { class: 'Tile_Line' },
48
+ h('span', { class: 'Tile_Title' }, s?.day ?? ''),
49
+ ),
50
+ ' ',
51
+ h('span', { class: 'Tile_Sub' }, s ? facts(s).join(' · ') : ''),
52
+ ),
53
+ )
54
+ }
55
+
56
+ /** Draws a day's tally by name. */
57
+ export let views: Registry = define([
58
+ {
59
+ view: 'Title',
60
+ match: parse('.spend'),
61
+ render: (e, h) => h('span', null, title(spendOf(e))),
62
+ },
63
+ ...['Tile', 'List.Tile'].map((view) => ({
64
+ view,
65
+ match: parse('.spend'),
66
+ render: tile,
67
+ })),
68
+ {
69
+ view: 'Inline',
70
+ match: parse('.spend'),
71
+ render: <Node>(e: Bundle, h: H<Node>, ctx: RenderContext<Node>) =>
72
+ h(
73
+ 'a',
74
+ { href: (ctx as Ctx<Node>).link?.(e.entity.eid) },
75
+ title(spendOf(e)),
76
+ ),
77
+ },
78
+ ])
79
+
80
+ /** The page and a day's page, loaded when first drawn. */
81
+ export let inspectViews = (): Promise<View[]> =>
82
+ import('./page.js').then((m) => m.inspectViews)
83
+
84
+ /** The page the sidebar offers: where the tokens went, over the days. */
85
+ export let destinations = [
86
+ {
87
+ key: 'spend',
88
+ name: 'Spend',
89
+ icon: 'coins',
90
+ query: '.spend * .order=-spend.day',
91
+ view: 'Spend.Page',
92
+ },
93
+ ]
94
+
95
+ /** The glyph it wears there. */
96
+ export let icons = { coins: Coins }
package/vocab.js ADDED
@@ -0,0 +1,17 @@
1
+ // The component and tool declarations, and nothing else: the module a server
2
+ // or a browser page imports at `@yaks/spend/vocab`. It reaches no storage and
3
+ // no runtime.
4
+ import doc from './vocab.json' with {
5
+ type: 'json'
6
+ };
7
+ import manifest from './package.json' with {
8
+ type: 'json'
9
+ };
10
+ /** The spend vocabulary: `spend{day, requests, tokens, dollars, parts,
11
+ * sessions, tasks, models, entries}`, `untallied{day}`, and the `spend_show` and
12
+ * `spend_backfill` tools, in the form `loadVocab` accepts. */ export let spendDoc = doc;
13
+ /** What this package is: its manifest's description. */ export let description = manifest.description;
14
+ /** Every document this package declares. */ export let docs = [
15
+ spendDoc
16
+ ];
17
+ //# sourceMappingURL=vocab.js.map
package/vocab.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,8EAA8E;AAC9E,cAAc;AAGd,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AACpD,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D;;4DAE4D,GAC5D,OAAO,IAAI,WAAqB,IAAe;AAE/C,sDAAsD,GACtD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AAErD,0CAA0C,GAC1C,OAAO,IAAI,OAAmB;EAAC;CAAS,CAAA"}
package/vocab.json ADDED
@@ -0,0 +1,237 @@
1
+ {
2
+ "$id": "urn:uuid:fea551fc-ed67-457f-8a90-2dbda48fe650",
3
+ "$vocabulary": {
4
+ "https://yak.sh/vocab/core": true
5
+ },
6
+ "title": "spend",
7
+ "$defs": {
8
+ "spend": {
9
+ "$id": "urn:uuid:9562bb25-5700-4e19-852a-2432ef30ec26",
10
+ "component": true,
11
+ "journal": false,
12
+ "wire": false,
13
+ "embed": false,
14
+ "type": "object",
15
+ "description": "where one UTC day's model requests went, tallied from each request's `usage` and `context_parts`: by part of the context, session, task, model and large entry, each as tokens by how they were paid (read from the provider cache, written to it, sent fresh, generated) and dollars. Dollars are what the provider reported, else the tokens at the model's list `price`. The totals are measured; the split across parts, and across the tasks a session worked on, is estimated",
16
+ "properties": {
17
+ "day": {
18
+ "$id": "urn:uuid:f3c6cd4d-ad07-421f-80b1-fe756454cd16",
19
+ "type": "string",
20
+ "identity": true,
21
+ "description": "the UTC day, `2026-10-20`"
22
+ },
23
+ "requests": {
24
+ "$id": "urn:uuid:ec996e89-a695-4e9a-bcfc-0fa7a0ee21b9",
25
+ "type": "integer",
26
+ "stamped": true,
27
+ "description": "how many model requests it tallies"
28
+ },
29
+ "tokens": {
30
+ "$id": "urn:uuid:933e7806-41c2-4885-ad73-a33ee9edd1e7",
31
+ "type": "array",
32
+ "items": { "type": "integer" },
33
+ "minItems": 4,
34
+ "maxItems": 4,
35
+ "stamped": true,
36
+ "description": "its tokens as [read, write, fresh, generated]"
37
+ },
38
+ "dollars": {
39
+ "$id": "urn:uuid:3fd54392-c870-4f31-8749-eda368bb1f4e",
40
+ "type": "number",
41
+ "stamped": true,
42
+ "description": "what they cost"
43
+ },
44
+ "parts": {
45
+ "$id": "urn:uuid:dc7d297c-95f6-46dd-8545-a4a49a0bd5d8",
46
+ "type": "object",
47
+ "additionalProperties": {
48
+ "type": "array",
49
+ "items": { "type": "number" },
50
+ "minItems": 5,
51
+ "maxItems": 5
52
+ },
53
+ "stamped": true,
54
+ "description": "by part of the context (`persona <id>`, `tool <name>`, `skills`, `system`, `result <tool>`, `call <tool>`, `input`, `notice`, `output`, `reasoning`, `checkpoint`, and `unrecorded` for a request whose context was not recorded): [read, write, fresh, generated, dollars]"
55
+ },
56
+ "sessions": {
57
+ "$id": "urn:uuid:ded7186d-6f88-413a-9802-0badc0b2371b",
58
+ "type": "object",
59
+ "additionalProperties": {
60
+ "type": "array",
61
+ "items": { "type": "number" },
62
+ "minItems": 3,
63
+ "maxItems": 3
64
+ },
65
+ "stamped": true,
66
+ "description": "by session eid: [tokens, dollars, requests]"
67
+ },
68
+ "tasks": {
69
+ "$id": "urn:uuid:3021fe1d-5fb7-49b2-87b3-7389c87d4a38",
70
+ "type": "object",
71
+ "additionalProperties": {
72
+ "type": "array",
73
+ "items": { "type": "number" },
74
+ "minItems": 2,
75
+ "maxItems": 2
76
+ },
77
+ "stamped": true,
78
+ "description": "by task eid, each session's share split evenly across the tasks it worked on, a subagent's across the tasks its brief names, else as the session that started it: [tokens, dollars]"
79
+ },
80
+ "models": {
81
+ "$id": "urn:uuid:6a4563b0-e370-4709-a761-82997cab675b",
82
+ "type": "object",
83
+ "additionalProperties": {
84
+ "type": "array",
85
+ "items": { "type": "number" },
86
+ "minItems": 6,
87
+ "maxItems": 6
88
+ },
89
+ "stamped": true,
90
+ "description": "by model eid: [read, write, fresh, generated, dollars, requests]"
91
+ },
92
+ "plans": {
93
+ "$id": "urn:uuid:5e71f0e6-5b7c-40fe-9f0e-20743d1c182b",
94
+ "type": "object",
95
+ "additionalProperties": {
96
+ "type": "object",
97
+ "properties": {
98
+ "dollars": { "type": "number" },
99
+ "unpriced": { "type": "array", "items": { "type": "string" } },
100
+ "metered": {
101
+ "type": "object",
102
+ "additionalProperties": {
103
+ "type": "object",
104
+ "additionalProperties": { "type": "number" }
105
+ }
106
+ },
107
+ "parts": {
108
+ "type": "object",
109
+ "additionalProperties": { "type": "number" }
110
+ },
111
+ "sessions": {
112
+ "type": "object",
113
+ "additionalProperties": { "type": "number" }
114
+ },
115
+ "tasks": {
116
+ "type": "object",
117
+ "additionalProperties": { "type": "number" }
118
+ },
119
+ "models": {
120
+ "type": "object",
121
+ "additionalProperties": { "type": "number" }
122
+ },
123
+ "entries": {
124
+ "type": "object",
125
+ "additionalProperties": { "type": "number" }
126
+ }
127
+ }
128
+ },
129
+ "stamped": true,
130
+ "description": "by the account whose plan its requests billed against (a transcript's request the account whose directory keeps it, a native harness's the account its provider's CLI signs in with here; a request paid per token bills none): the dollars they ran up on it, in all and by part, session, task, model, entry and (`metered`) each model's by session, which is what a limit on one model counts. Against that account's weekly limit, its dollars say what share of the week each row used"
131
+ },
132
+ "unpriced": {
133
+ "$id": "urn:uuid:7ab5ece5-d15c-430b-84f4-210d37d17763",
134
+ "type": "array",
135
+ "items": { "type": "string" },
136
+ "stamped": true,
137
+ "description": "the model eids a request was tallied for that had no `price` and reported no dollars: what they cost is missing from the dollars, not zero. Also kept per account, so an account's rate is known incomplete"
138
+ },
139
+ "entries": {
140
+ "$id": "urn:uuid:c0d26212-1e7e-4896-97ea-92e85bae88c6",
141
+ "type": "object",
142
+ "additionalProperties": {
143
+ "type": "array",
144
+ "items": { "type": "number" },
145
+ "minItems": 2,
146
+ "maxItems": 2
147
+ },
148
+ "stamped": true,
149
+ "description": "the transcript entries that cost most as they were carried request after request, by eid: [tokens, dollars]"
150
+ }
151
+ }
152
+ },
153
+ "untallied": {
154
+ "$id": "urn:uuid:ae0fa60e-d0f8-4f16-ab62-d4d4cc8c9ef7",
155
+ "component": true,
156
+ "journal": false,
157
+ "wire": false,
158
+ "embed": false,
159
+ "type": "object",
160
+ "description": "a past day some of whose requests were written after it ended, as when a transcript is read in after the fact: on its day's row until the duty tallies that day again",
161
+ "properties": {
162
+ "day": {
163
+ "$id": "urn:uuid:056f6b4f-e394-4ee7-a216-a15b0b2d7368",
164
+ "type": "string",
165
+ "stamped": true,
166
+ "description": "the UTC day, `2026-10-20`"
167
+ }
168
+ }
169
+ },
170
+ "spend_show": {
171
+ "tool": true,
172
+ "noun": "spend",
173
+ "verb": "show",
174
+ "title": "Show where the tokens went",
175
+ "description": "Report where the model requests of a span of days went, from the tallies the duty keeps: each plan limit against its pace, a line for each topic with a share of the week (attended.share) saying how much of it the topic's sessions used, then the parts of the context, sessions, tasks, models and large entries that cost most, by dollars. Reads tallies only; changes nothing.",
176
+ "input": {
177
+ "since": {
178
+ "type": "string",
179
+ "format": "date",
180
+ "description": "the first UTC day, `2026-10-13`, `yesterday` or `3 days ago` (default: the day the Claude plan's week began, else six days ago)"
181
+ },
182
+ "until": {
183
+ "type": "string",
184
+ "format": "date",
185
+ "description": "the last UTC day, written as since is (default: today)"
186
+ },
187
+ "top": {
188
+ "type": "integer",
189
+ "description": "how many rows each ranking shows (default 10)"
190
+ },
191
+ "plans": {
192
+ "type": "boolean",
193
+ "description": "true reports only each plan limit against its pace, one line each, as the Spend page shows first"
194
+ },
195
+ "done": {
196
+ "type": "boolean",
197
+ "description": "true reports only the cost per completed task in each UTC week (Monday to Sunday) from since's: the dollars charged to the tasks completed that week over those of them charged any, as cash and as usage at list price, by the tasks split (an estimate)"
198
+ }
199
+ },
200
+ "readOnly": true,
201
+ "positional": ["since"]
202
+ },
203
+ "spend_tally": {
204
+ "tool": true,
205
+ "surfaces": ["cli"],
206
+ "noun": "spend",
207
+ "verb": "tally",
208
+ "title": "Tally days again from their requests",
209
+ "description": "Tally each day since a day again from the requests the graph holds, as the duty does every ten minutes: a changed price or plan shows at once. Measures nothing; a transcript's requests are measured by spend_backfill.",
210
+ "input": {
211
+ "since": {
212
+ "type": "string",
213
+ "format": "date",
214
+ "description": "the first UTC day, `2026-10-13`, `yesterday` or `3 days ago` (default: today)"
215
+ }
216
+ },
217
+ "positional": ["since"]
218
+ },
219
+ "spend_backfill": {
220
+ "tool": true,
221
+ "surfaces": ["cli"],
222
+ "noun": "spend",
223
+ "verb": "backfill",
224
+ "title": "Measure past requests and tally their days",
225
+ "description": "Read every transcript and managed run's log written since a day again for its model requests (@yaks/session and @yaks/spawn `recounted`), walk the graph's own sessions for theirs, then tally each day since. Running it again leaves what is there alone.",
226
+ "input": {
227
+ "since": {
228
+ "type": "string",
229
+ "format": "date",
230
+ "description": "the first UTC day, `2026-10-13`, `yesterday` or `3 days ago`"
231
+ }
232
+ },
233
+ "required": ["since"],
234
+ "positional": ["since"]
235
+ }
236
+ }
237
+ }