@llumiverse/drivers 1.2.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 (403) 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/anthropic/index.d.ts +23 -0
  6. package/lib/anthropic/index.d.ts.map +1 -0
  7. package/lib/anthropic/index.js +68 -0
  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 +17 -5
  68. package/lib/openai/index.d.ts.map +1 -0
  69. package/lib/{esm/openai → openai}/index.js +254 -148
  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/shared/claude-messages.d.ts +99 -0
  88. package/lib/shared/claude-messages.d.ts.map +1 -0
  89. package/lib/shared/claude-messages.js +869 -0
  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/vertexai/models/claude.d.ts +16 -0
  143. package/lib/vertexai/models/claude.d.ts.map +1 -0
  144. package/lib/vertexai/models/claude.js +65 -0
  145. package/lib/vertexai/models/claude.js.map +1 -0
  146. package/lib/{types/vertexai → vertexai}/models/gemini.d.ts +29 -5
  147. package/lib/vertexai/models/gemini.d.ts.map +1 -0
  148. package/lib/{esm/vertexai → vertexai}/models/gemini.js +255 -127
  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 +88 -36
  173. package/src/adobe/firefly.ts +60 -43
  174. package/src/anthropic/index.ts +121 -0
  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 -15
  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 +389 -254
  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 +1085 -0
  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 +339 -253
  219. package/src/vertexai/models/claude-error-handling.test.ts +86 -39
  220. package/src/vertexai/models/claude-streaming-spacing.test.ts +82 -75
  221. package/src/vertexai/models/claude.ts +84 -1041
  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 +372 -182
  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/azure/azure_foundry.js +0 -432
  235. package/lib/cjs/azure/azure_foundry.js.map +0 -1
  236. package/lib/cjs/bedrock/converse.js +0 -359
  237. package/lib/cjs/bedrock/converse.js.map +0 -1
  238. package/lib/cjs/bedrock/index.js +0 -1508
  239. package/lib/cjs/bedrock/index.js.map +0 -1
  240. package/lib/cjs/bedrock/nova-image-payload.js +0 -207
  241. package/lib/cjs/bedrock/nova-image-payload.js.map +0 -1
  242. package/lib/cjs/bedrock/payloads.js +0 -3
  243. package/lib/cjs/bedrock/s3.js +0 -107
  244. package/lib/cjs/bedrock/s3.js.map +0 -1
  245. package/lib/cjs/bedrock/twelvelabs.js +0 -87
  246. package/lib/cjs/bedrock/twelvelabs.js.map +0 -1
  247. package/lib/cjs/groq/index.js +0 -328
  248. package/lib/cjs/groq/index.js.map +0 -1
  249. package/lib/cjs/huggingface_ie.js +0 -201
  250. package/lib/cjs/huggingface_ie.js.map +0 -1
  251. package/lib/cjs/index.js +0 -31
  252. package/lib/cjs/index.js.map +0 -1
  253. package/lib/cjs/mistral/index.js +0 -176
  254. package/lib/cjs/mistral/index.js.map +0 -1
  255. package/lib/cjs/mistral/types.js +0 -83
  256. package/lib/cjs/mistral/types.js.map +0 -1
  257. package/lib/cjs/openai/azure_openai.js +0 -72
  258. package/lib/cjs/openai/azure_openai.js.map +0 -1
  259. package/lib/cjs/openai/index.js +0 -1129
  260. package/lib/cjs/openai/index.js.map +0 -1
  261. package/lib/cjs/openai/openai.js +0 -21
  262. package/lib/cjs/openai/openai.js.map +0 -1
  263. package/lib/cjs/openai/openai_compatible.js +0 -63
  264. package/lib/cjs/openai/openai_compatible.js.map +0 -1
  265. package/lib/cjs/openai/openai_format.js +0 -131
  266. package/lib/cjs/openai/openai_format.js.map +0 -1
  267. package/lib/cjs/package.json +0 -3
  268. package/lib/cjs/replicate.js +0 -275
  269. package/lib/cjs/replicate.js.map +0 -1
  270. package/lib/cjs/shared/claude-thinking.js +0 -60
  271. package/lib/cjs/shared/claude-thinking.js.map +0 -1
  272. package/lib/cjs/test-driver/TestErrorCompletionStream.js +0 -20
  273. package/lib/cjs/test-driver/TestErrorCompletionStream.js.map +0 -1
  274. package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js +0 -24
  275. package/lib/cjs/test-driver/TestValidationErrorCompletionStream.js.map +0 -1
  276. package/lib/cjs/test-driver/index.js +0 -109
  277. package/lib/cjs/test-driver/index.js.map +0 -1
  278. package/lib/cjs/test-driver/utils.js +0 -30
  279. package/lib/cjs/test-driver/utils.js.map +0 -1
  280. package/lib/cjs/togetherai/index.js +0 -126
  281. package/lib/cjs/togetherai/index.js.map +0 -1
  282. package/lib/cjs/togetherai/interfaces.js +0 -3
  283. package/lib/cjs/togetherai/interfaces.js.map +0 -1
  284. package/lib/cjs/vertexai/debug.js +0 -12
  285. package/lib/cjs/vertexai/debug.js.map +0 -1
  286. package/lib/cjs/vertexai/embeddings/embeddings-image.js +0 -27
  287. package/lib/cjs/vertexai/embeddings/embeddings-image.js.map +0 -1
  288. package/lib/cjs/vertexai/embeddings/embeddings-text.js +0 -23
  289. package/lib/cjs/vertexai/embeddings/embeddings-text.js.map +0 -1
  290. package/lib/cjs/vertexai/index.js +0 -644
  291. package/lib/cjs/vertexai/index.js.map +0 -1
  292. package/lib/cjs/vertexai/models/claude.js +0 -917
  293. package/lib/cjs/vertexai/models/claude.js.map +0 -1
  294. package/lib/cjs/vertexai/models/gemini.js +0 -858
  295. package/lib/cjs/vertexai/models/gemini.js.map +0 -1
  296. package/lib/cjs/vertexai/models/imagen.js +0 -303
  297. package/lib/cjs/vertexai/models/imagen.js.map +0 -1
  298. package/lib/cjs/vertexai/models/llama.js +0 -183
  299. package/lib/cjs/vertexai/models/llama.js.map +0 -1
  300. package/lib/cjs/vertexai/models.js +0 -35
  301. package/lib/cjs/vertexai/models.js.map +0 -1
  302. package/lib/cjs/watsonx/index.js +0 -161
  303. package/lib/cjs/watsonx/index.js.map +0 -1
  304. package/lib/cjs/watsonx/interfaces.js +0 -3
  305. package/lib/cjs/xai/index.js +0 -65
  306. package/lib/cjs/xai/index.js.map +0 -1
  307. package/lib/esm/adobe/firefly.js +0 -116
  308. package/lib/esm/adobe/firefly.js.map +0 -1
  309. package/lib/esm/azure/azure_foundry.js.map +0 -1
  310. package/lib/esm/bedrock/converse.js.map +0 -1
  311. package/lib/esm/bedrock/index.js.map +0 -1
  312. package/lib/esm/bedrock/nova-image-payload.js.map +0 -1
  313. package/lib/esm/bedrock/payloads.js.map +0 -1
  314. package/lib/esm/bedrock/s3.js.map +0 -1
  315. package/lib/esm/bedrock/twelvelabs.js.map +0 -1
  316. package/lib/esm/groq/index.js.map +0 -1
  317. package/lib/esm/huggingface_ie.js.map +0 -1
  318. package/lib/esm/index.js +0 -15
  319. package/lib/esm/index.js.map +0 -1
  320. package/lib/esm/mistral/index.js.map +0 -1
  321. package/lib/esm/mistral/types.js.map +0 -1
  322. package/lib/esm/openai/azure_openai.js.map +0 -1
  323. package/lib/esm/openai/index.js.map +0 -1
  324. package/lib/esm/openai/openai.js +0 -14
  325. package/lib/esm/openai/openai.js.map +0 -1
  326. package/lib/esm/openai/openai_compatible.js.map +0 -1
  327. package/lib/esm/openai/openai_format.js.map +0 -1
  328. package/lib/esm/replicate.js.map +0 -1
  329. package/lib/esm/shared/claude-thinking.js.map +0 -1
  330. package/lib/esm/test-driver/TestErrorCompletionStream.js +0 -16
  331. package/lib/esm/test-driver/TestErrorCompletionStream.js.map +0 -1
  332. package/lib/esm/test-driver/TestValidationErrorCompletionStream.js.map +0 -1
  333. package/lib/esm/test-driver/index.js.map +0 -1
  334. package/lib/esm/test-driver/utils.js.map +0 -1
  335. package/lib/esm/togetherai/index.js +0 -122
  336. package/lib/esm/togetherai/index.js.map +0 -1
  337. package/lib/esm/togetherai/interfaces.js.map +0 -1
  338. package/lib/esm/vertexai/debug.js.map +0 -1
  339. package/lib/esm/vertexai/embeddings/embeddings-image.js +0 -24
  340. package/lib/esm/vertexai/embeddings/embeddings-image.js.map +0 -1
  341. package/lib/esm/vertexai/embeddings/embeddings-text.js +0 -20
  342. package/lib/esm/vertexai/embeddings/embeddings-text.js.map +0 -1
  343. package/lib/esm/vertexai/index.js.map +0 -1
  344. package/lib/esm/vertexai/models/claude.js +0 -906
  345. package/lib/esm/vertexai/models/claude.js.map +0 -1
  346. package/lib/esm/vertexai/models/gemini.js.map +0 -1
  347. package/lib/esm/vertexai/models/imagen.js.map +0 -1
  348. package/lib/esm/vertexai/models/llama.js.map +0 -1
  349. package/lib/esm/vertexai/models.js.map +0 -1
  350. package/lib/esm/watsonx/index.js.map +0 -1
  351. package/lib/esm/xai/index.js.map +0 -1
  352. package/lib/types/adobe/firefly.d.ts.map +0 -1
  353. package/lib/types/azure/azure_foundry.d.ts.map +0 -1
  354. package/lib/types/bedrock/converse.d.ts.map +0 -1
  355. package/lib/types/bedrock/index.d.ts.map +0 -1
  356. package/lib/types/bedrock/nova-image-payload.d.ts.map +0 -1
  357. package/lib/types/bedrock/payloads.d.ts.map +0 -1
  358. package/lib/types/bedrock/s3.d.ts.map +0 -1
  359. package/lib/types/bedrock/twelvelabs.d.ts.map +0 -1
  360. package/lib/types/groq/index.d.ts.map +0 -1
  361. package/lib/types/huggingface_ie.d.ts.map +0 -1
  362. package/lib/types/index.d.ts +0 -15
  363. package/lib/types/index.d.ts.map +0 -1
  364. package/lib/types/mistral/index.d.ts.map +0 -1
  365. package/lib/types/mistral/types.d.ts.map +0 -1
  366. package/lib/types/openai/azure_openai.d.ts.map +0 -1
  367. package/lib/types/openai/index.d.ts.map +0 -1
  368. package/lib/types/openai/openai.d.ts.map +0 -1
  369. package/lib/types/openai/openai_compatible.d.ts.map +0 -1
  370. package/lib/types/openai/openai_format.d.ts.map +0 -1
  371. package/lib/types/replicate.d.ts.map +0 -1
  372. package/lib/types/shared/claude-thinking.d.ts.map +0 -1
  373. package/lib/types/test-driver/TestErrorCompletionStream.d.ts.map +0 -1
  374. package/lib/types/test-driver/TestValidationErrorCompletionStream.d.ts.map +0 -1
  375. package/lib/types/test-driver/index.d.ts.map +0 -1
  376. package/lib/types/test-driver/utils.d.ts.map +0 -1
  377. package/lib/types/togetherai/index.d.ts +0 -23
  378. package/lib/types/togetherai/index.d.ts.map +0 -1
  379. package/lib/types/togetherai/interfaces.d.ts.map +0 -1
  380. package/lib/types/vertexai/debug.d.ts +0 -2
  381. package/lib/types/vertexai/debug.d.ts.map +0 -1
  382. package/lib/types/vertexai/embeddings/embeddings-image.d.ts +0 -11
  383. package/lib/types/vertexai/embeddings/embeddings-image.d.ts.map +0 -1
  384. package/lib/types/vertexai/embeddings/embeddings-text.d.ts +0 -10
  385. package/lib/types/vertexai/embeddings/embeddings-text.d.ts.map +0 -1
  386. package/lib/types/vertexai/index.d.ts.map +0 -1
  387. package/lib/types/vertexai/models/claude.d.ts +0 -119
  388. package/lib/types/vertexai/models/claude.d.ts.map +0 -1
  389. package/lib/types/vertexai/models/gemini.d.ts.map +0 -1
  390. package/lib/types/vertexai/models/imagen.d.ts.map +0 -1
  391. package/lib/types/vertexai/models/llama.d.ts.map +0 -1
  392. package/lib/types/vertexai/models.d.ts +0 -20
  393. package/lib/types/vertexai/models.d.ts.map +0 -1
  394. package/lib/types/watsonx/index.d.ts.map +0 -1
  395. package/lib/types/watsonx/interfaces.d.ts.map +0 -1
  396. package/lib/types/xai/index.d.ts.map +0 -1
  397. package/src/vertexai/embeddings/embeddings-image.ts +0 -52
  398. package/src/vertexai/embeddings/embeddings-text.ts +0 -54
  399. /package/lib/{esm/bedrock → bedrock}/payloads.js +0 -0
  400. /package/lib/{esm/mistral → mistral}/types.js +0 -0
  401. /package/lib/{esm/togetherai → togetherai}/interfaces.js +0 -0
  402. /package/lib/{types/watsonx → watsonx}/interfaces.d.ts +0 -0
  403. /package/lib/{esm/watsonx → watsonx}/interfaces.js +0 -0
@@ -0,0 +1,869 @@
1
+ /**
2
+ * Shared utilities for Anthropic SDK-based drivers.
3
+ *
4
+ * Used by both the native AnthropicDriver (drivers/src/anthropic/) and the
5
+ * VertexAI Claude pathway (drivers/src/vertexai/models/claude.ts). Both use
6
+ * the same Anthropic Messages API surface — the only difference is the client
7
+ * (Anthropic vs AnthropicVertex) and how auth is wired up.
8
+ */
9
+ import { AnthropicError, APIConnectionError, APIConnectionTimeoutError, APIError, APIUserAbortError, AuthenticationError, BadRequestError, ConflictError, InternalServerError, NotFoundError, PermissionDeniedError, RateLimitError, UnprocessableEntityError, } from '@anthropic-ai/sdk/error';
10
+ import { getClaudeMaxTokensLimit } from '@llumiverse/common';
11
+ import { getConversationMeta, incrementConversationTurn, LlumiverseError, PromptRole, readStreamAsBase64, readStreamAsString, stripBase64ImagesFromConversation, stripHeartbeatsFromConversation, truncateLargeTextInConversation, } from '@llumiverse/core';
12
+ import { asyncMap } from '@llumiverse/core/async';
13
+ import { resolveClaudeThinking } from './claude-thinking.js';
14
+ import { truncateBinaryForDebug } from './debug-prompt.js';
15
+ // ============================================================================
16
+ // Types
17
+ // ============================================================================
18
+ // Conversation text-trim policy (applied only when the caller requests text
19
+ // trimming via stripTextMaxTokens). Keep the last N messages fully intact (the
20
+ // active working set) and cap large text in older messages at the token ceiling
21
+ // below — so long agent conversations don't balloon context.
22
+ const KEEP_RECENT_MESSAGES = 12;
23
+ const OLD_MESSAGE_TEXT_MAX_TOKENS = 2000;
24
+ function formatClaudeContentBlockForDebug(block) {
25
+ if (block.type === 'image' && block.source.type === 'base64') {
26
+ return {
27
+ ...block,
28
+ source: {
29
+ ...block.source,
30
+ data: truncateBinaryForDebug(block.source.data),
31
+ },
32
+ };
33
+ }
34
+ if (block.type === 'document' && block.source.type === 'base64') {
35
+ return {
36
+ ...block,
37
+ source: {
38
+ ...block.source,
39
+ data: truncateBinaryForDebug(block.source.data),
40
+ },
41
+ };
42
+ }
43
+ return block;
44
+ }
45
+ export function formatClaudeDebugPrompt(prompt) {
46
+ return {
47
+ ...prompt,
48
+ messages: prompt.messages.map((message) => ({
49
+ ...message,
50
+ content: Array.isArray(message.content)
51
+ ? message.content.map(formatClaudeContentBlockForDebug)
52
+ : message.content,
53
+ })),
54
+ };
55
+ }
56
+ function streamClaudeMessages(client, payload, requestOptions) {
57
+ // AnthropicVertex intentionally wraps the Anthropic Messages API, but it depends on its
58
+ // own @anthropic-ai/sdk copy. Cast at the boundary so the implementation can call the
59
+ // shared runtime-compatible stream API without TS trying to call a union of SDK versions.
60
+ return Promise.resolve(client.messages.stream(payload, requestOptions));
61
+ }
62
+ // ============================================================================
63
+ // Token usage
64
+ // ============================================================================
65
+ export function anthropicUsageToTokenUsage(usage) {
66
+ const cacheRead = usage.cache_read_input_tokens ?? 0;
67
+ const cacheWrite = usage.cache_creation_input_tokens ?? 0;
68
+ return {
69
+ prompt_new: usage.input_tokens,
70
+ prompt: usage.input_tokens + cacheRead + cacheWrite,
71
+ result: usage.output_tokens,
72
+ total: usage.input_tokens + usage.output_tokens + cacheRead + cacheWrite,
73
+ prompt_cached: usage.cache_read_input_tokens ?? undefined,
74
+ prompt_cache_write: usage.cache_creation_input_tokens ?? undefined,
75
+ };
76
+ }
77
+ // ============================================================================
78
+ // Finish reason
79
+ // ============================================================================
80
+ export function claudeFinishReason(reason) {
81
+ if (!reason)
82
+ return undefined;
83
+ switch (reason) {
84
+ case 'end_turn':
85
+ return 'stop';
86
+ case 'max_tokens':
87
+ return 'length';
88
+ default:
89
+ return reason; // stop_sequence, tool_use
90
+ }
91
+ }
92
+ // ============================================================================
93
+ // Content extraction
94
+ // ============================================================================
95
+ export function collectClaudeTools(content) {
96
+ const out = [];
97
+ for (const block of content) {
98
+ if (block.type === 'tool_use') {
99
+ out.push({
100
+ id: block.id,
101
+ tool_name: block.name,
102
+ tool_input: block.input,
103
+ });
104
+ }
105
+ }
106
+ return out.length > 0 ? out : undefined;
107
+ }
108
+ export function collectAllTextContent(content, includeThoughts = false) {
109
+ const textParts = [];
110
+ if (includeThoughts) {
111
+ for (const block of content) {
112
+ if (block.type === 'thinking' && block.thinking) {
113
+ textParts.push(block.thinking);
114
+ }
115
+ else if (block.type === 'redacted_thinking' && block.data) {
116
+ textParts.push(`[Redacted thinking: ${block.data}]`);
117
+ }
118
+ }
119
+ if (textParts.length > 0) {
120
+ textParts.push('');
121
+ }
122
+ }
123
+ for (const block of content) {
124
+ if (block.type === 'text' && block.text) {
125
+ textParts.push(block.text);
126
+ }
127
+ }
128
+ return textParts.join('\n');
129
+ }
130
+ // ============================================================================
131
+ // Max tokens
132
+ // ============================================================================
133
+ export function claudeMaxTokens(option) {
134
+ const modelOptions = option.model_options;
135
+ if (modelOptions && typeof modelOptions.max_tokens === 'number') {
136
+ return modelOptions.max_tokens;
137
+ }
138
+ let maxSupportedTokens = getClaudeMaxTokensLimit(option.model);
139
+ // Claude 3.7 supports up to 128k with a beta header; default to 64k when no budget is set.
140
+ if (option.model.includes('claude-3-7-sonnet') && (modelOptions?.thinking_budget_tokens ?? 0) < 48000) {
141
+ maxSupportedTokens = 64000;
142
+ }
143
+ return maxSupportedTokens;
144
+ }
145
+ // ============================================================================
146
+ // File / multimodal block helpers
147
+ // ============================================================================
148
+ async function collectFileBlocks(segment, logger) {
149
+ const contentBlocks = [];
150
+ for (const file of segment.files || []) {
151
+ if (file.mime_type?.startsWith('image/')) {
152
+ const allowedTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/webp'];
153
+ if (!allowedTypes.includes(file.mime_type)) {
154
+ throw new Error(`Unsupported image type: ${file.mime_type}`);
155
+ }
156
+ const mimeType = String(file.mime_type);
157
+ contentBlocks.push({
158
+ type: 'image',
159
+ source: {
160
+ type: 'base64',
161
+ data: await readStreamAsBase64(await file.getStream()),
162
+ media_type: mimeType,
163
+ },
164
+ });
165
+ }
166
+ else if (file.mime_type?.startsWith('video/')) {
167
+ logger?.warn({
168
+ file_name: file.name,
169
+ mime_type: file.mime_type,
170
+ }, '[Claude] Skipping unsupported video attachment');
171
+ }
172
+ else if (file.mime_type === 'application/pdf') {
173
+ contentBlocks.push({
174
+ title: file.name,
175
+ type: 'document',
176
+ source: {
177
+ type: 'base64',
178
+ data: await readStreamAsBase64(await file.getStream()),
179
+ media_type: 'application/pdf',
180
+ },
181
+ });
182
+ }
183
+ else if (file.mime_type?.startsWith('text/')) {
184
+ contentBlocks.push({
185
+ title: file.name,
186
+ type: 'document',
187
+ source: {
188
+ type: 'text',
189
+ data: await readStreamAsString(await file.getStream()),
190
+ media_type: 'text/plain',
191
+ },
192
+ });
193
+ }
194
+ }
195
+ return contentBlocks;
196
+ }
197
+ // ============================================================================
198
+ // Prompt formatting (PromptSegment[] → ClaudePrompt)
199
+ // ============================================================================
200
+ export async function formatClaudePrompt(segments, options, logger) {
201
+ let system = segments
202
+ .filter((s) => s.role === PromptRole.system)
203
+ .map((s) => ({ text: s.content, type: 'text' }));
204
+ if (options.result_schema) {
205
+ const schemaText = options.tools && options.tools.length > 0
206
+ ? `When not calling tools, the answer must be a JSON object using the following JSON Schema:\n${JSON.stringify(options.result_schema)}`
207
+ : `The answer must be a JSON object using the following JSON Schema:\n${JSON.stringify(options.result_schema)}`;
208
+ system.push({ text: schemaText, type: 'text' });
209
+ }
210
+ let messages = [];
211
+ const safetyMessages = [];
212
+ for (const segment of segments) {
213
+ if (segment.role === PromptRole.system)
214
+ continue;
215
+ if (segment.role === PromptRole.tool) {
216
+ if (!segment.tool_use_id) {
217
+ throw new Error('Tool prompt segment must have a tool use ID');
218
+ }
219
+ const contentBlocks = [];
220
+ if (segment.content) {
221
+ contentBlocks.push({ type: 'text', text: segment.content });
222
+ }
223
+ contentBlocks.push(...(await collectFileBlocks(segment, logger)));
224
+ messages.push({
225
+ role: 'user',
226
+ content: [
227
+ {
228
+ type: 'tool_result',
229
+ tool_use_id: segment.tool_use_id,
230
+ content: contentBlocks,
231
+ },
232
+ ],
233
+ });
234
+ }
235
+ else {
236
+ const contentBlocks = [];
237
+ if (segment.content) {
238
+ contentBlocks.push({ type: 'text', text: segment.content });
239
+ }
240
+ contentBlocks.push(...(await collectFileBlocks(segment, logger)));
241
+ if (contentBlocks.length === 0)
242
+ continue;
243
+ const messageParam = {
244
+ role: segment.role === PromptRole.assistant ? 'assistant' : 'user',
245
+ content: contentBlocks,
246
+ };
247
+ if (segment.role === PromptRole.safety) {
248
+ safetyMessages.push(messageParam);
249
+ }
250
+ else {
251
+ messages.push(messageParam);
252
+ }
253
+ }
254
+ }
255
+ messages = messages.concat(safetyMessages);
256
+ if (system && system.length === 0)
257
+ system = undefined;
258
+ return { messages, system };
259
+ }
260
+ // ============================================================================
261
+ // Conversation management
262
+ // ============================================================================
263
+ export function createPromptFromResponse(response) {
264
+ return {
265
+ messages: [{ role: response.role, content: response.content }],
266
+ system: undefined,
267
+ };
268
+ }
269
+ export function mergeConsecutiveUserMessages(messages) {
270
+ if (messages.length === 0)
271
+ return [];
272
+ const needsMerging = messages.some((msg, i) => i < messages.length - 1 && msg.role === 'user' && messages[i + 1].role === 'user');
273
+ if (!needsMerging)
274
+ return messages;
275
+ const result = [];
276
+ let i = 0;
277
+ while (i < messages.length) {
278
+ const current = messages[i];
279
+ if (current.role === 'user') {
280
+ const mergedContent = [];
281
+ while (i < messages.length && messages[i].role === 'user') {
282
+ const userMsg = messages[i];
283
+ if (Array.isArray(userMsg.content)) {
284
+ mergedContent.push(...userMsg.content);
285
+ }
286
+ else if (typeof userMsg.content === 'string') {
287
+ mergedContent.push({ type: 'text', text: userMsg.content });
288
+ }
289
+ i++;
290
+ }
291
+ result.push({ role: 'user', content: mergedContent });
292
+ }
293
+ else {
294
+ result.push(current);
295
+ i++;
296
+ }
297
+ }
298
+ return result;
299
+ }
300
+ export function sanitizeMessages(messages) {
301
+ const result = [];
302
+ for (const message of messages) {
303
+ if (typeof message.content === 'string') {
304
+ if (message.content.trim())
305
+ result.push(message);
306
+ continue;
307
+ }
308
+ const filteredContent = message.content.filter((block) => {
309
+ if (block.type === 'text')
310
+ return block.text && block.text.trim().length > 0;
311
+ return true;
312
+ });
313
+ if (filteredContent.length > 0) {
314
+ result.push({ ...message, content: filteredContent });
315
+ }
316
+ }
317
+ return result;
318
+ }
319
+ export function fixOrphanedToolUse(messages) {
320
+ if (messages.length < 2)
321
+ return messages;
322
+ const result = [];
323
+ for (let i = 0; i < messages.length; i++) {
324
+ const current = messages[i];
325
+ result.push(current);
326
+ if (current.role === 'assistant' && Array.isArray(current.content)) {
327
+ const toolUseBlocks = current.content.filter((block) => block.type === 'tool_use');
328
+ if (toolUseBlocks.length > 0) {
329
+ const nextMessage = messages[i + 1];
330
+ if (nextMessage && nextMessage.role === 'user' && Array.isArray(nextMessage.content)) {
331
+ const toolResultIds = new Set(nextMessage.content
332
+ .filter((block) => block.type === 'tool_result')
333
+ .map((block) => block.tool_use_id));
334
+ const orphaned = toolUseBlocks.filter((block) => !toolResultIds.has(block.id));
335
+ if (orphaned.length > 0) {
336
+ const syntheticResults = orphaned.map((block) => ({
337
+ type: 'tool_result',
338
+ tool_use_id: block.id,
339
+ content: `[Tool interrupted: The user stopped the operation before "${block.name}" could execute.]`,
340
+ }));
341
+ messages[i + 1] = { ...nextMessage, content: [...syntheticResults, ...nextMessage.content] };
342
+ }
343
+ }
344
+ else if (nextMessage && nextMessage.role === 'user') {
345
+ const syntheticResults = toolUseBlocks.map((block) => ({
346
+ type: 'tool_result',
347
+ tool_use_id: block.id,
348
+ content: `[Tool interrupted: The user stopped the operation before "${block.name}" could execute.]`,
349
+ }));
350
+ const textContent = typeof nextMessage.content === 'string'
351
+ ? { type: 'text', text: nextMessage.content }
352
+ : { type: 'text', text: '' };
353
+ messages[i + 1] = { role: 'user', content: [...syntheticResults, textContent] };
354
+ }
355
+ }
356
+ }
357
+ }
358
+ return result;
359
+ }
360
+ /**
361
+ * Drop tool_result blocks whose tool_use_id has no matching tool_use in the
362
+ * immediately preceding assistant message. This is the mirror of
363
+ * {@link fixOrphanedToolUse}: that function synthesizes results for tool_uses
364
+ * left unanswered (e.g. a cancelled run); this one removes results left dangling
365
+ * after their tool_use was dropped (e.g. by conversation compaction/trimming, or
366
+ * a parallel tool batch whose results were split across messages that cannot be
367
+ * re-paired).
368
+ *
369
+ * Without this, the Anthropic / Vertex-Anthropic API rejects the request with a
370
+ * non-retryable 400: "unexpected `tool_use_id` found in `tool_result` blocks ...
371
+ * Each `tool_result` block must have a corresponding `tool_use` block in the
372
+ * previous message." — which terminates the conversation.
373
+ *
374
+ * Must run AFTER mergeConsecutiveUserMessages so that parallel tool results which
375
+ * were split across separate user messages are first combined into the single
376
+ * user turn that follows the assistant tool_use message (otherwise valid results
377
+ * whose "previous message" is another user message would be wrongly dropped).
378
+ */
379
+ export function fixOrphanedToolResults(messages) {
380
+ if (messages.length === 0)
381
+ return messages;
382
+ const result = [];
383
+ for (let i = 0; i < messages.length; i++) {
384
+ const message = messages[i];
385
+ if (message.role !== 'user' || !Array.isArray(message.content)) {
386
+ result.push(message);
387
+ continue;
388
+ }
389
+ const hasToolResult = message.content.some((block) => block.type === 'tool_result');
390
+ if (!hasToolResult) {
391
+ result.push(message);
392
+ continue;
393
+ }
394
+ // Collect the tool_use ids declared by the immediately preceding assistant message.
395
+ const prev = messages[i - 1];
396
+ const allowedIds = new Set();
397
+ if (prev && prev.role === 'assistant' && Array.isArray(prev.content)) {
398
+ for (const block of prev.content) {
399
+ if (block.type === 'tool_use')
400
+ allowedIds.add(block.id);
401
+ }
402
+ }
403
+ const filtered = message.content.filter((block) => block.type === 'tool_result' ? allowedIds.has(block.tool_use_id) : true);
404
+ // If every block was an orphaned tool_result, drop the message rather than
405
+ // emit an empty (and invalid) content array.
406
+ if (filtered.length === 0)
407
+ continue;
408
+ result.push(filtered.length === message.content.length ? message : { ...message, content: filtered });
409
+ }
410
+ return result;
411
+ }
412
+ export function updateClaudeConversation(conversation, prompt) {
413
+ const baseSystemMessages = conversation?.system || [];
414
+ const baseMessages = conversation?.messages || [];
415
+ const system = baseSystemMessages.concat(prompt.system || []);
416
+ const combined = sanitizeMessages(baseMessages.concat(prompt.messages || []));
417
+ const mergedMessages = mergeConsecutiveUserMessages(combined);
418
+ return {
419
+ messages: mergedMessages,
420
+ system: system.length > 0 ? system : undefined,
421
+ };
422
+ }
423
+ export function claudeMessagesContainToolBlocks(messages) {
424
+ for (const msg of messages) {
425
+ if (!Array.isArray(msg.content))
426
+ continue;
427
+ for (const block of msg.content) {
428
+ if (typeof block === 'object' && block !== null && 'type' in block) {
429
+ if (block.type === 'tool_use' || block.type === 'tool_result')
430
+ return true;
431
+ }
432
+ }
433
+ }
434
+ return false;
435
+ }
436
+ export function convertClaudeToolBlocksToText(messages) {
437
+ return messages.map((msg) => {
438
+ if (!Array.isArray(msg.content))
439
+ return msg;
440
+ let hasToolBlocks = false;
441
+ for (const block of msg.content) {
442
+ if (typeof block === 'object' &&
443
+ block !== null &&
444
+ 'type' in block &&
445
+ (block.type === 'tool_use' || block.type === 'tool_result')) {
446
+ hasToolBlocks = true;
447
+ break;
448
+ }
449
+ }
450
+ if (!hasToolBlocks)
451
+ return msg;
452
+ const newContent = [];
453
+ for (const block of msg.content) {
454
+ if (typeof block === 'string') {
455
+ newContent.push(block);
456
+ continue;
457
+ }
458
+ if (block.type === 'tool_use') {
459
+ const inputStr = block.input ? JSON.stringify(block.input) : '';
460
+ const truncated = inputStr.length > 500 ? `${inputStr.substring(0, 500)}...` : inputStr;
461
+ newContent.push({
462
+ type: 'text',
463
+ text: `[Tool call: ${block.name}(${truncated})]`,
464
+ });
465
+ }
466
+ else if (block.type === 'tool_result') {
467
+ let resultStr = 'No content';
468
+ if (typeof block.content === 'string') {
469
+ resultStr = block.content.length > 500 ? `${block.content.substring(0, 500)}...` : block.content;
470
+ }
471
+ else if (Array.isArray(block.content)) {
472
+ const texts = block.content
473
+ .filter((c) => c.type === 'text')
474
+ .map((c) => (c.text.length > 500 ? `${c.text.substring(0, 500)}...` : c.text));
475
+ resultStr = texts.join('\n') || 'No text content';
476
+ }
477
+ newContent.push({
478
+ type: 'text',
479
+ text: `[Tool result: ${resultStr}]`,
480
+ });
481
+ }
482
+ else {
483
+ newContent.push(block);
484
+ }
485
+ }
486
+ return { ...msg, content: newContent };
487
+ });
488
+ }
489
+ // ============================================================================
490
+ // Cache control stripping
491
+ // ============================================================================
492
+ function stripClaudeCacheControlFromBlock(block) {
493
+ if (typeof block === 'object' && block !== null && 'cache_control' in block) {
494
+ const { cache_control: _cc, ...rest } = block;
495
+ return rest;
496
+ }
497
+ return block;
498
+ }
499
+ function stripClaudeCacheControlFromMessages(messages) {
500
+ return messages.map((msg) => {
501
+ if (!Array.isArray(msg.content))
502
+ return msg;
503
+ return { ...msg, content: msg.content.map(stripClaudeCacheControlFromBlock) };
504
+ });
505
+ }
506
+ function stripClaudeCacheControlFromSystem(system) {
507
+ if (!system)
508
+ return undefined;
509
+ return system.map(stripClaudeCacheControlFromBlock);
510
+ }
511
+ function stripClaudeCacheControlFromTools(tools) {
512
+ if (!tools)
513
+ return undefined;
514
+ return tools.map((tool) => {
515
+ if ('cache_control' in tool) {
516
+ const { cache_control: _cc, ...rest } = tool;
517
+ return rest;
518
+ }
519
+ return tool;
520
+ });
521
+ }
522
+ // ============================================================================
523
+ // Payload builder
524
+ // ============================================================================
525
+ export function getClaudePayload(options, prompt) {
526
+ const modelName = options.model;
527
+ const model_options = options.model_options;
528
+ let requestOptions;
529
+ if (modelName.includes('claude-3-7-sonnet') &&
530
+ ((model_options?.max_tokens ?? 0) > 64000 || (model_options?.thinking_budget_tokens ?? 0) > 64000)) {
531
+ requestOptions = { headers: { 'anthropic-beta': 'output-128k-2025-02-19' } };
532
+ }
533
+ // Merge first so parallel tool results split across user messages are recombined
534
+ // into the single user turn after their assistant tool_use message; then fix both
535
+ // orphan directions (tool_use without result, and result without tool_use) so the
536
+ // request can never trip Anthropic/Vertex's tool_use/tool_result pairing validation.
537
+ const mergedMessages = mergeConsecutiveUserMessages(prompt.messages);
538
+ const fixedMessages = fixOrphanedToolResults(fixOrphanedToolUse(mergedMessages));
539
+ let sanitizedMessages = sanitizeMessages(fixedMessages);
540
+ if (options.tools) {
541
+ for (const tool of options.tools) {
542
+ if (tool.input_schema.type !== 'object') {
543
+ throw new Error(`Tool "${tool.name}" has invalid input_schema.type: expected "object", got "${tool.input_schema.type}"`);
544
+ }
545
+ }
546
+ }
547
+ const hasTools = options.tools && options.tools.length > 0;
548
+ if (!hasTools && claudeMessagesContainToolBlocks(sanitizedMessages)) {
549
+ sanitizedMessages = convertClaudeToolBlocksToText(sanitizedMessages);
550
+ }
551
+ sanitizedMessages = stripClaudeCacheControlFromMessages(sanitizedMessages);
552
+ const sanitizedSystem = stripClaudeCacheControlFromSystem(prompt.system);
553
+ const sanitizedTools = hasTools
554
+ ? stripClaudeCacheControlFromTools(options.tools)
555
+ : undefined;
556
+ const cacheEnabled = model_options?.cache_enabled === true;
557
+ if (cacheEnabled) {
558
+ const cacheTtl = model_options?.cache_ttl;
559
+ const cacheControl = { type: 'ephemeral', ...(cacheTtl && { ttl: cacheTtl }) };
560
+ if (sanitizedSystem && sanitizedSystem.length > 0) {
561
+ const lastBlock = sanitizedSystem[sanitizedSystem.length - 1];
562
+ lastBlock.cache_control = cacheControl;
563
+ }
564
+ if (sanitizedTools && sanitizedTools.length > 0) {
565
+ const lastTool = sanitizedTools[sanitizedTools.length - 1];
566
+ lastTool.cache_control = cacheControl;
567
+ }
568
+ if (sanitizedMessages.length >= 4) {
569
+ const pivotMsg = sanitizedMessages[sanitizedMessages.length - 2];
570
+ if (Array.isArray(pivotMsg.content) && pivotMsg.content.length > 0) {
571
+ const lastBlock = pivotMsg.content[pivotMsg.content.length - 1];
572
+ if (typeof lastBlock === 'object' &&
573
+ lastBlock !== null &&
574
+ 'type' in lastBlock &&
575
+ lastBlock.type !== 'thinking' &&
576
+ lastBlock.type !== 'redacted_thinking') {
577
+ lastBlock.cache_control = cacheControl;
578
+ }
579
+ }
580
+ }
581
+ }
582
+ const { thinking, outputConfig, hasSamplingRestriction } = resolveClaudeThinking(modelName, model_options);
583
+ const payload = {
584
+ messages: sanitizedMessages,
585
+ system: sanitizedSystem,
586
+ tools: sanitizedTools,
587
+ temperature: hasSamplingRestriction ? undefined : model_options?.temperature,
588
+ model: modelName,
589
+ max_tokens: claudeMaxTokens(options),
590
+ top_p: hasSamplingRestriction
591
+ ? undefined
592
+ : model_options?.temperature != null
593
+ ? undefined
594
+ : model_options?.top_p,
595
+ top_k: hasSamplingRestriction ? undefined : model_options?.top_k,
596
+ stop_sequences: model_options?.stop_sequence,
597
+ thinking,
598
+ stream: true,
599
+ ...(outputConfig && { output_config: outputConfig }),
600
+ };
601
+ return { payload, requestOptions };
602
+ }
603
+ // ============================================================================
604
+ // Streaming conversation builder (called after stream completes)
605
+ // ============================================================================
606
+ export function buildClaudeStreamingConversation(prompt, result, toolUse, options) {
607
+ const completionResults = result;
608
+ const text = completionResults
609
+ .filter((r) => r.type === 'text')
610
+ .map((r) => r.value)
611
+ .join('');
612
+ let conversation = updateClaudeConversation(options.conversation, prompt);
613
+ if (text) {
614
+ const assistantMsg = { role: 'assistant', content: text };
615
+ conversation = updateClaudeConversation(conversation, { messages: [assistantMsg] });
616
+ }
617
+ if (toolUse && toolUse.length > 0) {
618
+ const toolBlocks = toolUse.map((t) => ({
619
+ type: 'tool_use',
620
+ id: t.id,
621
+ name: t.tool_name,
622
+ input: t.tool_input ?? {},
623
+ }));
624
+ const assistantToolMsg = { role: 'assistant', content: toolBlocks };
625
+ conversation = updateClaudeConversation(conversation, { messages: [assistantToolMsg] });
626
+ }
627
+ conversation = incrementConversationTurn(conversation);
628
+ const currentTurn = getConversationMeta(conversation).turnNumber;
629
+ // When text trimming is requested, keep the agent's active working set (the
630
+ // most recent messages — the file it just read/wrote, latest diagnostics)
631
+ // fully intact, and aggressively shrink large text only in OLDER messages.
632
+ // Clamp the effective cap so a lax caller value (e.g. 10000) still bites on
633
+ // stale blocks; long agent conversations otherwise balloon context and
634
+ // trigger frequent expensive checkpoints.
635
+ const requestedTextMax = options.stripTextMaxTokens;
636
+ const stripOptions = {
637
+ keepForTurns: options.stripImagesAfterTurns ?? Infinity,
638
+ currentTurn,
639
+ textMaxTokens: requestedTextMax ? Math.min(requestedTextMax, OLD_MESSAGE_TEXT_MAX_TOKENS) : undefined,
640
+ keepRecentMessages: requestedTextMax ? KEEP_RECENT_MESSAGES : undefined,
641
+ };
642
+ let processed = stripBase64ImagesFromConversation(conversation, stripOptions);
643
+ processed = truncateLargeTextInConversation(processed, stripOptions);
644
+ processed = stripHeartbeatsFromConversation(processed, {
645
+ keepForTurns: options.stripHeartbeatsAfterTurns ?? 1,
646
+ currentTurn,
647
+ });
648
+ return processed;
649
+ }
650
+ // ============================================================================
651
+ // Execution helpers (standalone, take a client parameter)
652
+ // ============================================================================
653
+ /**
654
+ * Execute a non-streaming Claude completion.
655
+ * Works with any Anthropic-compatible client (Anthropic or AnthropicVertex).
656
+ */
657
+ export async function executeClaudeCompletion(client, prompt, options) {
658
+ const model_options = options.model_options;
659
+ let conversation = updateClaudeConversation(options.conversation, prompt);
660
+ const { payload, requestOptions } = getClaudePayload(options, conversation);
661
+ const responseStream = await streamClaudeMessages(client, payload, requestOptions);
662
+ const result = await responseStream.finalMessage();
663
+ const includeThoughts = model_options?.include_thoughts ?? false;
664
+ const text = collectAllTextContent(result.content, includeThoughts);
665
+ const tool_use = collectClaudeTools(result.content);
666
+ conversation = updateClaudeConversation(conversation, createPromptFromResponse(result));
667
+ conversation = incrementConversationTurn(conversation);
668
+ const currentTurn = getConversationMeta(conversation).turnNumber;
669
+ const stripOpts = {
670
+ keepForTurns: options.stripImagesAfterTurns ?? Infinity,
671
+ currentTurn,
672
+ textMaxTokens: options.stripTextMaxTokens,
673
+ };
674
+ let processedConversation = stripBase64ImagesFromConversation(conversation, stripOpts);
675
+ processedConversation = truncateLargeTextInConversation(processedConversation, stripOpts);
676
+ processedConversation = stripHeartbeatsFromConversation(processedConversation, {
677
+ keepForTurns: options.stripHeartbeatsAfterTurns ?? 1,
678
+ currentTurn,
679
+ });
680
+ return {
681
+ result: text ? [{ type: 'text', value: text }] : [{ type: 'text', value: '' }],
682
+ tool_use,
683
+ token_usage: anthropicUsageToTokenUsage(result.usage),
684
+ finish_reason: tool_use ? 'tool_use' : claudeFinishReason(result?.stop_reason ?? ''),
685
+ conversation: processedConversation,
686
+ };
687
+ }
688
+ /**
689
+ * Execute a streaming Claude completion.
690
+ * Works with any Anthropic-compatible client (Anthropic or AnthropicVertex).
691
+ */
692
+ export async function streamClaudeCompletion(client, prompt, options) {
693
+ const model_options = options.model_options;
694
+ const conversation = updateClaudeConversation(options.conversation, prompt);
695
+ const { payload, requestOptions } = getClaudePayload(options, conversation);
696
+ const streamingPayload = { ...payload, stream: true };
697
+ const response_stream = await streamClaudeMessages(client, streamingPayload, requestOptions);
698
+ let currentToolUse = null;
699
+ let pendingSpacing = false;
700
+ const stream = asyncMap(response_stream, async (streamEvent) => {
701
+ switch (streamEvent.type) {
702
+ case 'message_start':
703
+ return {
704
+ result: [{ type: 'text', value: '' }],
705
+ token_usage: anthropicUsageToTokenUsage(streamEvent.message.usage),
706
+ };
707
+ case 'message_delta':
708
+ return {
709
+ result: [{ type: 'text', value: '' }],
710
+ token_usage: { result: streamEvent.usage.output_tokens },
711
+ finish_reason: claudeFinishReason(streamEvent.delta.stop_reason ?? undefined),
712
+ };
713
+ case 'content_block_start':
714
+ if (streamEvent.content_block.type === 'tool_use') {
715
+ currentToolUse = {
716
+ id: streamEvent.content_block.id,
717
+ name: streamEvent.content_block.name,
718
+ inputJson: '',
719
+ };
720
+ return {
721
+ result: [],
722
+ tool_use: [
723
+ {
724
+ id: streamEvent.content_block.id,
725
+ tool_name: streamEvent.content_block.name,
726
+ tool_input: '',
727
+ },
728
+ ],
729
+ };
730
+ }
731
+ if (streamEvent.content_block.type === 'redacted_thinking' && model_options?.include_thoughts) {
732
+ return {
733
+ result: [{ type: 'text', value: `[Redacted thinking: ${streamEvent.content_block.data}]` }],
734
+ };
735
+ }
736
+ break;
737
+ case 'content_block_delta':
738
+ switch (streamEvent.delta.type) {
739
+ case 'text_delta': {
740
+ const prefix = pendingSpacing ? '\n\n' : '';
741
+ pendingSpacing = false;
742
+ return {
743
+ result: streamEvent.delta.text
744
+ ? [{ type: 'text', value: prefix + streamEvent.delta.text }]
745
+ : [],
746
+ };
747
+ }
748
+ case 'input_json_delta':
749
+ if (currentToolUse && streamEvent.delta.partial_json) {
750
+ return {
751
+ result: [],
752
+ tool_use: [
753
+ {
754
+ id: currentToolUse.id,
755
+ tool_name: '',
756
+ tool_input: streamEvent.delta.partial_json,
757
+ },
758
+ ],
759
+ };
760
+ }
761
+ break;
762
+ case 'thinking_delta':
763
+ if (model_options?.include_thoughts) {
764
+ return {
765
+ result: streamEvent.delta.thinking
766
+ ? [{ type: 'text', value: streamEvent.delta.thinking }]
767
+ : [],
768
+ };
769
+ }
770
+ break;
771
+ case 'signature_delta':
772
+ if (model_options?.include_thoughts) {
773
+ pendingSpacing = true;
774
+ }
775
+ break;
776
+ }
777
+ break;
778
+ case 'content_block_stop':
779
+ if (currentToolUse) {
780
+ currentToolUse = null;
781
+ pendingSpacing = false;
782
+ }
783
+ break;
784
+ }
785
+ return { result: [] };
786
+ });
787
+ return stream;
788
+ }
789
+ // ============================================================================
790
+ // Error handling
791
+ // ============================================================================
792
+ export function formatAnthropicLlumiverseError(error, context) {
793
+ if (error instanceof AnthropicError && !(error instanceof APIError)) {
794
+ // Client-side SDK error (e.g. "Streaming is required for operations that may take longer than 10 minutes").
795
+ // These are structural/configuration errors — retrying will never succeed.
796
+ const errorName = error.constructor?.name || 'AnthropicError';
797
+ return new LlumiverseError(`[${context.provider}] ${error.message}`, false, context, error, undefined, errorName);
798
+ }
799
+ if (!(error instanceof APIError)) {
800
+ // Not an Anthropic error — rethrow for default handling
801
+ throw error;
802
+ }
803
+ const apiError = error;
804
+ const httpStatusCode = apiError.status;
805
+ let message = apiError.message || String(error);
806
+ let errorType;
807
+ if (apiError.error && typeof apiError.error === 'object') {
808
+ const nested = apiError.error;
809
+ if (nested.error && typeof nested.error === 'object') {
810
+ const innerError = nested.error;
811
+ errorType = innerError.type;
812
+ if (typeof innerError.message === 'string') {
813
+ message = innerError.message;
814
+ }
815
+ }
816
+ }
817
+ let userMessage = message;
818
+ if (httpStatusCode)
819
+ userMessage = `[${httpStatusCode}] ${userMessage}`;
820
+ if (errorType && errorType !== 'error')
821
+ userMessage = `${errorType}: ${userMessage}`;
822
+ if (apiError.requestID)
823
+ userMessage += ` (Request ID: ${apiError.requestID})`;
824
+ const retryable = isClaudeErrorRetryable(error, httpStatusCode, errorType, apiError.headers ?? undefined);
825
+ const errorName = error.constructor?.name || 'AnthropicError';
826
+ return new LlumiverseError(`[${context.provider}] ${userMessage}`, retryable, context, error, httpStatusCode, errorName);
827
+ }
828
+ export function isClaudeErrorRetryable(error, httpStatusCode, errorType, headers) {
829
+ // Honour the server's explicit retry directive first (mirrors SDK shouldRetry logic).
830
+ const shouldRetryHeader = headers?.get('x-should-retry');
831
+ if (shouldRetryHeader === 'true')
832
+ return true;
833
+ if (shouldRetryHeader === 'false')
834
+ return false;
835
+ if (error instanceof APIUserAbortError)
836
+ return false;
837
+ if (error instanceof RateLimitError)
838
+ return true;
839
+ if (error instanceof InternalServerError)
840
+ return true;
841
+ if (error instanceof APIConnectionTimeoutError)
842
+ return true;
843
+ if (error instanceof BadRequestError)
844
+ return false;
845
+ if (error instanceof AuthenticationError)
846
+ return false;
847
+ if (error instanceof PermissionDeniedError)
848
+ return false;
849
+ if (error instanceof NotFoundError)
850
+ return false;
851
+ if (error instanceof ConflictError)
852
+ return true; // SDK retries 409 (lock timeouts)
853
+ if (error instanceof UnprocessableEntityError)
854
+ return false;
855
+ if (errorType === 'invalid_request_error')
856
+ return false;
857
+ if (httpStatusCode !== undefined) {
858
+ if (httpStatusCode === 429 || httpStatusCode === 408 || httpStatusCode === 529)
859
+ return true;
860
+ if (httpStatusCode >= 500 && httpStatusCode < 600)
861
+ return true;
862
+ if (httpStatusCode >= 400 && httpStatusCode < 500)
863
+ return false;
864
+ }
865
+ if (error instanceof APIConnectionError && !(error instanceof APIConnectionTimeoutError))
866
+ return true;
867
+ return undefined;
868
+ }
869
+ //# sourceMappingURL=claude-messages.js.map