omnicell-router 0.7.0 → 0.8.2

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 (246) hide show
  1. package/backend/dist/cli/clients/activity.d.ts +35 -0
  2. package/backend/dist/cli/clients/activity.js +67 -0
  3. package/backend/dist/cli/clients/base.d.ts +13 -2
  4. package/backend/dist/cli/clients/claude.d.ts +2 -2
  5. package/backend/dist/cli/clients/codex.d.ts +2 -2
  6. package/backend/dist/cli/clients/core-adapters.d.ts +3 -4
  7. package/backend/dist/cli/clients/core-adapters.js +3 -4
  8. package/backend/dist/cli/clients/cursor-proxy.d.ts +88 -0
  9. package/backend/dist/cli/clients/cursor-proxy.js +421 -0
  10. package/backend/dist/cli/clients/dsh.d.ts +2 -2
  11. package/backend/dist/cli/clients/grok.d.ts +2 -2
  12. package/backend/dist/cli/clients/index.d.ts +8 -1
  13. package/backend/dist/cli/clients/index.js +55 -21
  14. package/backend/dist/cli/clients/kimi.d.ts +2 -2
  15. package/backend/dist/cli/clients/launcher.d.ts +8 -0
  16. package/backend/dist/cli/clients/model-meta.js +1 -1
  17. package/backend/dist/cli/clients/omp.d.ts +2 -2
  18. package/backend/dist/cli/clients/openclaw.d.ts +2 -2
  19. package/backend/dist/cli/clients/opencode-proxy.d.ts +11 -0
  20. package/backend/dist/cli/clients/opencode-proxy.js +351 -0
  21. package/backend/dist/cli/clients/opencode.d.ts +2 -2
  22. package/backend/dist/cli/clients/pi.d.ts +2 -2
  23. package/backend/dist/cli/clients/plugin-adapters.d.ts +3 -1
  24. package/backend/dist/cli/clients/plugin-adapters.js +6 -29
  25. package/backend/dist/cli/clients/qoder-proxy.d.ts +84 -0
  26. package/backend/dist/cli/clients/qoder-proxy.js +497 -0
  27. package/backend/dist/cli/clients/reserved.d.ts +2 -6
  28. package/backend/dist/cli/clients/reserved.js +2 -0
  29. package/backend/dist/cli/clients/workbuddy.d.ts +2 -2
  30. package/backend/dist/cli/index.js +60 -9
  31. package/backend/dist/cli/types.d.ts +29 -8
  32. package/backend/dist/compression/caveman-prompts.d.ts +1 -1
  33. package/backend/dist/config/comments-en.js +6 -12
  34. package/backend/dist/config/comments-zh.js +6 -12
  35. package/backend/dist/config/defaults.d.ts +56 -1
  36. package/backend/dist/config/defaults.js +68 -0
  37. package/backend/dist/config/index.d.ts +9 -2
  38. package/backend/dist/config/index.js +26 -3
  39. package/backend/dist/config/types.d.ts +55 -43
  40. package/backend/dist/forward-proxy/egress.d.ts +10 -2
  41. package/backend/dist/forward-proxy/egress.js +42 -4
  42. package/backend/dist/forward-proxy/ordered-relay.d.ts +29 -0
  43. package/backend/dist/forward-proxy/ordered-relay.js +46 -0
  44. package/backend/dist/forward-proxy/rules.d.ts +66 -19
  45. package/backend/dist/forward-proxy/rules.js +61 -14
  46. package/backend/dist/forward-proxy/server.js +226 -77
  47. package/backend/dist/forward-proxy/system-proxy.d.ts +42 -2
  48. package/backend/dist/forward-proxy/system-proxy.js +199 -22
  49. package/backend/dist/harness/common/market.d.ts +3 -3
  50. package/backend/dist/harness/host/adapters/amp.d.ts +1 -1
  51. package/backend/dist/harness/host/adapters/antigravity.d.ts +1 -1
  52. package/backend/dist/harness/host/adapters/claude.d.ts +1 -1
  53. package/backend/dist/harness/host/adapters/cline.d.ts +1 -1
  54. package/backend/dist/harness/host/adapters/codex.d.ts +1 -1
  55. package/backend/dist/harness/host/adapters/cursor.d.ts +1 -1
  56. package/backend/dist/harness/host/adapters/dsh.d.ts +2 -2
  57. package/backend/dist/harness/host/adapters/factory.d.ts +1 -1
  58. package/backend/dist/harness/host/adapters/gemini-cli.d.ts +1 -1
  59. package/backend/dist/harness/host/adapters/grok.d.ts +1 -1
  60. package/backend/dist/harness/host/adapters/kimi.d.ts +1 -1
  61. package/backend/dist/harness/host/adapters/omp.d.ts +1 -1
  62. package/backend/dist/harness/host/adapters/openclaw.d.ts +1 -1
  63. package/backend/dist/harness/host/adapters/opencode.d.ts +1 -1
  64. package/backend/dist/harness/host/adapters/pi.d.ts +1 -1
  65. package/backend/dist/harness/host/adapters/universal.d.ts +1 -1
  66. package/backend/dist/harness/host/adapters/windsurf.d.ts +1 -1
  67. package/backend/dist/harness/host/adapters/zed.d.ts +1 -1
  68. package/backend/dist/harness/skill/skills-sh.d.ts +1 -1
  69. package/backend/dist/harness/translation.js +64 -21
  70. package/backend/dist/harness/types.d.ts +1 -1
  71. package/backend/dist/harness/types.js +1 -1
  72. package/backend/dist/index.js +31 -18
  73. package/backend/dist/ir/adapters/anthropic-stream.d.ts +18 -0
  74. package/backend/dist/ir/adapters/anthropic-stream.js +247 -0
  75. package/backend/dist/ir/adapters/anthropic.d.ts +29 -0
  76. package/backend/dist/ir/adapters/anthropic.js +788 -0
  77. package/backend/dist/ir/adapters/chat.d.ts +15 -0
  78. package/backend/dist/ir/adapters/chat.js +602 -0
  79. package/backend/dist/ir/adapters/google-stream.d.ts +22 -0
  80. package/backend/dist/ir/adapters/google-stream.js +242 -0
  81. package/backend/dist/ir/adapters/google.d.ts +9 -0
  82. package/backend/dist/ir/adapters/google.js +317 -0
  83. package/backend/dist/ir/adapters/openai-responses-output.js +8 -5
  84. package/backend/dist/ir/adapters/openai-responses.d.ts +15 -0
  85. package/backend/dist/ir/adapters/openai-responses.js +26 -1
  86. package/backend/dist/ir/adapters/responses-output.d.ts +50 -0
  87. package/backend/dist/ir/adapters/responses-output.js +348 -0
  88. package/backend/dist/ir/adapters/responses-stream.d.ts +12 -0
  89. package/backend/dist/ir/adapters/responses-stream.js +178 -0
  90. package/backend/dist/ir/adapters/responses.d.ts +14 -0
  91. package/backend/dist/ir/adapters/responses.js +646 -0
  92. package/backend/dist/ir/errors.d.ts +2 -2
  93. package/backend/dist/ir/types.d.ts +2 -1
  94. package/backend/dist/metrics/request-cost.d.ts +11 -2
  95. package/backend/dist/metrics/request-cost.js +12 -3
  96. package/backend/dist/observability/request-identity.d.ts +1 -1
  97. package/backend/dist/observability/trace-id.d.ts +40 -0
  98. package/backend/dist/observability/trace-id.js +134 -0
  99. package/backend/dist/observability/upstream-events.d.ts +82 -60
  100. package/backend/dist/observability/upstream-events.js +142 -67
  101. package/backend/dist/opencode/catalog/types.d.ts +9 -0
  102. package/backend/dist/opencode/credentials/types.d.ts +2 -2
  103. package/backend/dist/opencode/probe.d.ts +24 -1
  104. package/backend/dist/opencode/probe.js +28 -4
  105. package/backend/dist/opencode/user-config.d.ts +5 -0
  106. package/backend/dist/pipeline/directives.d.ts +6 -2
  107. package/backend/dist/pipeline/directives.js +7 -3
  108. package/backend/dist/pipeline/orchestrator.d.ts +28 -27
  109. package/backend/dist/pipeline/orchestrator.js +220 -194
  110. package/backend/dist/pipeline/unresolved-model.d.ts +33 -0
  111. package/backend/dist/pipeline/unresolved-model.js +43 -0
  112. package/backend/dist/plugins/catalog-def.d.ts +143 -0
  113. package/backend/dist/plugins/catalog-def.js +177 -0
  114. package/backend/dist/plugins/gateways.d.ts +62 -3
  115. package/backend/dist/plugins/gateways.js +139 -2
  116. package/backend/dist/plugins/host.d.ts +42 -5
  117. package/backend/dist/plugins/host.js +454 -38
  118. package/backend/dist/plugins/manifest.d.ts +119 -10
  119. package/backend/dist/plugins/manifest.js +265 -14
  120. package/backend/dist/plugins/pack.js +26 -0
  121. package/backend/dist/plugins/types.d.ts +187 -21
  122. package/backend/dist/protocols/systemone/convert.d.ts +1 -0
  123. package/backend/dist/protocols/systemone/convert.js +6 -1
  124. package/backend/dist/protocols/systemone/decide.js +18 -8
  125. package/backend/dist/providers/anthropic-messages.d.ts +12 -0
  126. package/backend/dist/providers/anthropic-messages.js +53 -0
  127. package/backend/dist/providers/anthropic.d.ts +7 -8
  128. package/backend/dist/providers/anthropic.js +36 -324
  129. package/backend/dist/providers/base.d.ts +14 -2
  130. package/backend/dist/providers/boot-direct.js +68 -38
  131. package/backend/dist/providers/dispatch.d.ts +5 -19
  132. package/backend/dist/providers/dispatch.js +46 -58
  133. package/backend/dist/providers/executors/anthropic-messages.d.ts +1 -0
  134. package/backend/dist/providers/executors/anthropic-messages.js +3 -0
  135. package/backend/dist/providers/executors/dispatch.d.ts +16 -2
  136. package/backend/dist/providers/executors/dispatch.js +39 -9
  137. package/backend/dist/providers/executors/google-gemini.d.ts +1 -0
  138. package/backend/dist/providers/executors/google-gemini.js +3 -0
  139. package/backend/dist/providers/executors/http-stream.js +16 -3
  140. package/backend/dist/providers/executors/openai-chat.d.ts +1 -0
  141. package/backend/dist/providers/executors/openai-chat.js +3 -0
  142. package/backend/dist/providers/executors/openai-responses.d.ts +1 -0
  143. package/backend/dist/providers/executors/openai-responses.js +3 -0
  144. package/backend/dist/providers/executors/plugin-adapter.js +4 -4
  145. package/backend/dist/providers/google-gemini.d.ts +12 -0
  146. package/backend/dist/providers/google-gemini.js +50 -0
  147. package/backend/dist/providers/google.d.ts +7 -8
  148. package/backend/dist/providers/google.js +35 -308
  149. package/backend/dist/providers/http-json.d.ts +19 -0
  150. package/backend/dist/providers/http-json.js +104 -0
  151. package/backend/dist/providers/http-stream.d.ts +52 -0
  152. package/backend/dist/providers/http-stream.js +423 -0
  153. package/backend/dist/providers/member-match.d.ts +4 -5
  154. package/backend/dist/providers/member-match.js +4 -5
  155. package/backend/dist/providers/openai-chat.d.ts +13 -0
  156. package/backend/dist/providers/openai-chat.js +99 -0
  157. package/backend/dist/providers/openai-compatible.d.ts +7 -4
  158. package/backend/dist/providers/openai-compatible.js +71 -368
  159. package/backend/dist/providers/openai-responses.d.ts +12 -0
  160. package/backend/dist/providers/openai-responses.js +68 -0
  161. package/backend/dist/providers/opencode-proxy.d.ts +5 -5
  162. package/backend/dist/providers/opencode-proxy.js +49 -125
  163. package/backend/dist/providers/plugin-adapter.d.ts +9 -0
  164. package/backend/dist/providers/plugin-adapter.js +31 -0
  165. package/backend/dist/providers/registry.d.ts +49 -24
  166. package/backend/dist/providers/registry.js +97 -55
  167. package/backend/dist/providers/responses.d.ts +7 -28
  168. package/backend/dist/providers/responses.js +45 -253
  169. package/backend/dist/providers/tool-markup-leak.d.ts +41 -0
  170. package/backend/dist/providers/tool-markup-leak.js +57 -0
  171. package/backend/dist/providers/tool-markup-recovery.d.ts +27 -0
  172. package/backend/dist/providers/tool-markup-recovery.js +786 -0
  173. package/backend/dist/providers/virtual-meta.d.ts +21 -2
  174. package/backend/dist/providers/virtual-meta.js +49 -9
  175. package/backend/dist/proxy/asset-download.d.ts +54 -0
  176. package/backend/dist/proxy/asset-download.js +420 -0
  177. package/backend/dist/proxy/manual-node-spec.d.ts +16 -16
  178. package/backend/dist/proxy/mihomo-binary.d.ts +17 -2
  179. package/backend/dist/proxy/mihomo-binary.js +62 -51
  180. package/backend/dist/proxy/mihomo-client.js +15 -1
  181. package/backend/dist/proxy/mihomo-config.js +11 -2
  182. package/backend/dist/proxy/mihomo-geo.d.ts +87 -0
  183. package/backend/dist/proxy/mihomo-geo.js +213 -0
  184. package/backend/dist/proxy/mihomo-logs.d.ts +27 -0
  185. package/backend/dist/proxy/mihomo-logs.js +70 -0
  186. package/backend/dist/proxy/mihomo-pac.d.ts +11 -0
  187. package/backend/dist/proxy/mihomo-pac.js +15 -0
  188. package/backend/dist/proxy/mihomo-rules-import.d.ts +1 -1
  189. package/backend/dist/proxy/mihomo-rules-import.js +7 -7
  190. package/backend/dist/proxy/mihomo-sidecar.d.ts +17 -1
  191. package/backend/dist/proxy/mihomo-sidecar.js +148 -26
  192. package/backend/dist/quota/adapters/minimax.js +36 -13
  193. package/backend/dist/quota/cli-login.d.ts +8 -0
  194. package/backend/dist/quota/cli-login.js +18 -0
  195. package/backend/dist/quota/declarations.js +10 -0
  196. package/backend/dist/quota/dsl.js +18 -5
  197. package/backend/dist/quota/engine.d.ts +22 -1
  198. package/backend/dist/quota/engine.js +170 -29
  199. package/backend/dist/quota/prober.d.ts +5 -0
  200. package/backend/dist/quota/prober.js +11 -2
  201. package/backend/dist/quota/types.d.ts +87 -8
  202. package/backend/dist/quota/verdict.d.ts +35 -0
  203. package/backend/dist/quota/verdict.js +135 -0
  204. package/backend/dist/ratelimit/admission.d.ts +131 -0
  205. package/backend/dist/ratelimit/admission.js +233 -0
  206. package/backend/dist/ratelimit/rate-limiter.js +6 -0
  207. package/backend/dist/resilience/types.d.ts +2 -2
  208. package/backend/dist/router/layer2-decision.d.ts +1 -1
  209. package/backend/dist/router/layer2-decision.js +5 -8
  210. package/backend/dist/routes/console.js +248 -52
  211. package/backend/dist/routes/mihomo.d.ts +2 -0
  212. package/backend/dist/routes/mihomo.js +334 -29
  213. package/backend/dist/routes/responses.d.ts +87 -8
  214. package/backend/dist/routes/responses.js +314 -33
  215. package/backend/dist/server.js +34 -6
  216. package/backend/dist/session/session-manager.d.ts +6 -0
  217. package/backend/dist/session/session-manager.js +20 -9
  218. package/backend/dist/surfaces/chat-pipeline.d.ts +2 -2
  219. package/backend/dist/surfaces/chat-pipeline.js +17 -7
  220. package/backend/dist/surfaces/chat.js +15 -0
  221. package/backend/dist/surfaces/decision-pipeline.js +7 -10
  222. package/backend/dist/surfaces/direct-forward.d.ts +3 -0
  223. package/backend/dist/surfaces/direct-forward.js +206 -0
  224. package/backend/dist/surfaces/rate-limit-response.d.ts +17 -0
  225. package/backend/dist/surfaces/rate-limit-response.js +57 -0
  226. package/backend/dist/surfaces/types.d.ts +11 -0
  227. package/backend/dist/trace/tracker.d.ts +32 -1
  228. package/backend/dist/trace/tracker.js +29 -4
  229. package/backend/dist/types/router.d.ts +34 -12
  230. package/backend/dist/types/router.js +35 -6
  231. package/backend/dist/utils/chat-stream.d.ts +21 -8
  232. package/backend/dist/utils/chat-stream.js +46 -9
  233. package/backend/dist/utils/event-ring.d.ts +4 -0
  234. package/backend/dist/utils/event-ring.js +4 -0
  235. package/backend/dist/utils/popularity.js +29 -4
  236. package/backend/dist/utils/proxy.d.ts +7 -0
  237. package/backend/dist/utils/proxy.js +35 -10
  238. package/backend/dist/utils/retry-after.d.ts +21 -0
  239. package/backend/dist/utils/retry-after.js +36 -0
  240. package/bin/ocr.js +2 -2
  241. package/frontend/dist/assets/index-55VVwfFQ.js +1521 -0
  242. package/frontend/dist/assets/{index-CQSLGFTV.css → index-DxZ9MHKB.css} +1 -1
  243. package/frontend/dist/assets/workbuddy.color-DOMpQkxJ.svg +29 -0
  244. package/frontend/dist/index.html +2 -2
  245. package/package.json +1 -1
  246. package/frontend/dist/assets/index-CLvxJjp_.js +0 -1476
@@ -1,4 +1,13 @@
1
1
  import type { Usage } from '../ir/types.js';
2
2
  import type { ModelPricing } from '../types/router.js';
3
- /** Request-time historical cost from the canonical accounting plane. */
4
- export declare function computeRequestCost(usage: Usage | undefined, pricing: ModelPricing): number;
3
+ /**
4
+ * Request-time historical cost from the canonical accounting plane.
5
+ *
6
+ * Returns `undefined` when the model carries no known price. That is NOT the
7
+ * same as 0: 0 means "served for free", while `undefined` means the vendor
8
+ * publishes no dollar price (qoder publishes only a `price_factor` credit
9
+ * multiplier). Collapsing the two is what made every unpriced model show a
10
+ * confident but invented cost, so callers must keep the distinction and let
11
+ * an unknown sum out as unknown.
12
+ */
13
+ export declare function computeRequestCost(usage: Usage | undefined, pricing: ModelPricing | undefined): number | undefined;
@@ -1,7 +1,16 @@
1
- /** Request-time historical cost from the canonical accounting plane. */
1
+ /**
2
+ * Request-time historical cost from the canonical accounting plane.
3
+ *
4
+ * Returns `undefined` when the model carries no known price. That is NOT the
5
+ * same as 0: 0 means "served for free", while `undefined` means the vendor
6
+ * publishes no dollar price (qoder publishes only a `price_factor` credit
7
+ * multiplier). Collapsing the two is what made every unpriced model show a
8
+ * confident but invented cost, so callers must keep the distinction and let
9
+ * an unknown sum out as unknown.
10
+ */
2
11
  export function computeRequestCost(usage, pricing) {
3
- if (!usage)
4
- return 0;
12
+ if (!usage || !pricing)
13
+ return undefined;
5
14
  const cachedTokens = usage.cachedInputTokens ?? 0;
6
15
  const uncachedInput = Math.max(0, usage.inputTokens - cachedTokens);
7
16
  const outputTokens = usage.outputTokens;
@@ -4,7 +4,7 @@ import type { FastifyRequest } from 'fastify';
4
4
  * canonical, most explicit form) first, then the de-facto LLM-SDK headers.
5
5
  * A malformed value falls through — the request is never rejected over it.
6
6
  */
7
- declare const CLIENT_REQUEST_ID_HEADERS: readonly ['traceparent', 'x-request-id', 'x-trace-id', 'x-correlation-id', 'request-id'];
7
+ declare const CLIENT_REQUEST_ID_HEADERS: readonly ["traceparent", "x-request-id", "x-trace-id", "x-correlation-id", "request-id"];
8
8
  export type ClientRequestIdSource = (typeof CLIENT_REQUEST_ID_HEADERS)[number];
9
9
  /**
10
10
  * Extract the caller's correlation id. Absent when the caller sent none —
@@ -0,0 +1,40 @@
1
+ import type { FastifyRequest } from 'fastify';
2
+ /**
3
+ * Extract / mint the traceId for an inbound request. Priority:
4
+ * 1. W3C `traceparent` (canonical; most explicit)
5
+ * 2. `x-request-id` (de-facto LLM SDK standard)
6
+ * 3. `x-trace-id` / `x-correlation-id` / `request-id`
7
+ * 4. freshly minted 32-hex id (via crypto.randomBytes, no Math.random fallback)
8
+ *
9
+ * `provider` lets the caller decide what to do when the inbound request has
10
+ * no trace id. Pass `'client-or-mint'` to mint one (always-on tracing);
11
+ * pass `'client-only'` to surface an error when nothing was supplied
12
+ * (off by default — most clients don't send a trace id).
13
+ */
14
+ export declare function resolveTraceId(req: Pick<FastifyRequest, 'headers'>, provider?: 'client-or-mint' | 'client-only'): {
15
+ traceId: string;
16
+ source: 'traceparent' | 'x-request-id' | 'x-trace-id' | 'x-correlation-id' | 'request-id' | 'minted';
17
+ };
18
+ /**
19
+ * Extract / mint a per-exchange spanId. Priority:
20
+ * 1. W3C `traceparent` parentSpanId (canonical child-span semantics)
21
+ * 2. `x-span-id` (some OpenTelemetry-aware proxies)
22
+ * 3. freshly minted 16-hex id
23
+ *
24
+ * The spanId is set by the gateway when minting the traceId (parent span
25
+ * becomes a new child). For inbound/exchange spans, we always mint — the
26
+ * span scope is local to one HTTP exchange.
27
+ */
28
+ export declare function resolveSpanId(req: Pick<FastifyRequest, 'headers'>, hint?: {
29
+ traceparent?: string;
30
+ }): {
31
+ spanId: string;
32
+ source: 'traceparent' | 'x-span-id' | 'minted';
33
+ };
34
+ /**
35
+ * Build a W3C-compatible traceparent for an OUTGOING request — used by
36
+ * provider adapters when they propagate the traceId to upstream calls.
37
+ * The provider's child span (16-hex) is generated locally so the upstream
38
+ * sees the gateway as its parent.
39
+ */
40
+ export declare function buildTraceparent(traceId: string, parentSpanId: string): string;
@@ -0,0 +1,134 @@
1
+ import crypto from 'node:crypto';
2
+ /**
3
+ * Trace identifier model: every HTTP exchange event and every trace record
4
+ * carries two correlated ids:
5
+ *
6
+ * - `traceId` — stable across the whole inference turn (4 capture events
7
+ * and the trace record all share one). When the client supplied an
8
+ * upstream trace id (W3C `traceparent` or `x-request-id`), we adopt it
9
+ * so the gateway becomes a transparent span in the caller's
10
+ * distributed trace; otherwise we mint a fresh 32-hex id.
11
+ *
12
+ * - `spanId` — one per HTTP exchange (req + resp share it). Same value
13
+ * across the inbound pair and another value across the outbound pair
14
+ * (two exchanges per turn).
15
+ *
16
+ * Source-of-truth: W3C Trace Context (https://www.w3.org/TR/trace-context/)
17
+ * `traceparent: 00-{traceId}-{parentSpanId}-{flags}` is the standard. We
18
+ * also accept the de-facto `x-request-id` / `x-trace-id` /
19
+ * `x-correlation-id` headers common to LLM SDKs. When multiple are present,
20
+ * W3C wins (most explicit format), then `x-request-id` (most common),
21
+ * then the others as fallbacks.
22
+ */
23
+ const TRACEPARENT_RE = /^([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/i;
24
+ const TRACE_ID_RE = /^[0-9a-f]{32}$/i;
25
+ const SPAN_ID_RE = /^[0-9a-f]{16}$/i;
26
+ /** Priority chain for inbound traceId extraction. */
27
+ const TRACE_ID_HEADERS = [
28
+ 'traceparent', // W3C Trace Context (level 1 priority, see below)
29
+ 'x-request-id', // de-facto LLM-SDK standard
30
+ 'x-trace-id', // OpenAI / Anthropic convention
31
+ 'x-correlation-id',
32
+ 'request-id',
33
+ ];
34
+ const SPAN_ID_HEADERS = [
35
+ 'x-span-id',
36
+ 'tracestate', // secondary in traceparent
37
+ ];
38
+ /**
39
+ * Extract / mint the traceId for an inbound request. Priority:
40
+ * 1. W3C `traceparent` (canonical; most explicit)
41
+ * 2. `x-request-id` (de-facto LLM SDK standard)
42
+ * 3. `x-trace-id` / `x-correlation-id` / `request-id`
43
+ * 4. freshly minted 32-hex id (via crypto.randomBytes, no Math.random fallback)
44
+ *
45
+ * `provider` lets the caller decide what to do when the inbound request has
46
+ * no trace id. Pass `'client-or-mint'` to mint one (always-on tracing);
47
+ * pass `'client-only'` to surface an error when nothing was supplied
48
+ * (off by default — most clients don't send a trace id).
49
+ */
50
+ export function resolveTraceId(req, provider = 'client-or-mint') {
51
+ const headers = req.headers;
52
+ // 1. W3C traceparent — canonical. Parse the four fields; if malformed,
53
+ // ignore (don't reject the request, just fall through to the next source).
54
+ const traceparent = pickHeader(headers, 'traceparent');
55
+ if (traceparent) {
56
+ const m = TRACEPARENT_RE.exec(traceparent.trim());
57
+ if (m && TRACE_ID_RE.test(m[2])) {
58
+ return { traceId: m[2].toLowerCase(), source: 'traceparent' };
59
+ }
60
+ }
61
+ // 2-4. De-facto headers.
62
+ for (const name of TRACE_ID_HEADERS.slice(1)) {
63
+ const v = pickHeader(headers, name);
64
+ if (v && TRACE_ID_RE.test(v.trim())) {
65
+ return { traceId: v.trim().toLowerCase(), source: name };
66
+ }
67
+ // Some clients send non-hex ids (UUIDs, NanoIDs, base32). Accept as-is
68
+ // so we don't lose the link — only mint when truly absent.
69
+ if (v && v.trim().length > 0 && v.trim().length <= 128) {
70
+ return { traceId: v.trim(), source: name };
71
+ }
72
+ }
73
+ if (provider === 'client-only') {
74
+ throw new Error('No upstream traceId provided and policy is client-only');
75
+ }
76
+ return { traceId: mintTraceId(), source: 'minted' };
77
+ }
78
+ /**
79
+ * Extract / mint a per-exchange spanId. Priority:
80
+ * 1. W3C `traceparent` parentSpanId (canonical child-span semantics)
81
+ * 2. `x-span-id` (some OpenTelemetry-aware proxies)
82
+ * 3. freshly minted 16-hex id
83
+ *
84
+ * The spanId is set by the gateway when minting the traceId (parent span
85
+ * becomes a new child). For inbound/exchange spans, we always mint — the
86
+ * span scope is local to one HTTP exchange.
87
+ */
88
+ export function resolveSpanId(req, hint) {
89
+ const headers = req.headers;
90
+ // 1. W3C traceparent — if a fresh traceparent was supplied, the parentSpanId
91
+ // inside is the natural caller-supplied span for our inbound span.
92
+ const tp = hint?.traceparent ?? pickHeader(headers, 'traceparent');
93
+ if (tp) {
94
+ const m = TRACEPARENT_RE.exec(tp.trim());
95
+ if (m && SPAN_ID_RE.test(m[3])) {
96
+ return { spanId: m[3].toLowerCase(), source: 'traceparent' };
97
+ }
98
+ }
99
+ // 2. Explicit span header.
100
+ for (const name of SPAN_ID_HEADERS) {
101
+ const v = pickHeader(headers, name);
102
+ if (v && SPAN_ID_RE.test(v.trim())) {
103
+ return { spanId: v.trim().toLowerCase(), source: name };
104
+ }
105
+ }
106
+ return { spanId: mintSpanId(), source: 'minted' };
107
+ }
108
+ /**
109
+ * Build a W3C-compatible traceparent for an OUTGOING request — used by
110
+ * provider adapters when they propagate the traceId to upstream calls.
111
+ * The provider's child span (16-hex) is generated locally so the upstream
112
+ * sees the gateway as its parent.
113
+ */
114
+ export function buildTraceparent(traceId, parentSpanId) {
115
+ return `00-${traceId}-${parentSpanId}-01`;
116
+ }
117
+ function pickHeader(headers, name) {
118
+ const v = headers[name] ?? headers[name.toLowerCase()];
119
+ if (v === undefined)
120
+ return undefined;
121
+ if (Array.isArray(v))
122
+ return v[0];
123
+ return v;
124
+ }
125
+ function mintTraceId() {
126
+ // 32 hex chars = 16 bytes of entropy = 128 bits — OTel-compliant. node:crypto
127
+ // is available on every supported runtime; Math.random is NOT used (collision
128
+ // risk across bursty gateway traffic).
129
+ return crypto.randomBytes(16).toString('hex');
130
+ }
131
+ function mintSpanId() {
132
+ // 16 hex chars = 8 bytes = 64 bits — OTel-compliant.
133
+ return crypto.randomBytes(8).toString('hex');
134
+ }
@@ -1,86 +1,107 @@
1
1
  import type { CaptureKeyIdentity, CaptureRecorder, CaptureEvent } from '../capture/recorder.js';
2
2
  import type { TurnIdentity } from '../capture/turn-boundary.js';
3
3
  /**
4
- * Event-stream upstream context shared by the provider adapters. The shape
5
- * is intentionally minimal — the orchestrator fills it once, and the
6
- * adapter passes it to every `emitUpstreamRequest` / `emitUpstreamResponse`
7
- * call. Centralizing the field names here keeps the 5 provider adapters
8
- * from each spelling out the same boilerplate.
4
+ * Archive handle for one recorded upstream call. Built only while request
5
+ * capture is on. The conversation id lives in `#sessionId` and is copied
6
+ * onto archive lines by the methods below. It is not a field. A value that
7
+ * is not an instance of this class writes nothing. Executors that need the
8
+ * live conversation take it as their own argument.
9
9
  */
10
- export interface UpstreamEventContext {
11
- sessionId: string;
10
+ export declare class UpstreamEventContext {
11
+ #private;
12
12
  /** Caller correlation value, when it sent one (see CaptureEvent). */
13
- clientRequestId?: string;
13
+ readonly clientRequestId?: string;
14
14
  /** Gateway turn id (ADR-0021) — the capture turn-join key. */
15
- requestId: string;
16
- model: string;
17
- recorder: CaptureRecorder;
15
+ readonly requestId: string;
16
+ readonly model: string;
17
+ readonly recorder: CaptureRecorder;
18
18
  /**
19
19
  * Client API-key identity of the owning turn, forwarded onto every event
20
20
  * emitted through this context so the upstream leg is attributable to the
21
21
  * same application as the client leg. Undefined for key-less legs (MITM
22
22
  * tunnel, master-key access) — events then simply omit the field.
23
23
  */
24
- apiKey?: CaptureKeyIdentity;
24
+ readonly apiKey?: CaptureKeyIdentity;
25
25
  /**
26
26
  * Deterministic conversation key of the owning turn (content-addressed,
27
27
  * see CaptureEvent.conversationKey) forwarded onto every event emitted
28
28
  * through this context.
29
29
  */
30
- conversationKey?: string;
30
+ readonly conversationKey?: string;
31
31
  /**
32
- * Non-default call label forwarded onto every event emitted through this
33
- * context. The Layer 2 decision layer's decision-model leg carries 'l2-decision' so
34
- * the archive can tell it apart from candidate-execution events sharing the
35
- * SAME requestId/sessionId (the outer request's identity).
32
+ * Non-default call label. The Layer 2 decision leg carries 'l2-decision'
33
+ * so the archive can tell it apart from candidate-execution events that
34
+ * share this request.
36
35
  */
37
- purpose?: CaptureEvent['purpose'];
38
- /** Write-time turn identity, allocated once at the admission gate and
39
- * copied onto every event of this request. */
40
- turn?: TurnIdentity;
36
+ readonly purpose?: CaptureEvent['purpose'];
37
+ /** Write-time turn identity, allocated once at the admission gate. */
38
+ readonly turn?: TurnIdentity;
39
+ constructor(init: {
40
+ sessionId: string;
41
+ clientRequestId?: string;
42
+ requestId: string;
43
+ model: string;
44
+ recorder: CaptureRecorder;
45
+ apiKey?: CaptureKeyIdentity;
46
+ conversationKey?: string;
47
+ purpose?: CaptureEvent['purpose'];
48
+ turn?: TurnIdentity;
49
+ });
50
+ /** Same sealed conversation, different archive label. */
51
+ withPurpose(purpose: CaptureEvent['purpose']): UpstreamEventContext;
52
+ /** Returns the exchangeId minted for this upstream request. */
53
+ emitRequest(args: {
54
+ url: string;
55
+ method: string;
56
+ requestHeaders: Record<string, string>;
57
+ requestBody: string;
58
+ /** Actual upstream model on the wire (defaults to this.model). */
59
+ model?: string;
60
+ /** Raw proxy URL when routed via proxy (redacted to origin at emit). */
61
+ proxy?: string;
62
+ }): string | undefined;
63
+ /** Emit a SUCCESS upstream response event. */
64
+ emitResponse(args: {
65
+ exchangeId: string;
66
+ url: string;
67
+ method: string;
68
+ status: number;
69
+ statusText: string;
70
+ responseHeaders: Headers;
71
+ responseBody: string;
72
+ /** Epoch ms of the first streamed body chunk (streamed exchanges only). */
73
+ firstByteTs?: number;
74
+ model?: string;
75
+ }): void;
76
+ /**
77
+ * Emit an upstream FAILURE response event. Pairs with the request event
78
+ * fired earlier. Status is the upstream HTTP status when known, else `0`.
79
+ */
80
+ emitFailure(args: {
81
+ exchangeId: string;
82
+ url: string;
83
+ method: string;
84
+ status: number;
85
+ statusText: string;
86
+ responseHeaders?: Headers;
87
+ responseBody?: string;
88
+ /** Epoch ms of the first streamed body chunk (a stream that died mid-body still carries it). */
89
+ firstByteTs?: number;
90
+ error: string;
91
+ model?: string;
92
+ }): void;
41
93
  }
42
- /** Returns the exchangeId minted by `emitUpstreamRequest`, or undefined when the context is absent (capture off / mock mode / tests). */
43
- export declare function emitUpstreamRequestOnce(ctx: UpstreamEventContext | undefined, args: {
44
- url: string;
45
- method: string;
46
- requestHeaders: Record<string, string>;
47
- requestBody: string;
48
- /** Actual upstream model on the wire (defaults to ctx.model = the client-requested model). */
49
- model?: string;
50
- /** Raw proxy URL when routed via proxy (redacted to origin at emit). */
51
- proxy?: string;
52
- }): string | undefined;
53
- /** Emit a SUCCESS upstream response event. */
54
- export declare function emitUpstreamResponseOnce(ctx: UpstreamEventContext | undefined, args: {
94
+ /** Returns the exchangeId minted by `emitUpstreamRequest`, or undefined when capture is off or the handle is not a real context. */
95
+ export declare function emitUpstreamRequestOnce(ctx: UpstreamEventContext | undefined, args: Parameters<UpstreamEventContext['emitRequest']>[0]): string | undefined;
96
+ type ResponseEmit = Parameters<UpstreamEventContext['emitResponse']>[0];
97
+ type FailureEmit = Parameters<UpstreamEventContext['emitFailure']>[0];
98
+ /** Emit a SUCCESS upstream response event. No exchangeId, or a non-instance, writes nothing. */
99
+ export declare function emitUpstreamResponseOnce(ctx: UpstreamEventContext | undefined, args: Omit<ResponseEmit, 'exchangeId'> & {
55
100
  exchangeId: string | undefined;
56
- url: string;
57
- method: string;
58
- status: number;
59
- statusText: string;
60
- responseHeaders: Headers;
61
- responseBody: string;
62
- /** Epoch ms of the first streamed body chunk (streamed exchanges only). */
63
- firstByteTs?: number;
64
- model?: string;
65
101
  }): void;
66
- /**
67
- * Emit an upstream FAILURE response event. Always pairs with the request
68
- * event fired earlier so console readers don't see dangling request
69
- * events that never resolve. Status is reported as the upstream's HTTP
70
- * status when known, else `0` (network / DNS / TLS / timeout).
71
- */
72
- export declare function emitUpstreamFailureOnce(ctx: UpstreamEventContext | undefined, args: {
102
+ /** Emit an upstream FAILURE response event. No exchangeId, or a non-instance, writes nothing. */
103
+ export declare function emitUpstreamFailureOnce(ctx: UpstreamEventContext | undefined, args: Omit<FailureEmit, 'exchangeId'> & {
73
104
  exchangeId: string | undefined;
74
- url: string;
75
- method: string;
76
- status: number;
77
- statusText: string;
78
- responseHeaders?: Headers;
79
- responseBody?: string;
80
- /** Epoch ms of the first streamed body chunk (a stream that died mid-body still carries it). */
81
- firstByteTs?: number;
82
- error: string;
83
- model?: string;
84
105
  }): void;
85
106
  /**
86
107
  * Wrap a provider fetch+parse in a try/catch that guarantees a response
@@ -91,3 +112,4 @@ export declare function emitUpstreamFailureOnce(ctx: UpstreamEventContext | unde
91
112
  * req/resp pair.
92
113
  */
93
114
  export declare function withUpstreamFailureGuard<T>(ctx: UpstreamEventContext | undefined, exchangeId: string | undefined, url: string, method: string, fn: () => Promise<T>, model?: string): Promise<T>;
115
+ export {};
@@ -1,78 +1,153 @@
1
1
  import { emitUpstreamRequest, emitUpstreamResponse } from './http-exchange.js';
2
- /** Returns the exchangeId minted by `emitUpstreamRequest`, or undefined when the context is absent (capture off / mock mode / tests). */
2
+ /**
3
+ * Archive handle for one recorded upstream call. Built only while request
4
+ * capture is on. The conversation id lives in `#sessionId` and is copied
5
+ * onto archive lines by the methods below. It is not a field. A value that
6
+ * is not an instance of this class writes nothing. Executors that need the
7
+ * live conversation take it as their own argument.
8
+ */
9
+ export class UpstreamEventContext {
10
+ #sessionId;
11
+ /** Caller correlation value, when it sent one (see CaptureEvent). */
12
+ clientRequestId;
13
+ /** Gateway turn id (ADR-0021) — the capture turn-join key. */
14
+ requestId;
15
+ model;
16
+ recorder;
17
+ /**
18
+ * Client API-key identity of the owning turn, forwarded onto every event
19
+ * emitted through this context so the upstream leg is attributable to the
20
+ * same application as the client leg. Undefined for key-less legs (MITM
21
+ * tunnel, master-key access) — events then simply omit the field.
22
+ */
23
+ apiKey;
24
+ /**
25
+ * Deterministic conversation key of the owning turn (content-addressed,
26
+ * see CaptureEvent.conversationKey) forwarded onto every event emitted
27
+ * through this context.
28
+ */
29
+ conversationKey;
30
+ /**
31
+ * Non-default call label. The Layer 2 decision leg carries 'l2-decision'
32
+ * so the archive can tell it apart from candidate-execution events that
33
+ * share this request.
34
+ */
35
+ purpose;
36
+ /** Write-time turn identity, allocated once at the admission gate. */
37
+ turn;
38
+ constructor(init) {
39
+ this.#sessionId = init.sessionId;
40
+ this.clientRequestId = init.clientRequestId;
41
+ this.requestId = init.requestId;
42
+ this.model = init.model;
43
+ this.recorder = init.recorder;
44
+ this.apiKey = init.apiKey;
45
+ this.conversationKey = init.conversationKey;
46
+ this.purpose = init.purpose;
47
+ this.turn = init.turn;
48
+ }
49
+ /** Same sealed conversation, different archive label. */
50
+ withPurpose(purpose) {
51
+ return new UpstreamEventContext({
52
+ sessionId: this.#sessionId,
53
+ clientRequestId: this.clientRequestId,
54
+ requestId: this.requestId,
55
+ model: this.model,
56
+ recorder: this.recorder,
57
+ apiKey: this.apiKey,
58
+ conversationKey: this.conversationKey,
59
+ purpose,
60
+ turn: this.turn,
61
+ });
62
+ }
63
+ /** Returns the exchangeId minted for this upstream request. */
64
+ emitRequest(args) {
65
+ return emitUpstreamRequest({
66
+ recorder: this.recorder,
67
+ url: args.url,
68
+ method: args.method,
69
+ requestHeaders: args.requestHeaders,
70
+ requestBody: args.requestBody,
71
+ sessionId: this.#sessionId,
72
+ clientRequestId: this.clientRequestId,
73
+ requestId: this.requestId,
74
+ model: args.model ?? this.model,
75
+ proxy: args.proxy,
76
+ purpose: this.purpose,
77
+ turn: this.turn,
78
+ apiKey: this.apiKey,
79
+ conversationKey: this.conversationKey,
80
+ });
81
+ }
82
+ /** Emit a SUCCESS upstream response event. */
83
+ emitResponse(args) {
84
+ emitUpstreamResponse({
85
+ recorder: this.recorder,
86
+ url: args.url,
87
+ method: args.method,
88
+ status: args.status,
89
+ statusText: args.statusText,
90
+ responseHeaders: args.responseHeaders,
91
+ responseBody: args.responseBody,
92
+ firstByteTs: args.firstByteTs,
93
+ sessionId: this.#sessionId,
94
+ clientRequestId: this.clientRequestId,
95
+ requestId: this.requestId,
96
+ model: args.model ?? this.model,
97
+ exchangeId: args.exchangeId,
98
+ purpose: this.purpose,
99
+ turn: this.turn,
100
+ apiKey: this.apiKey,
101
+ conversationKey: this.conversationKey,
102
+ });
103
+ }
104
+ /**
105
+ * Emit an upstream FAILURE response event. Pairs with the request event
106
+ * fired earlier. Status is the upstream HTTP status when known, else `0`.
107
+ */
108
+ emitFailure(args) {
109
+ emitUpstreamResponse({
110
+ recorder: this.recorder,
111
+ url: args.url,
112
+ method: args.method,
113
+ status: args.status,
114
+ statusText: args.statusText,
115
+ responseHeaders: args.responseHeaders ?? new Headers(),
116
+ responseBody: args.responseBody ?? '',
117
+ firstByteTs: args.firstByteTs,
118
+ sessionId: this.#sessionId,
119
+ clientRequestId: this.clientRequestId,
120
+ requestId: this.requestId,
121
+ model: args.model ?? this.model,
122
+ exchangeId: args.exchangeId,
123
+ error: args.error,
124
+ purpose: this.purpose,
125
+ turn: this.turn,
126
+ apiKey: this.apiKey,
127
+ conversationKey: this.conversationKey,
128
+ });
129
+ }
130
+ }
131
+ function captureOf(ctx) {
132
+ return ctx instanceof UpstreamEventContext ? ctx : undefined;
133
+ }
134
+ /** Returns the exchangeId minted by `emitUpstreamRequest`, or undefined when capture is off or the handle is not a real context. */
3
135
  export function emitUpstreamRequestOnce(ctx, args) {
4
- if (!ctx)
5
- return undefined;
6
- return emitUpstreamRequest({
7
- recorder: ctx.recorder,
8
- url: args.url,
9
- method: args.method,
10
- requestHeaders: args.requestHeaders,
11
- requestBody: args.requestBody,
12
- sessionId: ctx.sessionId,
13
- clientRequestId: ctx.clientRequestId,
14
- requestId: ctx.requestId,
15
- model: args.model ?? ctx.model,
16
- proxy: args.proxy,
17
- purpose: ctx.purpose,
18
- turn: ctx.turn,
19
- apiKey: ctx.apiKey,
20
- conversationKey: ctx.conversationKey,
21
- });
136
+ return captureOf(ctx)?.emitRequest(args);
22
137
  }
23
- /** Emit a SUCCESS upstream response event. */
138
+ /** Emit a SUCCESS upstream response event. No exchangeId, or a non-instance, writes nothing. */
24
139
  export function emitUpstreamResponseOnce(ctx, args) {
25
- if (!ctx || !args.exchangeId)
140
+ const real = captureOf(ctx);
141
+ if (!real || !args.exchangeId)
26
142
  return;
27
- emitUpstreamResponse({
28
- recorder: ctx.recorder,
29
- url: args.url,
30
- method: args.method,
31
- status: args.status,
32
- statusText: args.statusText,
33
- responseHeaders: args.responseHeaders,
34
- responseBody: args.responseBody,
35
- firstByteTs: args.firstByteTs,
36
- sessionId: ctx.sessionId,
37
- clientRequestId: ctx.clientRequestId,
38
- requestId: ctx.requestId,
39
- model: args.model ?? ctx.model,
40
- exchangeId: args.exchangeId,
41
- purpose: ctx.purpose,
42
- turn: ctx.turn,
43
- apiKey: ctx.apiKey,
44
- conversationKey: ctx.conversationKey,
45
- });
143
+ real.emitResponse({ ...args, exchangeId: args.exchangeId });
46
144
  }
47
- /**
48
- * Emit an upstream FAILURE response event. Always pairs with the request
49
- * event fired earlier so console readers don't see dangling request
50
- * events that never resolve. Status is reported as the upstream's HTTP
51
- * status when known, else `0` (network / DNS / TLS / timeout).
52
- */
145
+ /** Emit an upstream FAILURE response event. No exchangeId, or a non-instance, writes nothing. */
53
146
  export function emitUpstreamFailureOnce(ctx, args) {
54
- if (!ctx || !args.exchangeId)
147
+ const real = captureOf(ctx);
148
+ if (!real || !args.exchangeId)
55
149
  return;
56
- emitUpstreamResponse({
57
- recorder: ctx.recorder,
58
- url: args.url,
59
- method: args.method,
60
- status: args.status,
61
- statusText: args.statusText,
62
- responseHeaders: args.responseHeaders ?? new Headers(),
63
- responseBody: args.responseBody ?? '',
64
- firstByteTs: args.firstByteTs,
65
- sessionId: ctx.sessionId,
66
- clientRequestId: ctx.clientRequestId,
67
- requestId: ctx.requestId,
68
- model: args.model ?? ctx.model,
69
- exchangeId: args.exchangeId,
70
- error: args.error,
71
- purpose: ctx.purpose,
72
- turn: ctx.turn,
73
- apiKey: ctx.apiKey,
74
- conversationKey: ctx.conversationKey,
75
- });
150
+ real.emitFailure({ ...args, exchangeId: args.exchangeId });
76
151
  }
77
152
  /**
78
153
  * Wrap a provider fetch+parse in a try/catch that guarantees a response
@@ -49,6 +49,15 @@ export interface CatalogModel {
49
49
  defaultEffort?: ReasoningEffort;
50
50
  attachment?: boolean;
51
51
  reasoning?: boolean;
52
+ /**
53
+ * OCR extension (ADR-0035): the vendor's on/off fact for a thinking model.
54
+ * `false` = the model ALWAYS thinks — `none` must never enter its effort
55
+ * ladder, even when the def carries no reasoningOptions (the boolean would
56
+ * otherwise expand to the full ladder INCLUDING `none`, and the router
57
+ * would send thinking-off into the vendor's 1210 refusal). `true`/absent =
58
+ * off allowed / not stated. Written by the shared catalog-def mapper.
59
+ */
60
+ reasoningDisableable?: boolean;
52
61
  tool_call?: boolean;
53
62
  temperature?: boolean;
54
63
  /**
@@ -251,8 +251,8 @@ export declare class CapabilityUnsupportedError extends Error {
251
251
  */
252
252
  export declare class WriteFailedError extends Error {
253
253
  readonly method: string;
254
- readonly cause?: unknown;
255
- constructor(method: string, detail: string, cause?: unknown);
254
+ readonly cause?: unknown | undefined;
255
+ constructor(method: string, detail: string, cause?: unknown | undefined);
256
256
  }
257
257
  /** providerId has no matching opencode v2 integration id — typed so the reconcile step (B3) can consume it. */
258
258
  export declare class IntegrationUnmatchedError extends Error {