stitchkit 0.133.2 → 0.134.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 (616) hide show
  1. package/CHANGELOG.md +115 -0
  2. package/README.md +14 -11
  3. package/dist/agent-executor/acp/v1-connect.d.ts +3 -0
  4. package/dist/agent-executor/acp/v1-connect.d.ts.map +1 -1
  5. package/dist/agent-executor/acp/v1-map.d.ts +13 -3
  6. package/dist/agent-executor/acp/v1-map.d.ts.map +1 -1
  7. package/dist/agent-executor/acp/v1-protocol.d.ts +59 -0
  8. package/dist/agent-executor/acp/v1-protocol.d.ts.map +1 -1
  9. package/dist/agent-executor/codex/connect.d.ts +2 -0
  10. package/dist/agent-executor/codex/connect.d.ts.map +1 -1
  11. package/dist/agent-executor/codex/driver.d.ts +3 -0
  12. package/dist/agent-executor/codex/driver.d.ts.map +1 -1
  13. package/dist/agent-executor/contract.d.ts +42 -0
  14. package/dist/agent-executor/contract.d.ts.map +1 -1
  15. package/dist/agent-executor/engine-process.d.ts +20 -0
  16. package/dist/agent-executor/engine-process.d.ts.map +1 -1
  17. package/dist/agent-executor/executor-contract.d.ts +42 -0
  18. package/dist/agent-executor/executor-contract.d.ts.map +1 -1
  19. package/dist/agent-executor/handoff-schemas.d.ts +30 -0
  20. package/dist/agent-executor/handoff-schemas.d.ts.map +1 -1
  21. package/dist/agent-executor/projection.d.ts.map +1 -1
  22. package/dist/agent-executor/session-primitives.d.ts +4 -1
  23. package/dist/agent-executor/session-primitives.d.ts.map +1 -1
  24. package/dist/agent-executor/vocabulary.d.ts +100 -0
  25. package/dist/agent-executor/vocabulary.d.ts.map +1 -1
  26. package/dist/agent-parts/model.d.ts +1559 -0
  27. package/dist/agent-parts/model.d.ts.map +1 -0
  28. package/dist/agent-parts/parts.d.ts +21 -0
  29. package/dist/agent-parts/parts.d.ts.map +1 -1
  30. package/dist/agent-runtime/ai-sdk-convert.d.ts +15 -0
  31. package/dist/agent-runtime/ai-sdk-convert.d.ts.map +1 -0
  32. package/dist/agent-runtime/ai-sdk.d.ts +31 -0
  33. package/dist/agent-runtime/ai-sdk.d.ts.map +1 -0
  34. package/dist/agent-runtime/anthropic-media.d.ts +11 -0
  35. package/dist/agent-runtime/anthropic-media.d.ts.map +1 -0
  36. package/dist/agent-runtime/anthropic-messages.d.ts +24 -0
  37. package/dist/agent-runtime/anthropic-messages.d.ts.map +1 -0
  38. package/dist/agent-runtime/anthropic-options.d.ts +79 -0
  39. package/dist/agent-runtime/anthropic-options.d.ts.map +1 -0
  40. package/dist/agent-runtime/anthropic-response.d.ts +19 -0
  41. package/dist/agent-runtime/anthropic-response.d.ts.map +1 -0
  42. package/dist/agent-runtime/anthropic-sse.d.ts +4 -0
  43. package/dist/agent-runtime/anthropic-sse.d.ts.map +1 -0
  44. package/dist/agent-runtime/anthropic.d.ts +27 -0
  45. package/dist/agent-runtime/anthropic.d.ts.map +1 -0
  46. package/dist/agent-runtime/branch-observations.d.ts +25 -25
  47. package/dist/agent-runtime/branch-provider.d.ts +86 -0
  48. package/dist/agent-runtime/branch-provider.d.ts.map +1 -1
  49. package/dist/agent-runtime/branch-replay.d.ts +30 -0
  50. package/dist/agent-runtime/branch-replay.d.ts.map +1 -1
  51. package/dist/agent-runtime/browser-control-contract.d.ts +204 -0
  52. package/dist/agent-runtime/browser-control-contract.d.ts.map +1 -1
  53. package/dist/agent-runtime/browser-controller-view.d.ts +44 -0
  54. package/dist/agent-runtime/browser-controller-view.d.ts.map +1 -0
  55. package/dist/agent-runtime/browser-controller.d.ts +12 -8
  56. package/dist/agent-runtime/browser-controller.d.ts.map +1 -1
  57. package/dist/agent-runtime/child-budget-policy.d.ts +2 -3
  58. package/dist/agent-runtime/child-budget-policy.d.ts.map +1 -1
  59. package/dist/agent-runtime/compaction.d.ts +27 -10
  60. package/dist/agent-runtime/compaction.d.ts.map +1 -1
  61. package/dist/agent-runtime/control-interaction-schema.d.ts +157 -0
  62. package/dist/agent-runtime/control-interaction-schema.d.ts.map +1 -0
  63. package/dist/agent-runtime/control-schema.d.ts +183 -27
  64. package/dist/agent-runtime/control-schema.d.ts.map +1 -1
  65. package/dist/agent-runtime/control-view.d.ts +64 -0
  66. package/dist/agent-runtime/control-view.d.ts.map +1 -0
  67. package/dist/agent-runtime/conversations.d.ts +10 -0
  68. package/dist/agent-runtime/conversations.d.ts.map +1 -1
  69. package/dist/agent-runtime/deferred-tool-selection.d.ts +2 -2
  70. package/dist/agent-runtime/deferred-tool-selection.d.ts.map +1 -1
  71. package/dist/agent-runtime/deferred-tool-types.d.ts +2 -2
  72. package/dist/agent-runtime/deferred-tool-types.d.ts.map +1 -1
  73. package/dist/agent-runtime/deferred-tools-internal.d.ts +8 -11
  74. package/dist/agent-runtime/deferred-tools-internal.d.ts.map +1 -1
  75. package/dist/agent-runtime/deferred-tools.d.ts.map +1 -1
  76. package/dist/agent-runtime/event-schema.d.ts +80 -0
  77. package/dist/agent-runtime/event-schema.d.ts.map +1 -1
  78. package/dist/agent-runtime/harness-contract.d.ts +2 -2
  79. package/dist/agent-runtime/harness-contract.d.ts.map +1 -1
  80. package/dist/agent-runtime/harness-control.d.ts.map +1 -1
  81. package/dist/agent-runtime/harness-realtime.d.ts.map +1 -1
  82. package/dist/agent-runtime/harness.d.ts +2 -2
  83. package/dist/agent-runtime/harness.d.ts.map +1 -1
  84. package/dist/agent-runtime/history.d.ts +25 -16
  85. package/dist/agent-runtime/history.d.ts.map +1 -1
  86. package/dist/agent-runtime/invocation-attempt.d.ts +7 -3
  87. package/dist/agent-runtime/invocation-attempt.d.ts.map +1 -1
  88. package/dist/agent-runtime/invocation-payload.d.ts +13 -7
  89. package/dist/agent-runtime/invocation-payload.d.ts.map +1 -1
  90. package/dist/agent-runtime/invocation-stream.d.ts +20 -0
  91. package/dist/agent-runtime/invocation-stream.d.ts.map +1 -0
  92. package/dist/agent-runtime/invocations.d.ts +2 -2
  93. package/dist/agent-runtime/invocations.d.ts.map +1 -1
  94. package/dist/agent-runtime/models-driver-step.d.ts +212 -0
  95. package/dist/agent-runtime/models-driver-step.d.ts.map +1 -0
  96. package/dist/agent-runtime/models-driver-telemetry.d.ts +42 -0
  97. package/dist/agent-runtime/models-driver-telemetry.d.ts.map +1 -0
  98. package/dist/agent-runtime/models-driver.d.ts +235 -0
  99. package/dist/agent-runtime/models-driver.d.ts.map +1 -0
  100. package/dist/agent-runtime/models.d.ts +3 -26
  101. package/dist/agent-runtime/models.d.ts.map +1 -1
  102. package/dist/agent-runtime/observability.d.ts +64 -0
  103. package/dist/agent-runtime/observability.d.ts.map +1 -1
  104. package/dist/agent-runtime/openai-messages.d.ts +11 -0
  105. package/dist/agent-runtime/openai-messages.d.ts.map +1 -0
  106. package/dist/agent-runtime/openai-options.d.ts +91 -0
  107. package/dist/agent-runtime/openai-options.d.ts.map +1 -0
  108. package/dist/agent-runtime/openai-response.d.ts +77 -0
  109. package/dist/agent-runtime/openai-response.d.ts.map +1 -0
  110. package/dist/agent-runtime/openai-sse-state.d.ts +54 -0
  111. package/dist/agent-runtime/openai-sse-state.d.ts.map +1 -0
  112. package/dist/agent-runtime/openai-sse.d.ts +4 -0
  113. package/dist/agent-runtime/openai-sse.d.ts.map +1 -0
  114. package/dist/agent-runtime/openai.d.ts +31 -0
  115. package/dist/agent-runtime/openai.d.ts.map +1 -0
  116. package/dist/agent-runtime/openrouter-admission.d.ts +3 -0
  117. package/dist/agent-runtime/openrouter-admission.d.ts.map +1 -1
  118. package/dist/agent-runtime/openrouter-benchmarks.d.ts +17 -0
  119. package/dist/agent-runtime/openrouter-benchmarks.d.ts.map +1 -0
  120. package/dist/agent-runtime/openrouter-catalog-cache.d.ts +70 -0
  121. package/dist/agent-runtime/openrouter-catalog-cache.d.ts.map +1 -0
  122. package/dist/agent-runtime/openrouter-catalog-query.d.ts +57 -0
  123. package/dist/agent-runtime/openrouter-catalog-query.d.ts.map +1 -0
  124. package/dist/agent-runtime/openrouter-catalog.d.ts +189 -14
  125. package/dist/agent-runtime/openrouter-catalog.d.ts.map +1 -1
  126. package/dist/agent-runtime/openrouter-models.d.ts +168 -31
  127. package/dist/agent-runtime/openrouter-models.d.ts.map +1 -1
  128. package/dist/agent-runtime/openrouter-options.d.ts +45 -15
  129. package/dist/agent-runtime/openrouter-options.d.ts.map +1 -1
  130. package/dist/agent-runtime/openrouter-response.d.ts +22 -0
  131. package/dist/agent-runtime/openrouter-response.d.ts.map +1 -0
  132. package/dist/agent-runtime/openrouter.d.ts +21 -24
  133. package/dist/agent-runtime/openrouter.d.ts.map +1 -1
  134. package/dist/agent-runtime/owned-provider-stream.d.ts +4 -4
  135. package/dist/agent-runtime/owned-provider-stream.d.ts.map +1 -1
  136. package/dist/agent-runtime/prompt.d.ts +3 -3
  137. package/dist/agent-runtime/prompt.d.ts.map +1 -1
  138. package/dist/agent-runtime/protocol.d.ts +28 -3
  139. package/dist/agent-runtime/protocol.d.ts.map +1 -1
  140. package/dist/agent-runtime/provider-cache.d.ts +30 -0
  141. package/dist/agent-runtime/provider-cache.d.ts.map +1 -0
  142. package/dist/agent-runtime/provider-content.d.ts +147 -0
  143. package/dist/agent-runtime/provider-content.d.ts.map +1 -1
  144. package/dist/agent-runtime/provider-failure.d.ts +51 -26
  145. package/dist/agent-runtime/provider-failure.d.ts.map +1 -1
  146. package/dist/agent-runtime/provider-history.d.ts +13 -0
  147. package/dist/agent-runtime/provider-history.d.ts.map +1 -0
  148. package/dist/agent-runtime/provider-http.d.ts +68 -0
  149. package/dist/agent-runtime/provider-http.d.ts.map +1 -0
  150. package/dist/agent-runtime/provider-journal.d.ts +149 -0
  151. package/dist/agent-runtime/provider-journal.d.ts.map +1 -1
  152. package/dist/agent-runtime/provider-json-schema-strip.d.ts +8 -0
  153. package/dist/agent-runtime/provider-json-schema-strip.d.ts.map +1 -0
  154. package/dist/agent-runtime/provider-json-schema.d.ts +28 -0
  155. package/dist/agent-runtime/provider-json-schema.d.ts.map +1 -0
  156. package/dist/agent-runtime/provider-media.d.ts +49 -0
  157. package/dist/agent-runtime/provider-media.d.ts.map +1 -0
  158. package/dist/agent-runtime/provider-openai-chat-helpers.d.ts +27 -0
  159. package/dist/agent-runtime/provider-openai-chat-helpers.d.ts.map +1 -0
  160. package/dist/agent-runtime/provider-openai-chat-sse.d.ts +25 -0
  161. package/dist/agent-runtime/provider-openai-chat-sse.d.ts.map +1 -0
  162. package/dist/agent-runtime/provider-openai-chat.d.ts +27 -0
  163. package/dist/agent-runtime/provider-openai-chat.d.ts.map +1 -0
  164. package/dist/agent-runtime/provider-origin.d.ts +3 -12
  165. package/dist/agent-runtime/provider-origin.d.ts.map +1 -1
  166. package/dist/agent-runtime/provider-sse.d.ts +30 -0
  167. package/dist/agent-runtime/provider-sse.d.ts.map +1 -0
  168. package/dist/agent-runtime/provider-stream-parts.d.ts +5 -0
  169. package/dist/agent-runtime/provider-stream-parts.d.ts.map +1 -0
  170. package/dist/agent-runtime/provider-stream-segments.d.ts +63 -0
  171. package/dist/agent-runtime/provider-stream-segments.d.ts.map +1 -0
  172. package/dist/agent-runtime/provider-usage.d.ts +24 -0
  173. package/dist/agent-runtime/provider-usage.d.ts.map +1 -0
  174. package/dist/agent-runtime/retry-policy.d.ts +19 -3
  175. package/dist/agent-runtime/retry-policy.d.ts.map +1 -1
  176. package/dist/agent-runtime/run-acquisition.d.ts +4 -4
  177. package/dist/agent-runtime/run-acquisition.d.ts.map +1 -1
  178. package/dist/agent-runtime/run-approval.d.ts +82 -0
  179. package/dist/agent-runtime/run-approval.d.ts.map +1 -0
  180. package/dist/agent-runtime/run-complete-retry.d.ts +24 -0
  181. package/dist/agent-runtime/run-complete-retry.d.ts.map +1 -0
  182. package/dist/agent-runtime/run-complete-stream.d.ts +9 -0
  183. package/dist/agent-runtime/run-complete-stream.d.ts.map +1 -0
  184. package/dist/agent-runtime/run-complete-tools.d.ts +21 -0
  185. package/dist/agent-runtime/run-complete-tools.d.ts.map +1 -0
  186. package/dist/agent-runtime/run-complete-types.d.ts +168 -0
  187. package/dist/agent-runtime/run-complete-types.d.ts.map +1 -0
  188. package/dist/agent-runtime/run-complete.d.ts +25 -0
  189. package/dist/agent-runtime/run-complete.d.ts.map +1 -0
  190. package/dist/agent-runtime/run-driver-compose.d.ts +21 -0
  191. package/dist/agent-runtime/run-driver-compose.d.ts.map +1 -0
  192. package/dist/agent-runtime/run-execution-state.d.ts +23 -10
  193. package/dist/agent-runtime/run-execution-state.d.ts.map +1 -1
  194. package/dist/agent-runtime/run-execution.d.ts +2 -2
  195. package/dist/agent-runtime/run-execution.d.ts.map +1 -1
  196. package/dist/agent-runtime/run-failure.d.ts +2 -2
  197. package/dist/agent-runtime/run-failure.d.ts.map +1 -1
  198. package/dist/agent-runtime/run-history-parts.d.ts +14 -0
  199. package/dist/agent-runtime/run-history-parts.d.ts.map +1 -0
  200. package/dist/agent-runtime/run-loop-events.d.ts +126 -0
  201. package/dist/agent-runtime/run-loop-events.d.ts.map +1 -0
  202. package/dist/agent-runtime/run-model-attempts.d.ts +2 -2
  203. package/dist/agent-runtime/run-model-attempts.d.ts.map +1 -1
  204. package/dist/agent-runtime/run-model-messages.d.ts +9 -0
  205. package/dist/agent-runtime/run-model-messages.d.ts.map +1 -0
  206. package/dist/agent-runtime/run-model-step.d.ts +43 -0
  207. package/dist/agent-runtime/run-model-step.d.ts.map +1 -0
  208. package/dist/agent-runtime/run-model-stream.d.ts +27 -0
  209. package/dist/agent-runtime/run-model-stream.d.ts.map +1 -0
  210. package/dist/agent-runtime/run-operation-lifecycle.d.ts +8 -4
  211. package/dist/agent-runtime/run-operation-lifecycle.d.ts.map +1 -1
  212. package/dist/agent-runtime/run-prompt-history.d.ts +8 -13
  213. package/dist/agent-runtime/run-prompt-history.d.ts.map +1 -1
  214. package/dist/agent-runtime/run-provider-ledger.d.ts +7 -4
  215. package/dist/agent-runtime/run-provider-ledger.d.ts.map +1 -1
  216. package/dist/agent-runtime/run-step-barriers.d.ts +58 -16
  217. package/dist/agent-runtime/run-step-barriers.d.ts.map +1 -1
  218. package/dist/agent-runtime/run-step-helpers.d.ts +49 -0
  219. package/dist/agent-runtime/run-step-helpers.d.ts.map +1 -0
  220. package/dist/agent-runtime/run-step-prepare.d.ts +34 -0
  221. package/dist/agent-runtime/run-step-prepare.d.ts.map +1 -0
  222. package/dist/agent-runtime/run-step-spend.d.ts +85 -10
  223. package/dist/agent-runtime/run-step-spend.d.ts.map +1 -1
  224. package/dist/agent-runtime/run-stream-parts.d.ts +13 -9
  225. package/dist/agent-runtime/run-stream-parts.d.ts.map +1 -1
  226. package/dist/agent-runtime/run-stream-recorded.d.ts +8 -0
  227. package/dist/agent-runtime/run-stream-recorded.d.ts.map +1 -0
  228. package/dist/agent-runtime/run-stream-step-text.d.ts +29 -0
  229. package/dist/agent-runtime/run-stream-step-text.d.ts.map +1 -0
  230. package/dist/agent-runtime/run-terminal.d.ts +3 -3
  231. package/dist/agent-runtime/run-terminal.d.ts.map +1 -1
  232. package/dist/agent-runtime/run-tool-approval-flow.d.ts +36 -0
  233. package/dist/agent-runtime/run-tool-approval-flow.d.ts.map +1 -0
  234. package/dist/agent-runtime/run-tool-approval-records.d.ts +24 -0
  235. package/dist/agent-runtime/run-tool-approval-records.d.ts.map +1 -0
  236. package/dist/agent-runtime/run-tool-call.d.ts +73 -0
  237. package/dist/agent-runtime/run-tool-call.d.ts.map +1 -0
  238. package/dist/agent-runtime/run-tool-errors.d.ts +29 -0
  239. package/dist/agent-runtime/run-tool-errors.d.ts.map +1 -0
  240. package/dist/agent-runtime/run-tool-executor.d.ts +34 -0
  241. package/dist/agent-runtime/run-tool-executor.d.ts.map +1 -0
  242. package/dist/agent-runtime/run-tool-progress.d.ts +2 -2
  243. package/dist/agent-runtime/run-tool-progress.d.ts.map +1 -1
  244. package/dist/agent-runtime/run-turn-preparation.d.ts +7 -6
  245. package/dist/agent-runtime/run-turn-preparation.d.ts.map +1 -1
  246. package/dist/agent-runtime/run-unacquired.d.ts +2 -2
  247. package/dist/agent-runtime/run-unacquired.d.ts.map +1 -1
  248. package/dist/agent-runtime/runtime-control.d.ts +4 -4
  249. package/dist/agent-runtime/runtime-control.d.ts.map +1 -1
  250. package/dist/agent-runtime/runtime-internals.d.ts +35 -44
  251. package/dist/agent-runtime/runtime-internals.d.ts.map +1 -1
  252. package/dist/agent-runtime/runtime-recovery.d.ts +3 -3
  253. package/dist/agent-runtime/runtime-recovery.d.ts.map +1 -1
  254. package/dist/agent-runtime/runtime-state.d.ts +3 -3
  255. package/dist/agent-runtime/runtime-state.d.ts.map +1 -1
  256. package/dist/agent-runtime/runtime-submit.d.ts +2 -2
  257. package/dist/agent-runtime/runtime-submit.d.ts.map +1 -1
  258. package/dist/agent-runtime/runtime.d.ts +51 -16
  259. package/dist/agent-runtime/runtime.d.ts.map +1 -1
  260. package/dist/agent-runtime/sandbox-process-linux.d.ts +8 -0
  261. package/dist/agent-runtime/sandbox-process-linux.d.ts.map +1 -0
  262. package/dist/agent-runtime/sandbox-process-macos.d.ts +21 -0
  263. package/dist/agent-runtime/sandbox-process-macos.d.ts.map +1 -0
  264. package/dist/agent-runtime/sandbox-process-spec.d.ts +114 -0
  265. package/dist/agent-runtime/sandbox-process-spec.d.ts.map +1 -0
  266. package/dist/agent-runtime/sandbox-process-wrapper.d.ts +19 -0
  267. package/dist/agent-runtime/sandbox-process-wrapper.d.ts.map +1 -0
  268. package/dist/agent-runtime/schemas-message.d.ts +10 -0
  269. package/dist/agent-runtime/schemas-message.d.ts.map +1 -1
  270. package/dist/agent-runtime/schemas-provider.d.ts +2 -0
  271. package/dist/agent-runtime/schemas-provider.d.ts.map +1 -1
  272. package/dist/agent-runtime/schemas-run.d.ts +10 -0
  273. package/dist/agent-runtime/schemas-run.d.ts.map +1 -1
  274. package/dist/agent-runtime/schemas-usage-state.d.ts +6 -0
  275. package/dist/agent-runtime/schemas-usage-state.d.ts.map +1 -0
  276. package/dist/agent-runtime/schemas-usage.d.ts +8 -0
  277. package/dist/agent-runtime/schemas-usage.d.ts.map +1 -1
  278. package/dist/agent-runtime/sql-rows.d.ts +20 -10
  279. package/dist/agent-runtime/sql-rows.d.ts.map +1 -1
  280. package/dist/agent-runtime/store-conformance-lifecycle.d.ts +40 -20
  281. package/dist/agent-runtime/store-conformance-lifecycle.d.ts.map +1 -1
  282. package/dist/agent-runtime/store-conformance-support.d.ts +20 -10
  283. package/dist/agent-runtime/store-conformance-support.d.ts.map +1 -1
  284. package/dist/agent-runtime/store-driver-contract.d.ts +70 -0
  285. package/dist/agent-runtime/store-driver-contract.d.ts.map +1 -1
  286. package/dist/agent-runtime/store-events.d.ts +120 -0
  287. package/dist/agent-runtime/store-events.d.ts.map +1 -1
  288. package/dist/agent-runtime/store.d.ts +140 -0
  289. package/dist/agent-runtime/store.d.ts.map +1 -1
  290. package/dist/agent-runtime/terminal-commit.d.ts +10 -0
  291. package/dist/agent-runtime/terminal-commit.d.ts.map +1 -1
  292. package/dist/agent-runtime/testing.d.ts +20 -0
  293. package/dist/agent-runtime/testing.d.ts.map +1 -1
  294. package/dist/browser/client-call-arguments.d.ts.map +1 -1
  295. package/dist/browser/client-executors.d.ts.map +1 -1
  296. package/dist/browser/client-url.d.ts.map +1 -1
  297. package/dist/browser/client.d.ts +6 -0
  298. package/dist/browser/client.d.ts.map +1 -1
  299. package/dist/browser/device-presence-client.d.ts +42 -0
  300. package/dist/browser/device-presence-client.d.ts.map +1 -0
  301. package/dist/browser/device-presence-ops.d.ts +47 -0
  302. package/dist/browser/device-presence-ops.d.ts.map +1 -0
  303. package/dist/browser/device-presence-relay-adapter.d.ts +37 -0
  304. package/dist/browser/device-presence-relay-adapter.d.ts.map +1 -0
  305. package/dist/browser/http-fetch.d.ts +7 -2
  306. package/dist/browser/http-fetch.d.ts.map +1 -1
  307. package/dist/browser/http-helpers.d.ts +15 -0
  308. package/dist/browser/http-helpers.d.ts.map +1 -0
  309. package/dist/browser/http.d.ts +8 -0
  310. package/dist/browser/http.d.ts.map +1 -1
  311. package/dist/contract/client-types.d.ts +8 -0
  312. package/dist/contract/client-types.d.ts.map +1 -1
  313. package/dist/contract/runtime-context.d.ts +13 -1
  314. package/dist/contract/runtime-context.d.ts.map +1 -1
  315. package/dist/device-relay/agent-handlers.d.ts +45 -0
  316. package/dist/device-relay/agent-handlers.d.ts.map +1 -0
  317. package/dist/device-relay/agent.d.ts +80 -0
  318. package/dist/device-relay/agent.d.ts.map +1 -0
  319. package/dist/device-relay/broker.d.ts +49 -0
  320. package/dist/device-relay/broker.d.ts.map +1 -0
  321. package/dist/device-relay/contract.d.ts +614 -0
  322. package/dist/device-relay/contract.d.ts.map +1 -0
  323. package/dist/device-relay/encoding.d.ts +30 -0
  324. package/dist/device-relay/encoding.d.ts.map +1 -0
  325. package/dist/device-relay/hub-connection.d.ts +43 -0
  326. package/dist/device-relay/hub-connection.d.ts.map +1 -0
  327. package/dist/device-relay/hub-dispatch.d.ts +19 -0
  328. package/dist/device-relay/hub-dispatch.d.ts.map +1 -0
  329. package/dist/device-relay/hub-handshake.d.ts +20 -0
  330. package/dist/device-relay/hub-handshake.d.ts.map +1 -0
  331. package/dist/device-relay/hub-shared-streams.d.ts +24 -0
  332. package/dist/device-relay/hub-shared-streams.d.ts.map +1 -0
  333. package/dist/device-relay/hub-streams.d.ts +57 -0
  334. package/dist/device-relay/hub-streams.d.ts.map +1 -0
  335. package/dist/device-relay/hub.d.ts +117 -0
  336. package/dist/device-relay/hub.d.ts.map +1 -0
  337. package/dist/entrypoints/agent-executor/acp.d.ts +1 -1
  338. package/dist/entrypoints/agent-executor/acp.d.ts.map +1 -1
  339. package/dist/entrypoints/agent-executor/acp.js +183 -29
  340. package/dist/entrypoints/agent-executor/claude-sdk.js +15 -15
  341. package/dist/entrypoints/agent-executor/claude.js +13 -15
  342. package/dist/entrypoints/agent-executor/codex.js +88 -48
  343. package/dist/entrypoints/agent-executor/testing/claude-sdk.js +5 -5
  344. package/dist/entrypoints/agent-executor/testing.js +31 -30
  345. package/dist/entrypoints/agent-executor.d.ts +2 -2
  346. package/dist/entrypoints/agent-executor.d.ts.map +1 -1
  347. package/dist/entrypoints/agent-executor.js +23 -17
  348. package/dist/entrypoints/agent-runtime/ai-sdk.d.ts +9 -0
  349. package/dist/entrypoints/agent-runtime/ai-sdk.d.ts.map +1 -0
  350. package/dist/entrypoints/agent-runtime/ai-sdk.js +821 -0
  351. package/dist/entrypoints/agent-runtime/anthropic.d.ts +4 -0
  352. package/dist/entrypoints/agent-runtime/anthropic.d.ts.map +1 -0
  353. package/dist/entrypoints/agent-runtime/anthropic.js +1260 -0
  354. package/dist/entrypoints/agent-runtime/browser.d.ts +4 -1
  355. package/dist/entrypoints/agent-runtime/browser.d.ts.map +1 -1
  356. package/dist/entrypoints/agent-runtime/browser.js +449 -50
  357. package/dist/entrypoints/agent-runtime/coding-tools.js +3 -3
  358. package/dist/entrypoints/agent-runtime/harness.js +24 -19
  359. package/dist/entrypoints/agent-runtime/openai.d.ts +4 -0
  360. package/dist/entrypoints/agent-runtime/openai.d.ts.map +1 -0
  361. package/dist/entrypoints/agent-runtime/openai.js +1246 -0
  362. package/dist/entrypoints/agent-runtime/openrouter.d.ts +5 -2
  363. package/dist/entrypoints/agent-runtime/openrouter.d.ts.map +1 -1
  364. package/dist/entrypoints/agent-runtime/openrouter.js +2355 -371
  365. package/dist/entrypoints/agent-runtime/postgres.js +12 -10
  366. package/dist/entrypoints/agent-runtime/realtime.js +11 -10
  367. package/dist/entrypoints/agent-runtime/sandbox.d.ts +3 -0
  368. package/dist/entrypoints/agent-runtime/sandbox.d.ts.map +1 -1
  369. package/dist/entrypoints/agent-runtime/sandbox.js +530 -7
  370. package/dist/entrypoints/agent-runtime/sqlite/bun.js +13 -11
  371. package/dist/entrypoints/agent-runtime/sqlite/node.js +13 -11
  372. package/dist/entrypoints/agent-runtime/testing.d.ts +1 -1
  373. package/dist/entrypoints/agent-runtime/testing.d.ts.map +1 -1
  374. package/dist/entrypoints/agent-runtime/testing.js +56 -15
  375. package/dist/entrypoints/agent-runtime.d.ts +14 -6
  376. package/dist/entrypoints/agent-runtime.d.ts.map +1 -1
  377. package/dist/entrypoints/agent-runtime.js +1412 -115
  378. package/dist/entrypoints/application/diagnostic-journal.js +6 -6
  379. package/dist/entrypoints/application/directory-inbox.js +6 -6
  380. package/dist/entrypoints/application/opentelemetry.js +3 -3
  381. package/dist/entrypoints/application.js +17 -17
  382. package/dist/entrypoints/cli/publish.js +8 -8
  383. package/dist/entrypoints/cli.js +14 -15
  384. package/dist/entrypoints/contract.d.ts +1 -0
  385. package/dist/entrypoints/contract.d.ts.map +1 -1
  386. package/dist/entrypoints/contract.js +13 -9
  387. package/dist/entrypoints/device-relay.d.ts +10 -0
  388. package/dist/entrypoints/device-relay.d.ts.map +1 -0
  389. package/dist/entrypoints/device-relay.js +2517 -0
  390. package/dist/entrypoints/files/packaging.js +3 -3
  391. package/dist/entrypoints/files.js +11 -11
  392. package/dist/entrypoints/geo.js +5 -5
  393. package/dist/entrypoints/google.js +1 -1
  394. package/dist/entrypoints/handler.js +6 -6
  395. package/dist/entrypoints/http.js +9 -9
  396. package/dist/entrypoints/index.js +21 -17
  397. package/dist/entrypoints/live.js +4 -4
  398. package/dist/entrypoints/node.js +13 -13
  399. package/dist/entrypoints/observability.js +9 -9
  400. package/dist/entrypoints/process/bootstrap.js +1 -1
  401. package/dist/entrypoints/process.js +5 -5
  402. package/dist/entrypoints/react.js +4 -4
  403. package/dist/entrypoints/realtime.d.ts +3 -0
  404. package/dist/entrypoints/realtime.d.ts.map +1 -1
  405. package/dist/entrypoints/realtime.js +413 -4
  406. package/dist/entrypoints/release.js +1 -1
  407. package/dist/entrypoints/remote.js +10 -10
  408. package/dist/entrypoints/server.d.ts +1 -0
  409. package/dist/entrypoints/server.d.ts.map +1 -1
  410. package/dist/entrypoints/server.js +546 -40
  411. package/dist/entrypoints/service-host/macos.js +10 -10
  412. package/dist/entrypoints/service-host/work.js +7 -7
  413. package/dist/entrypoints/service-host.js +15 -15
  414. package/dist/entrypoints/spend.js +3 -3
  415. package/dist/entrypoints/sync.js +4 -4
  416. package/dist/entrypoints/telegram/screens.js +6 -6
  417. package/dist/entrypoints/telegram.js +20 -20
  418. package/dist/entrypoints/testing.js +14 -14
  419. package/dist/entrypoints/tools/agent.d.ts +2 -1
  420. package/dist/entrypoints/tools/agent.d.ts.map +1 -1
  421. package/dist/entrypoints/tools/agent.js +8 -7
  422. package/dist/entrypoints/tools/connections.js +5 -5
  423. package/dist/entrypoints/tools/invoker.js +3 -4
  424. package/dist/entrypoints/tools/mcp.js +8 -9
  425. package/dist/entrypoints/tools/toolkit.js +32 -13
  426. package/dist/entrypoints/tools.d.ts +3 -1
  427. package/dist/entrypoints/tools.d.ts.map +1 -1
  428. package/dist/entrypoints/tools.js +37 -35
  429. package/dist/entrypoints/tracking/contract.js +1 -1
  430. package/dist/entrypoints/tracking/performance.js +4 -4
  431. package/dist/entrypoints/tracking.js +3 -3
  432. package/dist/{index-09f7w2qq.js → index-02kdtrcd.js} +1 -1
  433. package/dist/{index-y53cqwke.js → index-033tyx59.js} +1 -1
  434. package/dist/{index-9ft4n9j1.js → index-05b65gj4.js} +2 -2
  435. package/dist/{index-d04w81fz.js → index-0sxj3y45.js} +1 -1
  436. package/dist/{index-dm6nxkn1.js → index-18pf56dq.js} +4 -6
  437. package/dist/index-1gmgwfn3.js +439 -0
  438. package/dist/index-23wn8sgf.js +47 -0
  439. package/dist/{index-tqjamnax.js → index-27srek2g.js} +15 -13
  440. package/dist/{index-2nwvn7m2.js → index-29v1p1f4.js} +2 -2
  441. package/dist/index-36dmtzjr.js +194 -0
  442. package/dist/{index-ycr4pk0z.js → index-42n9jycn.js} +3 -3
  443. package/dist/{index-ke02kxsr.js → index-43k180c2.js} +1 -1
  444. package/dist/{index-pbekeqzm.js → index-5c4m8zdf.js} +2 -2
  445. package/dist/{index-qggqqja2.js → index-5dxgx3yj.js} +1 -1
  446. package/dist/{index-x5ayy5bn.js → index-5gdexvke.js} +3 -1
  447. package/dist/{index-t1a2jrg7.js → index-5xsxvk2x.js} +1 -1
  448. package/dist/{index-mbd92r1w.js → index-654rhe5d.js} +1 -1
  449. package/dist/{index-2dzc7mv9.js → index-67pnt4bh.js} +1 -1
  450. package/dist/index-6kpwkpgx.js +415 -0
  451. package/dist/{index-s43tr4my.js → index-6kwdfb9m.js} +18 -3
  452. package/dist/{index-1qwd54n3.js → index-6nzzj7qr.js} +1 -1
  453. package/dist/{index-rkjfzs0a.js → index-6p5pshcd.js} +2 -2
  454. package/dist/{index-118aebbg.js → index-6t4c98gr.js} +1 -1
  455. package/dist/{index-pws5c3x6.js → index-6xw1wf4m.js} +51 -32
  456. package/dist/{index-kwsr7j5k.js → index-6yb4stq3.js} +1 -1
  457. package/dist/{index-8jf4bt7v.js → index-76yhxa41.js} +8 -8
  458. package/dist/{index-7416mhyh.js → index-79vqbjcz.js} +1 -1
  459. package/dist/{index-13wmzpa6.js → index-7an2fs5h.js} +1 -1
  460. package/dist/index-7qza24g1.js +14 -0
  461. package/dist/{index-gtaaxe9d.js → index-8cek82ar.js} +1 -1
  462. package/dist/index-9f60gnfn.js +393 -0
  463. package/dist/{index-stpy56r3.js → index-aacwkve4.js} +209 -10
  464. package/dist/{index-rpyn7geq.js → index-ac3rfec5.js} +2 -2
  465. package/dist/{index-d2drhb9c.js → index-b0mh2t9x.js} +2 -2
  466. package/dist/index-b23s6e2c.js +158 -0
  467. package/dist/{index-an7vcmwf.js → index-bvpevwt6.js} +5 -5
  468. package/dist/{index-msxf2r8x.js → index-by8w2461.js} +1 -1
  469. package/dist/{index-8t9dr41h.js → index-c0yym4a0.js} +4 -4
  470. package/dist/{index-wddcfbng.js → index-c7fj4yw9.js} +1 -1
  471. package/dist/{index-tf70nm22.js → index-cb4nnvfr.js} +8 -6
  472. package/dist/{index-205456mf.js → index-d3s070eh.js} +2 -2
  473. package/dist/{index-apf5g7y9.js → index-dz6tstcp.js} +2 -2
  474. package/dist/{index-agy5fqgf.js → index-e1y74yra.js} +15 -8
  475. package/dist/index-edtg4tak.js +55 -0
  476. package/dist/{index-pq6g1k9t.js → index-ek9w11r0.js} +2 -2
  477. package/dist/{index-x46y9z26.js → index-epyj7ptp.js} +7 -5
  478. package/dist/{index-6kmjcm81.js → index-er483f3w.js} +93 -4
  479. package/dist/{index-827bgv33.js → index-f16b7rze.js} +1 -1
  480. package/dist/{index-gbhj370g.js → index-fec05c9k.js} +21 -21
  481. package/dist/{index-9wdrwx3b.js → index-fjac2w5e.js} +20 -22
  482. package/dist/{index-6918kxbf.js → index-fthw8dwa.js} +1 -0
  483. package/dist/{index-2mz8hj58.js → index-g04hszcm.js} +1 -1
  484. package/dist/{index-rw8ze745.js → index-ga6avd48.js} +1 -1
  485. package/dist/{index-paff5y0r.js → index-grhhsrd5.js} +9 -7
  486. package/dist/{index-mxt0wfjy.js → index-h20qcyh5.js} +5 -2
  487. package/dist/index-j3r7r1mh.js +150 -0
  488. package/dist/{index-vgytj9wp.js → index-jmaw365j.js} +1 -1
  489. package/dist/{index-cjytermm.js → index-jzdbkanz.js} +3 -3
  490. package/dist/index-k137ezb6.js +79 -0
  491. package/dist/{index-tazm4dxf.js → index-kdw0n92g.js} +1 -1
  492. package/dist/{index-wh70tmpd.js → index-ke6jtxkd.js} +1 -1
  493. package/dist/index-kgj8j7bv.js +83 -0
  494. package/dist/{index-9kpxz49v.js → index-kgkevngs.js} +1 -1
  495. package/dist/{index-hab790s6.js → index-kp1j7cc8.js} +4 -4
  496. package/dist/{index-jcge9tx3.js → index-krekhmp7.js} +142 -123
  497. package/dist/{index-3y2tzhr8.js → index-ky79wwgs.js} +4 -4
  498. package/dist/{index-39rmc00v.js → index-mfe9a266.js} +1 -1
  499. package/dist/{index-qpzyahe3.js → index-mgr0mdqp.js} +5 -2
  500. package/dist/{index-n20tybgk.js → index-mh3jdb25.js} +2 -2
  501. package/dist/{index-g5fgeb6v.js → index-mkmep637.js} +1 -1
  502. package/dist/{index-02r5sq9p.js → index-mrng24wq.js} +19 -21
  503. package/dist/index-mx2gya05.js +465 -0
  504. package/dist/{index-8yt3rwe7.js → index-n2t87ggz.js} +5 -5
  505. package/dist/index-nfeafbzh.js +7379 -0
  506. package/dist/{index-j0ef04jw.js → index-nfm15ej7.js} +4 -4
  507. package/dist/index-pd9e8rz4.js +193 -0
  508. package/dist/{index-zwgwyvdt.js → index-pdcbjd6a.js} +1 -1
  509. package/dist/{index-q427q23r.js → index-pjjh91vw.js} +2 -2
  510. package/dist/{index-gnjgkecx.js → index-q46b16xz.js} +1 -1
  511. package/dist/{index-9xr44d2g.js → index-qv5v6w9c.js} +330 -106
  512. package/dist/{index-tmv3ez1h.js → index-qymek778.js} +69 -12
  513. package/dist/{index-yrr3n391.js → index-ry8m2mhg.js} +2 -2
  514. package/dist/{index-93n6jwy0.js → index-s0n0nnz5.js} +3 -3
  515. package/dist/{index-1p8q0yrt.js → index-t5zp1jjj.js} +5 -7
  516. package/dist/{index-2rf33y2h.js → index-tav72wz5.js} +4 -2
  517. package/dist/{index-jpecjm30.js → index-tgehj4bp.js} +1 -1
  518. package/dist/{index-ma26mfnt.js → index-v5hmyef8.js} +1 -1
  519. package/dist/index-vebzvse6.js +230 -0
  520. package/dist/index-vv0hxrmy.js +295 -0
  521. package/dist/index-vxm0ya3f.js +707 -0
  522. package/dist/{index-t3qcac2a.js → index-w3s79kcb.js} +1 -1
  523. package/dist/{index-r97ppg0g.js → index-x1r7kxzd.js} +1 -1
  524. package/dist/{index-kynwa08q.js → index-xrsryvd2.js} +2 -2
  525. package/dist/{index-3mcbr8sx.js → index-xtxxt87z.js} +1 -1
  526. package/dist/index-zk839vzf.js +231 -0
  527. package/dist/{index-9c76vfxf.js → index-zyyerdny.js} +4 -4
  528. package/dist/internal/base64.d.ts +9 -0
  529. package/dist/internal/base64.d.ts.map +1 -0
  530. package/dist/internal/deadline-budget.d.ts +57 -0
  531. package/dist/internal/deadline-budget.d.ts.map +1 -0
  532. package/dist/internal/deadline.d.ts.map +1 -1
  533. package/dist/internal/timers.d.ts +2 -0
  534. package/dist/internal/timers.d.ts.map +1 -1
  535. package/dist/realtime/device-presence-contract.d.ts +194 -0
  536. package/dist/realtime/device-presence-contract.d.ts.map +1 -0
  537. package/dist/server/context-contribution.d.ts.map +1 -1
  538. package/dist/server/contract-method.d.ts.map +1 -1
  539. package/dist/server/device-presence-handlers.d.ts +54 -0
  540. package/dist/server/device-presence-handlers.d.ts.map +1 -0
  541. package/dist/server/device-presence-registry.d.ts +35 -0
  542. package/dist/server/device-presence-registry.d.ts.map +1 -0
  543. package/dist/server/device-presence.d.ts +45 -0
  544. package/dist/server/device-presence.d.ts.map +1 -0
  545. package/dist/server/endpoint-dispatch.d.ts.map +1 -1
  546. package/dist/server/middleware/cors.d.ts +3 -1
  547. package/dist/server/middleware/cors.d.ts.map +1 -1
  548. package/dist/server/types.d.ts +4 -11
  549. package/dist/server/types.d.ts.map +1 -1
  550. package/dist/service-host/systemd-render-shared.d.ts.map +1 -1
  551. package/dist/tools/agent-messages.d.ts +6 -0
  552. package/dist/tools/agent-messages.d.ts.map +1 -0
  553. package/dist/tools/agent-tool-error.d.ts +2 -2
  554. package/dist/tools/agent-tools.d.ts +153 -0
  555. package/dist/tools/agent-tools.d.ts.map +1 -0
  556. package/dist/tools/agent.d.ts +6 -56
  557. package/dist/tools/agent.d.ts.map +1 -1
  558. package/dist/tools/runtime-tool.d.ts +3 -4
  559. package/dist/tools/runtime-tool.d.ts.map +1 -1
  560. package/dist/tools/toolkit.d.ts +3 -1
  561. package/dist/tools/toolkit.d.ts.map +1 -1
  562. package/dist/tools/transfer/define-view-file-tool.d.ts.map +1 -1
  563. package/llms/agent-executor-acp.txt +2 -2
  564. package/llms/agent-executor-codex.txt +1 -1
  565. package/llms/agent-executor.txt +7 -3
  566. package/llms/agent-runtime-ai-sdk.txt +26 -0
  567. package/llms/agent-runtime-anthropic.txt +33 -0
  568. package/llms/agent-runtime-browser.txt +22 -8
  569. package/llms/agent-runtime-harness.txt +2 -2
  570. package/llms/agent-runtime-openai.txt +31 -0
  571. package/llms/agent-runtime-openrouter.txt +17 -7
  572. package/llms/agent-runtime-sandbox.txt +53 -1
  573. package/llms/agent-runtime-testing.txt +4 -2
  574. package/llms/agent-runtime.2.txt +158 -271
  575. package/llms/agent-runtime.3.txt +289 -243
  576. package/llms/agent-runtime.4.txt +273 -216
  577. package/llms/agent-runtime.5.txt +209 -2
  578. package/llms/agent-runtime.txt +238 -181
  579. package/llms/contract.txt +2 -1
  580. package/llms/device-relay.txt +34 -0
  581. package/llms/realtime.txt +20 -0
  582. package/llms/server.2.txt +125 -1
  583. package/llms/server.3.txt +111 -122
  584. package/llms/server.4.txt +1 -1
  585. package/llms/server.5.txt +3 -1
  586. package/llms/server.txt +31 -1
  587. package/llms/spend.txt +11 -6
  588. package/llms/stitchkit.2.txt +21 -43
  589. package/llms/stitchkit.3.txt +43 -0
  590. package/llms/stitchkit.txt +93 -84
  591. package/llms/tools-agent.txt +7 -5
  592. package/llms/tools-toolkit.txt +1 -1
  593. package/llms/tools.4.txt +7 -2
  594. package/llms/{upgrading-0.128.0-0.133.0.txt → upgrading-0.128.0-0.134.0.txt} +173 -3
  595. package/llms/upgrading.txt +1 -1
  596. package/llms-full.txt +981 -210
  597. package/llms.txt +21 -17
  598. package/package.json +18 -7
  599. package/dist/index-0p0s5hrj.js +0 -249
  600. package/dist/index-1dn2wjq6.js +0 -135
  601. package/dist/index-1psbyyhj.js +0 -21
  602. package/dist/index-34k82crg.js +0 -3961
  603. package/dist/index-5n3a8hd8.js +0 -102
  604. package/dist/index-97rp94ff.js +0 -8
  605. package/dist/index-bn2baakz.js +0 -336
  606. package/dist/index-eh71zr32.js +0 -14
  607. package/dist/index-emgrmavr.js +0 -1193
  608. package/dist/index-fzd477xk.js +0 -135
  609. package/dist/index-vgzhw2zk.js +0 -304
  610. package/dist/internal/ai-sdk-typed.d.ts +0 -14
  611. package/dist/internal/ai-sdk-typed.d.ts.map +0 -1
  612. package/dist/tools/runtime-call-context.d.ts +0 -15
  613. package/dist/tools/runtime-call-context.d.ts.map +0 -1
  614. package/dist/{index-vmbxj75t.js → index-jwtmw39p.js} +0 -0
  615. package/dist/{index-877e9y8r.js → index-sk03xe0n.js} +3 -3
  616. package/dist/{index-21cfxc4z.js → index-vh4291qh.js} +3 -3
@@ -0,0 +1,1260 @@
1
+ import {
2
+ AgentCacheControlSchema2
3
+ } from "../../index-vebzvse6.js";
4
+ import {
5
+ redactProviderFailureText,
6
+ joinProviderUrl,
7
+ assertNoExtraBodyClash,
8
+ resolveProviderApiKey,
9
+ parseHttpDateHeader,
10
+ postProviderStream,
11
+ nonEmptyString2,
12
+ recordField,
13
+ reportedInt,
14
+ computedInt,
15
+ buildProviderUsage,
16
+ normalizeProviderHistory,
17
+ historyHasToolTraffic,
18
+ parseSseStream,
19
+ parseSseJson
20
+ } from "../../index-vxm0ya3f.js";
21
+ import {
22
+ MAX_CACHE_BREAKPOINTS,
23
+ toWireCacheControl,
24
+ assertValidCacheTtlOrder,
25
+ budgetCacheBreakpoints
26
+ } from "../../index-23wn8sgf.js";
27
+ import {
28
+ prepareAnthropicJsonSchema
29
+ } from "../../index-1gmgwfn3.js";
30
+ import {
31
+ sanitizeSurrogates,
32
+ deterministicJson,
33
+ resolveFilePayload,
34
+ normalizeMime,
35
+ createToolCallIdMapper,
36
+ readPortableCacheControl,
37
+ AgentProviderError2,
38
+ AgentProviderStreamCutError2
39
+ } from "../../index-6kpwkpgx.js";
40
+ import {
41
+ base64ToBytes
42
+ } from "../../index-edtg4tak.js";
43
+ import {
44
+ isRecord
45
+ } from "../../index-ps3djm8d.js";
46
+
47
+ // src/agent-runtime/anthropic-options.ts
48
+ import { z } from "zod";
49
+ var AnthropicThinkingPolicySchema = z.discriminatedUnion("type", [
50
+ z.strictObject({ type: z.literal("adaptive") }),
51
+ z.strictObject({
52
+ type: z.literal("enabled"),
53
+ budgetTokens: z.int().min(1024)
54
+ }),
55
+ z.strictObject({ type: z.literal("disabled") })
56
+ ]);
57
+ var AnthropicRequestPolicyFieldsSchema = z.strictObject({
58
+ maxOutputTokens: z.int().positive().optional(),
59
+ thinking: AnthropicThinkingPolicySchema.optional(),
60
+ effort: z.enum(["low", "medium", "high", "max"]).optional(),
61
+ cacheControl: AgentCacheControlSchema2.optional(),
62
+ eagerInputStreaming: z.boolean().optional(),
63
+ disableParallelToolUse: z.boolean().optional(),
64
+ serviceTier: z.enum(["auto", "standard_only"]).optional(),
65
+ betas: z.array(z.string().min(1)).optional()
66
+ });
67
+ var AnthropicRequestPolicySchema = AnthropicRequestPolicyFieldsSchema.superRefine((policy, context) => {
68
+ if (policy.thinking?.type === "enabled" && policy.maxOutputTokens !== undefined && policy.maxOutputTokens <= policy.thinking.budgetTokens) {
69
+ context.addIssue({
70
+ code: "custom",
71
+ path: ["thinking", "budgetTokens"],
72
+ message: "maxOutputTokens must be strictly greater than thinking.budgetTokens"
73
+ });
74
+ }
75
+ });
76
+ var ANTHROPIC_OWNED_BODY_KEYS = [
77
+ "model",
78
+ "messages",
79
+ "system",
80
+ "max_tokens",
81
+ "stream",
82
+ "tools",
83
+ "tool_choice",
84
+ "thinking",
85
+ "output_config",
86
+ "cache_control",
87
+ "service_tier",
88
+ "temperature",
89
+ "top_p",
90
+ "top_k",
91
+ "stop_sequences"
92
+ ];
93
+ var INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14";
94
+ var FINE_GRAINED_TOOL_STREAMING_BETA = "fine-grained-tool-streaming-2025-05-14";
95
+ function resolveAnthropicPolicy(raw = {}) {
96
+ return AnthropicRequestPolicySchema.parse(raw);
97
+ }
98
+ function isAnthropicThinkingEnabled(thinking) {
99
+ return thinking?.type === "enabled" || thinking?.type === "adaptive";
100
+ }
101
+ function mergeAnthropicBetas(...groups) {
102
+ const seen = new Set;
103
+ const ordered = [];
104
+ for (const group of groups) {
105
+ if (!group)
106
+ continue;
107
+ for (const raw of group) {
108
+ for (const piece of raw.split(",")) {
109
+ const trimmed = piece.trim();
110
+ if (trimmed.length === 0 || seen.has(trimmed))
111
+ continue;
112
+ seen.add(trimmed);
113
+ ordered.push(trimmed);
114
+ }
115
+ }
116
+ }
117
+ return ordered.length > 0 ? ordered.join(",") : undefined;
118
+ }
119
+ function readAnthropicProviderOptions(providerOptions) {
120
+ const entry = providerOptions?.anthropic;
121
+ return isRecord(entry) ? entry : undefined;
122
+ }
123
+ function mergeAnthropicStepPolicy(base, providerOptions, requestMaxOutputTokens) {
124
+ const raw = readAnthropicProviderOptions(providerOptions);
125
+ if (!raw || Object.keys(raw).length === 0)
126
+ return base;
127
+ const override = AnthropicRequestPolicyFieldsSchema.parse(raw);
128
+ const mergedBetas = base.betas !== undefined || override.betas !== undefined ? Array.from(new Set([...base.betas ?? [], ...override.betas ?? []])) : undefined;
129
+ const merged = {
130
+ ...override.maxOutputTokens !== undefined ? { maxOutputTokens: override.maxOutputTokens } : base.maxOutputTokens !== undefined && { maxOutputTokens: base.maxOutputTokens },
131
+ ...override.thinking !== undefined ? { thinking: override.thinking } : base.thinking !== undefined && { thinking: base.thinking },
132
+ ...override.effort !== undefined ? { effort: override.effort } : base.effort !== undefined && { effort: base.effort },
133
+ ...override.cacheControl !== undefined ? { cacheControl: override.cacheControl } : base.cacheControl !== undefined && { cacheControl: base.cacheControl },
134
+ ...override.eagerInputStreaming !== undefined ? { eagerInputStreaming: override.eagerInputStreaming } : base.eagerInputStreaming !== undefined && {
135
+ eagerInputStreaming: base.eagerInputStreaming
136
+ },
137
+ ...override.disableParallelToolUse !== undefined ? { disableParallelToolUse: override.disableParallelToolUse } : base.disableParallelToolUse !== undefined && {
138
+ disableParallelToolUse: base.disableParallelToolUse
139
+ },
140
+ ...override.serviceTier !== undefined ? { serviceTier: override.serviceTier } : base.serviceTier !== undefined && { serviceTier: base.serviceTier },
141
+ ...mergedBetas !== undefined && mergedBetas.length > 0 && { betas: mergedBetas }
142
+ };
143
+ const effectiveMaxOutputTokens = requestMaxOutputTokens ?? merged.maxOutputTokens;
144
+ if (merged.thinking?.type === "enabled" && effectiveMaxOutputTokens !== undefined && effectiveMaxOutputTokens <= merged.thinking.budgetTokens) {
145
+ throw new TypeError(`anthropicProvider requires maxOutputTokens (${effectiveMaxOutputTokens}) to exceed thinking.budgetTokens (${merged.thinking.budgetTokens})`);
146
+ }
147
+ return merged;
148
+ }
149
+ function validateAnthropicRequest(request, policy) {
150
+ const maxTokens = request.settings.maxOutputTokens ?? policy.maxOutputTokens;
151
+ if (maxTokens === undefined) {
152
+ throw new TypeError("anthropicProvider requires maxOutputTokens in request settings or policy");
153
+ }
154
+ if (!Number.isInteger(maxTokens) || maxTokens <= 0) {
155
+ throw new TypeError("anthropicProvider requires maxOutputTokens to be a positive integer");
156
+ }
157
+ if (policy.thinking?.type === "enabled" && maxTokens <= policy.thinking.budgetTokens) {
158
+ throw new TypeError(`anthropicProvider requires maxOutputTokens (${maxTokens}) to exceed thinking.budgetTokens (${policy.thinking.budgetTokens})`);
159
+ }
160
+ const unsupported = [];
161
+ if (request.settings.presencePenalty !== undefined)
162
+ unsupported.push("presencePenalty");
163
+ if (request.settings.frequencyPenalty !== undefined)
164
+ unsupported.push("frequencyPenalty");
165
+ if (request.settings.seed !== undefined)
166
+ unsupported.push("seed");
167
+ if (unsupported.length > 0) {
168
+ throw new TypeError(`anthropicProvider does not support sampling setting(s): ${unsupported.join(", ")}`);
169
+ }
170
+ const forcedToolChoice = request.toolChoice === "required" || typeof request.toolChoice === "object" && request.toolChoice !== null;
171
+ if (request.settings.temperature !== undefined && request.settings.topP !== undefined) {
172
+ throw new TypeError("anthropicProvider does not allow setting both temperature and topP in the same request");
173
+ }
174
+ if (isAnthropicThinkingEnabled(policy.thinking)) {
175
+ if (request.settings.temperature !== undefined && request.settings.temperature !== 1) {
176
+ throw new TypeError("anthropicProvider does not allow temperature other than 1 when thinking is enabled");
177
+ }
178
+ if (request.settings.topP !== undefined && (request.settings.topP < 0.95 || request.settings.topP > 1)) {
179
+ throw new TypeError("anthropicProvider requires topP between 0.95 and 1 when thinking is enabled");
180
+ }
181
+ if (request.settings.topK !== undefined) {
182
+ throw new TypeError("anthropicProvider does not allow topK when thinking is enabled");
183
+ }
184
+ if (forcedToolChoice) {
185
+ throw new TypeError("anthropicProvider does not allow forced toolChoice when thinking is enabled");
186
+ }
187
+ }
188
+ if (request.responseFormat?.type === "json-object") {
189
+ throw new TypeError("anthropicProvider does not support schemaless json-object responseFormat; use json-schema");
190
+ }
191
+ if (request.tools.length === 0 && forcedToolChoice) {
192
+ throw new TypeError("anthropicProvider cannot force toolChoice when no tools are provided");
193
+ }
194
+ return maxTokens;
195
+ }
196
+ function encodeTools(specs, policy, plannedBreakpoints) {
197
+ const tools = [];
198
+ let eagerUsed = policy.eagerInputStreaming === true;
199
+ for (const spec of specs) {
200
+ if (spec.type === "provider") {
201
+ throw new TypeError(`anthropicProvider: unsupported provider tool "${spec.id}" (type: "provider")`);
202
+ }
203
+ const opts = readAnthropicProviderOptions(spec.providerOptions);
204
+ const eager = policy.eagerInputStreaming === true || opts?.eagerInputStreaming === true || opts?.eager_input_streaming === true;
205
+ if (eager)
206
+ eagerUsed = true;
207
+ const inputSchema = spec.strict === true ? prepareAnthropicJsonSchema(spec.inputSchema) : spec.inputSchema;
208
+ const toolObj = {
209
+ name: spec.name,
210
+ ...spec.description !== undefined && {
211
+ description: sanitizeSurrogates(spec.description)
212
+ },
213
+ input_schema: inputSchema,
214
+ ...spec.strict !== undefined && { strict: spec.strict },
215
+ ...eager && { eager_input_streaming: true }
216
+ };
217
+ tools.push(toolObj);
218
+ const control = readPortableCacheControl(spec.cacheControl, spec.providerOptions, "anthropic");
219
+ if (control !== undefined) {
220
+ plannedBreakpoints.push({ slot: "tools", control, target: toolObj });
221
+ }
222
+ }
223
+ return { tools, eagerUsed };
224
+ }
225
+ function encodeToolChoice(toolChoice, hasTools, hasToolsField, disableParallel) {
226
+ if (!hasTools) {
227
+ return hasToolsField && toolChoice === "none" ? { type: "none" } : undefined;
228
+ }
229
+ if (toolChoice === undefined) {
230
+ return disableParallel ? { type: "auto", disable_parallel_tool_use: true } : undefined;
231
+ }
232
+ if (toolChoice === "none")
233
+ return { type: "none" };
234
+ const parallel = disableParallel ? { disable_parallel_tool_use: true } : {};
235
+ if (toolChoice === "auto")
236
+ return { type: "auto", ...parallel };
237
+ if (toolChoice === "required")
238
+ return { type: "any", ...parallel };
239
+ return { type: "tool", name: toolChoice.toolName, ...parallel };
240
+ }
241
+ function buildAnthropicPolicyFields(input) {
242
+ const { request, policy, maxTokens, hasToolTraffic, systemBreakpoints, messageBreakpoints } = input;
243
+ const toolBreakpoints = [];
244
+ const { tools: encodedTools, eagerUsed } = encodeTools(request.tools, policy, toolBreakpoints);
245
+ const topLevelCacheControl = request.cacheControl ?? policy.cacheControl;
246
+ const explicitLimit = topLevelCacheControl !== undefined ? MAX_CACHE_BREAKPOINTS - 1 : MAX_CACHE_BREAKPOINTS;
247
+ const kept = budgetCacheBreakpoints([...toolBreakpoints, ...systemBreakpoints, ...messageBreakpoints], explicitLimit);
248
+ const wireControls = [
249
+ ...kept.map((bp) => bp.control),
250
+ ...topLevelCacheControl !== undefined ? [topLevelCacheControl] : []
251
+ ];
252
+ assertValidCacheTtlOrder(wireControls, "anthropicProvider");
253
+ for (const bp of kept) {
254
+ bp.target.cache_control = toWireCacheControl(bp.control);
255
+ }
256
+ const includeTools = encodedTools.length > 0 || hasToolTraffic;
257
+ const toolChoice = encodeToolChoice(request.toolChoice, encodedTools.length > 0, includeTools, policy.disableParallelToolUse === true);
258
+ const outputConfig = {
259
+ ...policy.effort !== undefined && { effort: policy.effort },
260
+ ...request.responseFormat?.type === "json-schema" && {
261
+ format: {
262
+ type: "json_schema",
263
+ schema: prepareAnthropicJsonSchema(request.responseFormat.schema)
264
+ }
265
+ }
266
+ };
267
+ const thinkingWire = policy.thinking === undefined ? undefined : policy.thinking.type === "enabled" ? { type: "enabled", budget_tokens: policy.thinking.budgetTokens } : { type: policy.thinking.type };
268
+ const fields = {
269
+ max_tokens: maxTokens,
270
+ stream: true,
271
+ ...includeTools && { tools: encodedTools },
272
+ ...toolChoice !== undefined && { tool_choice: toolChoice },
273
+ ...thinkingWire !== undefined && { thinking: thinkingWire },
274
+ ...Object.keys(outputConfig).length > 0 && { output_config: outputConfig },
275
+ ...topLevelCacheControl !== undefined && {
276
+ cache_control: toWireCacheControl(topLevelCacheControl)
277
+ },
278
+ ...policy.serviceTier !== undefined && { service_tier: policy.serviceTier },
279
+ ...request.settings.temperature !== undefined && {
280
+ temperature: request.settings.temperature
281
+ },
282
+ ...request.settings.topP !== undefined && { top_p: request.settings.topP },
283
+ ...request.settings.topK !== undefined && { top_k: request.settings.topK },
284
+ ...request.settings.stopSequences !== undefined && request.settings.stopSequences.length > 0 && {
285
+ stop_sequences: [...request.settings.stopSequences]
286
+ }
287
+ };
288
+ const autoBetas = [];
289
+ if (isAnthropicThinkingEnabled(policy.thinking))
290
+ autoBetas.push(INTERLEAVED_THINKING_BETA);
291
+ if (eagerUsed)
292
+ autoBetas.push(FINE_GRAINED_TOOL_STREAMING_BETA);
293
+ return { fields, autoBetas };
294
+ }
295
+
296
+ // src/agent-runtime/anthropic-media.ts
297
+ var SUPPORTED_IMAGE_TYPES = new Set(["image/jpeg", "image/png", "image/gif", "image/webp"]);
298
+ function encodeAnthropicFileBlock(data, mediaType, filename) {
299
+ const declared = normalizeMime(mediaType);
300
+ if (declared.startsWith("audio/") || declared.startsWith("video/")) {
301
+ throw new TypeError(`anthropicProvider does not support ${declared} media inputs`);
302
+ }
303
+ const resolved = resolveFilePayload(data, declared);
304
+ const mime = resolved.kind === "base64" ? normalizeMime(resolved.mediaType) : declared;
305
+ if (mime.startsWith("audio/") || mime.startsWith("video/")) {
306
+ throw new TypeError(`anthropicProvider does not support ${mime} media inputs`);
307
+ }
308
+ const sanitizedFilename = filename !== undefined ? sanitizeSurrogates(filename) : undefined;
309
+ const title = sanitizedFilename !== undefined && sanitizedFilename.length > 0 ? { title: sanitizedFilename } : {};
310
+ if (resolved.kind === "reference") {
311
+ const fileId = resolved.reference.anthropic;
312
+ if (typeof fileId !== "string" || fileId.length === 0) {
313
+ throw new TypeError('anthropicProvider cannot encode a file reference without an "anthropic" file id');
314
+ }
315
+ const isImage = mime.startsWith("image/");
316
+ return {
317
+ type: isImage ? "image" : "document",
318
+ source: { type: "file", file_id: fileId },
319
+ ...!isImage ? title : {}
320
+ };
321
+ }
322
+ if (mime.startsWith("image/")) {
323
+ if (resolved.kind === "url") {
324
+ return { type: "image", source: { type: "url", url: resolved.url } };
325
+ }
326
+ if (resolved.kind === "base64") {
327
+ if (!SUPPORTED_IMAGE_TYPES.has(mime)) {
328
+ throw new TypeError(`anthropicProvider does not support image media type "${mime}"`);
329
+ }
330
+ return {
331
+ type: "image",
332
+ source: { type: "base64", media_type: mime, data: resolved.base64 }
333
+ };
334
+ }
335
+ throw new TypeError(`anthropicProvider cannot encode inline text as "${mime}"`);
336
+ }
337
+ if (mime === "application/pdf") {
338
+ if (resolved.kind === "url") {
339
+ return { type: "document", source: { type: "url", url: resolved.url }, ...title };
340
+ }
341
+ if (resolved.kind === "base64") {
342
+ return {
343
+ type: "document",
344
+ source: { type: "base64", media_type: "application/pdf", data: resolved.base64 },
345
+ ...title
346
+ };
347
+ }
348
+ throw new TypeError("anthropicProvider cannot encode inline text as application/pdf");
349
+ }
350
+ if (mime === "text/plain") {
351
+ if (resolved.kind === "text") {
352
+ return {
353
+ type: "document",
354
+ source: { type: "text", media_type: "text/plain", data: resolved.text },
355
+ ...title
356
+ };
357
+ }
358
+ if (resolved.kind === "base64") {
359
+ const text = sanitizeSurrogates(new TextDecoder().decode(base64ToBytes(resolved.base64)));
360
+ return {
361
+ type: "document",
362
+ source: { type: "text", media_type: "text/plain", data: text },
363
+ ...title
364
+ };
365
+ }
366
+ throw new TypeError("anthropicProvider does not support URL sources for text/plain documents");
367
+ }
368
+ throw new TypeError(`anthropicProvider does not support file media type "${mime || mediaType}"`);
369
+ }
370
+ function isErrorFlagSet(providerOptions) {
371
+ const entry = readAnthropicProviderOptions(providerOptions);
372
+ return entry?.isError === true || entry?.is_error === true;
373
+ }
374
+ function encodeAnthropicToolOutput(output, part) {
375
+ switch (output.type) {
376
+ case "text":
377
+ return { content: output.value, isError: false };
378
+ case "error-text":
379
+ return { content: output.value, isError: true };
380
+ case "json":
381
+ return { content: deterministicJson(output.value), isError: false };
382
+ case "error-json":
383
+ return { content: deterministicJson(output.value), isError: true };
384
+ case "execution-denied":
385
+ return {
386
+ content: output.reason && output.reason.length > 0 ? output.reason : "Tool execution denied",
387
+ isError: true
388
+ };
389
+ case "content": {
390
+ const isError = isErrorFlagSet(part.providerOptions) || isErrorFlagSet(output.providerOptions);
391
+ const blocks = [];
392
+ for (const item of output.value) {
393
+ if (item.type === "text" && item.text.length > 0)
394
+ blocks.push({ type: "text", text: item.text });
395
+ else if (item.type === "file") {
396
+ blocks.push(encodeAnthropicFileBlock(item.data, item.mediaType, item.filename));
397
+ } else if (item.type === "custom") {
398
+ blocks.push({ type: "text", text: deterministicJson(item) });
399
+ }
400
+ }
401
+ if (blocks.length === 0) {
402
+ return { content: isError ? "Tool execution failed" : "(no tool output)", isError };
403
+ }
404
+ return { content: blocks, isError };
405
+ }
406
+ }
407
+ }
408
+
409
+ // src/agent-runtime/anthropic-messages.ts
410
+ var TOOL_CALL_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
411
+ var TOOL_CALL_ID_MAX_LENGTH = 64;
412
+ function splitErrorToolResultImages(blocks) {
413
+ const toolResults = [];
414
+ const otherBlocks = [];
415
+ const trailingImages = [];
416
+ for (const block of blocks) {
417
+ if (block.type !== "tool_result") {
418
+ otherBlocks.push(block);
419
+ continue;
420
+ }
421
+ if (block.is_error === true && Array.isArray(block.content)) {
422
+ const kept = [];
423
+ let extracted = false;
424
+ for (const item of block.content) {
425
+ if (isRecord(item) && item.type === "image") {
426
+ trailingImages.push(item);
427
+ extracted = true;
428
+ } else {
429
+ kept.push(item);
430
+ }
431
+ }
432
+ if (extracted) {
433
+ toolResults.push({
434
+ ...block,
435
+ content: kept.length > 0 ? kept : "Tool execution failed (see attached image)"
436
+ });
437
+ continue;
438
+ }
439
+ }
440
+ toolResults.push(block);
441
+ }
442
+ return [...toolResults, ...otherBlocks, ...trailingImages];
443
+ }
444
+ function encodeToolMessage(message, mapToolCallId) {
445
+ const blocks = [];
446
+ let cacheControl = readPortableCacheControl(message.cacheControl, message.providerOptions, "anthropic");
447
+ for (const part of message.content) {
448
+ if (part.type !== "tool-result")
449
+ continue;
450
+ const encoded = encodeAnthropicToolOutput(part.output, part);
451
+ blocks.push({
452
+ type: "tool_result",
453
+ tool_use_id: mapToolCallId(part.toolCallId),
454
+ content: encoded.content,
455
+ ...encoded.isError && { is_error: true }
456
+ });
457
+ const partCache = readPortableCacheControl(part.cacheControl, part.providerOptions, "anthropic") ?? readPortableCacheControl(part.output.cacheControl, part.output.providerOptions, "anthropic");
458
+ if (partCache !== undefined)
459
+ cacheControl = partCache;
460
+ }
461
+ return { blocks, ...cacheControl !== undefined && { cacheControl } };
462
+ }
463
+ function encodeUserMessage(message) {
464
+ const msgCache = readPortableCacheControl(message.cacheControl, message.providerOptions, "anthropic");
465
+ const blocks = [];
466
+ if (typeof message.content === "string") {
467
+ if (message.content.length > 0) {
468
+ blocks.push({
469
+ block: { type: "text", text: message.content },
470
+ ...msgCache !== undefined && { cacheControl: msgCache }
471
+ });
472
+ }
473
+ return blocks;
474
+ }
475
+ for (const part of message.content) {
476
+ const partCache = readPortableCacheControl(part.cacheControl, part.providerOptions, "anthropic");
477
+ if (part.type === "text" && part.text.length > 0) {
478
+ blocks.push({
479
+ block: { type: "text", text: part.text },
480
+ ...partCache !== undefined && { cacheControl: partCache }
481
+ });
482
+ } else if (part.type === "file") {
483
+ blocks.push({
484
+ block: encodeAnthropicFileBlock(part.data, part.mediaType, part.filename),
485
+ ...partCache !== undefined && { cacheControl: partCache }
486
+ });
487
+ }
488
+ }
489
+ const lastIndex = blocks.length - 1;
490
+ const last = blocks[lastIndex];
491
+ if (msgCache !== undefined && last && last.cacheControl === undefined) {
492
+ blocks[lastIndex] = { block: last.block, cacheControl: msgCache };
493
+ }
494
+ return blocks;
495
+ }
496
+ function encodeAssistantMessage(message, mapToolCallId) {
497
+ const msgCache = readPortableCacheControl(message.cacheControl, message.providerOptions, "anthropic");
498
+ const blocks = [];
499
+ if (typeof message.content === "string") {
500
+ if (message.content.length > 0) {
501
+ blocks.push({
502
+ kind: "text",
503
+ block: { type: "text", text: message.content },
504
+ ...msgCache !== undefined && { cacheControl: msgCache }
505
+ });
506
+ }
507
+ return blocks;
508
+ }
509
+ for (const part of message.content) {
510
+ if (part.type === "reasoning") {
511
+ const opts = readAnthropicProviderOptions(part.providerOptions);
512
+ if (opts?.type === "redacted_thinking" && typeof opts.data === "string" && opts.data.length > 0) {
513
+ blocks.push({
514
+ kind: "thinking",
515
+ block: { type: "redacted_thinking", data: opts.data }
516
+ });
517
+ } else if (typeof opts?.signature === "string" && opts.signature.length > 0) {
518
+ blocks.push({
519
+ kind: "thinking",
520
+ block: { type: "thinking", thinking: part.text, signature: opts.signature }
521
+ });
522
+ }
523
+ continue;
524
+ }
525
+ if (part.type === "text" && part.text.length > 0) {
526
+ const partCache = readPortableCacheControl(part.cacheControl, part.providerOptions, "anthropic");
527
+ blocks.push({
528
+ kind: "text",
529
+ block: { type: "text", text: part.text },
530
+ ...partCache !== undefined && { cacheControl: partCache }
531
+ });
532
+ continue;
533
+ }
534
+ if (part.type === "tool-call" && part.providerExecuted !== true) {
535
+ const partCache = readPortableCacheControl(part.cacheControl, part.providerOptions, "anthropic");
536
+ blocks.push({
537
+ kind: "tool_use",
538
+ block: {
539
+ type: "tool_use",
540
+ id: mapToolCallId(part.toolCallId),
541
+ name: part.toolName,
542
+ input: isRecord(part.input) ? part.input : {}
543
+ },
544
+ ...partCache !== undefined && { cacheControl: partCache }
545
+ });
546
+ }
547
+ }
548
+ if (msgCache !== undefined) {
549
+ for (let i = blocks.length - 1;i >= 0; i -= 1) {
550
+ const candidate = blocks[i];
551
+ if (!candidate || candidate.kind === "thinking")
552
+ continue;
553
+ if (candidate.cacheControl === undefined)
554
+ blocks[i] = { ...candidate, cacheControl: msgCache };
555
+ break;
556
+ }
557
+ }
558
+ return blocks;
559
+ }
560
+ function encodeSystemBlocks(instructions, inlineSystems, plannedBreakpoints) {
561
+ const blocks = [];
562
+ for (const entry of [...instructions, ...inlineSystems]) {
563
+ const clean = sanitizeSurrogates(entry.content);
564
+ if (clean.length === 0)
565
+ continue;
566
+ const block = { type: "text", text: clean };
567
+ blocks.push(block);
568
+ const control = readPortableCacheControl(entry.cacheControl, entry.providerOptions, "anthropic");
569
+ if (control !== undefined)
570
+ plannedBreakpoints.push({ slot: "system", control, target: block });
571
+ }
572
+ return blocks;
573
+ }
574
+ function collectPendingTurns(messages, mapToolCallId) {
575
+ const inlineSystems = [];
576
+ const turns = [];
577
+ for (const message of messages) {
578
+ if (message.role === "system") {
579
+ inlineSystems.push(message);
580
+ continue;
581
+ }
582
+ if (message.role === "tool") {
583
+ const encoded = encodeToolMessage(message, mapToolCallId);
584
+ if (encoded.blocks.length === 0)
585
+ continue;
586
+ const last = turns.at(-1);
587
+ if (last?.role === "user") {
588
+ last.toolResults.push(...encoded.blocks);
589
+ if (encoded.cacheControl !== undefined)
590
+ last.toolCacheControl = encoded.cacheControl;
591
+ } else {
592
+ turns.push({
593
+ role: "user",
594
+ toolResults: [...encoded.blocks],
595
+ ...encoded.cacheControl !== undefined && {
596
+ toolCacheControl: encoded.cacheControl
597
+ },
598
+ userBlocks: []
599
+ });
600
+ }
601
+ continue;
602
+ }
603
+ if (message.role === "user") {
604
+ const encoded = encodeUserMessage(message);
605
+ if (encoded.length === 0)
606
+ continue;
607
+ const last = turns.at(-1);
608
+ if (last?.role === "user")
609
+ last.userBlocks.push(...encoded);
610
+ else
611
+ turns.push({ role: "user", toolResults: [], userBlocks: [...encoded] });
612
+ continue;
613
+ }
614
+ const encoded = encodeAssistantMessage(message, mapToolCallId);
615
+ if (encoded.length === 0)
616
+ continue;
617
+ const last = turns.at(-1);
618
+ if (last?.role === "assistant")
619
+ last.blocks.push(...encoded);
620
+ else
621
+ turns.push({ role: "assistant", blocks: [...encoded] });
622
+ }
623
+ return { inlineSystems, turns };
624
+ }
625
+ function serializeTurns(turns, plannedBreakpoints) {
626
+ const wireMessages = [];
627
+ for (const turn of turns) {
628
+ if (turn.role === "user") {
629
+ const userCacheMap = new Map;
630
+ for (const entry of turn.userBlocks) {
631
+ if (entry.cacheControl !== undefined)
632
+ userCacheMap.set(entry.block, entry.cacheControl);
633
+ }
634
+ const finalized = splitErrorToolResultImages([
635
+ ...turn.toolResults,
636
+ ...turn.userBlocks.map((entry) => entry.block)
637
+ ]);
638
+ if (finalized.length === 0)
639
+ continue;
640
+ let lastToolResult;
641
+ for (const block of finalized) {
642
+ if (block.type === "tool_result")
643
+ lastToolResult = block;
644
+ }
645
+ if (turn.toolCacheControl !== undefined && lastToolResult !== undefined) {
646
+ plannedBreakpoints.push({
647
+ slot: "message",
648
+ control: turn.toolCacheControl,
649
+ target: lastToolResult
650
+ });
651
+ }
652
+ for (const block of finalized) {
653
+ const control = userCacheMap.get(block);
654
+ if (control !== undefined)
655
+ plannedBreakpoints.push({ slot: "message", control, target: block });
656
+ }
657
+ wireMessages.push({ role: "user", content: finalized });
658
+ continue;
659
+ }
660
+ const ordered = turn.blocks;
661
+ if (ordered.length === 0)
662
+ continue;
663
+ for (const entry of ordered) {
664
+ if (entry.kind !== "thinking" && entry.cacheControl !== undefined) {
665
+ plannedBreakpoints.push({
666
+ slot: "message",
667
+ control: entry.cacheControl,
668
+ target: entry.block
669
+ });
670
+ }
671
+ }
672
+ wireMessages.push({ role: "assistant", content: ordered.map((entry) => entry.block) });
673
+ }
674
+ return wireMessages;
675
+ }
676
+ function buildAnthropicRequest(input) {
677
+ const { modelId, request, extraBody } = input;
678
+ const policy = mergeAnthropicStepPolicy(input.policy, request.providerOptions, request.settings.maxOutputTokens);
679
+ const maxTokens = validateAnthropicRequest(request, policy);
680
+ const normalized = normalizeProviderHistory(request.messages);
681
+ const mapToolCallId = createToolCallIdMapper({
682
+ pattern: TOOL_CALL_ID_PATTERN,
683
+ maxLength: TOOL_CALL_ID_MAX_LENGTH
684
+ });
685
+ const systemBreakpoints = [];
686
+ const messageBreakpoints = [];
687
+ const { inlineSystems, turns } = collectPendingTurns(normalized, mapToolCallId);
688
+ const wireMessages = serializeTurns(turns, messageBreakpoints);
689
+ const systemBlocks = encodeSystemBlocks(request.instructions, inlineSystems, systemBreakpoints);
690
+ const { fields, autoBetas } = buildAnthropicPolicyFields({
691
+ request,
692
+ policy,
693
+ maxTokens,
694
+ hasToolTraffic: historyHasToolTraffic(normalized),
695
+ systemBreakpoints,
696
+ messageBreakpoints
697
+ });
698
+ const body = {
699
+ ...extraBody ?? {},
700
+ model: modelId,
701
+ ...fields,
702
+ messages: wireMessages,
703
+ ...systemBlocks.length > 0 && { system: systemBlocks }
704
+ };
705
+ return {
706
+ body,
707
+ autoBetas,
708
+ ...policy.betas !== undefined && { policyBetas: policy.betas }
709
+ };
710
+ }
711
+
712
+ // src/agent-runtime/anthropic-response.ts
713
+ var CREDIT_BALANCE_PATTERN = /credit balance is too low/i;
714
+ function resolveCacheWriteTokens(source) {
715
+ const direct = reportedInt(source.cache_creation_input_tokens);
716
+ if (direct.value !== undefined)
717
+ return direct;
718
+ const breakdown = recordField(source, "cache_creation");
719
+ if (!breakdown)
720
+ return { provenance: "unavailable" };
721
+ const ttl1h = reportedInt(breakdown.ephemeral_1h_input_tokens);
722
+ const ttl5m = reportedInt(breakdown.ephemeral_5m_input_tokens);
723
+ if (ttl1h.value !== undefined && ttl5m.value !== undefined) {
724
+ const total = ttl1h.value + ttl5m.value;
725
+ return ttl1h.value > 0 && ttl5m.value > 0 ? computedInt(total) : reportedInt(total);
726
+ }
727
+ if (ttl1h.value !== undefined)
728
+ return ttl1h;
729
+ if (ttl5m.value !== undefined)
730
+ return ttl5m;
731
+ return { provenance: "unavailable" };
732
+ }
733
+ function normalizeAnthropicUsage(rawUsage) {
734
+ if (!isRecord(rawUsage)) {
735
+ return buildProviderUsage({
736
+ inputTokens: { provenance: "unavailable" },
737
+ outputTokens: { provenance: "unavailable" }
738
+ });
739
+ }
740
+ const baseInput = reportedInt(rawUsage.input_tokens);
741
+ const cacheReadTokens = reportedInt(rawUsage.cache_read_input_tokens);
742
+ const cacheWriteTokens = resolveCacheWriteTokens(rawUsage);
743
+ const cacheReadVal = cacheReadTokens.value ?? 0;
744
+ const cacheWriteVal = cacheWriteTokens.value ?? 0;
745
+ const inputTokens = baseInput.value === undefined ? { provenance: "unavailable" } : cacheReadVal > 0 || cacheWriteVal > 0 ? computedInt(baseInput.value + cacheReadVal + cacheWriteVal) : baseInput;
746
+ const outputTokens = reportedInt(rawUsage.output_tokens);
747
+ const outputDetails = recordField(rawUsage, "output_tokens_details");
748
+ const reasoningTokens = reportedInt(outputDetails?.thinking_tokens);
749
+ return buildProviderUsage({
750
+ inputTokens,
751
+ outputTokens,
752
+ reasoningTokens,
753
+ cacheReadTokens,
754
+ cacheWriteTokens,
755
+ cost: { provenance: "unavailable" }
756
+ });
757
+ }
758
+ function mapAnthropicStopReason(raw) {
759
+ switch (raw) {
760
+ case "end_turn":
761
+ case "stop_sequence":
762
+ return "stop";
763
+ case "max_tokens":
764
+ case "model_context_window_exceeded":
765
+ return "length";
766
+ case "tool_use":
767
+ return "tool-calls";
768
+ case "refusal":
769
+ return "content-filter";
770
+ default:
771
+ return "other";
772
+ }
773
+ }
774
+ function parseAnthropicError(payload, rawText, status) {
775
+ const errObj = recordField(payload, "error") ?? (isRecord(payload) ? payload : undefined);
776
+ const rawType = nonEmptyString2(errObj?.type);
777
+ const errorType = rawType === "error" ? undefined : rawType;
778
+ const message = nonEmptyString2(errObj?.message) ?? (rawText.trim().length > 0 ? rawText.trim() : `anthropic HTTP ${status}`);
779
+ let failureReason;
780
+ if (errorType === "overloaded_error") {
781
+ failureReason = "overloaded";
782
+ } else if (errorType === "rate_limit_error") {
783
+ failureReason = "rate-limited";
784
+ } else if (errorType === "api_error") {
785
+ failureReason = "server-error";
786
+ } else if (errorType === "request_too_large") {
787
+ failureReason = "context-overflow";
788
+ } else if (errorType === "invalid_request_error" && CREDIT_BALANCE_PATTERN.test(message)) {
789
+ failureReason = "insufficient-credits";
790
+ }
791
+ return {
792
+ message,
793
+ ...errorType !== undefined && { errorType, code: errorType },
794
+ ...failureReason !== undefined && { failureReason }
795
+ };
796
+ }
797
+ async function* decodeAnthropicJsonResponse(body, responseTimestamp) {
798
+ if (!isRecord(body)) {
799
+ throw new AgentProviderError2("anthropic returned an invalid JSON response", {
800
+ provider: "anthropic"
801
+ });
802
+ }
803
+ if (body.type === "error" || "error" in body && body.error !== undefined && body.error !== null) {
804
+ const parsed = parseAnthropicError(body, "", 200);
805
+ throw new AgentProviderError2(redactProviderFailureText(parsed.message), {
806
+ provider: "anthropic",
807
+ ...parsed.code !== undefined && { code: parsed.code },
808
+ ...parsed.errorType !== undefined && { errorType: parsed.errorType },
809
+ ...parsed.failureReason !== undefined && { failureReason: parsed.failureReason }
810
+ });
811
+ }
812
+ const id = nonEmptyString2(body.id);
813
+ const modelId = nonEmptyString2(body.model);
814
+ if (id !== undefined || modelId !== undefined || responseTimestamp !== undefined) {
815
+ yield {
816
+ type: "response-metadata",
817
+ ...id !== undefined && { id },
818
+ ...modelId !== undefined && { modelId },
819
+ ...responseTimestamp !== undefined && { timestamp: responseTimestamp }
820
+ };
821
+ }
822
+ const content = Array.isArray(body.content) ? body.content : [];
823
+ for (let index = 0;index < content.length; index += 1) {
824
+ const block = content[index];
825
+ if (!isRecord(block))
826
+ continue;
827
+ if (block.type === "text") {
828
+ const text = typeof block.text === "string" ? block.text : "";
829
+ const blockId = `text-${index}`;
830
+ yield { type: "text-start", id: blockId };
831
+ if (text.length > 0)
832
+ yield { type: "text-delta", id: blockId, delta: text };
833
+ yield { type: "text-end", id: blockId };
834
+ continue;
835
+ }
836
+ if (block.type === "thinking") {
837
+ const thinking = typeof block.thinking === "string" ? block.thinking : "";
838
+ const signature = typeof block.signature === "string" ? block.signature : "";
839
+ const blockId = `reasoning-${index}`;
840
+ yield { type: "reasoning-start", id: blockId };
841
+ if (thinking.length > 0)
842
+ yield { type: "reasoning-delta", id: blockId, delta: thinking };
843
+ yield {
844
+ type: "reasoning-end",
845
+ id: blockId,
846
+ provider: {
847
+ schemaVersion: 1,
848
+ provider: "anthropic",
849
+ data: { type: "thinking", signature }
850
+ }
851
+ };
852
+ continue;
853
+ }
854
+ if (block.type === "redacted_thinking") {
855
+ const data = typeof block.data === "string" ? block.data : "";
856
+ const blockId = `reasoning-${index}`;
857
+ yield { type: "reasoning-start", id: blockId };
858
+ yield {
859
+ type: "reasoning-end",
860
+ id: blockId,
861
+ provider: {
862
+ schemaVersion: 1,
863
+ provider: "anthropic",
864
+ data: { type: "redacted_thinking", data }
865
+ }
866
+ };
867
+ continue;
868
+ }
869
+ if (block.type === "tool_use") {
870
+ const toolCallId = nonEmptyString2(block.id) ?? crypto.randomUUID();
871
+ const toolName = typeof block.name === "string" ? block.name : "";
872
+ const input = block.input !== undefined ? JSON.stringify(block.input) : "{}";
873
+ yield { type: "tool-input-start", toolCallId, toolName };
874
+ yield { type: "tool-input-delta", toolCallId, delta: input };
875
+ yield { type: "tool-input-end", toolCallId };
876
+ yield { type: "tool-call", toolCallId, toolName, input };
877
+ }
878
+ }
879
+ const rawFinishReason = nonEmptyString2(body.stop_reason);
880
+ yield {
881
+ type: "finish",
882
+ finishReason: mapAnthropicStopReason(rawFinishReason),
883
+ ...rawFinishReason !== undefined && { rawFinishReason },
884
+ usage: normalizeAnthropicUsage(body.usage),
885
+ ...(id !== undefined || modelId !== undefined || responseTimestamp !== undefined) && {
886
+ response: {
887
+ ...id !== undefined && { id },
888
+ ...modelId !== undefined && { modelId },
889
+ ...responseTimestamp !== undefined && { timestamp: responseTimestamp }
890
+ }
891
+ }
892
+ };
893
+ }
894
+
895
+ // src/agent-runtime/anthropic-sse.ts
896
+ function mergeRawUsage(target, incoming) {
897
+ if (!isRecord(incoming))
898
+ return;
899
+ for (const [key, value] of Object.entries(incoming)) {
900
+ if (value === undefined || value === null)
901
+ continue;
902
+ if (isRecord(value)) {
903
+ const existing = target[key];
904
+ const mergedNested = isRecord(existing) ? { ...existing } : {};
905
+ for (const [nestedKey, nestedValue] of Object.entries(value)) {
906
+ if (nestedValue !== undefined && nestedValue !== null) {
907
+ mergedNested[nestedKey] = nestedValue;
908
+ }
909
+ }
910
+ target[key] = mergedNested;
911
+ continue;
912
+ }
913
+ target[key] = value;
914
+ }
915
+ }
916
+ function readBlockIndex(payload) {
917
+ return typeof payload.index === "number" && Number.isInteger(payload.index) ? payload.index : undefined;
918
+ }
919
+ function* handleBlockStart(payload, state) {
920
+ const index = readBlockIndex(payload);
921
+ const block = recordField(payload, "content_block");
922
+ if (index === undefined || !block)
923
+ return;
924
+ if (block.type === "text") {
925
+ const id = `text-${index}`;
926
+ state.blocks.set(index, { kind: "text", id });
927
+ yield { type: "text-start", id };
928
+ if (typeof block.text === "string" && block.text.length > 0) {
929
+ yield { type: "text-delta", id, delta: block.text };
930
+ }
931
+ return;
932
+ }
933
+ if (block.type === "thinking") {
934
+ const id = `reasoning-${index}`;
935
+ const initialText = typeof block.thinking === "string" ? block.thinking : "";
936
+ const initialSignature = typeof block.signature === "string" ? block.signature : "";
937
+ state.blocks.set(index, {
938
+ kind: "thinking",
939
+ id,
940
+ text: initialText,
941
+ signature: initialSignature
942
+ });
943
+ yield { type: "reasoning-start", id };
944
+ if (initialText.length > 0)
945
+ yield { type: "reasoning-delta", id, delta: initialText };
946
+ return;
947
+ }
948
+ if (block.type === "redacted_thinking") {
949
+ const id = `reasoning-${index}`;
950
+ const data = typeof block.data === "string" ? block.data : "";
951
+ state.blocks.set(index, { kind: "redacted_thinking", id });
952
+ yield { type: "reasoning-start", id };
953
+ yield {
954
+ type: "reasoning-end",
955
+ id,
956
+ provider: {
957
+ schemaVersion: 1,
958
+ provider: "anthropic",
959
+ data: { type: "redacted_thinking", data }
960
+ }
961
+ };
962
+ return;
963
+ }
964
+ if (block.type === "tool_use") {
965
+ const toolCallId = nonEmptyString2(block.id) ?? crypto.randomUUID();
966
+ const toolName = typeof block.name === "string" ? block.name : "";
967
+ const initialInput = isRecord(block.input) && Object.keys(block.input).length > 0 ? JSON.stringify(block.input) : undefined;
968
+ state.blocks.set(index, {
969
+ kind: "tool_use",
970
+ callId: toolCallId,
971
+ toolName,
972
+ ...initialInput !== undefined && { initialInput },
973
+ partialJson: ""
974
+ });
975
+ yield { type: "tool-input-start", toolCallId, toolName };
976
+ if (initialInput !== undefined) {
977
+ yield { type: "tool-input-delta", toolCallId, delta: initialInput };
978
+ }
979
+ }
980
+ }
981
+ function* handleBlockDelta(payload, state) {
982
+ const index = readBlockIndex(payload);
983
+ const delta = recordField(payload, "delta");
984
+ if (index === undefined || !delta)
985
+ return;
986
+ const active = state.blocks.get(index);
987
+ if (!active)
988
+ return;
989
+ if (delta.type === "text_delta" && active.kind === "text") {
990
+ if (typeof delta.text === "string" && delta.text.length > 0) {
991
+ yield { type: "text-delta", id: active.id, delta: delta.text };
992
+ }
993
+ return;
994
+ }
995
+ if (delta.type === "thinking_delta" && active.kind === "thinking") {
996
+ if (typeof delta.thinking === "string" && delta.thinking.length > 0) {
997
+ active.text += delta.thinking;
998
+ yield { type: "reasoning-delta", id: active.id, delta: delta.thinking };
999
+ }
1000
+ return;
1001
+ }
1002
+ if (delta.type === "signature_delta" && active.kind === "thinking") {
1003
+ if (typeof delta.signature === "string") {
1004
+ active.signature += delta.signature;
1005
+ }
1006
+ return;
1007
+ }
1008
+ if (delta.type === "input_json_delta" && active.kind === "tool_use") {
1009
+ if (typeof delta.partial_json === "string") {
1010
+ active.partialJson += delta.partial_json;
1011
+ if (delta.partial_json.length > 0) {
1012
+ yield {
1013
+ type: "tool-input-delta",
1014
+ toolCallId: active.callId,
1015
+ delta: delta.partial_json
1016
+ };
1017
+ }
1018
+ }
1019
+ }
1020
+ }
1021
+ function* handleBlockStop(payload, state) {
1022
+ const index = readBlockIndex(payload);
1023
+ if (index === undefined)
1024
+ return;
1025
+ const active = state.blocks.get(index);
1026
+ if (!active)
1027
+ return;
1028
+ state.blocks.delete(index);
1029
+ if (active.kind === "text") {
1030
+ yield { type: "text-end", id: active.id };
1031
+ return;
1032
+ }
1033
+ if (active.kind === "thinking") {
1034
+ yield {
1035
+ type: "reasoning-end",
1036
+ id: active.id,
1037
+ provider: {
1038
+ schemaVersion: 1,
1039
+ provider: "anthropic",
1040
+ data: { type: "thinking", signature: active.signature }
1041
+ }
1042
+ };
1043
+ return;
1044
+ }
1045
+ if (active.kind === "tool_use") {
1046
+ yield { type: "tool-input-end", toolCallId: active.callId };
1047
+ const input = active.partialJson.trim().length > 0 ? active.partialJson : active.initialInput ?? "{}";
1048
+ yield {
1049
+ type: "tool-call",
1050
+ toolCallId: active.callId,
1051
+ toolName: active.toolName,
1052
+ input
1053
+ };
1054
+ }
1055
+ }
1056
+ async function* decodeAnthropicSseStream(body, signal, responseTimestamp) {
1057
+ const state = {
1058
+ blocks: new Map,
1059
+ mergedUsage: {},
1060
+ sawMessageStop: false,
1061
+ sawError: false
1062
+ };
1063
+ try {
1064
+ for await (const frame of parseSseStream(body, { provider: "anthropic", signal })) {
1065
+ if (frame.kind === "ping") {
1066
+ yield { type: "ping" };
1067
+ continue;
1068
+ }
1069
+ const payload = parseSseJson("anthropic", frame.data);
1070
+ if (!isRecord(payload))
1071
+ continue;
1072
+ const eventType = nonEmptyString2(payload.type) ?? frame.event;
1073
+ if (eventType === "ping") {
1074
+ yield { type: "ping" };
1075
+ continue;
1076
+ }
1077
+ if (eventType === "message_start") {
1078
+ const message = recordField(payload, "message");
1079
+ const id = nonEmptyString2(message?.id);
1080
+ const modelId = nonEmptyString2(message?.model);
1081
+ if (id !== undefined)
1082
+ state.responseId = id;
1083
+ if (modelId !== undefined)
1084
+ state.responseModelId = modelId;
1085
+ mergeRawUsage(state.mergedUsage, message?.usage);
1086
+ if (state.responseId !== undefined || state.responseModelId !== undefined || responseTimestamp !== undefined) {
1087
+ yield {
1088
+ type: "response-metadata",
1089
+ ...state.responseId !== undefined && { id: state.responseId },
1090
+ ...state.responseModelId !== undefined && { modelId: state.responseModelId },
1091
+ ...responseTimestamp !== undefined && { timestamp: responseTimestamp }
1092
+ };
1093
+ }
1094
+ continue;
1095
+ }
1096
+ if (eventType === "content_block_start") {
1097
+ yield* handleBlockStart(payload, state);
1098
+ continue;
1099
+ }
1100
+ if (eventType === "content_block_delta") {
1101
+ yield* handleBlockDelta(payload, state);
1102
+ continue;
1103
+ }
1104
+ if (eventType === "content_block_stop") {
1105
+ yield* handleBlockStop(payload, state);
1106
+ continue;
1107
+ }
1108
+ if (eventType === "message_delta") {
1109
+ const delta = recordField(payload, "delta");
1110
+ const stopReason = nonEmptyString2(delta?.stop_reason);
1111
+ if (stopReason !== undefined)
1112
+ state.rawStopReason = stopReason;
1113
+ mergeRawUsage(state.mergedUsage, payload.usage);
1114
+ continue;
1115
+ }
1116
+ if (eventType === "message_stop") {
1117
+ state.sawMessageStop = true;
1118
+ for (const active of state.blocks.values()) {
1119
+ if (active.kind === "text") {
1120
+ yield { type: "text-end", id: active.id };
1121
+ } else if (active.kind === "thinking") {
1122
+ yield {
1123
+ type: "reasoning-end",
1124
+ id: active.id,
1125
+ provider: {
1126
+ schemaVersion: 1,
1127
+ provider: "anthropic",
1128
+ data: { type: "thinking", signature: active.signature }
1129
+ }
1130
+ };
1131
+ }
1132
+ }
1133
+ state.blocks.clear();
1134
+ yield {
1135
+ type: "finish",
1136
+ finishReason: mapAnthropicStopReason(state.rawStopReason),
1137
+ ...state.rawStopReason !== undefined && { rawFinishReason: state.rawStopReason },
1138
+ usage: normalizeAnthropicUsage(state.mergedUsage),
1139
+ ...(state.responseId !== undefined || state.responseModelId !== undefined || responseTimestamp !== undefined) && {
1140
+ response: {
1141
+ ...state.responseId !== undefined && { id: state.responseId },
1142
+ ...state.responseModelId !== undefined && { modelId: state.responseModelId },
1143
+ ...responseTimestamp !== undefined && { timestamp: responseTimestamp }
1144
+ }
1145
+ }
1146
+ };
1147
+ continue;
1148
+ }
1149
+ if (eventType === "error") {
1150
+ state.sawError = true;
1151
+ state.blocks.clear();
1152
+ const parsed = parseAnthropicError(payload, frame.data, 200);
1153
+ yield {
1154
+ type: "error",
1155
+ error: new AgentProviderError2(redactProviderFailureText(parsed.message), {
1156
+ provider: "anthropic",
1157
+ ...parsed.code !== undefined && { code: parsed.code },
1158
+ ...parsed.errorType !== undefined && { errorType: parsed.errorType },
1159
+ ...parsed.failureReason !== undefined && { failureReason: parsed.failureReason }
1160
+ })
1161
+ };
1162
+ return;
1163
+ }
1164
+ }
1165
+ } catch (error) {
1166
+ if (signal?.aborted || error instanceof AgentProviderError2 || error instanceof AgentProviderStreamCutError2 || error instanceof Error && error.name === "AbortError") {
1167
+ throw error;
1168
+ }
1169
+ throw new AgentProviderStreamCutError2("Anthropic SSE stream disconnected before message_stop", { provider: "anthropic", cause: error });
1170
+ }
1171
+ if (!state.sawMessageStop && !state.sawError) {
1172
+ throw new AgentProviderStreamCutError2("Anthropic SSE stream ended before message_stop or error", { provider: "anthropic" });
1173
+ }
1174
+ }
1175
+
1176
+ // src/agent-runtime/anthropic.ts
1177
+ var DEFAULT_ANTHROPIC_BASE_URL = "https://api.anthropic.com/v1";
1178
+ var DEFAULT_ANTHROPIC_VERSION = "2023-06-01";
1179
+ function splitHeaderBetas(headers) {
1180
+ if (!headers)
1181
+ return { headerBetas: [] };
1182
+ const headerBetas = [];
1183
+ const remaining = {};
1184
+ for (const [key, value] of Object.entries(headers)) {
1185
+ if (key.toLowerCase() === "anthropic-beta") {
1186
+ headerBetas.push(value);
1187
+ } else {
1188
+ remaining[key] = value;
1189
+ }
1190
+ }
1191
+ return {
1192
+ headerBetas,
1193
+ ...Object.keys(remaining).length > 0 && { extraHeaders: remaining }
1194
+ };
1195
+ }
1196
+ function anthropicProvider(settings, access = {}) {
1197
+ assertNoExtraBodyClash("anthropicProvider", settings.extraBody, ANTHROPIC_OWNED_BODY_KEYS);
1198
+ const defaultPolicy = access.defaultPolicy !== undefined ? resolveAnthropicPolicy(access.defaultPolicy) : undefined;
1199
+ const endpoint = joinProviderUrl(settings.baseUrl ?? DEFAULT_ANTHROPIC_BASE_URL, "/messages");
1200
+ const apiVersion = settings.apiVersion ?? DEFAULT_ANTHROPIC_VERSION;
1201
+ const { headerBetas, extraHeaders } = splitHeaderBetas(settings.headers);
1202
+ return {
1203
+ create(modelId) {
1204
+ return {
1205
+ provider: "anthropic",
1206
+ modelId,
1207
+ async stream(request, signal) {
1208
+ const resolved = access.resolvePolicy?.(modelId);
1209
+ const basePolicy = resolved !== undefined ? resolveAnthropicPolicy(resolved) : defaultPolicy ?? {};
1210
+ const built = buildAnthropicRequest({
1211
+ modelId,
1212
+ request,
1213
+ policy: basePolicy,
1214
+ ...settings.extraBody !== undefined && { extraBody: settings.extraBody }
1215
+ });
1216
+ const { headerBetas: requestBetas, extraHeaders: requestExtraHeaders } = splitHeaderBetas(request.headers);
1217
+ const betaHeader = mergeAnthropicBetas(headerBetas, requestBetas, settings.betas, built.policyBetas, built.autoBetas);
1218
+ const apiKey = await resolveProviderApiKey(settings.apiKey);
1219
+ const headers = {
1220
+ "x-api-key": apiKey,
1221
+ "anthropic-version": apiVersion,
1222
+ ...betaHeader !== undefined && { "anthropic-beta": betaHeader }
1223
+ };
1224
+ const response = await postProviderStream({
1225
+ provider: "anthropic",
1226
+ url: endpoint,
1227
+ headers,
1228
+ ...extraHeaders !== undefined && { extraHeaders },
1229
+ ...requestExtraHeaders !== undefined && { requestHeaders: requestExtraHeaders },
1230
+ body: built.body,
1231
+ signal,
1232
+ ...settings.fetch !== undefined && { fetcher: settings.fetch },
1233
+ parseError: parseAnthropicError
1234
+ });
1235
+ const responseTimestamp = parseHttpDateHeader(response.headers);
1236
+ if (response.kind === "json") {
1237
+ return {
1238
+ ...responseTimestamp !== undefined && {
1239
+ response: { timestamp: responseTimestamp }
1240
+ },
1241
+ parts: decodeAnthropicJsonResponse(response.json, responseTimestamp)
1242
+ };
1243
+ }
1244
+ return {
1245
+ ...responseTimestamp !== undefined && {
1246
+ response: { timestamp: responseTimestamp }
1247
+ },
1248
+ parts: decodeAnthropicSseStream(response.body, signal, responseTimestamp)
1249
+ };
1250
+ }
1251
+ };
1252
+ }
1253
+ };
1254
+ }
1255
+ export {
1256
+ AnthropicRequestPolicySchema,
1257
+ AnthropicThinkingPolicySchema,
1258
+ anthropicProvider,
1259
+ normalizeAnthropicUsage
1260
+ };