@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/_dist/route.d.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { Eid } from "@yaks/graph";
|
|
2
|
+
/** One offering a default may run on. */ export type Route = {
|
|
3
|
+
provider: Eid;
|
|
4
|
+
model: Eid;
|
|
5
|
+
/** what the model is called, for the reason */ name: string;
|
|
6
|
+
/** the provider whose plan limits meter it (`provider.plan`, else its own) */ plan: Eid;
|
|
7
|
+
/** when it was pinned (`routes.pinned`): a pinned offering is the answer,
|
|
8
|
+
* whatever the plans read */ pinned?: string;
|
|
9
|
+
};
|
|
10
|
+
/** A plan limit's latest reading, as its `rate_limit` row holds it. */ export type Reading = {
|
|
11
|
+
provider: Eid;
|
|
12
|
+
/** the account whose plan it is, where the reading came from one */ account?: Eid;
|
|
13
|
+
/** minutes: 10080 a week, 300 five hours */ window: number;
|
|
14
|
+
/** the model whose requests it meters, where it is a limit on one model */ model?: Eid;
|
|
15
|
+
/** percent used */ used: number;
|
|
16
|
+
resets?: string;
|
|
17
|
+
at?: string;
|
|
18
|
+
};
|
|
19
|
+
/** What a router is asked: the offerings, what is known of their plans, and
|
|
20
|
+
* the moment. A later rule's parameter (the work's kind) is another property
|
|
21
|
+
* here. */ export type Asked = {
|
|
22
|
+
routes: Route[];
|
|
23
|
+
readings: Reading[];
|
|
24
|
+
now: number;
|
|
25
|
+
};
|
|
26
|
+
/** The offering chosen and, in one line, why. */ export type Routed = {
|
|
27
|
+
provider: Eid;
|
|
28
|
+
model: Eid;
|
|
29
|
+
why: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The offering to ask, or nothing where no rule can say: no routes, or none
|
|
33
|
+
* pinned and no reading of any of their plans.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { type Reading, route } from './route.ts'
|
|
37
|
+
* import { equal } from '@yaks/testing'
|
|
38
|
+
*
|
|
39
|
+
* let routes = [
|
|
40
|
+
* { provider: 'claude', model: 'opus', name: 'opus', plan: 'claude' },
|
|
41
|
+
* { provider: 'openai', model: 'sol', name: 'sol', plan: 'codex' },
|
|
42
|
+
* ]
|
|
43
|
+
* let week = (provider: string, used: number, resets = '2026-11-17T00:00:00Z') =>
|
|
44
|
+
* ({ provider, window: 10080, used, resets })
|
|
45
|
+
* let now = Date.parse('2026-11-11T00:00:00Z')
|
|
46
|
+
* let ask = (readings: Reading[]) =>
|
|
47
|
+
* route({ routes, readings, now })
|
|
48
|
+
*
|
|
49
|
+
* equal(ask([week('claude', 84), week('codex', 82)]), {
|
|
50
|
+
* provider: 'openai',
|
|
51
|
+
* model: 'sol',
|
|
52
|
+
* why: 'sol has 18% of its week left, opus has 16% of its week left',
|
|
53
|
+
* })
|
|
54
|
+
* // A week that has reset since it was read is unused.
|
|
55
|
+
* equal(ask([week('claude', 84, '2026-11-10T00:00:00Z'), week('codex', 82)])?.model, 'opus')
|
|
56
|
+
* // A plan nothing has read can't win, and with no reading nothing is chosen.
|
|
57
|
+
* equal(ask([week('claude', 99)])?.why, 'opus has 1% of its week left')
|
|
58
|
+
* equal(ask([]), undefined)
|
|
59
|
+
* // A limit on one model counts for that model where it leaves less.
|
|
60
|
+
* equal(ask([week('claude', 40), { ...week('claude', 95), model: 'opus' }, week('codex', 82)])?.model, 'sol')
|
|
61
|
+
* // A pinned offering wins over usage, and needs no reading.
|
|
62
|
+
* let pin = { ...routes[0], pinned: '2026-11-11T00:00:00Z' }
|
|
63
|
+
* equal(route({ routes: [pin, routes[1]], readings: [week('codex', 0)], now }), {
|
|
64
|
+
* provider: 'claude',
|
|
65
|
+
* model: 'opus',
|
|
66
|
+
* why: 'opus is pinned',
|
|
67
|
+
* })
|
|
68
|
+
* equal(route({ routes: [pin], readings: [], now })?.model, 'opus')
|
|
69
|
+
* ```
|
|
70
|
+
*/ export declare let route: (asked: Asked) => Routed | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Where a conversation goes on once the plan `from` runs on is spent: of the
|
|
73
|
+
* offerings on another plan whose readings leave room, the one {@link route}
|
|
74
|
+
* chooses, else the first of them. Nothing where every other plan is spent
|
|
75
|
+
* too, or none is routed. The reason says which plan ran out.
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { onward } from './route.ts'
|
|
79
|
+
* import { equal } from '@yaks/testing'
|
|
80
|
+
*
|
|
81
|
+
* let routes = [
|
|
82
|
+
* { provider: 'claude', model: 'opus', name: 'opus', plan: 'claude' },
|
|
83
|
+
* { provider: 'openai', model: 'sol', name: 'sol', plan: 'codex' },
|
|
84
|
+
* { provider: 'grok', model: 'grok-5', name: 'grok-5', plan: 'grok' },
|
|
85
|
+
* ]
|
|
86
|
+
* let now = Date.parse('2026-11-23T12:00:00Z')
|
|
87
|
+
* let full = { provider: 'codex', window: 10080, used: 100, resets: '2026-11-25T00:00:00Z' }
|
|
88
|
+
* let from = { provider: 'claude', model: 'opus' }
|
|
89
|
+
* // With no readings, the next routed offering.
|
|
90
|
+
* equal(onward({ routes, readings: [], now, from }), {
|
|
91
|
+
* provider: 'openai', model: 'sol', why: "opus's plan is spent; sol is next",
|
|
92
|
+
* })
|
|
93
|
+
* // A plan read full is passed over.
|
|
94
|
+
* equal(onward({ routes, readings: [full], now, from })?.model, 'grok-5')
|
|
95
|
+
* equal(onward({ routes: routes.slice(0, 2), readings: [full], now, from }), undefined)
|
|
96
|
+
* ```
|
|
97
|
+
*/ export declare let onward: (asked: Asked & {
|
|
98
|
+
from: {
|
|
99
|
+
provider: Eid;
|
|
100
|
+
model?: Eid;
|
|
101
|
+
};
|
|
102
|
+
passed?: ReadonlySet<Eid>;
|
|
103
|
+
available?: ReadonlySet<Eid>;
|
|
104
|
+
}) => Routed | undefined;
|
|
105
|
+
/** An account an offering may run on (`account`), and what it is called,
|
|
106
|
+
* for the reason. */ export type Account = {
|
|
107
|
+
account: Eid;
|
|
108
|
+
name: string;
|
|
109
|
+
};
|
|
110
|
+
/** The account chosen and, in one line, why; `full` where its plan has no
|
|
111
|
+
* room, as every account's had none. */ export type Freest = {
|
|
112
|
+
account: Eid;
|
|
113
|
+
why: string;
|
|
114
|
+
full?: true;
|
|
115
|
+
};
|
|
116
|
+
/** A window's usage against its pace, in points of the window. */ export type Pace = {
|
|
117
|
+
/** the share of the window gone, rounded */ gone: number;
|
|
118
|
+
/** the points used that its reset does not give back */ spent: number;
|
|
119
|
+
/** `gone` less `spent`: below zero, over pace */ under: number;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* A window's usage against the share of it gone: `used` less the points
|
|
123
|
+
* `back` a reset gives back (Fable's share of Claude's week), set against the
|
|
124
|
+
* share of the window gone by `now`. A window that resets at an unknown time
|
|
125
|
+
* is taken as all gone. The pace `claude-budget` prints (`bin/pace.ts`) and
|
|
126
|
+
* the one {@link freest} picks an account by.
|
|
127
|
+
*
|
|
128
|
+
* ```ts
|
|
129
|
+
* import { pace } from './route.ts'
|
|
130
|
+
* import { equal } from '@yaks/testing'
|
|
131
|
+
*
|
|
132
|
+
* let now = Date.parse('2026-11-14T00:00:00Z') // three days into the week
|
|
133
|
+
* let week = { window: 10080, used: 40, resets: '2026-11-18T00:00:00Z' }
|
|
134
|
+
* equal(pace(week, now), { gone: 43, spent: 40, under: 3 })
|
|
135
|
+
* equal(pace(week, now, 7), { gone: 43, spent: 33, under: 10 })
|
|
136
|
+
* equal(pace({ ...week, used: 5 }, now, 7).spent, 0)
|
|
137
|
+
* equal(pace({ window: 10080, used: 40 }, now).gone, 100)
|
|
138
|
+
* ```
|
|
139
|
+
*/ export declare let pace: (window: {
|
|
140
|
+
window: number;
|
|
141
|
+
used: number;
|
|
142
|
+
resets?: string;
|
|
143
|
+
}, now: number, back?: number) => Pace;
|
|
144
|
+
/**
|
|
145
|
+
* The account further under its weekly pace ({@link pace}): the most points
|
|
146
|
+
* between the share of its week gone and the share used, less what a reset of
|
|
147
|
+
* its limit on one model gives back, half that limit's reading. A week that
|
|
148
|
+
* has reset since its reading is unused, and an account nothing has read yet
|
|
149
|
+
* goes first, so its first run reads it. An account whose five hours are
|
|
150
|
+
* spent or nearly so goes after every account with room, and the answer says
|
|
151
|
+
* `full` where none has any. Nothing where
|
|
152
|
+
* there are no accounts.
|
|
153
|
+
*
|
|
154
|
+
* ```ts
|
|
155
|
+
* import { freest } from './route.ts'
|
|
156
|
+
* import { equal } from '@yaks/testing'
|
|
157
|
+
*
|
|
158
|
+
* let accounts = [{ account: 'a', name: '.claude' }, { account: 'b', name: '.claude-teams' }]
|
|
159
|
+
* let now = Date.parse('2026-11-14T00:00:00Z') // three days into the week
|
|
160
|
+
* let week = (account: string, used: number, resets = '2026-11-18T00:00:00Z') =>
|
|
161
|
+
* ({ provider: 'claude', account, window: 10080, used, resets })
|
|
162
|
+
* equal(freest({ accounts, readings: [week('a', 20), week('b', 50)], now }), {
|
|
163
|
+
* account: 'a',
|
|
164
|
+
* why: '.claude is 23 points under its weekly pace, ' +
|
|
165
|
+
* '.claude-teams is 7 points over its weekly pace',
|
|
166
|
+
* })
|
|
167
|
+
* // Fable's share, half its cap's reading, is not held against the week.
|
|
168
|
+
* let fable = { ...week('b', 80), model: 'fable' }
|
|
169
|
+
* equal(freest({ accounts, readings: [week('a', 20), week('b', 50), fable], now })?.account, 'b')
|
|
170
|
+
* equal(freest({ accounts, readings: [week('a', 20)], now })?.account, 'b')
|
|
171
|
+
* equal(freest({ accounts, readings: [week('a', 20), week('b', 90, '2026-11-13T00:00:00Z')], now })?.account, 'b')
|
|
172
|
+
* // Five hours used up put an account last, until they reset.
|
|
173
|
+
* let hours = (account: string, used: number, resets = '2026-11-14T02:00:00Z') =>
|
|
174
|
+
* ({ provider: 'claude', account, window: 300, used, resets })
|
|
175
|
+
* equal(freest({ accounts, readings: [week('a', 20), hours('a', 95), week('b', 50)], now }), {
|
|
176
|
+
* account: 'b',
|
|
177
|
+
* why: '.claude-teams is 7 points over its weekly pace, ' +
|
|
178
|
+
* '.claude has used 95% of its five hours',
|
|
179
|
+
* })
|
|
180
|
+
* equal(freest({ accounts, readings: [hours('a', 100, '2026-11-13T23:00:00Z')], now })?.account, 'a')
|
|
181
|
+
* equal(freest({ accounts: accounts.slice(0, 1), readings: [hours('a', 100)], now })?.full, true)
|
|
182
|
+
* equal(freest({ accounts: [], readings: [], now }), undefined)
|
|
183
|
+
* ```
|
|
184
|
+
*/ export declare let freest: (asked: {
|
|
185
|
+
accounts: Account[];
|
|
186
|
+
readings: Reading[];
|
|
187
|
+
now: number;
|
|
188
|
+
}) => Freest | undefined;
|
|
189
|
+
//# sourceMappingURL=route.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route.d.ts","sources":["../route.ts"],"names":[],"mappings":"AASA,cAAc,GAAG,2BAAgC;AAEjD,uCAAuC,GACvC,YAAY;EACV,UAAU;EACV,OAAO;EACP,6CAA6C,GAC7C,MAAM,MAAM;EACZ,4EAA4E,GAC5E,MAAM;EACN;6BAC2B,GAC3B,SAAS,MAAM;;AAGjB,qEAAqE,GACrE,YAAY;EACV,UAAU;EACV,kEAAkE,GAClE,UAAU;EACV,0CAA0C,GAC1C,QAAQ,MAAM;EACd,yEAAyE,GACzE,QAAQ;EACR,iBAAiB,GACjB,MAAM,MAAM;EACZ,SAAS,MAAM;EACf,KAAK,MAAM;;AAGb;;SAES,GACT,YAAY;EAAU,QAAQ;EAAS,UAAU;EAAW,KAAK,MAAM;;AAEvE,+CAA+C,GAC/C,YAAY;EAAW,UAAU;EAAK,OAAO;EAAK,KAAK,MAAM;;AA8C7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuCC,GACD,OAAO,YAAI,QAAS,OAAO,UAAQ,SAAS,SAAS,CAUpD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC,GACD,OAAO,YAAI,SACT,OAAO;EACL;IAAQ,UAAU;IAAK,QAAQ;;EAC/B,SAAS,YAAY;EACrB,YAAY,YAAY;MAEzB,SAAS,SAAS,CA6BpB;AAED;mBACmB,GACnB,YAAY;EAAY,SAAS;EAAK,MAAM,MAAM;;AAElD;sCACsC,GACtC,YAAY;EAAW,SAAS;EAAK,KAAK,MAAM;EAAE,OAAO,IAAI;;AAK7D,gEAAgE,GAChE,YAAY;EACV,0CAA0C,GAC1C,MAAM,MAAM;EACZ,sDAAsD,GACtD,OAAO,MAAM;EACb,+CAA+C,GAC/C,OAAO,MAAM;;AAGf;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,YAAI,OACT;EAAU,QAAQ,MAAM;EAAE,MAAM,MAAM;EAAE,SAAS,MAAM;GACvD,KAAK,MAAM,EACX,kBACC,KAQF;AAWD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwCC,GACD,OAAO,YAAI,SACT;EAAS,UAAU;EAAW,UAAU;EAAW,KAAK,MAAM;MAC7D,SAAS,SAAS,CA6CpB"}
|
package/_dist/sse.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** JSON SSE frames across arbitrary byte chunks, CRLF and a final frame with
|
|
2
|
+
* no blank line after it; `done` hears a `[DONE]` sentinel. The reader is
|
|
3
|
+
* released even if a consumer stops early or throws. */ export declare let jsonFrames: (body: ReadableStream<Uint8Array>, fault: (kind: 'transport' | 'stalled' | 'malformed_stream', message: string) => Error, check?: (value: Record<string, unknown>) => void, done?: () => void) => AsyncGenerator<Record<string, unknown>>;
|
|
4
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sources":["../sse.ts"],"names":[],"mappings":"AAaA;;sDAEsD,GACtD,OAAO,YAAI,aACT,MAAM,eAAe,aACrB,QACE,MAAM,cAAc,YAAY,oBAChC,SAAS,MAAM,KACZ,OACL,SAAS,OAAO,OAAO,MAAM,EAAE,OAAO,MAAM,IAAI,EAChD,aAAa,IAAI,KAChB,eAAe,OAAO,MAAM,EAAE,OAAO,GAyDvC"}
|
package/_dist/stall.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How long a provider's stream may go quiet. A request whose connection, first
|
|
3
|
+
* byte or any later gap in its stream goes that long without progress fails
|
|
4
|
+
* as stalled, instead of waiting for ever on a server that stopped talking.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { stallMs, watched } from '@yaks/model/stall'
|
|
8
|
+
*
|
|
9
|
+
* let ask = watched(fetch, stallMs({ stallTimeout: 60 }))
|
|
10
|
+
* // a call through `ask` fails with `Stalled` after 60s without a byte
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/ import { type Clock } from "@yaks/fp";
|
|
15
|
+
/** Seconds a stream may go without progress before its request fails as
|
|
16
|
+
* stalled, where neither the request nor its provider's row names its own
|
|
17
|
+
* (`Request.stallTimeout`, `provider{stall_timeout}`). Five minutes, the Codex
|
|
18
|
+
* CLI's own stream idle timeout: a reasoning model can think in silence for
|
|
19
|
+
* minutes, and none answers after hours. */ export declare const STALL_TIMEOUT: 300;
|
|
20
|
+
/** The milliseconds a request's stream may stay quiet: its own
|
|
21
|
+
* `stallTimeout`, else {@link STALL_TIMEOUT}. 0 waits for ever. */ export declare let stallMs: (req: {
|
|
22
|
+
stallTimeout?: number;
|
|
23
|
+
}) => number;
|
|
24
|
+
/** What a stalled exchange's call or read fails with. */ export declare class Stalled extends Error {
|
|
25
|
+
constructor(ms: number);
|
|
26
|
+
}
|
|
27
|
+
/** A resettable idle deadline for one exchange ({@link watchdog}). */ export type Watchdog = {
|
|
28
|
+
/** aborted when the deadline passes, or when the relayed stop does */ signal?: AbortSignal;
|
|
29
|
+
/** push the deadline `ms` out from now */ kick: () => void;
|
|
30
|
+
/** hold the deadline while the waiting is not the source's */ rest: () => void;
|
|
31
|
+
/** whether the deadline passed (a relayed stop leaves this false) */ stalled: () => boolean;
|
|
32
|
+
close: () => void;
|
|
33
|
+
};
|
|
34
|
+
/** A resettable idle deadline for one exchange. It aborts its own signal,
|
|
35
|
+
* never the caller's `stop`, when `ms` passes with no progress, and each sign
|
|
36
|
+
* of progress calls kick() to push the deadline out. The caller reads
|
|
37
|
+
* stalled() to turn that self-abort into a named failure instead of a silent
|
|
38
|
+
* hang. A relayed stop aborts too, with its own reason, but leaves stalled()
|
|
39
|
+
* false so the caller keeps its own path. `ms` 0 sets no deadline. */ export declare let watchdog: (ms: number, clock?: Clock, stop?: AbortSignal) => Watchdog;
|
|
40
|
+
/** `body`, failing with {@link Stalled} once a read waits `ms` on it, or with
|
|
41
|
+
* `stop`'s reason when it aborts. `ms` 0 and no `stop` leave it as it is. */ export declare let watchedBody: <Bytes extends Uint8Array>(body: ReadableStream<Bytes>, ms: number, clock?: Clock, stop?: AbortSignal) => ReadableStream<Bytes>;
|
|
42
|
+
/** `fetch` whose every exchange is watched: the connection and the head get
|
|
43
|
+
* `ms` to arrive, then each read of the body gets `ms` for its next chunk.
|
|
44
|
+
* Passing a deadline aborts the exchange and fails the waiting call or read
|
|
45
|
+
* with {@link Stalled}; the caller's own signal still stops it with its own
|
|
46
|
+
* reason. `ms` 0 leaves `fetch` as it is. */ export declare let watched: (fetch: typeof globalThis.fetch, ms: number, clock?: Clock) => typeof globalThis.fetch;
|
|
47
|
+
//# sourceMappingURL=stall.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stall.d.ts","sources":["../stall.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;CAaC,GAED,SAAS,KAAK,KAAK,wBAAqC;AAExD;;;;0CAI0C,GAC1C,OAAO,cAAM,eAAgB,IAAG;AAEhC;iEACiE,GACjE,OAAO,YAAI,UAAW;EAAO,eAAe,MAAM;MAAK,MAAM,CACN;AAEvD,uDAAuD,GACvD,OAAO,cAAM,gBAAgB;EAC3B,YAAY,IAAI,MAAM;AAIxB;AAEA,oEAAoE,GACpE,YAAY;EACV,oEAAoE,GACpE,SAAS;EACT,wCAAwC,GACxC,YAAY,IAAI;EAChB,4DAA4D,GAC5D,YAAY,IAAI;EAChB,mEAAmE,GACnE,eAAe,OAAO;EACtB,aAAa,IAAI;;AAGnB;;;;;oEAKoE,GACpE,OAAO,YAAI,WACT,IAAI,MAAM,EACV,QAAO,OACP,OAAO,gBACN,SAyCF;AAuDD;2EAC2E,GAC3E,OAAO,YAAI,cAAe,cAAc,YACtC,MAAM,eAAe,QACrB,IAAI,MAAM,EACV,QAAO,OACP,OAAO,gBACN,eAAe,OAGjB;AAED;;;;2CAI2C,GAC3C,OAAO,YAAI,UACT,cAAc,WAAW,OACzB,IAAI,MAAM,EACV,QAAO,iBACC,WAAW,MAuBlB"}
|
package/_dist/step.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One model step across two hosts. A step is one call of a `Model`: a request
|
|
3
|
+
* in, a reply out. Here one host forwards the step and another answers it
|
|
4
|
+
* from its own providers, which is how a provider comes to be a machine
|
|
5
|
+
* (`provider{transport: 'machine', machine}`).
|
|
6
|
+
*
|
|
7
|
+
* The serving host mounts {@link answering} at {@link PATH} over whichever of
|
|
8
|
+
* its providers it lends (`serving`, @yaks/model). The calling host builds a
|
|
9
|
+
* `Model` with {@link forwarding} over whatever `fetch` reaches that host, or
|
|
10
|
+
* one for each provider row a machine serves with {@link forwarded}.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { answering, forwarding } from '@yaks/model/step'
|
|
14
|
+
* import { equal } from '@yaks/testing'
|
|
15
|
+
*
|
|
16
|
+
* let answer = answering((req) =>
|
|
17
|
+
* Promise.resolve({
|
|
18
|
+
* id: 'r1',
|
|
19
|
+
* model: req.model,
|
|
20
|
+
* items: [{ kind: 'assistant', text: 'Hello' }],
|
|
21
|
+
* }), console.error)
|
|
22
|
+
* let model = forwarding({ fetch: (at, init) => answer(new Request(at, init)) })
|
|
23
|
+
* let reply = await model({ model: 'example', items: [], tools: [] })
|
|
24
|
+
* equal(reply.items, [{ kind: 'assistant', text: 'Hello' }])
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* On the wire the step is a POST of the request as JSON (`frozenRequest`),
|
|
28
|
+
* answered with server-sent events: `{first}` when the reply's first output
|
|
29
|
+
* arrived, `{text}` for each `TextDelta`, then one `{reply}` or `{error}`. A
|
|
30
|
+
* step that is quiet sends a comment every {@link BEAT} milliseconds, so the
|
|
31
|
+
* caller's stall timeout measures the link between the hosts, and the serving
|
|
32
|
+
* host's own measures its provider.
|
|
33
|
+
*
|
|
34
|
+
* @module
|
|
35
|
+
*/ import { type Clock } from "@yaks/fp";
|
|
36
|
+
import type { Eid, Graph } from "@yaks/graph";
|
|
37
|
+
import { type Model } from "./mod.js";
|
|
38
|
+
import type { Lent } from "./provider.js";
|
|
39
|
+
/** Where a serving host answers a step. */ export declare const PATH: '/model/step';
|
|
40
|
+
/** Milliseconds between the comments a quiet step sends. */ export declare const BEAT: 15_000;
|
|
41
|
+
/**
|
|
42
|
+
* The serving host's end: a web handler that answers one step from `serve`
|
|
43
|
+
* and streams it as it arrives. The caller hanging up, or cancelling the
|
|
44
|
+
* answer, aborts the step's `signal`. A `ModelError` reaches the caller as it
|
|
45
|
+
* was thrown; anything else is this host's bug, handed to `report`, and the
|
|
46
|
+
* caller is told only that the step failed here (a `ModelError` coded
|
|
47
|
+
* `serving`). A body that is no model request is refused with a 400.
|
|
48
|
+
*/ export declare let answering: (serve: Model, report: (error: unknown) => void, clock?: Clock) => (request: Request) => Promise<Response>;
|
|
49
|
+
/**
|
|
50
|
+
* The calling host's end: a model that sends each step to the host `fetch`
|
|
51
|
+
* reaches, at {@link PATH} under `base`, and answers with its reply: text as
|
|
52
|
+
* it streams, the `usage` the serving provider counted, and no `cost`, since
|
|
53
|
+
* what the step cost the serving host is not what it costs this one. The
|
|
54
|
+
* request's `signal` stops the step on both hosts.
|
|
55
|
+
*
|
|
56
|
+
* A step asking for anything but text is refused (`modalities`): generated
|
|
57
|
+
* media would stay in the serving host's store. A host that does not answer
|
|
58
|
+
* fails as `unreachable` and one that goes quiet as `stalled`, both of which
|
|
59
|
+
* may pass.
|
|
60
|
+
*/ export declare let forwarding: (lent: Pick<Lent, 'fetch'>, options?: {
|
|
61
|
+
base?: string;
|
|
62
|
+
clock?: Clock;
|
|
63
|
+
}) => Model;
|
|
64
|
+
/**
|
|
65
|
+
* The providers `g`'s rows say a machine serves
|
|
66
|
+
* (`provider{transport: 'machine', machine}`), keyed by `provider.name`, each
|
|
67
|
+
* forwarding its steps through the `fetch` that `reach` gives for its machine.
|
|
68
|
+
* A row naming no machine, or a machine `reach` has no `fetch` for, is served
|
|
69
|
+
* by nobody and left out.
|
|
70
|
+
*/ export declare let forwarded: (g: Pick<Graph, 'read'>, reach: (machine: Eid) => typeof fetch | undefined) => Promise<Record<string, Model>>;
|
|
71
|
+
//# sourceMappingURL=step.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"step.d.ts","sources":["../step.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCC,GAED,SAAS,KAAK,KAAK,wBAAqC;AACxD,cAAoB,GAAG,EAAE,KAAK,2BAAgC;AAM9D,SACE,KAAK,KAAK,mBAKK;AACjB,cAAc,IAAI,wBAAuB;AAKzC,yCAAyC,GACzC,OAAO,cAAM,MAAO,cAAa;AAEjC,0DAA0D,GAC1D,OAAO,cAAM,MAAO,OAAM;AAoB1B;;;;;;;CAOC,GACD,OAAO,YAAI,YACT,OAAO,OACP,SAAS,OAAO,OAAO,KAAK,IAAI,EAChC,QAAO,WACL,SAAS,YAAY,QAAQ,UAuEhC;AAQD;;;;;;;;;;;CAWC,GACD,OAAO,YAAI,aACT,MAAM,KAAK,MAAM,UACjB;EAAW,OAAO,MAAM;EAAE,QAAQ;MACjC,MA0DF;AAED;;;;;;CAMC,GACD,OAAO,YAAI,YACT,GAAG,KAAK,OAAO,SACf,QAAQ,SAAS,eAAe,QAAQ,SAAS,KAChD,QAAQ,OAAO,MAAM,EAAE,QASzB"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Graph } from "@yaks/graph";
|
|
2
|
+
import { type Mark, type Request } from "./mod.js";
|
|
3
|
+
/** A guarded write, not a read followed by an unguarded decision to purchase. */ export declare let submitCall: (g: Pick<Graph, 'apply'>, call: string, at: string) => Promise<void>;
|
|
4
|
+
/** Invoke outside a transport catch: storage failure is not a network retry. */ export declare let submitted: (req: Request) => Promise<void>;
|
|
5
|
+
/** The reply's first output arrived; tells `onFirst` the first time only. */ export declare let began: (req: Request) => void;
|
|
6
|
+
/** Provider-owned components keep provider bookkeeping out of the host. */ export declare let acknowledged: (req: Request, mark: Mark) => Promise<void>;
|
|
7
|
+
//# sourceMappingURL=submission.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"submission.d.ts","sources":["../submission.ts"],"names":[],"mappings":"AAEA,cAAc,KAAK,2BAAgC;AACnD,SAAS,KAAK,IAAI,EAAc,KAAK,OAAO,mBAAkB;AAE9D,+EAA+E,GAC/E,OAAO,YAAI,aACT,GAAG,KAAK,OAAO,UACf,MAAM,MAAM,EACZ,IAAI,MAAM,KACT,QAAQ,IAAI,EASd;AAED,8EAA8E,GAC9E,OAAO,YAAI,YAAmB,KAAK,YAAU,QAAQ,IAAI,EAExD;AAGD,2EAA2E,GAC3E,OAAO,YAAI,QAAS,KAAK,YAAU,IAAI,CAItC;AAED,yEAAyE,GACzE,OAAO,YAAI,eAAsB,KAAK,SAAS,MAAM,SAAO,QAAQ,IAAI,EAEvE"}
|
package/_dist/vocab.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { VocabDoc } from "@yaks/vocab";
|
|
2
|
+
import type { Paid } from "./attribute.js";
|
|
3
|
+
/** The `provider` and `model` components, to load beside your own. */ export declare let modelDoc: VocabDoc;
|
|
4
|
+
/** What this package is: its manifest's description, the one place it is
|
|
5
|
+
* written, which a host stamps on each document here. */ export declare let description: string;
|
|
6
|
+
/** Every document this plugin declares. */ export declare let docs: VocabDoc[];
|
|
7
|
+
export declare let PROVIDER: string;
|
|
8
|
+
export declare let MODEL: string;
|
|
9
|
+
export declare let USAGE: string;
|
|
10
|
+
export declare let PRICE: string;
|
|
11
|
+
export declare let COST: string;
|
|
12
|
+
export declare let RATE_LIMIT: string;
|
|
13
|
+
/** Counts for one request. Cached input and reasoning output are subsets,
|
|
14
|
+
* not additional tokens. Missing counts are unknown. */ export type Usage = {
|
|
15
|
+
input_tokens?: number;
|
|
16
|
+
output_tokens?: number;
|
|
17
|
+
total_tokens?: number;
|
|
18
|
+
cached_tokens?: number;
|
|
19
|
+
cache_write_tokens?: number;
|
|
20
|
+
cache_write_1h_tokens?: number;
|
|
21
|
+
cache_write_12h_tokens?: number;
|
|
22
|
+
reasoning_tokens?: number;
|
|
23
|
+
/** web searches a provider ran for the request, each billed at
|
|
24
|
+
* {@link Price} `web_search` */ web_searches?: number;
|
|
25
|
+
};
|
|
26
|
+
/** What a model costs, in dollars per million tokens: the `price` component
|
|
27
|
+
* on its row. `cached` is the price of an input token the provider read from
|
|
28
|
+
* its cache, where it has one; `cache_write` of one it wrote there, and
|
|
29
|
+
* `cache_write_1h` of one written to be kept an hour. `web_search` is dollars
|
|
30
|
+
* per web search its provider runs for a request. */ export type Price = {
|
|
31
|
+
input: number;
|
|
32
|
+
output: number;
|
|
33
|
+
cached?: number;
|
|
34
|
+
cache_write?: number;
|
|
35
|
+
cache_write_1h?: number;
|
|
36
|
+
cache_write_12h?: number;
|
|
37
|
+
web_search?: number;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* What a request cost, in dollars, weighed at its model's price: its cached
|
|
41
|
+
* input at the cached price, what it wrote to the cache at the write price
|
|
42
|
+
* (an hour's writes at theirs), the rest of its input and all of its output at
|
|
43
|
+
* theirs, and each web search its provider ran at the search price.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { assertAlmostEquals } from '@std/assert'
|
|
47
|
+
* import { weigh } from '@yaks/model'
|
|
48
|
+
*
|
|
49
|
+
* let flash = { input: 0.15, cached: 0.03, output: 0.5 }
|
|
50
|
+
* let cost = weigh(flash, {
|
|
51
|
+
* input_tokens: 2_000_000,
|
|
52
|
+
* cached_tokens: 1_000_000,
|
|
53
|
+
* output_tokens: 1_000_000,
|
|
54
|
+
* })
|
|
55
|
+
* assertAlmostEquals(cost, 0.15 + 0.03 + 0.5)
|
|
56
|
+
*
|
|
57
|
+
* let opus = { input: 4, cached: 0.2, cache_write: 5, cache_write_1h: 8, output: 20 }
|
|
58
|
+
* assertAlmostEquals(weigh(opus, {
|
|
59
|
+
* input_tokens: 3_000_000,
|
|
60
|
+
* cached_tokens: 1_000_000,
|
|
61
|
+
* cache_write_tokens: 2_000_000,
|
|
62
|
+
* cache_write_1h_tokens: 1_000_000,
|
|
63
|
+
* }), 0.2 + 5 + 8)
|
|
64
|
+
*
|
|
65
|
+
* let searched = { input: 2, output: 10, web_search: 0.01 }
|
|
66
|
+
* assertAlmostEquals(weigh(searched, { web_searches: 3 }), 0.03)
|
|
67
|
+
* ```
|
|
68
|
+
*/ export declare let weigh: (price: Price, n: Usage) => number;
|
|
69
|
+
/** The share of a request's cache writes kept an hour, 0 to 1. */ export declare let hourly: (n: Usage) => number;
|
|
70
|
+
/**
|
|
71
|
+
* What tokens cost at a price, paid as `Paid` says (read from the cache,
|
|
72
|
+
* written to it, fresh, generated), with `hour` of the writes kept an hour.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { dollars } from '@yaks/model'
|
|
76
|
+
* import { assertAlmostEquals } from '@std/assert'
|
|
77
|
+
*
|
|
78
|
+
* let opus = { input: 4, cached: 0.2, cache_write: 5, cache_write_1h: 8, output: 20 }
|
|
79
|
+
* assertAlmostEquals(dollars(opus, [1e6, 1e6, 1e6, 1e6], 0.5), 0.2 + 6.5 + 4 + 20)
|
|
80
|
+
* ```
|
|
81
|
+
*/ export declare let dollars: (price: Price, []: Paid, hour?: number, halfDay?: number) => number;
|
|
82
|
+
//# sourceMappingURL=vocab.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AASA,cAAc,QAAQ,2BAAgC;AACtD,cAAc,IAAI,yBAAwB;AAI1C,oEAAoE,GACpE,OAAO,YAAI,UAAU,SAAc;AAEnC;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAAuB;AAExC,OAAO,YAAI,iBAAqB;AAChC,OAAO,YAAI,cAAe;AAC1B,OAAO,YAAI,cAAe;AAC1B,OAAO,YAAI,cAAe;AAC1B,OAAO,YAAI,aAAa;AACxB,OAAO,YAAI,mBAAyB;AAEpC;sDACsD,GACtD,YAAY;EACV,eAAe,MAAM;EACrB,gBAAgB,MAAM;EACtB,eAAe,MAAM;EACrB,gBAAgB,MAAM;EACtB,qBAAqB,MAAM;EAC3B,wBAAwB,MAAM;EAC9B,yBAAyB,MAAM;EAC/B,mBAAmB,MAAM;EACzB;gCAC8B,GAC9B,eAAe,MAAM;;AAGvB;;;;mDAImD,GACnD,YAAY;EACV,OAAO,MAAM;EACb,QAAQ,MAAM;EACd,SAAS,MAAM;EACf,cAAc,MAAM;EACpB,iBAAiB,MAAM;EACvB,kBAAkB,MAAM;EACxB,aAAa,MAAM;;AAGrB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6BC,GACD,OAAO,YAAI,QAAS,OAAO,OAAO,GAAG,UAAQ,MAAM,CAYlD;AAED,gEAAgE,GAChE,OAAO,YAAI,SAAU,GAAG,UAAQ,MAAM,CAG/B;AAEP;;;;;;;;;;;CAWC,GACD,OAAO,YAAI,UACT,OAAO,OACP,IAA8B,IAAI,EAClC,eACA,qBACC,MAAM,CASR"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Graph } from "@yaks/graph";
|
|
2
|
+
import type { Listed } from "./mod.js";
|
|
3
|
+
/** Give each listed model's row the window its catalog says, where the row
|
|
4
|
+
* has none of its own: a window set by hand stands. Answers each listed
|
|
5
|
+
* model with the window its row now has. */ export declare let windows: (graph: Pick<Graph, 'get' | 'apply'>, models: Listed[]) => Promise<Listed[]>;
|
|
6
|
+
//# sourceMappingURL=windows.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"windows.d.ts","sources":["../windows.ts"],"names":[],"mappings":"AAEA,cAAoB,KAAK,2BAAgC;AAEzD,cAAc,MAAM,mBAAkB;AAGtC;;0CAE0C,GAC1C,OAAO,YAAI,UACT,OAAO,KAAK,OAAO,QAAQ,UAC3B,QAAQ,aACP,QAAQ,UAcV"}
|
package/afford.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// A caller supplies an upper bound on every input token the provider may bill,
|
|
2
|
+
// including retained history and provider framing. Estimates are not bounds.
|
|
3
|
+
import { weigh } from "./vocab.js";
|
|
4
|
+
/** Tokens a provider may add around a prompt: role markers, a chat template's
|
|
5
|
+
* own system text, tool framing. */ export let FRAMING = 1024;
|
|
6
|
+
let media = new Set([
|
|
7
|
+
'image',
|
|
8
|
+
'document',
|
|
9
|
+
'image_url',
|
|
10
|
+
'input_image',
|
|
11
|
+
'audio',
|
|
12
|
+
'input_audio',
|
|
13
|
+
'video'
|
|
14
|
+
]);
|
|
15
|
+
let textual = (value)=>value == null || typeof value != 'object' || !ArrayBuffer.isView(value) && !(value instanceof ArrayBuffer) && (Array.isArray(value) ? value.every(textual) : Object.entries(value).every(([k, v])=>!media.has(k) && !(k == 'kind' && media.has(String(v))) && textual(v)));
|
|
16
|
+
let size = (value)=>new TextEncoder().encode(JSON.stringify(value) ?? '').length;
|
|
17
|
+
/** An upper bound on the input tokens a text prompt and the tools it declares
|
|
18
|
+
* can bill, never above the model's context window. A text token covers at
|
|
19
|
+
* least one UTF-8 byte, so the compact JSON bounds the text; doubling it
|
|
20
|
+
* covers a template that re-renders that JSON with spaces and indentation,
|
|
21
|
+
* and FRAMING the markers around it. Media is tokenized by its pixels or
|
|
22
|
+
* samples, not its bytes, so a prompt carrying any is bounded by the context
|
|
23
|
+
* window alone. Only the prompt is searched for media: a tool is declared as
|
|
24
|
+
* text, whatever its arguments are named. */ export let promptBound = (prompt, context, tools)=>textual(prompt) ? Math.min(context, 2 * (size(prompt) + size(tools)) + FRAMING) : context;
|
|
25
|
+
/** Bytes of compact JSON a text token covers on average, where
|
|
26
|
+
* {@link promptBound} takes one byte a token to bound it. */ let BYTES = 4;
|
|
27
|
+
let promptOf = (req)=>[
|
|
28
|
+
req.instructions,
|
|
29
|
+
req.items,
|
|
30
|
+
...req.turnContext?.length ? [
|
|
31
|
+
req.turnContext
|
|
32
|
+
] : [],
|
|
33
|
+
req.questions,
|
|
34
|
+
req.input
|
|
35
|
+
];
|
|
36
|
+
/**
|
|
37
|
+
* What a request that failed with nothing measured most likely cost, never
|
|
38
|
+
* above `most`, its hold: the text it sent at the input tariff, its anchored
|
|
39
|
+
* history uncounted since a cut request reads little of it, and the `output`
|
|
40
|
+
* tokens it was seen writing at the output tariff. An estimate, not a bound:
|
|
41
|
+
* it settles a hold the provider said nothing of, where the worst case would
|
|
42
|
+
* charge a space for money never spent.
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { likely } from '@yaks/model'
|
|
46
|
+
* import { equal } from '@yaks/testing'
|
|
47
|
+
* let price = { input: 1, output: 10 }
|
|
48
|
+
* // 1,003 tokens of text sent, about four bytes each
|
|
49
|
+
* let req = { instructions: 'x'.repeat(3990), items: [], tools: [] }
|
|
50
|
+
* equal(likely(req, price, 5), 0.001003)
|
|
51
|
+
* equal(likely(req, price, 5, 100), 0.002003)
|
|
52
|
+
* equal(likely(req, price, 0.0005), 0.0005)
|
|
53
|
+
* ```
|
|
54
|
+
*/ export let likely = (req, price, most, output = 0)=>{
|
|
55
|
+
let input = Math.ceil((size(promptOf(req)) + size(req.tools)) / BYTES);
|
|
56
|
+
let dollars = upward((input * price.input + output * price.output) / 1e6);
|
|
57
|
+
return Math.min(most, dollars);
|
|
58
|
+
};
|
|
59
|
+
/** A request's input bound. An anchor continues a conversation the provider
|
|
60
|
+
* holds, whose earlier turns this side cannot measure. */ export let requestBound = (req, context)=>req.anchor ? context : promptBound(promptOf(req), context, req.tools);
|
|
61
|
+
/** A request's price as it can be billed. A provider keeps the longest cache
|
|
62
|
+
* lifetime it offers that is not over what the request asks
|
|
63
|
+
* (`Request.cacheRetention`), or its default, so only a request asking an
|
|
64
|
+
* hour can write to the hour's cache at its dearer tariff. */ export let retained = (price, seconds = 0, shared = 0)=>({
|
|
65
|
+
...price,
|
|
66
|
+
...Math.max(seconds, shared) >= HOUR ? {} : {
|
|
67
|
+
cache_write_1h: undefined
|
|
68
|
+
},
|
|
69
|
+
...shared >= 43_200 ? {} : {
|
|
70
|
+
cache_write_12h: undefined
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
let HOUR = 3600;
|
|
74
|
+
let valid = (n)=>Number.isFinite(n) && n >= 0;
|
|
75
|
+
let upward = (dollars)=>Math.ceil(dollars * 1e12) / 1e12;
|
|
76
|
+
/** The most output affordable after reserving every input token at its highest
|
|
77
|
+
* possible tariff, and `searches` web searches at theirs. The returned dollars
|
|
78
|
+
* cover that entire call; a provider must enforce the returned output token
|
|
79
|
+
* limit before it spends anything. */ export let affordable = (price, input, remaining, tokens = 4096, searches = 0)=>{
|
|
80
|
+
let rates = [
|
|
81
|
+
price.input,
|
|
82
|
+
price.cached ?? price.input,
|
|
83
|
+
price.cache_write ?? price.input,
|
|
84
|
+
price.cache_write_1h ?? price.cache_write ?? price.input,
|
|
85
|
+
price.cache_write_12h ?? price.cache_write ?? price.input
|
|
86
|
+
];
|
|
87
|
+
if (!rates.every(valid) || !valid(price.output) || !valid(input) || !valid(remaining) || !valid(price.web_search ?? 0) || !Number.isSafeInteger(searches) || searches < 0 || !Number.isSafeInteger(input) || !Number.isSafeInteger(tokens) || tokens < 0 || tokens == 0 && price.output > 0) return null;
|
|
88
|
+
let fixed = upward(input * Math.max(...rates) / 1e6 + searches * (price.web_search ?? 0));
|
|
89
|
+
if (fixed > remaining) return null;
|
|
90
|
+
let output = price.output ? Math.min(tokens, Math.floor((remaining - fixed) * 1e6 / price.output)) : tokens;
|
|
91
|
+
let dollars = upward(fixed + output * price.output / 1e6);
|
|
92
|
+
// Floating point division can round the quotient up by one output token.
|
|
93
|
+
if (dollars > remaining && output > 0) {
|
|
94
|
+
output--;
|
|
95
|
+
dollars = upward(fixed + output * price.output / 1e6);
|
|
96
|
+
}
|
|
97
|
+
return dollars <= remaining && (output > 0 || tokens == 0) ? {
|
|
98
|
+
tokens: output,
|
|
99
|
+
dollars
|
|
100
|
+
} : null;
|
|
101
|
+
};
|
|
102
|
+
/** Measured dollars require reported cost or complete usage. Input the
|
|
103
|
+
* provider classified (it said how many tokens it wrote to the cache, none
|
|
104
|
+
* included) is weighed as it said; unclassified input uses its highest cache
|
|
105
|
+
* tariff; missing usage stays unknown. */ export let measured = (reply, price)=>{
|
|
106
|
+
if (reply.cost != null) {
|
|
107
|
+
return {
|
|
108
|
+
dollars: reply.cost,
|
|
109
|
+
reported: reply.costReported ?? true
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
let n = reply.usage;
|
|
113
|
+
if (!price || n?.input_tokens == null || price.output > 0 && n.output_tokens == null || !Object.values(n ?? {}).every((count)=>count == null || Number.isSafeInteger(count) && count >= 0)) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
if (n.cache_write_tokens != null) {
|
|
117
|
+
return {
|
|
118
|
+
dollars: weigh(price, n),
|
|
119
|
+
reported: false
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
let input = Math.max(price.input, price.cached ?? price.input, price.cache_write ?? price.input, price.cache_write_1h ?? price.cache_write ?? price.input, price.cache_write_12h ?? price.cache_write ?? price.input);
|
|
123
|
+
return {
|
|
124
|
+
dollars: weigh({
|
|
125
|
+
...price,
|
|
126
|
+
input
|
|
127
|
+
}, n),
|
|
128
|
+
reported: false
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
/** A conservative charge where the caller explicitly accounts unknown usage. */ export let charged = (reply, price, maximum)=>measured(reply, price) ?? {
|
|
132
|
+
dollars: maximum,
|
|
133
|
+
reported: false
|
|
134
|
+
};
|
|
135
|
+
//# sourceMappingURL=afford.js.map
|
package/afford.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"afford.js","sources":["./afford.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,6EAA6E;AAE7E,SAAiC,KAAK,qBAAoB;AAI1D;kCACkC,GAClC,OAAO,IAAI,UAAU,KAAI;AAEzB,IAAI,QAAQ,IAAI,IAAI;EAClB;EACA;EACA;EACA;EACA;EACA;EACA;CACD;AACD,IAAI,UAAU,CAAC,QACb,SAAS,QAAQ,OAAO,SAAS,YACjC,CAAC,YAAY,MAAM,CAAC,UAAU,CAAC,CAAC,iBAAiB,WAAW,KAC1D,CAAC,MAAM,OAAO,CAAC,SAAS,MAAM,KAAK,CAAC,WAAW,OAAO,OAAO,CAAC,OAAO,KAAK,CACxE,CAAC,CAAC,GAAG,EAAE,GACL,CAAC,MAAM,GAAG,CAAC,MAAM,CAAC,CAAC,KAAK,UAAU,MAAM,GAAG,CAAC,OAAO,GAAG,KAAK,QAAQ,GACtE;AAEL,IAAI,OAAO,CAAC,QACV,IAAI,cAAc,MAAM,CAAC,KAAK,SAAS,CAAC,UAAU,IAAI,MAAM;AAE9D;;;;;;;2CAO2C,GAC3C,OAAO,IAAI,cAAc,CACvB,QACA,SACA,QAEA,QAAQ,UACJ,KAAK,GAAG,CAAC,SAAS,IAAI,CAAC,KAAK,UAAU,K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