aegiscode 6.8.37 → 7.0.1

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 (110) hide show
  1. package/README.md +39 -1
  2. package/bin/aegiscode.js +137 -0
  3. package/package.json +1 -1
  4. package/scripts/check.mjs +5 -0
  5. package/scripts/demo.mjs +5 -0
  6. package/scripts/predist.mjs +29 -2
  7. package/scripts/runtests.mjs +5 -0
  8. package/scripts/seal-gate.mjs +76 -0
  9. package/src/agents.js +5 -0
  10. package/src/app.js +59 -1
  11. package/src/art.js +5 -0
  12. package/src/autonomous.js +23 -0
  13. package/src/caps.js +5 -0
  14. package/src/chatflow.js +104 -31
  15. package/src/checkpoint.js +5 -0
  16. package/src/clipboard.js +5 -0
  17. package/src/cloudsync.js +5 -0
  18. package/src/commands.js +487 -4
  19. package/src/config.js +5 -0
  20. package/src/credentials.js +5 -0
  21. package/src/deps.js +5 -0
  22. package/src/devrun.js +5 -0
  23. package/src/diff.js +5 -0
  24. package/src/diffview.js +5 -0
  25. package/src/engine.js +5 -0
  26. package/src/entitlement.js +5 -0
  27. package/src/events.js +15 -0
  28. package/src/export.js +5 -0
  29. package/src/fingerprint.js +5 -0
  30. package/src/format.js +5 -0
  31. package/src/fuzzy.js +5 -0
  32. package/src/gate.js +12 -0
  33. package/src/history.js +5 -0
  34. package/src/init.js +5 -0
  35. package/src/input.js +5 -0
  36. package/src/keys.js +29 -2
  37. package/src/limits.js +192 -0
  38. package/src/markdown.js +5 -0
  39. package/src/mcp.js +13 -0
  40. package/src/models.js +5 -0
  41. package/src/naming.js +5 -0
  42. package/src/overlays.js +7 -2
  43. package/src/panels.js +5 -0
  44. package/src/permissions.js +56 -1
  45. package/src/ping.js +5 -0
  46. package/src/presence.js +5 -0
  47. package/src/provenance.js +238 -0
  48. package/src/provenance.lock.json +36 -0
  49. package/src/render.js +5 -0
  50. package/src/screen.js +5 -0
  51. package/src/screens.js +48 -37
  52. package/src/secret.js +5 -0
  53. package/src/securefs.js +5 -0
  54. package/src/settings.js +5 -0
  55. package/src/shared.js +5 -0
  56. package/src/sharedpaths.js +5 -0
  57. package/src/signup.js +5 -0
  58. package/src/summarize.js +5 -0
  59. package/src/sysdiag.js +582 -0
  60. package/src/system.js +5 -0
  61. package/src/theme.js +5 -0
  62. package/src/title.js +5 -0
  63. package/src/todos.js +5 -0
  64. package/src/tokens.js +5 -0
  65. package/src/topup.js +5 -0
  66. package/src/update.js +5 -0
  67. package/vendor/client/aegis.js +27 -0
  68. package/vendor/client/attest.js +5 -0
  69. package/vendor/client/authorship-key.js +5 -0
  70. package/vendor/client/auto-provision.js +5 -0
  71. package/vendor/client/brain-catalog.js +5 -0
  72. package/vendor/client/byok-catalog.js +48 -8
  73. package/vendor/client/credentials.js +5 -0
  74. package/vendor/client/device-fingerprint.js +5 -0
  75. package/vendor/client/env-file.js +5 -0
  76. package/vendor/client/fingerprint.js +5 -0
  77. package/vendor/client/foreign-memory.js +5 -0
  78. package/vendor/client/hooks.js +5 -0
  79. package/vendor/client/model-discovery.js +332 -0
  80. package/vendor/client/platform.js +5 -0
  81. package/vendor/client/prefs.js +5 -0
  82. package/vendor/client/presence-constants.js +5 -0
  83. package/vendor/client/presence.js +5 -0
  84. package/vendor/client/record-vault.js +5 -0
  85. package/vendor/client/session-store.js +5 -0
  86. package/vendor/client/topup.js +5 -0
  87. package/vendor/client/touch.js +5 -0
  88. package/vendor/client/update.js +5 -0
  89. package/vendor/client/user-models.js +383 -0
  90. package/vendor/desktop/lib/local/aegis-seal.mjs +5 -0
  91. package/vendor/desktop/lib/local/agents.js +5 -0
  92. package/vendor/desktop/lib/local/autonomous.js +86 -0
  93. package/vendor/desktop/lib/local/context.js +5 -0
  94. package/vendor/desktop/lib/local/covenant.js +5 -0
  95. package/vendor/desktop/lib/local/engine.js +463 -12
  96. package/vendor/desktop/lib/local/entitlement.js +5 -0
  97. package/vendor/desktop/lib/local/git-scope.js +5 -0
  98. package/vendor/desktop/lib/local/local.js +5 -0
  99. package/vendor/desktop/lib/local/prompt.js +5 -0
  100. package/vendor/desktop/lib/local/queue.js +5 -0
  101. package/vendor/desktop/lib/local/session-rounds.js +5 -0
  102. package/vendor/desktop/lib/local/shell.js +88 -9
  103. package/vendor/desktop/lib/local/tools.js +13 -6
  104. package/vendor/desktop/lib/local/turn-guard.js +5 -0
  105. package/vendor/desktop/lib/local/worktree-lock.js +5 -0
  106. package/vendor/desktop/lib/settings.js +5 -0
  107. package/vendor/desktop/renderer/usage.js +73 -1
  108. package/vendor/mcp/server.js +5 -0
  109. package/vendor/mcp/tools.js +5 -0
  110. package/vendor/tools/agent-gate.mjs +23 -0
@@ -1,4 +1,9 @@
1
1
  #!/usr/bin/env node
2
+ // SPDX-License-Identifier: AGPL-3.0-or-later
3
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
4
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
5
+ // Conveying a modified copy, or running one as a network service, owes the
6
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
2
7
  /**
3
8
  * AEGIS thin client — zero-dependency transport to aegiscloud.org.
4
9
  *
@@ -1440,7 +1445,29 @@ function createClient(opts = {}) {
1440
1445
  .sort((a, b) => a[0] - b[0])
1441
1446
  .map(([idx, c]) => ({ ...c, id: c.id || `call_${idx}` }));
1442
1447
 
1448
+ // The hidden chain-of-thought belongs on the RESULT, not only on the live
1449
+ // stream. `reasoningText` above is accumulated and forwarded to the caller's
1450
+ // `onReasoning`/`onStream` so the thinking can be shown while it happens —
1451
+ // but the resolved result carried only `content`, so every down-stream
1452
+ // diagnosis of "the model spent the whole budget thinking and never wrote a
1453
+ // word" read an empty reasoning channel on THIS lane and concluded there had
1454
+ // been no thinking at all.
1455
+ //
1456
+ // That is not a cosmetic loss. desktop/lib/local/engine.js decides its
1457
+ // reasoning-only recovery from exactly this field (`reasonedButSilent` →
1458
+ // `reasoningTextOf(res)`), so on byok the gate was dead code: DeepSeek
1459
+ // returns `finish_reason: 'stop'` with empty content and a full
1460
+ // `reasoning_content`, which matched neither `isTruncated` (the reason says
1461
+ // 'stop', not 'length') nor the reasoning retry (nothing to detect), and the
1462
+ // turn fell straight through to emptyTurnError — the "backend error: the
1463
+ // model returned no answer … max_tokens 16384" the user sees. Measured live
1464
+ // on byok/deepseek-v4-flash 2026-10-11.
1465
+ //
1466
+ // Kept out of `content` on purpose: deliberation is not an answer, and
1467
+ // folding it in would make a no-answer turn look answered to every caller's
1468
+ // empty-check (see the note at the `reasoningText` accumulation above).
1443
1469
  const message = { content: cleanText };
1470
+ if (reasoningText) message.reasoning_content = reasoningText;
1444
1471
  if (toolCalls.length) message.tool_calls = toolCalls;
1445
1472
  const result = {
1446
1473
  model: resultModel,
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -95,14 +100,49 @@ const BYOK_MODEL_ADDITIONS = Object.freeze({
95
100
  // catalog.py) pins exactly one default_model, `claude-sonnet-5`, and
96
101
  // `anthropic-haiku` pins `claude-haiku-4-5` as its own provider row — so
97
102
  // between them an Anthropic BYOK key could pin Sonnet or Haiku but never
98
- // Opus or Fable, the other two members of the current Claude 5 lineup the
99
- // vendor already serves. Both ids already have a priced rate row
100
- // (desktop/renderer/usage.js `opus`/`fable`, matched by family substring),
101
- // so the picker just had no way to offer them, not an unpriced gap. Listed
102
- // under the `anthropic` provider id because that is the row the server
103
- // advertises a configured Anthropic key against — the relay forwards
104
- // whichever of the two names the caller actually pins.
105
- anthropic: Object.freeze(['claude-opus-5', 'claude-fable-5']),
103
+ // Opus or Fable, the other two members of the Claude 5 lineup the vendor
104
+ // already serves. Listed under the `anthropic` provider id because that is
105
+ // the row the server advertises a configured Anthropic key against — the
106
+ // relay forwards whichever of these names the caller actually pins.
107
+ //
108
+ // Verified live 2026-10-11 against `GET https://api.anthropic.com/v1/models`
109
+ // (14 ids, `has_more: false`), which is where every id below is copied from
110
+ // rather than from memory:
111
+ //
112
+ // claude-haiku-5-5, claude-sonnet-5-5, claude-opus-5-5, claude-fable-5-1,
113
+ // claude-opus-5, claude-sonnet-5, claude-fable-5, claude-opus-4-8,
114
+ // claude-opus-4-7, claude-sonnet-4-6, claude-opus-4-6,
115
+ // claude-opus-4-5-20251101, claude-haiku-4-5-20251001,
116
+ // claude-sonnet-4-5-20250929
117
+ //
118
+ // The server's two defaults cover `claude-sonnet-5` and `claude-haiku-4-5`;
119
+ // the five below are the CURRENT members of the lineup it does not name —
120
+ // the 5.5 generation (Haiku, Sonnet, Opus) plus Fable 5.1, and the older
121
+ // Opus 5 / Fable 5 that were already listed. Every one has a priced rate row
122
+ // (desktop/renderer/usage.js, the Claude 5 block), so the picker was missing
123
+ // the ability to OFFER them, not a rate.
124
+ //
125
+ // What is deliberately NOT here, and why it matters more than what is:
126
+ // `claude-opus-4-1` (and its dated form `claude-opus-4-1-20250805`) is
127
+ // RETIRED upstream. Probed live 2026-10-11, `POST /v1/messages` with that
128
+ // model answers HTTP 404 `not_found_error` — the same probe answers
129
+ // `rate_limit_error` (i.e. the model resolved) for every id listed above,
130
+ // which is what makes 404 mean "no such model" rather than "no credit".
131
+ // The engine's own MODELS table still carries a legacy `claude-opus-4-1`
132
+ // row for saved configs, and that is fine — it is a local label that
133
+ // RESOLVES; a client-side catalog addition is a promise the picker will pin
134
+ // and the relay will forward, and this module's whole reason for existing is
135
+ // that a fabricated id "would pin, look accepted, and 404 on the first
136
+ // turn". So Opus 4.1 stays out of the offered list until Anthropic serves
137
+ // it again.
138
+ anthropic: Object.freeze([
139
+ 'claude-opus-5',
140
+ 'claude-fable-5',
141
+ 'claude-opus-5-5',
142
+ 'claude-sonnet-5-5',
143
+ 'claude-haiku-5-5',
144
+ 'claude-fable-5-1',
145
+ ]),
106
146
  // xAI's `grok` descriptor still pins `grok-4.5` as default_model — current
107
147
  // when that descriptor was written, but xAI has since shipped grok-4.6 and
108
148
  // grok-4.7 (flagship, Sept 2026) on the same `grok-4.5`-shaped id
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -0,0 +1,332 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
6
+ 'use strict';
7
+
8
+ /**
9
+ * Ask the VENDOR what it serves, so declaring a model stops depending on the
10
+ * user remembering an id.
11
+ *
12
+ * `client/user-models.js` is how a model the AEGIS catalog does not name
13
+ * becomes a pickable row, and `/model-add` is how a host writes one — but a
14
+ * path whose only input is "type the vendor's model id" moves the release-lag
15
+ * problem onto the user's memory instead of removing it. The cost is visible
16
+ * in this repo: `client/byok-catalog.js` carries a hand-copied, hand-verified
17
+ * list of Anthropic's ids ("Verified live 2026-10-11 against `GET
18
+ * https://api.anthropic.com/v1/models`") plus a paragraph on which id is
19
+ * retired — a chore the list endpoint answers in one request, and which goes
20
+ * stale the day the vendor ships.
21
+ *
22
+ * So this module asks. It is pure in the parts that matter:
23
+ * - no filesystem and no key store: the caller resolves the key and passes
24
+ * it in, exactly as the callers of `env-file.js` already do;
25
+ * - no network at load time and no module-scope `process` read;
26
+ * - `fetchImpl` is injectable, so every test runs against a stub;
27
+ * - it NEVER throws. A wrong key, a blocked network or a vendor changing
28
+ * its response shape returns `{ ok: false, error }` for the caller to
29
+ * report. Discovery sits on top of a path that already works by typing
30
+ * the id, so a failure here must not be able to take that path down.
31
+ *
32
+ * Only ids `client/user-models.js` would accept come back, because the store
33
+ * that is about to be written is the one with the rule — a discovery list that
34
+ * offered an id `/model-add` then refused would be worse than no list.
35
+ */
36
+
37
+ const { sanitizeModel } = require('./user-models.js');
38
+
39
+ /** How long a vendor gets to answer before discovery stands down. */
40
+ const DISCOVERY_TIMEOUT_MS = 8000;
41
+
42
+ /** `Authorization: Bearer <key>` — what most OpenAI-compatible vendors take. */
43
+ function bearer(key) {
44
+ return { authorization: `Bearer ${key}` };
45
+ }
46
+
47
+ /**
48
+ * The providers whose OWN list endpoint this module knows, by the id the BYOK
49
+ * relay uses. A provider that is not here is not an error: it is a provider
50
+ * whose list is either undocumented or has no stable endpoint, and the caller
51
+ * falls back to asking the user to type the id.
52
+ *
53
+ * Keys are lowercase provider ids; `aliases` maps the other spellings a user
54
+ * might type (`/model-discover xai` and `/model-discover grok` must hit the
55
+ * same vendor).
56
+ */
57
+ const ENDPOINTS = Object.freeze({
58
+ anthropic: Object.freeze({
59
+ url: 'https://api.anthropic.com/v1/models',
60
+ headers: (key) => ({ 'x-api-key': key, 'anthropic-version': '2023-06-01' }),
61
+ note: 'the vendor requires anthropic-version; the console key is sent as x-api-key',
62
+ }),
63
+ openai: Object.freeze({
64
+ url: 'https://api.openai.com/v1/models',
65
+ headers: bearer,
66
+ note: 'OpenAI-compatible: { data: [ { id } ] }',
67
+ }),
68
+ deepseek: Object.freeze({
69
+ url: 'https://api.deepseek.com/models',
70
+ headers: bearer,
71
+ note: 'OpenAI-compatible; /v1/models answers the same list',
72
+ }),
73
+ grok: Object.freeze({
74
+ url: 'https://api.x.ai/v1/models',
75
+ headers: bearer,
76
+ note: 'xAI answers on /v1/models under the vendor id grok',
77
+ }),
78
+ gemini: Object.freeze({
79
+ url: 'https://generativelanguage.googleapis.com/v1beta/models',
80
+ headers: () => ({}),
81
+ query: (key) => ({ key }),
82
+ note: 'the key travels in the query string; ids come back as "models/<id>"',
83
+ }),
84
+ groq: Object.freeze({
85
+ url: 'https://api.groq.com/openai/v1/models',
86
+ headers: bearer,
87
+ note: 'OpenAI-compatible under /openai/v1',
88
+ }),
89
+ mistral: Object.freeze({
90
+ url: 'https://api.mistral.ai/v1/models',
91
+ headers: bearer,
92
+ note: 'OpenAI-compatible',
93
+ }),
94
+ openrouter: Object.freeze({
95
+ url: 'https://openrouter.ai/api/v1/models',
96
+ headers: bearer,
97
+ note: 'OpenAI-compatible; ids are prefixed "vendor/model"',
98
+ }),
99
+ together: Object.freeze({
100
+ url: 'https://api.together.xyz/v1/models',
101
+ headers: bearer,
102
+ note: 'OpenAI-compatible',
103
+ }),
104
+ });
105
+
106
+ /** Other spellings a user types for a provider this module knows. */
107
+ const ALIASES = Object.freeze({
108
+ 'x.ai': 'grok',
109
+ xai: 'grok',
110
+ 'google': 'gemini',
111
+ 'google-ai': 'gemini',
112
+ 'googleai': 'gemini',
113
+ 'open-ai': 'openai',
114
+ 'deep-seek': 'deepseek',
115
+ });
116
+
117
+ /**
118
+ * The families that are NOT chat models, so a picker is not 400 lines of
119
+ * embedding rows. These are not dropped — they come back in `others`, because
120
+ * "the vendor lists 60 ids and the picker showed 12" is a fact the user is
121
+ * owed, not a silent filter.
122
+ */
123
+ const NON_CHAT_RE = /embed|whisper|\btts\b|dall-e|moderation|rerank|imagen|\bveo\b|\baqa\b|transcribe|audio|speech|synthia|guard-content/i;
124
+
125
+ /** The frozen endpoint for a provider id (or `null` when unknown). */
126
+ function endpointFor(provider) {
127
+ const raw = String(provider == null ? '' : provider).trim().toLowerCase();
128
+ if (!raw) return null;
129
+ const id = ALIASES[raw] || raw;
130
+ const entry = ENDPOINTS[id];
131
+ return entry ? Object.freeze({ id, ...entry }) : null;
132
+ }
133
+
134
+ /** True when discovery knows how to ask this provider at all. */
135
+ function canDiscover(provider) {
136
+ return Boolean(endpointFor(provider));
137
+ }
138
+
139
+ /** Every provider id discovery can ask, sorted — for a hint line. */
140
+ function discoverableProviders() {
141
+ return Object.keys(ENDPOINTS).sort();
142
+ }
143
+
144
+ /** True when a vendor id names something other than a chat model. */
145
+ function looksNonChat(id) {
146
+ return NON_CHAT_RE.test(String(id));
147
+ }
148
+
149
+ /**
150
+ * Pull candidate ids out of whatever shape the vendor answered with.
151
+ *
152
+ * Handled: `{ data: [ { id } ] }` (OpenAI, DeepSeek, xAI, Groq, Mistral,
153
+ * OpenRouter, Together), Anthropic's identical `{ data: [ { id } ] }`, Gemini's
154
+ * `{ models: [ { name: "models/<id>" } ] }`, and a bare array of strings or
155
+ * objects — vendors that answer unversioned occasionally answer either.
156
+ *
157
+ * Returns `null` when the payload has no list at all, which the caller reports
158
+ * as "the vendor answered in a shape this client does not know" rather than as
159
+ * an empty catalog (an empty list is a real, and very different, answer).
160
+ */
161
+ function idsFromPayload(payload) {
162
+ const rows = Array.isArray(payload)
163
+ ? payload
164
+ : Array.isArray(payload && payload.data)
165
+ ? payload.data
166
+ : Array.isArray(payload && payload.models)
167
+ ? payload.models
168
+ : null;
169
+ if (!rows) return null;
170
+ const ids = [];
171
+ const others = [];
172
+ for (const row of rows) {
173
+ if (row == null) continue;
174
+ const raw = typeof row === 'string' ? row : row.id || row.name || row.model;
175
+ if (typeof raw !== 'string' || !raw.trim()) continue;
176
+ // Gemini names rows "models/<id>"; the vendor id is the part after it.
177
+ const candidate = raw.trim().replace(/^models\//, '');
178
+ // A row that declares the methods it supports and does NOT list
179
+ // generateContent is not a chat model (Imagen says "predict"). That is a
180
+ // stronger signal than the name, so it is used before the name is.
181
+ const methods = row && Array.isArray(row.supportedGenerationMethods)
182
+ ? row.supportedGenerationMethods
183
+ : row && Array.isArray(row.supported_actions)
184
+ ? row.supported_actions
185
+ : null;
186
+ const chatty = methods ? methods.includes('generateContent') || methods.includes('generate') : null;
187
+ let id;
188
+ try {
189
+ id = sanitizeModel(candidate);
190
+ } catch {
191
+ continue; // not a string the declaration store would accept either
192
+ }
193
+ if (chatty === false || (chatty === null && looksNonChat(id))) others.push(id);
194
+ else ids.push(id);
195
+ }
196
+ const uniq = (list) => [...new Set(list)];
197
+ return { ids: uniq(ids), others: uniq(others).filter((id) => !ids.includes(id)) };
198
+ }
199
+
200
+ /**
201
+ * Ask a provider what it serves.
202
+ *
203
+ * `opts.key` is the user's own vendor key — required, and never read or logged
204
+ * here. `opts.fetchImpl` defaults to the global fetch. `opts.timeoutMs` bounds
205
+ * the request; the timer is always cleared, and the caller's `opts.signal` is
206
+ * honoured so a cancelled turn cancels the request.
207
+ *
208
+ * Resolves — never rejects — with one of:
209
+ * { ok: true, provider, endpoint, ids, others, count }
210
+ * { ok: false, provider, supported, error }
211
+ * `supported: false` means this module has no endpoint for that vendor, which
212
+ * is a different thing from a vendor that refused the key.
213
+ */
214
+ async function discoverModels(provider, opts = {}) {
215
+ const entry = endpointFor(provider);
216
+ const asked = String(provider == null ? '' : provider).trim().toLowerCase();
217
+ const key = String(opts.key == null ? '' : opts.key).trim();
218
+ if (!entry) {
219
+ return {
220
+ ok: false,
221
+ provider: asked,
222
+ supported: false,
223
+ error: `${asked || 'that provider'} has no model-list endpoint this client knows`,
224
+ };
225
+ }
226
+ if (!key) {
227
+ return { ok: false, provider: entry.id, supported: true, error: `no ${entry.id} key on this machine` };
228
+ }
229
+ const fetchImpl = opts.fetchImpl || (typeof fetch === 'function' ? fetch : null);
230
+ if (!fetchImpl) {
231
+ return { ok: false, provider: entry.id, supported: true, error: 'no fetch implementation available' };
232
+ }
233
+ const timeoutMs = Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : DISCOVERY_TIMEOUT_MS;
234
+ const controller = typeof AbortController === 'function' ? new AbortController() : null;
235
+ const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
236
+ // Deliberately NOT unref'd: the timer is the thing that must fire when a
237
+ // vendor goes quiet. An unref'd timer in a process whose only other pending
238
+ // work is the await would be collected before it fired, and the hang this
239
+ // timeout exists to prevent is exactly what that produces. It is cleared in
240
+ // the `finally` below, so it never outlives the request either way.
241
+ const onAbort = () => controller && controller.abort();
242
+ if (controller && opts.signal) {
243
+ if (opts.signal.aborted) onAbort();
244
+ else if (typeof opts.signal.addEventListener === 'function') opts.signal.addEventListener('abort', onAbort, { once: true });
245
+ }
246
+ try {
247
+ const url = new URL(entry.url);
248
+ if (typeof entry.query === 'function') {
249
+ for (const [k, v] of Object.entries(entry.query(key) || {})) url.searchParams.set(k, v);
250
+ }
251
+ const headers = { accept: 'application/json' };
252
+ for (const [k, v] of Object.entries(entry.headers(key) || {})) {
253
+ // Vendors that want the header spelled a particular way get it; the
254
+ // table stores the canonical lowercase name.
255
+ headers[k === 'authorization' ? 'Authorization' : k] = v;
256
+ }
257
+ const res = await fetchImpl(url.toString(), {
258
+ method: 'GET',
259
+ headers,
260
+ signal: controller ? controller.signal : undefined,
261
+ });
262
+ const status = res && typeof res.status === 'number' ? res.status : 0;
263
+ if (!res || (typeof res.ok === 'boolean' && !res.ok)) {
264
+ // 401/403 is the common case and deserves its own sentence: the fix is
265
+ // a different key, not a different provider.
266
+ const why = status === 401 || status === 403
267
+ ? `the vendor refused the key (HTTP ${status})`
268
+ : status === 404
269
+ ? `the vendor has no model list at ${url.pathname} (HTTP 404)`
270
+ : `the vendor answered HTTP ${status || 'nothing'}`;
271
+ return { ok: false, provider: entry.id, supported: true, status, error: why };
272
+ }
273
+ let payload;
274
+ try {
275
+ payload = await res.json();
276
+ } catch (err) {
277
+ return {
278
+ ok: false,
279
+ provider: entry.id,
280
+ supported: true,
281
+ status,
282
+ error: `the vendor's reply was not JSON (${(err && err.message) || err})`,
283
+ };
284
+ }
285
+ const parsed = idsFromPayload(payload);
286
+ if (!parsed) {
287
+ return {
288
+ ok: false,
289
+ provider: entry.id,
290
+ supported: true,
291
+ status,
292
+ error: `the vendor answered in a shape this client does not know (no "data" or "models" list)`,
293
+ };
294
+ }
295
+ return {
296
+ ok: true,
297
+ provider: entry.id,
298
+ endpoint: entry.url,
299
+ ids: parsed.ids,
300
+ others: parsed.others,
301
+ count: parsed.ids.length,
302
+ };
303
+ } catch (err) {
304
+ const aborted = (err && (err.name === 'AbortError' || err.code === 'ABORT_ERR')) || (controller && controller.signal.aborted);
305
+ return {
306
+ ok: false,
307
+ provider: entry.id,
308
+ supported: true,
309
+ error: aborted
310
+ ? `the vendor did not answer within ${timeoutMs} ms`
311
+ : `could not reach the vendor (${(err && err.message) || err})`,
312
+ };
313
+ } finally {
314
+ if (timer) clearTimeout(timer);
315
+ if (controller && opts.signal && typeof opts.signal.removeEventListener === 'function') {
316
+ opts.signal.removeEventListener('abort', onAbort);
317
+ }
318
+ }
319
+ }
320
+
321
+ module.exports = {
322
+ DISCOVERY_TIMEOUT_MS,
323
+ ENDPOINTS,
324
+ ALIASES,
325
+ NON_CHAT_RE,
326
+ endpointFor,
327
+ canDiscover,
328
+ discoverableProviders,
329
+ looksNonChat,
330
+ idsFromPayload,
331
+ discoverModels,
332
+ };
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  'use strict';
2
7
 
3
8
  /**
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
+ // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
+ // Conveying a modified copy, or running one as a network service, owes the
5
+ // corresponding source under this licence (§5/§13) — see LICENSING.md.
1
6
  /**
2
7
  * "There is a newer version" — the notice this CLI never had.
3
8
  *