aegiscode 6.7.6 → 6.7.7

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/src/engine.js CHANGED
@@ -37,12 +37,20 @@
37
37
  */
38
38
 
39
39
  const os = require('node:os');
40
- const { createLocalEngine, createSettingsStore } = require('./deps.js');
40
+ const { createLocalEngine, createSettingsStore, providers } = require('./deps.js');
41
41
  const { credentials } = require('./shared.js');
42
+ const custom = require('./custommodels.js');
42
43
  const VERSION = require('../package.json').version;
43
44
 
44
- /** The classes this host can actually run (see the docstring above). */
45
- const HOST_CLASSES = Object.freeze(['aegis', 'byok']);
45
+ /**
46
+ * The classes this host can actually run (see the docstring above).
47
+ * aegis — the pooled route (account key pays, pool picks the provider).
48
+ * byok — the BYOK relay (your provider key, AEGIS bills a handling fee).
49
+ * custom — the /model add catalog: your own base URL + key, called DIRECTLY
50
+ * through the desktop transport with the full tool loop, never
51
+ * touching aegiscloud.org (no margin, no fee).
52
+ */
53
+ const HOST_CLASSES = Object.freeze(['aegis', 'byok', 'custom']);
46
54
 
47
55
  /** The class a turn runs on when nothing else is said. */
48
56
  const DEFAULT_CLASS = 'aegis';
@@ -51,6 +59,7 @@ const DEFAULT_CLASS = 'aegis';
51
59
  const CLASS_LABELS = Object.freeze({
52
60
  aegis: 'AEGIS Cloud (pooled)',
53
61
  byok: 'Bring your own key (relayed, billed)',
62
+ custom: 'Custom endpoint (your key, direct)',
54
63
  });
55
64
 
56
65
  function unsupported(label) {
@@ -85,9 +94,14 @@ function createEngine({ client, getConfirmMode, getClass, settings: injectedSett
85
94
  listTags: async () => [],
86
95
  chat: unsupported('Ollama'),
87
96
  },
97
+ // The real direct-provider transport — no longer a throwing stub. The
98
+ // `custom` class (the /model add catalog) rewrites a turn to the
99
+ // 'openai-compat' / 'anthropic' class the desktop engine already drives
100
+ // through exactly these two functions, so a user's own endpoint gets the
101
+ // same tool loop the pooled class does.
88
102
  providers: {
89
- anthropicMessages: unsupported('a custom Anthropic-compatible endpoint'),
90
- openaiCompatible: unsupported('a custom OpenAI-compatible endpoint'),
103
+ anthropicMessages: providers.anthropicMessages,
104
+ openaiCompatible: providers.openaiCompatible,
91
105
  },
92
106
  env: {
93
107
  platform: process.platform,
@@ -105,21 +119,74 @@ function createEngine({ client, getConfirmMode, getClass, settings: injectedSett
105
119
  return HOST_CLASSES.includes(asked) ? asked : DEFAULT_CLASS;
106
120
  }
107
121
 
108
- return {
122
+ /**
123
+ * Rewrite a `custom`-class turn to the concrete transport class the desktop
124
+ * engine already drives. The catalog entry is the source of truth for the
125
+ * endpoint; its base URL + key are written just-in-time into the wire-class
126
+ * settings row (settings.get(cls).baseURL / rawKey(cls) is what the engine's
127
+ * dispatch reads), and the payload's class + model are set to that wire class
128
+ * and the entry's real model string. Throws a clear, in-process error rather
129
+ * than shipping `model: undefined` or an empty base URL upstream.
130
+ */
131
+ function prepareCustom(payload) {
132
+ const modelId = payload && payload.model;
133
+ const resolved = modelId ? custom.resolveCustom(modelId, settings) : null;
134
+ if (!resolved) {
135
+ const err = new Error(
136
+ modelId
137
+ ? `custom: no model "${modelId}" in the catalog — /model add <id> <name> <model> <baseURL>, or /models to list.`
138
+ : 'custom: no model pinned — pin one with /model <id> (/models lists your custom endpoints).'
139
+ );
140
+ err.status = 400;
141
+ throw err;
142
+ }
143
+ if (resolved.wire === 'anthropic' && !resolved.key) {
144
+ const err = new Error(
145
+ `custom: "${resolved.id}" is an Anthropic-style endpoint and needs a key — /model key ${resolved.id}.`
146
+ );
147
+ err.status = 400;
148
+ throw err;
149
+ }
150
+ // Just-in-time scratch write: the wire-class row is only ever this session's
151
+ // active custom endpoint, re-set on every custom turn, so two custom models
152
+ // sharing a wire never collide.
153
+ settings.set(resolved.wireClass, { baseURL: resolved.baseURL, key: resolved.key || null });
154
+ return { ...payload, class: resolved.wireClass, model: resolved.model };
155
+ }
156
+
157
+ function chat(payload, onDelta) {
158
+ const cls = currentClass();
159
+ if (cls === 'custom') return local.chat(prepareCustom(payload), onDelta);
109
160
  // `class` is stamped here rather than sent by every caller, so the class a
110
161
  // turn runs on has exactly one source of truth (the live session state) and
111
162
  // a caller cannot accidentally pin a stale one.
112
- chat: (payload, onDelta) => local.chat({ ...payload, class: currentClass() }, onDelta),
163
+ return local.chat({ ...payload, class: cls }, onDelta);
164
+ }
165
+
166
+ return {
167
+ chat,
113
168
  cancel: local.cancel,
114
169
  respondApproval: local.respondApproval,
115
170
  clearSessionApprovals: local.clearSessionApprovals,
116
- // The engine's own class table, narrowed to the two this host runs — so a
171
+ // The engine's own class table, narrowed to the ones this host runs — so a
117
172
  // UI built from listClasses() can never offer a class that would throw.
173
+ // `custom` is not in the desktop engine's own CLASSES (it is a CLI concept
174
+ // that rewrites to openai-compat/anthropic), so it is appended here.
118
175
  // Async, like the local engine's: listClasses() probes Ollama (a live
119
176
  // HTTP round-trip) before it can answer, so returning the raw promise made
120
177
  // `/class` throw "filter is not a function". The caller awaits it.
121
- listClasses: async () => (await local.listClasses()).filter((c) => HOST_CLASSES.includes(c.class)),
122
- listModels: (cls) => local.listModels(cls || currentClass()),
178
+ listClasses: async () => {
179
+ const base = (await local.listClasses()).filter((c) => HOST_CLASSES.includes(c.class));
180
+ const configured = custom.listCustomModels(settings).length > 0;
181
+ return [...base, { class: 'custom', label: CLASS_LABELS.custom, kind: 'custom', configured }];
182
+ },
183
+ // The `custom` class enumerates the catalog (the desktop engine has no such
184
+ // class, so delegating would throw); every other class delegates.
185
+ listModels: (cls) => {
186
+ const want = cls || currentClass();
187
+ if (want === 'custom') return { class: 'custom', models: custom.listCustomModels(settings) };
188
+ return local.listModels(want);
189
+ },
123
190
  classLabel: (cls) => CLASS_LABELS[cls] || CLASS_LABELS[DEFAULT_CLASS],
124
191
  getClass: currentClass,
125
192
  settings,
@@ -354,17 +354,27 @@ function createClient(opts = {}) {
354
354
  *
355
355
  * The caller's *AEGIS* key is sent alongside the provider key as X-AEGIS-Key.
356
356
  * Two credentials, two headers, on purpose: the provider key authenticates the
357
- * upstream call, the AEGIS key says whose bank pays the handling fee. With no
358
- * AEGIS key configured the header is omitted and the call stays anonymous:
359
- * the relay still serves it, and `_byok_identify_user` on the server resolves
360
- * no payer, so `charge_byok` logs the fee against user 0 as uncollected. The
361
- * turn is not refused — `AEGIS_BYOK_REQUIRE_BALANCE` (off by default) is the
362
- * server's own gate and the only place that decides this, deliberately, so
363
- * that pre-existing unauthenticated BYOK clients do not start getting 402s on
364
- * upgrade (aegis1 tests/test_byok_fee.py pins the anonymous path as allowed).
365
- * No client enforces it either: the desktop is one caller of a route that is
366
- * unauthenticated by design. What this header changes is attribution — sending
367
- * it is what makes the fee collectable from the account it belongs to.
357
+ * upstream call, the AEGIS key says whose bank pays the handling fee —
358
+ * `_byok_identify_user` on the server resolves the payer from it, and nothing
359
+ * else. With no AEGIS key the header is omitted and the call is anonymous;
360
+ * THIS function still sends it, because it is a transport and the route is
361
+ * unauthenticated by design (aegis1 tests/test_byok_fee.py pins the anonymous
362
+ * path as allowed) — refusing here would break every other caller of a
363
+ * published endpoint.
364
+ *
365
+ * Attribution is the whole game, because an unattributed turn is the one way
366
+ * a BYOK call escapes the fee: the relay serves it, `charge_byok` never runs
367
+ * (`app.py` charges only `if _byok_user_id:`), and the call is logged against
368
+ * user 0 as uncollected — a free ride. So the shipped HOSTS refuse a keyless
369
+ * BYOK send before the relay is reached: desktop/lib/local/engine.js raises
370
+ * 401, and the desktop app and the CLI share that engine (the CLI stages it
371
+ * as cli/vendor/desktop/lib/local/engine.js), so one gate covers both. Every
372
+ * turn is then attributable, and therefore billable: a funded balance is
373
+ * debited, an unfunded one is recorded as owed — `charge_byok` clamps the
374
+ * debit at zero and never refuses the call, so "cannot pay" and "must not be
375
+ * billed" stop being the same thing. `AEGIS_BYOK_REQUIRE_BALANCE` (off by
376
+ * default) remains the server's own, separate 402 gate and is not what makes
377
+ * the fee land.
368
378
  */
369
379
  async function byokChatCompletion({
370
380
  provider = 'openai',
@@ -956,18 +956,42 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
956
956
  // automatically by byokChatCompletion() as X-AEGIS-Key so the account
957
957
  // gets billed the handling fee; see client/aegis.js.
958
958
  const { provider, model: bareModel } = splitByokModel(opts.model);
959
- return aegis.byokChatCompletion({
960
- provider,
961
- model: bareModel,
962
- providerKey: opts.apiKey,
963
- prompt: opts.prompt,
964
- system: opts.system,
965
- messages: opts.messages,
966
- maxTokens: opts.maxTokens,
967
- stream: true,
968
- onStream: opts.onDelta,
969
- signal: opts.signal,
970
- });
959
+ try {
960
+ return await aegis.byokChatCompletion({
961
+ provider,
962
+ model: bareModel,
963
+ providerKey: opts.apiKey,
964
+ prompt: opts.prompt,
965
+ system: opts.system,
966
+ messages: opts.messages,
967
+ maxTokens: opts.maxTokens,
968
+ stream: true,
969
+ onStream: opts.onDelta,
970
+ signal: opts.signal,
971
+ });
972
+ } catch (e) {
973
+ // The relay's own balance gate (aegis1 `AEGIS_BYOK_REQUIRE_BALANCE`,
974
+ // app.py `byok_chat_completions`) answers 402 with a body aimed at an
975
+ // API consumer — "Insufficient balance. Top up to continue using
976
+ // BYOK." Shown raw in a chat transcript that reads as a crash rather
977
+ // than a bill, so the one thing the user has to DO is said here, in the
978
+ // hosts' own voice. The status is preserved so every existing caller
979
+ // (the CLI's error painter, the desktop turn guard) still sees a 402.
980
+ //
981
+ // The provider key is untouched by this: it is the caller's own and it
982
+ // is still valid. What ran out is the AEGIS balance the handling fee
983
+ // is billed against, which is the whole reason this lane has a fee.
984
+ if (e && e.status === 402) {
985
+ const err = new Error(
986
+ 'byok: this account has no AEGIS balance left, and the BYOK handling fee is ' +
987
+ 'billed there — your provider key is still valid, this is not a key problem. ' +
988
+ 'Top up the account, then try again (or /class aegis to use the pooled lane).'
989
+ );
990
+ err.status = 402;
991
+ throw err;
992
+ }
993
+ throw e;
994
+ }
971
995
  }
972
996
 
973
997
  const common = {
@@ -1111,13 +1135,24 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
1111
1135
  // …and the AEGIS account key is what makes the turn BILLABLE at all. The
1112
1136
  // relay authenticates on the provider key and resolves the payer
1113
1137
  // separately, from X-AEGIS-Key (app.py `_byok_identify_user`): with no
1114
- // account key the fee is logged against user 0 as uncollected, and with
1115
- // AEGIS_BYOK_REQUIRE_BALANCE off — the default — the turn is served
1116
- // anyway. That is the server's documented behaviour, so nothing is
1117
- // refused here: `listModels` reports the missing key as `needsAegisKey`
1118
- // and the settings UI says plainly where the fee lands, which is the part
1119
- // a client can do honestly. Enforcement lives in the relay's own gate,
1120
- // which every caller passes through.
1138
+ // account key the server can attribute the handling fee to no one, so it
1139
+ // is logged against user 0 as uncollected — an anonymous free ride the
1140
+ // fee exists to close. Require the account key here so EVERY BYOK turn is
1141
+ // attributed and every caller pays the handling fee: a funded balance is
1142
+ // debited immediately, an unfunded one records the fee as owed
1143
+ // (token_bank.charge_byok clamps to zero and never refuses), and nobody
1144
+ // is served free. Balance is the server's own concern, not this gate's —
1145
+ // with a balance or without one, the account still pays. `listModels`
1146
+ // still answers anonymously (the catalog is how a user finds out which
1147
+ // key to get), so this refuses only the send, and only until a key lands.
1148
+ if (cls === 'byok' && !aegis.apiKey) {
1149
+ const err = new Error(
1150
+ 'byok: connect your AEGIS account key first — the BYOK handling fee is billed there. ' +
1151
+ 'Add it in the Status card (or run /login), then try again.'
1152
+ );
1153
+ err.status = 401;
1154
+ throw err;
1155
+ }
1121
1156
 
1122
1157
  // Custom classes carry no enumerable model list (see listModels), so a
1123
1158
  // blank id here means the user never typed one. Fail loudly in-process