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,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
  }
@@ -4,6 +4,11 @@ export declare const en: {
4
4
  reasoningEffort: string;
5
5
  thinkingMode: string;
6
6
  thinkingModeOff: string;
7
+ levelsLabel: string;
8
+ levelsHint: string;
9
+ stateDeclared: string;
10
+ stateUndeclared: string;
11
+ modalityText: string;
7
12
  autoAdapt: string;
8
13
  autoAdaptAll: string;
9
14
  autoAdaptWorking: string;
@@ -27,6 +32,9 @@ export declare const en: {
27
32
  referenceTitle: string;
28
33
  contextWindowLabel: string;
29
34
  maxTokensLabel: string;
35
+ capacityBlank: string;
36
+ capacityRule: string;
37
+ tokensUnit: string;
30
38
  inputModality: string;
31
39
  modalityImage: string;
32
40
  modalityInherit: string;
@@ -39,6 +47,8 @@ export declare const en: {
39
47
  renderFailed: string;
40
48
  wirePlaceholder: string;
41
49
  wireValue: string;
50
+ wireShow: string;
51
+ wireHide: string;
42
52
  reasoningDisabled: string;
43
53
  bareHint: string;
44
54
  defaultRiskDisabled: string;
@@ -67,6 +77,7 @@ export declare const en: {
67
77
  defaultEffortLabel: string;
68
78
  defaultEffortUnset: string;
69
79
  defaultEffortHint: string;
80
+ followMemory: string;
70
81
  clearDefaultEffort: string;
71
82
  modelSearchPlaceholder: string;
72
83
  modelSearchNoMatches: string;
@@ -83,7 +94,7 @@ export declare const en: {
83
94
  keysAdd: string;
84
95
  keysAddConfirm: string;
85
96
  keysEnable: string;
86
- keysEnabled: string;
97
+ keysDisable: string;
87
98
  keysMissing: string;
88
99
  keysEdit: string;
89
100
  keysRemove: string;
@@ -100,9 +111,44 @@ export declare const en: {
100
111
  keysRenamed: string;
101
112
  keysReplaced: string;
102
113
  keysRemoved: string;
103
- keysEnabledNote: string;
114
+ keysScope: string;
115
+ keysModels: string;
116
+ keysScopeAll: string;
117
+ keysScopeNone: string;
118
+ keysScopeCount: string;
119
+ keysScopeSaved: string;
120
+ keysScopeHint: string;
121
+ keysScopeEmpty: string;
122
+ keysMergedOffer: string;
123
+ keysMergedApply: string;
124
+ keysMergedApplied: string;
125
+ keysProtocolLabel: string;
126
+ keysProtocolCreate: string;
127
+ keysProtocolCreateHint: string;
128
+ keysProtocolDefault: string;
129
+ keysProtocolDefaultHint: string;
130
+ keysProtocolAnthropic: string;
131
+ keysProtocolAnthropicHint: string;
132
+ keysProtocolOpenAI: string;
133
+ keysProtocolOpenAIHint: string;
134
+ keysProtocolResponses: string;
135
+ keysProtocolResponsesHint: string;
136
+ keysProtocolGemini: string;
137
+ keysProtocolGeminiHint: string;
138
+ keysProtocolShortChat: string;
139
+ keysProtocolShortResponses: string;
140
+ keysProtocolShortAnthropic: string;
141
+ keysProtocolSaved: string;
142
+ keysEndpointOpenAI: string;
143
+ keysEndpointAnthropic: string;
144
+ keysEndpointGemini: string;
145
+ keysMoreEndpoints: string;
146
+ keysEndpointsCount: string;
147
+ keysEndpointInvalid: string;
104
148
  keysUnreachable: string;
105
149
  keysPanelFailed: string;
150
+ keysDraftHint: string;
151
+ keysLastEnabledOff: string;
106
152
  headersTitle: string;
107
153
  headersHint: string;
108
154
  headersEmpty: string;
@@ -117,10 +163,6 @@ export declare const en: {
117
163
  headersSaved: string;
118
164
  headersCleared: string;
119
165
  headersReadOnly: string;
120
- headersUserAgentTitle: string;
121
- headersUserAgentHint: string;
122
- headersUserAgentClear: string;
123
- headersUserAgentPlaceholder: string;
124
166
  headersConflictOrigin: string;
125
167
  headersConflictPatched: string;
126
168
  headersConflictSiblings: string;