pi-dent-purse 0.2.0 → 0.2.2
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 +30 -30
- package/lib/adapters/amd.ts +19 -19
- package/lib/adapters/anthropic.ts +38 -38
- package/lib/adapters/antigravity.ts +15 -16
- 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 +1 -1
- 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 +12 -13
- package/lib/ui.ts +15 -13
- package/package.json +1 -1
package/lib/protocol.ts
CHANGED
|
@@ -10,16 +10,16 @@
|
|
|
10
10
|
import type { Refresher, RefreshMode, Timers } from "./refresh.ts";
|
|
11
11
|
import type { ProviderEntry } from "./state.ts";
|
|
12
12
|
|
|
13
|
-
/** The consumer → service channel (
|
|
13
|
+
/** The consumer → service channel (specs/query-protocol.md#channels). */
|
|
14
14
|
export const QUERY_CHANNEL = "purse:query:v1";
|
|
15
15
|
|
|
16
|
-
/** A provider id (
|
|
16
|
+
/** A provider id (specs/query-protocol.md#request); the pattern is compiled with the `u` flag. */
|
|
17
17
|
const PROVIDER_ID = /^[A-Za-z0-9][A-Za-z0-9._~-]{0,127}$/u;
|
|
18
18
|
|
|
19
|
-
/** The reply status values (
|
|
19
|
+
/** The reply status values (specs/query-protocol.md#service-obligations). */
|
|
20
20
|
export type QueryStatus = "ok" | "not-ready" | "bad-request" | "cancelled";
|
|
21
21
|
|
|
22
|
-
/** A reply (
|
|
22
|
+
/** A reply (specs/query-protocol.md#reply). */
|
|
23
23
|
export interface QueryReply {
|
|
24
24
|
protocol: 1;
|
|
25
25
|
status: QueryStatus;
|
|
@@ -27,10 +27,10 @@ export interface QueryReply {
|
|
|
27
27
|
providers: Record<string, ProviderEntry>;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
/** The offer callback a request carries (
|
|
30
|
+
/** The offer callback a request carries (specs/query-protocol.md#request). */
|
|
31
31
|
export type Respond = (reply: Promise<QueryReply>) => boolean;
|
|
32
32
|
|
|
33
|
-
/** A request (
|
|
33
|
+
/** A request (specs/query-protocol.md#request). Every field may hold any value: the payload is untrusted. */
|
|
34
34
|
export interface QueryRequest {
|
|
35
35
|
providers?: string[];
|
|
36
36
|
mode?: RefreshMode;
|
|
@@ -126,8 +126,8 @@ type Parsed =
|
|
|
126
126
|
};
|
|
127
127
|
|
|
128
128
|
/**
|
|
129
|
-
* Reads a payload per
|
|
130
|
-
* function is ignored; any other field that violates
|
|
129
|
+
* Reads a payload per specs/query-protocol.md#request: a payload that is not an object or whose `respond` is not a
|
|
130
|
+
* function is ignored; any other field that violates specs/query-protocol.md#request makes the request invalid.
|
|
131
131
|
* Unknown fields are ignored.
|
|
132
132
|
*/
|
|
133
133
|
function parseRequest(payload: unknown): Parsed {
|
|
@@ -154,7 +154,7 @@ function parseRequest(payload: unknown): Parsed {
|
|
|
154
154
|
const ids: string[] = [];
|
|
155
155
|
for (const provider of providers) {
|
|
156
156
|
if (typeof provider !== "string") return { kind: "invalid", respond };
|
|
157
|
-
//
|
|
157
|
+
// specs/query-protocol.md#request: an id outside the pattern is not served and has no entry.
|
|
158
158
|
if (PROVIDER_ID.test(provider)) ids.push(provider);
|
|
159
159
|
}
|
|
160
160
|
parsedProviders = ids;
|
|
@@ -199,12 +199,12 @@ function compareIds(left: string, right: string): number {
|
|
|
199
199
|
return left < right ? -1 : left > right ? 1 : 0;
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
/** Unique provider ids, sorted for stability; duplicates collapse (
|
|
202
|
+
/** Unique provider ids, sorted for stability; duplicates collapse (specs/query-protocol.md#request). */
|
|
203
203
|
function sortedUnique(ids: readonly string[]): string[] {
|
|
204
204
|
return [...new Set(ids)].toSorted(compareIds);
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
/** A deep copy, so a reply shares no state with the refresher or another reply (
|
|
207
|
+
/** A deep copy, so a reply shares no state with the refresher or another reply (specs/query-protocol.md#service-obligations). */
|
|
208
208
|
function cloneEntry(entry: ProviderEntry): ProviderEntry {
|
|
209
209
|
try {
|
|
210
210
|
return structuredClone(entry);
|
|
@@ -268,7 +268,7 @@ export function createQueryHandler(dependencies: QueryHandlerDependencies): Quer
|
|
|
268
268
|
detach = null;
|
|
269
269
|
}
|
|
270
270
|
if (link !== null) {
|
|
271
|
-
// Releases the refresher's waiters; it never aborts a refresh (refresh.md
|
|
271
|
+
// Releases the refresher's waiters; it never aborts a refresh (specs/refresh.md#refresh).
|
|
272
272
|
link.abort();
|
|
273
273
|
link = null;
|
|
274
274
|
}
|
|
@@ -336,7 +336,7 @@ export function createQueryHandler(dependencies: QueryHandlerDependencies): Quer
|
|
|
336
336
|
return;
|
|
337
337
|
}
|
|
338
338
|
|
|
339
|
-
// A non-accepting offer stops the service (
|
|
339
|
+
// A non-accepting offer stops the service (specs/query-protocol.md#service-obligations): no refresh, no listener on the
|
|
340
340
|
// signal. The reply still settles at once, with entries as they stand.
|
|
341
341
|
if (!accepted) {
|
|
342
342
|
finish("ok");
|
package/lib/refresh.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* single-flight, suppression, backoff, the refresh deadline, and offline mode. Provider
|
|
4
4
|
* entries are derived by `deriveProviderEntry` from the cache entry at call time.
|
|
5
5
|
*
|
|
6
|
-
* This module also owns what adapter-contract.md
|
|
6
|
+
* This module also owns what specs/adapter-contract.md#boundary, specs/adapter-contract.md#credentials put before `run`: it resolves
|
|
7
7
|
* and checks credentials (`checkCredentials`), binds each adapter's transport to its host
|
|
8
8
|
* allowlist, and holds the process-wide adapter stores and header observations.
|
|
9
9
|
*
|
|
@@ -37,24 +37,24 @@ import {
|
|
|
37
37
|
type ReasonCode,
|
|
38
38
|
} from "./state.ts";
|
|
39
39
|
|
|
40
|
-
/** The least time between the starts of two refreshes of one provider (
|
|
40
|
+
/** The least time between the starts of two refreshes of one provider (specs/refresh.md#refresh). */
|
|
41
41
|
export const MINIMUM_INTERVAL_MS = 30_000;
|
|
42
42
|
|
|
43
|
-
/** A refresh must settle within this time of its start (
|
|
43
|
+
/** A refresh must settle within this time of its start (specs/refresh.md#refresh). */
|
|
44
44
|
export const REFRESH_DEADLINE_MS = 15_000;
|
|
45
45
|
|
|
46
|
-
/** The serving age floor in seconds (
|
|
46
|
+
/** The serving age floor in seconds (specs/refresh.md#cache). */
|
|
47
47
|
export const SERVING_AGE_FLOOR_SECONDS = 3600;
|
|
48
48
|
|
|
49
|
-
/** Backoff cap and the `retry-after` clamp bounds in seconds (
|
|
49
|
+
/** Backoff cap and the `retry-after` clamp bounds in seconds (specs/refresh.md#refresh). */
|
|
50
50
|
export const BACKOFF_MAX_SECONDS = 3600;
|
|
51
51
|
export const RETRY_AFTER_MIN_SECONDS = 60;
|
|
52
52
|
export const RETRY_AFTER_MAX_SECONDS = 3600;
|
|
53
53
|
|
|
54
|
-
/** First step of the doubling backoff, in seconds (
|
|
54
|
+
/** First step of the doubling backoff, in seconds (specs/refresh.md#refresh). */
|
|
55
55
|
const BACKOFF_BASE_SECONDS = 60;
|
|
56
56
|
|
|
57
|
-
/** Reasons with a fixed 3600-second backoff (
|
|
57
|
+
/** Reasons with a fixed 3600-second backoff (specs/refresh.md#refresh). */
|
|
58
58
|
const LONG_BACKOFF_REASONS: ReadonlySet<ReasonCode> = new Set<ReasonCode>([
|
|
59
59
|
"http-401",
|
|
60
60
|
"http-403",
|
|
@@ -92,7 +92,7 @@ const REASON_CODES: Record<ReasonCode, true> = {
|
|
|
92
92
|
internal: true,
|
|
93
93
|
};
|
|
94
94
|
|
|
95
|
-
/** The request modes of query-protocol.md
|
|
95
|
+
/** The request modes of specs/query-protocol.md#request. */
|
|
96
96
|
export type RefreshMode = "cached" | "auto" | "refresh";
|
|
97
97
|
|
|
98
98
|
/** Timer functions; injectable so tests need no real waiting. */
|
|
@@ -101,7 +101,7 @@ export interface Timers {
|
|
|
101
101
|
clearTimeout(handle: unknown): void;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
/** What the live context provides (refresh.md
|
|
104
|
+
/** What the live context provides (specs/refresh.md#live-context); read at each use, never cached. */
|
|
105
105
|
export interface RefreshHost {
|
|
106
106
|
/** The provider's inventory models (`modelRegistry.getAll()`); `[]` when none. */
|
|
107
107
|
inventory(provider: string): readonly InventoryModel[];
|
|
@@ -123,7 +123,7 @@ export interface RefresherOptions {
|
|
|
123
123
|
host: () => RefreshHost | undefined;
|
|
124
124
|
/**
|
|
125
125
|
* A variable of the pi process's environment, read at each use; `undefined` when unset
|
|
126
|
-
* (adapter-contract.md
|
|
126
|
+
* (specs/adapter-contract.md#credentials). Only tool providers' credential variables are read.
|
|
127
127
|
*/
|
|
128
128
|
environment: (variable: string) => string | undefined;
|
|
129
129
|
/**
|
|
@@ -151,7 +151,7 @@ export interface RequestOptions {
|
|
|
151
151
|
export interface Refresher {
|
|
152
152
|
/** Whether a live context exists (query-protocol.md `not-ready` otherwise). */
|
|
153
153
|
ready(): boolean;
|
|
154
|
-
/** The known providers in ascending ASCII order (refresh.md
|
|
154
|
+
/** The known providers in ascending ASCII order (specs/refresh.md#live-context); `[]` without host. */
|
|
155
155
|
knownProviders(): string[];
|
|
156
156
|
/**
|
|
157
157
|
* Records a header observation for the provider, in the shape its adapter spec fixes,
|
|
@@ -164,7 +164,7 @@ export interface Refresher {
|
|
|
164
164
|
* Serves the provider per `mode` and resolves with its entry after any requested refresh
|
|
165
165
|
* settled. Never rejects. `cached`: the entry as it stands. `auto`: refreshes when no fresh
|
|
166
166
|
* capture. `refresh`: refreshes regardless of freshness. A refresh is joined when one is in
|
|
167
|
-
* flight, suppressed per
|
|
167
|
+
* flight, suppressed per specs/refresh.md#refresh (the current entry is returned), and never started for a
|
|
168
168
|
* provider without adapter, a free or adapter-disabled provider, a provider other than a tool
|
|
169
169
|
* provider without inventory models, or without live context. An aborted `signal` resolves at
|
|
170
170
|
* once with the current entry.
|
|
@@ -178,7 +178,7 @@ export interface Refresher {
|
|
|
178
178
|
clear(): void;
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
/** Backoff in seconds after a failed refresh (or a capture reason),
|
|
181
|
+
/** Backoff in seconds after a failed refresh (or a capture reason), specs/refresh.md#refresh. */
|
|
182
182
|
export function backoffSeconds(
|
|
183
183
|
reason: ReasonCode,
|
|
184
184
|
failureCount: number,
|
|
@@ -192,7 +192,7 @@ export function backoffSeconds(
|
|
|
192
192
|
}
|
|
193
193
|
|
|
194
194
|
/**
|
|
195
|
-
* The credential checks of adapter-contract.md
|
|
195
|
+
* The credential checks of specs/adapter-contract.md#credentials, in order: a non-empty `apiKey`, then
|
|
196
196
|
* the endpoint: the origin of `baseUrl` when present, else of every inventory model, equals the
|
|
197
197
|
* provider origin. A tool provider has no provider origin, so the endpoint check is skipped for
|
|
198
198
|
* it. Origins compare as `originOf` normalizes them.
|
|
@@ -218,7 +218,7 @@ export function checkCredentials(
|
|
|
218
218
|
return { apiKey, oauth: credentials.oauth === true };
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
/** Adapter-private store over the clock (adapter-contract.md
|
|
221
|
+
/** Adapter-private store over the clock (specs/adapter-contract.md#boundary). */
|
|
222
222
|
function createStore(clock: () => number): Store {
|
|
223
223
|
const items = new Map<string, { value: unknown; expiresAt: number | null }>();
|
|
224
224
|
return {
|
|
@@ -293,7 +293,7 @@ function failureOf(value: unknown): Outcome {
|
|
|
293
293
|
: { kind: "failure", reason };
|
|
294
294
|
}
|
|
295
295
|
|
|
296
|
-
/** Whether any facet of the result's `facets` has state `known` (refresh.md
|
|
296
|
+
/** Whether any facet of the result's `facets` has state `known` (specs/refresh.md#refresh). */
|
|
297
297
|
function hasKnownFacet(facets: unknown): boolean {
|
|
298
298
|
if (!isRecord(facets)) return false;
|
|
299
299
|
return ["plan", "balance", "limits"].some((name) => {
|
|
@@ -302,7 +302,7 @@ function hasKnownFacet(facets: unknown): boolean {
|
|
|
302
302
|
});
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
-
/**
|
|
305
|
+
/** specs/refresh.md#refresh Outcome: an untrusted adapter result becomes a failure or a success. */
|
|
306
306
|
function interpret(result: AdapterResult | unknown): Outcome {
|
|
307
307
|
if (!isRecord(result)) return { kind: "failure", reason: "internal" };
|
|
308
308
|
if ("reason" in result) return failureOf(result);
|
|
@@ -390,7 +390,7 @@ export function createRefresher(options: RefresherOptions): Refresher {
|
|
|
390
390
|
const cache = new Map<string, CacheEntry>();
|
|
391
391
|
const stores = new Map<string, Store>();
|
|
392
392
|
const observations = new Map<string, unknown>();
|
|
393
|
-
/** When each provider's latest header observation was recorded (
|
|
393
|
+
/** When each provider's latest header observation was recorded (specs/refresh.md#cache, freshness). */
|
|
394
394
|
const observedAt = new Map<string, number>();
|
|
395
395
|
const transports = new Map<string, Transport>();
|
|
396
396
|
|
|
@@ -402,7 +402,7 @@ export function createRefresher(options: RefresherOptions): Refresher {
|
|
|
402
402
|
const inventoryOf = (provider: string): readonly InventoryModel[] =>
|
|
403
403
|
options.host()?.inventory(provider) ?? [];
|
|
404
404
|
|
|
405
|
-
/** Whether a header observation was recorded after `lastStart` (
|
|
405
|
+
/** Whether a header observation was recorded after `lastStart` (specs/refresh.md#cache, freshness). */
|
|
406
406
|
const observedSince = (provider: string, lastStart: number | null): boolean => {
|
|
407
407
|
const at = observedAt.get(provider);
|
|
408
408
|
return at !== undefined && (lastStart === null || at > lastStart);
|
|
@@ -437,7 +437,7 @@ export function createRefresher(options: RefresherOptions): Refresher {
|
|
|
437
437
|
for (const [provider, settings] of Object.entries(configuration.providers)) {
|
|
438
438
|
if (settings.free === true && host.inventory(provider).length > 0) providers.add(provider);
|
|
439
439
|
}
|
|
440
|
-
//
|
|
440
|
+
// specs/refresh.md#live-context: every tool provider whose credential variable holds a non-empty value.
|
|
441
441
|
for (const adapter of adapterMap.values()) {
|
|
442
442
|
const variable = adapter.credentialVariable;
|
|
443
443
|
if (variable === undefined) continue;
|
|
@@ -466,7 +466,7 @@ export function createRefresher(options: RefresherOptions): Refresher {
|
|
|
466
466
|
}
|
|
467
467
|
|
|
468
468
|
/**
|
|
469
|
-
* The credentials of the adapter's provider (
|
|
469
|
+
* The credentials of the adapter's provider (specs/refresh.md#refresh): a tool provider's `apiKey` is the
|
|
470
470
|
* value of its credential variable, absent when unset, with no `oauth` and no `baseUrl`;
|
|
471
471
|
* every other provider's come from the host.
|
|
472
472
|
*/
|
|
@@ -483,7 +483,7 @@ export function createRefresher(options: RefresherOptions): Refresher {
|
|
|
483
483
|
});
|
|
484
484
|
}
|
|
485
485
|
|
|
486
|
-
/** Resolves and checks credentials, then builds the adapter's dependencies (
|
|
486
|
+
/** Resolves and checks credentials, then builds the adapter's dependencies (specs/refresh.md#cache, specs/refresh.md#refresh). */
|
|
487
487
|
async function prepare(
|
|
488
488
|
adapter: Adapter,
|
|
489
489
|
host: RefreshHost,
|
|
@@ -525,7 +525,7 @@ export function createRefresher(options: RefresherOptions): Refresher {
|
|
|
525
525
|
function record(state: CacheEntry, adapter: Adapter, outcome: Outcome, now: number): void {
|
|
526
526
|
if (outcome.kind === "failure") {
|
|
527
527
|
state.failure = { reason: outcome.reason, at: now };
|
|
528
|
-
//
|
|
528
|
+
// specs/refresh.md#refresh: an `offline` failure is recorded but changes neither the failure count
|
|
529
529
|
// nor the backoff end.
|
|
530
530
|
if (outcome.reason !== "offline") {
|
|
531
531
|
state.failureCount += 1;
|
|
@@ -652,7 +652,7 @@ export function createRefresher(options: RefresherOptions): Refresher {
|
|
|
652
652
|
host === undefined ||
|
|
653
653
|
settings.free === true ||
|
|
654
654
|
settings.adapter === false ||
|
|
655
|
-
//
|
|
655
|
+
// specs/refresh.md#refresh: only a tool provider needs no inventory model.
|
|
656
656
|
(adapter.credentialVariable === undefined && host.inventory(provider).length === 0)
|
|
657
657
|
) {
|
|
658
658
|
return Promise.resolve(entry(provider));
|
package/lib/state.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* provider entry.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
/** Provider entry status (billing-state.md
|
|
11
|
+
/** Provider entry status (specs/billing-state.md#status), first matching row wins. */
|
|
12
12
|
export type Status =
|
|
13
13
|
| "unknown-provider"
|
|
14
14
|
| "free"
|
|
@@ -18,10 +18,10 @@ export type Status =
|
|
|
18
18
|
| "unavailable"
|
|
19
19
|
| "not-collected";
|
|
20
20
|
|
|
21
|
-
/** Severity, in ascending gravity (billing-state.md
|
|
21
|
+
/** Severity, in ascending gravity (specs/billing-state.md#severity). */
|
|
22
22
|
export type Severity = "ok" | "low" | "critical" | "exhausted" | "unknown";
|
|
23
23
|
|
|
24
|
-
/** Reason code (billing-state.md
|
|
24
|
+
/** Reason code (specs/billing-state.md#reason-codes). */
|
|
25
25
|
export type ReasonCode =
|
|
26
26
|
| "not-collected"
|
|
27
27
|
| "offline"
|
|
@@ -47,10 +47,10 @@ export type ReasonCode =
|
|
|
47
47
|
| "project-unresolved"
|
|
48
48
|
| "internal";
|
|
49
49
|
|
|
50
|
-
/** A facet name (billing-state.md
|
|
50
|
+
/** A facet name (specs/billing-state.md#provider-entry). */
|
|
51
51
|
export type FacetName = "plan" | "balance" | "limits";
|
|
52
52
|
|
|
53
|
-
/** Unit of a limit window's `used` and `limit` (billing-state.md
|
|
53
|
+
/** Unit of a limit window's `used` and `limit` (specs/billing-state.md#limit-window). */
|
|
54
54
|
export type Unit =
|
|
55
55
|
| "percent"
|
|
56
56
|
| "credits"
|
|
@@ -59,16 +59,16 @@ export type Unit =
|
|
|
59
59
|
| "concurrent-requests"
|
|
60
60
|
| "currency";
|
|
61
61
|
|
|
62
|
-
/** How a limit window was obtained (billing-state.md
|
|
62
|
+
/** How a limit window was obtained (specs/billing-state.md#limit-window). */
|
|
63
63
|
export type Source = "endpoint" | "headers" | "documented";
|
|
64
64
|
|
|
65
|
-
/** A facet value: exactly one of the three shapes (billing-state.md
|
|
65
|
+
/** A facet value: exactly one of the three shapes (specs/billing-state.md#provider-entry). */
|
|
66
66
|
export type Facet<T> =
|
|
67
67
|
| { state: "known"; value: T }
|
|
68
68
|
| { state: "not-exposed" }
|
|
69
69
|
| { state: "unavailable"; reason: ReasonCode };
|
|
70
70
|
|
|
71
|
-
/** A limit window's captured fields (billing-state.md
|
|
71
|
+
/** A limit window's captured fields (specs/billing-state.md#limit-window). */
|
|
72
72
|
export interface LimitWindowBase {
|
|
73
73
|
id: string;
|
|
74
74
|
label: string;
|
|
@@ -83,18 +83,18 @@ export interface LimitWindowBase {
|
|
|
83
83
|
source: Source;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
/** A limit window in a provider entry (billing-state.md
|
|
86
|
+
/** A limit window in a provider entry (specs/billing-state.md#limit-window). */
|
|
87
87
|
export interface LimitWindow extends LimitWindowBase {
|
|
88
88
|
severity: Severity;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
/**
|
|
92
92
|
* A limit window as an adapter reports it: `severity` is absent unless the adapter spec
|
|
93
|
-
* overrides it (billing-state.md
|
|
93
|
+
* overrides it (specs/billing-state.md#severity).
|
|
94
94
|
*/
|
|
95
95
|
export type LimitWindowCapture = LimitWindowBase & { severity?: Severity };
|
|
96
96
|
|
|
97
|
-
/** A balance item's captured fields (billing-state.md
|
|
97
|
+
/** A balance item's captured fields (specs/billing-state.md#balance). */
|
|
98
98
|
export interface BalanceItemBase {
|
|
99
99
|
amount: number;
|
|
100
100
|
currency: string;
|
|
@@ -102,18 +102,18 @@ export interface BalanceItemBase {
|
|
|
102
102
|
gatesUsage: boolean;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
/** A balance item in a provider entry (billing-state.md
|
|
105
|
+
/** A balance item in a provider entry (specs/billing-state.md#balance). */
|
|
106
106
|
export interface BalanceItem extends BalanceItemBase {
|
|
107
107
|
severity: Severity;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
111
|
* A balance item as an adapter reports it: `severity` is absent unless the adapter spec
|
|
112
|
-
* overrides it (billing-state.md
|
|
112
|
+
* overrides it (specs/billing-state.md#severity).
|
|
113
113
|
*/
|
|
114
114
|
export type BalanceItemCapture = BalanceItemBase & { severity?: Severity };
|
|
115
115
|
|
|
116
|
-
/** USD per million tokens for one token class (prices.md
|
|
116
|
+
/** USD per million tokens for one token class (specs/prices.md#price). */
|
|
117
117
|
export interface Rates {
|
|
118
118
|
input: number | null;
|
|
119
119
|
output: number | null;
|
|
@@ -121,22 +121,22 @@ export interface Rates {
|
|
|
121
121
|
cacheWrite: number | null;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
/** Rates applying above an input-token count (prices.md
|
|
124
|
+
/** Rates applying above an input-token count (specs/prices.md#price). */
|
|
125
125
|
export interface PriceTier extends Rates {
|
|
126
126
|
inputTokensAbove: number;
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
/** A price's provenance (prices.md
|
|
129
|
+
/** A price's provenance (specs/prices.md#derivation). */
|
|
130
130
|
export type PriceSource = "inventory" | "config" | "free" | "unknown";
|
|
131
131
|
|
|
132
|
-
/** One recurring time window of a price schedule (prices.md
|
|
132
|
+
/** One recurring time window of a price schedule (specs/prices.md#schedule). */
|
|
133
133
|
export interface ScheduleWindow {
|
|
134
134
|
days: number[];
|
|
135
135
|
from: string;
|
|
136
136
|
to: string;
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
/** A price schedule with its derivation-time fields (prices.md
|
|
139
|
+
/** A price schedule with its derivation-time fields (specs/prices.md#schedule). */
|
|
140
140
|
export interface Schedule {
|
|
141
141
|
multiplier: number;
|
|
142
142
|
timeZone: string;
|
|
@@ -145,7 +145,7 @@ export interface Schedule {
|
|
|
145
145
|
changesAt: number | null;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
/** A model's price (prices.md
|
|
148
|
+
/** A model's price (specs/prices.md#price). */
|
|
149
149
|
export interface Price {
|
|
150
150
|
rates: Rates;
|
|
151
151
|
tiers: PriceTier[];
|
|
@@ -153,7 +153,7 @@ export interface Price {
|
|
|
153
153
|
schedule: Schedule | null;
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
/** A provider entry (billing-state.md
|
|
156
|
+
/** A provider entry (specs/billing-state.md#provider-entry). */
|
|
157
157
|
export interface ProviderEntry {
|
|
158
158
|
provider: string;
|
|
159
159
|
status: Status;
|
|
@@ -171,7 +171,7 @@ export interface ProviderEntry {
|
|
|
171
171
|
|
|
172
172
|
/**
|
|
173
173
|
* The facet values one capture holds; only the adapter's exposed facets are present
|
|
174
|
-
* (adapter-contract.md
|
|
174
|
+
* (specs/adapter-contract.md#boundary).
|
|
175
175
|
*/
|
|
176
176
|
export interface CaptureFacets {
|
|
177
177
|
plan?: Facet<string>;
|
|
@@ -179,20 +179,20 @@ export interface CaptureFacets {
|
|
|
179
179
|
limits?: Facet<LimitWindowCapture[]>;
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
-
/** One successful refresh's capture (refresh.md
|
|
182
|
+
/** One successful refresh's capture (specs/refresh.md#cache). */
|
|
183
183
|
export interface Capture {
|
|
184
184
|
facets: CaptureFacets;
|
|
185
185
|
capturedAt: number;
|
|
186
186
|
captureReason?: ReasonCode;
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
/** A whole adapter failure (adapter-contract.md
|
|
189
|
+
/** A whole adapter failure (specs/adapter-contract.md#boundary). */
|
|
190
190
|
export interface AdapterFailure {
|
|
191
191
|
reason: ReasonCode;
|
|
192
192
|
retryAfter?: number;
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
-
/** A successful adapter run (adapter-contract.md
|
|
195
|
+
/** A successful adapter run (specs/adapter-contract.md#boundary). */
|
|
196
196
|
export interface AdapterSuccess {
|
|
197
197
|
facets: CaptureFacets;
|
|
198
198
|
firstFailure?: AdapterFailure;
|
|
@@ -200,21 +200,20 @@ export interface AdapterSuccess {
|
|
|
200
200
|
captureReason?: ReasonCode;
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
/** An adapter's result (adapter-contract.md
|
|
203
|
+
/** An adapter's result (specs/adapter-contract.md#boundary). */
|
|
204
204
|
export type AdapterResult = AdapterFailure | AdapterSuccess;
|
|
205
205
|
|
|
206
|
-
/** The adapter constants entry derivation needs (adapter-contract.md
|
|
206
|
+
/** The adapter constants entry derivation needs (specs/adapter-contract.md#adapters). */
|
|
207
207
|
export interface AdapterProfile {
|
|
208
208
|
exposedFacets: readonly FacetName[];
|
|
209
209
|
freshnessSeconds: number;
|
|
210
210
|
/**
|
|
211
|
-
* A tool provider's environment variable holding its credential (adapter-contract.md
|
|
212
|
-
* Section 1); absent for every other provider.
|
|
211
|
+
* A tool provider's environment variable holding its credential (specs/adapter-contract.md#adapters); absent for every other provider.
|
|
213
212
|
*/
|
|
214
213
|
credentialVariable?: string;
|
|
215
214
|
}
|
|
216
215
|
|
|
217
|
-
/** The provider configuration entry derivation reads (config-file.md
|
|
216
|
+
/** The provider configuration entry derivation reads (specs/config-file.md#keys). */
|
|
218
217
|
export interface ProviderSettings {
|
|
219
218
|
free?: boolean;
|
|
220
219
|
adapter?: boolean;
|
|
@@ -222,13 +221,13 @@ export interface ProviderSettings {
|
|
|
222
221
|
lowBalance?: number;
|
|
223
222
|
}
|
|
224
223
|
|
|
225
|
-
/** An inventory model id with its derived price (prices.md
|
|
224
|
+
/** An inventory model id with its derived price (specs/prices.md#price). */
|
|
226
225
|
export interface ModelPrice {
|
|
227
226
|
id: string;
|
|
228
227
|
price: Price;
|
|
229
228
|
}
|
|
230
229
|
|
|
231
|
-
/** Everything entry derivation reads (billing-state.md
|
|
230
|
+
/** Everything entry derivation reads (specs/billing-state.md#status, specs/refresh.md#cache). */
|
|
232
231
|
export interface ProviderEntryInput {
|
|
233
232
|
provider: string;
|
|
234
233
|
now: number;
|
|
@@ -240,20 +239,20 @@ export interface ProviderEntryInput {
|
|
|
240
239
|
failure: ReasonCode | null;
|
|
241
240
|
}
|
|
242
241
|
|
|
243
|
-
/** The configured percent thresholds (config-file.md
|
|
242
|
+
/** The configured percent thresholds (specs/config-file.md#keys, specs/billing-state.md#severity). */
|
|
244
243
|
export interface SeverityThresholds {
|
|
245
244
|
low: number;
|
|
246
245
|
critical: number;
|
|
247
246
|
}
|
|
248
247
|
|
|
249
|
-
/** Thresholds used when none are configured (config-file.md
|
|
248
|
+
/** Thresholds used when none are configured (specs/config-file.md#keys). */
|
|
250
249
|
export const DEFAULT_SEVERITY_THRESHOLDS: SeverityThresholds = { low: 75, critical: 90 };
|
|
251
250
|
|
|
252
|
-
/** Longest vendor string an adapter may copy into a field (billing-state.md
|
|
251
|
+
/** Longest vendor string an adapter may copy into a field (specs/billing-state.md#content-restrictions). */
|
|
253
252
|
const VENDOR_STRING_LENGTH = 64;
|
|
254
253
|
|
|
255
254
|
/**
|
|
256
|
-
* Clean a vendor string an adapter spec names (billing-state.md
|
|
255
|
+
* Clean a vendor string an adapter spec names (specs/billing-state.md#content-restrictions): control
|
|
257
256
|
* characters removed, trimmed, cut to 64 Unicode code points; `null` when absent. Adapters
|
|
258
257
|
* apply it before returning their result.
|
|
259
258
|
*
|
|
@@ -267,13 +266,13 @@ export function cleanVendorString(value: unknown): string | null {
|
|
|
267
266
|
return cut === "" ? null : cut;
|
|
268
267
|
}
|
|
269
268
|
|
|
270
|
-
/** A finite vendor number, else `null` (billing-state.md
|
|
269
|
+
/** A finite vendor number, else `null` (specs/billing-state.md#content-restrictions). */
|
|
271
270
|
export function finiteNumber(value: unknown): number | null {
|
|
272
271
|
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
273
272
|
}
|
|
274
273
|
|
|
275
274
|
/**
|
|
276
|
-
* The percent a limit window consumed (billing-state.md
|
|
275
|
+
* The percent a limit window consumed (specs/billing-state.md#limit-window): rule 1 takes `captured`
|
|
277
276
|
* when it is a finite number ≥ 0; rules 2–4 derive it from `used` and `limit`.
|
|
278
277
|
*/
|
|
279
278
|
export function derivePercent(
|
|
@@ -308,7 +307,7 @@ function gravest(severities: readonly Severity[]): Severity {
|
|
|
308
307
|
return result;
|
|
309
308
|
}
|
|
310
309
|
|
|
311
|
-
/** Limit window severity (billing-state.md
|
|
310
|
+
/** Limit window severity (specs/billing-state.md#severity); an adapter-spec override wins. */
|
|
312
311
|
export function limitWindowSeverity(
|
|
313
312
|
percent: number | null,
|
|
314
313
|
thresholds: SeverityThresholds,
|
|
@@ -322,7 +321,7 @@ export function limitWindowSeverity(
|
|
|
322
321
|
return "ok";
|
|
323
322
|
}
|
|
324
323
|
|
|
325
|
-
/** Balance item severity (billing-state.md
|
|
324
|
+
/** Balance item severity (specs/billing-state.md#severity); an adapter-spec override wins. */
|
|
326
325
|
export function balanceSeverity(
|
|
327
326
|
amount: number,
|
|
328
327
|
lowBalance: number | null,
|
|
@@ -358,7 +357,7 @@ function severityOfFacets(
|
|
|
358
357
|
return gravest(severities);
|
|
359
358
|
}
|
|
360
359
|
|
|
361
|
-
/** Provider severity (billing-state.md
|
|
360
|
+
/** Provider severity (specs/billing-state.md#severity). */
|
|
362
361
|
export function providerSeverity(
|
|
363
362
|
status: Status,
|
|
364
363
|
limits: Facet<LimitWindow[]>,
|
|
@@ -367,7 +366,7 @@ export function providerSeverity(
|
|
|
367
366
|
return severityOfFacets(status, limits, balance, (window) => window.models === null);
|
|
368
367
|
}
|
|
369
368
|
|
|
370
|
-
/** Model severity (billing-state.md
|
|
369
|
+
/** Model severity (specs/billing-state.md#severity). */
|
|
371
370
|
export function modelSeverity(
|
|
372
371
|
status: Status,
|
|
373
372
|
limits: Facet<LimitWindow[]>,
|
|
@@ -419,7 +418,7 @@ function modelRecords(
|
|
|
419
418
|
|
|
420
419
|
/**
|
|
421
420
|
* The capture a cache entry serves: one younger than the serving age
|
|
422
|
-
* `max(3600 seconds, freshness period)` (refresh.md
|
|
421
|
+
* `max(3600 seconds, freshness period)` (specs/refresh.md#cache, specs/billing-state.md#status).
|
|
423
422
|
*/
|
|
424
423
|
function servedCapture(
|
|
425
424
|
capture: Capture | null,
|
|
@@ -436,7 +435,7 @@ function deriveLimitWindow(
|
|
|
436
435
|
thresholds: SeverityThresholds,
|
|
437
436
|
now: number,
|
|
438
437
|
): LimitWindow {
|
|
439
|
-
//
|
|
438
|
+
// specs/billing-state.md#limit-window: past its reset, a window's captured usage no longer holds.
|
|
440
439
|
const reset = window.resetsAt !== null && window.resetsAt <= now;
|
|
441
440
|
const used = reset ? null : window.used;
|
|
442
441
|
const percent = reset ? null : derivePercent(window.used, window.limit, window.percent);
|
|
@@ -513,7 +512,7 @@ function balanceFacet(
|
|
|
513
512
|
|
|
514
513
|
/**
|
|
515
514
|
* Derive a provider entry from the cache state, configuration, inventory prices, and the
|
|
516
|
-
* current time (billing-state.md
|
|
515
|
+
* current time (specs/billing-state.md#provider-entry, specs/billing-state.md#status, specs/billing-state.md#limit-window, specs/billing-state.md#balance, specs/billing-state.md#severity, specs/refresh.md#cache).
|
|
517
516
|
*/
|
|
518
517
|
export function deriveProviderEntry(input: ProviderEntryInput): ProviderEntry {
|
|
519
518
|
const { provider, models, adapter, settings, thresholds, now } = input;
|