@omnicross/core 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/dist/ApiConverter.cjs +2 -2
  2. package/dist/ApiConverter.js +1 -1
  3. package/dist/{ProviderProxy-C-xqrkKi.d.ts → ProviderProxy--ROec6VI.d.ts} +22 -3
  4. package/dist/{ProviderProxy-CnMQYN59.d.cts → ProviderProxy-QdwNV6l1.d.cts} +22 -3
  5. package/dist/auth/GeminiCodeAssistProjectResolver.cjs +4 -3
  6. package/dist/auth/GeminiCodeAssistProjectResolver.js +3 -2
  7. package/dist/{chunk-3Z5BQZFM.cjs → chunk-235MXAGX.cjs} +3 -1
  8. package/dist/{chunk-SQYVORNP.cjs → chunk-26K6T2YS.cjs} +1520 -523
  9. package/dist/{chunk-DLXZEZWO.cjs → chunk-2E42SAW3.cjs} +42 -4
  10. package/dist/{chunk-7AK7B36B.cjs → chunk-2Z4XM3H5.cjs} +6 -6
  11. package/dist/{chunk-OEWZUX4G.js → chunk-32O35CIF.js} +9 -20
  12. package/dist/chunk-3SCOLIPT.cjs +63 -0
  13. package/dist/{chunk-SYQF7OIR.js → chunk-3WD4HTR2.js} +2 -0
  14. package/dist/{chunk-AT7B7SOZ.js → chunk-5NJLQ6T6.js} +21 -10
  15. package/dist/chunk-5RSZYQJH.cjs +62 -0
  16. package/dist/{chunk-IFXJ2BG7.js → chunk-7C7DET6S.js} +51 -6
  17. package/dist/{chunk-SNYBEXLB.cjs → chunk-EPN67EGP.cjs} +6 -1
  18. package/dist/chunk-FU7EAR73.js +62 -0
  19. package/dist/{chunk-UX7VWA46.js → chunk-FXR6N3SR.js} +39 -1
  20. package/dist/{chunk-WYTEQ4Q5.cjs → chunk-GDIXXKF2.cjs} +21 -10
  21. package/dist/{chunk-WB6PH56J.js → chunk-GFIZ6LQI.js} +12 -8
  22. package/dist/chunk-GL6XM527.js +61 -0
  23. package/dist/{chunk-EJLH6NFT.cjs → chunk-J3QQ6JS5.cjs} +12 -8
  24. package/dist/{chunk-VRFBB5DB.js → chunk-MYYR3HZL.js} +4 -4
  25. package/dist/{chunk-YJVHCHQX.js → chunk-N7OOJI5V.js} +46 -2
  26. package/dist/chunk-NKVZ6Z3Y.cjs +61 -0
  27. package/dist/chunk-OTZC4TNH.cjs +79 -0
  28. package/dist/{chunk-6VUQPOV3.cjs → chunk-OVQBNVSV.cjs} +10 -21
  29. package/dist/{chunk-4VLRHHFE.cjs → chunk-OXHNSZ53.cjs} +9 -11
  30. package/dist/{chunk-6CL5HTPQ.js → chunk-PEGTIPB3.js} +8 -10
  31. package/dist/chunk-PO3XB56Z.js +79 -0
  32. package/dist/{chunk-UDCC7EMR.js → chunk-QYC54PMG.js} +1242 -245
  33. package/dist/{chunk-SIZQQD6T.js → chunk-RJ5ZGNI3.js} +42 -5
  34. package/dist/{chunk-KCVJWJST.cjs → chunk-RJUONLUQ.cjs} +47 -10
  35. package/dist/{chunk-BPKCU575.js → chunk-UAPBLNN2.js} +6 -1
  36. package/dist/{chunk-UNWKG6EI.cjs → chunk-XPIOJQS2.cjs} +51 -6
  37. package/dist/chunk-XXFYLL7R.js +63 -0
  38. package/dist/{chunk-YK57NMIF.cjs → chunk-YWL7YZWS.cjs} +47 -3
  39. package/dist/completion/ApiKeyPoolService.cjs +32 -1
  40. package/dist/completion/ApiKeyPoolService.d.cts +17 -1
  41. package/dist/completion/ApiKeyPoolService.d.ts +17 -1
  42. package/dist/completion/ApiKeyPoolService.js +31 -0
  43. package/dist/completion/CompletionService.cjs +23 -19
  44. package/dist/completion/CompletionService.d.cts +1 -0
  45. package/dist/completion/CompletionService.d.ts +1 -0
  46. package/dist/completion/CompletionService.js +22 -18
  47. package/dist/completion.cjs +23 -19
  48. package/dist/completion.d.cts +1 -0
  49. package/dist/completion.d.ts +1 -0
  50. package/dist/completion.js +22 -18
  51. package/dist/index.cjs +81 -21
  52. package/dist/index.d.cts +5 -5
  53. package/dist/index.d.ts +5 -5
  54. package/dist/index.js +82 -22
  55. package/dist/outbound-api/auditCapture.cjs +2 -2
  56. package/dist/outbound-api/auditCapture.d.cts +3 -4
  57. package/dist/outbound-api/auditCapture.d.ts +3 -4
  58. package/dist/outbound-api/auditCapture.js +1 -1
  59. package/dist/outbound-api/routeResolver.d.cts +4 -3
  60. package/dist/outbound-api/routeResolver.d.ts +4 -3
  61. package/dist/outbound-api/subscriptionRegistryPort.d.cts +1 -0
  62. package/dist/outbound-api/subscriptionRegistryPort.d.ts +1 -0
  63. package/dist/outbound-api/types.d.cts +3 -2
  64. package/dist/outbound-api/types.d.ts +3 -2
  65. package/dist/outbound-api.cjs +23 -19
  66. package/dist/outbound-api.d.cts +8 -6
  67. package/dist/outbound-api.d.ts +8 -6
  68. package/dist/outbound-api.js +22 -18
  69. package/dist/pipeline/AccountAllowanceScheduling.d.cts +3 -2
  70. package/dist/pipeline/AccountAllowanceScheduling.d.ts +3 -2
  71. package/dist/pipeline/AccountRouteActivity.cjs +10 -0
  72. package/dist/pipeline/AccountRouteActivity.d.cts +61 -0
  73. package/dist/pipeline/AccountRouteActivity.d.ts +61 -0
  74. package/dist/pipeline/AccountRouteActivity.js +10 -0
  75. package/dist/pipeline/ServerOverloadCounter.cjs +16 -0
  76. package/dist/pipeline/ServerOverloadCounter.d.cts +77 -0
  77. package/dist/pipeline/ServerOverloadCounter.d.ts +77 -0
  78. package/dist/pipeline/ServerOverloadCounter.js +16 -0
  79. package/dist/pipeline/SubscriptionAccountHealth.cjs +4 -2
  80. package/dist/pipeline/SubscriptionAccountHealth.d.cts +32 -3
  81. package/dist/pipeline/SubscriptionAccountHealth.d.ts +32 -3
  82. package/dist/pipeline/SubscriptionAccountHealth.js +3 -1
  83. package/dist/pipeline/upstreamFetch.cjs +4 -3
  84. package/dist/pipeline/upstreamFetch.d.cts +35 -6
  85. package/dist/pipeline/upstreamFetch.d.ts +35 -6
  86. package/dist/pipeline/upstreamFetch.js +3 -2
  87. package/dist/pipeline/upstreamTrace.cjs +4 -2
  88. package/dist/pipeline/upstreamTrace.d.cts +22 -8
  89. package/dist/pipeline/upstreamTrace.d.ts +22 -8
  90. package/dist/pipeline/upstreamTrace.js +3 -1
  91. package/dist/ports.d.cts +2 -2
  92. package/dist/ports.d.ts +2 -2
  93. package/dist/provider-proxy/ProviderProxy.cjs +23 -19
  94. package/dist/provider-proxy/ProviderProxy.d.cts +2 -1
  95. package/dist/provider-proxy/ProviderProxy.d.ts +2 -1
  96. package/dist/provider-proxy/ProviderProxy.js +22 -18
  97. package/dist/provider-proxy/identity/codexCliHeaders.cjs +10 -0
  98. package/dist/provider-proxy/identity/codexCliHeaders.d.cts +45 -0
  99. package/dist/provider-proxy/identity/codexCliHeaders.d.ts +45 -0
  100. package/dist/provider-proxy/identity/codexCliHeaders.js +10 -0
  101. package/dist/provider-proxy/ingress/providerProxyShared.cjs +23 -19
  102. package/dist/provider-proxy/ingress/providerProxyShared.d.cts +11 -1
  103. package/dist/provider-proxy/ingress/providerProxyShared.d.ts +11 -1
  104. package/dist/provider-proxy/ingress/providerProxyShared.js +22 -18
  105. package/dist/provider-proxy/matchText.cjs +2 -2
  106. package/dist/provider-proxy/matchText.d.cts +1 -3
  107. package/dist/provider-proxy/matchText.d.ts +1 -3
  108. package/dist/provider-proxy/matchText.js +1 -1
  109. package/dist/provider-proxy/types.d.cts +20 -1
  110. package/dist/provider-proxy/types.d.ts +20 -1
  111. package/dist/provider-proxy.cjs +79 -19
  112. package/dist/provider-proxy.d.cts +255 -8
  113. package/dist/provider-proxy.d.ts +255 -8
  114. package/dist/provider-proxy.js +80 -20
  115. package/dist/{routeResolver-CHTv_Bs5.d.ts → routeResolver-C0mkkZaM.d.ts} +1 -1
  116. package/dist/{routeResolver-kepxnfcC.d.cts → routeResolver-CVFE8KKe.d.cts} +1 -1
  117. package/dist/transformer/transformers/AnthropicTransformer.cjs +3 -2
  118. package/dist/transformer/transformers/AnthropicTransformer.js +2 -1
  119. package/dist/transformer/transformers/OpenAIResponseTransformer.cjs +3 -2
  120. package/dist/transformer/transformers/OpenAIResponseTransformer.js +2 -1
  121. package/dist/transformer/transformers.cjs +7 -6
  122. package/dist/transformer/transformers.js +6 -5
  123. package/dist/transformer.cjs +9 -8
  124. package/dist/transformer.js +8 -7
  125. package/dist/{types-hsoKrVQS.d.ts → types-BB8pxk_3.d.ts} +40 -1
  126. package/dist/{types-H3wGZSOo.d.cts → types-C2MwkLah.d.cts} +40 -1
  127. package/dist/usage/pricing-engine.cjs +2 -2
  128. package/dist/usage/pricing-engine.d.cts +20 -0
  129. package/dist/usage/pricing-engine.d.ts +20 -0
  130. package/dist/usage/pricing-engine.js +1 -1
  131. package/dist/usage/usage-recorder.cjs +2 -2
  132. package/dist/usage/usage-recorder.d.cts +11 -1
  133. package/dist/usage/usage-recorder.d.ts +11 -1
  134. package/dist/usage/usage-recorder.js +1 -1
  135. package/dist/usage.cjs +3 -3
  136. package/dist/usage.js +2 -2
  137. package/package.json +1 -1
@@ -1,30 +1,30 @@
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
+ var _chunkHQP3K7PUcjs = require('./chunk-HQP3K7PU.cjs');
3
4
 
4
5
 
5
- var _chunkEJLH6NFTcjs = require('./chunk-EJLH6NFT.cjs');
6
6
 
7
+ var _chunkOXHNSZ53cjs = require('./chunk-OXHNSZ53.cjs');
7
8
 
8
- var _chunkHQP3K7PUcjs = require('./chunk-HQP3K7PU.cjs');
9
9
 
10
+ var _chunkV4TXMEJScjs = require('./chunk-V4TXMEJS.cjs');
10
11
 
11
- var _chunkIIRPUVPHcjs = require('./chunk-IIRPUVPH.cjs');
12
12
 
13
+ var _chunkOVQBNVSVcjs = require('./chunk-OVQBNVSV.cjs');
13
14
 
14
- var _chunkGBHYAGX3cjs = require('./chunk-GBHYAGX3.cjs');
15
15
 
16
+ var _chunkIIRPUVPHcjs = require('./chunk-IIRPUVPH.cjs');
16
17
 
17
- var _chunkV4TXMEJScjs = require('./chunk-V4TXMEJS.cjs');
18
18
 
19
+ var _chunkGBHYAGX3cjs = require('./chunk-GBHYAGX3.cjs');
19
20
 
20
- var _chunk6VUQPOV3cjs = require('./chunk-6VUQPOV3.cjs');
21
21
 
22
22
 
23
23
 
24
- var _chunk4VLRHHFEcjs = require('./chunk-4VLRHHFE.cjs');
25
24
 
26
25
 
27
- var _chunkR3HK6R4Bcjs = require('./chunk-R3HK6R4B.cjs');
26
+
27
+ var _chunk3SCOLIPTcjs = require('./chunk-3SCOLIPT.cjs');
28
28
 
29
29
 
30
30
  var _chunkRPII3E6Zcjs = require('./chunk-RPII3E6Z.cjs');
@@ -34,20 +34,34 @@ var _chunkRPII3E6Zcjs = require('./chunk-RPII3E6Z.cjs');
34
34
  var _chunk6RPZADX3cjs = require('./chunk-6RPZADX3.cjs');
35
35
 
36
36
 
37
+
38
+
39
+ var _chunkJ3QQ6JS5cjs = require('./chunk-J3QQ6JS5.cjs');
40
+
41
+
42
+ var _chunkUIBD2YY5cjs = require('./chunk-UIBD2YY5.cjs');
43
+
44
+
45
+ var _chunkR3HK6R4Bcjs = require('./chunk-R3HK6R4B.cjs');
46
+
47
+
48
+ var _chunkQXD4MV2Xcjs = require('./chunk-QXD4MV2X.cjs');
49
+
50
+
37
51
  var _chunkNYUNU75Acjs = require('./chunk-NYUNU75A.cjs');
38
52
 
39
53
 
54
+ var _chunkOTZC4TNHcjs = require('./chunk-OTZC4TNH.cjs');
40
55
 
41
- var _chunkYK57NMIFcjs = require('./chunk-YK57NMIF.cjs');
42
56
 
43
57
 
44
- var _chunkYNJNTCSQcjs = require('./chunk-YNJNTCSQ.cjs');
58
+ var _chunkYWL7YZWScjs = require('./chunk-YWL7YZWS.cjs');
45
59
 
46
60
 
47
- var _chunkSVNDB62Dcjs = require('./chunk-SVNDB62D.cjs');
61
+ var _chunkYNJNTCSQcjs = require('./chunk-YNJNTCSQ.cjs');
48
62
 
49
63
 
50
- var _chunkUIBD2YY5cjs = require('./chunk-UIBD2YY5.cjs');
64
+ var _chunkSVNDB62Dcjs = require('./chunk-SVNDB62D.cjs');
51
65
 
52
66
 
53
67
  var _chunk6QQG7YG3cjs = require('./chunk-6QQG7YG3.cjs');
@@ -64,9 +78,6 @@ var _chunkMUHWHQVMcjs = require('./chunk-MUHWHQVM.cjs');
64
78
  var _chunkQ27JY5RGcjs = require('./chunk-Q27JY5RG.cjs');
65
79
 
66
80
 
67
- var _chunkQXD4MV2Xcjs = require('./chunk-QXD4MV2X.cjs');
68
-
69
-
70
81
  var _chunkAPYG5QD7cjs = require('./chunk-APYG5QD7.cjs');
71
82
 
72
83
 
@@ -85,19 +96,28 @@ var _chunk4YINUUKQcjs = require('./chunk-4YINUUKQ.cjs');
85
96
  var _chunk4VZX5F4Tcjs = require('./chunk-4VZX5F4T.cjs');
86
97
 
87
98
 
99
+ var _chunkQBZJ7P2Tcjs = require('./chunk-QBZJ7P2T.cjs');
100
+
101
+
88
102
 
89
103
 
90
104
  var _chunkA2YMUCBTcjs = require('./chunk-A2YMUCBT.cjs');
91
105
 
92
106
 
93
- var _chunkUNWKG6EIcjs = require('./chunk-UNWKG6EI.cjs');
107
+ var _chunkXPIOJQS2cjs = require('./chunk-XPIOJQS2.cjs');
108
+
109
+
94
110
 
111
+ var _chunkGDIXXKF2cjs = require('./chunk-GDIXXKF2.cjs');
95
112
 
96
113
 
97
- var _chunkWYTEQ4Q5cjs = require('./chunk-WYTEQ4Q5.cjs');
114
+ var _chunkRJUONLUQcjs = require('./chunk-RJUONLUQ.cjs');
98
115
 
99
116
 
100
- var _chunkKCVJWJSTcjs = require('./chunk-KCVJWJST.cjs');
117
+ var _chunkHVBBDBS3cjs = require('./chunk-HVBBDBS3.cjs');
118
+
119
+
120
+ var _chunkNKVZ6Z3Ycjs = require('./chunk-NKVZ6Z3Y.cjs');
101
121
 
102
122
 
103
123
  var _chunkJMJBSACDcjs = require('./chunk-JMJBSACD.cjs');
@@ -415,12 +435,16 @@ var ProviderProxyRouteMap = (_class = class {
415
435
  * (`ANTHROPIC_AUTH_TOKEN` / `OPENAI_API_KEY`). Optionally override the idle
416
436
  * timeout (tests use a short one).
417
437
  */
418
- addRoute(context, idleMs) {
438
+ addRoute(context, idleMsOrOptions) {
439
+ const options = typeof idleMsOrOptions === "number" ? { idleMs: idleMsOrOptions } : _nullishCoalesce(idleMsOrOptions, () => ( {}));
419
440
  const token = _crypto.randomBytes.call(void 0, TOKEN_BYTES).toString("hex");
420
441
  const entry = {
421
442
  context,
422
443
  idleTimer: null,
423
- idleMs: _nullishCoalesce(idleMs, () => ( this.defaultIdleMs))
444
+ idleMs: _nullishCoalesce(options.idleMs, () => ( this.defaultIdleMs)),
445
+ onActivity: options.onActivity,
446
+ onEvicted: options.onEvicted,
447
+ evicted: false
424
448
  };
425
449
  this.routes.set(token, entry);
426
450
  this.armIdleTimer(token, entry);
@@ -435,6 +459,10 @@ var ProviderProxyRouteMap = (_class = class {
435
459
  if (!token) return void 0;
436
460
  const entry = this.routes.get(token);
437
461
  if (!entry) return void 0;
462
+ try {
463
+ _optionalChain([entry, 'access', _5 => _5.onActivity, 'optionalCall', _6 => _6(Date.now())]);
464
+ } catch (e4) {
465
+ }
438
466
  this.armIdleTimer(token, entry);
439
467
  return entry.context;
440
468
  }
@@ -442,8 +470,16 @@ var ProviderProxyRouteMap = (_class = class {
442
470
  removeRoute(token) {
443
471
  const entry = this.routes.get(token);
444
472
  if (!entry) return false;
445
- this.clearIdleTimer(entry);
446
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);
447
483
  return true;
448
484
  }
449
485
  /** Current live-route count (tests / diagnostics). */
@@ -456,10 +492,11 @@ var ProviderProxyRouteMap = (_class = class {
456
492
  }
457
493
  /** Tear down every route (proxy stop / app teardown). */
458
494
  clear() {
459
- for (const entry of this.routes.values()) {
460
- this.clearIdleTimer(entry);
461
- }
495
+ const entries2 = [...this.routes.values()];
462
496
  this.routes.clear();
497
+ for (const entry of entries2) {
498
+ this.evict(entry, "cleared");
499
+ }
463
500
  }
464
501
  // ---------------------------------------------------------------------------
465
502
  // Idle reaping (modeled on AcpSessionManager.armIdleTimer / clearIdleTimer)
@@ -467,9 +504,10 @@ var ProviderProxyRouteMap = (_class = class {
467
504
  armIdleTimer(token, entry) {
468
505
  this.clearIdleTimer(entry);
469
506
  entry.idleTimer = setTimeout(() => {
470
- this.routes.delete(token);
507
+ if (!this.routes.delete(token)) return;
508
+ this.evict(entry, "idle");
471
509
  }, entry.idleMs);
472
- _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()]);
473
511
  }
474
512
  clearIdleTimer(entry) {
475
513
  if (entry.idleTimer) {
@@ -477,8 +515,77 @@ var ProviderProxyRouteMap = (_class = class {
477
515
  entry.idleTimer = null;
478
516
  }
479
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
+ }
480
527
  }, _class);
481
528
 
529
+ // src/provider-proxy/identity/claudeCodeHeaders.ts
530
+ var DEFAULT_ANTHROPIC_VERSION = "2023-06-01";
531
+ var OAUTH_BETA = "oauth-2025-04-20";
532
+ var CLAUDE_CODE_BETA = "claude-code-20250219";
533
+ var INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14";
534
+ var TOOL_STREAMING_BETA = "fine-grained-tool-streaming-2025-05-14";
535
+ var DEFAULT_CLAUDE_CODE_HEADERS = Object.freeze({
536
+ "user-agent": "claude-cli/1.0.119 (external, cli)",
537
+ "x-app": "cli",
538
+ accept: "application/json",
539
+ "accept-language": "*",
540
+ "sec-fetch-mode": "cors",
541
+ "anthropic-dangerous-direct-browser-access": "true",
542
+ "x-stainless-lang": "js",
543
+ "x-stainless-package-version": "0.55.1",
544
+ "x-stainless-os": "Windows",
545
+ "x-stainless-arch": "x64",
546
+ "x-stainless-runtime": "node",
547
+ "x-stainless-runtime-version": "v20.19.2",
548
+ "x-stainless-retry-count": "0",
549
+ "x-stainless-timeout": "60"
550
+ });
551
+ var FORCED_ACCEPT_ENCODING = "identity";
552
+ var CLAUDE_FORWARD_ALLOWLIST = /* @__PURE__ */ new Set([
553
+ "accept",
554
+ "accept-language",
555
+ "sec-fetch-mode",
556
+ "user-agent",
557
+ "x-app",
558
+ "anthropic-dangerous-direct-browser-access",
559
+ "anthropic-version"
560
+ ]);
561
+ var STAINLESS_PREFIX = "x-stainless-";
562
+ function extractClaudeClientHeaders(reqHeaders) {
563
+ const out = {};
564
+ for (const [rawKey, rawVal] of Object.entries(reqHeaders)) {
565
+ const key = rawKey.toLowerCase();
566
+ if (_chunk3SCOLIPTcjs.NEVER_FORWARD_HEADERS.has(key)) continue;
567
+ if (!CLAUDE_FORWARD_ALLOWLIST.has(key) && !key.startsWith(STAINLESS_PREFIX)) continue;
568
+ const value = _chunk3SCOLIPTcjs.flattenHeaderValue.call(void 0, rawVal);
569
+ if (value !== void 0) out[key] = value;
570
+ }
571
+ return out;
572
+ }
573
+ function buildAnthropicBeta(model, callerBeta) {
574
+ const isHaiku = typeof model === "string" && model.toLowerCase().includes("haiku");
575
+ const baseline = isHaiku ? [OAUTH_BETA, INTERLEAVED_THINKING_BETA] : [CLAUDE_CODE_BETA, OAUTH_BETA, INTERLEAVED_THINKING_BETA, TOOL_STREAMING_BETA];
576
+ const seen = /* @__PURE__ */ new Set();
577
+ const out = [];
578
+ const add = (flag) => {
579
+ const trimmed = flag.trim();
580
+ if (!trimmed || seen.has(trimmed)) return;
581
+ seen.add(trimmed);
582
+ out.push(trimmed);
583
+ };
584
+ baseline.forEach(add);
585
+ if (callerBeta) callerBeta.split(",").forEach(add);
586
+ return out.join(",");
587
+ }
588
+
482
589
  // src/provider-proxy/usage/recordAnthropicUsage.ts
483
590
  function readAnthropicUsage(usage) {
484
591
  if (!usage) return null;
@@ -506,6 +613,10 @@ function emitAnthropicUsageRecord(recorder, rawUsage, attribution) {
506
613
  providerId: attribution.providerId,
507
614
  model: attribution.model,
508
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)),
509
620
  auditResponse: attribution.auditResponse,
510
621
  engineOrigin: "codex-ingress",
511
622
  usage: tapped,
@@ -520,13 +631,13 @@ function recordAnthropicNonStreamUsage(recorder, bodyText, attribution) {
520
631
  parsed.usage,
521
632
  attribution
522
633
  );
523
- } catch (e4) {
634
+ } catch (e6) {
524
635
  }
525
636
  }
526
637
  function recordAnthropicStreamUsage(recorder, rawUsage, attribution) {
527
638
  try {
528
639
  emitAnthropicUsageRecord(recorder, rawUsage, attribution);
529
- } catch (e5) {
640
+ } catch (e7) {
530
641
  }
531
642
  }
532
643
 
@@ -540,12 +651,12 @@ function getSharedExecutor() {
540
651
  }
541
652
  var sharedResponses = null;
542
653
  function getResponsesEndpointTransformer() {
543
- if (!sharedResponses) sharedResponses = new (0, _chunk6VUQPOV3cjs.OpenAIResponseTransformer)();
654
+ if (!sharedResponses) sharedResponses = new (0, _chunkOVQBNVSVcjs.OpenAIResponseTransformer)();
544
655
  return sharedResponses;
545
656
  }
546
657
  var sharedAnthropic = null;
547
658
  function getAnthropicEndpointTransformer() {
548
- if (!sharedAnthropic) sharedAnthropic = new (0, _chunk4VLRHHFEcjs.AnthropicTransformer)();
659
+ if (!sharedAnthropic) sharedAnthropic = new (0, _chunkOXHNSZ53cjs.AnthropicTransformer)();
549
660
  return sharedAnthropic;
550
661
  }
551
662
  var sharedGemini = null;
@@ -576,7 +687,7 @@ function rewriteJsonModel(bodyText, rewriteModel) {
576
687
  changed = true;
577
688
  }
578
689
  return changed ? JSON.stringify(obj) : bodyText;
579
- } catch (e6) {
690
+ } catch (e8) {
580
691
  return bodyText;
581
692
  }
582
693
  }
@@ -609,7 +720,7 @@ function rewriteSseLine(line, rewriteModel) {
609
720
  }
610
721
  if (!changed) return line;
611
722
  return `data:${leadingSpace}${JSON.stringify(obj)}${newline}`;
612
- } catch (e7) {
723
+ } catch (e9) {
613
724
  return line;
614
725
  }
615
726
  }
@@ -620,7 +731,7 @@ function tapSseDataLine(line, onEvent) {
620
731
  if (!payload || payload === "[DONE]") return;
621
732
  try {
622
733
  onEvent(JSON.parse(payload));
623
- } catch (e8) {
734
+ } catch (e10) {
624
735
  }
625
736
  }
626
737
  async function relaySseBody(res, body, options) {
@@ -653,7 +764,7 @@ async function relaySseBody(res, body, options) {
653
764
  res.end();
654
765
  }
655
766
  }
656
- async function relayResponse(res, providerResponse, isStream, rewriteModel, usageTap) {
767
+ async function relayResponse(res, providerResponse, isStream, rewriteModel, usageTap, onSseEvent) {
657
768
  const contentType = _nullishCoalesce(providerResponse.headers.get("Content-Type"), () => ( ""));
658
769
  const status = providerResponse.status && providerResponse.status >= 100 ? providerResponse.status : 200;
659
770
  const upstreamIsSse = contentType.includes("text/event-stream");
@@ -667,13 +778,16 @@ async function relayResponse(res, providerResponse, isStream, rewriteModel, usag
667
778
  res.end();
668
779
  return null;
669
780
  }
670
- if (rewriteModel || usageTap) {
781
+ if (rewriteModel || usageTap || onSseEvent) {
671
782
  let capturedUsage = null;
672
783
  await relaySseBody(res, providerResponse.body, {
673
784
  rewriteModel,
674
- onSseEvent: usageTap ? (event) => {
675
- const raw = usageTap.extractUsage(event);
676
- if (raw) capturedUsage = raw;
785
+ onSseEvent: onSseEvent || usageTap ? (event) => {
786
+ _optionalChain([onSseEvent, 'optionalCall', _20 => _20(event)]);
787
+ if (usageTap) {
788
+ const raw = usageTap.extractUsage(event);
789
+ if (raw) capturedUsage = raw;
790
+ }
677
791
  } : void 0
678
792
  });
679
793
  if (capturedUsage) usageTap.onUsage(capturedUsage);
@@ -717,7 +831,7 @@ async function aggregateAnthropicSseToJsonBody(response, rewriteModel) {
717
831
  let ev;
718
832
  try {
719
833
  ev = JSON.parse(payload);
720
- } catch (e9) {
834
+ } catch (e11) {
721
835
  continue;
722
836
  }
723
837
  switch (ev["type"]) {
@@ -772,7 +886,7 @@ async function aggregateAnthropicSseToJsonBody(response, rewriteModel) {
772
886
  let input = {};
773
887
  try {
774
888
  input = blk.inputJson ? JSON.parse(blk.inputJson) : {};
775
- } catch (e10) {
889
+ } catch (e12) {
776
890
  input = {};
777
891
  }
778
892
  return { type: "tool_use", id: _nullishCoalesce(blk.id, () => ( "")), name: _nullishCoalesce(blk.name, () => ( "")), input };
@@ -822,7 +936,7 @@ function resolveApiKey(apiKey) {
822
936
  }
823
937
  async function resolvePoolBoundKey(deps, providerId, provider, sessionId, preferredKeyId, boundKeyFallbackPolicy) {
824
938
  if (preferredKeyId) {
825
- const preferred = await _optionalChain([deps, 'access', _8 => _8.apiKeyPool, 'optionalAccess', _9 => _9.getKeyById, 'call', _10 => _10(providerId, preferredKeyId, sessionId)]);
939
+ const preferred = await _optionalChain([deps, 'access', _21 => _21.apiKeyPool, 'optionalAccess', _22 => _22.getKeyById, 'call', _23 => _23(providerId, preferredKeyId, sessionId)]);
826
940
  if (preferred) return preferred;
827
941
  if (boundKeyFallbackPolicy !== "pool") return "";
828
942
  }
@@ -848,7 +962,7 @@ async function buildSubscriptionPlan(res, route, deps, anthropicBody, routeModel
848
962
  resolvedModel = mapped.resolvedModel;
849
963
  scenario = mapped.scenario;
850
964
  }
851
- const sessionKey = _chunkEJLH6NFTcjs.deriveSubscriptionSessionKey.call(void 0, anthropicBody);
965
+ const sessionKey = _chunkJ3QQ6JS5cjs.deriveSubscriptionSessionKey.call(void 0, anthropicBody);
852
966
  const plan = buildSubscriptionIterationPlan(profile, route, deps, resolvedModel, isStream, scenario, sessionKey);
853
967
  if (!plan) {
854
968
  writeError(res, 502, "Subscription profile is missing resolveUpstreamUrl");
@@ -857,11 +971,11 @@ async function buildSubscriptionPlan(res, route, deps, anthropicBody, routeModel
857
971
  return plan;
858
972
  }
859
973
  function buildSubscriptionIterationPlan(profile, route, deps, resolvedModel, isStream, scenario, sessionKey) {
860
- const upstreamUrl = _optionalChain([profile, 'access', _11 => _11.resolveUpstreamUrl, 'optionalCall', _12 => _12(resolvedModel, route.subscriptionConfig)]);
974
+ const upstreamUrl = _optionalChain([profile, 'access', _24 => _24.resolveUpstreamUrl, 'optionalCall', _25 => _25(resolvedModel, route.subscriptionConfig)]);
861
975
  if (!upstreamUrl) return null;
862
976
  const auth = _nullishCoalesce(route.auth, () => ( new (0, _chunkYNJNTCSQcjs.SubscriptionAuthSource)(profile)));
863
977
  const sameFormat = profile.mode === "pass-through" || upstreamUrl.endsWith("/v1/messages");
864
- const overrideNames = _optionalChain([profile, 'access', _13 => _13.resolveProviderTransformerNames, 'optionalCall', _14 => _14(
978
+ const overrideNames = _optionalChain([profile, 'access', _26 => _26.resolveProviderTransformerNames, 'optionalCall', _27 => _27(
865
979
  resolvedModel,
866
980
  route.subscriptionConfig
867
981
  )]);
@@ -926,7 +1040,7 @@ function buildSubscriptionRequestSummary(anthropicBody) {
926
1040
  estimatedInputTokens: Math.ceil(totalChars / 4),
927
1041
  // Single source of truth (shared with the dispatcher builder) so both
928
1042
  // ingress paths yield identical `matchText` for the same body.
929
- matchText: _chunkEJLH6NFTcjs.collectMatchText.call(void 0, anthropicBody)
1043
+ matchText: _chunkJ3QQ6JS5cjs.collectMatchText.call(void 0, anthropicBody)
930
1044
  };
931
1045
  }
932
1046
  function collectText(value) {
@@ -960,7 +1074,7 @@ async function runPipeline(anthropicBody, plan, reportSelection) {
960
1074
  boundAccountFallbackPolicy: plan.boundAccountFallbackPolicy,
961
1075
  reportSelection: (accountId, isActive) => {
962
1076
  proxyAccountId = accountId;
963
- _optionalChain([reportSelection, 'optionalCall', _15 => _15(accountId, isActive)]);
1077
+ _optionalChain([reportSelection, 'optionalCall', _28 => _28(accountId, isActive)]);
964
1078
  }
965
1079
  });
966
1080
  let rawStatus = null;
@@ -982,10 +1096,19 @@ async function runPipeline(anthropicBody, plan, reportSelection) {
982
1096
  },
983
1097
  fetchFn: (url, headers, body) => {
984
1098
  console.info(`[ProviderProxy:anthropic] -> ${url} model=${resolvedModel} stream=${isStream}`);
985
- return _chunkKCVJWJSTcjs.fetchUpstream.call(void 0,
1099
+ return _chunkRJUONLUQcjs.fetchUpstream.call(void 0,
986
1100
  url,
987
1101
  { method: "POST", headers, body: JSON.stringify(body) },
988
- { providerId: proxyProviderId(plan), accountId: proxyAccountId }
1102
+ {
1103
+ providerId: proxyProviderId(plan),
1104
+ accountId: proxyAccountId,
1105
+ routeActivity: plan.isSubscription ? {
1106
+ endpoint: "messages",
1107
+ sessionKey: plan.sessionKey,
1108
+ sessionSource: plan.sessionKey ? "content-fingerprint" : "none",
1109
+ model: resolvedModel
1110
+ } : void 0
1111
+ }
989
1112
  ).then((r) => {
990
1113
  rawStatus = r.status;
991
1114
  return r;
@@ -1009,10 +1132,11 @@ async function runSubscriptionSameFormatFetch(rawBody, plan, reportSelection, op
1009
1132
  reportSelection: (accountId, isActive, remapped) => {
1010
1133
  proxyAccountId = accountId;
1011
1134
  remappedModel = remapped;
1012
- _optionalChain([reportSelection, 'optionalCall', _16 => _16(accountId, isActive, remapped)]);
1135
+ _optionalChain([reportSelection, 'optionalCall', _29 => _29(accountId, isActive, remapped)]);
1013
1136
  }
1014
1137
  });
1015
- if (plan.isSubscription && plan.transformerProvider.name === "claude") {
1138
+ const isClaudeSubscription = plan.isSubscription && plan.transformerProvider.name === "claude";
1139
+ if (isClaudeSubscription) {
1016
1140
  _chunk6RPZADX3cjs.applyFingerprint.call(void 0,
1017
1141
  _chunkRPII3E6Zcjs.getSharedIdentityStore.call(void 0, ),
1018
1142
  headers,
@@ -1021,18 +1145,38 @@ async function runSubscriptionSameFormatFetch(rawBody, plan, reportSelection, op
1021
1145
  options.callerIdentity
1022
1146
  );
1023
1147
  }
1148
+ if (isClaudeSubscription) {
1149
+ _chunk3SCOLIPTcjs.fillMissingHeaders.call(void 0, headers, _nullishCoalesce(options.callerClientHeaders, () => ( {})));
1150
+ _chunk3SCOLIPTcjs.fillMissingHeaders.call(void 0, headers, DEFAULT_CLAUDE_CODE_HEADERS);
1151
+ }
1152
+ _chunk3SCOLIPTcjs.fillMissingHeaders.call(void 0, headers, { "anthropic-version": DEFAULT_ANTHROPIC_VERSION });
1153
+ headers["accept-encoding"] = FORCED_ACCEPT_ENCODING;
1024
1154
  const applyRemap = remappedModel !== void 0 && outboundRemapApplies(plan);
1025
1155
  const outboundBody = applyRemap ? rewriteBodyModel(rawBody, remappedModel) : rawBody;
1026
1156
  const outboundModel = applyRemap ? remappedModel : plan.resolvedModel;
1157
+ if (isClaudeSubscription) {
1158
+ headers["anthropic-beta"] = buildAnthropicBeta(outboundModel, options.callerAnthropicBeta);
1159
+ } else if (options.callerAnthropicBeta) {
1160
+ _chunk3SCOLIPTcjs.fillMissingHeaders.call(void 0, headers, { "anthropic-beta": options.callerAnthropicBeta });
1161
+ }
1027
1162
  console.info(
1028
1163
  `[ProviderProxy:anthropic] (subscription same-format) -> ${plan.upstreamUrl} model=${outboundModel} stream=${plan.isStream}`
1029
1164
  );
1030
- const response = await _chunkKCVJWJSTcjs.fetchUpstream.call(void 0,
1165
+ const response = await _chunkRJUONLUQcjs.fetchUpstream.call(void 0,
1031
1166
  plan.upstreamUrl,
1032
1167
  { method: "POST", headers, body: outboundBody },
1033
- { providerId: proxyProviderId(plan), accountId: proxyAccountId }
1168
+ {
1169
+ providerId: proxyProviderId(plan),
1170
+ accountId: proxyAccountId,
1171
+ routeActivity: plan.isSubscription ? {
1172
+ endpoint: "messages",
1173
+ sessionKey: plan.sessionKey,
1174
+ sessionSource: plan.sessionKey ? "content-fingerprint" : "none",
1175
+ model: outboundModel
1176
+ } : void 0
1177
+ }
1034
1178
  );
1035
- return { response, rawStatus: response.status };
1179
+ return { response, rawStatus: response.status, actualModel: outboundModel };
1036
1180
  }
1037
1181
  function outboundRemapApplies(plan) {
1038
1182
  return plan.isSubscription && plan.transformerProvider.name === "claude";
@@ -1043,7 +1187,7 @@ function rewriteBodyModel(rawBody, model) {
1043
1187
  if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return rawBody;
1044
1188
  parsed.model = model;
1045
1189
  return JSON.stringify(parsed);
1046
- } catch (e11) {
1190
+ } catch (e13) {
1047
1191
  return rawBody;
1048
1192
  }
1049
1193
  }
@@ -1056,7 +1200,7 @@ async function runSubscriptionAttemptWith401Retry(anthropicBody, relayBody, plan
1056
1200
  const first = await runOnce();
1057
1201
  let result = first;
1058
1202
  if (first.rawStatus === 401) {
1059
- const refreshed = await _optionalChain([plan, 'access', _17 => _17.auth, 'access', _18 => _18.onUnauthorized, 'optionalCall', _19 => _19(plan.sessionKey)]);
1203
+ const refreshed = await _optionalChain([plan, 'access', _30 => _30.auth, 'access', _31 => _31.onUnauthorized, 'optionalCall', _32 => _32(plan.sessionKey)]);
1060
1204
  if (refreshed) {
1061
1205
  console.info("[ProviderProxy:anthropic] 401 \u2192 token refreshed; retrying once");
1062
1206
  result = await runOnce();
@@ -1071,7 +1215,7 @@ async function readBoundedBody(response, max = 2048) {
1071
1215
  try {
1072
1216
  const text = await response.clone().text();
1073
1217
  return text.slice(0, max);
1074
- } catch (e12) {
1218
+ } catch (e14) {
1075
1219
  return void 0;
1076
1220
  }
1077
1221
  }
@@ -1079,9 +1223,9 @@ async function markSubscriptionHealth(plan, selectedAccountId, result) {
1079
1223
  if (!plan.isSubscription || selectedAccountId === void 0) return;
1080
1224
  const providerId = plan.transformerProvider.name;
1081
1225
  const status = result.rawStatus;
1082
- const { resetHeaderSeconds, retryAfterSeconds } = _chunkYK57NMIFcjs.resolveResetSeconds.call(void 0, providerId, result.response.headers);
1226
+ const { resetHeaderSeconds, retryAfterSeconds } = _chunkYWL7YZWScjs.resolveResetSeconds.call(void 0, providerId, result.response.headers);
1083
1227
  const bodyText = status === 403 ? await readBoundedBody(result.response) : void 0;
1084
- _chunkYK57NMIFcjs.getSharedAccountHealth.call(void 0, ).recordUpstreamOutcome(providerId, selectedAccountId, {
1228
+ _chunkYWL7YZWScjs.getSharedAccountHealth.call(void 0, ).recordUpstreamOutcome(providerId, selectedAccountId, {
1085
1229
  status,
1086
1230
  resetHeaderSeconds,
1087
1231
  retryAfterSeconds,
@@ -1099,7 +1243,7 @@ function breakerOutcome(rawStatus) {
1099
1243
  function recordBreakerOutcome(profile, modelId, rawStatus) {
1100
1244
  const outcome = breakerOutcome(rawStatus);
1101
1245
  if (outcome === "neutral") return;
1102
- _optionalChain([profile, 'optionalAccess', _20 => _20.recordModelOutcome, 'optionalCall', _21 => _21(modelId, outcome === "success")]);
1246
+ _optionalChain([profile, 'optionalAccess', _33 => _33.recordModelOutcome, 'optionalCall', _34 => _34(modelId, outcome === "success")]);
1103
1247
  }
1104
1248
  async function runSubscriptionAttemptOutcome(anthropicBody, relayBody, plan, options = {}) {
1105
1249
  try {
@@ -1118,7 +1262,7 @@ function settleOutcome(outcome) {
1118
1262
  async function runPipelineWithSubscriptionRetry(anthropicBody, rawBody, initialPlan, route, deps, options = {}) {
1119
1263
  const profile = route.subscriptionProfile;
1120
1264
  const scenario = initialPlan.scenario;
1121
- if (!_optionalChain([profile, 'optionalAccess', _22 => _22.nextFallback]) || scenario === void 0) {
1265
+ if (!_optionalChain([profile, 'optionalAccess', _35 => _35.nextFallback]) || scenario === void 0) {
1122
1266
  const loneOutcome = await runSubscriptionAttemptOutcome(anthropicBody, rawBody, initialPlan, options);
1123
1267
  recordBreakerOutcome(profile, initialPlan.resolvedModel, outcomeStatus(loneOutcome));
1124
1268
  return settleOutcome(loneOutcome);
@@ -1182,7 +1326,7 @@ async function handleAnthropicMessagesByo(res, rawBody, route, deps, options = {
1182
1326
  let anthropicBody;
1183
1327
  try {
1184
1328
  anthropicBody = JSON.parse(rawBody);
1185
- } catch (e13) {
1329
+ } catch (e15) {
1186
1330
  writeError(res, 400, "Invalid JSON in request body");
1187
1331
  return;
1188
1332
  }
@@ -1198,11 +1342,13 @@ async function handleAnthropicMessagesByo(res, rawBody, route, deps, options = {
1198
1342
  const upstreamSse = (_nullishCoalesce(upstreamResponse.headers.get("content-type"), () => ( ""))).includes(
1199
1343
  "text/event-stream"
1200
1344
  );
1345
+ const actualModel = "actualModel" in providerResponse && typeof providerResponse.actualModel === "string" ? providerResponse.actualModel : void 0;
1201
1346
  const usageAttribution = {
1202
1347
  sessionId: route.sessionId,
1203
1348
  providerId: _nullishCoalesce(route.providerId, () => ( "anthropic")),
1204
- model: plan.resolvedModel,
1349
+ model: _nullishCoalesce(actualModel, () => ( plan.resolvedModel)),
1205
1350
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
1351
+ routeLease: route.routeLease,
1206
1352
  // request-audit-log: correlate this request's tokens/cost to its audit record.
1207
1353
  auditResponse: res
1208
1354
  };
@@ -1212,7 +1358,7 @@ async function handleAnthropicMessagesByo(res, rawBody, route, deps, options = {
1212
1358
  extractUsage(event) {
1213
1359
  if (event["type"] === "message_start") {
1214
1360
  const msg = event["message"];
1215
- const u = _optionalChain([msg, 'optionalAccess', _23 => _23["usage"]]);
1361
+ const u = _optionalChain([msg, 'optionalAccess', _36 => _36["usage"]]);
1216
1362
  if (u) anthropicInputUsage = u;
1217
1363
  return null;
1218
1364
  }
@@ -1302,7 +1448,7 @@ async function buildByoPlan(res, route, deps, resolvedModel, isStream) {
1302
1448
  isSubscription: false,
1303
1449
  provider,
1304
1450
  apiKey,
1305
- extendedContextEnabled: _nullishCoalesce(_optionalChain([route, 'access', _24 => _24.anthropicSdkHints, 'optionalAccess', _25 => _25.extendedContext, 'optionalAccess', _26 => _26.enabled]), () => ( false))
1451
+ extendedContextEnabled: _nullishCoalesce(_optionalChain([route, 'access', _37 => _37.anthropicSdkHints, 'optionalAccess', _38 => _38.extendedContext, 'optionalAccess', _39 => _39.enabled]), () => ( false))
1306
1452
  };
1307
1453
  }
1308
1454
  async function runSameFormatFetch(bodyToSend, plan, options, keyOverride) {
@@ -1312,7 +1458,7 @@ async function runSameFormatFetch(bodyToSend, plan, options, keyOverride) {
1312
1458
  }
1313
1459
  const effectiveKey = _nullishCoalesce(_nullishCoalesce(keyOverride, () => ( apiKey)), () => ( ""));
1314
1460
  const headers = _chunkQ27JY5RGcjs.getProviderHeaders.call(void 0, provider, effectiveKey);
1315
- const callerBeta = _optionalChain([options, 'access', _27 => _27.callerAnthropicBeta, 'optionalAccess', _28 => _28.trim, 'call', _29 => _29()]);
1461
+ const callerBeta = _optionalChain([options, 'access', _40 => _40.callerAnthropicBeta, 'optionalAccess', _41 => _41.trim, 'call', _42 => _42()]);
1316
1462
  if (callerBeta) {
1317
1463
  const existing = _nullishCoalesce(headers["anthropic-beta"], () => ( headers["Anthropic-Beta"]));
1318
1464
  const parts = new Set(
@@ -1326,7 +1472,7 @@ async function runSameFormatFetch(bodyToSend, plan, options, keyOverride) {
1326
1472
  _chunkGBHYAGX3cjs.injectExtendedContextBeta.call(void 0, headers, resolvedModel, _nullishCoalesce(extendedContextEnabled, () => ( false)));
1327
1473
  const url = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: resolvedModel, stream: isStream });
1328
1474
  console.info(`[ProviderProxy:anthropic] (same-format) -> ${url} model=${resolvedModel} stream=${isStream}`);
1329
- const response = await _chunkKCVJWJSTcjs.fetchUpstream.call(void 0,
1475
+ const response = await _chunkRJUONLUQcjs.fetchUpstream.call(void 0,
1330
1476
  url,
1331
1477
  { method: "POST", headers, body: bodyToSend },
1332
1478
  { providerId: "byo" }
@@ -1338,7 +1484,7 @@ function resolveSameFormatBody(anthropicBody, rawBody, plan) {
1338
1484
  try {
1339
1485
  const m = JSON.parse(rawBody).model;
1340
1486
  originalModel = typeof m === "string" ? m : void 0;
1341
- } catch (e14) {
1487
+ } catch (e16) {
1342
1488
  originalModel = void 0;
1343
1489
  }
1344
1490
  return plan.resolvedModel !== originalModel ? JSON.stringify(anthropicBody) : rawBody;
@@ -1347,8 +1493,8 @@ async function runPipelineWithPoolReporting(anthropicBody, rawBody, plan, option
1347
1493
  const sameFormatBody = plan.sameFormat ? resolveSameFormatBody(anthropicBody, rawBody, plan) : rawBody;
1348
1494
  const runOnce = (keyOverride) => plan.sameFormat ? runSameFormatFetch(sameFormatBody, plan, options, keyOverride) : runPipeline(anthropicBody, plan);
1349
1495
  const first = await runOnce();
1350
- const outcome = await _optionalChain([plan, 'access', _30 => _30.auth, 'access', _31 => _31.onResult, 'optionalCall', _32 => _32(first.rawStatus)]);
1351
- if (_optionalChain([outcome, 'optionalAccess', _33 => _33.rebound])) {
1496
+ const outcome = await _optionalChain([plan, 'access', _43 => _43.auth, 'access', _44 => _44.onResult, 'optionalCall', _45 => _45(first.rawStatus)]);
1497
+ if (_optionalChain([outcome, 'optionalAccess', _46 => _46.rebound])) {
1352
1498
  console.info(
1353
1499
  "[ProviderProxy:anthropic] pool re-bound key after status",
1354
1500
  first.rawStatus,
@@ -1370,7 +1516,12 @@ async function handleAnthropicMessagesRequest(req, res, route, deps) {
1370
1516
  const callerBetaRaw = req.headers["anthropic-beta"];
1371
1517
  const callerAnthropicBeta = Array.isArray(callerBetaRaw) ? callerBetaRaw.join(",") : callerBetaRaw;
1372
1518
  const callerIdentity = _chunk6RPZADX3cjs.captureCallerIdentity.call(void 0, _chunkRPII3E6Zcjs.getSharedIdentityStore.call(void 0, ), req.headers);
1373
- await handleAnthropicMessagesByo(res, rawBody, route, deps, { callerAnthropicBeta, callerIdentity });
1519
+ const callerClientHeaders = extractClaudeClientHeaders(req.headers);
1520
+ await handleAnthropicMessagesByo(res, rawBody, route, deps, {
1521
+ callerAnthropicBeta,
1522
+ callerIdentity,
1523
+ callerClientHeaders
1524
+ });
1374
1525
  return;
1375
1526
  }
1376
1527
  const hints = route.anthropicSdkHints;
@@ -1456,6 +1607,10 @@ function emitGeminiUsageRecord(recorder, rawUsageMetadata, attribution) {
1456
1607
  providerId: attribution.providerId,
1457
1608
  model: attribution.model,
1458
1609
  apiKeyId: attribution.apiKeyId,
1610
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _47 => _47.routeLease, 'optionalAccess', _48 => _48.runId]), () => ( null)),
1611
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _49 => _49.routeLease, 'optionalAccess', _50 => _50.leaseId]), () => ( null)),
1612
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _51 => _51.routeLease, 'optionalAccess', _52 => _52.consumer]), () => ( null)),
1613
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _53 => _53.routeLease, 'optionalAccess', _54 => _54.stageId]), () => ( null)),
1459
1614
  auditResponse: attribution.auditResponse,
1460
1615
  engineOrigin: "codex-ingress",
1461
1616
  usage: tapped,
@@ -1470,13 +1625,13 @@ function recordGeminiNonStreamUsage(recorder, bodyText, attribution) {
1470
1625
  parsed.usageMetadata,
1471
1626
  attribution
1472
1627
  );
1473
- } catch (e15) {
1628
+ } catch (e17) {
1474
1629
  }
1475
1630
  }
1476
1631
  function recordGeminiStreamUsage(recorder, rawUsageMetadata, attribution) {
1477
1632
  try {
1478
1633
  emitGeminiUsageRecord(recorder, rawUsageMetadata, attribution);
1479
- } catch (e16) {
1634
+ } catch (e18) {
1480
1635
  }
1481
1636
  }
1482
1637
 
@@ -1485,14 +1640,14 @@ var GENERATE_CONTENT_ACTION = ":generateContent";
1485
1640
  var STREAM_GENERATE_CONTENT_ACTION = ":streamGenerateContent";
1486
1641
  function isGeminiGenerateContentRequest(method, url) {
1487
1642
  if (method !== "POST" || !url) return false;
1488
- const path = _nullishCoalesce(_optionalChain([url, 'access', _34 => _34.split, 'call', _35 => _35("?"), 'access', _36 => _36[0], 'optionalAccess', _37 => _37.replace, 'call', _38 => _38(/\/+$/, "")]), () => ( ""));
1643
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _55 => _55.split, 'call', _56 => _56("?"), 'access', _57 => _57[0], 'optionalAccess', _58 => _58.replace, 'call', _59 => _59(/\/+$/, "")]), () => ( ""));
1489
1644
  const lastSegment = _nullishCoalesce(path.split("/").pop(), () => ( ""));
1490
1645
  return lastSegment.includes(STREAM_GENERATE_CONTENT_ACTION) || lastSegment.includes(GENERATE_CONTENT_ACTION);
1491
1646
  }
1492
1647
  function isGeminiStreamRequest(url) {
1493
1648
  if (!url) return false;
1494
1649
  const [path, query = ""] = url.split("?");
1495
- const lastSegment = _nullishCoalesce((_nullishCoalesce(_optionalChain([path, 'optionalAccess', _39 => _39.replace, 'call', _40 => _40(/\/+$/, "")]), () => ( ""))).split("/").pop(), () => ( ""));
1650
+ const lastSegment = _nullishCoalesce((_nullishCoalesce(_optionalChain([path, 'optionalAccess', _60 => _60.replace, 'call', _61 => _61(/\/+$/, "")]), () => ( ""))).split("/").pop(), () => ( ""));
1496
1651
  if (lastSegment.includes(STREAM_GENERATE_CONTENT_ACTION)) return true;
1497
1652
  return /(^|&)alt=sse(&|$)/.test(query);
1498
1653
  }
@@ -1500,7 +1655,7 @@ async function handleGeminiGenerateContentRequest(res, rawBody, url, route, deps
1500
1655
  let geminiBody;
1501
1656
  try {
1502
1657
  geminiBody = JSON.parse(rawBody);
1503
- } catch (e17) {
1658
+ } catch (e19) {
1504
1659
  writeError(res, 400, "Invalid JSON in request body");
1505
1660
  return;
1506
1661
  }
@@ -1521,6 +1676,7 @@ async function handleGeminiGenerateContentRequest(res, rawBody, url, route, deps
1521
1676
  providerId: _nullishCoalesce(route.providerId, () => ( "gemini")),
1522
1677
  model: resolvedModel,
1523
1678
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
1679
+ routeLease: route.routeLease,
1524
1680
  // request-audit-log: correlate this request's tokens/cost to its audit record.
1525
1681
  auditResponse: res
1526
1682
  };
@@ -1622,7 +1778,7 @@ async function runPipeline2(geminiBody, plan) {
1622
1778
  },
1623
1779
  fetchFn: (url, headers, body) => {
1624
1780
  console.log(`[ProviderProxy:gemini] -> ${url} model=${resolvedModel} stream=${isStream}`);
1625
- return _chunkKCVJWJSTcjs.fetchUpstream.call(void 0,
1781
+ return _chunkRJUONLUQcjs.fetchUpstream.call(void 0,
1626
1782
  url,
1627
1783
  { method: "POST", headers, body: JSON.stringify(body) },
1628
1784
  { providerId: "byo" }
@@ -1637,8 +1793,8 @@ async function runPipeline2(geminiBody, plan) {
1637
1793
  }
1638
1794
  async function runPipelineWithPoolReporting2(geminiBody, plan) {
1639
1795
  const first = await runPipeline2(geminiBody, plan);
1640
- const outcome = await _optionalChain([plan, 'access', _41 => _41.auth, 'access', _42 => _42.onResult, 'optionalCall', _43 => _43(first.rawStatus)]);
1641
- if (_optionalChain([outcome, 'optionalAccess', _44 => _44.rebound])) {
1796
+ const outcome = await _optionalChain([plan, 'access', _62 => _62.auth, 'access', _63 => _63.onResult, 'optionalCall', _64 => _64(first.rawStatus)]);
1797
+ if (_optionalChain([outcome, 'optionalAccess', _65 => _65.rebound])) {
1642
1798
  console.log(
1643
1799
  "[ProviderProxy:gemini] pool re-bound key after status",
1644
1800
  first.rawStatus,
@@ -1677,6 +1833,10 @@ function emitChatCompletionsUsageRecord(recorder, rawUsage, attribution) {
1677
1833
  providerId: attribution.providerId,
1678
1834
  model: attribution.model,
1679
1835
  apiKeyId: attribution.apiKeyId,
1836
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _66 => _66.routeLease, 'optionalAccess', _67 => _67.runId]), () => ( null)),
1837
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _68 => _68.routeLease, 'optionalAccess', _69 => _69.leaseId]), () => ( null)),
1838
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _70 => _70.routeLease, 'optionalAccess', _71 => _71.consumer]), () => ( null)),
1839
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _72 => _72.routeLease, 'optionalAccess', _73 => _73.stageId]), () => ( null)),
1680
1840
  auditResponse: attribution.auditResponse,
1681
1841
  engineOrigin: "codex-ingress",
1682
1842
  usage: tapped,
@@ -1691,20 +1851,20 @@ function recordChatCompletionsNonStreamUsage(recorder, bodyText, attribution) {
1691
1851
  parsed.usage,
1692
1852
  attribution
1693
1853
  );
1694
- } catch (e18) {
1854
+ } catch (e20) {
1695
1855
  }
1696
1856
  }
1697
1857
  function recordChatCompletionsStreamUsage(recorder, rawUsage, attribution) {
1698
1858
  try {
1699
1859
  emitChatCompletionsUsageRecord(recorder, rawUsage, attribution);
1700
- } catch (e19) {
1860
+ } catch (e21) {
1701
1861
  }
1702
1862
  }
1703
1863
 
1704
1864
  // src/provider-proxy/ingress/openaiChatIngress.ts
1705
1865
  function isOpenAIChatRequest(method, url) {
1706
1866
  if (method !== "POST" || !url) return false;
1707
- const path = _nullishCoalesce(_optionalChain([url, 'access', _45 => _45.split, 'call', _46 => _46("?"), 'access', _47 => _47[0], 'optionalAccess', _48 => _48.replace, 'call', _49 => _49(/\/+$/, "")]), () => ( ""));
1867
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _74 => _74.split, 'call', _75 => _75("?"), 'access', _76 => _76[0], 'optionalAccess', _77 => _77.replace, 'call', _78 => _78(/\/+$/, "")]), () => ( ""));
1708
1868
  return path.endsWith("/chat/completions");
1709
1869
  }
1710
1870
  var CHAT_BRIDGE_SUBSCRIPTION_PROVIDERS = /* @__PURE__ */ new Set(["claude"]);
@@ -1713,7 +1873,7 @@ async function handleOpenAIChatRequest(res, rawBody, route, deps) {
1713
1873
  let chatBody;
1714
1874
  try {
1715
1875
  chatBody = JSON.parse(rawBody);
1716
- } catch (e20) {
1876
+ } catch (e22) {
1717
1877
  writeError(res, 400, "Invalid JSON in request body");
1718
1878
  return;
1719
1879
  }
@@ -1729,6 +1889,7 @@ async function handleOpenAIChatRequest(res, rawBody, route, deps) {
1729
1889
  providerId: _nullishCoalesce(route.providerId, () => ( "openai")),
1730
1890
  model: resolvedModel,
1731
1891
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
1892
+ routeLease: route.routeLease,
1732
1893
  // request-audit-log: correlate this request's tokens/cost to its audit record.
1733
1894
  auditResponse: res
1734
1895
  };
@@ -1826,7 +1987,7 @@ async function buildSubscriptionPlan2(res, route, deps, resolvedModel, isStream)
1826
1987
  );
1827
1988
  return null;
1828
1989
  }
1829
- const upstreamUrl = _optionalChain([profile, 'access', _50 => _50.resolveUpstreamUrl, 'optionalCall', _51 => _51(resolvedModel)]);
1990
+ const upstreamUrl = _optionalChain([profile, 'access', _79 => _79.resolveUpstreamUrl, 'optionalCall', _80 => _80(resolvedModel)]);
1830
1991
  if (!upstreamUrl) {
1831
1992
  writeError(res, 502, "Subscription profile is missing resolveUpstreamUrl");
1832
1993
  return null;
@@ -1891,7 +2052,7 @@ async function runPipeline3(chatBody, plan) {
1891
2052
  },
1892
2053
  fetchFn: (url, headers, body) => {
1893
2054
  console.log(`[ProviderProxy:chat] -> ${url} model=${resolvedModel} stream=${isStream}`);
1894
- return _chunkKCVJWJSTcjs.fetchUpstream.call(void 0,
2055
+ return _chunkRJUONLUQcjs.fetchUpstream.call(void 0,
1895
2056
  url,
1896
2057
  { method: "POST", headers, body: JSON.stringify(body) },
1897
2058
  { providerId: plan.proxyProviderId, accountId: proxyAccountId }
@@ -1906,8 +2067,8 @@ async function runPipeline3(chatBody, plan) {
1906
2067
  }
1907
2068
  async function runPipelineWithPoolReporting3(chatBody, plan) {
1908
2069
  const first = await runPipeline3(chatBody, plan);
1909
- const outcome = await _optionalChain([plan, 'access', _52 => _52.auth, 'access', _53 => _53.onResult, 'optionalCall', _54 => _54(first.rawStatus)]);
1910
- if (_optionalChain([outcome, 'optionalAccess', _55 => _55.rebound])) {
2070
+ const outcome = await _optionalChain([plan, 'access', _81 => _81.auth, 'access', _82 => _82.onResult, 'optionalCall', _83 => _83(first.rawStatus)]);
2071
+ if (_optionalChain([outcome, 'optionalAccess', _84 => _84.rebound])) {
1911
2072
  console.log(
1912
2073
  "[ProviderProxy:chat] pool re-bound key after status",
1913
2074
  first.rawStatus,
@@ -1920,7 +2081,7 @@ async function runPipelineWithPoolReporting3(chatBody, plan) {
1920
2081
  async function runPipelineWithSubscriptionRetry2(chatBody, plan) {
1921
2082
  const first = await runPipeline3(chatBody, plan);
1922
2083
  if (first.rawStatus !== 401) return first;
1923
- const refreshed = await _optionalChain([plan, 'access', _56 => _56.auth, 'access', _57 => _57.onUnauthorized, 'optionalCall', _58 => _58()]);
2084
+ const refreshed = await _optionalChain([plan, 'access', _85 => _85.auth, 'access', _86 => _86.onUnauthorized, 'optionalCall', _87 => _87()]);
1924
2085
  if (!refreshed) {
1925
2086
  console.warn("[ProviderProxy:chat] 401 not recoverable (onUnauthorized returned false)");
1926
2087
  return first;
@@ -1957,6 +2118,12 @@ function emitResponsesUsageRecord(recorder, rawUsage, attribution) {
1957
2118
  providerId: attribution.providerId,
1958
2119
  model: attribution.model,
1959
2120
  apiKeyId: attribution.apiKeyId,
2121
+ runId: _nullishCoalesce(_optionalChain([attribution, 'access', _88 => _88.routeLease, 'optionalAccess', _89 => _89.runId]), () => ( null)),
2122
+ routeLeaseId: _nullishCoalesce(_optionalChain([attribution, 'access', _90 => _90.routeLease, 'optionalAccess', _91 => _91.leaseId]), () => ( null)),
2123
+ routeLeaseConsumer: _nullishCoalesce(_optionalChain([attribution, 'access', _92 => _92.routeLease, 'optionalAccess', _93 => _93.consumer]), () => ( null)),
2124
+ routeLeaseStageId: _nullishCoalesce(_optionalChain([attribution, 'access', _94 => _94.routeLease, 'optionalAccess', _95 => _95.stageId]), () => ( null)),
2125
+ cacheKeySource: attribution.cacheKeySource,
2126
+ cacheKeyInjected: attribution.cacheKeyInjected,
1960
2127
  auditResponse: attribution.auditResponse,
1961
2128
  engineOrigin: "codex-ingress",
1962
2129
  usage: tapped,
@@ -1971,32 +2138,92 @@ function recordResponsesNonStreamUsage(recorder, bodyText, attribution) {
1971
2138
  parsed.usage,
1972
2139
  attribution
1973
2140
  );
1974
- } catch (e21) {
2141
+ } catch (e23) {
1975
2142
  }
1976
2143
  }
1977
2144
  function recordResponsesStreamUsage(recorder, rawUsage, attribution) {
1978
2145
  try {
1979
2146
  emitResponsesUsageRecord(recorder, rawUsage, attribution);
1980
- } catch (e22) {
2147
+ } catch (e24) {
1981
2148
  }
1982
2149
  }
1983
2150
 
2151
+ // src/provider-proxy/ingress/codexUsageLimitDetection.ts
2152
+ var PROVIDER_ID = "codex";
2153
+ var DEFAULT_QUOTA_COOLDOWN_MS = 5 * 6e4;
2154
+ var QUOTA_EXHAUSTED_PERCENT = 90;
2155
+ function parseTryAgainDeadline(bodyText) {
2156
+ const match = bodyText.match(/try again (?:at|on)\s+(.+?)(?:[.\n]|$)/i);
2157
+ if (!match) return void 0;
2158
+ const cleaned = match[1].replace(/(\d+)(st|nd|rd|th)/giu, "$1").trim();
2159
+ const ms = Date.parse(cleaned);
2160
+ return Number.isFinite(ms) ? ms : void 0;
2161
+ }
2162
+ function deadlineFromAllowanceSnapshot(accountId, now) {
2163
+ const snapshot = _chunkHVBBDBS3cjs.getSharedAccountAllowanceStore.call(void 0, ).get(PROVIDER_ID, accountId, now);
2164
+ if (!snapshot) return void 0;
2165
+ const candidates = snapshot.windows.filter(
2166
+ (w) => typeof w.usedPercent === "number" && w.usedPercent >= QUOTA_EXHAUSTED_PERCENT && typeof w.resetsAt === "string"
2167
+ ).map((w) => ({ ms: Date.parse(w.resetsAt), usedPercent: w.usedPercent })).filter((x) => Number.isFinite(x.ms) && x.ms > now).sort((a, b) => b.usedPercent - a.usedPercent);
2168
+ return _optionalChain([candidates, 'access', _96 => _96[0], 'optionalAccess', _97 => _97.ms]);
2169
+ }
2170
+ function resolveCodexQuotaDeadline(accountId, bodyText, now = Date.now()) {
2171
+ const parsed = parseTryAgainDeadline(bodyText);
2172
+ const parsedDeadline = parsed !== void 0 && parsed > now ? parsed : void 0;
2173
+ return _nullishCoalesce(_nullishCoalesce(deadlineFromAllowanceSnapshot(accountId, now), () => ( parsedDeadline)), () => ( now + DEFAULT_QUOTA_COOLDOWN_MS));
2174
+ }
2175
+ function markCodexUsageLimitExhaustion(accountId, bodyText, now = Date.now()) {
2176
+ if (!accountId) return;
2177
+ const deadline = resolveCodexQuotaDeadline(accountId, bodyText, now);
2178
+ _chunkYWL7YZWScjs.getSharedAccountHealth.call(void 0, ).markQuotaExhausted(PROVIDER_ID, accountId, deadline, now);
2179
+ }
2180
+
1984
2181
  // src/provider-proxy/ingress/openaiResponsesIngress.ts
2182
+ var SERVER_OVERLOADED_CODES = /* @__PURE__ */ new Set(["server_is_overloaded", "slow_down"]);
2183
+ function isCodexServerOverloadEvent(event) {
2184
+ if (event["type"] !== "response.failed") return false;
2185
+ const error = _optionalChain([event, 'access', _98 => _98["response"], 'optionalAccess', _99 => _99["error"]]);
2186
+ const code = _optionalChain([error, 'optionalAccess', _100 => _100["code"]]);
2187
+ return typeof code === "string" && SERVER_OVERLOADED_CODES.has(code);
2188
+ }
1985
2189
  function isOpenAIResponsesRequest(method, url) {
1986
2190
  if (method !== "POST" || !url) return false;
1987
- const path = _nullishCoalesce(_optionalChain([url, 'access', _59 => _59.split, 'call', _60 => _60("?"), 'access', _61 => _61[0], 'optionalAccess', _62 => _62.replace, 'call', _63 => _63(/\/+$/, "")]), () => ( ""));
2191
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _101 => _101.split, 'call', _102 => _102("?"), 'access', _103 => _103[0], 'optionalAccess', _104 => _104.replace, 'call', _105 => _105(/\/+$/, "")]), () => ( ""));
1988
2192
  return path.endsWith("/responses");
1989
2193
  }
2194
+ var SAFE_INJECTED_CACHE_KEY_SOURCES = /* @__PURE__ */ new Set([
2195
+ "session-header",
2196
+ "thread-header",
2197
+ "body-session-id",
2198
+ "body-thread-id",
2199
+ "content-fingerprint"
2200
+ ]);
2201
+ function isSafeInjectedCacheKeySource(source) {
2202
+ return SAFE_INJECTED_CACHE_KEY_SOURCES.has(source);
2203
+ }
2204
+ function ensureCodexPromptCacheKey(body, proxyProviderId2, derivedSession) {
2205
+ if (typeof body.prompt_cache_key === "string" && body.prompt_cache_key.trim()) {
2206
+ return { cacheKeySource: "client", cacheKeyInjected: false };
2207
+ }
2208
+ if (proxyProviderId2 !== "codex" || !isSafeInjectedCacheKeySource(derivedSession.source)) {
2209
+ return { cacheKeySource: "none", cacheKeyInjected: false };
2210
+ }
2211
+ body.prompt_cache_key = `omnicross:${derivedSession.source}:${derivedSession.key}`;
2212
+ return {
2213
+ cacheKeySource: derivedSession.source,
2214
+ cacheKeyInjected: true
2215
+ };
2216
+ }
1990
2217
  async function handleOpenAIResponsesRequest(res, rawBody, route, deps, requestHeaders = {}) {
1991
2218
  let responsesBody;
1992
2219
  try {
1993
2220
  responsesBody = JSON.parse(rawBody);
1994
- } catch (e23) {
2221
+ } catch (e25) {
1995
2222
  writeError(res, 400, "Invalid JSON in request body");
1996
2223
  return;
1997
2224
  }
1998
2225
  const isStream = responsesBody.stream === true;
1999
- const derivedSession = _chunkEJLH6NFTcjs.deriveGatewaySessionKey.call(void 0, responsesBody, requestHeaders, {
2226
+ const derivedSession = _chunkJ3QQ6JS5cjs.deriveGatewaySessionKey.call(void 0, responsesBody, requestHeaders, {
2000
2227
  endpoint: "responses",
2001
2228
  // A verified outbound key is the strongest route-local fallback. Resident
2002
2229
  // proxy callers without one still get a stable run/session bucket; the
@@ -2007,14 +2234,32 @@ async function handleOpenAIResponsesRequest(res, rawBody, route, deps, requestHe
2007
2234
  const resolvedModel = route.model;
2008
2235
  responsesBody.model = resolvedModel;
2009
2236
  try {
2010
- const plan = route.authMode === "subscription" ? await buildSubscriptionPlan3(res, route, deps, resolvedModel, isStream, sessionKey) : await buildByoPlan4(res, route, deps, resolvedModel, isStream);
2237
+ const plan = route.authMode === "subscription" ? await buildSubscriptionPlan3(
2238
+ res,
2239
+ route,
2240
+ deps,
2241
+ resolvedModel,
2242
+ isStream,
2243
+ sessionKey,
2244
+ derivedSession.source,
2245
+ requestHeaders
2246
+ ) : await buildByoPlan4(res, route, deps, resolvedModel, isStream);
2011
2247
  if (!plan) return;
2012
- const providerResponse = route.authMode === "subscription" ? await runPipelineWithSubscriptionRetry3(responsesBody, plan) : await runPipelineWithPoolReporting4(responsesBody, plan);
2248
+ const cacheKeyAttribution = ensureCodexPromptCacheKey(
2249
+ responsesBody,
2250
+ plan.proxyProviderId,
2251
+ derivedSession
2252
+ );
2253
+ let providerResponse = route.authMode === "subscription" ? await runPipelineWithSubscriptionRetry3(responsesBody, plan) : await runPipelineWithPoolReporting4(responsesBody, plan);
2254
+ providerResponse = await retryAroundCodexUsageLimit(responsesBody, plan, providerResponse);
2013
2255
  const usageAttribution = {
2014
2256
  sessionId: route.sessionId,
2015
2257
  providerId: _nullishCoalesce(route.providerId, () => ( "codex")),
2016
- model: resolvedModel,
2258
+ model: _nullishCoalesce(providerResponse.actualModel, () => ( resolvedModel)),
2017
2259
  apiKeyId: _nullishCoalesce(route.apiKeyId, () => ( null)),
2260
+ routeLease: route.routeLease,
2261
+ cacheKeySource: cacheKeyAttribution.cacheKeySource,
2262
+ cacheKeyInjected: cacheKeyAttribution.cacheKeyInjected,
2018
2263
  // request-audit-log: correlate this request's tokens/cost to its audit record.
2019
2264
  auditResponse: res
2020
2265
  };
@@ -2023,19 +2268,40 @@ async function handleOpenAIResponsesRequest(res, rawBody, route, deps, requestHe
2023
2268
  extractUsage(event) {
2024
2269
  if (event["type"] !== "response.completed") return null;
2025
2270
  const response = event["response"];
2026
- const usage = _optionalChain([response, 'optionalAccess', _64 => _64["usage"]]);
2271
+ const usage = _optionalChain([response, 'optionalAccess', _106 => _106["usage"]]);
2027
2272
  return _nullishCoalesce(usage, () => ( null));
2028
2273
  },
2029
2274
  onUsage(rawUsage) {
2030
2275
  recordResponsesStreamUsage(usageRecorder, rawUsage, usageAttribution);
2031
2276
  }
2032
2277
  } : void 0;
2278
+ const overloadActivityId = providerResponse.activityRecordId;
2279
+ const overloadAccountId = providerResponse.accountId;
2280
+ const overloadProviderId = plan.proxyProviderId;
2281
+ let overloadRecorded = false;
2282
+ const overloadDetector = (event) => {
2283
+ if (overloadRecorded || !isCodexServerOverloadEvent(event)) return;
2284
+ overloadRecorded = true;
2285
+ if (overloadActivityId) {
2286
+ _chunkNKVZ6Z3Ycjs.getSharedAccountRouteActivity.call(void 0, ).amend(overloadActivityId, {
2287
+ streamError: "server_overloaded"
2288
+ });
2289
+ }
2290
+ if (overloadAccountId) {
2291
+ _chunkOTZC4TNHcjs.getSharedOverloadCounter.call(void 0, ).recordOverload({
2292
+ providerId: overloadProviderId,
2293
+ accountId: overloadAccountId,
2294
+ endpoint: "responses"
2295
+ });
2296
+ }
2297
+ };
2033
2298
  const bodyText = await relayResponse(
2034
2299
  res,
2035
2300
  providerResponse.response,
2036
2301
  isStream,
2037
2302
  route.requestedModel,
2038
- usageTap
2303
+ usageTap,
2304
+ overloadDetector
2039
2305
  );
2040
2306
  if (bodyText && deps.usageRecorder) {
2041
2307
  recordResponsesNonStreamUsage(deps.usageRecorder, bodyText, usageAttribution);
@@ -2099,13 +2365,13 @@ async function buildByoPlan4(res, route, deps, resolvedModel, isStream) {
2099
2365
  proxyProviderId: "byo"
2100
2366
  };
2101
2367
  }
2102
- async function buildSubscriptionPlan3(res, route, deps, resolvedModel, isStream, sessionKey) {
2368
+ async function buildSubscriptionPlan3(res, route, deps, resolvedModel, isStream, sessionKey, sessionSource, requestHeaders) {
2103
2369
  const profile = route.subscriptionProfile;
2104
2370
  if (!profile) {
2105
2371
  writeError(res, 502, "Subscription mode requires a codex subscription profile");
2106
2372
  return null;
2107
2373
  }
2108
- const upstreamUrl = _optionalChain([profile, 'access', _65 => _65.resolveUpstreamUrl, 'optionalCall', _66 => _66(resolvedModel)]);
2374
+ const upstreamUrl = _optionalChain([profile, 'access', _107 => _107.resolveUpstreamUrl, 'optionalCall', _108 => _108(resolvedModel)]);
2109
2375
  if (!upstreamUrl) {
2110
2376
  writeError(res, 502, "Subscription profile is missing resolveUpstreamUrl");
2111
2377
  return null;
@@ -2142,6 +2408,7 @@ async function buildSubscriptionPlan3(res, route, deps, resolvedModel, isStream,
2142
2408
  return {
2143
2409
  auth,
2144
2410
  sessionKey,
2411
+ sessionSource,
2145
2412
  preferredAccountId: route.preferredAccountId,
2146
2413
  preferredAccountGroup: route.preferredAccountGroup,
2147
2414
  boundAccountFallbackPolicy: route.boundAccountFallbackPolicy,
@@ -2154,7 +2421,10 @@ async function buildSubscriptionPlan3(res, route, deps, resolvedModel, isStream,
2154
2421
  // profile's `resolveUpstreamUrl` for non-Code-Assist subscriptions.
2155
2422
  resolveUrl: (config) => config.url instanceof URL ? config.url.toString() : typeof config.url === "string" ? config.url : upstreamUrl,
2156
2423
  upstreamUrl,
2157
- proxyProviderId: profile.authStrategy.providerId
2424
+ proxyProviderId: profile.authStrategy.providerId,
2425
+ // Only the codex backend understands these; other subscription providers
2426
+ // (gemini Code Assist) share this relay and must not receive them.
2427
+ callerClientHeaders: profile.authStrategy.providerId === "codex" ? _chunk3SCOLIPTcjs.extractCodexClientHeaders.call(void 0, requestHeaders) : void 0
2158
2428
  };
2159
2429
  }
2160
2430
  async function resolveGeminiCodeAssistProject(profile, hints) {
@@ -2172,6 +2442,8 @@ async function runPipeline4(responsesBody, plan) {
2172
2442
  const endpointTransformer = getResponsesEndpointTransformer();
2173
2443
  const { auth, sessionKey, chain, transformerProvider, resolvedModel, isStream, resolveUrl, upstreamUrl } = plan;
2174
2444
  let proxyAccountId;
2445
+ let actualModel = resolvedModel;
2446
+ let activityRecordId;
2175
2447
  const authHeaders = {};
2176
2448
  await auth.applyHeaders(authHeaders, {
2177
2449
  upstreamUrl,
@@ -2180,10 +2452,13 @@ async function runPipeline4(responsesBody, plan) {
2180
2452
  preferredAccountId: plan.preferredAccountId,
2181
2453
  preferredAccountGroup: plan.preferredAccountGroup,
2182
2454
  boundAccountFallbackPolicy: plan.boundAccountFallbackPolicy,
2183
- reportSelection: (accountId) => {
2455
+ reportSelection: (accountId, _isActive, remappedModel) => {
2184
2456
  proxyAccountId = accountId;
2457
+ if (remappedModel) actualModel = remappedModel;
2185
2458
  }
2186
2459
  });
2460
+ responsesBody.model = actualModel;
2461
+ transformerProvider.models = [actualModel];
2187
2462
  let rawStatus = null;
2188
2463
  const { response } = await _chunkMUHWHQVMcjs.executeProviderCall.call(void 0, {
2189
2464
  executor,
@@ -2199,14 +2474,31 @@ async function runPipeline4(responsesBody, plan) {
2199
2474
  if (value !== void 0 && !(key in headers)) headers[key] = value;
2200
2475
  }
2201
2476
  }
2477
+ if (plan.proxyProviderId === "codex") {
2478
+ _chunk3SCOLIPTcjs.fillMissingHeaders.call(void 0, headers, _nullishCoalesce(plan.callerClientHeaders, () => ( {})));
2479
+ _chunk3SCOLIPTcjs.fillMissingHeaders.call(void 0, headers, _chunk3SCOLIPTcjs.DEFAULT_CODEX_CLI_HEADERS);
2480
+ _chunk3SCOLIPTcjs.fillMissingHeaders.call(void 0, headers, { accept: _chunk3SCOLIPTcjs.codexAcceptHeader.call(void 0, isStream) });
2481
+ }
2202
2482
  return headers;
2203
2483
  },
2204
2484
  fetchFn: (url, headers, body) => {
2205
- console.log(`[ProviderProxy:responses] -> ${url} model=${resolvedModel} stream=${isStream}`);
2206
- return _chunkKCVJWJSTcjs.fetchUpstream.call(void 0,
2485
+ console.log(`[ProviderProxy:responses] -> ${url} model=${actualModel} stream=${isStream}`);
2486
+ return _chunkRJUONLUQcjs.fetchUpstream.call(void 0,
2207
2487
  url,
2208
2488
  { method: "POST", headers, body: JSON.stringify(body) },
2209
- { providerId: plan.proxyProviderId, accountId: proxyAccountId }
2489
+ {
2490
+ providerId: plan.proxyProviderId,
2491
+ accountId: proxyAccountId,
2492
+ routeActivity: plan.proxyProviderId === "byo" ? void 0 : {
2493
+ endpoint: "responses",
2494
+ sessionKey,
2495
+ sessionSource: _nullishCoalesce(plan.sessionSource, () => ( "none")),
2496
+ model: actualModel,
2497
+ onRecorded: (record) => {
2498
+ activityRecordId = record.id;
2499
+ }
2500
+ }
2501
+ }
2210
2502
  ).then((r) => {
2211
2503
  rawStatus = r.status;
2212
2504
  return r;
@@ -2214,12 +2506,12 @@ async function runPipeline4(responsesBody, plan) {
2214
2506
  },
2215
2507
  runResponseChain: true
2216
2508
  });
2217
- return { response, rawStatus };
2509
+ return { response, rawStatus, accountId: proxyAccountId, actualModel, activityRecordId };
2218
2510
  }
2219
2511
  async function runPipelineWithPoolReporting4(responsesBody, plan) {
2220
2512
  const first = await runPipeline4(responsesBody, plan);
2221
- const outcome = await _optionalChain([plan, 'access', _67 => _67.auth, 'access', _68 => _68.onResult, 'optionalCall', _69 => _69(first.rawStatus)]);
2222
- if (_optionalChain([outcome, 'optionalAccess', _70 => _70.rebound])) {
2513
+ const outcome = await _optionalChain([plan, 'access', _109 => _109.auth, 'access', _110 => _110.onResult, 'optionalCall', _111 => _111(first.rawStatus)]);
2514
+ if (_optionalChain([outcome, 'optionalAccess', _112 => _112.rebound])) {
2223
2515
  console.log(
2224
2516
  "[ProviderProxy:responses] pool re-bound key after status",
2225
2517
  first.rawStatus,
@@ -2232,7 +2524,7 @@ async function runPipelineWithPoolReporting4(responsesBody, plan) {
2232
2524
  async function runPipelineWithSubscriptionRetry3(responsesBody, plan) {
2233
2525
  const first = await runPipeline4(responsesBody, plan);
2234
2526
  if (first.rawStatus !== 401) return first;
2235
- const refreshed = await _optionalChain([plan, 'access', _71 => _71.auth, 'access', _72 => _72.onUnauthorized, 'optionalCall', _73 => _73(plan.sessionKey)]);
2527
+ const refreshed = await _optionalChain([plan, 'access', _113 => _113.auth, 'access', _114 => _114.onUnauthorized, 'optionalCall', _115 => _115(plan.sessionKey)]);
2236
2528
  if (!refreshed) {
2237
2529
  console.warn("[ProviderProxy:responses] 401 not recoverable (onUnauthorized returned false)");
2238
2530
  return first;
@@ -2240,6 +2532,22 @@ async function runPipelineWithSubscriptionRetry3(responsesBody, plan) {
2240
2532
  console.log("[ProviderProxy:responses] 401 \u2192 token refreshed; retrying once");
2241
2533
  return runPipeline4(responsesBody, plan);
2242
2534
  }
2535
+ var MAX_QUOTA_RETRIES = 3;
2536
+ function isCodexAccountQuota429(plan, result) {
2537
+ return plan.proxyProviderId === "codex" && !!result.accountId && result.rawStatus === 429;
2538
+ }
2539
+ async function retryAroundCodexUsageLimit(responsesBody, plan, first, runAttempt = runPipelineWithSubscriptionRetry3) {
2540
+ if (!isCodexAccountQuota429(plan, first)) return first;
2541
+ let current = first;
2542
+ for (let attempt = 0; attempt < MAX_QUOTA_RETRIES; attempt++) {
2543
+ const errorBody = await current.response.text().catch(() => "");
2544
+ markCodexUsageLimitExhaustion(current.accountId, errorBody);
2545
+ const next = await runAttempt(responsesBody, plan);
2546
+ if (!isCodexAccountQuota429(plan, next)) return next;
2547
+ current = next;
2548
+ }
2549
+ return current;
2550
+ }
2243
2551
 
2244
2552
  // src/provider-proxy/providerProxyRouter.ts
2245
2553
  function extractRouteToken(authHeader) {
@@ -2254,7 +2562,7 @@ function resolveRouteToken(req) {
2254
2562
  if (fromAuth) return fromAuth;
2255
2563
  const googKey = req.headers["x-goog-api-key"];
2256
2564
  const value = Array.isArray(googKey) ? googKey[0] : googKey;
2257
- return _optionalChain([value, 'optionalAccess', _74 => _74.trim, 'call', _75 => _75()]) || void 0;
2565
+ return _optionalChain([value, 'optionalAccess', _116 => _116.trim, 'call', _117 => _117()]) || void 0;
2258
2566
  }
2259
2567
  async function routeRequest(req, res, routes, deps) {
2260
2568
  const token = resolveRouteToken(req);
@@ -2294,7 +2602,7 @@ function isLoopbackAddress(addr) {
2294
2602
  return normalized === "127.0.0.1" || normalized.startsWith("127.") || normalized === "::1" || addr === "::1";
2295
2603
  }
2296
2604
  var ProviderProxy = (_class2 = class {
2297
- constructor(deps, routes) {;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);
2605
+ constructor(deps, routes) {;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);
2298
2606
  this.deps = deps;
2299
2607
  this.routes = _nullishCoalesce(routes, () => ( new ProviderProxyRouteMap()));
2300
2608
  }
@@ -2302,6 +2610,7 @@ var ProviderProxy = (_class2 = class {
2302
2610
  __init2() {this.server = null}
2303
2611
  __init3() {this.port = 0}
2304
2612
 
2613
+ __init4() {this.beforeStop = /* @__PURE__ */ new Set()}
2305
2614
  /**
2306
2615
  * Start the resident listener on a stable port on 127.0.0.1. Idempotent —
2307
2616
  * a second `start()` returns the already-bound port.
@@ -2342,6 +2651,12 @@ var ProviderProxy = (_class2 = class {
2342
2651
  }
2343
2652
  /** Stop the listener, clear all routes, and release the port. */
2344
2653
  async stop() {
2654
+ for (const cleanup of [...this.beforeStop]) {
2655
+ try {
2656
+ await cleanup();
2657
+ } catch (e26) {
2658
+ }
2659
+ }
2345
2660
  this.routes.clear();
2346
2661
  const server = this.server;
2347
2662
  if (!server) return;
@@ -2358,6 +2673,15 @@ var ProviderProxy = (_class2 = class {
2358
2673
  getBaseUrl() {
2359
2674
  return `http://127.0.0.1:${this.port}`;
2360
2675
  }
2676
+ /** Register daemon-owned cleanup that must run before route-map teardown. */
2677
+ registerBeforeStop(cleanup) {
2678
+ this.beforeStop.add(cleanup);
2679
+ return () => this.beforeStop.delete(cleanup);
2680
+ }
2681
+ /** Whether the resident listener has completed binding. */
2682
+ isReady() {
2683
+ return this.server !== null && this.port > 0;
2684
+ }
2361
2685
  /**
2362
2686
  * The SHARED route map. Exposed so the outbound API server
2363
2687
  * (`outbound-api-server`) can mint per-request routes on the SAME map and
@@ -2374,19 +2698,819 @@ var ProviderProxy = (_class2 = class {
2374
2698
  getDeps() {
2375
2699
  return this.deps;
2376
2700
  }
2377
- /** Register a route for one run; returns the crypto route token (task 2.2). */
2378
- addRoute(context, idleMs) {
2379
- return this.routes.addRoute(context, idleMs);
2701
+ /** Register a route for one run; returns the crypto route token (task 2.2). */
2702
+ addRoute(context, idleMsOrOptions) {
2703
+ return this.routes.addRoute(context, idleMsOrOptions);
2704
+ }
2705
+ /** Re-arm one route while retaining the exact same bearer token. */
2706
+ renewRoute(token, idleMs) {
2707
+ return this.routes.renewRoute(token, idleMs);
2708
+ }
2709
+ /** Remove a route at run end. Returns true if an entry existed. */
2710
+ removeRoute(token) {
2711
+ return this.routes.removeRoute(token);
2712
+ }
2713
+ /** Live-route count (diagnostics / tests). */
2714
+ routeCount() {
2715
+ return this.routes.size();
2716
+ }
2717
+ }, _class2);
2718
+
2719
+ // src/provider-proxy/RouteLeaseManager.ts
2720
+
2721
+
2722
+ // src/provider-proxy/routeLeaseSchema.ts
2723
+
2724
+ var ROUTE_LEASE_REQUEST_SCHEMA = "omnicross.route-lease.request/1";
2725
+ var ROUTE_LEASE_RESULT_SCHEMA = "omnicross.route-lease/1";
2726
+ var ROUTE_LEASE_CAPABILITIES_SCHEMA = "omnicross.route-lease.capabilities/1";
2727
+ var ROUTE_LEASE_API_VERSION = 1;
2728
+ var ROUTE_LEASE_DEFAULT_TTL_SECONDS = 600;
2729
+ var ROUTE_LEASE_MAX_TTL_SECONDS = 3600;
2730
+ var ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES = 256;
2731
+ var ROUTE_LEASE_MAX_CONSUMER_BYTES = 64;
2732
+ var ROUTE_LEASE_MAX_EXECUTION_ID_BYTES = 128;
2733
+ var ROUTE_LEASE_MAX_SESSION_ID_BYTES = 512;
2734
+ var ROUTE_LEASE_MAX_MODEL_BYTES = 256;
2735
+ var ROUTE_LEASE_SESSION_HASH_DOMAIN = "omnicross.route-lease.session/v1";
2736
+ var ROUTE_LEASE_CODEX_TOKEN_ENV = "OMNICROSS_CODEX_ROUTE_TOKEN";
2737
+ var ROUTE_LEASE_RUNTIMES = ["claude", "codex"];
2738
+ var ROUTE_LEASE_CAPABILITIES = Object.freeze({
2739
+ schemaVersion: ROUTE_LEASE_CAPABILITIES_SCHEMA,
2740
+ runtimes: Object.freeze(["claude", "codex"]),
2741
+ upstreamKinds: Object.freeze(["provider", "account", "account-group", "account-pool"]),
2742
+ leaseApiVersion: ROUTE_LEASE_API_VERSION,
2743
+ codexAuthMode: "env_key",
2744
+ maxTtlSeconds: ROUTE_LEASE_MAX_TTL_SECONDS
2745
+ });
2746
+ var ROUTE_LEASE_RUNTIME_TABLE = Object.freeze({
2747
+ claude: Object.freeze({ endpoint: "messages", ingressFormat: "anthropic-messages", wirePath: "/v1/messages" }),
2748
+ codex: Object.freeze({ endpoint: "responses", ingressFormat: "openai-responses", wirePath: "/openai/responses" })
2749
+ });
2750
+ var ERROR_DEFAULTS = {
2751
+ invalid_request: { status: 400, retryable: false },
2752
+ runtime_unsupported: { status: 400, retryable: false },
2753
+ model_not_configured: { status: 400, retryable: false },
2754
+ format_unsupported: { status: 400, retryable: false },
2755
+ control_unauthorized: { status: 403, retryable: false },
2756
+ upstream_not_found: { status: 404, retryable: false },
2757
+ lease_not_found: { status: 404, retryable: false },
2758
+ idempotency_conflict: { status: 409, retryable: false },
2759
+ upstream_unavailable: { status: 409, retryable: true },
2760
+ lease_expired: { status: 410, retryable: false },
2761
+ upstream_exhausted: { status: 429, retryable: true },
2762
+ daemon_not_ready: { status: 503, retryable: true }
2763
+ };
2764
+ var RouteLeaseError = (_class3 = class extends Error {
2765
+ constructor(code, message, options = {}) {
2766
+ super(message, options.cause === void 0 ? void 0 : { cause: options.cause });_class3.prototype.__init5.call(this);;
2767
+ this.code = code;
2768
+ this.name = "RouteLeaseError";
2769
+ const defaults = ERROR_DEFAULTS[code];
2770
+ this.status = _nullishCoalesce(options.status, () => ( defaults.status));
2771
+ this.retryable = _nullishCoalesce(options.retryable, () => ( defaults.retryable));
2772
+ if (options.retryAfterSeconds !== void 0) {
2773
+ this.retryAfterSeconds = Math.max(1, Math.min(3600, Math.floor(options.retryAfterSeconds)));
2774
+ }
2775
+ }
2776
+
2777
+ __init5() {this.type = "route_lease_error"}
2778
+
2779
+
2780
+
2781
+ toResponse() {
2782
+ return { error: { type: this.type, code: this.code, message: this.message, retryable: this.retryable } };
2783
+ }
2784
+ }, _class3);
2785
+ var IDEMPOTENCY_RE = /^[A-Za-z0-9._:-]+$/u;
2786
+ var CONTROL_RE = /[\u0000-\u001f\u007f-\u009f]/u;
2787
+ var PROCESS_SESSION_HMAC_KEY = _crypto.randomBytes.call(void 0, 32);
2788
+ function isRecord(value) {
2789
+ return value !== null && typeof value === "object" && !Array.isArray(value);
2790
+ }
2791
+ function byteLength(value) {
2792
+ return Buffer.byteLength(value, "utf8");
2793
+ }
2794
+ function normalizedString(value, field, maxBytes, options = {}) {
2795
+ if (typeof value !== "string") throw invalid(`${field} must be a string`);
2796
+ const normalized = value.trim();
2797
+ if (!normalized || byteLength(normalized) > maxBytes || CONTROL_RE.test(normalized)) {
2798
+ throw invalid(`${field} is invalid`);
2799
+ }
2800
+ if (options.asciiPattern && !options.asciiPattern.test(normalized)) {
2801
+ throw invalid(`${field} is invalid`);
2802
+ }
2803
+ return normalized;
2804
+ }
2805
+ function optionalExecutionId(value, field) {
2806
+ return value === void 0 ? void 0 : normalizedString(value, field, ROUTE_LEASE_MAX_EXECUTION_ID_BYTES);
2807
+ }
2808
+ function invalid(message) {
2809
+ return new RouteLeaseError("invalid_request", message);
2810
+ }
2811
+ function normalizeRouteLeaseIdempotencyKey(value) {
2812
+ return normalizedString(value, "Idempotency-Key", ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES, {
2813
+ asciiPattern: IDEMPOTENCY_RE
2814
+ });
2815
+ }
2816
+ function normalizeRouteLeaseTtl(value) {
2817
+ if (value === void 0) return ROUTE_LEASE_DEFAULT_TTL_SECONDS;
2818
+ if (!Number.isInteger(value) || value < 1 || value > ROUTE_LEASE_MAX_TTL_SECONDS) {
2819
+ throw invalid(`ttlSeconds must be an integer from 1 to ${ROUTE_LEASE_MAX_TTL_SECONDS}`);
2820
+ }
2821
+ return value;
2822
+ }
2823
+ function normalizeUpstream(value) {
2824
+ if (!isRecord(value) || typeof value.kind !== "string") throw invalid("upstream is invalid");
2825
+ const providerId = normalizedString(value.providerId, "upstream.providerId", 128);
2826
+ switch (value.kind) {
2827
+ case "provider": {
2828
+ const keyId = value.keyId === void 0 ? void 0 : normalizedString(value.keyId, "upstream.keyId", 128);
2829
+ return keyId ? { kind: "provider", providerId, keyId } : { kind: "provider", providerId };
2830
+ }
2831
+ case "account":
2832
+ return {
2833
+ kind: "account",
2834
+ providerId,
2835
+ accountId: normalizedString(value.accountId, "upstream.accountId", 128)
2836
+ };
2837
+ case "account-group":
2838
+ return {
2839
+ kind: "account-group",
2840
+ providerId,
2841
+ group: normalizedString(value.group, "upstream.group", 128)
2842
+ };
2843
+ case "account-pool":
2844
+ return { kind: "account-pool", providerId };
2845
+ default:
2846
+ throw invalid("upstream kind is unsupported");
2847
+ }
2848
+ }
2849
+ function hashRouteLeaseSessionId(sessionId, hmacKey = PROCESS_SESSION_HMAC_KEY) {
2850
+ return _crypto.createHmac.call(void 0, "sha256", hmacKey).update(ROUTE_LEASE_SESSION_HASH_DOMAIN, "utf8").update("\0", "utf8").update(sessionId, "utf8").digest("hex");
2851
+ }
2852
+ function canonicalizeRouteLeasePayload(value) {
2853
+ if (value === null || typeof value !== "object") return JSON.stringify(value);
2854
+ if (Array.isArray(value)) {
2855
+ return `[${value.map((entry) => canonicalizeRouteLeasePayload(entry)).join(",")}]`;
2856
+ }
2857
+ const record = value;
2858
+ const fields = Object.keys(record).filter((key) => record[key] !== void 0).sort().map((key) => `${JSON.stringify(key)}:${canonicalizeRouteLeasePayload(record[key])}`);
2859
+ return `{${fields.join(",")}}`;
2860
+ }
2861
+ function hashRouteLeasePayload(value) {
2862
+ return _crypto.createHash.call(void 0, "sha256").update(canonicalizeRouteLeasePayload(value), "utf8").digest("hex");
2863
+ }
2864
+ function parseRouteLeaseCreate(raw, idempotencyHeader, hmacKey = PROCESS_SESSION_HMAC_KEY) {
2865
+ if (!isRecord(raw)) throw invalid("request body must be a JSON object");
2866
+ if (raw.schemaVersion !== ROUTE_LEASE_REQUEST_SCHEMA) {
2867
+ throw invalid(`schemaVersion must be ${ROUTE_LEASE_REQUEST_SCHEMA}`);
2868
+ }
2869
+ const consumer = normalizedString(raw.consumer, "consumer", ROUTE_LEASE_MAX_CONSUMER_BYTES);
2870
+ const runtime = raw.runtime;
2871
+ if (runtime !== "claude" && runtime !== "codex") {
2872
+ throw new RouteLeaseError("runtime_unsupported", "runtime is unsupported");
2873
+ }
2874
+ const upstream = normalizeUpstream(raw.upstream);
2875
+ if (typeof raw.model !== "string" || raw.model.trim().length === 0) {
2876
+ throw new RouteLeaseError("model_not_configured", "model is not configured");
2877
+ }
2878
+ const model = normalizedString(raw.model, "model", ROUTE_LEASE_MAX_MODEL_BYTES);
2879
+ const ttlSeconds = normalizeRouteLeaseTtl(raw.ttlSeconds);
2880
+ const idempotencyKey = normalizeRouteLeaseIdempotencyKey(idempotencyHeader);
2881
+ let execution;
2882
+ let semanticExecution;
2883
+ if (raw.execution !== void 0) {
2884
+ if (!isRecord(raw.execution)) throw invalid("execution must be a JSON object");
2885
+ const runId = optionalExecutionId(raw.execution.runId, "execution.runId");
2886
+ const stageId = optionalExecutionId(raw.execution.stageId, "execution.stageId");
2887
+ let attempt;
2888
+ if (raw.execution.attempt !== void 0) {
2889
+ if (!Number.isSafeInteger(raw.execution.attempt) || raw.execution.attempt < 1) {
2890
+ throw invalid("execution.attempt must be a positive integer");
2891
+ }
2892
+ attempt = raw.execution.attempt;
2893
+ }
2894
+ let sessionId;
2895
+ let sessionIdHash;
2896
+ if (raw.execution.sessionId !== void 0) {
2897
+ sessionId = normalizedString(raw.execution.sessionId, "execution.sessionId", ROUTE_LEASE_MAX_SESSION_ID_BYTES);
2898
+ sessionIdHash = hashRouteLeaseSessionId(sessionId, hmacKey);
2899
+ }
2900
+ if (runId !== void 0 || stageId !== void 0 || attempt !== void 0 || sessionIdHash !== void 0) {
2901
+ execution = { runId, stageId, attempt, sessionIdHash };
2902
+ semanticExecution = { runId, stageId, attempt, sessionId };
2903
+ }
2904
+ }
2905
+ const request = {
2906
+ schemaVersion: ROUTE_LEASE_REQUEST_SCHEMA,
2907
+ consumer,
2908
+ runtime,
2909
+ upstream,
2910
+ model,
2911
+ ...execution ? { execution } : {},
2912
+ ttlSeconds
2913
+ };
2914
+ const semanticPayload = {
2915
+ schemaVersion: request.schemaVersion,
2916
+ consumer,
2917
+ runtime,
2918
+ upstream,
2919
+ model,
2920
+ ...semanticExecution ? { execution: semanticExecution } : {},
2921
+ ttlSeconds
2922
+ };
2923
+ return { request, idempotencyKey, payloadHash: hashRouteLeasePayload(semanticPayload) };
2924
+ }
2925
+ function routeLeaseRuntime(runtime) {
2926
+ switch (runtime) {
2927
+ case "claude":
2928
+ return ROUTE_LEASE_RUNTIME_TABLE.claude;
2929
+ case "codex":
2930
+ return ROUTE_LEASE_RUNTIME_TABLE.codex;
2931
+ default: {
2932
+ const exhaustive = runtime;
2933
+ throw new RouteLeaseError("runtime_unsupported", `runtime is unsupported: ${String(exhaustive)}`);
2934
+ }
2935
+ }
2936
+ }
2937
+
2938
+ // src/provider-proxy/RouteLeaseManager.ts
2939
+ var SYSTEM_ROUTE_LEASE_CLOCK = {
2940
+ now: () => Date.now(),
2941
+ setTimeout(callback, delayMs) {
2942
+ const timer = setTimeout(callback, delayMs);
2943
+ _optionalChain([timer, 'access', _118 => _118.unref, 'optionalCall', _119 => _119()]);
2944
+ return timer;
2945
+ },
2946
+ clearTimeout(handle) {
2947
+ clearTimeout(handle);
2948
+ }
2949
+ };
2950
+ function cloneUpstream(upstream) {
2951
+ return { ...upstream };
2952
+ }
2953
+ function metadataCopy(metadata) {
2954
+ return {
2955
+ ...metadata,
2956
+ upstream: cloneUpstream(metadata.upstream),
2957
+ ...metadata.execution ? { execution: { ...metadata.execution } } : {}
2958
+ };
2959
+ }
2960
+ var RouteLeaseManager = (_class4 = class {
2961
+ constructor(routePort, targetResolver, descriptors, options = {}) {;_class4.prototype.__init6.call(this);_class4.prototype.__init7.call(this);_class4.prototype.__init8.call(this);_class4.prototype.__init9.call(this);_class4.prototype.__init10.call(this);
2962
+ this.routePort = routePort;
2963
+ this.targetResolver = targetResolver;
2964
+ this.descriptors = descriptors;
2965
+ this.clock = _nullishCoalesce(options.clock, () => ( SYSTEM_ROUTE_LEASE_CLOCK));
2966
+ this.hmacKey = _nullishCoalesce(options.hmacKey, () => ( _crypto.randomBytes.call(void 0, 32)));
2967
+ this.tombstoneMaxCount = Math.max(1, _nullishCoalesce(options.tombstoneMaxCount, () => ( 512)));
2968
+ this.tombstoneMaxAgeMs = Math.max(1, _nullishCoalesce(options.tombstoneMaxAgeMs, () => ( 60 * 60 * 1e3)));
2969
+ this.logger = options.logger;
2970
+ this.afterPublish = options.afterPublish;
2971
+ }
2972
+
2973
+
2974
+
2975
+ __init6() {this.active = /* @__PURE__ */ new Map()}
2976
+ __init7() {this.idempotency = /* @__PURE__ */ new Map()}
2977
+ __init8() {this.pendingCreates = /* @__PURE__ */ new Map()}
2978
+ __init9() {this.tombstones = /* @__PURE__ */ new Map()}
2979
+
2980
+
2981
+
2982
+
2983
+
2984
+
2985
+ __init10() {this.closed = false}
2986
+ parse(raw, idempotencyKey) {
2987
+ return parseRouteLeaseCreate(raw, idempotencyKey, this.hmacKey);
2988
+ }
2989
+ async createFromRequest(raw, idempotencyKey) {
2990
+ return this.create(this.parse(raw, idempotencyKey));
2991
+ }
2992
+ async create(input) {
2993
+ if (this.closed) throw new RouteLeaseError("daemon_not_ready", "route lease manager is shutting down");
2994
+ const request = input.request;
2995
+ const scope = `${request.consumer}\0${input.idempotencyKey}`;
2996
+ const existing = this.idempotency.get(scope);
2997
+ if (existing) {
2998
+ const lease = this.active.get(existing.leaseId);
2999
+ if (!lease) {
3000
+ this.idempotency.delete(scope);
3001
+ } else if (existing.payloadHash !== input.payloadHash) {
3002
+ throw new RouteLeaseError("idempotency_conflict", "idempotency key is already used for a different live request");
3003
+ } else {
3004
+ return { created: false, result: lease.result };
3005
+ }
3006
+ }
3007
+ const pending = this.pendingCreates.get(scope);
3008
+ if (pending) {
3009
+ if (pending.payloadHash !== input.payloadHash) {
3010
+ throw new RouteLeaseError("idempotency_conflict", "idempotency key is already used for a different live request");
3011
+ }
3012
+ const outcome2 = await pending.outcome;
3013
+ return { created: false, result: outcome2.result };
3014
+ }
3015
+ const outcome = this.createNew(input, scope);
3016
+ const pendingCreate = { payloadHash: input.payloadHash, outcome };
3017
+ this.pendingCreates.set(scope, pendingCreate);
3018
+ try {
3019
+ return await outcome;
3020
+ } finally {
3021
+ if (this.pendingCreates.get(scope) === pendingCreate) this.pendingCreates.delete(scope);
3022
+ }
3023
+ }
3024
+ async createNew(input, scope) {
3025
+ const request = input.request;
3026
+ if (!this.routePort.isReady()) {
3027
+ throw new RouteLeaseError("daemon_not_ready", "resident provider proxy is not ready");
3028
+ }
3029
+ if (!this.descriptors.has(request.runtime)) {
3030
+ throw new RouteLeaseError("runtime_unsupported", "runtime launch adapter is unavailable");
3031
+ }
3032
+ const leaseId = _crypto.randomUUID.call(void 0, );
3033
+ const resolved = await this.targetResolver.resolve(request);
3034
+ this.assertOpen();
3035
+ const route = {
3036
+ ...resolved,
3037
+ sessionId: _nullishCoalesce(_optionalChain([request, 'access', _120 => _120.execution, 'optionalAccess', _121 => _121.sessionIdHash]), () => ( null)),
3038
+ routeLease: {
3039
+ leaseId,
3040
+ consumer: request.consumer,
3041
+ ..._optionalChain([request, 'access', _122 => _122.execution, 'optionalAccess', _123 => _123.runId]) ? { runId: request.execution.runId } : {},
3042
+ ..._optionalChain([request, 'access', _124 => _124.execution, 'optionalAccess', _125 => _125.stageId]) ? { stageId: request.execution.stageId } : {}
3043
+ }
3044
+ };
3045
+ const now = this.clock.now();
3046
+ const ttlMs = request.ttlSeconds * 1e3;
3047
+ const expiresAtMs = now + ttlMs;
3048
+ let routeToken;
3049
+ let published = false;
3050
+ let activeLease;
3051
+ try {
3052
+ routeToken = this.routePort.addRoute(route, {
3053
+ idleMs: ttlMs,
3054
+ onActivity: (at) => this.noteActivity(leaseId, at),
3055
+ onEvicted: () => this.onRouteEvicted(leaseId)
3056
+ });
3057
+ const launch = this.descriptors.build(request.runtime, {
3058
+ proxyBaseUrl: this.routePort.getBaseUrl(),
3059
+ model: request.model,
3060
+ routeToken
3061
+ });
3062
+ this.assertOpen();
3063
+ const metadata = {
3064
+ leaseId,
3065
+ consumer: request.consumer,
3066
+ runtime: request.runtime,
3067
+ upstream: cloneUpstream(request.upstream),
3068
+ model: request.model,
3069
+ createdAt: new Date(now).toISOString(),
3070
+ expiresAt: new Date(expiresAtMs).toISOString(),
3071
+ status: "active",
3072
+ ...request.execution ? { execution: { ...request.execution } } : {}
3073
+ };
3074
+ const result = {
3075
+ schemaVersion: ROUTE_LEASE_RESULT_SCHEMA,
3076
+ ...metadata,
3077
+ status: "active",
3078
+ launch: { env: { ...launch.env }, extraArgs: [...launch.extraArgs] }
3079
+ };
3080
+ const timer = this.clock.setTimeout(() => this.expire(leaseId), ttlMs);
3081
+ activeLease = {
3082
+ leaseId,
3083
+ routeToken,
3084
+ idempotencyScope: scope,
3085
+ payloadHash: input.payloadHash,
3086
+ request,
3087
+ route,
3088
+ result,
3089
+ metadata,
3090
+ expiresAtMs,
3091
+ timer
3092
+ };
3093
+ this.assertOpen();
3094
+ this.active.set(leaseId, activeLease);
3095
+ this.idempotency.set(scope, { leaseId, payloadHash: input.payloadHash });
3096
+ published = true;
3097
+ _optionalChain([this, 'access', _126 => _126.afterPublish, 'optionalCall', _127 => _127()]);
3098
+ if (this.closed) {
3099
+ this.finalize(activeLease, "released");
3100
+ throw new RouteLeaseError("daemon_not_ready", "route lease manager is shutting down");
3101
+ }
3102
+ return { created: true, result };
3103
+ } catch (error) {
3104
+ if (published && activeLease) {
3105
+ this.active.delete(leaseId);
3106
+ this.idempotency.delete(scope);
3107
+ this.clock.clearTimeout(activeLease.timer);
3108
+ }
3109
+ if (routeToken) this.safeRemoveRoute(routeToken, leaseId);
3110
+ if (error instanceof RouteLeaseError) throw error;
3111
+ throw new RouteLeaseError("upstream_unavailable", "route lease creation failed safely", { cause: error });
3112
+ }
3113
+ }
3114
+ list() {
3115
+ this.pruneTombstones();
3116
+ return [
3117
+ ...[...this.active.values()].map((lease) => metadataCopy(lease.metadata)),
3118
+ ...[...this.tombstones.values()].map((entry) => metadataCopy(entry.metadata))
3119
+ ].sort((left, right) => left.createdAt.localeCompare(right.createdAt));
3120
+ }
3121
+ get(leaseId) {
3122
+ const active = this.active.get(leaseId);
3123
+ if (active) return metadataCopy(active.metadata);
3124
+ this.pruneTombstones();
3125
+ const tombstone = this.tombstones.get(leaseId);
3126
+ if (tombstone) return metadataCopy(tombstone.metadata);
3127
+ throw new RouteLeaseError("lease_not_found", "route lease was not found");
3128
+ }
3129
+ renew(leaseId, ttlSeconds) {
3130
+ const lease = this.active.get(leaseId);
3131
+ if (!lease) {
3132
+ this.pruneTombstones();
3133
+ const known = this.tombstones.get(leaseId);
3134
+ if (_optionalChain([known, 'optionalAccess', _128 => _128.metadata, 'access', _129 => _129.status]) === "expired") {
3135
+ throw new RouteLeaseError("lease_expired", "route lease has expired");
3136
+ }
3137
+ throw new RouteLeaseError("lease_not_found", "route lease was not found");
3138
+ }
3139
+ const now = this.clock.now();
3140
+ if (now >= lease.expiresAtMs) {
3141
+ this.finalize(lease, "expired");
3142
+ throw new RouteLeaseError("lease_expired", "route lease has expired");
3143
+ }
3144
+ const ttlMs = ttlSeconds * 1e3;
3145
+ const expiresAtMs = now + ttlMs;
3146
+ const nextTimer = this.clock.setTimeout(() => this.expire(leaseId), ttlMs);
3147
+ const previousTimer = lease.timer;
3148
+ lease.timer = nextTimer;
3149
+ lease.expiresAtMs = expiresAtMs;
3150
+ lease.metadata = { ...lease.metadata, expiresAt: new Date(expiresAtMs).toISOString() };
3151
+ this.clock.clearTimeout(previousTimer);
3152
+ if (!this.routePort.renewRoute(lease.routeToken, ttlMs)) {
3153
+ this.finalize(lease, "expired", true);
3154
+ throw new RouteLeaseError("lease_expired", "route lease route has expired");
3155
+ }
3156
+ return { leaseId, expiresAt: lease.metadata.expiresAt, status: "active" };
3157
+ }
3158
+ release(leaseId) {
3159
+ const lease = this.active.get(leaseId);
3160
+ if (!lease) return { leaseId, released: false };
3161
+ this.finalize(lease, "released");
3162
+ return { leaseId, released: true };
3163
+ }
3164
+ shutdown() {
3165
+ if (this.closed) return;
3166
+ this.closed = true;
3167
+ for (const lease of [...this.active.values()]) {
3168
+ try {
3169
+ this.finalize(lease, "released");
3170
+ } catch (error) {
3171
+ _optionalChain([this, 'access', _130 => _130.logger, 'optionalAccess', _131 => _131.warn, 'call', _132 => _132("route lease cleanup failed during shutdown", { leaseId: lease.leaseId, error: safeErrorName(error) })]);
3172
+ }
3173
+ }
3174
+ }
3175
+ activeCount() {
3176
+ return this.active.size;
3177
+ }
3178
+ assertOpen() {
3179
+ if (this.closed) {
3180
+ throw new RouteLeaseError("daemon_not_ready", "route lease manager is shutting down");
3181
+ }
3182
+ }
3183
+ noteActivity(leaseId, at) {
3184
+ const lease = this.active.get(leaseId);
3185
+ if (!lease || at >= lease.expiresAtMs) return;
3186
+ lease.metadata = { ...lease.metadata, lastActivityAt: new Date(at).toISOString() };
3187
+ }
3188
+ onRouteEvicted(leaseId) {
3189
+ const lease = this.active.get(leaseId);
3190
+ if (lease) this.finalize(lease, "expired", true);
3191
+ }
3192
+ expire(leaseId) {
3193
+ const lease = this.active.get(leaseId);
3194
+ if (!lease) return;
3195
+ const remaining = lease.expiresAtMs - this.clock.now();
3196
+ if (remaining > 0) {
3197
+ lease.timer = this.clock.setTimeout(() => this.expire(leaseId), remaining);
3198
+ return;
3199
+ }
3200
+ this.finalize(lease, "expired");
3201
+ }
3202
+ finalize(lease, status, routeAlreadyGone = false) {
3203
+ if (this.active.get(lease.leaseId) !== lease) return;
3204
+ this.active.delete(lease.leaseId);
3205
+ this.idempotency.delete(lease.idempotencyScope);
3206
+ this.clock.clearTimeout(lease.timer);
3207
+ if (!routeAlreadyGone) this.safeRemoveRoute(lease.routeToken, lease.leaseId);
3208
+ const metadata = { ...lease.metadata, status };
3209
+ this.tombstones.delete(lease.leaseId);
3210
+ this.tombstones.set(lease.leaseId, { metadata, terminalAtMs: this.clock.now() });
3211
+ this.pruneTombstones();
3212
+ }
3213
+ safeRemoveRoute(routeToken, leaseId) {
3214
+ try {
3215
+ this.routePort.removeRoute(routeToken);
3216
+ } catch (error) {
3217
+ _optionalChain([this, 'access', _133 => _133.logger, 'optionalAccess', _134 => _134.warn, 'call', _135 => _135("route lease route cleanup failed", { leaseId, error: safeErrorName(error) })]);
3218
+ }
3219
+ }
3220
+ pruneTombstones() {
3221
+ const cutoff = this.clock.now() - this.tombstoneMaxAgeMs;
3222
+ for (const [leaseId, tombstone] of this.tombstones) {
3223
+ if (tombstone.terminalAtMs < cutoff) this.tombstones.delete(leaseId);
3224
+ }
3225
+ while (this.tombstones.size > this.tombstoneMaxCount) {
3226
+ const oldest = this.tombstones.keys().next().value;
3227
+ if (!oldest) break;
3228
+ this.tombstones.delete(oldest);
3229
+ }
3230
+ }
3231
+ }, _class4);
3232
+ function safeErrorName(error) {
3233
+ return error instanceof Error ? error.name : "unknown";
3234
+ }
3235
+
3236
+ // src/outbound-api/gatewayBindingResolver.ts
3237
+ var MESSAGE_FALLBACK_KINDS = ["sonnet", "opus", "haiku", "fable"];
3238
+ function nonBlank(value) {
3239
+ return typeof value === "string" && value.trim() !== "";
3240
+ }
3241
+ function gatewayBindingAllowsKey(binding, apiKeyId) {
3242
+ const scope = _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _136 => _136.apiKeyIds, 'optionalAccess', _137 => _137.length]) ? "selected" : "all")));
3243
+ return scope === "all" || Boolean(_optionalChain([binding, 'access', _138 => _138.apiKeyIds, 'optionalAccess', _139 => _139.includes, 'call', _140 => _140(apiKeyId)]));
3244
+ }
3245
+ function wildcardMatches(pattern, value) {
3246
+ const escaped = pattern.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")).join(".*");
3247
+ return new RegExp(`^${escaped}$`, "iu").test(value);
3248
+ }
3249
+ function resolveGatewayModelMapping(mappings, requestedModel) {
3250
+ if (!nonBlank(requestedModel)) return void 0;
3251
+ const wanted = requestedModel.trim();
3252
+ const exact = (_nullishCoalesce(mappings, () => ( []))).find(
3253
+ (mapping) => nonBlank(mapping.source) && !mapping.source.includes("*") && mapping.source.trim().toLocaleLowerCase() === wanted.toLocaleLowerCase()
3254
+ );
3255
+ if (exact && nonBlank(exact.target)) return exact.target.trim();
3256
+ const wildcard = (_nullishCoalesce(mappings, () => ( []))).find(
3257
+ (mapping) => nonBlank(mapping.source) && mapping.source.includes("*") && wildcardMatches(mapping.source.trim(), wanted)
3258
+ );
3259
+ return wildcard && nonBlank(wildcard.target) ? wildcard.target.trim() : void 0;
3260
+ }
3261
+ function routeCanServe(binding, requestedModel, role) {
3262
+ if (binding.modelMode === "passthrough") return nonBlank(requestedModel);
3263
+ if (_optionalChain([binding, 'access', _141 => _141.modelMappings, 'optionalAccess', _142 => _142.length])) {
3264
+ return nonBlank(resolveGatewayModelMapping(binding.modelMappings, requestedModel));
3265
+ }
3266
+ if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3267
+ const map = _nullishCoalesce(binding.modelMap, () => ( {}));
3268
+ const kind = _chunk4VZX5F4Tcjs.detectModelKind.call(void 0, binding.endpoint, requestedModel);
3269
+ if (kind && nonBlank(map[kind])) return true;
3270
+ if (binding.endpoint === "messages") {
3271
+ return MESSAGE_FALLBACK_KINDS.some((candidate) => nonBlank(map[candidate]));
3272
+ }
3273
+ return nonBlank(map.codex);
3274
+ }
3275
+ if (binding.endpoint === "chat" && binding.dispatchMode === "prefix") {
3276
+ return _chunk4VZX5F4Tcjs.resolvePrefixTarget.call(void 0, binding.prefixTargets, requestedModel) !== null;
3277
+ }
3278
+ if (binding.endpoint === "chat") {
3279
+ if (!requestedModel) return false;
3280
+ const wanted = requestedModel.trim().toLowerCase();
3281
+ return (_nullishCoalesce(binding.models, () => ( []))).some((ref) => _optionalChain([modelIdOf, 'call', _143 => _143(ref), 'optionalAccess', _144 => _144.toLowerCase, 'call', _145 => _145()]) === wanted);
3282
+ }
3283
+ return role === "background" ? nonBlank(binding.backgroundModel) : nonBlank(binding.defaultModel);
3284
+ }
3285
+ function modelIdOf(ref) {
3286
+ if (!nonBlank(ref)) return void 0;
3287
+ const trimmed = ref.trim();
3288
+ const comma = trimmed.indexOf(",");
3289
+ const modelId = comma >= 0 ? trimmed.slice(comma + 1).trim() : trimmed;
3290
+ return modelId || void 0;
3291
+ }
3292
+ function targetProviderId(target) {
3293
+ return target.providerId.trim();
3294
+ }
3295
+ function targetRef(target, ref) {
3296
+ const modelId = _nullishCoalesce(modelIdOf(ref), () => ( ""));
3297
+ return modelId ? `${targetProviderId(target)},${modelId}` : "";
3298
+ }
3299
+ function targetRefs(target, refs) {
3300
+ return (_nullishCoalesce(refs, () => ( []))).map((ref) => targetRef(target, ref)).filter(nonBlank);
3301
+ }
3302
+ function targetPrefixRefs(target, refs) {
3303
+ if (!refs) return void 0;
3304
+ const mapped = {};
3305
+ for (const kind of ["claude", "gpt", "gemini"]) {
3306
+ const ref = targetRef(target, refs[kind]);
3307
+ if (ref) mapped[kind] = ref;
3308
+ }
3309
+ return mapped.claude || mapped.gpt || mapped.gemini ? mapped : void 0;
3310
+ }
3311
+ function applySingleModel(config, binding, targetModel) {
3312
+ const ref = targetRef(binding.target, targetModel);
3313
+ if (!ref) return;
3314
+ if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3315
+ config.modelMap = Object.fromEntries(
3316
+ _chunk4VZX5F4Tcjs.modelKindsForEndpoint.call(void 0, binding.endpoint).map((kind) => [kind, ref])
3317
+ );
3318
+ } else if (binding.endpoint === "chat") {
3319
+ config.models = [ref];
3320
+ config.dispatchMode = "list";
3321
+ } else {
3322
+ config.defaultModel = ref;
3323
+ config.backgroundModel = ref;
3324
+ }
3325
+ }
3326
+ function gatewayBindingToEndpointConfig(binding, requestedModel) {
3327
+ const target = binding.target;
3328
+ const config = {
3329
+ endpoint: binding.endpoint,
3330
+ useSubscription: target.kind !== "provider"
3331
+ };
3332
+ const usesGenericModelHandling = binding.modelMode === "passthrough" || Boolean(_optionalChain([binding, 'access', _146 => _146.modelMappings, 'optionalAccess', _147 => _147.length]));
3333
+ const dynamicModel = binding.modelMode === "passthrough" ? requestedModel : resolveGatewayModelMapping(binding.modelMappings, requestedModel);
3334
+ if (usesGenericModelHandling) {
3335
+ if (nonBlank(dynamicModel)) applySingleModel(config, binding, dynamicModel);
3336
+ } else if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3337
+ config.modelMap = Object.fromEntries(
3338
+ Object.entries(_nullishCoalesce(binding.modelMap, () => ( {}))).map(([kind, ref]) => [kind, targetRef(target, ref)])
3339
+ );
3340
+ } else if (binding.endpoint === "chat") {
3341
+ config.models = targetRefs(target, binding.models);
3342
+ if (binding.dispatchMode === "prefix") {
3343
+ config.dispatchMode = "prefix";
3344
+ config.prefixTargets = targetPrefixRefs(target, binding.prefixTargets);
3345
+ }
3346
+ } else {
3347
+ config.defaultModel = targetRef(target, binding.defaultModel);
3348
+ config.backgroundModel = targetRef(target, binding.backgroundModel);
3349
+ if (binding.backgroundModelIds) config.backgroundModelIds = [...binding.backgroundModelIds];
3350
+ }
3351
+ if (target.kind === "account") {
3352
+ config.boundAccountId = target.accountId;
3353
+ config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3354
+ } else if (target.kind === "account-group") {
3355
+ config.boundAccountGroup = target.group;
3356
+ config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3357
+ } else if (target.kind === "provider" && target.keyId) {
3358
+ config.boundKeyId = target.keyId;
3359
+ config.boundKeyFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3360
+ }
3361
+ return config;
3362
+ }
3363
+ function candidateGatewayBindings(bindings, apiKeyId, endpoint) {
3364
+ const candidates = (_nullishCoalesce(bindings, () => ( []))).filter(
3365
+ (binding) => binding.enabled && binding.endpoint === endpoint && gatewayBindingAllowsKey(binding, apiKeyId)
3366
+ );
3367
+ const scopeOf = (binding) => _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _148 => _148.apiKeyIds, 'optionalAccess', _149 => _149.length]) ? "selected" : "all")));
3368
+ const scoped = candidates.filter((binding) => scopeOf(binding) === "selected");
3369
+ const pool = scoped.length > 0 ? scoped : candidates.filter((binding) => scopeOf(binding) === "all");
3370
+ return pool.sort(
3371
+ (left, right) => (_nullishCoalesce(left.priority, () => ( 100))) - (_nullishCoalesce(right.priority, () => ( 100))) || left.id.localeCompare(right.id)
3372
+ );
3373
+ }
3374
+ function candidateBackgroundModelIds(bindings, apiKeyId, endpoint) {
3375
+ const ids = [
3376
+ ...new Set(
3377
+ candidateGatewayBindings(bindings, apiKeyId, endpoint).flatMap(
3378
+ (binding) => _nullishCoalesce(binding.backgroundModelIds, () => ( []))
3379
+ )
3380
+ )
3381
+ ];
3382
+ return ids.length > 0 ? ids : void 0;
3383
+ }
3384
+ function resolveGatewayBinding(input) {
3385
+ const candidates = candidateGatewayBindings(input.bindings, input.apiKeyId, input.endpoint);
3386
+ const binding = _nullishCoalesce(candidates.find(
3387
+ (candidate) => candidate.fallback === "fail" || routeCanServe(candidate, input.requestedModel, input.role)
3388
+ ), () => ( candidates[0]));
3389
+ if (!binding) return { source: "none" };
3390
+ return {
3391
+ source: "binding",
3392
+ binding,
3393
+ config: gatewayBindingToEndpointConfig(binding, input.requestedModel)
3394
+ };
3395
+ }
3396
+
3397
+ // src/provider-proxy/RouteLeaseTargetResolver.ts
3398
+ function resolveEnvKey(raw) {
3399
+ if (!raw) return "";
3400
+ return raw.startsWith("$") ? _nullishCoalesce(process.env[raw.slice(1)], () => ( "")) : raw;
3401
+ }
3402
+ function providerSupportsModel(provider, model) {
3403
+ const flat = _nullishCoalesce(provider.models, () => ( []));
3404
+ const configured = _nullishCoalesce(provider.modelConfigs, () => ( []));
3405
+ if (flat.length === 0 && configured.length === 0) return false;
3406
+ return flat.includes(model) || configured.some((entry) => entry.id === model && entry.enabled !== false);
3407
+ }
3408
+ function syntheticBinding(request) {
3409
+ const runtime = routeLeaseRuntime(request.runtime);
3410
+ return {
3411
+ id: "route-lease-ephemeral",
3412
+ name: "Route Lease ephemeral target",
3413
+ enabled: true,
3414
+ endpoint: runtime.endpoint,
3415
+ target: request.upstream,
3416
+ fallback: "fail",
3417
+ modelMode: "passthrough"
3418
+ };
3419
+ }
3420
+ function transformerRegistered(llmConfig, name) {
3421
+ return Boolean(_optionalChain([llmConfig, 'access', _150 => _150.getTransformerService, 'call', _151 => _151(), 'optionalAccess', _152 => _152.getTransformer, 'call', _153 => _153(name)]));
3422
+ }
3423
+ var RouteLeaseTargetResolver = class {
3424
+ constructor(llmConfig, options = {}) {
3425
+ this.llmConfig = llmConfig;
3426
+ this.options = options;
3427
+ }
3428
+
3429
+
3430
+ async resolve(request) {
3431
+ const runtime = routeLeaseRuntime(request.runtime);
3432
+ await this.preflightTarget(request);
3433
+ const binding = syntheticBinding(request);
3434
+ const config = gatewayBindingToEndpointConfig(binding, request.model);
3435
+ const result = await _chunk4VZX5F4Tcjs.resolveRoute.call(void 0, {
3436
+ config,
3437
+ ingressFormat: runtime.ingressFormat,
3438
+ llmConfig: this.llmConfig,
3439
+ requestedModel: request.model,
3440
+ sessionId: _nullishCoalesce(_optionalChain([request, 'access', _154 => _154.execution, 'optionalAccess', _155 => _155.sessionIdHash]), () => ( null))
3441
+ });
3442
+ if (!result.ok) {
3443
+ throw new RouteLeaseError(
3444
+ result.error.status === 404 ? "upstream_not_found" : "upstream_unavailable",
3445
+ "the selected upstream could not be resolved"
3446
+ );
3447
+ }
3448
+ return { ...result.route, requestedModel: void 0 };
2380
3449
  }
2381
- /** Remove a route at run end. Returns true if an entry existed. */
2382
- removeRoute(token) {
2383
- return this.routes.removeRoute(token);
3450
+ async preflightTarget(request) {
3451
+ const target = request.upstream;
3452
+ if (target.kind !== "provider") {
3453
+ await this.preflightSubscription(target, request.model);
3454
+ return;
3455
+ }
3456
+ const provider = await this.llmConfig.getProvider(target.providerId);
3457
+ if (!provider) throw new RouteLeaseError("upstream_not_found", "provider was not found");
3458
+ if (provider.enabled === false) throw new RouteLeaseError("upstream_unavailable", "provider is disabled");
3459
+ if (!providerSupportsModel(provider, request.model)) {
3460
+ throw new RouteLeaseError("model_not_configured", "model is not configured for the provider");
3461
+ }
3462
+ if (target.keyId) {
3463
+ const availability = await _optionalChain([this, 'access', _156 => _156.options, 'access', _157 => _157.providerKeys, 'optionalAccess', _158 => _158.getKeyAvailability, 'optionalCall', _159 => _159(
3464
+ target.providerId,
3465
+ target.keyId
3466
+ )]);
3467
+ if (availability === "not-found") {
3468
+ throw new RouteLeaseError("upstream_not_found", "the selected provider key was not found");
3469
+ }
3470
+ const usable = availability === "usable" || availability === void 0 && Boolean(this.options.providerKeys) && await this.options.providerKeys.hasUsableKey(target.providerId, target.keyId);
3471
+ if (!usable) {
3472
+ throw new RouteLeaseError("upstream_unavailable", "the selected provider key is unavailable");
3473
+ }
3474
+ } else {
3475
+ const directKey = resolveEnvKey(provider.api_key);
3476
+ const poolAvailability = await _optionalChain([this, 'access', _160 => _160.options, 'access', _161 => _161.providerKeys, 'optionalAccess', _162 => _162.getPoolAvailability, 'optionalCall', _163 => _163(target.providerId)]);
3477
+ const poolReady = poolAvailability ? poolAvailability.outcome === "usable" : this.options.providerKeys ? await this.options.providerKeys.hasUsableKeys(target.providerId) : false;
3478
+ if (!directKey && _optionalChain([poolAvailability, 'optionalAccess', _164 => _164.outcome]) === "exhausted") {
3479
+ throw new RouteLeaseError("upstream_exhausted", "provider key pool is temporarily exhausted", {
3480
+ retryAfterSeconds: poolAvailability.retryAfterSeconds
3481
+ });
3482
+ }
3483
+ if (!directKey && !poolReady) {
3484
+ throw new RouteLeaseError("upstream_unavailable", "provider has no usable credential");
3485
+ }
3486
+ }
3487
+ const sameFormat = request.runtime === "claude" && provider.apiFormat === "anthropic" || request.runtime === "codex" && provider.apiFormat === "openai-response";
3488
+ if (!sameFormat) {
3489
+ try {
3490
+ const chain = await _chunkQXD4MV2Xcjs.resolveProviderChain.call(void 0, this.llmConfig, provider.id, request.model);
3491
+ if (!chain.hasTransformers) {
3492
+ throw new RouteLeaseError("format_unsupported", "no transformer chain supports this runtime and upstream");
3493
+ }
3494
+ } catch (error) {
3495
+ if (error instanceof RouteLeaseError) throw error;
3496
+ throw new RouteLeaseError("format_unsupported", "the transformer chain could not be resolved", { cause: error });
3497
+ }
3498
+ }
2384
3499
  }
2385
- /** Live-route count (diagnostics / tests). */
2386
- routeCount() {
2387
- return this.routes.size();
3500
+ async preflightSubscription(target, model) {
3501
+ const registry = _chunkQBZJ7P2Tcjs.getSubscriptionRegistryForOutbound.call(void 0, );
3502
+ const profile = _nullishCoalesce(_optionalChain([registry, 'optionalAccess', _165 => _165.getProfile, 'call', _166 => _166(target.providerId)]), () => ( null));
3503
+ if (!profile) throw new RouteLeaseError("upstream_not_found", "subscription provider was not found");
3504
+ await _optionalChain([this, 'access', _167 => _167.options, 'access', _168 => _168.subscriptions, 'optionalAccess', _169 => _169.assertAvailable, 'call', _170 => _170(target, model)]);
3505
+ const names = _nullishCoalesce(_nullishCoalesce(_optionalChain([profile, 'access', _171 => _171.resolveProviderTransformerNames, 'optionalCall', _172 => _172(model)]), () => ( profile.providerTransformerNames)), () => ( []));
3506
+ if (names.some((name) => !transformerRegistered(this.llmConfig, name))) {
3507
+ throw new RouteLeaseError("format_unsupported", "a required transformer is unavailable");
3508
+ }
3509
+ if (_optionalChain([profile, 'access', _173 => _173.modelTransformerNames, 'optionalAccess', _174 => _174.some, 'call', _175 => _175((name) => !transformerRegistered(this.llmConfig, name))])) {
3510
+ throw new RouteLeaseError("format_unsupported", "a required model transformer is unavailable");
3511
+ }
2388
3512
  }
2389
- }, _class2);
3513
+ };
2390
3514
 
2391
3515
  // src/provider-proxy/index.ts
2392
3516
  var instance = null;
@@ -2409,9 +3533,9 @@ var _os = require('os');
2409
3533
  var _healthloggingtypes = require('@omnicross/contracts/health-logging-types');
2410
3534
 
2411
3535
  // src/outbound-api/keyedMutex.ts
2412
- var KeyedMutex = (_class3 = class {constructor() { _class3.prototype.__init4.call(this); }
3536
+ var KeyedMutex = (_class5 = class {constructor() { _class5.prototype.__init11.call(this); }
2413
3537
  /** key → the tail of the pending-op chain (resolves when the last op frees). */
2414
- __init4() {this.tails = /* @__PURE__ */ new Map()}
3538
+ __init11() {this.tails = /* @__PURE__ */ new Map()}
2415
3539
  /**
2416
3540
  * Run `fn` exclusively for `key`: it starts only after every previously
2417
3541
  * enqueued op for the SAME key has settled, and the next waiter starts only
@@ -2434,7 +3558,7 @@ var KeyedMutex = (_class3 = class {constructor() { _class3.prototype.__init4.cal
2434
3558
  if (this.tails.get(key) === chain) this.tails.delete(key);
2435
3559
  }
2436
3560
  }
2437
- }, _class3);
3561
+ }, _class5);
2438
3562
 
2439
3563
  // src/outbound-api/outboundApiRouter.ts
2440
3564
  var _stream = require('stream');
@@ -2462,11 +3586,11 @@ var DEFAULT_ACCOUNT_HEALTH = {
2462
3586
  overloadCooldownMs: 10 * 6e4
2463
3587
  };
2464
3588
  function normalizeAccountHealth(raw) {
2465
- const ah = _optionalChain([raw, 'optionalAccess', _76 => _76.accountHealth]);
3589
+ const ah = _optionalChain([raw, 'optionalAccess', _176 => _176.accountHealth]);
2466
3590
  return {
2467
- overloadCooldownEnabled: _optionalChain([ah, 'optionalAccess', _77 => _77.overloadCooldownEnabled]) !== false,
3591
+ overloadCooldownEnabled: _optionalChain([ah, 'optionalAccess', _177 => _177.overloadCooldownEnabled]) !== false,
2468
3592
  overloadCooldownMs: clampNumber(
2469
- _optionalChain([ah, 'optionalAccess', _78 => _78.overloadCooldownMs]),
3593
+ _optionalChain([ah, 'optionalAccess', _178 => _178.overloadCooldownMs]),
2470
3594
  6e4,
2471
3595
  36e5,
2472
3596
  DEFAULT_ACCOUNT_HEALTH.overloadCooldownMs
@@ -2488,25 +3612,25 @@ var DEFAULT_ALLOWANCE_SCHEDULING = {
2488
3612
  priorityPenalty: 100
2489
3613
  };
2490
3614
  function normalizeAllowanceScheduling(raw) {
2491
- const value = _optionalChain([raw, 'optionalAccess', _79 => _79.allowanceScheduling]);
3615
+ const value = _optionalChain([raw, 'optionalAccess', _179 => _179.allowanceScheduling]);
2492
3616
  const demoteAtPercent = clampNumber(
2493
- _optionalChain([value, 'optionalAccess', _80 => _80.demoteAtPercent]),
3617
+ _optionalChain([value, 'optionalAccess', _180 => _180.demoteAtPercent]),
2494
3618
  0,
2495
3619
  100,
2496
3620
  DEFAULT_ALLOWANCE_SCHEDULING.demoteAtPercent
2497
3621
  );
2498
3622
  const requestedPause = clampNumber(
2499
- _optionalChain([value, 'optionalAccess', _81 => _81.pauseAtPercent]),
3623
+ _optionalChain([value, 'optionalAccess', _181 => _181.pauseAtPercent]),
2500
3624
  0,
2501
3625
  100,
2502
3626
  DEFAULT_ALLOWANCE_SCHEDULING.pauseAtPercent
2503
3627
  );
2504
3628
  return {
2505
- enabled: _optionalChain([value, 'optionalAccess', _82 => _82.enabled]) === true,
3629
+ enabled: _optionalChain([value, 'optionalAccess', _182 => _182.enabled]) === true,
2506
3630
  demoteAtPercent,
2507
3631
  pauseAtPercent: Math.max(demoteAtPercent, requestedPause),
2508
3632
  priorityPenalty: Math.trunc(clampNumber(
2509
- _optionalChain([value, 'optionalAccess', _83 => _83.priorityPenalty]),
3633
+ _optionalChain([value, 'optionalAccess', _183 => _183.priorityPenalty]),
2510
3634
  1,
2511
3635
  1e3,
2512
3636
  DEFAULT_ALLOWANCE_SCHEDULING.priorityPenalty
@@ -2514,53 +3638,58 @@ function normalizeAllowanceScheduling(raw) {
2514
3638
  };
2515
3639
  }
2516
3640
  function normalizeAccountProbe(raw) {
2517
- const ap = _optionalChain([raw, 'optionalAccess', _84 => _84.accountProbe]);
3641
+ const ap = _optionalChain([raw, 'optionalAccess', _184 => _184.accountProbe]);
2518
3642
  return {
2519
- enabled: _optionalChain([ap, 'optionalAccess', _85 => _85.enabled]) === true,
2520
- intervalMs: clampNumber(_optionalChain([ap, 'optionalAccess', _86 => _86.intervalMs]), 6e4, 864e5, DEFAULT_ACCOUNT_PROBE.intervalMs),
2521
- onlyMultiAccount: _optionalChain([ap, 'optionalAccess', _87 => _87.onlyMultiAccount]) !== false,
2522
- timeoutMs: clampNumber(_optionalChain([ap, 'optionalAccess', _88 => _88.timeoutMs]), 1e3, 6e4, DEFAULT_ACCOUNT_PROBE.timeoutMs),
3643
+ enabled: _optionalChain([ap, 'optionalAccess', _185 => _185.enabled]) === true,
3644
+ intervalMs: clampNumber(_optionalChain([ap, 'optionalAccess', _186 => _186.intervalMs]), 6e4, 864e5, DEFAULT_ACCOUNT_PROBE.intervalMs),
3645
+ onlyMultiAccount: _optionalChain([ap, 'optionalAccess', _187 => _187.onlyMultiAccount]) !== false,
3646
+ timeoutMs: clampNumber(_optionalChain([ap, 'optionalAccess', _188 => _188.timeoutMs]), 1e3, 6e4, DEFAULT_ACCOUNT_PROBE.timeoutMs),
2523
3647
  historySize: Math.trunc(
2524
- clampNumber(_optionalChain([ap, 'optionalAccess', _89 => _89.historySize]), 1, 200, DEFAULT_ACCOUNT_PROBE.historySize)
3648
+ clampNumber(_optionalChain([ap, 'optionalAccess', _189 => _189.historySize]), 1, 200, DEFAULT_ACCOUNT_PROBE.historySize)
2525
3649
  ),
2526
- staggerMs: clampNumber(_optionalChain([ap, 'optionalAccess', _90 => _90.staggerMs]), 0, 6e4, DEFAULT_ACCOUNT_PROBE.staggerMs)
3650
+ staggerMs: clampNumber(_optionalChain([ap, 'optionalAccess', _190 => _190.staggerMs]), 0, 6e4, DEFAULT_ACCOUNT_PROBE.staggerMs)
2527
3651
  };
2528
3652
  }
2529
3653
  function normalizeAudit(raw) {
2530
- const a = _optionalChain([raw, 'optionalAccess', _91 => _91.audit]);
3654
+ const a = _optionalChain([raw, 'optionalAccess', _191 => _191.audit]);
2531
3655
  return {
2532
- enabled: _optionalChain([a, 'optionalAccess', _92 => _92.enabled]) === true,
2533
- captureBodies: _optionalChain([a, 'optionalAccess', _93 => _93.captureBodies]) === true,
2534
- maxBodyBytes: Math.trunc(
2535
- clampNumber(_optionalChain([a, 'optionalAccess', _94 => _94.maxBodyBytes]), 256, 1048576, _audittypes.DEFAULT_AUDIT_CONFIG.maxBodyBytes)
2536
- ),
3656
+ enabled: _optionalChain([a, 'optionalAccess', _192 => _192.enabled]) === true,
3657
+ captureBodies: _optionalChain([a, 'optionalAccess', _193 => _193.captureBodies]) === true,
3658
+ maxBodyBytes: normalizeMaxBodyBytes(_optionalChain([a, 'optionalAccess', _194 => _194.maxBodyBytes])),
2537
3659
  retentionDays: Math.trunc(
2538
- clampNumber(_optionalChain([a, 'optionalAccess', _95 => _95.retentionDays]), 1, 365, _audittypes.DEFAULT_AUDIT_CONFIG.retentionDays)
3660
+ clampNumber(_optionalChain([a, 'optionalAccess', _195 => _195.retentionDays]), 1, 365, _audittypes.DEFAULT_AUDIT_CONFIG.retentionDays)
2539
3661
  ),
2540
- trustForwardedFor: _optionalChain([a, 'optionalAccess', _96 => _96.trustForwardedFor]) === true
3662
+ trustForwardedFor: _optionalChain([a, 'optionalAccess', _196 => _196.trustForwardedFor]) === true
2541
3663
  };
2542
3664
  }
3665
+ function normalizeMaxBodyBytes(value) {
3666
+ if (value === -1) return -1;
3667
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
3668
+ return _audittypes.DEFAULT_AUDIT_CONFIG.maxBodyBytes;
3669
+ }
3670
+ return Math.trunc(Math.min(1048576, value));
3671
+ }
2543
3672
  function normalizeBilling(raw) {
2544
- const b = _optionalChain([raw, 'optionalAccess', _97 => _97.billing]);
3673
+ const b = _optionalChain([raw, 'optionalAccess', _197 => _197.billing]);
2545
3674
  const config = {
2546
- enabled: _optionalChain([b, 'optionalAccess', _98 => _98.enabled]) === true,
3675
+ enabled: _optionalChain([b, 'optionalAccess', _198 => _198.enabled]) === true,
2547
3676
  maxRetryAgeMs: Math.trunc(
2548
- clampNumber(_optionalChain([b, 'optionalAccess', _99 => _99.maxRetryAgeMs]), 6e4, 2592e6, _billingtypes.DEFAULT_BILLING_CONFIG.maxRetryAgeMs)
3677
+ clampNumber(_optionalChain([b, 'optionalAccess', _199 => _199.maxRetryAgeMs]), 6e4, 2592e6, _billingtypes.DEFAULT_BILLING_CONFIG.maxRetryAgeMs)
2549
3678
  )
2550
3679
  };
2551
- if (typeof _optionalChain([b, 'optionalAccess', _100 => _100.endpoint]) === "string" && b.endpoint.trim()) config.endpoint = b.endpoint.trim();
2552
- if (typeof _optionalChain([b, 'optionalAccess', _101 => _101.secret]) === "string" && b.secret.length > 0) config.secret = b.secret;
3680
+ if (typeof _optionalChain([b, 'optionalAccess', _200 => _200.endpoint]) === "string" && b.endpoint.trim()) config.endpoint = b.endpoint.trim();
3681
+ if (typeof _optionalChain([b, 'optionalAccess', _201 => _201.secret]) === "string" && b.secret.length > 0) config.secret = b.secret;
2553
3682
  return config;
2554
3683
  }
2555
3684
  var DEFAULT_FINGERPRINT = { enabled: false };
2556
3685
  function normalizeFingerprint(raw) {
2557
- const f = _optionalChain([raw, 'optionalAccess', _102 => _102.fingerprint]);
2558
- const out = { enabled: _optionalChain([f, 'optionalAccess', _103 => _103.enabled]) === true };
2559
- if (typeof _optionalChain([f, 'optionalAccess', _104 => _104.ua]) === "string" && f.ua.trim().length > 0) out.ua = f.ua.trim();
3686
+ const f = _optionalChain([raw, 'optionalAccess', _202 => _202.fingerprint]);
3687
+ const out = { enabled: _optionalChain([f, 'optionalAccess', _203 => _203.enabled]) === true };
3688
+ if (typeof _optionalChain([f, 'optionalAccess', _204 => _204.ua]) === "string" && f.ua.trim().length > 0) out.ua = f.ua.trim();
2560
3689
  return out;
2561
3690
  }
2562
3691
  function normalizeVoucher(raw) {
2563
- return { enabled: _optionalChain([raw, 'optionalAccess', _105 => _105.voucher, 'optionalAccess', _106 => _106.enabled]) === true };
3692
+ return { enabled: _optionalChain([raw, 'optionalAccess', _205 => _205.voucher, 'optionalAccess', _206 => _206.enabled]) === true };
2564
3693
  }
2565
3694
  var PROXY_TYPES = ["http", "https", "socks5"];
2566
3695
  function normalizeProxyConfig(raw) {
@@ -2644,14 +3773,14 @@ function clampNumber(value, min, max, fallback) {
2644
3773
  return Math.min(max, Math.max(min, n));
2645
3774
  }
2646
3775
  function normalizeQueueSegments(raw) {
2647
- const umq = _optionalChain([raw, 'optionalAccess', _107 => _107.userMessageQueue]);
2648
- const cq = _optionalChain([raw, 'optionalAccess', _108 => _108.concurrencyQueue]);
3776
+ const umq = _optionalChain([raw, 'optionalAccess', _207 => _207.userMessageQueue]);
3777
+ const cq = _optionalChain([raw, 'optionalAccess', _208 => _208.concurrencyQueue]);
2649
3778
  return {
2650
3779
  userMessageQueue: {
2651
- enabled: _optionalChain([umq, 'optionalAccess', _109 => _109.enabled]) === true,
2652
- delayMs: clampNumber(_optionalChain([umq, 'optionalAccess', _110 => _110.delayMs]), 0, 1e4, DEFAULT_USER_MESSAGE_QUEUE.delayMs),
3780
+ enabled: _optionalChain([umq, 'optionalAccess', _209 => _209.enabled]) === true,
3781
+ delayMs: clampNumber(_optionalChain([umq, 'optionalAccess', _210 => _210.delayMs]), 0, 1e4, DEFAULT_USER_MESSAGE_QUEUE.delayMs),
2653
3782
  waitTimeoutMs: clampNumber(
2654
- _optionalChain([umq, 'optionalAccess', _111 => _111.waitTimeoutMs]),
3783
+ _optionalChain([umq, 'optionalAccess', _211 => _211.waitTimeoutMs]),
2655
3784
  1e3,
2656
3785
  3e5,
2657
3786
  DEFAULT_USER_MESSAGE_QUEUE.waitTimeoutMs
@@ -2659,14 +3788,14 @@ function normalizeQueueSegments(raw) {
2659
3788
  },
2660
3789
  concurrencyQueue: {
2661
3790
  maxQueueSizeFactor: clampNumber(
2662
- _optionalChain([cq, 'optionalAccess', _112 => _112.maxQueueSizeFactor]),
3791
+ _optionalChain([cq, 'optionalAccess', _212 => _212.maxQueueSizeFactor]),
2663
3792
  1,
2664
3793
  10,
2665
3794
  DEFAULT_CONCURRENCY_QUEUE.maxQueueSizeFactor
2666
3795
  ),
2667
- minQueueSize: clampNumber(_optionalChain([cq, 'optionalAccess', _113 => _113.minQueueSize]), 1, 100, DEFAULT_CONCURRENCY_QUEUE.minQueueSize),
3796
+ minQueueSize: clampNumber(_optionalChain([cq, 'optionalAccess', _213 => _213.minQueueSize]), 1, 100, DEFAULT_CONCURRENCY_QUEUE.minQueueSize),
2668
3797
  waitTimeoutMs: clampNumber(
2669
- _optionalChain([cq, 'optionalAccess', _114 => _114.waitTimeoutMs]),
3798
+ _optionalChain([cq, 'optionalAccess', _214 => _214.waitTimeoutMs]),
2670
3799
  1e3,
2671
3800
  3e5,
2672
3801
  DEFAULT_CONCURRENCY_QUEUE.waitTimeoutMs
@@ -2779,7 +3908,7 @@ function normalizeGatewayModelMappings(raw) {
2779
3908
  return mappings;
2780
3909
  }
2781
3910
  function hasConfiguredLegacyModels(config) {
2782
- return Object.values(_nullishCoalesce(config.modelMap, () => ( {}))).some((value) => typeof value === "string" && value.trim()) || Boolean(_optionalChain([config, 'access', _115 => _115.models, 'optionalAccess', _116 => _116.some, 'call', _117 => _117((value) => typeof value === "string" && value.trim())])) || Boolean(_optionalChain([config, 'access', _118 => _118.defaultModel, 'optionalAccess', _119 => _119.trim, 'call', _120 => _120()])) || Boolean(_optionalChain([config, 'access', _121 => _121.backgroundModel, 'optionalAccess', _122 => _122.trim, 'call', _123 => _123()])) || Boolean(Object.values(_nullishCoalesce(config.prefixTargets, () => ( {}))).some((value) => _optionalChain([value, 'optionalAccess', _124 => _124.trim, 'call', _125 => _125()])));
3911
+ return Object.values(_nullishCoalesce(config.modelMap, () => ( {}))).some((value) => typeof value === "string" && value.trim()) || Boolean(_optionalChain([config, 'access', _215 => _215.models, 'optionalAccess', _216 => _216.some, 'call', _217 => _217((value) => typeof value === "string" && value.trim())])) || Boolean(_optionalChain([config, 'access', _218 => _218.defaultModel, 'optionalAccess', _219 => _219.trim, 'call', _220 => _220()])) || Boolean(_optionalChain([config, 'access', _221 => _221.backgroundModel, 'optionalAccess', _222 => _222.trim, 'call', _223 => _223()])) || Boolean(Object.values(_nullishCoalesce(config.prefixTargets, () => ( {}))).some((value) => _optionalChain([value, 'optionalAccess', _224 => _224.trim, 'call', _225 => _225()])));
2783
3912
  }
2784
3913
  function splitModelRef(ref) {
2785
3914
  if (typeof ref !== "string") return null;
@@ -2847,7 +3976,7 @@ function legacyEndpointToBindings(config) {
2847
3976
  if (!parsed) continue;
2848
3977
  const route = routeFor(parsed.providerId);
2849
3978
  route[field] = ref;
2850
- if (_optionalChain([config, 'access', _126 => _126.backgroundModelIds, 'optionalAccess', _127 => _127.length])) {
3979
+ if (_optionalChain([config, 'access', _226 => _226.backgroundModelIds, 'optionalAccess', _227 => _227.length])) {
2851
3980
  route.backgroundModelIds = [...config.backgroundModelIds];
2852
3981
  }
2853
3982
  }
@@ -2997,167 +4126,6 @@ function mergeServerConfig(current, patch) {
2997
4126
  });
2998
4127
  }
2999
4128
 
3000
- // src/outbound-api/gatewayBindingResolver.ts
3001
- var MESSAGE_FALLBACK_KINDS = ["sonnet", "opus", "haiku", "fable"];
3002
- function nonBlank(value) {
3003
- return typeof value === "string" && value.trim() !== "";
3004
- }
3005
- function gatewayBindingAllowsKey(binding, apiKeyId) {
3006
- const scope = _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _128 => _128.apiKeyIds, 'optionalAccess', _129 => _129.length]) ? "selected" : "all")));
3007
- return scope === "all" || Boolean(_optionalChain([binding, 'access', _130 => _130.apiKeyIds, 'optionalAccess', _131 => _131.includes, 'call', _132 => _132(apiKeyId)]));
3008
- }
3009
- function wildcardMatches(pattern, value) {
3010
- const escaped = pattern.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")).join(".*");
3011
- return new RegExp(`^${escaped}$`, "iu").test(value);
3012
- }
3013
- function resolveGatewayModelMapping(mappings, requestedModel) {
3014
- if (!nonBlank(requestedModel)) return void 0;
3015
- const wanted = requestedModel.trim();
3016
- const exact = (_nullishCoalesce(mappings, () => ( []))).find(
3017
- (mapping) => nonBlank(mapping.source) && !mapping.source.includes("*") && mapping.source.trim().toLocaleLowerCase() === wanted.toLocaleLowerCase()
3018
- );
3019
- if (exact && nonBlank(exact.target)) return exact.target.trim();
3020
- const wildcard = (_nullishCoalesce(mappings, () => ( []))).find(
3021
- (mapping) => nonBlank(mapping.source) && mapping.source.includes("*") && wildcardMatches(mapping.source.trim(), wanted)
3022
- );
3023
- return wildcard && nonBlank(wildcard.target) ? wildcard.target.trim() : void 0;
3024
- }
3025
- function routeCanServe(binding, requestedModel, role) {
3026
- if (binding.modelMode === "passthrough") return nonBlank(requestedModel);
3027
- if (_optionalChain([binding, 'access', _133 => _133.modelMappings, 'optionalAccess', _134 => _134.length])) {
3028
- return nonBlank(resolveGatewayModelMapping(binding.modelMappings, requestedModel));
3029
- }
3030
- if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3031
- const map = _nullishCoalesce(binding.modelMap, () => ( {}));
3032
- const kind = _chunk4VZX5F4Tcjs.detectModelKind.call(void 0, binding.endpoint, requestedModel);
3033
- if (kind && nonBlank(map[kind])) return true;
3034
- if (binding.endpoint === "messages") {
3035
- return MESSAGE_FALLBACK_KINDS.some((candidate) => nonBlank(map[candidate]));
3036
- }
3037
- return nonBlank(map.codex);
3038
- }
3039
- if (binding.endpoint === "chat" && binding.dispatchMode === "prefix") {
3040
- return _chunk4VZX5F4Tcjs.resolvePrefixTarget.call(void 0, binding.prefixTargets, requestedModel) !== null;
3041
- }
3042
- if (binding.endpoint === "chat") {
3043
- if (!requestedModel) return false;
3044
- const wanted = requestedModel.trim().toLowerCase();
3045
- return (_nullishCoalesce(binding.models, () => ( []))).some((ref) => _optionalChain([modelIdOf, 'call', _135 => _135(ref), 'optionalAccess', _136 => _136.toLowerCase, 'call', _137 => _137()]) === wanted);
3046
- }
3047
- return role === "background" ? nonBlank(binding.backgroundModel) : nonBlank(binding.defaultModel);
3048
- }
3049
- function modelIdOf(ref) {
3050
- if (!nonBlank(ref)) return void 0;
3051
- const trimmed = ref.trim();
3052
- const comma = trimmed.indexOf(",");
3053
- const modelId = comma >= 0 ? trimmed.slice(comma + 1).trim() : trimmed;
3054
- return modelId || void 0;
3055
- }
3056
- function targetProviderId(target) {
3057
- return target.providerId.trim();
3058
- }
3059
- function targetRef(target, ref) {
3060
- const modelId = _nullishCoalesce(modelIdOf(ref), () => ( ""));
3061
- return modelId ? `${targetProviderId(target)},${modelId}` : "";
3062
- }
3063
- function targetRefs(target, refs) {
3064
- return (_nullishCoalesce(refs, () => ( []))).map((ref) => targetRef(target, ref)).filter(nonBlank);
3065
- }
3066
- function targetPrefixRefs(target, refs) {
3067
- if (!refs) return void 0;
3068
- const mapped = {};
3069
- for (const kind of ["claude", "gpt", "gemini"]) {
3070
- const ref = targetRef(target, refs[kind]);
3071
- if (ref) mapped[kind] = ref;
3072
- }
3073
- return mapped.claude || mapped.gpt || mapped.gemini ? mapped : void 0;
3074
- }
3075
- function applySingleModel(config, binding, targetModel) {
3076
- const ref = targetRef(binding.target, targetModel);
3077
- if (!ref) return;
3078
- if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3079
- config.modelMap = Object.fromEntries(
3080
- _chunk4VZX5F4Tcjs.modelKindsForEndpoint.call(void 0, binding.endpoint).map((kind) => [kind, ref])
3081
- );
3082
- } else if (binding.endpoint === "chat") {
3083
- config.models = [ref];
3084
- config.dispatchMode = "list";
3085
- } else {
3086
- config.defaultModel = ref;
3087
- config.backgroundModel = ref;
3088
- }
3089
- }
3090
- function gatewayBindingToEndpointConfig(binding, requestedModel) {
3091
- const target = binding.target;
3092
- const config = {
3093
- endpoint: binding.endpoint,
3094
- useSubscription: target.kind !== "provider"
3095
- };
3096
- const usesGenericModelHandling = binding.modelMode === "passthrough" || Boolean(_optionalChain([binding, 'access', _138 => _138.modelMappings, 'optionalAccess', _139 => _139.length]));
3097
- const dynamicModel = binding.modelMode === "passthrough" ? requestedModel : resolveGatewayModelMapping(binding.modelMappings, requestedModel);
3098
- if (usesGenericModelHandling) {
3099
- if (nonBlank(dynamicModel)) applySingleModel(config, binding, dynamicModel);
3100
- } else if (_chunk4VZX5F4Tcjs.isKindMappedEndpoint.call(void 0, binding.endpoint)) {
3101
- config.modelMap = Object.fromEntries(
3102
- Object.entries(_nullishCoalesce(binding.modelMap, () => ( {}))).map(([kind, ref]) => [kind, targetRef(target, ref)])
3103
- );
3104
- } else if (binding.endpoint === "chat") {
3105
- config.models = targetRefs(target, binding.models);
3106
- if (binding.dispatchMode === "prefix") {
3107
- config.dispatchMode = "prefix";
3108
- config.prefixTargets = targetPrefixRefs(target, binding.prefixTargets);
3109
- }
3110
- } else {
3111
- config.defaultModel = targetRef(target, binding.defaultModel);
3112
- config.backgroundModel = targetRef(target, binding.backgroundModel);
3113
- if (binding.backgroundModelIds) config.backgroundModelIds = [...binding.backgroundModelIds];
3114
- }
3115
- if (target.kind === "account") {
3116
- config.boundAccountId = target.accountId;
3117
- config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3118
- } else if (target.kind === "account-group") {
3119
- config.boundAccountGroup = target.group;
3120
- config.boundAccountFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3121
- } else if (target.kind === "provider" && target.keyId) {
3122
- config.boundKeyId = target.keyId;
3123
- config.boundKeyFallbackPolicy = binding.fallback === "next" ? "pool" : "strict";
3124
- }
3125
- return config;
3126
- }
3127
- function candidateGatewayBindings(bindings, apiKeyId, endpoint) {
3128
- const candidates = (_nullishCoalesce(bindings, () => ( []))).filter(
3129
- (binding) => binding.enabled && binding.endpoint === endpoint && gatewayBindingAllowsKey(binding, apiKeyId)
3130
- );
3131
- const scopeOf = (binding) => _nullishCoalesce(binding.keyScope, () => ( (_optionalChain([binding, 'access', _140 => _140.apiKeyIds, 'optionalAccess', _141 => _141.length]) ? "selected" : "all")));
3132
- const scoped = candidates.filter((binding) => scopeOf(binding) === "selected");
3133
- const pool = scoped.length > 0 ? scoped : candidates.filter((binding) => scopeOf(binding) === "all");
3134
- return pool.sort(
3135
- (left, right) => (_nullishCoalesce(left.priority, () => ( 100))) - (_nullishCoalesce(right.priority, () => ( 100))) || left.id.localeCompare(right.id)
3136
- );
3137
- }
3138
- function candidateBackgroundModelIds(bindings, apiKeyId, endpoint) {
3139
- const ids = [
3140
- ...new Set(
3141
- candidateGatewayBindings(bindings, apiKeyId, endpoint).flatMap(
3142
- (binding) => _nullishCoalesce(binding.backgroundModelIds, () => ( []))
3143
- )
3144
- )
3145
- ];
3146
- return ids.length > 0 ? ids : void 0;
3147
- }
3148
- function resolveGatewayBinding(input) {
3149
- const candidates = candidateGatewayBindings(input.bindings, input.apiKeyId, input.endpoint);
3150
- const binding = _nullishCoalesce(candidates.find(
3151
- (candidate) => candidate.fallback === "fail" || routeCanServe(candidate, input.requestedModel, input.role)
3152
- ), () => ( candidates[0]));
3153
- if (!binding) return { source: "none" };
3154
- return {
3155
- source: "binding",
3156
- binding,
3157
- config: gatewayBindingToEndpointConfig(binding, input.requestedModel)
3158
- };
3159
- }
3160
-
3161
4129
  // src/outbound-api/outboundApiKeyAuth.ts
3162
4130
 
3163
4131
 
@@ -3240,7 +4208,8 @@ async function createNamedKey(db, name) {
3240
4208
  id,
3241
4209
  name,
3242
4210
  keyHash: hashKey(secret),
3243
- keyPrefix: prefix
4211
+ keyPrefix: prefix,
4212
+ plaintext: secret
3244
4213
  });
3245
4214
  return {
3246
4215
  id: row.id,
@@ -3261,7 +4230,8 @@ async function createIntegrationKey(db, name, allowedEndpoints = ["responses", "
3261
4230
  keyPrefix: prefix,
3262
4231
  kind: "integration",
3263
4232
  allowedEndpoints: [...new Set(allowedEndpoints)],
3264
- loopbackOnly: true
4233
+ loopbackOnly: true,
4234
+ plaintext: secret
3265
4235
  });
3266
4236
  return {
3267
4237
  id: row.id,
@@ -3333,42 +4303,42 @@ async function verifyPresentedKey(db, presentedKey) {
3333
4303
  }
3334
4304
 
3335
4305
  // src/outbound-api/outboundConcurrencyGate.ts
3336
- var ConcurrencyQueueFullError = (_class4 = class extends Error {
4306
+ var ConcurrencyQueueFullError = (_class6 = class extends Error {
3337
4307
  constructor(apiKeyId, maxQueueSize) {
3338
- super(`concurrency queue full (max ${maxQueueSize}) for key '${apiKeyId}'`);_class4.prototype.__init5.call(this);;
4308
+ super(`concurrency queue full (max ${maxQueueSize}) for key '${apiKeyId}'`);_class6.prototype.__init12.call(this);;
3339
4309
  this.apiKeyId = apiKeyId;
3340
4310
  this.maxQueueSize = maxQueueSize;
3341
4311
  this.name = "ConcurrencyQueueFullError";
3342
4312
  }
3343
4313
 
3344
4314
 
3345
- __init5() {this.code = "concurrency_queue_full"}
3346
- }, _class4);
3347
- var ConcurrencyWaitTimeoutError = (_class5 = class extends Error {
4315
+ __init12() {this.code = "concurrency_queue_full"}
4316
+ }, _class6);
4317
+ var ConcurrencyWaitTimeoutError = (_class7 = class extends Error {
3348
4318
  constructor(apiKeyId, waitTimeoutMs) {
3349
- super(`concurrency wait timed out after ${waitTimeoutMs}ms for key '${apiKeyId}'`);_class5.prototype.__init6.call(this);;
4319
+ super(`concurrency wait timed out after ${waitTimeoutMs}ms for key '${apiKeyId}'`);_class7.prototype.__init13.call(this);;
3350
4320
  this.apiKeyId = apiKeyId;
3351
4321
  this.waitTimeoutMs = waitTimeoutMs;
3352
4322
  this.name = "ConcurrencyWaitTimeoutError";
3353
4323
  }
3354
4324
 
3355
4325
 
3356
- __init6() {this.code = "concurrency_wait_timeout"}
3357
- }, _class5);
3358
- var ConcurrencyWaitCancelledError = (_class6 = class extends Error {
4326
+ __init13() {this.code = "concurrency_wait_timeout"}
4327
+ }, _class7);
4328
+ var ConcurrencyWaitCancelledError = (_class8 = class extends Error {
3359
4329
  constructor(apiKeyId) {
3360
- super(`concurrency wait cancelled for key '${apiKeyId}'`);_class6.prototype.__init7.call(this);;
4330
+ super(`concurrency wait cancelled for key '${apiKeyId}'`);_class8.prototype.__init14.call(this);;
3361
4331
  this.apiKeyId = apiKeyId;
3362
4332
  this.name = "ConcurrencyWaitCancelledError";
3363
4333
  }
3364
4334
 
3365
- __init7() {this.code = "concurrency_wait_cancelled"}
3366
- }, _class6);
4335
+ __init14() {this.code = "concurrency_wait_cancelled"}
4336
+ }, _class8);
3367
4337
  function isConcurrencyRejection(err) {
3368
4338
  return err instanceof ConcurrencyQueueFullError || err instanceof ConcurrencyWaitTimeoutError || err instanceof ConcurrencyWaitCancelledError;
3369
4339
  }
3370
- var OutboundConcurrencyGate = (_class7 = class {constructor() { _class7.prototype.__init8.call(this); }
3371
- __init8() {this.states = /* @__PURE__ */ new Map()}
4340
+ var OutboundConcurrencyGate = (_class9 = class {constructor() { _class9.prototype.__init15.call(this); }
4341
+ __init15() {this.states = /* @__PURE__ */ new Map()}
3372
4342
  ensure(apiKeyId) {
3373
4343
  let state = this.states.get(apiKeyId);
3374
4344
  if (!state) {
@@ -3492,17 +4462,17 @@ var OutboundConcurrencyGate = (_class7 = class {constructor() { _class7.prototyp
3492
4462
  }
3493
4463
  this.states.clear();
3494
4464
  }
3495
- }, _class7);
4465
+ }, _class9);
3496
4466
 
3497
4467
  // src/outbound-api/outboundRateLimiter.ts
3498
4468
  var DEFAULT_WINDOW_MS = 6e4;
3499
4469
  var DEFAULT_MAX_REQUESTS = 60;
3500
- var OutboundRateLimiter = (_class8 = class {
4470
+ var OutboundRateLimiter = (_class10 = class {
3501
4471
 
3502
4472
 
3503
4473
  /** apiKeyId → ascending request timestamps within the current window. */
3504
- __init9() {this.hits = /* @__PURE__ */ new Map()}
3505
- constructor(options = {}) {;_class8.prototype.__init9.call(this);
4474
+ __init16() {this.hits = /* @__PURE__ */ new Map()}
4475
+ constructor(options = {}) {;_class10.prototype.__init16.call(this);
3506
4476
  this.windowMs = _nullishCoalesce(options.windowMs, () => ( DEFAULT_WINDOW_MS));
3507
4477
  this.maxRequests = _nullishCoalesce(options.maxRequests, () => ( DEFAULT_MAX_REQUESTS));
3508
4478
  }
@@ -3518,8 +4488,8 @@ var OutboundRateLimiter = (_class8 = class {
3518
4488
  * recorded (no bucket growth).
3519
4489
  */
3520
4490
  check(apiKeyId, now = Date.now(), override) {
3521
- const maxRequests = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _142 => _142.maxRequests]), () => ( this.maxRequests));
3522
- const windowMs = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _143 => _143.windowMs]), () => ( this.windowMs));
4491
+ const maxRequests = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _228 => _228.maxRequests]), () => ( this.maxRequests));
4492
+ const windowMs = _nullishCoalesce(_optionalChain([override, 'optionalAccess', _229 => _229.windowMs]), () => ( this.windowMs));
3523
4493
  if (maxRequests === 0) return { allowed: true, retryAfterSeconds: 0 };
3524
4494
  const cutoff = now - windowMs;
3525
4495
  const timestamps = (_nullishCoalesce(this.hits.get(apiKeyId), () => ( []))).filter((ts) => ts > cutoff);
@@ -3544,7 +4514,7 @@ var OutboundRateLimiter = (_class8 = class {
3544
4514
  reset() {
3545
4515
  this.hits.clear();
3546
4516
  }
3547
- }, _class8);
4517
+ }, _class10);
3548
4518
 
3549
4519
  // src/outbound-api/userMessageDetection.ts
3550
4520
  function asArray(v) {
@@ -3564,7 +4534,7 @@ function isUserMessageAnthropic(body) {
3564
4534
  if (typeof content === "string") return true;
3565
4535
  if (Array.isArray(content)) {
3566
4536
  const hasToolResult = content.some(
3567
- (block) => _optionalChain([asObject, 'call', _144 => _144(block), 'optionalAccess', _145 => _145["type"]]) === "tool_result"
4537
+ (block) => _optionalChain([asObject, 'call', _230 => _230(block), 'optionalAccess', _231 => _231["type"]]) === "tool_result"
3568
4538
  );
3569
4539
  return !hasToolResult;
3570
4540
  }
@@ -3593,7 +4563,7 @@ function isUserMessageGemini(body) {
3593
4563
  if (!lastContent || lastContent["role"] !== "user") return false;
3594
4564
  const parts = asArray(lastContent["parts"]);
3595
4565
  const hasFunctionResponse = parts.some(
3596
- (part) => _optionalChain([asObject, 'call', _146 => _146(part), 'optionalAccess', _147 => _147["functionResponse"]]) !== void 0
4566
+ (part) => _optionalChain([asObject, 'call', _232 => _232(part), 'optionalAccess', _233 => _233["functionResponse"]]) !== void 0
3597
4567
  );
3598
4568
  return !hasFunctionResponse;
3599
4569
  }
@@ -3615,22 +4585,22 @@ function isUserMessageRequest(endpoint, parsedBody) {
3615
4585
  }
3616
4586
 
3617
4587
  // src/outbound-api/userMessageSerialQueue.ts
3618
- var SerialQueueTimeoutError = (_class9 = class extends Error {
4588
+ var SerialQueueTimeoutError = (_class11 = class extends Error {
3619
4589
  constructor(providerId, waitTimeoutMs) {
3620
- super(`user-message serial queue wait timed out after ${waitTimeoutMs}ms for '${providerId}'`);_class9.prototype.__init10.call(this);;
4590
+ super(`user-message serial queue wait timed out after ${waitTimeoutMs}ms for '${providerId}'`);_class11.prototype.__init17.call(this);;
3621
4591
  this.providerId = providerId;
3622
4592
  this.waitTimeoutMs = waitTimeoutMs;
3623
4593
  this.name = "SerialQueueTimeoutError";
3624
4594
  }
3625
4595
 
3626
4596
 
3627
- __init10() {this.code = "serial_queue_timeout"}
3628
- }, _class9);
4597
+ __init17() {this.code = "serial_queue_timeout"}
4598
+ }, _class11);
3629
4599
  function isSerialQueueTimeout(err) {
3630
4600
  return err instanceof SerialQueueTimeoutError;
3631
4601
  }
3632
- var UserMessageSerialQueue = (_class10 = class {constructor() { _class10.prototype.__init11.call(this); }
3633
- __init11() {this.states = /* @__PURE__ */ new Map()}
4602
+ var UserMessageSerialQueue = (_class12 = class {constructor() { _class12.prototype.__init18.call(this); }
4603
+ __init18() {this.states = /* @__PURE__ */ new Map()}
3634
4604
  ensure(providerId) {
3635
4605
  let state = this.states.get(providerId);
3636
4606
  if (!state) {
@@ -3745,7 +4715,7 @@ var UserMessageSerialQueue = (_class10 = class {constructor() { _class10.prototy
3745
4715
  }
3746
4716
  this.states.clear();
3747
4717
  }
3748
- }, _class10);
4718
+ }, _class12);
3749
4719
 
3750
4720
  // src/outbound-api/voucher.ts
3751
4721
 
@@ -3767,14 +4737,14 @@ function newVoucherId() {
3767
4737
  }
3768
4738
  function computeVoucherGrant(voucher, currentKey, now) {
3769
4739
  if (voucher.type === "credit") {
3770
- const current = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _148 => _148.totalCostLimitUsd]), () => ( 0));
4740
+ const current = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _234 => _234.totalCostLimitUsd]), () => ( 0));
3771
4741
  let granted2 = current + (_nullishCoalesce(voucher.creditUsd, () => ( 0)));
3772
4742
  if (voucher.maxTotalCostLimitUsd != null) {
3773
4743
  granted2 = Math.min(granted2, voucher.maxTotalCostLimitUsd);
3774
4744
  }
3775
4745
  return { totalCostLimitUsd: granted2 };
3776
4746
  }
3777
- const currentExpiry = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _149 => _149.expiresAt]), () => ( now));
4747
+ const currentExpiry = _nullishCoalesce(_optionalChain([currentKey, 'optionalAccess', _235 => _235.expiresAt]), () => ( now));
3778
4748
  let granted = currentExpiry + (_nullishCoalesce(voucher.renewalDays, () => ( 0))) * DAY_MS2;
3779
4749
  if (voucher.maxExpiryDays != null) {
3780
4750
  granted = Math.min(granted, now + voucher.maxExpiryDays * DAY_MS2);
@@ -3805,7 +4775,7 @@ function toVoucherInfo(v) {
3805
4775
  // src/outbound-api/voucherRedeem.ts
3806
4776
  function isRedeemRequest(method, url) {
3807
4777
  if (method !== "POST" || !url) return false;
3808
- const path = _nullishCoalesce(_optionalChain([url, 'access', _150 => _150.split, 'call', _151 => _151("?"), 'access', _152 => _152[0], 'optionalAccess', _153 => _153.replace, 'call', _154 => _154(/\/+$/, "")]), () => ( ""));
4778
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _236 => _236.split, 'call', _237 => _237("?"), 'access', _238 => _238[0], 'optionalAccess', _239 => _239.replace, 'call', _240 => _240(/\/+$/, "")]), () => ( ""));
3809
4779
  return path.endsWith("/redeem");
3810
4780
  }
3811
4781
  function writeErr(res, status, message, headers = {}) {
@@ -3835,8 +4805,8 @@ function grantToResult(type, granted) {
3835
4805
  function currentResult(type, keyRow) {
3836
4806
  const result = { type };
3837
4807
  if (type === "credit") {
3838
- if (_optionalChain([keyRow, 'optionalAccess', _155 => _155.totalCostLimitUsd]) != null) result.totalCostLimitUsd = keyRow.totalCostLimitUsd;
3839
- } else if (_optionalChain([keyRow, 'optionalAccess', _156 => _156.expiresAt]) != null) {
4808
+ if (_optionalChain([keyRow, 'optionalAccess', _241 => _241.totalCostLimitUsd]) != null) result.totalCostLimitUsd = keyRow.totalCostLimitUsd;
4809
+ } else if (_optionalChain([keyRow, 'optionalAccess', _242 => _242.expiresAt]) != null) {
3840
4810
  result.expiresAt = keyRow.expiresAt;
3841
4811
  }
3842
4812
  return result;
@@ -3874,7 +4844,7 @@ async function handleVoucherRedeem(req, res, deps, voucherEnabled, redeemLimiter
3874
4844
  let code;
3875
4845
  try {
3876
4846
  code = raw ? JSON.parse(raw)["code"] : void 0;
3877
- } catch (e24) {
4847
+ } catch (e27) {
3878
4848
  writeErr(res, 400, "Invalid JSON in request body");
3879
4849
  return;
3880
4850
  }
@@ -3923,7 +4893,7 @@ async function handleVoucherRedeem(req, res, deps, voucherEnabled, redeemLimiter
3923
4893
  const won = await voucherDb.voucherRedeemCas(voucher.id, verifiedKeyId, absolute, now);
3924
4894
  if (!won) {
3925
4895
  const after = await voucherDb.voucherGetByHash(codeHash);
3926
- if (_optionalChain([after, 'optionalAccess', _157 => _157.status]) === "redeemed" && after.redeemedByKeyId === verifiedKeyId) {
4896
+ if (_optionalChain([after, 'optionalAccess', _243 => _243.status]) === "redeemed" && after.redeemedByKeyId === verifiedKeyId) {
3927
4897
  return after.grantApplied === true ? { ok: true, result: currentResult(after.type, await deps.db.outboundApiKeysGetByHash(keyHash)) } : await applyFirstPass(after, recordedAbsolute(after));
3928
4898
  }
3929
4899
  return { ok: false, status: 409, message: "Code was already redeemed" };
@@ -3984,10 +4954,10 @@ function extractPresentedKey(req) {
3984
4954
  }
3985
4955
  const xApiKey = req.headers["x-api-key"];
3986
4956
  const xApi = Array.isArray(xApiKey) ? xApiKey[0] : xApiKey;
3987
- if (_optionalChain([xApi, 'optionalAccess', _158 => _158.trim, 'call', _159 => _159()])) return xApi.trim();
4957
+ if (_optionalChain([xApi, 'optionalAccess', _244 => _244.trim, 'call', _245 => _245()])) return xApi.trim();
3988
4958
  const goog = req.headers["x-goog-api-key"];
3989
4959
  const g = Array.isArray(goog) ? goog[0] : goog;
3990
- return _optionalChain([g, 'optionalAccess', _160 => _160.trim, 'call', _161 => _161()]) || void 0;
4960
+ return _optionalChain([g, 'optionalAccess', _246 => _246.trim, 'call', _247 => _247()]) || void 0;
3991
4961
  }
3992
4962
  function isLoopbackPeer(address) {
3993
4963
  if (!address) return false;
@@ -3996,7 +4966,7 @@ function isLoopbackPeer(address) {
3996
4966
  }
3997
4967
  function selectEndpoint(method, url) {
3998
4968
  if (method !== "POST" || !url) return null;
3999
- const path = _nullishCoalesce(_optionalChain([url, 'access', _162 => _162.split, 'call', _163 => _163("?"), 'access', _164 => _164[0], 'optionalAccess', _165 => _165.replace, 'call', _166 => _166(/\/+$/, "")]), () => ( ""));
4969
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _248 => _248.split, 'call', _249 => _249("?"), 'access', _250 => _250[0], 'optionalAccess', _251 => _251.replace, 'call', _252 => _252(/\/+$/, "")]), () => ( ""));
4000
4970
  if (path.endsWith("/chat/completions")) return "chat";
4001
4971
  if (path.endsWith("/responses")) return "responses";
4002
4972
  if (path.includes("/v1/messages")) return "messages";
@@ -4014,7 +4984,7 @@ function extractGeminiModelFromUrl(url) {
4014
4984
  }
4015
4985
  function isModelsListRequest(url) {
4016
4986
  if (!url) return false;
4017
- const path = _nullishCoalesce(_optionalChain([url, 'access', _167 => _167.split, 'call', _168 => _168("?"), 'access', _169 => _169[0], 'optionalAccess', _170 => _170.replace, 'call', _171 => _171(/\/+$/, "")]), () => ( ""));
4987
+ const path = _nullishCoalesce(_optionalChain([url, 'access', _253 => _253.split, 'call', _254 => _254("?"), 'access', _255 => _255[0], 'optionalAccess', _256 => _256.replace, 'call', _257 => _257(/\/+$/, "")]), () => ( ""));
4018
4988
  return path.endsWith("/models");
4019
4989
  }
4020
4990
  function writeModelsList(res, config, apiKeyId, allowedEndpoints) {
@@ -4062,7 +5032,7 @@ function shimAuthHeader(req, routeToken) {
4062
5032
  }
4063
5033
  async function handleOutboundRequest(req, res, deps, config, rateLimiter, serialQueue, concurrencyGate, redeemLimiter, redeemMutex) {
4064
5034
  const now = Date.now();
4065
- const audit = _chunkUNWKG6EIcjs.beginAuditCapture.call(void 0, req, res, now);
5035
+ const audit = _chunkXPIOJQS2cjs.beginAuditCapture.call(void 0, req, res, now);
4066
5036
  const billing = _chunkAPYG5QD7cjs.beginBillingCapture.call(void 0, req, res, now);
4067
5037
  const presented = extractPresentedKey(req);
4068
5038
  const verification = await verifyKey(deps.db, presented, now);
@@ -4077,7 +5047,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4077
5047
  const verified = verification.key;
4078
5048
  if (audit) audit.keyId = verified.id;
4079
5049
  if (billing) billing.keyId = verified.id;
4080
- if (verified.loopbackOnly && !isLoopbackPeer(_optionalChain([req, 'access', _172 => _172.socket, 'optionalAccess', _173 => _173.remoteAddress]))) {
5050
+ if (verified.loopbackOnly && !isLoopbackPeer(_optionalChain([req, 'access', _258 => _258.socket, 'optionalAccess', _259 => _259.remoteAddress]))) {
4081
5051
  writeJsonError(res, 403, "This integration key is restricted to loopback clients");
4082
5052
  return;
4083
5053
  }
@@ -4090,7 +5060,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4090
5060
  req,
4091
5061
  res,
4092
5062
  deps,
4093
- _optionalChain([config, 'access', _174 => _174.voucher, 'optionalAccess', _175 => _175.enabled]) === true,
5063
+ _optionalChain([config, 'access', _260 => _260.voucher, 'optionalAccess', _261 => _261.enabled]) === true,
4094
5064
  _nullishCoalesce(redeemLimiter, () => ( getFallbackRedeemLimiter())),
4095
5065
  verified.id,
4096
5066
  _nullishCoalesce(presented, () => ( "")),
@@ -4188,7 +5158,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4188
5158
  let parsedBody = {};
4189
5159
  try {
4190
5160
  parsedBody = rawBody ? JSON.parse(rawBody) : {};
4191
- } catch (e25) {
5161
+ } catch (e28) {
4192
5162
  writeJsonError(res, 400, "Invalid JSON in request body");
4193
5163
  return;
4194
5164
  }
@@ -4259,7 +5229,7 @@ async function handleOutboundRequest(req, res, deps, config, rateLimiter, serial
4259
5229
  }
4260
5230
  let releaseSerial = null;
4261
5231
  const providerKey = resolved.route.providerId;
4262
- if (_optionalChain([config, 'access', _176 => _176.userMessageQueue, 'optionalAccess', _177 => _177.enabled]) && providerKey && isUserMessageRequest(endpoint, parsedBody)) {
5232
+ if (_optionalChain([config, 'access', _262 => _262.userMessageQueue, 'optionalAccess', _263 => _263.enabled]) && providerKey && isUserMessageRequest(endpoint, parsedBody)) {
4263
5233
  const umq = config.userMessageQueue;
4264
5234
  try {
4265
5235
  const serialSlot = await serialQueue.acquire(providerKey, {
@@ -4337,37 +5307,37 @@ function makeReplayRequest(req, rawBody) {
4337
5307
  var DEFAULT_OUTBOUND_PORT = 8765;
4338
5308
  var LOOPBACK_ADDR = "127.0.0.1";
4339
5309
  var LAN_ADDR = "0.0.0.0";
4340
- var OutboundApiServer = (_class11 = class {
4341
- constructor(deps, onPortChange) {;_class11.prototype.__init12.call(this);_class11.prototype.__init13.call(this);_class11.prototype.__init14.call(this);_class11.prototype.__init15.call(this);_class11.prototype.__init16.call(this);_class11.prototype.__init17.call(this);_class11.prototype.__init18.call(this);_class11.prototype.__init19.call(this);_class11.prototype.__init20.call(this);_class11.prototype.__init21.call(this);
5310
+ var OutboundApiServer = (_class13 = class {
5311
+ constructor(deps, onPortChange) {;_class13.prototype.__init19.call(this);_class13.prototype.__init20.call(this);_class13.prototype.__init21.call(this);_class13.prototype.__init22.call(this);_class13.prototype.__init23.call(this);_class13.prototype.__init24.call(this);_class13.prototype.__init25.call(this);_class13.prototype.__init26.call(this);_class13.prototype.__init27.call(this);_class13.prototype.__init28.call(this);
4342
5312
  this.deps = deps;
4343
5313
  this.onPortChange = onPortChange;
4344
5314
  }
4345
5315
 
4346
5316
 
4347
- __init12() {this.server = null}
4348
- __init13() {this.boundPort = 0}
4349
- __init14() {this.boundAddr = LOOPBACK_ADDR}
4350
- __init15() {this.endpoints = []}
4351
- __init16() {this.bindings = []}
5317
+ __init19() {this.server = null}
5318
+ __init20() {this.boundPort = 0}
5319
+ __init21() {this.boundAddr = LOOPBACK_ADDR}
5320
+ __init22() {this.endpoints = []}
5321
+ __init23() {this.bindings = []}
4352
5322
 
4353
5323
 
4354
5324
 
4355
- __init17() {this.rateLimiter = new OutboundRateLimiter()}
5325
+ __init24() {this.rateLimiter = new OutboundRateLimiter()}
4356
5326
  /**
4357
5327
  * Redeem-attempt limiter (voucher-redemption #9, design D6) — a SEPARATE bucket
4358
5328
  * from the traffic `rateLimiter`, keyed by the authenticating key id, so
4359
5329
  * brute-forcing `CC_` codes is throttled (a handful/min) without touching the
4360
5330
  * per-key request rate. Conservative fixed defaults (10 / 60s).
4361
5331
  */
4362
- __init18() {this.redeemLimiter = new OutboundRateLimiter({ maxRequests: 10, windowMs: 6e4 })}
5332
+ __init25() {this.redeemLimiter = new OutboundRateLimiter({ maxRequests: 10, windowMs: 6e4 })}
4363
5333
  /**
4364
5334
  * Per-key redeem mutex (voucher-redemption #9, MJ1 fix). One instance for the
4365
5335
  * server's lifetime so concurrent redeem REQUESTS for the same key serialize
4366
5336
  * (relative grant increments accumulate instead of clobbering a snapshot).
4367
5337
  */
4368
- __init19() {this.redeemMutex = new KeyedMutex()}
4369
- __init20() {this.serialQueue = new UserMessageSerialQueue()}
4370
- __init21() {this.concurrencyGate = new OutboundConcurrencyGate()}
5338
+ __init26() {this.redeemMutex = new KeyedMutex()}
5339
+ __init27() {this.serialQueue = new UserMessageSerialQueue()}
5340
+ __init28() {this.concurrencyGate = new OutboundConcurrencyGate()}
4371
5341
  /**
4372
5342
  * Apply a config. Restarts the listener ONLY when the bind address or port
4373
5343
  * changes (or when toggling enabled); per-endpoint routing config is updated
@@ -4399,7 +5369,7 @@ var OutboundApiServer = (_class11 = class {
4399
5369
  const actualPort = await this.listen(bindAddr, port);
4400
5370
  this.boundAddr = bindAddr;
4401
5371
  this.boundPort = actualPort;
4402
- if (actualPort !== port) _optionalChain([this, 'access', _178 => _178.onPortChange, 'optionalCall', _179 => _179(actualPort)]);
5372
+ if (actualPort !== port) _optionalChain([this, 'access', _264 => _264.onPortChange, 'optionalCall', _265 => _265(actualPort)]);
4403
5373
  this.logInfo(`[OutboundApiServer] Listening on ${bindAddr}:${actualPort}`);
4404
5374
  return actualPort;
4405
5375
  }
@@ -4468,7 +5438,7 @@ var OutboundApiServer = (_class11 = class {
4468
5438
  const provider = this.deps.healthReportProvider;
4469
5439
  if (!provider) return false;
4470
5440
  if (req.method !== "GET" && req.method !== "HEAD") return false;
4471
- const path = _optionalChain([(_nullishCoalesce(req.url, () => ( "/"))), 'access', _180 => _180.split, 'call', _181 => _181("?"), 'access', _182 => _182[0], 'optionalAccess', _183 => _183.replace, 'call', _184 => _184(/\/+$/, "")]) || "/";
5441
+ const path = _optionalChain([(_nullishCoalesce(req.url, () => ( "/"))), 'access', _266 => _266.split, 'call', _267 => _267("?"), 'access', _268 => _268[0], 'optionalAccess', _269 => _269.replace, 'call', _270 => _270(/\/+$/, "")]) || "/";
4472
5442
  if (path !== "/health" && path !== "/healthz") return false;
4473
5443
  const report = provider();
4474
5444
  res.writeHead(_healthloggingtypes.healthHttpStatus.call(void 0, report.status), { "Content-Type": "application/json" });
@@ -4538,7 +5508,7 @@ var OutboundApiServer = (_class11 = class {
4538
5508
  concurrency: this.concurrencyGate.getStatus()
4539
5509
  };
4540
5510
  }
4541
- }, _class11);
5511
+ }, _class13);
4542
5512
  function formatUrls(base) {
4543
5513
  return {
4544
5514
  chat: `${base}/v1/chat/completions`,
@@ -4579,13 +5549,13 @@ function startOfLocalWeek(ts) {
4579
5549
  day.setDate(day.getDate() - day.getDay());
4580
5550
  return day.getTime();
4581
5551
  }
4582
- var KeySpendTracker = (_class12 = class {
4583
- constructor(seeder) {;_class12.prototype.__init22.call(this);
5552
+ var KeySpendTracker = (_class14 = class {
5553
+ constructor(seeder) {;_class14.prototype.__init29.call(this);
4584
5554
  this.seeder = seeder;
4585
5555
  }
4586
5556
 
4587
5557
  /** apiKeyId → live accumulated spend (seeded lazily). */
4588
- __init22() {this.spend = /* @__PURE__ */ new Map()}
5558
+ __init29() {this.spend = /* @__PURE__ */ new Map()}
4589
5559
  /**
4590
5560
  * Read the key's accumulated spend, seeding from the durable store on a cache
4591
5561
  * miss and rolling the daily/weekly windows on a hit.
@@ -4638,7 +5608,7 @@ var KeySpendTracker = (_class12 = class {
4638
5608
  });
4639
5609
  return { dailyUsd, dailyWindowStart, weeklyUsd, weeklyWindowStart, totalUsd };
4640
5610
  }
4641
- }, _class12);
5611
+ }, _class14);
4642
5612
  function rollWindows(s, now) {
4643
5613
  const day = startOfLocalDay(now);
4644
5614
  if (s.dailyWindowStart !== day) {
@@ -4746,14 +5716,14 @@ async function callAnthropicCompletion(provider, apiKey, options, logger) {
4746
5716
  };
4747
5717
  }
4748
5718
  const anthropicResponse2 = await response2.json();
4749
- const openaiResponse2 = _chunkWYTEQ4Q5cjs.convertAnthropicToOpenAI.call(void 0, anthropicResponse2);
5719
+ const openaiResponse2 = _chunkGDIXXKF2cjs.convertAnthropicToOpenAI.call(void 0, anthropicResponse2);
4750
5720
  const choice2 = openaiResponse2.choices[0];
4751
5721
  return {
4752
5722
  success: true,
4753
5723
  message: {
4754
5724
  id: `assistant-${Date.now()}`,
4755
5725
  role: "assistant",
4756
- content: _optionalChain([choice2, 'access', _185 => _185.message, 'optionalAccess', _186 => _186.content]) || "",
5726
+ content: _optionalChain([choice2, 'access', _271 => _271.message, 'optionalAccess', _272 => _272.content]) || "",
4757
5727
  timestamp: Date.now()
4758
5728
  },
4759
5729
  usage: openaiResponse2.usage ? {
@@ -4777,7 +5747,7 @@ async function callAnthropicCompletion(provider, apiKey, options, logger) {
4777
5747
  const config = {
4778
5748
  defaultModel: options.model
4779
5749
  };
4780
- const anthropicRequest = _chunkWYTEQ4Q5cjs.convertOpenAIToAnthropic.call(void 0, openaiRequest, config);
5750
+ const anthropicRequest = _chunkGDIXXKF2cjs.convertOpenAIToAnthropic.call(void 0, openaiRequest, config);
4781
5751
  const apiUrl = _chunkA2YMUCBTcjs.buildProviderApiUrl.call(void 0, provider, { model: options.model, stream: false });
4782
5752
  const headers = _chunkQ27JY5RGcjs.getProviderHeaders.call(void 0, provider, apiKey);
4783
5753
  logger.info("Calling Anthropic completion API", { url: apiUrl, model: options.model });
@@ -4794,7 +5764,7 @@ async function callAnthropicCompletion(provider, apiKey, options, logger) {
4794
5764
  };
4795
5765
  }
4796
5766
  const anthropicResponse = await response.json();
4797
- const openaiResponse = _chunkWYTEQ4Q5cjs.convertAnthropicToOpenAI.call(void 0, anthropicResponse);
5767
+ const openaiResponse = _chunkGDIXXKF2cjs.convertAnthropicToOpenAI.call(void 0, anthropicResponse);
4798
5768
  const choice = openaiResponse.choices[0];
4799
5769
  return {
4800
5770
  success: true,
@@ -4858,7 +5828,7 @@ async function callGeminiCompletion(provider, apiKey, options, logger) {
4858
5828
  const candidate = candidates[0];
4859
5829
  const content = candidate.content;
4860
5830
  let textContent = "";
4861
- if (_optionalChain([content, 'optionalAccess', _187 => _187.parts])) {
5831
+ if (_optionalChain([content, 'optionalAccess', _273 => _273.parts])) {
4862
5832
  for (const part of content.parts) {
4863
5833
  if (part.text) {
4864
5834
  textContent += part.text;
@@ -4991,7 +5961,7 @@ async function streamOpenAICompletion(provider, apiKey, options, messageId, call
4991
5961
  });
4992
5962
  if (!response.ok) {
4993
5963
  const errorText = await response.text();
4994
- _optionalChain([callbacks, 'access', _188 => _188.onError, 'optionalCall', _189 => _189(`API error (${response.status}): ${errorText}`)]);
5964
+ _optionalChain([callbacks, 'access', _274 => _274.onError, 'optionalCall', _275 => _275(`API error (${response.status}): ${errorText}`)]);
4995
5965
  return;
4996
5966
  }
4997
5967
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "openai", {
@@ -5001,7 +5971,7 @@ async function streamOpenAICompletion(provider, apiKey, options, messageId, call
5001
5971
  onVideo: callbacks.onVideo,
5002
5972
  onError: callbacks.onError
5003
5973
  });
5004
- _optionalChain([callbacks, 'access', _190 => _190.onDone, 'optionalCall', _191 => _191(
5974
+ _optionalChain([callbacks, 'access', _276 => _276.onDone, 'optionalCall', _277 => _277(
5005
5975
  {
5006
5976
  id: messageId,
5007
5977
  role: "assistant",
@@ -5059,7 +6029,7 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5059
6029
  temperature: thinkingConfig ? void 0 : options.temperature,
5060
6030
  stream: true
5061
6031
  };
5062
- anthropicRequest = _chunkWYTEQ4Q5cjs.convertOpenAIToAnthropic.call(void 0, openaiRequest, config);
6032
+ anthropicRequest = _chunkGDIXXKF2cjs.convertOpenAIToAnthropic.call(void 0, openaiRequest, config);
5063
6033
  if (thinkingConfig) {
5064
6034
  anthropicRequest.thinking = thinkingConfig;
5065
6035
  delete anthropicRequest.temperature;
@@ -5073,7 +6043,7 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5073
6043
  logger.info("Streaming Anthropic completion request", {
5074
6044
  url: apiUrl,
5075
6045
  model: anthropicRequest.model,
5076
- messagesCount: _optionalChain([anthropicRequest, 'access', _192 => _192.messages, 'optionalAccess', _193 => _193.length]),
6046
+ messagesCount: _optionalChain([anthropicRequest, 'access', _278 => _278.messages, 'optionalAccess', _279 => _279.length]),
5077
6047
  max_tokens: anthropicRequest.max_tokens,
5078
6048
  temperature: anthropicRequest.temperature,
5079
6049
  stream: anthropicRequest.stream,
@@ -5090,7 +6060,7 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5090
6060
  if (!response.ok) {
5091
6061
  const errorText = await response.text();
5092
6062
  logger.error("Anthropic API error", void 0, { status: response.status, errorText });
5093
- _optionalChain([callbacks, 'access', _194 => _194.onError, 'optionalCall', _195 => _195(`API error (${response.status}): ${errorText}`)]);
6063
+ _optionalChain([callbacks, 'access', _280 => _280.onError, 'optionalCall', _281 => _281(`API error (${response.status}): ${errorText}`)]);
5094
6064
  return;
5095
6065
  }
5096
6066
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "anthropic", {
@@ -5101,10 +6071,10 @@ async function streamAnthropicCompletion(provider, apiKey, options, messageId, c
5101
6071
  });
5102
6072
  logger.info("Anthropic stream complete", {
5103
6073
  contentLength: result.content.length,
5104
- reasoningLength: _optionalChain([result, 'access', _196 => _196.reasoning, 'optionalAccess', _197 => _197.length]) || 0,
6074
+ reasoningLength: _optionalChain([result, 'access', _282 => _282.reasoning, 'optionalAccess', _283 => _283.length]) || 0,
5105
6075
  blocksCount: result.blocks.length
5106
6076
  });
5107
- _optionalChain([callbacks, 'access', _198 => _198.onDone, 'optionalCall', _199 => _199(
6077
+ _optionalChain([callbacks, 'access', _284 => _284.onDone, 'optionalCall', _285 => _285(
5108
6078
  {
5109
6079
  id: messageId,
5110
6080
  role: "assistant",
@@ -5160,7 +6130,7 @@ async function streamGeminiCompletion(provider, apiKey, options, messageId, call
5160
6130
  if (!response.ok) {
5161
6131
  const errorText = await response.text();
5162
6132
  logger.error("Gemini API error", void 0, { status: response.status, errorText });
5163
- _optionalChain([callbacks, 'access', _200 => _200.onError, 'optionalCall', _201 => _201(`API error (${response.status}): ${errorText}`)]);
6133
+ _optionalChain([callbacks, 'access', _286 => _286.onError, 'optionalCall', _287 => _287(`API error (${response.status}): ${errorText}`)]);
5164
6134
  return;
5165
6135
  }
5166
6136
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "gemini", {
@@ -5169,7 +6139,7 @@ async function streamGeminiCompletion(provider, apiKey, options, messageId, call
5169
6139
  onError: callbacks.onError
5170
6140
  });
5171
6141
  logger.info("Gemini stream complete", { contentLength: result.content.length });
5172
- _optionalChain([callbacks, 'access', _202 => _202.onDone, 'optionalCall', _203 => _203(
6142
+ _optionalChain([callbacks, 'access', _288 => _288.onDone, 'optionalCall', _289 => _289(
5173
6143
  {
5174
6144
  id: messageId,
5175
6145
  role: "assistant",
@@ -5221,7 +6191,7 @@ async function streamOpenAIResponseCompletion(provider, apiKey, options, message
5221
6191
  });
5222
6192
  if (!response.ok) {
5223
6193
  const errorText = await response.text();
5224
- _optionalChain([callbacks, 'access', _204 => _204.onError, 'optionalCall', _205 => _205(`API error (${response.status}): ${errorText}`)]);
6194
+ _optionalChain([callbacks, 'access', _290 => _290.onError, 'optionalCall', _291 => _291(`API error (${response.status}): ${errorText}`)]);
5225
6195
  return;
5226
6196
  }
5227
6197
  const result = await _chunkHQP3K7PUcjs.streamSSEResponse.call(void 0, response, "openai-response", {
@@ -5230,7 +6200,7 @@ async function streamOpenAIResponseCompletion(provider, apiKey, options, message
5230
6200
  onError: callbacks.onError
5231
6201
  });
5232
6202
  logger.info("OpenAI Response API stream complete", { contentLength: result.content.length });
5233
- _optionalChain([callbacks, 'access', _206 => _206.onDone, 'optionalCall', _207 => _207(
6203
+ _optionalChain([callbacks, 'access', _292 => _292.onDone, 'optionalCall', _293 => _293(
5234
6204
  {
5235
6205
  id: messageId,
5236
6206
  role: "assistant",
@@ -5258,7 +6228,7 @@ async function resolveEffectiveMaxTokens(llmConfig, _getProvider, logger, _provi
5258
6228
  }
5259
6229
  try {
5260
6230
  const globalParams = await llmConfig.getGlobalModelParameters();
5261
- if (_optionalChain([globalParams, 'optionalAccess', _208 => _208.maxTokens, 'optionalAccess', _209 => _209.enabled]) && globalParams.maxTokens.value > 0) {
6231
+ if (_optionalChain([globalParams, 'optionalAccess', _294 => _294.maxTokens, 'optionalAccess', _295 => _295.enabled]) && globalParams.maxTokens.value > 0) {
5262
6232
  logger.debug("Using global maxTokens", { maxTokens: globalParams.maxTokens.value });
5263
6233
  return globalParams.maxTokens.value;
5264
6234
  }
@@ -5286,7 +6256,7 @@ async function resolveThinkingBudget(getProvider, logger, providerId, modelId, m
5286
6256
  }
5287
6257
  const thinkingBudget = _thinkingconfig.calculateThinkingBudget.call(void 0, modelId, thinkLevel, maxTokens);
5288
6258
  const provider = await getProvider(providerId);
5289
- const providerName = _optionalChain([provider, 'optionalAccess', _210 => _210.name, 'optionalAccess', _211 => _211.toLowerCase, 'call', _212 => _212()]) || "";
6259
+ const providerName = _optionalChain([provider, 'optionalAccess', _296 => _296.name, 'optionalAccess', _297 => _297.toLowerCase, 'call', _298 => _298()]) || "";
5290
6260
  const apiFormat = provider ? _chunkA2YMUCBTcjs.resolveApiFormat.call(void 0, provider) : "openai";
5291
6261
  if (apiFormat === "anthropic" || providerName === "anthropic" || providerName.includes("claude")) {
5292
6262
  const thinkingConfig = _thinkingconfig.buildAnthropicThinking.call(void 0, modelId, thinkLevel, maxTokens);
@@ -5323,7 +6293,7 @@ function logToolFormat(tools, logger) {
5323
6293
  tools: openaiTools.map((t) => t.function.name),
5324
6294
  firstTool: {
5325
6295
  name: firstTool.function.name,
5326
- description: _optionalChain([firstTool, 'access', _213 => _213.function, 'access', _214 => _214.description, 'optionalAccess', _215 => _215.slice, 'call', _216 => _216(0, 100)]),
6296
+ description: _optionalChain([firstTool, 'access', _299 => _299.function, 'access', _300 => _300.description, 'optionalAccess', _301 => _301.slice, 'call', _302 => _302(0, 100)]),
5327
6297
  parameters: firstTool.function.parameters
5328
6298
  }
5329
6299
  });
@@ -5334,7 +6304,7 @@ function logToolFormat(tools, logger) {
5334
6304
  tools: anthropicTools.map((t) => t.name),
5335
6305
  firstTool: {
5336
6306
  name: firstTool.name,
5337
- description: _optionalChain([firstTool, 'access', _217 => _217.description, 'optionalAccess', _218 => _218.slice, 'call', _219 => _219(0, 100)]),
6307
+ description: _optionalChain([firstTool, 'access', _303 => _303.description, 'optionalAccess', _304 => _304.slice, 'call', _305 => _305(0, 100)]),
5338
6308
  input_schema: firstTool.input_schema
5339
6309
  }
5340
6310
  });
@@ -5345,9 +6315,9 @@ function logToolFormat(tools, logger) {
5345
6315
  count: allDeclarations.length,
5346
6316
  tools: allDeclarations.map((t) => t.name),
5347
6317
  firstTool: {
5348
- name: _optionalChain([allDeclarations, 'access', _220 => _220[0], 'optionalAccess', _221 => _221.name]),
5349
- description: _optionalChain([allDeclarations, 'access', _222 => _222[0], 'optionalAccess', _223 => _223.description, 'optionalAccess', _224 => _224.slice, 'call', _225 => _225(0, 100)]),
5350
- parameters: _optionalChain([allDeclarations, 'access', _226 => _226[0], 'optionalAccess', _227 => _227.parameters])
6318
+ name: _optionalChain([allDeclarations, 'access', _306 => _306[0], 'optionalAccess', _307 => _307.name]),
6319
+ description: _optionalChain([allDeclarations, 'access', _308 => _308[0], 'optionalAccess', _309 => _309.description, 'optionalAccess', _310 => _310.slice, 'call', _311 => _311(0, 100)]),
6320
+ parameters: _optionalChain([allDeclarations, 'access', _312 => _312[0], 'optionalAccess', _313 => _313.parameters])
5351
6321
  }
5352
6322
  });
5353
6323
  }
@@ -5381,7 +6351,7 @@ function buildToolRequest(apiFormat, conversationMessages, actualModel, options,
5381
6351
  // Native Anthropic Messages requires this field. Resolve an absent
5382
6352
  // caller cap to the model's real ceiling rather than a small constant —
5383
6353
  // see `resolveAnthropicMaxTokens`.
5384
- max_tokens: _chunk4VLRHHFEcjs.resolveAnthropicMaxTokens.call(void 0, actualModel, options.maxTokens),
6354
+ max_tokens: _chunkOXHNSZ53cjs.resolveAnthropicMaxTokens.call(void 0, actualModel, options.maxTokens),
5385
6355
  temperature: _nullishCoalesce(options.temperature, () => ( 0.7)),
5386
6356
  stream: true,
5387
6357
  tools: options.tools
@@ -5410,7 +6380,7 @@ function buildToolRequest(apiFormat, conversationMessages, actualModel, options,
5410
6380
  function extractDeltaContent(rawJson, apiFormat) {
5411
6381
  const json = rawJson;
5412
6382
  if (apiFormat === "google") {
5413
- const parts = _optionalChain([json, 'access', _228 => _228.candidates, 'optionalAccess', _229 => _229[0], 'optionalAccess', _230 => _230.content, 'optionalAccess', _231 => _231.parts]) || [];
6383
+ const parts = _optionalChain([json, 'access', _314 => _314.candidates, 'optionalAccess', _315 => _315[0], 'optionalAccess', _316 => _316.content, 'optionalAccess', _317 => _317.parts]) || [];
5414
6384
  let text = "";
5415
6385
  for (const part of parts) {
5416
6386
  if (part.thought === true) continue;
@@ -5418,15 +6388,15 @@ function extractDeltaContent(rawJson, apiFormat) {
5418
6388
  }
5419
6389
  return text;
5420
6390
  } else if (apiFormat === "anthropic") {
5421
- return _optionalChain([json, 'access', _232 => _232.delta, 'optionalAccess', _233 => _233.text]) || "";
6391
+ return _optionalChain([json, 'access', _318 => _318.delta, 'optionalAccess', _319 => _319.text]) || "";
5422
6392
  } else {
5423
- return _optionalChain([json, 'access', _234 => _234.choices, 'optionalAccess', _235 => _235[0], 'optionalAccess', _236 => _236.delta, 'optionalAccess', _237 => _237.content]) || "";
6393
+ return _optionalChain([json, 'access', _320 => _320.choices, 'optionalAccess', _321 => _321[0], 'optionalAccess', _322 => _322.delta, 'optionalAccess', _323 => _323.content]) || "";
5424
6394
  }
5425
6395
  }
5426
6396
  function extractDeltaReasoning(rawJson, apiFormat) {
5427
6397
  const json = rawJson;
5428
6398
  if (apiFormat === "google") {
5429
- const parts = _optionalChain([json, 'access', _238 => _238.candidates, 'optionalAccess', _239 => _239[0], 'optionalAccess', _240 => _240.content, 'optionalAccess', _241 => _241.parts]) || [];
6399
+ const parts = _optionalChain([json, 'access', _324 => _324.candidates, 'optionalAccess', _325 => _325[0], 'optionalAccess', _326 => _326.content, 'optionalAccess', _327 => _327.parts]) || [];
5430
6400
  let reasoning = "";
5431
6401
  for (const part of parts) {
5432
6402
  if (part.thought === true && part.text) reasoning += part.text;
@@ -5435,14 +6405,14 @@ function extractDeltaReasoning(rawJson, apiFormat) {
5435
6405
  } else if (apiFormat === "anthropic") {
5436
6406
  return "";
5437
6407
  } else {
5438
- const delta = _optionalChain([json, 'access', _242 => _242.choices, 'optionalAccess', _243 => _243[0], 'optionalAccess', _244 => _244.delta]);
5439
- return (_optionalChain([delta, 'optionalAccess', _245 => _245.thinking, 'optionalAccess', _246 => _246.content]) || "") + (_optionalChain([delta, 'optionalAccess', _247 => _247.reasoning_content]) || "");
6408
+ const delta = _optionalChain([json, 'access', _328 => _328.choices, 'optionalAccess', _329 => _329[0], 'optionalAccess', _330 => _330.delta]);
6409
+ return (_optionalChain([delta, 'optionalAccess', _331 => _331.thinking, 'optionalAccess', _332 => _332.content]) || "") + (_optionalChain([delta, 'optionalAccess', _333 => _333.reasoning_content]) || "");
5440
6410
  }
5441
6411
  }
5442
6412
  function extractToolCalls(rawJson, apiFormat, toolCalls, callbacks, logger, pendingOpenAIToolCalls) {
5443
6413
  const json = rawJson;
5444
6414
  if (apiFormat === "google") {
5445
- const parts = _optionalChain([json, 'access', _248 => _248.candidates, 'optionalAccess', _249 => _249[0], 'optionalAccess', _250 => _250.content, 'optionalAccess', _251 => _251.parts]) || [];
6415
+ const parts = _optionalChain([json, 'access', _334 => _334.candidates, 'optionalAccess', _335 => _335[0], 'optionalAccess', _336 => _336.content, 'optionalAccess', _337 => _337.parts]) || [];
5446
6416
  for (const part of parts) {
5447
6417
  if (part.functionCall) {
5448
6418
  logger.info("Function call detected", { functionCall: part.functionCall });
@@ -5452,13 +6422,13 @@ function extractToolCalls(rawJson, apiFormat, toolCalls, callbacks, logger, pend
5452
6422
  args: part.functionCall.args || {}
5453
6423
  };
5454
6424
  toolCalls.push(toolCall);
5455
- _optionalChain([callbacks, 'access', _252 => _252.onToolCall, 'optionalCall', _253 => _253(toolCall)]);
6425
+ _optionalChain([callbacks, 'access', _338 => _338.onToolCall, 'optionalCall', _339 => _339(toolCall)]);
5456
6426
  logger.info("Tool called", { toolName: toolCall.name });
5457
6427
  }
5458
6428
  }
5459
6429
  } else if (apiFormat !== "anthropic") {
5460
- const delta = _optionalChain([json, 'access', _254 => _254.choices, 'optionalAccess', _255 => _255[0], 'optionalAccess', _256 => _256.delta]);
5461
- if (_optionalChain([delta, 'optionalAccess', _257 => _257.tool_calls]) && pendingOpenAIToolCalls) {
6430
+ const delta = _optionalChain([json, 'access', _340 => _340.choices, 'optionalAccess', _341 => _341[0], 'optionalAccess', _342 => _342.delta]);
6431
+ if (_optionalChain([delta, 'optionalAccess', _343 => _343.tool_calls]) && pendingOpenAIToolCalls) {
5462
6432
  for (const tc of delta.tool_calls) {
5463
6433
  const index = _nullishCoalesce(tc.index, () => ( 0));
5464
6434
  let pending = pendingOpenAIToolCalls.get(index);
@@ -5467,8 +6437,8 @@ function extractToolCalls(rawJson, apiFormat, toolCalls, callbacks, logger, pend
5467
6437
  pendingOpenAIToolCalls.set(index, pending);
5468
6438
  }
5469
6439
  if (tc.id) pending.id = tc.id;
5470
- if (_optionalChain([tc, 'access', _258 => _258.function, 'optionalAccess', _259 => _259.name])) pending.name += tc.function.name;
5471
- if (_optionalChain([tc, 'access', _260 => _260.function, 'optionalAccess', _261 => _261.arguments])) pending.arguments += tc.function.arguments;
6440
+ if (_optionalChain([tc, 'access', _344 => _344.function, 'optionalAccess', _345 => _345.name])) pending.name += tc.function.name;
6441
+ if (_optionalChain([tc, 'access', _346 => _346.function, 'optionalAccess', _347 => _347.arguments])) pending.arguments += tc.function.arguments;
5472
6442
  }
5473
6443
  }
5474
6444
  }
@@ -5483,7 +6453,7 @@ function finalizeOpenAIToolCalls(pendingOpenAIToolCalls, toolCalls, callbacks, l
5483
6453
  args: JSON.parse(pending.arguments || "{}")
5484
6454
  };
5485
6455
  toolCalls.push(toolCall);
5486
- _optionalChain([callbacks, 'access', _262 => _262.onToolCall, 'optionalCall', _263 => _263(toolCall)]);
6456
+ _optionalChain([callbacks, 'access', _348 => _348.onToolCall, 'optionalCall', _349 => _349(toolCall)]);
5487
6457
  logger.info("Tool called (OpenAI)", { toolName: toolCall.name, args: toolCall.args });
5488
6458
  } catch (e) {
5489
6459
  logger.error("Failed to parse OpenAI tool call arguments", e instanceof Error ? e : void 0, {
@@ -5497,38 +6467,38 @@ function finalizeOpenAIToolCalls(pendingOpenAIToolCalls, toolCalls, callbacks, l
5497
6467
  function parseStreamChunk(rawJson, apiFormat, callbacks, logger) {
5498
6468
  const json = rawJson;
5499
6469
  if (apiFormat === "google") {
5500
- const candidate = _optionalChain([json, 'access', _264 => _264.candidates, 'optionalAccess', _265 => _265[0]]);
6470
+ const candidate = _optionalChain([json, 'access', _350 => _350.candidates, 'optionalAccess', _351 => _351[0]]);
5501
6471
  if (!candidate) {
5502
6472
  logger.warn("No candidate found in Gemini response");
5503
6473
  return;
5504
6474
  }
5505
- const parts = _optionalChain([candidate, 'access', _266 => _266.content, 'optionalAccess', _267 => _267.parts]) || [];
6475
+ const parts = _optionalChain([candidate, 'access', _352 => _352.content, 'optionalAccess', _353 => _353.parts]) || [];
5506
6476
  for (const part of parts) {
5507
6477
  if (part.thought === true && part.text) {
5508
- _optionalChain([callbacks, 'access', _268 => _268.onReasoning, 'optionalCall', _269 => _269(part.text)]);
6478
+ _optionalChain([callbacks, 'access', _354 => _354.onReasoning, 'optionalCall', _355 => _355(part.text)]);
5509
6479
  continue;
5510
6480
  } else if (part.thoughtSignature && !part.thought) {
5511
6481
  logger.debug("Gemini returned thoughtSignature without thought content");
5512
6482
  }
5513
6483
  if (part.text) {
5514
- _optionalChain([callbacks, 'access', _270 => _270.onDelta, 'optionalCall', _271 => _271(part.text)]);
6484
+ _optionalChain([callbacks, 'access', _356 => _356.onDelta, 'optionalCall', _357 => _357(part.text)]);
5515
6485
  }
5516
6486
  }
5517
6487
  } else if (apiFormat === "anthropic") {
5518
6488
  const delta = json.delta;
5519
- if (_optionalChain([delta, 'optionalAccess', _272 => _272.text])) {
5520
- _optionalChain([callbacks, 'access', _273 => _273.onDelta, 'optionalCall', _274 => _274(delta.text)]);
6489
+ if (_optionalChain([delta, 'optionalAccess', _358 => _358.text])) {
6490
+ _optionalChain([callbacks, 'access', _359 => _359.onDelta, 'optionalCall', _360 => _360(delta.text)]);
5521
6491
  }
5522
6492
  } else {
5523
- const delta = _optionalChain([json, 'access', _275 => _275.choices, 'optionalAccess', _276 => _276[0], 'optionalAccess', _277 => _277.delta]);
5524
- if (_optionalChain([delta, 'optionalAccess', _278 => _278.content])) {
5525
- _optionalChain([callbacks, 'access', _279 => _279.onDelta, 'optionalCall', _280 => _280(delta.content)]);
6493
+ const delta = _optionalChain([json, 'access', _361 => _361.choices, 'optionalAccess', _362 => _362[0], 'optionalAccess', _363 => _363.delta]);
6494
+ if (_optionalChain([delta, 'optionalAccess', _364 => _364.content])) {
6495
+ _optionalChain([callbacks, 'access', _365 => _365.onDelta, 'optionalCall', _366 => _366(delta.content)]);
5526
6496
  }
5527
- if (_optionalChain([delta, 'optionalAccess', _281 => _281.thinking, 'optionalAccess', _282 => _282.content])) {
5528
- _optionalChain([callbacks, 'access', _283 => _283.onReasoning, 'optionalCall', _284 => _284(delta.thinking.content)]);
6497
+ if (_optionalChain([delta, 'optionalAccess', _367 => _367.thinking, 'optionalAccess', _368 => _368.content])) {
6498
+ _optionalChain([callbacks, 'access', _369 => _369.onReasoning, 'optionalCall', _370 => _370(delta.thinking.content)]);
5529
6499
  }
5530
- if (_optionalChain([delta, 'optionalAccess', _285 => _285.reasoning_content])) {
5531
- _optionalChain([callbacks, 'access', _286 => _286.onReasoning, 'optionalCall', _287 => _287(delta.reasoning_content)]);
6500
+ if (_optionalChain([delta, 'optionalAccess', _371 => _371.reasoning_content])) {
6501
+ _optionalChain([callbacks, 'access', _372 => _372.onReasoning, 'optionalCall', _373 => _373(delta.reasoning_content)]);
5532
6502
  }
5533
6503
  }
5534
6504
  }
@@ -5541,7 +6511,7 @@ function buildIterationBlocks(iteration, content, reasoning, blocks, callbacks)
5541
6511
  content: reasoning
5542
6512
  };
5543
6513
  blocks.push(thinkingBlock);
5544
- _optionalChain([callbacks, 'access', _288 => _288.onBlock, 'optionalCall', _289 => _289(thinkingBlock)]);
6514
+ _optionalChain([callbacks, 'access', _374 => _374.onBlock, 'optionalCall', _375 => _375(thinkingBlock)]);
5545
6515
  }
5546
6516
  if (content && content.trim()) {
5547
6517
  const textBlock = {
@@ -5550,7 +6520,7 @@ function buildIterationBlocks(iteration, content, reasoning, blocks, callbacks)
5550
6520
  content
5551
6521
  };
5552
6522
  blocks.push(textBlock);
5553
- _optionalChain([callbacks, 'access', _290 => _290.onBlock, 'optionalCall', _291 => _291(textBlock)]);
6523
+ _optionalChain([callbacks, 'access', _376 => _376.onBlock, 'optionalCall', _377 => _377(textBlock)]);
5554
6524
  }
5555
6525
  }
5556
6526
  async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbacks, logger, builtinExecutor) {
@@ -5565,7 +6535,7 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5565
6535
  status: "running"
5566
6536
  };
5567
6537
  blocks.push(toolUseBlock);
5568
- _optionalChain([callbacks, 'access', _292 => _292.onBlock, 'optionalCall', _293 => _293(toolUseBlock)]);
6538
+ _optionalChain([callbacks, 'access', _378 => _378.onBlock, 'optionalCall', _379 => _379(toolUseBlock)]);
5569
6539
  try {
5570
6540
  logger.info("Executing tool", { toolName: toolCall.name });
5571
6541
  const mcpTool = mcpTools.find((t) => t.id === toolCall.name);
@@ -5588,8 +6558,8 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5588
6558
  result = { isError: true, content: [{ type: "text", text: "MCP service not available" }] };
5589
6559
  }
5590
6560
  logger.info("Tool result received", { result });
5591
- _optionalChain([callbacks, 'access', _294 => _294.onToolResult, 'optionalCall', _295 => _295(toolCall.id, result)]);
5592
- const resultText = _optionalChain([result, 'access', _296 => _296.content, 'optionalAccess', _297 => _297[0], 'optionalAccess', _298 => _298.text]) || JSON.stringify(result);
6561
+ _optionalChain([callbacks, 'access', _380 => _380.onToolResult, 'optionalCall', _381 => _381(toolCall.id, result)]);
6562
+ const resultText = _optionalChain([result, 'access', _382 => _382.content, 'optionalAccess', _383 => _383[0], 'optionalAccess', _384 => _384.text]) || JSON.stringify(result);
5593
6563
  const toolResultBlock = {
5594
6564
  id: `${toolCall.id}_result`,
5595
6565
  type: "tool_result",
@@ -5599,11 +6569,11 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5599
6569
  isError: result.isError
5600
6570
  };
5601
6571
  blocks.push(toolResultBlock);
5602
- _optionalChain([callbacks, 'access', _299 => _299.onBlock, 'optionalCall', _300 => _300(toolResultBlock)]);
6572
+ _optionalChain([callbacks, 'access', _385 => _385.onBlock, 'optionalCall', _386 => _386(toolResultBlock)]);
5603
6573
  const toolUseBlockRef = blocks.find((b) => b.id === `${toolCall.id}_use`);
5604
6574
  if (toolUseBlockRef && toolUseBlockRef.type === "tool_use") {
5605
6575
  toolUseBlockRef.status = result.isError ? "error" : "completed";
5606
- _optionalChain([callbacks, 'access', _301 => _301.onBlock, 'optionalCall', _302 => _302({ ...toolUseBlockRef })]);
6576
+ _optionalChain([callbacks, 'access', _387 => _387.onBlock, 'optionalCall', _388 => _388({ ...toolUseBlockRef })]);
5607
6577
  }
5608
6578
  toolResults.push({ toolCall, result });
5609
6579
  } catch (error) {
@@ -5613,7 +6583,7 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5613
6583
  isError: true,
5614
6584
  content: [{ type: "text", text: `Error: ${errorMessage}` }]
5615
6585
  };
5616
- _optionalChain([callbacks, 'access', _303 => _303.onToolResult, 'optionalCall', _304 => _304(toolCall.id, errorResult)]);
6586
+ _optionalChain([callbacks, 'access', _389 => _389.onToolResult, 'optionalCall', _390 => _390(toolCall.id, errorResult)]);
5617
6587
  const errorToolResultBlock = {
5618
6588
  id: `${toolCall.id}_result`,
5619
6589
  type: "tool_result",
@@ -5623,11 +6593,11 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5623
6593
  isError: true
5624
6594
  };
5625
6595
  blocks.push(errorToolResultBlock);
5626
- _optionalChain([callbacks, 'access', _305 => _305.onBlock, 'optionalCall', _306 => _306(errorToolResultBlock)]);
6596
+ _optionalChain([callbacks, 'access', _391 => _391.onBlock, 'optionalCall', _392 => _392(errorToolResultBlock)]);
5627
6597
  const errorToolUseBlockRef = blocks.find((b) => b.id === `${toolCall.id}_use`);
5628
6598
  if (errorToolUseBlockRef && errorToolUseBlockRef.type === "tool_use") {
5629
6599
  errorToolUseBlockRef.status = "error";
5630
- _optionalChain([callbacks, 'access', _307 => _307.onBlock, 'optionalCall', _308 => _308({ ...errorToolUseBlockRef })]);
6600
+ _optionalChain([callbacks, 'access', _393 => _393.onBlock, 'optionalCall', _394 => _394({ ...errorToolUseBlockRef })]);
5631
6601
  }
5632
6602
  toolResults.push({ toolCall, result: errorResult });
5633
6603
  }
@@ -5637,7 +6607,7 @@ async function executeToolCalls(toolCalls, mcpTools, mcpService, blocks, callbac
5637
6607
  function mapToolArguments(args, mcpTool, logger) {
5638
6608
  const mappedArgs = { ...args };
5639
6609
  const toolSchema = mcpTool.inputSchema;
5640
- if (_optionalChain([toolSchema, 'optionalAccess', _309 => _309.properties])) {
6610
+ if (_optionalChain([toolSchema, 'optionalAccess', _395 => _395.properties])) {
5641
6611
  const expectedParams = Object.keys(toolSchema.properties);
5642
6612
  const paramMappings = {
5643
6613
  "search_query": ["query", "q", "searchQuery", "search"],
@@ -5671,25 +6641,25 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
5671
6641
  options.providerId,
5672
6642
  options.model
5673
6643
  );
5674
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _310 => _310.actualProviderId]) || options.providerId;
5675
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _311 => _311.actualModelId]) || options.model;
6644
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _396 => _396.actualProviderId]) || options.providerId;
6645
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _397 => _397.actualModelId]) || options.model;
5676
6646
  const provider = await getProvider(actualProviderId);
5677
6647
  if (!provider) {
5678
- _optionalChain([callbacks, 'access', _312 => _312.onError, 'optionalCall', _313 => _313(`Provider not found: ${actualProviderId}`)]);
6648
+ _optionalChain([callbacks, 'access', _398 => _398.onError, 'optionalCall', _399 => _399(`Provider not found: ${actualProviderId}`)]);
5679
6649
  return;
5680
6650
  }
5681
6651
  if (!provider.enabled) {
5682
- _optionalChain([callbacks, 'access', _314 => _314.onError, 'optionalCall', _315 => _315(`Provider is disabled: ${provider.name}`)]);
6652
+ _optionalChain([callbacks, 'access', _400 => _400.onError, 'optionalCall', _401 => _401(`Provider is disabled: ${provider.name}`)]);
5683
6653
  return;
5684
6654
  }
5685
6655
  const { apiKey: effectiveKey } = _chunkA2YMUCBTcjs.resolveProviderEndpoint.call(void 0, provider);
5686
6656
  const apiKey = resolveApiKey2(effectiveKey);
5687
6657
  if (!apiKey) {
5688
- _optionalChain([callbacks, 'access', _316 => _316.onError, 'optionalCall', _317 => _317("API key not configured")]);
6658
+ _optionalChain([callbacks, 'access', _402 => _402.onError, 'optionalCall', _403 => _403("API key not configured")]);
5689
6659
  return;
5690
6660
  }
5691
6661
  const messageId = `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`;
5692
- _optionalChain([callbacks, 'access', _318 => _318.onStart, 'optionalCall', _319 => _319(messageId)]);
6662
+ _optionalChain([callbacks, 'access', _404 => _404.onStart, 'optionalCall', _405 => _405(messageId)]);
5693
6663
  const globalParams = await llmConfig.getGlobalModelParameters();
5694
6664
  const MAX_ITERATIONS = _nullishCoalesce(globalParams.toolMaxTurns, () => ( 100));
5695
6665
  let iteration = 0;
@@ -5736,12 +6706,12 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
5736
6706
  });
5737
6707
  if (!response.ok) {
5738
6708
  const errorText = await response.text();
5739
- _optionalChain([callbacks, 'access', _320 => _320.onError, 'optionalCall', _321 => _321(`API error (${response.status}): ${errorText}`)]);
6709
+ _optionalChain([callbacks, 'access', _406 => _406.onError, 'optionalCall', _407 => _407(`API error (${response.status}): ${errorText}`)]);
5740
6710
  return;
5741
6711
  }
5742
- const reader = _optionalChain([response, 'access', _322 => _322.body, 'optionalAccess', _323 => _323.getReader, 'call', _324 => _324()]);
6712
+ const reader = _optionalChain([response, 'access', _408 => _408.body, 'optionalAccess', _409 => _409.getReader, 'call', _410 => _410()]);
5743
6713
  if (!reader) {
5744
- _optionalChain([callbacks, 'access', _325 => _325.onError, 'optionalCall', _326 => _326("No response body")]);
6714
+ _optionalChain([callbacks, 'access', _411 => _411.onError, 'optionalCall', _412 => _412("No response body")]);
5745
6715
  return;
5746
6716
  }
5747
6717
  const decoder = new TextDecoder();
@@ -5774,7 +6744,7 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
5774
6744
  logger.debug("Parsed JSON", { json });
5775
6745
  if (json.error) {
5776
6746
  logger.error("API Error", void 0, { error: json.error });
5777
- _optionalChain([callbacks, 'access', _327 => _327.onError, 'optionalCall', _328 => _328(`API Error: ${json.error.message || JSON.stringify(json.error)}`)]);
6747
+ _optionalChain([callbacks, 'access', _413 => _413.onError, 'optionalCall', _414 => _414(`API Error: ${json.error.message || JSON.stringify(json.error)}`)]);
5778
6748
  return;
5779
6749
  }
5780
6750
  parseStreamChunk(json, apiFormat, callbacks, logger);
@@ -5807,7 +6777,7 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
5807
6777
  toolCallsLength: toolCalls.length,
5808
6778
  executableToolCallsLength: executableToolCalls.length,
5809
6779
  hasMcpTools: !!options.mcpTools,
5810
- mcpToolsLength: _optionalChain([options, 'access', _329 => _329.mcpTools, 'optionalAccess', _330 => _330.length]),
6780
+ mcpToolsLength: _optionalChain([options, 'access', _415 => _415.mcpTools, 'optionalAccess', _416 => _416.length]),
5811
6781
  hasMcpService: !!mcpService
5812
6782
  });
5813
6783
  if (executableToolCalls.length > 0 && options.mcpTools && (mcpService || builtinExecutor)) {
@@ -5828,7 +6798,7 @@ async function streamWithTools(options, callbacks, mcpService, llmConfig, getPro
5828
6798
  builtinExecutor
5829
6799
  );
5830
6800
  const toolResultsText = toolResults.map(({ toolCall, result }) => {
5831
- const resultText = typeof result === "string" ? result : _optionalChain([result, 'access', _331 => _331.content, 'optionalAccess', _332 => _332[0], 'optionalAccess', _333 => _333.text]) || JSON.stringify(result);
6801
+ const resultText = typeof result === "string" ? result : _optionalChain([result, 'access', _417 => _417.content, 'optionalAccess', _418 => _418[0], 'optionalAccess', _419 => _419.text]) || JSON.stringify(result);
5832
6802
  return `Tool ${toolCall.name} result:
5833
6803
  ${resultText}`;
5834
6804
  }).join("\n\n");
@@ -5846,12 +6816,12 @@ ${resultText}`;
5846
6816
  }
5847
6817
  } catch (streamError) {
5848
6818
  logger.error("Stream processing error", streamError instanceof Error ? streamError : void 0);
5849
- _optionalChain([callbacks, 'access', _334 => _334.onError, 'optionalCall', _335 => _335(`Stream error: ${streamError instanceof Error ? streamError.message : "Unknown error"}`)]);
6819
+ _optionalChain([callbacks, 'access', _420 => _420.onError, 'optionalCall', _421 => _421(`Stream error: ${streamError instanceof Error ? streamError.message : "Unknown error"}`)]);
5850
6820
  return;
5851
6821
  }
5852
6822
  }
5853
6823
  logger.info("Sending final callback", { blocksCount: blocks.length });
5854
- _optionalChain([callbacks, 'access', _336 => _336.onDone, 'optionalCall', _337 => _337({
6824
+ _optionalChain([callbacks, 'access', _422 => _422.onDone, 'optionalCall', _423 => _423({
5855
6825
  id: messageId,
5856
6826
  role: "assistant",
5857
6827
  content: finalContent,
@@ -5862,7 +6832,7 @@ ${resultText}`;
5862
6832
  } catch (error) {
5863
6833
  logger.error("Error in streamWithTools", error instanceof Error ? error : void 0);
5864
6834
  const message = error instanceof Error ? error.message : "Unknown error";
5865
- _optionalChain([callbacks, 'access', _338 => _338.onError, 'optionalCall', _339 => _339(message)]);
6835
+ _optionalChain([callbacks, 'access', _424 => _424.onError, 'optionalCall', _425 => _425(message)]);
5866
6836
  }
5867
6837
  }
5868
6838
 
@@ -5913,8 +6883,8 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
5913
6883
  options.providerId,
5914
6884
  options.model
5915
6885
  );
5916
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _340 => _340.actualProviderId]) || options.providerId;
5917
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _341 => _341.actualModelId]) || options.model;
6886
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _426 => _426.actualProviderId]) || options.providerId;
6887
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _427 => _427.actualModelId]) || options.model;
5918
6888
  const provider = await getProvider(actualProviderId);
5919
6889
  if (!provider) {
5920
6890
  return { success: false, error: `Provider not found: ${actualProviderId}` };
@@ -5990,18 +6960,18 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
5990
6960
  { endpointTransformer: void 0 }
5991
6961
  );
5992
6962
  const data = await transformedResponse.json();
5993
- const choice = _optionalChain([data, 'access', _342 => _342.choices, 'optionalAccess', _343 => _343[0]]);
6963
+ const choice = _optionalChain([data, 'access', _428 => _428.choices, 'optionalAccess', _429 => _429[0]]);
5994
6964
  if (!choice) {
5995
6965
  return { success: false, error: "No choices in response" };
5996
6966
  }
5997
- const rawToolCalls = _optionalChain([choice, 'access', _344 => _344.message, 'optionalAccess', _345 => _345.tool_calls]);
5998
- const toolCalls = _optionalChain([rawToolCalls, 'optionalAccess', _346 => _346.map, 'call', _347 => _347((tc) => ({
6967
+ const rawToolCalls = _optionalChain([choice, 'access', _430 => _430.message, 'optionalAccess', _431 => _431.tool_calls]);
6968
+ const toolCalls = _optionalChain([rawToolCalls, 'optionalAccess', _432 => _432.map, 'call', _433 => _433((tc) => ({
5999
6969
  id: tc.id,
6000
6970
  name: tc.function.name,
6001
6971
  args: typeof tc.function.arguments === "string" ? JSON.parse(tc.function.arguments) : tc.function.arguments
6002
6972
  }))]);
6003
6973
  const tapped = readUsageFromOpenAIResponse(data.usage);
6004
- if (_optionalChain([recording, 'optionalAccess', _348 => _348.recorder]) && tapped) {
6974
+ if (_optionalChain([recording, 'optionalAccess', _434 => _434.recorder]) && tapped) {
6005
6975
  recording.recorder.record({
6006
6976
  messageId: _nullishCoalesce(options.messageId, () => ( null)),
6007
6977
  parentMessageId: _nullishCoalesce(options.parentMessageId, () => ( null)),
@@ -6019,9 +6989,9 @@ async function completeWithTransformers(options, llmConfig, getProvider, resolve
6019
6989
  message: {
6020
6990
  id: `msg_${Date.now()}`,
6021
6991
  role: "assistant",
6022
- content: _optionalChain([choice, 'access', _349 => _349.message, 'optionalAccess', _350 => _350.content]) || "",
6992
+ content: _optionalChain([choice, 'access', _435 => _435.message, 'optionalAccess', _436 => _436.content]) || "",
6023
6993
  timestamp: Date.now(),
6024
- thinking: data.reasoning_content || _optionalChain([choice, 'access', _351 => _351.message, 'optionalAccess', _352 => _352.thinking, 'optionalAccess', _353 => _353.content]) ? { content: data.reasoning_content || _optionalChain([choice, 'access', _354 => _354.message, 'optionalAccess', _355 => _355.thinking, 'optionalAccess', _356 => _356.content]) || "", signature: _optionalChain([choice, 'access', _357 => _357.message, 'optionalAccess', _358 => _358.thinking, 'optionalAccess', _359 => _359.signature]) } : void 0,
6994
+ thinking: data.reasoning_content || _optionalChain([choice, 'access', _437 => _437.message, 'optionalAccess', _438 => _438.thinking, 'optionalAccess', _439 => _439.content]) ? { content: data.reasoning_content || _optionalChain([choice, 'access', _440 => _440.message, 'optionalAccess', _441 => _441.thinking, 'optionalAccess', _442 => _442.content]) || "", signature: _optionalChain([choice, 'access', _443 => _443.message, 'optionalAccess', _444 => _444.thinking, 'optionalAccess', _445 => _445.signature]) } : void 0,
6025
6995
  toolCalls
6026
6996
  },
6027
6997
  usage: data.usage ? {
@@ -6042,21 +7012,21 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6042
7012
  options.providerId,
6043
7013
  options.model
6044
7014
  );
6045
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _360 => _360.actualProviderId]) || options.providerId;
6046
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _361 => _361.actualModelId]) || options.model;
7015
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _446 => _446.actualProviderId]) || options.providerId;
7016
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _447 => _447.actualModelId]) || options.model;
6047
7017
  const provider = await getProvider(actualProviderId);
6048
7018
  if (!provider) {
6049
- _optionalChain([callbacks, 'access', _362 => _362.onError, 'optionalCall', _363 => _363(`Provider not found: ${actualProviderId}`)]);
7019
+ _optionalChain([callbacks, 'access', _448 => _448.onError, 'optionalCall', _449 => _449(`Provider not found: ${actualProviderId}`)]);
6050
7020
  return;
6051
7021
  }
6052
7022
  if (!provider.enabled) {
6053
- _optionalChain([callbacks, 'access', _364 => _364.onError, 'optionalCall', _365 => _365(`Provider is disabled: ${provider.name}`)]);
7023
+ _optionalChain([callbacks, 'access', _450 => _450.onError, 'optionalCall', _451 => _451(`Provider is disabled: ${provider.name}`)]);
6054
7024
  return;
6055
7025
  }
6056
7026
  const { apiKey: effectiveKey } = _chunkA2YMUCBTcjs.resolveProviderEndpoint.call(void 0, provider);
6057
7027
  const apiKey = resolveApiKey2(effectiveKey);
6058
7028
  if (!apiKey) {
6059
- _optionalChain([callbacks, 'access', _366 => _366.onError, 'optionalCall', _367 => _367("API key not configured")]);
7029
+ _optionalChain([callbacks, 'access', _452 => _452.onError, 'optionalCall', _453 => _453("API key not configured")]);
6060
7030
  return;
6061
7031
  }
6062
7032
  const { chain, hasTransformers } = await resolveChainWithMain(
@@ -6068,7 +7038,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6068
7038
  return completeStreamFallback(options, callbacks);
6069
7039
  }
6070
7040
  const messageId = `msg_${Date.now()}`;
6071
- _optionalChain([callbacks, 'access', _368 => _368.onStart, 'optionalCall', _369 => _369(messageId)]);
7041
+ _optionalChain([callbacks, 'access', _454 => _454.onStart, 'optionalCall', _455 => _455(messageId)]);
6072
7042
  const unifiedRequest = buildUnifiedCompletionRequest(actualModel, options, true);
6073
7043
  const transformerProvider = {
6074
7044
  name: provider.name,
@@ -6107,7 +7077,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6107
7077
  });
6108
7078
  if (!response.ok) {
6109
7079
  const errorText = await response.text();
6110
- _optionalChain([callbacks, 'access', _370 => _370.onError, 'optionalCall', _371 => _371(`API error (${response.status}): ${errorText}`)]);
7080
+ _optionalChain([callbacks, 'access', _456 => _456.onError, 'optionalCall', _457 => _457(`API error (${response.status}): ${errorText}`)]);
6111
7081
  return;
6112
7082
  }
6113
7083
  const transformedResponse = await executor.executeResponseChain(
@@ -6117,9 +7087,9 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6117
7087
  chain,
6118
7088
  { endpointTransformer: void 0 }
6119
7089
  );
6120
- const reader = _optionalChain([transformedResponse, 'access', _372 => _372.body, 'optionalAccess', _373 => _373.getReader, 'call', _374 => _374()]);
7090
+ const reader = _optionalChain([transformedResponse, 'access', _458 => _458.body, 'optionalAccess', _459 => _459.getReader, 'call', _460 => _460()]);
6121
7091
  if (!reader) {
6122
- _optionalChain([callbacks, 'access', _375 => _375.onError, 'optionalCall', _376 => _376("No response body")]);
7092
+ _optionalChain([callbacks, 'access', _461 => _461.onError, 'optionalCall', _462 => _462("No response body")]);
6123
7093
  return;
6124
7094
  }
6125
7095
  const decoder = new TextDecoder();
@@ -6137,22 +7107,22 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6137
7107
  if (data === "[DONE]") continue;
6138
7108
  try {
6139
7109
  const json = JSON.parse(data);
6140
- const delta = _optionalChain([json, 'access', _377 => _377.choices, 'optionalAccess', _378 => _378[0], 'optionalAccess', _379 => _379.delta]);
6141
- if (_optionalChain([delta, 'optionalAccess', _380 => _380.content])) {
7110
+ const delta = _optionalChain([json, 'access', _463 => _463.choices, 'optionalAccess', _464 => _464[0], 'optionalAccess', _465 => _465.delta]);
7111
+ if (_optionalChain([delta, 'optionalAccess', _466 => _466.content])) {
6142
7112
  content += delta.content;
6143
- _optionalChain([callbacks, 'access', _381 => _381.onDelta, 'optionalCall', _382 => _382(delta.content)]);
7113
+ _optionalChain([callbacks, 'access', _467 => _467.onDelta, 'optionalCall', _468 => _468(delta.content)]);
6144
7114
  }
6145
- if (_optionalChain([delta, 'optionalAccess', _383 => _383.thinking, 'optionalAccess', _384 => _384.content])) {
7115
+ if (_optionalChain([delta, 'optionalAccess', _469 => _469.thinking, 'optionalAccess', _470 => _470.content])) {
6146
7116
  reasoning += delta.thinking.content;
6147
- _optionalChain([callbacks, 'access', _385 => _385.onReasoning, 'optionalCall', _386 => _386(delta.thinking.content)]);
7117
+ _optionalChain([callbacks, 'access', _471 => _471.onReasoning, 'optionalCall', _472 => _472(delta.thinking.content)]);
6148
7118
  }
6149
- if (_optionalChain([delta, 'optionalAccess', _387 => _387.reasoning_content])) {
7119
+ if (_optionalChain([delta, 'optionalAccess', _473 => _473.reasoning_content])) {
6150
7120
  reasoning += delta.reasoning_content;
6151
- _optionalChain([callbacks, 'access', _388 => _388.onReasoning, 'optionalCall', _389 => _389(delta.reasoning_content)]);
7121
+ _optionalChain([callbacks, 'access', _474 => _474.onReasoning, 'optionalCall', _475 => _475(delta.reasoning_content)]);
6152
7122
  }
6153
7123
  if (json.usage) {
6154
7124
  const tapped = readUsageFromOpenAIResponse(json.usage);
6155
- if (_optionalChain([recording, 'optionalAccess', _390 => _390.recorder]) && tapped) {
7125
+ if (_optionalChain([recording, 'optionalAccess', _476 => _476.recorder]) && tapped) {
6156
7126
  recording.recorder.record({
6157
7127
  messageId: _nullishCoalesce(options.messageId, () => ( null)),
6158
7128
  parentMessageId: _nullishCoalesce(options.parentMessageId, () => ( null)),
@@ -6165,7 +7135,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6165
7135
  rawUsage: json.usage
6166
7136
  });
6167
7137
  }
6168
- _optionalChain([callbacks, 'access', _391 => _391.onDone, 'optionalCall', _392 => _392(
7138
+ _optionalChain([callbacks, 'access', _477 => _477.onDone, 'optionalCall', _478 => _478(
6169
7139
  {
6170
7140
  id: messageId,
6171
7141
  role: "assistant",
@@ -6186,7 +7156,7 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6186
7156
  }
6187
7157
  }
6188
7158
  }
6189
- _optionalChain([callbacks, 'access', _393 => _393.onDone, 'optionalCall', _394 => _394({
7159
+ _optionalChain([callbacks, 'access', _479 => _479.onDone, 'optionalCall', _480 => _480({
6190
7160
  id: messageId,
6191
7161
  role: "assistant",
6192
7162
  content,
@@ -6198,13 +7168,13 @@ async function completeStreamWithTransformers(options, callbacks, llmConfig, get
6198
7168
  }
6199
7169
  } catch (error) {
6200
7170
  const message = error instanceof Error ? error.message : "Unknown error";
6201
- _optionalChain([callbacks, 'access', _395 => _395.onError, 'optionalCall', _396 => _396(message)]);
7171
+ _optionalChain([callbacks, 'access', _481 => _481.onError, 'optionalCall', _482 => _482(message)]);
6202
7172
  }
6203
7173
  }
6204
7174
 
6205
7175
  // src/completion/CompletionService.ts
6206
- var CompletionService = (_class13 = class {
6207
- constructor(paths, llmConfig, logger) {;_class13.prototype.__init23.call(this);_class13.prototype.__init24.call(this);_class13.prototype.__init25.call(this);_class13.prototype.__init26.call(this);
7176
+ var CompletionService = (_class15 = class {
7177
+ constructor(paths, llmConfig, logger) {;_class15.prototype.__init30.call(this);_class15.prototype.__init31.call(this);_class15.prototype.__init32.call(this);_class15.prototype.__init33.call(this);
6208
7178
  this.paths = paths;
6209
7179
  this.llmConfig = llmConfig;
6210
7180
  this.logger = logger;
@@ -6212,10 +7182,10 @@ var CompletionService = (_class13 = class {
6212
7182
 
6213
7183
 
6214
7184
 
6215
- __init23() {this.apiKeyPool = null}
6216
- __init24() {this.usageRecorder = null}
6217
- __init25() {this.usageEventSink = null}
6218
- __init26() {this.visionFallbackProvider = null}
7185
+ __init30() {this.apiKeyPool = null}
7186
+ __init31() {this.usageRecorder = null}
7187
+ __init32() {this.usageEventSink = null}
7188
+ __init33() {this.visionFallbackProvider = null}
6219
7189
  /**
6220
7190
  * Set the API key pool service for multi-key load balancing.
6221
7191
  * When set, keys are resolved via the pool instead of directly from the provider.
@@ -6265,8 +7235,8 @@ var CompletionService = (_class13 = class {
6265
7235
  options.providerId,
6266
7236
  options.model
6267
7237
  );
6268
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _397 => _397.actualProviderId]) || options.providerId;
6269
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _398 => _398.actualModelId]) || options.model;
7238
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _483 => _483.actualProviderId]) || options.providerId;
7239
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _484 => _484.actualModelId]) || options.model;
6270
7240
  const provider = await this.getProvider(actualProviderId);
6271
7241
  if (!provider) {
6272
7242
  return { success: false, error: `Provider not found: ${actualProviderId}` };
@@ -6299,7 +7269,7 @@ var CompletionService = (_class13 = class {
6299
7269
  this.apiKeyPool.reportSuccess(options.sessionId);
6300
7270
  }
6301
7271
  if (result.success && result.usage && options.sessionId) {
6302
- _optionalChain([this, 'access', _399 => _399.usageEventSink, 'optionalAccess', _400 => _400.emit, 'call', _401 => _401({
7272
+ _optionalChain([this, 'access', _485 => _485.usageEventSink, 'optionalAccess', _486 => _486.emit, 'call', _487 => _487({
6303
7273
  sessionId: options.sessionId,
6304
7274
  modelId: actualModel,
6305
7275
  usage: result.usage,
@@ -6328,8 +7298,8 @@ var CompletionService = (_class13 = class {
6328
7298
  options.model
6329
7299
  );
6330
7300
  this.logger.debug("Resolved routed model", { routedInfo });
6331
- const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _402 => _402.actualProviderId]) || options.providerId;
6332
- const actualModel = _optionalChain([routedInfo, 'optionalAccess', _403 => _403.actualModelId]) || options.model;
7301
+ const actualProviderId = _optionalChain([routedInfo, 'optionalAccess', _488 => _488.actualProviderId]) || options.providerId;
7302
+ const actualModel = _optionalChain([routedInfo, 'optionalAccess', _489 => _489.actualModelId]) || options.model;
6333
7303
  this.logger.debug("Resolved provider and model", { actualProviderId, actualModel });
6334
7304
  const provider = await this.getProvider(actualProviderId);
6335
7305
  this.logger.debug("Retrieved provider", provider ? {
@@ -6341,22 +7311,22 @@ var CompletionService = (_class13 = class {
6341
7311
  enabled: provider.enabled
6342
7312
  } : { error: "Provider not found" });
6343
7313
  if (!provider) {
6344
- _optionalChain([callbacks, 'access', _404 => _404.onError, 'optionalCall', _405 => _405(`Provider not found: ${actualProviderId}`)]);
7314
+ _optionalChain([callbacks, 'access', _490 => _490.onError, 'optionalCall', _491 => _491(`Provider not found: ${actualProviderId}`)]);
6345
7315
  return;
6346
7316
  }
6347
7317
  if (!provider.enabled) {
6348
- _optionalChain([callbacks, 'access', _406 => _406.onError, 'optionalCall', _407 => _407(`Provider is disabled: ${provider.name}`)]);
7318
+ _optionalChain([callbacks, 'access', _492 => _492.onError, 'optionalCall', _493 => _493(`Provider is disabled: ${provider.name}`)]);
6349
7319
  return;
6350
7320
  }
6351
7321
  const apiKey = await this.resolveApiKeyForRequest(provider, actualProviderId, options.sessionId);
6352
7322
  if (!apiKey) {
6353
- _optionalChain([callbacks, 'access', _408 => _408.onError, 'optionalCall', _409 => _409("API key not configured")]);
7323
+ _optionalChain([callbacks, 'access', _494 => _494.onError, 'optionalCall', _495 => _495("API key not configured")]);
6354
7324
  return;
6355
7325
  }
6356
7326
  const apiFormat = _chunkA2YMUCBTcjs.resolveApiFormat.call(void 0, provider);
6357
7327
  this.logger.info("Using API format for stream", { apiFormat });
6358
7328
  const messageId = `msg_${Date.now()}`;
6359
- _optionalChain([callbacks, 'access', _410 => _410.onStart, 'optionalCall', _411 => _411(messageId)]);
7329
+ _optionalChain([callbacks, 'access', _496 => _496.onStart, 'optionalCall', _497 => _497(messageId)]);
6360
7330
  const resolvedOptions = { ...options, model: actualModel };
6361
7331
  if (options.sessionId) {
6362
7332
  const sid = options.sessionId;
@@ -6365,14 +7335,14 @@ var CompletionService = (_class13 = class {
6365
7335
  ...callbacks,
6366
7336
  onDone: (message, usage, metrics) => {
6367
7337
  if (usage) {
6368
- _optionalChain([this, 'access', _412 => _412.usageEventSink, 'optionalAccess', _413 => _413.emit, 'call', _414 => _414({
7338
+ _optionalChain([this, 'access', _498 => _498.usageEventSink, 'optionalAccess', _499 => _499.emit, 'call', _500 => _500({
6369
7339
  sessionId: sid,
6370
7340
  modelId: actualModel,
6371
7341
  usage,
6372
7342
  engineOrigin: "completion"
6373
7343
  })]);
6374
7344
  }
6375
- _optionalChain([userOnDone, 'optionalCall', _415 => _415(message, usage, metrics)]);
7345
+ _optionalChain([userOnDone, 'optionalCall', _501 => _501(message, usage, metrics)]);
6376
7346
  }
6377
7347
  };
6378
7348
  }
@@ -6388,11 +7358,11 @@ var CompletionService = (_class13 = class {
6388
7358
  retryState.error = { status, message: error };
6389
7359
  return;
6390
7360
  }
6391
- _optionalChain([callbacks, 'access', _416 => _416.onError, 'optionalCall', _417 => _417(error)]);
7361
+ _optionalChain([callbacks, 'access', _502 => _502.onError, 'optionalCall', _503 => _503(error)]);
6392
7362
  },
6393
7363
  onDone: (message, usage, metrics) => {
6394
7364
  this.apiKeyPool.reportSuccess(options.sessionId);
6395
- _optionalChain([callbacks, 'access', _418 => _418.onDone, 'optionalCall', _419 => _419(message, usage, metrics)]);
7365
+ _optionalChain([callbacks, 'access', _504 => _504.onDone, 'optionalCall', _505 => _505(message, usage, metrics)]);
6396
7366
  }
6397
7367
  };
6398
7368
  await this.callStreamHandler(apiFormat, provider, apiKey, resolvedOptions, messageId, interceptCallbacks);
@@ -6413,7 +7383,7 @@ var CompletionService = (_class13 = class {
6413
7383
  // don't fire onStart again
6414
7384
  });
6415
7385
  } else {
6416
- _optionalChain([callbacks, 'access', _420 => _420.onError, 'optionalCall', _421 => _421(retryState.error.message)]);
7386
+ _optionalChain([callbacks, 'access', _506 => _506.onError, 'optionalCall', _507 => _507(retryState.error.message)]);
6417
7387
  }
6418
7388
  }
6419
7389
  } else {
@@ -6426,7 +7396,7 @@ var CompletionService = (_class13 = class {
6426
7396
  } catch (error) {
6427
7397
  const message = error instanceof Error ? error.message : "Unknown error";
6428
7398
  this.logger.error("Stream completion error", error instanceof Error ? error : void 0, { message });
6429
- _optionalChain([callbacks, 'access', _422 => _422.onError, 'optionalCall', _423 => _423(message)]);
7399
+ _optionalChain([callbacks, 'access', _508 => _508.onError, 'optionalCall', _509 => _509(message)]);
6430
7400
  }
6431
7401
  }
6432
7402
  /**
@@ -6466,7 +7436,7 @@ var CompletionService = (_class13 = class {
6466
7436
  return {
6467
7437
  success: true,
6468
7438
  message: "Connection successful",
6469
- response: _optionalChain([result, 'access', _424 => _424.message, 'optionalAccess', _425 => _425.content]) || "",
7439
+ response: _optionalChain([result, 'access', _510 => _510.message, 'optionalAccess', _511 => _511.content]) || "",
6470
7440
  model: modelId,
6471
7441
  durationMs
6472
7442
  };
@@ -6531,7 +7501,7 @@ ${description}`;
6531
7501
  * 3. Legacy single key from provider config
6532
7502
  */
6533
7503
  async resolveApiKeyForRequest(provider, providerId, sessionId) {
6534
- if (_optionalChain([provider, 'access', _426 => _426.codingPlan, 'optionalAccess', _427 => _427.enabled]) && provider.codingPlan.apiKey) {
7504
+ if (_optionalChain([provider, 'access', _512 => _512.codingPlan, 'optionalAccess', _513 => _513.enabled]) && provider.codingPlan.apiKey) {
6535
7505
  return this.resolveApiKey(provider.codingPlan.apiKey);
6536
7506
  }
6537
7507
  if (this.apiKeyPool) {
@@ -6697,7 +7667,34 @@ ${description}`;
6697
7667
  builtinExecutor
6698
7668
  );
6699
7669
  }
6700
- }, _class13);
7670
+ }, _class15);
7671
+
7672
+
7673
+
7674
+
7675
+
7676
+
7677
+
7678
+
7679
+
7680
+
7681
+
7682
+
7683
+
7684
+
7685
+
7686
+
7687
+
7688
+
7689
+
7690
+
7691
+
7692
+
7693
+
7694
+
7695
+
7696
+
7697
+
6701
7698
 
6702
7699
 
6703
7700
 
@@ -6791,4 +7788,4 @@ ${description}`;
6791
7788
 
6792
7789
 
6793
7790
 
6794
- exports.CompletionService = CompletionService; exports.register = register; exports.emit = emit; exports.attach = attach; exports.release = release; exports.convertMessageToOpenAI = convertMessageToOpenAI; exports.convertMessageToAnthropic = convertMessageToAnthropic; exports.convertMessageToGemini = convertMessageToGemini; exports.getOpenRouterProviderConfig = getOpenRouterProviderConfig; exports.addOpenRouterProviderToRequest = addOpenRouterProviderToRequest; exports.DEFAULT_ROUTE_IDLE_MS = DEFAULT_ROUTE_IDLE_MS; exports.ProviderProxyRouteMap = ProviderProxyRouteMap; exports.getSharedExecutor = getSharedExecutor; exports.getResponsesEndpointTransformer = getResponsesEndpointTransformer; exports.getAnthropicEndpointTransformer = getAnthropicEndpointTransformer; exports.getGeminiEndpointTransformer = getGeminiEndpointTransformer; exports.readBody = readBody; exports.relayResponse = relayResponse; exports.aggregateAnthropicSseToJsonBody = aggregateAnthropicSseToJsonBody; exports.writeError = writeError; exports.writeBoundAccountError = writeBoundAccountError; exports.resolvePoolBoundKey = resolvePoolBoundKey; exports.extractRouteToken = extractRouteToken; exports.isLoopbackAddress = isLoopbackAddress; exports.ProviderProxy = ProviderProxy; exports.getProviderProxy = getProviderProxy; exports.__resetProviderProxyForTests = __resetProviderProxyForTests; exports.KeyedMutex = KeyedMutex; exports.OUTBOUND_API_SERVER_CONFIG_KEY = OUTBOUND_API_SERVER_CONFIG_KEY; exports.DEFAULT_USER_MESSAGE_QUEUE = DEFAULT_USER_MESSAGE_QUEUE; exports.DEFAULT_CONCURRENCY_QUEUE = DEFAULT_CONCURRENCY_QUEUE; exports.DEFAULT_ACCOUNT_PROBE = DEFAULT_ACCOUNT_PROBE; exports.DEFAULT_ALLOWANCE_SCHEDULING = DEFAULT_ALLOWANCE_SCHEDULING; exports.normalizeAllowanceScheduling = normalizeAllowanceScheduling; exports.normalizeAccountProbe = normalizeAccountProbe; exports.normalizeAudit = normalizeAudit; exports.normalizeBilling = normalizeBilling; exports.DEFAULT_FINGERPRINT = DEFAULT_FINGERPRINT; exports.normalizeFingerprint = normalizeFingerprint; exports.normalizeVoucher = normalizeVoucher; exports.normalizeProxyConfig = normalizeProxyConfig; exports.normalizeProxySegment = normalizeProxySegment; exports.normalizeWebhookDestination = normalizeWebhookDestination; exports.normalizeWebhookSegment = normalizeWebhookSegment; exports.normalizeQueueSegments = normalizeQueueSegments; exports.normalizePrefixTargets = normalizePrefixTargets; exports.legacyEndpointsToBindings = legacyEndpointsToBindings; exports.normalizeGatewayBindings = normalizeGatewayBindings; exports.defaultServerConfig = defaultServerConfig; exports.normalizeServerConfig = normalizeServerConfig; exports.loadServerConfig = loadServerConfig; exports.saveServerConfig = saveServerConfig; exports.mergeServerConfig = mergeServerConfig; exports.gatewayBindingToEndpointConfig = gatewayBindingToEndpointConfig; exports.candidateGatewayBindings = candidateGatewayBindings; exports.candidateBackgroundModelIds = candidateBackgroundModelIds; exports.resolveGatewayBinding = resolveGatewayBinding; exports.computeKeyExpiry = computeKeyExpiry; exports.checkKeyQuota = checkKeyQuota; exports.randomBase62 = randomBase62; exports.hashKey = hashKey; exports.createNamedKey = createNamedKey; exports.createIntegrationKey = createIntegrationKey; exports.verifyKey = verifyKey; exports.verifyPresentedKey = verifyPresentedKey; exports.ConcurrencyQueueFullError = ConcurrencyQueueFullError; exports.ConcurrencyWaitTimeoutError = ConcurrencyWaitTimeoutError; exports.ConcurrencyWaitCancelledError = ConcurrencyWaitCancelledError; exports.isConcurrencyRejection = isConcurrencyRejection; exports.OutboundConcurrencyGate = OutboundConcurrencyGate; exports.OutboundRateLimiter = OutboundRateLimiter; exports.isUserMessageRequest = isUserMessageRequest; exports.SerialQueueTimeoutError = SerialQueueTimeoutError; exports.isSerialQueueTimeout = isSerialQueueTimeout; exports.UserMessageSerialQueue = UserMessageSerialQueue; exports.generateVoucherCode = generateVoucherCode; exports.hashVoucherCode = hashVoucherCode; exports.voucherCodePrefix = voucherCodePrefix; exports.newVoucherId = newVoucherId; exports.computeVoucherGrant = computeVoucherGrant; exports.toVoucherInfo = toVoucherInfo; exports.isRedeemRequest = isRedeemRequest; exports.handleVoucherRedeem = handleVoucherRedeem; exports.DEFAULT_OUTBOUND_PORT = DEFAULT_OUTBOUND_PORT; exports.OutboundApiServer = OutboundApiServer; exports.formatUrls = formatUrls; exports.startOfLocalDay = startOfLocalDay; exports.startOfLocalWeek = startOfLocalWeek; exports.KeySpendTracker = KeySpendTracker; exports.getOutboundApiServer = getOutboundApiServer; exports.__resetOutboundApiServerForTests = __resetOutboundApiServerForTests;
7791
+ exports.CompletionService = CompletionService; exports.register = register; exports.emit = emit; exports.attach = attach; exports.release = release; exports.convertMessageToOpenAI = convertMessageToOpenAI; exports.convertMessageToAnthropic = convertMessageToAnthropic; exports.convertMessageToGemini = convertMessageToGemini; exports.getOpenRouterProviderConfig = getOpenRouterProviderConfig; exports.addOpenRouterProviderToRequest = addOpenRouterProviderToRequest; exports.DEFAULT_ROUTE_IDLE_MS = DEFAULT_ROUTE_IDLE_MS; exports.ProviderProxyRouteMap = ProviderProxyRouteMap; exports.getSharedExecutor = getSharedExecutor; exports.getResponsesEndpointTransformer = getResponsesEndpointTransformer; exports.getAnthropicEndpointTransformer = getAnthropicEndpointTransformer; exports.getGeminiEndpointTransformer = getGeminiEndpointTransformer; exports.readBody = readBody; exports.relayResponse = relayResponse; exports.aggregateAnthropicSseToJsonBody = aggregateAnthropicSseToJsonBody; exports.writeError = writeError; exports.writeBoundAccountError = writeBoundAccountError; exports.resolvePoolBoundKey = resolvePoolBoundKey; exports.extractRouteToken = extractRouteToken; exports.isLoopbackAddress = isLoopbackAddress; exports.ProviderProxy = ProviderProxy; exports.ROUTE_LEASE_REQUEST_SCHEMA = ROUTE_LEASE_REQUEST_SCHEMA; exports.ROUTE_LEASE_RESULT_SCHEMA = ROUTE_LEASE_RESULT_SCHEMA; exports.ROUTE_LEASE_CAPABILITIES_SCHEMA = ROUTE_LEASE_CAPABILITIES_SCHEMA; exports.ROUTE_LEASE_API_VERSION = ROUTE_LEASE_API_VERSION; exports.ROUTE_LEASE_DEFAULT_TTL_SECONDS = ROUTE_LEASE_DEFAULT_TTL_SECONDS; exports.ROUTE_LEASE_MAX_TTL_SECONDS = ROUTE_LEASE_MAX_TTL_SECONDS; exports.ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES = ROUTE_LEASE_MAX_IDEMPOTENCY_BYTES; exports.ROUTE_LEASE_MAX_CONSUMER_BYTES = ROUTE_LEASE_MAX_CONSUMER_BYTES; exports.ROUTE_LEASE_MAX_EXECUTION_ID_BYTES = ROUTE_LEASE_MAX_EXECUTION_ID_BYTES; exports.ROUTE_LEASE_MAX_SESSION_ID_BYTES = ROUTE_LEASE_MAX_SESSION_ID_BYTES; exports.ROUTE_LEASE_MAX_MODEL_BYTES = ROUTE_LEASE_MAX_MODEL_BYTES; exports.ROUTE_LEASE_SESSION_HASH_DOMAIN = ROUTE_LEASE_SESSION_HASH_DOMAIN; exports.ROUTE_LEASE_CODEX_TOKEN_ENV = ROUTE_LEASE_CODEX_TOKEN_ENV; exports.ROUTE_LEASE_RUNTIMES = ROUTE_LEASE_RUNTIMES; exports.ROUTE_LEASE_CAPABILITIES = ROUTE_LEASE_CAPABILITIES; exports.ROUTE_LEASE_RUNTIME_TABLE = ROUTE_LEASE_RUNTIME_TABLE; exports.RouteLeaseError = RouteLeaseError; exports.normalizeRouteLeaseIdempotencyKey = normalizeRouteLeaseIdempotencyKey; exports.normalizeRouteLeaseTtl = normalizeRouteLeaseTtl; exports.hashRouteLeaseSessionId = hashRouteLeaseSessionId; exports.canonicalizeRouteLeasePayload = canonicalizeRouteLeasePayload; exports.hashRouteLeasePayload = hashRouteLeasePayload; exports.parseRouteLeaseCreate = parseRouteLeaseCreate; exports.routeLeaseRuntime = routeLeaseRuntime; exports.SYSTEM_ROUTE_LEASE_CLOCK = SYSTEM_ROUTE_LEASE_CLOCK; exports.RouteLeaseManager = RouteLeaseManager; exports.gatewayBindingToEndpointConfig = gatewayBindingToEndpointConfig; exports.candidateGatewayBindings = candidateGatewayBindings; exports.candidateBackgroundModelIds = candidateBackgroundModelIds; exports.resolveGatewayBinding = resolveGatewayBinding; exports.RouteLeaseTargetResolver = RouteLeaseTargetResolver; exports.getProviderProxy = getProviderProxy; exports.__resetProviderProxyForTests = __resetProviderProxyForTests; exports.KeyedMutex = KeyedMutex; exports.OUTBOUND_API_SERVER_CONFIG_KEY = OUTBOUND_API_SERVER_CONFIG_KEY; exports.DEFAULT_USER_MESSAGE_QUEUE = DEFAULT_USER_MESSAGE_QUEUE; exports.DEFAULT_CONCURRENCY_QUEUE = DEFAULT_CONCURRENCY_QUEUE; exports.DEFAULT_ACCOUNT_PROBE = DEFAULT_ACCOUNT_PROBE; exports.DEFAULT_ALLOWANCE_SCHEDULING = DEFAULT_ALLOWANCE_SCHEDULING; exports.normalizeAllowanceScheduling = normalizeAllowanceScheduling; exports.normalizeAccountProbe = normalizeAccountProbe; exports.normalizeAudit = normalizeAudit; exports.normalizeBilling = normalizeBilling; exports.DEFAULT_FINGERPRINT = DEFAULT_FINGERPRINT; exports.normalizeFingerprint = normalizeFingerprint; exports.normalizeVoucher = normalizeVoucher; exports.normalizeProxyConfig = normalizeProxyConfig; exports.normalizeProxySegment = normalizeProxySegment; exports.normalizeWebhookDestination = normalizeWebhookDestination; exports.normalizeWebhookSegment = normalizeWebhookSegment; exports.normalizeQueueSegments = normalizeQueueSegments; exports.normalizePrefixTargets = normalizePrefixTargets; exports.legacyEndpointsToBindings = legacyEndpointsToBindings; exports.normalizeGatewayBindings = normalizeGatewayBindings; exports.defaultServerConfig = defaultServerConfig; exports.normalizeServerConfig = normalizeServerConfig; exports.loadServerConfig = loadServerConfig; exports.saveServerConfig = saveServerConfig; exports.mergeServerConfig = mergeServerConfig; exports.computeKeyExpiry = computeKeyExpiry; exports.checkKeyQuota = checkKeyQuota; exports.randomBase62 = randomBase62; exports.hashKey = hashKey; exports.createNamedKey = createNamedKey; exports.createIntegrationKey = createIntegrationKey; exports.verifyKey = verifyKey; exports.verifyPresentedKey = verifyPresentedKey; exports.ConcurrencyQueueFullError = ConcurrencyQueueFullError; exports.ConcurrencyWaitTimeoutError = ConcurrencyWaitTimeoutError; exports.ConcurrencyWaitCancelledError = ConcurrencyWaitCancelledError; exports.isConcurrencyRejection = isConcurrencyRejection; exports.OutboundConcurrencyGate = OutboundConcurrencyGate; exports.OutboundRateLimiter = OutboundRateLimiter; exports.isUserMessageRequest = isUserMessageRequest; exports.SerialQueueTimeoutError = SerialQueueTimeoutError; exports.isSerialQueueTimeout = isSerialQueueTimeout; exports.UserMessageSerialQueue = UserMessageSerialQueue; exports.generateVoucherCode = generateVoucherCode; exports.hashVoucherCode = hashVoucherCode; exports.voucherCodePrefix = voucherCodePrefix; exports.newVoucherId = newVoucherId; exports.computeVoucherGrant = computeVoucherGrant; exports.toVoucherInfo = toVoucherInfo; exports.isRedeemRequest = isRedeemRequest; exports.handleVoucherRedeem = handleVoucherRedeem; exports.DEFAULT_OUTBOUND_PORT = DEFAULT_OUTBOUND_PORT; exports.OutboundApiServer = OutboundApiServer; exports.formatUrls = formatUrls; exports.startOfLocalDay = startOfLocalDay; exports.startOfLocalWeek = startOfLocalWeek; exports.KeySpendTracker = KeySpendTracker; exports.getOutboundApiServer = getOutboundApiServer; exports.__resetOutboundApiServerForTests = __resetOutboundApiServerForTests;