@mupt-ai/dari-gateway 0.2.0 → 0.3.0

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 (385) hide show
  1. package/README.md +8 -4
  2. package/dist/bun.d.ts +2 -0
  3. package/dist/bun.js +4 -0
  4. package/dist/context/eval_catalog.d.ts +3 -0
  5. package/dist/context/eval_catalog.js +7 -0
  6. package/dist/context/interaction_fingerprint.d.ts +27 -0
  7. package/dist/context/interaction_fingerprint.js +519 -0
  8. package/dist/context/interaction_fingerprint_format.d.ts +42 -0
  9. package/dist/context/interaction_fingerprint_format.js +113 -0
  10. package/dist/context/interaction_lease_depth.d.ts +2 -0
  11. package/dist/context/interaction_lease_depth.js +110 -0
  12. package/dist/context/lease_history.d.ts +7 -0
  13. package/dist/context/lease_history.js +158 -0
  14. package/dist/context/pricing.d.ts +4 -0
  15. package/dist/context/pricing.js +27 -0
  16. package/dist/context/provider_prompt_accounting.d.ts +14 -0
  17. package/dist/context/provider_prompt_accounting.js +187 -0
  18. package/dist/context/routing_context.d.ts +40 -0
  19. package/dist/context/routing_context.js +211 -0
  20. package/dist/http/auth.d.ts +2 -0
  21. package/dist/http/auth.js +35 -0
  22. package/dist/http/errors.d.ts +19 -0
  23. package/dist/http/errors.js +89 -0
  24. package/dist/http/handler.d.ts +12 -0
  25. package/dist/http/handler.js +88 -0
  26. package/dist/http/http2_server.d.ts +6 -0
  27. package/dist/http/http2_server.js +103 -0
  28. package/dist/http/serve.d.ts +20 -0
  29. package/dist/http/serve.js +78 -0
  30. package/dist/index.d.ts +7 -0
  31. package/dist/index.js +9 -0
  32. package/dist/ingress/anthropic_messages.d.ts +3 -0
  33. package/dist/ingress/anthropic_messages.js +122 -0
  34. package/dist/ingress/anthropic_models.d.ts +1 -0
  35. package/dist/ingress/anthropic_models.js +23 -0
  36. package/dist/ingress/chat_completions.d.ts +2 -0
  37. package/dist/ingress/chat_completions.js +44 -0
  38. package/dist/ingress/claude_code/request_context.d.ts +2 -0
  39. package/dist/ingress/claude_code/request_context.js +31 -0
  40. package/dist/ingress/codex.d.ts +3 -0
  41. package/dist/ingress/codex.js +69 -0
  42. package/dist/ingress/codex_models.d.ts +2 -0
  43. package/dist/ingress/codex_models.js +89 -0
  44. package/dist/ingress/json.d.ts +1 -0
  45. package/dist/ingress/json.js +95 -0
  46. package/dist/ingress/models.d.ts +1 -0
  47. package/dist/ingress/models.js +65 -0
  48. package/dist/ingress/muse_code.d.ts +2 -0
  49. package/dist/ingress/muse_code.js +8 -0
  50. package/dist/ingress/muse_models.d.ts +9 -0
  51. package/dist/ingress/muse_models.js +32 -0
  52. package/dist/interaction/capabilities.d.ts +3 -0
  53. package/dist/interaction/capabilities.js +24 -0
  54. package/dist/interaction/image.d.ts +4 -0
  55. package/dist/interaction/image.js +13 -0
  56. package/dist/interaction/portable_reasoning_state.d.ts +4 -0
  57. package/dist/interaction/portable_reasoning_state.js +18 -0
  58. package/dist/interaction/provider_continuation_state.d.ts +6 -0
  59. package/dist/interaction/provider_continuation_state.js +27 -0
  60. package/dist/interaction/tool_identity.d.ts +10 -0
  61. package/dist/interaction/tool_identity.js +41 -0
  62. package/dist/interaction/types.d.ts +204 -0
  63. package/dist/interaction/types.js +5 -0
  64. package/dist/models/catalog.d.ts +35 -0
  65. package/dist/models/catalog.js +232 -0
  66. package/dist/models/context_limits.d.ts +2 -0
  67. package/dist/models/context_limits.js +7 -0
  68. package/dist/models/execution_provider.d.ts +9 -0
  69. package/dist/models/execution_provider.js +17 -0
  70. package/dist/models/gateways.d.ts +11 -0
  71. package/dist/models/gateways.js +45 -0
  72. package/dist/models/ids.d.ts +1 -0
  73. package/dist/models/ids.js +15 -0
  74. package/dist/models/provider_defaults.d.ts +4 -0
  75. package/dist/models/provider_defaults.js +44 -0
  76. package/dist/models/runtime_overrides.d.ts +7 -0
  77. package/dist/models/runtime_overrides.js +226 -0
  78. package/dist/models/subscriptions.d.ts +8 -0
  79. package/dist/models/subscriptions.js +37 -0
  80. package/dist/observability/errors.d.ts +8 -0
  81. package/dist/observability/errors.js +11 -0
  82. package/dist/platform/platform.d.ts +156 -0
  83. package/dist/platform/platform.js +11 -0
  84. package/dist/platform/routing_contract.d.ts +2 -0
  85. package/dist/platform/routing_contract.js +5 -0
  86. package/dist/protocol/input/anthropic_messages.d.ts +2 -0
  87. package/dist/protocol/input/anthropic_messages.js +494 -0
  88. package/dist/protocol/input/chat_completions.d.ts +4 -0
  89. package/dist/protocol/input/chat_completions.js +214 -0
  90. package/dist/protocol/input/chat_completions_validation.d.ts +2 -0
  91. package/dist/protocol/input/chat_completions_validation.js +258 -0
  92. package/dist/protocol/input/openai_responses.d.ts +2 -0
  93. package/dist/protocol/input/openai_responses.js +666 -0
  94. package/dist/protocol/input/router_model.d.ts +7 -0
  95. package/dist/protocol/input/router_model.js +43 -0
  96. package/dist/protocol/output/anthropic_messages.d.ts +10 -0
  97. package/dist/protocol/output/anthropic_messages.js +15 -0
  98. package/dist/protocol/output/anthropic_messages_output.d.ts +10 -0
  99. package/dist/protocol/output/anthropic_messages_output.js +124 -0
  100. package/dist/protocol/output/anthropic_messages_stream.d.ts +16 -0
  101. package/dist/protocol/output/anthropic_messages_stream.js +267 -0
  102. package/dist/protocol/output/chat_completions.d.ts +29 -0
  103. package/dist/protocol/output/chat_completions.js +406 -0
  104. package/dist/protocol/output/chat_reasoning_detail.d.ts +2 -0
  105. package/dist/protocol/output/chat_reasoning_detail.js +13 -0
  106. package/dist/protocol/output/delivered_stream_snapshot.d.ts +10 -0
  107. package/dist/protocol/output/delivered_stream_snapshot.js +31 -0
  108. package/dist/protocol/output/openai_responses.d.ts +10 -0
  109. package/dist/protocol/output/openai_responses.js +14 -0
  110. package/dist/protocol/output/openai_responses_output.d.ts +14 -0
  111. package/dist/protocol/output/openai_responses_output.js +125 -0
  112. package/dist/protocol/output/openai_responses_stream.d.ts +18 -0
  113. package/dist/protocol/output/openai_responses_stream.js +546 -0
  114. package/dist/protocol/output/sse_stream.d.ts +31 -0
  115. package/dist/protocol/output/sse_stream.js +67 -0
  116. package/dist/providers/anthropic_safeguards.d.ts +11 -0
  117. package/dist/providers/anthropic_safeguards.js +81 -0
  118. package/dist/providers/aws_assume_role.d.ts +17 -0
  119. package/dist/providers/aws_assume_role.js +107 -0
  120. package/dist/providers/cancel_estimate.d.ts +43 -0
  121. package/dist/providers/cancel_estimate.js +89 -0
  122. package/dist/providers/execution.d.ts +72 -0
  123. package/dist/providers/execution.js +491 -0
  124. package/dist/providers/model_capabilities.d.ts +11 -0
  125. package/dist/providers/model_capabilities.js +29 -0
  126. package/dist/providers/pi.d.ts +65 -0
  127. package/dist/providers/pi.js +325 -0
  128. package/dist/providers/pi_context.d.ts +7 -0
  129. package/dist/providers/pi_context.js +405 -0
  130. package/dist/providers/pi_options.d.ts +11 -0
  131. package/dist/providers/pi_options.js +410 -0
  132. package/dist/providers/pi_output.d.ts +2 -0
  133. package/dist/providers/pi_output.js +453 -0
  134. package/dist/providers/provider_attempt_runner.d.ts +55 -0
  135. package/dist/providers/provider_attempt_runner.js +90 -0
  136. package/dist/providers/provider_call.d.ts +77 -0
  137. package/dist/providers/provider_call.js +296 -0
  138. package/dist/providers/provider_reported_cost.d.ts +10 -0
  139. package/dist/providers/provider_reported_cost.js +123 -0
  140. package/dist/providers/reasoning.d.ts +12 -0
  141. package/dist/providers/reasoning.js +30 -0
  142. package/dist/providers/response_capture.d.ts +23 -0
  143. package/dist/providers/response_capture.js +47 -0
  144. package/dist/providers/stream_finalization.d.ts +46 -0
  145. package/dist/providers/stream_finalization.js +165 -0
  146. package/dist/providers/subscription_limits.d.ts +33 -0
  147. package/dist/providers/subscription_limits.js +179 -0
  148. package/dist/providers/subscription_usage.d.ts +10 -0
  149. package/dist/providers/subscription_usage.js +79 -0
  150. package/dist/runtime/claude_code/auxiliary_routing.d.ts +3 -0
  151. package/dist/runtime/claude_code/auxiliary_routing.js +60 -0
  152. package/dist/runtime/customer_disconnect.d.ts +20 -0
  153. package/dist/runtime/customer_disconnect.js +49 -0
  154. package/dist/runtime/explicit_model.d.ts +7 -0
  155. package/dist/runtime/explicit_model.js +57 -0
  156. package/dist/runtime/harness_identity/conversation.d.ts +33 -0
  157. package/dist/runtime/harness_identity/conversation.js +39 -0
  158. package/dist/runtime/harness_identity/extract.d.ts +8 -0
  159. package/dist/runtime/harness_identity/extract.js +214 -0
  160. package/dist/runtime/harness_identity/index.d.ts +2 -0
  161. package/dist/runtime/harness_identity/index.js +2 -0
  162. package/dist/runtime/prepare_routed_completion.d.ts +33 -0
  163. package/dist/runtime/prepare_routed_completion.js +186 -0
  164. package/dist/runtime/provider_stream_lifecycle.d.ts +31 -0
  165. package/dist/runtime/provider_stream_lifecycle.js +88 -0
  166. package/dist/runtime/provider_stream_outcome.d.ts +23 -0
  167. package/dist/runtime/provider_stream_outcome.js +55 -0
  168. package/dist/runtime/request_tags.d.ts +7 -0
  169. package/dist/runtime/request_tags.js +33 -0
  170. package/dist/runtime/routed_completion.d.ts +17 -0
  171. package/dist/runtime/routed_completion.js +80 -0
  172. package/dist/runtime/routed_protocol.d.ts +30 -0
  173. package/dist/runtime/routed_protocol.js +1 -0
  174. package/dist/runtime/routed_runtime.d.ts +40 -0
  175. package/dist/runtime/routed_runtime.js +535 -0
  176. package/dist/runtime/routing_lease.d.ts +26 -0
  177. package/dist/runtime/routing_lease.js +210 -0
  178. package/dist/runtime/runtime_reporter.d.ts +71 -0
  179. package/dist/runtime/runtime_reporter.js +35 -0
  180. package/dist/runtime/strict_provider_access.d.ts +6 -0
  181. package/dist/runtime/strict_provider_access.js +40 -0
  182. package/dist/self_host/config.d.ts +46 -0
  183. package/dist/self_host/config.js +105 -0
  184. package/dist/self_host/main.d.ts +16 -0
  185. package/dist/self_host/main.js +95 -0
  186. package/dist/self_host/platform.d.ts +13 -0
  187. package/dist/self_host/platform.js +50 -0
  188. package/dist/self_host/prefix_store.d.ts +46 -0
  189. package/dist/self_host/prefix_store.js +261 -0
  190. package/dist/self_host/resolve.d.ts +28 -0
  191. package/dist/self_host/resolve.js +167 -0
  192. package/dist/self_host/subscriptions/access.d.ts +10 -0
  193. package/dist/self_host/subscriptions/access.js +37 -0
  194. package/dist/self_host/subscriptions/http.d.ts +15 -0
  195. package/dist/self_host/subscriptions/http.js +187 -0
  196. package/dist/self_host/subscriptions/index.d.ts +24 -0
  197. package/dist/self_host/subscriptions/index.js +24 -0
  198. package/dist/self_host/subscriptions/oauth.d.ts +59 -0
  199. package/dist/self_host/subscriptions/oauth.js +376 -0
  200. package/dist/self_host/subscriptions/store.d.ts +78 -0
  201. package/dist/self_host/subscriptions/store.js +236 -0
  202. package/dist/strategies/auto_routing_selector.d.ts +12 -0
  203. package/dist/strategies/auto_routing_selector.js +61 -0
  204. package/dist/strategies/eval_relevance.d.ts +17 -0
  205. package/dist/strategies/eval_relevance.js +32 -0
  206. package/dist/strategies/fallback_decision.d.ts +8 -0
  207. package/dist/strategies/fallback_decision.js +17 -0
  208. package/dist/strategies/priority_selector.d.ts +7 -0
  209. package/dist/strategies/priority_selector.js +37 -0
  210. package/dist/strategies/route_decision.d.ts +22 -0
  211. package/dist/strategies/route_decision.js +65 -0
  212. package/dist/strategies/routing.d.ts +74 -0
  213. package/dist/strategies/routing.js +280 -0
  214. package/dist/strategies/selector_projection.d.ts +15 -0
  215. package/dist/strategies/selector_projection.js +161 -0
  216. package/dist/telemetry/background.d.ts +7 -0
  217. package/dist/telemetry/background.js +33 -0
  218. package/dist/telemetry/benchmark_replay.d.ts +37 -0
  219. package/dist/telemetry/benchmark_replay.js +108 -0
  220. package/dist/telemetry/billing_finalization.d.ts +64 -0
  221. package/dist/telemetry/billing_finalization.js +96 -0
  222. package/dist/telemetry/ingest_values.d.ts +4 -0
  223. package/dist/telemetry/ingest_values.js +12 -0
  224. package/dist/telemetry/model_cost.d.ts +3 -0
  225. package/dist/telemetry/model_cost.js +15 -0
  226. package/dist/telemetry/prefix_state.d.ts +47 -0
  227. package/dist/telemetry/prefix_state.js +116 -0
  228. package/dist/telemetry/report.d.ts +88 -0
  229. package/dist/telemetry/report.js +621 -0
  230. package/dist/telemetry/requests/interaction_observability.d.ts +45 -0
  231. package/dist/telemetry/requests/interaction_observability.js +180 -0
  232. package/dist/telemetry/requests/retained_interaction_request.d.ts +8 -0
  233. package/dist/telemetry/requests/retained_interaction_request.js +10 -0
  234. package/dist/telemetry/scrub.d.ts +2 -0
  235. package/dist/telemetry/scrub.js +31 -0
  236. package/dist/telemetry/skills/claude/catalog.d.ts +6 -0
  237. package/dist/telemetry/skills/claude/catalog.js +99 -0
  238. package/dist/telemetry/skills/claude/file_reads.d.ts +4 -0
  239. package/dist/telemetry/skills/claude/file_reads.js +110 -0
  240. package/dist/telemetry/skills/claude/observations.d.ts +3 -0
  241. package/dist/telemetry/skills/claude/observations.js +366 -0
  242. package/dist/telemetry/skills/codex/catalog.d.ts +11 -0
  243. package/dist/telemetry/skills/codex/catalog.js +138 -0
  244. package/dist/telemetry/skills/codex/exec_file_reads.d.ts +9 -0
  245. package/dist/telemetry/skills/codex/exec_file_reads.js +36 -0
  246. package/dist/telemetry/skills/codex/explicit.d.ts +3 -0
  247. package/dist/telemetry/skills/codex/explicit.js +90 -0
  248. package/dist/telemetry/skills/codex/file_reads.d.ts +4 -0
  249. package/dist/telemetry/skills/codex/file_reads.js +37 -0
  250. package/dist/telemetry/skills/codex/identity.d.ts +2 -0
  251. package/dist/telemetry/skills/codex/identity.js +20 -0
  252. package/dist/telemetry/skills/observations.d.ts +21 -0
  253. package/dist/telemetry/skills/observations.js +41 -0
  254. package/dist/telemetry/skills/paths.d.ts +11 -0
  255. package/dist/telemetry/skills/paths.js +61 -0
  256. package/dist/telemetry/skills/pi.d.ts +6 -0
  257. package/dist/telemetry/skills/pi.js +223 -0
  258. package/dist/telemetry/skills/shell_file_reads.d.ts +7 -0
  259. package/dist/telemetry/skills/shell_file_reads.js +226 -0
  260. package/dist/telemetry/skills/shell_words.d.ts +13 -0
  261. package/dist/telemetry/skills/shell_words.js +189 -0
  262. package/dist/telemetry/tools/input.d.ts +2 -0
  263. package/dist/telemetry/tools/input.js +14 -0
  264. package/dist/telemetry/tools/observations.d.ts +29 -0
  265. package/dist/telemetry/tools/observations.js +247 -0
  266. package/dist/telemetry/usage.d.ts +57 -0
  267. package/dist/telemetry/usage.js +136 -0
  268. package/dist/types.d.ts +97 -0
  269. package/dist/types.js +1 -0
  270. package/package.json +22 -7
  271. package/src/bun.ts +2 -2
  272. package/src/context/eval_catalog.ts +1 -1
  273. package/src/context/interaction_fingerprint.ts +9 -9
  274. package/src/context/interaction_fingerprint_format.ts +1 -1
  275. package/src/context/interaction_lease_depth.ts +1 -1
  276. package/src/context/pricing.ts +6 -3
  277. package/src/context/provider_prompt_accounting.ts +5 -5
  278. package/src/context/routing_context.ts +16 -16
  279. package/src/http/auth.ts +1 -1
  280. package/src/http/handler.ts +15 -8
  281. package/src/http/serve.ts +3 -3
  282. package/src/index.ts +14 -6
  283. package/src/ingress/anthropic_messages.ts +12 -12
  284. package/src/ingress/anthropic_models.ts +1 -1
  285. package/src/ingress/chat_completions.ts +17 -10
  286. package/src/ingress/claude_code/request_context.ts +2 -2
  287. package/src/ingress/codex.ts +14 -10
  288. package/src/ingress/codex_models.ts +4 -4
  289. package/src/ingress/json.ts +1 -1
  290. package/src/ingress/models.ts +22 -10
  291. package/src/ingress/muse_code.ts +9 -0
  292. package/src/ingress/muse_models.ts +36 -0
  293. package/src/interaction/capabilities.ts +1 -1
  294. package/src/interaction/portable_reasoning_state.ts +1 -1
  295. package/src/interaction/provider_continuation_state.ts +1 -1
  296. package/src/interaction/tool_identity.ts +2 -2
  297. package/src/interaction/types.ts +2 -0
  298. package/src/models/catalog.ts +310 -0
  299. package/src/models/context_limits.ts +1 -1
  300. package/src/models/execution_provider.ts +5 -2
  301. package/src/models/ids.ts +1 -1
  302. package/src/models/provider_defaults.ts +4 -1
  303. package/src/models/runtime_overrides.ts +23 -1
  304. package/src/models/subscriptions.ts +54 -0
  305. package/src/observability/errors.ts +2 -2
  306. package/src/platform/platform.ts +9 -5
  307. package/src/protocol/input/anthropic_messages.ts +7 -7
  308. package/src/protocol/input/chat_completions.ts +8 -6
  309. package/src/protocol/input/chat_completions_validation.ts +6 -5
  310. package/src/protocol/input/openai_responses.ts +6 -6
  311. package/src/protocol/output/anthropic_messages.ts +3 -3
  312. package/src/protocol/output/anthropic_messages_output.ts +6 -4
  313. package/src/protocol/output/anthropic_messages_stream.ts +7 -7
  314. package/src/protocol/output/chat_completions.ts +13 -13
  315. package/src/protocol/output/chat_reasoning_detail.ts +1 -1
  316. package/src/protocol/output/openai_responses.ts +3 -3
  317. package/src/protocol/output/openai_responses_output.ts +4 -2
  318. package/src/protocol/output/openai_responses_stream.ts +6 -6
  319. package/src/protocol/output/sse_stream.ts +3 -3
  320. package/src/providers/anthropic_safeguards.ts +2 -2
  321. package/src/providers/aws_assume_role.ts +2 -2
  322. package/src/providers/cancel_estimate.ts +1 -1
  323. package/src/providers/execution.ts +17 -17
  324. package/src/providers/pi.ts +34 -19
  325. package/src/providers/pi_context.ts +6 -6
  326. package/src/providers/pi_options.ts +27 -11
  327. package/src/providers/pi_output.ts +5 -5
  328. package/src/providers/provider_attempt_runner.ts +1 -1
  329. package/src/providers/provider_call.ts +21 -17
  330. package/src/providers/provider_reported_cost.ts +3 -3
  331. package/src/providers/reasoning.ts +2 -2
  332. package/src/providers/stream_finalization.ts +9 -9
  333. package/src/providers/subscription_limits.ts +24 -7
  334. package/src/runtime/claude_code/auxiliary_routing.ts +5 -5
  335. package/src/runtime/customer_disconnect.ts +1 -1
  336. package/src/runtime/explicit_model.ts +19 -20
  337. package/src/runtime/harness_identity/conversation.ts +1 -1
  338. package/src/runtime/harness_identity/extract.ts +3 -3
  339. package/src/runtime/harness_identity/index.ts +2 -2
  340. package/src/runtime/prepare_routed_completion.ts +35 -16
  341. package/src/runtime/provider_stream_lifecycle.ts +1 -1
  342. package/src/runtime/provider_stream_outcome.ts +2 -2
  343. package/src/runtime/request_tags.ts +2 -2
  344. package/src/runtime/routed_completion.ts +11 -11
  345. package/src/runtime/routed_protocol.ts +2 -2
  346. package/src/runtime/routed_runtime.ts +20 -19
  347. package/src/runtime/routing_lease.ts +7 -7
  348. package/src/runtime/runtime_reporter.ts +3 -3
  349. package/src/runtime/strict_provider_access.ts +42 -0
  350. package/src/self_host/main.ts +12 -12
  351. package/src/self_host/platform.ts +5 -5
  352. package/src/self_host/prefix_store.ts +2 -2
  353. package/src/self_host/resolve.ts +17 -31
  354. package/src/self_host/subscriptions/access.ts +2 -2
  355. package/src/self_host/subscriptions/http.ts +2 -2
  356. package/src/self_host/subscriptions/index.ts +4 -4
  357. package/src/self_host/subscriptions/oauth.ts +139 -8
  358. package/src/self_host/subscriptions/store.ts +2 -1
  359. package/src/strategies/auto_routing_selector.ts +2 -2
  360. package/src/strategies/eval_relevance.ts +3 -3
  361. package/src/strategies/priority_selector.ts +1 -1
  362. package/src/strategies/route_decision.ts +7 -7
  363. package/src/strategies/routing.ts +10 -10
  364. package/src/strategies/selector_projection.ts +2 -2
  365. package/src/telemetry/billing_finalization.ts +3 -3
  366. package/src/telemetry/model_cost.ts +1 -1
  367. package/src/telemetry/prefix_state.ts +4 -4
  368. package/src/telemetry/report.ts +21 -21
  369. package/src/telemetry/requests/interaction_observability.ts +1 -1
  370. package/src/telemetry/requests/retained_interaction_request.ts +1 -1
  371. package/src/telemetry/skills/claude/catalog.ts +1 -1
  372. package/src/telemetry/skills/claude/file_reads.ts +6 -6
  373. package/src/telemetry/skills/claude/observations.ts +3 -3
  374. package/src/telemetry/skills/codex/catalog.ts +3 -3
  375. package/src/telemetry/skills/codex/exec_file_reads.ts +4 -4
  376. package/src/telemetry/skills/codex/explicit.ts +3 -3
  377. package/src/telemetry/skills/codex/file_reads.ts +5 -5
  378. package/src/telemetry/skills/observations.ts +7 -7
  379. package/src/telemetry/skills/pi.ts +3 -3
  380. package/src/telemetry/skills/shell_file_reads.ts +2 -2
  381. package/src/telemetry/skills/shell_words.ts +1 -1
  382. package/src/telemetry/tools/input.ts +1 -1
  383. package/src/telemetry/tools/observations.ts +9 -9
  384. package/src/telemetry/usage.ts +5 -5
  385. package/src/types.ts +20 -3
@@ -0,0 +1,325 @@
1
+ import { getSupportedThinkingLevels, normalizeContext, } from "@mupt-ai/pi-ai";
2
+ import { anthropicMessagesApi } from "@mupt-ai/pi-ai/api/anthropic-messages.lazy";
3
+ import { azureOpenAIResponsesApi } from "@mupt-ai/pi-ai/api/azure-openai-responses.lazy";
4
+ import { bedrockConverseStreamApi } from "@mupt-ai/pi-ai/api/bedrock-converse-stream.lazy";
5
+ import { googleGenerativeAIApi } from "@mupt-ai/pi-ai/api/google-generative-ai.lazy";
6
+ import { googleVertexApi } from "@mupt-ai/pi-ai/api/google-vertex.lazy";
7
+ import { mistralConversationsApi } from "@mupt-ai/pi-ai/api/mistral-conversations.lazy";
8
+ import { openAICodexResponsesApi } from "@mupt-ai/pi-ai/api/openai-codex-responses.lazy";
9
+ import { openAICompletionsApi } from "@mupt-ai/pi-ai/api/openai-completions.lazy";
10
+ import { openAIResponsesApi } from "@mupt-ai/pi-ai/api/openai-responses.lazy";
11
+ import { getBuiltinModel } from "@mupt-ai/pi-ai/providers/all";
12
+ import { REASONING_EFFORTS, isRecord, nativeModelId, providerForModel, } from "@mupt-ai/dari-router/policy-engine";
13
+ import { HttpError } from "../http/errors.js";
14
+ import { effectiveContextWindow } from "../models/context_limits.js";
15
+ import { defaultBaseUrl } from "../models/provider_defaults.js";
16
+ import { contextWindowOverride, inputModalitiesOverride, providerThinkingLevelOverrides, runtimeCompat, withThinkingLevelOverrides, } from "../models/runtime_overrides.js";
17
+ import { BEDROCK_API } from "./model_capabilities.js";
18
+ import { buildContext } from "./pi_context.js";
19
+ import { providerIdentityFromModel } from "../interaction/provider_continuation_state.js";
20
+ import { buildOptions, supportedThinkingLevels } from "./pi_options.js";
21
+ import { eventsWithSafeguardResults, forwardsAnthropicSafeguards, messageWithSafeguardResults, safeguardResultsTap, } from "./anthropic_safeguards.js";
22
+ import { eventsWithProviderReportedCost, messageWithProviderReportedCost, providerReportedCostTap, reportsProviderCost, } from "./provider_reported_cost.js";
23
+ import { eventsWithSubscriptionCacheWrites, messageWithSubscriptionCacheWrites, modelPricingFromPiModel, } from "./subscription_usage.js";
24
+ // Pi's built-in providers stream through these API implementations unchanged.
25
+ // Dari always passes resolved credentials, so it dispatches on the API rather
26
+ // than through a Models collection with its own auth resolution.
27
+ const PI_APIS = {
28
+ "anthropic-messages": anthropicMessagesApi(),
29
+ "azure-openai-responses": azureOpenAIResponsesApi(),
30
+ "bedrock-converse-stream": bedrockConverseStreamApi(),
31
+ "google-generative-ai": googleGenerativeAIApi(),
32
+ "google-vertex": googleVertexApi(),
33
+ "mistral-conversations": mistralConversationsApi(),
34
+ "openai-codex-responses": openAICodexResponsesApi(),
35
+ "openai-completions": openAICompletionsApi(),
36
+ "openai-responses": openAIResponsesApi(),
37
+ };
38
+ function piStreamSimple(model, context, options) {
39
+ const api = PI_APIS[String(model.api)];
40
+ if (!api)
41
+ throw new Error(`No Pi API implementation for ${String(model.api)}.`);
42
+ return api.streamSimple(model, normalizeContext(context), options);
43
+ }
44
+ export const pi = {
45
+ completeSimple: async (model, context, options) => await piStreamSimple(model, context, options).result(),
46
+ streamSimple: (model, context, options) => piStreamSimple(model, context, options),
47
+ getModel: (provider, id) => getBuiltinModel(provider, id),
48
+ getSupportedThinkingLevels: (model) => getSupportedThinkingLevels(model),
49
+ };
50
+ export async function completeInteractionModel(args) {
51
+ const model = modelAtEndpoint(buildModel(pi, args.model, args.pricing, args.config), args.endpoint);
52
+ const source = providerIdentityFromModel(model);
53
+ const context = buildContext(args.request, source);
54
+ const costTap = reportsProviderCost(model) ? providerReportedCostTap(args.fetch) : undefined;
55
+ const safeguards = forwardsAnthropicSafeguards(args.request, model)
56
+ ? safeguardResultsTap(costTap?.fetch ?? args.fetch)
57
+ : undefined;
58
+ const options = buildOptions(args.request, args.auth ?? args.apiKey, args.model, model, pi, args.sessionId, args.selectedReasoningEffort, args.timeoutMs, args.signal, safeguards?.fetch ?? costTap?.fetch ?? args.fetch);
59
+ const usageContext = subscriptionUsageContext(args.model, model, source);
60
+ const message = options.emitProviderEvents === true
61
+ ? await completeProviderEventStream(pi.streamSimple(model, context, options))
62
+ : await pi.completeSimple(model, context, options);
63
+ return {
64
+ message: messageWithProviderReportedCost(messageWithSubscriptionCacheWrites(messageWithSafeguardResults(message, safeguards?.results()), usageContext), costTap?.cost()),
65
+ source,
66
+ };
67
+ }
68
+ function subscriptionUsageContext(modelId, model, source) {
69
+ // pi-ai priced this call from model.cost, so the repricing must read it back
70
+ // rather than the caller's catalog pricing, which fast mode can override.
71
+ return { api: source.api, model: modelId, pricing: modelPricingFromPiModel(model) };
72
+ }
73
+ export async function completeProviderEventStream(events) {
74
+ let providerResponse;
75
+ const providerOutput = new Map();
76
+ for await (const event of events) {
77
+ if (event.type === "provider_event" && isRecord(event.event)) {
78
+ const raw = event.event;
79
+ if (raw.type === "response.output_item.done" && isRecord(raw.item)) {
80
+ const outputIndex = raw.output_index;
81
+ if (typeof outputIndex !== "number" || !Number.isInteger(outputIndex) || outputIndex < 0) {
82
+ throw new Error("Provider output item is missing a valid output index.");
83
+ }
84
+ providerOutput.set(outputIndex, raw.item);
85
+ }
86
+ if ((raw.type === "response.completed" || raw.type === "response.incomplete") &&
87
+ isRecord(raw.response)) {
88
+ providerResponse = raw.response;
89
+ }
90
+ continue;
91
+ }
92
+ if (event.type === "done" && isRecord(event.message)) {
93
+ if (providerResponse) {
94
+ const terminalOutput = providerResponse.output;
95
+ if ((!Array.isArray(terminalOutput) || terminalOutput.length === 0) && providerOutput.size > 0) {
96
+ const entries = [...providerOutput.entries()].sort(([left], [right]) => left - right);
97
+ if (entries.some(([index], position) => index !== position)) {
98
+ throw new Error("Provider stream ended with non-contiguous output item indexes.");
99
+ }
100
+ providerResponse = { ...providerResponse, output: entries.map(([, item]) => item) };
101
+ }
102
+ return { ...event.message, providerResponse };
103
+ }
104
+ return event.message;
105
+ }
106
+ if (event.type === "error" && isRecord(event.error))
107
+ return event.error;
108
+ }
109
+ throw new Error("Provider stream ended without a terminal event.");
110
+ }
111
+ export async function streamInteractionModel(args) {
112
+ const model = modelAtEndpoint(buildModel(pi, args.model, args.pricing, args.config), args.endpoint);
113
+ const source = providerIdentityFromModel(model);
114
+ const costTap = reportsProviderCost(model) ? providerReportedCostTap(args.fetch) : undefined;
115
+ const safeguards = forwardsAnthropicSafeguards(args.request, model)
116
+ ? safeguardResultsTap(costTap?.fetch ?? args.fetch)
117
+ : undefined;
118
+ const events = pi.streamSimple(model, buildContext(args.request, source), buildOptions(args.request, args.auth ?? args.apiKey, args.model, model, pi, args.sessionId, args.selectedReasoningEffort, undefined, args.signal, safeguards?.fetch ?? costTap?.fetch ?? args.fetch));
119
+ const withCacheWrites = eventsWithSubscriptionCacheWrites(safeguards ? eventsWithSafeguardResults(events, safeguards) : events, subscriptionUsageContext(args.model, model, source));
120
+ return {
121
+ events: costTap ? eventsWithProviderReportedCost(withCacheWrites, costTap) : withCacheWrites,
122
+ source,
123
+ };
124
+ }
125
+ function modelAtEndpoint(model, endpoint) {
126
+ if (endpoint === undefined)
127
+ return model;
128
+ return {
129
+ ...model,
130
+ ...(endpoint.baseUrl ? { baseUrl: endpoint.baseUrl } : {}),
131
+ ...(endpoint.headers
132
+ ? { headers: { ...(isRecord(model.headers) ? model.headers : {}), ...endpoint.headers } }
133
+ : {}),
134
+ };
135
+ }
136
+ export function modelContextWindow(modelId) {
137
+ const provider = providerForModel(modelId);
138
+ const nativeId = nativeModelId(modelId);
139
+ const registered = resolveModelMetadata(pi, provider, modelId, nativeId);
140
+ return contextWindowForModel(modelId, registered);
141
+ }
142
+ /**
143
+ * Pi's catalog price for a canonical model served by `provider`, in USD per
144
+ * million tokens. Null when Pi does not list the model.
145
+ */
146
+ export function registeredModelPrice(modelId, provider = providerForModel(modelId)) {
147
+ const registered = resolveModelMetadata(pi, provider, modelId, nativeModelId(modelId));
148
+ const cost = registered?.cost;
149
+ if (!isRecord(cost) || typeof cost.input !== "number" || typeof cost.output !== "number") {
150
+ return null;
151
+ }
152
+ return {
153
+ input: cost.input,
154
+ output: cost.output,
155
+ cached_input: typeof cost.cacheRead === "number" ? cost.cacheRead : null,
156
+ cache_write: typeof cost.cacheWrite === "number" ? cost.cacheWrite : null,
157
+ };
158
+ }
159
+ export function modelMetadataLookup(configs) {
160
+ return (modelId, pricing) => {
161
+ const model = buildModel(pi, modelId, pricing, configs?.[modelId]);
162
+ return {
163
+ model,
164
+ supportedThinkingLevels: supportedThinkingLevels(model, pi),
165
+ };
166
+ };
167
+ }
168
+ export function buildModel(pi, modelId, pricing, config) {
169
+ const provider = config?.provider ?? providerForModel(modelId);
170
+ const nativeId = config?.native_model_id ?? nativeModelId(modelId);
171
+ const registered = resolveModelMetadata(pi, provider, modelId, nativeId);
172
+ const id = registered?.id ?? (provider === "anthropic" ? nativeId.replace(/(?<=\d)\.(?=\d)/g, "-") : nativeId);
173
+ const price = executionPricing(pricing, config);
174
+ const configuredThinkingLevels = config?.supported_thinking_levels;
175
+ // A null catalog value delegates to Pi's registry when it knows the model.
176
+ // Custom OpenAI-compatible models are not necessarily registered, so keep
177
+ // the public effort set permissive and let the provider validate the wire
178
+ // value instead of silently treating an unknown model as off-only. A
179
+ // built-in provider with its own thinking levels (Meta) serves ids newer
180
+ // than the registry the same way, with only its key configured.
181
+ const providerThinking = providerThinkingLevelOverrides(provider);
182
+ const fallbackThinkingLevels = (config !== undefined || providerThinking !== undefined) &&
183
+ configuredThinkingLevels == null &&
184
+ registered === undefined
185
+ ? REASONING_EFFORTS
186
+ : undefined;
187
+ const effectiveThinkingLevels = Array.isArray(configuredThinkingLevels)
188
+ ? configuredThinkingLevels
189
+ : fallbackThinkingLevels;
190
+ const configuredThinking = effectiveThinkingLevels
191
+ ? thinkingMetadata(effectiveThinkingLevels, registered ?? { thinkingLevelMap: providerThinking })
192
+ : null;
193
+ const defaultThinkingLevel = config?.default_thinking_level ?? undefined;
194
+ const api = config?.api ?? registered?.api ?? defaultApi(provider);
195
+ const baseUrl = config?.base_url ?? registered?.baseUrl ?? defaultBaseUrl(provider);
196
+ if (!baseUrl) {
197
+ throw new HttpError(502, `No base URL is configured for provider ${provider}.`, "provider_configuration_error");
198
+ }
199
+ const contextWindow = contextWindowForModel(modelId, registered, config) ?? 128000;
200
+ const compat = responsesToolContract(api, runtimeCompat(provider, registered, modelId));
201
+ return {
202
+ ...(registered ?? {}),
203
+ ...(compat !== undefined ? { compat } : {}),
204
+ id,
205
+ name: registered?.name ?? id,
206
+ provider,
207
+ api,
208
+ baseUrl,
209
+ reasoning: configuredThinking?.reasoning ?? registered?.reasoning ?? false,
210
+ input: registered?.input ?? inputModalitiesOverride(modelId) ?? ["text"],
211
+ cost: price ?? registered?.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
212
+ contextWindow,
213
+ maxTokens: config?.max_output_tokens ?? registered?.maxTokens ?? contextWindow,
214
+ ...(defaultThinkingLevel !== undefined ? { defaultThinkingLevel } : {}),
215
+ ...(configuredThinking ? { thinkingLevelMap: configuredThinking.thinkingLevelMap } : {}),
216
+ ...(effectiveThinkingLevels
217
+ ? { supportedThinkingLevels: [...effectiveThinkingLevels] }
218
+ : {}),
219
+ fastMode: config?.fast_mode ?? false,
220
+ zeroDataRetention: config?.zdr ?? false,
221
+ };
222
+ }
223
+ function thinkingMetadata(supportedLevels, registered) {
224
+ const supported = new Set(supportedLevels);
225
+ const registeredMap = isRecord(registered?.thinkingLevelMap)
226
+ ? registered.thinkingLevelMap
227
+ : {};
228
+ const thinkingLevelMap = {};
229
+ for (const level of REASONING_EFFORTS) {
230
+ if (!supported.has(level)) {
231
+ thinkingLevelMap[level] = null;
232
+ continue;
233
+ }
234
+ const registeredValue = registeredMap[level];
235
+ if (typeof registeredValue === "string") {
236
+ thinkingLevelMap[level] = registeredValue;
237
+ }
238
+ else if (level === "xhigh" || level === "max") {
239
+ // Pi requires extended levels to be explicitly mapped. The catalog
240
+ // levels use the provider-native OpenAI spelling.
241
+ thinkingLevelMap[level] = level;
242
+ }
243
+ }
244
+ return {
245
+ reasoning: supportedLevels.some((level) => level !== "off"),
246
+ thinkingLevelMap,
247
+ };
248
+ }
249
+ function resolveModelMetadata(pi, provider, modelId, nativeId) {
250
+ const registry = registryProvider(provider, modelId);
251
+ return withThinkingLevelOverrides(provider, pi.getModel?.(registry, nativeId) ?? pi.getModel?.(registry, modelId));
252
+ }
253
+ // Dari's "azure" provider has no Pi registry of its own. One Foundry resource
254
+ // serves OpenAI deployments through Pi's Azure OpenAI Responses registry and
255
+ // Claude deployments through the Anthropic registry, whose model metadata
256
+ // (input modalities, adaptive-thinking compat, thinking level maps) is the
257
+ // same on Foundry; the resolved config still owns api, base URL, and cost.
258
+ function registryProvider(provider, modelId) {
259
+ if (provider !== "azure")
260
+ return provider;
261
+ return providerForModel(modelId) === "anthropic" ? "anthropic" : "azure-openai-responses";
262
+ }
263
+ function contextWindowForModel(modelId, registered, config) {
264
+ const registeredWindow = typeof registered?.contextWindow === "number" ? registered.contextWindow : null;
265
+ const configWindow = effectiveContextWindow(config, config?.allow_long_context === true);
266
+ const override = contextWindowOverride(modelId);
267
+ const configured = configWindow ?? registeredWindow;
268
+ if (configWindow !== null || override === undefined)
269
+ return configured;
270
+ return Math.max(configured ?? 0, override);
271
+ }
272
+ // Pi 1.4 sends Responses tool definitions as conversation items, not the request's
273
+ // `tools` field, when a model supports mid-conversation system messages. The gateway
274
+ // keeps tools at the request level until that native tool-change path is validated.
275
+ const RESPONSES_APIS = new Set(["openai-responses", "azure-openai-responses", "openai-codex-responses"]);
276
+ function responsesToolContract(api, compat) {
277
+ if (typeof api !== "string" || !RESPONSES_APIS.has(api))
278
+ return compat;
279
+ if (!isRecord(compat) || compat.supportsMidConvoSystemMessages !== true)
280
+ return compat;
281
+ return { ...compat, supportsMidConvoSystemMessages: false };
282
+ }
283
+ function modelPricingFromConfig(config) {
284
+ const configuredCost = config?.fast_mode && config.provider === "anthropic" && config.fast_mode_cost
285
+ ? config.fast_mode_cost
286
+ : config?.cost;
287
+ if (!configuredCost || typeof configuredCost.input !== "number" || typeof configuredCost.output !== "number")
288
+ return null;
289
+ return {
290
+ input: configuredCost.input,
291
+ output: configuredCost.output,
292
+ cacheRead: configuredCost.cached_input ?? configuredCost.input,
293
+ cacheWrite: configuredCost.cache_write ?? configuredCost.input,
294
+ ...(configuredCost.tiers?.length ? { tiers: configuredCost.tiers.map((tier) => ({
295
+ inputTokensAbove: tier.input_tokens_above,
296
+ input: tier.input,
297
+ output: tier.output,
298
+ cacheRead: tier.cached_input ?? tier.input,
299
+ cacheWrite: tier.cache_write ?? tier.input,
300
+ })) } : {}),
301
+ };
302
+ }
303
+ function executionPricing(pricing, config) {
304
+ if (config?.fast_mode &&
305
+ config.provider === "openai" &&
306
+ config.api === "openai-responses") {
307
+ // The platform publishes fast prices for routing decisions and the UI,
308
+ // while Pi multiplies the standard rates using the service tier actually
309
+ // returned by OpenAI. Feeding Pi premium rates here would charge twice.
310
+ return modelPricingFromConfig({ ...config, fast_mode: false });
311
+ }
312
+ return pricing ?? modelPricingFromConfig(config);
313
+ }
314
+ function defaultApi(provider) {
315
+ // Meta recommends Responses: its Chat Completions drops reasoning between turns.
316
+ if (provider === "openai" || provider === "xai" || provider === "meta")
317
+ return "openai-responses";
318
+ if (provider === "anthropic")
319
+ return "anthropic-messages";
320
+ if (provider === "amazon-bedrock")
321
+ return BEDROCK_API;
322
+ if (provider === "azure")
323
+ return "azure-openai-responses";
324
+ return "openai-completions";
325
+ }
@@ -0,0 +1,7 @@
1
+ import type { InteractionRequest, ProviderIdentity } from "../interaction/types.js";
2
+ export type PiContext = Record<string, unknown> & {
3
+ messages: Array<Record<string, unknown>>;
4
+ systemPrompt?: string;
5
+ tools?: Array<Record<string, unknown>>;
6
+ };
7
+ export declare function buildContext(request: InteractionRequest, target: ProviderIdentity): PiContext;