@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.
- package/LICENSE +202 -0
- package/README.md +159 -2
- package/_dist/graph.d.ts +26 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/mod.d.ts +11 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/routes.d.ts +10 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/service.d.ts +41 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/tally.d.ts +111 -0
- package/_dist/tally.d.ts.map +1 -0
- package/_dist/tools.d.ts +52 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/use.d.ts +31 -0
- package/_dist/use.d.ts.map +1 -0
- package/_dist/views.d.ts +27 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/window.d.ts +132 -0
- package/_dist/window.d.ts.map +1 -0
- package/_dist/words.d.ts +40 -0
- package/_dist/words.d.ts.map +1 -0
- package/budget.js +131 -0
- package/budget.js.map +1 -0
- package/budget.ts +128 -0
- package/day.js +336 -0
- package/day.js.map +1 -0
- package/day.ts +364 -0
- package/deno.json +1 -0
- package/graph.js +59 -0
- package/graph.js.map +1 -0
- package/graph.ts +61 -0
- package/mod.js +11 -0
- package/mod.js.map +1 -0
- package/mod.ts +34 -0
- package/package.json +57 -4
- package/page.js +352 -0
- package/page.js.map +1 -0
- package/page.ts +491 -0
- package/routes.js +22 -0
- package/routes.js.map +1 -0
- package/routes.ts +19 -0
- package/service.js +124 -0
- package/service.js.map +1 -0
- package/service.ts +149 -0
- package/tally.js +385 -0
- package/tally.js.map +1 -0
- package/tally.ts +424 -0
- package/tools.js +243 -0
- package/tools.js.map +1 -0
- package/tools.ts +357 -0
- package/use.js +172 -0
- package/use.js.map +1 -0
- package/use.ts +226 -0
- package/views.js +79 -0
- package/views.js.map +1 -0
- package/views.ts +96 -0
- package/vocab.js +17 -0
- package/vocab.js.map +1 -0
- package/vocab.json +237 -0
- package/vocab.ts +18 -0
- package/window.js +259 -0
- package/window.js.map +1 -0
- package/window.ts +336 -0
- package/words.js +47 -0
- package/words.js.map +1 -0
- package/words.ts +77 -0
package/day.ts
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import { env } from '@yaks/process/env'
|
|
2
|
+
/**
|
|
3
|
+
* A day's tally, read from the graph and kept as its `spend` row: the
|
|
4
|
+
* requests whose entries were written that UTC day (an imported entry is
|
|
5
|
+
* dated when its line was written), the prices, the sessions' fixed parts,
|
|
6
|
+
* and the tasks each session worked on: the ones it claims, or commented on,
|
|
7
|
+
* filed or finished from that day to the next. A subagent writes as the
|
|
8
|
+
* session that started it, so its tasks are the ones its brief names, else
|
|
9
|
+
* that session's.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
14
|
+
import { type Built, line, sub } from '@yaks/query'
|
|
15
|
+
import { identityEid } from '@yaks/graph/identity'
|
|
16
|
+
import { idsIn, parse } from '@yaks/id'
|
|
17
|
+
import { accountEid, listPrice, type Price } from '@yaks/model'
|
|
18
|
+
import { type Home, home } from '@yaks/session/files'
|
|
19
|
+
import { absolute } from '@yaks/file/local'
|
|
20
|
+
import { after, dayEid, midnight } from './window.js'
|
|
21
|
+
import { type Known, type Spend, tally } from './tally.js'
|
|
22
|
+
|
|
23
|
+
/** The row that keeps a day's tally. */
|
|
24
|
+
|
|
25
|
+
let PAGE = 2000
|
|
26
|
+
let comp = (b: Bundle | undefined, name: string) => (b?.[name] ?? {}) as Comp
|
|
27
|
+
let str = (v: unknown) => v == null ? '' : String(v)
|
|
28
|
+
|
|
29
|
+
let provider = (name: string) => identityEid('provider', [name])
|
|
30
|
+
|
|
31
|
+
type Runner = Home['runner']
|
|
32
|
+
|
|
33
|
+
/** Which CLI's plan each provider that serves models bills against: the
|
|
34
|
+
* Claude and Codex CLIs their own subscriptions, and OpenAI's API the ChatGPT
|
|
35
|
+
* sign-in's, which is Codex's, unless an API key pays for it by the token (the
|
|
36
|
+
* harness takes a key first). Any other provider bills no plan. */
|
|
37
|
+
export let PLANS = (key = false): Map<Eid, Runner> => {
|
|
38
|
+
let plans: Record<string, Runner> = {
|
|
39
|
+
claude: 'claude',
|
|
40
|
+
codex: 'codex',
|
|
41
|
+
'codex-cli': 'codex',
|
|
42
|
+
...key ? {} : { openai: 'codex' },
|
|
43
|
+
}
|
|
44
|
+
return new Map(
|
|
45
|
+
Object.entries(plans).map(([from, to]) => [provider(from), to]),
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The account whose plan a request billed against: the account its
|
|
51
|
+
* transcript is in, which `paths` says where each file of this computer is
|
|
52
|
+
* and `homes` where each account keeps its own; else, for one the native
|
|
53
|
+
* harness sent, the account the CLI of its `using` provider signs in with
|
|
54
|
+
* here. A managed run's transcript is its command's output, kept in no
|
|
55
|
+
* account's home, so its requests bill the account of the provider its
|
|
56
|
+
* session asked for: of the session's requests in `asked`, each the entry
|
|
57
|
+
* that asked and the provider, the last at or before this one. A request
|
|
58
|
+
* that billed no plan was paid by the token.
|
|
59
|
+
*
|
|
60
|
+
* ```ts
|
|
61
|
+
* import { planOf, PLANS } from './day.ts'
|
|
62
|
+
* import { home, type Home } from '@yaks/session/files'
|
|
63
|
+
* import { accountEid } from '@yaks/model'
|
|
64
|
+
* import { identityEid } from '@yaks/graph/identity'
|
|
65
|
+
* import { equal } from '@yaks/testing'
|
|
66
|
+
*
|
|
67
|
+
* let p = (name: string) => identityEid('provider', [name])
|
|
68
|
+
* let paths = new Map([['f', '/u/.claude-work/projects/p/s.jsonl']])
|
|
69
|
+
* let homes = new Map([['/u/.claude-work', 'w']])
|
|
70
|
+
* let plan = planOf(PLANS(), paths, homes)
|
|
71
|
+
* let r = { entity: { eid: 'r' } }
|
|
72
|
+
* equal(plan({ ...r, lines: { file: 'f', start: 3 } }), 'w')
|
|
73
|
+
* equal(plan({ ...r, using: { provider: p('openai') } }), accountEid(home('codex')!))
|
|
74
|
+
* equal(planOf(PLANS(true))({ ...r, using: { provider: p('openai') } }), undefined)
|
|
75
|
+
* equal(plan({ ...r, using: { provider: p('openrouter') } }), undefined)
|
|
76
|
+
*
|
|
77
|
+
* let asked = new Map([['s', [[1, p('claude')], [9, p('openrouter')]] as [number, string][]]])
|
|
78
|
+
* let run = planOf(PLANS(), new Map([['o', '/u/.tasks/processes/s.out']]), homes, asked)
|
|
79
|
+
* let sent = (seq: number) => ({ ...r, lines: { file: 'o' }, entry: { session: 's', seq } })
|
|
80
|
+
* equal([run(sent(4)), run(sent(12))], [accountEid(home('claude')!), undefined])
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export let planOf = (
|
|
84
|
+
plans: Map<Eid, Runner>,
|
|
85
|
+
paths: Map<Eid, string> = new Map(),
|
|
86
|
+
homes: Map<string, Eid> = new Map(),
|
|
87
|
+
asked: Map<Eid, [number, Eid][]> = new Map(),
|
|
88
|
+
) =>
|
|
89
|
+
(b: Bundle): Eid | undefined => {
|
|
90
|
+
let signed = (provider?: Eid) => {
|
|
91
|
+
let dir = plans.has(provider ?? '')
|
|
92
|
+
? home(plans.get(provider!)!)
|
|
93
|
+
: undefined
|
|
94
|
+
return dir && accountEid(dir)
|
|
95
|
+
}
|
|
96
|
+
let served = str(comp(b, 'using').provider)
|
|
97
|
+
if (served) return signed(served)
|
|
98
|
+
let source = paths.get(str(comp(b, 'lines').file)) ?? ''
|
|
99
|
+
let kept = [...homes].find(([dir]) => source.startsWith(`${dir}/`))?.[1]
|
|
100
|
+
if (kept) return kept
|
|
101
|
+
let { session, seq } = comp(b, 'entry')
|
|
102
|
+
return signed(
|
|
103
|
+
asked.get(str(session))?.findLast(([at]) => at <= Number(seq))?.[1],
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// The provider each of these sessions asked for, at each entry that asked: a
|
|
108
|
+
// managed run's requests (@yaks/spawn), in the order they were made.
|
|
109
|
+
let requested = async (
|
|
110
|
+
g: Graph,
|
|
111
|
+
sessions: Eid[],
|
|
112
|
+
): Promise<Map<Eid, [number, Eid][]>> => {
|
|
113
|
+
let out = new Map<Eid, [number, Eid][]>()
|
|
114
|
+
for (let i = 0; i < sessions.length; i += 200) {
|
|
115
|
+
let rows = await g.rows(
|
|
116
|
+
line`.using !lines !ask .entry.session=${
|
|
117
|
+
sessions.slice(i, i + 200)
|
|
118
|
+
} .order=entry.seq .fields=entry.session,entry.seq,using.provider`,
|
|
119
|
+
) as Record<string, unknown>[]
|
|
120
|
+
for (let r of rows) {
|
|
121
|
+
let provider = str(r['using.provider'])
|
|
122
|
+
if (!provider) continue
|
|
123
|
+
let s = str(r['entry.session'])
|
|
124
|
+
let at = out.get(s) ?? out.set(s, []).get(s)!
|
|
125
|
+
at.push([Number(r['entry.seq']), provider])
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return out
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Where each transcript the rows were read from is on this computer's disks.
|
|
132
|
+
let located = async (g: Graph, rows: Bundle[]): Promise<Map<Eid, string>> => {
|
|
133
|
+
let files = [
|
|
134
|
+
...new Set(rows.map((b) => str(comp(b, 'lines').file)).filter(Boolean)),
|
|
135
|
+
]
|
|
136
|
+
let found = await Promise.all(
|
|
137
|
+
(files.length ? await g.get(files, ['file']) : []).map(async (f) =>
|
|
138
|
+
[f.entity.eid, await absolute(g, f)] as const
|
|
139
|
+
),
|
|
140
|
+
)
|
|
141
|
+
return new Map(found.flatMap(([eid, path]) => path ? [[eid, path]] : []))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Every row a query finds, a page at a time.
|
|
145
|
+
let every = async (g: Graph, q: Built): Promise<Bundle[]> => {
|
|
146
|
+
let out: Bundle[] = []
|
|
147
|
+
let last: Eid | undefined
|
|
148
|
+
for (;;) {
|
|
149
|
+
let page = await g.read(
|
|
150
|
+
line`${sub(q)} .order=entity.eid .limit=${PAGE}${
|
|
151
|
+
sub(last ? line` .after=${last}` : line``)
|
|
152
|
+
} *`,
|
|
153
|
+
)
|
|
154
|
+
out.push(...page)
|
|
155
|
+
if (page.length < PAGE) return out
|
|
156
|
+
last = page.at(-1)!.entity.eid
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The row of each of these sessions and of every session above it by
|
|
161
|
+
* `spawned.parent`, however long its line, with `spawned` and these
|
|
162
|
+
* components. */
|
|
163
|
+
export let lineage = async (
|
|
164
|
+
g: Graph,
|
|
165
|
+
sessions: Eid[],
|
|
166
|
+
comps: string[],
|
|
167
|
+
): Promise<Map<Eid, Bundle>> => {
|
|
168
|
+
let rows = new Map<Eid, Bundle>()
|
|
169
|
+
for (let ask = [...new Set(sessions)]; ask.length;) {
|
|
170
|
+
let got = await g.get(ask, ['spawned', ...comps])
|
|
171
|
+
ask = []
|
|
172
|
+
for (let r of got) {
|
|
173
|
+
if (!r || rows.has(r.entity.eid)) continue
|
|
174
|
+
rows.set(r.entity.eid, r)
|
|
175
|
+
ask.push(str(comp(r, 'spawned').parent))
|
|
176
|
+
}
|
|
177
|
+
ask = [...new Set(ask)].filter((s) => s && !rows.has(s))
|
|
178
|
+
}
|
|
179
|
+
return rows
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Each session's root: the session that started it, and that one's, up.
|
|
183
|
+
let roots = async (g: Graph, sessions: Eid[]) => {
|
|
184
|
+
let parent = new Map<Eid, Eid>()
|
|
185
|
+
let fixed = new Map<Eid, Record<string, number>>()
|
|
186
|
+
for (let [eid, r] of await lineage(g, sessions, ['fixed'])) {
|
|
187
|
+
let parts = comp(r, 'fixed').parts as Record<string, number> | undefined
|
|
188
|
+
if (parts) fixed.set(eid, parts)
|
|
189
|
+
let up = str(comp(r, 'spawned').parent)
|
|
190
|
+
if (up) parent.set(eid, up)
|
|
191
|
+
}
|
|
192
|
+
let root = (s: Eid) => {
|
|
193
|
+
for (let seen = new Set<Eid>(); parent.has(s) && !seen.has(s);) {
|
|
194
|
+
seen.add(s)
|
|
195
|
+
s = parent.get(s)!
|
|
196
|
+
}
|
|
197
|
+
return s
|
|
198
|
+
}
|
|
199
|
+
return { root, parent, fixed }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// The tasks a subagent was started on: those its brief, the first thing its
|
|
203
|
+
// transcript says, names. Its writes are its parent's, so they cannot say. An
|
|
204
|
+
// id is its number, whatever its letter, and it names a task when the entity
|
|
205
|
+
// carrying that number is one; a number nothing carries names nothing.
|
|
206
|
+
let briefed = async (
|
|
207
|
+
g: Graph,
|
|
208
|
+
subagents: Eid[],
|
|
209
|
+
): Promise<Map<Eid, Eid[]>> => {
|
|
210
|
+
let named = new Map<Eid, number[]>()
|
|
211
|
+
for (let s of subagents) {
|
|
212
|
+
let [first] = await g.rows(
|
|
213
|
+
line`.entry.session=${s} .content .order=entry.seq .limit=1 .fields=content.body`,
|
|
214
|
+
) as Record<string, unknown>[]
|
|
215
|
+
let nums = idsIn(str(first?.['content.body']))
|
|
216
|
+
.map((id) => parse(id)!.num)
|
|
217
|
+
if (nums.length) named.set(s, [...new Set(nums)])
|
|
218
|
+
}
|
|
219
|
+
let all = [...new Set([...named.values()].flat())]
|
|
220
|
+
let tasks = new Map<number, Eid>()
|
|
221
|
+
if (all.length) {
|
|
222
|
+
let found = await g.read(
|
|
223
|
+
line`.task .entity.num=${all} .fields=entity.num`,
|
|
224
|
+
)
|
|
225
|
+
for (let b of found) tasks.set(Number(b.entity.num), b.entity.eid)
|
|
226
|
+
}
|
|
227
|
+
return new Map(
|
|
228
|
+
[...named].map((
|
|
229
|
+
[s, ns],
|
|
230
|
+
) => [s, ns.map((n) => tasks.get(n)).filter((e): e is Eid => !!e)]),
|
|
231
|
+
)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// The tasks each root session worked on from `day` to the next.
|
|
235
|
+
let worked = async (
|
|
236
|
+
g: Graph,
|
|
237
|
+
day: string,
|
|
238
|
+
root: (s: Eid) => Eid,
|
|
239
|
+
): Promise<Map<Eid, Eid[]>> => {
|
|
240
|
+
let from = midnight(day), to = midnight(after(day, 2))
|
|
241
|
+
let links: [Eid, Eid][] = [] // session, entity
|
|
242
|
+
let rows = async (q: Built, of: string, by: string) => {
|
|
243
|
+
for (let r of await g.rows(q) as Record<string, unknown>[]) {
|
|
244
|
+
if (r[of] && r[by]) links.push([str(r[by]), str(r[of])])
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
await rows(
|
|
248
|
+
line`.claim .fields=entity.eid,claim.session`,
|
|
249
|
+
'eid',
|
|
250
|
+
'claim.session',
|
|
251
|
+
)
|
|
252
|
+
await rows(
|
|
253
|
+
line`.comment .created.at>=${from} .created.at<${to} .fields=comment.target,created.via`,
|
|
254
|
+
'comment.target',
|
|
255
|
+
'created.via',
|
|
256
|
+
)
|
|
257
|
+
await rows(
|
|
258
|
+
line`.task .created.at>=${from} .created.at<${to} .fields=entity.eid,created.via`,
|
|
259
|
+
'eid',
|
|
260
|
+
'created.via',
|
|
261
|
+
)
|
|
262
|
+
await rows(
|
|
263
|
+
line`.task .completed.at>=${from} .completed.at<${to} .fields=entity.eid,completed.via`,
|
|
264
|
+
'eid',
|
|
265
|
+
'completed.via',
|
|
266
|
+
)
|
|
267
|
+
let named = [...new Set(links.map(([, e]) => e))]
|
|
268
|
+
let tasks = new Set(
|
|
269
|
+
(await g.get(named, ['task'])).filter((r) => r?.task).map((r) =>
|
|
270
|
+
r!.entity.eid
|
|
271
|
+
),
|
|
272
|
+
)
|
|
273
|
+
let out = new Map<Eid, Set<Eid>>()
|
|
274
|
+
for (let [s, e] of links) {
|
|
275
|
+
if (!tasks.has(e)) continue
|
|
276
|
+
let at = out.get(root(s)) ?? out.set(root(s), new Set()).get(root(s))!
|
|
277
|
+
at.add(e)
|
|
278
|
+
}
|
|
279
|
+
return new Map([...out].map(([s, ts]) => [s, [...ts]]))
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** What a day's tally reads beside its requests. */
|
|
283
|
+
export let known = async (
|
|
284
|
+
g: Graph,
|
|
285
|
+
day: string,
|
|
286
|
+
rows: Bundle[],
|
|
287
|
+
): Promise<Known> => {
|
|
288
|
+
let prices = new Map(
|
|
289
|
+
(await g.read('.price *')).map((b) => [b.entity.eid, b.price as Price]),
|
|
290
|
+
)
|
|
291
|
+
let sessions = [
|
|
292
|
+
...new Set(rows.map((b) => str(comp(b, 'entry').session)).filter(Boolean)),
|
|
293
|
+
]
|
|
294
|
+
let { root, parent, fixed } = await roots(g, sessions)
|
|
295
|
+
let byRoot = await worked(g, day, root)
|
|
296
|
+
let brief = await briefed(g, [...parent.keys()])
|
|
297
|
+
// A subagent's tasks are its brief's, else the nearest brief up its line,
|
|
298
|
+
// else what the session at its root worked on.
|
|
299
|
+
let of = (s: Eid): Eid[] => {
|
|
300
|
+
let seen = new Set<Eid>()
|
|
301
|
+
for (let at: Eid | undefined = s; at && !seen.has(at);) {
|
|
302
|
+
if (brief.get(at)?.length) return brief.get(at)!
|
|
303
|
+
seen.add(at)
|
|
304
|
+
at = parent.get(at)
|
|
305
|
+
}
|
|
306
|
+
return byRoot.get(root(s)) ?? []
|
|
307
|
+
}
|
|
308
|
+
let tasks = new Map(sessions.map((s) => [s, of(s)]))
|
|
309
|
+
let homes = new Map(
|
|
310
|
+
(await g.read('.account *')).map((b) => [
|
|
311
|
+
str(comp(b, 'account').home),
|
|
312
|
+
b.entity.eid,
|
|
313
|
+
]),
|
|
314
|
+
)
|
|
315
|
+
let plan = planOf(
|
|
316
|
+
PLANS(!!env.get('OPENAI_API_KEY')),
|
|
317
|
+
await located(g, rows),
|
|
318
|
+
homes,
|
|
319
|
+
await requested(g, sessions),
|
|
320
|
+
)
|
|
321
|
+
let plans = new Map(
|
|
322
|
+
rows.flatMap((b): [Eid, Eid][] => {
|
|
323
|
+
let at = plan(b)
|
|
324
|
+
return at ? [[b.entity.eid, at]] : []
|
|
325
|
+
}),
|
|
326
|
+
)
|
|
327
|
+
return { prices, fixed, tasks, plans }
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** A day's requests: every entry with usage that asked a model. */
|
|
331
|
+
export let requests = (g: Graph, day: string): Promise<Bundle[]> =>
|
|
332
|
+
every(
|
|
333
|
+
g,
|
|
334
|
+
line`.ask .usage .created.at>=${midnight(day)} .created.at<${
|
|
335
|
+
midnight(after(day))
|
|
336
|
+
}`,
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
/** Give each of these models its list price on its row, where the table
|
|
340
|
+
* holds its name: a row made before the table knew it, or by a writer that
|
|
341
|
+
* names a model by its row. Says whether any took one. */
|
|
342
|
+
let listed = async (g: Graph, models: Eid[]): Promise<boolean> => {
|
|
343
|
+
let prices = (await g.get(models.filter(Boolean), ['model'])).flatMap(
|
|
344
|
+
(b): Bundle[] => {
|
|
345
|
+
let price = listPrice(str(comp(b, 'model').name))
|
|
346
|
+
return price ? [{ entity: { eid: b.entity.eid }, price }] : []
|
|
347
|
+
},
|
|
348
|
+
)
|
|
349
|
+
if (prices.length) await g.apply(prices, { trusted: true })
|
|
350
|
+
return prices.length > 0
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Tally a day from the graph and keep it on its row. A model it meets with
|
|
354
|
+
* no price takes its list price first, so only a model the table lacks is
|
|
355
|
+
* left unpriced. */
|
|
356
|
+
export let recompute = async (g: Graph, day: string): Promise<Spend> => {
|
|
357
|
+
let rows = await requests(g, day)
|
|
358
|
+
let spend = tally(day, rows, await known(g, day, rows))
|
|
359
|
+
if (await listed(g, spend.unpriced)) {
|
|
360
|
+
spend = tally(day, rows, await known(g, day, rows))
|
|
361
|
+
}
|
|
362
|
+
await g.apply([{ entity: { eid: dayEid(day) }, spend }], { trusted: true })
|
|
363
|
+
return spend
|
|
364
|
+
}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/spend","version":"0.2.36","license":"Apache-2.0","description":"Where model tokens go: each day's requests tallied by part of the context, session, task, model and large entry, at the price paid, beside each plan limit's pace, and a page that reads the tallies so looking costs nothing.","exports":{".":"./mod.ts","./vocab":"./vocab.ts","./graph":"./graph.ts","./tools":"./tools.ts","./service":"./service.ts","./routes":"./routes.ts","./views":"./views.ts"},"imports":{"@yaks/spend":"./mod.ts","@yaks/spend/vocab":"./vocab.ts","@yaks/spend/graph":"./graph.ts","@yaks/spend/tools":"./tools.ts","@yaks/spend/service":"./service.ts","@yaks/spend/routes":"./routes.ts","@yaks/spend/views":"./views.ts"}}
|
package/graph.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The graph plugin: the module a server imports at `@yaks/spend/graph`. A
|
|
3
|
+
* request written after its day ended, as when a transcript is read in after
|
|
4
|
+
* the fact, marks its day's row `untallied`, so the duty (./service.ts) tallies
|
|
5
|
+
* that day again without being asked. Today needs no mark: every pass tallies
|
|
6
|
+
* it.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/ import { hook } from "@yaks/graph/plugin";
|
|
10
|
+
import { after } from "@yaks/fp";
|
|
11
|
+
import { dayEid, dayOf } from "./window.js";
|
|
12
|
+
/** The days before `today` the requests in a batch were made on. A batch's
|
|
13
|
+
* stamps ride in bundles of their own, so a request's `created.at` is found by
|
|
14
|
+
* its eid.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { late } from '@yaks/spend/graph'
|
|
18
|
+
* import { equal } from '@yaks/testing'
|
|
19
|
+
* let asked = { entity: { eid: 'a' }, ask: {}, usage: { input_tokens: 1 } }
|
|
20
|
+
* let made = (at: string) => ({ entity: { eid: 'a' }, created: { at } })
|
|
21
|
+
* equal(late([asked, made('2026-10-20T12:00:00Z')], '2026-10-25'), ['2026-10-20'])
|
|
22
|
+
* equal(late([asked, made('2026-10-25T01:00:00Z')], '2026-10-25'), [])
|
|
23
|
+
* ```
|
|
24
|
+
*/ export let late = (bundles, today)=>{
|
|
25
|
+
let asked = new Set(bundles.filter((b)=>b.ask && b.usage).map((b)=>b.entity.eid));
|
|
26
|
+
let days = new Set();
|
|
27
|
+
for (let b of bundles){
|
|
28
|
+
let at = b.created?.at;
|
|
29
|
+
if (!asked.has(b.entity.eid) || typeof at != 'string') continue;
|
|
30
|
+
if (dayOf(at) < today) days.add(dayOf(at));
|
|
31
|
+
}
|
|
32
|
+
return [
|
|
33
|
+
...days
|
|
34
|
+
];
|
|
35
|
+
};
|
|
36
|
+
/** Mark each past day a batch adds requests to, inside its transaction, so a
|
|
37
|
+
* day is never read in without being owed a tally. `now` is the clock. */ export let untallied = (now = Date.now)=>({
|
|
38
|
+
name: '@yaks/spend',
|
|
39
|
+
hooks: {
|
|
40
|
+
commit: hook([
|
|
41
|
+
'usage'
|
|
42
|
+
], (bundles, tx)=>{
|
|
43
|
+
let days = late(bundles, dayOf(now()));
|
|
44
|
+
if (!days.length) return bundles;
|
|
45
|
+
return after(tx.patch(days.map((day)=>({
|
|
46
|
+
entity: {
|
|
47
|
+
eid: dayEid(day)
|
|
48
|
+
},
|
|
49
|
+
untallied: {
|
|
50
|
+
day
|
|
51
|
+
}
|
|
52
|
+
}))), ()=>bundles);
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
/** The plugins this package adds to a graph. */ export let plugins = ()=>[
|
|
57
|
+
untallied()
|
|
58
|
+
];
|
|
59
|
+
//# sourceMappingURL=graph.js.map
|
package/graph.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.js","sources":["./graph.ts"],"names":[],"mappings":"AAAA;;;;;;;;CAQC,GAED,SAAS,IAAI,kCAAuC;AACpD,SAAS,KAAK,wBAA6B;AAC3C,SAAS,MAAM,EAAE,KAAK,sBAAqB;AAE3C;;;;;;;;;;;;CAYC,GACD,OAAO,IAAI,OAAO,CAAC,SAAmB;EACpC,IAAI,QAAQ,IAAI,IACd,QAAQ,MAAM,CAAC,CAAC,IAAM,EAAE,GAAG,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,IAAM,EAAE,MAAM,CAAC,GAAG;EAEjE,IAAI,OAAO,IAAI;EACf,KAAK,IAAI,KAAK,QAAS;IACrB,IAAI,KAAM,EAAE,OAAO,EAA0C;IAC7D,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,MAAM,CAAC,GAAG,KAAK,OAAO,MAAM,UAAU;IACvD,IAAI,MAAM,MAAM,OAAO,KAAK,GAAG,CAAC,MAAM;EACxC;EACA,OAAO;OAAI;GAAK;AAClB,EAAC;AAED;wEACwE,GACxE,OAAO,IAAI,YAAY,CAAC,MAAoB,KAAK,GAAG,GAAa,CAAC;IAChE,MAAM;IACN,OAAO;MACL,QAAQ,KAAK;QAAC;OAAQ,EAAE,CAAC,SAAS;QAChC,IAAI,OAAO,KAAK,SAAS,MAAM;QAC/B,IAAI,CAAC,KAAK,MAAM,EAAE,OAAO;QACzB,OAAO,MACL,GAAG,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,MAAQ,CAAC;YAC1B,QAAQ;cAAE,KAAK,OAAO;YAAK;YAC3B,WAAW;cAAE;YAAI;UACnB,CAAC,KACD,IAAM;MAEV;IACF;EACF,CAAC,EAAC;AAEF,8CAA8C,GAC9C,OAAO,IAAI,UAAU,IAAgB;IAAC;GAAY,CAAA"}
|
package/graph.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The graph plugin: the module a server imports at `@yaks/spend/graph`. A
|
|
3
|
+
* request written after its day ended, as when a transcript is read in after
|
|
4
|
+
* the fact, marks its day's row `untallied`, so the duty (./service.ts) tallies
|
|
5
|
+
* that day again without being asked. Today needs no mark: every pass tallies
|
|
6
|
+
* it.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type { Bundle, Plugin } from 'jsr:@yaks/graph@0.2.36'
|
|
11
|
+
import { hook } from '@yaks/graph/plugin'
|
|
12
|
+
import { after } from '@yaks/fp'
|
|
13
|
+
import { dayEid, dayOf } from './window.js'
|
|
14
|
+
|
|
15
|
+
/** The days before `today` the requests in a batch were made on. A batch's
|
|
16
|
+
* stamps ride in bundles of their own, so a request's `created.at` is found by
|
|
17
|
+
* its eid.
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { late } from '@yaks/spend/graph'
|
|
21
|
+
* import { equal } from '@yaks/testing'
|
|
22
|
+
* let asked = { entity: { eid: 'a' }, ask: {}, usage: { input_tokens: 1 } }
|
|
23
|
+
* let made = (at: string) => ({ entity: { eid: 'a' }, created: { at } })
|
|
24
|
+
* equal(late([asked, made('2026-10-20T12:00:00Z')], '2026-10-25'), ['2026-10-20'])
|
|
25
|
+
* equal(late([asked, made('2026-10-25T01:00:00Z')], '2026-10-25'), [])
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export let late = (bundles: Bundle[], today: string): string[] => {
|
|
29
|
+
let asked = new Set(
|
|
30
|
+
bundles.filter((b) => b.ask && b.usage).map((b) => b.entity.eid),
|
|
31
|
+
)
|
|
32
|
+
let days = new Set<string>()
|
|
33
|
+
for (let b of bundles) {
|
|
34
|
+
let at = (b.created as { at?: unknown } | null | undefined)?.at
|
|
35
|
+
if (!asked.has(b.entity.eid) || typeof at != 'string') continue
|
|
36
|
+
if (dayOf(at) < today) days.add(dayOf(at))
|
|
37
|
+
}
|
|
38
|
+
return [...days]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Mark each past day a batch adds requests to, inside its transaction, so a
|
|
42
|
+
* day is never read in without being owed a tally. `now` is the clock. */
|
|
43
|
+
export let untallied = (now: () => number = Date.now): Plugin => ({
|
|
44
|
+
name: '@yaks/spend',
|
|
45
|
+
hooks: {
|
|
46
|
+
commit: hook(['usage'], (bundles, tx) => {
|
|
47
|
+
let days = late(bundles, dayOf(now()))
|
|
48
|
+
if (!days.length) return bundles
|
|
49
|
+
return after(
|
|
50
|
+
tx.patch(days.map((day) => ({
|
|
51
|
+
entity: { eid: dayEid(day) },
|
|
52
|
+
untallied: { day },
|
|
53
|
+
}))),
|
|
54
|
+
() => bundles,
|
|
55
|
+
)
|
|
56
|
+
}),
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
/** The plugins this package adds to a graph. */
|
|
61
|
+
export let plugins = (): Plugin[] => [untallied()]
|
package/mod.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where model tokens go. A duty tallies each UTC day's requests into its
|
|
3
|
+
* `spend` row (./service.ts, ./day.ts); a page and `yak spend show` read the
|
|
4
|
+
* rows. This module is the pure part both sides share: the tally, plan limits
|
|
5
|
+
* and their pace, and the words numbers are said in.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/ export { empty, ENTRIES, merged, ranked, spendOf, tally } from "./tally.js";
|
|
9
|
+
export { after, billedOn, current, dayOf, limitRates, limitsOf, paceOf, rates, span, weekOf } from "./window.js";
|
|
10
|
+
export { dollars, label, percent, tokens } from "./words.js";
|
|
11
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;CAOC,GACD,SACE,KAAK,EACL,OAAO,EAEP,MAAM,EAEN,MAAM,EAGN,OAAO,EACP,KAAK,qBACY;AACnB,SACE,KAAK,EACL,QAAQ,EACR,OAAO,EACP,KAAK,EAEL,UAAU,EACV,QAAQ,EACR,MAAM,EACN,KAAK,EACL,IAAI,EACJ,MAAM,sBACY;AACpB,SAAS,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAoB"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where model tokens go. A duty tallies each UTC day's requests into its
|
|
3
|
+
* `spend` row (./service.ts, ./day.ts); a page and `yak spend show` read the
|
|
4
|
+
* rows. This module is the pure part both sides share: the tally, plan limits
|
|
5
|
+
* and their pace, and the words numbers are said in.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
export {
|
|
10
|
+
empty,
|
|
11
|
+
ENTRIES,
|
|
12
|
+
type Known,
|
|
13
|
+
merged,
|
|
14
|
+
type Plan,
|
|
15
|
+
ranked,
|
|
16
|
+
type Rows,
|
|
17
|
+
type Spend,
|
|
18
|
+
spendOf,
|
|
19
|
+
tally,
|
|
20
|
+
} from './tally.js'
|
|
21
|
+
export {
|
|
22
|
+
after,
|
|
23
|
+
billedOn,
|
|
24
|
+
current,
|
|
25
|
+
dayOf,
|
|
26
|
+
type Limit,
|
|
27
|
+
limitRates,
|
|
28
|
+
limitsOf,
|
|
29
|
+
paceOf,
|
|
30
|
+
rates,
|
|
31
|
+
span,
|
|
32
|
+
weekOf,
|
|
33
|
+
} from './window.js'
|
|
34
|
+
export { dollars, label, percent, tokens } from './words.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/spend",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/spend",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
},
|
|
11
|
+
"./vocab": {
|
|
12
|
+
"types": "./_dist/vocab.d.ts",
|
|
13
|
+
"default": "./vocab.js"
|
|
14
|
+
},
|
|
15
|
+
"./graph": {
|
|
16
|
+
"types": "./_dist/graph.d.ts",
|
|
17
|
+
"default": "./graph.js"
|
|
18
|
+
},
|
|
19
|
+
"./tools": {
|
|
20
|
+
"types": "./_dist/tools.d.ts",
|
|
21
|
+
"default": "./tools.js"
|
|
22
|
+
},
|
|
23
|
+
"./service": {
|
|
24
|
+
"types": "./_dist/service.d.ts",
|
|
25
|
+
"default": "./service.js"
|
|
26
|
+
},
|
|
27
|
+
"./routes": {
|
|
28
|
+
"types": "./_dist/routes.d.ts",
|
|
29
|
+
"default": "./routes.js"
|
|
30
|
+
},
|
|
31
|
+
"./views": {
|
|
32
|
+
"types": "./_dist/views.d.ts",
|
|
33
|
+
"default": "./views.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"license": "Apache-2.0",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@yaks/api": "0.2.36",
|
|
39
|
+
"@yaks/file": "0.2.36",
|
|
40
|
+
"@yaks/fp": "0.2.36",
|
|
41
|
+
"@yaks/graph": "0.2.36",
|
|
42
|
+
"@yaks/id": "0.2.36",
|
|
43
|
+
"@yaks/inspect": "0.2.36",
|
|
44
|
+
"@yaks/model": "0.2.36",
|
|
45
|
+
"@yaks/process": "0.2.36",
|
|
46
|
+
"@yaks/query": "0.2.36",
|
|
47
|
+
"@yaks/render": "0.2.36",
|
|
48
|
+
"@yaks/session": "0.2.36",
|
|
49
|
+
"@yaks/spawn": "0.2.36",
|
|
50
|
+
"@yaks/topic": "0.2.36",
|
|
51
|
+
"@yaks/ui": "0.2.36",
|
|
52
|
+
"@yaks/vocab": "0.2.36",
|
|
53
|
+
"@types/react": "19.3.0",
|
|
54
|
+
"lucide": "1.31.0",
|
|
55
|
+
"react": "19.3.0"
|
|
56
|
+
},
|
|
57
|
+
"description": "Where model tokens go: each day's requests tallied by part of the context, session, task, model and large entry, at the price paid, beside each plan limit's pace, and a page that reads the tallies so looking costs nothing.",
|
|
58
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
59
|
+
}
|