pi-dent-purse 0.1.0 → 0.2.1
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/README.md +63 -5
- package/index.ts +5 -5
- package/lib/adapter.ts +36 -31
- package/lib/adapters/amd.ts +19 -19
- package/lib/adapters/anthropic.ts +38 -38
- package/lib/adapters/antigravity.ts +346 -0
- package/lib/adapters/deepseek.ts +20 -20
- package/lib/adapters/exa.ts +25 -25
- package/lib/adapters/google.ts +64 -64
- package/lib/adapters/headers.ts +1 -1
- package/lib/adapters/index.ts +6 -2
- package/lib/adapters/openai-codex.ts +6 -6
- package/lib/adapters/zai.ts +23 -23
- package/lib/config.ts +45 -45
- package/lib/guidance.ts +1 -1
- package/lib/prices.ts +14 -14
- package/lib/protocol.ts +13 -13
- package/lib/refresh.ts +24 -24
- package/lib/state.ts +43 -44
- package/lib/transport.ts +27 -20
- package/lib/ui.ts +13 -13
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,16 +1,74 @@
|
|
|
1
1
|
# pi-dent-purse
|
|
2
2
|
|
|
3
|
-
A pi extension that collects per-provider LLM billing state
|
|
4
|
-
serves it to
|
|
3
|
+
A pi extension that collects per-provider LLM billing state — prices, balances, usage limits —
|
|
4
|
+
and serves it to the user and to other extensions. [pi-dent](https://www.npmjs.com/package/pi-dent-agent)
|
|
5
|
+
bundles it; a standalone pi can install it on its own.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
An agent spread over many LLM providers runs out of balances and into rate windows mid-task.
|
|
10
|
+
Purse keeps the answer in one place instead of leaving every tool to call provider APIs itself:
|
|
11
|
+
|
|
12
|
+
- One adapter per provider, fixed by its spec: it contacts only its allowlisted https origins
|
|
13
|
+
and caps every response body, so a bug cannot send credentials anywhere else.
|
|
14
|
+
- Prices are derived from pi's model inventory and the configuration; deriving them never
|
|
15
|
+
issues a request or runs a command.
|
|
16
|
+
- A value no adapter captured is `null`, never `0` — a failed capture cannot read as free.
|
|
17
|
+
- Failures back off instead of retrying at once; `PURSE_OFFLINE=1` stops all refreshes.
|
|
18
|
+
|
|
19
|
+
It is a separate package on purpose: it reads no pi-dent configuration and depends on no other
|
|
20
|
+
extension, so pi-dent can bundle it and a bare pi can install it.
|
|
21
|
+
|
|
22
|
+
## How it works
|
|
23
|
+
|
|
24
|
+
- At the first `session_start` of a boot window, pi-dent-purse reads the configuration once,
|
|
25
|
+
works out the known providers — the providers the session can actually use — and serves that
|
|
26
|
+
image for the whole window.
|
|
27
|
+
- For each provider with an adapter (`amd`, `anthropic`, `antigravity`, `deepseek`, `google`,
|
|
28
|
+
`openai-codex`, `zai`, and the tool provider `exa`), it captures plan, balances, and
|
|
29
|
+
usage-limit windows into an in-memory cache; nothing is written to disk. Freshness periods
|
|
30
|
+
and failure backoff decide when an adapter runs again (`specs/refresh.md`). For `anthropic`
|
|
31
|
+
and `amd` it also reads the billing headers of the provider's own inference responses, so an
|
|
32
|
+
ordinary model call doubles as a capture.
|
|
33
|
+
- The user sees `/purse [all] [refresh] [provider …]` and, with `statusLine` on, a status line
|
|
34
|
+
for the current model's provider; `specs/user-interface.md` owns both.
|
|
35
|
+
- Other extensions read provider entries on `purse:query:v1`, hear `purse:changed:v1`, and
|
|
36
|
+
contribute header observations on `purse:observe:v1` (`specs/query-protocol.md`).
|
|
37
|
+
- Unknown keys and values that violate their contract are ignored, one diagnostic each; the
|
|
38
|
+
rest still loads (`specs/config-file.md`).
|
|
39
|
+
|
|
40
|
+
## Using it
|
|
5
41
|
|
|
6
42
|
```sh
|
|
7
43
|
pi install npm:pi-dent-purse # add it to pi's packages
|
|
8
|
-
pi -e npm:pi-dent-purse # or try it for one run
|
|
9
44
|
```
|
|
10
45
|
|
|
11
|
-
|
|
46
|
+
pi-dent bundles it: do not install both. Requires Node 26 or later.
|
|
47
|
+
|
|
48
|
+
Configuration is optional — the file `~/.pi/purse/purse.conf` (JSONC), or the absolute path in
|
|
49
|
+
`PURSE_CONF`:
|
|
50
|
+
|
|
51
|
+
```jsonc
|
|
52
|
+
{
|
|
53
|
+
"providers": { "deepseek": { "lowBalance": 5 } },
|
|
54
|
+
"statusLine": true,
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Root keys, provider keys, and the `PURSE_*` variables are owned by `specs/config-file.md`.
|
|
59
|
+
|
|
60
|
+
## Documentation
|
|
12
61
|
|
|
13
|
-
|
|
62
|
+
| Document | Read it when you … |
|
|
63
|
+
| --------------------------- | -------------------------------------------- |
|
|
64
|
+
| `docs/development.md` | work on this repository. |
|
|
65
|
+
| `specs/adapter-contract.md` | change or add an adapter. |
|
|
66
|
+
| `specs/billing-state.md` | change the provider entry shape. |
|
|
67
|
+
| `specs/config-file.md` | change configuration keys or environment. |
|
|
68
|
+
| `specs/prices.md` | change how prices are derived. |
|
|
69
|
+
| `specs/query-protocol.md` | write an extension that reads billing state. |
|
|
70
|
+
| `specs/refresh.md` | change refresh, caching, or backoff. |
|
|
71
|
+
| `specs/user-interface.md` | change `/purse` or the status line. |
|
|
14
72
|
|
|
15
73
|
## License
|
|
16
74
|
|
package/index.ts
CHANGED
|
@@ -44,7 +44,7 @@ export interface WiringDependencies {
|
|
|
44
44
|
loadConfiguration?: () => LoadedConfiguration;
|
|
45
45
|
/**
|
|
46
46
|
* A variable of the pi process's environment, read at each use; defaults to `process.env`.
|
|
47
|
-
* Only tool providers' credential variables are read (adapter-contract.md
|
|
47
|
+
* Only tool providers' credential variables are read (specs/adapter-contract.md#credentials).
|
|
48
48
|
*/
|
|
49
49
|
environment?: (variable: string) => string | undefined;
|
|
50
50
|
/** Where the boot window lives; a fresh one by default. */
|
|
@@ -60,7 +60,7 @@ interface Window {
|
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
62
|
* State outliving one `wire` call. The host calls the extension factory again, with a new `pi`
|
|
63
|
-
* and bus, on session replacement; the boot window survives it (specs/refresh.md
|
|
63
|
+
* and bus, on session replacement; the boot window survives it (specs/refresh.md#live-context).
|
|
64
64
|
*/
|
|
65
65
|
export interface Boot {
|
|
66
66
|
pi: ExtensionAPI | undefined;
|
|
@@ -203,7 +203,7 @@ export function wire(pi: ExtensionAPI, dependencies: WiringDependencies = {}): v
|
|
|
203
203
|
}),
|
|
204
204
|
};
|
|
205
205
|
}
|
|
206
|
-
// refresh.md
|
|
206
|
+
// specs/refresh.md#live-context: purse is ready; consumers answered `not-ready` may ask again.
|
|
207
207
|
// The status line renders through the `purse:changed:v1` listener below.
|
|
208
208
|
(boot.pi ?? pi).events.emit(CHANGED_CHANNEL, {
|
|
209
209
|
protocol: 1,
|
|
@@ -262,14 +262,14 @@ export function wire(pi: ExtensionAPI, dependencies: WiringDependencies = {}): v
|
|
|
262
262
|
|
|
263
263
|
pi.on("after_provider_response", (event, ctx) => observe(ctx.model?.provider, event.headers));
|
|
264
264
|
|
|
265
|
-
// A forwarded response (query-protocol.md
|
|
265
|
+
// A forwarded response (specs/query-protocol.md#forwarded-response).
|
|
266
266
|
pi.events.on(OBSERVE_CHANNEL, (payload: unknown) => {
|
|
267
267
|
const forwarded = forwardedResponse(payload);
|
|
268
268
|
if (forwarded !== undefined) observe(forwarded.provider, forwarded.headers);
|
|
269
269
|
});
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
-
/** The forwarded response a payload is, or `undefined` (query-protocol.md
|
|
272
|
+
/** The forwarded response a payload is, or `undefined` (specs/query-protocol.md#forwarded-response). */
|
|
273
273
|
function forwardedResponse(
|
|
274
274
|
payload: unknown,
|
|
275
275
|
): { provider: string; headers: Record<string, string> } | undefined {
|
package/lib/adapter.ts
CHANGED
|
@@ -11,34 +11,34 @@ import type {
|
|
|
11
11
|
Rates,
|
|
12
12
|
} from "./state.ts";
|
|
13
13
|
|
|
14
|
-
/** Deadline of one request (
|
|
14
|
+
/** Deadline of one request (specs/adapter-contract.md#transport); the refresh signal may stop it earlier. */
|
|
15
15
|
export const REQUEST_DEADLINE_MS = 8000;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* An adapter (
|
|
18
|
+
* An adapter (specs/adapter-contract.md#adapters, specs/adapter-contract.md#boundary): its id is its provider id; the constants come from its adapter
|
|
19
19
|
* spec. `exposedFacets`, `freshnessSeconds`, and `credentialVariable` come from
|
|
20
20
|
* `AdapterProfile`.
|
|
21
21
|
*/
|
|
22
22
|
export interface Adapter extends AdapterProfile {
|
|
23
|
-
/** The provider id (
|
|
23
|
+
/** The provider id (specs/adapter-contract.md#adapters). */
|
|
24
24
|
id: string;
|
|
25
25
|
/**
|
|
26
|
-
* The https origin the host sends the provider's inference requests to (
|
|
26
|
+
* The https origin the host sends the provider's inference requests to (specs/adapter-contract.md#adapters); absent
|
|
27
27
|
* for a tool provider, which has none.
|
|
28
28
|
*/
|
|
29
29
|
providerOrigin?: string;
|
|
30
|
-
/** The https origins the adapter may contact (
|
|
30
|
+
/** The https origins the adapter may contact (specs/adapter-contract.md#adapters). */
|
|
31
31
|
hostAllowlist: readonly string[];
|
|
32
32
|
/**
|
|
33
|
-
* Runs one refresh (
|
|
33
|
+
* Runs one refresh (specs/adapter-contract.md#boundary). Must not throw across the boundary: an unexpected exception
|
|
34
34
|
* is a whole failure with `internal`. `signal` aborts at the refresh deadline.
|
|
35
35
|
*/
|
|
36
36
|
run(deps: AdapterDependencies, signal: AbortSignal): Promise<AdapterResult>;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
|
-
* The only way an adapter reaches the outside (
|
|
41
|
-
* the
|
|
40
|
+
* The only way an adapter reaches the outside (specs/adapter-contract.md#boundary). The refresh layer builds it after
|
|
41
|
+
* the specs/adapter-contract.md#credentials checks passed: `transport` is bound to the adapter's `hostAllowlist`, and
|
|
42
42
|
* `credentials` resolves to checked credentials.
|
|
43
43
|
*/
|
|
44
44
|
export interface AdapterDependencies {
|
|
@@ -54,21 +54,26 @@ export interface AdapterDependencies {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
|
-
* One request under
|
|
58
|
-
*
|
|
57
|
+
* One request under specs/adapter-contract.md#transport: `accept: application/json` and `user-agent: pi-dent-purse`
|
|
58
|
+
* added by the transport; method `GET` unless the request names `POST` with its spec's
|
|
59
|
+
* verbatim `body`. The allowlist is bound into the transport.
|
|
59
60
|
*/
|
|
60
61
|
export interface TransportRequest {
|
|
61
62
|
/** An `https:` URL without userinfo whose origin is in the bound allowlist, else `internal`. */
|
|
62
63
|
url: string;
|
|
63
|
-
/** Only the headers the adapter spec lists (
|
|
64
|
+
/** Only the headers the adapter spec lists (specs/adapter-contract.md#credentials, specs/adapter-contract.md#transport). */
|
|
64
65
|
headers?: Record<string, string>;
|
|
65
|
-
/**
|
|
66
|
+
/** `POST` only, and only where the adapter spec states the request's body verbatim. */
|
|
67
|
+
method?: "POST";
|
|
68
|
+
/** The verbatim body of a `POST` request; a body without `method` is refused. */
|
|
69
|
+
body?: string;
|
|
70
|
+
/** Body cap in bytes of a 2xx body (specs/adapter-contract.md#transport). */
|
|
66
71
|
bodyCap: number;
|
|
67
72
|
/** The refresh's abort signal: aborted means `deadline`; the 8-second expiry is `timeout`. */
|
|
68
73
|
signal: AbortSignal;
|
|
69
74
|
}
|
|
70
75
|
|
|
71
|
-
/** A transport success (
|
|
76
|
+
/** A transport success (specs/adapter-contract.md#boundary). */
|
|
72
77
|
export interface TransportSuccess {
|
|
73
78
|
status: number;
|
|
74
79
|
/** Lowercase-keyed. */
|
|
@@ -77,22 +82,22 @@ export interface TransportSuccess {
|
|
|
77
82
|
body: string;
|
|
78
83
|
}
|
|
79
84
|
|
|
80
|
-
/** A transport failure (
|
|
85
|
+
/** A transport failure (specs/adapter-contract.md#boundary, specs/adapter-contract.md#transport): `status` is present when a response arrived. */
|
|
81
86
|
export interface TransportFailure extends AdapterFailure {
|
|
82
87
|
status?: number;
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
/**
|
|
86
|
-
* Outcome of one request: a failure is the outcome with a `reason` field (
|
|
91
|
+
* Outcome of one request: a failure is the outcome with a `reason` field (specs/adapter-contract.md#boundary).
|
|
87
92
|
*/
|
|
88
93
|
export type TransportOutcome = TransportSuccess | TransportFailure;
|
|
89
94
|
|
|
90
95
|
/**
|
|
91
|
-
* Performs one request (
|
|
96
|
+
* Performs one request (specs/adapter-contract.md#boundary); never rejects.
|
|
92
97
|
*/
|
|
93
98
|
export type Transport = (request: TransportRequest) => Promise<TransportOutcome>;
|
|
94
99
|
|
|
95
|
-
/** One command under
|
|
100
|
+
/** One command under specs/adapter-contract.md#command-runner: no shell, run in the user's home directory. */
|
|
96
101
|
export interface CommandRequest {
|
|
97
102
|
command: string;
|
|
98
103
|
/** The argument vector the adapter spec gives. */
|
|
@@ -106,7 +111,7 @@ export interface CommandRequest {
|
|
|
106
111
|
}
|
|
107
112
|
|
|
108
113
|
/**
|
|
109
|
-
* A command that ran to exit (
|
|
114
|
+
* A command that ran to exit (specs/adapter-contract.md#boundary). A missing executable arrives here with code 1 and
|
|
110
115
|
* `ENOENT` in `stderr` (docs/development.md, Exec).
|
|
111
116
|
*/
|
|
112
117
|
export interface CommandSuccess {
|
|
@@ -115,32 +120,32 @@ export interface CommandSuccess {
|
|
|
115
120
|
stderr: string;
|
|
116
121
|
}
|
|
117
122
|
|
|
118
|
-
/** A command failure (
|
|
123
|
+
/** A command failure (specs/adapter-contract.md#boundary, specs/adapter-contract.md#command-runner): `timeout`, `deadline`, `response-too-large`, or `internal`. */
|
|
119
124
|
export interface CommandFailure {
|
|
120
125
|
reason: AdapterFailure["reason"];
|
|
121
126
|
}
|
|
122
127
|
|
|
123
|
-
/** Outcome of one command: a failure is the outcome with a `reason` field (
|
|
128
|
+
/** Outcome of one command: a failure is the outcome with a `reason` field (specs/adapter-contract.md#boundary). */
|
|
124
129
|
export type CommandOutcome = CommandSuccess | CommandFailure;
|
|
125
130
|
|
|
126
|
-
/** Runs one command (
|
|
131
|
+
/** Runs one command (specs/adapter-contract.md#boundary); never rejects. */
|
|
127
132
|
export type CommandRunner = (request: CommandRequest) => Promise<CommandOutcome>;
|
|
128
133
|
|
|
129
|
-
/** Credentials of a provider as the host resolves them (
|
|
134
|
+
/** Credentials of a provider as the host resolves them (specs/adapter-contract.md#credentials). */
|
|
130
135
|
export interface ProviderCredentials {
|
|
131
136
|
apiKey?: string;
|
|
132
137
|
oauth: boolean;
|
|
133
138
|
baseUrl?: string;
|
|
134
139
|
}
|
|
135
140
|
|
|
136
|
-
/** Credentials that passed the
|
|
141
|
+
/** Credentials that passed the specs/adapter-contract.md#credentials checks: a non-empty `apiKey`. */
|
|
137
142
|
export interface CheckedCredentials {
|
|
138
143
|
apiKey: string;
|
|
139
144
|
oauth: boolean;
|
|
140
145
|
}
|
|
141
146
|
|
|
142
147
|
/**
|
|
143
|
-
* Credentials of the adapter's provider (
|
|
148
|
+
* Credentials of the adapter's provider (specs/adapter-contract.md#boundary, specs/adapter-contract.md#credentials): the first call returns the credentials
|
|
144
149
|
* the refresh checked; each later call resolves and checks them afresh, `undefined` when the
|
|
145
150
|
* checks fail.
|
|
146
151
|
*/
|
|
@@ -155,7 +160,7 @@ export interface InventoryCostTier {
|
|
|
155
160
|
cacheWrite: number;
|
|
156
161
|
}
|
|
157
162
|
|
|
158
|
-
/** An inventory model's cost, USD per million tokens (
|
|
163
|
+
/** An inventory model's cost, USD per million tokens (specs/adapter-contract.md#boundary). */
|
|
159
164
|
export interface InventoryCost {
|
|
160
165
|
input: number;
|
|
161
166
|
output: number;
|
|
@@ -164,28 +169,28 @@ export interface InventoryCost {
|
|
|
164
169
|
tiers?: readonly InventoryCostTier[];
|
|
165
170
|
}
|
|
166
171
|
|
|
167
|
-
/** An inventory model (
|
|
172
|
+
/** An inventory model (specs/adapter-contract.md#boundary): the one inventory shape of purse. */
|
|
168
173
|
export interface InventoryModel {
|
|
169
174
|
id: string;
|
|
170
175
|
baseUrl: string;
|
|
171
176
|
cost: InventoryCost;
|
|
172
177
|
}
|
|
173
178
|
|
|
174
|
-
/** Current epoch milliseconds (
|
|
179
|
+
/** Current epoch milliseconds (specs/adapter-contract.md#boundary). */
|
|
175
180
|
export type Clock = () => number;
|
|
176
181
|
|
|
177
182
|
/**
|
|
178
|
-
* The provider's configuration section (
|
|
183
|
+
* The provider's configuration section (specs/adapter-contract.md#boundary, specs/config-file.md#keys): the keys
|
|
179
184
|
* entry derivation reads, `project` (`google` only), and the rate overrides.
|
|
180
185
|
*/
|
|
181
186
|
export interface ProviderConfiguration extends ProviderSettings {
|
|
182
187
|
project?: string;
|
|
183
|
-
/** Rate overrides keyed by model id (prices.md
|
|
188
|
+
/** Rate overrides keyed by model id (specs/prices.md#derivation). */
|
|
184
189
|
prices?: Record<string, { [Field in keyof Rates]?: number | null }>;
|
|
185
190
|
}
|
|
186
191
|
|
|
187
192
|
/**
|
|
188
|
-
* Adapter-private key-value state with per-item expiry, discarded with the cache (
|
|
193
|
+
* Adapter-private key-value state with per-item expiry, discarded with the cache (specs/adapter-contract.md#boundary):
|
|
189
194
|
* synchronous, `undefined` for absent or expired, no `ttlSeconds` meaning no expiry.
|
|
190
195
|
*/
|
|
191
196
|
export interface Store {
|
|
@@ -194,7 +199,7 @@ export interface Store {
|
|
|
194
199
|
}
|
|
195
200
|
|
|
196
201
|
/**
|
|
197
|
-
* Read access to the header observations of the adapter's provider (
|
|
202
|
+
* Read access to the header observations of the adapter's provider (specs/adapter-contract.md#boundary): `undefined`
|
|
198
203
|
* when none exists. The observation is in the shape the provider's adapter spec fixes, so it
|
|
199
204
|
* is `unknown` here and the adapter narrows it with a type guard.
|
|
200
205
|
*/
|
package/lib/adapters/amd.ts
CHANGED
|
@@ -4,37 +4,37 @@
|
|
|
4
4
|
* purse recorded. Shared behavior is owned by specs/adapter-contract.md.
|
|
5
5
|
*
|
|
6
6
|
* The module performs no I/O: the recorded observation and the clock arrive as dependencies
|
|
7
|
-
* (adapter-contract.md
|
|
8
|
-
* (billing-state.md
|
|
9
|
-
* (adapter-amd.md
|
|
7
|
+
* (specs/adapter-contract.md#boundary). Percent and severity are derived after this adapter returns
|
|
8
|
+
* (specs/billing-state.md#limit-window, specs/billing-state.md#severity); a window carries the raw vendor values the spec names
|
|
9
|
+
* (specs/adapter-amd.md#limits).
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import type { Adapter, AdapterDependencies } from "../adapter.ts";
|
|
13
13
|
import type { AdapterResult, LimitWindowCapture } from "../state.ts";
|
|
14
14
|
import { headerNumber, lowercaseHeaders } from "./headers.ts";
|
|
15
15
|
|
|
16
|
-
/** The provider origin and host allowlist (adapter-amd.md
|
|
16
|
+
/** The provider origin and host allowlist (specs/adapter-amd.md#constants): the adapter contacts none. */
|
|
17
17
|
const PROVIDER_ORIGIN = "https://api.tokenlab.amd.com";
|
|
18
18
|
const HOST_ALLOWLIST: readonly string[] = [];
|
|
19
19
|
|
|
20
|
-
/** The rpm window resets and is as long as this, in milliseconds and seconds (
|
|
20
|
+
/** The rpm window resets and is as long as this, in milliseconds and seconds (specs/adapter-amd.md#limits). */
|
|
21
21
|
const RPM_WINDOW_MS = 60_000;
|
|
22
22
|
const RPM_WINDOW_SECONDS = 60;
|
|
23
23
|
|
|
24
|
-
/** A window of a header observation that reports its allowance and what is left (
|
|
24
|
+
/** A window of a header observation that reports its allowance and what is left (specs/adapter-amd.md#header-observations). */
|
|
25
25
|
export interface AmdWindowObservation {
|
|
26
26
|
limit: number | null;
|
|
27
27
|
remaining: number | null;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
/** The budget window of a header observation (
|
|
30
|
+
/** The budget window of a header observation (specs/adapter-amd.md#header-observations): USD, with its own usage and reset. */
|
|
31
31
|
export interface AmdBudgetObservation extends AmdWindowObservation {
|
|
32
32
|
used: number | null;
|
|
33
33
|
/** Epoch milliseconds; `null` when unobserved. */
|
|
34
34
|
resetsAt: number | null;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
/** A header observation (
|
|
37
|
+
/** A header observation (specs/adapter-amd.md#header-observations). */
|
|
38
38
|
export interface AmdObservation {
|
|
39
39
|
/** Epoch milliseconds when recorded. */
|
|
40
40
|
observedAt: number;
|
|
@@ -42,7 +42,7 @@ export interface AmdObservation {
|
|
|
42
42
|
rpm: AmdWindowObservation;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
/** The headers a header observation reads (
|
|
45
|
+
/** The headers a header observation reads (specs/adapter-amd.md#header-observations). */
|
|
46
46
|
const OBSERVATION_HEADERS = {
|
|
47
47
|
budgetLimit: "x-ratelimit-limit-user-budget-usd",
|
|
48
48
|
budgetUsed: "x-ratelimit-used-user-budget-usd",
|
|
@@ -56,17 +56,17 @@ function isObject(value: unknown): value is Record<string, unknown> {
|
|
|
56
56
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
/** Whether `value` is a number or `null`, as an observed field is (
|
|
59
|
+
/** Whether `value` is a number or `null`, as an observed field is (specs/adapter-amd.md#header-observations). */
|
|
60
60
|
function isNumberOrNull(value: unknown): value is number | null {
|
|
61
61
|
return value === null || typeof value === "number";
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
/** Whether `value` is a window of an amd header observation (
|
|
64
|
+
/** Whether `value` is a window of an amd header observation (specs/adapter-amd.md#header-observations). */
|
|
65
65
|
function isWindow(value: unknown): value is AmdWindowObservation {
|
|
66
66
|
return isObject(value) && isNumberOrNull(value["limit"]) && isNumberOrNull(value["remaining"]);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
/** Whether `value` is the budget window of an amd header observation (
|
|
69
|
+
/** Whether `value` is the budget window of an amd header observation (specs/adapter-amd.md#header-observations). */
|
|
70
70
|
function isBudget(value: unknown): value is AmdBudgetObservation {
|
|
71
71
|
return (
|
|
72
72
|
isWindow(value) &&
|
|
@@ -76,7 +76,7 @@ function isBudget(value: unknown): value is AmdBudgetObservation {
|
|
|
76
76
|
);
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
/** Whether `value` is an amd header observation this adapter recorded (
|
|
79
|
+
/** Whether `value` is an amd header observation this adapter recorded (specs/adapter-amd.md#header-observations). */
|
|
80
80
|
function isAmdObservation(value: unknown): value is AmdObservation {
|
|
81
81
|
return (
|
|
82
82
|
isObject(value) &&
|
|
@@ -87,7 +87,7 @@ function isAmdObservation(value: unknown): value is AmdObservation {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
|
-
* The header observation inference-response headers yield (adapter-amd.md
|
|
90
|
+
* The header observation inference-response headers yield (specs/adapter-amd.md#header-observations), or
|
|
91
91
|
* `undefined` when neither limit header is present. Header names compare case-insensitively;
|
|
92
92
|
* a value that is absent, empty after trimming, or not a finite number is `null`. The caller
|
|
93
93
|
* records it only for responses of provider `amd`.
|
|
@@ -116,20 +116,20 @@ export function observeAmdHeaders(
|
|
|
116
116
|
};
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
/** The budget window's used: the observed one, else limit − remaining when both are known (
|
|
119
|
+
/** The budget window's used: the observed one, else limit − remaining when both are known (specs/adapter-amd.md#limits). */
|
|
120
120
|
function budgetUsed(budget: AmdBudgetObservation): number | null {
|
|
121
121
|
if (budget.used !== null) return budget.used;
|
|
122
122
|
if (budget.limit === null || budget.remaining === null) return null;
|
|
123
123
|
return budget.limit - budget.remaining;
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
/** The rpm window's used: limit − remaining when both are known (
|
|
126
|
+
/** The rpm window's used: limit − remaining when both are known (specs/adapter-amd.md#limits). */
|
|
127
127
|
function rpmUsed(rpm: AmdWindowObservation): number | null {
|
|
128
128
|
if (rpm.limit === null || rpm.remaining === null) return null;
|
|
129
129
|
return rpm.limit - rpm.remaining;
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
/** The observation's windows, budget then rpm; one whose limit is `null` is omitted (
|
|
132
|
+
/** The observation's windows, budget then rpm; one whose limit is `null` is omitted (specs/adapter-amd.md#limits). */
|
|
133
133
|
function observedWindows(observation: AmdObservation): LimitWindowCapture[] {
|
|
134
134
|
const { budget, rpm } = observation;
|
|
135
135
|
const windows: LimitWindowCapture[] = [];
|
|
@@ -165,7 +165,7 @@ function observedWindows(observation: AmdObservation): LimitWindowCapture[] {
|
|
|
165
165
|
return windows;
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
/** One refresh (adapter-contract.md
|
|
168
|
+
/** One refresh (specs/adapter-contract.md#boundary, specs/adapter-amd.md#header-observations, specs/adapter-amd.md#limits): no I/O. */
|
|
169
169
|
function refresh(deps: AdapterDependencies): AdapterResult {
|
|
170
170
|
const observation = deps.observations.latest();
|
|
171
171
|
if (!isAmdObservation(observation)) {
|
|
@@ -184,7 +184,7 @@ function refresh(deps: AdapterDependencies): AdapterResult {
|
|
|
184
184
|
};
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
/** The AMD TokenLab adapter (adapter-amd.md
|
|
187
|
+
/** The AMD TokenLab adapter (specs/adapter-amd.md#constants). */
|
|
188
188
|
export const amd: Adapter = {
|
|
189
189
|
id: "amd",
|
|
190
190
|
providerOrigin: PROVIDER_ORIGIN,
|