@omnicross/core 0.1.7 → 0.1.8

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 (53) hide show
  1. package/dist/{ProviderProxy-C-xqrkKi.d.ts → ProviderProxy--ROec6VI.d.ts} +22 -3
  2. package/dist/{ProviderProxy-CnMQYN59.d.cts → ProviderProxy-QdwNV6l1.d.cts} +22 -3
  3. package/dist/{chunk-4BB343BI.cjs → chunk-26K6T2YS.cjs} +1209 -472
  4. package/dist/{chunk-OJGQKHN5.cjs → chunk-EPN67EGP.cjs} +3 -0
  5. package/dist/{chunk-E3COOKUP.js → chunk-QYC54PMG.js} +924 -187
  6. package/dist/{chunk-SOO562NM.js → chunk-UAPBLNN2.js} +3 -0
  7. package/dist/completion/ApiKeyPoolService.cjs +32 -1
  8. package/dist/completion/ApiKeyPoolService.d.cts +17 -1
  9. package/dist/completion/ApiKeyPoolService.d.ts +17 -1
  10. package/dist/completion/ApiKeyPoolService.js +31 -0
  11. package/dist/completion/CompletionService.cjs +3 -3
  12. package/dist/completion/CompletionService.js +2 -2
  13. package/dist/completion.cjs +3 -3
  14. package/dist/completion.js +2 -2
  15. package/dist/index.cjs +59 -3
  16. package/dist/index.d.cts +5 -5
  17. package/dist/index.d.ts +5 -5
  18. package/dist/index.js +58 -2
  19. package/dist/outbound-api/routeResolver.d.cts +3 -3
  20. package/dist/outbound-api/routeResolver.d.ts +3 -3
  21. package/dist/outbound-api/types.d.cts +2 -2
  22. package/dist/outbound-api/types.d.ts +2 -2
  23. package/dist/outbound-api.cjs +3 -3
  24. package/dist/outbound-api.d.cts +4 -4
  25. package/dist/outbound-api.d.ts +4 -4
  26. package/dist/outbound-api.js +2 -2
  27. package/dist/pipeline/AccountAllowanceScheduling.d.cts +2 -2
  28. package/dist/pipeline/AccountAllowanceScheduling.d.ts +2 -2
  29. package/dist/ports.d.cts +2 -2
  30. package/dist/ports.d.ts +2 -2
  31. package/dist/provider-proxy/ProviderProxy.cjs +3 -3
  32. package/dist/provider-proxy/ProviderProxy.d.cts +1 -1
  33. package/dist/provider-proxy/ProviderProxy.d.ts +1 -1
  34. package/dist/provider-proxy/ProviderProxy.js +2 -2
  35. package/dist/provider-proxy/ingress/providerProxyShared.cjs +3 -3
  36. package/dist/provider-proxy/ingress/providerProxyShared.js +2 -2
  37. package/dist/provider-proxy/types.d.cts +15 -1
  38. package/dist/provider-proxy/types.d.ts +15 -1
  39. package/dist/provider-proxy.cjs +59 -3
  40. package/dist/provider-proxy.d.cts +254 -8
  41. package/dist/provider-proxy.d.ts +254 -8
  42. package/dist/provider-proxy.js +60 -4
  43. package/dist/{routeResolver-BxOfunbm.d.ts → routeResolver-C0mkkZaM.d.ts} +1 -1
  44. package/dist/{routeResolver-D5OzD0iM.d.cts → routeResolver-CVFE8KKe.d.cts} +1 -1
  45. package/dist/{types-CqZD77rS.d.ts → types-BB8pxk_3.d.ts} +1 -1
  46. package/dist/{types-gjJ5lJ5n.d.cts → types-C2MwkLah.d.cts} +1 -1
  47. package/dist/usage/usage-recorder.cjs +2 -2
  48. package/dist/usage/usage-recorder.d.cts +6 -0
  49. package/dist/usage/usage-recorder.d.ts +6 -0
  50. package/dist/usage/usage-recorder.js +1 -1
  51. package/dist/usage.cjs +2 -2
  52. package/dist/usage.js +1 -1
  53. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15;
2
2
 
3
3
  var _chunkHQP3K7PUcjs = require('./chunk-HQP3K7PU.cjs');
4
4
 
@@ -96,6 +96,9 @@ var _chunk4YINUUKQcjs = require('./chunk-4YINUUKQ.cjs');
96
96
  var _chunk4VZX5F4Tcjs = require('./chunk-4VZX5F4T.cjs');
97
97
 
98
98
 
99
+ var _chunkQBZJ7P2Tcjs = require('./chunk-QBZJ7P2T.cjs');
100
+
101
+
99
102
 
100
103
 
101
104
  var _chunkA2YMUCBTcjs = require('./chunk-A2YMUCBT.cjs');
@@ -432,12 +435,16 @@ var ProviderProxyRouteMap = (_class = class {
432
435
  * (`ANTHROPIC_AUTH_TOKEN` / `OPENAI_API_KEY`). Optionally override the idle
433
436
  * timeout (tests use a short one).
434
437
  */
435
- addRoute(context, idleMs) {
438
+ addRoute(context, idleMsOrOptions) {
439
+ const options = typeof idleMsOrOptions === "number" ? { idleMs: idleMsOrOptions } : _nullishCoalesce(idleMsOrOptions, () => ( {}));
436
440
  const token = _crypto.randomBytes.call(void 0, TOKEN_BYTES).toString("hex");
437
441
  const entry = {
438
442
  context,
439
443
  idleTimer: null,
440
- idleMs: _nullishCoalesce(idleMs, () => ( this.defaultIdleMs))
444
+ idleMs: _nullishCoalesce(options.idleMs, () => ( this.defaultIdleMs)),
445
+ onActivity: options.onActivity,
446
+ onEvicted: options.onEvicted,
447
+ evicted: false
441
448
  };
442
449
  this.routes.set(token, entry);
443
450
  this.armIdleTimer(token, entry);
@@ -452,6 +459,10 @@ var ProviderProxyRouteMap = (_class = class {
452
459
  if (!token) return void 0;
453
460
  const entry = this.routes.get(token);
454
461
  if (!entry) return void 0;
462
+ try {
463
+ _optionalChain([entry, 'access', _5 => _5.onActivity, 'optionalCall', _6 => _6(Date.now())]);
464
+ } catch (e4) {
465
+ }
455
466
  this.armIdleTimer(token, entry);
456
467
  return entry.context;
457
468
  }
@@ -459,8 +470,16 @@ var ProviderProxyRouteMap = (_class = class {
459
470
  removeRoute(token) {
460
471
  const entry = this.routes.get(token);
461
472
  if (!entry) return false;
462
- this.clearIdleTimer(entry);
463
473
  this.routes.delete(token);
474
+ this.evict(entry, "removed");
475
+ return true;
476
+ }
477
+ /** Re-arm one existing route without rotating its token. */
478
+ renewRoute(token, idleMs) {
479
+ const entry = this.routes.get(token);
480
+ if (!entry) return false;
481
+ if (idleMs !== void 0) entry.idleMs = idleMs;
482
+ this.armIdleTimer(token, entry);
464
483
  return true;
465
484
  }
466
485
  /** Current live-route count (tests / diagnostics). */
@@ -473,10 +492,11 @@ var ProviderProxyRouteMap = (_class = class {
473
492
  }
474
493
  /** Tear down every route (proxy stop / app teardown). */
475
494
  clear() {
476
- for (const entry of this.routes.values()) {
477
- this.clearIdleTimer(entry);
478
- }
495
+ const entries2 = [...this.routes.values()];
479
496
  this.routes.clear();
497
+ for (const entry of entries2) {
498
+ this.evict(entry, "cleared");
499
+ }
480
500
  }
481
501
  // ---------------------------------------------------------------------------
482
502
  // Idle reaping (modeled on AcpSessionManager.armIdleTimer / clearIdleTimer)
@@ -484,9 +504,10 @@ var ProviderProxyRouteMap = (_class = class {
484
504
  armIdleTimer(token, entry) {
485
505
  this.clearIdleTimer(entry);
486
506
  entry.idleTimer = setTimeout(() => {
487
- this.routes.delete(token);
507
+ if (!this.routes.delete(token)) return;
508
+ this.evict(entry, "idle");
488
509
  }, entry.idleMs);
489
- _optionalChain([entry, 'access', _5 => _5.idleTimer, 'access', _6 => _6.unref, 'optionalCall', _7 => _7()]);
510
+ _optionalChain([entry, 'access', _7 => _7.idleTimer, 'access', _8 => _8.unref, 'optionalCall', _9 => _9()]);
490
511
  }
491
512
  clearIdleTimer(entry) {
492
513
  if (entry.idleTimer) {
@@ -494,6 +515,15 @@ var ProviderProxyRouteMap = (_class = class {
494
515
  entry.idleTimer = null;
495
516
  }
496
517
  }
518
+ evict(entry, reason) {
519
+ if (entry.evicted) return;
520
+ entry.evicted = true;
521
+ this.clearIdleTimer(entry);
522
+ try {
523
+ _optionalChain([entry, 'access', _10 => _10.onEvicted, 'optionalCall', _11 => _11(reason)]);
524
+ } catch (e5) {
525
+ }
526
+ }
497
527
  }, _class);
498
528
 
499
529
  // src/provider-proxy/identity/claudeCodeHeaders.ts
@@ -583,6 +613,10 @@ function emitAnthropicUsageRecord(recorder, rawUsage, attribution) {
583
613
  providerId: attribution.providerId,
584
614
  model: attribution.model,
585
615
  apiKeyId: attribution.apiKeyId,
616
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _12 => _12.routeLease, 'optionalAccess', _13 => _13.runId]), () => ( null)),
617
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _14 => _14.routeLease, 'optionalAccess', _15 => _15.leaseId]), () => ( null)),
618
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _16 => _16.routeLease, 'optionalAccess', _17 => _17.consumer]), () => ( null)),
619
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _18 => _18.routeLease, 'optionalAccess', _19 => _19.stageId]), () => ( null)),
586
620
  auditResponse: attribution.auditResponse,
587
621
  engineOrigin: "codex-ingress",
588
622
  usage: tapped,
@@ -597,13 +631,13 @@ function recordAnthropicNonStreamUsage(recorder, bodyText, attribution) {
597
631
  parsed.usage,
598
632
  attribution
599
633
  );
600
- } catch (e4) {
634
+ } catch (e6) {
601
635
  }
602
636
  }
603
637
  function recordAnthropicStreamUsage(recorder, rawUsage, attribution) {
604
638
  try {
605
639
  emitAnthropicUsageRecord(recorder, rawUsage, attribution);
606
- } catch (e5) {
640
+ } catch (e7) {
607
641
  }
608
642
  }
609
643
 
@@ -653,7 +687,7 @@ function rewriteJsonModel(bodyText, rewriteModel) {
653
687
  changed = true;
654
688
  }
655
689
  return changed ? JSON.stringify(obj) : bodyText;
656
- } catch (e6) {
690
+ } catch (e8) {
657
691
  return bodyText;
658
692
  }
659
693
  }
@@ -686,7 +720,7 @@ function rewriteSseLine(line, rewriteModel) {
686
720
  }
687
721
  if (!changed) return line;
688
722
  return `data:${leadingSpace}${JSON.stringify(obj)}${newline}`;
689
- } catch (e7) {
723
+ } catch (e9) {
690
724
  return line;
691
725
  }
692
726
  }
@@ -697,7 +731,7 @@ function tapSseDataLine(line, onEvent) {
697
731
  if (!payload || payload === "[DONE]") return;
698
732
  try {
699
733
  onEvent(JSON.parse(payload));
700
- } catch (e8) {
734
+ } catch (e10) {
701
735
  }
702
736
  }
703
737
  async function relaySseBody(res, body, options) {
@@ -749,7 +783,7 @@ async function relayResponse(res, providerResponse, isStream, rewriteModel, usag
749
783
  await relaySseBody(res, providerResponse.body, {
750
784
  rewriteModel,
751
785
  onSseEvent: onSseEvent || usageTap ? (event) => {
752
- _optionalChain([onSseEvent, 'optionalCall', _8 => _8(event)]);
786
+ _optionalChain([onSseEvent, 'optionalCall', _20 => _20(event)]);
753
787
  if (usageTap) {
754
788
  const raw = usageTap.extractUsage(event);
755
789
  if (raw) capturedUsage = raw;
@@ -797,7 +831,7 @@ async function aggregateAnthropicSseToJsonBody(response, rewriteModel) {
797
831
  let ev;
798
832
  try {
799
833
  ev = JSON.parse(payload);
800
- } catch (e9) {
834
+ } catch (e11) {
801
835
  continue;
802
836
  }
803
837
  switch (ev["type"]) {
@@ -852,7 +886,7 @@ async function aggregateAnthropicSseToJsonBody(response, rewriteModel) {
852
886
  let input = {};
853
887
  try {
854
888
  input = blk.inputJson ? JSON.parse(blk.inputJson) : {};
855
- } catch (e10) {
889
+ } catch (e12) {
856
890
  input = {};
857
891
  }
858
892
  return { type: "tool_use", id: _nullishCoalesce(blk.id, () => ( "")), name: _nullishCoalesce(blk.name, () => ( "")), input };
@@ -902,7 +936,7 @@ function resolveApiKey(apiKey) {
902
936
  }
903
937
  async function resolvePoolBoundKey(deps, providerId, provider, sessionId, preferredKeyId, boundKeyFallbackPolicy) {
904
938
  if (preferredKeyId) {
905
- const preferred = await _optionalChain([deps, 'access', _9 => _9.apiKeyPool, 'optionalAccess', _10 => _10.getKeyById, 'call', _11 => _11(providerId, preferredKeyId, sessionId)]);
939
+ const preferred = await _optionalChain([deps, 'access', _21 => _21.apiKeyPool, 'optionalAccess', _22 => _22.getKeyById, 'call', _23 => _23(providerId, preferredKeyId, sessionId)]);
906
940
  if (preferred) return preferred;
907
941
  if (boundKeyFallbackPolicy !== "pool") return "";
908
942
  }
@@ -937,11 +971,11 @@ async function buildSubscriptionPlan(res, route, deps, anthropicBody, routeModel
937
971
  return plan;
938
972
  }
939
973
  function buildSubscriptionIterationPlan(profile, route, deps, resolvedModel, isStream, scenario, sessionKey) {
940
- const upstreamUrl = _optionalChain([profile, 'access', _12 => _12.resolveUpstreamUrl, 'optionalCall', _13 => _13(resolvedModel, route.subscriptionConfig)]);
974
+ const upstreamUrl = _optionalChain([profile, 'access', _24 => _24.resolveUpstreamUrl, 'optionalCall', _25 => _25(resolvedModel, route.subscriptionConfig)]);
941
975
  if (!upstreamUrl) return null;
942
976
  const auth = _nullishCoalesce(route.auth, () => ( new (0, _chunkYNJNTCSQcjs.SubscriptionAuthSource)(profile)));
943
977
  const sameFormat = profile.mode === "pass-through" || upstreamUrl.endsWith("/v1/messages");
944
- const overrideNames = _optionalChain([profile, 'access', _14 => _14.resolveProviderTransformerNames, 'optionalCall', _15 => _15(
978
+ const overrideNames = _optionalChain([profile, 'access', _26 => _26.resolveProviderTransformerNames, 'optionalCall', _27 => _27(
945
979
  resolvedModel,
946
980
  route.subscriptionConfig
947
981
  )]);
@@ -1040,7 +1074,7 @@ async function runPipeline(anthropicBody, plan, reportSelection) {
1040
1074
  boundAccountFallbackPolicy: plan.boundAccountFallbackPolicy,
1041
1075
  reportSelection: (accountId, isActive) => {
1042
1076
  proxyAccountId = accountId;
1043
- _optionalChain([reportSelection, 'optionalCall', _16 => _16(accountId, isActive)]);
1077
+ _optionalChain([reportSelection, 'optionalCall', _28 => _28(accountId, isActive)]);
1044
1078
  }
1045
1079
  });
1046
1080
  let rawStatus = null;
@@ -1098,7 +1132,7 @@ async function runSubscriptionSameFormatFetch(rawBody, plan, reportSelection, op
1098
1132
  reportSelection: (accountId, isActive, remapped) => {
1099
1133
  proxyAccountId = accountId;
1100
1134
  remappedModel = remapped;
1101
- _optionalChain([reportSelection, 'optionalCall', _17 => _17(accountId, isActive, remapped)]);
1135
+ _optionalChain([reportSelection, 'optionalCall', _29 => _29(accountId, isActive, remapped)]);
1102
1136
  }
1103
1137
  });
1104
1138
  const isClaudeSubscription = plan.isSubscription && plan.transformerProvider.name === "claude";
@@ -1142,7 +1176,7 @@ async function runSubscriptionSameFormatFetch(rawBody, plan, reportSelection, op
1142
1176
  } : void 0
1143
1177
  }
1144
1178
  );
1145
- return { response, rawStatus: response.status };
1179
+ return { response, rawStatus: response.status, actualModel: outboundModel };
1146
1180
  }
1147
1181
  function outboundRemapApplies(plan) {
1148
1182
  return plan.isSubscription && plan.transformerProvider.name === "claude";
@@ -1153,7 +1187,7 @@ function rewriteBodyModel(rawBody, model) {
1153
1187
  if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return rawBody;
1154
1188
  parsed.model = model;
1155
1189
  return JSON.stringify(parsed);
1156
- } catch (e11) {
1190
+ } catch (e13) {
1157
1191
  return rawBody;
1158
1192
  }
1159
1193
  }
@@ -1166,7 +1200,7 @@ async function runSubscriptionAttemptWith401Retry(anthropicBody, relayBody, plan
1166
1200
  const first = await runOnce();
1167
1201
  let result = first;
1168
1202
  if (first.rawStatus === 401) {
1169
- const refreshed = await _optionalChain([plan, 'access', _18 => _18.auth, 'access', _19 => _19.onUnauthorized, 'optionalCall', _20 => _20(plan.sessionKey)]);
1203
+ const refreshed = await _optionalChain([plan, 'access', _30 => _30.auth, 'access', _31 => _31.onUnauthorized, 'optionalCall', _32 => _32(plan.sessionKey)]);
1170
1204
  if (refreshed) {
1171
1205
  console.info("[ProviderProxy:anthropic] 401 \u2192 token refreshed; retrying once");
1172
1206
  result = await runOnce();
@@ -1181,7 +1215,7 @@ async function readBoundedBody(response, max = 2048) {
1181
1215
  try {
1182
1216
  const text = await response.clone().text();
1183
1217
  return text.slice(0, max);
1184
- } catch (e12) {
1218
+ } catch (e14) {
1185
1219
  return void 0;
1186
1220
  }
1187
1221
  }
@@ -1209,7 +1243,7 @@ function breakerOutcome(rawStatus) {
1209
1243
  function recordBreakerOutcome(profile, modelId, rawStatus) {
1210
1244
  const outcome = breakerOutcome(rawStatus);
1211
1245
  if (outcome === "neutral") return;
1212
- _optionalChain([profile, 'optionalAccess', _21 => _21.recordModelOutcome, 'optionalCall', _22 => _22(modelId, outcome === "success")]);
1246
+ _optionalChain([profile, 'optionalAccess', _33 => _33.recordModelOutcome, 'optionalCall', _34 => _34(modelId, outcome === "success")]);
1213
1247
  }
1214
1248
  async function runSubscriptionAttemptOutcome(anthropicBody, relayBody, plan, options = {}) {
1215
1249
  try {
@@ -1228,7 +1262,7 @@ function settleOutcome(outcome) {
1228
1262
  async function runPipelineWithSubscriptionRetry(anthropicBody, rawBody, initialPlan, route, deps, options = {}) {
1229
1263
  const profile = route.subscriptionProfile;
1230
1264
  const scenario = initialPlan.scenario;
1231
- if (!_optionalChain([profile, 'optionalAccess', _23 => _23.nextFallback]) || scenario === void 0) {
1265
+ if (!_optionalChain([profile, 'optionalAccess', _35 => _35.nextFallback]) || scenario === void 0) {
1232
1266
  const loneOutcome = await runSubscriptionAttemptOutcome(anthropicBody, rawBody, initialPlan, options);
1233
1267
  recordBreakerOutcome(profile, initialPlan.resolvedModel, outcomeStatus(loneOutcome));
1234
1268
  return settleOutcome(loneOutcome);
@@ -1292,7 +1326,7 @@ async function handleAnthropicMessagesByo(res, rawBody, route, deps, options = {
1292
1326
  let anthropicBody;
1293
1327
  try {
1294
1328
  anthropicBody = JSON.parse(rawBody);
1295
- } catch (e13) {
1329
+ } catch (e15) {
1296
1330
  writeError(res, 400, "Invalid JSON in request body");
1297
1331
  return;
1298
1332
  }
@@ -1308,11 +1342,13 @@ async function handleAnthropicMessagesByo(res, rawBody, route, deps, options = {
1308
1342
  const upstreamSse = (_nullishCoalesce(upstreamResponse.headers.get("content-type"), () => ( ""))).includes(
1309
1343
  "text/event-stream"
1310
1344
  );
1345
+ const actualModel = "actualModel" in providerResponse && typeof providerResponse.actualModel === "string" ? providerResponse.actualModel : void 0;
1311
1346
  const usageAttribution = {
1312
1347
  sessionId: route.sessionId,
1313
1348
  providerId: _nullishCoalesce(route.providerId, () => ( "anthropic")),
1314
- model: plan.resolvedModel,
1349
+ model: _nullishCoalesce(actualModel, () => ( plan.resolvedModel)),
1315
1350
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
1351
+ routeLease: route.routeLease,
1316
1352
  // request-audit-log: correlate this request's tokens/cost to its audit record.
1317
1353
  auditResponse: res
1318
1354
  };
@@ -1322,7 +1358,7 @@ async function handleAnthropicMessagesByo(res, rawBody, route, deps, options = {
1322
1358
  extractUsage(event) {
1323
1359
  if (event["type"] === "message_start") {
1324
1360
  const msg = event["message"];
1325
- const u = _optionalChain([msg, 'optionalAccess', _24 => _24["usage"]]);
1361
+ const u = _optionalChain([msg, 'optionalAccess', _36 => _36["usage"]]);
1326
1362
  if (u) anthropicInputUsage = u;
1327
1363
  return null;
1328
1364
  }
@@ -1412,7 +1448,7 @@ async function buildByoPlan(res, route, deps, resolvedModel, isStream) {
1412
1448
  isSubscription: false,
1413
1449
  provider,
1414
1450
  apiKey,
1415
- extendedContextEnabled: _nullishCoalesce(_optionalChain([route, 'access', _25 => _25.anthropicSdkHints, 'optionalAccess', _26 => _26.extendedContext, 'optionalAccess', _27 => _27.enabled]), () => ( false))
1451
+ extendedContextEnabled: _nullishCoalesce(_optionalChain([route, 'access', _37 => _37.anthropicSdkHints, 'optionalAccess', _38 => _38.extendedContext, 'optionalAccess', _39 => _39.enabled]), () => ( false))
1416
1452
  };
1417
1453
  }
1418
1454
  async function runSameFormatFetch(bodyToSend, plan, options, keyOverride) {
@@ -1422,7 +1458,7 @@ async function runSameFormatFetch(bodyToSend, plan, options, keyOverride) {
1422
1458
  }
1423
1459
  const effectiveKey = _nullishCoalesce(_nullishCoalesce(keyOverride, () => ( apiKey)), () => ( ""));
1424
1460
  const headers = _chunkQ27JY5RGcjs.getProviderHeaders.call(void 0, provider, effectiveKey);
1425
- const callerBeta = _optionalChain([options, 'access', _28 => _28.callerAnthropicBeta, 'optionalAccess', _29 => _29.trim, 'call', _30 => _30()]);
1461
+ const callerBeta = _optionalChain([options, 'access', _40 => _40.callerAnthropicBeta, 'optionalAccess', _41 => _41.trim, 'call', _42 => _42()]);
1426
1462
  if (callerBeta) {
1427
1463
  const existing = _nullishCoalesce(headers["anthropic-beta"], () => ( headers["Anthropic-Beta"]));
1428
1464
  const parts = new Set(
@@ -1448,7 +1484,7 @@ function resolveSameFormatBody(anthropicBody, rawBody, plan) {
1448
1484
  try {
1449
1485
  const m = JSON.parse(rawBody).model;
1450
1486
  originalModel = typeof m === "string" ? m : void 0;
1451
- } catch (e14) {
1487
+ } catch (e16) {
1452
1488
  originalModel = void 0;
1453
1489
  }
1454
1490
  return plan.resolvedModel !== originalModel ? JSON.stringify(anthropicBody) : rawBody;
@@ -1457,8 +1493,8 @@ async function runPipelineWithPoolReporting(anthropicBody, rawBody, plan, option
1457
1493
  const sameFormatBody = plan.sameFormat ? resolveSameFormatBody(anthropicBody, rawBody, plan) : rawBody;
1458
1494
  const runOnce = (keyOverride) => plan.sameFormat ? runSameFormatFetch(sameFormatBody, plan, options, keyOverride) : runPipeline(anthropicBody, plan);
1459
1495
  const first = await runOnce();
1460
- const outcome = await _optionalChain([plan, 'access', _31 => _31.auth, 'access', _32 => _32.onResult, 'optionalCall', _33 => _33(first.rawStatus)]);
1461
- if (_optionalChain([outcome, 'optionalAccess', _34 => _34.rebound])) {
1496
+ const outcome = await _optionalChain([plan, 'access', _43 => _43.auth, 'access', _44 => _44.onResult, 'optionalCall', _45 => _45(first.rawStatus)]);
1497
+ if (_optionalChain([outcome, 'optionalAccess', _46 => _46.rebound])) {
1462
1498
  console.info(
1463
1499
  "[ProviderProxy:anthropic] pool re-bound key after status",
1464
1500
  first.rawStatus,
@@ -1571,6 +1607,10 @@ function emitGeminiUsageRecord(recorder, rawUsageMetadata, attribution) {
1571
1607
  providerId: attribution.providerId,
1572
1608
  model: attribution.model,
1573
1609
  apiKeyId: attribution.apiKeyId,
1610
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _47 => _47.routeLease, 'optionalAccess', _48 => _48.runId]), () => ( null)),
1611
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _49 => _49.routeLease, 'optionalAccess', _50 => _50.leaseId]), () => ( null)),
1612
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _51 => _51.routeLease, 'optionalAccess', _52 => _52.consumer]), () => ( null)),
1613
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _53 => _53.routeLease, 'optionalAccess', _54 => _54.stageId]), () => ( null)),
1574
1614
  auditResponse: attribution.auditResponse,
1575
1615
  engineOrigin: "codex-ingress",
1576
1616
  usage: tapped,
@@ -1585,13 +1625,13 @@ function recordGeminiNonStreamUsage(recorder, bodyText, attribution) {
1585
1625
  parsed.usageMetadata,
1586
1626
  attribution
1587
1627
  );
1588
- } catch (e15) {
1628
+ } catch (e17) {
1589
1629
  }
1590
1630
  }
1591
1631
  function recordGeminiStreamUsage(recorder, rawUsageMetadata, attribution) {
1592
1632
  try {
1593
1633
  emitGeminiUsageRecord(recorder, rawUsageMetadata, attribution);
1594
- } catch (e16) {
1634
+ } catch (e18) {
1595
1635
  }
1596
1636
  }
1597
1637
 
@@ -1600,14 +1640,14 @@ var GENERATE_CONTENT_ACTION = ":generateContent";
1600
1640
  var STREAM_GENERATE_CONTENT_ACTION = ":streamGenerateContent";
1601
1641
  function isGeminiGenerateContentRequest(method, url) {
1602
1642
  if (method !== "POST" || !url) return false;
1603
- const path = _nullishCoalesce(_optionalChain([url, 'access', _35 => _35.split, 'call', _36 => _36("?"), 'access', _37 => _37[0], 'optionalAccess', _38 => _38.replace, 'call', _39 => _39(/\/+$/, "")]), () => ( ""));
1643
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _55 => _55.split, 'call', _56 => _56("?"), 'access', _57 => _57[0], 'optionalAccess', _58 => _58.replace, 'call', _59 => _59(/\/+$/, "")]), () => ( ""));
1604
1644
  const lastSegment = _nullishCoalesce(path.split("/").pop(), () => ( ""));
1605
1645
  return lastSegment.includes(STREAM_GENERATE_CONTENT_ACTION) || lastSegment.includes(GENERATE_CONTENT_ACTION);
1606
1646
  }
1607
1647
  function isGeminiStreamRequest(url) {
1608
1648
  if (!url) return false;
1609
1649
  const [path, query = ""] = url.split("?");
1610
- const lastSegment = _nullishCoalesce((_nullishCoalesce(_optionalChain([path, 'optionalAccess', _40 => _40.replace, 'call', _41 => _41(/\/+$/, "")]), () => ( ""))).split("/").pop(), () => ( ""));
1650
+ const lastSegment = _nullishCoalesce((_nullishCoalesce(_optionalChain([path, 'optionalAccess', _60 => _60.replace, 'call', _61 => _61(/\/+$/, "")]), () => ( ""))).split("/").pop(), () => ( ""));
1611
1651
  if (lastSegment.includes(STREAM_GENERATE_CONTENT_ACTION)) return true;
1612
1652
  return /(^|&)alt=sse(&|$)/.test(query);
1613
1653
  }
@@ -1615,7 +1655,7 @@ async function handleGeminiGenerateContentRequest(res, rawBody, url, route, deps
1615
1655
  let geminiBody;
1616
1656
  try {
1617
1657
  geminiBody = JSON.parse(rawBody);
1618
- } catch (e17) {
1658
+ } catch (e19) {
1619
1659
  writeError(res, 400, "Invalid JSON in request body");
1620
1660
  return;
1621
1661
  }
@@ -1636,6 +1676,7 @@ async function handleGeminiGenerateContentRequest(res, rawBody, url, route, deps
1636
1676
  providerId: _nullishCoalesce(route.providerId, () => ( "gemini")),
1637
1677
  model: resolvedModel,
1638
1678
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
1679
+ routeLease: route.routeLease,
1639
1680
  // request-audit-log: correlate this request's tokens/cost to its audit record.
1640
1681
  auditResponse: res
1641
1682
  };
@@ -1752,8 +1793,8 @@ async function runPipeline2(geminiBody, plan) {
1752
1793
  }
1753
1794
  async function runPipelineWithPoolReporting2(geminiBody, plan) {
1754
1795
  const first = await runPipeline2(geminiBody, plan);
1755
- const outcome = await _optionalChain([plan, 'access', _42 => _42.auth, 'access', _43 => _43.onResult, 'optionalCall', _44 => _44(first.rawStatus)]);
1756
- if (_optionalChain([outcome, 'optionalAccess', _45 => _45.rebound])) {
1796
+ const outcome = await _optionalChain([plan, 'access', _62 => _62.auth, 'access', _63 => _63.onResult, 'optionalCall', _64 => _64(first.rawStatus)]);
1797
+ if (_optionalChain([outcome, 'optionalAccess', _65 => _65.rebound])) {
1757
1798
  console.log(
1758
1799
  "[ProviderProxy:gemini] pool re-bound key after status",
1759
1800
  first.rawStatus,
@@ -1792,6 +1833,10 @@ function emitChatCompletionsUsageRecord(recorder, rawUsage, attribution) {
1792
1833
  providerId: attribution.providerId,
1793
1834
  model: attribution.model,
1794
1835
  apiKeyId: attribution.apiKeyId,
1836
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _66 => _66.routeLease, 'optionalAccess', _67 => _67.runId]), () => ( null)),
1837
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _68 => _68.routeLease, 'optionalAccess', _69 => _69.leaseId]), () => ( null)),
1838
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _70 => _70.routeLease, 'optionalAccess', _71 => _71.consumer]), () => ( null)),
1839
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _72 => _72.routeLease, 'optionalAccess', _73 => _73.stageId]), () => ( null)),
1795
1840
  auditResponse: attribution.auditResponse,
1796
1841
  engineOrigin: "codex-ingress",
1797
1842
  usage: tapped,
@@ -1806,20 +1851,20 @@ function recordChatCompletionsNonStreamUsage(recorder, bodyText, attribution) {
1806
1851
  parsed.usage,
1807
1852
  attribution
1808
1853
  );
1809
- } catch (e18) {
1854
+ } catch (e20) {
1810
1855
  }
1811
1856
  }
1812
1857
  function recordChatCompletionsStreamUsage(recorder, rawUsage, attribution) {
1813
1858
  try {
1814
1859
  emitChatCompletionsUsageRecord(recorder, rawUsage, attribution);
1815
- } catch (e19) {
1860
+ } catch (e21) {
1816
1861
  }
1817
1862
  }
1818
1863
 
1819
1864
  // src/provider-proxy/ingress/openaiChatIngress.ts
1820
1865
  function isOpenAIChatRequest(method, url) {
1821
1866
  if (method !== "POST" || !url) return false;
1822
- const path = _nullishCoalesce(_optionalChain([url, 'access', _46 => _46.split, 'call', _47 => _47("?"), 'access', _48 => _48[0], 'optionalAccess', _49 => _49.replace, 'call', _50 => _50(/\/+$/, "")]), () => ( ""));
1867
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _74 => _74.split, 'call', _75 => _75("?"), 'access', _76 => _76[0], 'optionalAccess', _77 => _77.replace, 'call', _78 => _78(/\/+$/, "")]), () => ( ""));
1823
1868
  return path.endsWith("/chat/completions");
1824
1869
  }
1825
1870
  var CHAT_BRIDGE_SUBSCRIPTION_PROVIDERS = /* @__PURE__ */ new Set(["claude"]);
@@ -1828,7 +1873,7 @@ async function handleOpenAIChatRequest(res, rawBody, route, deps) {
1828
1873
  let chatBody;
1829
1874
  try {
1830
1875
  chatBody = JSON.parse(rawBody);
1831
- } catch (e20) {
1876
+ } catch (e22) {
1832
1877
  writeError(res, 400, "Invalid JSON in request body");
1833
1878
  return;
1834
1879
  }
@@ -1844,6 +1889,7 @@ async function handleOpenAIChatRequest(res, rawBody, route, deps) {
1844
1889
  providerId: _nullishCoalesce(route.providerId, () => ( "openai")),
1845
1890
  model: resolvedModel,
1846
1891
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
1892
+ routeLease: route.routeLease,
1847
1893
  // request-audit-log: correlate this request's tokens/cost to its audit record.
1848
1894
  auditResponse: res
1849
1895
  };
@@ -1941,7 +1987,7 @@ async function buildSubscriptionPlan2(res, route, deps, resolvedModel, isStream)
1941
1987
  );
1942
1988
  return null;
1943
1989
  }
1944
- const upstreamUrl = _optionalChain([profile, 'access', _51 => _51.resolveUpstreamUrl, 'optionalCall', _52 => _52(resolvedModel)]);
1990
+ const upstreamUrl = _optionalChain([profile, 'access', _79 => _79.resolveUpstreamUrl, 'optionalCall', _80 => _80(resolvedModel)]);
1945
1991
  if (!upstreamUrl) {
1946
1992
  writeError(res, 502, "Subscription profile is missing resolveUpstreamUrl");
1947
1993
  return null;
@@ -2021,8 +2067,8 @@ async function runPipeline3(chatBody, plan) {
2021
2067
  }
2022
2068
  async function runPipelineWithPoolReporting3(chatBody, plan) {
2023
2069
  const first = await runPipeline3(chatBody, plan);
2024
- const outcome = await _optionalChain([plan, 'access', _53 => _53.auth, 'access', _54 => _54.onResult, 'optionalCall', _55 => _55(first.rawStatus)]);
2025
- if (_optionalChain([outcome, 'optionalAccess', _56 => _56.rebound])) {
2070
+ const outcome = await _optionalChain([plan, 'access', _81 => _81.auth, 'access', _82 => _82.onResult, 'optionalCall', _83 => _83(first.rawStatus)]);
2071
+ if (_optionalChain([outcome, 'optionalAccess', _84 => _84.rebound])) {
2026
2072
  console.log(
2027
2073
  "[ProviderProxy:chat] pool re-bound key after status",
2028
2074
  first.rawStatus,
@@ -2035,7 +2081,7 @@ async function runPipelineWithPoolReporting3(chatBody, plan) {
2035
2081
  async function runPipelineWithSubscriptionRetry2(chatBody, plan) {
2036
2082
  const first = await runPipeline3(chatBody, plan);
2037
2083
  if (first.rawStatus !== 401) return first;
2038
- const refreshed = await _optionalChain([plan, 'access', _57 => _57.auth, 'access', _58 => _58.onUnauthorized, 'optionalCall', _59 => _59()]);
2084
+ const refreshed = await _optionalChain([plan, 'access', _85 => _85.auth, 'access', _86 => _86.onUnauthorized, 'optionalCall', _87 => _87()]);
2039
2085
  if (!refreshed) {
2040
2086
  console.warn("[ProviderProxy:chat] 401 not recoverable (onUnauthorized returned false)");
2041
2087
  return first;
@@ -2072,6 +2118,10 @@ function emitResponsesUsageRecord(recorder, rawUsage, attribution) {
2072
2118
  providerId: attribution.providerId,
2073
2119
  model: attribution.model,
2074
2120
  apiKeyId: attribution.apiKeyId,
2121
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _88 => _88.routeLease, 'optionalAccess', _89 => _89.runId]), () => ( null)),
2122
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _90 => _90.routeLease, 'optionalAccess', _91 => _91.leaseId]), () => ( null)),
2123
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _92 => _92.routeLease, 'optionalAccess', _93 => _93.consumer]), () => ( null)),
2124
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _94 => _94.routeLease, 'optionalAccess', _95 => _95.stageId]), () => ( null)),
2075
2125
  cacheKeySource: attribution.cacheKeySource,
2076
2126
  cacheKeyInjected: attribution.cacheKeyInjected,
2077
2127
  auditResponse: attribution.auditResponse,
@@ -2088,13 +2138,13 @@ function recordResponsesNonStreamUsage(recorder, bodyText, attribution) {
2088
2138
  parsed.usage,
2089
2139
  attribution
2090
2140
  );
2091
- } catch (e21) {
2141
+ } catch (e23) {
2092
2142
  }
2093
2143
  }
2094
2144
  function recordResponsesStreamUsage(recorder, rawUsage, attribution) {
2095
2145
  try {
2096
2146
  emitResponsesUsageRecord(recorder, rawUsage, attribution);
2097
- } catch (e22) {
2147
+ } catch (e24) {
2098
2148
  }
2099
2149
  }
2100
2150
 
@@ -2115,7 +2165,7 @@ function deadlineFromAllowanceSnapshot(accountId, now) {
2115
2165
  const candidates = snapshot.windows.filter(
2116
2166
  (w) => typeof w.usedPercent === "number" && w.usedPercent >= QUOTA_EXHAUSTED_PERCENT && typeof w.resetsAt === "string"
2117
2167
  ).map((w) => ({ ms: Date.parse(w.resetsAt), usedPercent: w.usedPercent })).filter((x) => Number.isFinite(x.ms) && x.ms > now).sort((a, b) => b.usedPercent - a.usedPercent);
2118
- return _optionalChain([candidates, 'access', _60 => _60[0], 'optionalAccess', _61 => _61.ms]);
2168
+ return _optionalChain([candidates, 'access', _96 => _96[0], 'optionalAccess', _97 => _97.ms]);
2119
2169
  }
2120
2170
  function resolveCodexQuotaDeadline(accountId, bodyText, now = Date.now()) {
2121
2171
  const parsed = parseTryAgainDeadline(bodyText);
@@ -2132,13 +2182,13 @@ function markCodexUsageLimitExhaustion(accountId, bodyText, now = Date.now()) {
2132
2182
  var SERVER_OVERLOADED_CODES = /* @__PURE__ */ new Set(["server_is_overloaded", "slow_down"]);
2133
2183
  function isCodexServerOverloadEvent(event) {
2134
2184
  if (event["type"] !== "response.failed") return false;
2135
- const error = _optionalChain([event, 'access', _62 => _62["response"], 'optionalAccess', _63 => _63["error"]]);
2136
- const code = _optionalChain([error, 'optionalAccess', _64 => _64["code"]]);
2185
+ const error = _optionalChain([event, 'access', _98 => _98["response"], 'optionalAccess', _99 => _99["error"]]);
2186
+ const code = _optionalChain([error, 'optionalAccess', _100 => _100["code"]]);
2137
2187
  return typeof code === "string" && SERVER_OVERLOADED_CODES.has(code);
2138
2188
  }
2139
2189
  function isOpenAIResponsesRequest(method, url) {
2140
2190
  if (method !== "POST" || !url) return false;
2141
- const path = _nullishCoalesce(_optionalChain([url, 'access', _65 => _65.split, 'call', _66 => _66("?"), 'access', _67 => _67[0], 'optionalAccess', _68 => _68.replace, 'call', _69 => _69(/\/+$/, "")]), () => ( ""));
2191
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _101 => _101.split, 'call', _102 => _102("?"), 'access', _103 => _103[0], 'optionalAccess', _104 => _104.replace, 'call', _105 => _105(/\/+$/, "")]), () => ( ""));
2142
2192
  return path.endsWith("/responses");
2143
2193
  }
2144
2194
  var SAFE_INJECTED_CACHE_KEY_SOURCES = /* @__PURE__ */ new Set([
@@ -2168,7 +2218,7 @@ async function handleOpenAIResponsesRequest(res, rawBody, route, deps, requestHe
2168
2218
  let responsesBody;
2169
2219
  try {
2170
2220
  responsesBody = JSON.parse(rawBody);
2171
- } catch (e23) {
2221
+ } catch (e25) {
2172
2222
  writeError(res, 400, "Invalid JSON in request body");
2173
2223
  return;
2174
2224
  }
@@ -2205,8 +2255,9 @@ async function handleOpenAIResponsesRequest(res, rawBody, route, deps, requestHe
2205
2255
  const usageAttribution = {
2206
2256
  sessionId: route.sessionId,
2207
2257
  providerId: _nullishCoalesce(route.providerId, () => ( "codex")),
2208
- model: resolvedModel,
2258
+ model: _nullishCoalesce(providerResponse.actualModel, () => ( resolvedModel)),
2209
2259
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
2260
+ routeLease: route.routeLease,
2210
2261
  cacheKeySource: cacheKeyAttribution.cacheKeySource,
2211
2262
  cacheKeyInjected: cacheKeyAttribution.cacheKeyInjected,
2212
2263
  // request-audit-log: correlate this request's tokens/cost to its audit record.
@@ -2217,7 +2268,7 @@ async function handleOpenAIResponsesRequest(res, rawBody, route, deps, requestHe
2217
2268
  extractUsage(event) {
2218
2269
  if (event["type"] !== "response.completed") return null;
2219
2270
  const response = event["response"];
2220
- const usage = _optionalChain([response, 'optionalAccess', _70 => _70["usage"]]);
2271
+ const usage = _optionalChain([response, 'optionalAccess', _106 => _106["usage"]]);
2221
2272
  return _nullishCoalesce(usage, () => ( null));
2222
2273
  },
2223
2274
  onUsage(rawUsage) {
@@ -2320,7 +2371,7 @@ async function buildSubscriptionPlan3(res, route, deps, resolvedModel, isStream,
2320
2371
  writeError(res, 502, "Subscription mode requires a codex subscription profile");
2321
2372
  return null;
2322
2373
  }
2323
- const upstreamUrl = _optionalChain([profile, 'access', _71 => _71.resolveUpstreamUrl, 'optionalCall', _72 => _72(resolvedModel)]);
2374
+ const upstreamUrl = _optionalChain([profile, 'access', _107 => _107.resolveUpstreamUrl, 'optionalCall', _108 => _108(resolvedModel)]);
2324
2375
  if (!upstreamUrl) {
2325
2376
  writeError(res, 502, "Subscription profile is missing resolveUpstreamUrl");
2326
2377
  return null;
@@ -2391,6 +2442,7 @@ async function runPipeline4(responsesBody, plan) {
2391
2442
  const endpointTransformer = getResponsesEndpointTransformer();
2392
2443
  const { auth, sessionKey, chain, transformerProvider, resolvedModel, isStream, resolveUrl, upstreamUrl } = plan;
2393
2444
  let proxyAccountId;
2445
+ let actualModel = resolvedModel;
2394
2446
  let activityRecordId;
2395
2447
  const authHeaders = {};
2396
2448
  await auth.applyHeaders(authHeaders, {
@@ -2400,10 +2452,13 @@ async function runPipeline4(responsesBody, plan) {
2400
2452
  preferredAccountId: plan.preferredAccountId,
2401
2453
  preferredAccountGroup: plan.preferredAccountGroup,
2402
2454
  boundAccountFallbackPolicy: plan.boundAccountFallbackPolicy,
2403
- reportSelection: (accountId) => {
2455
+ reportSelection: (accountId, _isActive, remappedModel) => {
2404
2456
  proxyAccountId = accountId;
2457
+ if (remappedModel) actualModel = remappedModel;
2405
2458
  }
2406
2459
  });
2460
+ responsesBody.model = actualModel;
2461
+ transformerProvider.models = [actualModel];
2407
2462
  let rawStatus = null;
2408
2463
  const { response } = await _chunkMUHWHQVMcjs.executeProviderCall.call(void 0, {
2409
2464
  executor,
@@ -2427,7 +2482,7 @@ async function runPipeline4(responsesBody, plan) {
2427
2482
  return headers;
2428
2483
  },
2429
2484
  fetchFn: (url, headers, body) => {
2430
- console.log(`[ProviderProxy:responses] -> ${url} model=${resolvedModel} stream=${isStream}`);
2485
+ console.log(`[ProviderProxy:responses] -> ${url} model=${actualModel} stream=${isStream}`);
2431
2486
  return _chunkRJUONLUQcjs.fetchUpstream.call(void 0,
2432
2487
  url,
2433
2488
  { method: "POST", headers, body: JSON.stringify(body) },
@@ -2438,7 +2493,7 @@ async function runPipeline4(responsesBody, plan) {
2438
2493
  endpoint: "responses",
2439
2494
  sessionKey,
2440
2495
  sessionSource: _nullishCoalesce(plan.sessionSource, () => ( "none")),
2441
- model: resolvedModel,
2496
+ model: actualModel,
2442
2497
  onRecorded: (record) => {
2443
2498
  activityRecordId = record.id;
2444
2499
  }
@@ -2451,12 +2506,12 @@ async function runPipeline4(responsesBody, plan) {
2451
2506
  },
2452
2507
  runResponseChain: true
2453
2508
  });
2454
- return { response, rawStatus, accountId: proxyAccountId, activityRecordId };
2509
+ return { response, rawStatus, accountId: proxyAccountId, actualModel, activityRecordId };
2455
2510
  }
2456
2511
  async function runPipelineWithPoolReporting4(responsesBody, plan) {
2457
2512
  const first = await runPipeline4(responsesBody, plan);
2458
- const outcome = await _optionalChain([plan, 'access', _73 => _73.auth, 'access', _74 => _74.onResult, 'optionalCall', _75 => _75(first.rawStatus)]);
2459
- if (_optionalChain([outcome, 'optionalAccess', _76 => _76.rebound])) {
2513
+ const outcome = await _optionalChain([plan, 'access', _109 => _109.auth, 'access', _110 => _110.onResult, 'optionalCall', _111 => _111(first.rawStatus)]);
2514
+ if (_optionalChain([outcome, 'optionalAccess', _112 => _112.rebound])) {
2460
2515
  console.log(
2461
2516
  "[ProviderProxy:responses] pool re-bound key after status",
2462
2517
  first.rawStatus,
@@ -2469,7 +2524,7 @@ async function runPipelineWithPoolReporting4(responsesBody, plan) {
2469
2524
  async function runPipelineWithSubscriptionRetry3(responsesBody, plan) {
2470
2525
  const first = await runPipeline4(responsesBody, plan);
2471
2526
  if (first.rawStatus !== 401) return first;
2472
- const refreshed = await _optionalChain([plan, 'access', _77 => _77.auth, 'access', _78 => _78.onUnauthorized, 'optionalCall', _79 => _79(plan.sessionKey)]);
2527
+ const refreshed = await _optionalChain([plan, 'access', _113 => _113.auth, 'access', _114 => _114.onUnauthorized, 'optionalCall', _115 => _115(plan.sessionKey)]);
2473
2528
  if (!refreshed) {
2474
2529
  console.warn("[ProviderProxy:responses] 401 not recoverable (onUnauthorized returned false)");
2475
2530
  return first;
@@ -2507,7 +2562,7 @@ function resolveRouteToken(req) {
2507
2562
  if (fromAuth) return fromAuth;
2508
2563
  const googKey = req.headers["x-goog-api-key"];
2509
2564
  const value = Array.isArray(googKey) ? googKey[0] : googKey;
2510
- return _optionalChain([value, 'optionalAccess', _80 => _80.trim, 'call', _81 => _81()]) || void 0;
2565
+ return _optionalChain([value, 'optionalAccess', _116 => _116.trim, 'call', _117 => _117()]) || void 0;
2511
2566
  }
2512
2567
  async function routeRequest(req, res, routes, deps) {
2513
2568
  const token = resolveRouteToken(req);
@@ -2547,7 +2602,7 @@ function isLoopbackAddress(addr) {
2547
2602
  return normalized === "127.0.0.1" || normalized.startsWith("127.") || normalized === "::1" || addr === "::1";
2548
2603
  }
2549
2604
  var ProviderProxy = (_class2 = class {
2550
- constructor(deps, routes) {;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);
2605
+ constructor(deps, routes) {;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);
2551
2606
  this.deps = deps;
2552
2607
  this.routes = _nullishCoalesce(routes, () => ( new ProviderProxyRouteMap()));
2553
2608
  }
@@ -2555,6 +2610,7 @@ var ProviderProxy = (_class2 = class {
2555
2610
  __init2() {this.server = null}
2556
2611
  __init3() {this.port = 0}
2557
2612
 
2613
+ __init4() {this.beforeStop = /* @__PURE__ */ new Set()}
2558
2614
  /**
2559
2615
  * Start the resident listener on a stable port on 127.0.0.1. Idempotent —
2560
2616
  * a second `start()` returns the already-bound port.
@@ -2595,6 +2651,12 @@ var ProviderProxy = (_class2 = class {
2595
2651
  }
2596
2652
  /** Stop the listener, clear all routes, and release the port. */
2597
2653
  async stop() {
2654
+ for (const cleanup of [...this.beforeStop]) {
2655
+ try {
2656
+ await cleanup();
2657
+ } catch (e26) {
2658
+ }
2659
+ }
2598
2660
  this.routes.clear();
2599
2661
  const server = this.server;
2600
2662
  if (!server) return;
@@ -2611,6 +2673,15 @@ var ProviderProxy = (_class2 = class {
2611
2673
  getBaseUrl() {
2612
2674
  return `http://127.0.0.1:${this.port}`;
2613
2675
  }
2676
+ /** Register daemon-owned cleanup that must run before route-map teardown. */
2677
+ registerBeforeStop(cleanup) {
2678
+ this.beforeStop.add(cleanup);
2679
+ return () => this.beforeStop.delete(cleanup);
2680
+ }
2681
+ /** Whether the resident listener has completed binding. */
2682
+ isReady() {
2683
+ return this.server !== null && this.port > 0;
2684
+ }
2614
2685
  /**
2615
2686
  * The SHARED route map. Exposed so the outbound API server
2616
2687
  * (`outbound-api-server`) can mint per-request routes on the SAME map and
@@ -2627,19 +2698,819 @@ var ProviderProxy = (_class2 = class {
2627
2698
  getDeps() {
2628
2699
  return this.deps;
2629
2700
  }
2630
- /** Register a route for one run; returns the crypto route token (task 2.2). */
2631
- addRoute(context, idleMs) {
2632
- return this.routes.addRoute(context, idleMs);
2701
+ /** Register a route for one run; returns the crypto route token (task 2.2). */
2702
+ addRoute(context, idleMsOrOptions) {
2703
+ return this.routes.addRoute(context, idleMsOrOptions);
2704
+ }
2705
+ /** Re-arm one route while retaining the exact same bearer token. */
2706
+ renewRoute(token, idleMs) {
2707
+ return this.routes.renewRoute(token, idleMs);
2708
+ }
2709
+ /** Remove a route at run end. Returns true if an entry existed. */
2710
+ removeRoute(token) {
2711
+ return this.routes.removeRoute(token);
2712
+ }
2713
+ /** Live-route count (diagnostics / tests). */
2714
+ routeCount() {
2715
+ return this.routes.size();
2716
+ }
2717
+ }, _class2);
2718
+
2719
+ // src/provider-proxy/RouteLeaseManager.ts
2720
+
2721
+
2722
+ // src/provider-proxy/routeLeaseSchema.ts
2723
+
2724
+ var ROUTE_LEASE_REQUEST_SCHEMA = "omnicross.route-lease.request/1";
2725
+ var ROUTE_LEASE_RESULT_SCHEMA = "omnicross.route-lease/1";
2726
+ var ROUTE_LEASE_CAPABILITIES_SCHEMA = "omnicross.route-lease.capabilities/1";
2727
+ var ROUTE_LEASE_API_VERSION = 1;
2728
+ var ROUTE_LEASE_DEFAULT_TTL_SECONDS = 600;
2729
+ var ROUTE_LEASE_MAX_TTL_SECONDS = 3600;
2730
+ var ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES = 256;
2731
+ var ROUTE_LEASE_MAX_CONSUMER_BYTES = 64;
2732
+ var ROUTE_LEASE_MAX_EXECUTION_ID_BYTES = 128;
2733
+ var ROUTE_LEASE_MAX_SESSION_ID_BYTES = 512;
2734
+ var ROUTE_LEASE_MAX_MODEL_BYTES = 256;
2735
+ var ROUTE_LEASE_SESSION_HASH_DOMAIN = "omnicross.route-lease.session/v1";
2736
+ var ROUTE_LEASE_CODEX_TOKEN_ENV = "OMNICROSS_CODEX_ROUTE_TOKEN";
2737
+ var ROUTE_LEASE_RUNTIMES = ["claude", "codex"];
2738
+ var ROUTE_LEASE_CAPABILITIES = Object.freeze({
2739
+ schemaVersion: ROUTE_LEASE_CAPABILITIES_SCHEMA,
2740
+ runtimes: Object.freeze(["claude", "codex"]),
2741
+ upstreamKinds: Object.freeze(["provider", "account", "account-group", "account-pool"]),
2742
+ leaseApiVersion: ROUTE_LEASE_API_VERSION,
2743
+ codexAuthMode: "env_key",
2744
+ maxTtlSeconds: ROUTE_LEASE_MAX_TTL_SECONDS
2745
+ });
2746
+ var ROUTE_LEASE_RUNTIME_TABLE = Object.freeze({
2747
+ claude: Object.freeze({ endpoint: "messages", ingressFormat: "anthropic-messages", wirePath: "/v1/messages" }),
2748
+ codex: Object.freeze({ endpoint: "responses", ingressFormat: "openai-responses", wirePath: "/openai/responses" })
2749
+ });
2750
+ var ERROR_DEFAULTS = {
2751
+ invalid_request: { status: 400, retryable: false },
2752
+ runtime_unsupported: { status: 400, retryable: false },
2753
+ model_not_configured: { status: 400, retryable: false },
2754
+ format_unsupported: { status: 400, retryable: false },
2755
+ control_unauthorized: { status: 403, retryable: false },
2756
+ upstream_not_found: { status: 404, retryable: false },
2757
+ lease_not_found: { status: 404, retryable: false },
2758
+ idempotency_conflict: { status: 409, retryable: false },
2759
+ upstream_unavailable: { status: 409, retryable: true },
2760
+ lease_expired: { status: 410, retryable: false },
2761
+ upstream_exhausted: { status: 429, retryable: true },
2762
+ daemon_not_ready: { status: 503, retryable: true }
2763
+ };
2764
+ var RouteLeaseError = (_class3 = class extends Error {
2765
+ constructor(code, message, options = {}) {
2766
+ super(message, options.cause === void 0 ? void 0 : { cause: options.cause });_class3.prototype.__init5.call(this);;
2767
+ this.code = code;
2768
+ this.name = "RouteLeaseError";
2769
+ const defaults = ERROR_DEFAULTS[code];
2770
+ this.status = _nullishCoalesce(options.status, () => ( defaults.status));
2771
+ this.retryable = _nullishCoalesce(options.retryable, () => ( defaults.retryable));
2772
+ if (options.retryAfterSeconds !== void 0) {
2773
+ this.retryAfterSeconds = Math.max(1, Math.min(3600, Math.floor(options.retryAfterSeconds)));
2774
+ }
2775
+ }
2776
+
2777
+ __init5() {this.type = "route_lease_error"}
2778
+
2779
+
2780
+
2781
+ toResponse() {
2782
+ return { error: { type: this.type, code: this.code, message: this.message, retryable: this.retryable } };
2783
+ }
2784
+ }, _class3);
2785
+ var IDEMPOTENCY_RE = /^[A-Za-z0-9._:-]+$/u;
2786
+ var CONTROL_RE = /[\u0000-\u001f\u007f-\u009f]/u;
2787
+ var PROCESS_SESSION_HMAC_KEY = _crypto.randomBytes.call(void 0, 32);
2788
+ function isRecord(value) {
2789
+ return value !== null && typeof value === "object" && !Array.isArray(value);
2790
+ }
2791
+ function byteLength(value) {
2792
+ return Buffer.byteLength(value, "utf8");
2793
+ }
2794
+ function normalizedString(value, field, maxBytes, options = {}) {
2795
+ if (typeof value !== "string") throw invalid(`${field} must be a string`);
2796
+ const normalized = value.trim();
2797
+ if (!normalized || byteLength(normalized) > maxBytes || CONTROL_RE.test(normalized)) {
2798
+ throw invalid(`${field} is invalid`);
2799
+ }
2800
+ if (options.asciiPattern && !options.asciiPattern.test(normalized)) {
2801
+ throw invalid(`${field} is invalid`);
2802
+ }
2803
+ return normalized;
2804
+ }
2805
+ function optionalExecutionId(value, field) {
2806
+ return value === void 0 ? void 0 : normalizedString(value, field, ROUTE_LEASE_MAX_EXECUTION_ID_BYTES);
2807
+ }
2808
+ function invalid(message) {
2809
+ return new RouteLeaseError("invalid_request", message);
2810
+ }
2811
+ function normalizeRouteLeaseIdempotencyKey(value) {
2812
+ return normalizedString(value, "Idempotency-Key", ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES, {
2813
+ asciiPattern: IDEMPOTENCY_RE
2814
+ });
2815
+ }
2816
+ function normalizeRouteLeaseTtl(value) {
2817
+ if (value === void 0) return ROUTE_LEASE_DEFAULT_TTL_SECONDS;
2818
+ if (!Number.isInteger(value) || value < 1 || value > ROUTE_LEASE_MAX_TTL_SECONDS) {
2819
+ throw invalid(`ttlSeconds must be an integer from 1 to ${ROUTE_LEASE_MAX_TTL_SECONDS}`);
2820
+ }
2821
+ return value;
2822
+ }
2823
+ function normalizeUpstream(value) {
2824
+ if (!isRecord(value) || typeof value.kind !== "string") throw invalid("upstream is invalid");
2825
+ const providerId = normalizedString(value.providerId, "upstream.providerId", 128);
2826
+ switch (value.kind) {
2827
+ case "provider": {
2828
+ const keyId = value.keyId === void 0 ? void 0 : normalizedString(value.keyId, "upstream.keyId", 128);
2829
+ return keyId ? { kind: "provider", providerId, keyId } : { kind: "provider", providerId };
2830
+ }
2831
+ case "account":
2832
+ return {
2833
+ kind: "account",
2834
+ providerId,
2835
+ accountId: normalizedString(value.accountId, "upstream.accountId", 128)
2836
+ };
2837
+ case "account-group":
2838
+ return {
2839
+ kind: "account-group",
2840
+ providerId,
2841
+ group: normalizedString(value.group, "upstream.group", 128)
2842
+ };
2843
+ case "account-pool":
2844
+ return { kind: "account-pool", providerId };
2845
+ default:
2846
+ throw invalid("upstream kind is unsupported");
2847
+ }
2848
+ }
2849
+ function hashRouteLeaseSessionId(sessionId, hmacKey = PROCESS_SESSION_HMAC_KEY) {
2850
+ return _crypto.createHmac.call(void 0, "sha256", hmacKey).update(ROUTE_LEASE_SESSION_HASH_DOMAIN, "utf8").update("\0", "utf8").update(sessionId, "utf8").digest("hex");
2851
+ }
2852
+ function canonicalizeRouteLeasePayload(value) {
2853
+ if (value === null || typeof value !== "object") return JSON.stringify(value);
2854
+ if (Array.isArray(value)) {
2855
+ return `[${value.map((entry) => canonicalizeRouteLeasePayload(entry)).join(",")}]`;
2856
+ }
2857
+ const record = value;
2858
+ const fields = Object.keys(record).filter((key) => record[key] !== void 0).sort().map((key) => `${JSON.stringify(key)}:${canonicalizeRouteLeasePayload(record[key])}`);
2859
+ return `{${fields.join(",")}}`;
2860
+ }
2861
+ function hashRouteLeasePayload(value) {
2862
+ return _crypto.createHash.call(void 0, "sha256").update(canonicalizeRouteLeasePayload(value), "utf8").digest("hex");
2863
+ }
2864
+ function parseRouteLeaseCreate(raw, idempotencyHeader, hmacKey = PROCESS_SESSION_HMAC_KEY) {
2865
+ if (!isRecord(raw)) throw invalid("request body must be a JSON object");
2866
+ if (raw.schemaVersion !== ROUTE_LEASE_REQUEST_SCHEMA) {
2867
+ throw invalid(`schemaVersion must be ${ROUTE_LEASE_REQUEST_SCHEMA}`);
2868
+ }
2869
+ const consumer = normalizedString(raw.consumer, "consumer", ROUTE_LEASE_MAX_CONSUMER_BYTES);
2870
+ const runtime = raw.runtime;
2871
+ if (runtime !== "claude" && runtime !== "codex") {
2872
+ throw new RouteLeaseError("runtime_unsupported", "runtime is unsupported");
2873
+ }
2874
+ const upstream = normalizeUpstream(raw.upstream);
2875
+ if (typeof raw.model !== "string" || raw.model.trim().length === 0) {
2876
+ throw new RouteLeaseError("model_not_configured", "model is not configured");
2877
+ }
2878
+ const model = normalizedString(raw.model, "model", ROUTE_LEASE_MAX_MODEL_BYTES);
2879
+ const ttlSeconds = normalizeRouteLeaseTtl(raw.ttlSeconds);
2880
+ const idempotencyKey = normalizeRouteLeaseIdempotencyKey(idempotencyHeader);
2881
+ let execution;
2882
+ let semanticExecution;
2883
+ if (raw.execution !== void 0) {
2884
+ if (!isRecord(raw.execution)) throw invalid("execution must be a JSON object");
2885
+ const runId = optionalExecutionId(raw.execution.runId, "execution.runId");
2886
+ const stageId = optionalExecutionId(raw.execution.stageId, "execution.stageId");
2887
+ let attempt;
2888
+ if (raw.execution.attempt !== void 0) {
2889
+ if (!Number.isSafeInteger(raw.execution.attempt) || raw.execution.attempt < 1) {
2890
+ throw invalid("execution.attempt must be a positive integer");
2891
+ }
2892
+ attempt = raw.execution.attempt;
2893
+ }
2894
+ let sessionId;
2895
+ let sessionIdHash;
2896
+ if (raw.execution.sessionId !== void 0) {
2897
+ sessionId = normalizedString(raw.execution.sessionId, "execution.sessionId", ROUTE_LEASE_MAX_SESSION_ID_BYTES);
2898
+ sessionIdHash = hashRouteLeaseSessionId(sessionId, hmacKey);
2899
+ }
2900
+ if (runId !== void 0 || stageId !== void 0 || attempt !== void 0 || sessionIdHash !== void 0) {
2901
+ execution = { runId, stageId, attempt, sessionIdHash };
2902
+ semanticExecution = { runId, stageId, attempt, sessionId };
2903
+ }
2904
+ }
2905
+ const request = {
2906
+ schemaVersion: ROUTE_LEASE_REQUEST_SCHEMA,
2907
+ consumer,
2908
+ runtime,
2909
+ upstream,
2910
+ model,
2911
+ ...execution ? { execution } : {},
2912
+ ttlSeconds
2913
+ };
2914
+ const semanticPayload = {
2915
+ schemaVersion: request.schemaVersion,
2916
+ consumer,
2917
+ runtime,
2918
+ upstream,
2919
+ model,
2920
+ ...semanticExecution ? { execution: semanticExecution } : {},
2921
+ ttlSeconds
2922
+ };
2923
+ return { request, idempotencyKey, payloadHash: hashRouteLeasePayload(semanticPayload) };
2924
+ }
2925
+ function routeLeaseRuntime(runtime) {
2926
+ switch (runtime) {
2927
+ case "claude":
2928
+ return ROUTE_LEASE_RUNTIME_TABLE.claude;
2929
+ case "codex":
2930
+ return ROUTE_LEASE_RUNTIME_TABLE.codex;
2931
+ default: {
2932
+ const exhaustive = runtime;
2933
+ throw new RouteLeaseError("runtime_unsupported", `runtime is unsupported: ${String(exhaustive)}`);
2934
+ }
2935
+ }
2936
+ }
2937
+
2938
+ // src/provider-proxy/RouteLeaseManager.ts
2939
+ var SYSTEM_ROUTE_LEASE_CLOCK = {
2940
+ now: () => Date.now(),
2941
+ setTimeout(callback, delayMs) {
2942
+ const timer = setTimeout(callback, delayMs);
2943
+ _optionalChain([timer, 'access', _118 => _118.unref, 'optionalCall', _119 => _119()]);
2944
+ return timer;
2945
+ },
2946
+ clearTimeout(handle) {
2947
+ clearTimeout(handle);
2948
+ }
2949
+ };
2950
+ function cloneUpstream(upstream) {
2951
+ return { ...upstream };
2952
+ }
2953
+ function metadataCopy(metadata) {
2954
+ return {
2955
+ ...metadata,
2956
+ upstream: cloneUpstream(metadata.upstream),
2957
+ ...metadata.execution ? { execution: { ...metadata.execution } } : {}
2958
+ };
2959
+ }
2960
+ var RouteLeaseManager = (_class4 = class {
2961
+ constructor(routePort, targetResolver, descriptors, options = {}) {;_class4.prototype.__init6.call(this);_class4.prototype.__init7.call(this);_class4.prototype.__init8.call(this);_class4.prototype.__init9.call(this);_class4.prototype.__init10.call(this);
2962
+ this.routePort = routePort;
2963
+ this.targetResolver = targetResolver;
2964
+ this.descriptors = descriptors;
2965
+ this.clock = _nullishCoalesce(options.clock, () => ( SYSTEM_ROUTE_LEASE_CLOCK));
2966
+ this.hmacKey = _nullishCoalesce(options.hmacKey, () => ( _crypto.randomBytes.call(void 0, 32)));
2967
+ this.tombstoneMaxCount = Math.max(1, _nullishCoalesce(options.tombstoneMaxCount, () => ( 512)));
2968
+ this.tombstoneMaxAgeMs = Math.max(1, _nullishCoalesce(options.tombstoneMaxAgeMs, () => ( 60 * 60 * 1e3)));
2969
+ this.logger = options.logger;
2970
+ this.afterPublish = options.afterPublish;
2971
+ }
2972
+
2973
+
2974
+
2975
+ __init6() {this.active = /* @__PURE__ */ new Map()}
2976
+ __init7() {this.idempotency = /* @__PURE__ */ new Map()}
2977
+ __init8() {this.pendingCreates = /* @__PURE__ */ new Map()}
2978
+ __init9() {this.tombstones = /* @__PURE__ */ new Map()}
2979
+
2980
+
2981
+
2982
+
2983
+
2984
+
2985
+ __init10() {this.closed = false}
2986
+ parse(raw, idempotencyKey) {
2987
+ return parseRouteLeaseCreate(raw, idempotencyKey, this.hmacKey);
2988
+ }
2989
+ async createFromRequest(raw, idempotencyKey) {
2990
+ return this.create(this.parse(raw, idempotencyKey));
2991
+ }
2992
+ async create(input) {
2993
+ if (this.closed) throw new RouteLeaseError("daemon_not_ready", "route lease manager is shutting down");
2994
+ const request = input.request;
2995
+ const scope = `${request.consumer}\0${input.idempotencyKey}`;
2996
+ const existing = this.idempotency.get(scope);
2997
+ if (existing) {
2998
+ const lease = this.active.get(existing.leaseId);
2999
+ if (!lease) {
3000
+ this.idempotency.delete(scope);
3001
+ } else if (existing.payloadHash !== input.payloadHash) {
3002
+ throw new RouteLeaseError("idempotency_conflict", "idempotency key is already used for a different live request");
3003
+ } else {
3004
+ return { created: false, result: lease.result };
3005
+ }
3006
+ }
3007
+ const pending = this.pendingCreates.get(scope);
3008
+ if (pending) {
3009
+ if (pending.payloadHash !== input.payloadHash) {
3010
+ throw new RouteLeaseError("idempotency_conflict", "idempotency key is already used for a different live request");
3011
+ }
3012
+ const outcome2 = await pending.outcome;
3013
+ return { created: false, result: outcome2.result };
3014
+ }
3015
+ const outcome = this.createNew(input, scope);
3016
+ const pendingCreate = { payloadHash: input.payloadHash, outcome };
3017
+ this.pendingCreates.set(scope, pendingCreate);
3018
+ try {
3019
+ return await outcome;
3020
+ } finally {
3021
+ if (this.pendingCreates.get(scope) === pendingCreate) this.pendingCreates.delete(scope);
3022
+ }
3023
+ }
3024
+ async createNew(input, scope) {
3025
+ const request = input.request;
3026
+ if (!this.routePort.isReady()) {
3027
+ throw new RouteLeaseError("daemon_not_ready", "resident provider proxy is not ready");
3028
+ }
3029
+ if (!this.descriptors.has(request.runtime)) {
3030
+ throw new RouteLeaseError("runtime_unsupported", "runtime launch adapter is unavailable");
3031
+ }
3032
+ const leaseId = _crypto.randomUUID.call(void 0, );
3033
+ const resolved = await this.targetResolver.resolve(request);
3034
+ this.assertOpen();
3035
+ const route = {
3036
+ ...resolved,
3037
+ sessionId: _nullishCoalesce(_optionalChain([request, 'access', _120 => _120.execution, 'optionalAccess', _121 => _121.sessionIdHash]), () => ( null)),
3038
+ routeLease: {
3039
+ leaseId,
3040
+ consumer: request.consumer,
3041
+ ..._optionalChain([request, 'access', _122 => _122.execution, 'optionalAccess', _123 => _123.runId]) ? { runId: request.execution.runId } : {},
3042
+ ..._optionalChain([request, 'access', _124 => _124.execution, 'optionalAccess', _125 => _125.stageId]) ? { stageId: request.execution.stageId } : {}
3043
+ }
3044
+ };
3045
+ const now = this.clock.now();
3046
+ const ttlMs = request.ttlSeconds * 1e3;
3047
+ const expiresAtMs = now + ttlMs;
3048
+ let routeToken;
3049
+ let published = false;
3050
+ let activeLease;
3051
+ try {
3052
+ routeToken = this.routePort.addRoute(route, {
3053
+ idleMs: ttlMs,
3054
+ onActivity: (at) => this.noteActivity(leaseId, at),
3055
+ onEvicted: () => this.onRouteEvicted(leaseId)
3056
+ });
3057
+ const launch = this.descriptors.build(request.runtime, {
3058
+ proxyBaseUrl: this.routePort.getBaseUrl(),
3059
+ model: request.model,
3060
+ routeToken
3061
+ });
3062
+ this.assertOpen();
3063
+ const metadata = {
3064
+ leaseId,
3065
+ consumer: request.consumer,
3066
+ runtime: request.runtime,
3067
+ upstream: cloneUpstream(request.upstream),
3068
+ model: request.model,
3069
+ createdAt: new Date(now).toISOString(),
3070
+ expiresAt: new Date(expiresAtMs).toISOString(),
3071
+ status: "active",
3072
+ ...request.execution ? { execution: { ...request.execution } } : {}
3073
+ };
3074
+ const result = {
3075
+ schemaVersion: ROUTE_LEASE_RESULT_SCHEMA,
3076
+ ...metadata,
3077
+ status: "active",
3078
+ launch: { env: { ...launch.env }, extraArgs: [...launch.extraArgs] }
3079
+ };
3080
+ const timer = this.clock.setTimeout(() => this.expire(leaseId), ttlMs);
3081
+ activeLease = {
3082
+ leaseId,
3083
+ routeToken,
3084
+ idempotencyScope: scope,
3085
+ payloadHash: input.payloadHash,
3086
+ request,
3087
+ route,
3088
+ result,
3089
+ metadata,
3090
+ expiresAtMs,
3091
+ timer
3092
+ };
3093
+ this.assertOpen();
3094
+ this.active.set(leaseId, activeLease);
3095
+ this.idempotency.set(scope, { leaseId, payloadHash: input.payloadHash });
3096
+ published = true;
3097
+ _optionalChain([this, 'access', _126 => _126.afterPublish, 'optionalCall', _127 => _127()]);
3098
+ if (this.closed) {
3099
+ this.finalize(activeLease, "released");
3100
+ throw new RouteLeaseError("daemon_not_ready", "route lease manager is shutting down");
3101
+ }
3102
+ return { created: true, result };
3103
+ } catch (error) {
3104
+ if (published && activeLease) {
3105
+ this.active.delete(leaseId);
3106
+ this.idempotency.delete(scope);
3107
+ this.clock.clearTimeout(activeLease.timer);
3108
+ }
3109
+ if (routeToken) this.safeRemoveRoute(routeToken, leaseId);
3110
+ if (error instanceof RouteLeaseError) throw error;
3111
+ throw new RouteLeaseError("upstream_unavailable", "route lease creation failed safely", { cause: error });
3112
+ }
3113
+ }
3114
+ list() {
3115
+ this.pruneTombstones();
3116
+ return [
3117
+ ...[...this.active.values()].map((lease) => metadataCopy(lease.metadata)),
3118
+ ...[...this.tombstones.values()].map((entry) => metadataCopy(entry.metadata))
3119
+ ].sort((left, right) => left.createdAt.localeCompare(right.createdAt));
3120
+ }
3121
+ get(leaseId) {
3122
+ const active = this.active.get(leaseId);
3123
+ if (active) return metadataCopy(active.metadata);
3124
+ this.pruneTombstones();
3125
+ const tombstone = this.tombstones.get(leaseId);
3126
+ if (tombstone) return metadataCopy(tombstone.metadata);
3127
+ throw new RouteLeaseError("lease_not_found", "route lease was not found");
3128
+ }
3129
+ renew(leaseId, ttlSeconds) {
3130
+ const lease = this.active.get(leaseId);
3131
+ if (!lease) {
3132
+ this.pruneTombstones();
3133
+ const known = this.tombstones.get(leaseId);
3134
+ if (_optionalChain([known, 'optionalAccess', _128 => _128.metadata, 'access', _129 => _129.status]) === "expired") {
3135
+ throw new RouteLeaseError("lease_expired", "route lease has expired");
3136
+ }
3137
+ throw new RouteLeaseError("lease_not_found", "route lease was not found");
3138
+ }
3139
+ const now = this.clock.now();
3140
+ if (now >= lease.expiresAtMs) {
3141
+ this.finalize(lease, "expired");
3142
+ throw new RouteLeaseError("lease_expired", "route lease has expired");
3143
+ }
3144
+ const ttlMs = ttlSeconds * 1e3;
3145
+ const expiresAtMs = now + ttlMs;
3146
+ const nextTimer = this.clock.setTimeout(() => this.expire(leaseId), ttlMs);
3147
+ const previousTimer = lease.timer;
3148
+ lease.timer = nextTimer;
3149
+ lease.expiresAtMs = expiresAtMs;
3150
+ lease.metadata = { ...lease.metadata, expiresAt: new Date(expiresAtMs).toISOString() };
3151
+ this.clock.clearTimeout(previousTimer);
3152
+ if (!this.routePort.renewRoute(lease.routeToken, ttlMs)) {
3153
+ this.finalize(lease, "expired", true);
3154
+ throw new RouteLeaseError("lease_expired", "route lease route has expired");
3155
+ }
3156
+ return { leaseId, expiresAt: lease.metadata.expiresAt, status: "active" };
3157
+ }
3158
+ release(leaseId) {
3159
+ const lease = this.active.get(leaseId);
3160
+ if (!lease) return { leaseId, released: false };
3161
+ this.finalize(lease, "released");
3162
+ return { leaseId, released: true };
3163
+ }
3164
+ shutdown() {
3165
+ if (this.closed) return;
3166
+ this.closed = true;
3167
+ for (const lease of [...this.active.values()]) {
3168
+ try {
3169
+ this.finalize(lease, "released");
3170
+ } catch (error) {
3171
+ _optionalChain([this, 'access', _130 => _130.logger, 'optionalAccess', _131 => _131.warn, 'call', _132 => _132("route lease cleanup failed during shutdown", { leaseId: lease.leaseId, error: safeErrorName(error) })]);
3172
+ }
3173
+ }
3174
+ }
3175
+ activeCount() {
3176
+ return this.active.size;
3177
+ }
3178
+ assertOpen() {
3179
+ if (this.closed) {
3180
+ throw new RouteLeaseError("daemon_not_ready", "route lease manager is shutting down");
3181
+ }
3182
+ }
3183
+ noteActivity(leaseId, at) {
3184
+ const lease = this.active.get(leaseId);
3185
+ if (!lease || at >= lease.expiresAtMs) return;
3186
+ lease.metadata = { ...lease.metadata, lastActivityAt: new Date(at).toISOString() };
3187
+ }
3188
+ onRouteEvicted(leaseId) {
3189
+ const lease = this.active.get(leaseId);
3190
+ if (lease) this.finalize(lease, "expired", true);
3191
+ }
3192
+ expire(leaseId) {
3193
+ const lease = this.active.get(leaseId);
3194
+ if (!lease) return;
3195
+ const remaining = lease.expiresAtMs - this.clock.now();
3196
+ if (remaining > 0) {
3197
+ lease.timer = this.clock.setTimeout(() => this.expire(leaseId), remaining);
3198
+ return;
3199
+ }
3200
+ this.finalize(lease, "expired");
3201
+ }
3202
+ finalize(lease, status, routeAlreadyGone = false) {
3203
+ if (this.active.get(lease.leaseId) !== lease) return;
3204
+ this.active.delete(lease.leaseId);
3205
+ this.idempotency.delete(lease.idempotencyScope);
3206
+ this.clock.clearTimeout(lease.timer);
3207
+ if (!routeAlreadyGone) this.safeRemoveRoute(lease.routeToken, lease.leaseId);
3208
+ const metadata = { ...lease.metadata, status };
3209
+ this.tombstones.delete(lease.leaseId);
3210
+ this.tombstones.set(lease.leaseId, { metadata, terminalAtMs: this.clock.now() });
3211
+ this.pruneTombstones();
3212
+ }
3213
+ safeRemoveRoute(routeToken, leaseId) {
3214
+ try {
3215
+ this.routePort.removeRoute(routeToken);
3216
+ } catch (error) {
3217
+ _optionalChain([this, 'access', _133 => _133.logger, 'optionalAccess', _134 => _134.warn, 'call', _135 => _135("route lease route cleanup failed", { leaseId, error: safeErrorName(error) })]);
3218
+ }
3219
+ }
3220
+ pruneTombstones() {
3221
+ const cutoff = this.clock.now() - this.tombstoneMaxAgeMs;
3222
+ for (const [leaseId, tombstone] of this.tombstones) {
3223
+ if (tombstone.terminalAtMs < cutoff) this.tombstones.delete(leaseId);
3224
+ }
3225
+ while (this.tombstones.size > this.tombstoneMaxCount) {
3226
+ const oldest = this.tombstones.keys().next().value;
3227
+ if (!oldest) break;
3228
+ this.tombstones.delete(oldest);
3229
+ }
3230
+ }
3231
+ }, _class4);
3232
+ function safeErrorName(error) {
3233
+ return error instanceof Error ? error.name : "unknown";
3234
+ }
3235
+
3236
+ // src/outbound-api/gatewayBindingResolver.ts
3237
+ var MESSAGE_FALLBACK_KINDS = ["sonnet", "opus", "haiku", "fable"];
3238
+ function nonBlank(value) {
3239
+ return typeof value === "string" && value.trim() !== "";
3240
+ }
3241
+ function gatewayBindingAllowsKey(binding, apiKeyId) {
3242
+ const scope = _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _136 => _136.apiKeyIds, 'optionalAccess', _137 => _137.length]) ? "selected" : "all")));
3243
+ return scope === "all" || Boolean(_optionalChain([binding, 'access', _138 => _138.apiKeyIds, 'optionalAccess', _139 => _139.includes, 'call', _140 => _140(apiKeyId)]));
3244
+ }
3245
+ function wildcardMatches(pattern, value) {
3246
+ const escaped = pattern.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")).join(".*");
3247
+ return new RegExp(`^${escaped}$`, "iu").test(value);
3248
+ }
3249
+ function resolveGatewayModelMapping(mappings, requestedModel) {
3250
+ if (!nonBlank(requestedModel)) return void 0;
3251
+ const wanted = requestedModel.trim();
3252
+ const exact = (_nullishCoalesce(mappings, () => ( []))).find(
3253
+ (mapping) => nonBlank(mapping.source) && !mapping.source.includes("*") && mapping.source.trim().toLocaleLowerCase() === wanted.toLocaleLowerCase()
3254
+ );
3255
+ if (exact && nonBlank(exact.target)) return exact.target.trim();
3256
+ const wildcard = (_nullishCoalesce(mappings, () => ( []))).find(
3257
+ (mapping) => nonBlank(mapping.source) && mapping.source.includes("*") && wildcardMatches(mapping.source.trim(), wanted)
3258
+ );
3259
+ return wildcard && nonBlank(wildcard.target) ? wildcard.target.trim() : void 0;
3260
+ }
3261
+ function routeCanServe(binding, requestedModel, role) {
3262
+ if (binding.modelMode === "passthrough") return nonBlank(requestedModel);
3263
+ if (_optionalChain([binding, 'access', _141 => _141.modelMappings, 'optionalAccess', _142 => _142.length])) {
3264
+ return nonBlank(resolveGatewayModelMapping(binding.modelMappings, requestedModel));
3265
+ }
3266
+ if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3267
+ const map = _nullishCoalesce(binding.modelMap, () => ( {}));
3268
+ const kind = _chunk4VZX5F4Tcjs.detectModelKind.call(void 0, binding.endpoint, requestedModel);
3269
+ if (kind && nonBlank(map[kind])) return true;
3270
+ if (binding.endpoint === "messages") {
3271
+ return MESSAGE_FALLBACK_KINDS.some((candidate) => nonBlank(map[candidate]));
3272
+ }
3273
+ return nonBlank(map.codex);
3274
+ }
3275
+ if (binding.endpoint === "chat" && binding.dispatchMode === "prefix") {
3276
+ return _chunk4VZX5F4Tcjs.resolvePrefixTarget.call(void 0, binding.prefixTargets, requestedModel) !== null;
3277
+ }
3278
+ if (binding.endpoint === "chat") {
3279
+ if (!requestedModel) return false;
3280
+ const wanted = requestedModel.trim().toLowerCase();
3281
+ return (_nullishCoalesce(binding.models, () => ( []))).some((ref) => _optionalChain([modelIdOf, 'call', _143 => _143(ref), 'optionalAccess', _144 => _144.toLowerCase, 'call', _145 => _145()]) === wanted);
3282
+ }
3283
+ return role === "background" ? nonBlank(binding.backgroundModel) : nonBlank(binding.defaultModel);
3284
+ }
3285
+ function modelIdOf(ref) {
3286
+ if (!nonBlank(ref)) return void 0;
3287
+ const trimmed = ref.trim();
3288
+ const comma = trimmed.indexOf(",");
3289
+ const modelId = comma >= 0 ? trimmed.slice(comma + 1).trim() : trimmed;
3290
+ return modelId || void 0;
3291
+ }
3292
+ function targetProviderId(target) {
3293
+ return target.providerId.trim();
3294
+ }
3295
+ function targetRef(target, ref) {
3296
+ const modelId = _nullishCoalesce(modelIdOf(ref), () => ( ""));
3297
+ return modelId ? `${targetProviderId(target)},${modelId}` : "";
3298
+ }
3299
+ function targetRefs(target, refs) {
3300
+ return (_nullishCoalesce(refs, () => ( []))).map((ref) => targetRef(target, ref)).filter(nonBlank);
3301
+ }
3302
+ function targetPrefixRefs(target, refs) {
3303
+ if (!refs) return void 0;
3304
+ const mapped = {};
3305
+ for (const kind of ["claude", "gpt", "gemini"]) {
3306
+ const ref = targetRef(target, refs[kind]);
3307
+ if (ref) mapped[kind] = ref;
3308
+ }
3309
+ return mapped.claude || mapped.gpt || mapped.gemini ? mapped : void 0;
3310
+ }
3311
+ function applySingleModel(config, binding, targetModel) {
3312
+ const ref = targetRef(binding.target, targetModel);
3313
+ if (!ref) return;
3314
+ if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3315
+ config.modelMap = Object.fromEntries(
3316
+ _chunk4VZX5F4Tcjs.modelKindsForEndpoint.call(void 0, binding.endpoint).map((kind) => [kind, ref])
3317
+ );
3318
+ } else if (binding.endpoint === "chat") {
3319
+ config.models = [ref];
3320
+ config.dispatchMode = "list";
3321
+ } else {
3322
+ config.defaultModel = ref;
3323
+ config.backgroundModel = ref;
3324
+ }
3325
+ }
3326
+ function gatewayBindingToEndpointConfig(binding, requestedModel) {
3327
+ const target = binding.target;
3328
+ const config = {
3329
+ endpoint: binding.endpoint,
3330
+ useSubscription: target.kind !== "provider"
3331
+ };
3332
+ const usesGenericModelHandling = binding.modelMode === "passthrough" || Boolean(_optionalChain([binding, 'access', _146 => _146.modelMappings, 'optionalAccess', _147 => _147.length]));
3333
+ const dynamicModel = binding.modelMode === "passthrough" ? requestedModel : resolveGatewayModelMapping(binding.modelMappings, requestedModel);
3334
+ if (usesGenericModelHandling) {
3335
+ if (nonBlank(dynamicModel)) applySingleModel(config, binding, dynamicModel);
3336
+ } else if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3337
+ config.modelMap = Object.fromEntries(
3338
+ Object.entries(_nullishCoalesce(binding.modelMap, () => ( {}))).map(([kind, ref]) => [kind, targetRef(target, ref)])
3339
+ );
3340
+ } else if (binding.endpoint === "chat") {
3341
+ config.models = targetRefs(target, binding.models);
3342
+ if (binding.dispatchMode === "prefix") {
3343
+ config.dispatchMode = "prefix";
3344
+ config.prefixTargets = targetPrefixRefs(target, binding.prefixTargets);
3345
+ }
3346
+ } else {
3347
+ config.defaultModel = targetRef(target, binding.defaultModel);
3348
+ config.backgroundModel = targetRef(target, binding.backgroundModel);
3349
+ if (binding.backgroundModelIds) config.backgroundModelIds = [...binding.backgroundModelIds];
3350
+ }
3351
+ if (target.kind === "account") {
3352
+ config.boundAccountId = target.accountId;
3353
+ config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3354
+ } else if (target.kind === "account-group") {
3355
+ config.boundAccountGroup = target.group;
3356
+ config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3357
+ } else if (target.kind === "provider" && target.keyId) {
3358
+ config.boundKeyId = target.keyId;
3359
+ config.boundKeyFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3360
+ }
3361
+ return config;
3362
+ }
3363
+ function candidateGatewayBindings(bindings, apiKeyId, endpoint) {
3364
+ const candidates = (_nullishCoalesce(bindings, () => ( []))).filter(
3365
+ (binding) => binding.enabled && binding.endpoint === endpoint && gatewayBindingAllowsKey(binding, apiKeyId)
3366
+ );
3367
+ const scopeOf = (binding) => _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _148 => _148.apiKeyIds, 'optionalAccess', _149 => _149.length]) ? "selected" : "all")));
3368
+ const scoped = candidates.filter((binding) => scopeOf(binding) === "selected");
3369
+ const pool = scoped.length > 0 ? scoped : candidates.filter((binding) => scopeOf(binding) === "all");
3370
+ return pool.sort(
3371
+ (left, right) => (_nullishCoalesce(left.priority, () => ( 100))) - (_nullishCoalesce(right.priority, () => ( 100))) || left.id.localeCompare(right.id)
3372
+ );
3373
+ }
3374
+ function candidateBackgroundModelIds(bindings, apiKeyId, endpoint) {
3375
+ const ids = [
3376
+ ...new Set(
3377
+ candidateGatewayBindings(bindings, apiKeyId, endpoint).flatMap(
3378
+ (binding) => _nullishCoalesce(binding.backgroundModelIds, () => ( []))
3379
+ )
3380
+ )
3381
+ ];
3382
+ return ids.length > 0 ? ids : void 0;
3383
+ }
3384
+ function resolveGatewayBinding(input) {
3385
+ const candidates = candidateGatewayBindings(input.bindings, input.apiKeyId, input.endpoint);
3386
+ const binding = _nullishCoalesce(candidates.find(
3387
+ (candidate) => candidate.fallback === "fail" || routeCanServe(candidate, input.requestedModel, input.role)
3388
+ ), () => ( candidates[0]));
3389
+ if (!binding) return { source: "none" };
3390
+ return {
3391
+ source: "binding",
3392
+ binding,
3393
+ config: gatewayBindingToEndpointConfig(binding, input.requestedModel)
3394
+ };
3395
+ }
3396
+
3397
+ // src/provider-proxy/RouteLeaseTargetResolver.ts
3398
+ function resolveEnvKey(raw) {
3399
+ if (!raw) return "";
3400
+ return raw.startsWith("$") ? _nullishCoalesce(process.env[raw.slice(1)], () => ( "")) : raw;
3401
+ }
3402
+ function providerSupportsModel(provider, model) {
3403
+ const flat = _nullishCoalesce(provider.models, () => ( []));
3404
+ const configured = _nullishCoalesce(provider.modelConfigs, () => ( []));
3405
+ if (flat.length === 0 && configured.length === 0) return false;
3406
+ return flat.includes(model) || configured.some((entry) => entry.id === model && entry.enabled !== false);
3407
+ }
3408
+ function syntheticBinding(request) {
3409
+ const runtime = routeLeaseRuntime(request.runtime);
3410
+ return {
3411
+ id: "route-lease-ephemeral",
3412
+ name: "Route Lease ephemeral target",
3413
+ enabled: true,
3414
+ endpoint: runtime.endpoint,
3415
+ target: request.upstream,
3416
+ fallback: "fail",
3417
+ modelMode: "passthrough"
3418
+ };
3419
+ }
3420
+ function transformerRegistered(llmConfig, name) {
3421
+ return Boolean(_optionalChain([llmConfig, 'access', _150 => _150.getTransformerService, 'call', _151 => _151(), 'optionalAccess', _152 => _152.getTransformer, 'call', _153 => _153(name)]));
3422
+ }
3423
+ var RouteLeaseTargetResolver = class {
3424
+ constructor(llmConfig, options = {}) {
3425
+ this.llmConfig = llmConfig;
3426
+ this.options = options;
3427
+ }
3428
+
3429
+
3430
+ async resolve(request) {
3431
+ const runtime = routeLeaseRuntime(request.runtime);
3432
+ await this.preflightTarget(request);
3433
+ const binding = syntheticBinding(request);
3434
+ const config = gatewayBindingToEndpointConfig(binding, request.model);
3435
+ const result = await _chunk4VZX5F4Tcjs.resolveRoute.call(void 0, {
3436
+ config,
3437
+ ingressFormat: runtime.ingressFormat,
3438
+ llmConfig: this.llmConfig,
3439
+ requestedModel: request.model,
3440
+ sessionId: _nullishCoalesce(_optionalChain([request, 'access', _154 => _154.execution, 'optionalAccess', _155 => _155.sessionIdHash]), () => ( null))
3441
+ });
3442
+ if (!result.ok) {
3443
+ throw new RouteLeaseError(
3444
+ result.error.status === 404 ? "upstream_not_found" : "upstream_unavailable",
3445
+ "the selected upstream could not be resolved"
3446
+ );
3447
+ }
3448
+ return { ...result.route, requestedModel: void 0 };
2633
3449
  }
2634
- /** Remove a route at run end. Returns true if an entry existed. */
2635
- removeRoute(token) {
2636
- return this.routes.removeRoute(token);
3450
+ async preflightTarget(request) {
3451
+ const target = request.upstream;
3452
+ if (target.kind !== "provider") {
3453
+ await this.preflightSubscription(target, request.model);
3454
+ return;
3455
+ }
3456
+ const provider = await this.llmConfig.getProvider(target.providerId);
3457
+ if (!provider) throw new RouteLeaseError("upstream_not_found", "provider was not found");
3458
+ if (provider.enabled === false) throw new RouteLeaseError("upstream_unavailable", "provider is disabled");
3459
+ if (!providerSupportsModel(provider, request.model)) {
3460
+ throw new RouteLeaseError("model_not_configured", "model is not configured for the provider");
3461
+ }
3462
+ if (target.keyId) {
3463
+ const availability = await _optionalChain([this, 'access', _156 => _156.options, 'access', _157 => _157.providerKeys, 'optionalAccess', _158 => _158.getKeyAvailability, 'optionalCall', _159 => _159(
3464
+ target.providerId,
3465
+ target.keyId
3466
+ )]);
3467
+ if (availability === "not-found") {
3468
+ throw new RouteLeaseError("upstream_not_found", "the selected provider key was not found");
3469
+ }
3470
+ const usable = availability === "usable" || availability === void 0 && Boolean(this.options.providerKeys) && await this.options.providerKeys.hasUsableKey(target.providerId, target.keyId);
3471
+ if (!usable) {
3472
+ throw new RouteLeaseError("upstream_unavailable", "the selected provider key is unavailable");
3473
+ }
3474
+ } else {
3475
+ const directKey = resolveEnvKey(provider.api_key);
3476
+ const poolAvailability = await _optionalChain([this, 'access', _160 => _160.options, 'access', _161 => _161.providerKeys, 'optionalAccess', _162 => _162.getPoolAvailability, 'optionalCall', _163 => _163(target.providerId)]);
3477
+ const poolReady = poolAvailability ? poolAvailability.outcome === "usable" : this.options.providerKeys ? await this.options.providerKeys.hasUsableKeys(target.providerId) : false;
3478
+ if (!directKey && _optionalChain([poolAvailability, 'optionalAccess', _164 => _164.outcome]) === "exhausted") {
3479
+ throw new RouteLeaseError("upstream_exhausted", "provider key pool is temporarily exhausted", {
3480
+ retryAfterSeconds: poolAvailability.retryAfterSeconds
3481
+ });
3482
+ }
3483
+ if (!directKey && !poolReady) {
3484
+ throw new RouteLeaseError("upstream_unavailable", "provider has no usable credential");
3485
+ }
3486
+ }
3487
+ const sameFormat = request.runtime === "claude" && provider.apiFormat === "anthropic" || request.runtime === "codex" && provider.apiFormat === "openai-response";
3488
+ if (!sameFormat) {
3489
+ try {
3490
+ const chain = await _chunkQXD4MV2Xcjs.resolveProviderChain.call(void 0, this.llmConfig, provider.id, request.model);
3491
+ if (!chain.hasTransformers) {
3492
+ throw new RouteLeaseError("format_unsupported", "no transformer chain supports this runtime and upstream");
3493
+ }
3494
+ } catch (error) {
3495
+ if (error instanceof RouteLeaseError) throw error;
3496
+ throw new RouteLeaseError("format_unsupported", "the transformer chain could not be resolved", { cause: error });
3497
+ }
3498
+ }
2637
3499
  }
2638
- /** Live-route count (diagnostics / tests). */
2639
- routeCount() {
2640
- return this.routes.size();
3500
+ async preflightSubscription(target, model) {
3501
+ const registry = _chunkQBZJ7P2Tcjs.getSubscriptionRegistryForOutbound.call(void 0, );
3502
+ const profile = _nullishCoalesce(_optionalChain([registry, 'optionalAccess', _165 => _165.getProfile, 'call', _166 => _166(target.providerId)]), () => ( null));
3503
+ if (!profile) throw new RouteLeaseError("upstream_not_found", "subscription provider was not found");
3504
+ await _optionalChain([this, 'access', _167 => _167.options, 'access', _168 => _168.subscriptions, 'optionalAccess', _169 => _169.assertAvailable, 'call', _170 => _170(target, model)]);
3505
+ const names = _nullishCoalesce(_nullishCoalesce(_optionalChain([profile, 'access', _171 => _171.resolveProviderTransformerNames, 'optionalCall', _172 => _172(model)]), () => ( profile.providerTransformerNames)), () => ( []));
3506
+ if (names.some((name) => !transformerRegistered(this.llmConfig, name))) {
3507
+ throw new RouteLeaseError("format_unsupported", "a required transformer is unavailable");
3508
+ }
3509
+ if (_optionalChain([profile, 'access', _173 => _173.modelTransformerNames, 'optionalAccess', _174 => _174.some, 'call', _175 => _175((name) => !transformerRegistered(this.llmConfig, name))])) {
3510
+ throw new RouteLeaseError("format_unsupported", "a required model transformer is unavailable");
3511
+ }
2641
3512
  }
2642
- }, _class2);
3513
+ };
2643
3514
 
2644
3515
  // src/provider-proxy/index.ts
2645
3516
  var instance = null;
@@ -2662,9 +3533,9 @@ var _os = require('os');
2662
3533
  var _healthloggingtypes = require('@omnicross/contracts/health-logging-types');
2663
3534
 
2664
3535
  // src/outbound-api/keyedMutex.ts
2665
- var KeyedMutex = (_class3 = class {constructor() { _class3.prototype.__init4.call(this); }
3536
+ var KeyedMutex = (_class5 = class {constructor() { _class5.prototype.__init11.call(this); }
2666
3537
  /** key → the tail of the pending-op chain (resolves when the last op frees). */
2667
- __init4() {this.tails = /* @__PURE__ */ new Map()}
3538
+ __init11() {this.tails = /* @__PURE__ */ new Map()}
2668
3539
  /**
2669
3540
  * Run `fn` exclusively for `key`: it starts only after every previously
2670
3541
  * enqueued op for the SAME key has settled, and the next waiter starts only
@@ -2687,7 +3558,7 @@ var KeyedMutex = (_class3 = class {constructor() { _class3.prototype.__init4.cal
2687
3558
  if (this.tails.get(key) === chain) this.tails.delete(key);
2688
3559
  }
2689
3560
  }
2690
- }, _class3);
3561
+ }, _class5);
2691
3562
 
2692
3563
  // src/outbound-api/outboundApiRouter.ts
2693
3564
  var _stream = require('stream');
@@ -2715,11 +3586,11 @@ var DEFAULT_ACCOUNT_HEALTH = {
2715
3586
  overloadCooldownMs: 10 * 6e4
2716
3587
  };
2717
3588
  function normalizeAccountHealth(raw) {
2718
- const ah = _optionalChain([raw, 'optionalAccess', _82 => _82.accountHealth]);
3589
+ const ah = _optionalChain([raw, 'optionalAccess', _176 => _176.accountHealth]);
2719
3590
  return {
2720
- overloadCooldownEnabled: _optionalChain([ah, 'optionalAccess', _83 => _83.overloadCooldownEnabled]) !== false,
3591
+ overloadCooldownEnabled: _optionalChain([ah, 'optionalAccess', _177 => _177.overloadCooldownEnabled]) !== false,
2721
3592
  overloadCooldownMs: clampNumber(
2722
- _optionalChain([ah, 'optionalAccess', _84 => _84.overloadCooldownMs]),
3593
+ _optionalChain([ah, 'optionalAccess', _178 => _178.overloadCooldownMs]),
2723
3594
  6e4,
2724
3595
  36e5,
2725
3596
  DEFAULT_ACCOUNT_HEALTH.overloadCooldownMs
@@ -2741,25 +3612,25 @@ var DEFAULT_ALLOWANCE_SCHEDULING = {
2741
3612
  priorityPenalty: 100
2742
3613
  };
2743
3614
  function normalizeAllowanceScheduling(raw) {
2744
- const value = _optionalChain([raw, 'optionalAccess', _85 => _85.allowanceScheduling]);
3615
+ const value = _optionalChain([raw, 'optionalAccess', _179 => _179.allowanceScheduling]);
2745
3616
  const demoteAtPercent = clampNumber(
2746
- _optionalChain([value, 'optionalAccess', _86 => _86.demoteAtPercent]),
3617
+ _optionalChain([value, 'optionalAccess', _180 => _180.demoteAtPercent]),
2747
3618
  0,
2748
3619
  100,
2749
3620
  DEFAULT_ALLOWANCE_SCHEDULING.demoteAtPercent
2750
3621
  );
2751
3622
  const requestedPause = clampNumber(
2752
- _optionalChain([value, 'optionalAccess', _87 => _87.pauseAtPercent]),
3623
+ _optionalChain([value, 'optionalAccess', _181 => _181.pauseAtPercent]),
2753
3624
  0,
2754
3625
  100,
2755
3626
  DEFAULT_ALLOWANCE_SCHEDULING.pauseAtPercent
2756
3627
  );
2757
3628
  return {
2758
- enabled: _optionalChain([value, 'optionalAccess', _88 => _88.enabled]) === true,
3629
+ enabled: _optionalChain([value, 'optionalAccess', _182 => _182.enabled]) === true,
2759
3630
  demoteAtPercent,
2760
3631
  pauseAtPercent: Math.max(demoteAtPercent, requestedPause),
2761
3632
  priorityPenalty: Math.trunc(clampNumber(
2762
- _optionalChain([value, 'optionalAccess', _89 => _89.priorityPenalty]),
3633
+ _optionalChain([value, 'optionalAccess', _183 => _183.priorityPenalty]),
2763
3634
  1,
2764
3635
  1e3,
2765
3636
  DEFAULT_ALLOWANCE_SCHEDULING.priorityPenalty
@@ -2767,28 +3638,28 @@ function normalizeAllowanceScheduling(raw) {
2767
3638
  };
2768
3639
  }
2769
3640
  function normalizeAccountProbe(raw) {
2770
- const ap = _optionalChain([raw, 'optionalAccess', _90 => _90.accountProbe]);
3641
+ const ap = _optionalChain([raw, 'optionalAccess', _184 => _184.accountProbe]);
2771
3642
  return {
2772
- enabled: _optionalChain([ap, 'optionalAccess', _91 => _91.enabled]) === true,
2773
- intervalMs: clampNumber(_optionalChain([ap, 'optionalAccess', _92 => _92.intervalMs]), 6e4, 864e5, DEFAULT_ACCOUNT_PROBE.intervalMs),
2774
- onlyMultiAccount: _optionalChain([ap, 'optionalAccess', _93 => _93.onlyMultiAccount]) !== false,
2775
- timeoutMs: clampNumber(_optionalChain([ap, 'optionalAccess', _94 => _94.timeoutMs]), 1e3, 6e4, DEFAULT_ACCOUNT_PROBE.timeoutMs),
3643
+ enabled: _optionalChain([ap, 'optionalAccess', _185 => _185.enabled]) === true,
3644
+ intervalMs: clampNumber(_optionalChain([ap, 'optionalAccess', _186 => _186.intervalMs]), 6e4, 864e5, DEFAULT_ACCOUNT_PROBE.intervalMs),
3645
+ onlyMultiAccount: _optionalChain([ap, 'optionalAccess', _187 => _187.onlyMultiAccount]) !== false,
3646
+ timeoutMs: clampNumber(_optionalChain([ap, 'optionalAccess', _188 => _188.timeoutMs]), 1e3, 6e4, DEFAULT_ACCOUNT_PROBE.timeoutMs),
2776
3647
  historySize: Math.trunc(
2777
- clampNumber(_optionalChain([ap, 'optionalAccess', _95 => _95.historySize]), 1, 200, DEFAULT_ACCOUNT_PROBE.historySize)
3648
+ clampNumber(_optionalChain([ap, 'optionalAccess', _189 => _189.historySize]), 1, 200, DEFAULT_ACCOUNT_PROBE.historySize)
2778
3649
  ),
2779
- staggerMs: clampNumber(_optionalChain([ap, 'optionalAccess', _96 => _96.staggerMs]), 0, 6e4, DEFAULT_ACCOUNT_PROBE.staggerMs)
3650
+ staggerMs: clampNumber(_optionalChain([ap, 'optionalAccess', _190 => _190.staggerMs]), 0, 6e4, DEFAULT_ACCOUNT_PROBE.staggerMs)
2780
3651
  };
2781
3652
  }
2782
3653
  function normalizeAudit(raw) {
2783
- const a = _optionalChain([raw, 'optionalAccess', _97 => _97.audit]);
3654
+ const a = _optionalChain([raw, 'optionalAccess', _191 => _191.audit]);
2784
3655
  return {
2785
- enabled: _optionalChain([a, 'optionalAccess', _98 => _98.enabled]) === true,
2786
- captureBodies: _optionalChain([a, 'optionalAccess', _99 => _99.captureBodies]) === true,
2787
- maxBodyBytes: normalizeMaxBodyBytes(_optionalChain([a, 'optionalAccess', _100 => _100.maxBodyBytes])),
3656
+ enabled: _optionalChain([a, 'optionalAccess', _192 => _192.enabled]) === true,
3657
+ captureBodies: _optionalChain([a, 'optionalAccess', _193 => _193.captureBodies]) === true,
3658
+ maxBodyBytes: normalizeMaxBodyBytes(_optionalChain([a, 'optionalAccess', _194 => _194.maxBodyBytes])),
2788
3659
  retentionDays: Math.trunc(
2789
- clampNumber(_optionalChain([a, 'optionalAccess', _101 => _101.retentionDays]), 1, 365, _audittypes.DEFAULT_AUDIT_CONFIG.retentionDays)
3660
+ clampNumber(_optionalChain([a, 'optionalAccess', _195 => _195.retentionDays]), 1, 365, _audittypes.DEFAULT_AUDIT_CONFIG.retentionDays)
2790
3661
  ),
2791
- trustForwardedFor: _optionalChain([a, 'optionalAccess', _102 => _102.trustForwardedFor]) === true
3662
+ trustForwardedFor: _optionalChain([a, 'optionalAccess', _196 => _196.trustForwardedFor]) === true
2792
3663
  };
2793
3664
  }
2794
3665
  function normalizeMaxBodyBytes(value) {
@@ -2799,26 +3670,26 @@ function normalizeMaxBodyBytes(value) {
2799
3670
  return Math.trunc(Math.min(1048576, value));
2800
3671
  }
2801
3672
  function normalizeBilling(raw) {
2802
- const b = _optionalChain([raw, 'optionalAccess', _103 => _103.billing]);
3673
+ const b = _optionalChain([raw, 'optionalAccess', _197 => _197.billing]);
2803
3674
  const config = {
2804
- enabled: _optionalChain([b, 'optionalAccess', _104 => _104.enabled]) === true,
3675
+ enabled: _optionalChain([b, 'optionalAccess', _198 => _198.enabled]) === true,
2805
3676
  maxRetryAgeMs: Math.trunc(
2806
- clampNumber(_optionalChain([b, 'optionalAccess', _105 => _105.maxRetryAgeMs]), 6e4, 2592e6, _billingtypes.DEFAULT_BILLING_CONFIG.maxRetryAgeMs)
3677
+ clampNumber(_optionalChain([b, 'optionalAccess', _199 => _199.maxRetryAgeMs]), 6e4, 2592e6, _billingtypes.DEFAULT_BILLING_CONFIG.maxRetryAgeMs)
2807
3678
  )
2808
3679
  };
2809
- if (typeof _optionalChain([b, 'optionalAccess', _106 => _106.endpoint]) === "string" && b.endpoint.trim()) config.endpoint = b.endpoint.trim();
2810
- if (typeof _optionalChain([b, 'optionalAccess', _107 => _107.secret]) === "string" && b.secret.length > 0) config.secret = b.secret;
3680
+ if (typeof _optionalChain([b, 'optionalAccess', _200 => _200.endpoint]) === "string" && b.endpoint.trim()) config.endpoint = b.endpoint.trim();
3681
+ if (typeof _optionalChain([b, 'optionalAccess', _201 => _201.secret]) === "string" && b.secret.length > 0) config.secret = b.secret;
2811
3682
  return config;
2812
3683
  }
2813
3684
  var DEFAULT_FINGERPRINT = { enabled: false };
2814
3685
  function normalizeFingerprint(raw) {
2815
- const f = _optionalChain([raw, 'optionalAccess', _108 => _108.fingerprint]);
2816
- const out = { enabled: _optionalChain([f, 'optionalAccess', _109 => _109.enabled]) === true };
2817
- if (typeof _optionalChain([f, 'optionalAccess', _110 => _110.ua]) === "string" && f.ua.trim().length > 0) out.ua = f.ua.trim();
3686
+ const f = _optionalChain([raw, 'optionalAccess', _202 => _202.fingerprint]);
3687
+ const out = { enabled: _optionalChain([f, 'optionalAccess', _203 => _203.enabled]) === true };
3688
+ if (typeof _optionalChain([f, 'optionalAccess', _204 => _204.ua]) === "string" && f.ua.trim().length > 0) out.ua = f.ua.trim();
2818
3689
  return out;
2819
3690
  }
2820
3691
  function normalizeVoucher(raw) {
2821
- return { enabled: _optionalChain([raw, 'optionalAccess', _111 => _111.voucher, 'optionalAccess', _112 => _112.enabled]) === true };
3692
+ return { enabled: _optionalChain([raw, 'optionalAccess', _205 => _205.voucher, 'optionalAccess', _206 => _206.enabled]) === true };
2822
3693
  }
2823
3694
  var PROXY_TYPES = ["http", "https", "socks5"];
2824
3695
  function normalizeProxyConfig(raw) {
@@ -2902,14 +3773,14 @@ function clampNumber(value, min, max, fallback) {
2902
3773
  return Math.min(max, Math.max(min, n));
2903
3774
  }
2904
3775
  function normalizeQueueSegments(raw) {
2905
- const umq = _optionalChain([raw, 'optionalAccess', _113 => _113.userMessageQueue]);
2906
- const cq = _optionalChain([raw, 'optionalAccess', _114 => _114.concurrencyQueue]);
3776
+ const umq = _optionalChain([raw, 'optionalAccess', _207 => _207.userMessageQueue]);
3777
+ const cq = _optionalChain([raw, 'optionalAccess', _208 => _208.concurrencyQueue]);
2907
3778
  return {
2908
3779
  userMessageQueue: {
2909
- enabled: _optionalChain([umq, 'optionalAccess', _115 => _115.enabled]) === true,
2910
- delayMs: clampNumber(_optionalChain([umq, 'optionalAccess', _116 => _116.delayMs]), 0, 1e4, DEFAULT_USER_MESSAGE_QUEUE.delayMs),
3780
+ enabled: _optionalChain([umq, 'optionalAccess', _209 => _209.enabled]) === true,
3781
+ delayMs: clampNumber(_optionalChain([umq, 'optionalAccess', _210 => _210.delayMs]), 0, 1e4, DEFAULT_USER_MESSAGE_QUEUE.delayMs),
2911
3782
  waitTimeoutMs: clampNumber(
2912
- _optionalChain([umq, 'optionalAccess', _117 => _117.waitTimeoutMs]),
3783
+ _optionalChain([umq, 'optionalAccess', _211 => _211.waitTimeoutMs]),
2913
3784
  1e3,
2914
3785
  3e5,
2915
3786
  DEFAULT_USER_MESSAGE_QUEUE.waitTimeoutMs
@@ -2917,14 +3788,14 @@ function normalizeQueueSegments(raw) {
2917
3788
  },
2918
3789
  concurrencyQueue: {
2919
3790
  maxQueueSizeFactor: clampNumber(
2920
- _optionalChain([cq, 'optionalAccess', _118 => _118.maxQueueSizeFactor]),
3791
+ _optionalChain([cq, 'optionalAccess', _212 => _212.maxQueueSizeFactor]),
2921
3792
  1,
2922
3793
  10,
2923
3794
  DEFAULT_CONCURRENCY_QUEUE.maxQueueSizeFactor
2924
3795
  ),
2925
- minQueueSize: clampNumber(_optionalChain([cq, 'optionalAccess', _119 => _119.minQueueSize]), 1, 100, DEFAULT_CONCURRENCY_QUEUE.minQueueSize),
3796
+ minQueueSize: clampNumber(_optionalChain([cq, 'optionalAccess', _213 => _213.minQueueSize]), 1, 100, DEFAULT_CONCURRENCY_QUEUE.minQueueSize),
2926
3797
  waitTimeoutMs: clampNumber(
2927
- _optionalChain([cq, 'optionalAccess', _120 => _120.waitTimeoutMs]),
3798
+ _optionalChain([cq, 'optionalAccess', _214 => _214.waitTimeoutMs]),
2928
3799
  1e3,
2929
3800
  3e5,
2930
3801
  DEFAULT_CONCURRENCY_QUEUE.waitTimeoutMs
@@ -3037,7 +3908,7 @@ function normalizeGatewayModelMappings(raw) {
3037
3908
  return mappings;
3038
3909
  }
3039
3910
  function hasConfiguredLegacyModels(config) {
3040
- return Object.values(_nullishCoalesce(config.modelMap, () => ( {}))).some((value) => typeof value === "string" && value.trim()) || Boolean(_optionalChain([config, 'access', _121 => _121.models, 'optionalAccess', _122 => _122.some, 'call', _123 => _123((value) => typeof value === "string" && value.trim())])) || Boolean(_optionalChain([config, 'access', _124 => _124.defaultModel, 'optionalAccess', _125 => _125.trim, 'call', _126 => _126()])) || Boolean(_optionalChain([config, 'access', _127 => _127.backgroundModel, 'optionalAccess', _128 => _128.trim, 'call', _129 => _129()])) || Boolean(Object.values(_nullishCoalesce(config.prefixTargets, () => ( {}))).some((value) => _optionalChain([value, 'optionalAccess', _130 => _130.trim, 'call', _131 => _131()])));
3911
+ return Object.values(_nullishCoalesce(config.modelMap, () => ( {}))).some((value) => typeof value === "string" && value.trim()) || Boolean(_optionalChain([config, 'access', _215 => _215.models, 'optionalAccess', _216 => _216.some, 'call', _217 => _217((value) => typeof value === "string" && value.trim())])) || Boolean(_optionalChain([config, 'access', _218 => _218.defaultModel, 'optionalAccess', _219 => _219.trim, 'call', _220 => _220()])) || Boolean(_optionalChain([config, 'access', _221 => _221.backgroundModel, 'optionalAccess', _222 => _222.trim, 'call', _223 => _223()])) || Boolean(Object.values(_nullishCoalesce(config.prefixTargets, () => ( {}))).some((value) => _optionalChain([value, 'optionalAccess', _224 => _224.trim, 'call', _225 => _225()])));
3041
3912
  }
3042
3913
  function splitModelRef(ref) {
3043
3914
  if (typeof ref !== "string") return null;
@@ -3105,7 +3976,7 @@ function legacyEndpointToBindings(config) {
3105
3976
  if (!parsed) continue;
3106
3977
  const route = routeFor(parsed.providerId);
3107
3978
  route[field] = ref;
3108
- if (_optionalChain([config, 'access', _132 => _132.backgroundModelIds, 'optionalAccess', _133 => _133.length])) {
3979
+ if (_optionalChain([config, 'access', _226 => _226.backgroundModelIds, 'optionalAccess', _227 => _227.length])) {
3109
3980
  route.backgroundModelIds = [...config.backgroundModelIds];
3110
3981
  }
3111
3982
  }
@@ -3255,167 +4126,6 @@ function mergeServerConfig(current, patch) {
3255
4126
  });
3256
4127
  }
3257
4128
 
3258
- // src/outbound-api/gatewayBindingResolver.ts
3259
- var MESSAGE_FALLBACK_KINDS = ["sonnet", "opus", "haiku", "fable"];
3260
- function nonBlank(value) {
3261
- return typeof value === "string" && value.trim() !== "";
3262
- }
3263
- function gatewayBindingAllowsKey(binding, apiKeyId) {
3264
- const scope = _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _134 => _134.apiKeyIds, 'optionalAccess', _135 => _135.length]) ? "selected" : "all")));
3265
- return scope === "all" || Boolean(_optionalChain([binding, 'access', _136 => _136.apiKeyIds, 'optionalAccess', _137 => _137.includes, 'call', _138 => _138(apiKeyId)]));
3266
- }
3267
- function wildcardMatches(pattern, value) {
3268
- const escaped = pattern.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")).join(".*");
3269
- return new RegExp(`^${escaped}$`, "iu").test(value);
3270
- }
3271
- function resolveGatewayModelMapping(mappings, requestedModel) {
3272
- if (!nonBlank(requestedModel)) return void 0;
3273
- const wanted = requestedModel.trim();
3274
- const exact = (_nullishCoalesce(mappings, () => ( []))).find(
3275
- (mapping) => nonBlank(mapping.source) && !mapping.source.includes("*") && mapping.source.trim().toLocaleLowerCase() === wanted.toLocaleLowerCase()
3276
- );
3277
- if (exact && nonBlank(exact.target)) return exact.target.trim();
3278
- const wildcard = (_nullishCoalesce(mappings, () => ( []))).find(
3279
- (mapping) => nonBlank(mapping.source) && mapping.source.includes("*") && wildcardMatches(mapping.source.trim(), wanted)
3280
- );
3281
- return wildcard && nonBlank(wildcard.target) ? wildcard.target.trim() : void 0;
3282
- }
3283
- function routeCanServe(binding, requestedModel, role) {
3284
- if (binding.modelMode === "passthrough") return nonBlank(requestedModel);
3285
- if (_optionalChain([binding, 'access', _139 => _139.modelMappings, 'optionalAccess', _140 => _140.length])) {
3286
- return nonBlank(resolveGatewayModelMapping(binding.modelMappings, requestedModel));
3287
- }
3288
- if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3289
- const map = _nullishCoalesce(binding.modelMap, () => ( {}));
3290
- const kind = _chunk4VZX5F4Tcjs.detectModelKind.call(void 0, binding.endpoint, requestedModel);
3291
- if (kind && nonBlank(map[kind])) return true;
3292
- if (binding.endpoint === "messages") {
3293
- return MESSAGE_FALLBACK_KINDS.some((candidate) => nonBlank(map[candidate]));
3294
- }
3295
- return nonBlank(map.codex);
3296
- }
3297
- if (binding.endpoint === "chat" && binding.dispatchMode === "prefix") {
3298
- return _chunk4VZX5F4Tcjs.resolvePrefixTarget.call(void 0, binding.prefixTargets, requestedModel) !== null;
3299
- }
3300
- if (binding.endpoint === "chat") {
3301
- if (!requestedModel) return false;
3302
- const wanted = requestedModel.trim().toLowerCase();
3303
- return (_nullishCoalesce(binding.models, () => ( []))).some((ref) => _optionalChain([modelIdOf, 'call', _141 => _141(ref), 'optionalAccess', _142 => _142.toLowerCase, 'call', _143 => _143()]) === wanted);
3304
- }
3305
- return role === "background" ? nonBlank(binding.backgroundModel) : nonBlank(binding.defaultModel);
3306
- }
3307
- function modelIdOf(ref) {
3308
- if (!nonBlank(ref)) return void 0;
3309
- const trimmed = ref.trim();
3310
- const comma = trimmed.indexOf(",");
3311
- const modelId = comma >= 0 ? trimmed.slice(comma + 1).trim() : trimmed;
3312
- return modelId || void 0;
3313
- }
3314
- function targetProviderId(target) {
3315
- return target.providerId.trim();
3316
- }
3317
- function targetRef(target, ref) {
3318
- const modelId = _nullishCoalesce(modelIdOf(ref), () => ( ""));
3319
- return modelId ? `${targetProviderId(target)},${modelId}` : "";
3320
- }
3321
- function targetRefs(target, refs) {
3322
- return (_nullishCoalesce(refs, () => ( []))).map((ref) => targetRef(target, ref)).filter(nonBlank);
3323
- }
3324
- function targetPrefixRefs(target, refs) {
3325
- if (!refs) return void 0;
3326
- const mapped = {};
3327
- for (const kind of ["claude", "gpt", "gemini"]) {
3328
- const ref = targetRef(target, refs[kind]);
3329
- if (ref) mapped[kind] = ref;
3330
- }
3331
- return mapped.claude || mapped.gpt || mapped.gemini ? mapped : void 0;
3332
- }
3333
- function applySingleModel(config, binding, targetModel) {
3334
- const ref = targetRef(binding.target, targetModel);
3335
- if (!ref) return;
3336
- if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3337
- config.modelMap = Object.fromEntries(
3338
- _chunk4VZX5F4Tcjs.modelKindsForEndpoint.call(void 0, binding.endpoint).map((kind) => [kind, ref])
3339
- );
3340
- } else if (binding.endpoint === "chat") {
3341
- config.models = [ref];
3342
- config.dispatchMode = "list";
3343
- } else {
3344
- config.defaultModel = ref;
3345
- config.backgroundModel = ref;
3346
- }
3347
- }
3348
- function gatewayBindingToEndpointConfig(binding, requestedModel) {
3349
- const target = binding.target;
3350
- const config = {
3351
- endpoint: binding.endpoint,
3352
- useSubscription: target.kind !== "provider"
3353
- };
3354
- const usesGenericModelHandling = binding.modelMode === "passthrough" || Boolean(_optionalChain([binding, 'access', _144 => _144.modelMappings, 'optionalAccess', _145 => _145.length]));
3355
- const dynamicModel = binding.modelMode === "passthrough" ? requestedModel : resolveGatewayModelMapping(binding.modelMappings, requestedModel);
3356
- if (usesGenericModelHandling) {
3357
- if (nonBlank(dynamicModel)) applySingleModel(config, binding, dynamicModel);
3358
- } else if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3359
- config.modelMap = Object.fromEntries(
3360
- Object.entries(_nullishCoalesce(binding.modelMap, () => ( {}))).map(([kind, ref]) => [kind, targetRef(target, ref)])
3361
- );
3362
- } else if (binding.endpoint === "chat") {
3363
- config.models = targetRefs(target, binding.models);
3364
- if (binding.dispatchMode === "prefix") {
3365
- config.dispatchMode = "prefix";
3366
- config.prefixTargets = targetPrefixRefs(target, binding.prefixTargets);
3367
- }
3368
- } else {
3369
- config.defaultModel = targetRef(target, binding.defaultModel);
3370
- config.backgroundModel = targetRef(target, binding.backgroundModel);
3371
- if (binding.backgroundModelIds) config.backgroundModelIds = [...binding.backgroundModelIds];
3372
- }
3373
- if (target.kind === "account") {
3374
- config.boundAccountId = target.accountId;
3375
- config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3376
- } else if (target.kind === "account-group") {
3377
- config.boundAccountGroup = target.group;
3378
- config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3379
- } else if (target.kind === "provider" && target.keyId) {
3380
- config.boundKeyId = target.keyId;
3381
- config.boundKeyFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3382
- }
3383
- return config;
3384
- }
3385
- function candidateGatewayBindings(bindings, apiKeyId, endpoint) {
3386
- const candidates = (_nullishCoalesce(bindings, () => ( []))).filter(
3387
- (binding) => binding.enabled && binding.endpoint === endpoint && gatewayBindingAllowsKey(binding, apiKeyId)
3388
- );
3389
- const scopeOf = (binding) => _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _146 => _146.apiKeyIds, 'optionalAccess', _147 => _147.length]) ? "selected" : "all")));
3390
- const scoped = candidates.filter((binding) => scopeOf(binding) === "selected");
3391
- const pool = scoped.length > 0 ? scoped : candidates.filter((binding) => scopeOf(binding) === "all");
3392
- return pool.sort(
3393
- (left, right) => (_nullishCoalesce(left.priority, () => ( 100))) - (_nullishCoalesce(right.priority, () => ( 100))) || left.id.localeCompare(right.id)
3394
- );
3395
- }
3396
- function candidateBackgroundModelIds(bindings, apiKeyId, endpoint) {
3397
- const ids = [
3398
- ...new Set(
3399
- candidateGatewayBindings(bindings, apiKeyId, endpoint).flatMap(
3400
- (binding) => _nullishCoalesce(binding.backgroundModelIds, () => ( []))
3401
- )
3402
- )
3403
- ];
3404
- return ids.length > 0 ? ids : void 0;
3405
- }
3406
- function resolveGatewayBinding(input) {
3407
- const candidates = candidateGatewayBindings(input.bindings, input.apiKeyId, input.endpoint);
3408
- const binding = _nullishCoalesce(candidates.find(
3409
- (candidate) => candidate.fallback === "fail" || routeCanServe(candidate, input.requestedModel, input.role)
3410
- ), () => ( candidates[0]));
3411
- if (!binding) return { source: "none" };
3412
- return {
3413
- source: "binding",
3414
- binding,
3415
- config: gatewayBindingToEndpointConfig(binding, input.requestedModel)
3416
- };
3417
- }
3418
-
3419
4129
  // src/outbound-api/outboundApiKeyAuth.ts
3420
4130
 
3421
4131
 
@@ -3593,42 +4303,42 @@ async function verifyPresentedKey(db, presentedKey) {
3593
4303
  }
3594
4304
 
3595
4305
  // src/outbound-api/outboundConcurrencyGate.ts
3596
- var ConcurrencyQueueFullError = (_class4 = class extends Error {
4306
+ var ConcurrencyQueueFullError = (_class6 = class extends Error {
3597
4307
  constructor(apiKeyId, maxQueueSize) {
3598
- super(`concurrency queue full (max ${maxQueueSize}) for key '${apiKeyId}'`);_class4.prototype.__init5.call(this);;
4308
+ super(`concurrency queue full (max ${maxQueueSize}) for key '${apiKeyId}'`);_class6.prototype.__init12.call(this);;
3599
4309
  this.apiKeyId = apiKeyId;
3600
4310
  this.maxQueueSize = maxQueueSize;
3601
4311
  this.name = "ConcurrencyQueueFullError";
3602
4312
  }
3603
4313
 
3604
4314
 
3605
- __init5() {this.code = "concurrency_queue_full"}
3606
- }, _class4);
3607
- var ConcurrencyWaitTimeoutError = (_class5 = class extends Error {
4315
+ __init12() {this.code = "concurrency_queue_full"}
4316
+ }, _class6);
4317
+ var ConcurrencyWaitTimeoutError = (_class7 = class extends Error {
3608
4318
  constructor(apiKeyId, waitTimeoutMs) {
3609
- super(`concurrency wait timed out after ${waitTimeoutMs}ms for key '${apiKeyId}'`);_class5.prototype.__init6.call(this);;
4319
+ super(`concurrency wait timed out after ${waitTimeoutMs}ms for key '${apiKeyId}'`);_class7.prototype.__init13.call(this);;
3610
4320
  this.apiKeyId = apiKeyId;
3611
4321
  this.waitTimeoutMs = waitTimeoutMs;
3612
4322
  this.name = "ConcurrencyWaitTimeoutError";
3613
4323
  }
3614
4324
 
3615
4325
 
3616
- __init6() {this.code = "concurrency_wait_timeout"}
3617
- }, _class5);
3618
- var ConcurrencyWaitCancelledError = (_class6 = class extends Error {
4326
+ __init13() {this.code = "concurrency_wait_timeout"}
4327
+ }, _class7);
4328
+ var ConcurrencyWaitCancelledError = (_class8 = class extends Error {
3619
4329
  constructor(apiKeyId) {
3620
- super(`concurrency wait cancelled for key '${apiKeyId}'`);_class6.prototype.__init7.call(this);;
4330
+ super(`concurrency wait cancelled for key '${apiKeyId}'`);_class8.prototype.__init14.call(this);;
3621
4331
  this.apiKeyId = apiKeyId;
3622
4332
  this.name = "ConcurrencyWaitCancelledError";
3623
4333
  }
3624
4334
 
3625
- __init7() {this.code = "concurrency_wait_cancelled"}
3626
- }, _class6);
4335
+ __init14() {this.code = "concurrency_wait_cancelled"}
4336
+ }, _class8);
3627
4337
  function isConcurrencyRejection(err) {
3628
4338
  return err instanceof ConcurrencyQueueFullError || err instanceof ConcurrencyWaitTimeoutError || err instanceof ConcurrencyWaitCancelledError;
3629
4339
  }
3630
- var OutboundConcurrencyGate = (_class7 = class {constructor() { _class7.prototype.__init8.call(this); }
3631
- __init8() {this.states = /* @__PURE__ */ new Map()}
4340
+ var OutboundConcurrencyGate = (_class9 = class {constructor() { _class9.prototype.__init15.call(this); }
4341
+ __init15() {this.states = /* @__PURE__ */ new Map()}
3632
4342
  ensure(apiKeyId) {
3633
4343
  let state = this.states.get(apiKeyId);
3634
4344
  if (!state) {
@@ -3752,17 +4462,17 @@ var OutboundConcurrencyGate = (_class7 = class {constructor() { _class7.prototyp
3752
4462
  }
3753
4463
  this.states.clear();
3754
4464
  }
3755
- }, _class7);
4465
+ }, _class9);
3756
4466
 
3757
4467
  // src/outbound-api/outboundRateLimiter.ts
3758
4468
  var DEFAULT_WINDOW_MS = 6e4;
3759
4469
  var DEFAULT_MAX_REQUESTS = 60;
3760
- var OutboundRateLimiter = (_class8 = class {
4470
+ var OutboundRateLimiter = (_class10 = class {
3761
4471
 
3762
4472
 
3763
4473
  /** apiKeyId → ascending request timestamps within the current window. */
3764
- __init9() {this.hits = /* @__PURE__ */ new Map()}
3765
- constructor(options = {}) {;_class8.prototype.__init9.call(this);
4474
+ __init16() {this.hits = /* @__PURE__ */ new Map()}
4475
+ constructor(options = {}) {;_class10.prototype.__init16.call(this);
3766
4476
  this.windowMs = _nullishCoalesce(options.windowMs, () => ( DEFAULT_WINDOW_MS));
3767
4477
  this.maxRequests = _nullishCoalesce(options.maxRequests, () => ( DEFAULT_MAX_REQUESTS));
3768
4478
  }
@@ -3778,8 +4488,8 @@ var OutboundRateLimiter = (_class8 = class {
3778
4488
  * recorded (no bucket growth).
3779
4489
  */
3780
4490
  check(apiKeyId, now = Date.now(), override) {
3781
- const maxRequests = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _148 => _148.maxRequests]), () => ( this.maxRequests));
3782
- const windowMs = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _149 => _149.windowMs]), () => ( this.windowMs));
4491
+ const maxRequests = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _228 => _228.maxRequests]), () => ( this.maxRequests));
4492
+ const windowMs = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _229 => _229.windowMs]), () => ( this.windowMs));
3783
4493
  if (maxRequests === 0) return { allowed: true, retryAfterSeconds: 0 };
3784
4494
  const cutoff = now - windowMs;
3785
4495
  const timestamps = (_nullishCoalesce(this.hits.get(apiKeyId), () => ( []))).filter((ts) => ts > cutoff);
@@ -3804,7 +4514,7 @@ var OutboundRateLimiter = (_class8 = class {
3804
4514
  reset() {
3805
4515
  this.hits.clear();
3806
4516
  }
3807
- }, _class8);
4517
+ }, _class10);
3808
4518
 
3809
4519
  // src/outbound-api/userMessageDetection.ts
3810
4520
  function asArray(v) {
@@ -3824,7 +4534,7 @@ function isUserMessageAnthropic(body) {
3824
4534
  if (typeof content === "string") return true;
3825
4535
  if (Array.isArray(content)) {
3826
4536
  const hasToolResult = content.some(
3827
- (block) => _optionalChain([asObject, 'call', _150 => _150(block), 'optionalAccess', _151 => _151["type"]]) === "tool_result"
4537
+ (block) => _optionalChain([asObject, 'call', _230 => _230(block), 'optionalAccess', _231 => _231["type"]]) === "tool_result"
3828
4538
  );
3829
4539
  return !hasToolResult;
3830
4540
  }
@@ -3853,7 +4563,7 @@ function isUserMessageGemini(body) {
3853
4563
  if (!lastContent || lastContent["role"] !== "user") return false;
3854
4564
  const parts = asArray(lastContent["parts"]);
3855
4565
  const hasFunctionResponse = parts.some(
3856
- (part) => _optionalChain([asObject, 'call', _152 => _152(part), 'optionalAccess', _153 => _153["functionResponse"]]) !== void 0
4566
+ (part) => _optionalChain([asObject, 'call', _232 => _232(part), 'optionalAccess', _233 => _233["functionResponse"]]) !== void 0
3857
4567
  );
3858
4568
  return !hasFunctionResponse;
3859
4569
  }
@@ -3875,22 +4585,22 @@ function isUserMessageRequest(endpoint, parsedBody) {
3875
4585
  }
3876
4586
 
3877
4587
  // src/outbound-api/userMessageSerialQueue.ts
3878
- var SerialQueueTimeoutError = (_class9 = class extends Error {
4588
+ var SerialQueueTimeoutError = (_class11 = class extends Error {
3879
4589
  constructor(providerId, waitTimeoutMs) {
3880
- super(`user-message serial queue wait timed out after ${waitTimeoutMs}ms for '${providerId}'`);_class9.prototype.__init10.call(this);;
4590
+ super(`user-message serial queue wait timed out after ${waitTimeoutMs}ms for '${providerId}'`);_class11.prototype.__init17.call(this);;
3881
4591
  this.providerId = providerId;
3882
4592
  this.waitTimeoutMs = waitTimeoutMs;
3883
4593
  this.name = "SerialQueueTimeoutError";
3884
4594
  }
3885
4595
 
3886
4596
 
3887
- __init10() {this.code = "serial_queue_timeout"}
3888
- }, _class9);
4597
+ __init17() {this.code = "serial_queue_timeout"}
4598
+ }, _class11);
3889
4599
  function isSerialQueueTimeout(err) {
3890
4600
  return err instanceof SerialQueueTimeoutError;
3891
4601
  }
3892
- var UserMessageSerialQueue = (_class10 = class {constructor() { _class10.prototype.__init11.call(this); }
3893
- __init11() {this.states = /* @__PURE__ */ new Map()}
4602
+ var UserMessageSerialQueue = (_class12 = class {constructor() { _class12.prototype.__init18.call(this); }
4603
+ __init18() {this.states = /* @__PURE__ */ new Map()}
3894
4604
  ensure(providerId) {
3895
4605
  let state = this.states.get(providerId);
3896
4606
  if (!state) {
@@ -4005,7 +4715,7 @@ var UserMessageSerialQueue = (_class10 = class {constructor() { _class10.prototy
4005
4715
  }
4006
4716
  this.states.clear();
4007
4717
  }
4008
- }, _class10);
4718
+ }, _class12);
4009
4719
 
4010
4720
  // src/outbound-api/voucher.ts
4011
4721
 
@@ -4027,14 +4737,14 @@ function newVoucherId() {
4027
4737
  }
4028
4738
  function computeVoucherGrant(voucher, currentKey, now) {
4029
4739
  if (voucher.type === "credit") {
4030
- const current = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _154 => _154.totalCostLimitUsd]), () => ( 0));
4740
+ const current = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _234 => _234.totalCostLimitUsd]), () => ( 0));
4031
4741
  let granted2 = current + (_nullishCoalesce(voucher.creditUsd, () => ( 0)));
4032
4742
  if (voucher.maxTotalCostLimitUsd != null) {
4033
4743
  granted2 = Math.min(granted2, voucher.maxTotalCostLimitUsd);
4034
4744
  }
4035
4745
  return { totalCostLimitUsd: granted2 };
4036
4746
  }
4037
- const currentExpiry = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _155 => _155.expiresAt]), () => ( now));
4747
+ const currentExpiry = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _235 => _235.expiresAt]), () => ( now));
4038
4748
  let granted = currentExpiry + (_nullishCoalesce(voucher.renewalDays, () => ( 0))) * DAY_MS2;
4039
4749
  if (voucher.maxExpiryDays != null) {
4040
4750
  granted = Math.min(granted, now + voucher.maxExpiryDays * DAY_MS2);
@@ -4065,7 +4775,7 @@ function toVoucherInfo(v) {
4065
4775
  // src/outbound-api/voucherRedeem.ts
4066
4776
  function isRedeemRequest(method, url) {
4067
4777
  if (method !== "POST" || !url) return false;
4068
- const path = _nullishCoalesce(_optionalChain([url, 'access', _156 => _156.split, 'call', _157 => _157("?"), 'access', _158 => _158[0], 'optionalAccess', _159 => _159.replace, 'call', _160 => _160(/\/+$/, "")]), () => ( ""));
4778
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _236 => _236.split, 'call', _237 => _237("?"), 'access', _238 => _238[0], 'optionalAccess', _239 => _239.replace, 'call', _240 => _240(/\/+$/, "")]), () => ( ""));
4069
4779
  return path.endsWith("/redeem");
4070
4780
  }
4071
4781
  function writeErr(res, status, message, headers = {}) {
@@ -4095,8 +4805,8 @@ function grantToResult(type, granted) {
4095
4805
  function currentResult(type, keyRow) {
4096
4806
  const result = { type };
4097
4807
  if (type === "credit") {
4098
- if (_optionalChain([keyRow, 'optionalAccess', _161 => _161.totalCostLimitUsd]) != null) result.totalCostLimitUsd = keyRow.totalCostLimitUsd;
4099
- } else if (_optionalChain([keyRow, 'optionalAccess', _162 => _162.expiresAt]) != null) {
4808
+ if (_optionalChain([keyRow, 'optionalAccess', _241 => _241.totalCostLimitUsd]) != null) result.totalCostLimitUsd = keyRow.totalCostLimitUsd;
4809
+ } else if (_optionalChain([keyRow, 'optionalAccess', _242 => _242.expiresAt]) != null) {
4100
4810
  result.expiresAt = keyRow.expiresAt;
4101
4811
  }
4102
4812
  return result;
@@ -4134,7 +4844,7 @@ async function handleVoucherRedeem(req, res, deps, voucherEnabled, redeemLimiter
4134
4844
  let code;
4135
4845
  try {
4136
4846
  code = raw ? JSON.parse(raw)["code"] : void 0;
4137
- } catch (e24) {
4847
+ } catch (e27) {
4138
4848
  writeErr(res, 400, "Invalid JSON in request body");
4139
4849
  return;
4140
4850
  }
@@ -4183,7 +4893,7 @@ async function handleVoucherRedeem(req, res, deps, voucherEnabled, redeemLimiter
4183
4893
  const won = await voucherDb.voucherRedeemCas(voucher.id, verifiedKeyId, absolute, now);
4184
4894
  if (!won) {
4185
4895
  const after = await voucherDb.voucherGetByHash(codeHash);
4186
- if (_optionalChain([after, 'optionalAccess', _163 => _163.status]) === "redeemed" && after.redeemedByKeyId === verifiedKeyId) {
4896
+ if (_optionalChain([after, 'optionalAccess', _243 => _243.status]) === "redeemed" && after.redeemedByKeyId === verifiedKeyId) {
4187
4897
  return after.grantApplied === true ? { ok: true, result: currentResult(after.type, await deps.db.outboundApiKeysGetByHash(keyHash)) } : await applyFirstPass(after, recordedAbsolute(after));
4188
4898
  }
4189
4899
  return { ok: false, status: 409, message: "Code was already redeemed" };
@@ -4244,10 +4954,10 @@ function extractPresentedKey(req) {
4244
4954
  }
4245
4955
  const xApiKey = req.headers["x-api-key"];
4246
4956
  const xApi = Array.isArray(xApiKey) ? xApiKey[0] : xApiKey;
4247
- if (_optionalChain([xApi, 'optionalAccess', _164 => _164.trim, 'call', _165 => _165()])) return xApi.trim();
4957
+ if (_optionalChain([xApi, 'optionalAccess', _244 => _244.trim, 'call', _245 => _245()])) return xApi.trim();
4248
4958
  const goog = req.headers["x-goog-api-key"];
4249
4959
  const g = Array.isArray(goog) ? goog[0] : goog;
4250
- return _optionalChain([g, 'optionalAccess', _166 => _166.trim, 'call', _167 => _167()]) || void 0;
4960
+ return _optionalChain([g, 'optionalAccess', _246 => _246.trim, 'call', _247 => _247()]) || void 0;
4251
4961
  }
4252
4962
  function isLoopbackPeer(address) {
4253
4963
  if (!address) return false;
@@ -4256,7 +4966,7 @@ function isLoopbackPeer(address) {
4256
4966
  }
4257
4967
  function selectEndpoint(method, url) {
4258
4968
  if (method !== "POST" || !url) return null;
4259
- const path = _nullishCoalesce(_optionalChain([url, 'access', _168 => _168.split, 'call', _169 => _169("?"), 'access', _170 => _170[0], 'optionalAccess', _171 => _171.replace, 'call', _172 => _172(/\/+$/, "")]), () => ( ""));
4969
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _248 => _248.split, 'call', _249 => _249("?"), 'access', _250 => _250[0], 'optionalAccess', _251 => _251.replace, 'call', _252 => _252(/\/+$/, "")]), () => ( ""));
4260
4970
  if (path.endsWith("/chat/completions")) return "chat";
4261
4971
  if (path.endsWith("/responses")) return "responses";
4262
4972
  if (path.includes("/v1/messages")) return "messages";
@@ -4274,7 +4984,7 @@ function extractGeminiModelFromUrl(url) {
4274
4984
  }
4275
4985
  function isModelsListRequest(url) {
4276
4986
  if (!url) return false;
4277
- const path = _nullishCoalesce(_optionalChain([url, 'access', _173 => _173.split, 'call', _174 => _174("?"), 'access', _175 => _175[0], 'optionalAccess', _176 => _176.replace, 'call', _177 => _177(/\/+$/, "")]), () => ( ""));
4987
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _253 => _253.split, 'call', _254 => _254("?"), 'access', _255 => _255[0], 'optionalAccess', _256 => _256.replace, 'call', _257 => _257(/\/+$/, "")]), () => ( ""));
4278
4988
  return path.endsWith("/models");
4279
4989
  }
4280
4990
  function writeModelsList(res, config, apiKeyId, allowedEndpoints) {
@@ -4337,7 +5047,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4337
5047
  const verified = verification.key;
4338
5048
  if (audit) audit.keyId = verified.id;
4339
5049
  if (billing) billing.keyId = verified.id;
4340
- if (verified.loopbackOnly && !isLoopbackPeer(_optionalChain([req, 'access', _178 => _178.socket, 'optionalAccess', _179 => _179.remoteAddress]))) {
5050
+ if (verified.loopbackOnly && !isLoopbackPeer(_optionalChain([req, 'access', _258 => _258.socket, 'optionalAccess', _259 => _259.remoteAddress]))) {
4341
5051
  writeJsonError(res, 403, "This integration key is restricted to loopback clients");
4342
5052
  return;
4343
5053
  }
@@ -4350,7 +5060,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4350
5060
  req,
4351
5061
  res,
4352
5062
  deps,
4353
- _optionalChain([config, 'access', _180 => _180.voucher, 'optionalAccess', _181 => _181.enabled]) === true,
5063
+ _optionalChain([config, 'access', _260 => _260.voucher, 'optionalAccess', _261 => _261.enabled]) === true,
4354
5064
  _nullishCoalesce(redeemLimiter, () => ( getFallbackRedeemLimiter())),
4355
5065
  verified.id,
4356
5066
  _nullishCoalesce(presented, () => ( "")),
@@ -4448,7 +5158,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4448
5158
  let parsedBody = {};
4449
5159
  try {
4450
5160
  parsedBody = rawBody ? JSON.parse(rawBody) : {};
4451
- } catch (e25) {
5161
+ } catch (e28) {
4452
5162
  writeJsonError(res, 400, "Invalid JSON in request body");
4453
5163
  return;
4454
5164
  }
@@ -4519,7 +5229,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4519
5229
  }
4520
5230
  let releaseSerial = null;
4521
5231
  const providerKey = resolved.route.providerId;
4522
- if (_optionalChain([config, 'access', _182 => _182.userMessageQueue, 'optionalAccess', _183 => _183.enabled]) && providerKey && isUserMessageRequest(endpoint, parsedBody)) {
5232
+ if (_optionalChain([config, 'access', _262 => _262.userMessageQueue, 'optionalAccess', _263 => _263.enabled]) && providerKey && isUserMessageRequest(endpoint, parsedBody)) {
4523
5233
  const umq = config.userMessageQueue;
4524
5234
  try {
4525
5235
  const serialSlot = await serialQueue.acquire(providerKey, {
@@ -4597,37 +5307,37 @@ function makeReplayRequest(req, rawBody) {
4597
5307
  var DEFAULT_OUTBOUND_PORT = 8765;
4598
5308
  var LOOPBACK_ADDR = "127.0.0.1";
4599
5309
  var LAN_ADDR = "0.0.0.0";
4600
- var OutboundApiServer = (_class11 = class {
4601
- constructor(deps, onPortChange) {;_class11.prototype.__init12.call(this);_class11.prototype.__init13.call(this);_class11.prototype.__init14.call(this);_class11.prototype.__init15.call(this);_class11.prototype.__init16.call(this);_class11.prototype.__init17.call(this);_class11.prototype.__init18.call(this);_class11.prototype.__init19.call(this);_class11.prototype.__init20.call(this);_class11.prototype.__init21.call(this);
5310
+ var OutboundApiServer = (_class13 = class {
5311
+ constructor(deps, onPortChange) {;_class13.prototype.__init19.call(this);_class13.prototype.__init20.call(this);_class13.prototype.__init21.call(this);_class13.prototype.__init22.call(this);_class13.prototype.__init23.call(this);_class13.prototype.__init24.call(this);_class13.prototype.__init25.call(this);_class13.prototype.__init26.call(this);_class13.prototype.__init27.call(this);_class13.prototype.__init28.call(this);
4602
5312
  this.deps = deps;
4603
5313
  this.onPortChange = onPortChange;
4604
5314
  }
4605
5315
 
4606
5316
 
4607
- __init12() {this.server = null}
4608
- __init13() {this.boundPort = 0}
4609
- __init14() {this.boundAddr = LOOPBACK_ADDR}
4610
- __init15() {this.endpoints = []}
4611
- __init16() {this.bindings = []}
5317
+ __init19() {this.server = null}
5318
+ __init20() {this.boundPort = 0}
5319
+ __init21() {this.boundAddr = LOOPBACK_ADDR}
5320
+ __init22() {this.endpoints = []}
5321
+ __init23() {this.bindings = []}
4612
5322
 
4613
5323
 
4614
5324
 
4615
- __init17() {this.rateLimiter = new OutboundRateLimiter()}
5325
+ __init24() {this.rateLimiter = new OutboundRateLimiter()}
4616
5326
  /**
4617
5327
  * Redeem-attempt limiter (voucher-redemption #9, design D6) — a SEPARATE bucket
4618
5328
  * from the traffic `rateLimiter`, keyed by the authenticating key id, so
4619
5329
  * brute-forcing `CC_` codes is throttled (a handful/min) without touching the
4620
5330
  * per-key request rate. Conservative fixed defaults (10 / 60s).
4621
5331
  */
4622
- __init18() {this.redeemLimiter = new OutboundRateLimiter({ maxRequests: 10, windowMs: 6e4 })}
5332
+ __init25() {this.redeemLimiter = new OutboundRateLimiter({ maxRequests: 10, windowMs: 6e4 })}
4623
5333
  /**
4624
5334
  * Per-key redeem mutex (voucher-redemption #9, MJ1 fix). One instance for the
4625
5335
  * server's lifetime so concurrent redeem REQUESTS for the same key serialize
4626
5336
  * (relative grant increments accumulate instead of clobbering a snapshot).
4627
5337
  */
4628
- __init19() {this.redeemMutex = new KeyedMutex()}
4629
- __init20() {this.serialQueue = new UserMessageSerialQueue()}
4630
- __init21() {this.concurrencyGate = new OutboundConcurrencyGate()}
5338
+ __init26() {this.redeemMutex = new KeyedMutex()}
5339
+ __init27() {this.serialQueue = new UserMessageSerialQueue()}
5340
+ __init28() {this.concurrencyGate = new OutboundConcurrencyGate()}
4631
5341
  /**
4632
5342
  * Apply a config. Restarts the listener ONLY when the bind address or port
4633
5343
  * changes (or when toggling enabled); per-endpoint routing config is updated
@@ -4659,7 +5369,7 @@ var OutboundApiServer = (_class11 = class {
4659
5369
  const actualPort = await this.listen(bindAddr, port);
4660
5370
  this.boundAddr = bindAddr;
4661
5371
  this.boundPort = actualPort;
4662
- if (actualPort !== port) _optionalChain([this, 'access', _184 => _184.onPortChange, 'optionalCall', _185 => _185(actualPort)]);
5372
+ if (actualPort !== port) _optionalChain([this, 'access', _264 => _264.onPortChange, 'optionalCall', _265 => _265(actualPort)]);
4663
5373
  this.logInfo(`[OutboundApiServer] Listening on ${bindAddr}:${actualPort}`);
4664
5374
  return actualPort;
4665
5375
  }
@@ -4728,7 +5438,7 @@ var OutboundApiServer = (_class11 = class {
4728
5438
  const provider = this.deps.healthReportProvider;
4729
5439
  if (!provider) return false;
4730
5440
  if (req.method !== "GET" && req.method !== "HEAD") return false;
4731
- const path = _optionalChain([(_nullishCoalesce(req.url, () => ( "/"))), 'access', _186 => _186.split, 'call', _187 => _187("?"), 'access', _188 => _188[0], 'optionalAccess', _189 => _189.replace, 'call', _190 => _190(/\/+$/, "")]) || "/";
5441
+ const path = _optionalChain([(_nullishCoalesce(req.url, () => ( "/"))), 'access', _266 => _266.split, 'call', _267 => _267("?"), 'access', _268 => _268[0], 'optionalAccess', _269 => _269.replace, 'call', _270 => _270(/\/+$/, "")]) || "/";
4732
5442
  if (path !== "/health" && path !== "/healthz") return false;
4733
5443
  const report = provider();
4734
5444
  res.writeHead(_healthloggingtypes.healthHttpStatus.call(void 0, report.status), { "Content-Type": "application/json" });
@@ -4798,7 +5508,7 @@ var OutboundApiServer = (_class11 = class {
4798
5508
  concurrency: this.concurrencyGate.getStatus()
4799
5509
  };
4800
5510
  }
4801
- }, _class11);
5511
+ }, _class13);
4802
5512
  function formatUrls(base) {
4803
5513
  return {
4804
5514
  chat: `${base}/v1/chat/completions`,
@@ -4839,13 +5549,13 @@ function startOfLocalWeek(ts) {
4839
5549
  day.setDate(day.getDate() - day.getDay());
4840
5550
  return day.getTime();
4841
5551
  }
4842
- var KeySpendTracker = (_class12 = class {
4843
- constructor(seeder) {;_class12.prototype.__init22.call(this);
5552
+ var KeySpendTracker = (_class14 = class {
5553
+ constructor(seeder) {;_class14.prototype.__init29.call(this);
4844
5554
  this.seeder = seeder;
4845
5555
  }
4846
5556
 
4847
5557
  /** apiKeyId → live accumulated spend (seeded lazily). */
4848
- __init22() {this.spend = /* @__PURE__ */ new Map()}
5558
+ __init29() {this.spend = /* @__PURE__ */ new Map()}
4849
5559
  /**
4850
5560
  * Read the key's accumulated spend, seeding from the durable store on a cache
4851
5561
  * miss and rolling the daily/weekly windows on a hit.
@@ -4898,7 +5608,7 @@ var KeySpendTracker = (_class12 = class {
4898
5608
  });
4899
5609
  return { dailyUsd, dailyWindowStart, weeklyUsd, weeklyWindowStart, totalUsd };
4900
5610
  }
4901
- }, _class12);
5611
+ }, _class14);
4902
5612
  function rollWindows(s, now) {
4903
5613
  const day = startOfLocalDay(now);
4904
5614
  if (s.dailyWindowStart !== day) {
@@ -5013,7 +5723,7 @@ async function callAnthropicCompletion(provider, apiKey, options, logger) {
5013
5723
  message: {
5014
5724
  id: `assistant-${Date.now()}`,
5015
5725
  role: "assistant",
5016
- content: _optionalChain([choice2, 'access', _191 => _191.message, 'optionalAccess', _192 => _192.content]) || "",
5726
+ content: _optionalChain([choice2, 'access', _271 => _271.message, 'optionalAccess', _272 => _272.content]) || "",
5017
5727
  timestamp: Date.now()
5018
5728
  },
5019
5729
  usage: openaiResponse2.usage ? {
@@ -5118,7 +5828,7 @@ async function callGeminiCompletion(provider, apiKey, options, logger) {
5118
5828
  const candidate = candidates[0];
5119
5829
  const content = candidate.content;
5120
5830
  let textContent = "";
5121
- if (_optionalChain([content, 'optionalAccess', _193 => _193.parts])) {
5831
+ if (_optionalChain([content, 'optionalAccess', _273 => _273.parts])) {
5122
5832
  for (const part of content.parts) {
5123
5833
  if (part.text) {
5124
5834
  textContent += part.text;
@@ -5251,7 +5961,7 @@ async function streamOpenAICompletion(provider, apiKey, options, messageId, call
5251
5961
  });
5252
5962
  if (!response.ok) {
5253
5963
  const errorText = await response.text();
5254
- _optionalChain([callbacks, 'access', _194 => _194.onError, 'optionalCall', _195 => _195(`API error (${response.status}): ${errorText}`)]);
5964
+ _optionalChain([callbacks, 'access', _274 => _274.onError, 'optionalCall', _275 => _275(`API error (${response.status}): ${errorText}`)]);
5255
5965
  return;
5256
5966
  }
5257
5967
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "openai", {
@@ -5261,7 +5971,7 @@ async function streamOpenAICompletion(provider, apiKey, options, messageId, call
5261
5971
  onVideo: callbacks.onVideo,
5262
5972
  onError: callbacks.onError
5263
5973
  });
5264
- _optionalChain([callbacks, 'access', _196 => _196.onDone, 'optionalCall', _197 => _197(
5974
+ _optionalChain([callbacks, 'access', _276 => _276.onDone, 'optionalCall', _277 => _277(
5265
5975
  {
5266
5976
  id: messageId,
5267
5977
  role: "assistant",
@@ -5333,7 +6043,7 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5333
6043
  logger.info("Streaming Anthropic completion request", {
5334
6044
  url: apiUrl,
5335
6045
  model: anthropicRequest.model,
5336
- messagesCount: _optionalChain([anthropicRequest, 'access', _198 => _198.messages, 'optionalAccess', _199 => _199.length]),
6046
+ messagesCount: _optionalChain([anthropicRequest, 'access', _278 => _278.messages, 'optionalAccess', _279 => _279.length]),
5337
6047
  max_tokens: anthropicRequest.max_tokens,
5338
6048
  temperature: anthropicRequest.temperature,
5339
6049
  stream: anthropicRequest.stream,
@@ -5350,7 +6060,7 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5350
6060
  if (!response.ok) {
5351
6061
  const errorText = await response.text();
5352
6062
  logger.error("Anthropic API error", void 0, { status: response.status, errorText });
5353
- _optionalChain([callbacks, 'access', _200 => _200.onError, 'optionalCall', _201 => _201(`API error (${response.status}): ${errorText}`)]);
6063
+ _optionalChain([callbacks, 'access', _280 => _280.onError, 'optionalCall', _281 => _281(`API error (${response.status}): ${errorText}`)]);
5354
6064
  return;
5355
6065
  }
5356
6066
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "anthropic", {
@@ -5361,10 +6071,10 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5361
6071
  });
5362
6072
  logger.info("Anthropic stream complete", {
5363
6073
  contentLength: result.content.length,
5364
- reasoningLength: _optionalChain([result, 'access', _202 => _202.reasoning, 'optionalAccess', _203 => _203.length]) || 0,
6074
+ reasoningLength: _optionalChain([result, 'access', _282 => _282.reasoning, 'optionalAccess', _283 => _283.length]) || 0,
5365
6075
  blocksCount: result.blocks.length
5366
6076
  });
5367
- _optionalChain([callbacks, 'access', _204 => _204.onDone, 'optionalCall', _205 => _205(
6077
+ _optionalChain([callbacks, 'access', _284 => _284.onDone, 'optionalCall', _285 => _285(
5368
6078
  {
5369
6079
  id: messageId,
5370
6080
  role: "assistant",
@@ -5420,7 +6130,7 @@ async function streamGeminiCompletion(provider, apiKey, options, messageId, call
5420
6130
  if (!response.ok) {
5421
6131
  const errorText = await response.text();
5422
6132
  logger.error("Gemini API error", void 0, { status: response.status, errorText });
5423
- _optionalChain([callbacks, 'access', _206 => _206.onError, 'optionalCall', _207 => _207(`API error (${response.status}): ${errorText}`)]);
6133
+ _optionalChain([callbacks, 'access', _286 => _286.onError, 'optionalCall', _287 => _287(`API error (${response.status}): ${errorText}`)]);
5424
6134
  return;
5425
6135
  }
5426
6136
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "gemini", {
@@ -5429,7 +6139,7 @@ async function streamGeminiCompletion(provider, apiKey, options, messageId, call
5429
6139
  onError: callbacks.onError
5430
6140
  });
5431
6141
  logger.info("Gemini stream complete", { contentLength: result.content.length });
5432
- _optionalChain([callbacks, 'access', _208 => _208.onDone, 'optionalCall', _209 => _209(
6142
+ _optionalChain([callbacks, 'access', _288 => _288.onDone, 'optionalCall', _289 => _289(
5433
6143
  {
5434
6144
  id: messageId,
5435
6145
  role: "assistant",
@@ -5481,7 +6191,7 @@ async function streamOpenAIResponseCompletion(provider, apiKey, options, message
5481
6191
  });
5482
6192
  if (!response.ok) {
5483
6193
  const errorText = await response.text();
5484
- _optionalChain([callbacks, 'access', _210 => _210.onError, 'optionalCall', _211 => _211(`API error (${response.status}): ${errorText}`)]);
6194
+ _optionalChain([callbacks, 'access', _290 => _290.onError, 'optionalCall', _291 => _291(`API error (${response.status}): ${errorText}`)]);
5485
6195
  return;
5486
6196
  }
5487
6197
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "openai-response", {
@@ -5490,7 +6200,7 @@ async function streamOpenAIResponseCompletion(provider, apiKey, options, message
5490
6200
  onError: callbacks.onError
5491
6201
  });
5492
6202
  logger.info("OpenAI Response API stream complete", { contentLength: result.content.length });
5493
- _optionalChain([callbacks, 'access', _212 => _212.onDone, 'optionalCall', _213 => _213(
6203
+ _optionalChain([callbacks, 'access', _292 => _292.onDone, 'optionalCall', _293 => _293(
5494
6204
  {
5495
6205
  id: messageId,
5496
6206
  role: "assistant",
@@ -5518,7 +6228,7 @@ async function resolveEffectiveMaxTokens(llmConfig, _getProvider, logger, _provi
5518
6228
  }
5519
6229
  try {
5520
6230
  const globalParams = await llmConfig.getGlobalModelParameters();
5521
- if (_optionalChain([globalParams, 'optionalAccess', _214 => _214.maxTokens, 'optionalAccess', _215 => _215.enabled]) && globalParams.maxTokens.value > 0) {
6231
+ if (_optionalChain([globalParams, 'optionalAccess', _294 => _294.maxTokens, 'optionalAccess', _295 => _295.enabled]) && globalParams.maxTokens.value > 0) {
5522
6232
  logger.debug("Using global maxTokens", { maxTokens: globalParams.maxTokens.value });
5523
6233
  return globalParams.maxTokens.value;
5524
6234
  }
@@ -5546,7 +6256,7 @@ async function resolveThinkingBudget(getProvider, logger, providerId, modelId, m
5546
6256
  }
5547
6257
  const thinkingBudget = _thinkingconfig.calculateThinkingBudget.call(void 0, modelId, thinkLevel, maxTokens);
5548
6258
  const provider = await getProvider(providerId);
5549
- const providerName = _optionalChain([provider, 'optionalAccess', _216 => _216.name, 'optionalAccess', _217 => _217.toLowerCase, 'call', _218 => _218()]) || "";
6259
+ const providerName = _optionalChain([provider, 'optionalAccess', _296 => _296.name, 'optionalAccess', _297 => _297.toLowerCase, 'call', _298 => _298()]) || "";
5550
6260
  const apiFormat = provider ? _chunkA2YMUCBTcjs.resolveApiFormat.call(void 0, provider) : "openai";
5551
6261
  if (apiFormat === "anthropic" || providerName === "anthropic" || providerName.includes("claude")) {
5552
6262
  const thinkingConfig = _thinkingconfig.buildAnthropicThinking.call(void 0, modelId, thinkLevel, maxTokens);
@@ -5583,7 +6293,7 @@ function logToolFormat(tools, logger) {
5583
6293
  tools: openaiTools.map((t) => t.function.name),
5584
6294
  firstTool: {
5585
6295
  name: firstTool.function.name,
5586
- description: _optionalChain([firstTool, 'access', _219 => _219.function, 'access', _220 => _220.description, 'optionalAccess', _221 => _221.slice, 'call', _222 => _222(0, 100)]),
6296
+ description: _optionalChain([firstTool, 'access', _299 => _299.function, 'access', _300 => _300.description, 'optionalAccess', _301 => _301.slice, 'call', _302 => _302(0, 100)]),
5587
6297
  parameters: firstTool.function.parameters
5588
6298
  }
5589
6299
  });
@@ -5594,7 +6304,7 @@ function logToolFormat(tools, logger) {
5594
6304
  tools: anthropicTools.map((t) => t.name),
5595
6305
  firstTool: {
5596
6306
  name: firstTool.name,
5597
- description: _optionalChain([firstTool, 'access', _223 => _223.description, 'optionalAccess', _224 => _224.slice, 'call', _225 => _225(0, 100)]),
6307
+ description: _optionalChain([firstTool, 'access', _303 => _303.description, 'optionalAccess', _304 => _304.slice, 'call', _305 => _305(0, 100)]),
5598
6308
  input_schema: firstTool.input_schema
5599
6309
  }
5600
6310
  });
@@ -5605,9 +6315,9 @@ function logToolFormat(tools, logger) {
5605
6315
  count: allDeclarations.length,
5606
6316
  tools: allDeclarations.map((t) => t.name),
5607
6317
  firstTool: {
5608
- name: _optionalChain([allDeclarations, 'access', _226 => _226[0], 'optionalAccess', _227 => _227.name]),
5609
- description: _optionalChain([allDeclarations, 'access', _228 => _228[0], 'optionalAccess', _229 => _229.description, 'optionalAccess', _230 => _230.slice, 'call', _231 => _231(0, 100)]),
5610
- parameters: _optionalChain([allDeclarations, 'access', _232 => _232[0], 'optionalAccess', _233 => _233.parameters])
6318
+ name: _optionalChain([allDeclarations, 'access', _306 => _306[0], 'optionalAccess', _307 => _307.name]),
6319
+ description: _optionalChain([allDeclarations, 'access', _308 => _308[0], 'optionalAccess', _309 => _309.description, 'optionalAccess', _310 => _310.slice, 'call', _311 => _311(0, 100)]),
6320
+ parameters: _optionalChain([allDeclarations, 'access', _312 => _312[0], 'optionalAccess', _313 => _313.parameters])
5611
6321
  }
5612
6322
  });
5613
6323
  }
@@ -5670,7 +6380,7 @@ function buildToolRequest(apiFormat, conversationMessages, actualModel, options,
5670
6380
  function extractDeltaContent(rawJson, apiFormat) {
5671
6381
  const json = rawJson;
5672
6382
  if (apiFormat === "google") {
5673
- const parts = _optionalChain([json, 'access', _234 => _234.candidates, 'optionalAccess', _235 => _235[0], 'optionalAccess', _236 => _236.content, 'optionalAccess', _237 => _237.parts]) || [];
6383
+ const parts = _optionalChain([json, 'access', _314 => _314.candidates, 'optionalAccess', _315 => _315[0], 'optionalAccess', _316 => _316.content, 'optionalAccess', _317 => _317.parts]) || [];
5674
6384
  let text = "";
5675
6385
  for (const part of parts) {
5676
6386
  if (part.thought === true) continue;
@@ -5678,15 +6388,15 @@ function extractDeltaContent(rawJson, apiFormat) {
5678
6388
  }
5679
6389
  return text;
5680
6390
  } else if (apiFormat === "anthropic") {
5681
- return _optionalChain([json, 'access', _238 => _238.delta, 'optionalAccess', _239 => _239.text]) || "";
6391
+ return _optionalChain([json, 'access', _318 => _318.delta, 'optionalAccess', _319 => _319.text]) || "";
5682
6392
  } else {
5683
- return _optionalChain([json, 'access', _240 => _240.choices, 'optionalAccess', _241 => _241[0], 'optionalAccess', _242 => _242.delta, 'optionalAccess', _243 => _243.content]) || "";
6393
+ return _optionalChain([json, 'access', _320 => _320.choices, 'optionalAccess', _321 => _321[0], 'optionalAccess', _322 => _322.delta, 'optionalAccess', _323 => _323.content]) || "";
5684
6394
  }
5685
6395
  }
5686
6396
  function extractDeltaReasoning(rawJson, apiFormat) {
5687
6397
  const json = rawJson;
5688
6398
  if (apiFormat === "google") {
5689
- const parts = _optionalChain([json, 'access', _244 => _244.candidates, 'optionalAccess', _245 => _245[0], 'optionalAccess', _246 => _246.content, 'optionalAccess', _247 => _247.parts]) || [];
6399
+ const parts = _optionalChain([json, 'access', _324 => _324.candidates, 'optionalAccess', _325 => _325[0], 'optionalAccess', _326 => _326.content, 'optionalAccess', _327 => _327.parts]) || [];
5690
6400
  let reasoning = "";
5691
6401
  for (const part of parts) {
5692
6402
  if (part.thought === true && part.text) reasoning += part.text;
@@ -5695,14 +6405,14 @@ function extractDeltaReasoning(rawJson, apiFormat) {
5695
6405
  } else if (apiFormat === "anthropic") {
5696
6406
  return "";
5697
6407
  } else {
5698
- const delta = _optionalChain([json, 'access', _248 => _248.choices, 'optionalAccess', _249 => _249[0], 'optionalAccess', _250 => _250.delta]);
5699
- return (_optionalChain([delta, 'optionalAccess', _251 => _251.thinking, 'optionalAccess', _252 => _252.content]) || "") + (_optionalChain([delta, 'optionalAccess', _253 => _253.reasoning_content]) || "");
6408
+ const delta = _optionalChain([json, 'access', _328 => _328.choices, 'optionalAccess', _329 => _329[0], 'optionalAccess', _330 => _330.delta]);
6409
+ return (_optionalChain([delta, 'optionalAccess', _331 => _331.thinking, 'optionalAccess', _332 => _332.content]) || "") + (_optionalChain([delta, 'optionalAccess', _333 => _333.reasoning_content]) || "");
5700
6410
  }
5701
6411
  }
5702
6412
  function extractToolCalls(rawJson, apiFormat, toolCalls, callbacks, logger, pendingOpenAIToolCalls) {
5703
6413
  const json = rawJson;
5704
6414
  if (apiFormat === "google") {
5705
- const parts = _optionalChain([json, 'access', _254 => _254.candidates, 'optionalAccess', _255 => _255[0], 'optionalAccess', _256 => _256.content, 'optionalAccess', _257 => _257.parts]) || [];
6415
+ const parts = _optionalChain([json, 'access', _334 => _334.candidates, 'optionalAccess', _335 => _335[0], 'optionalAccess', _336 => _336.content, 'optionalAccess', _337 => _337.parts]) || [];
5706
6416
  for (const part of parts) {
5707
6417
  if (part.functionCall) {
5708
6418
  logger.info("Function call detected", { functionCall: part.functionCall });
@@ -5712,13 +6422,13 @@ function extractToolCalls(rawJson, apiFormat, toolCalls, callbacks, logger, pend
5712
6422
  args: part.functionCall.args || {}
5713
6423
  };
5714
6424
  toolCalls.push(toolCall);
5715
- _optionalChain([callbacks, 'access', _258 => _258.onToolCall, 'optionalCall', _259 => _259(toolCall)]);
6425
+ _optionalChain([callbacks, 'access', _338 => _338.onToolCall, 'optionalCall', _339 => _339(toolCall)]);
5716
6426
  logger.info("Tool called", { toolName: toolCall.name });
5717
6427
  }
5718
6428
  }
5719
6429
  } else if (apiFormat !== "anthropic") {
5720
- const delta = _optionalChain([json, 'access', _260 => _260.choices, 'optionalAccess', _261 => _261[0], 'optionalAccess', _262 => _262.delta]);
5721
- if (_optionalChain([delta, 'optionalAccess', _263 => _263.tool_calls]) && pendingOpenAIToolCalls) {
6430
+ const delta = _optionalChain([json, 'access', _340 => _340.choices, 'optionalAccess', _341 => _341[0], 'optionalAccess', _342 => _342.delta]);
6431
+ if (_optionalChain([delta, 'optionalAccess', _343 => _343.tool_calls]) && pendingOpenAIToolCalls) {
5722
6432
  for (const tc of delta.tool_calls) {
5723
6433
  const index = _nullishCoalesce(tc.index, () => ( 0));
5724
6434
  let pending = pendingOpenAIToolCalls.get(index);
@@ -5727,8 +6437,8 @@ function extractToolCalls(rawJson, apiFormat, toolCalls, callbacks, logger, pend
5727
6437
  pendingOpenAIToolCalls.set(index, pending);
5728
6438
  }
5729
6439
  if (tc.id) pending.id = tc.id;
5730
- if (_optionalChain([tc, 'access', _264 => _264.function, 'optionalAccess', _265 => _265.name])) pending.name += tc.function.name;
5731
- if (_optionalChain([tc, 'access', _266 => _266.function, 'optionalAccess', _267 => _267.arguments])) pending.arguments += tc.function.arguments;
6440
+ if (_optionalChain([tc, 'access', _344 => _344.function, 'optionalAccess', _345 => _345.name])) pending.name += tc.function.name;
6441
+ if (_optionalChain([tc, 'access', _346 => _346.function, 'optionalAccess', _347 => _347.arguments])) pending.arguments += tc.function.arguments;
5732
6442
  }
5733
6443
  }
5734
6444
  }
@@ -5743,7 +6453,7 @@ function finalizeOpenAIToolCalls(pendingOpenAIToolCalls, toolCalls, callbacks, l
5743
6453
  args: JSON.parse(pending.arguments || "{}")
5744
6454
  };
5745
6455
  toolCalls.push(toolCall);
5746
- _optionalChain([callbacks, 'access', _268 => _268.onToolCall, 'optionalCall', _269 => _269(toolCall)]);
6456
+ _optionalChain([callbacks, 'access', _348 => _348.onToolCall, 'optionalCall', _349 => _349(toolCall)]);
5747
6457
  logger.info("Tool called (OpenAI)", { toolName: toolCall.name, args: toolCall.args });
5748
6458
  } catch (e) {
5749
6459
  logger.error("Failed to parse OpenAI tool call arguments", e instanceof Error ? e : void 0, {
@@ -5757,38 +6467,38 @@ function finalizeOpenAIToolCalls(pendingOpenAIToolCalls, toolCalls, callbacks, l
5757
6467
  function parseStreamChunk(rawJson, apiFormat, callbacks, logger) {
5758
6468
  const json = rawJson;
5759
6469
  if (apiFormat === "google") {
5760
- const candidate = _optionalChain([json, 'access', _270 => _270.candidates, 'optionalAccess', _271 => _271[0]]);
6470
+ const candidate = _optionalChain([json, 'access', _350 => _350.candidates, 'optionalAccess', _351 => _351[0]]);
5761
6471
  if (!candidate) {
5762
6472
  logger.warn("No candidate found in Gemini response");
5763
6473
  return;
5764
6474
  }
5765
- const parts = _optionalChain([candidate, 'access', _272 => _272.content, 'optionalAccess', _273 => _273.parts]) || [];
6475
+ const parts = _optionalChain([candidate, 'access', _352 => _352.content, 'optionalAccess', _353 => _353.parts]) || [];
5766
6476
  for (const part of parts) {
5767
6477
  if (part.thought === true && part.text) {
5768
- _optionalChain([callbacks, 'access', _274 => _274.onReasoning, 'optionalCall', _275 => _275(part.text)]);
6478
+ _optionalChain([callbacks, 'access', _354 => _354.onReasoning, 'optionalCall', _355 => _355(part.text)]);
5769
6479
  continue;
5770
6480
  } else if (part.thoughtSignature && !part.thought) {
5771
6481
  logger.debug("Gemini returned thoughtSignature without thought content");
5772
6482
  }
5773
6483
  if (part.text) {
5774
- _optionalChain([callbacks, 'access', _276 => _276.onDelta, 'optionalCall', _277 => _277(part.text)]);
6484
+ _optionalChain([callbacks, 'access', _356 => _356.onDelta, 'optionalCall', _357 => _357(part.text)]);
5775
6485
  }
5776
6486
  }
5777
6487
  } else if (apiFormat === "anthropic") {
5778
6488
  const delta = json.delta;
5779
- if (_optionalChain([delta, 'optionalAccess', _278 => _278.text])) {
5780
- _optionalChain([callbacks, 'access', _279 => _279.onDelta, 'optionalCall', _280 => _280(delta.text)]);
6489
+ if (_optionalChain([delta, 'optionalAccess', _358 => _358.text])) {
6490
+ _optionalChain([callbacks, 'access', _359 => _359.onDelta, 'optionalCall', _360 => _360(delta.text)]);
5781
6491
  }
5782
6492
  } else {
5783
- const delta = _optionalChain([json, 'access', _281 => _281.choices, 'optionalAccess', _282 => _282[0], 'optionalAccess', _283 => _283.delta]);
5784
- if (_optionalChain([delta, 'optionalAccess', _284 => _284.content])) {
5785
- _optionalChain([callbacks, 'access', _285 => _285.onDelta, 'optionalCall', _286 => _286(delta.content)]);
6493
+ const delta = _optionalChain([json, 'access', _361 => _361.choices, 'optionalAccess', _362 => _362[0], 'optionalAccess', _363 => _363.delta]);
6494
+ if (_optionalChain([delta, 'optionalAccess', _364 => _364.content])) {
6495
+ _optionalChain([callbacks, 'access', _365 => _365.onDelta, 'optionalCall', _366 => _366(delta.content)]);
5786
6496
  }
5787
- if (_optionalChain([delta, 'optionalAccess', _287 => _287.thinking, 'optionalAccess', _288 => _288.content])) {
5788
- _optionalChain([callbacks, 'access', _289 => _289.onReasoning, 'optionalCall', _290 => _290(delta.thinking.content)]);
6497
+ if (_optionalChain([delta, 'optionalAccess', _367 => _367.thinking, 'optionalAccess', _368 => _368.content])) {
6498
+ _optionalChain([callbacks, 'access', _369 => _369.onReasoning, 'optionalCall', _370 => _370(delta.thinking.content)]);
5789
6499
  }
5790
- if (_optionalChain([delta, 'optionalAccess', _291 => _291.reasoning_content])) {
5791
- _optionalChain([callbacks, 'access', _292 => _292.onReasoning, 'optionalCall', _293 => _293(delta.reasoning_content)]);
6500
+ if (_optionalChain([delta, 'optionalAccess', _371 => _371.reasoning_content])) {
6501
+ _optionalChain([callbacks, 'access', _372 => _372.onReasoning, 'optionalCall', _373 => _373(delta.reasoning_content)]);
5792
6502
  }
5793
6503
  }
5794
6504
  }
@@ -5801,7 +6511,7 @@ function buildIterationBlocks(iteration, content, reasoning, blocks, callbacks)
5801
6511
  content: reasoning
5802
6512
  };
5803
6513
  blocks.push(thinkingBlock);
5804
- _optionalChain([callbacks, 'access', _294 => _294.onBlock, 'optionalCall', _295 => _295(thinkingBlock)]);
6514
+ _optionalChain([callbacks, 'access', _374 => _374.onBlock, 'optionalCall', _375 => _375(thinkingBlock)]);
5805
6515
  }
5806
6516
  if (content && content.trim()) {
5807
6517
  const textBlock = {
@@ -5810,7 +6520,7 @@ function buildIterationBlocks(iteration, content, reasoning, blocks, callbacks)
5810
6520
  content
5811
6521
  };
5812
6522
  blocks.push(textBlock);
5813
- _optionalChain([callbacks, 'access', _296 => _296.onBlock, 'optionalCall', _297 => _297(textBlock)]);
6523
+ _optionalChain([callbacks, 'access', _376 => _376.onBlock, 'optionalCall', _377 => _377(textBlock)]);
5814
6524
  }
5815
6525
  }
5816
6526
  async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbacks, logger, builtinExecutor) {
@@ -5825,7 +6535,7 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5825
6535
  status: "running"
5826
6536
  };
5827
6537
  blocks.push(toolUseBlock);
5828
- _optionalChain([callbacks, 'access', _298 => _298.onBlock, 'optionalCall', _299 => _299(toolUseBlock)]);
6538
+ _optionalChain([callbacks, 'access', _378 => _378.onBlock, 'optionalCall', _379 => _379(toolUseBlock)]);
5829
6539
  try {
5830
6540
  logger.info("Executing tool", { toolName: toolCall.name });
5831
6541
  const mcpTool = mcpTools.find((t) => t.id === toolCall.name);
@@ -5848,8 +6558,8 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5848
6558
  result = { isError: true, content: [{ type: "text", text: "MCP service not available" }] };
5849
6559
  }
5850
6560
  logger.info("Tool result received", { result });
5851
- _optionalChain([callbacks, 'access', _300 => _300.onToolResult, 'optionalCall', _301 => _301(toolCall.id, result)]);
5852
- const resultText = _optionalChain([result, 'access', _302 => _302.content, 'optionalAccess', _303 => _303[0], 'optionalAccess', _304 => _304.text]) || JSON.stringify(result);
6561
+ _optionalChain([callbacks, 'access', _380 => _380.onToolResult, 'optionalCall', _381 => _381(toolCall.id, result)]);
6562
+ const resultText = _optionalChain([result, 'access', _382 => _382.content, 'optionalAccess', _383 => _383[0], 'optionalAccess', _384 => _384.text]) || JSON.stringify(result);
5853
6563
  const toolResultBlock = {
5854
6564
  id: `${toolCall.id}_result`,
5855
6565
  type: "tool_result",
@@ -5859,11 +6569,11 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5859
6569
  isError: result.isError
5860
6570
  };
5861
6571
  blocks.push(toolResultBlock);
5862
- _optionalChain([callbacks, 'access', _305 => _305.onBlock, 'optionalCall', _306 => _306(toolResultBlock)]);
6572
+ _optionalChain([callbacks, 'access', _385 => _385.onBlock, 'optionalCall', _386 => _386(toolResultBlock)]);
5863
6573
  const toolUseBlockRef = blocks.find((b) => b.id === `${toolCall.id}_use`);
5864
6574
  if (toolUseBlockRef && toolUseBlockRef.type === "tool_use") {
5865
6575
  toolUseBlockRef.status = result.isError ? "error" : "completed";
5866
- _optionalChain([callbacks, 'access', _307 => _307.onBlock, 'optionalCall', _308 => _308({ ...toolUseBlockRef })]);
6576
+ _optionalChain([callbacks, 'access', _387 => _387.onBlock, 'optionalCall', _388 => _388({ ...toolUseBlockRef })]);
5867
6577
  }
5868
6578
  toolResults.push({ toolCall, result });
5869
6579
  } catch (error) {
@@ -5873,7 +6583,7 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5873
6583
  isError: true,
5874
6584
  content: [{ type: "text", text: `Error: ${errorMessage}` }]
5875
6585
  };
5876
- _optionalChain([callbacks, 'access', _309 => _309.onToolResult, 'optionalCall', _310 => _310(toolCall.id, errorResult)]);
6586
+ _optionalChain([callbacks, 'access', _389 => _389.onToolResult, 'optionalCall', _390 => _390(toolCall.id, errorResult)]);
5877
6587
  const errorToolResultBlock = {
5878
6588
  id: `${toolCall.id}_result`,
5879
6589
  type: "tool_result",
@@ -5883,11 +6593,11 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5883
6593
  isError: true
5884
6594
  };
5885
6595
  blocks.push(errorToolResultBlock);
5886
- _optionalChain([callbacks, 'access', _311 => _311.onBlock, 'optionalCall', _312 => _312(errorToolResultBlock)]);
6596
+ _optionalChain([callbacks, 'access', _391 => _391.onBlock, 'optionalCall', _392 => _392(errorToolResultBlock)]);
5887
6597
  const errorToolUseBlockRef = blocks.find((b) => b.id === `${toolCall.id}_use`);
5888
6598
  if (errorToolUseBlockRef && errorToolUseBlockRef.type === "tool_use") {
5889
6599
  errorToolUseBlockRef.status = "error";
5890
- _optionalChain([callbacks, 'access', _313 => _313.onBlock, 'optionalCall', _314 => _314({ ...errorToolUseBlockRef })]);
6600
+ _optionalChain([callbacks, 'access', _393 => _393.onBlock, 'optionalCall', _394 => _394({ ...errorToolUseBlockRef })]);
5891
6601
  }
5892
6602
  toolResults.push({ toolCall, result: errorResult });
5893
6603
  }
@@ -5897,7 +6607,7 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5897
6607
  function mapToolArguments(args, mcpTool, logger) {
5898
6608
  const mappedArgs = { ...args };
5899
6609
  const toolSchema = mcpTool.inputSchema;
5900
- if (_optionalChain([toolSchema, 'optionalAccess', _315 => _315.properties])) {
6610
+ if (_optionalChain([toolSchema, 'optionalAccess', _395 => _395.properties])) {
5901
6611
  const expectedParams = Object.keys(toolSchema.properties);
5902
6612
  const paramMappings = {
5903
6613
  "search_query": ["query", "q", "searchQuery", "search"],
@@ -5931,25 +6641,25 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
5931
6641
  options.providerId,
5932
6642
  options.model
5933
6643
  );
5934
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _316 => _316.actualProviderId]) || options.providerId;
5935
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _317 => _317.actualModelId]) || options.model;
6644
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _396 => _396.actualProviderId]) || options.providerId;
6645
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _397 => _397.actualModelId]) || options.model;
5936
6646
  const provider = await getProvider(actualProviderId);
5937
6647
  if (!provider) {
5938
- _optionalChain([callbacks, 'access', _318 => _318.onError, 'optionalCall', _319 => _319(`Provider not found: ${actualProviderId}`)]);
6648
+ _optionalChain([callbacks, 'access', _398 => _398.onError, 'optionalCall', _399 => _399(`Provider not found: ${actualProviderId}`)]);
5939
6649
  return;
5940
6650
  }
5941
6651
  if (!provider.enabled) {
5942
- _optionalChain([callbacks, 'access', _320 => _320.onError, 'optionalCall', _321 => _321(`Provider is disabled: ${provider.name}`)]);
6652
+ _optionalChain([callbacks, 'access', _400 => _400.onError, 'optionalCall', _401 => _401(`Provider is disabled: ${provider.name}`)]);
5943
6653
  return;
5944
6654
  }
5945
6655
  const { apiKey: effectiveKey } = _chunkA2YMUCBTcjs.resolveProviderEndpoint.call(void 0, provider);
5946
6656
  const apiKey = resolveApiKey2(effectiveKey);
5947
6657
  if (!apiKey) {
5948
- _optionalChain([callbacks, 'access', _322 => _322.onError, 'optionalCall', _323 => _323("API key not configured")]);
6658
+ _optionalChain([callbacks, 'access', _402 => _402.onError, 'optionalCall', _403 => _403("API key not configured")]);
5949
6659
  return;
5950
6660
  }
5951
6661
  const messageId = `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`;
5952
- _optionalChain([callbacks, 'access', _324 => _324.onStart, 'optionalCall', _325 => _325(messageId)]);
6662
+ _optionalChain([callbacks, 'access', _404 => _404.onStart, 'optionalCall', _405 => _405(messageId)]);
5953
6663
  const globalParams = await llmConfig.getGlobalModelParameters();
5954
6664
  const MAX_ITERATIONS = _nullishCoalesce(globalParams.toolMaxTurns, () => ( 100));
5955
6665
  let iteration = 0;
@@ -5996,12 +6706,12 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
5996
6706
  });
5997
6707
  if (!response.ok) {
5998
6708
  const errorText = await response.text();
5999
- _optionalChain([callbacks, 'access', _326 => _326.onError, 'optionalCall', _327 => _327(`API error (${response.status}): ${errorText}`)]);
6709
+ _optionalChain([callbacks, 'access', _406 => _406.onError, 'optionalCall', _407 => _407(`API error (${response.status}): ${errorText}`)]);
6000
6710
  return;
6001
6711
  }
6002
- const reader = _optionalChain([response, 'access', _328 => _328.body, 'optionalAccess', _329 => _329.getReader, 'call', _330 => _330()]);
6712
+ const reader = _optionalChain([response, 'access', _408 => _408.body, 'optionalAccess', _409 => _409.getReader, 'call', _410 => _410()]);
6003
6713
  if (!reader) {
6004
- _optionalChain([callbacks, 'access', _331 => _331.onError, 'optionalCall', _332 => _332("No response body")]);
6714
+ _optionalChain([callbacks, 'access', _411 => _411.onError, 'optionalCall', _412 => _412("No response body")]);
6005
6715
  return;
6006
6716
  }
6007
6717
  const decoder = new TextDecoder();
@@ -6034,7 +6744,7 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
6034
6744
  logger.debug("Parsed JSON", { json });
6035
6745
  if (json.error) {
6036
6746
  logger.error("API Error", void 0, { error: json.error });
6037
- _optionalChain([callbacks, 'access', _333 => _333.onError, 'optionalCall', _334 => _334(`API Error: ${json.error.message || JSON.stringify(json.error)}`)]);
6747
+ _optionalChain([callbacks, 'access', _413 => _413.onError, 'optionalCall', _414 => _414(`API Error: ${json.error.message || JSON.stringify(json.error)}`)]);
6038
6748
  return;
6039
6749
  }
6040
6750
  parseStreamChunk(json, apiFormat, callbacks, logger);
@@ -6067,7 +6777,7 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
6067
6777
  toolCallsLength: toolCalls.length,
6068
6778
  executableToolCallsLength: executableToolCalls.length,
6069
6779
  hasMcpTools: !!options.mcpTools,
6070
- mcpToolsLength: _optionalChain([options, 'access', _335 => _335.mcpTools, 'optionalAccess', _336 => _336.length]),
6780
+ mcpToolsLength: _optionalChain([options, 'access', _415 => _415.mcpTools, 'optionalAccess', _416 => _416.length]),
6071
6781
  hasMcpService: !!mcpService
6072
6782
  });
6073
6783
  if (executableToolCalls.length > 0 && options.mcpTools && (mcpService || builtinExecutor)) {
@@ -6088,7 +6798,7 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
6088
6798
  builtinExecutor
6089
6799
  );
6090
6800
  const toolResultsText = toolResults.map(({ toolCall, result }) => {
6091
- const resultText = typeof result === "string" ? result : _optionalChain([result, 'access', _337 => _337.content, 'optionalAccess', _338 => _338[0], 'optionalAccess', _339 => _339.text]) || JSON.stringify(result);
6801
+ const resultText = typeof result === "string" ? result : _optionalChain([result, 'access', _417 => _417.content, 'optionalAccess', _418 => _418[0], 'optionalAccess', _419 => _419.text]) || JSON.stringify(result);
6092
6802
  return `Tool ${toolCall.name} result:
6093
6803
  ${resultText}`;
6094
6804
  }).join("\n\n");
@@ -6106,12 +6816,12 @@ ${resultText}`;
6106
6816
  }
6107
6817
  } catch (streamError) {
6108
6818
  logger.error("Stream processing error", streamError instanceof Error ? streamError : void 0);
6109
- _optionalChain([callbacks, 'access', _340 => _340.onError, 'optionalCall', _341 => _341(`Stream error: ${streamError instanceof Error ? streamError.message : "Unknown error"}`)]);
6819
+ _optionalChain([callbacks, 'access', _420 => _420.onError, 'optionalCall', _421 => _421(`Stream error: ${streamError instanceof Error ? streamError.message : "Unknown error"}`)]);
6110
6820
  return;
6111
6821
  }
6112
6822
  }
6113
6823
  logger.info("Sending final callback", { blocksCount: blocks.length });
6114
- _optionalChain([callbacks, 'access', _342 => _342.onDone, 'optionalCall', _343 => _343({
6824
+ _optionalChain([callbacks, 'access', _422 => _422.onDone, 'optionalCall', _423 => _423({
6115
6825
  id: messageId,
6116
6826
  role: "assistant",
6117
6827
  content: finalContent,
@@ -6122,7 +6832,7 @@ ${resultText}`;
6122
6832
  } catch (error) {
6123
6833
  logger.error("Error in streamWithTools", error instanceof Error ? error : void 0);
6124
6834
  const message = error instanceof Error ? error.message : "Unknown error";
6125
- _optionalChain([callbacks, 'access', _344 => _344.onError, 'optionalCall', _345 => _345(message)]);
6835
+ _optionalChain([callbacks, 'access', _424 => _424.onError, 'optionalCall', _425 => _425(message)]);
6126
6836
  }
6127
6837
  }
6128
6838
 
@@ -6173,8 +6883,8 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
6173
6883
  options.providerId,
6174
6884
  options.model
6175
6885
  );
6176
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _346 => _346.actualProviderId]) || options.providerId;
6177
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _347 => _347.actualModelId]) || options.model;
6886
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _426 => _426.actualProviderId]) || options.providerId;
6887
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _427 => _427.actualModelId]) || options.model;
6178
6888
  const provider = await getProvider(actualProviderId);
6179
6889
  if (!provider) {
6180
6890
  return { success: false, error: `Provider not found: ${actualProviderId}` };
@@ -6250,18 +6960,18 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
6250
6960
  { endpointTransformer: void 0 }
6251
6961
  );
6252
6962
  const data = await transformedResponse.json();
6253
- const choice = _optionalChain([data, 'access', _348 => _348.choices, 'optionalAccess', _349 => _349[0]]);
6963
+ const choice = _optionalChain([data, 'access', _428 => _428.choices, 'optionalAccess', _429 => _429[0]]);
6254
6964
  if (!choice) {
6255
6965
  return { success: false, error: "No choices in response" };
6256
6966
  }
6257
- const rawToolCalls = _optionalChain([choice, 'access', _350 => _350.message, 'optionalAccess', _351 => _351.tool_calls]);
6258
- const toolCalls = _optionalChain([rawToolCalls, 'optionalAccess', _352 => _352.map, 'call', _353 => _353((tc) => ({
6967
+ const rawToolCalls = _optionalChain([choice, 'access', _430 => _430.message, 'optionalAccess', _431 => _431.tool_calls]);
6968
+ const toolCalls = _optionalChain([rawToolCalls, 'optionalAccess', _432 => _432.map, 'call', _433 => _433((tc) => ({
6259
6969
  id: tc.id,
6260
6970
  name: tc.function.name,
6261
6971
  args: typeof tc.function.arguments === "string" ? JSON.parse(tc.function.arguments) : tc.function.arguments
6262
6972
  }))]);
6263
6973
  const tapped = readUsageFromOpenAIResponse(data.usage);
6264
- if (_optionalChain([recording, 'optionalAccess', _354 => _354.recorder]) && tapped) {
6974
+ if (_optionalChain([recording, 'optionalAccess', _434 => _434.recorder]) && tapped) {
6265
6975
  recording.recorder.record({
6266
6976
  messageId: _nullishCoalesce(options.messageId, () => ( null)),
6267
6977
  parentMessageId: _nullishCoalesce(options.parentMessageId, () => ( null)),
@@ -6279,9 +6989,9 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
6279
6989
  message: {
6280
6990
  id: `msg_${Date.now()}`,
6281
6991
  role: "assistant",
6282
- content: _optionalChain([choice, 'access', _355 => _355.message, 'optionalAccess', _356 => _356.content]) || "",
6992
+ content: _optionalChain([choice, 'access', _435 => _435.message, 'optionalAccess', _436 => _436.content]) || "",
6283
6993
  timestamp: Date.now(),
6284
- thinking: data.reasoning_content || _optionalChain([choice, 'access', _357 => _357.message, 'optionalAccess', _358 => _358.thinking, 'optionalAccess', _359 => _359.content]) ? { content: data.reasoning_content || _optionalChain([choice, 'access', _360 => _360.message, 'optionalAccess', _361 => _361.thinking, 'optionalAccess', _362 => _362.content]) || "", signature: _optionalChain([choice, 'access', _363 => _363.message, 'optionalAccess', _364 => _364.thinking, 'optionalAccess', _365 => _365.signature]) } : void 0,
6994
+ thinking: data.reasoning_content || _optionalChain([choice, 'access', _437 => _437.message, 'optionalAccess', _438 => _438.thinking, 'optionalAccess', _439 => _439.content]) ? { content: data.reasoning_content || _optionalChain([choice, 'access', _440 => _440.message, 'optionalAccess', _441 => _441.thinking, 'optionalAccess', _442 => _442.content]) || "", signature: _optionalChain([choice, 'access', _443 => _443.message, 'optionalAccess', _444 => _444.thinking, 'optionalAccess', _445 => _445.signature]) } : void 0,
6285
6995
  toolCalls
6286
6996
  },
6287
6997
  usage: data.usage ? {
@@ -6302,21 +7012,21 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6302
7012
  options.providerId,
6303
7013
  options.model
6304
7014
  );
6305
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _366 => _366.actualProviderId]) || options.providerId;
6306
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _367 => _367.actualModelId]) || options.model;
7015
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _446 => _446.actualProviderId]) || options.providerId;
7016
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _447 => _447.actualModelId]) || options.model;
6307
7017
  const provider = await getProvider(actualProviderId);
6308
7018
  if (!provider) {
6309
- _optionalChain([callbacks, 'access', _368 => _368.onError, 'optionalCall', _369 => _369(`Provider not found: ${actualProviderId}`)]);
7019
+ _optionalChain([callbacks, 'access', _448 => _448.onError, 'optionalCall', _449 => _449(`Provider not found: ${actualProviderId}`)]);
6310
7020
  return;
6311
7021
  }
6312
7022
  if (!provider.enabled) {
6313
- _optionalChain([callbacks, 'access', _370 => _370.onError, 'optionalCall', _371 => _371(`Provider is disabled: ${provider.name}`)]);
7023
+ _optionalChain([callbacks, 'access', _450 => _450.onError, 'optionalCall', _451 => _451(`Provider is disabled: ${provider.name}`)]);
6314
7024
  return;
6315
7025
  }
6316
7026
  const { apiKey: effectiveKey } = _chunkA2YMUCBTcjs.resolveProviderEndpoint.call(void 0, provider);
6317
7027
  const apiKey = resolveApiKey2(effectiveKey);
6318
7028
  if (!apiKey) {
6319
- _optionalChain([callbacks, 'access', _372 => _372.onError, 'optionalCall', _373 => _373("API key not configured")]);
7029
+ _optionalChain([callbacks, 'access', _452 => _452.onError, 'optionalCall', _453 => _453("API key not configured")]);
6320
7030
  return;
6321
7031
  }
6322
7032
  const { chain, hasTransformers } = await resolveChainWithMain(
@@ -6328,7 +7038,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6328
7038
  return completeStreamFallback(options, callbacks);
6329
7039
  }
6330
7040
  const messageId = `msg_${Date.now()}`;
6331
- _optionalChain([callbacks, 'access', _374 => _374.onStart, 'optionalCall', _375 => _375(messageId)]);
7041
+ _optionalChain([callbacks, 'access', _454 => _454.onStart, 'optionalCall', _455 => _455(messageId)]);
6332
7042
  const unifiedRequest = buildUnifiedCompletionRequest(actualModel, options, true);
6333
7043
  const transformerProvider = {
6334
7044
  name: provider.name,
@@ -6367,7 +7077,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6367
7077
  });
6368
7078
  if (!response.ok) {
6369
7079
  const errorText = await response.text();
6370
- _optionalChain([callbacks, 'access', _376 => _376.onError, 'optionalCall', _377 => _377(`API error (${response.status}): ${errorText}`)]);
7080
+ _optionalChain([callbacks, 'access', _456 => _456.onError, 'optionalCall', _457 => _457(`API error (${response.status}): ${errorText}`)]);
6371
7081
  return;
6372
7082
  }
6373
7083
  const transformedResponse = await executor.executeResponseChain(
@@ -6377,9 +7087,9 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6377
7087
  chain,
6378
7088
  { endpointTransformer: void 0 }
6379
7089
  );
6380
- const reader = _optionalChain([transformedResponse, 'access', _378 => _378.body, 'optionalAccess', _379 => _379.getReader, 'call', _380 => _380()]);
7090
+ const reader = _optionalChain([transformedResponse, 'access', _458 => _458.body, 'optionalAccess', _459 => _459.getReader, 'call', _460 => _460()]);
6381
7091
  if (!reader) {
6382
- _optionalChain([callbacks, 'access', _381 => _381.onError, 'optionalCall', _382 => _382("No response body")]);
7092
+ _optionalChain([callbacks, 'access', _461 => _461.onError, 'optionalCall', _462 => _462("No response body")]);
6383
7093
  return;
6384
7094
  }
6385
7095
  const decoder = new TextDecoder();
@@ -6397,22 +7107,22 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6397
7107
  if (data === "[DONE]") continue;
6398
7108
  try {
6399
7109
  const json = JSON.parse(data);
6400
- const delta = _optionalChain([json, 'access', _383 => _383.choices, 'optionalAccess', _384 => _384[0], 'optionalAccess', _385 => _385.delta]);
6401
- if (_optionalChain([delta, 'optionalAccess', _386 => _386.content])) {
7110
+ const delta = _optionalChain([json, 'access', _463 => _463.choices, 'optionalAccess', _464 => _464[0], 'optionalAccess', _465 => _465.delta]);
7111
+ if (_optionalChain([delta, 'optionalAccess', _466 => _466.content])) {
6402
7112
  content += delta.content;
6403
- _optionalChain([callbacks, 'access', _387 => _387.onDelta, 'optionalCall', _388 => _388(delta.content)]);
7113
+ _optionalChain([callbacks, 'access', _467 => _467.onDelta, 'optionalCall', _468 => _468(delta.content)]);
6404
7114
  }
6405
- if (_optionalChain([delta, 'optionalAccess', _389 => _389.thinking, 'optionalAccess', _390 => _390.content])) {
7115
+ if (_optionalChain([delta, 'optionalAccess', _469 => _469.thinking, 'optionalAccess', _470 => _470.content])) {
6406
7116
  reasoning += delta.thinking.content;
6407
- _optionalChain([callbacks, 'access', _391 => _391.onReasoning, 'optionalCall', _392 => _392(delta.thinking.content)]);
7117
+ _optionalChain([callbacks, 'access', _471 => _471.onReasoning, 'optionalCall', _472 => _472(delta.thinking.content)]);
6408
7118
  }
6409
- if (_optionalChain([delta, 'optionalAccess', _393 => _393.reasoning_content])) {
7119
+ if (_optionalChain([delta, 'optionalAccess', _473 => _473.reasoning_content])) {
6410
7120
  reasoning += delta.reasoning_content;
6411
- _optionalChain([callbacks, 'access', _394 => _394.onReasoning, 'optionalCall', _395 => _395(delta.reasoning_content)]);
7121
+ _optionalChain([callbacks, 'access', _474 => _474.onReasoning, 'optionalCall', _475 => _475(delta.reasoning_content)]);
6412
7122
  }
6413
7123
  if (json.usage) {
6414
7124
  const tapped = readUsageFromOpenAIResponse(json.usage);
6415
- if (_optionalChain([recording, 'optionalAccess', _396 => _396.recorder]) && tapped) {
7125
+ if (_optionalChain([recording, 'optionalAccess', _476 => _476.recorder]) && tapped) {
6416
7126
  recording.recorder.record({
6417
7127
  messageId: _nullishCoalesce(options.messageId, () => ( null)),
6418
7128
  parentMessageId: _nullishCoalesce(options.parentMessageId, () => ( null)),
@@ -6425,7 +7135,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6425
7135
  rawUsage: json.usage
6426
7136
  });
6427
7137
  }
6428
- _optionalChain([callbacks, 'access', _397 => _397.onDone, 'optionalCall', _398 => _398(
7138
+ _optionalChain([callbacks, 'access', _477 => _477.onDone, 'optionalCall', _478 => _478(
6429
7139
  {
6430
7140
  id: messageId,
6431
7141
  role: "assistant",
@@ -6446,7 +7156,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6446
7156
  }
6447
7157
  }
6448
7158
  }
6449
- _optionalChain([callbacks, 'access', _399 => _399.onDone, 'optionalCall', _400 => _400({
7159
+ _optionalChain([callbacks, 'access', _479 => _479.onDone, 'optionalCall', _480 => _480({
6450
7160
  id: messageId,
6451
7161
  role: "assistant",
6452
7162
  content,
@@ -6458,13 +7168,13 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6458
7168
  }
6459
7169
  } catch (error) {
6460
7170
  const message = error instanceof Error ? error.message : "Unknown error";
6461
- _optionalChain([callbacks, 'access', _401 => _401.onError, 'optionalCall', _402 => _402(message)]);
7171
+ _optionalChain([callbacks, 'access', _481 => _481.onError, 'optionalCall', _482 => _482(message)]);
6462
7172
  }
6463
7173
  }
6464
7174
 
6465
7175
  // src/completion/CompletionService.ts
6466
- var CompletionService = (_class13 = class {
6467
- constructor(paths, llmConfig, logger) {;_class13.prototype.__init23.call(this);_class13.prototype.__init24.call(this);_class13.prototype.__init25.call(this);_class13.prototype.__init26.call(this);
7176
+ var CompletionService = (_class15 = class {
7177
+ constructor(paths, llmConfig, logger) {;_class15.prototype.__init30.call(this);_class15.prototype.__init31.call(this);_class15.prototype.__init32.call(this);_class15.prototype.__init33.call(this);
6468
7178
  this.paths = paths;
6469
7179
  this.llmConfig = llmConfig;
6470
7180
  this.logger = logger;
@@ -6472,10 +7182,10 @@ var CompletionService = (_class13 = class {
6472
7182
 
6473
7183
 
6474
7184
 
6475
- __init23() {this.apiKeyPool = null}
6476
- __init24() {this.usageRecorder = null}
6477
- __init25() {this.usageEventSink = null}
6478
- __init26() {this.visionFallbackProvider = null}
7185
+ __init30() {this.apiKeyPool = null}
7186
+ __init31() {this.usageRecorder = null}
7187
+ __init32() {this.usageEventSink = null}
7188
+ __init33() {this.visionFallbackProvider = null}
6479
7189
  /**
6480
7190
  * Set the API key pool service for multi-key load balancing.
6481
7191
  * When set, keys are resolved via the pool instead of directly from the provider.
@@ -6525,8 +7235,8 @@ var CompletionService = (_class13 = class {
6525
7235
  options.providerId,
6526
7236
  options.model
6527
7237
  );
6528
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _403 => _403.actualProviderId]) || options.providerId;
6529
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _404 => _404.actualModelId]) || options.model;
7238
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _483 => _483.actualProviderId]) || options.providerId;
7239
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _484 => _484.actualModelId]) || options.model;
6530
7240
  const provider = await this.getProvider(actualProviderId);
6531
7241
  if (!provider) {
6532
7242
  return { success: false, error: `Provider not found: ${actualProviderId}` };
@@ -6559,7 +7269,7 @@ var CompletionService = (_class13 = class {
6559
7269
  this.apiKeyPool.reportSuccess(options.sessionId);
6560
7270
  }
6561
7271
  if (result.success && result.usage && options.sessionId) {
6562
- _optionalChain([this, 'access', _405 => _405.usageEventSink, 'optionalAccess', _406 => _406.emit, 'call', _407 => _407({
7272
+ _optionalChain([this, 'access', _485 => _485.usageEventSink, 'optionalAccess', _486 => _486.emit, 'call', _487 => _487({
6563
7273
  sessionId: options.sessionId,
6564
7274
  modelId: actualModel,
6565
7275
  usage: result.usage,
@@ -6588,8 +7298,8 @@ var CompletionService = (_class13 = class {
6588
7298
  options.model
6589
7299
  );
6590
7300
  this.logger.debug("Resolved routed model", { routedInfo });
6591
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _408 => _408.actualProviderId]) || options.providerId;
6592
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _409 => _409.actualModelId]) || options.model;
7301
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _488 => _488.actualProviderId]) || options.providerId;
7302
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _489 => _489.actualModelId]) || options.model;
6593
7303
  this.logger.debug("Resolved provider and model", { actualProviderId, actualModel });
6594
7304
  const provider = await this.getProvider(actualProviderId);
6595
7305
  this.logger.debug("Retrieved provider", provider ? {
@@ -6601,22 +7311,22 @@ var CompletionService = (_class13 = class {
6601
7311
  enabled: provider.enabled
6602
7312
  } : { error: "Provider not found" });
6603
7313
  if (!provider) {
6604
- _optionalChain([callbacks, 'access', _410 => _410.onError, 'optionalCall', _411 => _411(`Provider not found: ${actualProviderId}`)]);
7314
+ _optionalChain([callbacks, 'access', _490 => _490.onError, 'optionalCall', _491 => _491(`Provider not found: ${actualProviderId}`)]);
6605
7315
  return;
6606
7316
  }
6607
7317
  if (!provider.enabled) {
6608
- _optionalChain([callbacks, 'access', _412 => _412.onError, 'optionalCall', _413 => _413(`Provider is disabled: ${provider.name}`)]);
7318
+ _optionalChain([callbacks, 'access', _492 => _492.onError, 'optionalCall', _493 => _493(`Provider is disabled: ${provider.name}`)]);
6609
7319
  return;
6610
7320
  }
6611
7321
  const apiKey = await this.resolveApiKeyForRequest(provider, actualProviderId, options.sessionId);
6612
7322
  if (!apiKey) {
6613
- _optionalChain([callbacks, 'access', _414 => _414.onError, 'optionalCall', _415 => _415("API key not configured")]);
7323
+ _optionalChain([callbacks, 'access', _494 => _494.onError, 'optionalCall', _495 => _495("API key not configured")]);
6614
7324
  return;
6615
7325
  }
6616
7326
  const apiFormat = _chunkA2YMUCBTcjs.resolveApiFormat.call(void 0, provider);
6617
7327
  this.logger.info("Using API format for stream", { apiFormat });
6618
7328
  const messageId = `msg_${Date.now()}`;
6619
- _optionalChain([callbacks, 'access', _416 => _416.onStart, 'optionalCall', _417 => _417(messageId)]);
7329
+ _optionalChain([callbacks, 'access', _496 => _496.onStart, 'optionalCall', _497 => _497(messageId)]);
6620
7330
  const resolvedOptions = { ...options, model: actualModel };
6621
7331
  if (options.sessionId) {
6622
7332
  const sid = options.sessionId;
@@ -6625,14 +7335,14 @@ var CompletionService = (_class13 = class {
6625
7335
  ...callbacks,
6626
7336
  onDone: (message, usage, metrics) => {
6627
7337
  if (usage) {
6628
- _optionalChain([this, 'access', _418 => _418.usageEventSink, 'optionalAccess', _419 => _419.emit, 'call', _420 => _420({
7338
+ _optionalChain([this, 'access', _498 => _498.usageEventSink, 'optionalAccess', _499 => _499.emit, 'call', _500 => _500({
6629
7339
  sessionId: sid,
6630
7340
  modelId: actualModel,
6631
7341
  usage,
6632
7342
  engineOrigin: "completion"
6633
7343
  })]);
6634
7344
  }
6635
- _optionalChain([userOnDone, 'optionalCall', _421 => _421(message, usage, metrics)]);
7345
+ _optionalChain([userOnDone, 'optionalCall', _501 => _501(message, usage, metrics)]);
6636
7346
  }
6637
7347
  };
6638
7348
  }
@@ -6648,11 +7358,11 @@ var CompletionService = (_class13 = class {
6648
7358
  retryState.error = { status, message: error };
6649
7359
  return;
6650
7360
  }
6651
- _optionalChain([callbacks, 'access', _422 => _422.onError, 'optionalCall', _423 => _423(error)]);
7361
+ _optionalChain([callbacks, 'access', _502 => _502.onError, 'optionalCall', _503 => _503(error)]);
6652
7362
  },
6653
7363
  onDone: (message, usage, metrics) => {
6654
7364
  this.apiKeyPool.reportSuccess(options.sessionId);
6655
- _optionalChain([callbacks, 'access', _424 => _424.onDone, 'optionalCall', _425 => _425(message, usage, metrics)]);
7365
+ _optionalChain([callbacks, 'access', _504 => _504.onDone, 'optionalCall', _505 => _505(message, usage, metrics)]);
6656
7366
  }
6657
7367
  };
6658
7368
  await this.callStreamHandler(apiFormat, provider, apiKey, resolvedOptions, messageId, interceptCallbacks);
@@ -6673,7 +7383,7 @@ var CompletionService = (_class13 = class {
6673
7383
  // don't fire onStart again
6674
7384
  });
6675
7385
  } else {
6676
- _optionalChain([callbacks, 'access', _426 => _426.onError, 'optionalCall', _427 => _427(retryState.error.message)]);
7386
+ _optionalChain([callbacks, 'access', _506 => _506.onError, 'optionalCall', _507 => _507(retryState.error.message)]);
6677
7387
  }
6678
7388
  }
6679
7389
  } else {
@@ -6686,7 +7396,7 @@ var CompletionService = (_class13 = class {
6686
7396
  } catch (error) {
6687
7397
  const message = error instanceof Error ? error.message : "Unknown error";
6688
7398
  this.logger.error("Stream completion error", error instanceof Error ? error : void 0, { message });
6689
- _optionalChain([callbacks, 'access', _428 => _428.onError, 'optionalCall', _429 => _429(message)]);
7399
+ _optionalChain([callbacks, 'access', _508 => _508.onError, 'optionalCall', _509 => _509(message)]);
6690
7400
  }
6691
7401
  }
6692
7402
  /**
@@ -6726,7 +7436,7 @@ var CompletionService = (_class13 = class {
6726
7436
  return {
6727
7437
  success: true,
6728
7438
  message: "Connection successful",
6729
- response: _optionalChain([result, 'access', _430 => _430.message, 'optionalAccess', _431 => _431.content]) || "",
7439
+ response: _optionalChain([result, 'access', _510 => _510.message, 'optionalAccess', _511 => _511.content]) || "",
6730
7440
  model: modelId,
6731
7441
  durationMs
6732
7442
  };
@@ -6791,7 +7501,7 @@ ${description}`;
6791
7501
  * 3. Legacy single key from provider config
6792
7502
  */
6793
7503
  async resolveApiKeyForRequest(provider, providerId, sessionId) {
6794
- if (_optionalChain([provider, 'access', _432 => _432.codingPlan, 'optionalAccess', _433 => _433.enabled]) && provider.codingPlan.apiKey) {
7504
+ if (_optionalChain([provider, 'access', _512 => _512.codingPlan, 'optionalAccess', _513 => _513.enabled]) && provider.codingPlan.apiKey) {
6795
7505
  return this.resolveApiKey(provider.codingPlan.apiKey);
6796
7506
  }
6797
7507
  if (this.apiKeyPool) {
@@ -6957,7 +7667,34 @@ ${description}`;
6957
7667
  builtinExecutor
6958
7668
  );
6959
7669
  }
6960
- }, _class13);
7670
+ }, _class15);
7671
+
7672
+
7673
+
7674
+
7675
+
7676
+
7677
+
7678
+
7679
+
7680
+
7681
+
7682
+
7683
+
7684
+
7685
+
7686
+
7687
+
7688
+
7689
+
7690
+
7691
+
7692
+
7693
+
7694
+
7695
+
7696
+
7697
+
6961
7698
 
6962
7699
 
6963
7700
 
@@ -7051,4 +7788,4 @@ ${description}`;
7051
7788
 
7052
7789
 
7053
7790
 
7054
- exports.CompletionService = CompletionService; exports.register = register; exports.emit = emit; exports.attach = attach; exports.release = release; exports.convertMessageToOpenAI = convertMessageToOpenAI; exports.convertMessageToAnthropic = convertMessageToAnthropic; exports.convertMessageToGemini = convertMessageToGemini; exports.getOpenRouterProviderConfig = getOpenRouterProviderConfig; exports.addOpenRouterProviderToRequest = addOpenRouterProviderToRequest; exports.DEFAULT_ROUTE_IDLE_MS = DEFAULT_ROUTE_IDLE_MS; exports.ProviderProxyRouteMap = ProviderProxyRouteMap; exports.getSharedExecutor = getSharedExecutor; exports.getResponsesEndpointTransformer = getResponsesEndpointTransformer; exports.getAnthropicEndpointTransformer = getAnthropicEndpointTransformer; exports.getGeminiEndpointTransformer = getGeminiEndpointTransformer; exports.readBody = readBody; exports.relayResponse = relayResponse; exports.aggregateAnthropicSseToJsonBody = aggregateAnthropicSseToJsonBody; exports.writeError = writeError; exports.writeBoundAccountError = writeBoundAccountError; exports.resolvePoolBoundKey = resolvePoolBoundKey; exports.extractRouteToken = extractRouteToken; exports.isLoopbackAddress = isLoopbackAddress; exports.ProviderProxy = ProviderProxy; exports.getProviderProxy = getProviderProxy; exports.__resetProviderProxyForTests = __resetProviderProxyForTests; exports.KeyedMutex = KeyedMutex; exports.OUTBOUND_API_SERVER_CONFIG_KEY = OUTBOUND_API_SERVER_CONFIG_KEY; exports.DEFAULT_USER_MESSAGE_QUEUE = DEFAULT_USER_MESSAGE_QUEUE; exports.DEFAULT_CONCURRENCY_QUEUE = DEFAULT_CONCURRENCY_QUEUE; exports.DEFAULT_ACCOUNT_PROBE = DEFAULT_ACCOUNT_PROBE; exports.DEFAULT_ALLOWANCE_SCHEDULING = DEFAULT_ALLOWANCE_SCHEDULING; exports.normalizeAllowanceScheduling = normalizeAllowanceScheduling; exports.normalizeAccountProbe = normalizeAccountProbe; exports.normalizeAudit = normalizeAudit; exports.normalizeBilling = normalizeBilling; exports.DEFAULT_FINGERPRINT = DEFAULT_FINGERPRINT; exports.normalizeFingerprint = normalizeFingerprint; exports.normalizeVoucher = normalizeVoucher; exports.normalizeProxyConfig = normalizeProxyConfig; exports.normalizeProxySegment = normalizeProxySegment; exports.normalizeWebhookDestination = normalizeWebhookDestination; exports.normalizeWebhookSegment = normalizeWebhookSegment; exports.normalizeQueueSegments = normalizeQueueSegments; exports.normalizePrefixTargets = normalizePrefixTargets; exports.legacyEndpointsToBindings = legacyEndpointsToBindings; exports.normalizeGatewayBindings = normalizeGatewayBindings; exports.defaultServerConfig = defaultServerConfig; exports.normalizeServerConfig = normalizeServerConfig; exports.loadServerConfig = loadServerConfig; exports.saveServerConfig = saveServerConfig; exports.mergeServerConfig = mergeServerConfig; exports.gatewayBindingToEndpointConfig = gatewayBindingToEndpointConfig; exports.candidateGatewayBindings = candidateGatewayBindings; exports.candidateBackgroundModelIds = candidateBackgroundModelIds; exports.resolveGatewayBinding = resolveGatewayBinding; exports.computeKeyExpiry = computeKeyExpiry; exports.checkKeyQuota = checkKeyQuota; exports.randomBase62 = randomBase62; exports.hashKey = hashKey; exports.createNamedKey = createNamedKey; exports.createIntegrationKey = createIntegrationKey; exports.verifyKey = verifyKey; exports.verifyPresentedKey = verifyPresentedKey; exports.ConcurrencyQueueFullError = ConcurrencyQueueFullError; exports.ConcurrencyWaitTimeoutError = ConcurrencyWaitTimeoutError; exports.ConcurrencyWaitCancelledError = ConcurrencyWaitCancelledError; exports.isConcurrencyRejection = isConcurrencyRejection; exports.OutboundConcurrencyGate = OutboundConcurrencyGate; exports.OutboundRateLimiter = OutboundRateLimiter; exports.isUserMessageRequest = isUserMessageRequest; exports.SerialQueueTimeoutError = SerialQueueTimeoutError; exports.isSerialQueueTimeout = isSerialQueueTimeout; exports.UserMessageSerialQueue = UserMessageSerialQueue; exports.generateVoucherCode = generateVoucherCode; exports.hashVoucherCode = hashVoucherCode; exports.voucherCodePrefix = voucherCodePrefix; exports.newVoucherId = newVoucherId; exports.computeVoucherGrant = computeVoucherGrant; exports.toVoucherInfo = toVoucherInfo; exports.isRedeemRequest = isRedeemRequest; exports.handleVoucherRedeem = handleVoucherRedeem; exports.DEFAULT_OUTBOUND_PORT = DEFAULT_OUTBOUND_PORT; exports.OutboundApiServer = OutboundApiServer; exports.formatUrls = formatUrls; exports.startOfLocalDay = startOfLocalDay; exports.startOfLocalWeek = startOfLocalWeek; exports.KeySpendTracker = KeySpendTracker; exports.getOutboundApiServer = getOutboundApiServer; exports.__resetOutboundApiServerForTests = __resetOutboundApiServerForTests;
7791
+ exports.CompletionService = CompletionService; exports.register = register; exports.emit = emit; exports.attach = attach; exports.release = release; exports.convertMessageToOpenAI = convertMessageToOpenAI; exports.convertMessageToAnthropic = convertMessageToAnthropic; exports.convertMessageToGemini = convertMessageToGemini; exports.getOpenRouterProviderConfig = getOpenRouterProviderConfig; exports.addOpenRouterProviderToRequest = addOpenRouterProviderToRequest; exports.DEFAULT_ROUTE_IDLE_MS = DEFAULT_ROUTE_IDLE_MS; exports.ProviderProxyRouteMap = ProviderProxyRouteMap; exports.getSharedExecutor = getSharedExecutor; exports.getResponsesEndpointTransformer = getResponsesEndpointTransformer; exports.getAnthropicEndpointTransformer = getAnthropicEndpointTransformer; exports.getGeminiEndpointTransformer = getGeminiEndpointTransformer; exports.readBody = readBody; exports.relayResponse = relayResponse; exports.aggregateAnthropicSseToJsonBody = aggregateAnthropicSseToJsonBody; exports.writeError = writeError; exports.writeBoundAccountError = writeBoundAccountError; exports.resolvePoolBoundKey = resolvePoolBoundKey; exports.extractRouteToken = extractRouteToken; exports.isLoopbackAddress = isLoopbackAddress; exports.ProviderProxy = ProviderProxy; exports.ROUTE_LEASE_REQUEST_SCHEMA = ROUTE_LEASE_REQUEST_SCHEMA; exports.ROUTE_LEASE_RESULT_SCHEMA = ROUTE_LEASE_RESULT_SCHEMA; exports.ROUTE_LEASE_CAPABILITIES_SCHEMA = ROUTE_LEASE_CAPABILITIES_SCHEMA; exports.ROUTE_LEASE_API_VERSION = ROUTE_LEASE_API_VERSION; exports.ROUTE_LEASE_DEFAULT_TTL_SECONDS = ROUTE_LEASE_DEFAULT_TTL_SECONDS; exports.ROUTE_LEASE_MAX_TTL_SECONDS = ROUTE_LEASE_MAX_TTL_SECONDS; exports.ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES = ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES; exports.ROUTE_LEASE_MAX_CONSUMER_BYTES = ROUTE_LEASE_MAX_CONSUMER_BYTES; exports.ROUTE_LEASE_MAX_EXECUTION_ID_BYTES = ROUTE_LEASE_MAX_EXECUTION_ID_BYTES; exports.ROUTE_LEASE_MAX_SESSION_ID_BYTES = ROUTE_LEASE_MAX_SESSION_ID_BYTES; exports.ROUTE_LEASE_MAX_MODEL_BYTES = ROUTE_LEASE_MAX_MODEL_BYTES; exports.ROUTE_LEASE_SESSION_HASH_DOMAIN = ROUTE_LEASE_SESSION_HASH_DOMAIN; exports.ROUTE_LEASE_CODEX_TOKEN_ENV = ROUTE_LEASE_CODEX_TOKEN_ENV; exports.ROUTE_LEASE_RUNTIMES = ROUTE_LEASE_RUNTIMES; exports.ROUTE_LEASE_CAPABILITIES = ROUTE_LEASE_CAPABILITIES; exports.ROUTE_LEASE_RUNTIME_TABLE = ROUTE_LEASE_RUNTIME_TABLE; exports.RouteLeaseError = RouteLeaseError; exports.normalizeRouteLeaseIdempotencyKey = normalizeRouteLeaseIdempotencyKey; exports.normalizeRouteLeaseTtl = normalizeRouteLeaseTtl; exports.hashRouteLeaseSessionId = hashRouteLeaseSessionId; exports.canonicalizeRouteLeasePayload = canonicalizeRouteLeasePayload; exports.hashRouteLeasePayload = hashRouteLeasePayload; exports.parseRouteLeaseCreate = parseRouteLeaseCreate; exports.routeLeaseRuntime = routeLeaseRuntime; exports.SYSTEM_ROUTE_LEASE_CLOCK = SYSTEM_ROUTE_LEASE_CLOCK; exports.RouteLeaseManager = RouteLeaseManager; exports.gatewayBindingToEndpointConfig = gatewayBindingToEndpointConfig; exports.candidateGatewayBindings = candidateGatewayBindings; exports.candidateBackgroundModelIds = candidateBackgroundModelIds; exports.resolveGatewayBinding = resolveGatewayBinding; exports.RouteLeaseTargetResolver = RouteLeaseTargetResolver; exports.getProviderProxy = getProviderProxy; exports.__resetProviderProxyForTests = __resetProviderProxyForTests; exports.KeyedMutex = KeyedMutex; exports.OUTBOUND_API_SERVER_CONFIG_KEY = OUTBOUND_API_SERVER_CONFIG_KEY; exports.DEFAULT_USER_MESSAGE_QUEUE = DEFAULT_USER_MESSAGE_QUEUE; exports.DEFAULT_CONCURRENCY_QUEUE = DEFAULT_CONCURRENCY_QUEUE; exports.DEFAULT_ACCOUNT_PROBE = DEFAULT_ACCOUNT_PROBE; exports.DEFAULT_ALLOWANCE_SCHEDULING = DEFAULT_ALLOWANCE_SCHEDULING; exports.normalizeAllowanceScheduling = normalizeAllowanceScheduling; exports.normalizeAccountProbe = normalizeAccountProbe; exports.normalizeAudit = normalizeAudit; exports.normalizeBilling = normalizeBilling; exports.DEFAULT_FINGERPRINT = DEFAULT_FINGERPRINT; exports.normalizeFingerprint = normalizeFingerprint; exports.normalizeVoucher = normalizeVoucher; exports.normalizeProxyConfig = normalizeProxyConfig; exports.normalizeProxySegment = normalizeProxySegment; exports.normalizeWebhookDestination = normalizeWebhookDestination; exports.normalizeWebhookSegment = normalizeWebhookSegment; exports.normalizeQueueSegments = normalizeQueueSegments; exports.normalizePrefixTargets = normalizePrefixTargets; exports.legacyEndpointsToBindings = legacyEndpointsToBindings; exports.normalizeGatewayBindings = normalizeGatewayBindings; exports.defaultServerConfig = defaultServerConfig; exports.normalizeServerConfig = normalizeServerConfig; exports.loadServerConfig = loadServerConfig; exports.saveServerConfig = saveServerConfig; exports.mergeServerConfig = mergeServerConfig; exports.computeKeyExpiry = computeKeyExpiry; exports.checkKeyQuota = checkKeyQuota; exports.randomBase62 = randomBase62; exports.hashKey = hashKey; exports.createNamedKey = createNamedKey; exports.createIntegrationKey = createIntegrationKey; exports.verifyKey = verifyKey; exports.verifyPresentedKey = verifyPresentedKey; exports.ConcurrencyQueueFullError = ConcurrencyQueueFullError; exports.ConcurrencyWaitTimeoutError = ConcurrencyWaitTimeoutError; exports.ConcurrencyWaitCancelledError = ConcurrencyWaitCancelledError; exports.isConcurrencyRejection = isConcurrencyRejection; exports.OutboundConcurrencyGate = OutboundConcurrencyGate; exports.OutboundRateLimiter = OutboundRateLimiter; exports.isUserMessageRequest = isUserMessageRequest; exports.SerialQueueTimeoutError = SerialQueueTimeoutError; exports.isSerialQueueTimeout = isSerialQueueTimeout; exports.UserMessageSerialQueue = UserMessageSerialQueue; exports.generateVoucherCode = generateVoucherCode; exports.hashVoucherCode = hashVoucherCode; exports.voucherCodePrefix = voucherCodePrefix; exports.newVoucherId = newVoucherId; exports.computeVoucherGrant = computeVoucherGrant; exports.toVoucherInfo = toVoucherInfo; exports.isRedeemRequest = isRedeemRequest; exports.handleVoucherRedeem = handleVoucherRedeem; exports.DEFAULT_OUTBOUND_PORT = DEFAULT_OUTBOUND_PORT; exports.OutboundApiServer = OutboundApiServer; exports.formatUrls = formatUrls; exports.startOfLocalDay = startOfLocalDay; exports.startOfLocalWeek = startOfLocalWeek; exports.KeySpendTracker = KeySpendTracker; exports.getOutboundApiServer = getOutboundApiServer; exports.__resetOutboundApiServerForTests = __resetOutboundApiServerForTests;