@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/_dist/use.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
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 type { Eid } from "@yaks/graph";
|
|
13
|
+
/** What some work ran up against one weekly limit in its running week: a
|
|
14
|
+
* plan's, or a plan's limit on one model. */ export type Billed = {
|
|
15
|
+
plan: Eid;
|
|
16
|
+
/** the model the limit meters, where it is a limit on one model */ model?: Eid;
|
|
17
|
+
/** what the limit is called */ name: string;
|
|
18
|
+
/** usage at list price, not cash: the plan is a subscription */ dollars: number;
|
|
19
|
+
/** those dollars as a part of the limit's week, 0 to 1, where the limit has
|
|
20
|
+
* a reading of this week */ used?: number;
|
|
21
|
+
/** how much of the limit's week is gone, 0 to 1 */ gone: number;
|
|
22
|
+
};
|
|
23
|
+
/** A topic's share against its use; with no `topic`, the work no share
|
|
24
|
+
* covers against the part of the week no topic was given. */ export type Use = {
|
|
25
|
+
topic?: Eid;
|
|
26
|
+
/** its share, 0 to 1 */ share: number;
|
|
27
|
+
/** its share less the shares below it */ keeps: number;
|
|
28
|
+
/** how many shares it sits under */ depth: number;
|
|
29
|
+
plans: Billed[];
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=use.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use.d.ts","sources":["../use.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;CAWC,GACD,cAAc,GAAG,2BAAgC;AAWjD;2CAC2C,GAC3C,YAAY;EACV,MAAM;EACN,iEAAiE,GACjE,QAAQ;EACR,6BAA6B,GAC7B,MAAM,MAAM;EACZ,8DAA8D,GAC9D,SAAS,MAAM;EACf;4BAC0B,GAC1B,OAAO,MAAM;EACb,iDAAiD,GACjD,MAAM,MAAM;;AAGd;2DAC2D,GAC3D,YAAY;EACV,QAAQ;EACR,sBAAsB,GACtB,OAAO,MAAM;EACb,uCAAuC,GACvC,OAAO,MAAM;EACb,kCAAkC,GAClC,OAAO,MAAM;EACb,OAAO"}
|
package/_dist/views.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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 { type Registry } from "@yaks/render";
|
|
10
|
+
import type { View } from "@yaks/inspect";
|
|
11
|
+
// @ts-types="@yaks/ui/lucide"
|
|
12
|
+
import Coins from "lucide/dist/esm/icons/coins.mjs";
|
|
13
|
+
import { type Spend } from "./tally.js";
|
|
14
|
+
/** What a day's row says in a line. */ export declare let facts: (s: Spend) => string[];
|
|
15
|
+
/** Draws a day's tally by name. */ export declare let views: Registry;
|
|
16
|
+
/** The page and a day's page, loaded when first drawn. */ export declare let inspectViews: () => Promise<View[]>;
|
|
17
|
+
/** The page the sidebar offers: where the tokens went, over the days. */ export declare let destinations: readonly [{
|
|
18
|
+
readonly key: string;
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly icon: string;
|
|
21
|
+
readonly query: string;
|
|
22
|
+
readonly view: string;
|
|
23
|
+
}];
|
|
24
|
+
/** The glyph it wears there. */ export declare let icons: {
|
|
25
|
+
readonly coins: any;
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=views.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"views.d.ts","sources":["../views.ts"],"names":[],"mappings":"AAAA;;;;;;;;CAQC,GACD,SAAyB,KAAK,QAAQ,4BAAqD;AAG3F,cAAc,IAAI,6BAAkC;AAEpD,8BAA8B;AAC9B,OAAO,6CAAuD;AAC9D,SAAiB,KAAK,KAAK,qBAA6B;AASxD,qCAAqC,GACrC,OAAO,YAAI,QAAS,GAAG,UAAQ,MAAM,GAQpC;AAqBD,iCAAiC,GACjC,OAAO,YAAI,OAAO,SAqBhB;AAEF,wDAAwD,GACxD,OAAO,YAAI,oBAAmB,QAAQ,QACW;AAEjD,uEAAuE,GACvE,OAAO,YAAI;aAEP;aACA;aACA;aACA;aACA;KAEH;AAED,8BAA8B,GAC9B,OAAO,YAAI;WAAU;EAAc"}
|
package/_dist/vocab.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { VocabDoc } from "@yaks/vocab";
|
|
2
|
+
/** The spend vocabulary: `spend{day, requests, tokens, dollars, parts,
|
|
3
|
+
* sessions, tasks, models, entries}`, `untallied{day}`, and the `spend_show` and
|
|
4
|
+
* `spend_backfill` tools, in the form `loadVocab` accepts. */ export declare let spendDoc: VocabDoc;
|
|
5
|
+
/** What this package is: its manifest's description. */ export declare let description: string;
|
|
6
|
+
/** Every document this package declares. */ export declare let docs: VocabDoc[];
|
|
7
|
+
//# sourceMappingURL=vocab.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,2BAAgC;AAItD;;4DAE4D,GAC5D,OAAO,YAAI,UAAU,SAA0B;AAE/C,sDAAsD,GACtD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,0CAA0C,GAC1C,OAAO,YAAI,MAAM,WAAuB"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan limits and the span of days a report covers: pure, for the page and
|
|
3
|
+
* the report alike. A limit's pace is how much of its window is gone, which
|
|
4
|
+
* is how much of it an even spend would have used by now.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/ import type { Bundle, Eid } from "@yaks/graph";
|
|
8
|
+
import type { Spend } from "./tally.js";
|
|
9
|
+
/** A plan limit, as its `rate_limit` row reads, its provider named. */ export type Limit = {
|
|
10
|
+
provider: string;
|
|
11
|
+
/** the plan's: its account's eid, else, for a reading that named no
|
|
12
|
+
* account, its provider's */ of: Eid;
|
|
13
|
+
/** the model whose requests it meters, where it is a limit on one model
|
|
14
|
+
* and not on the whole plan */ model?: Eid;
|
|
15
|
+
/** what the limit is called: its plan's name (its account's, else its
|
|
16
|
+
* provider's); a limit on one model, the model's, after its account's
|
|
17
|
+
* where the provider keeps several */ name: string;
|
|
18
|
+
/** minutes: 10080 a week, 300 five hours */ window: number;
|
|
19
|
+
/** percent used */ used: number;
|
|
20
|
+
resets?: string;
|
|
21
|
+
at?: string;
|
|
22
|
+
};
|
|
23
|
+
/** The UTC day after `day`, and `n` days from it. */ export declare let after: (day: string, n?: number) => string;
|
|
24
|
+
/** The UTC day a moment falls on. */ export declare let dayOf: (at: number | string) => string;
|
|
25
|
+
/**
|
|
26
|
+
* The limits `rate_limit` rows hold, each plan's longest window first, by
|
|
27
|
+
* `names`: the providers', the accounts' and the models'.
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { limitsOf } from './window.ts'
|
|
31
|
+
* import { equal } from '@yaks/testing'
|
|
32
|
+
*
|
|
33
|
+
* let row = (more: object) =>
|
|
34
|
+
* ({ entity: { eid: 'r' }, rate_limit: { provider: 'p', account: 'a', window: 10080, ...more } })
|
|
35
|
+
* let names = new Map([['p', 'claude'], ['a', 'claude'], ['m', 'fable']])
|
|
36
|
+
* equal(limitsOf([row({ used: 6, model: 'm' }), row({ used: 43 })], names).map((l) => [l.name, l.used]),
|
|
37
|
+
* [['claude', 43], ['fable', 6]])
|
|
38
|
+
* equal(limitsOf([row({ model: 'm' })], new Map([...names, ['a', '.claude-work']]))[0].name,
|
|
39
|
+
* '.claude-work fable')
|
|
40
|
+
* ```
|
|
41
|
+
*/ export declare let limitsOf: (rows: Bundle[], names: Map<Eid, string>) => Limit[];
|
|
42
|
+
/**
|
|
43
|
+
* How much of a limit's window is gone at `now`, 0 to 1: what an even spend
|
|
44
|
+
* would have used by now.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { paceOf } from './window.ts'
|
|
48
|
+
* import { equal } from '@yaks/testing'
|
|
49
|
+
*
|
|
50
|
+
* let week = { provider: 'claude', of: 'c', name: 'claude', window: 10080, used: 25,
|
|
51
|
+
* resets: '2026-10-27T11:00:00Z' }
|
|
52
|
+
* equal(paceOf(week, Date.parse('2026-10-23T23:00:00Z')), 0.5)
|
|
53
|
+
* ```
|
|
54
|
+
*/ export declare let paceOf: (l: Limit, now: number) => number;
|
|
55
|
+
/** Whether a reading is of the window now running: one taken before the
|
|
56
|
+
* window last reset says nothing of this one. */ export declare let current: (l: Limit, now: number) => boolean;
|
|
57
|
+
/**
|
|
58
|
+
* The UTC day a plan's running week began, for a weekly limit that says when
|
|
59
|
+
* it resets: a reading of a week gone by still says when this one began.
|
|
60
|
+
*
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { weekOf } from './window.ts'
|
|
63
|
+
* import { equal } from '@yaks/testing'
|
|
64
|
+
*
|
|
65
|
+
* let week = { provider: 'claude', of: 'c', name: 'claude', window: 10080, used: 25,
|
|
66
|
+
* resets: '2026-10-27T11:00:00Z' }
|
|
67
|
+
* equal(weekOf(week, Date.parse('2026-10-21T12:00:00Z')), '2026-10-20')
|
|
68
|
+
* equal(weekOf(week, Date.parse('2026-10-29T12:00:00Z')), '2026-10-27')
|
|
69
|
+
* equal(weekOf({ ...week, window: 300 }, Date.parse('2026-10-21T12:00:00Z')), undefined)
|
|
70
|
+
* ```
|
|
71
|
+
*/ export declare let weekOf: (l: Limit, now: number) => string | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* The days a report covers: from `since`, else the UTC day the Claude plan's
|
|
74
|
+
* week began, else six days before; to `until`, else today.
|
|
75
|
+
*
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { span } from './window.ts'
|
|
78
|
+
* import { equal } from '@yaks/testing'
|
|
79
|
+
*
|
|
80
|
+
* let week = { provider: 'claude', of: 'c', name: 'claude', window: 10080, used: 25,
|
|
81
|
+
* resets: '2026-10-27T11:00:00Z' }
|
|
82
|
+
* let now = Date.parse('2026-10-21T12:00:00Z')
|
|
83
|
+
* equal(span([week], now), ['2026-10-20', '2026-10-21'])
|
|
84
|
+
* equal(span([], now), ['2026-10-15', '2026-10-21'])
|
|
85
|
+
* ```
|
|
86
|
+
*/ export declare let span: (limits: Limit[], now: number, since?: string, until?: string) => [string, string];
|
|
87
|
+
/** What a day's requests ran up on a limit, and each session's part of it:
|
|
88
|
+
* its plan's, or its model's on that plan where it meters one model. */ export declare let billedOn: (l: Limit, d: Spend) => {
|
|
89
|
+
dollars: number;
|
|
90
|
+
sessions: Record<Eid, number>;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* What a dollar is worth in each weekly limit: the percent of the week its
|
|
94
|
+
* latest reading says is used, over the dollars the requests it meters ran up
|
|
95
|
+
* at list price from the day the week began to the day of the reading. An
|
|
96
|
+
* estimate: the days are whole UTC days, and the dollars are what the tokens
|
|
97
|
+
* would cost bought one by one. A limit with no reading of this week, or no
|
|
98
|
+
* dollars in it, has none.
|
|
99
|
+
*
|
|
100
|
+
* ```ts
|
|
101
|
+
* import { limitRates } from './window.ts'
|
|
102
|
+
* import { empty } from './tally.ts'
|
|
103
|
+
* import { equal } from '@yaks/testing'
|
|
104
|
+
*
|
|
105
|
+
* let week = { provider: 'claude', of: 'c', name: 'claude', window: 10080, used: 30,
|
|
106
|
+
* resets: '2026-10-27T11:00:00Z', at: '2026-10-21T12:00:00Z' }
|
|
107
|
+
* let cap = { ...week, name: 'fable', model: 'f', used: 10 }
|
|
108
|
+
* let day = { ...empty('2026-10-21'), plans: { c: { dollars: 300, parts: {}, sessions: {}, tasks: {},
|
|
109
|
+
* models: { f: 50, o: 250 }, entries: {} } } }
|
|
110
|
+
* let rate = limitRates([week, cap], [day], Date.parse('2026-10-21T13:00:00Z'))
|
|
111
|
+
* equal([rate.get(week), rate.get(cap)], [0.1, 0.2])
|
|
112
|
+
* ```
|
|
113
|
+
*/ export declare let limitRates: (limits: Limit[], days: Spend[], now: number) => Map<Limit, number>;
|
|
114
|
+
/**
|
|
115
|
+
* What a dollar of each plan is worth in its weekly limit ({@link
|
|
116
|
+
* limitRates}), by the plan's eid (its account's): the limit on the whole
|
|
117
|
+
* plan, not one on a model.
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { rates } from './window.ts'
|
|
121
|
+
* import { empty } from './tally.ts'
|
|
122
|
+
* import { equal } from '@yaks/testing'
|
|
123
|
+
*
|
|
124
|
+
* let week = { provider: 'claude', of: 'c', name: 'claude', window: 10080, used: 30,
|
|
125
|
+
* resets: '2026-10-27T11:00:00Z', at: '2026-10-21T12:00:00Z' }
|
|
126
|
+
* let day = (d: string, dollars: number) => ({ ...empty(d),
|
|
127
|
+
* plans: { c: { dollars, parts: {}, sessions: {}, tasks: {}, models: {}, entries: {} } } })
|
|
128
|
+
* let days = [day('2026-10-19', 900), day('2026-10-20', 100), day('2026-10-21', 200)]
|
|
129
|
+
* equal(rates([week], days, Date.parse('2026-10-21T13:00:00Z')).get('c'), 0.1)
|
|
130
|
+
* ```
|
|
131
|
+
*/ export declare let rates: (limits: Limit[], days: Spend[], now: number) => Map<Eid, number>;
|
|
132
|
+
//# sourceMappingURL=window.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"window.d.ts","sources":["../window.ts"],"names":[],"mappings":"AAAA;;;;;;CAMC,GACD,cAAc,MAAM,EAAE,GAAG,2BAAgC;AAEzD,cAAoB,KAAK,qBAAoB;AAE7C,qEAAqE,GACrE,YAAY;EACV,UAAU,MAAM;EAChB;6BAC2B,GAC3B,IAAI;EACJ;+BAC6B,GAC7B,QAAQ;EACR;;sCAEoC,GACpC,MAAM,MAAM;EACZ,0CAA0C,GAC1C,QAAQ,MAAM;EACd,iBAAiB,GACjB,MAAM,MAAM;EACZ,SAAS,MAAM;EACf,KAAK,MAAM;;AAUb,mDAAmD,GACnD,OAAO,YAAI,QAAS,KAAK,MAAM,EAAE,eAAQ,MAAM,CAE9B;AAEjB,mCAAmC,GACnC,OAAO,YAAI,QAAS,IAAI,MAAM,GAAG,MAAM,KAAG,MAAM,CACP;AAqDzC;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,YAAI,WAAY,MAAM,UAAU,OAAO,IAAI,KAAK,MAAM,MAAI,QAqB9D;AAMH;;;;;;;;;;;;CAYC,GACD,OAAO,YAAI,SAAU,GAAG,OAAO,KAAK,MAAM,KAAG,MAAM,CAKlD;AAED;+CAC+C,GAC/C,OAAO,YAAI,UAAW,GAAG,OAAO,KAAK,MAAM,KAAG,OAAO,CACZ;AAEzC;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,SAAU,GAAG,OAAO,KAAK,MAAM,KAAG,MAAM,GAAG,SAAS,CAGhD;AAEf;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,OACT,QAAQ,SACR,KAAK,MAAM,EACX,QAAQ,MAAM,EACd,QAAQ,MAAM,MACZ,MAAM,EAAE,MAAM,EAMjB;AAED;sEACsE,GACtE,OAAO,YAAI,WACT,GAAG,OACH,GAAG;EACA,SAAS,MAAM;EAAE,UAAU,OAAO,KAAK,MAAM;EAQjD;AAED;;;;;;;;;;;;;;;;;;;;;CAqBC,GACD,OAAO,YAAI,aACT,QAAQ,SACR,MAAM,SACN,KAAK,MAAM,KACV,IAAI,OAAO,MAAM,EAOf;AAEL;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,YAAI,QACT,QAAQ,SACR,MAAM,SACN,KAAK,MAAM,KACV,IAAI,KAAK,MAAM,EAIf"}
|
package/_dist/words.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Numbers and rows as a reader takes them in: dollars, token counts, and
|
|
3
|
+
* what a row a tally names is, in a line. Pure, for the page and the report.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/ import type { Bundle } from "@yaks/graph";
|
|
7
|
+
/**
|
|
8
|
+
* Dollars, to the cent below a hundred and whole above.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { dollars } from './words.ts'
|
|
12
|
+
* import { equal } from '@yaks/testing'
|
|
13
|
+
*
|
|
14
|
+
* equal([dollars(4.216), dollars(1234.5)], ['$4.22', '$1,235'])
|
|
15
|
+
* ```
|
|
16
|
+
*/ export declare let dollars: (n?: number) => string;
|
|
17
|
+
/**
|
|
18
|
+
* A percent, to a tenth, and a share too small to show as less than that.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { percent } from './words.ts'
|
|
22
|
+
* import { equal } from '@yaks/testing'
|
|
23
|
+
*
|
|
24
|
+
* equal([percent(12.345), percent(0.04), percent(0)], ['12.3%', '<0.1%', '0%'])
|
|
25
|
+
* ```
|
|
26
|
+
*/ export declare let percent: (n?: number) => string;
|
|
27
|
+
/**
|
|
28
|
+
* A count of tokens, to three figures.
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { tokens } from './words.ts'
|
|
32
|
+
* import { equal } from '@yaks/testing'
|
|
33
|
+
*
|
|
34
|
+
* equal([tokens(950), tokens(20600), tokens(53100000), tokens(1.2e9)], ['950', '20.6k', '53.1M', '1.2B'])
|
|
35
|
+
* ```
|
|
36
|
+
*/ export declare let tokens: (n?: number) => string;
|
|
37
|
+
/** What a row a tally names is, in a line: a model by its name, a task or
|
|
38
|
+
* anything with a title by its id and title, a session by its id and the
|
|
39
|
+
* harness's id for it, an entry by what it says. */ export declare let label: (b: Bundle, id: string) => string;
|
|
40
|
+
//# sourceMappingURL=words.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"words.d.ts","sources":["../words.ts"],"names":[],"mappings":"AAAA;;;;;CAKC,GACD,cAAc,MAAM,2BAAgC;AAEpD;;;;;;;;;CASC,GACD,OAAO,YAAI,UAAW,eAAQ,MAAM,CACyC;AAE7E;;;;;;;;;CASC,GACD,OAAO,YAAI,UAAW,eAAQ,MAAM,CACiB;AAErD;;;;;;;;;CASC,GACD,OAAO,YAAI,SAAU,eAAQ,MAAM,CAUlC;AAUD;;kDAEkD,GAClD,OAAO,YAAI,QAAS,GAAG,QAAQ,IAAI,MAAM,KAAG,MAAM,CAS1C"}
|
package/budget.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { env } from "@yaks/process/env";
|
|
2
|
+
import * as io from "@yaks/file/io";
|
|
3
|
+
import { identityEid } from "@yaks/graph/identity";
|
|
4
|
+
import { accountEid, limitEid } from "@yaks/model";
|
|
5
|
+
import { home } from "@yaks/session/files";
|
|
6
|
+
let iso = (s)=>s && Number(s) ? new Date(Number(s) * 1000).toISOString() : undefined;
|
|
7
|
+
/**
|
|
8
|
+
* A cache file's reading, as rows: of the account it names, else of the
|
|
9
|
+
* account in `dir`.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { limits } from './budget.ts'
|
|
13
|
+
* import { accountEid } from '@yaks/model'
|
|
14
|
+
* import { identityEid } from '@yaks/graph/identity'
|
|
15
|
+
* import { equal } from '@yaks/testing'
|
|
16
|
+
*
|
|
17
|
+
* let [, , week] = limits('SEVEN_DAY_PCT=25\nSEVEN_DAY_RESET=1793098800\nUPDATED_AT=1792531227\n', '/u/.claude')
|
|
18
|
+
* equal(week.rate_limit, {
|
|
19
|
+
* provider: identityEid('provider', ['claude']),
|
|
20
|
+
* account: accountEid('/u/.claude'),
|
|
21
|
+
* window: 10080,
|
|
22
|
+
* used: 25,
|
|
23
|
+
* resets: '2026-10-27T11:00:00.000Z',
|
|
24
|
+
* at: '2026-10-20T21:20:27.000Z',
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*/ export let limits = (text, dir)=>{
|
|
28
|
+
let v = Object.fromEntries(text.split('\n').map((l)=>l.split('=')).filter((p)=>p.length == 2));
|
|
29
|
+
let provider = identityEid('provider', [
|
|
30
|
+
'claude'
|
|
31
|
+
]);
|
|
32
|
+
let of = v.CLAUDE_CONFIG_DIR || dir;
|
|
33
|
+
let account = of ? accountEid(of) : undefined;
|
|
34
|
+
let at = iso(v.UPDATED_AT);
|
|
35
|
+
let row = (window, used, resets)=>used == null || used === '' ? [] : [
|
|
36
|
+
{
|
|
37
|
+
entity: {
|
|
38
|
+
eid: limitEid(provider, window, account)
|
|
39
|
+
},
|
|
40
|
+
rate_limit: {
|
|
41
|
+
provider,
|
|
42
|
+
...account ? {
|
|
43
|
+
account
|
|
44
|
+
} : {},
|
|
45
|
+
window,
|
|
46
|
+
used: Number(used),
|
|
47
|
+
...iso(resets) ? {
|
|
48
|
+
resets: iso(resets)
|
|
49
|
+
} : {},
|
|
50
|
+
...at ? {
|
|
51
|
+
at
|
|
52
|
+
} : {}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
];
|
|
56
|
+
let rows = [
|
|
57
|
+
...row(10080, v.SEVEN_DAY_PCT, v.SEVEN_DAY_RESET),
|
|
58
|
+
...row(300, v.FIVE_HOUR_PCT, v.FIVE_HOUR_RESET)
|
|
59
|
+
];
|
|
60
|
+
return rows.length ? [
|
|
61
|
+
{
|
|
62
|
+
entity: {
|
|
63
|
+
eid: provider
|
|
64
|
+
},
|
|
65
|
+
provider: {
|
|
66
|
+
name: 'claude'
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
...account ? [
|
|
70
|
+
{
|
|
71
|
+
entity: {
|
|
72
|
+
eid: account
|
|
73
|
+
},
|
|
74
|
+
account: {
|
|
75
|
+
home: of,
|
|
76
|
+
provider
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
] : [],
|
|
80
|
+
...rows
|
|
81
|
+
] : [];
|
|
82
|
+
};
|
|
83
|
+
/** Where claude-status keeps its readings. */ export let usageDir = ()=>{
|
|
84
|
+
return env.get('CLAUDE_USAGE_DIR') ?? (env.get('HOME') ? `${env.get('HOME')}/.cache/claude-usage` : undefined);
|
|
85
|
+
};
|
|
86
|
+
// The newest reading of each account in a directory, or none.
|
|
87
|
+
let newest = (dir)=>{
|
|
88
|
+
let best = new Map();
|
|
89
|
+
try {
|
|
90
|
+
for (let f of io.readDirSync(dir)){
|
|
91
|
+
if (!f.isFile || !f.name.startsWith('usage.')) continue;
|
|
92
|
+
let path = `${dir}/${f.name}`;
|
|
93
|
+
let at = io.statSync(path).mtime?.getTime() ?? 0;
|
|
94
|
+
let text = io.readTextFileSync(path);
|
|
95
|
+
let of = text.match(/^CLAUDE_CONFIG_DIR=(.*)$/m)?.[1] ?? '';
|
|
96
|
+
if (at > (best.get(of)?.[0] ?? -1)) best.set(of, [
|
|
97
|
+
at,
|
|
98
|
+
text
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
} catch {
|
|
102
|
+
return [] // no cache: Claude Code has no status line here
|
|
103
|
+
;
|
|
104
|
+
}
|
|
105
|
+
return [
|
|
106
|
+
...best.values()
|
|
107
|
+
].map(([, text])=>text);
|
|
108
|
+
};
|
|
109
|
+
/** Keep each account's newest reading as rows, where it is newer than the
|
|
110
|
+
* one kept. */ export let read = async (g, dir = usageDir())=>{
|
|
111
|
+
let rows = (dir ? newest(dir) : []).flatMap((text)=>limits(text, home('claude')));
|
|
112
|
+
let readings = rows.filter((b)=>b.rate_limit);
|
|
113
|
+
if (!readings.length) return;
|
|
114
|
+
let kept = new Map((await g.get(readings.map((b)=>b.entity.eid), [
|
|
115
|
+
'rate_limit'
|
|
116
|
+
])).filter((r)=>r).map((r)=>[
|
|
117
|
+
r.entity.eid,
|
|
118
|
+
r
|
|
119
|
+
]));
|
|
120
|
+
let at = (b)=>String(b?.rate_limit?.at ?? '');
|
|
121
|
+
let newer = readings.filter((b)=>at(b) > at(kept.get(b.entity.eid)));
|
|
122
|
+
if (newer.length) {
|
|
123
|
+
await g.apply([
|
|
124
|
+
...rows.filter((b)=>!b.rate_limit),
|
|
125
|
+
...newer
|
|
126
|
+
], {
|
|
127
|
+
trusted: true
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
//# sourceMappingURL=budget.js.map
|
package/budget.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.js","sources":["./budget.ts"],"names":[],"mappings":"AAAA,SAAS,GAAG,iCAAsC;AAClD,YAAY,6BAAkC;AAmB9C,SAAS,WAAW,oCAAyC;AAC7D,SAAS,UAAU,EAAE,QAAQ,2BAAgC;AAC7D,SAAS,IAAI,mCAAwC;AAErD,IAAI,MAAM,CAAC,IACT,KAAK,OAAO,KAAK,IAAI,KAAK,OAAO,KAAK,MAAM,WAAW,KAAK;AAE9D;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,IAAI,SAAS,CAAC,MAAc;EACjC,IAAI,IAAI,OAAO,WAAW,CACxB,KAAK,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,IAAM,EAAE,KAAK,CAAC,MAAM,MAAM,CAAC,CAAC,IAAM,EAAE,MAAM,IAAI;EAEtE,IAAI,WAAgB,YAAY,YAAY;IAAC;GAAS;EACtD,IAAI,KAAK,EAAE,iBAAiB,IAAI;EAChC,IAAI,UAAU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AAS;IACX;EACF;AACF,EAAC"}
|
package/budget.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { env } from '@yaks/process/env'
|
|
2
|
+
import * as io from '@yaks/file/io'
|
|
3
|
+
/**
|
|
4
|
+
* The Claude plan's limits, as `rate_limit` rows (@yaks/model). Claude Code
|
|
5
|
+
* hands its status line the account's limits, and claude-status keeps the
|
|
6
|
+
* last reading in a cache file per project (`usage.<label>` in
|
|
7
|
+
* `~/.cache/claude-usage`): `SEVEN_DAY_PCT`, `SEVEN_DAY_RESET`,
|
|
8
|
+
* `FIVE_HOUR_PCT`, `FIVE_HOUR_RESET` and `UPDATED_AT`, times in epoch
|
|
9
|
+
* seconds. The limits are the account's, so each account's newest file is its
|
|
10
|
+
* reading: of the account a `CLAUDE_CONFIG_DIR` line in it names, else of the
|
|
11
|
+
* one `yak serve`'s environment signs in with. Only an interactive session has a
|
|
12
|
+
* status line: a `claude -p` run reports its limits in its stream, read with
|
|
13
|
+
* the run (@yaks/session's `claude` reader), and the newer of the two
|
|
14
|
+
* readings is the one kept. Codex's arrive with its sessions, each account's
|
|
15
|
+
* in its own (@yaks/session's importer). A native session's requests carry
|
|
16
|
+
* theirs on each reply (@yaks/model `Reply.limits`), kept as it is recorded.
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
import type { Bundle, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
21
|
+
import { identityEid } from '@yaks/graph/identity'
|
|
22
|
+
import { accountEid, limitEid } from '@yaks/model'
|
|
23
|
+
import { home } from '@yaks/session/files'
|
|
24
|
+
|
|
25
|
+
let iso = (s?: string) =>
|
|
26
|
+
s && Number(s) ? new Date(Number(s) * 1000).toISOString() : undefined
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A cache file's reading, as rows: of the account it names, else of the
|
|
30
|
+
* account in `dir`.
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { limits } from './budget.ts'
|
|
34
|
+
* import { accountEid } from '@yaks/model'
|
|
35
|
+
* import { identityEid } from '@yaks/graph/identity'
|
|
36
|
+
* import { equal } from '@yaks/testing'
|
|
37
|
+
*
|
|
38
|
+
* let [, , week] = limits('SEVEN_DAY_PCT=25\nSEVEN_DAY_RESET=1793098800\nUPDATED_AT=1792531227\n', '/u/.claude')
|
|
39
|
+
* equal(week.rate_limit, {
|
|
40
|
+
* provider: identityEid('provider', ['claude']),
|
|
41
|
+
* account: accountEid('/u/.claude'),
|
|
42
|
+
* window: 10080,
|
|
43
|
+
* used: 25,
|
|
44
|
+
* resets: '2026-10-27T11:00:00.000Z',
|
|
45
|
+
* at: '2026-10-20T21:20:27.000Z',
|
|
46
|
+
* })
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export let limits = (text: string, dir?: string): Bundle[] => {
|
|
50
|
+
let v = Object.fromEntries(
|
|
51
|
+
text.split('\n').map((l) => l.split('=')).filter((p) => p.length == 2),
|
|
52
|
+
) as Record<string, string>
|
|
53
|
+
let provider: Eid = identityEid('provider', ['claude'])
|
|
54
|
+
let of = v.CLAUDE_CONFIG_DIR || dir
|
|
55
|
+
let account = of ? accountEid(of) : undefined
|
|
56
|
+
let at = iso(v.UPDATED_AT)
|
|
57
|
+
let row = (window: number, used?: string, resets?: string): Bundle[] =>
|
|
58
|
+
used == null || used === '' ? [] : [{
|
|
59
|
+
entity: { eid: limitEid(provider, window, account) },
|
|
60
|
+
rate_limit: {
|
|
61
|
+
provider,
|
|
62
|
+
...account ? { account } : {},
|
|
63
|
+
window,
|
|
64
|
+
used: Number(used),
|
|
65
|
+
...iso(resets) ? { resets: iso(resets) } : {},
|
|
66
|
+
...at ? { at } : {},
|
|
67
|
+
},
|
|
68
|
+
}]
|
|
69
|
+
let rows = [
|
|
70
|
+
...row(10080, v.SEVEN_DAY_PCT, v.SEVEN_DAY_RESET),
|
|
71
|
+
...row(300, v.FIVE_HOUR_PCT, v.FIVE_HOUR_RESET),
|
|
72
|
+
]
|
|
73
|
+
return rows.length
|
|
74
|
+
? [
|
|
75
|
+
{ entity: { eid: provider }, provider: { name: 'claude' } },
|
|
76
|
+
...account
|
|
77
|
+
? [{ entity: { eid: account }, account: { home: of, provider } }]
|
|
78
|
+
: [],
|
|
79
|
+
...rows,
|
|
80
|
+
]
|
|
81
|
+
: []
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Where claude-status keeps its readings. */
|
|
85
|
+
export let usageDir = (): string | undefined => {
|
|
86
|
+
return env.get('CLAUDE_USAGE_DIR') ??
|
|
87
|
+
(env.get('HOME') ? `${env.get('HOME')}/.cache/claude-usage` : undefined)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The newest reading of each account in a directory, or none.
|
|
91
|
+
let newest = (dir: string): string[] => {
|
|
92
|
+
let best = new Map<string, [number, string]>()
|
|
93
|
+
try {
|
|
94
|
+
for (let f of io.readDirSync(dir)) {
|
|
95
|
+
if (!f.isFile || !f.name.startsWith('usage.')) continue
|
|
96
|
+
let path = `${dir}/${f.name}`
|
|
97
|
+
let at = io.statSync(path).mtime?.getTime() ?? 0
|
|
98
|
+
let text = io.readTextFileSync(path)
|
|
99
|
+
let of = text.match(/^CLAUDE_CONFIG_DIR=(.*)$/m)?.[1] ?? ''
|
|
100
|
+
if (at > (best.get(of)?.[0] ?? -1)) best.set(of, [at, text])
|
|
101
|
+
}
|
|
102
|
+
} catch {
|
|
103
|
+
return [] // no cache: Claude Code has no status line here
|
|
104
|
+
}
|
|
105
|
+
return [...best.values()].map(([, text]) => text)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Keep each account's newest reading as rows, where it is newer than the
|
|
109
|
+
* one kept. */
|
|
110
|
+
export let read = async (g: Graph, dir = usageDir()): Promise<void> => {
|
|
111
|
+
let rows = (dir ? newest(dir) : []).flatMap((text) =>
|
|
112
|
+
limits(text, home('claude'))
|
|
113
|
+
)
|
|
114
|
+
let readings = rows.filter((b) => b.rate_limit)
|
|
115
|
+
if (!readings.length) return
|
|
116
|
+
let kept = new Map(
|
|
117
|
+
(await g.get(readings.map((b) => b.entity.eid), ['rate_limit']))
|
|
118
|
+
.filter((r) => r).map((r) => [r!.entity.eid, r!]),
|
|
119
|
+
)
|
|
120
|
+
let at = (b?: Bundle) =>
|
|
121
|
+
String((b?.rate_limit as { at?: string } | undefined)?.at ?? '')
|
|
122
|
+
let newer = readings.filter((b) => at(b) > at(kept.get(b.entity.eid)))
|
|
123
|
+
if (newer.length) {
|
|
124
|
+
await g.apply([...rows.filter((b) => !b.rate_limit), ...newer], {
|
|
125
|
+
trusted: true,
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
}
|