@omnicross/core 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/dist/ApiConverter.cjs +2 -2
  2. package/dist/ApiConverter.js +1 -1
  3. package/dist/{CompletionService-DdWaAxq0.d.ts → CompletionService-C3BX0Ovy.d.ts} +4 -3
  4. package/dist/{CompletionService-D3a47vdU.d.cts → CompletionService-kNA17cfI.d.cts} +4 -3
  5. package/dist/auth/GeminiCodeAssistProjectResolver.cjs +8 -6
  6. package/dist/auth/GeminiCodeAssistProjectResolver.js +6 -4
  7. package/dist/{chunk-3Z5BQZFM.cjs → chunk-235MXAGX.cjs} +3 -1
  8. package/dist/{chunk-HRRYG2AX.js → chunk-23S5OPIO.js} +10 -1
  9. package/dist/chunk-25GXJCEZ.cjs +40 -0
  10. package/dist/chunk-2E42SAW3.cjs +323 -0
  11. package/dist/chunk-2JYRIADS.js +40 -0
  12. package/dist/chunk-2Z4XM3H5.cjs +44 -0
  13. package/dist/{chunk-BODMAL6G.js → chunk-32O35CIF.js} +271 -91
  14. package/dist/chunk-3SCOLIPT.cjs +63 -0
  15. package/dist/{chunk-SYQF7OIR.js → chunk-3WD4HTR2.js} +2 -0
  16. package/dist/{chunk-CGKLE6IW.cjs → chunk-4BB343BI.cjs} +1489 -520
  17. package/dist/{chunk-VACWOIRE.cjs → chunk-4VZX5F4T.cjs} +39 -15
  18. package/dist/{chunk-5ZQBEOMD.js → chunk-5NJLQ6T6.js} +46 -12
  19. package/dist/chunk-5RSZYQJH.cjs +62 -0
  20. package/dist/chunk-6QQG7YG3.cjs +153 -0
  21. package/dist/{chunk-IFXJ2BG7.js → chunk-7C7DET6S.js} +51 -6
  22. package/dist/{chunk-IK3KE3AU.js → chunk-DE2SRBFZ.js} +1 -1
  23. package/dist/{chunk-FH2P4OCD.js → chunk-E3COOKUP.js} +1240 -271
  24. package/dist/{chunk-UHGVBFU3.js → chunk-EBLFCNPY.js} +38 -14
  25. package/dist/chunk-FU7EAR73.js +62 -0
  26. package/dist/chunk-FXR6N3SR.js +323 -0
  27. package/dist/{chunk-SUEGJ75D.cjs → chunk-GDIXXKF2.cjs} +46 -12
  28. package/dist/chunk-GFIZ6LQI.js +170 -0
  29. package/dist/chunk-GL6XM527.js +61 -0
  30. package/dist/chunk-HVBBDBS3.cjs +350 -0
  31. package/dist/{chunk-46ZIFWYG.cjs → chunk-IIRPUVPH.cjs} +7 -1
  32. package/dist/chunk-J3QQ6JS5.cjs +170 -0
  33. package/dist/{chunk-JWVTXXPX.cjs → chunk-KBM2W6UY.cjs} +8 -4
  34. package/dist/chunk-LB6EBS6Y.js +69 -0
  35. package/dist/{chunk-IWQAX3CS.js → chunk-LVUCAWMK.js} +1 -1
  36. package/dist/chunk-MKF46GNZ.js +350 -0
  37. package/dist/{chunk-HYN75H6G.cjs → chunk-MUHWHQVM.cjs} +1 -1
  38. package/dist/chunk-MYYR3HZL.js +44 -0
  39. package/dist/{chunk-MFZ5437A.js → chunk-N5T4M4GU.js} +1 -1
  40. package/dist/{chunk-5ENKBSWO.js → chunk-N7OOJI5V.js} +88 -8
  41. package/dist/chunk-NKVZ6Z3Y.cjs +61 -0
  42. package/dist/chunk-OF3BECU2.cjs +69 -0
  43. package/dist/{chunk-SNYBEXLB.cjs → chunk-OJGQKHN5.cjs} +3 -1
  44. package/dist/{chunk-DDMRUL76.cjs → chunk-OKMJLEXX.cjs} +5 -5
  45. package/dist/chunk-OPRS4IQZ.js +153 -0
  46. package/dist/chunk-OTZC4TNH.cjs +79 -0
  47. package/dist/{chunk-3Q6Z6JTO.cjs → chunk-OVQBNVSV.cjs} +287 -107
  48. package/dist/{chunk-4XMYPW46.cjs → chunk-OXHNSZ53.cjs} +84 -44
  49. package/dist/{chunk-C6OV7GCC.js → chunk-PEGTIPB3.js} +52 -12
  50. package/dist/chunk-PO3XB56Z.js +79 -0
  51. package/dist/{chunk-D3TDKWRP.js → chunk-RJ5ZGNI3.js} +54 -5
  52. package/dist/{chunk-PG7PS2NN.cjs → chunk-RJUONLUQ.cjs} +59 -10
  53. package/dist/{chunk-BPKCU575.js → chunk-SOO562NM.js} +3 -1
  54. package/dist/{chunk-6QZGDLL4.js → chunk-TOVPMMHM.js} +5 -1
  55. package/dist/{chunk-K52THW2C.cjs → chunk-V4TXMEJS.cjs} +5 -5
  56. package/dist/{chunk-G2UTNSGU.js → chunk-WNKWAEUR.js} +7 -1
  57. package/dist/{chunk-UNWKG6EI.cjs → chunk-XPIOJQS2.cjs} +51 -6
  58. package/dist/chunk-XXFYLL7R.js +63 -0
  59. package/dist/{chunk-KFI44N2R.cjs → chunk-YNJNTCSQ.cjs} +10 -1
  60. package/dist/{chunk-VQY5W4YW.cjs → chunk-YWL7YZWS.cjs} +90 -10
  61. package/dist/completion/ApiKeyPoolService.cjs +12 -0
  62. package/dist/completion/ApiKeyPoolService.d.cts +6 -0
  63. package/dist/completion/ApiKeyPoolService.d.ts +6 -0
  64. package/dist/completion/ApiKeyPoolService.js +12 -0
  65. package/dist/completion/CompletionService.cjs +30 -24
  66. package/dist/completion/CompletionService.d.cts +3 -1
  67. package/dist/completion/CompletionService.d.ts +3 -1
  68. package/dist/completion/CompletionService.js +29 -23
  69. package/dist/completion.cjs +32 -26
  70. package/dist/completion.d.cts +3 -1
  71. package/dist/completion.d.ts +3 -1
  72. package/dist/completion.js +31 -25
  73. package/dist/index.cjs +56 -29
  74. package/dist/index.d.cts +8 -8
  75. package/dist/index.d.ts +8 -8
  76. package/dist/index.js +61 -34
  77. package/dist/{keyPolicy-Cp0FntZl.d.cts → keyPolicy-zdWTu3CQ.d.cts} +1 -1
  78. package/dist/{keyPolicy-Cp0FntZl.d.ts → keyPolicy-zdWTu3CQ.d.ts} +1 -1
  79. package/dist/outbound-api/auditCapture.cjs +2 -2
  80. package/dist/outbound-api/auditCapture.d.cts +3 -4
  81. package/dist/outbound-api/auditCapture.d.ts +3 -4
  82. package/dist/outbound-api/auditCapture.js +1 -1
  83. package/dist/outbound-api/quotaWarn.d.cts +1 -1
  84. package/dist/outbound-api/quotaWarn.d.ts +1 -1
  85. package/dist/outbound-api/routeResolver.cjs +2 -2
  86. package/dist/outbound-api/routeResolver.d.cts +5 -3
  87. package/dist/outbound-api/routeResolver.d.ts +5 -3
  88. package/dist/outbound-api/routeResolver.js +1 -1
  89. package/dist/outbound-api/subscriptionRegistryPort.d.cts +2 -0
  90. package/dist/outbound-api/subscriptionRegistryPort.d.ts +2 -0
  91. package/dist/outbound-api/types.d.cts +4 -2
  92. package/dist/outbound-api/types.d.ts +4 -2
  93. package/dist/outbound-api.cjs +52 -25
  94. package/dist/outbound-api.d.cts +131 -87
  95. package/dist/outbound-api.d.ts +131 -87
  96. package/dist/outbound-api.js +55 -28
  97. package/dist/pipeline/AccountAllowanceScheduling.cjs +15 -0
  98. package/dist/pipeline/AccountAllowanceScheduling.d.cts +91 -0
  99. package/dist/pipeline/AccountAllowanceScheduling.d.ts +91 -0
  100. package/dist/pipeline/AccountAllowanceScheduling.js +15 -0
  101. package/dist/pipeline/AccountAllowanceStore.cjs +20 -0
  102. package/dist/pipeline/AccountAllowanceStore.d.cts +77 -0
  103. package/dist/pipeline/AccountAllowanceStore.d.ts +77 -0
  104. package/dist/pipeline/AccountAllowanceStore.js +20 -0
  105. package/dist/pipeline/AccountRouteActivity.cjs +10 -0
  106. package/dist/pipeline/AccountRouteActivity.d.cts +61 -0
  107. package/dist/pipeline/AccountRouteActivity.d.ts +61 -0
  108. package/dist/pipeline/AccountRouteActivity.js +10 -0
  109. package/dist/pipeline/AuthSource.d.cts +11 -2
  110. package/dist/pipeline/AuthSource.d.ts +11 -2
  111. package/dist/pipeline/BoundAccountSelectionError.cjs +10 -0
  112. package/dist/pipeline/BoundAccountSelectionError.d.cts +28 -0
  113. package/dist/pipeline/BoundAccountSelectionError.d.ts +28 -0
  114. package/dist/pipeline/BoundAccountSelectionError.js +10 -0
  115. package/dist/pipeline/LlmConfigProviderAuth.d.cts +1 -0
  116. package/dist/pipeline/LlmConfigProviderAuth.d.ts +1 -0
  117. package/dist/pipeline/ServerOverloadCounter.cjs +16 -0
  118. package/dist/pipeline/ServerOverloadCounter.d.cts +77 -0
  119. package/dist/pipeline/ServerOverloadCounter.d.ts +77 -0
  120. package/dist/pipeline/ServerOverloadCounter.js +16 -0
  121. package/dist/pipeline/SubscriptionAccountHealth.cjs +6 -2
  122. package/dist/pipeline/SubscriptionAccountHealth.d.cts +65 -8
  123. package/dist/pipeline/SubscriptionAccountHealth.d.ts +65 -8
  124. package/dist/pipeline/SubscriptionAccountHealth.js +5 -1
  125. package/dist/pipeline/SubscriptionAuthSource.cjs +5 -2
  126. package/dist/pipeline/SubscriptionAuthSource.d.cts +1 -0
  127. package/dist/pipeline/SubscriptionAuthSource.d.ts +1 -0
  128. package/dist/pipeline/SubscriptionAuthSource.js +4 -1
  129. package/dist/pipeline/SubscriptionAuthStrategy.d.cts +8 -3
  130. package/dist/pipeline/SubscriptionAuthStrategy.d.ts +8 -3
  131. package/dist/pipeline/executeProviderCall.cjs +2 -2
  132. package/dist/pipeline/executeProviderCall.d.cts +8 -3
  133. package/dist/pipeline/executeProviderCall.d.ts +8 -3
  134. package/dist/pipeline/executeProviderCall.js +1 -1
  135. package/dist/pipeline/resolveProviderChain.d.cts +2 -3
  136. package/dist/pipeline/resolveProviderChain.d.ts +2 -3
  137. package/dist/pipeline/resolveSubscriptionChain.d.cts +1 -0
  138. package/dist/pipeline/resolveSubscriptionChain.d.ts +1 -0
  139. package/dist/pipeline/upstreamFetch.cjs +5 -3
  140. package/dist/pipeline/upstreamFetch.d.cts +35 -6
  141. package/dist/pipeline/upstreamFetch.d.ts +35 -6
  142. package/dist/pipeline/upstreamFetch.js +4 -2
  143. package/dist/pipeline/upstreamTrace.cjs +4 -2
  144. package/dist/pipeline/upstreamTrace.d.cts +22 -8
  145. package/dist/pipeline/upstreamTrace.d.ts +22 -8
  146. package/dist/pipeline/upstreamTrace.js +3 -1
  147. package/dist/ports/pricing-store.d.cts +4 -3
  148. package/dist/ports/pricing-store.d.ts +4 -3
  149. package/dist/ports.d.cts +4 -3
  150. package/dist/ports.d.ts +4 -3
  151. package/dist/provider-proxy/ProviderProxy.cjs +30 -24
  152. package/dist/provider-proxy/ProviderProxy.d.cts +2 -0
  153. package/dist/provider-proxy/ProviderProxy.d.ts +2 -0
  154. package/dist/provider-proxy/ProviderProxy.js +29 -23
  155. package/dist/provider-proxy/identity/codexCliHeaders.cjs +10 -0
  156. package/dist/provider-proxy/identity/codexCliHeaders.d.cts +45 -0
  157. package/dist/provider-proxy/identity/codexCliHeaders.d.ts +45 -0
  158. package/dist/provider-proxy/identity/codexCliHeaders.js +10 -0
  159. package/dist/provider-proxy/ingress/providerProxyShared.cjs +32 -24
  160. package/dist/provider-proxy/ingress/providerProxyShared.d.cts +44 -5
  161. package/dist/provider-proxy/ingress/providerProxyShared.d.ts +44 -5
  162. package/dist/provider-proxy/ingress/providerProxyShared.js +31 -23
  163. package/dist/provider-proxy/matchText.cjs +4 -2
  164. package/dist/provider-proxy/matchText.d.cts +39 -6
  165. package/dist/provider-proxy/matchText.d.ts +39 -6
  166. package/dist/provider-proxy/matchText.js +3 -1
  167. package/dist/provider-proxy/types.d.cts +35 -2
  168. package/dist/provider-proxy/types.d.ts +35 -2
  169. package/dist/provider-proxy.cjs +30 -24
  170. package/dist/provider-proxy.d.cts +2 -0
  171. package/dist/provider-proxy.d.ts +2 -0
  172. package/dist/provider-proxy.js +29 -23
  173. package/dist/{routeResolver-C4T7yClx.d.ts → routeResolver-BxOfunbm.d.ts} +1 -1
  174. package/dist/{routeResolver-CB3tvJKy.d.cts → routeResolver-D5OzD0iM.d.cts} +1 -1
  175. package/dist/transformer/TransformerChainExecutor.cjs +2 -2
  176. package/dist/transformer/TransformerChainExecutor.js +1 -1
  177. package/dist/transformer/transformers/AnthropicTransformer.cjs +3 -2
  178. package/dist/transformer/transformers/AnthropicTransformer.js +2 -1
  179. package/dist/transformer/transformers/GeminiCodeAssistTransformer.cjs +3 -3
  180. package/dist/transformer/transformers/GeminiCodeAssistTransformer.js +2 -2
  181. package/dist/transformer/transformers/GeminiTransformer.cjs +3 -3
  182. package/dist/transformer/transformers/GeminiTransformer.js +2 -2
  183. package/dist/transformer/transformers/OpenAIResponseTransformer.cjs +3 -2
  184. package/dist/transformer/transformers/OpenAIResponseTransformer.d.cts +1 -1
  185. package/dist/transformer/transformers/OpenAIResponseTransformer.d.ts +1 -1
  186. package/dist/transformer/transformers/OpenAIResponseTransformer.js +2 -1
  187. package/dist/transformer/transformers/OpenAITransformer.cjs +6 -0
  188. package/dist/transformer/transformers/OpenAITransformer.d.cts +68 -0
  189. package/dist/transformer/transformers/OpenAITransformer.d.ts +68 -0
  190. package/dist/transformer/transformers/OpenAITransformer.js +6 -0
  191. package/dist/transformer/transformers.cjs +9 -14
  192. package/dist/transformer/transformers.d.cts +12 -48
  193. package/dist/transformer/transformers.d.ts +12 -48
  194. package/dist/transformer/transformers.js +11 -16
  195. package/dist/transformer/types.d.cts +17 -0
  196. package/dist/transformer/types.d.ts +17 -0
  197. package/dist/transformer.cjs +12 -12
  198. package/dist/transformer.d.cts +1 -2
  199. package/dist/transformer.d.ts +1 -2
  200. package/dist/transformer.js +12 -12
  201. package/dist/{types-lgPYQuZS.d.ts → types-CqZD77rS.d.ts} +173 -7
  202. package/dist/{types-B8Arpxn0.d.cts → types-gjJ5lJ5n.d.cts} +173 -7
  203. package/dist/usage/pricing-engine.cjs +2 -2
  204. package/dist/usage/pricing-engine.d.cts +29 -1
  205. package/dist/usage/pricing-engine.d.ts +29 -1
  206. package/dist/usage/pricing-engine.js +1 -1
  207. package/dist/usage/usage-recorder.cjs +2 -2
  208. package/dist/usage/usage-recorder.d.cts +5 -1
  209. package/dist/usage/usage-recorder.d.ts +5 -1
  210. package/dist/usage/usage-recorder.js +1 -1
  211. package/dist/usage.cjs +3 -3
  212. package/dist/usage.js +2 -2
  213. package/package.json +1 -1
  214. package/dist/chunk-7FEBOPJO.cjs +0 -50
  215. package/dist/chunk-DWKQZ2SK.cjs +0 -254
  216. package/dist/chunk-IAXWICZU.js +0 -69
  217. package/dist/chunk-IB3FDI4O.js +0 -50
  218. package/dist/chunk-OQWKZCXP.js +0 -242
  219. package/dist/chunk-QWBWGODS.cjs +0 -69
  220. package/dist/chunk-YMFAJFQT.cjs +0 -173
  221. package/dist/chunk-ZCIQ3L3R.cjs +0 -242
  222. package/dist/chunk-ZJZAOKI7.js +0 -254
  223. package/dist/chunk-ZLX3KGXX.js +0 -173
  224. package/dist/transformer/transformers/OpenCodeGoTransformer.cjs +0 -6
  225. package/dist/transformer/transformers/OpenCodeGoTransformer.d.cts +0 -51
  226. package/dist/transformer/transformers/OpenCodeGoTransformer.d.ts +0 -51
  227. package/dist/transformer/transformers/OpenCodeGoTransformer.js +0 -6
  228. package/dist/transformer/transformers/ReasoningTransformer.cjs +0 -6
  229. package/dist/transformer/transformers/ReasoningTransformer.d.cts +0 -47
  230. package/dist/transformer/transformers/ReasoningTransformer.d.ts +0 -47
  231. package/dist/transformer/transformers/ReasoningTransformer.js +0 -6
@@ -1,4 +1,8 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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;// src/transformer/transformers/AnthropicToolHandling.ts
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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;
2
+
3
+ var _chunk5RSZYQJHcjs = require('./chunk-5RSZYQJH.cjs');
4
+
5
+ // src/transformer/transformers/AnthropicToolHandling.ts
2
6
  function isServerSideTool(tool) {
3
7
  const type = String(tool.type || "");
4
8
  return type.startsWith("web_search_") || type.startsWith("code_execution_") || type.startsWith("text_editor_") || type.startsWith("memory_") || type.startsWith("web_fetch_") || type.startsWith("search_tool_");
@@ -25,6 +29,40 @@ function formatBase64(data, mediaType) {
25
29
  if (data.startsWith("data:")) return data;
26
30
  return `data:${mediaType || "image/png"};base64,${data}`;
27
31
  }
32
+ function flattenToolResultContent(content) {
33
+ if (typeof content === "string") return content;
34
+ if (content == null) return "";
35
+ if (!Array.isArray(content)) return JSON.stringify(content);
36
+ const parts = [];
37
+ for (const block of content) {
38
+ if (typeof block === "string") {
39
+ parts.push(block);
40
+ continue;
41
+ }
42
+ if (!block || typeof block !== "object") continue;
43
+ const typed = block;
44
+ if (typed.type === "image" || typed.type === "image_url") {
45
+ parts.push("[image omitted]");
46
+ } else if (typeof typed.text === "string") {
47
+ parts.push(typed.text);
48
+ }
49
+ }
50
+ return parts.filter((text) => text !== "").join("\n");
51
+ }
52
+
53
+ // src/anthropicMaxTokens.ts
54
+ var _canonicalmodels = require('@omnicross/contracts/canonical-models');
55
+ var ANTHROPIC_FALLBACK_MAX_TOKENS = 128e3;
56
+ function resolveAnthropicMaxTokens(model, requested) {
57
+ if (typeof requested === "number" && Number.isFinite(requested) && requested > 0) {
58
+ return requested;
59
+ }
60
+ const canonical = model ? _optionalChain([_canonicalmodels.lookupCanonicalCapabilities.call(void 0, model), 'optionalAccess', _ => _.maxTokens]) : void 0;
61
+ if (typeof canonical === "number" && Number.isFinite(canonical) && canonical > 0) {
62
+ return canonical;
63
+ }
64
+ return ANTHROPIC_FALLBACK_MAX_TOKENS;
65
+ }
28
66
 
29
67
  // src/transformer/transformers/AnthropicRequestBuilder.ts
30
68
  function buildAnthropicRequestBody(request) {
@@ -46,7 +84,7 @@ function buildAnthropicRequestBody(request) {
46
84
  }
47
85
  if (msg.role === "assistant") {
48
86
  const content = [];
49
- if (_optionalChain([msg, 'access', _ => _.thinking, 'optionalAccess', _2 => _2.content])) {
87
+ if (_optionalChain([msg, 'access', _2 => _2.thinking, 'optionalAccess', _3 => _3.content])) {
50
88
  const block = {
51
89
  type: "thinking",
52
90
  thinking: msg.thinking.content
@@ -62,7 +100,7 @@ function buildAnthropicRequestBody(request) {
62
100
  content.push({ type: "text", text });
63
101
  }
64
102
  }
65
- if (_optionalChain([msg, 'access', _3 => _3.tool_calls, 'optionalAccess', _4 => _4.length])) {
103
+ if (_optionalChain([msg, 'access', _4 => _4.tool_calls, 'optionalAccess', _5 => _5.length])) {
66
104
  for (const tc of msg.tool_calls) {
67
105
  let input;
68
106
  try {
@@ -129,7 +167,11 @@ function buildAnthropicRequestBody(request) {
129
167
  const body = {
130
168
  model: request.model,
131
169
  messages: anthropicMessages,
132
- max_tokens: request.max_tokens || 4096,
170
+ // Anthropic requires this field, so an absent caller cap must resolve to
171
+ // the model's real ceiling — NOT a small constant, which would silently
172
+ // truncate long responses (and corrupt tool-call JSON mid-stream).
173
+ // See `resolveAnthropicMaxTokens`.
174
+ max_tokens: resolveAnthropicMaxTokens(request.model, request.max_tokens),
133
175
  stream: _nullishCoalesce(request.stream, () => ( false))
134
176
  };
135
177
  if (request.temperature !== void 0) {
@@ -138,7 +180,7 @@ function buildAnthropicRequestBody(request) {
138
180
  if (systemContent !== void 0) {
139
181
  body.system = systemContent;
140
182
  }
141
- if (_optionalChain([request, 'access', _5 => _5.tools, 'optionalAccess', _6 => _6.length])) {
183
+ if (_optionalChain([request, 'access', _6 => _6.tools, 'optionalAccess', _7 => _7.length])) {
142
184
  body.tools = request.tools.map((tool) => ({
143
185
  name: tool.function.name,
144
186
  description: tool.function.description || "",
@@ -146,7 +188,7 @@ function buildAnthropicRequestBody(request) {
146
188
  }));
147
189
  }
148
190
  const serverSideTools = request._serverSideTools;
149
- if (_optionalChain([serverSideTools, 'optionalAccess', _7 => _7.length])) {
191
+ if (_optionalChain([serverSideTools, 'optionalAccess', _8 => _8.length])) {
150
192
  body.tools = [...body.tools || [], ...serverSideTools];
151
193
  }
152
194
  if (request.tool_choice) {
@@ -160,7 +202,7 @@ function buildAnthropicRequestBody(request) {
160
202
  body.tool_choice = { type: "tool", name: request.tool_choice.function.name };
161
203
  }
162
204
  }
163
- if (_optionalChain([request, 'access', _8 => _8.reasoning, 'optionalAccess', _9 => _9.enabled])) {
205
+ if (_optionalChain([request, 'access', _9 => _9.reasoning, 'optionalAccess', _10 => _10.enabled])) {
164
206
  const budgetMap = { low: 2048, medium: 8192, high: 32768 };
165
207
  const budget = request.reasoning.max_tokens || budgetMap[request.reasoning.effort || "medium"] || 8192;
166
208
  body.thinking = { type: "enabled", budget_tokens: budget };
@@ -178,7 +220,7 @@ function convertAnthropicResponseToOpenAI(anthropicResponse) {
178
220
  const searchResults = content.filter((c) => c.type === "web_search_tool_result");
179
221
  for (const sr of searchResults) {
180
222
  const searches = sr.content;
181
- if (_optionalChain([searches, 'optionalAccess', _10 => _10.length])) {
223
+ if (_optionalChain([searches, 'optionalAccess', _11 => _11.length])) {
182
224
  const formatted = searches.map((s) => `[${s.title}](${s.url}): ${s.page_content || s.snippet || ""}`).join("\n");
183
225
  textParts.push(`
184
226
 
@@ -223,22 +265,20 @@ ${formatted}`);
223
265
  message,
224
266
  finish_reason: stopReasonMapping[anthropicResponse.stop_reason] || "stop"
225
267
  }],
226
- usage: usage ? {
227
- prompt_tokens: usage.input_tokens || 0,
228
- completion_tokens: usage.output_tokens || 0,
229
- total_tokens: (usage.input_tokens || 0) + (usage.output_tokens || 0)
230
- } : void 0
268
+ // Preserve prompt-cache counts (cache-read → prompt_tokens_details.cached_tokens,
269
+ // cache-creation folded into prompt_tokens) instead of dropping them.
270
+ usage: _chunk5RSZYQJHcjs.anthropicUsageToChatUsage.call(void 0, usage)
231
271
  };
232
272
  }
233
273
  function convertOpenAIResponseToAnthropic(openaiResponse) {
234
- const choice = _optionalChain([openaiResponse, 'access', _11 => _11.choices, 'optionalAccess', _12 => _12[0]]);
274
+ const choice = _optionalChain([openaiResponse, 'access', _12 => _12.choices, 'optionalAccess', _13 => _13[0]]);
235
275
  if (!choice) {
236
276
  throw new Error("No choices found in OpenAI response");
237
277
  }
238
278
  const message = choice.message;
239
279
  const content = [];
240
280
  const thinking = message.thinking;
241
- if (_optionalChain([thinking, 'optionalAccess', _13 => _13.content])) {
281
+ if (_optionalChain([thinking, 'optionalAccess', _14 => _14.content])) {
242
282
  content.push({
243
283
  type: "thinking",
244
284
  thinking: thinking.content,
@@ -252,7 +292,7 @@ function convertOpenAIResponseToAnthropic(openaiResponse) {
252
292
  });
253
293
  }
254
294
  const toolCalls = message.tool_calls;
255
- if (_optionalChain([toolCalls, 'optionalAccess', _14 => _14.length])) {
295
+ if (_optionalChain([toolCalls, 'optionalAccess', _15 => _15.length])) {
256
296
  for (const toolCall of toolCalls) {
257
297
  const func = toolCall.function;
258
298
  let parsedInput = {};
@@ -278,7 +318,7 @@ function convertOpenAIResponseToAnthropic(openaiResponse) {
278
318
  content_filter: "stop_sequence"
279
319
  };
280
320
  const usage = openaiResponse.usage;
281
- const usageDetails = _optionalChain([usage, 'optionalAccess', _15 => _15.prompt_tokens_details]);
321
+ const usageDetails = _optionalChain([usage, 'optionalAccess', _16 => _16.prompt_tokens_details]);
282
322
  return {
283
323
  id: openaiResponse.id,
284
324
  type: "message",
@@ -288,9 +328,9 @@ function convertOpenAIResponseToAnthropic(openaiResponse) {
288
328
  stop_reason: stopReasonMapping[finishReason] || "end_turn",
289
329
  stop_sequence: null,
290
330
  usage: {
291
- input_tokens: (_optionalChain([usage, 'optionalAccess', _16 => _16.prompt_tokens]) || 0) - (_optionalChain([usageDetails, 'optionalAccess', _17 => _17.cached_tokens]) || 0),
292
- output_tokens: _optionalChain([usage, 'optionalAccess', _18 => _18.completion_tokens]) || 0,
293
- cache_read_input_tokens: _optionalChain([usageDetails, 'optionalAccess', _19 => _19.cached_tokens]) || 0
331
+ input_tokens: (_optionalChain([usage, 'optionalAccess', _17 => _17.prompt_tokens]) || 0) - (_optionalChain([usageDetails, 'optionalAccess', _18 => _18.cached_tokens]) || 0),
332
+ output_tokens: _optionalChain([usage, 'optionalAccess', _19 => _19.completion_tokens]) || 0,
333
+ cache_read_input_tokens: _optionalChain([usageDetails, 'optionalAccess', _20 => _20.cached_tokens]) || 0
294
334
  }
295
335
  };
296
336
  }
@@ -337,7 +377,10 @@ function transformAnthropicRequestToUnified(request) {
337
377
  for (const tool of toolParts) {
338
378
  messages.push({
339
379
  role: "tool",
340
- content: typeof tool.content === "string" ? tool.content : JSON.stringify(tool.content),
380
+ // Claude Code sends tool results as a block ARRAY; stringifying it
381
+ // fed the model the literal `[{"type":"text","text":"…"}]` envelope
382
+ // on every tool result.
383
+ content: flattenToolResultContent(tool.content),
341
384
  tool_call_id: tool.tool_use_id,
342
385
  cache_control: tool.cache_control
343
386
  });
@@ -393,7 +436,7 @@ function transformAnthropicRequestToUnified(request) {
393
436
  const thinkingPart = msg.content.find(
394
437
  (c) => c.type === "thinking"
395
438
  );
396
- if (_optionalChain([thinkingPart, 'optionalAccess', _20 => _20.thinking])) {
439
+ if (_optionalChain([thinkingPart, 'optionalAccess', _21 => _21.thinking])) {
397
440
  assistantMessage.thinking = {
398
441
  content: thinkingPart.thinking,
399
442
  signature: thinkingPart.signature
@@ -412,7 +455,7 @@ function transformAnthropicRequestToUnified(request) {
412
455
  max_tokens: anthropicRequest.max_tokens,
413
456
  temperature: anthropicRequest.temperature,
414
457
  stream: anthropicRequest.stream,
415
- tools: _optionalChain([functionTools, 'optionalAccess', _21 => _21.length]) ? functionTools : void 0
458
+ tools: _optionalChain([functionTools, 'optionalAccess', _22 => _22.length]) ? functionTools : void 0
416
459
  };
417
460
  if (serverSideTools.length > 0) {
418
461
  result._serverSideTools = serverSideTools;
@@ -537,7 +580,7 @@ function convertAnthropicStreamToOpenAI(anthropicStream, logger) {
537
580
  `);
538
581
  } else if (event.content_block.type === "web_search_tool_result") {
539
582
  const searches = event.content_block.content;
540
- if (_optionalChain([searches, 'optionalAccess', _22 => _22.length])) {
583
+ if (_optionalChain([searches, 'optionalAccess', _23 => _23.length])) {
541
584
  const formatted = searches.map((s) => `[${s.title}](${s.url}): ${s.page_content || s.snippet || ""}`).join("\n");
542
585
  const chunk = {
543
586
  id: messageId,
@@ -578,18 +621,14 @@ ${formatted}` },
578
621
  }]
579
622
  };
580
623
  if (event.usage) {
581
- chunk.usage = {
582
- prompt_tokens: event.usage.input_tokens || 0,
583
- completion_tokens: event.usage.output_tokens || 0,
584
- total_tokens: (event.usage.input_tokens || 0) + (event.usage.output_tokens || 0)
585
- };
624
+ chunk.usage = _chunk5RSZYQJHcjs.anthropicUsageToChatUsage.call(void 0, event.usage);
586
625
  }
587
626
  safeEnqueue(`data: ${JSON.stringify(chunk)}
588
627
 
589
628
  `);
590
629
  }
591
630
  } catch (e) {
592
- _optionalChain([logger, 'optionalAccess', _23 => _23.error, 'call', _24 => _24("Error parsing Anthropic stream event:", e)]);
631
+ _optionalChain([logger, 'optionalAccess', _24 => _24.error, 'call', _25 => _25("Error parsing Anthropic stream event:", e)]);
593
632
  }
594
633
  }
595
634
  }
@@ -721,20 +760,20 @@ data: ${JSON.stringify({
721
760
 
722
761
  `);
723
762
  }
724
- const choice = _optionalChain([chunk, 'access', _25 => _25.choices, 'optionalAccess', _26 => _26[0]]);
763
+ const choice = _optionalChain([chunk, 'access', _26 => _26.choices, 'optionalAccess', _27 => _27[0]]);
725
764
  if (!choice) continue;
726
765
  if (chunk.usage) {
727
766
  stopReasonDelta = {
728
767
  type: "message_delta",
729
768
  delta: { stop_reason: "end_turn", stop_sequence: null },
730
769
  usage: {
731
- input_tokens: (chunk.usage.prompt_tokens || 0) - (_optionalChain([chunk, 'access', _27 => _27.usage, 'access', _28 => _28.prompt_tokens_details, 'optionalAccess', _29 => _29.cached_tokens]) || 0),
770
+ input_tokens: (chunk.usage.prompt_tokens || 0) - (_optionalChain([chunk, 'access', _28 => _28.usage, 'access', _29 => _29.prompt_tokens_details, 'optionalAccess', _30 => _30.cached_tokens]) || 0),
732
771
  output_tokens: chunk.usage.completion_tokens || 0,
733
- cache_read_input_tokens: _optionalChain([chunk, 'access', _30 => _30.usage, 'access', _31 => _31.prompt_tokens_details, 'optionalAccess', _32 => _32.cached_tokens]) || 0
772
+ cache_read_input_tokens: _optionalChain([chunk, 'access', _31 => _31.usage, 'access', _32 => _32.prompt_tokens_details, 'optionalAccess', _33 => _33.cached_tokens]) || 0
734
773
  }
735
774
  };
736
775
  }
737
- if (_optionalChain([choice, 'access', _33 => _33.delta, 'optionalAccess', _34 => _34.thinking])) {
776
+ if (_optionalChain([choice, 'access', _34 => _34.delta, 'optionalAccess', _35 => _35.thinking])) {
738
777
  if (!isThinkingStarted) {
739
778
  const thinkingBlockIndex = assignContentBlockIndex();
740
779
  safeEnqueue(`event: content_block_start
@@ -776,7 +815,7 @@ data: ${JSON.stringify({
776
815
  `);
777
816
  }
778
817
  }
779
- if (_optionalChain([choice, 'access', _35 => _35.delta, 'optionalAccess', _36 => _36.content])) {
818
+ if (_optionalChain([choice, 'access', _36 => _36.delta, 'optionalAccess', _37 => _37.content])) {
780
819
  if (!hasTextContentStarted) {
781
820
  if (currentContentBlockIndex >= 0 && isThinkingStarted) {
782
821
  safeEnqueue(`event: content_block_stop
@@ -808,7 +847,7 @@ data: ${JSON.stringify({
808
847
 
809
848
  `);
810
849
  }
811
- if (_optionalChain([choice, 'access', _37 => _37.delta, 'optionalAccess', _38 => _38.tool_calls])) {
850
+ if (_optionalChain([choice, 'access', _38 => _38.delta, 'optionalAccess', _39 => _39.tool_calls])) {
812
851
  for (const toolCall of choice.delta.tool_calls) {
813
852
  const toolCallIndex = _nullishCoalesce(toolCall.index, () => ( 0));
814
853
  if (!toolCallIndexToContentBlockIndex.has(toolCallIndex)) {
@@ -831,7 +870,7 @@ data: ${JSON.stringify({
831
870
  content_block: {
832
871
  type: "tool_use",
833
872
  id: toolCall.id || `call_${Date.now()}_${toolCallIndex}`,
834
- name: _optionalChain([toolCall, 'access', _39 => _39.function, 'optionalAccess', _40 => _40.name]) || `tool_${toolCallIndex}`,
873
+ name: _optionalChain([toolCall, 'access', _40 => _40.function, 'optionalAccess', _41 => _41.name]) || `tool_${toolCallIndex}`,
835
874
  input: {}
836
875
  }
837
876
  })}
@@ -839,7 +878,7 @@ data: ${JSON.stringify({
839
878
  `);
840
879
  currentContentBlockIndex = newBlockIndex;
841
880
  }
842
- if (_optionalChain([toolCall, 'access', _41 => _41.function, 'optionalAccess', _42 => _42.arguments])) {
881
+ if (_optionalChain([toolCall, 'access', _42 => _42.function, 'optionalAccess', _43 => _43.arguments])) {
843
882
  const blockIndex = toolCallIndexToContentBlockIndex.get(toolCallIndex);
844
883
  if (blockIndex !== void 0) {
845
884
  safeEnqueue(`event: content_block_delta
@@ -868,14 +907,14 @@ data: ${JSON.stringify({
868
907
  stop_sequence: null
869
908
  },
870
909
  usage: {
871
- input_tokens: (_optionalChain([chunk, 'access', _43 => _43.usage, 'optionalAccess', _44 => _44.prompt_tokens]) || 0) - (_optionalChain([chunk, 'access', _45 => _45.usage, 'optionalAccess', _46 => _46.prompt_tokens_details, 'optionalAccess', _47 => _47.cached_tokens]) || 0),
872
- output_tokens: _optionalChain([chunk, 'access', _48 => _48.usage, 'optionalAccess', _49 => _49.completion_tokens]) || 0,
873
- cache_read_input_tokens: _optionalChain([chunk, 'access', _50 => _50.usage, 'optionalAccess', _51 => _51.prompt_tokens_details, 'optionalAccess', _52 => _52.cached_tokens]) || 0
910
+ input_tokens: (_optionalChain([chunk, 'access', _44 => _44.usage, 'optionalAccess', _45 => _45.prompt_tokens]) || 0) - (_optionalChain([chunk, 'access', _46 => _46.usage, 'optionalAccess', _47 => _47.prompt_tokens_details, 'optionalAccess', _48 => _48.cached_tokens]) || 0),
911
+ output_tokens: _optionalChain([chunk, 'access', _49 => _49.usage, 'optionalAccess', _50 => _50.completion_tokens]) || 0,
912
+ cache_read_input_tokens: _optionalChain([chunk, 'access', _51 => _51.usage, 'optionalAccess', _52 => _52.prompt_tokens_details, 'optionalAccess', _53 => _53.cached_tokens]) || 0
874
913
  }
875
914
  };
876
915
  }
877
916
  } catch (e) {
878
- _optionalChain([logger, 'optionalAccess', _53 => _53.error, 'call', _54 => _54("Error parsing stream chunk:", e)]);
917
+ _optionalChain([logger, 'optionalAccess', _54 => _54.error, 'call', _55 => _55("Error parsing stream chunk:", e)]);
879
918
  }
880
919
  }
881
920
  }
@@ -899,7 +938,7 @@ var AnthropicTransformer = (_class = class {
899
938
  __init2() {this.endPoint = "/v1/messages"}
900
939
 
901
940
  constructor(options) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);
902
- this.useBearer = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _55 => _55.UseBearer]), () => ( false));
941
+ this.useBearer = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _56 => _56.UseBearer]), () => ( false));
903
942
  }
904
943
  /**
905
944
  * Handle authentication - Anthropic uses x-api-key header
@@ -988,4 +1027,5 @@ var AnthropicTransformer = (_class = class {
988
1027
 
989
1028
 
990
1029
 
991
- exports.AnthropicTransformer = AnthropicTransformer;
1030
+
1031
+ exports.resolveAnthropicMaxTokens = resolveAnthropicMaxTokens; exports.AnthropicTransformer = AnthropicTransformer;
@@ -1,3 +1,7 @@
1
+ import {
2
+ anthropicUsageToChatUsage
3
+ } from "./chunk-FU7EAR73.js";
4
+
1
5
  // src/transformer/transformers/AnthropicToolHandling.ts
2
6
  function isServerSideTool(tool) {
3
7
  const type = String(tool.type || "");
@@ -25,6 +29,40 @@ function formatBase64(data, mediaType) {
25
29
  if (data.startsWith("data:")) return data;
26
30
  return `data:${mediaType || "image/png"};base64,${data}`;
27
31
  }
32
+ function flattenToolResultContent(content) {
33
+ if (typeof content === "string") return content;
34
+ if (content == null) return "";
35
+ if (!Array.isArray(content)) return JSON.stringify(content);
36
+ const parts = [];
37
+ for (const block of content) {
38
+ if (typeof block === "string") {
39
+ parts.push(block);
40
+ continue;
41
+ }
42
+ if (!block || typeof block !== "object") continue;
43
+ const typed = block;
44
+ if (typed.type === "image" || typed.type === "image_url") {
45
+ parts.push("[image omitted]");
46
+ } else if (typeof typed.text === "string") {
47
+ parts.push(typed.text);
48
+ }
49
+ }
50
+ return parts.filter((text) => text !== "").join("\n");
51
+ }
52
+
53
+ // src/anthropicMaxTokens.ts
54
+ import { lookupCanonicalCapabilities } from "@omnicross/contracts/canonical-models";
55
+ var ANTHROPIC_FALLBACK_MAX_TOKENS = 128e3;
56
+ function resolveAnthropicMaxTokens(model, requested) {
57
+ if (typeof requested === "number" && Number.isFinite(requested) && requested > 0) {
58
+ return requested;
59
+ }
60
+ const canonical = model ? lookupCanonicalCapabilities(model)?.maxTokens : void 0;
61
+ if (typeof canonical === "number" && Number.isFinite(canonical) && canonical > 0) {
62
+ return canonical;
63
+ }
64
+ return ANTHROPIC_FALLBACK_MAX_TOKENS;
65
+ }
28
66
 
29
67
  // src/transformer/transformers/AnthropicRequestBuilder.ts
30
68
  function buildAnthropicRequestBody(request) {
@@ -129,7 +167,11 @@ function buildAnthropicRequestBody(request) {
129
167
  const body = {
130
168
  model: request.model,
131
169
  messages: anthropicMessages,
132
- max_tokens: request.max_tokens || 4096,
170
+ // Anthropic requires this field, so an absent caller cap must resolve to
171
+ // the model's real ceiling — NOT a small constant, which would silently
172
+ // truncate long responses (and corrupt tool-call JSON mid-stream).
173
+ // See `resolveAnthropicMaxTokens`.
174
+ max_tokens: resolveAnthropicMaxTokens(request.model, request.max_tokens),
133
175
  stream: request.stream ?? false
134
176
  };
135
177
  if (request.temperature !== void 0) {
@@ -223,11 +265,9 @@ ${formatted}`);
223
265
  message,
224
266
  finish_reason: stopReasonMapping[anthropicResponse.stop_reason] || "stop"
225
267
  }],
226
- usage: usage ? {
227
- prompt_tokens: usage.input_tokens || 0,
228
- completion_tokens: usage.output_tokens || 0,
229
- total_tokens: (usage.input_tokens || 0) + (usage.output_tokens || 0)
230
- } : void 0
268
+ // Preserve prompt-cache counts (cache-read → prompt_tokens_details.cached_tokens,
269
+ // cache-creation folded into prompt_tokens) instead of dropping them.
270
+ usage: anthropicUsageToChatUsage(usage)
231
271
  };
232
272
  }
233
273
  function convertOpenAIResponseToAnthropic(openaiResponse) {
@@ -337,7 +377,10 @@ function transformAnthropicRequestToUnified(request) {
337
377
  for (const tool of toolParts) {
338
378
  messages.push({
339
379
  role: "tool",
340
- content: typeof tool.content === "string" ? tool.content : JSON.stringify(tool.content),
380
+ // Claude Code sends tool results as a block ARRAY; stringifying it
381
+ // fed the model the literal `[{"type":"text","text":"…"}]` envelope
382
+ // on every tool result.
383
+ content: flattenToolResultContent(tool.content),
341
384
  tool_call_id: tool.tool_use_id,
342
385
  cache_control: tool.cache_control
343
386
  });
@@ -578,11 +621,7 @@ ${formatted}` },
578
621
  }]
579
622
  };
580
623
  if (event.usage) {
581
- chunk.usage = {
582
- prompt_tokens: event.usage.input_tokens || 0,
583
- completion_tokens: event.usage.output_tokens || 0,
584
- total_tokens: (event.usage.input_tokens || 0) + (event.usage.output_tokens || 0)
585
- };
624
+ chunk.usage = anthropicUsageToChatUsage(event.usage);
586
625
  }
587
626
  safeEnqueue(`data: ${JSON.stringify(chunk)}
588
627
 
@@ -987,5 +1026,6 @@ var AnthropicTransformer = class {
987
1026
  };
988
1027
 
989
1028
  export {
1029
+ resolveAnthropicMaxTokens,
990
1030
  AnthropicTransformer
991
1031
  };
@@ -0,0 +1,79 @@
1
+ // src/pipeline/ServerOverloadCounter.ts
2
+ var OVERLOAD_RECENT_LIMIT = 64;
3
+ var OVERLOAD_ENTRY_TTL_MS = 24 * 60 * 6e4;
4
+ function entryKey(providerId, accountId, endpoint) {
5
+ return `${providerId}\0${accountId}\0${endpoint}`;
6
+ }
7
+ var ServerOverloadCounterStore = class {
8
+ entries = /* @__PURE__ */ new Map();
9
+ /**
10
+ * Record one overload event. Creates the entry on first sight, then bumps
11
+ * `count`, `lastTs`, and prepends the event to the bounded `recent` ring.
12
+ */
13
+ recordOverload(input, now = Date.now()) {
14
+ const key = entryKey(input.providerId, input.accountId, input.endpoint);
15
+ const existing = this.entries.get(key);
16
+ if (existing) {
17
+ existing.count += 1;
18
+ existing.lastTs = now;
19
+ existing.recent.unshift(now);
20
+ if (existing.recent.length > OVERLOAD_RECENT_LIMIT) {
21
+ existing.recent.length = OVERLOAD_RECENT_LIMIT;
22
+ }
23
+ return { ...existing, recent: [...existing.recent] };
24
+ }
25
+ const entry = {
26
+ providerId: input.providerId,
27
+ accountId: input.accountId,
28
+ endpoint: input.endpoint,
29
+ count: 1,
30
+ firstTs: now,
31
+ lastTs: now,
32
+ recent: [now]
33
+ };
34
+ this.entries.set(key, entry);
35
+ return { ...entry, recent: [...entry.recent] };
36
+ }
37
+ /**
38
+ * Snapshot all (optionally filtered) entries, pruning stale ones first. Returns
39
+ * copies — callers (admin API → JSON) never see the live mutable entries.
40
+ */
41
+ list(query = {}, now = Date.now()) {
42
+ const providerId = query.providerId?.trim();
43
+ const accountId = query.accountId?.trim();
44
+ const out = [];
45
+ for (const [key, entry] of this.entries) {
46
+ if (now - entry.lastTs > OVERLOAD_ENTRY_TTL_MS) {
47
+ this.entries.delete(key);
48
+ continue;
49
+ }
50
+ if (providerId && entry.providerId !== providerId) continue;
51
+ if (accountId && entry.accountId !== accountId) continue;
52
+ out.push({ ...entry, recent: [...entry.recent] });
53
+ }
54
+ return out;
55
+ }
56
+ clear() {
57
+ this.entries.clear();
58
+ }
59
+ };
60
+ var sharedStore = null;
61
+ function getSharedOverloadCounter() {
62
+ if (!sharedStore) sharedStore = new ServerOverloadCounterStore();
63
+ return sharedStore;
64
+ }
65
+ function setSharedOverloadCounter(instance) {
66
+ sharedStore = instance;
67
+ }
68
+ function __resetSharedOverloadCounterForTests() {
69
+ sharedStore = null;
70
+ }
71
+
72
+ export {
73
+ OVERLOAD_RECENT_LIMIT,
74
+ OVERLOAD_ENTRY_TTL_MS,
75
+ ServerOverloadCounterStore,
76
+ getSharedOverloadCounter,
77
+ setSharedOverloadCounter,
78
+ __resetSharedOverloadCounterForTests
79
+ };
@@ -1,8 +1,15 @@
1
1
  import {
2
+ REDACTED_BODY,
2
3
  getUpstreamTracePath,
3
4
  redactHeaders,
4
5
  writeUpstreamTrace
5
- } from "./chunk-SYQF7OIR.js";
6
+ } from "./chunk-3WD4HTR2.js";
7
+ import {
8
+ getSharedAccountAllowanceStore
9
+ } from "./chunk-MKF46GNZ.js";
10
+ import {
11
+ getSharedAccountRouteActivity
12
+ } from "./chunk-GL6XM527.js";
6
13
 
7
14
  // src/pipeline/upstreamFetch.ts
8
15
  import { Agent, buildConnector, ProxyAgent } from "undici";
@@ -102,17 +109,57 @@ function resolveUpstreamDispatcher(ctx) {
102
109
  function fetchUpstream(url, init, ctx) {
103
110
  const dispatcher = resolveUpstreamDispatcher({ ...ctx ?? {}, url });
104
111
  const doFetch = dispatcher ? () => fetch(url, { ...init, dispatcher }) : () => fetch(url, init);
112
+ const doFetchWithAllowanceTap = () => doFetch().then((response) => {
113
+ if (ctx?.providerId === "codex" && ctx.accountId) {
114
+ try {
115
+ getSharedAccountAllowanceStore().recordCodexHeaders(ctx.accountId, response.headers);
116
+ } catch {
117
+ }
118
+ }
119
+ return response;
120
+ });
121
+ const activity = ctx?.routeActivity;
122
+ const activityStartedAt = activity && ctx?.providerId && ctx.accountId ? Date.now() : 0;
123
+ const recordActivity = (status) => {
124
+ if (!activity || !ctx?.providerId || !ctx.accountId) return;
125
+ const record = getSharedAccountRouteActivity().record({
126
+ providerId: ctx.providerId,
127
+ accountId: ctx.accountId,
128
+ endpoint: activity.endpoint,
129
+ sessionKey: activity.sessionKey,
130
+ sessionSource: activity.sessionSource,
131
+ model: activity.model,
132
+ status,
133
+ durationMs: Math.max(0, Date.now() - activityStartedAt),
134
+ ts: activityStartedAt
135
+ });
136
+ try {
137
+ activity.onRecorded?.(record);
138
+ } catch {
139
+ }
140
+ };
141
+ const doFetchWithActivity = () => doFetchWithAllowanceTap().then(
142
+ (response) => {
143
+ recordActivity(response.status);
144
+ return response;
145
+ },
146
+ (error) => {
147
+ recordActivity(0);
148
+ throw error;
149
+ }
150
+ );
105
151
  const tracePath = getUpstreamTracePath();
106
- if (!tracePath || !ctx?.providerId) return doFetch();
152
+ if (!tracePath || !ctx?.providerId) return doFetchWithActivity();
107
153
  const startedAt = Date.now();
108
154
  const method = typeof init.method === "string" ? init.method : "GET";
109
155
  const requestHeaders = redactHeaders(
110
156
  init.headers
111
157
  );
112
- const requestBody = typeof init.body === "string" ? init.body : null;
158
+ const redactBodies = ctx.redactBodies === true;
159
+ const requestBody = redactBodies ? REDACTED_BODY : typeof init.body === "string" ? init.body : null;
113
160
  const providerId = ctx.providerId;
114
161
  const accountId = ctx.accountId;
115
- return doFetch().then((res) => {
162
+ return doFetchWithActivity().then((res) => {
116
163
  const clone = res.clone();
117
164
  const status = res.status;
118
165
  const statusText = res.statusText;
@@ -130,7 +177,9 @@ function fetchUpstream(url, init, ctx) {
130
177
  status,
131
178
  statusText,
132
179
  responseHeaders,
133
- responseBody: body,
180
+ // The byte count stays REAL even when redacted — it is a useful
181
+ // diagnostic and reveals nothing about the token itself.
182
+ responseBody: redactBodies ? REDACTED_BODY : body,
134
183
  responseBytes: Buffer.byteLength(body, "utf8")
135
184
  });
136
185
  }).catch(() => {