@omnicross/core 0.1.7 → 0.1.9

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 (117) 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/auth/GeminiCodeAssistProjectResolver.cjs +5 -3
  4. package/dist/auth/GeminiCodeAssistProjectResolver.js +4 -2
  5. package/dist/{chunk-N5T4M4GU.js → chunk-2UTGX6FM.js} +2 -2
  6. package/dist/{chunk-32O35CIF.js → chunk-45FHEOKW.js} +22 -7
  7. package/dist/{chunk-LVUCAWMK.js → chunk-55FMGZ3L.js} +13 -5
  8. package/dist/{chunk-OF3BECU2.cjs → chunk-56QD3YW6.cjs} +18 -5
  9. package/dist/{chunk-V4TXMEJS.cjs → chunk-5LNYGCIW.cjs} +15 -7
  10. package/dist/chunk-77USGNRW.cjs +37 -0
  11. package/dist/chunk-AOFWNWLT.cjs +203 -0
  12. package/dist/{chunk-LB6EBS6Y.js → chunk-CCVRRJOX.js} +17 -4
  13. package/dist/{chunk-OVQBNVSV.cjs → chunk-CMVXX7ON.cjs} +42 -27
  14. package/dist/{chunk-E3COOKUP.js → chunk-CQIJCPMF.js} +1108 -271
  15. package/dist/{chunk-OJGQKHN5.cjs → chunk-EPN67EGP.cjs} +3 -0
  16. package/dist/{chunk-PEGTIPB3.js → chunk-HKB656B4.js} +37 -43
  17. package/dist/chunk-N2EIM2XJ.js +203 -0
  18. package/dist/{chunk-MYYR3HZL.js → chunk-NGYOO5TO.js} +5 -5
  19. package/dist/{chunk-OKMJLEXX.cjs → chunk-OC6DOOFX.cjs} +5 -5
  20. package/dist/{chunk-2Z4XM3H5.cjs → chunk-P7EM6BND.cjs} +10 -10
  21. package/dist/{chunk-KBM2W6UY.cjs → chunk-RICPANBY.cjs} +41 -24
  22. package/dist/{chunk-TOVPMMHM.js → chunk-S3DYFEBE.js} +37 -20
  23. package/dist/{chunk-SOO562NM.js → chunk-UAPBLNN2.js} +3 -0
  24. package/dist/chunk-WYVGULTQ.js +37 -0
  25. package/dist/{chunk-4BB343BI.cjs → chunk-XELFM7KL.cjs} +1381 -544
  26. package/dist/{chunk-OXHNSZ53.cjs → chunk-ZQQUD66M.cjs} +54 -60
  27. package/dist/completion/ApiKeyPoolService.cjs +32 -1
  28. package/dist/completion/ApiKeyPoolService.d.cts +17 -1
  29. package/dist/completion/ApiKeyPoolService.d.ts +17 -1
  30. package/dist/completion/ApiKeyPoolService.js +31 -0
  31. package/dist/completion/CompletionService.cjs +13 -11
  32. package/dist/completion/CompletionService.js +12 -10
  33. package/dist/completion.cjs +13 -11
  34. package/dist/completion.js +12 -10
  35. package/dist/index.cjs +69 -11
  36. package/dist/index.d.cts +7 -7
  37. package/dist/index.d.ts +7 -7
  38. package/dist/index.js +68 -10
  39. package/dist/outbound-api/routeResolver.d.cts +4 -4
  40. package/dist/outbound-api/routeResolver.d.ts +4 -4
  41. package/dist/outbound-api/types.d.cts +3 -3
  42. package/dist/outbound-api/types.d.ts +3 -3
  43. package/dist/outbound-api.cjs +13 -11
  44. package/dist/outbound-api.d.cts +5 -5
  45. package/dist/outbound-api.d.ts +5 -5
  46. package/dist/outbound-api.js +12 -10
  47. package/dist/pipeline/AccountAllowanceScheduling.d.cts +3 -3
  48. package/dist/pipeline/AccountAllowanceScheduling.d.ts +3 -3
  49. package/dist/pipeline/executeProviderCall.d.cts +2 -0
  50. package/dist/pipeline/executeProviderCall.d.ts +2 -0
  51. package/dist/pipeline/resolveProviderChain.d.cts +1 -0
  52. package/dist/pipeline/resolveProviderChain.d.ts +1 -0
  53. package/dist/pipeline/resolveSubscriptionChain.d.cts +2 -0
  54. package/dist/pipeline/resolveSubscriptionChain.d.ts +2 -0
  55. package/dist/ports/provider-config-source.d.cts +1 -0
  56. package/dist/ports/provider-config-source.d.ts +1 -0
  57. package/dist/ports.d.cts +3 -3
  58. package/dist/ports.d.ts +3 -3
  59. package/dist/provider-proxy/ProviderProxy.cjs +13 -11
  60. package/dist/provider-proxy/ProviderProxy.d.cts +1 -1
  61. package/dist/provider-proxy/ProviderProxy.d.ts +1 -1
  62. package/dist/provider-proxy/ProviderProxy.js +12 -10
  63. package/dist/provider-proxy/ingress/providerProxyShared.cjs +13 -11
  64. package/dist/provider-proxy/ingress/providerProxyShared.js +12 -10
  65. package/dist/provider-proxy/types.d.cts +15 -1
  66. package/dist/provider-proxy/types.d.ts +15 -1
  67. package/dist/provider-proxy.cjs +69 -11
  68. package/dist/provider-proxy.d.cts +254 -8
  69. package/dist/provider-proxy.d.ts +254 -8
  70. package/dist/provider-proxy.js +70 -12
  71. package/dist/{routeResolver-BxOfunbm.d.ts → routeResolver-C0mkkZaM.d.ts} +1 -1
  72. package/dist/{routeResolver-D5OzD0iM.d.cts → routeResolver-CVFE8KKe.d.cts} +1 -1
  73. package/dist/transformer/TransformerChainExecutor.d.cts +2 -0
  74. package/dist/transformer/TransformerChainExecutor.d.ts +2 -0
  75. package/dist/transformer/TransformerService.d.cts +2 -0
  76. package/dist/transformer/TransformerService.d.ts +2 -0
  77. package/dist/transformer/transformers/AnthropicTransformer.cjs +4 -2
  78. package/dist/transformer/transformers/AnthropicTransformer.d.cts +7 -2
  79. package/dist/transformer/transformers/AnthropicTransformer.d.ts +7 -2
  80. package/dist/transformer/transformers/AnthropicTransformer.js +3 -1
  81. package/dist/transformer/transformers/GeminiCodeAssistTransformer.cjs +5 -3
  82. package/dist/transformer/transformers/GeminiCodeAssistTransformer.d.cts +2 -0
  83. package/dist/transformer/transformers/GeminiCodeAssistTransformer.d.ts +2 -0
  84. package/dist/transformer/transformers/GeminiCodeAssistTransformer.js +4 -2
  85. package/dist/transformer/transformers/GeminiTransformer.cjs +5 -3
  86. package/dist/transformer/transformers/GeminiTransformer.d.cts +4 -2
  87. package/dist/transformer/transformers/GeminiTransformer.d.ts +4 -2
  88. package/dist/transformer/transformers/GeminiTransformer.js +4 -2
  89. package/dist/transformer/transformers/OpenAIResponseTransformer.cjs +3 -2
  90. package/dist/transformer/transformers/OpenAIResponseTransformer.d.cts +4 -2
  91. package/dist/transformer/transformers/OpenAIResponseTransformer.d.ts +4 -2
  92. package/dist/transformer/transformers/OpenAIResponseTransformer.js +2 -1
  93. package/dist/transformer/transformers/OpenAITransformer.cjs +3 -2
  94. package/dist/transformer/transformers/OpenAITransformer.d.cts +3 -1
  95. package/dist/transformer/transformers/OpenAITransformer.d.ts +3 -1
  96. package/dist/transformer/transformers/OpenAITransformer.js +2 -1
  97. package/dist/transformer/transformers.cjs +10 -8
  98. package/dist/transformer/transformers.d.cts +2 -0
  99. package/dist/transformer/transformers.d.ts +2 -0
  100. package/dist/transformer/transformers.js +9 -7
  101. package/dist/transformer/types.d.cts +16 -5
  102. package/dist/transformer/types.d.ts +16 -5
  103. package/dist/transformer.cjs +28 -9
  104. package/dist/transformer.d.cts +76 -1
  105. package/dist/transformer.d.ts +76 -1
  106. package/dist/transformer.js +28 -9
  107. package/dist/{types-CqZD77rS.d.ts → types-BB8pxk_3.d.ts} +1 -1
  108. package/dist/{types-gjJ5lJ5n.d.cts → types-C2MwkLah.d.cts} +1 -1
  109. package/dist/usage/usage-recorder.cjs +2 -2
  110. package/dist/usage/usage-recorder.d.cts +6 -0
  111. package/dist/usage/usage-recorder.d.ts +6 -0
  112. package/dist/usage/usage-recorder.js +1 -1
  113. package/dist/usage.cjs +2 -2
  114. package/dist/usage.js +1 -1
  115. package/package.json +1 -1
  116. /package/dist/{chunk-FZ46M34D.cjs → chunk-QVOB2DNX.cjs} +0 -0
  117. /package/dist/{chunk-46RLHI7D.js → chunk-WTQMX2Q6.js} +0 -0
@@ -1,16 +1,16 @@
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
 
5
5
 
6
6
 
7
- var _chunkOXHNSZ53cjs = require('./chunk-OXHNSZ53.cjs');
7
+ var _chunkZQQUD66Mcjs = require('./chunk-ZQQUD66M.cjs');
8
8
 
9
9
 
10
- var _chunkV4TXMEJScjs = require('./chunk-V4TXMEJS.cjs');
10
+ var _chunk5LNYGCIWcjs = require('./chunk-5LNYGCIW.cjs');
11
11
 
12
12
 
13
- var _chunkOVQBNVSVcjs = require('./chunk-OVQBNVSV.cjs');
13
+ var _chunkCMVXX7ONcjs = require('./chunk-CMVXX7ON.cjs');
14
14
 
15
15
 
16
16
  var _chunkIIRPUVPHcjs = require('./chunk-IIRPUVPH.cjs');
@@ -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');
@@ -108,6 +111,9 @@ var _chunkXPIOJQS2cjs = require('./chunk-XPIOJQS2.cjs');
108
111
  var _chunkGDIXXKF2cjs = require('./chunk-GDIXXKF2.cjs');
109
112
 
110
113
 
114
+ var _chunkAOFWNWLTcjs = require('./chunk-AOFWNWLT.cjs');
115
+
116
+
111
117
  var _chunkRJUONLUQcjs = require('./chunk-RJUONLUQ.cjs');
112
118
 
113
119
 
@@ -410,9 +416,6 @@ function addOpenRouterProviderToRequest(requestBody, provider, modelId) {
410
416
  };
411
417
  }
412
418
 
413
- // src/completion/DirectApiHandler.ts
414
- var _thinkingconfig = require('@omnicross/contracts/thinking-config');
415
-
416
419
  // src/provider-proxy/ProviderProxy.ts
417
420
  var _http = require('http'); var _http2 = _interopRequireDefault(_http);
418
421
 
@@ -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
 
@@ -617,17 +651,17 @@ function getSharedExecutor() {
617
651
  }
618
652
  var sharedResponses = null;
619
653
  function getResponsesEndpointTransformer() {
620
- if (!sharedResponses) sharedResponses = new (0, _chunkOVQBNVSVcjs.OpenAIResponseTransformer)();
654
+ if (!sharedResponses) sharedResponses = new (0, _chunkCMVXX7ONcjs.OpenAIResponseTransformer)();
621
655
  return sharedResponses;
622
656
  }
623
657
  var sharedAnthropic = null;
624
658
  function getAnthropicEndpointTransformer() {
625
- if (!sharedAnthropic) sharedAnthropic = new (0, _chunkOXHNSZ53cjs.AnthropicTransformer)();
659
+ if (!sharedAnthropic) sharedAnthropic = new (0, _chunkZQQUD66Mcjs.AnthropicTransformer)();
626
660
  return sharedAnthropic;
627
661
  }
628
662
  var sharedGemini = null;
629
663
  function getGeminiEndpointTransformer() {
630
- if (!sharedGemini) sharedGemini = new (0, _chunkV4TXMEJScjs.GeminiTransformer)();
664
+ if (!sharedGemini) sharedGemini = new (0, _chunk5LNYGCIWcjs.GeminiTransformer)();
631
665
  return sharedGemini;
632
666
  }
633
667
  function readBody(req) {
@@ -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
  }
@@ -1396,7 +1432,8 @@ async function buildByoPlan(res, route, deps, resolvedModel, isStream) {
1396
1432
  name: provider.name,
1397
1433
  baseUrl: provider.api_base_url,
1398
1434
  apiKey,
1399
- models: provider.models || []
1435
+ models: provider.models || [],
1436
+ modelConfigs: provider.modelConfigs
1400
1437
  };
1401
1438
  const byoUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: resolvedModel, stream: isStream });
1402
1439
  const sameFormat = route.targetProviderFormat === "anthropic" || _chunkA2YMUCBTcjs.resolveApiFormat.call(void 0, provider) === "anthropic";
@@ -1412,7 +1449,7 @@ async function buildByoPlan(res, route, deps, resolvedModel, isStream) {
1412
1449
  isSubscription: false,
1413
1450
  provider,
1414
1451
  apiKey,
1415
- extendedContextEnabled: _nullishCoalesce(_optionalChain([route, 'access', _25 => _25.anthropicSdkHints, 'optionalAccess', _26 => _26.extendedContext, 'optionalAccess', _27 => _27.enabled]), () => ( false))
1452
+ extendedContextEnabled: _nullishCoalesce(_optionalChain([route, 'access', _37 => _37.anthropicSdkHints, 'optionalAccess', _38 => _38.extendedContext, 'optionalAccess', _39 => _39.enabled]), () => ( false))
1416
1453
  };
1417
1454
  }
1418
1455
  async function runSameFormatFetch(bodyToSend, plan, options, keyOverride) {
@@ -1422,7 +1459,7 @@ async function runSameFormatFetch(bodyToSend, plan, options, keyOverride) {
1422
1459
  }
1423
1460
  const effectiveKey = _nullishCoalesce(_nullishCoalesce(keyOverride, () => ( apiKey)), () => ( ""));
1424
1461
  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()]);
1462
+ const callerBeta = _optionalChain([options, 'access', _40 => _40.callerAnthropicBeta, 'optionalAccess', _41 => _41.trim, 'call', _42 => _42()]);
1426
1463
  if (callerBeta) {
1427
1464
  const existing = _nullishCoalesce(headers["anthropic-beta"], () => ( headers["Anthropic-Beta"]));
1428
1465
  const parts = new Set(
@@ -1448,7 +1485,7 @@ function resolveSameFormatBody(anthropicBody, rawBody, plan) {
1448
1485
  try {
1449
1486
  const m = JSON.parse(rawBody).model;
1450
1487
  originalModel = typeof m === "string" ? m : void 0;
1451
- } catch (e14) {
1488
+ } catch (e16) {
1452
1489
  originalModel = void 0;
1453
1490
  }
1454
1491
  return plan.resolvedModel !== originalModel ? JSON.stringify(anthropicBody) : rawBody;
@@ -1457,8 +1494,8 @@ async function runPipelineWithPoolReporting(anthropicBody, rawBody, plan, option
1457
1494
  const sameFormatBody = plan.sameFormat ? resolveSameFormatBody(anthropicBody, rawBody, plan) : rawBody;
1458
1495
  const runOnce = (keyOverride) => plan.sameFormat ? runSameFormatFetch(sameFormatBody, plan, options, keyOverride) : runPipeline(anthropicBody, plan);
1459
1496
  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])) {
1497
+ const outcome = await _optionalChain([plan, 'access', _43 => _43.auth, 'access', _44 => _44.onResult, 'optionalCall', _45 => _45(first.rawStatus)]);
1498
+ if (_optionalChain([outcome, 'optionalAccess', _46 => _46.rebound])) {
1462
1499
  console.info(
1463
1500
  "[ProviderProxy:anthropic] pool re-bound key after status",
1464
1501
  first.rawStatus,
@@ -1571,6 +1608,10 @@ function emitGeminiUsageRecord(recorder, rawUsageMetadata, attribution) {
1571
1608
  providerId: attribution.providerId,
1572
1609
  model: attribution.model,
1573
1610
  apiKeyId: attribution.apiKeyId,
1611
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _47 => _47.routeLease, 'optionalAccess', _48 => _48.runId]), () => ( null)),
1612
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _49 => _49.routeLease, 'optionalAccess', _50 => _50.leaseId]), () => ( null)),
1613
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _51 => _51.routeLease, 'optionalAccess', _52 => _52.consumer]), () => ( null)),
1614
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _53 => _53.routeLease, 'optionalAccess', _54 => _54.stageId]), () => ( null)),
1574
1615
  auditResponse: attribution.auditResponse,
1575
1616
  engineOrigin: "codex-ingress",
1576
1617
  usage: tapped,
@@ -1585,13 +1626,13 @@ function recordGeminiNonStreamUsage(recorder, bodyText, attribution) {
1585
1626
  parsed.usageMetadata,
1586
1627
  attribution
1587
1628
  );
1588
- } catch (e15) {
1629
+ } catch (e17) {
1589
1630
  }
1590
1631
  }
1591
1632
  function recordGeminiStreamUsage(recorder, rawUsageMetadata, attribution) {
1592
1633
  try {
1593
1634
  emitGeminiUsageRecord(recorder, rawUsageMetadata, attribution);
1594
- } catch (e16) {
1635
+ } catch (e18) {
1595
1636
  }
1596
1637
  }
1597
1638
 
@@ -1600,14 +1641,14 @@ var GENERATE_CONTENT_ACTION = ":generateContent";
1600
1641
  var STREAM_GENERATE_CONTENT_ACTION = ":streamGenerateContent";
1601
1642
  function isGeminiGenerateContentRequest(method, url) {
1602
1643
  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(/\/+$/, "")]), () => ( ""));
1644
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _55 => _55.split, 'call', _56 => _56("?"), 'access', _57 => _57[0], 'optionalAccess', _58 => _58.replace, 'call', _59 => _59(/\/+$/, "")]), () => ( ""));
1604
1645
  const lastSegment = _nullishCoalesce(path.split("/").pop(), () => ( ""));
1605
1646
  return lastSegment.includes(STREAM_GENERATE_CONTENT_ACTION) || lastSegment.includes(GENERATE_CONTENT_ACTION);
1606
1647
  }
1607
1648
  function isGeminiStreamRequest(url) {
1608
1649
  if (!url) return false;
1609
1650
  const [path, query = ""] = url.split("?");
1610
- const lastSegment = _nullishCoalesce((_nullishCoalesce(_optionalChain([path, 'optionalAccess', _40 => _40.replace, 'call', _41 => _41(/\/+$/, "")]), () => ( ""))).split("/").pop(), () => ( ""));
1651
+ const lastSegment = _nullishCoalesce((_nullishCoalesce(_optionalChain([path, 'optionalAccess', _60 => _60.replace, 'call', _61 => _61(/\/+$/, "")]), () => ( ""))).split("/").pop(), () => ( ""));
1611
1652
  if (lastSegment.includes(STREAM_GENERATE_CONTENT_ACTION)) return true;
1612
1653
  return /(^|&)alt=sse(&|$)/.test(query);
1613
1654
  }
@@ -1615,7 +1656,7 @@ async function handleGeminiGenerateContentRequest(res, rawBody, url, route, deps
1615
1656
  let geminiBody;
1616
1657
  try {
1617
1658
  geminiBody = JSON.parse(rawBody);
1618
- } catch (e17) {
1659
+ } catch (e19) {
1619
1660
  writeError(res, 400, "Invalid JSON in request body");
1620
1661
  return;
1621
1662
  }
@@ -1636,6 +1677,7 @@ async function handleGeminiGenerateContentRequest(res, rawBody, url, route, deps
1636
1677
  providerId: _nullishCoalesce(route.providerId, () => ( "gemini")),
1637
1678
  model: resolvedModel,
1638
1679
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
1680
+ routeLease: route.routeLease,
1639
1681
  // request-audit-log: correlate this request's tokens/cost to its audit record.
1640
1682
  auditResponse: res
1641
1683
  };
@@ -1699,7 +1741,8 @@ async function buildByoPlan2(res, route, deps, resolvedModel, isStream) {
1699
1741
  name: provider.name,
1700
1742
  baseUrl: provider.api_base_url,
1701
1743
  apiKey,
1702
- models: provider.models || []
1744
+ models: provider.models || [],
1745
+ modelConfigs: provider.modelConfigs
1703
1746
  };
1704
1747
  const byoUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: resolvedModel, stream: isStream });
1705
1748
  return {
@@ -1752,8 +1795,8 @@ async function runPipeline2(geminiBody, plan) {
1752
1795
  }
1753
1796
  async function runPipelineWithPoolReporting2(geminiBody, plan) {
1754
1797
  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])) {
1798
+ const outcome = await _optionalChain([plan, 'access', _62 => _62.auth, 'access', _63 => _63.onResult, 'optionalCall', _64 => _64(first.rawStatus)]);
1799
+ if (_optionalChain([outcome, 'optionalAccess', _65 => _65.rebound])) {
1757
1800
  console.log(
1758
1801
  "[ProviderProxy:gemini] pool re-bound key after status",
1759
1802
  first.rawStatus,
@@ -1792,6 +1835,10 @@ function emitChatCompletionsUsageRecord(recorder, rawUsage, attribution) {
1792
1835
  providerId: attribution.providerId,
1793
1836
  model: attribution.model,
1794
1837
  apiKeyId: attribution.apiKeyId,
1838
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _66 => _66.routeLease, 'optionalAccess', _67 => _67.runId]), () => ( null)),
1839
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _68 => _68.routeLease, 'optionalAccess', _69 => _69.leaseId]), () => ( null)),
1840
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _70 => _70.routeLease, 'optionalAccess', _71 => _71.consumer]), () => ( null)),
1841
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _72 => _72.routeLease, 'optionalAccess', _73 => _73.stageId]), () => ( null)),
1795
1842
  auditResponse: attribution.auditResponse,
1796
1843
  engineOrigin: "codex-ingress",
1797
1844
  usage: tapped,
@@ -1806,20 +1853,20 @@ function recordChatCompletionsNonStreamUsage(recorder, bodyText, attribution) {
1806
1853
  parsed.usage,
1807
1854
  attribution
1808
1855
  );
1809
- } catch (e18) {
1856
+ } catch (e20) {
1810
1857
  }
1811
1858
  }
1812
1859
  function recordChatCompletionsStreamUsage(recorder, rawUsage, attribution) {
1813
1860
  try {
1814
1861
  emitChatCompletionsUsageRecord(recorder, rawUsage, attribution);
1815
- } catch (e19) {
1862
+ } catch (e21) {
1816
1863
  }
1817
1864
  }
1818
1865
 
1819
1866
  // src/provider-proxy/ingress/openaiChatIngress.ts
1820
1867
  function isOpenAIChatRequest(method, url) {
1821
1868
  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(/\/+$/, "")]), () => ( ""));
1869
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _74 => _74.split, 'call', _75 => _75("?"), 'access', _76 => _76[0], 'optionalAccess', _77 => _77.replace, 'call', _78 => _78(/\/+$/, "")]), () => ( ""));
1823
1870
  return path.endsWith("/chat/completions");
1824
1871
  }
1825
1872
  var CHAT_BRIDGE_SUBSCRIPTION_PROVIDERS = /* @__PURE__ */ new Set(["claude"]);
@@ -1828,7 +1875,7 @@ async function handleOpenAIChatRequest(res, rawBody, route, deps) {
1828
1875
  let chatBody;
1829
1876
  try {
1830
1877
  chatBody = JSON.parse(rawBody);
1831
- } catch (e20) {
1878
+ } catch (e22) {
1832
1879
  writeError(res, 400, "Invalid JSON in request body");
1833
1880
  return;
1834
1881
  }
@@ -1844,6 +1891,7 @@ async function handleOpenAIChatRequest(res, rawBody, route, deps) {
1844
1891
  providerId: _nullishCoalesce(route.providerId, () => ( "openai")),
1845
1892
  model: resolvedModel,
1846
1893
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
1894
+ routeLease: route.routeLease,
1847
1895
  // request-audit-log: correlate this request's tokens/cost to its audit record.
1848
1896
  auditResponse: res
1849
1897
  };
@@ -1911,7 +1959,8 @@ async function buildByoPlan3(res, route, deps, resolvedModel, isStream) {
1911
1959
  name: provider.name,
1912
1960
  baseUrl: provider.api_base_url,
1913
1961
  apiKey,
1914
- models: provider.models || []
1962
+ models: provider.models || [],
1963
+ modelConfigs: provider.modelConfigs
1915
1964
  };
1916
1965
  const byoUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: resolvedModel, stream: isStream });
1917
1966
  return {
@@ -1941,7 +1990,7 @@ async function buildSubscriptionPlan2(res, route, deps, resolvedModel, isStream)
1941
1990
  );
1942
1991
  return null;
1943
1992
  }
1944
- const upstreamUrl = _optionalChain([profile, 'access', _51 => _51.resolveUpstreamUrl, 'optionalCall', _52 => _52(resolvedModel)]);
1993
+ const upstreamUrl = _optionalChain([profile, 'access', _79 => _79.resolveUpstreamUrl, 'optionalCall', _80 => _80(resolvedModel)]);
1945
1994
  if (!upstreamUrl) {
1946
1995
  writeError(res, 502, "Subscription profile is missing resolveUpstreamUrl");
1947
1996
  return null;
@@ -2021,8 +2070,8 @@ async function runPipeline3(chatBody, plan) {
2021
2070
  }
2022
2071
  async function runPipelineWithPoolReporting3(chatBody, plan) {
2023
2072
  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])) {
2073
+ const outcome = await _optionalChain([plan, 'access', _81 => _81.auth, 'access', _82 => _82.onResult, 'optionalCall', _83 => _83(first.rawStatus)]);
2074
+ if (_optionalChain([outcome, 'optionalAccess', _84 => _84.rebound])) {
2026
2075
  console.log(
2027
2076
  "[ProviderProxy:chat] pool re-bound key after status",
2028
2077
  first.rawStatus,
@@ -2035,7 +2084,7 @@ async function runPipelineWithPoolReporting3(chatBody, plan) {
2035
2084
  async function runPipelineWithSubscriptionRetry2(chatBody, plan) {
2036
2085
  const first = await runPipeline3(chatBody, plan);
2037
2086
  if (first.rawStatus !== 401) return first;
2038
- const refreshed = await _optionalChain([plan, 'access', _57 => _57.auth, 'access', _58 => _58.onUnauthorized, 'optionalCall', _59 => _59()]);
2087
+ const refreshed = await _optionalChain([plan, 'access', _85 => _85.auth, 'access', _86 => _86.onUnauthorized, 'optionalCall', _87 => _87()]);
2039
2088
  if (!refreshed) {
2040
2089
  console.warn("[ProviderProxy:chat] 401 not recoverable (onUnauthorized returned false)");
2041
2090
  return first;
@@ -2072,6 +2121,10 @@ function emitResponsesUsageRecord(recorder, rawUsage, attribution) {
2072
2121
  providerId: attribution.providerId,
2073
2122
  model: attribution.model,
2074
2123
  apiKeyId: attribution.apiKeyId,
2124
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _88 => _88.routeLease, 'optionalAccess', _89 => _89.runId]), () => ( null)),
2125
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _90 => _90.routeLease, 'optionalAccess', _91 => _91.leaseId]), () => ( null)),
2126
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _92 => _92.routeLease, 'optionalAccess', _93 => _93.consumer]), () => ( null)),
2127
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _94 => _94.routeLease, 'optionalAccess', _95 => _95.stageId]), () => ( null)),
2075
2128
  cacheKeySource: attribution.cacheKeySource,
2076
2129
  cacheKeyInjected: attribution.cacheKeyInjected,
2077
2130
  auditResponse: attribution.auditResponse,
@@ -2088,13 +2141,13 @@ function recordResponsesNonStreamUsage(recorder, bodyText, attribution) {
2088
2141
  parsed.usage,
2089
2142
  attribution
2090
2143
  );
2091
- } catch (e21) {
2144
+ } catch (e23) {
2092
2145
  }
2093
2146
  }
2094
2147
  function recordResponsesStreamUsage(recorder, rawUsage, attribution) {
2095
2148
  try {
2096
2149
  emitResponsesUsageRecord(recorder, rawUsage, attribution);
2097
- } catch (e22) {
2150
+ } catch (e24) {
2098
2151
  }
2099
2152
  }
2100
2153
 
@@ -2115,7 +2168,7 @@ function deadlineFromAllowanceSnapshot(accountId, now) {
2115
2168
  const candidates = snapshot.windows.filter(
2116
2169
  (w) => typeof w.usedPercent === "number" && w.usedPercent >= QUOTA_EXHAUSTED_PERCENT && typeof w.resetsAt === "string"
2117
2170
  ).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]);
2171
+ return _optionalChain([candidates, 'access', _96 => _96[0], 'optionalAccess', _97 => _97.ms]);
2119
2172
  }
2120
2173
  function resolveCodexQuotaDeadline(accountId, bodyText, now = Date.now()) {
2121
2174
  const parsed = parseTryAgainDeadline(bodyText);
@@ -2132,13 +2185,13 @@ function markCodexUsageLimitExhaustion(accountId, bodyText, now = Date.now()) {
2132
2185
  var SERVER_OVERLOADED_CODES = /* @__PURE__ */ new Set(["server_is_overloaded", "slow_down"]);
2133
2186
  function isCodexServerOverloadEvent(event) {
2134
2187
  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"]]);
2188
+ const error = _optionalChain([event, 'access', _98 => _98["response"], 'optionalAccess', _99 => _99["error"]]);
2189
+ const code = _optionalChain([error, 'optionalAccess', _100 => _100["code"]]);
2137
2190
  return typeof code === "string" && SERVER_OVERLOADED_CODES.has(code);
2138
2191
  }
2139
2192
  function isOpenAIResponsesRequest(method, url) {
2140
2193
  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(/\/+$/, "")]), () => ( ""));
2194
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _101 => _101.split, 'call', _102 => _102("?"), 'access', _103 => _103[0], 'optionalAccess', _104 => _104.replace, 'call', _105 => _105(/\/+$/, "")]), () => ( ""));
2142
2195
  return path.endsWith("/responses");
2143
2196
  }
2144
2197
  var SAFE_INJECTED_CACHE_KEY_SOURCES = /* @__PURE__ */ new Set([
@@ -2168,7 +2221,7 @@ async function handleOpenAIResponsesRequest(res, rawBody, route, deps, requestHe
2168
2221
  let responsesBody;
2169
2222
  try {
2170
2223
  responsesBody = JSON.parse(rawBody);
2171
- } catch (e23) {
2224
+ } catch (e25) {
2172
2225
  writeError(res, 400, "Invalid JSON in request body");
2173
2226
  return;
2174
2227
  }
@@ -2205,8 +2258,9 @@ async function handleOpenAIResponsesRequest(res, rawBody, route, deps, requestHe
2205
2258
  const usageAttribution = {
2206
2259
  sessionId: route.sessionId,
2207
2260
  providerId: _nullishCoalesce(route.providerId, () => ( "codex")),
2208
- model: resolvedModel,
2261
+ model: _nullishCoalesce(providerResponse.actualModel, () => ( resolvedModel)),
2209
2262
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
2263
+ routeLease: route.routeLease,
2210
2264
  cacheKeySource: cacheKeyAttribution.cacheKeySource,
2211
2265
  cacheKeyInjected: cacheKeyAttribution.cacheKeyInjected,
2212
2266
  // request-audit-log: correlate this request's tokens/cost to its audit record.
@@ -2217,7 +2271,7 @@ async function handleOpenAIResponsesRequest(res, rawBody, route, deps, requestHe
2217
2271
  extractUsage(event) {
2218
2272
  if (event["type"] !== "response.completed") return null;
2219
2273
  const response = event["response"];
2220
- const usage = _optionalChain([response, 'optionalAccess', _70 => _70["usage"]]);
2274
+ const usage = _optionalChain([response, 'optionalAccess', _106 => _106["usage"]]);
2221
2275
  return _nullishCoalesce(usage, () => ( null));
2222
2276
  },
2223
2277
  onUsage(rawUsage) {
@@ -2300,7 +2354,8 @@ async function buildByoPlan4(res, route, deps, resolvedModel, isStream) {
2300
2354
  name: provider.name,
2301
2355
  baseUrl: provider.api_base_url,
2302
2356
  apiKey,
2303
- models: provider.models || []
2357
+ models: provider.models || [],
2358
+ modelConfigs: provider.modelConfigs
2304
2359
  };
2305
2360
  const byoUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: resolvedModel, stream: isStream });
2306
2361
  return {
@@ -2320,7 +2375,7 @@ async function buildSubscriptionPlan3(res, route, deps, resolvedModel, isStream,
2320
2375
  writeError(res, 502, "Subscription mode requires a codex subscription profile");
2321
2376
  return null;
2322
2377
  }
2323
- const upstreamUrl = _optionalChain([profile, 'access', _71 => _71.resolveUpstreamUrl, 'optionalCall', _72 => _72(resolvedModel)]);
2378
+ const upstreamUrl = _optionalChain([profile, 'access', _107 => _107.resolveUpstreamUrl, 'optionalCall', _108 => _108(resolvedModel)]);
2324
2379
  if (!upstreamUrl) {
2325
2380
  writeError(res, 502, "Subscription profile is missing resolveUpstreamUrl");
2326
2381
  return null;
@@ -2391,6 +2446,7 @@ async function runPipeline4(responsesBody, plan) {
2391
2446
  const endpointTransformer = getResponsesEndpointTransformer();
2392
2447
  const { auth, sessionKey, chain, transformerProvider, resolvedModel, isStream, resolveUrl, upstreamUrl } = plan;
2393
2448
  let proxyAccountId;
2449
+ let actualModel = resolvedModel;
2394
2450
  let activityRecordId;
2395
2451
  const authHeaders = {};
2396
2452
  await auth.applyHeaders(authHeaders, {
@@ -2400,10 +2456,13 @@ async function runPipeline4(responsesBody, plan) {
2400
2456
  preferredAccountId: plan.preferredAccountId,
2401
2457
  preferredAccountGroup: plan.preferredAccountGroup,
2402
2458
  boundAccountFallbackPolicy: plan.boundAccountFallbackPolicy,
2403
- reportSelection: (accountId) => {
2459
+ reportSelection: (accountId, _isActive, remappedModel) => {
2404
2460
  proxyAccountId = accountId;
2461
+ if (remappedModel) actualModel = remappedModel;
2405
2462
  }
2406
2463
  });
2464
+ responsesBody.model = actualModel;
2465
+ transformerProvider.models = [actualModel];
2407
2466
  let rawStatus = null;
2408
2467
  const { response } = await _chunkMUHWHQVMcjs.executeProviderCall.call(void 0, {
2409
2468
  executor,
@@ -2427,7 +2486,7 @@ async function runPipeline4(responsesBody, plan) {
2427
2486
  return headers;
2428
2487
  },
2429
2488
  fetchFn: (url, headers, body) => {
2430
- console.log(`[ProviderProxy:responses] -> ${url} model=${resolvedModel} stream=${isStream}`);
2489
+ console.log(`[ProviderProxy:responses] -> ${url} model=${actualModel} stream=${isStream}`);
2431
2490
  return _chunkRJUONLUQcjs.fetchUpstream.call(void 0,
2432
2491
  url,
2433
2492
  { method: "POST", headers, body: JSON.stringify(body) },
@@ -2438,7 +2497,7 @@ async function runPipeline4(responsesBody, plan) {
2438
2497
  endpoint: "responses",
2439
2498
  sessionKey,
2440
2499
  sessionSource: _nullishCoalesce(plan.sessionSource, () => ( "none")),
2441
- model: resolvedModel,
2500
+ model: actualModel,
2442
2501
  onRecorded: (record) => {
2443
2502
  activityRecordId = record.id;
2444
2503
  }
@@ -2451,12 +2510,12 @@ async function runPipeline4(responsesBody, plan) {
2451
2510
  },
2452
2511
  runResponseChain: true
2453
2512
  });
2454
- return { response, rawStatus, accountId: proxyAccountId, activityRecordId };
2513
+ return { response, rawStatus, accountId: proxyAccountId, actualModel, activityRecordId };
2455
2514
  }
2456
2515
  async function runPipelineWithPoolReporting4(responsesBody, plan) {
2457
2516
  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])) {
2517
+ const outcome = await _optionalChain([plan, 'access', _109 => _109.auth, 'access', _110 => _110.onResult, 'optionalCall', _111 => _111(first.rawStatus)]);
2518
+ if (_optionalChain([outcome, 'optionalAccess', _112 => _112.rebound])) {
2460
2519
  console.log(
2461
2520
  "[ProviderProxy:responses] pool re-bound key after status",
2462
2521
  first.rawStatus,
@@ -2469,7 +2528,7 @@ async function runPipelineWithPoolReporting4(responsesBody, plan) {
2469
2528
  async function runPipelineWithSubscriptionRetry3(responsesBody, plan) {
2470
2529
  const first = await runPipeline4(responsesBody, plan);
2471
2530
  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)]);
2531
+ const refreshed = await _optionalChain([plan, 'access', _113 => _113.auth, 'access', _114 => _114.onUnauthorized, 'optionalCall', _115 => _115(plan.sessionKey)]);
2473
2532
  if (!refreshed) {
2474
2533
  console.warn("[ProviderProxy:responses] 401 not recoverable (onUnauthorized returned false)");
2475
2534
  return first;
@@ -2507,7 +2566,7 @@ function resolveRouteToken(req) {
2507
2566
  if (fromAuth) return fromAuth;
2508
2567
  const googKey = req.headers["x-goog-api-key"];
2509
2568
  const value = Array.isArray(googKey) ? googKey[0] : googKey;
2510
- return _optionalChain([value, 'optionalAccess', _80 => _80.trim, 'call', _81 => _81()]) || void 0;
2569
+ return _optionalChain([value, 'optionalAccess', _116 => _116.trim, 'call', _117 => _117()]) || void 0;
2511
2570
  }
2512
2571
  async function routeRequest(req, res, routes, deps) {
2513
2572
  const token = resolveRouteToken(req);
@@ -2547,7 +2606,7 @@ function isLoopbackAddress(addr) {
2547
2606
  return normalized === "127.0.0.1" || normalized.startsWith("127.") || normalized === "::1" || addr === "::1";
2548
2607
  }
2549
2608
  var ProviderProxy = (_class2 = class {
2550
- constructor(deps, routes) {;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);
2609
+ constructor(deps, routes) {;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);
2551
2610
  this.deps = deps;
2552
2611
  this.routes = _nullishCoalesce(routes, () => ( new ProviderProxyRouteMap()));
2553
2612
  }
@@ -2555,6 +2614,7 @@ var ProviderProxy = (_class2 = class {
2555
2614
  __init2() {this.server = null}
2556
2615
  __init3() {this.port = 0}
2557
2616
 
2617
+ __init4() {this.beforeStop = /* @__PURE__ */ new Set()}
2558
2618
  /**
2559
2619
  * Start the resident listener on a stable port on 127.0.0.1. Idempotent —
2560
2620
  * a second `start()` returns the already-bound port.
@@ -2595,6 +2655,12 @@ var ProviderProxy = (_class2 = class {
2595
2655
  }
2596
2656
  /** Stop the listener, clear all routes, and release the port. */
2597
2657
  async stop() {
2658
+ for (const cleanup of [...this.beforeStop]) {
2659
+ try {
2660
+ await cleanup();
2661
+ } catch (e26) {
2662
+ }
2663
+ }
2598
2664
  this.routes.clear();
2599
2665
  const server = this.server;
2600
2666
  if (!server) return;
@@ -2611,6 +2677,15 @@ var ProviderProxy = (_class2 = class {
2611
2677
  getBaseUrl() {
2612
2678
  return `http://127.0.0.1:${this.port}`;
2613
2679
  }
2680
+ /** Register daemon-owned cleanup that must run before route-map teardown. */
2681
+ registerBeforeStop(cleanup) {
2682
+ this.beforeStop.add(cleanup);
2683
+ return () => this.beforeStop.delete(cleanup);
2684
+ }
2685
+ /** Whether the resident listener has completed binding. */
2686
+ isReady() {
2687
+ return this.server !== null && this.port > 0;
2688
+ }
2614
2689
  /**
2615
2690
  * The SHARED route map. Exposed so the outbound API server
2616
2691
  * (`outbound-api-server`) can mint per-request routes on the SAME map and
@@ -2628,8 +2703,12 @@ var ProviderProxy = (_class2 = class {
2628
2703
  return this.deps;
2629
2704
  }
2630
2705
  /** 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);
2706
+ addRoute(context, idleMsOrOptions) {
2707
+ return this.routes.addRoute(context, idleMsOrOptions);
2708
+ }
2709
+ /** Re-arm one route while retaining the exact same bearer token. */
2710
+ renewRoute(token, idleMs) {
2711
+ return this.routes.renewRoute(token, idleMs);
2633
2712
  }
2634
2713
  /** Remove a route at run end. Returns true if an entry existed. */
2635
2714
  removeRoute(token) {
@@ -2641,6 +2720,802 @@ var ProviderProxy = (_class2 = class {
2641
2720
  }
2642
2721
  }, _class2);
2643
2722
 
2723
+ // src/provider-proxy/RouteLeaseManager.ts
2724
+
2725
+
2726
+ // src/provider-proxy/routeLeaseSchema.ts
2727
+
2728
+ var ROUTE_LEASE_REQUEST_SCHEMA = "omnicross.route-lease.request/1";
2729
+ var ROUTE_LEASE_RESULT_SCHEMA = "omnicross.route-lease/1";
2730
+ var ROUTE_LEASE_CAPABILITIES_SCHEMA = "omnicross.route-lease.capabilities/1";
2731
+ var ROUTE_LEASE_API_VERSION = 1;
2732
+ var ROUTE_LEASE_DEFAULT_TTL_SECONDS = 600;
2733
+ var ROUTE_LEASE_MAX_TTL_SECONDS = 3600;
2734
+ var ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES = 256;
2735
+ var ROUTE_LEASE_MAX_CONSUMER_BYTES = 64;
2736
+ var ROUTE_LEASE_MAX_EXECUTION_ID_BYTES = 128;
2737
+ var ROUTE_LEASE_MAX_SESSION_ID_BYTES = 512;
2738
+ var ROUTE_LEASE_MAX_MODEL_BYTES = 256;
2739
+ var ROUTE_LEASE_SESSION_HASH_DOMAIN = "omnicross.route-lease.session/v1";
2740
+ var ROUTE_LEASE_CODEX_TOKEN_ENV = "OMNICROSS_CODEX_ROUTE_TOKEN";
2741
+ var ROUTE_LEASE_RUNTIMES = ["claude", "codex"];
2742
+ var ROUTE_LEASE_CAPABILITIES = Object.freeze({
2743
+ schemaVersion: ROUTE_LEASE_CAPABILITIES_SCHEMA,
2744
+ runtimes: Object.freeze(["claude", "codex"]),
2745
+ upstreamKinds: Object.freeze(["provider", "account", "account-group", "account-pool"]),
2746
+ leaseApiVersion: ROUTE_LEASE_API_VERSION,
2747
+ codexAuthMode: "env_key",
2748
+ maxTtlSeconds: ROUTE_LEASE_MAX_TTL_SECONDS
2749
+ });
2750
+ var ROUTE_LEASE_RUNTIME_TABLE = Object.freeze({
2751
+ claude: Object.freeze({ endpoint: "messages", ingressFormat: "anthropic-messages", wirePath: "/v1/messages" }),
2752
+ codex: Object.freeze({ endpoint: "responses", ingressFormat: "openai-responses", wirePath: "/openai/responses" })
2753
+ });
2754
+ var ERROR_DEFAULTS = {
2755
+ invalid_request: { status: 400, retryable: false },
2756
+ runtime_unsupported: { status: 400, retryable: false },
2757
+ model_not_configured: { status: 400, retryable: false },
2758
+ format_unsupported: { status: 400, retryable: false },
2759
+ control_unauthorized: { status: 403, retryable: false },
2760
+ upstream_not_found: { status: 404, retryable: false },
2761
+ lease_not_found: { status: 404, retryable: false },
2762
+ idempotency_conflict: { status: 409, retryable: false },
2763
+ upstream_unavailable: { status: 409, retryable: true },
2764
+ lease_expired: { status: 410, retryable: false },
2765
+ upstream_exhausted: { status: 429, retryable: true },
2766
+ daemon_not_ready: { status: 503, retryable: true }
2767
+ };
2768
+ var RouteLeaseError = (_class3 = class extends Error {
2769
+ constructor(code, message, options = {}) {
2770
+ super(message, options.cause === void 0 ? void 0 : { cause: options.cause });_class3.prototype.__init5.call(this);;
2771
+ this.code = code;
2772
+ this.name = "RouteLeaseError";
2773
+ const defaults = ERROR_DEFAULTS[code];
2774
+ this.status = _nullishCoalesce(options.status, () => ( defaults.status));
2775
+ this.retryable = _nullishCoalesce(options.retryable, () => ( defaults.retryable));
2776
+ if (options.retryAfterSeconds !== void 0) {
2777
+ this.retryAfterSeconds = Math.max(1, Math.min(3600, Math.floor(options.retryAfterSeconds)));
2778
+ }
2779
+ }
2780
+
2781
+ __init5() {this.type = "route_lease_error"}
2782
+
2783
+
2784
+
2785
+ toResponse() {
2786
+ return { error: { type: this.type, code: this.code, message: this.message, retryable: this.retryable } };
2787
+ }
2788
+ }, _class3);
2789
+ var IDEMPOTENCY_RE = /^[A-Za-z0-9._:-]+$/u;
2790
+ var CONTROL_RE = /[\u0000-\u001f\u007f-\u009f]/u;
2791
+ var PROCESS_SESSION_HMAC_KEY = _crypto.randomBytes.call(void 0, 32);
2792
+ function isRecord(value) {
2793
+ return value !== null && typeof value === "object" && !Array.isArray(value);
2794
+ }
2795
+ function byteLength(value) {
2796
+ return Buffer.byteLength(value, "utf8");
2797
+ }
2798
+ function normalizedString(value, field, maxBytes, options = {}) {
2799
+ if (typeof value !== "string") throw invalid(`${field} must be a string`);
2800
+ const normalized = value.trim();
2801
+ if (!normalized || byteLength(normalized) > maxBytes || CONTROL_RE.test(normalized)) {
2802
+ throw invalid(`${field} is invalid`);
2803
+ }
2804
+ if (options.asciiPattern && !options.asciiPattern.test(normalized)) {
2805
+ throw invalid(`${field} is invalid`);
2806
+ }
2807
+ return normalized;
2808
+ }
2809
+ function optionalExecutionId(value, field) {
2810
+ return value === void 0 ? void 0 : normalizedString(value, field, ROUTE_LEASE_MAX_EXECUTION_ID_BYTES);
2811
+ }
2812
+ function invalid(message) {
2813
+ return new RouteLeaseError("invalid_request", message);
2814
+ }
2815
+ function normalizeRouteLeaseIdempotencyKey(value) {
2816
+ return normalizedString(value, "Idempotency-Key", ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES, {
2817
+ asciiPattern: IDEMPOTENCY_RE
2818
+ });
2819
+ }
2820
+ function normalizeRouteLeaseTtl(value) {
2821
+ if (value === void 0) return ROUTE_LEASE_DEFAULT_TTL_SECONDS;
2822
+ if (!Number.isInteger(value) || value < 1 || value > ROUTE_LEASE_MAX_TTL_SECONDS) {
2823
+ throw invalid(`ttlSeconds must be an integer from 1 to ${ROUTE_LEASE_MAX_TTL_SECONDS}`);
2824
+ }
2825
+ return value;
2826
+ }
2827
+ function normalizeUpstream(value) {
2828
+ if (!isRecord(value) || typeof value.kind !== "string") throw invalid("upstream is invalid");
2829
+ const providerId = normalizedString(value.providerId, "upstream.providerId", 128);
2830
+ switch (value.kind) {
2831
+ case "provider": {
2832
+ const keyId = value.keyId === void 0 ? void 0 : normalizedString(value.keyId, "upstream.keyId", 128);
2833
+ return keyId ? { kind: "provider", providerId, keyId } : { kind: "provider", providerId };
2834
+ }
2835
+ case "account":
2836
+ return {
2837
+ kind: "account",
2838
+ providerId,
2839
+ accountId: normalizedString(value.accountId, "upstream.accountId", 128)
2840
+ };
2841
+ case "account-group":
2842
+ return {
2843
+ kind: "account-group",
2844
+ providerId,
2845
+ group: normalizedString(value.group, "upstream.group", 128)
2846
+ };
2847
+ case "account-pool":
2848
+ return { kind: "account-pool", providerId };
2849
+ default:
2850
+ throw invalid("upstream kind is unsupported");
2851
+ }
2852
+ }
2853
+ function hashRouteLeaseSessionId(sessionId, hmacKey = PROCESS_SESSION_HMAC_KEY) {
2854
+ return _crypto.createHmac.call(void 0, "sha256", hmacKey).update(ROUTE_LEASE_SESSION_HASH_DOMAIN, "utf8").update("\0", "utf8").update(sessionId, "utf8").digest("hex");
2855
+ }
2856
+ function canonicalizeRouteLeasePayload(value) {
2857
+ if (value === null || typeof value !== "object") return JSON.stringify(value);
2858
+ if (Array.isArray(value)) {
2859
+ return `[${value.map((entry) => canonicalizeRouteLeasePayload(entry)).join(",")}]`;
2860
+ }
2861
+ const record = value;
2862
+ const fields = Object.keys(record).filter((key) => record[key] !== void 0).sort().map((key) => `${JSON.stringify(key)}:${canonicalizeRouteLeasePayload(record[key])}`);
2863
+ return `{${fields.join(",")}}`;
2864
+ }
2865
+ function hashRouteLeasePayload(value) {
2866
+ return _crypto.createHash.call(void 0, "sha256").update(canonicalizeRouteLeasePayload(value), "utf8").digest("hex");
2867
+ }
2868
+ function parseRouteLeaseCreate(raw, idempotencyHeader, hmacKey = PROCESS_SESSION_HMAC_KEY) {
2869
+ if (!isRecord(raw)) throw invalid("request body must be a JSON object");
2870
+ if (raw.schemaVersion !== ROUTE_LEASE_REQUEST_SCHEMA) {
2871
+ throw invalid(`schemaVersion must be ${ROUTE_LEASE_REQUEST_SCHEMA}`);
2872
+ }
2873
+ const consumer = normalizedString(raw.consumer, "consumer", ROUTE_LEASE_MAX_CONSUMER_BYTES);
2874
+ const runtime = raw.runtime;
2875
+ if (runtime !== "claude" && runtime !== "codex") {
2876
+ throw new RouteLeaseError("runtime_unsupported", "runtime is unsupported");
2877
+ }
2878
+ const upstream = normalizeUpstream(raw.upstream);
2879
+ if (typeof raw.model !== "string" || raw.model.trim().length === 0) {
2880
+ throw new RouteLeaseError("model_not_configured", "model is not configured");
2881
+ }
2882
+ const model = normalizedString(raw.model, "model", ROUTE_LEASE_MAX_MODEL_BYTES);
2883
+ const ttlSeconds = normalizeRouteLeaseTtl(raw.ttlSeconds);
2884
+ const idempotencyKey = normalizeRouteLeaseIdempotencyKey(idempotencyHeader);
2885
+ let execution;
2886
+ let semanticExecution;
2887
+ if (raw.execution !== void 0) {
2888
+ if (!isRecord(raw.execution)) throw invalid("execution must be a JSON object");
2889
+ const runId = optionalExecutionId(raw.execution.runId, "execution.runId");
2890
+ const stageId = optionalExecutionId(raw.execution.stageId, "execution.stageId");
2891
+ let attempt;
2892
+ if (raw.execution.attempt !== void 0) {
2893
+ if (!Number.isSafeInteger(raw.execution.attempt) || raw.execution.attempt < 1) {
2894
+ throw invalid("execution.attempt must be a positive integer");
2895
+ }
2896
+ attempt = raw.execution.attempt;
2897
+ }
2898
+ let sessionId;
2899
+ let sessionIdHash;
2900
+ if (raw.execution.sessionId !== void 0) {
2901
+ sessionId = normalizedString(raw.execution.sessionId, "execution.sessionId", ROUTE_LEASE_MAX_SESSION_ID_BYTES);
2902
+ sessionIdHash = hashRouteLeaseSessionId(sessionId, hmacKey);
2903
+ }
2904
+ if (runId !== void 0 || stageId !== void 0 || attempt !== void 0 || sessionIdHash !== void 0) {
2905
+ execution = { runId, stageId, attempt, sessionIdHash };
2906
+ semanticExecution = { runId, stageId, attempt, sessionId };
2907
+ }
2908
+ }
2909
+ const request = {
2910
+ schemaVersion: ROUTE_LEASE_REQUEST_SCHEMA,
2911
+ consumer,
2912
+ runtime,
2913
+ upstream,
2914
+ model,
2915
+ ...execution ? { execution } : {},
2916
+ ttlSeconds
2917
+ };
2918
+ const semanticPayload = {
2919
+ schemaVersion: request.schemaVersion,
2920
+ consumer,
2921
+ runtime,
2922
+ upstream,
2923
+ model,
2924
+ ...semanticExecution ? { execution: semanticExecution } : {},
2925
+ ttlSeconds
2926
+ };
2927
+ return { request, idempotencyKey, payloadHash: hashRouteLeasePayload(semanticPayload) };
2928
+ }
2929
+ function routeLeaseRuntime(runtime) {
2930
+ switch (runtime) {
2931
+ case "claude":
2932
+ return ROUTE_LEASE_RUNTIME_TABLE.claude;
2933
+ case "codex":
2934
+ return ROUTE_LEASE_RUNTIME_TABLE.codex;
2935
+ default: {
2936
+ const exhaustive = runtime;
2937
+ throw new RouteLeaseError("runtime_unsupported", `runtime is unsupported: ${String(exhaustive)}`);
2938
+ }
2939
+ }
2940
+ }
2941
+
2942
+ // src/provider-proxy/RouteLeaseManager.ts
2943
+ var SYSTEM_ROUTE_LEASE_CLOCK = {
2944
+ now: () => Date.now(),
2945
+ setTimeout(callback, delayMs) {
2946
+ const timer = setTimeout(callback, delayMs);
2947
+ _optionalChain([timer, 'access', _118 => _118.unref, 'optionalCall', _119 => _119()]);
2948
+ return timer;
2949
+ },
2950
+ clearTimeout(handle) {
2951
+ clearTimeout(handle);
2952
+ }
2953
+ };
2954
+ function cloneUpstream(upstream) {
2955
+ return { ...upstream };
2956
+ }
2957
+ function metadataCopy(metadata) {
2958
+ return {
2959
+ ...metadata,
2960
+ upstream: cloneUpstream(metadata.upstream),
2961
+ ...metadata.execution ? { execution: { ...metadata.execution } } : {}
2962
+ };
2963
+ }
2964
+ var RouteLeaseManager = (_class4 = class {
2965
+ 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);
2966
+ this.routePort = routePort;
2967
+ this.targetResolver = targetResolver;
2968
+ this.descriptors = descriptors;
2969
+ this.clock = _nullishCoalesce(options.clock, () => ( SYSTEM_ROUTE_LEASE_CLOCK));
2970
+ this.hmacKey = _nullishCoalesce(options.hmacKey, () => ( _crypto.randomBytes.call(void 0, 32)));
2971
+ this.tombstoneMaxCount = Math.max(1, _nullishCoalesce(options.tombstoneMaxCount, () => ( 512)));
2972
+ this.tombstoneMaxAgeMs = Math.max(1, _nullishCoalesce(options.tombstoneMaxAgeMs, () => ( 60 * 60 * 1e3)));
2973
+ this.logger = options.logger;
2974
+ this.afterPublish = options.afterPublish;
2975
+ }
2976
+
2977
+
2978
+
2979
+ __init6() {this.active = /* @__PURE__ */ new Map()}
2980
+ __init7() {this.idempotency = /* @__PURE__ */ new Map()}
2981
+ __init8() {this.pendingCreates = /* @__PURE__ */ new Map()}
2982
+ __init9() {this.tombstones = /* @__PURE__ */ new Map()}
2983
+
2984
+
2985
+
2986
+
2987
+
2988
+
2989
+ __init10() {this.closed = false}
2990
+ parse(raw, idempotencyKey) {
2991
+ return parseRouteLeaseCreate(raw, idempotencyKey, this.hmacKey);
2992
+ }
2993
+ async createFromRequest(raw, idempotencyKey) {
2994
+ return this.create(this.parse(raw, idempotencyKey));
2995
+ }
2996
+ async create(input) {
2997
+ if (this.closed) throw new RouteLeaseError("daemon_not_ready", "route lease manager is shutting down");
2998
+ const request = input.request;
2999
+ const scope = `${request.consumer}\0${input.idempotencyKey}`;
3000
+ const existing = this.idempotency.get(scope);
3001
+ if (existing) {
3002
+ const lease = this.active.get(existing.leaseId);
3003
+ if (!lease) {
3004
+ this.idempotency.delete(scope);
3005
+ } else if (existing.payloadHash !== input.payloadHash) {
3006
+ throw new RouteLeaseError("idempotency_conflict", "idempotency key is already used for a different live request");
3007
+ } else {
3008
+ return { created: false, result: lease.result };
3009
+ }
3010
+ }
3011
+ const pending = this.pendingCreates.get(scope);
3012
+ if (pending) {
3013
+ if (pending.payloadHash !== input.payloadHash) {
3014
+ throw new RouteLeaseError("idempotency_conflict", "idempotency key is already used for a different live request");
3015
+ }
3016
+ const outcome2 = await pending.outcome;
3017
+ return { created: false, result: outcome2.result };
3018
+ }
3019
+ const outcome = this.createNew(input, scope);
3020
+ const pendingCreate = { payloadHash: input.payloadHash, outcome };
3021
+ this.pendingCreates.set(scope, pendingCreate);
3022
+ try {
3023
+ return await outcome;
3024
+ } finally {
3025
+ if (this.pendingCreates.get(scope) === pendingCreate) this.pendingCreates.delete(scope);
3026
+ }
3027
+ }
3028
+ async createNew(input, scope) {
3029
+ const request = input.request;
3030
+ if (!this.routePort.isReady()) {
3031
+ throw new RouteLeaseError("daemon_not_ready", "resident provider proxy is not ready");
3032
+ }
3033
+ if (!this.descriptors.has(request.runtime)) {
3034
+ throw new RouteLeaseError("runtime_unsupported", "runtime launch adapter is unavailable");
3035
+ }
3036
+ const leaseId = _crypto.randomUUID.call(void 0, );
3037
+ const resolved = await this.targetResolver.resolve(request);
3038
+ this.assertOpen();
3039
+ const route = {
3040
+ ...resolved,
3041
+ sessionId: _nullishCoalesce(_optionalChain([request, 'access', _120 => _120.execution, 'optionalAccess', _121 => _121.sessionIdHash]), () => ( null)),
3042
+ routeLease: {
3043
+ leaseId,
3044
+ consumer: request.consumer,
3045
+ ..._optionalChain([request, 'access', _122 => _122.execution, 'optionalAccess', _123 => _123.runId]) ? { runId: request.execution.runId } : {},
3046
+ ..._optionalChain([request, 'access', _124 => _124.execution, 'optionalAccess', _125 => _125.stageId]) ? { stageId: request.execution.stageId } : {}
3047
+ }
3048
+ };
3049
+ const now = this.clock.now();
3050
+ const ttlMs = request.ttlSeconds * 1e3;
3051
+ const expiresAtMs = now + ttlMs;
3052
+ let routeToken;
3053
+ let published = false;
3054
+ let activeLease;
3055
+ try {
3056
+ routeToken = this.routePort.addRoute(route, {
3057
+ idleMs: ttlMs,
3058
+ onActivity: (at) => this.noteActivity(leaseId, at),
3059
+ onEvicted: () => this.onRouteEvicted(leaseId)
3060
+ });
3061
+ const launch = this.descriptors.build(request.runtime, {
3062
+ proxyBaseUrl: this.routePort.getBaseUrl(),
3063
+ model: request.model,
3064
+ routeToken
3065
+ });
3066
+ this.assertOpen();
3067
+ const metadata = {
3068
+ leaseId,
3069
+ consumer: request.consumer,
3070
+ runtime: request.runtime,
3071
+ upstream: cloneUpstream(request.upstream),
3072
+ model: request.model,
3073
+ createdAt: new Date(now).toISOString(),
3074
+ expiresAt: new Date(expiresAtMs).toISOString(),
3075
+ status: "active",
3076
+ ...request.execution ? { execution: { ...request.execution } } : {}
3077
+ };
3078
+ const result = {
3079
+ schemaVersion: ROUTE_LEASE_RESULT_SCHEMA,
3080
+ ...metadata,
3081
+ status: "active",
3082
+ launch: { env: { ...launch.env }, extraArgs: [...launch.extraArgs] }
3083
+ };
3084
+ const timer = this.clock.setTimeout(() => this.expire(leaseId), ttlMs);
3085
+ activeLease = {
3086
+ leaseId,
3087
+ routeToken,
3088
+ idempotencyScope: scope,
3089
+ payloadHash: input.payloadHash,
3090
+ request,
3091
+ route,
3092
+ result,
3093
+ metadata,
3094
+ expiresAtMs,
3095
+ timer
3096
+ };
3097
+ this.assertOpen();
3098
+ this.active.set(leaseId, activeLease);
3099
+ this.idempotency.set(scope, { leaseId, payloadHash: input.payloadHash });
3100
+ published = true;
3101
+ _optionalChain([this, 'access', _126 => _126.afterPublish, 'optionalCall', _127 => _127()]);
3102
+ if (this.closed) {
3103
+ this.finalize(activeLease, "released");
3104
+ throw new RouteLeaseError("daemon_not_ready", "route lease manager is shutting down");
3105
+ }
3106
+ return { created: true, result };
3107
+ } catch (error) {
3108
+ if (published && activeLease) {
3109
+ this.active.delete(leaseId);
3110
+ this.idempotency.delete(scope);
3111
+ this.clock.clearTimeout(activeLease.timer);
3112
+ }
3113
+ if (routeToken) this.safeRemoveRoute(routeToken, leaseId);
3114
+ if (error instanceof RouteLeaseError) throw error;
3115
+ throw new RouteLeaseError("upstream_unavailable", "route lease creation failed safely", { cause: error });
3116
+ }
3117
+ }
3118
+ list() {
3119
+ this.pruneTombstones();
3120
+ return [
3121
+ ...[...this.active.values()].map((lease) => metadataCopy(lease.metadata)),
3122
+ ...[...this.tombstones.values()].map((entry) => metadataCopy(entry.metadata))
3123
+ ].sort((left, right) => left.createdAt.localeCompare(right.createdAt));
3124
+ }
3125
+ get(leaseId) {
3126
+ const active = this.active.get(leaseId);
3127
+ if (active) return metadataCopy(active.metadata);
3128
+ this.pruneTombstones();
3129
+ const tombstone = this.tombstones.get(leaseId);
3130
+ if (tombstone) return metadataCopy(tombstone.metadata);
3131
+ throw new RouteLeaseError("lease_not_found", "route lease was not found");
3132
+ }
3133
+ renew(leaseId, ttlSeconds) {
3134
+ const lease = this.active.get(leaseId);
3135
+ if (!lease) {
3136
+ this.pruneTombstones();
3137
+ const known = this.tombstones.get(leaseId);
3138
+ if (_optionalChain([known, 'optionalAccess', _128 => _128.metadata, 'access', _129 => _129.status]) === "expired") {
3139
+ throw new RouteLeaseError("lease_expired", "route lease has expired");
3140
+ }
3141
+ throw new RouteLeaseError("lease_not_found", "route lease was not found");
3142
+ }
3143
+ const now = this.clock.now();
3144
+ if (now >= lease.expiresAtMs) {
3145
+ this.finalize(lease, "expired");
3146
+ throw new RouteLeaseError("lease_expired", "route lease has expired");
3147
+ }
3148
+ const ttlMs = ttlSeconds * 1e3;
3149
+ const expiresAtMs = now + ttlMs;
3150
+ const nextTimer = this.clock.setTimeout(() => this.expire(leaseId), ttlMs);
3151
+ const previousTimer = lease.timer;
3152
+ lease.timer = nextTimer;
3153
+ lease.expiresAtMs = expiresAtMs;
3154
+ lease.metadata = { ...lease.metadata, expiresAt: new Date(expiresAtMs).toISOString() };
3155
+ this.clock.clearTimeout(previousTimer);
3156
+ if (!this.routePort.renewRoute(lease.routeToken, ttlMs)) {
3157
+ this.finalize(lease, "expired", true);
3158
+ throw new RouteLeaseError("lease_expired", "route lease route has expired");
3159
+ }
3160
+ return { leaseId, expiresAt: lease.metadata.expiresAt, status: "active" };
3161
+ }
3162
+ release(leaseId) {
3163
+ const lease = this.active.get(leaseId);
3164
+ if (!lease) return { leaseId, released: false };
3165
+ this.finalize(lease, "released");
3166
+ return { leaseId, released: true };
3167
+ }
3168
+ shutdown() {
3169
+ if (this.closed) return;
3170
+ this.closed = true;
3171
+ for (const lease of [...this.active.values()]) {
3172
+ try {
3173
+ this.finalize(lease, "released");
3174
+ } catch (error) {
3175
+ _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) })]);
3176
+ }
3177
+ }
3178
+ }
3179
+ activeCount() {
3180
+ return this.active.size;
3181
+ }
3182
+ assertOpen() {
3183
+ if (this.closed) {
3184
+ throw new RouteLeaseError("daemon_not_ready", "route lease manager is shutting down");
3185
+ }
3186
+ }
3187
+ noteActivity(leaseId, at) {
3188
+ const lease = this.active.get(leaseId);
3189
+ if (!lease || at >= lease.expiresAtMs) return;
3190
+ lease.metadata = { ...lease.metadata, lastActivityAt: new Date(at).toISOString() };
3191
+ }
3192
+ onRouteEvicted(leaseId) {
3193
+ const lease = this.active.get(leaseId);
3194
+ if (lease) this.finalize(lease, "expired", true);
3195
+ }
3196
+ expire(leaseId) {
3197
+ const lease = this.active.get(leaseId);
3198
+ if (!lease) return;
3199
+ const remaining = lease.expiresAtMs - this.clock.now();
3200
+ if (remaining > 0) {
3201
+ lease.timer = this.clock.setTimeout(() => this.expire(leaseId), remaining);
3202
+ return;
3203
+ }
3204
+ this.finalize(lease, "expired");
3205
+ }
3206
+ finalize(lease, status, routeAlreadyGone = false) {
3207
+ if (this.active.get(lease.leaseId) !== lease) return;
3208
+ this.active.delete(lease.leaseId);
3209
+ this.idempotency.delete(lease.idempotencyScope);
3210
+ this.clock.clearTimeout(lease.timer);
3211
+ if (!routeAlreadyGone) this.safeRemoveRoute(lease.routeToken, lease.leaseId);
3212
+ const metadata = { ...lease.metadata, status };
3213
+ this.tombstones.delete(lease.leaseId);
3214
+ this.tombstones.set(lease.leaseId, { metadata, terminalAtMs: this.clock.now() });
3215
+ this.pruneTombstones();
3216
+ }
3217
+ safeRemoveRoute(routeToken, leaseId) {
3218
+ try {
3219
+ this.routePort.removeRoute(routeToken);
3220
+ } catch (error) {
3221
+ _optionalChain([this, 'access', _133 => _133.logger, 'optionalAccess', _134 => _134.warn, 'call', _135 => _135("route lease route cleanup failed", { leaseId, error: safeErrorName(error) })]);
3222
+ }
3223
+ }
3224
+ pruneTombstones() {
3225
+ const cutoff = this.clock.now() - this.tombstoneMaxAgeMs;
3226
+ for (const [leaseId, tombstone] of this.tombstones) {
3227
+ if (tombstone.terminalAtMs < cutoff) this.tombstones.delete(leaseId);
3228
+ }
3229
+ while (this.tombstones.size > this.tombstoneMaxCount) {
3230
+ const oldest = this.tombstones.keys().next().value;
3231
+ if (!oldest) break;
3232
+ this.tombstones.delete(oldest);
3233
+ }
3234
+ }
3235
+ }, _class4);
3236
+ function safeErrorName(error) {
3237
+ return error instanceof Error ? error.name : "unknown";
3238
+ }
3239
+
3240
+ // src/outbound-api/gatewayBindingResolver.ts
3241
+ var MESSAGE_FALLBACK_KINDS = ["sonnet", "opus", "haiku", "fable"];
3242
+ function nonBlank(value) {
3243
+ return typeof value === "string" && value.trim() !== "";
3244
+ }
3245
+ function gatewayBindingAllowsKey(binding, apiKeyId) {
3246
+ const scope = _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _136 => _136.apiKeyIds, 'optionalAccess', _137 => _137.length]) ? "selected" : "all")));
3247
+ return scope === "all" || Boolean(_optionalChain([binding, 'access', _138 => _138.apiKeyIds, 'optionalAccess', _139 => _139.includes, 'call', _140 => _140(apiKeyId)]));
3248
+ }
3249
+ function wildcardMatches(pattern, value) {
3250
+ const escaped = pattern.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")).join(".*");
3251
+ return new RegExp(`^${escaped}$`, "iu").test(value);
3252
+ }
3253
+ function resolveGatewayModelMapping(mappings, requestedModel) {
3254
+ if (!nonBlank(requestedModel)) return void 0;
3255
+ const wanted = requestedModel.trim();
3256
+ const exact = (_nullishCoalesce(mappings, () => ( []))).find(
3257
+ (mapping) => nonBlank(mapping.source) && !mapping.source.includes("*") && mapping.source.trim().toLocaleLowerCase() === wanted.toLocaleLowerCase()
3258
+ );
3259
+ if (exact && nonBlank(exact.target)) return exact.target.trim();
3260
+ const wildcard = (_nullishCoalesce(mappings, () => ( []))).find(
3261
+ (mapping) => nonBlank(mapping.source) && mapping.source.includes("*") && wildcardMatches(mapping.source.trim(), wanted)
3262
+ );
3263
+ return wildcard && nonBlank(wildcard.target) ? wildcard.target.trim() : void 0;
3264
+ }
3265
+ function routeCanServe(binding, requestedModel, role) {
3266
+ if (binding.modelMode === "passthrough") return nonBlank(requestedModel);
3267
+ if (_optionalChain([binding, 'access', _141 => _141.modelMappings, 'optionalAccess', _142 => _142.length])) {
3268
+ return nonBlank(resolveGatewayModelMapping(binding.modelMappings, requestedModel));
3269
+ }
3270
+ if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3271
+ const map = _nullishCoalesce(binding.modelMap, () => ( {}));
3272
+ const kind = _chunk4VZX5F4Tcjs.detectModelKind.call(void 0, binding.endpoint, requestedModel);
3273
+ if (kind && nonBlank(map[kind])) return true;
3274
+ if (binding.endpoint === "messages") {
3275
+ return MESSAGE_FALLBACK_KINDS.some((candidate) => nonBlank(map[candidate]));
3276
+ }
3277
+ return nonBlank(map.codex);
3278
+ }
3279
+ if (binding.endpoint === "chat" && binding.dispatchMode === "prefix") {
3280
+ return _chunk4VZX5F4Tcjs.resolvePrefixTarget.call(void 0, binding.prefixTargets, requestedModel) !== null;
3281
+ }
3282
+ if (binding.endpoint === "chat") {
3283
+ if (!requestedModel) return false;
3284
+ const wanted = requestedModel.trim().toLowerCase();
3285
+ return (_nullishCoalesce(binding.models, () => ( []))).some((ref) => _optionalChain([modelIdOf, 'call', _143 => _143(ref), 'optionalAccess', _144 => _144.toLowerCase, 'call', _145 => _145()]) === wanted);
3286
+ }
3287
+ return role === "background" ? nonBlank(binding.backgroundModel) : nonBlank(binding.defaultModel);
3288
+ }
3289
+ function modelIdOf(ref) {
3290
+ if (!nonBlank(ref)) return void 0;
3291
+ const trimmed = ref.trim();
3292
+ const comma = trimmed.indexOf(",");
3293
+ const modelId = comma >= 0 ? trimmed.slice(comma + 1).trim() : trimmed;
3294
+ return modelId || void 0;
3295
+ }
3296
+ function targetProviderId(target) {
3297
+ return target.providerId.trim();
3298
+ }
3299
+ function targetRef(target, ref) {
3300
+ const modelId = _nullishCoalesce(modelIdOf(ref), () => ( ""));
3301
+ return modelId ? `${targetProviderId(target)},${modelId}` : "";
3302
+ }
3303
+ function targetRefs(target, refs) {
3304
+ return (_nullishCoalesce(refs, () => ( []))).map((ref) => targetRef(target, ref)).filter(nonBlank);
3305
+ }
3306
+ function targetPrefixRefs(target, refs) {
3307
+ if (!refs) return void 0;
3308
+ const mapped = {};
3309
+ for (const kind of ["claude", "gpt", "gemini"]) {
3310
+ const ref = targetRef(target, refs[kind]);
3311
+ if (ref) mapped[kind] = ref;
3312
+ }
3313
+ return mapped.claude || mapped.gpt || mapped.gemini ? mapped : void 0;
3314
+ }
3315
+ function applySingleModel(config, binding, targetModel) {
3316
+ const ref = targetRef(binding.target, targetModel);
3317
+ if (!ref) return;
3318
+ if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3319
+ config.modelMap = Object.fromEntries(
3320
+ _chunk4VZX5F4Tcjs.modelKindsForEndpoint.call(void 0, binding.endpoint).map((kind) => [kind, ref])
3321
+ );
3322
+ } else if (binding.endpoint === "chat") {
3323
+ config.models = [ref];
3324
+ config.dispatchMode = "list";
3325
+ } else {
3326
+ config.defaultModel = ref;
3327
+ config.backgroundModel = ref;
3328
+ }
3329
+ }
3330
+ function gatewayBindingToEndpointConfig(binding, requestedModel) {
3331
+ const target = binding.target;
3332
+ const config = {
3333
+ endpoint: binding.endpoint,
3334
+ useSubscription: target.kind !== "provider"
3335
+ };
3336
+ const usesGenericModelHandling = binding.modelMode === "passthrough" || Boolean(_optionalChain([binding, 'access', _146 => _146.modelMappings, 'optionalAccess', _147 => _147.length]));
3337
+ const dynamicModel = binding.modelMode === "passthrough" ? requestedModel : resolveGatewayModelMapping(binding.modelMappings, requestedModel);
3338
+ if (usesGenericModelHandling) {
3339
+ if (nonBlank(dynamicModel)) applySingleModel(config, binding, dynamicModel);
3340
+ } else if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3341
+ config.modelMap = Object.fromEntries(
3342
+ Object.entries(_nullishCoalesce(binding.modelMap, () => ( {}))).map(([kind, ref]) => [kind, targetRef(target, ref)])
3343
+ );
3344
+ } else if (binding.endpoint === "chat") {
3345
+ config.models = targetRefs(target, binding.models);
3346
+ if (binding.dispatchMode === "prefix") {
3347
+ config.dispatchMode = "prefix";
3348
+ config.prefixTargets = targetPrefixRefs(target, binding.prefixTargets);
3349
+ }
3350
+ } else {
3351
+ config.defaultModel = targetRef(target, binding.defaultModel);
3352
+ config.backgroundModel = targetRef(target, binding.backgroundModel);
3353
+ if (binding.backgroundModelIds) config.backgroundModelIds = [...binding.backgroundModelIds];
3354
+ }
3355
+ if (target.kind === "account") {
3356
+ config.boundAccountId = target.accountId;
3357
+ config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3358
+ } else if (target.kind === "account-group") {
3359
+ config.boundAccountGroup = target.group;
3360
+ config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3361
+ } else if (target.kind === "provider" && target.keyId) {
3362
+ config.boundKeyId = target.keyId;
3363
+ config.boundKeyFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3364
+ }
3365
+ return config;
3366
+ }
3367
+ function candidateGatewayBindings(bindings, apiKeyId, endpoint) {
3368
+ const candidates = (_nullishCoalesce(bindings, () => ( []))).filter(
3369
+ (binding) => binding.enabled && binding.endpoint === endpoint && gatewayBindingAllowsKey(binding, apiKeyId)
3370
+ );
3371
+ const scopeOf = (binding) => _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _148 => _148.apiKeyIds, 'optionalAccess', _149 => _149.length]) ? "selected" : "all")));
3372
+ const scoped = candidates.filter((binding) => scopeOf(binding) === "selected");
3373
+ const pool = scoped.length > 0 ? scoped : candidates.filter((binding) => scopeOf(binding) === "all");
3374
+ return pool.sort(
3375
+ (left, right) => (_nullishCoalesce(left.priority, () => ( 100))) - (_nullishCoalesce(right.priority, () => ( 100))) || left.id.localeCompare(right.id)
3376
+ );
3377
+ }
3378
+ function candidateBackgroundModelIds(bindings, apiKeyId, endpoint) {
3379
+ const ids = [
3380
+ ...new Set(
3381
+ candidateGatewayBindings(bindings, apiKeyId, endpoint).flatMap(
3382
+ (binding) => _nullishCoalesce(binding.backgroundModelIds, () => ( []))
3383
+ )
3384
+ )
3385
+ ];
3386
+ return ids.length > 0 ? ids : void 0;
3387
+ }
3388
+ function resolveGatewayBinding(input) {
3389
+ const candidates = candidateGatewayBindings(input.bindings, input.apiKeyId, input.endpoint);
3390
+ const binding = _nullishCoalesce(candidates.find(
3391
+ (candidate) => candidate.fallback === "fail" || routeCanServe(candidate, input.requestedModel, input.role)
3392
+ ), () => ( candidates[0]));
3393
+ if (!binding) return { source: "none" };
3394
+ return {
3395
+ source: "binding",
3396
+ binding,
3397
+ config: gatewayBindingToEndpointConfig(binding, input.requestedModel)
3398
+ };
3399
+ }
3400
+
3401
+ // src/provider-proxy/RouteLeaseTargetResolver.ts
3402
+ function resolveEnvKey(raw) {
3403
+ if (!raw) return "";
3404
+ return raw.startsWith("$") ? _nullishCoalesce(process.env[raw.slice(1)], () => ( "")) : raw;
3405
+ }
3406
+ function providerSupportsModel(provider, model) {
3407
+ const flat = _nullishCoalesce(provider.models, () => ( []));
3408
+ const configured = _nullishCoalesce(provider.modelConfigs, () => ( []));
3409
+ if (flat.length === 0 && configured.length === 0) return false;
3410
+ return flat.includes(model) || configured.some((entry) => entry.id === model && entry.enabled !== false);
3411
+ }
3412
+ function syntheticBinding(request) {
3413
+ const runtime = routeLeaseRuntime(request.runtime);
3414
+ return {
3415
+ id: "route-lease-ephemeral",
3416
+ name: "Route Lease ephemeral target",
3417
+ enabled: true,
3418
+ endpoint: runtime.endpoint,
3419
+ target: request.upstream,
3420
+ fallback: "fail",
3421
+ modelMode: "passthrough"
3422
+ };
3423
+ }
3424
+ function transformerRegistered(llmConfig, name) {
3425
+ return Boolean(_optionalChain([llmConfig, 'access', _150 => _150.getTransformerService, 'call', _151 => _151(), 'optionalAccess', _152 => _152.getTransformer, 'call', _153 => _153(name)]));
3426
+ }
3427
+ var RouteLeaseTargetResolver = class {
3428
+ constructor(llmConfig, options = {}) {
3429
+ this.llmConfig = llmConfig;
3430
+ this.options = options;
3431
+ }
3432
+
3433
+
3434
+ async resolve(request) {
3435
+ const runtime = routeLeaseRuntime(request.runtime);
3436
+ await this.preflightTarget(request);
3437
+ const binding = syntheticBinding(request);
3438
+ const config = gatewayBindingToEndpointConfig(binding, request.model);
3439
+ const result = await _chunk4VZX5F4Tcjs.resolveRoute.call(void 0, {
3440
+ config,
3441
+ ingressFormat: runtime.ingressFormat,
3442
+ llmConfig: this.llmConfig,
3443
+ requestedModel: request.model,
3444
+ sessionId: _nullishCoalesce(_optionalChain([request, 'access', _154 => _154.execution, 'optionalAccess', _155 => _155.sessionIdHash]), () => ( null))
3445
+ });
3446
+ if (!result.ok) {
3447
+ throw new RouteLeaseError(
3448
+ result.error.status === 404 ? "upstream_not_found" : "upstream_unavailable",
3449
+ "the selected upstream could not be resolved"
3450
+ );
3451
+ }
3452
+ return { ...result.route, requestedModel: void 0 };
3453
+ }
3454
+ async preflightTarget(request) {
3455
+ const target = request.upstream;
3456
+ if (target.kind !== "provider") {
3457
+ await this.preflightSubscription(target, request.model);
3458
+ return;
3459
+ }
3460
+ const provider = await this.llmConfig.getProvider(target.providerId);
3461
+ if (!provider) throw new RouteLeaseError("upstream_not_found", "provider was not found");
3462
+ if (provider.enabled === false) throw new RouteLeaseError("upstream_unavailable", "provider is disabled");
3463
+ if (!providerSupportsModel(provider, request.model)) {
3464
+ throw new RouteLeaseError("model_not_configured", "model is not configured for the provider");
3465
+ }
3466
+ if (target.keyId) {
3467
+ const availability = await _optionalChain([this, 'access', _156 => _156.options, 'access', _157 => _157.providerKeys, 'optionalAccess', _158 => _158.getKeyAvailability, 'optionalCall', _159 => _159(
3468
+ target.providerId,
3469
+ target.keyId
3470
+ )]);
3471
+ if (availability === "not-found") {
3472
+ throw new RouteLeaseError("upstream_not_found", "the selected provider key was not found");
3473
+ }
3474
+ const usable = availability === "usable" || availability === void 0 && Boolean(this.options.providerKeys) && await this.options.providerKeys.hasUsableKey(target.providerId, target.keyId);
3475
+ if (!usable) {
3476
+ throw new RouteLeaseError("upstream_unavailable", "the selected provider key is unavailable");
3477
+ }
3478
+ } else {
3479
+ const directKey = resolveEnvKey(provider.api_key);
3480
+ const poolAvailability = await _optionalChain([this, 'access', _160 => _160.options, 'access', _161 => _161.providerKeys, 'optionalAccess', _162 => _162.getPoolAvailability, 'optionalCall', _163 => _163(target.providerId)]);
3481
+ const poolReady = poolAvailability ? poolAvailability.outcome === "usable" : this.options.providerKeys ? await this.options.providerKeys.hasUsableKeys(target.providerId) : false;
3482
+ if (!directKey && _optionalChain([poolAvailability, 'optionalAccess', _164 => _164.outcome]) === "exhausted") {
3483
+ throw new RouteLeaseError("upstream_exhausted", "provider key pool is temporarily exhausted", {
3484
+ retryAfterSeconds: poolAvailability.retryAfterSeconds
3485
+ });
3486
+ }
3487
+ if (!directKey && !poolReady) {
3488
+ throw new RouteLeaseError("upstream_unavailable", "provider has no usable credential");
3489
+ }
3490
+ }
3491
+ const sameFormat = request.runtime === "claude" && provider.apiFormat === "anthropic" || request.runtime === "codex" && provider.apiFormat === "openai-response";
3492
+ if (!sameFormat) {
3493
+ try {
3494
+ const chain = await _chunkQXD4MV2Xcjs.resolveProviderChain.call(void 0, this.llmConfig, provider.id, request.model);
3495
+ if (!chain.hasTransformers) {
3496
+ throw new RouteLeaseError("format_unsupported", "no transformer chain supports this runtime and upstream");
3497
+ }
3498
+ } catch (error) {
3499
+ if (error instanceof RouteLeaseError) throw error;
3500
+ throw new RouteLeaseError("format_unsupported", "the transformer chain could not be resolved", { cause: error });
3501
+ }
3502
+ }
3503
+ }
3504
+ async preflightSubscription(target, model) {
3505
+ const registry = _chunkQBZJ7P2Tcjs.getSubscriptionRegistryForOutbound.call(void 0, );
3506
+ const profile = _nullishCoalesce(_optionalChain([registry, 'optionalAccess', _165 => _165.getProfile, 'call', _166 => _166(target.providerId)]), () => ( null));
3507
+ if (!profile) throw new RouteLeaseError("upstream_not_found", "subscription provider was not found");
3508
+ await _optionalChain([this, 'access', _167 => _167.options, 'access', _168 => _168.subscriptions, 'optionalAccess', _169 => _169.assertAvailable, 'call', _170 => _170(target, model)]);
3509
+ const names = _nullishCoalesce(_nullishCoalesce(_optionalChain([profile, 'access', _171 => _171.resolveProviderTransformerNames, 'optionalCall', _172 => _172(model)]), () => ( profile.providerTransformerNames)), () => ( []));
3510
+ if (names.some((name) => !transformerRegistered(this.llmConfig, name))) {
3511
+ throw new RouteLeaseError("format_unsupported", "a required transformer is unavailable");
3512
+ }
3513
+ if (_optionalChain([profile, 'access', _173 => _173.modelTransformerNames, 'optionalAccess', _174 => _174.some, 'call', _175 => _175((name) => !transformerRegistered(this.llmConfig, name))])) {
3514
+ throw new RouteLeaseError("format_unsupported", "a required model transformer is unavailable");
3515
+ }
3516
+ }
3517
+ };
3518
+
2644
3519
  // src/provider-proxy/index.ts
2645
3520
  var instance = null;
2646
3521
  function getProviderProxy(deps) {
@@ -2662,9 +3537,9 @@ var _os = require('os');
2662
3537
  var _healthloggingtypes = require('@omnicross/contracts/health-logging-types');
2663
3538
 
2664
3539
  // src/outbound-api/keyedMutex.ts
2665
- var KeyedMutex = (_class3 = class {constructor() { _class3.prototype.__init4.call(this); }
3540
+ var KeyedMutex = (_class5 = class {constructor() { _class5.prototype.__init11.call(this); }
2666
3541
  /** key → the tail of the pending-op chain (resolves when the last op frees). */
2667
- __init4() {this.tails = /* @__PURE__ */ new Map()}
3542
+ __init11() {this.tails = /* @__PURE__ */ new Map()}
2668
3543
  /**
2669
3544
  * Run `fn` exclusively for `key`: it starts only after every previously
2670
3545
  * enqueued op for the SAME key has settled, and the next waiter starts only
@@ -2687,10 +3562,11 @@ var KeyedMutex = (_class3 = class {constructor() { _class3.prototype.__init4.cal
2687
3562
  if (this.tails.get(key) === chain) this.tails.delete(key);
2688
3563
  }
2689
3564
  }
2690
- }, _class3);
3565
+ }, _class5);
2691
3566
 
2692
3567
  // src/outbound-api/outboundApiRouter.ts
2693
3568
  var _stream = require('stream');
3569
+ var _subscriptionmodelcatalog = require('@omnicross/contracts/subscription-model-catalog');
2694
3570
 
2695
3571
  // src/outbound-api/apiServerConfig.ts
2696
3572
  var _audittypes = require('@omnicross/contracts/audit-types');
@@ -2715,11 +3591,11 @@ var DEFAULT_ACCOUNT_HEALTH = {
2715
3591
  overloadCooldownMs: 10 * 6e4
2716
3592
  };
2717
3593
  function normalizeAccountHealth(raw) {
2718
- const ah = _optionalChain([raw, 'optionalAccess', _82 => _82.accountHealth]);
3594
+ const ah = _optionalChain([raw, 'optionalAccess', _176 => _176.accountHealth]);
2719
3595
  return {
2720
- overloadCooldownEnabled: _optionalChain([ah, 'optionalAccess', _83 => _83.overloadCooldownEnabled]) !== false,
3596
+ overloadCooldownEnabled: _optionalChain([ah, 'optionalAccess', _177 => _177.overloadCooldownEnabled]) !== false,
2721
3597
  overloadCooldownMs: clampNumber(
2722
- _optionalChain([ah, 'optionalAccess', _84 => _84.overloadCooldownMs]),
3598
+ _optionalChain([ah, 'optionalAccess', _178 => _178.overloadCooldownMs]),
2723
3599
  6e4,
2724
3600
  36e5,
2725
3601
  DEFAULT_ACCOUNT_HEALTH.overloadCooldownMs
@@ -2741,25 +3617,25 @@ var DEFAULT_ALLOWANCE_SCHEDULING = {
2741
3617
  priorityPenalty: 100
2742
3618
  };
2743
3619
  function normalizeAllowanceScheduling(raw) {
2744
- const value = _optionalChain([raw, 'optionalAccess', _85 => _85.allowanceScheduling]);
3620
+ const value = _optionalChain([raw, 'optionalAccess', _179 => _179.allowanceScheduling]);
2745
3621
  const demoteAtPercent = clampNumber(
2746
- _optionalChain([value, 'optionalAccess', _86 => _86.demoteAtPercent]),
3622
+ _optionalChain([value, 'optionalAccess', _180 => _180.demoteAtPercent]),
2747
3623
  0,
2748
3624
  100,
2749
3625
  DEFAULT_ALLOWANCE_SCHEDULING.demoteAtPercent
2750
3626
  );
2751
3627
  const requestedPause = clampNumber(
2752
- _optionalChain([value, 'optionalAccess', _87 => _87.pauseAtPercent]),
3628
+ _optionalChain([value, 'optionalAccess', _181 => _181.pauseAtPercent]),
2753
3629
  0,
2754
3630
  100,
2755
3631
  DEFAULT_ALLOWANCE_SCHEDULING.pauseAtPercent
2756
3632
  );
2757
3633
  return {
2758
- enabled: _optionalChain([value, 'optionalAccess', _88 => _88.enabled]) === true,
3634
+ enabled: _optionalChain([value, 'optionalAccess', _182 => _182.enabled]) === true,
2759
3635
  demoteAtPercent,
2760
3636
  pauseAtPercent: Math.max(demoteAtPercent, requestedPause),
2761
3637
  priorityPenalty: Math.trunc(clampNumber(
2762
- _optionalChain([value, 'optionalAccess', _89 => _89.priorityPenalty]),
3638
+ _optionalChain([value, 'optionalAccess', _183 => _183.priorityPenalty]),
2763
3639
  1,
2764
3640
  1e3,
2765
3641
  DEFAULT_ALLOWANCE_SCHEDULING.priorityPenalty
@@ -2767,28 +3643,28 @@ function normalizeAllowanceScheduling(raw) {
2767
3643
  };
2768
3644
  }
2769
3645
  function normalizeAccountProbe(raw) {
2770
- const ap = _optionalChain([raw, 'optionalAccess', _90 => _90.accountProbe]);
3646
+ const ap = _optionalChain([raw, 'optionalAccess', _184 => _184.accountProbe]);
2771
3647
  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),
3648
+ enabled: _optionalChain([ap, 'optionalAccess', _185 => _185.enabled]) === true,
3649
+ intervalMs: clampNumber(_optionalChain([ap, 'optionalAccess', _186 => _186.intervalMs]), 6e4, 864e5, DEFAULT_ACCOUNT_PROBE.intervalMs),
3650
+ onlyMultiAccount: _optionalChain([ap, 'optionalAccess', _187 => _187.onlyMultiAccount]) !== false,
3651
+ timeoutMs: clampNumber(_optionalChain([ap, 'optionalAccess', _188 => _188.timeoutMs]), 1e3, 6e4, DEFAULT_ACCOUNT_PROBE.timeoutMs),
2776
3652
  historySize: Math.trunc(
2777
- clampNumber(_optionalChain([ap, 'optionalAccess', _95 => _95.historySize]), 1, 200, DEFAULT_ACCOUNT_PROBE.historySize)
3653
+ clampNumber(_optionalChain([ap, 'optionalAccess', _189 => _189.historySize]), 1, 200, DEFAULT_ACCOUNT_PROBE.historySize)
2778
3654
  ),
2779
- staggerMs: clampNumber(_optionalChain([ap, 'optionalAccess', _96 => _96.staggerMs]), 0, 6e4, DEFAULT_ACCOUNT_PROBE.staggerMs)
3655
+ staggerMs: clampNumber(_optionalChain([ap, 'optionalAccess', _190 => _190.staggerMs]), 0, 6e4, DEFAULT_ACCOUNT_PROBE.staggerMs)
2780
3656
  };
2781
3657
  }
2782
3658
  function normalizeAudit(raw) {
2783
- const a = _optionalChain([raw, 'optionalAccess', _97 => _97.audit]);
3659
+ const a = _optionalChain([raw, 'optionalAccess', _191 => _191.audit]);
2784
3660
  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])),
3661
+ enabled: _optionalChain([a, 'optionalAccess', _192 => _192.enabled]) === true,
3662
+ captureBodies: _optionalChain([a, 'optionalAccess', _193 => _193.captureBodies]) === true,
3663
+ maxBodyBytes: normalizeMaxBodyBytes(_optionalChain([a, 'optionalAccess', _194 => _194.maxBodyBytes])),
2788
3664
  retentionDays: Math.trunc(
2789
- clampNumber(_optionalChain([a, 'optionalAccess', _101 => _101.retentionDays]), 1, 365, _audittypes.DEFAULT_AUDIT_CONFIG.retentionDays)
3665
+ clampNumber(_optionalChain([a, 'optionalAccess', _195 => _195.retentionDays]), 1, 365, _audittypes.DEFAULT_AUDIT_CONFIG.retentionDays)
2790
3666
  ),
2791
- trustForwardedFor: _optionalChain([a, 'optionalAccess', _102 => _102.trustForwardedFor]) === true
3667
+ trustForwardedFor: _optionalChain([a, 'optionalAccess', _196 => _196.trustForwardedFor]) === true
2792
3668
  };
2793
3669
  }
2794
3670
  function normalizeMaxBodyBytes(value) {
@@ -2799,26 +3675,26 @@ function normalizeMaxBodyBytes(value) {
2799
3675
  return Math.trunc(Math.min(1048576, value));
2800
3676
  }
2801
3677
  function normalizeBilling(raw) {
2802
- const b = _optionalChain([raw, 'optionalAccess', _103 => _103.billing]);
3678
+ const b = _optionalChain([raw, 'optionalAccess', _197 => _197.billing]);
2803
3679
  const config = {
2804
- enabled: _optionalChain([b, 'optionalAccess', _104 => _104.enabled]) === true,
3680
+ enabled: _optionalChain([b, 'optionalAccess', _198 => _198.enabled]) === true,
2805
3681
  maxRetryAgeMs: Math.trunc(
2806
- clampNumber(_optionalChain([b, 'optionalAccess', _105 => _105.maxRetryAgeMs]), 6e4, 2592e6, _billingtypes.DEFAULT_BILLING_CONFIG.maxRetryAgeMs)
3682
+ clampNumber(_optionalChain([b, 'optionalAccess', _199 => _199.maxRetryAgeMs]), 6e4, 2592e6, _billingtypes.DEFAULT_BILLING_CONFIG.maxRetryAgeMs)
2807
3683
  )
2808
3684
  };
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;
3685
+ if (typeof _optionalChain([b, 'optionalAccess', _200 => _200.endpoint]) === "string" && b.endpoint.trim()) config.endpoint = b.endpoint.trim();
3686
+ if (typeof _optionalChain([b, 'optionalAccess', _201 => _201.secret]) === "string" && b.secret.length > 0) config.secret = b.secret;
2811
3687
  return config;
2812
3688
  }
2813
3689
  var DEFAULT_FINGERPRINT = { enabled: false };
2814
3690
  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();
3691
+ const f = _optionalChain([raw, 'optionalAccess', _202 => _202.fingerprint]);
3692
+ const out = { enabled: _optionalChain([f, 'optionalAccess', _203 => _203.enabled]) === true };
3693
+ if (typeof _optionalChain([f, 'optionalAccess', _204 => _204.ua]) === "string" && f.ua.trim().length > 0) out.ua = f.ua.trim();
2818
3694
  return out;
2819
3695
  }
2820
3696
  function normalizeVoucher(raw) {
2821
- return { enabled: _optionalChain([raw, 'optionalAccess', _111 => _111.voucher, 'optionalAccess', _112 => _112.enabled]) === true };
3697
+ return { enabled: _optionalChain([raw, 'optionalAccess', _205 => _205.voucher, 'optionalAccess', _206 => _206.enabled]) === true };
2822
3698
  }
2823
3699
  var PROXY_TYPES = ["http", "https", "socks5"];
2824
3700
  function normalizeProxyConfig(raw) {
@@ -2902,14 +3778,14 @@ function clampNumber(value, min, max, fallback) {
2902
3778
  return Math.min(max, Math.max(min, n));
2903
3779
  }
2904
3780
  function normalizeQueueSegments(raw) {
2905
- const umq = _optionalChain([raw, 'optionalAccess', _113 => _113.userMessageQueue]);
2906
- const cq = _optionalChain([raw, 'optionalAccess', _114 => _114.concurrencyQueue]);
3781
+ const umq = _optionalChain([raw, 'optionalAccess', _207 => _207.userMessageQueue]);
3782
+ const cq = _optionalChain([raw, 'optionalAccess', _208 => _208.concurrencyQueue]);
2907
3783
  return {
2908
3784
  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),
3785
+ enabled: _optionalChain([umq, 'optionalAccess', _209 => _209.enabled]) === true,
3786
+ delayMs: clampNumber(_optionalChain([umq, 'optionalAccess', _210 => _210.delayMs]), 0, 1e4, DEFAULT_USER_MESSAGE_QUEUE.delayMs),
2911
3787
  waitTimeoutMs: clampNumber(
2912
- _optionalChain([umq, 'optionalAccess', _117 => _117.waitTimeoutMs]),
3788
+ _optionalChain([umq, 'optionalAccess', _211 => _211.waitTimeoutMs]),
2913
3789
  1e3,
2914
3790
  3e5,
2915
3791
  DEFAULT_USER_MESSAGE_QUEUE.waitTimeoutMs
@@ -2917,14 +3793,14 @@ function normalizeQueueSegments(raw) {
2917
3793
  },
2918
3794
  concurrencyQueue: {
2919
3795
  maxQueueSizeFactor: clampNumber(
2920
- _optionalChain([cq, 'optionalAccess', _118 => _118.maxQueueSizeFactor]),
3796
+ _optionalChain([cq, 'optionalAccess', _212 => _212.maxQueueSizeFactor]),
2921
3797
  1,
2922
3798
  10,
2923
3799
  DEFAULT_CONCURRENCY_QUEUE.maxQueueSizeFactor
2924
3800
  ),
2925
- minQueueSize: clampNumber(_optionalChain([cq, 'optionalAccess', _119 => _119.minQueueSize]), 1, 100, DEFAULT_CONCURRENCY_QUEUE.minQueueSize),
3801
+ minQueueSize: clampNumber(_optionalChain([cq, 'optionalAccess', _213 => _213.minQueueSize]), 1, 100, DEFAULT_CONCURRENCY_QUEUE.minQueueSize),
2926
3802
  waitTimeoutMs: clampNumber(
2927
- _optionalChain([cq, 'optionalAccess', _120 => _120.waitTimeoutMs]),
3803
+ _optionalChain([cq, 'optionalAccess', _214 => _214.waitTimeoutMs]),
2928
3804
  1e3,
2929
3805
  3e5,
2930
3806
  DEFAULT_CONCURRENCY_QUEUE.waitTimeoutMs
@@ -3037,7 +3913,7 @@ function normalizeGatewayModelMappings(raw) {
3037
3913
  return mappings;
3038
3914
  }
3039
3915
  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()])));
3916
+ 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
3917
  }
3042
3918
  function splitModelRef(ref) {
3043
3919
  if (typeof ref !== "string") return null;
@@ -3105,7 +3981,7 @@ function legacyEndpointToBindings(config) {
3105
3981
  if (!parsed) continue;
3106
3982
  const route = routeFor(parsed.providerId);
3107
3983
  route[field] = ref;
3108
- if (_optionalChain([config, 'access', _132 => _132.backgroundModelIds, 'optionalAccess', _133 => _133.length])) {
3984
+ if (_optionalChain([config, 'access', _226 => _226.backgroundModelIds, 'optionalAccess', _227 => _227.length])) {
3109
3985
  route.backgroundModelIds = [...config.backgroundModelIds];
3110
3986
  }
3111
3987
  }
@@ -3255,167 +4131,6 @@ function mergeServerConfig(current, patch) {
3255
4131
  });
3256
4132
  }
3257
4133
 
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
4134
  // src/outbound-api/outboundApiKeyAuth.ts
3420
4135
 
3421
4136
 
@@ -3593,42 +4308,42 @@ async function verifyPresentedKey(db, presentedKey) {
3593
4308
  }
3594
4309
 
3595
4310
  // src/outbound-api/outboundConcurrencyGate.ts
3596
- var ConcurrencyQueueFullError = (_class4 = class extends Error {
4311
+ var ConcurrencyQueueFullError = (_class6 = class extends Error {
3597
4312
  constructor(apiKeyId, maxQueueSize) {
3598
- super(`concurrency queue full (max ${maxQueueSize}) for key '${apiKeyId}'`);_class4.prototype.__init5.call(this);;
4313
+ super(`concurrency queue full (max ${maxQueueSize}) for key '${apiKeyId}'`);_class6.prototype.__init12.call(this);;
3599
4314
  this.apiKeyId = apiKeyId;
3600
4315
  this.maxQueueSize = maxQueueSize;
3601
4316
  this.name = "ConcurrencyQueueFullError";
3602
4317
  }
3603
4318
 
3604
4319
 
3605
- __init5() {this.code = "concurrency_queue_full"}
3606
- }, _class4);
3607
- var ConcurrencyWaitTimeoutError = (_class5 = class extends Error {
4320
+ __init12() {this.code = "concurrency_queue_full"}
4321
+ }, _class6);
4322
+ var ConcurrencyWaitTimeoutError = (_class7 = class extends Error {
3608
4323
  constructor(apiKeyId, waitTimeoutMs) {
3609
- super(`concurrency wait timed out after ${waitTimeoutMs}ms for key '${apiKeyId}'`);_class5.prototype.__init6.call(this);;
4324
+ super(`concurrency wait timed out after ${waitTimeoutMs}ms for key '${apiKeyId}'`);_class7.prototype.__init13.call(this);;
3610
4325
  this.apiKeyId = apiKeyId;
3611
4326
  this.waitTimeoutMs = waitTimeoutMs;
3612
4327
  this.name = "ConcurrencyWaitTimeoutError";
3613
4328
  }
3614
4329
 
3615
4330
 
3616
- __init6() {this.code = "concurrency_wait_timeout"}
3617
- }, _class5);
3618
- var ConcurrencyWaitCancelledError = (_class6 = class extends Error {
4331
+ __init13() {this.code = "concurrency_wait_timeout"}
4332
+ }, _class7);
4333
+ var ConcurrencyWaitCancelledError = (_class8 = class extends Error {
3619
4334
  constructor(apiKeyId) {
3620
- super(`concurrency wait cancelled for key '${apiKeyId}'`);_class6.prototype.__init7.call(this);;
4335
+ super(`concurrency wait cancelled for key '${apiKeyId}'`);_class8.prototype.__init14.call(this);;
3621
4336
  this.apiKeyId = apiKeyId;
3622
4337
  this.name = "ConcurrencyWaitCancelledError";
3623
4338
  }
3624
4339
 
3625
- __init7() {this.code = "concurrency_wait_cancelled"}
3626
- }, _class6);
4340
+ __init14() {this.code = "concurrency_wait_cancelled"}
4341
+ }, _class8);
3627
4342
  function isConcurrencyRejection(err) {
3628
4343
  return err instanceof ConcurrencyQueueFullError || err instanceof ConcurrencyWaitTimeoutError || err instanceof ConcurrencyWaitCancelledError;
3629
4344
  }
3630
- var OutboundConcurrencyGate = (_class7 = class {constructor() { _class7.prototype.__init8.call(this); }
3631
- __init8() {this.states = /* @__PURE__ */ new Map()}
4345
+ var OutboundConcurrencyGate = (_class9 = class {constructor() { _class9.prototype.__init15.call(this); }
4346
+ __init15() {this.states = /* @__PURE__ */ new Map()}
3632
4347
  ensure(apiKeyId) {
3633
4348
  let state = this.states.get(apiKeyId);
3634
4349
  if (!state) {
@@ -3752,17 +4467,17 @@ var OutboundConcurrencyGate = (_class7 = class {constructor() { _class7.prototyp
3752
4467
  }
3753
4468
  this.states.clear();
3754
4469
  }
3755
- }, _class7);
4470
+ }, _class9);
3756
4471
 
3757
4472
  // src/outbound-api/outboundRateLimiter.ts
3758
4473
  var DEFAULT_WINDOW_MS = 6e4;
3759
4474
  var DEFAULT_MAX_REQUESTS = 60;
3760
- var OutboundRateLimiter = (_class8 = class {
4475
+ var OutboundRateLimiter = (_class10 = class {
3761
4476
 
3762
4477
 
3763
4478
  /** apiKeyId → ascending request timestamps within the current window. */
3764
- __init9() {this.hits = /* @__PURE__ */ new Map()}
3765
- constructor(options = {}) {;_class8.prototype.__init9.call(this);
4479
+ __init16() {this.hits = /* @__PURE__ */ new Map()}
4480
+ constructor(options = {}) {;_class10.prototype.__init16.call(this);
3766
4481
  this.windowMs = _nullishCoalesce(options.windowMs, () => ( DEFAULT_WINDOW_MS));
3767
4482
  this.maxRequests = _nullishCoalesce(options.maxRequests, () => ( DEFAULT_MAX_REQUESTS));
3768
4483
  }
@@ -3778,8 +4493,8 @@ var OutboundRateLimiter = (_class8 = class {
3778
4493
  * recorded (no bucket growth).
3779
4494
  */
3780
4495
  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));
4496
+ const maxRequests = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _228 => _228.maxRequests]), () => ( this.maxRequests));
4497
+ const windowMs = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _229 => _229.windowMs]), () => ( this.windowMs));
3783
4498
  if (maxRequests === 0) return { allowed: true, retryAfterSeconds: 0 };
3784
4499
  const cutoff = now - windowMs;
3785
4500
  const timestamps = (_nullishCoalesce(this.hits.get(apiKeyId), () => ( []))).filter((ts) => ts > cutoff);
@@ -3804,7 +4519,7 @@ var OutboundRateLimiter = (_class8 = class {
3804
4519
  reset() {
3805
4520
  this.hits.clear();
3806
4521
  }
3807
- }, _class8);
4522
+ }, _class10);
3808
4523
 
3809
4524
  // src/outbound-api/userMessageDetection.ts
3810
4525
  function asArray(v) {
@@ -3824,7 +4539,7 @@ function isUserMessageAnthropic(body) {
3824
4539
  if (typeof content === "string") return true;
3825
4540
  if (Array.isArray(content)) {
3826
4541
  const hasToolResult = content.some(
3827
- (block) => _optionalChain([asObject, 'call', _150 => _150(block), 'optionalAccess', _151 => _151["type"]]) === "tool_result"
4542
+ (block) => _optionalChain([asObject, 'call', _230 => _230(block), 'optionalAccess', _231 => _231["type"]]) === "tool_result"
3828
4543
  );
3829
4544
  return !hasToolResult;
3830
4545
  }
@@ -3853,7 +4568,7 @@ function isUserMessageGemini(body) {
3853
4568
  if (!lastContent || lastContent["role"] !== "user") return false;
3854
4569
  const parts = asArray(lastContent["parts"]);
3855
4570
  const hasFunctionResponse = parts.some(
3856
- (part) => _optionalChain([asObject, 'call', _152 => _152(part), 'optionalAccess', _153 => _153["functionResponse"]]) !== void 0
4571
+ (part) => _optionalChain([asObject, 'call', _232 => _232(part), 'optionalAccess', _233 => _233["functionResponse"]]) !== void 0
3857
4572
  );
3858
4573
  return !hasFunctionResponse;
3859
4574
  }
@@ -3875,22 +4590,22 @@ function isUserMessageRequest(endpoint, parsedBody) {
3875
4590
  }
3876
4591
 
3877
4592
  // src/outbound-api/userMessageSerialQueue.ts
3878
- var SerialQueueTimeoutError = (_class9 = class extends Error {
4593
+ var SerialQueueTimeoutError = (_class11 = class extends Error {
3879
4594
  constructor(providerId, waitTimeoutMs) {
3880
- super(`user-message serial queue wait timed out after ${waitTimeoutMs}ms for '${providerId}'`);_class9.prototype.__init10.call(this);;
4595
+ super(`user-message serial queue wait timed out after ${waitTimeoutMs}ms for '${providerId}'`);_class11.prototype.__init17.call(this);;
3881
4596
  this.providerId = providerId;
3882
4597
  this.waitTimeoutMs = waitTimeoutMs;
3883
4598
  this.name = "SerialQueueTimeoutError";
3884
4599
  }
3885
4600
 
3886
4601
 
3887
- __init10() {this.code = "serial_queue_timeout"}
3888
- }, _class9);
4602
+ __init17() {this.code = "serial_queue_timeout"}
4603
+ }, _class11);
3889
4604
  function isSerialQueueTimeout(err) {
3890
4605
  return err instanceof SerialQueueTimeoutError;
3891
4606
  }
3892
- var UserMessageSerialQueue = (_class10 = class {constructor() { _class10.prototype.__init11.call(this); }
3893
- __init11() {this.states = /* @__PURE__ */ new Map()}
4607
+ var UserMessageSerialQueue = (_class12 = class {constructor() { _class12.prototype.__init18.call(this); }
4608
+ __init18() {this.states = /* @__PURE__ */ new Map()}
3894
4609
  ensure(providerId) {
3895
4610
  let state = this.states.get(providerId);
3896
4611
  if (!state) {
@@ -4005,7 +4720,7 @@ var UserMessageSerialQueue = (_class10 = class {constructor() { _class10.prototy
4005
4720
  }
4006
4721
  this.states.clear();
4007
4722
  }
4008
- }, _class10);
4723
+ }, _class12);
4009
4724
 
4010
4725
  // src/outbound-api/voucher.ts
4011
4726
 
@@ -4027,14 +4742,14 @@ function newVoucherId() {
4027
4742
  }
4028
4743
  function computeVoucherGrant(voucher, currentKey, now) {
4029
4744
  if (voucher.type === "credit") {
4030
- const current = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _154 => _154.totalCostLimitUsd]), () => ( 0));
4745
+ const current = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _234 => _234.totalCostLimitUsd]), () => ( 0));
4031
4746
  let granted2 = current + (_nullishCoalesce(voucher.creditUsd, () => ( 0)));
4032
4747
  if (voucher.maxTotalCostLimitUsd != null) {
4033
4748
  granted2 = Math.min(granted2, voucher.maxTotalCostLimitUsd);
4034
4749
  }
4035
4750
  return { totalCostLimitUsd: granted2 };
4036
4751
  }
4037
- const currentExpiry = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _155 => _155.expiresAt]), () => ( now));
4752
+ const currentExpiry = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _235 => _235.expiresAt]), () => ( now));
4038
4753
  let granted = currentExpiry + (_nullishCoalesce(voucher.renewalDays, () => ( 0))) * DAY_MS2;
4039
4754
  if (voucher.maxExpiryDays != null) {
4040
4755
  granted = Math.min(granted, now + voucher.maxExpiryDays * DAY_MS2);
@@ -4065,7 +4780,7 @@ function toVoucherInfo(v) {
4065
4780
  // src/outbound-api/voucherRedeem.ts
4066
4781
  function isRedeemRequest(method, url) {
4067
4782
  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(/\/+$/, "")]), () => ( ""));
4783
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _236 => _236.split, 'call', _237 => _237("?"), 'access', _238 => _238[0], 'optionalAccess', _239 => _239.replace, 'call', _240 => _240(/\/+$/, "")]), () => ( ""));
4069
4784
  return path.endsWith("/redeem");
4070
4785
  }
4071
4786
  function writeErr(res, status, message, headers = {}) {
@@ -4095,8 +4810,8 @@ function grantToResult(type, granted) {
4095
4810
  function currentResult(type, keyRow) {
4096
4811
  const result = { type };
4097
4812
  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) {
4813
+ if (_optionalChain([keyRow, 'optionalAccess', _241 => _241.totalCostLimitUsd]) != null) result.totalCostLimitUsd = keyRow.totalCostLimitUsd;
4814
+ } else if (_optionalChain([keyRow, 'optionalAccess', _242 => _242.expiresAt]) != null) {
4100
4815
  result.expiresAt = keyRow.expiresAt;
4101
4816
  }
4102
4817
  return result;
@@ -4134,7 +4849,7 @@ async function handleVoucherRedeem(req, res, deps, voucherEnabled, redeemLimiter
4134
4849
  let code;
4135
4850
  try {
4136
4851
  code = raw ? JSON.parse(raw)["code"] : void 0;
4137
- } catch (e24) {
4852
+ } catch (e27) {
4138
4853
  writeErr(res, 400, "Invalid JSON in request body");
4139
4854
  return;
4140
4855
  }
@@ -4183,7 +4898,7 @@ async function handleVoucherRedeem(req, res, deps, voucherEnabled, redeemLimiter
4183
4898
  const won = await voucherDb.voucherRedeemCas(voucher.id, verifiedKeyId, absolute, now);
4184
4899
  if (!won) {
4185
4900
  const after = await voucherDb.voucherGetByHash(codeHash);
4186
- if (_optionalChain([after, 'optionalAccess', _163 => _163.status]) === "redeemed" && after.redeemedByKeyId === verifiedKeyId) {
4901
+ if (_optionalChain([after, 'optionalAccess', _243 => _243.status]) === "redeemed" && after.redeemedByKeyId === verifiedKeyId) {
4187
4902
  return after.grantApplied === true ? { ok: true, result: currentResult(after.type, await deps.db.outboundApiKeysGetByHash(keyHash)) } : await applyFirstPass(after, recordedAbsolute(after));
4188
4903
  }
4189
4904
  return { ok: false, status: 409, message: "Code was already redeemed" };
@@ -4244,10 +4959,10 @@ function extractPresentedKey(req) {
4244
4959
  }
4245
4960
  const xApiKey = req.headers["x-api-key"];
4246
4961
  const xApi = Array.isArray(xApiKey) ? xApiKey[0] : xApiKey;
4247
- if (_optionalChain([xApi, 'optionalAccess', _164 => _164.trim, 'call', _165 => _165()])) return xApi.trim();
4962
+ if (_optionalChain([xApi, 'optionalAccess', _244 => _244.trim, 'call', _245 => _245()])) return xApi.trim();
4248
4963
  const goog = req.headers["x-goog-api-key"];
4249
4964
  const g = Array.isArray(goog) ? goog[0] : goog;
4250
- return _optionalChain([g, 'optionalAccess', _166 => _166.trim, 'call', _167 => _167()]) || void 0;
4965
+ return _optionalChain([g, 'optionalAccess', _246 => _246.trim, 'call', _247 => _247()]) || void 0;
4251
4966
  }
4252
4967
  function isLoopbackPeer(address) {
4253
4968
  if (!address) return false;
@@ -4256,7 +4971,7 @@ function isLoopbackPeer(address) {
4256
4971
  }
4257
4972
  function selectEndpoint(method, url) {
4258
4973
  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(/\/+$/, "")]), () => ( ""));
4974
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _248 => _248.split, 'call', _249 => _249("?"), 'access', _250 => _250[0], 'optionalAccess', _251 => _251.replace, 'call', _252 => _252(/\/+$/, "")]), () => ( ""));
4260
4975
  if (path.endsWith("/chat/completions")) return "chat";
4261
4976
  if (path.endsWith("/responses")) return "responses";
4262
4977
  if (path.includes("/v1/messages")) return "messages";
@@ -4274,36 +4989,54 @@ function extractGeminiModelFromUrl(url) {
4274
4989
  }
4275
4990
  function isModelsListRequest(url) {
4276
4991
  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(/\/+$/, "")]), () => ( ""));
4992
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _253 => _253.split, 'call', _254 => _254("?"), 'access', _255 => _255[0], 'optionalAccess', _256 => _256.replace, 'call', _257 => _257(/\/+$/, "")]), () => ( ""));
4278
4993
  return path.endsWith("/models");
4279
4994
  }
4280
- function writeModelsList(res, config, apiKeyId, allowedEndpoints) {
4281
- const refs = [];
4282
- const endpointConfigs = [];
4995
+ async function writeModelsList(res, llmConfig, config, apiKeyId, allowedEndpoints) {
4996
+ const modelIds = [];
4283
4997
  for (const endpoint of ["chat", "responses", "messages", "gemini"]) {
4284
4998
  if (allowedEndpoints && !allowedEndpoints.includes(endpoint)) continue;
4285
- endpointConfigs.push(
4286
- ...candidateGatewayBindings(config.bindings, apiKeyId, endpoint).map(
4287
- (binding) => gatewayBindingToEndpointConfig(binding)
4288
- )
4289
- );
4290
- }
4291
- for (const endpoint of endpointConfigs) {
4292
- if (allowedEndpoints && !allowedEndpoints.includes(endpoint.endpoint)) continue;
4293
- if (endpoint.endpoint === "chat") refs.push(..._nullishCoalesce(endpoint.models, () => ( [])));
4294
- else if (endpoint.endpoint === "messages" || endpoint.endpoint === "responses") {
4295
- refs.push(...Object.values(_nullishCoalesce(endpoint.modelMap, () => ( {}))));
4296
- } else {
4297
- if (endpoint.defaultModel) refs.push(endpoint.defaultModel);
4298
- if (endpoint.backgroundModel) refs.push(endpoint.backgroundModel);
4999
+ for (const binding of candidateGatewayBindings(config.bindings, apiKeyId, endpoint)) {
5000
+ if (binding.modelMode === "passthrough") {
5001
+ if (binding.target.kind === "provider") {
5002
+ const provider = await llmConfig.getProvider(binding.target.providerId);
5003
+ modelIds.push(..._nullishCoalesce(_optionalChain([provider, 'optionalAccess', _258 => _258.models]), () => ( [])));
5004
+ } else if (Object.prototype.hasOwnProperty.call(
5005
+ _subscriptionmodelcatalog.SUBSCRIPTION_MODEL_CATALOG,
5006
+ binding.target.providerId
5007
+ )) {
5008
+ modelIds.push(
5009
+ ..._subscriptionmodelcatalog.SUBSCRIPTION_MODEL_CATALOG[binding.target.providerId]
5010
+ );
5011
+ }
5012
+ continue;
5013
+ }
5014
+ if (_optionalChain([binding, 'access', _259 => _259.modelMappings, 'optionalAccess', _260 => _260.length])) {
5015
+ modelIds.push(
5016
+ ...binding.modelMappings.map((mapping) => mapping.source.trim()).filter((source) => source !== "" && !source.includes("*"))
5017
+ );
5018
+ continue;
5019
+ }
5020
+ const endpointConfig = gatewayBindingToEndpointConfig(binding);
5021
+ const refs = [];
5022
+ if (endpointConfig.endpoint === "chat") refs.push(..._nullishCoalesce(endpointConfig.models, () => ( [])));
5023
+ else if (endpointConfig.endpoint === "messages" || endpointConfig.endpoint === "responses") {
5024
+ refs.push(...Object.values(_nullishCoalesce(endpointConfig.modelMap, () => ( {}))));
5025
+ } else {
5026
+ if (endpointConfig.defaultModel) refs.push(endpointConfig.defaultModel);
5027
+ if (endpointConfig.backgroundModel) refs.push(endpointConfig.backgroundModel);
5028
+ }
5029
+ modelIds.push(
5030
+ ...refs.map((ref) => _optionalChain([_chunk4VZX5F4Tcjs.parseModelRef.call(void 0, ref), 'optionalAccess', _261 => _261.modelId])).filter((modelId) => modelId !== void 0)
5031
+ );
4299
5032
  }
4300
5033
  }
4301
5034
  const seen = /* @__PURE__ */ new Set();
4302
- const data = refs.map((ref) => _chunk4VZX5F4Tcjs.parseModelRef.call(void 0, ref)).filter((p) => p !== null).filter((p) => {
4303
- if (seen.has(p.modelId)) return false;
4304
- seen.add(p.modelId);
5035
+ const data = modelIds.filter((modelId) => {
5036
+ if (seen.has(modelId)) return false;
5037
+ seen.add(modelId);
4305
5038
  return true;
4306
- }).map((p) => ({ id: p.modelId, object: "model", owned_by: "omnicross" }));
5039
+ }).map((modelId) => ({ id: modelId, object: "model", owned_by: "omnicross" }));
4307
5040
  res.writeHead(200, { "Content-Type": "application/json" });
4308
5041
  res.end(JSON.stringify({ object: "list", data }));
4309
5042
  }
@@ -4337,7 +5070,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4337
5070
  const verified = verification.key;
4338
5071
  if (audit) audit.keyId = verified.id;
4339
5072
  if (billing) billing.keyId = verified.id;
4340
- if (verified.loopbackOnly && !isLoopbackPeer(_optionalChain([req, 'access', _178 => _178.socket, 'optionalAccess', _179 => _179.remoteAddress]))) {
5073
+ if (verified.loopbackOnly && !isLoopbackPeer(_optionalChain([req, 'access', _262 => _262.socket, 'optionalAccess', _263 => _263.remoteAddress]))) {
4341
5074
  writeJsonError(res, 403, "This integration key is restricted to loopback clients");
4342
5075
  return;
4343
5076
  }
@@ -4350,7 +5083,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4350
5083
  req,
4351
5084
  res,
4352
5085
  deps,
4353
- _optionalChain([config, 'access', _180 => _180.voucher, 'optionalAccess', _181 => _181.enabled]) === true,
5086
+ _optionalChain([config, 'access', _264 => _264.voucher, 'optionalAccess', _265 => _265.enabled]) === true,
4354
5087
  _nullishCoalesce(redeemLimiter, () => ( getFallbackRedeemLimiter())),
4355
5088
  verified.id,
4356
5089
  _nullishCoalesce(presented, () => ( "")),
@@ -4371,7 +5104,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4371
5104
  writeJsonError(res, 403, "API key is not allowed to access this endpoint");
4372
5105
  return;
4373
5106
  }
4374
- writeModelsList(res, config, verified.id, verified.allowedEndpoints);
5107
+ await writeModelsList(res, deps.llmConfig, config, verified.id, verified.allowedEndpoints);
4375
5108
  return;
4376
5109
  }
4377
5110
  const endpoint = selectEndpoint(req.method, req.url);
@@ -4448,7 +5181,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4448
5181
  let parsedBody = {};
4449
5182
  try {
4450
5183
  parsedBody = rawBody ? JSON.parse(rawBody) : {};
4451
- } catch (e25) {
5184
+ } catch (e28) {
4452
5185
  writeJsonError(res, 400, "Invalid JSON in request body");
4453
5186
  return;
4454
5187
  }
@@ -4519,7 +5252,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4519
5252
  }
4520
5253
  let releaseSerial = null;
4521
5254
  const providerKey = resolved.route.providerId;
4522
- if (_optionalChain([config, 'access', _182 => _182.userMessageQueue, 'optionalAccess', _183 => _183.enabled]) && providerKey && isUserMessageRequest(endpoint, parsedBody)) {
5255
+ if (_optionalChain([config, 'access', _266 => _266.userMessageQueue, 'optionalAccess', _267 => _267.enabled]) && providerKey && isUserMessageRequest(endpoint, parsedBody)) {
4523
5256
  const umq = config.userMessageQueue;
4524
5257
  try {
4525
5258
  const serialSlot = await serialQueue.acquire(providerKey, {
@@ -4597,37 +5330,37 @@ function makeReplayRequest(req, rawBody) {
4597
5330
  var DEFAULT_OUTBOUND_PORT = 8765;
4598
5331
  var LOOPBACK_ADDR = "127.0.0.1";
4599
5332
  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);
5333
+ var OutboundApiServer = (_class13 = class {
5334
+ 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
5335
  this.deps = deps;
4603
5336
  this.onPortChange = onPortChange;
4604
5337
  }
4605
5338
 
4606
5339
 
4607
- __init12() {this.server = null}
4608
- __init13() {this.boundPort = 0}
4609
- __init14() {this.boundAddr = LOOPBACK_ADDR}
4610
- __init15() {this.endpoints = []}
4611
- __init16() {this.bindings = []}
5340
+ __init19() {this.server = null}
5341
+ __init20() {this.boundPort = 0}
5342
+ __init21() {this.boundAddr = LOOPBACK_ADDR}
5343
+ __init22() {this.endpoints = []}
5344
+ __init23() {this.bindings = []}
4612
5345
 
4613
5346
 
4614
5347
 
4615
- __init17() {this.rateLimiter = new OutboundRateLimiter()}
5348
+ __init24() {this.rateLimiter = new OutboundRateLimiter()}
4616
5349
  /**
4617
5350
  * Redeem-attempt limiter (voucher-redemption #9, design D6) — a SEPARATE bucket
4618
5351
  * from the traffic `rateLimiter`, keyed by the authenticating key id, so
4619
5352
  * brute-forcing `CC_` codes is throttled (a handful/min) without touching the
4620
5353
  * per-key request rate. Conservative fixed defaults (10 / 60s).
4621
5354
  */
4622
- __init18() {this.redeemLimiter = new OutboundRateLimiter({ maxRequests: 10, windowMs: 6e4 })}
5355
+ __init25() {this.redeemLimiter = new OutboundRateLimiter({ maxRequests: 10, windowMs: 6e4 })}
4623
5356
  /**
4624
5357
  * Per-key redeem mutex (voucher-redemption #9, MJ1 fix). One instance for the
4625
5358
  * server's lifetime so concurrent redeem REQUESTS for the same key serialize
4626
5359
  * (relative grant increments accumulate instead of clobbering a snapshot).
4627
5360
  */
4628
- __init19() {this.redeemMutex = new KeyedMutex()}
4629
- __init20() {this.serialQueue = new UserMessageSerialQueue()}
4630
- __init21() {this.concurrencyGate = new OutboundConcurrencyGate()}
5361
+ __init26() {this.redeemMutex = new KeyedMutex()}
5362
+ __init27() {this.serialQueue = new UserMessageSerialQueue()}
5363
+ __init28() {this.concurrencyGate = new OutboundConcurrencyGate()}
4631
5364
  /**
4632
5365
  * Apply a config. Restarts the listener ONLY when the bind address or port
4633
5366
  * changes (or when toggling enabled); per-endpoint routing config is updated
@@ -4659,7 +5392,7 @@ var OutboundApiServer = (_class11 = class {
4659
5392
  const actualPort = await this.listen(bindAddr, port);
4660
5393
  this.boundAddr = bindAddr;
4661
5394
  this.boundPort = actualPort;
4662
- if (actualPort !== port) _optionalChain([this, 'access', _184 => _184.onPortChange, 'optionalCall', _185 => _185(actualPort)]);
5395
+ if (actualPort !== port) _optionalChain([this, 'access', _268 => _268.onPortChange, 'optionalCall', _269 => _269(actualPort)]);
4663
5396
  this.logInfo(`[OutboundApiServer] Listening on ${bindAddr}:${actualPort}`);
4664
5397
  return actualPort;
4665
5398
  }
@@ -4728,7 +5461,7 @@ var OutboundApiServer = (_class11 = class {
4728
5461
  const provider = this.deps.healthReportProvider;
4729
5462
  if (!provider) return false;
4730
5463
  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(/\/+$/, "")]) || "/";
5464
+ const path = _optionalChain([(_nullishCoalesce(req.url, () => ( "/"))), 'access', _270 => _270.split, 'call', _271 => _271("?"), 'access', _272 => _272[0], 'optionalAccess', _273 => _273.replace, 'call', _274 => _274(/\/+$/, "")]) || "/";
4732
5465
  if (path !== "/health" && path !== "/healthz") return false;
4733
5466
  const report = provider();
4734
5467
  res.writeHead(_healthloggingtypes.healthHttpStatus.call(void 0, report.status), { "Content-Type": "application/json" });
@@ -4798,7 +5531,7 @@ var OutboundApiServer = (_class11 = class {
4798
5531
  concurrency: this.concurrencyGate.getStatus()
4799
5532
  };
4800
5533
  }
4801
- }, _class11);
5534
+ }, _class13);
4802
5535
  function formatUrls(base) {
4803
5536
  return {
4804
5537
  chat: `${base}/v1/chat/completions`,
@@ -4839,13 +5572,13 @@ function startOfLocalWeek(ts) {
4839
5572
  day.setDate(day.getDate() - day.getDay());
4840
5573
  return day.getTime();
4841
5574
  }
4842
- var KeySpendTracker = (_class12 = class {
4843
- constructor(seeder) {;_class12.prototype.__init22.call(this);
5575
+ var KeySpendTracker = (_class14 = class {
5576
+ constructor(seeder) {;_class14.prototype.__init29.call(this);
4844
5577
  this.seeder = seeder;
4845
5578
  }
4846
5579
 
4847
5580
  /** apiKeyId → live accumulated spend (seeded lazily). */
4848
- __init22() {this.spend = /* @__PURE__ */ new Map()}
5581
+ __init29() {this.spend = /* @__PURE__ */ new Map()}
4849
5582
  /**
4850
5583
  * Read the key's accumulated spend, seeding from the durable store on a cache
4851
5584
  * miss and rolling the daily/weekly windows on a hit.
@@ -4898,7 +5631,7 @@ var KeySpendTracker = (_class12 = class {
4898
5631
  });
4899
5632
  return { dailyUsd, dailyWindowStart, weeklyUsd, weeklyWindowStart, totalUsd };
4900
5633
  }
4901
- }, _class12);
5634
+ }, _class14);
4902
5635
  function rollWindows(s, now) {
4903
5636
  const day = startOfLocalDay(now);
4904
5637
  if (s.dailyWindowStart !== day) {
@@ -4927,6 +5660,70 @@ function __resetOutboundApiServerForTests() {
4927
5660
  instance2 = null;
4928
5661
  }
4929
5662
 
5663
+ // src/completion/ReasoningRequestBuilder.ts
5664
+ var LEGACY_ANTHROPIC_MAX_TOKENS = 16384;
5665
+ function intentFromOptions(options) {
5666
+ return options.thinkLevel ? { effort: options.thinkLevel, enabled: options.thinkLevel !== "none" } : void 0;
5667
+ }
5668
+ function resolveCompletionReasoningPlan(provider, options, target, requestMaxTokens = options.maxTokens) {
5669
+ return _chunkAOFWNWLTcjs.resolveReasoningPlan.call(void 0, {
5670
+ intent: intentFromOptions(options),
5671
+ model: options.model,
5672
+ provider,
5673
+ target,
5674
+ requestMaxTokens
5675
+ });
5676
+ }
5677
+ function resolveOpenAIEffort(provider, options, target) {
5678
+ const plan = resolveCompletionReasoningPlan(provider, options, target);
5679
+ return _optionalChain([plan, 'optionalAccess', _275 => _275.kind]) === "level" && plan.enabled ? plan.effort : void 0;
5680
+ }
5681
+ function buildAnthropicReasoningWire(provider, options) {
5682
+ const configuredMaxTokens = _nullishCoalesce(options.maxTokens, () => ( LEGACY_ANTHROPIC_MAX_TOKENS));
5683
+ const legacyBound = Math.min(configuredMaxTokens, LEGACY_ANTHROPIC_MAX_TOKENS);
5684
+ const plan = resolveCompletionReasoningPlan(
5685
+ provider,
5686
+ options,
5687
+ "anthropic",
5688
+ legacyBound
5689
+ );
5690
+ if (_optionalChain([plan, 'optionalAccess', _276 => _276.kind]) === "level" && plan.enabled) {
5691
+ return {
5692
+ effectiveMaxTokens: configuredMaxTokens,
5693
+ thinking: { type: "adaptive" },
5694
+ outputConfig: { effort: plan.effort },
5695
+ legacyBudgetEnabled: false
5696
+ };
5697
+ }
5698
+ if (_optionalChain([plan, 'optionalAccess', _277 => _277.kind]) === "budget" && plan.enabled) {
5699
+ return {
5700
+ effectiveMaxTokens: legacyBound,
5701
+ thinking: { type: "enabled", budget_tokens: plan.budgetTokens },
5702
+ legacyBudgetEnabled: true
5703
+ };
5704
+ }
5705
+ return {
5706
+ effectiveMaxTokens: configuredMaxTokens,
5707
+ legacyBudgetEnabled: false
5708
+ };
5709
+ }
5710
+ function buildGeminiThinkingConfig(provider, options) {
5711
+ const plan = resolveCompletionReasoningPlan(provider, options, "gemini");
5712
+ if (_optionalChain([plan, 'optionalAccess', _278 => _278.kind]) === "level") {
5713
+ return {
5714
+ includeThoughts: plan.enabled,
5715
+ thinkingLevel: plan.effort
5716
+ };
5717
+ }
5718
+ if (_optionalChain([plan, 'optionalAccess', _279 => _279.kind]) === "budget") {
5719
+ return {
5720
+ includeThoughts: plan.enabled,
5721
+ thinkingBudget: plan.budgetTokens
5722
+ };
5723
+ }
5724
+ return void 0;
5725
+ }
5726
+
4930
5727
  // src/completion/DirectApiHandler.ts
4931
5728
  async function callOpenAICompletion(provider, apiKey, options, logger) {
4932
5729
  const request = {
@@ -4938,11 +5735,9 @@ async function callOpenAICompletion(provider, apiKey, options, logger) {
4938
5735
  stream: false
4939
5736
  // For now, non-streaming only
4940
5737
  };
4941
- if (options.thinkLevel && options.thinkLevel !== "none") {
4942
- const effort = _thinkingconfig.getOpenAIReasoningEffort.call(void 0, options.thinkLevel);
4943
- if (effort) {
4944
- request.reasoning_effort = effort;
4945
- }
5738
+ const effort = resolveOpenAIEffort(provider, options, "openai-chat");
5739
+ if (effort) {
5740
+ request.reasoning_effort = effort;
4946
5741
  }
4947
5742
  const apiUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: options.model, stream: false });
4948
5743
  const headers = _chunkQ27JY5RGcjs.getProviderHeaders.call(void 0, provider, apiKey);
@@ -4979,16 +5774,19 @@ async function callOpenAICompletion(provider, apiKey, options, logger) {
4979
5774
  }
4980
5775
  async function callAnthropicCompletion(provider, apiKey, options, logger) {
4981
5776
  const hasImages = options.messages.some((m) => m.images && m.images.length > 0);
5777
+ const reasoningWire = buildAnthropicReasoningWire(provider, options);
4982
5778
  if (hasImages) {
4983
5779
  const systemMessages = options.messages.filter((m) => m.role === "system");
4984
5780
  const nonSystemMessages = options.messages.filter((m) => m.role !== "system");
4985
5781
  const anthropicRequest2 = {
4986
5782
  model: options.model,
4987
- max_tokens: _nullishCoalesce(options.maxTokens, () => ( 16384)),
4988
- temperature: options.temperature,
5783
+ max_tokens: reasoningWire.effectiveMaxTokens,
5784
+ ...reasoningWire.legacyBudgetEnabled ? {} : { temperature: options.temperature },
4989
5785
  ...systemMessages.length > 0 ? { system: systemMessages.map((m) => m.content).join("\n\n") } : {},
4990
5786
  messages: nonSystemMessages.map((m) => convertMessageToAnthropic(m)),
4991
- stream: false
5787
+ stream: false,
5788
+ ...reasoningWire.thinking ? { thinking: reasoningWire.thinking } : {},
5789
+ ...reasoningWire.outputConfig ? { output_config: reasoningWire.outputConfig } : {}
4992
5790
  };
4993
5791
  const apiUrl2 = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: options.model, stream: false });
4994
5792
  const headers2 = _chunkQ27JY5RGcjs.getProviderHeaders.call(void 0, provider, apiKey);
@@ -5013,7 +5811,7 @@ async function callAnthropicCompletion(provider, apiKey, options, logger) {
5013
5811
  message: {
5014
5812
  id: `assistant-${Date.now()}`,
5015
5813
  role: "assistant",
5016
- content: _optionalChain([choice2, 'access', _191 => _191.message, 'optionalAccess', _192 => _192.content]) || "",
5814
+ content: _optionalChain([choice2, 'access', _280 => _280.message, 'optionalAccess', _281 => _281.content]) || "",
5017
5815
  timestamp: Date.now()
5018
5816
  },
5019
5817
  usage: openaiResponse2.usage ? {
@@ -5030,14 +5828,24 @@ async function callAnthropicCompletion(provider, apiKey, options, logger) {
5030
5828
  content: m.content
5031
5829
  })),
5032
5830
  // Anthropic requires max_tokens; use 16384 default if not explicitly set
5033
- max_tokens: _nullishCoalesce(options.maxTokens, () => ( 16384)),
5034
- temperature: options.temperature,
5831
+ max_tokens: reasoningWire.effectiveMaxTokens,
5832
+ temperature: reasoningWire.legacyBudgetEnabled ? void 0 : options.temperature,
5035
5833
  stream: false
5036
5834
  };
5037
5835
  const config = {
5038
5836
  defaultModel: options.model
5039
5837
  };
5040
5838
  const anthropicRequest = _chunkGDIXXKF2cjs.convertOpenAIToAnthropic.call(void 0, openaiRequest, config);
5839
+ const anthropicRequestBody = anthropicRequest;
5840
+ if (reasoningWire.thinking) {
5841
+ anthropicRequestBody.thinking = reasoningWire.thinking;
5842
+ }
5843
+ if (reasoningWire.outputConfig) {
5844
+ anthropicRequestBody.output_config = reasoningWire.outputConfig;
5845
+ }
5846
+ if (reasoningWire.legacyBudgetEnabled) {
5847
+ delete anthropicRequestBody.temperature;
5848
+ }
5041
5849
  const apiUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: options.model, stream: false });
5042
5850
  const headers = _chunkQ27JY5RGcjs.getProviderHeaders.call(void 0, provider, apiKey);
5043
5851
  logger.info("Calling Anthropic completion API", { url: apiUrl, model: options.model });
@@ -5082,11 +5890,13 @@ async function callGeminiCompletion(provider, apiKey, options, logger) {
5082
5890
  contents.push(convertMessageToGemini(msg));
5083
5891
  }
5084
5892
  }
5893
+ const thinkingConfig = buildGeminiThinkingConfig(provider, options);
5085
5894
  const request = {
5086
5895
  contents,
5087
5896
  generationConfig: {
5088
5897
  ...options.maxTokens !== void 0 ? { maxOutputTokens: options.maxTokens } : {},
5089
- ...options.temperature !== void 0 ? { temperature: options.temperature } : {}
5898
+ ...options.temperature !== void 0 ? { temperature: options.temperature } : {},
5899
+ ...thinkingConfig ? { thinkingConfig } : {}
5090
5900
  }
5091
5901
  };
5092
5902
  if (systemInstruction) {
@@ -5118,7 +5928,7 @@ async function callGeminiCompletion(provider, apiKey, options, logger) {
5118
5928
  const candidate = candidates[0];
5119
5929
  const content = candidate.content;
5120
5930
  let textContent = "";
5121
- if (_optionalChain([content, 'optionalAccess', _193 => _193.parts])) {
5931
+ if (_optionalChain([content, 'optionalAccess', _282 => _282.parts])) {
5122
5932
  for (const part of content.parts) {
5123
5933
  if (part.text) {
5124
5934
  textContent += part.text;
@@ -5157,11 +5967,9 @@ async function callOpenAIResponseCompletion(provider, apiKey, options, logger) {
5157
5967
  ...options.maxTokens !== void 0 ? { max_output_tokens: options.maxTokens } : {},
5158
5968
  ...options.temperature !== void 0 ? { temperature: options.temperature } : {}
5159
5969
  };
5160
- if (options.thinkLevel && options.thinkLevel !== "none") {
5161
- const effort = _thinkingconfig.getOpenAIReasoningEffort.call(void 0, options.thinkLevel);
5162
- if (effort) {
5163
- request.reasoning = { effort, summary: "auto" };
5164
- }
5970
+ const responseEffort = resolveOpenAIEffort(provider, options, "openai-responses");
5971
+ if (responseEffort) {
5972
+ request.reasoning = { effort: responseEffort, summary: "auto" };
5165
5973
  }
5166
5974
  const apiUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: options.model, stream: false });
5167
5975
  const headers = _chunkQ27JY5RGcjs.getProviderHeaders.call(void 0, provider, apiKey);
@@ -5213,7 +6021,6 @@ async function callOpenAIResponseCompletion(provider, apiKey, options, logger) {
5213
6021
  }
5214
6022
 
5215
6023
  // src/completion/StreamHandler.ts
5216
-
5217
6024
  async function streamOpenAICompletion(provider, apiKey, options, messageId, callbacks, logger) {
5218
6025
  const request = {
5219
6026
  model: options.model,
@@ -5223,11 +6030,9 @@ async function streamOpenAICompletion(provider, apiKey, options, messageId, call
5223
6030
  temperature: options.temperature,
5224
6031
  stream: true
5225
6032
  };
5226
- if (options.thinkLevel && options.thinkLevel !== "none") {
5227
- const effort = _thinkingconfig.getOpenAIReasoningEffort.call(void 0, options.thinkLevel);
5228
- if (effort) {
5229
- request.reasoning_effort = effort;
5230
- }
6033
+ const effort = resolveOpenAIEffort(provider, options, "openai-chat");
6034
+ if (effort) {
6035
+ request.reasoning_effort = effort;
5231
6036
  }
5232
6037
  const apiUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: options.model, stream: true });
5233
6038
  const headers = _chunkQ27JY5RGcjs.getProviderHeaders.call(void 0, provider, apiKey);
@@ -5251,7 +6056,7 @@ async function streamOpenAICompletion(provider, apiKey, options, messageId, call
5251
6056
  });
5252
6057
  if (!response.ok) {
5253
6058
  const errorText = await response.text();
5254
- _optionalChain([callbacks, 'access', _194 => _194.onError, 'optionalCall', _195 => _195(`API error (${response.status}): ${errorText}`)]);
6059
+ _optionalChain([callbacks, 'access', _283 => _283.onError, 'optionalCall', _284 => _284(`API error (${response.status}): ${errorText}`)]);
5255
6060
  return;
5256
6061
  }
5257
6062
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "openai", {
@@ -5261,7 +6066,7 @@ async function streamOpenAICompletion(provider, apiKey, options, messageId, call
5261
6066
  onVideo: callbacks.onVideo,
5262
6067
  onError: callbacks.onError
5263
6068
  });
5264
- _optionalChain([callbacks, 'access', _196 => _196.onDone, 'optionalCall', _197 => _197(
6069
+ _optionalChain([callbacks, 'access', _285 => _285.onDone, 'optionalCall', _286 => _286(
5265
6070
  {
5266
6071
  id: messageId,
5267
6072
  role: "assistant",
@@ -5277,16 +6082,11 @@ async function streamOpenAICompletion(provider, apiKey, options, messageId, call
5277
6082
  }
5278
6083
  async function streamAnthropicCompletion(provider, apiKey, options, messageId, callbacks, logger) {
5279
6084
  const hasImages = options.messages.some((m) => m.images && m.images.length > 0);
5280
- const MAX_TOKENS_FOR_THINKING = 16384;
5281
- let effectiveMaxTokens = _nullishCoalesce(options.maxTokens, () => ( 16384));
5282
- const thinkingMaxTokens = options.thinkLevel && options.thinkLevel !== "none" ? Math.min(effectiveMaxTokens, MAX_TOKENS_FOR_THINKING) : effectiveMaxTokens;
5283
- const thinkingConfig = options.thinkLevel && options.thinkLevel !== "none" ? _thinkingconfig.buildAnthropicThinking.call(void 0, options.model, options.thinkLevel, thinkingMaxTokens) : void 0;
5284
- if (thinkingConfig) {
5285
- effectiveMaxTokens = thinkingMaxTokens;
5286
- }
6085
+ const reasoningWire = buildAnthropicReasoningWire(provider, options);
6086
+ const effectiveMaxTokens = reasoningWire.effectiveMaxTokens;
5287
6087
  logger.debug("Anthropic thinking configuration", {
5288
6088
  thinkLevel: options.thinkLevel,
5289
- thinkingConfig,
6089
+ thinkingConfig: reasoningWire.thinking,
5290
6090
  effectiveMaxTokens
5291
6091
  });
5292
6092
  let anthropicRequest;
@@ -5297,12 +6097,15 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5297
6097
  model: options.model,
5298
6098
  max_tokens: effectiveMaxTokens,
5299
6099
  // Omit temperature when thinking is enabled (Anthropic will use default temperature=1)
5300
- ...thinkingConfig ? {} : { temperature: options.temperature },
6100
+ ...reasoningWire.legacyBudgetEnabled ? {} : { temperature: options.temperature },
5301
6101
  ...systemMessages.length > 0 ? { system: systemMessages.map((m) => m.content).join("\n\n") } : {},
5302
6102
  messages: nonSystemMessages.map((m) => convertMessageToAnthropic(m)),
5303
6103
  stream: true,
5304
- ...thinkingConfig ? { thinking: thinkingConfig } : {}
6104
+ ...reasoningWire.thinking ? { thinking: reasoningWire.thinking } : {}
5305
6105
  };
6106
+ if (reasoningWire.outputConfig) {
6107
+ anthropicRequest.output_config = reasoningWire.outputConfig;
6108
+ }
5306
6109
  } else {
5307
6110
  const config = {
5308
6111
  defaultModel: options.model
@@ -5316,12 +6119,17 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5316
6119
  // Anthropic requires max_tokens; use adjusted value
5317
6120
  max_tokens: effectiveMaxTokens,
5318
6121
  // Omit temperature when thinking is enabled (Anthropic will use default temperature=1)
5319
- temperature: thinkingConfig ? void 0 : options.temperature,
6122
+ temperature: reasoningWire.legacyBudgetEnabled ? void 0 : options.temperature,
5320
6123
  stream: true
5321
6124
  };
5322
6125
  anthropicRequest = _chunkGDIXXKF2cjs.convertOpenAIToAnthropic.call(void 0, openaiRequest, config);
5323
- if (thinkingConfig) {
5324
- anthropicRequest.thinking = thinkingConfig;
6126
+ if (reasoningWire.thinking) {
6127
+ anthropicRequest.thinking = reasoningWire.thinking;
6128
+ }
6129
+ if (reasoningWire.outputConfig) {
6130
+ anthropicRequest.output_config = reasoningWire.outputConfig;
6131
+ }
6132
+ if (reasoningWire.legacyBudgetEnabled) {
5325
6133
  delete anthropicRequest.temperature;
5326
6134
  }
5327
6135
  }
@@ -5333,7 +6141,7 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5333
6141
  logger.info("Streaming Anthropic completion request", {
5334
6142
  url: apiUrl,
5335
6143
  model: anthropicRequest.model,
5336
- messagesCount: _optionalChain([anthropicRequest, 'access', _198 => _198.messages, 'optionalAccess', _199 => _199.length]),
6144
+ messagesCount: _optionalChain([anthropicRequest, 'access', _287 => _287.messages, 'optionalAccess', _288 => _288.length]),
5337
6145
  max_tokens: anthropicRequest.max_tokens,
5338
6146
  temperature: anthropicRequest.temperature,
5339
6147
  stream: anthropicRequest.stream,
@@ -5350,7 +6158,7 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5350
6158
  if (!response.ok) {
5351
6159
  const errorText = await response.text();
5352
6160
  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}`)]);
6161
+ _optionalChain([callbacks, 'access', _289 => _289.onError, 'optionalCall', _290 => _290(`API error (${response.status}): ${errorText}`)]);
5354
6162
  return;
5355
6163
  }
5356
6164
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "anthropic", {
@@ -5361,10 +6169,10 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5361
6169
  });
5362
6170
  logger.info("Anthropic stream complete", {
5363
6171
  contentLength: result.content.length,
5364
- reasoningLength: _optionalChain([result, 'access', _202 => _202.reasoning, 'optionalAccess', _203 => _203.length]) || 0,
6172
+ reasoningLength: _optionalChain([result, 'access', _291 => _291.reasoning, 'optionalAccess', _292 => _292.length]) || 0,
5365
6173
  blocksCount: result.blocks.length
5366
6174
  });
5367
- _optionalChain([callbacks, 'access', _204 => _204.onDone, 'optionalCall', _205 => _205(
6175
+ _optionalChain([callbacks, 'access', _293 => _293.onDone, 'optionalCall', _294 => _294(
5368
6176
  {
5369
6177
  id: messageId,
5370
6178
  role: "assistant",
@@ -5387,11 +6195,13 @@ async function streamGeminiCompletion(provider, apiKey, options, messageId, call
5387
6195
  contents.push(convertMessageToGemini(msg));
5388
6196
  }
5389
6197
  }
6198
+ const thinkingConfig = buildGeminiThinkingConfig(provider, options);
5390
6199
  const request = {
5391
6200
  contents,
5392
6201
  generationConfig: {
5393
6202
  ...options.maxTokens !== void 0 ? { maxOutputTokens: options.maxTokens } : {},
5394
- ...options.temperature !== void 0 ? { temperature: options.temperature } : {}
6203
+ ...options.temperature !== void 0 ? { temperature: options.temperature } : {},
6204
+ ...thinkingConfig ? { thinkingConfig } : {}
5395
6205
  }
5396
6206
  };
5397
6207
  if (systemInstruction) {
@@ -5420,7 +6230,7 @@ async function streamGeminiCompletion(provider, apiKey, options, messageId, call
5420
6230
  if (!response.ok) {
5421
6231
  const errorText = await response.text();
5422
6232
  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}`)]);
6233
+ _optionalChain([callbacks, 'access', _295 => _295.onError, 'optionalCall', _296 => _296(`API error (${response.status}): ${errorText}`)]);
5424
6234
  return;
5425
6235
  }
5426
6236
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "gemini", {
@@ -5429,7 +6239,7 @@ async function streamGeminiCompletion(provider, apiKey, options, messageId, call
5429
6239
  onError: callbacks.onError
5430
6240
  });
5431
6241
  logger.info("Gemini stream complete", { contentLength: result.content.length });
5432
- _optionalChain([callbacks, 'access', _208 => _208.onDone, 'optionalCall', _209 => _209(
6242
+ _optionalChain([callbacks, 'access', _297 => _297.onDone, 'optionalCall', _298 => _298(
5433
6243
  {
5434
6244
  id: messageId,
5435
6245
  role: "assistant",
@@ -5457,11 +6267,9 @@ async function streamOpenAIResponseCompletion(provider, apiKey, options, message
5457
6267
  ...options.maxTokens !== void 0 ? { max_output_tokens: options.maxTokens } : {},
5458
6268
  ...options.temperature !== void 0 ? { temperature: options.temperature } : {}
5459
6269
  };
5460
- if (options.thinkLevel && options.thinkLevel !== "none") {
5461
- const effort = _thinkingconfig.getOpenAIReasoningEffort.call(void 0, options.thinkLevel);
5462
- if (effort) {
5463
- request.reasoning = { effort, summary: "auto" };
5464
- }
6270
+ const responseEffort = resolveOpenAIEffort(provider, options, "openai-responses");
6271
+ if (responseEffort) {
6272
+ request.reasoning = { effort: responseEffort, summary: "auto" };
5465
6273
  }
5466
6274
  const apiUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: options.model, stream: true });
5467
6275
  const headers = _chunkQ27JY5RGcjs.getProviderHeaders.call(void 0, provider, apiKey);
@@ -5481,7 +6289,7 @@ async function streamOpenAIResponseCompletion(provider, apiKey, options, message
5481
6289
  });
5482
6290
  if (!response.ok) {
5483
6291
  const errorText = await response.text();
5484
- _optionalChain([callbacks, 'access', _210 => _210.onError, 'optionalCall', _211 => _211(`API error (${response.status}): ${errorText}`)]);
6292
+ _optionalChain([callbacks, 'access', _299 => _299.onError, 'optionalCall', _300 => _300(`API error (${response.status}): ${errorText}`)]);
5485
6293
  return;
5486
6294
  }
5487
6295
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "openai-response", {
@@ -5490,7 +6298,7 @@ async function streamOpenAIResponseCompletion(provider, apiKey, options, message
5490
6298
  onError: callbacks.onError
5491
6299
  });
5492
6300
  logger.info("OpenAI Response API stream complete", { contentLength: result.content.length });
5493
- _optionalChain([callbacks, 'access', _212 => _212.onDone, 'optionalCall', _213 => _213(
6301
+ _optionalChain([callbacks, 'access', _301 => _301.onDone, 'optionalCall', _302 => _302(
5494
6302
  {
5495
6303
  id: messageId,
5496
6304
  role: "assistant",
@@ -5510,7 +6318,7 @@ async function streamOpenAIResponseCompletion(provider, apiKey, options, message
5510
6318
 
5511
6319
 
5512
6320
 
5513
-
6321
+ var _thinkingconfig = require('@omnicross/contracts/thinking-config');
5514
6322
  async function resolveEffectiveMaxTokens(llmConfig, _getProvider, logger, _providerId, _modelId, sessionMaxTokens) {
5515
6323
  if (sessionMaxTokens !== void 0 && sessionMaxTokens > 0) {
5516
6324
  logger.debug("Using session maxTokens", { sessionMaxTokens });
@@ -5518,7 +6326,7 @@ async function resolveEffectiveMaxTokens(llmConfig, _getProvider, logger, _provi
5518
6326
  }
5519
6327
  try {
5520
6328
  const globalParams = await llmConfig.getGlobalModelParameters();
5521
- if (_optionalChain([globalParams, 'optionalAccess', _214 => _214.maxTokens, 'optionalAccess', _215 => _215.enabled]) && globalParams.maxTokens.value > 0) {
6329
+ if (_optionalChain([globalParams, 'optionalAccess', _303 => _303.maxTokens, 'optionalAccess', _304 => _304.enabled]) && globalParams.maxTokens.value > 0) {
5522
6330
  logger.debug("Using global maxTokens", { maxTokens: globalParams.maxTokens.value });
5523
6331
  return globalParams.maxTokens.value;
5524
6332
  }
@@ -5546,7 +6354,7 @@ async function resolveThinkingBudget(getProvider, logger, providerId, modelId, m
5546
6354
  }
5547
6355
  const thinkingBudget = _thinkingconfig.calculateThinkingBudget.call(void 0, modelId, thinkLevel, maxTokens);
5548
6356
  const provider = await getProvider(providerId);
5549
- const providerName = _optionalChain([provider, 'optionalAccess', _216 => _216.name, 'optionalAccess', _217 => _217.toLowerCase, 'call', _218 => _218()]) || "";
6357
+ const providerName = _optionalChain([provider, 'optionalAccess', _305 => _305.name, 'optionalAccess', _306 => _306.toLowerCase, 'call', _307 => _307()]) || "";
5550
6358
  const apiFormat = provider ? _chunkA2YMUCBTcjs.resolveApiFormat.call(void 0, provider) : "openai";
5551
6359
  if (apiFormat === "anthropic" || providerName === "anthropic" || providerName.includes("claude")) {
5552
6360
  const thinkingConfig = _thinkingconfig.buildAnthropicThinking.call(void 0, modelId, thinkLevel, maxTokens);
@@ -5583,7 +6391,7 @@ function logToolFormat(tools, logger) {
5583
6391
  tools: openaiTools.map((t) => t.function.name),
5584
6392
  firstTool: {
5585
6393
  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)]),
6394
+ description: _optionalChain([firstTool, 'access', _308 => _308.function, 'access', _309 => _309.description, 'optionalAccess', _310 => _310.slice, 'call', _311 => _311(0, 100)]),
5587
6395
  parameters: firstTool.function.parameters
5588
6396
  }
5589
6397
  });
@@ -5594,7 +6402,7 @@ function logToolFormat(tools, logger) {
5594
6402
  tools: anthropicTools.map((t) => t.name),
5595
6403
  firstTool: {
5596
6404
  name: firstTool.name,
5597
- description: _optionalChain([firstTool, 'access', _223 => _223.description, 'optionalAccess', _224 => _224.slice, 'call', _225 => _225(0, 100)]),
6405
+ description: _optionalChain([firstTool, 'access', _312 => _312.description, 'optionalAccess', _313 => _313.slice, 'call', _314 => _314(0, 100)]),
5598
6406
  input_schema: firstTool.input_schema
5599
6407
  }
5600
6408
  });
@@ -5605,9 +6413,9 @@ function logToolFormat(tools, logger) {
5605
6413
  count: allDeclarations.length,
5606
6414
  tools: allDeclarations.map((t) => t.name),
5607
6415
  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])
6416
+ name: _optionalChain([allDeclarations, 'access', _315 => _315[0], 'optionalAccess', _316 => _316.name]),
6417
+ description: _optionalChain([allDeclarations, 'access', _317 => _317[0], 'optionalAccess', _318 => _318.description, 'optionalAccess', _319 => _319.slice, 'call', _320 => _320(0, 100)]),
6418
+ parameters: _optionalChain([allDeclarations, 'access', _321 => _321[0], 'optionalAccess', _322 => _322.parameters])
5611
6419
  }
5612
6420
  });
5613
6421
  }
@@ -5641,7 +6449,7 @@ function buildToolRequest(apiFormat, conversationMessages, actualModel, options,
5641
6449
  // Native Anthropic Messages requires this field. Resolve an absent
5642
6450
  // caller cap to the model's real ceiling rather than a small constant —
5643
6451
  // see `resolveAnthropicMaxTokens`.
5644
- max_tokens: _chunkOXHNSZ53cjs.resolveAnthropicMaxTokens.call(void 0, actualModel, options.maxTokens),
6452
+ max_tokens: _chunkZQQUD66Mcjs.resolveAnthropicMaxTokens.call(void 0, actualModel, options.maxTokens),
5645
6453
  temperature: _nullishCoalesce(options.temperature, () => ( 0.7)),
5646
6454
  stream: true,
5647
6455
  tools: options.tools
@@ -5670,7 +6478,7 @@ function buildToolRequest(apiFormat, conversationMessages, actualModel, options,
5670
6478
  function extractDeltaContent(rawJson, apiFormat) {
5671
6479
  const json = rawJson;
5672
6480
  if (apiFormat === "google") {
5673
- const parts = _optionalChain([json, 'access', _234 => _234.candidates, 'optionalAccess', _235 => _235[0], 'optionalAccess', _236 => _236.content, 'optionalAccess', _237 => _237.parts]) || [];
6481
+ const parts = _optionalChain([json, 'access', _323 => _323.candidates, 'optionalAccess', _324 => _324[0], 'optionalAccess', _325 => _325.content, 'optionalAccess', _326 => _326.parts]) || [];
5674
6482
  let text = "";
5675
6483
  for (const part of parts) {
5676
6484
  if (part.thought === true) continue;
@@ -5678,15 +6486,15 @@ function extractDeltaContent(rawJson, apiFormat) {
5678
6486
  }
5679
6487
  return text;
5680
6488
  } else if (apiFormat === "anthropic") {
5681
- return _optionalChain([json, 'access', _238 => _238.delta, 'optionalAccess', _239 => _239.text]) || "";
6489
+ return _optionalChain([json, 'access', _327 => _327.delta, 'optionalAccess', _328 => _328.text]) || "";
5682
6490
  } else {
5683
- return _optionalChain([json, 'access', _240 => _240.choices, 'optionalAccess', _241 => _241[0], 'optionalAccess', _242 => _242.delta, 'optionalAccess', _243 => _243.content]) || "";
6491
+ return _optionalChain([json, 'access', _329 => _329.choices, 'optionalAccess', _330 => _330[0], 'optionalAccess', _331 => _331.delta, 'optionalAccess', _332 => _332.content]) || "";
5684
6492
  }
5685
6493
  }
5686
6494
  function extractDeltaReasoning(rawJson, apiFormat) {
5687
6495
  const json = rawJson;
5688
6496
  if (apiFormat === "google") {
5689
- const parts = _optionalChain([json, 'access', _244 => _244.candidates, 'optionalAccess', _245 => _245[0], 'optionalAccess', _246 => _246.content, 'optionalAccess', _247 => _247.parts]) || [];
6497
+ const parts = _optionalChain([json, 'access', _333 => _333.candidates, 'optionalAccess', _334 => _334[0], 'optionalAccess', _335 => _335.content, 'optionalAccess', _336 => _336.parts]) || [];
5690
6498
  let reasoning = "";
5691
6499
  for (const part of parts) {
5692
6500
  if (part.thought === true && part.text) reasoning += part.text;
@@ -5695,14 +6503,14 @@ function extractDeltaReasoning(rawJson, apiFormat) {
5695
6503
  } else if (apiFormat === "anthropic") {
5696
6504
  return "";
5697
6505
  } 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]) || "");
6506
+ const delta = _optionalChain([json, 'access', _337 => _337.choices, 'optionalAccess', _338 => _338[0], 'optionalAccess', _339 => _339.delta]);
6507
+ return (_optionalChain([delta, 'optionalAccess', _340 => _340.thinking, 'optionalAccess', _341 => _341.content]) || "") + (_optionalChain([delta, 'optionalAccess', _342 => _342.reasoning_content]) || "");
5700
6508
  }
5701
6509
  }
5702
6510
  function extractToolCalls(rawJson, apiFormat, toolCalls, callbacks, logger, pendingOpenAIToolCalls) {
5703
6511
  const json = rawJson;
5704
6512
  if (apiFormat === "google") {
5705
- const parts = _optionalChain([json, 'access', _254 => _254.candidates, 'optionalAccess', _255 => _255[0], 'optionalAccess', _256 => _256.content, 'optionalAccess', _257 => _257.parts]) || [];
6513
+ const parts = _optionalChain([json, 'access', _343 => _343.candidates, 'optionalAccess', _344 => _344[0], 'optionalAccess', _345 => _345.content, 'optionalAccess', _346 => _346.parts]) || [];
5706
6514
  for (const part of parts) {
5707
6515
  if (part.functionCall) {
5708
6516
  logger.info("Function call detected", { functionCall: part.functionCall });
@@ -5712,13 +6520,13 @@ function extractToolCalls(rawJson, apiFormat, toolCalls, callbacks, logger, pend
5712
6520
  args: part.functionCall.args || {}
5713
6521
  };
5714
6522
  toolCalls.push(toolCall);
5715
- _optionalChain([callbacks, 'access', _258 => _258.onToolCall, 'optionalCall', _259 => _259(toolCall)]);
6523
+ _optionalChain([callbacks, 'access', _347 => _347.onToolCall, 'optionalCall', _348 => _348(toolCall)]);
5716
6524
  logger.info("Tool called", { toolName: toolCall.name });
5717
6525
  }
5718
6526
  }
5719
6527
  } 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) {
6528
+ const delta = _optionalChain([json, 'access', _349 => _349.choices, 'optionalAccess', _350 => _350[0], 'optionalAccess', _351 => _351.delta]);
6529
+ if (_optionalChain([delta, 'optionalAccess', _352 => _352.tool_calls]) && pendingOpenAIToolCalls) {
5722
6530
  for (const tc of delta.tool_calls) {
5723
6531
  const index = _nullishCoalesce(tc.index, () => ( 0));
5724
6532
  let pending = pendingOpenAIToolCalls.get(index);
@@ -5727,8 +6535,8 @@ function extractToolCalls(rawJson, apiFormat, toolCalls, callbacks, logger, pend
5727
6535
  pendingOpenAIToolCalls.set(index, pending);
5728
6536
  }
5729
6537
  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;
6538
+ if (_optionalChain([tc, 'access', _353 => _353.function, 'optionalAccess', _354 => _354.name])) pending.name += tc.function.name;
6539
+ if (_optionalChain([tc, 'access', _355 => _355.function, 'optionalAccess', _356 => _356.arguments])) pending.arguments += tc.function.arguments;
5732
6540
  }
5733
6541
  }
5734
6542
  }
@@ -5743,7 +6551,7 @@ function finalizeOpenAIToolCalls(pendingOpenAIToolCalls, toolCalls, callbacks, l
5743
6551
  args: JSON.parse(pending.arguments || "{}")
5744
6552
  };
5745
6553
  toolCalls.push(toolCall);
5746
- _optionalChain([callbacks, 'access', _268 => _268.onToolCall, 'optionalCall', _269 => _269(toolCall)]);
6554
+ _optionalChain([callbacks, 'access', _357 => _357.onToolCall, 'optionalCall', _358 => _358(toolCall)]);
5747
6555
  logger.info("Tool called (OpenAI)", { toolName: toolCall.name, args: toolCall.args });
5748
6556
  } catch (e) {
5749
6557
  logger.error("Failed to parse OpenAI tool call arguments", e instanceof Error ? e : void 0, {
@@ -5757,38 +6565,38 @@ function finalizeOpenAIToolCalls(pendingOpenAIToolCalls, toolCalls, callbacks, l
5757
6565
  function parseStreamChunk(rawJson, apiFormat, callbacks, logger) {
5758
6566
  const json = rawJson;
5759
6567
  if (apiFormat === "google") {
5760
- const candidate = _optionalChain([json, 'access', _270 => _270.candidates, 'optionalAccess', _271 => _271[0]]);
6568
+ const candidate = _optionalChain([json, 'access', _359 => _359.candidates, 'optionalAccess', _360 => _360[0]]);
5761
6569
  if (!candidate) {
5762
6570
  logger.warn("No candidate found in Gemini response");
5763
6571
  return;
5764
6572
  }
5765
- const parts = _optionalChain([candidate, 'access', _272 => _272.content, 'optionalAccess', _273 => _273.parts]) || [];
6573
+ const parts = _optionalChain([candidate, 'access', _361 => _361.content, 'optionalAccess', _362 => _362.parts]) || [];
5766
6574
  for (const part of parts) {
5767
6575
  if (part.thought === true && part.text) {
5768
- _optionalChain([callbacks, 'access', _274 => _274.onReasoning, 'optionalCall', _275 => _275(part.text)]);
6576
+ _optionalChain([callbacks, 'access', _363 => _363.onReasoning, 'optionalCall', _364 => _364(part.text)]);
5769
6577
  continue;
5770
6578
  } else if (part.thoughtSignature && !part.thought) {
5771
6579
  logger.debug("Gemini returned thoughtSignature without thought content");
5772
6580
  }
5773
6581
  if (part.text) {
5774
- _optionalChain([callbacks, 'access', _276 => _276.onDelta, 'optionalCall', _277 => _277(part.text)]);
6582
+ _optionalChain([callbacks, 'access', _365 => _365.onDelta, 'optionalCall', _366 => _366(part.text)]);
5775
6583
  }
5776
6584
  }
5777
6585
  } else if (apiFormat === "anthropic") {
5778
6586
  const delta = json.delta;
5779
- if (_optionalChain([delta, 'optionalAccess', _278 => _278.text])) {
5780
- _optionalChain([callbacks, 'access', _279 => _279.onDelta, 'optionalCall', _280 => _280(delta.text)]);
6587
+ if (_optionalChain([delta, 'optionalAccess', _367 => _367.text])) {
6588
+ _optionalChain([callbacks, 'access', _368 => _368.onDelta, 'optionalCall', _369 => _369(delta.text)]);
5781
6589
  }
5782
6590
  } 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)]);
6591
+ const delta = _optionalChain([json, 'access', _370 => _370.choices, 'optionalAccess', _371 => _371[0], 'optionalAccess', _372 => _372.delta]);
6592
+ if (_optionalChain([delta, 'optionalAccess', _373 => _373.content])) {
6593
+ _optionalChain([callbacks, 'access', _374 => _374.onDelta, 'optionalCall', _375 => _375(delta.content)]);
5786
6594
  }
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)]);
6595
+ if (_optionalChain([delta, 'optionalAccess', _376 => _376.thinking, 'optionalAccess', _377 => _377.content])) {
6596
+ _optionalChain([callbacks, 'access', _378 => _378.onReasoning, 'optionalCall', _379 => _379(delta.thinking.content)]);
5789
6597
  }
5790
- if (_optionalChain([delta, 'optionalAccess', _291 => _291.reasoning_content])) {
5791
- _optionalChain([callbacks, 'access', _292 => _292.onReasoning, 'optionalCall', _293 => _293(delta.reasoning_content)]);
6598
+ if (_optionalChain([delta, 'optionalAccess', _380 => _380.reasoning_content])) {
6599
+ _optionalChain([callbacks, 'access', _381 => _381.onReasoning, 'optionalCall', _382 => _382(delta.reasoning_content)]);
5792
6600
  }
5793
6601
  }
5794
6602
  }
@@ -5801,7 +6609,7 @@ function buildIterationBlocks(iteration, content, reasoning, blocks, callbacks)
5801
6609
  content: reasoning
5802
6610
  };
5803
6611
  blocks.push(thinkingBlock);
5804
- _optionalChain([callbacks, 'access', _294 => _294.onBlock, 'optionalCall', _295 => _295(thinkingBlock)]);
6612
+ _optionalChain([callbacks, 'access', _383 => _383.onBlock, 'optionalCall', _384 => _384(thinkingBlock)]);
5805
6613
  }
5806
6614
  if (content && content.trim()) {
5807
6615
  const textBlock = {
@@ -5810,7 +6618,7 @@ function buildIterationBlocks(iteration, content, reasoning, blocks, callbacks)
5810
6618
  content
5811
6619
  };
5812
6620
  blocks.push(textBlock);
5813
- _optionalChain([callbacks, 'access', _296 => _296.onBlock, 'optionalCall', _297 => _297(textBlock)]);
6621
+ _optionalChain([callbacks, 'access', _385 => _385.onBlock, 'optionalCall', _386 => _386(textBlock)]);
5814
6622
  }
5815
6623
  }
5816
6624
  async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbacks, logger, builtinExecutor) {
@@ -5825,7 +6633,7 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5825
6633
  status: "running"
5826
6634
  };
5827
6635
  blocks.push(toolUseBlock);
5828
- _optionalChain([callbacks, 'access', _298 => _298.onBlock, 'optionalCall', _299 => _299(toolUseBlock)]);
6636
+ _optionalChain([callbacks, 'access', _387 => _387.onBlock, 'optionalCall', _388 => _388(toolUseBlock)]);
5829
6637
  try {
5830
6638
  logger.info("Executing tool", { toolName: toolCall.name });
5831
6639
  const mcpTool = mcpTools.find((t) => t.id === toolCall.name);
@@ -5848,8 +6656,8 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5848
6656
  result = { isError: true, content: [{ type: "text", text: "MCP service not available" }] };
5849
6657
  }
5850
6658
  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);
6659
+ _optionalChain([callbacks, 'access', _389 => _389.onToolResult, 'optionalCall', _390 => _390(toolCall.id, result)]);
6660
+ const resultText = _optionalChain([result, 'access', _391 => _391.content, 'optionalAccess', _392 => _392[0], 'optionalAccess', _393 => _393.text]) || JSON.stringify(result);
5853
6661
  const toolResultBlock = {
5854
6662
  id: `${toolCall.id}_result`,
5855
6663
  type: "tool_result",
@@ -5859,11 +6667,11 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5859
6667
  isError: result.isError
5860
6668
  };
5861
6669
  blocks.push(toolResultBlock);
5862
- _optionalChain([callbacks, 'access', _305 => _305.onBlock, 'optionalCall', _306 => _306(toolResultBlock)]);
6670
+ _optionalChain([callbacks, 'access', _394 => _394.onBlock, 'optionalCall', _395 => _395(toolResultBlock)]);
5863
6671
  const toolUseBlockRef = blocks.find((b) => b.id === `${toolCall.id}_use`);
5864
6672
  if (toolUseBlockRef && toolUseBlockRef.type === "tool_use") {
5865
6673
  toolUseBlockRef.status = result.isError ? "error" : "completed";
5866
- _optionalChain([callbacks, 'access', _307 => _307.onBlock, 'optionalCall', _308 => _308({ ...toolUseBlockRef })]);
6674
+ _optionalChain([callbacks, 'access', _396 => _396.onBlock, 'optionalCall', _397 => _397({ ...toolUseBlockRef })]);
5867
6675
  }
5868
6676
  toolResults.push({ toolCall, result });
5869
6677
  } catch (error) {
@@ -5873,7 +6681,7 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5873
6681
  isError: true,
5874
6682
  content: [{ type: "text", text: `Error: ${errorMessage}` }]
5875
6683
  };
5876
- _optionalChain([callbacks, 'access', _309 => _309.onToolResult, 'optionalCall', _310 => _310(toolCall.id, errorResult)]);
6684
+ _optionalChain([callbacks, 'access', _398 => _398.onToolResult, 'optionalCall', _399 => _399(toolCall.id, errorResult)]);
5877
6685
  const errorToolResultBlock = {
5878
6686
  id: `${toolCall.id}_result`,
5879
6687
  type: "tool_result",
@@ -5883,11 +6691,11 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5883
6691
  isError: true
5884
6692
  };
5885
6693
  blocks.push(errorToolResultBlock);
5886
- _optionalChain([callbacks, 'access', _311 => _311.onBlock, 'optionalCall', _312 => _312(errorToolResultBlock)]);
6694
+ _optionalChain([callbacks, 'access', _400 => _400.onBlock, 'optionalCall', _401 => _401(errorToolResultBlock)]);
5887
6695
  const errorToolUseBlockRef = blocks.find((b) => b.id === `${toolCall.id}_use`);
5888
6696
  if (errorToolUseBlockRef && errorToolUseBlockRef.type === "tool_use") {
5889
6697
  errorToolUseBlockRef.status = "error";
5890
- _optionalChain([callbacks, 'access', _313 => _313.onBlock, 'optionalCall', _314 => _314({ ...errorToolUseBlockRef })]);
6698
+ _optionalChain([callbacks, 'access', _402 => _402.onBlock, 'optionalCall', _403 => _403({ ...errorToolUseBlockRef })]);
5891
6699
  }
5892
6700
  toolResults.push({ toolCall, result: errorResult });
5893
6701
  }
@@ -5897,7 +6705,7 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5897
6705
  function mapToolArguments(args, mcpTool, logger) {
5898
6706
  const mappedArgs = { ...args };
5899
6707
  const toolSchema = mcpTool.inputSchema;
5900
- if (_optionalChain([toolSchema, 'optionalAccess', _315 => _315.properties])) {
6708
+ if (_optionalChain([toolSchema, 'optionalAccess', _404 => _404.properties])) {
5901
6709
  const expectedParams = Object.keys(toolSchema.properties);
5902
6710
  const paramMappings = {
5903
6711
  "search_query": ["query", "q", "searchQuery", "search"],
@@ -5931,25 +6739,25 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
5931
6739
  options.providerId,
5932
6740
  options.model
5933
6741
  );
5934
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _316 => _316.actualProviderId]) || options.providerId;
5935
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _317 => _317.actualModelId]) || options.model;
6742
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _405 => _405.actualProviderId]) || options.providerId;
6743
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _406 => _406.actualModelId]) || options.model;
5936
6744
  const provider = await getProvider(actualProviderId);
5937
6745
  if (!provider) {
5938
- _optionalChain([callbacks, 'access', _318 => _318.onError, 'optionalCall', _319 => _319(`Provider not found: ${actualProviderId}`)]);
6746
+ _optionalChain([callbacks, 'access', _407 => _407.onError, 'optionalCall', _408 => _408(`Provider not found: ${actualProviderId}`)]);
5939
6747
  return;
5940
6748
  }
5941
6749
  if (!provider.enabled) {
5942
- _optionalChain([callbacks, 'access', _320 => _320.onError, 'optionalCall', _321 => _321(`Provider is disabled: ${provider.name}`)]);
6750
+ _optionalChain([callbacks, 'access', _409 => _409.onError, 'optionalCall', _410 => _410(`Provider is disabled: ${provider.name}`)]);
5943
6751
  return;
5944
6752
  }
5945
6753
  const { apiKey: effectiveKey } = _chunkA2YMUCBTcjs.resolveProviderEndpoint.call(void 0, provider);
5946
6754
  const apiKey = resolveApiKey2(effectiveKey);
5947
6755
  if (!apiKey) {
5948
- _optionalChain([callbacks, 'access', _322 => _322.onError, 'optionalCall', _323 => _323("API key not configured")]);
6756
+ _optionalChain([callbacks, 'access', _411 => _411.onError, 'optionalCall', _412 => _412("API key not configured")]);
5949
6757
  return;
5950
6758
  }
5951
6759
  const messageId = `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`;
5952
- _optionalChain([callbacks, 'access', _324 => _324.onStart, 'optionalCall', _325 => _325(messageId)]);
6760
+ _optionalChain([callbacks, 'access', _413 => _413.onStart, 'optionalCall', _414 => _414(messageId)]);
5953
6761
  const globalParams = await llmConfig.getGlobalModelParameters();
5954
6762
  const MAX_ITERATIONS = _nullishCoalesce(globalParams.toolMaxTurns, () => ( 100));
5955
6763
  let iteration = 0;
@@ -5996,12 +6804,12 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
5996
6804
  });
5997
6805
  if (!response.ok) {
5998
6806
  const errorText = await response.text();
5999
- _optionalChain([callbacks, 'access', _326 => _326.onError, 'optionalCall', _327 => _327(`API error (${response.status}): ${errorText}`)]);
6807
+ _optionalChain([callbacks, 'access', _415 => _415.onError, 'optionalCall', _416 => _416(`API error (${response.status}): ${errorText}`)]);
6000
6808
  return;
6001
6809
  }
6002
- const reader = _optionalChain([response, 'access', _328 => _328.body, 'optionalAccess', _329 => _329.getReader, 'call', _330 => _330()]);
6810
+ const reader = _optionalChain([response, 'access', _417 => _417.body, 'optionalAccess', _418 => _418.getReader, 'call', _419 => _419()]);
6003
6811
  if (!reader) {
6004
- _optionalChain([callbacks, 'access', _331 => _331.onError, 'optionalCall', _332 => _332("No response body")]);
6812
+ _optionalChain([callbacks, 'access', _420 => _420.onError, 'optionalCall', _421 => _421("No response body")]);
6005
6813
  return;
6006
6814
  }
6007
6815
  const decoder = new TextDecoder();
@@ -6034,7 +6842,7 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
6034
6842
  logger.debug("Parsed JSON", { json });
6035
6843
  if (json.error) {
6036
6844
  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)}`)]);
6845
+ _optionalChain([callbacks, 'access', _422 => _422.onError, 'optionalCall', _423 => _423(`API Error: ${json.error.message || JSON.stringify(json.error)}`)]);
6038
6846
  return;
6039
6847
  }
6040
6848
  parseStreamChunk(json, apiFormat, callbacks, logger);
@@ -6067,7 +6875,7 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
6067
6875
  toolCallsLength: toolCalls.length,
6068
6876
  executableToolCallsLength: executableToolCalls.length,
6069
6877
  hasMcpTools: !!options.mcpTools,
6070
- mcpToolsLength: _optionalChain([options, 'access', _335 => _335.mcpTools, 'optionalAccess', _336 => _336.length]),
6878
+ mcpToolsLength: _optionalChain([options, 'access', _424 => _424.mcpTools, 'optionalAccess', _425 => _425.length]),
6071
6879
  hasMcpService: !!mcpService
6072
6880
  });
6073
6881
  if (executableToolCalls.length > 0 && options.mcpTools && (mcpService || builtinExecutor)) {
@@ -6088,7 +6896,7 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
6088
6896
  builtinExecutor
6089
6897
  );
6090
6898
  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);
6899
+ const resultText = typeof result === "string" ? result : _optionalChain([result, 'access', _426 => _426.content, 'optionalAccess', _427 => _427[0], 'optionalAccess', _428 => _428.text]) || JSON.stringify(result);
6092
6900
  return `Tool ${toolCall.name} result:
6093
6901
  ${resultText}`;
6094
6902
  }).join("\n\n");
@@ -6106,12 +6914,12 @@ ${resultText}`;
6106
6914
  }
6107
6915
  } catch (streamError) {
6108
6916
  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"}`)]);
6917
+ _optionalChain([callbacks, 'access', _429 => _429.onError, 'optionalCall', _430 => _430(`Stream error: ${streamError instanceof Error ? streamError.message : "Unknown error"}`)]);
6110
6918
  return;
6111
6919
  }
6112
6920
  }
6113
6921
  logger.info("Sending final callback", { blocksCount: blocks.length });
6114
- _optionalChain([callbacks, 'access', _342 => _342.onDone, 'optionalCall', _343 => _343({
6922
+ _optionalChain([callbacks, 'access', _431 => _431.onDone, 'optionalCall', _432 => _432({
6115
6923
  id: messageId,
6116
6924
  role: "assistant",
6117
6925
  content: finalContent,
@@ -6122,7 +6930,7 @@ ${resultText}`;
6122
6930
  } catch (error) {
6123
6931
  logger.error("Error in streamWithTools", error instanceof Error ? error : void 0);
6124
6932
  const message = error instanceof Error ? error.message : "Unknown error";
6125
- _optionalChain([callbacks, 'access', _344 => _344.onError, 'optionalCall', _345 => _345(message)]);
6933
+ _optionalChain([callbacks, 'access', _433 => _433.onError, 'optionalCall', _434 => _434(message)]);
6126
6934
  }
6127
6935
  }
6128
6936
 
@@ -6173,8 +6981,8 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
6173
6981
  options.providerId,
6174
6982
  options.model
6175
6983
  );
6176
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _346 => _346.actualProviderId]) || options.providerId;
6177
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _347 => _347.actualModelId]) || options.model;
6984
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _435 => _435.actualProviderId]) || options.providerId;
6985
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _436 => _436.actualModelId]) || options.model;
6178
6986
  const provider = await getProvider(actualProviderId);
6179
6987
  if (!provider) {
6180
6988
  return { success: false, error: `Provider not found: ${actualProviderId}` };
@@ -6204,7 +7012,8 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
6204
7012
  name: provider.name,
6205
7013
  baseUrl: provider.api_base_url,
6206
7014
  apiKey,
6207
- models: provider.models || []
7015
+ models: provider.models || [],
7016
+ modelConfigs: provider.modelConfigs
6208
7017
  };
6209
7018
  const executor = getSharedExecutor2();
6210
7019
  const { response } = await _chunkMUHWHQVMcjs.executeProviderCall.call(void 0, {
@@ -6250,18 +7059,18 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
6250
7059
  { endpointTransformer: void 0 }
6251
7060
  );
6252
7061
  const data = await transformedResponse.json();
6253
- const choice = _optionalChain([data, 'access', _348 => _348.choices, 'optionalAccess', _349 => _349[0]]);
7062
+ const choice = _optionalChain([data, 'access', _437 => _437.choices, 'optionalAccess', _438 => _438[0]]);
6254
7063
  if (!choice) {
6255
7064
  return { success: false, error: "No choices in response" };
6256
7065
  }
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) => ({
7066
+ const rawToolCalls = _optionalChain([choice, 'access', _439 => _439.message, 'optionalAccess', _440 => _440.tool_calls]);
7067
+ const toolCalls = _optionalChain([rawToolCalls, 'optionalAccess', _441 => _441.map, 'call', _442 => _442((tc) => ({
6259
7068
  id: tc.id,
6260
7069
  name: tc.function.name,
6261
7070
  args: typeof tc.function.arguments === "string" ? JSON.parse(tc.function.arguments) : tc.function.arguments
6262
7071
  }))]);
6263
7072
  const tapped = readUsageFromOpenAIResponse(data.usage);
6264
- if (_optionalChain([recording, 'optionalAccess', _354 => _354.recorder]) && tapped) {
7073
+ if (_optionalChain([recording, 'optionalAccess', _443 => _443.recorder]) && tapped) {
6265
7074
  recording.recorder.record({
6266
7075
  messageId: _nullishCoalesce(options.messageId, () => ( null)),
6267
7076
  parentMessageId: _nullishCoalesce(options.parentMessageId, () => ( null)),
@@ -6279,9 +7088,9 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
6279
7088
  message: {
6280
7089
  id: `msg_${Date.now()}`,
6281
7090
  role: "assistant",
6282
- content: _optionalChain([choice, 'access', _355 => _355.message, 'optionalAccess', _356 => _356.content]) || "",
7091
+ content: _optionalChain([choice, 'access', _444 => _444.message, 'optionalAccess', _445 => _445.content]) || "",
6283
7092
  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,
7093
+ thinking: data.reasoning_content || _optionalChain([choice, 'access', _446 => _446.message, 'optionalAccess', _447 => _447.thinking, 'optionalAccess', _448 => _448.content]) ? { content: data.reasoning_content || _optionalChain([choice, 'access', _449 => _449.message, 'optionalAccess', _450 => _450.thinking, 'optionalAccess', _451 => _451.content]) || "", signature: _optionalChain([choice, 'access', _452 => _452.message, 'optionalAccess', _453 => _453.thinking, 'optionalAccess', _454 => _454.signature]) } : void 0,
6285
7094
  toolCalls
6286
7095
  },
6287
7096
  usage: data.usage ? {
@@ -6302,21 +7111,21 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6302
7111
  options.providerId,
6303
7112
  options.model
6304
7113
  );
6305
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _366 => _366.actualProviderId]) || options.providerId;
6306
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _367 => _367.actualModelId]) || options.model;
7114
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _455 => _455.actualProviderId]) || options.providerId;
7115
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _456 => _456.actualModelId]) || options.model;
6307
7116
  const provider = await getProvider(actualProviderId);
6308
7117
  if (!provider) {
6309
- _optionalChain([callbacks, 'access', _368 => _368.onError, 'optionalCall', _369 => _369(`Provider not found: ${actualProviderId}`)]);
7118
+ _optionalChain([callbacks, 'access', _457 => _457.onError, 'optionalCall', _458 => _458(`Provider not found: ${actualProviderId}`)]);
6310
7119
  return;
6311
7120
  }
6312
7121
  if (!provider.enabled) {
6313
- _optionalChain([callbacks, 'access', _370 => _370.onError, 'optionalCall', _371 => _371(`Provider is disabled: ${provider.name}`)]);
7122
+ _optionalChain([callbacks, 'access', _459 => _459.onError, 'optionalCall', _460 => _460(`Provider is disabled: ${provider.name}`)]);
6314
7123
  return;
6315
7124
  }
6316
7125
  const { apiKey: effectiveKey } = _chunkA2YMUCBTcjs.resolveProviderEndpoint.call(void 0, provider);
6317
7126
  const apiKey = resolveApiKey2(effectiveKey);
6318
7127
  if (!apiKey) {
6319
- _optionalChain([callbacks, 'access', _372 => _372.onError, 'optionalCall', _373 => _373("API key not configured")]);
7128
+ _optionalChain([callbacks, 'access', _461 => _461.onError, 'optionalCall', _462 => _462("API key not configured")]);
6320
7129
  return;
6321
7130
  }
6322
7131
  const { chain, hasTransformers } = await resolveChainWithMain(
@@ -6328,13 +7137,14 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6328
7137
  return completeStreamFallback(options, callbacks);
6329
7138
  }
6330
7139
  const messageId = `msg_${Date.now()}`;
6331
- _optionalChain([callbacks, 'access', _374 => _374.onStart, 'optionalCall', _375 => _375(messageId)]);
7140
+ _optionalChain([callbacks, 'access', _463 => _463.onStart, 'optionalCall', _464 => _464(messageId)]);
6332
7141
  const unifiedRequest = buildUnifiedCompletionRequest(actualModel, options, true);
6333
7142
  const transformerProvider = {
6334
7143
  name: provider.name,
6335
7144
  baseUrl: provider.api_base_url,
6336
7145
  apiKey,
6337
- models: provider.models || []
7146
+ models: provider.models || [],
7147
+ modelConfigs: provider.modelConfigs
6338
7148
  };
6339
7149
  const executor = getSharedExecutor2();
6340
7150
  const { response } = await _chunkMUHWHQVMcjs.executeProviderCall.call(void 0, {
@@ -6367,7 +7177,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6367
7177
  });
6368
7178
  if (!response.ok) {
6369
7179
  const errorText = await response.text();
6370
- _optionalChain([callbacks, 'access', _376 => _376.onError, 'optionalCall', _377 => _377(`API error (${response.status}): ${errorText}`)]);
7180
+ _optionalChain([callbacks, 'access', _465 => _465.onError, 'optionalCall', _466 => _466(`API error (${response.status}): ${errorText}`)]);
6371
7181
  return;
6372
7182
  }
6373
7183
  const transformedResponse = await executor.executeResponseChain(
@@ -6377,9 +7187,9 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6377
7187
  chain,
6378
7188
  { endpointTransformer: void 0 }
6379
7189
  );
6380
- const reader = _optionalChain([transformedResponse, 'access', _378 => _378.body, 'optionalAccess', _379 => _379.getReader, 'call', _380 => _380()]);
7190
+ const reader = _optionalChain([transformedResponse, 'access', _467 => _467.body, 'optionalAccess', _468 => _468.getReader, 'call', _469 => _469()]);
6381
7191
  if (!reader) {
6382
- _optionalChain([callbacks, 'access', _381 => _381.onError, 'optionalCall', _382 => _382("No response body")]);
7192
+ _optionalChain([callbacks, 'access', _470 => _470.onError, 'optionalCall', _471 => _471("No response body")]);
6383
7193
  return;
6384
7194
  }
6385
7195
  const decoder = new TextDecoder();
@@ -6397,22 +7207,22 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6397
7207
  if (data === "[DONE]") continue;
6398
7208
  try {
6399
7209
  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])) {
7210
+ const delta = _optionalChain([json, 'access', _472 => _472.choices, 'optionalAccess', _473 => _473[0], 'optionalAccess', _474 => _474.delta]);
7211
+ if (_optionalChain([delta, 'optionalAccess', _475 => _475.content])) {
6402
7212
  content += delta.content;
6403
- _optionalChain([callbacks, 'access', _387 => _387.onDelta, 'optionalCall', _388 => _388(delta.content)]);
7213
+ _optionalChain([callbacks, 'access', _476 => _476.onDelta, 'optionalCall', _477 => _477(delta.content)]);
6404
7214
  }
6405
- if (_optionalChain([delta, 'optionalAccess', _389 => _389.thinking, 'optionalAccess', _390 => _390.content])) {
7215
+ if (_optionalChain([delta, 'optionalAccess', _478 => _478.thinking, 'optionalAccess', _479 => _479.content])) {
6406
7216
  reasoning += delta.thinking.content;
6407
- _optionalChain([callbacks, 'access', _391 => _391.onReasoning, 'optionalCall', _392 => _392(delta.thinking.content)]);
7217
+ _optionalChain([callbacks, 'access', _480 => _480.onReasoning, 'optionalCall', _481 => _481(delta.thinking.content)]);
6408
7218
  }
6409
- if (_optionalChain([delta, 'optionalAccess', _393 => _393.reasoning_content])) {
7219
+ if (_optionalChain([delta, 'optionalAccess', _482 => _482.reasoning_content])) {
6410
7220
  reasoning += delta.reasoning_content;
6411
- _optionalChain([callbacks, 'access', _394 => _394.onReasoning, 'optionalCall', _395 => _395(delta.reasoning_content)]);
7221
+ _optionalChain([callbacks, 'access', _483 => _483.onReasoning, 'optionalCall', _484 => _484(delta.reasoning_content)]);
6412
7222
  }
6413
7223
  if (json.usage) {
6414
7224
  const tapped = readUsageFromOpenAIResponse(json.usage);
6415
- if (_optionalChain([recording, 'optionalAccess', _396 => _396.recorder]) && tapped) {
7225
+ if (_optionalChain([recording, 'optionalAccess', _485 => _485.recorder]) && tapped) {
6416
7226
  recording.recorder.record({
6417
7227
  messageId: _nullishCoalesce(options.messageId, () => ( null)),
6418
7228
  parentMessageId: _nullishCoalesce(options.parentMessageId, () => ( null)),
@@ -6425,7 +7235,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6425
7235
  rawUsage: json.usage
6426
7236
  });
6427
7237
  }
6428
- _optionalChain([callbacks, 'access', _397 => _397.onDone, 'optionalCall', _398 => _398(
7238
+ _optionalChain([callbacks, 'access', _486 => _486.onDone, 'optionalCall', _487 => _487(
6429
7239
  {
6430
7240
  id: messageId,
6431
7241
  role: "assistant",
@@ -6446,7 +7256,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6446
7256
  }
6447
7257
  }
6448
7258
  }
6449
- _optionalChain([callbacks, 'access', _399 => _399.onDone, 'optionalCall', _400 => _400({
7259
+ _optionalChain([callbacks, 'access', _488 => _488.onDone, 'optionalCall', _489 => _489({
6450
7260
  id: messageId,
6451
7261
  role: "assistant",
6452
7262
  content,
@@ -6458,13 +7268,13 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6458
7268
  }
6459
7269
  } catch (error) {
6460
7270
  const message = error instanceof Error ? error.message : "Unknown error";
6461
- _optionalChain([callbacks, 'access', _401 => _401.onError, 'optionalCall', _402 => _402(message)]);
7271
+ _optionalChain([callbacks, 'access', _490 => _490.onError, 'optionalCall', _491 => _491(message)]);
6462
7272
  }
6463
7273
  }
6464
7274
 
6465
7275
  // 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);
7276
+ var CompletionService = (_class15 = class {
7277
+ 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
7278
  this.paths = paths;
6469
7279
  this.llmConfig = llmConfig;
6470
7280
  this.logger = logger;
@@ -6472,10 +7282,10 @@ var CompletionService = (_class13 = class {
6472
7282
 
6473
7283
 
6474
7284
 
6475
- __init23() {this.apiKeyPool = null}
6476
- __init24() {this.usageRecorder = null}
6477
- __init25() {this.usageEventSink = null}
6478
- __init26() {this.visionFallbackProvider = null}
7285
+ __init30() {this.apiKeyPool = null}
7286
+ __init31() {this.usageRecorder = null}
7287
+ __init32() {this.usageEventSink = null}
7288
+ __init33() {this.visionFallbackProvider = null}
6479
7289
  /**
6480
7290
  * Set the API key pool service for multi-key load balancing.
6481
7291
  * When set, keys are resolved via the pool instead of directly from the provider.
@@ -6525,8 +7335,8 @@ var CompletionService = (_class13 = class {
6525
7335
  options.providerId,
6526
7336
  options.model
6527
7337
  );
6528
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _403 => _403.actualProviderId]) || options.providerId;
6529
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _404 => _404.actualModelId]) || options.model;
7338
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _492 => _492.actualProviderId]) || options.providerId;
7339
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _493 => _493.actualModelId]) || options.model;
6530
7340
  const provider = await this.getProvider(actualProviderId);
6531
7341
  if (!provider) {
6532
7342
  return { success: false, error: `Provider not found: ${actualProviderId}` };
@@ -6559,7 +7369,7 @@ var CompletionService = (_class13 = class {
6559
7369
  this.apiKeyPool.reportSuccess(options.sessionId);
6560
7370
  }
6561
7371
  if (result.success && result.usage && options.sessionId) {
6562
- _optionalChain([this, 'access', _405 => _405.usageEventSink, 'optionalAccess', _406 => _406.emit, 'call', _407 => _407({
7372
+ _optionalChain([this, 'access', _494 => _494.usageEventSink, 'optionalAccess', _495 => _495.emit, 'call', _496 => _496({
6563
7373
  sessionId: options.sessionId,
6564
7374
  modelId: actualModel,
6565
7375
  usage: result.usage,
@@ -6588,8 +7398,8 @@ var CompletionService = (_class13 = class {
6588
7398
  options.model
6589
7399
  );
6590
7400
  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;
7401
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _497 => _497.actualProviderId]) || options.providerId;
7402
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _498 => _498.actualModelId]) || options.model;
6593
7403
  this.logger.debug("Resolved provider and model", { actualProviderId, actualModel });
6594
7404
  const provider = await this.getProvider(actualProviderId);
6595
7405
  this.logger.debug("Retrieved provider", provider ? {
@@ -6601,22 +7411,22 @@ var CompletionService = (_class13 = class {
6601
7411
  enabled: provider.enabled
6602
7412
  } : { error: "Provider not found" });
6603
7413
  if (!provider) {
6604
- _optionalChain([callbacks, 'access', _410 => _410.onError, 'optionalCall', _411 => _411(`Provider not found: ${actualProviderId}`)]);
7414
+ _optionalChain([callbacks, 'access', _499 => _499.onError, 'optionalCall', _500 => _500(`Provider not found: ${actualProviderId}`)]);
6605
7415
  return;
6606
7416
  }
6607
7417
  if (!provider.enabled) {
6608
- _optionalChain([callbacks, 'access', _412 => _412.onError, 'optionalCall', _413 => _413(`Provider is disabled: ${provider.name}`)]);
7418
+ _optionalChain([callbacks, 'access', _501 => _501.onError, 'optionalCall', _502 => _502(`Provider is disabled: ${provider.name}`)]);
6609
7419
  return;
6610
7420
  }
6611
7421
  const apiKey = await this.resolveApiKeyForRequest(provider, actualProviderId, options.sessionId);
6612
7422
  if (!apiKey) {
6613
- _optionalChain([callbacks, 'access', _414 => _414.onError, 'optionalCall', _415 => _415("API key not configured")]);
7423
+ _optionalChain([callbacks, 'access', _503 => _503.onError, 'optionalCall', _504 => _504("API key not configured")]);
6614
7424
  return;
6615
7425
  }
6616
7426
  const apiFormat = _chunkA2YMUCBTcjs.resolveApiFormat.call(void 0, provider);
6617
7427
  this.logger.info("Using API format for stream", { apiFormat });
6618
7428
  const messageId = `msg_${Date.now()}`;
6619
- _optionalChain([callbacks, 'access', _416 => _416.onStart, 'optionalCall', _417 => _417(messageId)]);
7429
+ _optionalChain([callbacks, 'access', _505 => _505.onStart, 'optionalCall', _506 => _506(messageId)]);
6620
7430
  const resolvedOptions = { ...options, model: actualModel };
6621
7431
  if (options.sessionId) {
6622
7432
  const sid = options.sessionId;
@@ -6625,14 +7435,14 @@ var CompletionService = (_class13 = class {
6625
7435
  ...callbacks,
6626
7436
  onDone: (message, usage, metrics) => {
6627
7437
  if (usage) {
6628
- _optionalChain([this, 'access', _418 => _418.usageEventSink, 'optionalAccess', _419 => _419.emit, 'call', _420 => _420({
7438
+ _optionalChain([this, 'access', _507 => _507.usageEventSink, 'optionalAccess', _508 => _508.emit, 'call', _509 => _509({
6629
7439
  sessionId: sid,
6630
7440
  modelId: actualModel,
6631
7441
  usage,
6632
7442
  engineOrigin: "completion"
6633
7443
  })]);
6634
7444
  }
6635
- _optionalChain([userOnDone, 'optionalCall', _421 => _421(message, usage, metrics)]);
7445
+ _optionalChain([userOnDone, 'optionalCall', _510 => _510(message, usage, metrics)]);
6636
7446
  }
6637
7447
  };
6638
7448
  }
@@ -6648,11 +7458,11 @@ var CompletionService = (_class13 = class {
6648
7458
  retryState.error = { status, message: error };
6649
7459
  return;
6650
7460
  }
6651
- _optionalChain([callbacks, 'access', _422 => _422.onError, 'optionalCall', _423 => _423(error)]);
7461
+ _optionalChain([callbacks, 'access', _511 => _511.onError, 'optionalCall', _512 => _512(error)]);
6652
7462
  },
6653
7463
  onDone: (message, usage, metrics) => {
6654
7464
  this.apiKeyPool.reportSuccess(options.sessionId);
6655
- _optionalChain([callbacks, 'access', _424 => _424.onDone, 'optionalCall', _425 => _425(message, usage, metrics)]);
7465
+ _optionalChain([callbacks, 'access', _513 => _513.onDone, 'optionalCall', _514 => _514(message, usage, metrics)]);
6656
7466
  }
6657
7467
  };
6658
7468
  await this.callStreamHandler(apiFormat, provider, apiKey, resolvedOptions, messageId, interceptCallbacks);
@@ -6673,7 +7483,7 @@ var CompletionService = (_class13 = class {
6673
7483
  // don't fire onStart again
6674
7484
  });
6675
7485
  } else {
6676
- _optionalChain([callbacks, 'access', _426 => _426.onError, 'optionalCall', _427 => _427(retryState.error.message)]);
7486
+ _optionalChain([callbacks, 'access', _515 => _515.onError, 'optionalCall', _516 => _516(retryState.error.message)]);
6677
7487
  }
6678
7488
  }
6679
7489
  } else {
@@ -6686,7 +7496,7 @@ var CompletionService = (_class13 = class {
6686
7496
  } catch (error) {
6687
7497
  const message = error instanceof Error ? error.message : "Unknown error";
6688
7498
  this.logger.error("Stream completion error", error instanceof Error ? error : void 0, { message });
6689
- _optionalChain([callbacks, 'access', _428 => _428.onError, 'optionalCall', _429 => _429(message)]);
7499
+ _optionalChain([callbacks, 'access', _517 => _517.onError, 'optionalCall', _518 => _518(message)]);
6690
7500
  }
6691
7501
  }
6692
7502
  /**
@@ -6726,7 +7536,7 @@ var CompletionService = (_class13 = class {
6726
7536
  return {
6727
7537
  success: true,
6728
7538
  message: "Connection successful",
6729
- response: _optionalChain([result, 'access', _430 => _430.message, 'optionalAccess', _431 => _431.content]) || "",
7539
+ response: _optionalChain([result, 'access', _519 => _519.message, 'optionalAccess', _520 => _520.content]) || "",
6730
7540
  model: modelId,
6731
7541
  durationMs
6732
7542
  };
@@ -6791,7 +7601,7 @@ ${description}`;
6791
7601
  * 3. Legacy single key from provider config
6792
7602
  */
6793
7603
  async resolveApiKeyForRequest(provider, providerId, sessionId) {
6794
- if (_optionalChain([provider, 'access', _432 => _432.codingPlan, 'optionalAccess', _433 => _433.enabled]) && provider.codingPlan.apiKey) {
7604
+ if (_optionalChain([provider, 'access', _521 => _521.codingPlan, 'optionalAccess', _522 => _522.enabled]) && provider.codingPlan.apiKey) {
6795
7605
  return this.resolveApiKey(provider.codingPlan.apiKey);
6796
7606
  }
6797
7607
  if (this.apiKeyPool) {
@@ -6957,7 +7767,34 @@ ${description}`;
6957
7767
  builtinExecutor
6958
7768
  );
6959
7769
  }
6960
- }, _class13);
7770
+ }, _class15);
7771
+
7772
+
7773
+
7774
+
7775
+
7776
+
7777
+
7778
+
7779
+
7780
+
7781
+
7782
+
7783
+
7784
+
7785
+
7786
+
7787
+
7788
+
7789
+
7790
+
7791
+
7792
+
7793
+
7794
+
7795
+
7796
+
7797
+
6961
7798
 
6962
7799
 
6963
7800
 
@@ -7051,4 +7888,4 @@ ${description}`;
7051
7888
 
7052
7889
 
7053
7890
 
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;
7891
+ 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;