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/lib/transport.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  /**
2
- * Transport and command runner per specs/adapter-contract.md, Sections 4 and 5: the security
2
+ * Transport and command runner per specs/adapter-contract.md#transport, specs/adapter-contract.md#command-runner: the security
3
3
  * envelope every adapter request and command passes through.
4
4
  *
5
5
  * Both are factories over the one effect they perform — `createTransport` takes the `fetch` to
6
6
  * call, `createCommandRunner` takes the host's `pi.exec` — so tests reach no network and spawn
7
7
  * nothing. Neither rejects: a thrown fetch, an abort, a body over its cap, and invalid input all
8
- * resolve to a failure outcome with the reason code Section 4 or 5 assigns. A failure carries no
8
+ * resolve to a failure outcome with the reason code specs/adapter-contract.md#transport or 5 assigns. A failure carries no
9
9
  * response text, header, or stderr; no credential is read or written here.
10
10
  *
11
11
  * The allowlist is bound when the transport is created (`createTransport(fetch, allowlist)`):
@@ -39,7 +39,7 @@ export type ExecFunction = (
39
39
  options?: ExecOptions,
40
40
  ) => Promise<ExecResult>;
41
41
 
42
- /** Section 4: every request carries these two headers. */
42
+ /** specs/adapter-contract.md#transport: every request carries these two headers. */
43
43
  const ACCEPT_HEADER = "application/json";
44
44
  const USER_AGENT_HEADER = "pi-dent-purse";
45
45
 
@@ -51,8 +51,7 @@ function failure(reason: ReasonCode, status?: number, retryAfter?: number): Tran
51
51
  }
52
52
 
53
53
  /**
54
- * The origin an `https:` URL identifies, normalized for comparison (adapter-contract.md
55
- * Sections 3–4): lowercased host without trailing dots, default port omitted. `null` for
54
+ * The origin an `https:` URL identifies, normalized for comparison (specs/adapter-contract.md#credentials, specs/adapter-contract.md#transport): lowercased host without trailing dots, default port omitted. `null` for
56
55
  * anything that is not an `https:` URL, and for a URL carrying userinfo.
57
56
  */
58
57
  export function originOf(value: string): string | null {
@@ -69,7 +68,7 @@ export function originOf(value: string): string | null {
69
68
  return `https://${host}${url.port === "" ? "" : `:${url.port}`}`;
70
69
  }
71
70
 
72
- /** The `retry-after` value as seconds when it is a non-negative integer, else `undefined` (Section 4). */
71
+ /** The `retry-after` value as seconds when it is a non-negative integer, else `undefined` (specs/adapter-contract.md#transport). */
73
72
  function retryAfterSeconds(value: string | null): number | undefined {
74
73
  if (value === null) return undefined;
75
74
  const trimmed = value.trim();
@@ -78,7 +77,7 @@ function retryAfterSeconds(value: string | null): number | undefined {
78
77
  return Number.isSafeInteger(seconds) ? seconds : undefined;
79
78
  }
80
79
 
81
- /** Status-to-reason mapping (Section 4); a response arrived, so the failure carries `status`. */
80
+ /** Status-to-reason mapping (specs/adapter-contract.md#transport); a response arrived, so the failure carries `status`. */
82
81
  function statusFailure(status: number, headers: Headers): TransportFailure {
83
82
  if (status === 401) return failure("http-401", status);
84
83
  if (status === 403) return failure("http-403", status);
@@ -109,7 +108,7 @@ function decode(chunks: readonly Uint8Array[]): string {
109
108
  }
110
109
 
111
110
  /**
112
- * Reads a body up to `cap` bytes while it streams (Section 4): the cap is enforced on the
111
+ * Reads a body up to `cap` bytes while it streams (specs/adapter-contract.md#transport): the cap is enforced on the
113
112
  * chunks, never after buffering, and an abort interrupts a pending read. The text is returned
114
113
  * only on success; a failure keeps nothing.
115
114
  */
@@ -149,7 +148,7 @@ async function readBody(response: Response, cap: number, signal: AbortSignal): P
149
148
  }
150
149
 
151
150
  /**
152
- * The transport (Section 4) bound to one adapter's host allowlist: one request, the request
151
+ * The transport (specs/adapter-contract.md#transport) bound to one adapter's host allowlist: one request, the request
153
152
  * deadline, the body cap, and the status mapping; never a redirect, never a failure that
154
153
  * rejects.
155
154
  */
@@ -161,13 +160,13 @@ export function createTransport(
161
160
  allowedOrigins.map(originOf).filter((origin): origin is string => origin !== null),
162
161
  );
163
162
  return async (request: TransportRequest): Promise<TransportOutcome> => {
164
- // Section 4: the URL is checked before anything connects.
163
+ // specs/adapter-contract.md#transport: the URL is checked before anything connects.
165
164
  const origin = originOf(request.url);
166
165
  if (origin === null || !allowlist.has(origin)) return failure("internal");
167
166
 
168
167
  const cap = request.bodyCap;
169
168
  if (!Number.isFinite(cap) || cap < 0) return failure("internal");
170
- // Section 4: a body goes only with `POST`, and `POST` always carries one.
169
+ // specs/adapter-contract.md#transport: a body goes only with `POST`, and `POST` always carries one.
171
170
  const method = request.method === undefined ? "GET" : request.method;
172
171
  const requestBody = request.body;
173
172
  if (method === "POST" ? typeof requestBody !== "string" : requestBody !== undefined) {
@@ -220,7 +219,7 @@ export function createTransport(
220
219
  return failure("redirect", status === 0 ? undefined : status);
221
220
  }
222
221
 
223
- // Section 4: a non-2xx body is never read; the status and headers decide.
222
+ // specs/adapter-contract.md#transport: a non-2xx body is never read; the status and headers decide.
224
223
  if (status < 200 || status >= 300) {
225
224
  response.body?.cancel().catch(() => {});
226
225
  return statusFailure(status, response.headers);
@@ -238,7 +237,7 @@ export function createTransport(
238
237
  }
239
238
 
240
239
  /**
241
- * The command runner (Section 5): one command through the host exec API, without a shell, in the
240
+ * The command runner (specs/adapter-contract.md#command-runner): one command through the host exec API, without a shell, in the
242
241
  * user's home directory, with the adapter's deadline and output cap.
243
242
  */
244
243
  export function createCommandRunner(execFunction: ExecFunction): CommandRunner {
package/lib/ui.ts CHANGED
@@ -11,14 +11,14 @@ import type { Refresher, RequestOptions, RefreshMode, Timers } from "./refresh.t
11
11
  import type { GuidedModel } from "./guidance.ts";
12
12
  import type { BalanceItem, Facet, LimitWindow, Price, ProviderEntry } from "./state.ts";
13
13
 
14
- /** The command registered with the host (user-interface.md Section 1). */
14
+ /** The command registered with the host (specs/user-interface.md#purse-command). */
15
15
  export const PURSE_COMMAND_NAME = "purse";
16
- /** The command description (user-interface.md Section 1). */
16
+ /** The command description (specs/user-interface.md#purse-command). */
17
17
  export const PURSE_COMMAND_DESCRIPTION =
18
18
  "Show billing state: /purse [all] [refresh] [provider ...]";
19
- /** The status key used with `ctx.ui.setStatus` (user-interface.md Section 3). */
19
+ /** The status key used with `ctx.ui.setStatus` (specs/user-interface.md#status-line). */
20
20
  export const PURSE_STATUS_KEY = "purse";
21
- /** The custom type of the report message (user-interface.md Section 1). */
21
+ /** The custom type of the report message (specs/user-interface.md#purse-command). */
22
22
  export const PURSE_MESSAGE_TYPE = "purse";
23
23
 
24
24
  /** An argument token that selects a bypassing refresh. */
@@ -113,7 +113,7 @@ export function formatDuration(milliseconds: number): string {
113
113
  /**
114
114
  * A time left as up to two of its whole days, hours and minutes, each from the
115
115
  * remainder of the larger ones, e.g. `3d5h`, `2h14m`, `45m`; under a minute,
116
- * or at or below zero, `<1m` (user-interface.md Section 3).
116
+ * or at or below zero, `<1m` (specs/user-interface.md#status-line).
117
117
  */
118
118
  export function formatTimeLeft(milliseconds: number): string {
119
119
  if (!Number.isFinite(milliseconds)) return UNKNOWN_TEXT;
@@ -152,7 +152,7 @@ function unitText(window: LimitWindow): string {
152
152
  }
153
153
 
154
154
  // ---------------------------------------------------------------------------
155
- // Billing report (user-interface.md Section 2)
155
+ // Billing report (specs/user-interface.md#billing-report)
156
156
  // ---------------------------------------------------------------------------
157
157
 
158
158
  /** Options for {@link renderProviderEntry}. */
@@ -185,9 +185,11 @@ function limitLine(window: LimitWindow, now: number): string {
185
185
  };
186
186
 
187
187
  let line = ` ${sanitizeText(window.label)}`;
188
- // Section 2: no percent when it is unknown, the limit unknown, and used known.
188
+ // specs/user-interface.md#billing-report: no percent when it is unknown, the limit unknown, and used known.
189
189
  if (!(window.percent === null && window.limit === null && window.used !== null)) {
190
190
  line += ` ${formatPercent(window.percent)}`;
191
+ // specs/user-interface.md#billing-report: a known percent reads `<percent>% used`.
192
+ if (window.percent !== null) line += " used";
191
193
  }
192
194
  if (window.limit !== null) {
193
195
  line += ` ${amount(window.used)}/${amount(window.limit)} ${unitText(window)}`;
@@ -248,7 +250,7 @@ function narrowToGuided(
248
250
 
249
251
  /**
250
252
  * The entry the guided view shows: whole when it has no models, else only its guided models;
251
- * `undefined` when it has models but none guided (Section 1).
253
+ * `undefined` when it has models but none guided (specs/user-interface.md#purse-command).
252
254
  */
253
255
  function guidedEntry(
254
256
  entry: ProviderEntry,
@@ -311,10 +313,10 @@ export function renderReport(entries: readonly ProviderEntry[], options: ReportO
311
313
  }
312
314
 
313
315
  // ---------------------------------------------------------------------------
314
- // Status line (user-interface.md Section 3)
316
+ // Status line (specs/user-interface.md#status-line)
315
317
  // ---------------------------------------------------------------------------
316
318
 
317
- /** Whether the status line is active for a context (user-interface.md Section 3). */
319
+ /** Whether the status line is active for a context (specs/user-interface.md#status-line). */
318
320
  export function statusLineActive(
319
321
  statusLine: boolean,
320
322
  ctx: Pick<PurseStatusContext, "mode" | "hasUI">,
@@ -390,7 +392,7 @@ export function renderStatusLine(
390
392
  }
391
393
 
392
394
  // ---------------------------------------------------------------------------
393
- // Command argument parsing (user-interface.md Section 1)
395
+ // Command argument parsing (specs/user-interface.md#purse-command)
394
396
  // ---------------------------------------------------------------------------
395
397
 
396
398
  /** A parsed `/purse` invocation. */
@@ -542,7 +544,7 @@ export interface StatusLineOptions {
542
544
  export type StatusLineUpdater = (ctx: PurseStatusContext, model?: PurseModel) => void;
543
545
 
544
546
  // ---------------------------------------------------------------------------
545
- // Command (user-interface.md Section 1)
547
+ // Command (specs/user-interface.md#purse-command)
546
548
  // ---------------------------------------------------------------------------
547
549
 
548
550
  function knownProviders(refresher: Refresher): string[] {
@@ -600,7 +602,7 @@ export function createPurseCommand(options: PurseCommandOptions): PurseCommand {
600
602
  }
601
603
 
602
604
  // ---------------------------------------------------------------------------
603
- // Status line updater (user-interface.md Section 3)
605
+ // Status line updater (specs/user-interface.md#status-line)
604
606
  // ---------------------------------------------------------------------------
605
607
 
606
608
  /** Global timers that keep no reference on the process. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-dent-purse",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "A pi extension that collects per-provider LLM billing state and serves it to other extensions and the user",
5
5
  "keywords": [
6
6
  "billing",