@llumiverse/drivers 1.3.0 → 1.4.0-dev.20260629.090753Z

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 (409) hide show
  1. package/lib/{types/adobe → adobe}/firefly.d.ts +2 -1
  2. package/lib/adobe/firefly.d.ts.map +1 -0
  3. package/lib/adobe/firefly.js +120 -0
  4. package/lib/adobe/firefly.js.map +1 -0
  5. package/lib/{types/anthropic → anthropic}/index.d.ts +4 -2
  6. package/lib/anthropic/index.d.ts.map +1 -0
  7. package/lib/{esm/anthropic → anthropic}/index.js +19 -8
  8. package/lib/anthropic/index.js.map +1 -0
  9. package/lib/{types/azure → azure}/azure_foundry.d.ts +9 -7
  10. package/lib/azure/azure_foundry.d.ts.map +1 -0
  11. package/lib/{esm/azure → azure}/azure_foundry.js +138 -124
  12. package/lib/azure/azure_foundry.js.map +1 -0
  13. package/lib/{types/bedrock → bedrock}/converse.d.ts +2 -2
  14. package/lib/bedrock/converse.d.ts.map +1 -0
  15. package/lib/{esm/bedrock → bedrock}/converse.js +86 -79
  16. package/lib/bedrock/converse.js.map +1 -0
  17. package/lib/bedrock/embeddings.d.ts +4 -0
  18. package/lib/bedrock/embeddings.d.ts.map +1 -0
  19. package/lib/bedrock/embeddings.js +359 -0
  20. package/lib/bedrock/embeddings.js.map +1 -0
  21. package/lib/{types/bedrock → bedrock}/index.d.ts +33 -9
  22. package/lib/bedrock/index.d.ts.map +1 -0
  23. package/lib/{esm/bedrock → bedrock}/index.js +599 -324
  24. package/lib/bedrock/index.js.map +1 -0
  25. package/lib/{types/bedrock → bedrock}/nova-image-payload.d.ts +5 -5
  26. package/lib/bedrock/nova-image-payload.d.ts.map +1 -0
  27. package/lib/{esm/bedrock → bedrock}/nova-image-payload.js +31 -36
  28. package/lib/bedrock/nova-image-payload.js.map +1 -0
  29. package/lib/{types/bedrock → bedrock}/payloads.d.ts +1 -1
  30. package/lib/bedrock/payloads.d.ts.map +1 -0
  31. package/lib/{cjs/bedrock → bedrock}/payloads.js.map +1 -1
  32. package/lib/{types/bedrock → bedrock}/s3.d.ts +3 -2
  33. package/lib/bedrock/s3.d.ts.map +1 -0
  34. package/lib/{esm/bedrock → bedrock}/s3.js +27 -23
  35. package/lib/bedrock/s3.js.map +1 -0
  36. package/lib/{types/bedrock → bedrock}/twelvelabs.d.ts +8 -9
  37. package/lib/bedrock/twelvelabs.d.ts.map +1 -0
  38. package/lib/{esm/bedrock → bedrock}/twelvelabs.js +17 -21
  39. package/lib/bedrock/twelvelabs.js.map +1 -0
  40. package/lib/{types/groq → groq}/index.d.ts +6 -4
  41. package/lib/groq/index.d.ts.map +1 -0
  42. package/lib/{esm/groq → groq}/index.js +72 -39
  43. package/lib/groq/index.js.map +1 -0
  44. package/lib/{types/huggingface_ie.d.ts → huggingface_ie.d.ts} +4 -3
  45. package/lib/huggingface_ie.d.ts.map +1 -0
  46. package/lib/{esm/huggingface_ie.js → huggingface_ie.js} +32 -31
  47. package/lib/huggingface_ie.js.map +1 -0
  48. package/lib/index.d.ts +16 -0
  49. package/lib/index.d.ts.map +1 -0
  50. package/lib/index.js +16 -0
  51. package/lib/index.js.map +1 -0
  52. package/lib/{types/mistral → mistral}/index.d.ts +6 -5
  53. package/lib/mistral/index.d.ts.map +1 -0
  54. package/lib/{esm/mistral → mistral}/index.js +66 -38
  55. package/lib/mistral/index.js.map +1 -0
  56. package/lib/{types/mistral → mistral}/types.d.ts +1 -0
  57. package/lib/mistral/types.d.ts.map +1 -0
  58. package/lib/mistral/types.js.map +1 -0
  59. package/lib/{types/openai → openai}/azure_openai.d.ts +5 -4
  60. package/lib/openai/azure_openai.d.ts.map +1 -0
  61. package/lib/{esm/openai → openai}/azure_openai.js +17 -14
  62. package/lib/openai/azure_openai.js.map +1 -0
  63. package/lib/openai/drivers.d.ts +4 -0
  64. package/lib/openai/drivers.d.ts.map +1 -0
  65. package/lib/openai/drivers.js +9 -0
  66. package/lib/openai/drivers.js.map +1 -0
  67. package/lib/{types/openai → openai}/index.d.ts +16 -5
  68. package/lib/openai/index.d.ts.map +1 -0
  69. package/lib/{esm/openai → openai}/index.js +242 -141
  70. package/lib/openai/index.js.map +1 -0
  71. package/lib/{types/openai → openai}/openai.d.ts +3 -3
  72. package/lib/openai/openai.d.ts.map +1 -0
  73. package/lib/openai/openai.js +15 -0
  74. package/lib/openai/openai.js.map +1 -0
  75. package/lib/{types/openai → openai}/openai_compatible.d.ts +3 -3
  76. package/lib/openai/openai_compatible.d.ts.map +1 -0
  77. package/lib/{esm/openai → openai}/openai_compatible.js +14 -11
  78. package/lib/openai/openai_compatible.js.map +1 -0
  79. package/lib/{types/openai → openai}/openai_format.d.ts +3 -2
  80. package/lib/openai/openai_format.d.ts.map +1 -0
  81. package/lib/{esm/openai → openai}/openai_format.js +66 -24
  82. package/lib/openai/openai_format.js.map +1 -0
  83. package/lib/{types/replicate.d.ts → replicate.d.ts} +3 -2
  84. package/lib/replicate.d.ts.map +1 -0
  85. package/lib/{esm/replicate.js → replicate.js} +59 -49
  86. package/lib/replicate.js.map +1 -0
  87. package/lib/{types/shared → shared}/claude-messages.d.ts +25 -1
  88. package/lib/shared/claude-messages.d.ts.map +1 -0
  89. package/lib/{esm/shared → shared}/claude-messages.js +212 -59
  90. package/lib/shared/claude-messages.js.map +1 -0
  91. package/lib/{types/shared → shared}/claude-thinking.d.ts +1 -1
  92. package/lib/shared/claude-thinking.d.ts.map +1 -0
  93. package/lib/{esm/shared → shared}/claude-thinking.js +5 -7
  94. package/lib/shared/claude-thinking.js.map +1 -0
  95. package/lib/shared/debug-prompt.d.ts +5 -0
  96. package/lib/shared/debug-prompt.d.ts.map +1 -0
  97. package/lib/shared/debug-prompt.js +25 -0
  98. package/lib/shared/debug-prompt.js.map +1 -0
  99. package/lib/{types/test-driver → test-driver}/TestErrorCompletionStream.d.ts +1 -1
  100. package/lib/test-driver/TestErrorCompletionStream.d.ts.map +1 -0
  101. package/lib/test-driver/TestErrorCompletionStream.js +16 -0
  102. package/lib/test-driver/TestErrorCompletionStream.js.map +1 -0
  103. package/lib/{types/test-driver → test-driver}/TestValidationErrorCompletionStream.d.ts +1 -1
  104. package/lib/test-driver/TestValidationErrorCompletionStream.d.ts.map +1 -0
  105. package/lib/{esm/test-driver → test-driver}/TestValidationErrorCompletionStream.js +4 -4
  106. package/lib/test-driver/TestValidationErrorCompletionStream.js.map +1 -0
  107. package/lib/{types/test-driver → test-driver}/index.d.ts +3 -3
  108. package/lib/test-driver/index.d.ts.map +1 -0
  109. package/lib/{esm/test-driver → test-driver}/index.js +20 -20
  110. package/lib/test-driver/index.js.map +1 -0
  111. package/lib/{types/test-driver → test-driver}/utils.d.ts +1 -1
  112. package/lib/test-driver/utils.d.ts.map +1 -0
  113. package/lib/{esm/test-driver → test-driver}/utils.js +4 -4
  114. package/lib/test-driver/utils.js.map +1 -0
  115. package/lib/togetherai/index.d.ts +15 -0
  116. package/lib/togetherai/index.d.ts.map +1 -0
  117. package/lib/togetherai/index.js +54 -0
  118. package/lib/togetherai/index.js.map +1 -0
  119. package/lib/{types/togetherai → togetherai}/interfaces.d.ts +1 -1
  120. package/lib/togetherai/interfaces.d.ts.map +1 -0
  121. package/lib/{cjs/watsonx → togetherai}/interfaces.js.map +1 -1
  122. package/lib/vertexai/debug.d.ts +2 -0
  123. package/lib/vertexai/debug.d.ts.map +1 -0
  124. package/lib/{esm/vertexai → vertexai}/debug.js +1 -1
  125. package/lib/vertexai/debug.js.map +1 -0
  126. package/lib/vertexai/embeddings/embed-legacy-multimodal.d.ts +9 -0
  127. package/lib/vertexai/embeddings/embed-legacy-multimodal.d.ts.map +1 -0
  128. package/lib/vertexai/embeddings/embed-legacy-multimodal.js +141 -0
  129. package/lib/vertexai/embeddings/embed-legacy-multimodal.js.map +1 -0
  130. package/lib/vertexai/embeddings/embed.d.ts +13 -0
  131. package/lib/vertexai/embeddings/embed.d.ts.map +1 -0
  132. package/lib/vertexai/embeddings/embed.js +162 -0
  133. package/lib/vertexai/embeddings/embed.js.map +1 -0
  134. package/lib/vertexai/embeddings/source-utils.d.ts +9 -0
  135. package/lib/vertexai/embeddings/source-utils.d.ts.map +1 -0
  136. package/lib/vertexai/embeddings/source-utils.js +18 -0
  137. package/lib/vertexai/embeddings/source-utils.js.map +1 -0
  138. package/lib/{types/vertexai → vertexai}/index.d.ts +31 -19
  139. package/lib/vertexai/index.d.ts.map +1 -0
  140. package/lib/{esm/vertexai → vertexai}/index.js +221 -159
  141. package/lib/vertexai/index.js.map +1 -0
  142. package/lib/{types/vertexai → vertexai}/models/claude.d.ts +4 -4
  143. package/lib/vertexai/models/claude.d.ts.map +1 -0
  144. package/lib/{esm/vertexai → vertexai}/models/claude.js +17 -20
  145. package/lib/vertexai/models/claude.js.map +1 -0
  146. package/lib/{types/vertexai → vertexai}/models/gemini.d.ts +28 -4
  147. package/lib/vertexai/models/gemini.d.ts.map +1 -0
  148. package/lib/{esm/vertexai → vertexai}/models/gemini.js +238 -116
  149. package/lib/vertexai/models/gemini.js.map +1 -0
  150. package/lib/{types/vertexai → vertexai}/models/imagen.d.ts +12 -11
  151. package/lib/vertexai/models/imagen.d.ts.map +1 -0
  152. package/lib/{esm/vertexai → vertexai}/models/imagen.js +74 -47
  153. package/lib/vertexai/models/imagen.js.map +1 -0
  154. package/lib/{types/vertexai → vertexai}/models/llama.d.ts +4 -4
  155. package/lib/vertexai/models/llama.d.ts.map +1 -0
  156. package/lib/{esm/vertexai → vertexai}/models/llama.js +37 -33
  157. package/lib/vertexai/models/llama.js.map +1 -0
  158. package/lib/vertexai/models.d.ts +21 -0
  159. package/lib/vertexai/models.d.ts.map +1 -0
  160. package/lib/{esm/vertexai → vertexai}/models.js +12 -9
  161. package/lib/vertexai/models.js.map +1 -0
  162. package/lib/{types/watsonx → watsonx}/index.d.ts +4 -3
  163. package/lib/watsonx/index.d.ts.map +1 -0
  164. package/lib/{esm/watsonx → watsonx}/index.js +69 -41
  165. package/lib/watsonx/index.js.map +1 -0
  166. package/lib/watsonx/interfaces.d.ts.map +1 -0
  167. package/lib/{esm/watsonx → watsonx}/interfaces.js.map +1 -1
  168. package/lib/{types/xai → xai}/index.d.ts +4 -4
  169. package/lib/xai/index.d.ts.map +1 -0
  170. package/lib/{esm/xai → xai}/index.js +22 -18
  171. package/lib/xai/index.js.map +1 -0
  172. package/package.json +87 -36
  173. package/src/adobe/firefly.ts +60 -43
  174. package/src/anthropic/index.ts +37 -20
  175. package/src/azure/azure_foundry.ts +210 -165
  176. package/src/bedrock/converse.ts +124 -101
  177. package/src/bedrock/embeddings.test.ts +508 -0
  178. package/src/bedrock/embeddings.ts +517 -0
  179. package/src/bedrock/error-handling.test.ts +62 -14
  180. package/src/bedrock/index.ts +832 -466
  181. package/src/bedrock/nova-image-payload.ts +98 -91
  182. package/src/bedrock/payloads.ts +9 -9
  183. package/src/bedrock/s3.test.ts +39 -0
  184. package/src/bedrock/s3.ts +57 -38
  185. package/src/bedrock/streaming-tool-use.test.ts +86 -69
  186. package/src/bedrock/twelvelabs.ts +35 -32
  187. package/src/groq/index.ts +143 -60
  188. package/src/http-timeout-wiring.test.ts +263 -0
  189. package/src/huggingface_ie.ts +45 -61
  190. package/src/index.ts +15 -16
  191. package/src/mistral/index.ts +97 -48
  192. package/src/mistral/types.ts +13 -12
  193. package/src/openai/azure_openai.ts +35 -27
  194. package/src/openai/drivers.ts +9 -0
  195. package/src/openai/error-handling.test.ts +308 -113
  196. package/src/openai/index.ts +378 -248
  197. package/src/openai/openai.ts +6 -13
  198. package/src/openai/openai_compatible.ts +35 -25
  199. package/src/openai/openai_format.ts +90 -40
  200. package/src/replicate.ts +108 -86
  201. package/src/shared/claude-messages.test.ts +37 -0
  202. package/src/shared/claude-messages.ts +303 -97
  203. package/src/shared/claude-thinking.ts +6 -12
  204. package/src/shared/debug-prompt.ts +28 -0
  205. package/src/test-driver/TestErrorCompletionStream.ts +9 -10
  206. package/src/test-driver/TestValidationErrorCompletionStream.ts +9 -10
  207. package/src/test-driver/index.ts +43 -27
  208. package/src/test-driver/utils.ts +8 -8
  209. package/src/togetherai/index.test.ts +32 -0
  210. package/src/togetherai/index.ts +64 -128
  211. package/src/togetherai/interfaces.ts +5 -5
  212. package/src/vertexai/debug.ts +3 -3
  213. package/src/vertexai/embeddings/embed-legacy-multimodal.ts +211 -0
  214. package/src/vertexai/embeddings/embed.ts +206 -0
  215. package/src/vertexai/embeddings/embeddings.test.ts +497 -0
  216. package/src/vertexai/embeddings/source-utils.test.ts +54 -0
  217. package/src/vertexai/embeddings/source-utils.ts +29 -0
  218. package/src/vertexai/index.ts +331 -244
  219. package/src/vertexai/models/claude-error-handling.test.ts +83 -36
  220. package/src/vertexai/models/claude-streaming-spacing.test.ts +82 -75
  221. package/src/vertexai/models/claude.ts +55 -40
  222. package/src/vertexai/models/gemini-conversation-mutation.test.ts +65 -23
  223. package/src/vertexai/models/gemini-error-handling.test.ts +126 -10
  224. package/src/vertexai/models/gemini-orphaned-tool-result.test.ts +118 -0
  225. package/src/vertexai/models/gemini.ts +349 -172
  226. package/src/vertexai/models/imagen.ts +164 -100
  227. package/src/vertexai/models/llama.ts +70 -48
  228. package/src/vertexai/models.ts +36 -16
  229. package/src/watsonx/index.ts +101 -65
  230. package/src/watsonx/interfaces.ts +10 -15
  231. package/src/xai/index.ts +30 -30
  232. package/lib/cjs/adobe/firefly.js +0 -120
  233. package/lib/cjs/adobe/firefly.js.map +0 -1
  234. package/lib/cjs/anthropic/index.js +0 -64
  235. package/lib/cjs/anthropic/index.js.map +0 -1
  236. package/lib/cjs/azure/azure_foundry.js +0 -432
  237. package/lib/cjs/azure/azure_foundry.js.map +0 -1
  238. package/lib/cjs/bedrock/converse.js +0 -359
  239. package/lib/cjs/bedrock/converse.js.map +0 -1
  240. package/lib/cjs/bedrock/index.js +0 -1508
  241. package/lib/cjs/bedrock/index.js.map +0 -1
  242. package/lib/cjs/bedrock/nova-image-payload.js +0 -207
  243. package/lib/cjs/bedrock/nova-image-payload.js.map +0 -1
  244. package/lib/cjs/bedrock/payloads.js +0 -3
  245. package/lib/cjs/bedrock/s3.js +0 -107
  246. package/lib/cjs/bedrock/s3.js.map +0 -1
  247. package/lib/cjs/bedrock/twelvelabs.js +0 -87
  248. package/lib/cjs/bedrock/twelvelabs.js.map +0 -1
  249. package/lib/cjs/groq/index.js +0 -328
  250. package/lib/cjs/groq/index.js.map +0 -1
  251. package/lib/cjs/huggingface_ie.js +0 -201
  252. package/lib/cjs/huggingface_ie.js.map +0 -1
  253. package/lib/cjs/index.js +0 -32
  254. package/lib/cjs/index.js.map +0 -1
  255. package/lib/cjs/mistral/index.js +0 -176
  256. package/lib/cjs/mistral/index.js.map +0 -1
  257. package/lib/cjs/mistral/types.js +0 -83
  258. package/lib/cjs/mistral/types.js.map +0 -1
  259. package/lib/cjs/openai/azure_openai.js +0 -72
  260. package/lib/cjs/openai/azure_openai.js.map +0 -1
  261. package/lib/cjs/openai/index.js +0 -1135
  262. package/lib/cjs/openai/index.js.map +0 -1
  263. package/lib/cjs/openai/openai.js +0 -21
  264. package/lib/cjs/openai/openai.js.map +0 -1
  265. package/lib/cjs/openai/openai_compatible.js +0 -63
  266. package/lib/cjs/openai/openai_compatible.js.map +0 -1
  267. package/lib/cjs/openai/openai_format.js +0 -131
  268. package/lib/cjs/openai/openai_format.js.map +0 -1
  269. package/lib/cjs/package.json +0 -3
  270. package/lib/cjs/replicate.js +0 -275
  271. package/lib/cjs/replicate.js.map +0 -1
  272. package/lib/cjs/shared/claude-messages.js +0 -737
  273. package/lib/cjs/shared/claude-messages.js.map +0 -1
  274. package/lib/cjs/shared/claude-thinking.js +0 -60
  275. package/lib/cjs/shared/claude-thinking.js.map +0 -1
  276. package/lib/cjs/test-driver/TestErrorCompletionStream.js +0 -20
  277. package/lib/cjs/test-driver/TestErrorCompletionStream.js.map +0 -1
  278. package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js +0 -24
  279. package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js.map +0 -1
  280. package/lib/cjs/test-driver/index.js +0 -109
  281. package/lib/cjs/test-driver/index.js.map +0 -1
  282. package/lib/cjs/test-driver/utils.js +0 -30
  283. package/lib/cjs/test-driver/utils.js.map +0 -1
  284. package/lib/cjs/togetherai/index.js +0 -126
  285. package/lib/cjs/togetherai/index.js.map +0 -1
  286. package/lib/cjs/togetherai/interfaces.js +0 -3
  287. package/lib/cjs/togetherai/interfaces.js.map +0 -1
  288. package/lib/cjs/vertexai/debug.js +0 -12
  289. package/lib/cjs/vertexai/debug.js.map +0 -1
  290. package/lib/cjs/vertexai/embeddings/embeddings-image.js +0 -27
  291. package/lib/cjs/vertexai/embeddings/embeddings-image.js.map +0 -1
  292. package/lib/cjs/vertexai/embeddings/embeddings-text.js +0 -23
  293. package/lib/cjs/vertexai/embeddings/embeddings-text.js.map +0 -1
  294. package/lib/cjs/vertexai/index.js +0 -644
  295. package/lib/cjs/vertexai/index.js.map +0 -1
  296. package/lib/cjs/vertexai/models/claude.js +0 -72
  297. package/lib/cjs/vertexai/models/claude.js.map +0 -1
  298. package/lib/cjs/vertexai/models/gemini.js +0 -864
  299. package/lib/cjs/vertexai/models/gemini.js.map +0 -1
  300. package/lib/cjs/vertexai/models/imagen.js +0 -303
  301. package/lib/cjs/vertexai/models/imagen.js.map +0 -1
  302. package/lib/cjs/vertexai/models/llama.js +0 -183
  303. package/lib/cjs/vertexai/models/llama.js.map +0 -1
  304. package/lib/cjs/vertexai/models.js +0 -35
  305. package/lib/cjs/vertexai/models.js.map +0 -1
  306. package/lib/cjs/watsonx/index.js +0 -161
  307. package/lib/cjs/watsonx/index.js.map +0 -1
  308. package/lib/cjs/watsonx/interfaces.js +0 -3
  309. package/lib/cjs/xai/index.js +0 -65
  310. package/lib/cjs/xai/index.js.map +0 -1
  311. package/lib/esm/adobe/firefly.js +0 -116
  312. package/lib/esm/adobe/firefly.js.map +0 -1
  313. package/lib/esm/anthropic/index.js.map +0 -1
  314. package/lib/esm/azure/azure_foundry.js.map +0 -1
  315. package/lib/esm/bedrock/converse.js.map +0 -1
  316. package/lib/esm/bedrock/index.js.map +0 -1
  317. package/lib/esm/bedrock/nova-image-payload.js.map +0 -1
  318. package/lib/esm/bedrock/payloads.js.map +0 -1
  319. package/lib/esm/bedrock/s3.js.map +0 -1
  320. package/lib/esm/bedrock/twelvelabs.js.map +0 -1
  321. package/lib/esm/groq/index.js.map +0 -1
  322. package/lib/esm/huggingface_ie.js.map +0 -1
  323. package/lib/esm/index.js +0 -16
  324. package/lib/esm/index.js.map +0 -1
  325. package/lib/esm/mistral/index.js.map +0 -1
  326. package/lib/esm/mistral/types.js.map +0 -1
  327. package/lib/esm/openai/azure_openai.js.map +0 -1
  328. package/lib/esm/openai/index.js.map +0 -1
  329. package/lib/esm/openai/openai.js +0 -14
  330. package/lib/esm/openai/openai.js.map +0 -1
  331. package/lib/esm/openai/openai_compatible.js.map +0 -1
  332. package/lib/esm/openai/openai_format.js.map +0 -1
  333. package/lib/esm/replicate.js.map +0 -1
  334. package/lib/esm/shared/claude-messages.js.map +0 -1
  335. package/lib/esm/shared/claude-thinking.js.map +0 -1
  336. package/lib/esm/test-driver/TestErrorCompletionStream.js +0 -16
  337. package/lib/esm/test-driver/TestErrorCompletionStream.js.map +0 -1
  338. package/lib/esm/test-driver/TestValidationErrorCompletionStream.js.map +0 -1
  339. package/lib/esm/test-driver/index.js.map +0 -1
  340. package/lib/esm/test-driver/utils.js.map +0 -1
  341. package/lib/esm/togetherai/index.js +0 -122
  342. package/lib/esm/togetherai/index.js.map +0 -1
  343. package/lib/esm/togetherai/interfaces.js.map +0 -1
  344. package/lib/esm/vertexai/debug.js.map +0 -1
  345. package/lib/esm/vertexai/embeddings/embeddings-image.js +0 -24
  346. package/lib/esm/vertexai/embeddings/embeddings-image.js.map +0 -1
  347. package/lib/esm/vertexai/embeddings/embeddings-text.js +0 -20
  348. package/lib/esm/vertexai/embeddings/embeddings-text.js.map +0 -1
  349. package/lib/esm/vertexai/index.js.map +0 -1
  350. package/lib/esm/vertexai/models/claude.js.map +0 -1
  351. package/lib/esm/vertexai/models/gemini.js.map +0 -1
  352. package/lib/esm/vertexai/models/imagen.js.map +0 -1
  353. package/lib/esm/vertexai/models/llama.js.map +0 -1
  354. package/lib/esm/vertexai/models.js.map +0 -1
  355. package/lib/esm/watsonx/index.js.map +0 -1
  356. package/lib/esm/xai/index.js.map +0 -1
  357. package/lib/types/adobe/firefly.d.ts.map +0 -1
  358. package/lib/types/anthropic/index.d.ts.map +0 -1
  359. package/lib/types/azure/azure_foundry.d.ts.map +0 -1
  360. package/lib/types/bedrock/converse.d.ts.map +0 -1
  361. package/lib/types/bedrock/index.d.ts.map +0 -1
  362. package/lib/types/bedrock/nova-image-payload.d.ts.map +0 -1
  363. package/lib/types/bedrock/payloads.d.ts.map +0 -1
  364. package/lib/types/bedrock/s3.d.ts.map +0 -1
  365. package/lib/types/bedrock/twelvelabs.d.ts.map +0 -1
  366. package/lib/types/groq/index.d.ts.map +0 -1
  367. package/lib/types/huggingface_ie.d.ts.map +0 -1
  368. package/lib/types/index.d.ts +0 -16
  369. package/lib/types/index.d.ts.map +0 -1
  370. package/lib/types/mistral/index.d.ts.map +0 -1
  371. package/lib/types/mistral/types.d.ts.map +0 -1
  372. package/lib/types/openai/azure_openai.d.ts.map +0 -1
  373. package/lib/types/openai/index.d.ts.map +0 -1
  374. package/lib/types/openai/openai.d.ts.map +0 -1
  375. package/lib/types/openai/openai_compatible.d.ts.map +0 -1
  376. package/lib/types/openai/openai_format.d.ts.map +0 -1
  377. package/lib/types/replicate.d.ts.map +0 -1
  378. package/lib/types/shared/claude-messages.d.ts.map +0 -1
  379. package/lib/types/shared/claude-thinking.d.ts.map +0 -1
  380. package/lib/types/test-driver/TestErrorCompletionStream.d.ts.map +0 -1
  381. package/lib/types/test-driver/TestValidationErrorCompletionStream.d.ts.map +0 -1
  382. package/lib/types/test-driver/index.d.ts.map +0 -1
  383. package/lib/types/test-driver/utils.d.ts.map +0 -1
  384. package/lib/types/togetherai/index.d.ts +0 -23
  385. package/lib/types/togetherai/index.d.ts.map +0 -1
  386. package/lib/types/togetherai/interfaces.d.ts.map +0 -1
  387. package/lib/types/vertexai/debug.d.ts +0 -2
  388. package/lib/types/vertexai/debug.d.ts.map +0 -1
  389. package/lib/types/vertexai/embeddings/embeddings-image.d.ts +0 -11
  390. package/lib/types/vertexai/embeddings/embeddings-image.d.ts.map +0 -1
  391. package/lib/types/vertexai/embeddings/embeddings-text.d.ts +0 -10
  392. package/lib/types/vertexai/embeddings/embeddings-text.d.ts.map +0 -1
  393. package/lib/types/vertexai/index.d.ts.map +0 -1
  394. package/lib/types/vertexai/models/claude.d.ts.map +0 -1
  395. package/lib/types/vertexai/models/gemini.d.ts.map +0 -1
  396. package/lib/types/vertexai/models/imagen.d.ts.map +0 -1
  397. package/lib/types/vertexai/models/llama.d.ts.map +0 -1
  398. package/lib/types/vertexai/models.d.ts +0 -20
  399. package/lib/types/vertexai/models.d.ts.map +0 -1
  400. package/lib/types/watsonx/index.d.ts.map +0 -1
  401. package/lib/types/watsonx/interfaces.d.ts.map +0 -1
  402. package/lib/types/xai/index.d.ts.map +0 -1
  403. package/src/vertexai/embeddings/embeddings-image.ts +0 -52
  404. package/src/vertexai/embeddings/embeddings-text.ts +0 -54
  405. /package/lib/{esm/bedrock → bedrock}/payloads.js +0 -0
  406. /package/lib/{esm/mistral → mistral}/types.js +0 -0
  407. /package/lib/{esm/togetherai → togetherai}/interfaces.js +0 -0
  408. /package/lib/{types/watsonx → watsonx}/interfaces.d.ts +0 -0
  409. /package/lib/{esm/watsonx → watsonx}/interfaces.js +0 -0
@@ -34,10 +34,7 @@ import type {
34
34
  ToolResultBlockParam,
35
35
  } from '@anthropic-ai/sdk/resources/index.js';
36
36
  import type { MessageStreamParams } from '@anthropic-ai/sdk/resources/index.mjs';
37
- import type {
38
- MessageCreateParamsBase,
39
- RawMessageStreamEvent,
40
- } from '@anthropic-ai/sdk/resources/messages.js';
37
+ import type { MessageCreateParamsBase, RawMessageStreamEvent } from '@anthropic-ai/sdk/resources/messages.js';
41
38
  import type AnthropicVertex from '@anthropic-ai/vertex-sdk';
42
39
  import { getClaudeMaxTokensLimit } from '@llumiverse/common';
43
40
  import {
@@ -63,16 +60,62 @@ import {
63
60
  } from '@llumiverse/core';
64
61
  import { asyncMap } from '@llumiverse/core/async';
65
62
  import { resolveClaudeThinking } from './claude-thinking.js';
63
+ import { truncateBinaryForDebug } from './debug-prompt.js';
66
64
 
67
65
  // ============================================================================
68
66
  // Types
69
67
  // ============================================================================
70
68
 
69
+ // Conversation text-trim policy (applied only when the caller requests text
70
+ // trimming via stripTextMaxTokens). Keep the last N messages fully intact (the
71
+ // active working set) and cap large text in older messages at the token ceiling
72
+ // below — so long agent conversations don't balloon context.
73
+ const KEEP_RECENT_MESSAGES = 12;
74
+ const OLD_MESSAGE_TEXT_MAX_TOKENS = 2000;
75
+
76
+ interface WarnLogger {
77
+ warn: (data: Record<string, unknown>, message: string) => void;
78
+ }
79
+
71
80
  export interface ClaudePrompt {
72
81
  messages: MessageParam[];
73
82
  system?: TextBlockParam[];
74
83
  }
75
84
 
85
+ function formatClaudeContentBlockForDebug(block: ContentBlockParam): ContentBlockParam {
86
+ if (block.type === 'image' && block.source.type === 'base64') {
87
+ return {
88
+ ...block,
89
+ source: {
90
+ ...block.source,
91
+ data: truncateBinaryForDebug(block.source.data),
92
+ },
93
+ };
94
+ }
95
+ if (block.type === 'document' && block.source.type === 'base64') {
96
+ return {
97
+ ...block,
98
+ source: {
99
+ ...block.source,
100
+ data: truncateBinaryForDebug(block.source.data),
101
+ },
102
+ };
103
+ }
104
+ return block;
105
+ }
106
+
107
+ export function formatClaudeDebugPrompt(prompt: ClaudePrompt): ClaudePrompt {
108
+ return {
109
+ ...prompt,
110
+ messages: prompt.messages.map((message) => ({
111
+ ...message,
112
+ content: Array.isArray(message.content)
113
+ ? message.content.map(formatClaudeContentBlockForDebug)
114
+ : message.content,
115
+ })),
116
+ };
117
+ }
118
+
76
119
  export interface AnthropicUsageLike {
77
120
  input_tokens: number;
78
121
  output_tokens: number;
@@ -103,6 +146,25 @@ interface RequestOptions {
103
146
  }
104
147
 
105
148
  type ClaudeTool = NonNullable<MessageCreateParamsBase['tools']>[number];
149
+ type ClaudeMessageStream = AsyncIterable<RawMessageStreamEvent> & {
150
+ finalMessage(): Promise<Message>;
151
+ };
152
+ type ClaudeMessagesStreamClient = {
153
+ messages: {
154
+ stream(body: MessageStreamParams, options?: RequestOptions): ClaudeMessageStream;
155
+ };
156
+ };
157
+
158
+ function streamClaudeMessages(
159
+ client: Anthropic | AnthropicVertex,
160
+ payload: MessageStreamParams,
161
+ requestOptions: RequestOptions | undefined,
162
+ ): Promise<ClaudeMessageStream> {
163
+ // AnthropicVertex intentionally wraps the Anthropic Messages API, but it depends on its
164
+ // own @anthropic-ai/sdk copy. Cast at the boundary so the implementation can call the
165
+ // shared runtime-compatible stream API without TS trying to call a union of SDK versions.
166
+ return Promise.resolve((client as unknown as ClaudeMessagesStreamClient).messages.stream(payload, requestOptions));
167
+ }
106
168
 
107
169
  // ============================================================================
108
170
  // Token usage
@@ -128,9 +190,12 @@ export function anthropicUsageToTokenUsage(usage: AnthropicUsageLike): Execution
128
190
  export function claudeFinishReason(reason: string | undefined): string | undefined {
129
191
  if (!reason) return undefined;
130
192
  switch (reason) {
131
- case 'end_turn': return 'stop';
132
- case 'max_tokens': return 'length';
133
- default: return reason; // stop_sequence, tool_use
193
+ case 'end_turn':
194
+ return 'stop';
195
+ case 'max_tokens':
196
+ return 'length';
197
+ default:
198
+ return reason; // stop_sequence, tool_use
134
199
  }
135
200
  }
136
201
 
@@ -198,10 +263,11 @@ export function claudeMaxTokens(option: StatelessExecutionOptions): number {
198
263
  // File / multimodal block helpers
199
264
  // ============================================================================
200
265
 
201
- async function collectFileBlocks(segment: PromptSegment, restrictedTypes: true): Promise<Array<TextBlockParam | ImageBlockParam>>;
202
- async function collectFileBlocks(segment: PromptSegment, restrictedTypes?: false): Promise<ContentBlockParam[]>;
203
- async function collectFileBlocks(segment: PromptSegment, restrictedTypes = false): Promise<ContentBlockParam[]> {
204
- const contentBlocks: ContentBlockParam[] = [];
266
+ async function collectFileBlocks(
267
+ segment: PromptSegment,
268
+ logger?: WarnLogger,
269
+ ): Promise<Array<TextBlockParam | ImageBlockParam | DocumentBlockParam>> {
270
+ const contentBlocks: Array<TextBlockParam | ImageBlockParam | DocumentBlockParam> = [];
205
271
 
206
272
  for (const file of segment.files || []) {
207
273
  if (file.mime_type?.startsWith('image/')) {
@@ -218,28 +284,34 @@ async function collectFileBlocks(segment: PromptSegment, restrictedTypes = false
218
284
  media_type: mimeType,
219
285
  },
220
286
  } satisfies ImageBlockParam);
221
- } else if (!restrictedTypes) {
222
- if (file.mime_type === 'application/pdf') {
223
- contentBlocks.push({
224
- title: file.name,
225
- type: 'document',
226
- source: {
227
- type: 'base64',
228
- data: await readStreamAsBase64(await file.getStream()),
229
- media_type: 'application/pdf',
230
- },
231
- } satisfies DocumentBlockParam);
232
- } else if (file.mime_type?.startsWith('text/')) {
233
- contentBlocks.push({
234
- title: file.name,
235
- type: 'document',
236
- source: {
237
- type: 'text',
238
- data: await readStreamAsString(await file.getStream()),
239
- media_type: 'text/plain',
240
- },
241
- } satisfies DocumentBlockParam);
242
- }
287
+ } else if (file.mime_type?.startsWith('video/')) {
288
+ logger?.warn(
289
+ {
290
+ file_name: file.name,
291
+ mime_type: file.mime_type,
292
+ },
293
+ '[Claude] Skipping unsupported video attachment',
294
+ );
295
+ } else if (file.mime_type === 'application/pdf') {
296
+ contentBlocks.push({
297
+ title: file.name,
298
+ type: 'document',
299
+ source: {
300
+ type: 'base64',
301
+ data: await readStreamAsBase64(await file.getStream()),
302
+ media_type: 'application/pdf',
303
+ },
304
+ } satisfies DocumentBlockParam);
305
+ } else if (file.mime_type?.startsWith('text/')) {
306
+ contentBlocks.push({
307
+ title: file.name,
308
+ type: 'document',
309
+ source: {
310
+ type: 'text',
311
+ data: await readStreamAsString(await file.getStream()),
312
+ media_type: 'text/plain',
313
+ },
314
+ } satisfies DocumentBlockParam);
243
315
  }
244
316
  }
245
317
 
@@ -250,15 +322,20 @@ async function collectFileBlocks(segment: PromptSegment, restrictedTypes = false
250
322
  // Prompt formatting (PromptSegment[] → ClaudePrompt)
251
323
  // ============================================================================
252
324
 
253
- export async function formatClaudePrompt(segments: PromptSegment[], options: ExecutionOptions): Promise<ClaudePrompt> {
325
+ export async function formatClaudePrompt(
326
+ segments: PromptSegment[],
327
+ options: ExecutionOptions,
328
+ logger?: WarnLogger,
329
+ ): Promise<ClaudePrompt> {
254
330
  let system: TextBlockParam[] | undefined = segments
255
331
  .filter((s) => s.role === PromptRole.system)
256
332
  .map((s) => ({ text: s.content, type: 'text' as const }));
257
333
 
258
334
  if (options.result_schema) {
259
- const schemaText = options.tools && options.tools.length > 0
260
- ? 'When not calling tools, the answer must be a JSON object using the following JSON Schema:\n' + JSON.stringify(options.result_schema)
261
- : 'The answer must be a JSON object using the following JSON Schema:\n' + JSON.stringify(options.result_schema);
335
+ const schemaText =
336
+ options.tools && options.tools.length > 0
337
+ ? `When not calling tools, the answer must be a JSON object using the following JSON Schema:\n${JSON.stringify(options.result_schema)}`
338
+ : `The answer must be a JSON object using the following JSON Schema:\n${JSON.stringify(options.result_schema)}`;
262
339
  system.push({ text: schemaText, type: 'text' as const });
263
340
  }
264
341
 
@@ -272,25 +349,27 @@ export async function formatClaudePrompt(segments: PromptSegment[], options: Exe
272
349
  if (!segment.tool_use_id) {
273
350
  throw new Error('Tool prompt segment must have a tool use ID');
274
351
  }
275
- const contentBlocks: Array<TextBlockParam | ImageBlockParam> = [];
352
+ const contentBlocks: Array<TextBlockParam | ImageBlockParam | DocumentBlockParam> = [];
276
353
  if (segment.content) {
277
354
  contentBlocks.push({ type: 'text', text: segment.content } satisfies TextBlockParam);
278
355
  }
279
- contentBlocks.push(...(await collectFileBlocks(segment, true)));
356
+ contentBlocks.push(...(await collectFileBlocks(segment, logger)));
280
357
  messages.push({
281
358
  role: 'user',
282
- content: [{
283
- type: 'tool_result',
284
- tool_use_id: segment.tool_use_id,
285
- content: contentBlocks,
286
- } satisfies ToolResultBlockParam],
359
+ content: [
360
+ {
361
+ type: 'tool_result',
362
+ tool_use_id: segment.tool_use_id,
363
+ content: contentBlocks,
364
+ } satisfies ToolResultBlockParam,
365
+ ],
287
366
  });
288
367
  } else {
289
368
  const contentBlocks: ContentBlockParam[] = [];
290
369
  if (segment.content) {
291
370
  contentBlocks.push({ type: 'text', text: segment.content } satisfies TextBlockParam);
292
371
  }
293
- contentBlocks.push(...(await collectFileBlocks(segment, false)));
372
+ contentBlocks.push(...(await collectFileBlocks(segment, logger)));
294
373
  if (contentBlocks.length === 0) continue;
295
374
 
296
375
  const messageParam: MessageParam = {
@@ -326,8 +405,8 @@ export function createPromptFromResponse(response: Message): ClaudePrompt {
326
405
  export function mergeConsecutiveUserMessages(messages: MessageParam[]): MessageParam[] {
327
406
  if (messages.length === 0) return [];
328
407
 
329
- const needsMerging = messages.some((msg, i) =>
330
- i < messages.length - 1 && msg.role === 'user' && messages[i + 1].role === 'user'
408
+ const needsMerging = messages.some(
409
+ (msg, i) => i < messages.length - 1 && msg.role === 'user' && messages[i + 1].role === 'user',
331
410
  );
332
411
  if (!needsMerging) return messages;
333
412
 
@@ -383,7 +462,7 @@ export function fixOrphanedToolUse(messages: MessageParam[]): MessageParam[] {
383
462
  if (current.role === 'assistant' && Array.isArray(current.content)) {
384
463
  const toolUseBlocks = current.content.filter(
385
464
  (block): block is ContentBlockParam & { type: 'tool_use'; id: string; name: string } =>
386
- block.type === 'tool_use'
465
+ block.type === 'tool_use',
387
466
  );
388
467
 
389
468
  if (toolUseBlocks.length > 0) {
@@ -393,7 +472,7 @@ export function fixOrphanedToolUse(messages: MessageParam[]): MessageParam[] {
393
472
  const toolResultIds = new Set(
394
473
  nextMessage.content
395
474
  .filter((block): block is ToolResultBlockParam => block.type === 'tool_result')
396
- .map((block) => block.tool_use_id)
475
+ .map((block) => block.tool_use_id),
397
476
  );
398
477
  const orphaned = toolUseBlocks.filter((block) => !toolResultIds.has(block.id));
399
478
  if (orphaned.length > 0) {
@@ -410,9 +489,10 @@ export function fixOrphanedToolUse(messages: MessageParam[]): MessageParam[] {
410
489
  tool_use_id: block.id,
411
490
  content: `[Tool interrupted: The user stopped the operation before "${block.name}" could execute.]`,
412
491
  }));
413
- const textContent: TextBlockParam = typeof nextMessage.content === 'string'
414
- ? { type: 'text', text: nextMessage.content }
415
- : { type: 'text', text: '' };
492
+ const textContent: TextBlockParam =
493
+ typeof nextMessage.content === 'string'
494
+ ? { type: 'text', text: nextMessage.content }
495
+ : { type: 'text', text: '' };
416
496
  messages[i + 1] = { role: 'user', content: [...syntheticResults, textContent] };
417
497
  }
418
498
  }
@@ -421,7 +501,62 @@ export function fixOrphanedToolUse(messages: MessageParam[]): MessageParam[] {
421
501
  return result;
422
502
  }
423
503
 
424
- export function updateClaudeConversation(conversation: ClaudePrompt | undefined | null, prompt: ClaudePrompt): ClaudePrompt {
504
+ /**
505
+ * Drop tool_result blocks whose tool_use_id has no matching tool_use in the
506
+ * immediately preceding assistant message. This is the mirror of
507
+ * {@link fixOrphanedToolUse}: that function synthesizes results for tool_uses
508
+ * left unanswered (e.g. a cancelled run); this one removes results left dangling
509
+ * after their tool_use was dropped (e.g. by conversation compaction/trimming, or
510
+ * a parallel tool batch whose results were split across messages that cannot be
511
+ * re-paired).
512
+ *
513
+ * Without this, the Anthropic / Vertex-Anthropic API rejects the request with a
514
+ * non-retryable 400: "unexpected `tool_use_id` found in `tool_result` blocks ...
515
+ * Each `tool_result` block must have a corresponding `tool_use` block in the
516
+ * previous message." — which terminates the conversation.
517
+ *
518
+ * Must run AFTER mergeConsecutiveUserMessages so that parallel tool results which
519
+ * were split across separate user messages are first combined into the single
520
+ * user turn that follows the assistant tool_use message (otherwise valid results
521
+ * whose "previous message" is another user message would be wrongly dropped).
522
+ */
523
+ export function fixOrphanedToolResults(messages: MessageParam[]): MessageParam[] {
524
+ if (messages.length === 0) return messages;
525
+ const result: MessageParam[] = [];
526
+ for (let i = 0; i < messages.length; i++) {
527
+ const message = messages[i];
528
+ if (message.role !== 'user' || !Array.isArray(message.content)) {
529
+ result.push(message);
530
+ continue;
531
+ }
532
+ const hasToolResult = message.content.some((block) => block.type === 'tool_result');
533
+ if (!hasToolResult) {
534
+ result.push(message);
535
+ continue;
536
+ }
537
+ // Collect the tool_use ids declared by the immediately preceding assistant message.
538
+ const prev = messages[i - 1];
539
+ const allowedIds = new Set<string>();
540
+ if (prev && prev.role === 'assistant' && Array.isArray(prev.content)) {
541
+ for (const block of prev.content) {
542
+ if (block.type === 'tool_use') allowedIds.add(block.id);
543
+ }
544
+ }
545
+ const filtered = message.content.filter((block) =>
546
+ block.type === 'tool_result' ? allowedIds.has(block.tool_use_id) : true,
547
+ );
548
+ // If every block was an orphaned tool_result, drop the message rather than
549
+ // emit an empty (and invalid) content array.
550
+ if (filtered.length === 0) continue;
551
+ result.push(filtered.length === message.content.length ? message : { ...message, content: filtered });
552
+ }
553
+ return result;
554
+ }
555
+
556
+ export function updateClaudeConversation(
557
+ conversation: ClaudePrompt | undefined | null,
558
+ prompt: ClaudePrompt,
559
+ ): ClaudePrompt {
425
560
  const baseSystemMessages = conversation?.system || [];
426
561
  const baseMessages = conversation?.messages || [];
427
562
  const system = baseSystemMessages.concat(prompt.system || []);
@@ -450,8 +585,12 @@ export function convertClaudeToolBlocksToText(messages: MessageParam[]): Message
450
585
  if (!Array.isArray(msg.content)) return msg;
451
586
  let hasToolBlocks = false;
452
587
  for (const block of msg.content) {
453
- if (typeof block === 'object' && block !== null && 'type' in block &&
454
- (block.type === 'tool_use' || block.type === 'tool_result')) {
588
+ if (
589
+ typeof block === 'object' &&
590
+ block !== null &&
591
+ 'type' in block &&
592
+ (block.type === 'tool_use' || block.type === 'tool_result')
593
+ ) {
455
594
  hasToolBlocks = true;
456
595
  break;
457
596
  }
@@ -460,22 +599,31 @@ export function convertClaudeToolBlocksToText(messages: MessageParam[]): Message
460
599
 
461
600
  const newContent: MessageParam['content'] = [];
462
601
  for (const block of msg.content) {
463
- if (typeof block === 'string') { newContent.push(block); continue; }
602
+ if (typeof block === 'string') {
603
+ newContent.push(block);
604
+ continue;
605
+ }
464
606
  if (block.type === 'tool_use') {
465
607
  const inputStr = block.input ? JSON.stringify(block.input) : '';
466
- const truncated = inputStr.length > 500 ? inputStr.substring(0, 500) + '...' : inputStr;
467
- (newContent as Array<{ type: 'text'; text: string }>).push({ type: 'text', text: `[Tool call: ${block.name}(${truncated})]` });
608
+ const truncated = inputStr.length > 500 ? `${inputStr.substring(0, 500)}...` : inputStr;
609
+ (newContent as Array<{ type: 'text'; text: string }>).push({
610
+ type: 'text',
611
+ text: `[Tool call: ${block.name}(${truncated})]`,
612
+ });
468
613
  } else if (block.type === 'tool_result') {
469
614
  let resultStr = 'No content';
470
615
  if (typeof block.content === 'string') {
471
- resultStr = block.content.length > 500 ? block.content.substring(0, 500) + '...' : block.content;
616
+ resultStr = block.content.length > 500 ? `${block.content.substring(0, 500)}...` : block.content;
472
617
  } else if (Array.isArray(block.content)) {
473
618
  const texts = block.content
474
619
  .filter((c): c is { type: 'text'; text: string } => c.type === 'text')
475
- .map((c) => (c.text.length > 500 ? c.text.substring(0, 500) + '...' : c.text));
620
+ .map((c) => (c.text.length > 500 ? `${c.text.substring(0, 500)}...` : c.text));
476
621
  resultStr = texts.join('\n') || 'No text content';
477
622
  }
478
- (newContent as Array<{ type: 'text'; text: string }>).push({ type: 'text', text: `[Tool result: ${resultStr}]` });
623
+ (newContent as Array<{ type: 'text'; text: string }>).push({
624
+ type: 'text',
625
+ text: `[Tool result: ${resultStr}]`,
626
+ });
479
627
  } else {
480
628
  newContent.push(block as ContentBlockParam);
481
629
  }
@@ -509,7 +657,7 @@ function stripClaudeCacheControlFromSystem(system?: TextBlockParam[]): TextBlock
509
657
  }
510
658
 
511
659
  function stripClaudeCacheControlFromTools(
512
- tools?: MessageCreateParamsBase['tools']
660
+ tools?: MessageCreateParamsBase['tools'],
513
661
  ): MessageCreateParamsBase['tools'] | undefined {
514
662
  if (!tools) return undefined;
515
663
  return tools.map((tool) => {
@@ -527,24 +675,33 @@ function stripClaudeCacheControlFromTools(
527
675
 
528
676
  export function getClaudePayload(
529
677
  options: ExecutionOptions,
530
- prompt: ClaudePrompt
678
+ prompt: ClaudePrompt,
531
679
  ): { payload: MessageCreateParamsBase; requestOptions: RequestOptions | undefined } {
532
680
  const modelName = options.model;
533
681
  const model_options = options.model_options as ClaudeBaseOptions | undefined;
534
682
 
535
683
  let requestOptions: RequestOptions | undefined;
536
- if (modelName.includes('claude-3-7-sonnet') &&
537
- ((model_options?.max_tokens ?? 0) > 64000 || (model_options?.thinking_budget_tokens ?? 0) > 64000)) {
684
+ if (
685
+ modelName.includes('claude-3-7-sonnet') &&
686
+ ((model_options?.max_tokens ?? 0) > 64000 || (model_options?.thinking_budget_tokens ?? 0) > 64000)
687
+ ) {
538
688
  requestOptions = { headers: { 'anthropic-beta': 'output-128k-2025-02-19' } };
539
689
  }
540
690
 
541
- const fixedMessages = fixOrphanedToolUse(prompt.messages);
691
+ // Merge first so parallel tool results split across user messages are recombined
692
+ // into the single user turn after their assistant tool_use message; then fix both
693
+ // orphan directions (tool_use without result, and result without tool_use) so the
694
+ // request can never trip Anthropic/Vertex's tool_use/tool_result pairing validation.
695
+ const mergedMessages = mergeConsecutiveUserMessages(prompt.messages);
696
+ const fixedMessages = fixOrphanedToolResults(fixOrphanedToolUse(mergedMessages));
542
697
  let sanitizedMessages = sanitizeMessages(fixedMessages);
543
698
 
544
699
  if (options.tools) {
545
700
  for (const tool of options.tools) {
546
701
  if (tool.input_schema.type !== 'object') {
547
- throw new Error(`Tool "${tool.name}" has invalid input_schema.type: expected "object", got "${tool.input_schema.type}"`);
702
+ throw new Error(
703
+ `Tool "${tool.name}" has invalid input_schema.type: expected "object", got "${tool.input_schema.type}"`,
704
+ );
548
705
  }
549
706
  }
550
707
  }
@@ -566,7 +723,9 @@ export function getClaudePayload(
566
723
  const cacheControl = { type: 'ephemeral' as const, ...(cacheTtl && { ttl: cacheTtl }) };
567
724
 
568
725
  if (sanitizedSystem && sanitizedSystem.length > 0) {
569
- const lastBlock = sanitizedSystem[sanitizedSystem.length - 1] as TextBlockParam & { cache_control?: unknown };
726
+ const lastBlock = sanitizedSystem[sanitizedSystem.length - 1] as TextBlockParam & {
727
+ cache_control?: unknown;
728
+ };
570
729
  lastBlock.cache_control = cacheControl;
571
730
  }
572
731
  if (sanitizedTools && sanitizedTools.length > 0) {
@@ -577,15 +736,23 @@ export function getClaudePayload(
577
736
  const pivotMsg = sanitizedMessages[sanitizedMessages.length - 2];
578
737
  if (Array.isArray(pivotMsg.content) && pivotMsg.content.length > 0) {
579
738
  const lastBlock = pivotMsg.content[pivotMsg.content.length - 1];
580
- if (typeof lastBlock === 'object' && lastBlock !== null && 'type' in lastBlock &&
581
- lastBlock.type !== 'thinking' && lastBlock.type !== 'redacted_thinking') {
739
+ if (
740
+ typeof lastBlock === 'object' &&
741
+ lastBlock !== null &&
742
+ 'type' in lastBlock &&
743
+ lastBlock.type !== 'thinking' &&
744
+ lastBlock.type !== 'redacted_thinking'
745
+ ) {
582
746
  (lastBlock as TextBlockParam).cache_control = cacheControl;
583
747
  }
584
748
  }
585
749
  }
586
750
  }
587
751
 
588
- const { thinking, outputConfig, hasSamplingRestriction } = resolveClaudeThinking(modelName, model_options as Parameters<typeof resolveClaudeThinking>[1]);
752
+ const { thinking, outputConfig, hasSamplingRestriction } = resolveClaudeThinking(
753
+ modelName,
754
+ model_options as Parameters<typeof resolveClaudeThinking>[1],
755
+ );
589
756
 
590
757
  const payload: MessageCreateParamsBase = {
591
758
  messages: sanitizedMessages,
@@ -594,7 +761,11 @@ export function getClaudePayload(
594
761
  temperature: hasSamplingRestriction ? undefined : model_options?.temperature,
595
762
  model: modelName,
596
763
  max_tokens: claudeMaxTokens(options),
597
- top_p: hasSamplingRestriction ? undefined : (model_options?.temperature != null ? undefined : model_options?.top_p),
764
+ top_p: hasSamplingRestriction
765
+ ? undefined
766
+ : model_options?.temperature != null
767
+ ? undefined
768
+ : model_options?.top_p,
598
769
  top_k: hasSamplingRestriction ? undefined : model_options?.top_k,
599
770
  stop_sequences: model_options?.stop_sequence,
600
771
  thinking,
@@ -613,7 +784,7 @@ export function buildClaudeStreamingConversation(
613
784
  prompt: ClaudePrompt,
614
785
  result: unknown[],
615
786
  toolUse: unknown[] | undefined,
616
- options: ExecutionOptions
787
+ options: ExecutionOptions,
617
788
  ): ClaudePrompt {
618
789
  const completionResults = result as CompletionResult[];
619
790
  const text = completionResults
@@ -641,10 +812,18 @@ export function buildClaudeStreamingConversation(
641
812
 
642
813
  conversation = incrementConversationTurn(conversation) as ClaudePrompt;
643
814
  const currentTurn = getConversationMeta(conversation).turnNumber;
815
+ // When text trimming is requested, keep the agent's active working set (the
816
+ // most recent messages — the file it just read/wrote, latest diagnostics)
817
+ // fully intact, and aggressively shrink large text only in OLDER messages.
818
+ // Clamp the effective cap so a lax caller value (e.g. 10000) still bites on
819
+ // stale blocks; long agent conversations otherwise balloon context and
820
+ // trigger frequent expensive checkpoints.
821
+ const requestedTextMax = options.stripTextMaxTokens;
644
822
  const stripOptions = {
645
823
  keepForTurns: options.stripImagesAfterTurns ?? Infinity,
646
824
  currentTurn,
647
- textMaxTokens: options.stripTextMaxTokens,
825
+ textMaxTokens: requestedTextMax ? Math.min(requestedTextMax, OLD_MESSAGE_TEXT_MAX_TOKENS) : undefined,
826
+ keepRecentMessages: requestedTextMax ? KEEP_RECENT_MESSAGES : undefined,
648
827
  };
649
828
  let processed = stripBase64ImagesFromConversation(conversation, stripOptions);
650
829
  processed = truncateLargeTextInConversation(processed, stripOptions);
@@ -674,7 +853,8 @@ export async function executeClaudeCompletion(
674
853
 
675
854
  const { payload, requestOptions } = getClaudePayload(options, conversation);
676
855
 
677
- const result: Message = await client.messages.stream(payload, requestOptions).finalMessage();
856
+ const responseStream = await streamClaudeMessages(client, payload as MessageStreamParams, requestOptions);
857
+ const result = await responseStream.finalMessage();
678
858
 
679
859
  const includeThoughts = model_options?.include_thoughts ?? false;
680
860
  const text = collectAllTextContent(result.content, includeThoughts);
@@ -719,7 +899,7 @@ export async function streamClaudeCompletion(
719
899
  const { payload, requestOptions } = getClaudePayload(options, conversation);
720
900
  const streamingPayload: MessageStreamParams = { ...payload, stream: true };
721
901
 
722
- const response_stream = await client.messages.stream(streamingPayload, requestOptions);
902
+ const response_stream = await streamClaudeMessages(client, streamingPayload, requestOptions);
723
903
 
724
904
  let currentToolUse: { id: string; name: string; inputJson: string } | null = null;
725
905
  let pendingSpacing = false;
@@ -739,14 +919,20 @@ export async function streamClaudeCompletion(
739
919
  } satisfies CompletionChunkObject;
740
920
  case 'content_block_start':
741
921
  if (streamEvent.content_block.type === 'tool_use') {
742
- currentToolUse = { id: streamEvent.content_block.id, name: streamEvent.content_block.name, inputJson: '' };
922
+ currentToolUse = {
923
+ id: streamEvent.content_block.id,
924
+ name: streamEvent.content_block.name,
925
+ inputJson: '',
926
+ };
743
927
  return {
744
928
  result: [],
745
- tool_use: [{
746
- id: streamEvent.content_block.id,
747
- tool_name: streamEvent.content_block.name,
748
- tool_input: '' as unknown as JSONObject,
749
- }],
929
+ tool_use: [
930
+ {
931
+ id: streamEvent.content_block.id,
932
+ tool_name: streamEvent.content_block.name,
933
+ tool_input: '' as unknown as JSONObject,
934
+ },
935
+ ],
750
936
  } satisfies CompletionChunkObject;
751
937
  }
752
938
  if (streamEvent.content_block.type === 'redacted_thinking' && model_options?.include_thoughts) {
@@ -761,25 +947,31 @@ export async function streamClaudeCompletion(
761
947
  const prefix = pendingSpacing ? '\n\n' : '';
762
948
  pendingSpacing = false;
763
949
  return {
764
- result: streamEvent.delta.text ? [{ type: 'text', value: prefix + streamEvent.delta.text }] : [],
950
+ result: streamEvent.delta.text
951
+ ? [{ type: 'text', value: prefix + streamEvent.delta.text }]
952
+ : [],
765
953
  } satisfies CompletionChunkObject;
766
954
  }
767
955
  case 'input_json_delta':
768
956
  if (currentToolUse && streamEvent.delta.partial_json) {
769
957
  return {
770
958
  result: [],
771
- tool_use: [{
772
- id: currentToolUse.id,
773
- tool_name: '',
774
- tool_input: streamEvent.delta.partial_json as unknown as JSONObject,
775
- }],
959
+ tool_use: [
960
+ {
961
+ id: currentToolUse.id,
962
+ tool_name: '',
963
+ tool_input: streamEvent.delta.partial_json as unknown as JSONObject,
964
+ },
965
+ ],
776
966
  } satisfies CompletionChunkObject;
777
967
  }
778
968
  break;
779
969
  case 'thinking_delta':
780
970
  if (model_options?.include_thoughts) {
781
971
  return {
782
- result: streamEvent.delta.thinking ? [{ type: 'text', value: streamEvent.delta.thinking }] : [],
972
+ result: streamEvent.delta.thinking
973
+ ? [{ type: 'text', value: streamEvent.delta.thinking }]
974
+ : [],
783
975
  } satisfies CompletionChunkObject;
784
976
  }
785
977
  break;
@@ -813,7 +1005,14 @@ export function formatAnthropicLlumiverseError(error: unknown, context: Llumiver
813
1005
  // Client-side SDK error (e.g. "Streaming is required for operations that may take longer than 10 minutes").
814
1006
  // These are structural/configuration errors — retrying will never succeed.
815
1007
  const errorName = error.constructor?.name || 'AnthropicError';
816
- return new LlumiverseError(`[${context.provider}] ${error.message}`, false, context, error, undefined, errorName);
1008
+ return new LlumiverseError(
1009
+ `[${context.provider}] ${error.message}`,
1010
+ false,
1011
+ context,
1012
+ error,
1013
+ undefined,
1014
+ errorName,
1015
+ );
817
1016
  }
818
1017
  if (!(error instanceof APIError)) {
819
1018
  // Not an Anthropic error — rethrow for default handling
@@ -827,11 +1026,11 @@ export function formatAnthropicLlumiverseError(error: unknown, context: Llumiver
827
1026
 
828
1027
  if (apiError.error && typeof apiError.error === 'object') {
829
1028
  const nested = apiError.error as Record<string, unknown>;
830
- if (nested['error'] && typeof nested['error'] === 'object') {
831
- const innerError = nested['error'] as Record<string, unknown>;
832
- errorType = innerError['type'] as string | undefined;
833
- if (typeof innerError['message'] === 'string') {
834
- message = innerError['message'];
1029
+ if (nested.error && typeof nested.error === 'object') {
1030
+ const innerError = nested.error as Record<string, unknown>;
1031
+ errorType = innerError.type as string | undefined;
1032
+ if (typeof innerError.message === 'string') {
1033
+ message = innerError.message;
835
1034
  }
836
1035
  }
837
1036
  }
@@ -844,7 +1043,14 @@ export function formatAnthropicLlumiverseError(error: unknown, context: Llumiver
844
1043
  const retryable = isClaudeErrorRetryable(error, httpStatusCode, errorType, apiError.headers ?? undefined);
845
1044
  const errorName = error.constructor?.name || 'AnthropicError';
846
1045
 
847
- return new LlumiverseError(`[${context.provider}] ${userMessage}`, retryable, context, error, httpStatusCode, errorName);
1046
+ return new LlumiverseError(
1047
+ `[${context.provider}] ${userMessage}`,
1048
+ retryable,
1049
+ context,
1050
+ error,
1051
+ httpStatusCode,
1052
+ errorName,
1053
+ );
848
1054
  }
849
1055
 
850
1056
  export function isClaudeErrorRetryable(
@@ -866,7 +1072,7 @@ export function isClaudeErrorRetryable(
866
1072
  if (error instanceof AuthenticationError) return false;
867
1073
  if (error instanceof PermissionDeniedError) return false;
868
1074
  if (error instanceof NotFoundError) return false;
869
- if (error instanceof ConflictError) return true; // SDK retries 409 (lock timeouts)
1075
+ if (error instanceof ConflictError) return true; // SDK retries 409 (lock timeouts)
870
1076
  if (error instanceof UnprocessableEntityError) return false;
871
1077
  if (errorType === 'invalid_request_error') return false;
872
1078
  if (httpStatusCode !== undefined) {