@yaks/model 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 +868 -2
- package/_dist/afford.d.ts +69 -0
- package/_dist/afford.d.ts.map +1 -0
- package/_dist/attribute.d.ts +52 -0
- package/_dist/attribute.d.ts.map +1 -0
- package/_dist/batch.d.ts +28 -0
- package/_dist/batch.d.ts.map +1 -0
- package/_dist/cache.d.ts +15 -0
- package/_dist/cache.d.ts.map +1 -0
- package/_dist/deferred.d.ts +9 -0
- package/_dist/deferred.d.ts.map +1 -0
- package/_dist/graph.d.ts +3 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/image.d.ts +46 -0
- package/_dist/image.d.ts.map +1 -0
- package/_dist/inference.d.ts +58 -0
- package/_dist/inference.d.ts.map +1 -0
- package/_dist/limits.d.ts +142 -0
- package/_dist/limits.d.ts.map +1 -0
- package/_dist/media.d.ts +28 -0
- package/_dist/media.d.ts.map +1 -0
- package/_dist/message.d.ts +5 -0
- package/_dist/message.d.ts.map +1 -0
- package/_dist/mod.d.ts +298 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/offers.d.ts +19 -0
- package/_dist/offers.d.ts.map +1 -0
- package/_dist/onward.d.ts +77 -0
- package/_dist/onward.d.ts.map +1 -0
- package/_dist/prices.d.ts +27 -0
- package/_dist/prices.d.ts.map +1 -0
- package/_dist/provider.d.ts +49 -0
- package/_dist/provider.d.ts.map +1 -0
- package/_dist/results.d.ts +21 -0
- package/_dist/results.d.ts.map +1 -0
- package/_dist/route.d.ts +189 -0
- package/_dist/route.d.ts.map +1 -0
- package/_dist/sse.d.ts +4 -0
- package/_dist/sse.d.ts.map +1 -0
- package/_dist/stall.d.ts +47 -0
- package/_dist/stall.d.ts.map +1 -0
- package/_dist/step.d.ts +71 -0
- package/_dist/step.d.ts.map +1 -0
- package/_dist/submission.d.ts +7 -0
- package/_dist/submission.d.ts.map +1 -0
- package/_dist/vocab.d.ts +82 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/windows.d.ts +6 -0
- package/_dist/windows.d.ts.map +1 -0
- package/afford.js +135 -0
- package/afford.js.map +1 -0
- package/afford.ts +208 -0
- package/attribute.js +109 -0
- package/attribute.js.map +1 -0
- package/attribute.ts +120 -0
- package/batch.js +8 -0
- package/batch.js.map +1 -0
- package/batch.ts +37 -0
- package/cache.js +28 -0
- package/cache.js.map +1 -0
- package/cache.ts +53 -0
- package/deferred.js +29 -0
- package/deferred.js.map +1 -0
- package/deferred.ts +54 -0
- package/deno.json +1 -0
- package/graph.js +8 -0
- package/graph.js.map +1 -0
- package/graph.ts +9 -0
- package/image.js +118 -0
- package/image.js.map +1 -0
- package/image.ts +108 -0
- package/inference.js +119 -0
- package/inference.js.map +1 -0
- package/inference.ts +223 -0
- package/limits.js +204 -0
- package/limits.js.map +1 -0
- package/limits.ts +230 -0
- package/media.js +76 -0
- package/media.js.map +1 -0
- package/media.ts +139 -0
- package/message.js +21 -0
- package/message.js.map +1 -0
- package/message.ts +17 -0
- package/mod.js +396 -0
- package/mod.js.map +1 -0
- package/mod.ts +695 -0
- package/offers.js +53 -0
- package/offers.js.map +1 -0
- package/offers.ts +84 -0
- package/onward.js +110 -0
- package/onward.js.map +1 -0
- package/onward.ts +123 -0
- package/package.json +53 -4
- package/prices.js +61 -0
- package/prices.js.map +1 -0
- package/prices.json +643 -0
- package/prices.ts +60 -0
- package/provider.js +6 -0
- package/provider.js.map +1 -0
- package/provider.ts +89 -0
- package/results.js +51 -0
- package/results.js.map +1 -0
- package/results.ts +47 -0
- package/route.js +263 -0
- package/route.js.map +1 -0
- package/route.ts +359 -0
- package/sse.js +71 -0
- package/sse.js.map +1 -0
- package/sse.ts +82 -0
- package/stall.js +166 -0
- package/stall.js.map +1 -0
- package/stall.ts +201 -0
- package/step.js +226 -0
- package/step.js.map +1 -0
- package/step.ts +265 -0
- package/submission.js +38 -0
- package/submission.js.map +1 -0
- package/submission.ts +38 -0
- package/vocab.js +83 -0
- package/vocab.js.map +1 -0
- package/vocab.json +613 -0
- package/vocab.ts +139 -0
- package/windows.js +35 -0
- package/windows.js.map +1 -0
- package/windows.ts +28 -0
package/provider.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// A provider as a plugin. A package that serves models exports `./provider`,
|
|
2
|
+
// and a host running conversations asks each plugin its config names for the
|
|
3
|
+
// models it serves, lending them what only the host has: its connections, the
|
|
4
|
+
// person's sign-ins, where generated media is kept and its settings. A plugin
|
|
5
|
+
// never says where it runs, and the host never names a provider.
|
|
6
|
+
|
|
7
|
+
import type { ArtifactStore } from 'jsr:@yaks/blob@0.2.36'
|
|
8
|
+
import type { Hold, Scope } from 'jsr:@yaks/fp@0.2.36'
|
|
9
|
+
import type { Affordable } from './afford.ts'
|
|
10
|
+
import type { Listed, MediaReceipts, Model } from './mod.ts'
|
|
11
|
+
|
|
12
|
+
/** What a host lends the providers it composes. */
|
|
13
|
+
export type Lent = {
|
|
14
|
+
/** the connections every provider shares (a pool the host closes) */
|
|
15
|
+
fetch: typeof fetch
|
|
16
|
+
/** the key the person signed in to `integration` with, if they did */
|
|
17
|
+
key: (integration: string) => Promise<string | undefined>
|
|
18
|
+
/** a fresh key for `integration`, when `stale` stopped working */
|
|
19
|
+
refresh: (integration: string, stale: string) => Promise<string | undefined>
|
|
20
|
+
/** where media a model generates is kept */
|
|
21
|
+
store: ArtifactStore
|
|
22
|
+
/** the host's settings, `OPENAI_API_KEY` among them */
|
|
23
|
+
env: (name: string) => string | undefined
|
|
24
|
+
/** where a call's private provider results are kept, so an interrupted
|
|
25
|
+
* call is delivered again without being bought again (a host that keeps
|
|
26
|
+
* none lends none) */
|
|
27
|
+
receipts?: MediaReceipts
|
|
28
|
+
/** who may share a read under way, as @yaks/fp `sharing` takes it: the
|
|
29
|
+
* host's current request or object; a host that lends none shares only
|
|
30
|
+
* finished reads */
|
|
31
|
+
scope?: Scope
|
|
32
|
+
/** keeps work a call leaves behind (a cancel sent once its reply is
|
|
33
|
+
* dropped) alive past its request, as a Worker's `waitUntil` does */
|
|
34
|
+
hold?: Hold
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** What a host holds a provider's own calls to ({@link Native}): each is
|
|
38
|
+
* quoted before it is made, held to what the quote affords of what remains,
|
|
39
|
+
* and settled at what it cost. */
|
|
40
|
+
export type Spending = {
|
|
41
|
+
/** the most `quote` affords of what remains, held until settled; refused
|
|
42
|
+
* (a `ModelError` coded `limit`) where nothing fits */
|
|
43
|
+
hold: (quote: (remaining: number) => Affordable | null) => Promise<{
|
|
44
|
+
value: Affordable
|
|
45
|
+
settle: (dollars: number, reported: boolean) => Promise<unknown>
|
|
46
|
+
}>
|
|
47
|
+
/** a model answered, as its provider lists it */
|
|
48
|
+
answered?: (name: string, info: Listed) => Promise<void>
|
|
49
|
+
/** a failure that is the host's to hear rather than the caller's, such as
|
|
50
|
+
* the account's own credit spent */
|
|
51
|
+
report?: (error: unknown, model: string) => void
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A provider's own door: one call to a model, as the provider's API takes
|
|
55
|
+
* it, answered with what the model said. */
|
|
56
|
+
export type Native = {
|
|
57
|
+
/** whether it answers `model`; a door that doesn't say answers every name */
|
|
58
|
+
serves?: (model: string) => boolean
|
|
59
|
+
run: (
|
|
60
|
+
model: string,
|
|
61
|
+
input: unknown,
|
|
62
|
+
options?: { extraHeaders?: Record<string, string> },
|
|
63
|
+
) => Promise<unknown>
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A `./provider` facet: the models a plugin serves, keyed by the
|
|
67
|
+
* `provider.name` each serves under, given what the host lends and the
|
|
68
|
+
* plugin's own options from the config. */
|
|
69
|
+
export type ProviderFacet = {
|
|
70
|
+
providers?: (
|
|
71
|
+
lent: Lent,
|
|
72
|
+
options: Record<string, unknown>,
|
|
73
|
+
) => Record<string, Model>
|
|
74
|
+
/** the hosts those models call with these options: where a host may send a
|
|
75
|
+
* credential it puts in their calls, and nowhere else */
|
|
76
|
+
hosts?: (options: Record<string, unknown>) => string[]
|
|
77
|
+
/** The options to carry when another host runs a single model request. */
|
|
78
|
+
modelOptions?: (
|
|
79
|
+
options: Record<string, unknown>,
|
|
80
|
+
model: string,
|
|
81
|
+
) => Record<string, unknown>
|
|
82
|
+
/** its own door, each call held to what `spending` affords, for a host
|
|
83
|
+
* that passes a caller's input to the provider as it is */
|
|
84
|
+
native?: (
|
|
85
|
+
lent: Lent,
|
|
86
|
+
options: Record<string, unknown>,
|
|
87
|
+
spending: Spending,
|
|
88
|
+
) => Native
|
|
89
|
+
}
|
package/results.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Completed provider evidence is saved before accounting or transcript delivery.
|
|
2
|
+
/** Keep completed answers with their original ask in its own graph. */ export let modelResults = (g)=>({
|
|
3
|
+
read: async (call)=>{
|
|
4
|
+
let [ask] = await g.get([
|
|
5
|
+
call
|
|
6
|
+
], [
|
|
7
|
+
'model_result'
|
|
8
|
+
]);
|
|
9
|
+
let body = ask?.model_result?.body;
|
|
10
|
+
return body == null ? undefined : JSON.parse(body);
|
|
11
|
+
},
|
|
12
|
+
pending: async (call)=>{
|
|
13
|
+
let [ask] = await g.get([
|
|
14
|
+
call
|
|
15
|
+
], [
|
|
16
|
+
'model_result'
|
|
17
|
+
]);
|
|
18
|
+
let pending = ask?.model_result?.pending;
|
|
19
|
+
return pending == null ? undefined : JSON.parse(pending);
|
|
20
|
+
},
|
|
21
|
+
acknowledge: async (call, evidence)=>{
|
|
22
|
+
await g.apply([
|
|
23
|
+
{
|
|
24
|
+
entity: {
|
|
25
|
+
eid: call
|
|
26
|
+
},
|
|
27
|
+
model_result: {
|
|
28
|
+
pending: JSON.stringify(evidence)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
], {
|
|
32
|
+
trusted: true
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
save: async (call, result)=>{
|
|
36
|
+
await g.apply([
|
|
37
|
+
{
|
|
38
|
+
entity: {
|
|
39
|
+
eid: call
|
|
40
|
+
},
|
|
41
|
+
model_result: {
|
|
42
|
+
body: JSON.stringify(result),
|
|
43
|
+
pending: null
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
], {
|
|
47
|
+
trusted: true
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
//# sourceMappingURL=results.js.map
|
package/results.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"results.js","sources":["./results.ts"],"names":[],"mappings":"AAAA,iFAAiF;AAoBjF,qEAAqE,GACrE,OAAO,IAAI,eAAe,CACxB,IAC2B,CAAC;IAC5B,MAAM,OAAO;MACX,IAAI,CAAC,IAAI,GAAG,MAAM,EAAE,GAAG,CAAC;QAAC;OAAK,EAAE;QAAC;OAAe;MAChD,IAAI,OAAQ,KAAK,cAAgD;MACjE,OAAO,QAAQ,OAAO,YAAY,KAAK,KAAK,CAAC;IAC/C;IACA,SAAS,OAAO;MACd,IAAI,CAAC,IAAI,GAAG,MAAM,EAAE,GAAG,CAAC;QAAC;OAAK,EAAE;QAAC;OAAe;MAChD,IAAI,UAAW,KAAK,cAAuC;MAC3D,OAAO,WAAW,OAAO,YAAY,KAAK,KAAK,CAAC;IAClD;IACA,aAAa,OAAO,MAAM;MACxB,MAAM,EAAE,KAAK,CAAC;QAAC;UACb,QAAQ;YAAE,KAAK;UAAK;UACpB,cAAc;YAAE,SAAS,KAAK,SAAS,CAAC;UAAU;QACpD;OAAE,EAAE;QAAE,SAAS;MAAK;IACtB;IACA,MAAM,OAAO,MAAM;MACjB,MAAM,EAAE,KAAK,CAAC;QAAC;UACb,QAAQ;YAAE,KAAK;UAAK;UACpB,cAAc;YAAE,MAAM,KAAK,SAAS,CAAC;YAAS,SAAS;UAAK;QAC9D;OAAE,EAAE;QAAE,SAAS;MAAK;IACtB;EACF,CAAC,EAAC"}
|
package/results.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Completed provider evidence is saved before accounting or transcript delivery.
|
|
2
|
+
import type { Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
3
|
+
import type { Mark, Reply } from './mod.ts'
|
|
4
|
+
import type { Price } from './vocab.ts'
|
|
5
|
+
|
|
6
|
+
export type ModelResult = { reply: Reply; mark?: Mark }
|
|
7
|
+
export type Acknowledgement = {
|
|
8
|
+
provider: string
|
|
9
|
+
mark: Mark
|
|
10
|
+
tariff?: Price
|
|
11
|
+
requestedAt: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type ModelResults = {
|
|
15
|
+
read(call: string): Promise<ModelResult | undefined>
|
|
16
|
+
save(call: string, result: ModelResult): Promise<void>
|
|
17
|
+
pending?(call: string): Promise<Acknowledgement | undefined>
|
|
18
|
+
acknowledge?(call: string, evidence: Acknowledgement): Promise<void>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Keep completed answers with their original ask in its own graph. */
|
|
22
|
+
export let modelResults = (
|
|
23
|
+
g: Pick<Graph, 'get' | 'apply'>,
|
|
24
|
+
): Required<ModelResults> => ({
|
|
25
|
+
read: async (call) => {
|
|
26
|
+
let [ask] = await g.get([call], ['model_result'])
|
|
27
|
+
let body = (ask?.model_result as { body?: string } | undefined)?.body
|
|
28
|
+
return body == null ? undefined : JSON.parse(body)
|
|
29
|
+
},
|
|
30
|
+
pending: async (call) => {
|
|
31
|
+
let [ask] = await g.get([call], ['model_result'])
|
|
32
|
+
let pending = (ask?.model_result as { pending?: string })?.pending
|
|
33
|
+
return pending == null ? undefined : JSON.parse(pending)
|
|
34
|
+
},
|
|
35
|
+
acknowledge: async (call, evidence) => {
|
|
36
|
+
await g.apply([{
|
|
37
|
+
entity: { eid: call },
|
|
38
|
+
model_result: { pending: JSON.stringify(evidence) },
|
|
39
|
+
}], { trusted: true })
|
|
40
|
+
},
|
|
41
|
+
save: async (call, result) => {
|
|
42
|
+
await g.apply([{
|
|
43
|
+
entity: { eid: call },
|
|
44
|
+
model_result: { body: JSON.stringify(result), pending: null },
|
|
45
|
+
}], { trusted: true })
|
|
46
|
+
},
|
|
47
|
+
})
|
package/route.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// The router: which of several offerings a default runs on. Pure, and its
|
|
2
|
+
// own module (`@yaks/model/route`) so what reads a default loads nothing else.
|
|
3
|
+
// The offerings are graph data, `routes` edges to `serves` edges, and so are
|
|
4
|
+
// the readings (`rate_limit`); the one who asks reads both and hands them
|
|
5
|
+
// here. Each rule orders the offerings it can speak for, best first, and the
|
|
6
|
+
// first rule that speaks for any answers. There are two: a pinned offering,
|
|
7
|
+
// then the plan with the most usage left. Which of a provider's accounts an
|
|
8
|
+
// offering runs on is chosen here too ({@link freest}).
|
|
9
|
+
let span = (window)=>window == 10080 ? 'week' : window == 300 ? 'five hours' : `${window} min`;
|
|
10
|
+
// A plan's reading, and its limit's on `model` where it has one: each one's
|
|
11
|
+
// longest window's, the newest where accounts differ.
|
|
12
|
+
let reading = (plan, readings, model)=>readings.filter((r)=>r.provider == plan && r.window > 0 && r.model == model).sort((a, b)=>b.window - a.window || (b.at ?? '').localeCompare(a.at ?? ''))[0];
|
|
13
|
+
// The offerings whose plan has a reading, the most usage left first: what is
|
|
14
|
+
// left of the plan, or of its limit on the offering's model where that is
|
|
15
|
+
// less. A window that has reset since its reading is unused.
|
|
16
|
+
let roomiest = (routes, { readings, now })=>routes.flatMap((route)=>{
|
|
17
|
+
let left = (r)=>r.resets && Date.parse(r.resets) <= now ? 100 : 100 - r.used;
|
|
18
|
+
let [r] = [
|
|
19
|
+
reading(route.plan, readings),
|
|
20
|
+
reading(route.plan, readings, route.model)
|
|
21
|
+
].filter((r)=>r != null).sort((a, b)=>left(a) - left(b));
|
|
22
|
+
if (!r) return [];
|
|
23
|
+
let note = `has ${left(r)}% of its ${span(r.window)} left`;
|
|
24
|
+
return [
|
|
25
|
+
{
|
|
26
|
+
left: left(r),
|
|
27
|
+
route,
|
|
28
|
+
note
|
|
29
|
+
}
|
|
30
|
+
];
|
|
31
|
+
}).sort((a, b)=>b.left - a.left).map(({ route, note })=>({
|
|
32
|
+
...route,
|
|
33
|
+
note
|
|
34
|
+
}));
|
|
35
|
+
// The pinned offering, the newest pin where more than one is.
|
|
36
|
+
let pinned = (routes)=>routes.filter((r)=>r.pinned).sort((a, b)=>b.pinned.localeCompare(a.pinned)).slice(0, 1).map((r)=>({
|
|
37
|
+
...r,
|
|
38
|
+
note: 'is pinned'
|
|
39
|
+
}));
|
|
40
|
+
let rules = [
|
|
41
|
+
pinned,
|
|
42
|
+
roomiest
|
|
43
|
+
];
|
|
44
|
+
/**
|
|
45
|
+
* The offering to ask, or nothing where no rule can say: no routes, or none
|
|
46
|
+
* pinned and no reading of any of their plans.
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { type Reading, route } from './route.ts'
|
|
50
|
+
* import { equal } from '@yaks/testing'
|
|
51
|
+
*
|
|
52
|
+
* let routes = [
|
|
53
|
+
* { provider: 'claude', model: 'opus', name: 'opus', plan: 'claude' },
|
|
54
|
+
* { provider: 'openai', model: 'sol', name: 'sol', plan: 'codex' },
|
|
55
|
+
* ]
|
|
56
|
+
* let week = (provider: string, used: number, resets = '2026-11-17T00:00:00Z') =>
|
|
57
|
+
* ({ provider, window: 10080, used, resets })
|
|
58
|
+
* let now = Date.parse('2026-11-11T00:00:00Z')
|
|
59
|
+
* let ask = (readings: Reading[]) =>
|
|
60
|
+
* route({ routes, readings, now })
|
|
61
|
+
*
|
|
62
|
+
* equal(ask([week('claude', 84), week('codex', 82)]), {
|
|
63
|
+
* provider: 'openai',
|
|
64
|
+
* model: 'sol',
|
|
65
|
+
* why: 'sol has 18% of its week left, opus has 16% of its week left',
|
|
66
|
+
* })
|
|
67
|
+
* // A week that has reset since it was read is unused.
|
|
68
|
+
* equal(ask([week('claude', 84, '2026-11-10T00:00:00Z'), week('codex', 82)])?.model, 'opus')
|
|
69
|
+
* // A plan nothing has read can't win, and with no reading nothing is chosen.
|
|
70
|
+
* equal(ask([week('claude', 99)])?.why, 'opus has 1% of its week left')
|
|
71
|
+
* equal(ask([]), undefined)
|
|
72
|
+
* // A limit on one model counts for that model where it leaves less.
|
|
73
|
+
* equal(ask([week('claude', 40), { ...week('claude', 95), model: 'opus' }, week('codex', 82)])?.model, 'sol')
|
|
74
|
+
* // A pinned offering wins over usage, and needs no reading.
|
|
75
|
+
* let pin = { ...routes[0], pinned: '2026-11-11T00:00:00Z' }
|
|
76
|
+
* equal(route({ routes: [pin, routes[1]], readings: [week('codex', 0)], now }), {
|
|
77
|
+
* provider: 'claude',
|
|
78
|
+
* model: 'opus',
|
|
79
|
+
* why: 'opus is pinned',
|
|
80
|
+
* })
|
|
81
|
+
* equal(route({ routes: [pin], readings: [], now })?.model, 'opus')
|
|
82
|
+
* ```
|
|
83
|
+
*/ export let route = (asked)=>{
|
|
84
|
+
let left = rules.map((rule)=>rule(asked.routes, asked)).find((routes)=>routes.length) ?? [];
|
|
85
|
+
return left.length ? {
|
|
86
|
+
provider: left[0].provider,
|
|
87
|
+
model: left[0].model,
|
|
88
|
+
why: left.map((r)=>`${r.name} ${r.note}`).join(', ')
|
|
89
|
+
} : undefined;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Where a conversation goes on once the plan `from` runs on is spent: of the
|
|
93
|
+
* offerings on another plan whose readings leave room, the one {@link route}
|
|
94
|
+
* chooses, else the first of them. Nothing where every other plan is spent
|
|
95
|
+
* too, or none is routed. The reason says which plan ran out.
|
|
96
|
+
*
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { onward } from './route.ts'
|
|
99
|
+
* import { equal } from '@yaks/testing'
|
|
100
|
+
*
|
|
101
|
+
* let routes = [
|
|
102
|
+
* { provider: 'claude', model: 'opus', name: 'opus', plan: 'claude' },
|
|
103
|
+
* { provider: 'openai', model: 'sol', name: 'sol', plan: 'codex' },
|
|
104
|
+
* { provider: 'grok', model: 'grok-5', name: 'grok-5', plan: 'grok' },
|
|
105
|
+
* ]
|
|
106
|
+
* let now = Date.parse('2026-11-23T12:00:00Z')
|
|
107
|
+
* let full = { provider: 'codex', window: 10080, used: 100, resets: '2026-11-25T00:00:00Z' }
|
|
108
|
+
* let from = { provider: 'claude', model: 'opus' }
|
|
109
|
+
* // With no readings, the next routed offering.
|
|
110
|
+
* equal(onward({ routes, readings: [], now, from }), {
|
|
111
|
+
* provider: 'openai', model: 'sol', why: "opus's plan is spent; sol is next",
|
|
112
|
+
* })
|
|
113
|
+
* // A plan read full is passed over.
|
|
114
|
+
* equal(onward({ routes, readings: [full], now, from })?.model, 'grok-5')
|
|
115
|
+
* equal(onward({ routes: routes.slice(0, 2), readings: [full], now, from }), undefined)
|
|
116
|
+
* ```
|
|
117
|
+
*/ export let onward = (asked)=>{
|
|
118
|
+
let { routes, readings, now, from } = asked;
|
|
119
|
+
let was = routes.find((r)=>r.provider == from.provider && (!from.model || r.model == from.model)) ?? routes.find((r)=>r.provider == from.provider);
|
|
120
|
+
let plan = was?.plan ?? from.provider;
|
|
121
|
+
let passed = new Set([
|
|
122
|
+
plan,
|
|
123
|
+
...asked.passed ?? []
|
|
124
|
+
]);
|
|
125
|
+
for (let r of routes)if (asked.passed?.has(r.provider)) passed.add(r.plan);
|
|
126
|
+
let full = (plan)=>readings.some((r)=>r.provider == plan && r.used >= 100 && !(r.resets && Date.parse(r.resets) <= now));
|
|
127
|
+
let open = routes.filter((r)=>(!asked.available || asked.available.has(r.provider)) && !passed.has(r.plan) && !full(r.plan));
|
|
128
|
+
if (!open.length) return undefined;
|
|
129
|
+
let chosen = route({
|
|
130
|
+
...asked,
|
|
131
|
+
routes: open
|
|
132
|
+
}) ?? {
|
|
133
|
+
provider: open[0].provider,
|
|
134
|
+
model: open[0].model,
|
|
135
|
+
why: `${open[0].name} is next`
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
...chosen,
|
|
139
|
+
why: `${was ? `${was.name}'s plan` : 'its plan'} is spent; ${chosen.why}`
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
const WEEK = 10080;
|
|
143
|
+
const HOURS = 300;
|
|
144
|
+
/**
|
|
145
|
+
* A window's usage against the share of it gone: `used` less the points
|
|
146
|
+
* `back` a reset gives back (Fable's share of Claude's week), set against the
|
|
147
|
+
* share of the window gone by `now`. A window that resets at an unknown time
|
|
148
|
+
* is taken as all gone. The pace `claude-budget` prints (`bin/pace.ts`) and
|
|
149
|
+
* the one {@link freest} picks an account by.
|
|
150
|
+
*
|
|
151
|
+
* ```ts
|
|
152
|
+
* import { pace } from './route.ts'
|
|
153
|
+
* import { equal } from '@yaks/testing'
|
|
154
|
+
*
|
|
155
|
+
* let now = Date.parse('2026-11-14T00:00:00Z') // three days into the week
|
|
156
|
+
* let week = { window: 10080, used: 40, resets: '2026-11-18T00:00:00Z' }
|
|
157
|
+
* equal(pace(week, now), { gone: 43, spent: 40, under: 3 })
|
|
158
|
+
* equal(pace(week, now, 7), { gone: 43, spent: 33, under: 10 })
|
|
159
|
+
* equal(pace({ ...week, used: 5 }, now, 7).spent, 0)
|
|
160
|
+
* equal(pace({ window: 10080, used: 40 }, now).gone, 100)
|
|
161
|
+
* ```
|
|
162
|
+
*/ export let pace = (window, now, back = 0)=>{
|
|
163
|
+
let ends = window.resets ? Date.parse(window.resets) : NaN;
|
|
164
|
+
let left = (ends - now) / (window.window * 60000);
|
|
165
|
+
let gone = isNaN(left) ? 100 : Math.round(100 * Math.min(1, Math.max(0, 1 - left)));
|
|
166
|
+
let spent = Math.max(0, window.used - back);
|
|
167
|
+
return {
|
|
168
|
+
gone,
|
|
169
|
+
spent,
|
|
170
|
+
under: gone - spent
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
// The points of the week a plan's limit on one model holds: Claude's cap on
|
|
174
|
+
// Fable is half the week, and a Fable reset gives its share back.
|
|
175
|
+
const CAP = 0.5;
|
|
176
|
+
// Five hours are spent from this share on: an account whose five hours read
|
|
177
|
+
// this much used has no room for a run until they reset. The week is its
|
|
178
|
+
// pace's to weigh.
|
|
179
|
+
const FULL = 90;
|
|
180
|
+
/**
|
|
181
|
+
* The account further under its weekly pace ({@link pace}): the most points
|
|
182
|
+
* between the share of its week gone and the share used, less what a reset of
|
|
183
|
+
* its limit on one model gives back, half that limit's reading. A week that
|
|
184
|
+
* has reset since its reading is unused, and an account nothing has read yet
|
|
185
|
+
* goes first, so its first run reads it. An account whose five hours are
|
|
186
|
+
* spent or nearly so goes after every account with room, and the answer says
|
|
187
|
+
* `full` where none has any. Nothing where
|
|
188
|
+
* there are no accounts.
|
|
189
|
+
*
|
|
190
|
+
* ```ts
|
|
191
|
+
* import { freest } from './route.ts'
|
|
192
|
+
* import { equal } from '@yaks/testing'
|
|
193
|
+
*
|
|
194
|
+
* let accounts = [{ account: 'a', name: '.claude' }, { account: 'b', name: '.claude-teams' }]
|
|
195
|
+
* let now = Date.parse('2026-11-14T00:00:00Z') // three days into the week
|
|
196
|
+
* let week = (account: string, used: number, resets = '2026-11-18T00:00:00Z') =>
|
|
197
|
+
* ({ provider: 'claude', account, window: 10080, used, resets })
|
|
198
|
+
* equal(freest({ accounts, readings: [week('a', 20), week('b', 50)], now }), {
|
|
199
|
+
* account: 'a',
|
|
200
|
+
* why: '.claude is 23 points under its weekly pace, ' +
|
|
201
|
+
* '.claude-teams is 7 points over its weekly pace',
|
|
202
|
+
* })
|
|
203
|
+
* // Fable's share, half its cap's reading, is not held against the week.
|
|
204
|
+
* let fable = { ...week('b', 80), model: 'fable' }
|
|
205
|
+
* equal(freest({ accounts, readings: [week('a', 20), week('b', 50), fable], now })?.account, 'b')
|
|
206
|
+
* equal(freest({ accounts, readings: [week('a', 20)], now })?.account, 'b')
|
|
207
|
+
* equal(freest({ accounts, readings: [week('a', 20), week('b', 90, '2026-11-13T00:00:00Z')], now })?.account, 'b')
|
|
208
|
+
* // Five hours used up put an account last, until they reset.
|
|
209
|
+
* let hours = (account: string, used: number, resets = '2026-11-14T02:00:00Z') =>
|
|
210
|
+
* ({ provider: 'claude', account, window: 300, used, resets })
|
|
211
|
+
* equal(freest({ accounts, readings: [week('a', 20), hours('a', 95), week('b', 50)], now }), {
|
|
212
|
+
* account: 'b',
|
|
213
|
+
* why: '.claude-teams is 7 points over its weekly pace, ' +
|
|
214
|
+
* '.claude has used 95% of its five hours',
|
|
215
|
+
* })
|
|
216
|
+
* equal(freest({ accounts, readings: [hours('a', 100, '2026-11-13T23:00:00Z')], now })?.account, 'a')
|
|
217
|
+
* equal(freest({ accounts: accounts.slice(0, 1), readings: [hours('a', 100)], now })?.full, true)
|
|
218
|
+
* equal(freest({ accounts: [], readings: [], now }), undefined)
|
|
219
|
+
* ```
|
|
220
|
+
*/ export let freest = (asked)=>{
|
|
221
|
+
let { readings, now } = asked;
|
|
222
|
+
let current = (r)=>!(r.resets && Date.parse(r.resets) <= now);
|
|
223
|
+
// An account's newest reading of a window, of the plan or of its limit on
|
|
224
|
+
// one model.
|
|
225
|
+
let newest = (a, window, capped = false)=>readings.filter((r)=>r.account == a.account && r.window == window && !r.model == !capped).sort((x, y)=>(y.at ?? '').localeCompare(x.at ?? ''))[0];
|
|
226
|
+
// Its five hours' reading, where they are spent.
|
|
227
|
+
let spent = (a)=>{
|
|
228
|
+
let r = newest(a, HOURS);
|
|
229
|
+
return r && current(r) && r.used >= FULL ? r : undefined;
|
|
230
|
+
};
|
|
231
|
+
// Points under pace, and what the reason says where it is not a number.
|
|
232
|
+
let weigh = (a)=>{
|
|
233
|
+
let r = newest(a, WEEK);
|
|
234
|
+
if (!r) return {
|
|
235
|
+
under: Infinity,
|
|
236
|
+
fresh: 'has no reading of its week'
|
|
237
|
+
};
|
|
238
|
+
if (!current(r)) return {
|
|
239
|
+
under: Infinity,
|
|
240
|
+
fresh: 'has a fresh week'
|
|
241
|
+
};
|
|
242
|
+
let cap = newest(a, WEEK, true);
|
|
243
|
+
let running = cap && current(cap);
|
|
244
|
+
return {
|
|
245
|
+
under: Math.round(pace(r, now, running ? cap.used * CAP : 0).under)
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
let weighed = asked.accounts.map((a)=>({
|
|
249
|
+
...a,
|
|
250
|
+
...weigh(a),
|
|
251
|
+
spent: spent(a)
|
|
252
|
+
})).sort((x, y)=>Number(!!x.spent) - Number(!!y.spent) || y.under - x.under);
|
|
253
|
+
if (!weighed.length) return;
|
|
254
|
+
let said = (w)=>`${w.name} ${w.spent ? `has used ${w.spent.used}% of its ${span(w.spent.window)}` : w.fresh ?? `is ${Math.abs(w.under)} points ${w.under < 0 ? 'over' : 'under'} ` + 'its weekly pace'}`;
|
|
255
|
+
return {
|
|
256
|
+
account: weighed[0].account,
|
|
257
|
+
why: weighed.map(said).join(', '),
|
|
258
|
+
...weighed[0].spent ? {
|
|
259
|
+
full: true
|
|
260
|
+
} : {}
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
//# sourceMappingURL=route.js.map
|
package/route.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route.js","sources":["./route.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,+EAA+E;AAC/E,6EAA6E;AAC7E,0EAA0E;AAC1E,6EAA6E;AAC7E,4EAA4E;AAC5E,4EAA4E;AAC5E,wDAAwD;AA4CxD,IAAI,OAAO,CAAC,SACV,UAAU,QAAQ,SAAS,UAAU,MAAM,eAAe,GAAG,OAAO,IAAI,CAAC;AAE3E,4EAA4E;AAC5E,sDAAsD;AACtD,IAAI,UAAU,CACZ,MACA,UACA,QAEA,SAAS,MAAM,CAAC,CAAC,IAAM,EAAE,QAAQ,IAAI,QAAQ,EAAE,MAAM,GAAG,KAAK,EAAE,KAAK,IAAI,OACrE,IAAI,CAAC,CAAC,GAAG,IACR,EAAE,MAAM,GAAG,EAAE,MAAM,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,aAAa,CAAC,EAAE,EAAE,IAAI,IAC3D,CAAC,EAAE;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