aegisone 1.0.21 → 1.0.23

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 (226) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +1 -1
  3. package/SOURCE-OFFER.txt +9 -0
  4. package/bin/aegis.js +1 -322
  5. package/package.json +4 -2
  6. package/scripts/bump.mjs +1 -211
  7. package/scripts/bundle.js +1 -129
  8. package/scripts/predist.mjs +1 -939
  9. package/src/child.js +1 -285
  10. package/src/dispatch.js +1 -269
  11. package/src/doctor.js +1 -639
  12. package/src/host.js +1 -111
  13. package/src/permissions.js +1 -369
  14. package/src/probes.js +1 -159
  15. package/src/release.js +1 -409
  16. package/vendor/cli/bin/aegiscode.js +2 -914
  17. package/vendor/cli/package.json +3 -2
  18. package/vendor/cli/scripts/check.mjs +1 -52
  19. package/vendor/cli/scripts/demo.mjs +1 -94
  20. package/vendor/cli/scripts/predist.mjs +1 -75
  21. package/vendor/cli/scripts/runtests.mjs +1 -56
  22. package/vendor/cli/scripts/seal-gate.mjs +1 -74
  23. package/vendor/cli/src/agents.js +1 -139
  24. package/vendor/cli/src/app.js +1 -2932
  25. package/vendor/cli/src/art.js +1 -289
  26. package/vendor/cli/src/autonomous.js +1 -515
  27. package/vendor/cli/src/caps.js +1 -879
  28. package/vendor/cli/src/chatflow.js +1 -3008
  29. package/vendor/cli/src/checkpoint.js +1 -90
  30. package/vendor/cli/src/clipboard.js +1 -424
  31. package/vendor/cli/src/cloudsync.js +1 -528
  32. package/vendor/cli/src/commands.js +1 -3987
  33. package/vendor/cli/src/config.js +1 -581
  34. package/vendor/cli/src/connect.js +1 -349
  35. package/vendor/cli/src/credentials.js +1 -33
  36. package/vendor/cli/src/deps.js +1 -232
  37. package/vendor/cli/src/devrun.js +1 -187
  38. package/vendor/cli/src/diff.js +1 -333
  39. package/vendor/cli/src/diffview.js +1 -296
  40. package/vendor/cli/src/engine.js +1 -297
  41. package/vendor/cli/src/entitlement.js +1 -355
  42. package/vendor/cli/src/events.js +1 -707
  43. package/vendor/cli/src/export.js +1 -67
  44. package/vendor/cli/src/fingerprint.js +1 -592
  45. package/vendor/cli/src/format.js +1 -259
  46. package/vendor/cli/src/fuzzy.js +1 -119
  47. package/vendor/cli/src/gate.js +1 -55
  48. package/vendor/cli/src/history.js +1 -401
  49. package/vendor/cli/src/init.js +1 -175
  50. package/vendor/cli/src/input.js +1 -139
  51. package/vendor/cli/src/keys.js +1 -183
  52. package/vendor/cli/src/limits.js +1 -190
  53. package/vendor/cli/src/markdown.js +1 -306
  54. package/vendor/cli/src/mcp.js +1 -694
  55. package/vendor/cli/src/models.js +1 -487
  56. package/vendor/cli/src/naming.js +1 -229
  57. package/vendor/cli/src/overlays.js +1 -461
  58. package/vendor/cli/src/panels.js +1 -1418
  59. package/vendor/cli/src/permissions.js +1 -290
  60. package/vendor/cli/src/ping.js +1 -226
  61. package/vendor/cli/src/presence.js +1 -238
  62. package/vendor/cli/src/provenance.js +2 -182
  63. package/vendor/cli/src/render.js +1 -515
  64. package/vendor/cli/src/screen.js +1 -518
  65. package/vendor/cli/src/screens.js +1 -896
  66. package/vendor/cli/src/secret.js +1 -59
  67. package/vendor/cli/src/securefs.js +1 -359
  68. package/vendor/cli/src/settings.js +1 -155
  69. package/vendor/cli/src/shared.js +1 -52
  70. package/vendor/cli/src/sharedpaths.js +1 -53
  71. package/vendor/cli/src/signup.js +1 -156
  72. package/vendor/cli/src/summarize.js +1 -95
  73. package/vendor/cli/src/sysdiag.js +1 -585
  74. package/vendor/cli/src/system.js +1 -40
  75. package/vendor/cli/src/theme.js +1 -570
  76. package/vendor/cli/src/title.js +1 -220
  77. package/vendor/cli/src/todos.js +1 -115
  78. package/vendor/cli/src/tokens.js +1 -263
  79. package/vendor/cli/src/topup.js +1 -210
  80. package/vendor/cli/src/update.js +1 -181
  81. package/vendor/cli/vendor/client/aegis.js +1 -2054
  82. package/vendor/cli/vendor/client/attest.js +1 -442
  83. package/vendor/cli/vendor/client/authorship-key.js +1 -410
  84. package/vendor/cli/vendor/client/auto-provision.js +1 -134
  85. package/vendor/cli/vendor/client/brain-catalog.js +1 -268
  86. package/vendor/cli/vendor/client/byok-catalog.js +1 -230
  87. package/vendor/cli/vendor/client/credentials.js +1 -494
  88. package/vendor/cli/vendor/client/device-fingerprint.js +1 -141
  89. package/vendor/cli/vendor/client/drive-connect.js +1 -85
  90. package/vendor/cli/vendor/client/drive-upload.js +1 -174
  91. package/vendor/cli/vendor/client/env-file.js +1 -1273
  92. package/vendor/cli/vendor/client/fingerprint.js +1 -1064
  93. package/vendor/cli/vendor/client/foreign-memory.js +1 -669
  94. package/vendor/cli/vendor/client/google-signin.js +1 -125
  95. package/vendor/cli/vendor/client/hooks.js +1 -430
  96. package/vendor/cli/vendor/client/model-discovery.js +1 -330
  97. package/vendor/cli/vendor/client/platform.js +1 -937
  98. package/vendor/cli/vendor/client/prefs.js +1 -351
  99. package/vendor/cli/vendor/client/presence-constants.js +1 -107
  100. package/vendor/cli/vendor/client/presence.js +1 -438
  101. package/vendor/cli/vendor/client/record-vault.js +3 -1055
  102. package/vendor/cli/vendor/client/session-store.js +1 -548
  103. package/vendor/cli/vendor/client/topup.js +1 -297
  104. package/vendor/cli/vendor/client/touch.js +1 -458
  105. package/vendor/cli/vendor/client/update.js +1 -704
  106. package/vendor/cli/vendor/client/user-models.js +1 -381
  107. package/vendor/cli/vendor/desktop/lib/local/agents.js +1 -105
  108. package/vendor/cli/vendor/desktop/lib/local/autonomous.js +1 -1045
  109. package/vendor/cli/vendor/desktop/lib/local/claude-pro.js +1 -993
  110. package/vendor/cli/vendor/desktop/lib/local/context.js +1 -95
  111. package/vendor/cli/vendor/desktop/lib/local/engine.js +1 -3228
  112. package/vendor/cli/vendor/desktop/lib/local/entitlement.js +1 -654
  113. package/vendor/cli/vendor/desktop/lib/local/git-scope.js +1 -228
  114. package/vendor/cli/vendor/desktop/lib/local/local.js +1 -2135
  115. package/vendor/cli/vendor/desktop/lib/local/prompt.js +1 -275
  116. package/vendor/cli/vendor/desktop/lib/local/queue.js +1 -560
  117. package/vendor/cli/vendor/desktop/lib/local/session-rounds.js +1 -208
  118. package/vendor/cli/vendor/desktop/lib/local/shell.js +1 -680
  119. package/vendor/cli/vendor/desktop/lib/local/tools.js +1 -1238
  120. package/vendor/cli/vendor/desktop/lib/local/turn-guard.js +1 -300
  121. package/vendor/cli/vendor/desktop/lib/local/worktree-lock.js +1 -235
  122. package/vendor/cli/vendor/desktop/lib/settings.js +1 -890
  123. package/vendor/cli/vendor/desktop/renderer/usage.js +1 -1239
  124. package/vendor/cli/vendor/hosts/install.js +1 -638
  125. package/vendor/cli/vendor/hosts/openai-shim.js +1 -515
  126. package/vendor/cli/vendor/hosts/spec.js +1 -164
  127. package/vendor/cli/vendor/hosts/targets.js +1 -346
  128. package/vendor/cli/vendor/mcp/server.js +1 -221
  129. package/vendor/cli/vendor/mcp/tools.js +1 -408
  130. package/vendor/cli/vendor/one/src/child.js +1 -285
  131. package/vendor/cli/vendor/one/src/permissions.js +1 -369
  132. package/vendor/desktop/bin/aegis.js +1 -32
  133. package/vendor/desktop/lib/account-key.js +1 -56
  134. package/vendor/desktop/lib/auto.js +1 -430
  135. package/vendor/desktop/lib/avatar/companion.js +1 -698
  136. package/vendor/desktop/lib/avatar/cosmetics.js +1 -532
  137. package/vendor/desktop/lib/avatar/events.js +1 -344
  138. package/vendor/desktop/lib/avatar/holder-turn.js +1 -350
  139. package/vendor/desktop/lib/avatar/holders.js +1 -612
  140. package/vendor/desktop/lib/avatar/identity.js +1 -641
  141. package/vendor/desktop/lib/avatar/level.js +1 -368
  142. package/vendor/desktop/lib/avatar/mirror.js +1 -524
  143. package/vendor/desktop/lib/avatar/persona.js +1 -388
  144. package/vendor/desktop/lib/avatar/profile.js +1 -977
  145. package/vendor/desktop/lib/avatar/register.js +1 -307
  146. package/vendor/desktop/lib/avatar/store.js +1 -472
  147. package/vendor/desktop/lib/avatar/tiers.js +1 -38
  148. package/vendor/desktop/lib/avatar/transfer.js +1 -415
  149. package/vendor/desktop/lib/avatar/turn.js +1 -216
  150. package/vendor/desktop/lib/avatar/voice.js +1 -328
  151. package/vendor/desktop/lib/avatar/xp.js +1 -404
  152. package/vendor/desktop/lib/deep-link.js +1 -120
  153. package/vendor/desktop/lib/fingerprint.js +2 -347
  154. package/vendor/desktop/lib/google-connect.js +1 -169
  155. package/vendor/desktop/lib/local/agents.js +1 -105
  156. package/vendor/desktop/lib/local/autonomous.js +1 -1045
  157. package/vendor/desktop/lib/local/claude-pro.js +1 -993
  158. package/vendor/desktop/lib/local/context.js +1 -95
  159. package/vendor/desktop/lib/local/engine.js +1 -3228
  160. package/vendor/desktop/lib/local/entitlement.js +1 -654
  161. package/vendor/desktop/lib/local/git-scope.js +1 -228
  162. package/vendor/desktop/lib/local/local.js +1 -2135
  163. package/vendor/desktop/lib/local/prompt.js +1 -275
  164. package/vendor/desktop/lib/local/queue.js +1 -560
  165. package/vendor/desktop/lib/local/session-rounds.js +1 -208
  166. package/vendor/desktop/lib/local/shell.js +1 -680
  167. package/vendor/desktop/lib/local/telemetry.js +1 -470
  168. package/vendor/desktop/lib/local/tools.js +1 -1238
  169. package/vendor/desktop/lib/local/turn-guard.js +1 -300
  170. package/vendor/desktop/lib/local/worktree-lock.js +1 -235
  171. package/vendor/desktop/lib/provider-key-mirror.js +1 -252
  172. package/vendor/desktop/lib/quick-launcher.js +1 -84
  173. package/vendor/desktop/lib/settings.js +1 -890
  174. package/vendor/desktop/lib/sync/memory-queue.js +1 -60
  175. package/vendor/desktop/lib/sync/persist-gate.js +1 -148
  176. package/vendor/desktop/lib/sync/sessions.js +1 -106
  177. package/vendor/desktop/lib/window-state.js +1 -66
  178. package/vendor/desktop/main.js +1 -4390
  179. package/vendor/desktop/package.json +3 -5
  180. package/vendor/desktop/preload.js +1 -628
  181. package/vendor/desktop/renderer/app.js +1 -6560
  182. package/vendor/desktop/renderer/avatar/assemble.js +1 -335
  183. package/vendor/desktop/renderer/avatar/avatar.js +1 -380
  184. package/vendor/desktop/renderer/avatar/cards.js +1 -141
  185. package/vendor/desktop/renderer/avatar/hud.js +1 -206
  186. package/vendor/desktop/renderer/avatar/machine.js +1 -213
  187. package/vendor/desktop/renderer/avatar/pane.js +1 -439
  188. package/vendor/desktop/renderer/avatar/parts.js +1 -437
  189. package/vendor/desktop/renderer/budget.js +1 -168
  190. package/vendor/desktop/renderer/companion.js +1 -223
  191. package/vendor/desktop/renderer/diff.js +1 -359
  192. package/vendor/desktop/renderer/diffview.js +1 -336
  193. package/vendor/desktop/renderer/fingerprint.js +3 -720
  194. package/vendor/desktop/renderer/markdown.js +1 -168
  195. package/vendor/desktop/renderer/provider-toolbar.js +1 -134
  196. package/vendor/desktop/renderer/providers.js +1 -259
  197. package/vendor/desktop/renderer/quick.js +1 -191
  198. package/vendor/desktop/renderer/stream-policy.js +1 -306
  199. package/vendor/desktop/renderer/transcript-view.js +1 -323
  200. package/vendor/desktop/renderer/usage.js +1 -1239
  201. package/vendor/desktop/vendor/aegis.js +1 -2054
  202. package/vendor/desktop/vendor/attest.js +1 -442
  203. package/vendor/desktop/vendor/authorship-key.js +1 -410
  204. package/vendor/desktop/vendor/auto-provision.js +1 -134
  205. package/vendor/desktop/vendor/brain-catalog.js +1 -268
  206. package/vendor/desktop/vendor/byok-catalog.js +1 -230
  207. package/vendor/desktop/vendor/credentials.js +1 -494
  208. package/vendor/desktop/vendor/device-fingerprint.js +1 -141
  209. package/vendor/desktop/vendor/drive-connect.js +1 -85
  210. package/vendor/desktop/vendor/drive-upload.js +1 -174
  211. package/vendor/desktop/vendor/env-file.js +1 -1273
  212. package/vendor/desktop/vendor/fingerprint.js +1 -1064
  213. package/vendor/desktop/vendor/foreign-memory.js +1 -669
  214. package/vendor/desktop/vendor/google-signin.js +1 -125
  215. package/vendor/desktop/vendor/hooks.js +1 -430
  216. package/vendor/desktop/vendor/model-discovery.js +1 -330
  217. package/vendor/desktop/vendor/platform.js +1 -937
  218. package/vendor/desktop/vendor/prefs.js +1 -351
  219. package/vendor/desktop/vendor/presence-constants.js +1 -107
  220. package/vendor/desktop/vendor/presence.js +1 -438
  221. package/vendor/desktop/vendor/record-vault.js +3 -1055
  222. package/vendor/desktop/vendor/session-store.js +1 -548
  223. package/vendor/desktop/vendor/topup.js +1 -297
  224. package/vendor/desktop/vendor/touch.js +1 -458
  225. package/vendor/desktop/vendor/update.js +1 -704
  226. package/vendor/desktop/vendor/user-models.js +1 -381
@@ -1,332 +1,3 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-or-later
2
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
- };
3
+ "use strict";const{sanitizeModel:sanitizeModel}=require("./user-models.js"),DISCOVERY_TIMEOUT_MS=8e3;function bearer(e){return{authorization:`Bearer ${e}`}}const ENDPOINTS=Object.freeze({anthropic:Object.freeze({url:"https://api.anthropic.com/v1/models",headers:e=>({"x-api-key":e,"anthropic-version":"2023-06-01"}),note:"the vendor requires anthropic-version; the console key is sent as x-api-key"}),openai:Object.freeze({url:"https://api.openai.com/v1/models",headers:bearer,note:"OpenAI-compatible: { data: [ { id } ] }"}),deepseek:Object.freeze({url:"https://api.deepseek.com/models",headers:bearer,note:"OpenAI-compatible; /v1/models answers the same list"}),grok:Object.freeze({url:"https://api.x.ai/v1/models",headers:bearer,note:"xAI answers on /v1/models under the vendor id grok"}),gemini:Object.freeze({url:"https://generativelanguage.googleapis.com/v1beta/models",headers:()=>({}),query:e=>({key:e}),note:'the key travels in the query string; ids come back as "models/<id>"'}),groq:Object.freeze({url:"https://api.groq.com/openai/v1/models",headers:bearer,note:"OpenAI-compatible under /openai/v1"}),mistral:Object.freeze({url:"https://api.mistral.ai/v1/models",headers:bearer,note:"OpenAI-compatible"}),openrouter:Object.freeze({url:"https://openrouter.ai/api/v1/models",headers:bearer,note:'OpenAI-compatible; ids are prefixed "vendor/model"'}),together:Object.freeze({url:"https://api.together.xyz/v1/models",headers:bearer,note:"OpenAI-compatible"}),cerebras:Object.freeze({url:"https://api.cerebras.ai/v1/models",headers:bearer,note:"OpenAI-compatible under /v1"}),fireworks:Object.freeze({url:"https://api.fireworks.ai/inference/v1/models",headers:bearer,note:'OpenAI-compatible under /inference/v1; ids are "accounts/fireworks/models/<id>"'}),nvidia_nim:Object.freeze({url:"https://integrate.api.nvidia.com/v1/models",headers:bearer,note:'OpenAI-compatible; ids are "<vendor>/<model>"'}),sakana:Object.freeze({url:"https://api.sakana.ai/v1/models",headers:bearer,note:"OpenAI-compatible under /v1"})}),ALIASES=Object.freeze({"x.ai":"grok",xai:"grok",google:"gemini","google-ai":"gemini",googleai:"gemini","open-ai":"openai","deep-seek":"deepseek","openai-gpt4o-mini":"openai","anthropic-haiku":"anthropic"}),NON_CHAT_RE=/embed|whisper|\btts\b|dall-e|moderation|rerank|imagen|\bveo\b|\baqa\b|transcribe|audio|speech|synthia|guard-content/i;function endpointFor(e){const r=String(null==e?"":e).trim().toLowerCase();if(!r)return null;const o=ALIASES[r]||r,t=ENDPOINTS[o];return t?Object.freeze({id:o,...t}):null}function canDiscover(e){return Boolean(endpointFor(e))}function discoverableProviders(){return Object.keys(ENDPOINTS).sort()}function looksNonChat(e){return NON_CHAT_RE.test(String(e))}function idsFromPayload(e){const r=Array.isArray(e)?e:Array.isArray(e&&e.data)?e.data:Array.isArray(e&&e.models)?e.models:null;if(!r)return null;const o=[],t=[];for(const e of r){if(null==e)continue;const r="string"==typeof e?e:e.id||e.name||e.model;if("string"!=typeof r||!r.trim())continue;const n=r.trim().replace(/^models\//,""),i=e&&Array.isArray(e.supportedGenerationMethods)?e.supportedGenerationMethods:e&&Array.isArray(e.supported_actions)?e.supported_actions:null,s=i?i.includes("generateContent")||i.includes("generate"):null;let a;try{a=sanitizeModel(n)}catch{continue}!1===s||null===s&&looksNonChat(a)?t.push(a):o.push(a)}const n=e=>[...new Set(e)];return{ids:n(o),others:n(t).filter((e=>!o.includes(e)))}}async function discoverModels(e,r={}){const o=endpointFor(e),t=String(null==e?"":e).trim().toLowerCase(),n=String(null==r.key?"":r.key).trim();if(!o)return{ok:!1,provider:t,supported:!1,error:`${t||"that provider"} has no model-list endpoint this client knows`};if(!n)return{ok:!1,provider:o.id,supported:!0,error:`no ${o.id} key on this machine`};const i=r.fetchImpl||("function"==typeof fetch?fetch:null);if(!i)return{ok:!1,provider:o.id,supported:!0,error:"no fetch implementation available"};const s=Number.isFinite(r.timeoutMs)?r.timeoutMs:8e3,a="function"==typeof AbortController?new AbortController:null,d=a?setTimeout((()=>a.abort()),s):null,l=()=>a&&a.abort();a&&r.signal&&(r.signal.aborted?l():"function"==typeof r.signal.addEventListener&&r.signal.addEventListener("abort",l,{once:!0}));try{const r=new URL(o.url);if("function"==typeof o.query)for(const[e,t]of Object.entries(o.query(n)||{}))r.searchParams.set(e,t);const t={accept:"application/json"};for(const[e,r]of Object.entries(o.headers(n)||{}))t["authorization"===e?"Authorization":e]=r;const s=await i(r.toString(),{method:"GET",headers:t,signal:a?a.signal:void 0}),d=s&&"number"==typeof s.status?s.status:0;if(!s||"boolean"==typeof s.ok&&!s.ok){const e=401===d||403===d?`the vendor refused the key (HTTP ${d})`:404===d?`the vendor has no model list at ${r.pathname} (HTTP 404)`:`the vendor answered HTTP ${d||"nothing"}`;return{ok:!1,provider:o.id,supported:!0,status:d,error:e}}let l;try{l=await s.json()}catch(e){return{ok:!1,provider:o.id,supported:!0,status:d,error:`the vendor's reply was not JSON (${e&&e.message||e})`}}const c=idsFromPayload(l);return c?{ok:!0,provider:o.id,endpoint:o.url,ids:c.ids,others:c.others,count:c.ids.length}:{ok:!1,provider:o.id,supported:!0,status:d,error:'the vendor answered in a shape this client does not know (no "data" or "models" list)'}}catch(e){const r=e&&("AbortError"===e.name||"ABORT_ERR"===e.code)||a&&a.signal.aborted;return{ok:!1,provider:o.id,supported:!0,error:r?`the vendor did not answer within ${s} ms`:`could not reach the vendor (${e&&e.message||e})`}}finally{d&&clearTimeout(d),a&&r.signal&&"function"==typeof r.signal.removeEventListener&&r.signal.removeEventListener("abort",l)}}module.exports={DISCOVERY_TIMEOUT_MS:8e3,ENDPOINTS:ENDPOINTS,ALIASES:ALIASES,NON_CHAT_RE:NON_CHAT_RE,endpointFor:endpointFor,canDiscover:canDiscover,discoverableProviders:discoverableProviders,looksNonChat:looksNonChat,idsFromPayload:idsFromPayload,discoverModels:discoverModels};