aegiscode 6.7.4 → 6.7.6

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/tokens.js CHANGED
@@ -41,25 +41,53 @@ const RATES = {
41
41
  deepseek: { input: 0.14, output: 0.28, cacheRead: 0.014, cacheWrite: 0.14 },
42
42
  };
43
43
 
44
+ /**
45
+ * Rate rows a model id may name as a whole word rather than as a prefix.
46
+ * Anthropic spells its family *inside* the id — "claude-opus-4" matches no
47
+ * prefix — so prefix matching alone cannot see it.
48
+ */
49
+ const RATE_FAMILIES = Object.keys(RATES).filter((k) => k !== 'default');
50
+
44
51
  /**
45
52
  * The rate row for a model id, provider, or alias. Exact id wins, then the
46
- * longest matching prefix, then the Sonnet-class default — the same resolution
47
- * order contextWindowFor uses, so the cost meter and the context meter cannot
48
- * disagree about which family a model belongs to.
53
+ * longest matching prefix, then a family named inside the id, then the
54
+ * Sonnet-class default — the same resolution order contextWindowFor uses, so
55
+ * the cost meter and the context meter cannot disagree about which family a
56
+ * model belongs to.
49
57
  */
50
58
  function ratesFor(model) {
51
- const id = String(model || '').toLowerCase();
52
- if (!id) return RATES.default;
53
- if (RATES[id]) return RATES[id];
54
- let best = null;
55
- let bestLen = 0;
56
- for (const [prefix, rates] of Object.entries(RATES)) {
57
- if (id.startsWith(prefix) && prefix.length > bestLen) {
58
- best = rates;
59
- bestLen = prefix.length;
59
+ const raw = String(model || '').toLowerCase();
60
+ if (!raw) return RATES.default;
61
+ // `/class byok` selects a "provider:model" id, and the provider half is a
62
+ // routing label, not a rate: rating it as written fell through every prefix
63
+ // to RATES.default, so an Opus BYOK turn was priced at Sonnet's $3/$15
64
+ // against the vendor's real $5/$25.
65
+ const ids = [raw];
66
+ const tail = raw.slice(raw.lastIndexOf(':') + 1);
67
+ if (tail && tail !== raw) ids.push(tail);
68
+ for (const id of ids) {
69
+ if (RATES[id]) return RATES[id];
70
+ let best = null;
71
+ let bestLen = 0;
72
+ for (const [prefix, rates] of Object.entries(RATES)) {
73
+ if (id.startsWith(prefix) && prefix.length > bestLen) {
74
+ best = rates;
75
+ bestLen = prefix.length;
76
+ }
60
77
  }
78
+ if (best) return best;
61
79
  }
62
- return best || RATES.default;
80
+ // Longest family-key hit wins, so an overlapping pair resolves the same way
81
+ // twice running rather than by object key order.
82
+ let family = null;
83
+ let familyLen = 0;
84
+ for (const key of RATE_FAMILIES) {
85
+ if (raw.includes(key) && key.length > familyLen) {
86
+ family = RATES[key];
87
+ familyLen = key.length;
88
+ }
89
+ }
90
+ return family || RATES.default;
63
91
  }
64
92
 
65
93
  // System prompt + tool definitions overhead, roughly, in tokens.
@@ -97,11 +125,21 @@ const CONTEXT_WINDOWS = {
97
125
 
98
126
  /** The context budget to show/guard against for a model id, provider, or raw model string. */
99
127
  function contextWindowFor(model) {
100
- const id = String(model || '').toLowerCase();
101
- if (!id) return CONTEXT_WINDOW;
102
- if (CONTEXT_WINDOWS[id]) return CONTEXT_WINDOWS[id];
103
- for (const [prefix, win] of Object.entries(CONTEXT_WINDOWS)) {
104
- if (id.startsWith(prefix)) return win;
128
+ const raw = String(model || '').toLowerCase();
129
+ if (!raw) return CONTEXT_WINDOW;
130
+ // Same two-candidate lookup as ratesFor above, for the same reason: a byok
131
+ // id ("deepseek:deepseek-v4-flash") carries a routing label in front of the
132
+ // model, and matching it as written left the 1M-token DeepSeek window reading
133
+ // as the 200k default — a meter that would have told a user they were at 20%
134
+ // of a context that was 4% full.
135
+ const ids = [raw];
136
+ const tail = raw.slice(raw.lastIndexOf(':') + 1);
137
+ if (tail && tail !== raw) ids.push(tail);
138
+ for (const id of ids) {
139
+ if (CONTEXT_WINDOWS[id]) return CONTEXT_WINDOWS[id];
140
+ for (const [prefix, win] of Object.entries(CONTEXT_WINDOWS)) {
141
+ if (id.startsWith(prefix)) return win;
142
+ }
105
143
  }
106
144
  return CONTEXT_WINDOW;
107
145
  }
@@ -355,8 +355,16 @@ function createClient(opts = {}) {
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
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).
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.
360
368
  */
361
369
  async function byokChatCompletion({
362
370
  provider = 'openai',
@@ -87,8 +87,28 @@ const CLASSES = [
87
87
  { class: 'ollama', label: 'Ollama (local)', kind: 'local' },
88
88
  { class: 'openai-compat', label: 'Custom OpenAI-compatible', kind: 'custom' },
89
89
  { class: 'anthropic', label: 'Anthropic-compatible', kind: 'custom' },
90
+ { class: 'byok', label: 'Bring your own key', kind: 'cloud' },
90
91
  ];
91
92
 
93
+ /** Local settings namespace for one BYOK provider's key. A `byok:` prefix
94
+ * keeps this out of the 'anthropic'/'openai-compat' CUSTOM_CLASSES' own
95
+ * namespaces, which are a different feature (a self-hosted/compatible
96
+ * endpoint's base URL + key) — same store, deliberately separate rows. */
97
+ function byokNamespace(providerId) {
98
+ return `byok:${providerId}`;
99
+ }
100
+
101
+ /** Split a byok model id ("anthropic:claude-sonnet-5") into its provider and
102
+ * bare model parts. `modelId` may itself contain ':' (none of today's
103
+ * catalog ids do, but nothing guarantees that), so only the FIRST segment is
104
+ * the provider — the rest re-joins as the model. */
105
+ function splitByokModel(compound) {
106
+ const s = typeof compound === 'string' ? compound : '';
107
+ const i = s.indexOf(':');
108
+ if (i < 0) return { provider: '', model: s };
109
+ return { provider: s.slice(0, i), model: s.slice(i + 1) };
110
+ }
111
+
92
112
  /**
93
113
  * Mirrors aegiscodex-dev's src/backend.js DEEPSEEK_REASONING_MODEL_RE +
94
114
  * EFFORT_TOKEN_BUDGET verbatim. DeepSeek's reasoning models (deepseek-flash,
@@ -657,6 +677,16 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
657
677
  if (c.class === 'aegis') {
658
678
  return { ...c, configured: Boolean(aegis.apiKey) };
659
679
  }
680
+ if (c.class === 'byok') {
681
+ // Configured means "at least one provider has a locally-stored key",
682
+ // not a single baseURL+key pair like the CUSTOM_CLASSES below — byok
683
+ // holds one row per provider (byokNamespace), so customStatus's shape
684
+ // does not apply here.
685
+ const anyConfigured = (settings.list() || []).some(
686
+ (s) => s && typeof s.provider === 'string' && s.provider.startsWith('byok:') && s.configured
687
+ );
688
+ return { ...c, configured: anyConfigured };
689
+ }
660
690
  return { ...c, ...customStatus(c.class) };
661
691
  });
662
692
  }
@@ -681,6 +711,56 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
681
711
  const tags = await ollama.listTags();
682
712
  return { class: cls, models: tags.map((t) => ({ id: t.id })) };
683
713
  }
714
+ if (cls === 'byok') {
715
+ // The server's catalog names every provider it accepts a key for, the
716
+ // models each unlocks, and whether an AEGIS account key is even needed
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.
726
+ let providers = [];
727
+ let fee = null;
728
+ try {
729
+ const data = await aegis.byokProviders();
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;
738
+ } catch {
739
+ providers = [];
740
+ }
741
+ const models = [];
742
+ for (const p of providers) {
743
+ if (!p || !p.id) continue;
744
+ const local = settings.get(byokNamespace(p.id)) || {};
745
+ const rawModels = Array.isArray(p.models) ? p.models : [];
746
+ for (const m of rawModels) {
747
+ const modelId = typeof m === 'string' ? m : m && m.id;
748
+ if (!modelId) continue;
749
+ models.push({
750
+ id: `${p.id}:${modelId}`,
751
+ label: `${p.label || p.id} — ${modelId}`,
752
+ provider: p.id,
753
+ configured: Boolean(local.configured),
754
+ });
755
+ }
756
+ }
757
+ return {
758
+ class: cls, models, providers,
759
+ needsProviderKey: models.length > 0 && !models.some((m) => m.configured),
760
+ needsAegisKey: !aegis.apiKey,
761
+ fee,
762
+ };
763
+ }
684
764
  // Custom endpoints: the model id is the *user's* choice — a provider model
685
765
  // name, never a URL. Offering the configured base URL as an `id` meant that
686
766
  // leaving the default selection POSTed `model: "https://api.openai.com/v1"`,
@@ -867,6 +947,29 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
867
947
  });
868
948
  }
869
949
 
950
+ if (cls === 'byok') {
951
+ // opts.model is still the compound "provider:model" id here — the
952
+ // relay wants them split (a `provider` field plus a bare `model`).
953
+ // opts.apiKey is the PROVIDER key resolved by chat() above (from the
954
+ // byokNamespace(provider) settings row), never the account's own AEGIS
955
+ // key — that one is baked into the `aegis` client already and attached
956
+ // automatically by byokChatCompletion() as X-AEGIS-Key so the account
957
+ // gets billed the handling fee; see client/aegis.js.
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
+ });
971
+ }
972
+
870
973
  const common = {
871
974
  baseURL: opts.cfg.baseURL,
872
975
  apiKey: opts.apiKey,
@@ -887,7 +990,14 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
887
990
  async function chat(payload, onDelta) {
888
991
  const cls = payload && payload.class;
889
992
  const model = payload && payload.model;
890
- const maxTokens = reasoningBudget(cls, model, payload && payload.maxTokens, payload && payload.effort);
993
+ // byok's model id is "provider:model" (see splitByokModel) — strip the
994
+ // provider prefix before pattern-matching, or a DeepSeek reasoning model
995
+ // selected under byok ("byok" model "deepseek:deepseek-v4-flash") never
996
+ // matches DEEPSEEK_REASONING_MODEL_RE's anchored pattern and silently
997
+ // gets no stated budget, which is exactly the "hidden CoT ate the whole
998
+ // default and returned nothing" failure this budget exists to prevent.
999
+ const reasoningModelId = cls === 'byok' ? splitByokModel(model).model : model;
1000
+ const maxTokens = reasoningBudget(cls, reasoningModelId, payload && payload.maxTokens, payload && payload.effort);
891
1001
  // The caller's OWN number, kept apart from `maxTokens` above. That one
892
1002
  // collapses two different facts into a single value — "the caller stated
893
1003
  // 4096" and "effort implies 32768" — and the pooled path must treat them
@@ -937,8 +1047,12 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
937
1047
  const signal = controller.signal;
938
1048
 
939
1049
  // A caller can opt out of the agent loop entirely (`tools: false`) and get
940
- // the old single-shot turn back.
941
- const toolsEnabled = !(payload && payload.tools === false);
1050
+ // the old single-shot turn back. byok is ALWAYS single-shot: the
1051
+ // stateless relay (aegis.byokChatCompletion, /api/v1/byok/chat/completions)
1052
+ // has no tools/tool_choice parameter at all, so sending schemas here would
1053
+ // build a prompt promising tool access the transport silently drops —
1054
+ // the model would reason about exec/readFile and never see a result.
1055
+ const toolsEnabled = cls !== 'byok' && !(payload && payload.tools === false);
942
1056
  const wire = cls === 'anthropic' ? 'anthropic' : 'openai';
943
1057
  const toolSchemas = toolsEnabled ? T.toolsFor(wire, { includeSubagent: depth < MAX_SUBAGENT_DEPTH }) : [];
944
1058
  const toolChoice = (payload && payload.toolChoice) || null;
@@ -965,8 +1079,45 @@ function createLocalEngine({ aegis, settings, ollama, providers, tools, promptBu
965
1079
  const turnCwd = envFor(payload).cwd;
966
1080
 
967
1081
  try {
968
- const cfg = cls === 'aegis' || cls === 'ollama' ? {} : settings.get(cls) || {};
969
- const apiKey = cls === 'aegis' || cls === 'ollama' ? null : settings.rawKey(cls);
1082
+ // byok's settings row lives under the provider named IN THE MODEL id
1083
+ // ("anthropic:claude-sonnet-5" -> byokNamespace('anthropic')), never
1084
+ // under the literal class name — one flat 'byok' row could not hold
1085
+ // more than one provider's key at a time.
1086
+ const byokParts = cls === 'byok' ? splitByokModel(model) : null;
1087
+ const cfg =
1088
+ cls === 'aegis' || cls === 'ollama' ? {}
1089
+ : cls === 'byok' ? settings.get(byokNamespace(byokParts.provider)) || {}
1090
+ : settings.get(cls) || {};
1091
+ const apiKey =
1092
+ cls === 'aegis' || cls === 'ollama' ? null
1093
+ : cls === 'byok' ? settings.rawKey(byokNamespace(byokParts.provider))
1094
+ : settings.rawKey(cls);
1095
+
1096
+ if (cls === 'byok' && (!byokParts.provider || !byokParts.model)) {
1097
+ const err = new Error(
1098
+ "byok: model must be \"<provider>:<model>\" (pick one from /models) — got " +
1099
+ JSON.stringify(model || '')
1100
+ );
1101
+ err.status = 400;
1102
+ throw err;
1103
+ }
1104
+ if (cls === 'byok' && !apiKey) {
1105
+ const err = new Error(
1106
+ `byok: no key saved for "${byokParts.provider}" — add one before chatting with this model.`
1107
+ );
1108
+ err.status = 400;
1109
+ throw err;
1110
+ }
1111
+ // …and the AEGIS account key is what makes the turn BILLABLE at all. The
1112
+ // relay authenticates on the provider key and resolves the payer
1113
+ // 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.
970
1121
 
971
1122
  // Custom classes carry no enumerable model list (see listModels), so a
972
1123
  // blank id here means the user never typed one. Fail loudly in-process