dsh-model-think-level 1.1.8 → 2.0.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.
Files changed (56) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +38 -0
  3. package/assets/NOTICE.md +15 -0
  4. package/assets/unified_bank.json +32910 -0
  5. package/lib/client/ComposerSlider.d.ts +9 -9
  6. package/lib/client/EffortEditor.d.ts +22 -12
  7. package/lib/client/FingerprintPane.d.ts +36 -0
  8. package/lib/client/KeyManager.d.ts +36 -10
  9. package/lib/client/ProtocolMenu.d.ts +51 -0
  10. package/lib/client/capacity.d.ts +30 -0
  11. package/lib/client/events.d.ts +26 -0
  12. package/lib/client/fingerprint-client.d.ts +81 -0
  13. package/lib/client/fingerprint-runner.d.ts +61 -0
  14. package/lib/client/index.d.ts +6 -0
  15. package/lib/client/injection/add-card.d.ts +4 -0
  16. package/lib/client/injection/create-card-pane.d.ts +15 -0
  17. package/lib/client/injection/create-key-seat.d.ts +53 -0
  18. package/lib/client/injection/create-keys-pane.d.ts +46 -0
  19. package/lib/client/injection/editor-tab-bar.d.ts +2 -1
  20. package/lib/client/injection/editor-tabs.d.ts +11 -1
  21. package/lib/client/injection/key-reveal.d.ts +24 -0
  22. package/lib/client/injection/models-page-editor.d.ts +165 -1
  23. package/lib/client/injection/provider-card-slot.d.ts +3 -1
  24. package/lib/client/injection/provider-endpoints.d.ts +70 -0
  25. package/lib/client/injection/staged-keys.d.ts +169 -0
  26. package/lib/client/key-manager-client.d.ts +89 -12
  27. package/lib/client/locales.d.ts +80 -6
  28. package/lib/client/ops.d.ts +4 -0
  29. package/lib/client/refresh-scheduler.d.ts +29 -0
  30. package/lib/client/styles.d.ts +1 -1
  31. package/lib/client/types.d.ts +44 -2
  32. package/lib/client.js +2275 -266
  33. package/lib/client.js.map +4 -4
  34. package/lib/constants.d.ts +11 -0
  35. package/lib/discovery-merge.d.ts +35 -0
  36. package/lib/fingerprint/bank.d.ts +24 -0
  37. package/lib/fingerprint/challenges.d.ts +14 -0
  38. package/lib/fingerprint/core.d.ts +85 -0
  39. package/lib/fingerprint/eligibility.d.ts +8 -0
  40. package/lib/fingerprint/random.d.ts +4 -0
  41. package/lib/fingerprint/run.d.ts +110 -0
  42. package/lib/fingerprint/store.d.ts +31 -0
  43. package/lib/fingerprint/verdict.d.ts +64 -0
  44. package/lib/headers-fetch.d.ts +53 -3
  45. package/lib/headers-pi-ai.d.ts +31 -1
  46. package/lib/index.d.ts +3 -19
  47. package/lib/index.js +2438 -153
  48. package/lib/index.js.map +4 -4
  49. package/lib/key-index.d.ts +58 -1
  50. package/lib/key-probe.d.ts +126 -0
  51. package/lib/key-refs.d.ts +106 -0
  52. package/lib/key-route-core.d.ts +415 -0
  53. package/lib/knowledge.d.ts +6 -5
  54. package/lib/listing.d.ts +118 -0
  55. package/lib/multi-key-listing.d.ts +101 -0
  56. package/package.json +13 -5
@@ -69,6 +69,18 @@ export interface KeyRevealDeps {
69
69
  * how a takeover that has moved on (a provider whose key field is gone) drops
70
70
  * the affordance it no longer needs.
71
71
  */
72
+ /**
73
+ * Mark a field's key input without giving it an eye.
74
+ *
75
+ * The official page already draws its own reveal button INSIDE the API-key box
76
+ * (`position: absolute; inset: 1px 1px 1px auto`) whenever the box holds text,
77
+ * so on the add-provider form — where there is no stored credential for an eye
78
+ * to fetch anyway — a second button hanging under the field was pure redundancy
79
+ * drawn twice (user item 9). The mark is still worth making: it is what keeps
80
+ * the input findable after the official toggle unmasks it, so the key list below
81
+ * survives the click.
82
+ */
83
+ export declare function markKeyField(field: HTMLElement): void;
72
84
  export declare function revealKeyField(field: HTMLElement, deps: KeyRevealDeps): void;
73
85
  /** Undo {@link revealKeyField}: drop the button, the class, the marker, and
74
86
  * re-mask the input, leaving the official field exactly as it was rendered. */
@@ -82,3 +94,15 @@ export declare function resetKeyField(field: HTMLElement): void;
82
94
  * than uncovering an empty box.
83
95
  */
84
96
  export declare function loadProviderKey(route: string): Promise<string | undefined>;
97
+ /**
98
+ * Write into a React-controlled input the way the user's own typing would: the
99
+ * prototype setter bypasses React's value tracker (an instance-level override),
100
+ * and the bubbling `input` event then lets the official component take the
101
+ * value into its draft state — so a re-render keeps it instead of reverting to
102
+ * the empty draft.
103
+ *
104
+ * The create card's key-list pane uses the same gesture to take a value the
105
+ * user had already typed out of the official box when the draft list takes
106
+ * the field over.
107
+ */
108
+ export declare function writeInputValue(input: HTMLInputElement, value: string): void;
@@ -86,6 +86,16 @@ export interface HostLabels {
86
86
  * {@link echoOfficialModality}.
87
87
  */
88
88
  inputImage: readonly string[];
89
+ /**
90
+ * The disclosure's context-window input ("Context window" / "上下文窗口").
91
+ * Matched against the input's own aria-label when it carries one, else
92
+ * against the text of the label wrapping it — the DSH host renders the field
93
+ * label-wrapped with no aria-label of its own. The first copy is what a
94
+ * suggestion's capacities echo into: see {@link echoOfficialCapacities}.
95
+ */
96
+ contextWindow: readonly string[];
97
+ /** The disclosure's max-output input ("Max output" / "最大输出"). */
98
+ maxOutput: readonly string[];
89
99
  }
90
100
  /** The join the injector renders from. */
91
101
  export interface InjectorDeps {
@@ -166,6 +176,10 @@ export interface EditorMountProps {
166
176
  * DOM, never a kernel version.
167
177
  */
168
178
  officialInputTypes?: boolean;
179
+ /** The card reports whether it still covers the official capacity and
180
+ * input-type fields: it does while its thinking switch is on, and steps
181
+ * aside when the switch turns the card off. */
182
+ onCoverageChange?: (covered: boolean) => void;
169
183
  api: EffortEditorApi;
170
184
  readOnly: boolean;
171
185
  t: (key: string, params?: Record<string, string | number>) => string;
@@ -436,7 +450,18 @@ export declare function hideOfficialReasoning(container: HTMLElement): void;
436
450
  */
437
451
  export declare function showOfficialReasoning(container: HTMLElement): void;
438
452
  /**
439
- * The official input-types checkbox an image verdict belongs to, or undefined
453
+ * Stand the official context-window / max-output labels and the input-types
454
+ * fieldset down for a row whose editor renders those same decisions.
455
+ *
456
+ * Only DIRECT children of the disclosure are touched: the editor's own inputs
457
+ * (and its own modality pills) live inside `.bre-effort-slot`, which carries
458
+ * neither class stem. The marker attribute distinguishes this hide from one the
459
+ * host wrote itself, exactly like {@link hideOfficialReasoning}.
460
+ */
461
+ export declare function hideOfficialCapacityChrome(container: HTMLElement): void;
462
+ /** Hand the official capacity/input-type chrome back when the editor goes. */
463
+ export declare function showOfficialCapacityChrome(container: HTMLElement): void;
464
+ /** The official input-types checkbox an image verdict belongs to, or undefined
440
465
  * when this row ships no such control (a legacy kernel) — or ships one whose
441
466
  * boxes cannot be told apart.
442
467
  *
@@ -482,6 +507,79 @@ export declare function echoOfficialModality(container: HTMLElement, image: bool
482
507
  * untouched.
483
508
  */
484
509
  export declare function officialModalityIntent(container: HTMLElement | undefined, input: InputModalities | null | undefined, imageLabels: readonly string[]): InputModalities | null | undefined;
510
+ /**
511
+ * The official capacity inputs of one row's disclosure (context window, max
512
+ * output), or whatever of them could be identified.
513
+ *
514
+ * Two anchors, most specific first, plus a positional floor:
515
+ * 1. an input whose own aria-label starts with the field's copy — the same
516
+ * prefix match every other official control is found by;
517
+ * 2. an input whose WRAPPING LABEL's text carries the copy — the DSH host
518
+ * renders these fields label-wrapped with no aria-label of their own;
519
+ * 3. the disclosure's remaining plain inputs in document order, first =
520
+ * context window, second = max output. The floor only answers for a row
521
+ * whose labels matched nothing (an unnamed host variant); the DSH
522
+ * disclosure holds exactly these two inputs.
523
+ *
524
+ * The plugin's own editor sits inside the same disclosure, and a folded-row
525
+ * visit scopes the search to the whole row (head inputs included), so both are
526
+ * kept out of the candidate set: the editor by its `data-plugin` root, the row
527
+ * head's Model ID / Display name by their aria-labels.
528
+ */
529
+ export interface OfficialCapacityInputs {
530
+ contextWindow?: HTMLInputElement;
531
+ maxOutput?: HTMLInputElement;
532
+ }
533
+ export declare function officialCapacityInputsOf(scope: HTMLElement, labels: HostLabels): OfficialCapacityInputs;
534
+ /**
535
+ * Print a suggestion's reference capacities on the official inputs of one row.
536
+ *
537
+ * Like the modality echo, this edits the official form's own DRAFT — the inputs
538
+ * render the card's state, not the settings document, so writing the document
539
+ * could never show a value in them before the card is saved and reopened. The
540
+ * value goes through the native setter and a bubbled `input` event so React's
541
+ * controlled input carries it into the draft, and the card's own Save is what
542
+ * persists it. That is also why the capacities never ride the plugin's document
543
+ * write: two writers for one field would let a held write land after that Save
544
+ * and revert whatever the user chose in the meantime.
545
+ *
546
+ * FILL-ONLY: an input that already holds a positive number is a user decision
547
+ * and keeps its place — auto-adapt fills blanks, it does not overwrite.
548
+ * @returns which of the two fields actually landed a value.
549
+ */
550
+ export declare function echoOfficialCapacities(scope: HTMLElement, labels: HostLabels, values: {
551
+ contextWindow?: number;
552
+ maxTokens?: number;
553
+ }): {
554
+ contextWindow: boolean;
555
+ maxOutput: boolean;
556
+ };
557
+ /**
558
+ * Read the official capacity inputs as the row's form currently holds them.
559
+ *
560
+ * The editor's two fields seed from this rather than from a suggestion: the
561
+ * numbers belong to the form's draft (the catalogue's value, the user's own
562
+ * typing, or a suggestion already echoed into it), and the draft is the only
563
+ * place they exist before the card is saved.
564
+ */
565
+ export declare function readOfficialCapacities(scope: HTMLElement, labels: HostLabels): {
566
+ contextWindow?: number;
567
+ maxTokens?: number;
568
+ };
569
+ /**
570
+ * Write the editor's capacity fields into the official inputs — the USER's own
571
+ * typing, so unlike {@link echoOfficialCapacities} this one overwrites, and
572
+ * `undefined` empties the field (a capacity that inherits the catalogue value
573
+ * again instead of being pinned). A key the caller omits is left untouched.
574
+ * @returns which of the two fields the write actually changed.
575
+ */
576
+ export declare function writeOfficialCapacities(scope: HTMLElement, labels: HostLabels, next: {
577
+ contextWindow?: number | undefined;
578
+ maxTokens?: number | undefined;
579
+ }): {
580
+ contextWindow: boolean;
581
+ maxOutput: boolean;
582
+ };
485
583
  /**
486
584
  * One model row a card shows on screen: what the walk needs to name it, plus
487
585
  * the row element itself, so a verdict about its modality can be printed on
@@ -534,6 +632,72 @@ export interface ScreenModel {
534
632
  export declare function adaptEveryModel(state: ScanState, deps: InjectorDeps, route: string, visibleRows?: readonly ScreenModel[]): Promise<AutoAdaptReport>;
535
633
  /** Restore anchoring and remove every live re-pin window's frame and listeners. */
536
634
  export declare function releaseModelListRepins(): void;
635
+ /**
636
+ * Hold one card's viewport position while its content changes height.
637
+ *
638
+ * Growing a card below the header makes the browser's scroll anchoring keep a
639
+ * node near the GROWING part visually stable, which walks the card's header —
640
+ * and everything above it — up the viewport. Suppressing anchoring for the
641
+ * duration and re-pinning the card's top every frame turns that into the card
642
+ * growing DOWNWARD instead.
643
+ *
644
+ * The card does not reach its final height at once, which is what a
645
+ * stability-bounded loop got wrong: the official React commit renders the
646
+ * disclosure container on the click, and this plugin's editor mounts a
647
+ * debounced scan later through an async render. Measured on the live page with
648
+ * the settings list scrolled to its end, the first change lands in the click's
649
+ * own frame and the second ~100ms later: a loop that stopped three stable
650
+ * frames after the click (~50ms) had already handed anchoring back, and the
651
+ * second change then walked the card 587px up the viewport.
652
+ *
653
+ * So the window is TIME-bounded, and a window that is still watching the card
654
+ * grow is pushed out to {@link REPIN_QUIET_MS} past the last change it saw — it
655
+ * releases when the growth STOPS, not when a fixed guess says it should have.
656
+ * {@link REPIN_MAX_MS} bounds that.
657
+ *
658
+ * The window stands down early on two conditions, both of which mean the
659
+ * measurement it is working from is no longer valid: the USER scrolled (their
660
+ * scroll must not be fought), or the card left the document (React replaced
661
+ * it — a detached box measures 0, and correcting against that would throw the
662
+ * view somewhere arbitrary).
663
+ *
664
+ * "The user scrolled" is read from their INPUT — a wheel, a touch, a pointer
665
+ * press or a scrolling key — and NOT from the scroll offset, which is the one
666
+ * thing the page itself also moves: the browser clamps it when a card above
667
+ * collapses, and this loop moves it on purpose. Watching the offset called
668
+ * every one of those a user scroll and abandoned the pin, which is exactly
669
+ * what opening a provider card needs: one official editor at a time means the
670
+ * card that was open closes in the same commit, the content above the clicked
671
+ * row shrinks by the closing editor's height, and the row has to be held where
672
+ * the user left it rather than walk off the top of the view.
673
+ *
674
+ * The correction itself adds the delta to the offset because the offset and the
675
+ * viewport are inverse: a card that has moved DOWN the screen (a positive
676
+ * delta) is brought back by scrolling DOWN the document, and one that has moved
677
+ * up by scrolling up. Subtracting it — as this did while the suppression was
678
+ * doing all the work and the delta was always 0 — drives the card the wrong way
679
+ * whenever the page really did move it.
680
+ */
681
+ /**
682
+ * Let a growing card keep its place on screen without the browser buying the
683
+ * growth back.
684
+ *
685
+ * User item 1: opening 添加供应商 makes three things taller at once — the
686
+ * official page opens the first provider's editor above the new card, the
687
+ * create-card pass adds the tab bar, and the card's own key list lands a moment
688
+ * later. Scroll anchoring answers any of those by scrolling the page by the
689
+ * height that grew, which reads as the new card jumping and shoving the
690
+ * configured provider above it upward. Anchoring is switched off for a short
691
+ * window instead: no scroll write of ours, no compensation by the browser, and
692
+ * the card simply extends downward.
693
+ *
694
+ * Unlike {@link pinCardTop} this never writes a scroll offset — it only stops
695
+ * the browser from writing one — so it cannot fight a pin that is already
696
+ * running, and it releases itself on the same deadline a mount pin uses.
697
+ * @param node - an element whose ancestors' scroll containers should hold still.
698
+ * @param windowMs - how long to hold them still.
699
+ */
700
+ export declare function suppressScrollAnchoring(node: HTMLElement, windowMs?: number): void;
537
701
  /**
538
702
  * Hand a viewport delta back to whichever container actually moved the card.
539
703
  *
@@ -39,6 +39,8 @@ interface ProviderCardOccurrence {
39
39
  export type ProviderCardSlotProps = ProviderCardOccurrence & {
40
40
  api: RemoteApi;
41
41
  t: Translate;
42
+ /** The client root, so the fingerprint pane can subscribe to document edits. */
43
+ ctx?: ClientContext;
42
44
  };
43
45
  /**
44
46
  * The occurrence component: resolve this card's route, render the editor with
@@ -46,7 +48,7 @@ export type ProviderCardSlotProps = ProviderCardOccurrence & {
46
48
  * itself with the official editor. Exported for its own tests (the registration
47
49
  * below is the production path).
48
50
  */
49
- export declare function ProviderCardSlot({ provider, api, t }: ProviderCardSlotProps): ReactNode;
51
+ export declare function ProviderCardSlot({ provider, api, t, ctx }: ProviderCardSlotProps): ReactNode;
50
52
  /**
51
53
  * Take the provider-card seat.
52
54
  * @param ctx - client root context (its `slots` service registers the seat).
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Provider-card layout and dual endpoints (user items 3, 4, 6, 7).
3
+ *
4
+ * The official card renders its route fields in a fixed order — display name,
5
+ * base URL, API protocol — and offers a single base URL. This module reshapes
6
+ * that card in place on every scan. It is idempotent, so a React re-render
7
+ * self-heals on the next pass — the same pattern `hideOfficialReasoning` uses:
8
+ *
9
+ * - **the fields are re-ordered** to display name → API protocol → base URL,
10
+ * so the protocol sits above the address it governs.
11
+ * - **the API protocol is picked from cards.** The official `<select>` is kept
12
+ * as the value the host reads, but it is hidden behind a row of plugin cards —
13
+ * one per protocol, the one in force highlighted — so every choice is visible
14
+ * without opening a dropdown (user item 1).
15
+ * - **the address is labelled by protocol family.** The official base-URL
16
+ * input is prefixed with "OpenAI"/"Anthropic" according to the protocol the
17
+ * card currently selects, seated immediately LEFT of the input on its own
18
+ * line — never stacked above it — so the tag reads as the field's prefix.
19
+ * - **extra endpoints can be added.** A grey "更多端点" text button directly
20
+ * under the API address — where the rows it reveals appear — opens one more
21
+ * address row for EVERY family the selected protocol does not serve
22
+ * (Anthropic and Gemini under an OpenAI protocol, OpenAI and Gemini under
23
+ * Anthropic). They persist to the route's `endpoints` in this plugin's key
24
+ * index, which is what a key pinned to that family signs against.
25
+ *
26
+ * The add card carries the same three fields, so the same decoration applies to
27
+ * it — "添加供应商" is not left behind (user item 6).
28
+ *
29
+ * @module dsh-model-think-level/client/injection/provider-endpoints
30
+ */
31
+ import { type KeyManagerClient } from '../key-manager-client.js';
32
+ /** The card-field label anchors this module needs, as the host spells them. */
33
+ export interface EndpointLabels {
34
+ /** The provider-id field (names the route). */
35
+ readonly routeId: readonly string[];
36
+ /** The display-name field. */
37
+ readonly modelName: readonly string[];
38
+ /** The base-URL field. */
39
+ readonly baseUrl: readonly string[];
40
+ /** The API-protocol field. */
41
+ readonly apiProtocol: readonly string[];
42
+ }
43
+ /** The transport and copy an injection needs. */
44
+ export interface EndpointDeps {
45
+ /** Transport; injectable for tests. */
46
+ readonly client?: KeyManagerClient | undefined;
47
+ /** Copy for the injected controls. */
48
+ readonly t: (key: string, params?: Record<string, string | number>) => string;
49
+ }
50
+ /**
51
+ * Reshape one provider card: field order, protocol cards, address label, the
52
+ * 更多端点 toggle and the extra endpoint rows. Safe to call on every scan.
53
+ * @param card - the official card element.
54
+ * @param labels - the host's resolved label anchors.
55
+ * @param deps - the transport and copy.
56
+ */
57
+ export declare function decorateProviderCard(card: HTMLElement, labels: EndpointLabels, deps: EndpointDeps): void;
58
+ /**
59
+ * Hand the protocol selects back to the host (called on teardown): a hidden select
60
+ * is this module's doing, so it must not outlive the module.
61
+ */
62
+ export declare function forgetEndpointCache(): void;
63
+ /**
64
+ * Hand the provider cards back to the host entirely (called on teardown): every
65
+ * node and attribute this module injected — the hidden protocol selects, the
66
+ * protocol card groups, the family tags, the 更多端点 toggle and the extra
67
+ * address rows — comes out, so a disabled plugin leaves the official card
68
+ * exactly as the page built it.
69
+ */
70
+ export declare function teardownProviderEndpoints(): void;
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Deferred API keys: the create-provider card's key list, held until the
3
+ * provider it belongs to exists.
4
+ *
5
+ * The edit card's key list ({@link KeyManager}) is a view over HOST state —
6
+ * every mutation rides the key route, and that route refuses a provider the
7
+ * settings document does not declare. The create card's list edits exactly
8
+ * such a route: the id the user is TYPING is the one thing about the card the
9
+ * document does not have yet, and the card's own 保存 is what declares it. So
10
+ * the list on that card runs over a DRAFT store this module owns (see
11
+ * {@link createDraftKeyClient}), and the drafts wait here as an INTENT until
12
+ * the route really is declared — the same contract the card's request headers
13
+ * already follow (see `staged-headers`), for the same reason: the host's
14
+ * fail-closed fence on the key route is what keeps a stray profile out of the
15
+ * document, and it stays exactly as it is.
16
+ *
17
+ * The wait is bounded the same way: a pane's unmount cannot tell "the create
18
+ * landed and the card closed" from "the user gave up", so an intent is
19
+ * RELEASED at unmount and retired after {@link RELEASE_GRACE_MS}. And what
20
+ * decides whether an intent may write is the BASELINE — whether the route was
21
+ * already declared when the intent was staged — because an id that names an
22
+ * existing provider belongs to that provider, not to this card's drafts.
23
+ *
24
+ * The draft VALUES live in this page's memory only. They are never persisted,
25
+ * never sent anywhere but the eventual writes, and a reload discards them
26
+ * exactly as it discards the card's own unsaved fields — the contract the
27
+ * pane's copy states.
28
+ *
29
+ * @module dsh-model-think-level/client/injection/staged-keys
30
+ */
31
+ import { type RouteProtocol } from '../../key-refs.js';
32
+ import type { KeyManagerClient } from '../key-manager-client.js';
33
+ import type { RemoteApi } from '../types.js';
34
+ /**
35
+ * How long a released intent keeps waiting for its route to appear. The same
36
+ * window the headers ledger uses, and for the same reason: it only has to
37
+ * cover the gap between the card's own 保存 landing and the next scan reading
38
+ * it.
39
+ */
40
+ export declare const RELEASE_GRACE_MS = 15000;
41
+ /** One draft key as the create card's list holds it. The value never leaves
42
+ * this page until the drain writes it. */
43
+ export interface StagedKeyDraft {
44
+ /** Browser-side row identity; the host mints its own references on write. */
45
+ readonly ref: string;
46
+ /** The secret itself, held in memory until the drain stores it. */
47
+ readonly value: string;
48
+ /** Stored label; absent means the row shows its masked value. */
49
+ readonly alias?: string;
50
+ /** Wire protocol this key pins; absent follows the provider's own. */
51
+ readonly protocol?: RouteProtocol;
52
+ /** Whether the key is switched into the routing pool. */
53
+ readonly enabled: boolean;
54
+ }
55
+ /** How far the drain got with one draft: its host reference once the value
56
+ * landed, and whether the metadata after it has landed too. */
57
+ interface StagedKeyLanding {
58
+ /** The draft this landing belongs to ({@link StagedKeyDraft.ref}). */
59
+ readonly ref: string;
60
+ /** The reference the host minted when it stored the value. */
61
+ readonly hostRef: string;
62
+ /** Whether the protocol pin and the switch-off have landed too. */
63
+ done: boolean;
64
+ }
65
+ /** One pane's held key list, keyed by the route it will be written to. */
66
+ export interface StagedKeyIntent {
67
+ /** The complete draft list — the same whole-list value the pane renders. */
68
+ drafts: readonly StagedKeyDraft[];
69
+ /** Which drafts have already landed, with the host references they landed as. */
70
+ readonly landed: StagedKeyLanding[];
71
+ /** Whether this route was already declared when the intent was staged. */
72
+ declaredBefore?: boolean;
73
+ /** When the pane that staged this intent unmounted, if it has. */
74
+ releasedAt?: number;
75
+ /** Whether the drain has already said — once — that this route has no
76
+ * baseline, the one holding state that ends in silence. */
77
+ noted?: boolean;
78
+ /** Failed drain passes since the last one that wrote something. */
79
+ attempts: number;
80
+ }
81
+ /** The session's deferred-key ledger. */
82
+ export interface StagedKeysState {
83
+ /** Held drafts, by route. Readable so a test (and the console) can see what
84
+ * the session is still holding. */
85
+ readonly intents: Map<string, StagedKeyIntent>;
86
+ /** What each route's baseline read found, kept for the session so a list
87
+ * restaged after the drain retired its intent does not have to ask again. */
88
+ readonly baseline: Map<string, boolean>;
89
+ /** One drain at a time: an unmount's release-triggered flush and a scan's
90
+ * drain must not both walk the ledger and write the same route. */
91
+ draining: boolean;
92
+ }
93
+ /** A fresh, empty ledger. */
94
+ export declare function createStagedKeysState(): StagedKeysState;
95
+ /**
96
+ * Hold one pane's draft list for a route that is not declared yet.
97
+ *
98
+ * An empty list WITHDRAWS instead of staging: "no keys" is the state the route
99
+ * is already in, so staging it would only arm a write that changes nothing.
100
+ * The stamps of a known intent are kept: a list that changes after the pane is
101
+ * gone must not restart the grace window, and a list that changes after its
102
+ * route was already declared is still one that route never belonged to.
103
+ * @param state - the session's ledger.
104
+ * @param route - the route the pane's id field currently reads.
105
+ * @param drafts - the complete draft list.
106
+ */
107
+ export declare function stageKeys(state: StagedKeysState, route: string, drafts: readonly StagedKeyDraft[]): void;
108
+ /** Drop the drafts held for one route. */
109
+ export declare function withdrawKeys(state: StagedKeysState, route: string): void;
110
+ /**
111
+ * Mark one route's drafts as orphaned: the pane that staged them is gone and
112
+ * the card's own 保存 has already been pressed or will never be.
113
+ */
114
+ export declare function releaseKeys(state: StagedKeysState, route: string): void;
115
+ /**
116
+ * Record whether `route` was already declared when this card's drafts were
117
+ * staged. Kept per session, so a list restaged after the drain retired its
118
+ * intent does not ask twice.
119
+ */
120
+ export declare function markKeysRouteBaseline(state: StagedKeysState, route: string, declared: boolean): void;
121
+ /**
122
+ * Whether `route` still needs its baseline read.
123
+ * @returns True when the pane should ask the settings document.
124
+ */
125
+ export declare function needsKeysRouteBaseline(state: StagedKeysState, route: string): boolean;
126
+ /**
127
+ * Write every held draft list whose route is now declared.
128
+ *
129
+ * Runs on the debounced scan, beside the headers drain, which is the only
130
+ * place that already does wire work. Each intent lands in two phases — every
131
+ * value first, then each row's protocol pin and switch-off — because the host
132
+ * refuses to switch off the last enabled key, and a switch-off on the FIRST
133
+ * key would race the second one's arrival. Which drafts have already landed
134
+ * is remembered between passes, so a pass that fails half-way resumes without
135
+ * re-adding what the host already stored.
136
+ * @param state - the session's ledger.
137
+ * @param api - the settings Remote the baseline reads ride.
138
+ * @param client - the live key transport the writes ride.
139
+ * @returns How many RELEASED intents are still waiting for their route, so the
140
+ * caller keeps the scan coming while that is non-zero.
141
+ */
142
+ export declare function flushStagedKeys(state: StagedKeysState, api: RemoteApi, client: KeyManagerClient): Promise<number>;
143
+ /** The working store one draft pane edits: a plain mutable box the client and
144
+ * the pane both close over. */
145
+ export interface DraftKeyStore {
146
+ drafts: StagedKeyDraft[];
147
+ }
148
+ /** The repo's inline translator shape (see `KeyManager`). */
149
+ type Translator = (key: string, params?: Record<string, string | number>) => string;
150
+ /** What a draft client needs besides the store it edits. */
151
+ export interface DraftKeyClientDeps {
152
+ /** Copy for the one refusal the host would word itself on the edit card. */
153
+ readonly t: Translator;
154
+ /** Called with the complete list after every change. */
155
+ readonly onChange: (drafts: readonly StagedKeyDraft[]) => void;
156
+ }
157
+ /**
158
+ * The {@link KeyManagerClient} the create card's list runs on: every op edits
159
+ * the in-memory draft store and answers the whole list, so the card list the
160
+ * user sees is the same machinery the edit card runs over the host. Only the
161
+ * writes that need a route of their own (scope, endpoints, the merged-models
162
+ * offer) are no-ops here — the card has no models to scope by and no route to
163
+ * address.
164
+ * @param store - the pane's working store, shared by identity.
165
+ * @param deps - copy, and the change sink the pane stages through.
166
+ * @returns the client, stable for as long as the store is.
167
+ */
168
+ export declare function createDraftKeyClient(store: DraftKeyStore, deps: DraftKeyClientDeps): KeyManagerClient;
169
+ export {};
@@ -2,14 +2,23 @@
2
2
  * The browser half's client for the host's API-key manager.
3
3
  *
4
4
  * Every key fact — which references a provider lists, which label each carries,
5
- * which one is in use, and whether the credential store actually holds a value
6
- * — is host state: the list is the plugin's own index file, the value lives in
7
- * the credential store, and the "in use" seat is the profile's `apiKeyEnv`
8
- * field. All six operations therefore ride ONE same-origin route
5
+ * which models each may serve, and whether the credential store actually holds a
6
+ * value — is host state: the list is the plugin's own index file, the values live
7
+ * in the credential store, and each key's model scope is the `models` field on
8
+ * its index entry. All operations therefore ride ONE same-origin route
9
9
  * ({@link KEY_INDEX_PATH}), and the browser half only ever handles references,
10
- * labels, booleans and masked previews. It never sees a secret it did not
11
- * explicitly ask for: a key value is fetched one reference at a time through the
12
- * existing reveal route, on a click, and is never cached.
10
+ * labels, model ids, booleans and masked previews. It never sees a secret it did
11
+ * not explicitly ask for: a key value is fetched one reference at a time through
12
+ * the existing reveal route, on a click, and is never cached.
13
+ *
14
+ * Keys are listed in preference order, and every key that is switched ON is
15
+ * active: a model reachable through several keys goes to the first one that
16
+ * can serve it, a model only one key lists sticks to that key, and a failed
17
+ * request fails over to the next capable key. The per-key check on each row
18
+ * ({@link KeyRow.enabled}) switches a key into or out of that pool — off is a
19
+ * pause, not a deletion: the label, scope and protocol pin all stay.
20
+ * A key's model scope is chosen from the models the route already offers
21
+ * (never the provider's whole catalog), so no auto-discovery step is needed.
13
22
  *
14
23
  * Fails closed. A refused fence, a route that is not in the settings document,
15
24
  * a store that cannot be written, a network error — each answers `{ ok: false }`
@@ -18,13 +27,19 @@
18
27
  *
19
28
  * @module dsh-model-think-level/client/key-manager-client
20
29
  */
30
+ import { type RouteProtocol } from '../key-refs.js';
21
31
  /** One key row as the host reports it. */
22
32
  export interface KeyRow {
23
33
  /** Credential reference holding the value. */
24
34
  readonly ref: string;
25
35
  /** Stored label; absent means "show the reference itself". */
26
36
  readonly alias?: string;
27
- /** Whether this is the reference the profile currently names. */
37
+ /**
38
+ * Whether this key is switched into the routing pool (its row's circular
39
+ * check is lit). A switched-off key keeps its label, scope and protocol pin
40
+ * but signs nothing; being on is the default, so an answer without the
41
+ * field reads as on.
42
+ */
28
43
  readonly enabled: boolean;
29
44
  /** Whether the credential store holds a value; null when it could not say. */
30
45
  readonly configured: boolean | null;
@@ -34,14 +49,60 @@ export interface KeyRow {
34
49
  * nothing rather than a made-up preview.
35
50
  */
36
51
  readonly masked?: string;
52
+ /**
53
+ * The model ids this key serves, picked from the route's own added models.
54
+ * Absent means "unscoped": a catch-all that serves whatever is left.
55
+ */
56
+ readonly models?: readonly string[];
57
+ /**
58
+ * The wire protocol this key pins. Absent means "follow the provider's own
59
+ * protocol". A pinned protocol makes this key speak a different API shape than
60
+ * the rest of the route (e.g. one Anthropic key on an OpenAI route).
61
+ */
62
+ readonly protocol?: RouteProtocol;
63
+ /**
64
+ * The key's cooldown, when it is resting after a failure: how long until it
65
+ * is a normal candidate again, and how many consecutive failures produced
66
+ * the period. Absent means the key is a normal candidate right now — the
67
+ * state the row's status dot paints as healthy.
68
+ */
69
+ readonly rest?: {
70
+ /** Milliseconds until the key is a normal candidate again (>= 0). */
71
+ readonly remainingMs: number;
72
+ /** Consecutive failures that produced this period. */
73
+ readonly failures: number;
74
+ };
75
+ }
76
+ /** A protocol family's base URL as the host stores it (empty means unset). */
77
+ export interface KeyEndpoints {
78
+ readonly openai: string | null;
79
+ readonly anthropic: string | null;
80
+ readonly gemini: string | null;
37
81
  }
38
82
  /** A successful answer: the whole list, in the host's order. */
39
83
  export interface KeyState {
40
84
  readonly ok: true;
41
85
  readonly route: string;
42
- /** The reference in use, or null when the provider names none. */
43
- readonly enabledRef: string | null;
44
86
  readonly entries: readonly KeyRow[];
87
+ /**
88
+ * The models this route actually offers — the universe a key's scope is
89
+ * chosen from (the models already added to the provider, never its whole
90
+ * catalog). Absent when the route declares none yet.
91
+ */
92
+ readonly models?: readonly string[];
93
+ /**
94
+ * The provider's catalogue merged across its keys: every model any key
95
+ * advertises, in first-advertised order. Absent when no key has been detected
96
+ * yet or the route itself declares no models.
97
+ */
98
+ readonly mergedModels?: readonly string[];
99
+ /**
100
+ * The part of {@link mergedModels} the route's own `models` does not declare
101
+ * yet — what the panel offers to add. Absent when there is nothing to add.
102
+ */
103
+ readonly unmergedModels?: readonly string[];
104
+ /** The provider's two base URLs, keyed by protocol family. */
105
+ readonly endpoints: KeyEndpoints;
45
106
  }
46
107
  /** A refused answer, carrying the host's own words when it sent any. */
47
108
  export interface KeyFailure {
@@ -56,14 +117,30 @@ export interface KeyManagerClient {
56
117
  list(route: string): Promise<KeyAnswer>;
57
118
  /** Store a new key and list it, returning the list it produced. */
58
119
  add(route: string, value: string, alias: string | undefined): Promise<KeyAnswer>;
59
- /** Make one listed reference the key in use. */
60
- enable(route: string, ref: string): Promise<KeyAnswer>;
61
120
  /** Relabel one listed reference (undefined clears the label). */
62
121
  rename(route: string, ref: string, alias: string | undefined): Promise<KeyAnswer>;
63
122
  /** Replace the stored secret of one listed reference, keeping its name. */
64
123
  replace(route: string, ref: string, value: string): Promise<KeyAnswer>;
65
124
  /** Forget one listed reference and its value. */
66
125
  remove(route: string, ref: string): Promise<KeyAnswer>;
126
+ /** Set one key's model scope (undefined clears it back to the catch-all). */
127
+ setScope(route: string, ref: string, models: readonly string[] | undefined): Promise<KeyAnswer>;
128
+ /** Switch one key into or out of the routing pool. */
129
+ setEnabled(route: string, ref: string, enabled: boolean): Promise<KeyAnswer>;
130
+ /** Pin one key's wire protocol (undefined clears it back to the route's own). */
131
+ setProtocol(route: string, ref: string, protocol: RouteProtocol | undefined): Promise<KeyAnswer>;
132
+ /**
133
+ * Set the protocol families' base URLs (undefined/empty clears that side).
134
+ * The whole set travels together — the host stores it as one replacement —
135
+ * so callers merge with what the store holds first.
136
+ */
137
+ setEndpoints(route: string, openai: string | undefined, anthropic: string | undefined, gemini: string | undefined): Promise<KeyAnswer>;
138
+ /**
139
+ * Append the models the keys advertise but the provider does not declare yet
140
+ * to the provider's own model list — one click, never an automatic write.
141
+ * Declared models are untouched.
142
+ */
143
+ applyMerged(route: string): Promise<KeyAnswer>;
67
144
  /** The stored value of one listed reference, or undefined. */
68
145
  reveal(route: string, ref: string): Promise<string | undefined>;
69
146
  }