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.
@@ -4,9 +4,9 @@
4
4
  * inference responses. Shared behavior is owned by specs/adapter-contract.md.
5
5
  *
6
6
  * The module performs no I/O: transport, credentials, clock, and observations arrive as
7
- * dependencies (adapter-contract.md Section 2). Percent and severity are derived after this
8
- * adapter returns (billing-state.md Sections 3 and 5); a window carries the raw vendor values
9
- * the spec names (adapter-anthropic.md Section 3).
7
+ * dependencies (specs/adapter-contract.md#boundary). Percent and severity are derived after this
8
+ * adapter returns (specs/billing-state.md#limit-window, specs/billing-state.md#severity); a window carries the raw vendor values
9
+ * the spec names (specs/adapter-anthropic.md#usage-request).
10
10
  */
11
11
 
12
12
  import type {
@@ -28,20 +28,20 @@ import {
28
28
  } from "../state.ts";
29
29
  import { headerNumber, lowercaseHeaders } from "./headers.ts";
30
30
 
31
- /** The provider origin and host allowlist (adapter-anthropic.md Section 1). */
31
+ /** The provider origin and host allowlist (specs/adapter-anthropic.md#constants). */
32
32
  const PROVIDER_ORIGIN = "https://api.anthropic.com";
33
33
  const HOST_ALLOWLIST: readonly string[] = [PROVIDER_ORIGIN];
34
34
 
35
- /** The subscription usage endpoint (adapter-anthropic.md Section 3). */
35
+ /** The subscription usage endpoint (specs/adapter-anthropic.md#usage-request). */
36
36
  const USAGE_URL = `${PROVIDER_ORIGIN}/api/oauth/usage`;
37
37
 
38
- /** The body cap of one response body, 64 KiB (adapter-anthropic.md Section 1). */
38
+ /** The body cap of one response body, 64 KiB (specs/adapter-anthropic.md#constants). */
39
39
  const USAGE_BODY_CAP = 64 * 1024;
40
40
 
41
- /** The largest age of a header observation the fallback may use, 3600 seconds (Section 4). */
41
+ /** The largest age of a header observation the fallback may use, 3600 seconds (specs/adapter-anthropic.md#header-observations). */
42
42
  const OBSERVATION_MAX_AGE_MS = 3600 * 1000;
43
43
 
44
- /** One window of a header observation (Section 4). */
44
+ /** One window of a header observation (specs/adapter-anthropic.md#header-observations). */
45
45
  export interface ObservedWindow {
46
46
  /** Percent 0–100; `null` when unobserved. */
47
47
  percent: number | null;
@@ -49,7 +49,7 @@ export interface ObservedWindow {
49
49
  resetsAt: number | null;
50
50
  }
51
51
 
52
- /** A header observation (Section 4). */
52
+ /** A header observation (specs/adapter-anthropic.md#header-observations). */
53
53
  export interface HeaderObservation {
54
54
  /** Epoch milliseconds when recorded. */
55
55
  observedAt: number;
@@ -57,7 +57,7 @@ export interface HeaderObservation {
57
57
  weekly: ObservedWindow;
58
58
  }
59
59
 
60
- /** A provider-wide window the endpoint body maps to (adapter-anthropic.md Section 3). */
60
+ /** A provider-wide window the endpoint body maps to (specs/adapter-anthropic.md#usage-request). */
61
61
  interface WindowMapping {
62
62
  /** The body key holding the window. */
63
63
  key: string;
@@ -71,13 +71,13 @@ const WINDOW_MAPPINGS: readonly WindowMapping[] = [
71
71
  { key: "seven_day", id: "weekly", label: "7d", windowSeconds: 604800 },
72
72
  ];
73
73
 
74
- /** The kind of `limits` element that carries a separately measured model group (Section 3.1). */
74
+ /** The kind of `limits` element that carries a separately measured model group (specs/adapter-anthropic.md#scoped-windows). */
75
75
  const SCOPED_KIND = "weekly_scoped";
76
76
 
77
- /** The window length of a scoped weekly window, 604800 seconds (Section 3.1). */
77
+ /** The window length of a scoped weekly window, 604800 seconds (specs/adapter-anthropic.md#scoped-windows). */
78
78
  const WEEKLY_SECONDS = 604800;
79
79
 
80
- /** The windows the header fallback reports (Section 4). */
80
+ /** The windows the header fallback reports (specs/adapter-anthropic.md#header-observations). */
81
81
  const OBSERVED_WINDOWS = {
82
82
  fiveHour: { id: "five-hour", label: "5h", windowSeconds: 18000 },
83
83
  weekly: { id: "weekly", label: "7d", windowSeconds: 604800 },
@@ -91,17 +91,17 @@ type UsageOutcome =
91
91
  firstFailure?: AdapterFailure;
92
92
  }
93
93
  | {
94
- /** The failure that stands (Section 3). */
94
+ /** The failure that stands (specs/adapter-anthropic.md#usage-request). */
95
95
  failure: AdapterFailure;
96
96
  firstFailure: AdapterFailure;
97
97
  };
98
98
 
99
- /** A JSON object, as opposed to `null`, an array, or a primitive (Section 3). */
99
+ /** A JSON object, as opposed to `null`, an array, or a primitive (specs/adapter-anthropic.md#usage-request). */
100
100
  function isObject(value: unknown): value is Record<string, unknown> {
101
101
  return typeof value === "object" && value !== null && !Array.isArray(value);
102
102
  }
103
103
 
104
- /** Whether `value` is one window of a header observation (Section 4). */
104
+ /** Whether `value` is one window of a header observation (specs/adapter-anthropic.md#header-observations). */
105
105
  function isObservedWindow(value: unknown): value is ObservedWindow {
106
106
  if (!isObject(value)) return false;
107
107
  const percent = value["percent"];
@@ -112,7 +112,7 @@ function isObservedWindow(value: unknown): value is ObservedWindow {
112
112
  );
113
113
  }
114
114
 
115
- /** Whether `value` is a header observation this adapter recorded (Section 4). */
115
+ /** Whether `value` is a header observation this adapter recorded (specs/adapter-anthropic.md#header-observations). */
116
116
  function isHeaderObservation(value: unknown): value is HeaderObservation {
117
117
  if (!isObject(value)) return false;
118
118
  return (
@@ -122,14 +122,14 @@ function isHeaderObservation(value: unknown): value is HeaderObservation {
122
122
  );
123
123
  }
124
124
 
125
- /** A request failure as the adapter reports it (adapter-contract.md Section 2). */
125
+ /** A request failure as the adapter reports it (specs/adapter-contract.md#boundary). */
126
126
  function failureFrom(failure: TransportFailure): AdapterFailure {
127
127
  return failure.retryAfter === undefined
128
128
  ? { reason: failure.reason }
129
129
  : { reason: failure.reason, retryAfter: failure.retryAfter };
130
130
  }
131
131
 
132
- /** The usage body, parsed; `null` when it is not JSON or not an object (Section 3). */
132
+ /** The usage body, parsed; `null` when it is not JSON or not an object (specs/adapter-anthropic.md#usage-request). */
133
133
  function parseBody(body: string): Record<string, unknown> | null {
134
134
  let parsed: unknown;
135
135
  try {
@@ -140,14 +140,14 @@ function parseBody(body: string): Record<string, unknown> | null {
140
140
  return isObject(parsed) ? parsed : null;
141
141
  }
142
142
 
143
- /** `resets_at` as epoch milliseconds; `null` when absent or unparsable (Section 3). */
143
+ /** `resets_at` as epoch milliseconds; `null` when absent or unparsable (specs/adapter-anthropic.md#usage-request). */
144
144
  function epochMillis(value: unknown): number | null {
145
145
  if (typeof value !== "string") return null;
146
146
  const parsed = Date.parse(value);
147
147
  return Number.isFinite(parsed) ? parsed : null;
148
148
  }
149
149
 
150
- /** The inventory model ids of the provider whose lowercase form contains `name` (Section 3.1). */
150
+ /** The inventory model ids of the provider whose lowercase form contains `name` (specs/adapter-anthropic.md#scoped-windows). */
151
151
  function scopedModels(inventory: readonly InventoryModel[], name: string): string[] {
152
152
  return inventory
153
153
  .filter((model) => model.id.toLowerCase().includes(name))
@@ -155,7 +155,7 @@ function scopedModels(inventory: readonly InventoryModel[], name: string): strin
155
155
  }
156
156
 
157
157
  /**
158
- * One `limits` element of a separately measured model group (Section 3.1): its `<name>` and its
158
+ * One `limits` element of a separately measured model group (specs/adapter-anthropic.md#scoped-windows): its `<name>` and its
159
159
  * window, `null` when the element's `models` list is empty; `null` for an element that is not one.
160
160
  */
161
161
  function scopedWindow(
@@ -190,7 +190,7 @@ function scopedWindow(
190
190
  };
191
191
  }
192
192
 
193
- /** The separately measured model groups' windows, per `limits` element in body order (Section 3.1). */
193
+ /** The separately measured model groups' windows, per `limits` element in body order (specs/adapter-anthropic.md#scoped-windows). */
194
194
  function scopedWindows(
195
195
  body: Record<string, unknown>,
196
196
  inventory: readonly InventoryModel[],
@@ -208,7 +208,7 @@ function scopedWindows(
208
208
  return windows;
209
209
  }
210
210
 
211
- /** One provider-wide body window (Section 3); a key that is absent, `null`, or not an object yields none. */
211
+ /** One provider-wide body window (specs/adapter-anthropic.md#usage-request); a key that is absent, `null`, or not an object yields none. */
212
212
  function windowFrom(mapping: WindowMapping, window: Record<string, unknown>): LimitWindowCapture {
213
213
  return {
214
214
  id: mapping.id,
@@ -224,7 +224,7 @@ function windowFrom(mapping: WindowMapping, window: Record<string, unknown>): Li
224
224
  };
225
225
  }
226
226
 
227
- /** The windows the usage body maps to: provider-wide, then scoped (Section 3). */
227
+ /** The windows the usage body maps to: provider-wide, then scoped (specs/adapter-anthropic.md#usage-request). */
228
228
  function endpointWindows(
229
229
  body: Record<string, unknown>,
230
230
  inventory: readonly InventoryModel[],
@@ -238,7 +238,7 @@ function endpointWindows(
238
238
  return windows;
239
239
  }
240
240
 
241
- /** One observed window (Section 4): observed values, unobserved ones `null`. */
241
+ /** One observed window (specs/adapter-anthropic.md#header-observations): observed values, unobserved ones `null`. */
242
242
  function observedWindow(
243
243
  window: ObservedWindow,
244
244
  id: string,
@@ -259,7 +259,7 @@ function observedWindow(
259
259
  };
260
260
  }
261
261
 
262
- /** The header observation's windows, five-hour then weekly (Section 4). */
262
+ /** The header observation's windows, five-hour then weekly (specs/adapter-anthropic.md#header-observations). */
263
263
  function observedWindows(observation: HeaderObservation): LimitWindowCapture[] {
264
264
  const { fiveHour, weekly } = OBSERVED_WINDOWS;
265
265
  return [
@@ -268,14 +268,14 @@ function observedWindows(observation: HeaderObservation): LimitWindowCapture[] {
268
268
  ];
269
269
  }
270
270
 
271
- /** The latest header observation, when younger than 3600 seconds (Section 4). */
271
+ /** The latest header observation, when younger than 3600 seconds (specs/adapter-anthropic.md#header-observations). */
272
272
  function freshObservation(deps: AdapterDependencies): HeaderObservation | undefined {
273
273
  const observation = deps.observations.latest();
274
274
  if (!isHeaderObservation(observation)) return undefined;
275
275
  return deps.clock() - observation.observedAt < OBSERVATION_MAX_AGE_MS ? observation : undefined;
276
276
  }
277
277
 
278
- /** One usage request with the listed headers (Section 3). */
278
+ /** One usage request with the listed headers (specs/adapter-anthropic.md#usage-request). */
279
279
  function issueUsageRequest(
280
280
  token: string,
281
281
  deps: AdapterDependencies,
@@ -295,8 +295,8 @@ function issueUsageRequest(
295
295
 
296
296
  /**
297
297
  * Runs the usage request and, on `http-401`, resolves credentials once more and repeats the
298
- * request once with a differing token (Section 3). When the repeat fails, the `http-401` stands
299
- * (Section 3: "otherwise, or when the repeat fails, the failure stands").
298
+ * request once with a differing token (specs/adapter-anthropic.md#usage-request). When the repeat fails, the `http-401` stands
299
+ * (specs/adapter-anthropic.md#usage-request: "otherwise, or when the repeat fails, the failure stands").
300
300
  */
301
301
  async function usageRequest(
302
302
  token: string,
@@ -333,7 +333,7 @@ async function usageRequest(
333
333
  return { body, firstFailure };
334
334
  }
335
335
 
336
- /** The result fields a successful run shares (adapter-contract.md Section 2). */
336
+ /** The result fields a successful run shares (specs/adapter-contract.md#boundary). */
337
337
  function success(outcome: UsageOutcome, limits: Facet<LimitWindowCapture[]>): AdapterSuccess {
338
338
  return {
339
339
  facets: { limits },
@@ -341,9 +341,9 @@ function success(outcome: UsageOutcome, limits: Facet<LimitWindowCapture[]>): Ad
341
341
  };
342
342
  }
343
343
 
344
- /** One refresh (adapter-contract.md Section 2, adapter-anthropic.md Sections 2–4). */
344
+ /** One refresh (specs/adapter-contract.md#boundary, specs/adapter-anthropic.md#authentication, specs/adapter-anthropic.md#usage-request, specs/adapter-anthropic.md#header-observations). */
345
345
  async function refresh(deps: AdapterDependencies, signal: AbortSignal): Promise<AdapterResult> {
346
- // adapter-contract.md Section 3 checks ran before `run`; this is the adapter's own (Section 2).
346
+ // specs/adapter-contract.md#credentials checks ran before `run`; this is the adapter's own (specs/adapter-anthropic.md#authentication).
347
347
  const credentials = await deps.credentials();
348
348
  if (credentials === undefined) return { reason: "no-credentials" };
349
349
  if (!credentials.oauth) return { reason: "unsupported-auth" };
@@ -351,7 +351,7 @@ async function refresh(deps: AdapterDependencies, signal: AbortSignal): Promise<
351
351
  const outcome = await usageRequest(credentials.apiKey, deps, signal);
352
352
 
353
353
  if ("failure" in outcome) {
354
- // adapter-anthropic.md Section 4: an `internal` failure stands even when a fresh
354
+ // specs/adapter-anthropic.md#header-observations: an `internal` failure stands even when a fresh
355
355
  // header observation exists; only other reasons fall back to the observation.
356
356
  if (outcome.failure.reason === "internal") return outcome.failure;
357
357
  const observation = freshObservation(deps);
@@ -371,7 +371,7 @@ async function refresh(deps: AdapterDependencies, signal: AbortSignal): Promise<
371
371
  return success(outcome, { state: "known", value: windows });
372
372
  }
373
373
 
374
- /** The headers a header observation reads (adapter-anthropic.md Section 4). */
374
+ /** The headers a header observation reads (specs/adapter-anthropic.md#header-observations). */
375
375
  const OBSERVATION_HEADERS = {
376
376
  fiveHourUtilization: "anthropic-ratelimit-unified-5h-utilization",
377
377
  fiveHourReset: "anthropic-ratelimit-unified-5h-reset",
@@ -380,7 +380,7 @@ const OBSERVATION_HEADERS = {
380
380
  } as const;
381
381
 
382
382
  /**
383
- * The header observation inference-response headers yield (adapter-anthropic.md Section 4), or
383
+ * The header observation inference-response headers yield (specs/adapter-anthropic.md#header-observations), or
384
384
  * `undefined` when neither utilization header is present. Header names compare
385
385
  * case-insensitively. The caller records it only for responses of provider `anthropic`.
386
386
  */
@@ -416,7 +416,7 @@ export function observeHeaders(
416
416
  };
417
417
  }
418
418
 
419
- /** The Anthropic adapter (adapter-anthropic.md Section 1). */
419
+ /** The Anthropic adapter (specs/adapter-anthropic.md#constants). */
420
420
  export const anthropic: Adapter = {
421
421
  id: "anthropic",
422
422
  providerOrigin: PROVIDER_ORIGIN,
@@ -1,8 +1,7 @@
1
1
  /**
2
2
  * The Antigravity adapter (specs/adapter-antigravity.md): the Cloud Code Assist quota summary,
3
3
  * plan, and credits read with the Google account's OAuth credential. The module performs no
4
- * I/O; transport, credentials, and clock arrive as dependencies (adapter-contract.md Section
5
- * 2). `run` never rejects.
4
+ * I/O; transport, credentials, and clock arrive as dependencies (specs/adapter-contract.md#boundary). `run` never rejects.
6
5
  */
7
6
 
8
7
  import type { Adapter, AdapterDependencies, InventoryModel } from "../adapter.ts";
@@ -15,7 +14,7 @@ import {
15
14
  type LimitWindowCapture,
16
15
  } from "../state.ts";
17
16
 
18
- /** Provider origin and host allowlist (Section 1). */
17
+ /** Provider origin and host allowlist (specs/adapter-antigravity.md#constants). */
19
18
  const PROVIDER_ORIGIN = "https://daily-cloudcode-pa.googleapis.com";
20
19
  const HOST_ALLOWLIST: readonly string[] = [
21
20
  PROVIDER_ORIGIN,
@@ -23,21 +22,21 @@ const HOST_ALLOWLIST: readonly string[] = [
23
22
  "https://cloudcode-pa.googleapis.com",
24
23
  ];
25
24
 
26
- /** The usage endpoints (Sections 3–4). */
25
+ /** The usage endpoints (specs/adapter-antigravity.md#quota-summary, specs/adapter-antigravity.md#plan-and-credits). */
27
26
  const QUOTA_URL = `${PROVIDER_ORIGIN}/v1internal:retrieveUserQuotaSummary`;
28
27
  const ASSIST_URL = `${PROVIDER_ORIGIN}/v1internal:loadCodeAssist`;
29
28
 
30
- /** The verbatim request bodies and user agent (Sections 3–4). */
29
+ /** The verbatim request bodies and user agent (specs/adapter-antigravity.md#quota-summary, specs/adapter-antigravity.md#plan-and-credits). */
31
30
  const QUOTA_BODY = "{}";
32
31
  const ASSIST_BODY =
33
32
  '{"metadata":{"ideType":"ANTIGRAVITY","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}}';
34
33
  const USER_AGENT =
35
34
  "antigravity/cli/1.5.5 (aidev_client; os_type=linux; arch=amd64; cl=982146307; auth_method=consumer)";
36
35
 
37
- /** Body cap, 1 MiB (Section 1). */
36
+ /** Body cap, 1 MiB (specs/adapter-antigravity.md#constants). */
38
37
  const BODY_CAP = 1024 * 1024;
39
38
 
40
- /** The credential the host stores for the provider (Section 2). */
39
+ /** The credential the host stores for the provider (specs/adapter-antigravity.md#authentication). */
41
40
  interface AntigravityCredential {
42
41
  token: string;
43
42
  projectId: string;
@@ -45,7 +44,7 @@ interface AntigravityCredential {
45
44
 
46
45
  const TOKEN_PATTERN = /^[\x21-\x7e]+$/u;
47
46
 
48
- /** The credential of the `apiKey` blob, or `null` when it is not one (Section 2). */
47
+ /** The credential of the `apiKey` blob, or `null` when it is not one (specs/adapter-antigravity.md#authentication). */
49
48
  function parseCredential(apiKey: string): AntigravityCredential | null {
50
49
  let parsed: unknown;
51
50
  try {
@@ -81,7 +80,7 @@ function arrayAt(object: Record<string, unknown>, key: string): readonly unknown
81
80
  return Array.isArray(value) ? value : null;
82
81
  }
83
82
 
84
- /** The epoch milliseconds of an RFC 3339 date-time, else `null` (Section 3). */
83
+ /** The epoch milliseconds of an RFC 3339 date-time, else `null` (specs/adapter-antigravity.md#quota-summary). */
85
84
  function epochOf(value: unknown): number | null {
86
85
  if (typeof value !== "string") return null;
87
86
  const ms = Date.parse(value);
@@ -140,7 +139,7 @@ interface GroupScope {
140
139
  models: string[] | null;
141
140
  }
142
141
 
143
- /** Derives the compact group tag and matching inventory models from `group.displayName` (Section 3). */
142
+ /** Derives the compact group tag and matching inventory models from `group.displayName` (specs/adapter-antigravity.md#quota-summary). */
144
143
  function groupScope(
145
144
  rawDisplayName: string | null,
146
145
  inventory: readonly InventoryModel[],
@@ -167,7 +166,7 @@ function groupScope(
167
166
  return { tag, models: matched };
168
167
  }
169
168
 
170
- /** Recognizes 5h and 7d durations from a bucket's cleaned display name (Section 3). */
169
+ /** Recognizes 5h and 7d durations from a bucket's cleaned display name (specs/adapter-antigravity.md#quota-summary). */
171
170
  function bucketDuration(
172
171
  displayName: string | null,
173
172
  ): { token: "5h" | "7d"; seconds: number } | null {
@@ -193,7 +192,7 @@ function bucketDuration(
193
192
  return null;
194
193
  }
195
194
 
196
- /** The quota pools' windows (Section 3). */
195
+ /** The quota pools' windows (specs/adapter-antigravity.md#quota-summary). */
197
196
  function quotaWindows(
198
197
  body: Record<string, unknown> | null,
199
198
  inventory: readonly InventoryModel[],
@@ -210,7 +209,7 @@ function quotaWindows(
210
209
  const bucketId = cleanVendorString(stringAt(bucketObject, "bucketId"));
211
210
  const displayName = cleanVendorString(stringAt(bucketObject, "displayName"));
212
211
  const fraction = finiteNumber(bucketObject["remainingFraction"]);
213
- // A fraction outside [0, 1] carries no honest percentage (Section 3).
212
+ // A fraction outside [0, 1] carries no honest percentage (specs/adapter-antigravity.md#quota-summary).
214
213
  if (fraction === null || fraction < 0 || fraction > 1) continue;
215
214
  const id = bucketId ?? displayName;
216
215
  if (id === null || windows.has(id)) continue;
@@ -238,7 +237,7 @@ function quotaWindows(
238
237
  return [...windows.values()];
239
238
  }
240
239
 
241
- /** The plan name, first that applies (Section 4). */
240
+ /** The plan name, first that applies (specs/adapter-antigravity.md#plan-and-credits). */
242
241
  function planName(body: Record<string, unknown> | null): string | null {
243
242
  if (body === null) return null;
244
243
  const paid = asRecord(body["paidTier"]);
@@ -252,7 +251,7 @@ function planName(body: Record<string, unknown> | null): string | null {
252
251
  return names.find((name) => name !== null) ?? null;
253
252
  }
254
253
 
255
- /** The credit windows of the first credits array present (Section 4). */
254
+ /** The credit windows of the first credits array present (specs/adapter-antigravity.md#plan-and-credits). */
256
255
  function creditWindows(body: Record<string, unknown> | null): LimitWindowCapture[] {
257
256
  if (body === null) return [];
258
257
  const paid = asRecord(body["paidTier"]);
@@ -297,7 +296,7 @@ async function refresh(deps: AdapterDependencies, signal: AbortSignal): Promise<
297
296
  if (credential === null) return { reason: "unsupported-auth" };
298
297
  const { token } = credential;
299
298
 
300
- // Sections 3–4: the issue order, quota summary before plan.
299
+ // specs/adapter-antigravity.md#quota-summary, specs/adapter-antigravity.md#plan-and-credits: the issue order, quota summary before plan.
301
300
  const summary = await post(deps, QUOTA_URL, token, QUOTA_BODY, signal);
302
301
  const assist = await post(deps, ASSIST_URL, token, ASSIST_BODY, signal);
303
302
  const firstFailure = summary.failure ?? assist.failure;
@@ -5,10 +5,10 @@
5
5
  * request goes through the injected transport.
6
6
  *
7
7
  * The balance request is the adapter's only request. A balance problem — a failed request, a
8
- * body that is not JSON, a body violating the shape of Section 2, or no usable item — makes the
8
+ * body that is not JSON, a body violating the shape of specs/adapter-deepseek.md#balance, or no usable item — makes the
9
9
  * `balance` facet `unavailable` with its reason; the `limits` facet is known in every result
10
- * (Section 3). A failed request, or a `bad-response` body, is the result's `firstFailure`
11
- * (adapter-contract.md Section 2).
10
+ * (specs/adapter-deepseek.md#limits). A failed request, or a `bad-response` body, is the result's `firstFailure`
11
+ * (specs/adapter-contract.md#boundary).
12
12
  */
13
13
 
14
14
  import type { Adapter, AdapterDependencies, TransportFailure } from "../adapter.ts";
@@ -23,22 +23,22 @@ import type {
23
23
  } from "../state.ts";
24
24
  import { finiteNumber } from "../state.ts";
25
25
 
26
- /** The provider origin and the only allowed origin (Section 1). */
26
+ /** The provider origin and the only allowed origin (specs/adapter-deepseek.md#constants). */
27
27
  const PROVIDER_ORIGIN = "https://api.deepseek.com";
28
28
 
29
- /** The balance endpoint (Section 2). */
29
+ /** The balance endpoint (specs/adapter-deepseek.md#balance). */
30
30
  const BALANCE_URL = "https://api.deepseek.com/user/balance";
31
31
 
32
- /** Maximum bytes of one response body (Section 1). */
32
+ /** Maximum bytes of one response body (specs/adapter-deepseek.md#constants). */
33
33
  const BODY_CAP_BYTES = 16 * 1024;
34
34
 
35
- /** An ISO 4217 code as the vendor sends it (Section 2). */
35
+ /** An ISO 4217 code as the vendor sends it (specs/adapter-deepseek.md#balance). */
36
36
  const CURRENCY_PATTERN = /^[A-Z]{3}$/;
37
37
 
38
- /** A money amount as the vendor sends it: a decimal string, not a JSON number (Section 2). */
38
+ /** A money amount as the vendor sends it: a decimal string, not a JSON number (specs/adapter-deepseek.md#balance). */
39
39
  const AMOUNT_PATTERN = /^-?[0-9]+(\.[0-9]+)?$/;
40
40
 
41
- /** One documented concurrency limit before its per-result copy (Section 3). */
41
+ /** One documented concurrency limit before its per-result copy (specs/adapter-deepseek.md#limits). */
42
42
  interface DocumentedLimit {
43
43
  id: string;
44
44
  label: string;
@@ -46,7 +46,7 @@ interface DocumentedLimit {
46
46
  models: readonly string[];
47
47
  }
48
48
 
49
- /** The documented concurrency limits (Section 3), in the spec's order. */
49
+ /** The documented concurrency limits (specs/adapter-deepseek.md#limits), in the spec's order. */
50
50
  const DOCUMENTED_LIMITS: readonly DocumentedLimit[] = [
51
51
  {
52
52
  id: "concurrency-deepseek-flash",
@@ -62,7 +62,7 @@ const DOCUMENTED_LIMITS: readonly DocumentedLimit[] = [
62
62
  },
63
63
  ];
64
64
 
65
- /** A body that yields balance items, or the reason it yields none (Section 2). */
65
+ /** A body that yields balance items, or the reason it yields none (specs/adapter-deepseek.md#balance). */
66
66
  type BalanceOutcome = { items: BalanceItemCapture[] } | { reason: ReasonCode };
67
67
 
68
68
  /** Whether `value` is a JSON object (not `null`, not an array). */
@@ -70,7 +70,7 @@ function isRecord(value: unknown): value is Record<string, unknown> {
70
70
  return typeof value === "object" && value !== null && !Array.isArray(value);
71
71
  }
72
72
 
73
- /** Parse a response body; `null` when it is not JSON (adapter-contract.md Section 4). */
73
+ /** Parse a response body; `null` when it is not JSON (specs/adapter-contract.md#transport). */
74
74
  function parseJson(body: string): unknown {
75
75
  try {
76
76
  return JSON.parse(body);
@@ -79,7 +79,7 @@ function parseJson(body: string): unknown {
79
79
  }
80
80
  }
81
81
 
82
- /** The documented concurrency limits as limit windows (Section 3). */
82
+ /** The documented concurrency limits as limit windows (specs/adapter-deepseek.md#limits). */
83
83
  function documentedLimits(): LimitWindowCapture[] {
84
84
  return DOCUMENTED_LIMITS.map((limit) => ({
85
85
  id: limit.id,
@@ -96,7 +96,7 @@ function documentedLimits(): LimitWindowCapture[] {
96
96
  }
97
97
 
98
98
  /**
99
- * The balance items a parsed body yields (Section 2): one per distinct currency that matches
99
+ * The balance items a parsed body yields (specs/adapter-deepseek.md#balance): one per distinct currency that matches
100
100
  * the currency pattern and whose `total_balance` string matches the amount pattern, in body
101
101
  * order, the first of a repeated currency kept; `empty-response` when there is none.
102
102
  * `is_available` `false` overrides every item's severity to `exhausted`.
@@ -132,7 +132,7 @@ function balanceItems(body: unknown): BalanceOutcome {
132
132
  return { items };
133
133
  }
134
134
 
135
- /** A transport failure as the result's `firstFailure` (adapter-contract.md Section 2). */
135
+ /** A transport failure as the result's `firstFailure` (specs/adapter-contract.md#boundary). */
136
136
  function adapterFailure(failure: TransportFailure): AdapterFailure {
137
137
  return failure.retryAfter === undefined
138
138
  ? { reason: failure.reason }
@@ -141,7 +141,7 @@ function adapterFailure(failure: TransportFailure): AdapterFailure {
141
141
 
142
142
  /**
143
143
  * A result whose balance facet is `unavailable` with `reason`. A `bad-response` body makes the
144
- * request a failed one (adapter-contract.md Section 2); an `empty-response` body does not.
144
+ * request a failed one (specs/adapter-contract.md#boundary); an `empty-response` body does not.
145
145
  */
146
146
  function withUnavailableBalance(reason: ReasonCode): AdapterSuccess {
147
147
  const failed = reason === "bad-response";
@@ -154,9 +154,9 @@ function withUnavailableBalance(reason: ReasonCode): AdapterSuccess {
154
154
  };
155
155
  }
156
156
 
157
- /** One refresh: checks, the balance request, then the mapping (Sections 2–3). */
157
+ /** One refresh: checks, the balance request, then the mapping (specs/adapter-deepseek.md#balance, specs/adapter-deepseek.md#limits). */
158
158
  async function refresh(deps: AdapterDependencies, signal: AbortSignal): Promise<AdapterResult> {
159
- // adapter-contract.md Section 3 checks ran before `run`; any credential type is accepted.
159
+ // specs/adapter-contract.md#credentials checks ran before `run`; any credential type is accepted.
160
160
  const credentials = await deps.credentials();
161
161
  if (credentials === undefined) return { reason: "no-credentials" };
162
162
  const apiKey = credentials.apiKey;
@@ -190,10 +190,10 @@ async function refresh(deps: AdapterDependencies, signal: AbortSignal): Promise<
190
190
  }
191
191
 
192
192
  /**
193
- * The DeepSeek adapter (Section 1): id `deepseek`, provider origin and host allowlist
193
+ * The DeepSeek adapter (specs/adapter-deepseek.md#constants): id `deepseek`, provider origin and host allowlist
194
194
  * `https://api.deepseek.com`, freshness period 300 seconds, exposed facets `balance` and
195
195
  * `limits`. Must not throw across the boundary; an unexpected exception is `internal`
196
- * (adapter-contract.md Section 2).
196
+ * (specs/adapter-contract.md#boundary).
197
197
  */
198
198
  export const deepseek: Adapter = {
199
199
  id: "deepseek",