aegiscode 6.7.8 → 6.8.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.
- package/README.md +51 -97
- package/bin/aegiscode.js +24 -0
- package/package.json +1 -1
- package/scripts/predist.mjs +11 -16
- package/src/app.js +8 -63
- package/src/commands.js +24 -189
- package/src/config.js +0 -14
- package/src/deps.js +3 -16
- package/src/engine.js +14 -105
- package/src/screens.js +51 -0
- package/src/sharedpaths.js +4 -7
- package/vendor/client/aegis.js +45 -2
- package/vendor/client/env-file.js +182 -0
- package/vendor/desktop/lib/local/engine.js +225 -188
- package/vendor/desktop/lib/local/local.js +433 -0
- package/vendor/desktop/lib/local/tools.js +7 -56
- package/vendor/desktop/lib/settings.js +28 -31
- package/vendor/desktop/renderer/usage.js +4 -4
- package/src/custommodels.js +0 -258
- package/vendor/desktop/lib/local/endpoints.js +0 -228
- package/vendor/desktop/lib/local/providers.js +0 -679
package/src/custommodels.js
DELETED
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The named custom-model catalog behind the `custom` class.
|
|
5
|
-
*
|
|
6
|
-
* This is the aegiscodex-dev `/model add` concept ported into the CLI: a user
|
|
7
|
-
* registers an endpoint they bring themselves — their own base URL, their own
|
|
8
|
-
* model string, their own API key — and the turn is called DIRECTLY through the
|
|
9
|
-
* desktop transport (desktop/lib/local/providers.js openaiCompatible /
|
|
10
|
-
* anthropicMessages), the full agent loop and all, never through the pooled
|
|
11
|
-
* route and never through the BYOK relay. That lane is free of AEGIS margin and
|
|
12
|
-
* handling fee for exactly one reason: the request never touches
|
|
13
|
-
* aegiscloud.org — and that is only defensible for an endpoint ON THIS MACHINE.
|
|
14
|
-
*
|
|
15
|
-
* LOCAL ENDPOINTS ONLY (the policy this module enforces, see isLocalEndpoint).
|
|
16
|
-
* Every class that can bill does: `aegis` is the pooled route, `byok` is the
|
|
17
|
-
* stateless relay (services/pricing.price_byok_call → token_bank.charge_byok,
|
|
18
|
-
* the AEGIS handling fee). The direct transport bills nothing, so the ONLY
|
|
19
|
-
* unpaid-by-design usage it may carry is a local endpoint, where there is no
|
|
20
|
-
* vendor to pay in the first place. A remote base URL has nothing to bill
|
|
21
|
-
* against — the relay accepts a fixed catalog of provider ids
|
|
22
|
-
* (services/nexus_provider/catalog.py, key read from X-Provider-Key), so an
|
|
23
|
-
* arbitrary remote URL cannot be metered there either — and it is therefore
|
|
24
|
-
* REFUSED here, with a pointer at /class byok (billed) or a local address.
|
|
25
|
-
* Remote endpoints are not offered on this lane at all; there is deliberately
|
|
26
|
-
* no flag or env var that re-opens it, because such a flag would be a billing
|
|
27
|
-
* bypass.
|
|
28
|
-
*
|
|
29
|
-
* Split storage, on purpose:
|
|
30
|
-
* · metadata (id, name, model, baseURL, wire) → config.json `customModels`.
|
|
31
|
-
* None of it is secret, and config.json is where /model pins already live.
|
|
32
|
-
* · the API key → the settings store under `custom:<id>` (file mode 0600,
|
|
33
|
-
* the same store byok keys use). config.json is NOT 0600 and participates in
|
|
34
|
-
* cloud sync, so a provider key must never land there.
|
|
35
|
-
*
|
|
36
|
-
* `wire` selects the transport: 'anthropic' → anthropicMessages (x-api-key,
|
|
37
|
-
* the Messages API), 'openai' → openaiCompatible (Bearer, /chat/completions).
|
|
38
|
-
* It is auto-detected from the base URL host on add, and overridable.
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
const { loadConfig, updateConfig } = require('./config.js');
|
|
42
|
-
// The direct-dial policy, from the ONE implementation both hosts share
|
|
43
|
-
// (desktop/lib/local/endpoints.js). Resolved by path rather than required
|
|
44
|
-
// relatively because the CLI consumes desktop/lib/ in two layouts — beside cli/
|
|
45
|
-
// in the repo, and under cli/vendor/ in an installed package (src/deps.js
|
|
46
|
-
// documents both); sharedpaths.js is that resolution with no side effects, and
|
|
47
|
-
// it exists separately precisely so this module can reach the policy WITHOUT
|
|
48
|
-
// pulling in the engine (custommodels -> deps -> engine -> custommodels is a
|
|
49
|
-
// require cycle, and the cycle resolves to a half-built exports object).
|
|
50
|
-
//
|
|
51
|
-
// One implementation on purpose: a second copy is how the desktop would keep
|
|
52
|
-
// refusing remote endpoints while the terminal quietly served them — the exact
|
|
53
|
-
// drift that produces an unpaid turn.
|
|
54
|
-
const { hostOf, isLocalEndpoint, remoteRefusal } = require(
|
|
55
|
-
require('./sharedpaths.js').resolveShared(require('node:path').join('desktop', 'lib', 'local', 'endpoints.js'))
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
/** The settings-store row a custom model's key lives in. */
|
|
59
|
-
function customNamespace(id) {
|
|
60
|
-
return `custom:${id}`;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// `hostOf`, `isLocalEndpoint` and `remoteRefusal` come from the shared policy
|
|
64
|
-
// module above (see the require for why it is reached by path). The CLI's
|
|
65
|
-
// refusal text is that module's, plus the one thing only the CLI can name: the
|
|
66
|
-
// command that moves the user onto the billed lane. Previously this file held
|
|
67
|
-
// its own copy of all three, which is how the two hosts would have drifted.
|
|
68
|
-
const CLI_REMOTE_HINT =
|
|
69
|
-
'Remote providers are billed, so use /class byok with /byok-key <provider> ' +
|
|
70
|
-
'(the AEGIS relay charges the handling fee there), or point this entry at a local address.';
|
|
71
|
-
|
|
72
|
-
/** The refusal a remote custom endpoint gets here, in the CLI's own words. */
|
|
73
|
-
function customRefusal(baseURL) {
|
|
74
|
-
return remoteRefusal(baseURL, { subject: 'custom endpoints', hint: CLI_REMOTE_HINT });
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Which transport a base URL implies. Anthropic's Messages API is a different
|
|
79
|
-
* wire format (x-api-key, `/v1/messages`, a `system` top-level field) from the
|
|
80
|
-
* OpenAI-compatible majority, so it must be recognised rather than guessed at
|
|
81
|
-
* call time. Everything else — OpenAI, Groq, DeepSeek, together, openrouter,
|
|
82
|
-
* a local llama.cpp — speaks the OpenAI shape.
|
|
83
|
-
*/
|
|
84
|
-
function inferWire(baseURL) {
|
|
85
|
-
const host = hostOf(baseURL);
|
|
86
|
-
return host && /(^|\.)anthropic\.com$/.test(host) ? 'anthropic' : 'openai';
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const VALID_WIRES = Object.freeze(['openai', 'anthropic']);
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Validate and normalise a would-be catalog entry. Returns `{ entry }` on
|
|
93
|
-
* success or `{ error }` with a one-line reason — never throws, so the command
|
|
94
|
-
* layer can show the reason and keep the session alive.
|
|
95
|
-
*
|
|
96
|
-
* The id rules are load-bearing, not cosmetic: a `:` would collide with the
|
|
97
|
-
* `provider:model` shape byok parses, and a whitespace id cannot be typed back
|
|
98
|
-
* to `/model <id>`. A base URL is required and must be http(s) — the transport
|
|
99
|
-
* POSTs to `${baseURL}/…`, so a bare host or a typo becomes an unroutable call
|
|
100
|
-
* later instead of a clear refusal now.
|
|
101
|
-
*
|
|
102
|
-
* The last gate is the policy one: a base URL that is not local to this machine
|
|
103
|
-
* is refused (isLocalEndpoint). A custom entry is the only thing that reaches
|
|
104
|
-
* the direct transport, and the direct transport bills nothing, so a remote URL
|
|
105
|
-
* here would be unpaid non-local usage. The refusal names /class byok, which is
|
|
106
|
-
* billed, and a local address, which is free.
|
|
107
|
-
*/
|
|
108
|
-
function normalizeEntry({ id, name, model, baseURL, wire } = {}) {
|
|
109
|
-
const cleanId = String(id == null ? '' : id).trim();
|
|
110
|
-
if (!cleanId) return { error: 'an id is required — /model add <id> <name> <model> <baseURL>' };
|
|
111
|
-
if (/[\s:]/.test(cleanId)) return { error: `invalid id "${cleanId}" — no spaces or ":" (":" is the byok separator)` };
|
|
112
|
-
|
|
113
|
-
const cleanModel = String(model == null ? '' : model).trim();
|
|
114
|
-
if (!cleanModel) return { error: 'a model string is required — the id the provider expects on the wire' };
|
|
115
|
-
|
|
116
|
-
const cleanBase = String(baseURL == null ? '' : baseURL).trim();
|
|
117
|
-
if (!/^https?:\/\//i.test(cleanBase)) return { error: `base URL must start with http(s):// — got ${JSON.stringify(cleanBase)}` };
|
|
118
|
-
if (!hostOf(cleanBase)) return { error: `base URL does not parse — got ${JSON.stringify(cleanBase)}` };
|
|
119
|
-
|
|
120
|
-
let cleanWire = String(wire == null ? '' : wire).trim().toLowerCase();
|
|
121
|
-
if (cleanWire && !VALID_WIRES.includes(cleanWire)) {
|
|
122
|
-
return { error: `wire must be one of ${VALID_WIRES.join(', ')} — got ${JSON.stringify(cleanWire)}` };
|
|
123
|
-
}
|
|
124
|
-
if (!cleanWire) cleanWire = inferWire(cleanBase);
|
|
125
|
-
|
|
126
|
-
if (!isLocalEndpoint(cleanBase)) return { error: customRefusal(cleanBase) };
|
|
127
|
-
|
|
128
|
-
const cleanName = String(name == null ? '' : name).trim() || cleanId;
|
|
129
|
-
return { entry: { id: cleanId, name: cleanName, model: cleanModel, baseURL: cleanBase, wire: cleanWire } };
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** The stored catalog (metadata only), always an array. */
|
|
133
|
-
function rawCatalog() {
|
|
134
|
-
const list = loadConfig().customModels;
|
|
135
|
-
return Array.isArray(list) ? list.filter((e) => e && typeof e.id === 'string') : [];
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/** One entry's metadata by id, or null. */
|
|
139
|
-
function getCustom(id) {
|
|
140
|
-
const want = String(id == null ? '' : id).trim();
|
|
141
|
-
return rawCatalog().find((e) => e.id === want) || null;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* The catalog as pickable model rows ({ id, label, note, configured, local }) —
|
|
146
|
-
* the shape /models, the picker and the engine share. `configured` reflects
|
|
147
|
-
* whether a key is saved for the row, so the list can say which entries can
|
|
148
|
-
* actually run.
|
|
149
|
-
*
|
|
150
|
-
* `local` is recomputed from the base URL on every read rather than read off
|
|
151
|
-
* the stored row: a config.json written before this lane closed can still hold
|
|
152
|
-
* a remote entry, and it must classify as remote (and be refused by the
|
|
153
|
-
* dispatch gate) rather than inherit a stale "local: true" flag.
|
|
154
|
-
*/
|
|
155
|
-
function listCustomModels(settings) {
|
|
156
|
-
return rawCatalog().map((e) => {
|
|
157
|
-
const local = isLocalEndpoint(e.baseURL);
|
|
158
|
-
return {
|
|
159
|
-
id: e.id,
|
|
160
|
-
label: e.name || e.id,
|
|
161
|
-
note: `${e.model} · ${hostOf(e.baseURL) || e.baseURL} · ${e.wire}${local ? '' : ' · REMOTE — refused on this lane, use /class byok'}`,
|
|
162
|
-
configured: hasKey(settings, e.id),
|
|
163
|
-
wire: e.wire,
|
|
164
|
-
local,
|
|
165
|
-
};
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/** Whether a key is stored for this custom id. */
|
|
170
|
-
function hasKey(settings, id) {
|
|
171
|
-
if (!settings || typeof settings.rawKey !== 'function') return false;
|
|
172
|
-
return Boolean(settings.rawKey(customNamespace(id)));
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Add or replace a catalog entry, and store its key (when supplied) in the
|
|
177
|
-
* 0600 settings row. Returns `{ entry }` or `{ error }`. Adding an id that
|
|
178
|
-
* already exists REPLACES its metadata (and its key when a new one is given),
|
|
179
|
-
* so `/model add` doubles as an edit — re-running it to fix a typo'd base URL
|
|
180
|
-
* does not leave a second, dead row.
|
|
181
|
-
*/
|
|
182
|
-
function addCustom(fields, settings) {
|
|
183
|
-
const res = normalizeEntry(fields);
|
|
184
|
-
if (res.error) return res;
|
|
185
|
-
const { entry } = res;
|
|
186
|
-
const next = rawCatalog().filter((e) => e.id !== entry.id);
|
|
187
|
-
next.push(entry);
|
|
188
|
-
updateConfig({ customModels: next });
|
|
189
|
-
if (settings && typeof settings.set === 'function' && fields && fields.key != null && String(fields.key).trim()) {
|
|
190
|
-
settings.set(customNamespace(entry.id), { key: String(fields.key).trim() });
|
|
191
|
-
}
|
|
192
|
-
return { entry };
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/** Store (or clear) just the key for an existing custom id. */
|
|
196
|
-
function setCustomKey(id, key, settings) {
|
|
197
|
-
if (!settings || typeof settings.set !== 'function') return { error: 'no key store available' };
|
|
198
|
-
settings.set(customNamespace(id), { key: key ? String(key).trim() : null });
|
|
199
|
-
return { ok: true };
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Remove a catalog entry and forget its key. Returns whether anything was
|
|
204
|
-
* removed, so the command can say "removed" vs "there was no such entry".
|
|
205
|
-
*/
|
|
206
|
-
function removeCustom(id, settings) {
|
|
207
|
-
const want = String(id == null ? '' : id).trim();
|
|
208
|
-
const before = rawCatalog();
|
|
209
|
-
const after = before.filter((e) => e.id !== want);
|
|
210
|
-
const removed = after.length !== before.length;
|
|
211
|
-
if (removed) updateConfig({ customModels: after });
|
|
212
|
-
if (settings && typeof settings.remove === 'function') {
|
|
213
|
-
try { settings.remove(customNamespace(want)); } catch { /* nothing stored */ }
|
|
214
|
-
}
|
|
215
|
-
return { removed };
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Resolve a pinned custom id to everything the dispatch needs: the transport
|
|
220
|
-
* class it maps to ('anthropic' | 'openai-compat'), its base URL, its wire
|
|
221
|
-
* model string, its key, and whether the endpoint is local. Null when the id is
|
|
222
|
-
* not in the catalog. The key is read live from the settings store, never
|
|
223
|
-
* cached, so a key saved after the engine was constructed is picked up on the
|
|
224
|
-
* very next turn. `local` is likewise computed live from the base URL, so the
|
|
225
|
-
* engine's gate cannot be fooled by a row that was stored under the old,
|
|
226
|
-
* open-to-remote policy.
|
|
227
|
-
*/
|
|
228
|
-
function resolveCustom(id, settings) {
|
|
229
|
-
const entry = getCustom(id);
|
|
230
|
-
if (!entry) return null;
|
|
231
|
-
const key = settings && typeof settings.rawKey === 'function' ? settings.rawKey(customNamespace(entry.id)) : null;
|
|
232
|
-
return {
|
|
233
|
-
wireClass: entry.wire === 'anthropic' ? 'anthropic' : 'openai-compat',
|
|
234
|
-
baseURL: entry.baseURL,
|
|
235
|
-
model: entry.model,
|
|
236
|
-
key: key || null,
|
|
237
|
-
wire: entry.wire,
|
|
238
|
-
id: entry.id,
|
|
239
|
-
local: isLocalEndpoint(entry.baseURL),
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
module.exports = {
|
|
244
|
-
customNamespace,
|
|
245
|
-
inferWire,
|
|
246
|
-
isLocalEndpoint,
|
|
247
|
-
remoteRefusal,
|
|
248
|
-
customRefusal,
|
|
249
|
-
normalizeEntry,
|
|
250
|
-
getCustom,
|
|
251
|
-
listCustomModels,
|
|
252
|
-
hasKey,
|
|
253
|
-
addCustom,
|
|
254
|
-
setCustomKey,
|
|
255
|
-
removeCustom,
|
|
256
|
-
resolveCustom,
|
|
257
|
-
VALID_WIRES,
|
|
258
|
-
};
|
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The direct-dial policy: which base URLs may reach a provider transport, and
|
|
5
|
-
* why that is the only free lane left.
|
|
6
|
-
*
|
|
7
|
-
* Both hosts that can talk to a user-supplied endpoint — the desktop app
|
|
8
|
-
* (settings pane → 'openai-compat' / 'anthropic' classes) and the CLI
|
|
9
|
-
* (src/engine.js prepareCustom, the `/model add` catalog) — share THIS file
|
|
10
|
-
* rather than a copy each. The CLI vendors `desktop/lib/local/` wholesale and
|
|
11
|
-
* resolves this module through src/sharedpaths.js, so the two hosts cannot
|
|
12
|
-
* drift apart about what "local" means or which error a remote URL gets. A
|
|
13
|
-
* second implementation is how one host would keep billing correctly while the
|
|
14
|
-
* other quietly served unpaid traffic.
|
|
15
|
-
*
|
|
16
|
-
* The rule: every lane that can bill does. `aegis` is the pooled route (the
|
|
17
|
-
* account key is attached and the pool takes its margin); `byok` is the relay
|
|
18
|
-
* (`services/pricing.price_byok_call` → `token_bank.charge_byok`, the AEGIS
|
|
19
|
-
* handling fee, charged against the account resolved from `X-AEGIS-Key`). The
|
|
20
|
-
* provider transports in providers.js bill NOTHING — they are a direct dial to
|
|
21
|
-
* whoever owns the URL — so the only usage they may carry is an endpoint on
|
|
22
|
-
* this machine, where there is no vendor to pay in the first place.
|
|
23
|
-
*
|
|
24
|
-
* That is why a remote base URL is refused rather than metered: the relay
|
|
25
|
-
* accepts a fixed catalog of provider ids (`services/nexus_provider/catalog.py`,
|
|
26
|
-
* upstream key read from `X-Provider-Key`), so an arbitrary remote URL has
|
|
27
|
-
* nothing to be billed against even if a client wanted to invoice it. Remote
|
|
28
|
-
* providers belong on BYOK, which bills. There is deliberately no flag or env
|
|
29
|
-
* var that re-opens the direct lane for a remote URL, because such a flag would
|
|
30
|
-
* be a billing bypass.
|
|
31
|
-
*
|
|
32
|
-
* Where the gate lives — the seams, not the transport:
|
|
33
|
-
* · desktop/lib/settings.js `set()` refuses to STORE a remote base URL, so
|
|
34
|
-
* the unusable configuration cannot be created.
|
|
35
|
-
* · desktop/lib/local/engine.js `chat()` refuses to DIAL one before any
|
|
36
|
-
* transport call (a row hand-edited into the
|
|
37
|
-
* settings file, or written before this policy
|
|
38
|
-
* existed, still cannot be used).
|
|
39
|
-
* · cli/src/custommodels.js refuses one at `/model add` and again at
|
|
40
|
-
* dispatch, for the same two reasons.
|
|
41
|
-
* providers.js itself stays policy-free: it is a wire-format function, and
|
|
42
|
-
* pinning the policy there would make it unreachable for the tests that check
|
|
43
|
-
* URL building. Same split the BYOK balance gate uses — the relay owns the
|
|
44
|
-
* decision, the client refuses to send.
|
|
45
|
-
*/
|
|
46
|
-
|
|
47
|
-
/** Host of a URL, lower-cased, or '' if it does not parse. */
|
|
48
|
-
function hostOf(url) {
|
|
49
|
-
try {
|
|
50
|
-
return new URL(String(url)).host.toLowerCase();
|
|
51
|
-
} catch {
|
|
52
|
-
return '';
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Whether a base URL addresses an endpoint on this machine — the gate that
|
|
58
|
-
* decides whether the free direct lane is available at all.
|
|
59
|
-
*
|
|
60
|
-
* True for: loopback (localhost, `*.localhost`, 127.0.0.0/8, `::1`), RFC1918
|
|
61
|
-
* private ranges (10/8, 172.16/12, 192.168/16), IPv6 unique-local fc00::/7,
|
|
62
|
-
* link-local (169.254/16, fe80::/10), the reserved local suffixes `.local`,
|
|
63
|
-
* `.internal`, `.lan`, and a bare dotless hostname (`ollama`, `gpu-box` — it
|
|
64
|
-
* can only resolve through this machine's own resolver).
|
|
65
|
-
*
|
|
66
|
-
* FAIL CLOSED: anything that does not parse, carries no host, is a public
|
|
67
|
-
* address or name, or is merely ambiguous is NOT local. That direction is the
|
|
68
|
-
* whole point — a false "local" is an unpaid turn, a false "remote" is a
|
|
69
|
-
* refusal the user can fix by pointing at a local address or by moving to the
|
|
70
|
-
* billed lane, which is the outcome we want in a tie.
|
|
71
|
-
*/
|
|
72
|
-
function isLocalEndpoint(url) {
|
|
73
|
-
let host = '';
|
|
74
|
-
let scheme = '';
|
|
75
|
-
try {
|
|
76
|
-
const parsed = new URL(String(url == null ? '' : url));
|
|
77
|
-
host = parsed.hostname.toLowerCase();
|
|
78
|
-
scheme = parsed.protocol.toLowerCase();
|
|
79
|
-
} catch {
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
// Only a base URL a transport can actually dial. The transports in
|
|
83
|
-
// providers.js append '/chat/completions' and fetch it, so anything that is
|
|
84
|
-
// not http(s) — or has no scheme at all — is not an endpoint, and is refused
|
|
85
|
-
// by the same fail-closed rule as an unparseable value. Checked before the
|
|
86
|
-
// host, so `ftp://box.local` cannot ride the local suffix to a pass.
|
|
87
|
-
if (scheme !== 'http:' && scheme !== 'https:') return false;
|
|
88
|
-
if (!host) return false;
|
|
89
|
-
// WHATWG keeps the brackets on an IPv6 hostname; strip them so one spelling
|
|
90
|
-
// covers both `[::1]` and a bare `::1`.
|
|
91
|
-
if (host.startsWith('[') && host.endsWith(']')) host = host.slice(1, -1);
|
|
92
|
-
if (!host || /[\s/\\@]/.test(host)) return false;
|
|
93
|
-
|
|
94
|
-
// Loopback by name, plus the reserved local suffixes. `.local`/`.internal`/
|
|
95
|
-
// `.lan` are the mDNS / split-DNS names an on-box service answers to. The
|
|
96
|
-
// suffix must END the name: `example.local.evil.com` is somebody else's host.
|
|
97
|
-
if (host === 'localhost' || host.endsWith('.localhost')) return true;
|
|
98
|
-
if (/\.(local|internal|lan)$/.test(host)) return true;
|
|
99
|
-
|
|
100
|
-
// IPv6 literals.
|
|
101
|
-
if (host.includes(':')) {
|
|
102
|
-
if (host === '::1' || host === '0:0:0:0:0:0:0:1') return true; // loopback
|
|
103
|
-
if (/^f[cd][0-9a-f]{2}:/.test(host)) return true; // fc00::/7 unique-local
|
|
104
|
-
if (/^fe[89ab][0-9a-f]:/.test(host)) return true; // fe80::/10 link-local
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// IPv4 literals.
|
|
109
|
-
const quad = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(host);
|
|
110
|
-
if (quad) {
|
|
111
|
-
const o = quad.slice(1).map(Number);
|
|
112
|
-
if (o.some((n) => n > 255)) return false; // not an address, and not local
|
|
113
|
-
const [a, b] = o;
|
|
114
|
-
if (a === 127) return true; // 127.0.0.0/8 loopback
|
|
115
|
-
if (a === 10) return true; // 10.0.0.0/8
|
|
116
|
-
if (a === 172 && b >= 16 && b <= 31) return true; // 172.16.0.0/12
|
|
117
|
-
if (a === 192 && b === 168) return true; // 192.168.0.0/16
|
|
118
|
-
if (a === 169 && b === 254) return true; // 169.254.0.0/16 link-local
|
|
119
|
-
return false; // a real, routable address — remote, and billed
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// A bare, dotless hostname is local by convention (`ollama`, `llama-box`):
|
|
123
|
-
// it can only resolve through this machine's own resolver or /etc/hosts.
|
|
124
|
-
// Anything with a dot is a DNS name for somebody else's machine.
|
|
125
|
-
return !host.includes('.');
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** What IS allowed here, in one sentence, shared by both hosts' refusals. */
|
|
129
|
-
const ALLOWED_HINT =
|
|
130
|
-
'Allowed: localhost, 127.0.0.1, a private/LAN address (10.x, 172.16-31.x, 192.168.x), ' +
|
|
131
|
-
'*.local/.internal/.lan, or a dotless host like "ollama".';
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* The refusal a non-local base URL gets, in the words the user needs in order
|
|
135
|
-
* to act: it names the offending URL, the kind of address that IS allowed, and
|
|
136
|
-
* the lane that replaces this one. `hint` is the host-specific half —
|
|
137
|
-
* `/class byok` in the terminal, the Bring-your-own-key Settings row in the
|
|
138
|
-
* GUI — while the policy half is shared, so the two hosts cannot describe the
|
|
139
|
-
* same rule differently.
|
|
140
|
-
*/
|
|
141
|
-
function remoteRefusal(baseURL, { subject = 'custom endpoints', hint = '' } = {}) {
|
|
142
|
-
const where = String(baseURL == null ? '' : baseURL);
|
|
143
|
-
const tail = hint || 'Remote providers are billed, so use the lane that bills them, or point this at a local address.';
|
|
144
|
-
return `${subject} must be LOCAL — remote base URL ${JSON.stringify(where)} is not offered on this lane. ` +
|
|
145
|
-
`${ALLOWED_HINT} ${tail}`;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Throw the refusal (as a 400-class error every caller already paints) unless
|
|
150
|
-
* the base URL is local. The one-line form of the gate for the dispatch and
|
|
151
|
-
* storage seams, so neither has to remember the status code.
|
|
152
|
-
*
|
|
153
|
-
* An EMPTY base URL is refused here like any other non-local value, because
|
|
154
|
-
* there is no endpoint to dial. The storage seam does not use this function
|
|
155
|
-
* directly for that reason — see allowsDirectDialRow below, which treats "no
|
|
156
|
-
* URL configured" as "nothing to gate".
|
|
157
|
-
*/
|
|
158
|
-
function ensureLocalEndpoint(baseURL, opts) {
|
|
159
|
-
if (isLocalEndpoint(baseURL)) return true;
|
|
160
|
-
const err = new Error(remoteRefusal(baseURL, opts));
|
|
161
|
-
err.status = 400;
|
|
162
|
-
err.code = 'CUSTOM_ENDPOINT_NOT_LOCAL';
|
|
163
|
-
throw err;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* The settings-store rows whose base URL is dialled DIRECTLY by the transports
|
|
168
|
-
* in providers.js — i.e. the rows the gate above exists for.
|
|
169
|
-
*
|
|
170
|
-
* 'openai-compat', 'anthropic' the desktop's two custom endpoints (its
|
|
171
|
-
* Settings pane names them exactly so, and
|
|
172
|
-
* engine.js's CUSTOM_CLASSES reads the row
|
|
173
|
-
* under the class name).
|
|
174
|
-
* 'custom:*' the CLI's per-model namespace (src/
|
|
175
|
-
* custommodels.js). The CLI keeps the base URL
|
|
176
|
-
* in config.json `customModels` and stores only
|
|
177
|
-
* the KEY here, so this prefix is a second lock
|
|
178
|
-
* on a door that is already shut — deliberate:
|
|
179
|
-
* a future writer that puts a URL in this row
|
|
180
|
-
* must not silently reopen the lane.
|
|
181
|
-
*
|
|
182
|
-
* It matters that this is a NAMED list and not "every row". The same store
|
|
183
|
-
* holds the billed lanes' rows, and those legitimately carry remote values or
|
|
184
|
-
* nothing at all:
|
|
185
|
-
* · `byok:<provider>` — always remote by definition (the provider's own API,
|
|
186
|
-
* reached through AEGIS's relay, which is what bills it). The desktop's
|
|
187
|
-
* Settings pane saves those rows with baseURL '' (the relay owns the URL),
|
|
188
|
-
* and a blanket "must be local" rule would refuse even the empty string,
|
|
189
|
-
* breaking BYOK configuration entirely.
|
|
190
|
-
* · `aegis` — reserved namespace, never touched through this surface.
|
|
191
|
-
* So a gate keyed on the value alone is wrong; it is keyed on the ROW.
|
|
192
|
-
*/
|
|
193
|
-
const DIRECT_DIAL_ROWS = Object.freeze(['openai-compat', 'anthropic']);
|
|
194
|
-
const DIRECT_DIAL_PREFIXES = Object.freeze(['custom:']);
|
|
195
|
-
|
|
196
|
-
/** Whether a settings-store row's base URL is dialled directly (and so gated). */
|
|
197
|
-
function isDirectDialRow(provider) {
|
|
198
|
-
const p = String(provider == null ? '' : provider);
|
|
199
|
-
if (DIRECT_DIAL_ROWS.includes(p)) return true;
|
|
200
|
-
return DIRECT_DIAL_PREFIXES.some((pre) => p.startsWith(pre));
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Whether a row may STORE this base URL — the storage seam's question, which is
|
|
205
|
-
* not quite the dispatch seam's.
|
|
206
|
-
*
|
|
207
|
-
* A direct-dial row with no URL is fine to store: that is how a row is cleared,
|
|
208
|
-
* and how it looks before it is configured (`customStatus` reports it as
|
|
209
|
-
* unconfigured, so nothing dials it). What must never be stored is a NON-EMPTY
|
|
210
|
-
* remote URL, which would make the row look configured and usable.
|
|
211
|
-
*/
|
|
212
|
-
function allowsDirectDialRow(provider, baseURL) {
|
|
213
|
-
if (!isDirectDialRow(provider)) return true;
|
|
214
|
-
const url = String(baseURL == null ? '' : baseURL).trim();
|
|
215
|
-
if (!url) return true; // clearing the row, or a not-yet-configured one
|
|
216
|
-
return isLocalEndpoint(url);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
module.exports = {
|
|
220
|
-
hostOf,
|
|
221
|
-
isLocalEndpoint,
|
|
222
|
-
remoteRefusal,
|
|
223
|
-
ensureLocalEndpoint,
|
|
224
|
-
isDirectDialRow,
|
|
225
|
-
allowsDirectDialRow,
|
|
226
|
-
ALLOWED_HINT,
|
|
227
|
-
DIRECT_DIAL_ROWS,
|
|
228
|
-
};
|