dsh-model-think-level 1.1.8 → 2.0.0

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.
@@ -0,0 +1,35 @@
1
+ /**
2
+ * One provider listing probe: ask an endpoint for the models it advertises.
3
+ *
4
+ * The same discipline governs every place this plugin interrogates an endpoint
5
+ * — the probe route, the key panel's discover op, the per-key discovery pass
6
+ * and the fetch overlay's aggregated catalogue — so the one bounded, signed,
7
+ * redirect-refusing read lives here once: the profile's configured request
8
+ * headers ride along, the credential is signed the way the protocol expects, a
9
+ * listing is never a place to be forwarded, and the reply is bounded before it
10
+ * is parsed.
11
+ *
12
+ * @module dsh-model-think-level/discovery-merge
13
+ */
14
+ import { type ListedModel } from './listing.js';
15
+ import type { RouteProtocol } from './key-refs.js';
16
+ /** One listing request: where to ask, and which credential to sign it with. */
17
+ export interface ListingProbe {
18
+ readonly baseURL: string;
19
+ readonly api: RouteProtocol;
20
+ readonly apiKey?: string | undefined;
21
+ readonly headers?: unknown;
22
+ readonly timeoutMs: number;
23
+ /** Transport override: the tests' stub, and the plugin's own fetch seam. */
24
+ readonly send?: (url: string, init: RequestInit) => Promise<Response>;
25
+ }
26
+ /**
27
+ * Ask one endpoint for the models it advertises.
28
+ *
29
+ * A probe that cannot be read — network, status, malformed body — answers
30
+ * undefined instead of throwing: a listing probe is always an enrichment,
31
+ * never a precondition of the caller's own work.
32
+ * @param probe - the endpoint, protocol, credential, and transport.
33
+ * @returns the listing's models, or undefined when it could not be read.
34
+ */
35
+ export declare function probeListing(probe: ListingProbe): Promise<ListedModel[] | undefined>;
@@ -1,6 +1,9 @@
1
1
  /**
2
- * The fetch-layer installer for provider request-header overrides. The decision of
3
- * WHAT to override lives in `headers-core.ts`; this module owns the global seam.
2
+ * The fetch-layer installer for provider request-header overrides AND multi-key
3
+ * request signing. The decision of WHAT to send lives in `headers-core.ts` (which
4
+ * headers a route declares) and `key-route-core.ts` (which key a route should
5
+ * sign with, and whether a failed attempt deserves the next key); this module
6
+ * owns the global seam and performs the retries.
4
7
  *
5
8
  * Shape of the seam: one process-wide wrapper owns `globalThis.fetch`, and
6
9
  * every activation is an ENTRY inside it. That is what makes the plugin safe
@@ -23,9 +26,35 @@
23
26
  * public surface that sees the merged headers. Both were probed against the
24
27
  * real runtime — see `.tmp-rel/headers-probe/`.
25
28
  *
29
+ * Why fetch is ALSO where multi-key failover lives: the provider pushes its
30
+ * `start` event only after a successful HTTP response and turns a bad key
31
+ * (401/403) or rate limit (429) into a thrown error, so by the time pi-ai sees
32
+ * a failure the request has already been spent. This wrapper holds the raw
33
+ * `Response` — status included — before the SDK interprets it, so it can cancel
34
+ * a rejected attempt and re-issue it with the next key while the request body
35
+ * is still available.
36
+ *
26
37
  * @module dsh-model-think-level/headers-fetch
27
38
  */
28
39
  import { emptyIndex, type HeaderIndex } from './headers-core.js';
40
+ import { type KeyCandidate } from './key-route-core.js';
41
+ /** What a key resolver is told about the request it must sign. */
42
+ export interface KeyRouteContext {
43
+ /** The provider route whose profile matched the request URL. */
44
+ readonly route: string;
45
+ /** The model id the request body named, when it could be read. */
46
+ readonly model: string | undefined;
47
+ }
48
+ /** Context for aggregating a provider's model listing across its keys. */
49
+ export interface ModelListingContext {
50
+ readonly route: string;
51
+ readonly url: string;
52
+ readonly headers: Headers;
53
+ readonly keys: readonly KeyCandidate[];
54
+ readonly signal?: AbortSignal | null;
55
+ /** The fetch below this plugin's wrapper, avoiding recursive interception. */
56
+ readonly send: typeof globalThis.fetch;
57
+ }
29
58
  /** Handle one activation keeps; `dispose` is idempotent. */
30
59
  export interface HeaderOverlayHandle {
31
60
  /** Remove this activation's entry, restoring the original fetch when it was the last. */
@@ -38,15 +67,36 @@ export interface HeaderOverlay extends HeaderOverlayHandle {
38
67
  }
39
68
  /** A mutable index source an activation reads on every request. */
40
69
  export interface OverlaySource {
70
+ /** The live header index this activation contributes. */
41
71
  current: HeaderIndex;
72
+ /**
73
+ * Optional live key resolver. When present, requests to a matched route are
74
+ * signed with the returned keys in order and failed over across them. Read as
75
+ * a live property so an activation can begin routing keys after install.
76
+ */
77
+ keys?: (context: KeyRouteContext) => Promise<readonly KeyCandidate[]>;
78
+ /** Combines a GET model listing across the resolved key candidates. */
79
+ modelListing?: (context: ModelListingContext) => Promise<Response | undefined>;
42
80
  }
43
81
  /**
44
82
  * Install (or join) the process-wide header wrapper.
45
- * @param source - the live index this activation contributes.
83
+ * @param source - the live index (and optional key resolver) this activation contributes.
46
84
  * @returns a handle whose `dispose` removes exactly this activation.
47
85
  */
48
86
  export declare function installHeaderOverlay(source: OverlaySource): HeaderOverlay;
49
87
  /** Whether this plugin's wrapper currently owns the global fetch (diagnostics). */
50
88
  export declare function headerOverlayInstalled(): boolean;
89
+ /**
90
+ * The fetch beneath this plugin's wrapper.
91
+ *
92
+ * Used by callers that compose their OWN credential for a request — the model
93
+ * probe and the per-key discovery pass build the auth header from a specific
94
+ * reference, so letting the wrapper re-sign them with the route's FIRST key
95
+ * would probe the wrong credential. Reaching for the function under the wrapper
96
+ * keeps their composed headers exactly as built, while still honouring any
97
+ * wrapper some other plugin installed first. When this plugin's wrapper is not
98
+ * the live global (nothing to bypass), the live fetch is returned unchanged.
99
+ */
100
+ export declare function headerOverlayBypass(): typeof globalThis.fetch;
51
101
  /** The empty index, re-exported so callers building one need a single import. */
52
102
  export { emptyIndex };
@@ -16,9 +16,39 @@
16
16
  * @module dsh-model-think-level/headers-pi-ai
17
17
  */
18
18
  import { type HeaderIndex } from './headers-core.js';
19
- /** A live route index shared with the fetch overlay. */
19
+ import { type KeyCandidate } from './key-route-core.js';
20
+ /**
21
+ * Forget which wire served or failed which model.
22
+ *
23
+ * The memories are process-wide optimisations: they may be dropped at any time
24
+ * without changing an answer, only the number of rejected round trips it takes
25
+ * to find one. Tests (and a host that swaps its provider configuration) call
26
+ * this to start from a clean slate.
27
+ */
28
+ export declare function resetWireMemories(): void;
29
+ /**
30
+ * The protocol a key actually dispatches on this route, and how much the model
31
+ * costs there, are decided by the one plan both seams share — see
32
+ * `planKeys` in the routing module. Nothing here ranks keys on its own: a second
33
+ * ordering would only be able to disagree with the wire the plan chose.
34
+ */
35
+ /** What a key resolver is told about the request it must sign and dispatch. */
36
+ export interface PiAiKeyContext {
37
+ /** The provider route whose profile claims this request. */
38
+ readonly route: string;
39
+ /** The model id the request names, when it could be read. */
40
+ readonly model: string | undefined;
41
+ }
42
+ /** A live route index shared with the fetch overlay, plus optional key routing. */
20
43
  export interface PiAiHeaderSource {
21
44
  current: HeaderIndex;
45
+ /**
46
+ * Optional live key resolver. When present, the proxy picks the ordered keys
47
+ * that may sign this request and (a) signs with each in turn, (b) re-dispatches
48
+ * the request through a pinned key's own protocol, and (c) fails over to the
49
+ * next key when a stream errors before any content is emitted.
50
+ */
51
+ keys?: (context: PiAiKeyContext) => Promise<readonly KeyCandidate[]>;
22
52
  }
23
53
  /** Handle returned for one adapter patch. */
24
54
  export interface PiAiHeaderTransformHandle {
package/lib/index.d.ts CHANGED
@@ -14,9 +14,12 @@
14
14
  */
15
15
  import type { Context } from '@deepseek-ai/cordis';
16
16
  import Schema from '@deepseek-ai/schemastery';
17
+ import { composeProbeHeaders } from './listing.js';
17
18
  import { buildAutofillPatch } from './autofill.js';
18
19
  /** Re-exported so the package entry keeps naming the patch builder. */
19
20
  export { buildAutofillPatch };
21
+ /** Re-exported so the package entry keeps naming the probe-header composer. */
22
+ export { composeProbeHeaders };
20
23
  /** Stable plugin id, matching the cordis.patch.yml row and the bundle id. */
21
24
  export declare const name = "dsh-model-think-level";
22
25
  /** Hard dependencies: the loader waits for these before calling apply. */
@@ -59,25 +62,6 @@ export interface Config {
59
62
  }
60
63
  /** Schemastery schema: Cordis validates the row config and fills defaults before apply(). */
61
64
  export declare const Config: Schema<Config>;
62
- /**
63
- * Compose the raw-models probe request's headers, mirroring the discipline of
64
- * the harness's own model discovery: the provider
65
- * profile's configured request headers form the base (deployment-owned
66
- * credentials like `x-api-key` ride along), `accept` is always JSON, and a
67
- * resolved credential's Bearer overwrites a profile `authorization` — which
68
- * survives only when no credential resolves (a route may authenticate through
69
- * its configured headers alone). Anthropic Messages answers through
70
- * `x-api-key` plus a fixed `anthropic-version`, and its Bearer arm is never
71
- * used (a profile `authorization` survives untouched there, exactly as the
72
- * official discovery leaves it). Entries Fetch would refuse are dropped
73
- * rather than failing the probe. Harness attribution headers are deliberately
74
- * not sent — this is a same-origin diagnostic, not a harness request.
75
- * @param profileHeaders - the profile's raw `headers` dict (simply absent
76
- * on older documents).
77
- * @param apiKey - the resolved credential, when one resolved.
78
- * @param api - the profile's wire protocol.
79
- */
80
- export declare function composeProbeHeaders(profileHeaders: unknown, apiKey: string | undefined, api: string): Record<string, string>;
81
65
  /**
82
66
  * Apply the plugin: autofill undeclared models on boot and after every commit
83
67
  * that touches the pi-ai namespace.