aegiscode 6.7.5 → 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.
@@ -354,9 +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
- * allowed, just unbilled (see _byok_identify_user on the server).
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.
360
378
  */
361
379
  async function byokChatCompletion({
362
380
  provider = 'openai',
@@ -714,14 +714,27 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
714
714
  if (cls === 'byok') {
715
715
  // The server's catalog names every provider it accepts a key for, the
716
716
  // models each unlocks, and whether an AEGIS account key is even needed
717
- // to ask (it is not — see byokProviders' own docstring). Deliberately
718
- // NOT gated on aegis.apiKey the way the pooled class above is: BYOK's
719
- // whole point is a caller who brings their own credential, and the
720
- // catalog itself answers to an anonymous request.
717
+ // to ask (it is not — the catalog answers an anonymous request).
718
+ // Deliberately NOT gated on aegis.apiKey the way the pooled class above
719
+ // is: BYOK's whole point is a caller who brings their own credential, and
720
+ // hiding the catalog would hide the answer to "which key do I go and get"
721
+ // from exactly the person deciding whether to bother. Nothing here
722
+ // enforces billing either: the desktop is one client of a route that is
723
+ // unauthenticated by design, so refusing in this process would stop
724
+ // exactly one host out of many. The relay owns that decision, and reports
725
+ // it via `fee.require_balance` below.
721
726
  let providers = [];
727
+ let fee = null;
722
728
  try {
723
729
  const data = await aegis.byokProviders();
724
730
  providers = (data && data.providers) || [];
731
+ // The handling fee AEGIS adds on top of the caller's vendor bill. It is
732
+ // the server's own published rate (services/pricing.price_byok_call) and
733
+ // is passed through untouched — never re-derived here, because a client
734
+ // that hardcodes a fee is a client that can disagree with the ledger.
735
+ // Absent until the server publishes one, and the UI must then say
736
+ // nothing rather than show a guess.
737
+ fee = (data && data.fee) || null;
725
738
  } catch {
726
739
  providers = [];
727
740
  }
@@ -744,6 +757,8 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
744
757
  return {
745
758
  class: cls, models, providers,
746
759
  needsProviderKey: models.length > 0 && !models.some((m) => m.configured),
760
+ needsAegisKey: !aegis.apiKey,
761
+ fee,
747
762
  };
748
763
  }
749
764
  // Custom endpoints: the model id is the *user's* choice — a provider model
@@ -941,18 +956,42 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
941
956
  // automatically by byokChatCompletion() as X-AEGIS-Key so the account
942
957
  // gets billed the handling fee; see client/aegis.js.
943
958
  const { provider, model: bareModel } = splitByokModel(opts.model);
944
- return aegis.byokChatCompletion({
945
- provider,
946
- model: bareModel,
947
- providerKey: opts.apiKey,
948
- prompt: opts.prompt,
949
- system: opts.system,
950
- messages: opts.messages,
951
- maxTokens: opts.maxTokens,
952
- stream: true,
953
- onStream: opts.onDelta,
954
- signal: opts.signal,
955
- });
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
+ }
956
995
  }
957
996
 
958
997
  const common = {
@@ -1093,6 +1132,27 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
1093
1132
  err.status = 400;
1094
1133
  throw err;
1095
1134
  }
1135
+ // …and the AEGIS account key is what makes the turn BILLABLE at all. The
1136
+ // relay authenticates on the provider key and resolves the payer
1137
+ // separately, from X-AEGIS-Key (app.py `_byok_identify_user`): with no
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
+ }
1096
1156
 
1097
1157
  // Custom classes carry no enumerable model list (see listModels), so a
1098
1158
  // blank id here means the user never typed one. Fail loudly in-process