aegiscode 6.8.7 → 6.8.9

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,188 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * The pooled-brain entry, defined ONCE for every host.
5
+ *
6
+ * The platform's `/api/v1/models` payload lists the brain tier under five
7
+ * spellings (`nexus-brain` canonical; `aegis-brain`, `nexus-brain-smart`,
8
+ * `aegis-brain-smart`, `nexus-brain-neo` each sent as `hidden: true,
9
+ * alias_of: "nexus-brain"`). A host that showed all five painted five models
10
+ * where the server routes one, and a host that showed none (or a different one
11
+ * per release) left users comparing two clients that disagree about the same
12
+ * account.
13
+ *
14
+ * This module exists because that disagreement was real: the desktop collapsed
15
+ * the class to a single "Nexus" entry (desktop/lib/local/engine.js) while the
16
+ * CLI kept every distinct id, so the same account saw a different model list in
17
+ * each host. Two copies of the selection rule is the drift; one shared function
18
+ * is the fix, which is why both hosts require THIS file — the desktop from
19
+ * `desktop/lib/local/engine.js` (repo-relative `client/brain-catalog.js`, or
20
+ * `desktop/vendor/brain-catalog.js` in the packaged app) and the CLI from
21
+ * `cli/src/models.js` (through `src/sharedpaths.js`: repo-relative
22
+ * `client/brain-catalog.js`, or `vendor/client/brain-catalog.js` installed).
23
+ *
24
+ * The two hosts do NOT hand this module the same entry shape and must not have
25
+ * to: the desktop passes the relay's raw payload (`hidden`, `alias_of`), the
26
+ * CLI passes its own normalised entries (`hidden`, `aliasOf`). So the readers
27
+ * below accept either spelling — one rule, two shapes — instead of each host
28
+ * keeping a private copy of the rule to match its own shape, which is the drift
29
+ * this module exists to end.
30
+ *
31
+ * Pure: no I/O, no node builtins, no dependencies — safe to require from a
32
+ * renderer, a CLI start-up path, or an MCP host.
33
+ */
34
+
35
+ /** Every id the server uses for the pooled brain, canonical spelling first. */
36
+ const NEXUS_BRAIN_IDS = Object.freeze(['nexus-brain', 'aegis-brain']);
37
+
38
+ /** The name users see for the collapsed entry. */
39
+ const NEXUS_LABEL = 'Nexus';
40
+
41
+ /**
42
+ * The id this entry is an alias OF, or `null`. Reads the relay's `alias_of` and
43
+ * the CLI's normalised `aliasOf`, so callers may pass either shape.
44
+ */
45
+ function aliasOfEntry(m) {
46
+ if (!m || typeof m !== 'object') return null;
47
+ for (const value of [m.alias_of, m.aliasOf]) {
48
+ if (typeof value === 'string' && value.trim()) return value.trim();
49
+ }
50
+ return null;
51
+ }
52
+
53
+ /** Whether the entry is marked display-hidden, in either shape. */
54
+ function isHiddenEntry(m) {
55
+ return Boolean(m) && m.hidden === true;
56
+ }
57
+
58
+ /**
59
+ * Pick the one entry standing in for the pooled brain. The server serves
60
+ * `nexus-brain` as the canonical tier (`hidden: false`, no `alias_of`) and
61
+ * marks every other spelling — `aegis-brain` and the `-smart`/`-neo` tiers —
62
+ * as `hidden: true, alias_of: "nexus-brain"`. Prefer the canonical id so the
63
+ * request travels on the name the server owns; fall back to the alias, then to
64
+ * any tier whose `alias_of` names the brain, so a renamed or trimmed catalog
65
+ * still resolves to something selectable instead of silently emptying the
66
+ * dropdown (the previous fixed-id lookup returned [] if `aegis-brain` was ever
67
+ * retired, leaving the class with no model to choose).
68
+ *
69
+ * @param {Array<object>} models normalized or raw catalog entries.
70
+ * @returns {object|null}
71
+ */
72
+ function selectBrainEntry(models) {
73
+ const list = Array.isArray(models) ? models.filter(Boolean) : [];
74
+ return (
75
+ list.find((m) => NEXUS_BRAIN_IDS.includes(m.id) && !isHiddenEntry(m) && aliasOfEntry(m) === null)
76
+ || list.find((m) => NEXUS_BRAIN_IDS.includes(m.id))
77
+ || list.find((m) => NEXUS_BRAIN_IDS.includes(aliasOfEntry(m)))
78
+ || null
79
+ );
80
+ }
81
+
82
+ /**
83
+ * The Aegis-class catalog as a host must OFFER it: exactly one entry, the
84
+ * collapsed brain — never the raw provider list, and never the alias tiers.
85
+ *
86
+ * Offering this shape is not the same as accepting only this shape: the ids the
87
+ * server advertises still route (a pin on `deepseek` still runs), so callers
88
+ * keep the full catalog for validation and use this for what they show.
89
+ *
90
+ * Returns `[]` when the payload names no brain tier at all — which is the
91
+ * caller's signal that this is not a pooled-brain catalog (the BYOK listing is
92
+ * per-provider models and must be offered in full, not collapsed to nothing).
93
+ *
94
+ * @param {Array<object>} models the server's catalog (any shape).
95
+ * @returns {Array<object>} `[]` when the payload names no brain tier at all.
96
+ */
97
+ function filterAegisCatalog(models) {
98
+ const nexus = selectBrainEntry(models);
99
+ if (!nexus) return [];
100
+ // Drop the alias bookkeeping in BOTH spellings: this entry *is* the
101
+ // selection, so a renderer must never treat it as a hidden alias and filter
102
+ // it back out — which is exactly what happens if `aliasOf` survives here and
103
+ // the consumer re-runs its own "drop aliases" pass over the result.
104
+ const { hidden, alias_of, aliasOf, ...rest } = nexus;
105
+ return [{ ...rest, label: NEXUS_LABEL }];
106
+ }
107
+
108
+ /**
109
+ * The Aegis-class catalog as a host must OFFER it: the FULL list the server
110
+ * advertises — the pooled brain first, under its one name, then every other
111
+ * model the account can pin.
112
+ *
113
+ * `filterAegisCatalog` above collapses the class to the single brain row, and
114
+ * that was shipped: the desktop's dropdown and the terminal's alt+p picker both
115
+ * showed one entry, "Nexus", because collapsing was implemented to end a real
116
+ * disagreement (seven rows in one host, one in the other). Ending the
117
+ * disagreement by removing the choice was the wrong fix, and a user with a
118
+ * funded account could see no way to pin the deepseek or anthropic models the
119
+ * same catalogue advertises — the model was chosen for them, with no lever.
120
+ *
121
+ * So this is the rule both hosts now share: offer everything, collapse only the
122
+ * *aliases*.
123
+ *
124
+ * - The brain tier stays ONE row, labelled `NEXUS_LABEL`, first. The server
125
+ * lists it under five spellings (`nexus-brain` canonical, `aegis-brain` and
126
+ * the `-smart`/`-neo` tiers as `hidden: true, alias_of: "nexus-brain"`);
127
+ * those name one route, so they are folded into that one row rather than
128
+ * painted as five models — which is what `filterAegisCatalog` was for, and
129
+ * that half of it is kept.
130
+ * - Every other entry is offered as the server advertises it, INCLUDING
131
+ * entries the payload marks `hidden`, because "hidden" in this payload means
132
+ * "not the canonical spelling", not "do not offer".
133
+ * - An entry that is an alias OF another entry in the same payload is dropped
134
+ * only when its target is present — a pure duplicate row that would pin the
135
+ * id it points at, which is the `models.js` defect the CLI already guarded
136
+ * against. When the payload is nothing but aliases, they are all offered:
137
+ * an empty list is not a better answer than a list of aliases.
138
+ *
139
+ * Pure, like the rest of this file, and shape-tolerant (raw relay payload or
140
+ * the CLI's normalised entries) so both hosts can call it with what they hold.
141
+ * Returns `[]` for an empty payload; never invents an entry.
142
+ *
143
+ * @param {Array<object>} models the server's catalog (any shape).
144
+ * @returns {Array<object>} the entries to show, brain first (a new array).
145
+ */
146
+ function offerableCatalog(models) {
147
+ const list = Array.isArray(models) ? models.filter(Boolean) : [];
148
+ if (!list.length) return [];
149
+
150
+ const brain = selectBrainEntry(list);
151
+ const out = [];
152
+ const seen = new Set();
153
+
154
+ if (brain) {
155
+ // Same de-aliasing as filterAegisCatalog: this row IS the selection, so no
156
+ // renderer may filter it back out as a hidden alias.
157
+ const { hidden, alias_of, aliasOf, ...rest } = brain;
158
+ out.push({ ...rest, label: NEXUS_LABEL });
159
+ for (const id of NEXUS_BRAIN_IDS) seen.add(id);
160
+ if (typeof brain.id === 'string') seen.add(brain.id);
161
+ }
162
+
163
+ const ids = new Set(
164
+ list.map((m) => (typeof m.id === 'string' ? m.id.trim() : '')).filter(Boolean)
165
+ );
166
+ const aliasTargets = new Set(
167
+ list.map((m) => aliasOfEntry(m)).filter((t) => t && ids.has(t))
168
+ );
169
+
170
+ for (const m of list) {
171
+ const id = typeof m.id === 'string' ? m.id.trim() : '';
172
+ if (!id || seen.has(id)) continue;
173
+ // A row whose target is present is a duplicate pin, not a second model.
174
+ // Dropped here rather than per host, so the two hosts cannot disagree.
175
+ if (aliasOfEntry(m) && aliasTargets.has(aliasOfEntry(m))) continue;
176
+ seen.add(id);
177
+ out.push(m);
178
+ }
179
+ return out;
180
+ }
181
+
182
+ module.exports = {
183
+ NEXUS_BRAIN_IDS,
184
+ NEXUS_LABEL,
185
+ selectBrainEntry,
186
+ filterAegisCatalog,
187
+ offerableCatalog,
188
+ };
@@ -0,0 +1,144 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * BYOK model additions — the models a host OFFERS for a provider that the
5
+ * server's own catalog does not list.
6
+ *
7
+ * ── why a client-side addition is legitimate here, and only here ────────────
8
+ *
9
+ * For the POOLED class it would not be. `client/brain-catalog.js` exists
10
+ * precisely because a host may not invent an id: the pool can only address what
11
+ * it advertises, so an id a client made up would pin, look accepted, and 404 on
12
+ * the first turn. `/api/v1/models` is the whole list, and the rule is "offer
13
+ * what the server sends".
14
+ *
15
+ * BYOK is the opposite shape, and the server says so itself
16
+ * (services/byok_service.py, `byok_provider_catalog`): "the pooled route cannot
17
+ * address it, but BYOK relays whatever model the caller names, so it stays
18
+ * usable and stays listed". The relay is a stateless pass-through — the
19
+ * caller's own vendor key authenticates upstream, and AEGIS forwards the model
20
+ * string it was given. So for a provider the account holds a key for, an id the
21
+ * *vendor* serves is callable whether or not AEGIS's catalog happens to name
22
+ * it. That is the whole reason this file is allowed to exist.
23
+ *
24
+ * ── what was actually missing ──────────────────────────────────────────────
25
+ *
26
+ * The server derives each provider's `models` from MODEL_CATALOG, and for
27
+ * `deepseek` that catalog holds one descriptor (`default_model
28
+ * "deepseek-v4-flash"`, services/nexus_provider/catalog.py). Verified live
29
+ * 2026-09-18: `GET https://aegiscloud.org/api/v1/byok/providers` answers
30
+ * `deepseek` with `models: ["deepseek-v4-flash"]` and nothing else.
31
+ *
32
+ * So a user who brought a DeepSeek key saw exactly one pinnable model, and the
33
+ * 4.1 Flash — `deepseek-flash`, the spelling whose rate row, reasoning-token
34
+ * budget and effort rung this repo ALREADY carries (desktop/renderer/
35
+ * budget.js, desktop/lib/local/engine.js DEEPSEEK_REASONING_MODEL_RE, and the
36
+ * rate table in desktop/renderer/usage.js: "deepseek-flash, i.e. Flash 4.1")
37
+ * — was unreachable from the picker, with no hint that the key they had just
38
+ * pasted could call it.
39
+ *
40
+ * ── the rules this module keeps ────────────────────────────────────────────
41
+ *
42
+ * - ONE spelling per route. `deepseek-v4.1-flash` is the legacy alias the
43
+ * same regexes accept; listing both would paint two rows that pin one
44
+ * model, which is the "five spellings of one brain" defect the sibling
45
+ * brain-catalog module exists to end. The canonical 4.1 id is
46
+ * `deepseek-flash`, so that is the one added.
47
+ * - NEVER invent a provider. Only a provider the payload already lists is
48
+ * augmented. A provider row is a slot that accepts a vendor key; a row a
49
+ * client fabricated would invite a key for a slug the relay may not accept,
50
+ * and the failure would land on the user's first turn.
51
+ * - Idempotent and non-mutating. A payload that already carries the id comes
52
+ * back with the same provider objects untouched, so this is safe to apply
53
+ * twice (a caller that adds at fetch time and again before rendering cannot
54
+ * double-list) and safe to apply to a shared payload.
55
+ * - The server's order is preserved and additions are APPENDED, so the
56
+ * server's `default_model` keeps its first position — the default is what a
57
+ * bare provider id runs, and a client that reordered it would silently
58
+ * change which model a user gets for typing nothing.
59
+ *
60
+ * Pure: no I/O, no node builtins, no dependencies — requireable from a
61
+ * renderer, the CLI start-up path, the desktop main process, or an MCP host.
62
+ * Shared by the CLI and the desktop through one file for the same reason
63
+ * brain-catalog.js is: two copies is how those two hosts came to show one
64
+ * account two different model lists.
65
+ */
66
+
67
+ /**
68
+ * provider id → extra models to offer, in the order to append them.
69
+ *
70
+ * Frozen, and deliberately tiny: every entry here is a claim that the vendor
71
+ * serves an id the AEGIS catalog does not advertise. The durable fix for any of
72
+ * them is the server's MODEL_CATALOG (aegis1 services/nexus_provider/
73
+ * catalog.py) — this map is what keeps the two clients useful until that
74
+ * lands, and an entry must be deleted once the server lists the id itself
75
+ * (`withByokAdditions` then no longer reports it as an addition).
76
+ */
77
+ const BYOK_MODEL_ADDITIONS = Object.freeze({
78
+ // DeepSeek's 4.1 Flash. `deepseek-flash` is the spelling DeepSeek's own
79
+ // current API uses and the one this repo's rate table, token-budget and
80
+ // reasoning-model regexes all already key off; `deepseek-v4.1-flash` is the
81
+ // retired alias for the same route and is NOT listed separately.
82
+ deepseek: Object.freeze(['deepseek-flash']),
83
+ });
84
+
85
+ /** The extras to offer for one provider id (an empty array when there are none). */
86
+ function addedModelsFor(providerId) {
87
+ const id = typeof providerId === 'string' ? providerId.trim() : '';
88
+ const extra = id ? BYOK_MODEL_ADDITIONS[id] : null;
89
+ return Array.isArray(extra) ? extra.slice() : [];
90
+ }
91
+
92
+ /** The model ids an entry names, tolerating the server's two shapes. */
93
+ function modelIdsOf(provider) {
94
+ return (Array.isArray(provider && provider.models) ? provider.models : [])
95
+ .map((m) => (typeof m === 'string' ? m : m && m.id))
96
+ .map((m) => (typeof m === 'string' ? m.trim() : ''))
97
+ .filter(Boolean);
98
+ }
99
+
100
+ /**
101
+ * The server's BYOK provider catalog with the additions above applied.
102
+ *
103
+ * @param {Array<object>|object} providers the `providers` array from
104
+ * `GET /api/v1/byok/providers`, or the whole `{providers: [...]}` payload.
105
+ * @returns {Array<object>} a new array; provider objects are shared unless the
106
+ * row actually gained a model, and an empty/absent payload returns `[]`
107
+ * rather than a fabricated catalog.
108
+ */
109
+ function withByokAdditions(providers) {
110
+ const raw = Array.isArray(providers)
111
+ ? providers
112
+ : Array.isArray(providers && providers.providers)
113
+ ? providers.providers
114
+ : null;
115
+ if (!raw) return [];
116
+
117
+ const out = [];
118
+ for (const p of raw) {
119
+ if (!p || typeof p !== 'object') continue;
120
+ const id = typeof p.id === 'string' ? p.id.trim() : '';
121
+ const extra = id ? BYOK_MODEL_ADDITIONS[id] : null;
122
+ if (!Array.isArray(extra) || !extra.length) {
123
+ out.push(p);
124
+ continue;
125
+ }
126
+ const have = new Set(modelIdsOf(p));
127
+ const missing = extra.filter((m) => !have.has(m));
128
+ // Nothing to add: hand back the SAME object, so an already-complete payload
129
+ // is byte-identical after this pass and callers cannot tell it ran.
130
+ if (!missing.length) {
131
+ out.push(p);
132
+ continue;
133
+ }
134
+ const models = Array.isArray(p.models) ? p.models : [];
135
+ out.push({ ...p, models: [...models, ...missing] });
136
+ }
137
+ return out;
138
+ }
139
+
140
+ module.exports = {
141
+ BYOK_MODEL_ADDITIONS,
142
+ addedModelsFor,
143
+ withByokAdditions,
144
+ };