ai 6.0.33 → 6.0.35

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 (357) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/index.d.mts +50 -21
  3. package/dist/index.d.ts +50 -21
  4. package/dist/index.js +348 -286
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +280 -219
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/internal/index.js +1 -1
  9. package/dist/internal/index.mjs +1 -1
  10. package/docs/02-foundations/03-prompts.mdx +2 -2
  11. package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1 -1
  12. package/docs/07-reference/01-ai-sdk-core/28-output.mdx +1 -1
  13. package/docs/07-reference/05-ai-sdk-errors/ai-ui-message-stream-error.mdx +67 -0
  14. package/package.json +6 -4
  15. package/src/agent/agent.ts +116 -0
  16. package/src/agent/create-agent-ui-stream-response.test.ts +258 -0
  17. package/src/agent/create-agent-ui-stream-response.ts +50 -0
  18. package/src/agent/create-agent-ui-stream.ts +73 -0
  19. package/src/agent/index.ts +33 -0
  20. package/src/agent/infer-agent-tools.ts +7 -0
  21. package/src/agent/infer-agent-ui-message.test-d.ts +54 -0
  22. package/src/agent/infer-agent-ui-message.ts +11 -0
  23. package/src/agent/pipe-agent-ui-stream-to-response.ts +52 -0
  24. package/src/agent/tool-loop-agent-on-finish-callback.ts +31 -0
  25. package/src/agent/tool-loop-agent-on-step-finish-callback.ts +11 -0
  26. package/src/agent/tool-loop-agent-settings.ts +182 -0
  27. package/src/agent/tool-loop-agent.test-d.ts +114 -0
  28. package/src/agent/tool-loop-agent.test.ts +442 -0
  29. package/src/agent/tool-loop-agent.ts +114 -0
  30. package/src/embed/__snapshots__/embed-many.test.ts.snap +191 -0
  31. package/src/embed/__snapshots__/embed.test.ts.snap +81 -0
  32. package/src/embed/embed-many-result.ts +53 -0
  33. package/src/embed/embed-many.test.ts +653 -0
  34. package/src/embed/embed-many.ts +378 -0
  35. package/src/embed/embed-result.ts +50 -0
  36. package/src/embed/embed.test.ts +298 -0
  37. package/src/embed/embed.ts +211 -0
  38. package/src/embed/index.ts +4 -0
  39. package/src/error/index.ts +35 -0
  40. package/src/error/invalid-argument-error.ts +34 -0
  41. package/src/error/invalid-stream-part-error.ts +28 -0
  42. package/src/error/invalid-tool-approval-error.ts +26 -0
  43. package/src/error/invalid-tool-input-error.ts +33 -0
  44. package/src/error/no-image-generated-error.ts +39 -0
  45. package/src/error/no-object-generated-error.ts +70 -0
  46. package/src/error/no-output-generated-error.ts +26 -0
  47. package/src/error/no-speech-generated-error.ts +18 -0
  48. package/src/error/no-such-tool-error.ts +35 -0
  49. package/src/error/no-transcript-generated-error.ts +20 -0
  50. package/src/error/tool-call-not-found-for-approval-error.ts +32 -0
  51. package/src/error/tool-call-repair-error.ts +30 -0
  52. package/src/error/ui-message-stream-error.ts +48 -0
  53. package/src/error/unsupported-model-version-error.ts +23 -0
  54. package/src/error/verify-no-object-generated-error.ts +27 -0
  55. package/src/generate-image/generate-image-result.ts +42 -0
  56. package/src/generate-image/generate-image.test.ts +1420 -0
  57. package/src/generate-image/generate-image.ts +360 -0
  58. package/src/generate-image/index.ts +18 -0
  59. package/src/generate-object/__snapshots__/generate-object.test.ts.snap +133 -0
  60. package/src/generate-object/__snapshots__/stream-object.test.ts.snap +297 -0
  61. package/src/generate-object/generate-object-result.ts +67 -0
  62. package/src/generate-object/generate-object.test-d.ts +49 -0
  63. package/src/generate-object/generate-object.test.ts +1191 -0
  64. package/src/generate-object/generate-object.ts +518 -0
  65. package/src/generate-object/index.ts +9 -0
  66. package/src/generate-object/inject-json-instruction.test.ts +181 -0
  67. package/src/generate-object/inject-json-instruction.ts +30 -0
  68. package/src/generate-object/output-strategy.ts +415 -0
  69. package/src/generate-object/parse-and-validate-object-result.ts +111 -0
  70. package/src/generate-object/repair-text.ts +12 -0
  71. package/src/generate-object/stream-object-result.ts +120 -0
  72. package/src/generate-object/stream-object.test-d.ts +74 -0
  73. package/src/generate-object/stream-object.test.ts +1950 -0
  74. package/src/generate-object/stream-object.ts +986 -0
  75. package/src/generate-object/validate-object-generation-input.ts +144 -0
  76. package/src/generate-speech/generate-speech-result.ts +30 -0
  77. package/src/generate-speech/generate-speech.test.ts +300 -0
  78. package/src/generate-speech/generate-speech.ts +190 -0
  79. package/src/generate-speech/generated-audio-file.ts +65 -0
  80. package/src/generate-speech/index.ts +3 -0
  81. package/src/generate-text/__snapshots__/generate-text.test.ts.snap +1872 -0
  82. package/src/generate-text/__snapshots__/stream-text.test.ts.snap +1255 -0
  83. package/src/generate-text/collect-tool-approvals.test.ts +553 -0
  84. package/src/generate-text/collect-tool-approvals.ts +116 -0
  85. package/src/generate-text/content-part.ts +25 -0
  86. package/src/generate-text/execute-tool-call.ts +129 -0
  87. package/src/generate-text/extract-reasoning-content.ts +17 -0
  88. package/src/generate-text/extract-text-content.ts +15 -0
  89. package/src/generate-text/generate-text-result.ts +168 -0
  90. package/src/generate-text/generate-text.test-d.ts +68 -0
  91. package/src/generate-text/generate-text.test.ts +7011 -0
  92. package/src/generate-text/generate-text.ts +1223 -0
  93. package/src/generate-text/generated-file.ts +70 -0
  94. package/src/generate-text/index.ts +57 -0
  95. package/src/generate-text/is-approval-needed.ts +29 -0
  96. package/src/generate-text/output-utils.ts +23 -0
  97. package/src/generate-text/output.test.ts +698 -0
  98. package/src/generate-text/output.ts +590 -0
  99. package/src/generate-text/parse-tool-call.test.ts +570 -0
  100. package/src/generate-text/parse-tool-call.ts +188 -0
  101. package/src/generate-text/prepare-step.ts +103 -0
  102. package/src/generate-text/prune-messages.test.ts +720 -0
  103. package/src/generate-text/prune-messages.ts +167 -0
  104. package/src/generate-text/reasoning-output.ts +20 -0
  105. package/src/generate-text/reasoning.ts +8 -0
  106. package/src/generate-text/response-message.ts +10 -0
  107. package/src/generate-text/run-tools-transformation.test.ts +1143 -0
  108. package/src/generate-text/run-tools-transformation.ts +420 -0
  109. package/src/generate-text/smooth-stream.test.ts +2101 -0
  110. package/src/generate-text/smooth-stream.ts +162 -0
  111. package/src/generate-text/step-result.ts +238 -0
  112. package/src/generate-text/stop-condition.ts +29 -0
  113. package/src/generate-text/stream-text-result.ts +463 -0
  114. package/src/generate-text/stream-text.test-d.ts +200 -0
  115. package/src/generate-text/stream-text.test.ts +19979 -0
  116. package/src/generate-text/stream-text.ts +2505 -0
  117. package/src/generate-text/to-response-messages.test.ts +922 -0
  118. package/src/generate-text/to-response-messages.ts +163 -0
  119. package/src/generate-text/tool-approval-request-output.ts +21 -0
  120. package/src/generate-text/tool-call-repair-function.ts +27 -0
  121. package/src/generate-text/tool-call.ts +47 -0
  122. package/src/generate-text/tool-error.ts +34 -0
  123. package/src/generate-text/tool-output-denied.ts +21 -0
  124. package/src/generate-text/tool-output.ts +7 -0
  125. package/src/generate-text/tool-result.ts +36 -0
  126. package/src/generate-text/tool-set.ts +14 -0
  127. package/src/global.ts +24 -0
  128. package/src/index.ts +50 -0
  129. package/src/logger/index.ts +6 -0
  130. package/src/logger/log-warnings.test.ts +351 -0
  131. package/src/logger/log-warnings.ts +119 -0
  132. package/src/middleware/__snapshots__/simulate-streaming-middleware.test.ts.snap +64 -0
  133. package/src/middleware/add-tool-input-examples-middleware.test.ts +476 -0
  134. package/src/middleware/add-tool-input-examples-middleware.ts +90 -0
  135. package/src/middleware/default-embedding-settings-middleware.test.ts +126 -0
  136. package/src/middleware/default-embedding-settings-middleware.ts +22 -0
  137. package/src/middleware/default-settings-middleware.test.ts +388 -0
  138. package/src/middleware/default-settings-middleware.ts +33 -0
  139. package/src/middleware/extract-json-middleware.test.ts +827 -0
  140. package/src/middleware/extract-json-middleware.ts +197 -0
  141. package/src/middleware/extract-reasoning-middleware.test.ts +1028 -0
  142. package/src/middleware/extract-reasoning-middleware.ts +238 -0
  143. package/src/middleware/index.ts +10 -0
  144. package/src/middleware/simulate-streaming-middleware.test.ts +911 -0
  145. package/src/middleware/simulate-streaming-middleware.ts +79 -0
  146. package/src/middleware/wrap-embedding-model.test.ts +358 -0
  147. package/src/middleware/wrap-embedding-model.ts +86 -0
  148. package/src/middleware/wrap-image-model.test.ts +423 -0
  149. package/src/middleware/wrap-image-model.ts +85 -0
  150. package/src/middleware/wrap-language-model.test.ts +518 -0
  151. package/src/middleware/wrap-language-model.ts +104 -0
  152. package/src/middleware/wrap-provider.test.ts +120 -0
  153. package/src/middleware/wrap-provider.ts +51 -0
  154. package/src/model/as-embedding-model-v3.test.ts +319 -0
  155. package/src/model/as-embedding-model-v3.ts +24 -0
  156. package/src/model/as-image-model-v3.test.ts +409 -0
  157. package/src/model/as-image-model-v3.ts +24 -0
  158. package/src/model/as-language-model-v3.test.ts +508 -0
  159. package/src/model/as-language-model-v3.ts +103 -0
  160. package/src/model/as-provider-v3.ts +36 -0
  161. package/src/model/as-speech-model-v3.test.ts +356 -0
  162. package/src/model/as-speech-model-v3.ts +24 -0
  163. package/src/model/as-transcription-model-v3.test.ts +529 -0
  164. package/src/model/as-transcription-model-v3.ts +24 -0
  165. package/src/model/resolve-model.test.ts +244 -0
  166. package/src/model/resolve-model.ts +126 -0
  167. package/src/prompt/call-settings.ts +148 -0
  168. package/src/prompt/content-part.ts +209 -0
  169. package/src/prompt/convert-to-language-model-prompt.test.ts +2018 -0
  170. package/src/prompt/convert-to-language-model-prompt.ts +442 -0
  171. package/src/prompt/create-tool-model-output.test.ts +508 -0
  172. package/src/prompt/create-tool-model-output.ts +34 -0
  173. package/src/prompt/data-content.test.ts +15 -0
  174. package/src/prompt/data-content.ts +134 -0
  175. package/src/prompt/index.ts +27 -0
  176. package/src/prompt/invalid-data-content-error.ts +29 -0
  177. package/src/prompt/invalid-message-role-error.ts +27 -0
  178. package/src/prompt/message-conversion-error.ts +28 -0
  179. package/src/prompt/message.ts +68 -0
  180. package/src/prompt/prepare-call-settings.test.ts +159 -0
  181. package/src/prompt/prepare-call-settings.ts +108 -0
  182. package/src/prompt/prepare-tools-and-tool-choice.test.ts +461 -0
  183. package/src/prompt/prepare-tools-and-tool-choice.ts +86 -0
  184. package/src/prompt/prompt.ts +43 -0
  185. package/src/prompt/split-data-url.ts +17 -0
  186. package/src/prompt/standardize-prompt.test.ts +82 -0
  187. package/src/prompt/standardize-prompt.ts +99 -0
  188. package/src/prompt/wrap-gateway-error.ts +29 -0
  189. package/src/registry/custom-provider.test.ts +211 -0
  190. package/src/registry/custom-provider.ts +155 -0
  191. package/src/registry/index.ts +7 -0
  192. package/src/registry/no-such-provider-error.ts +41 -0
  193. package/src/registry/provider-registry.test.ts +691 -0
  194. package/src/registry/provider-registry.ts +328 -0
  195. package/src/rerank/index.ts +2 -0
  196. package/src/rerank/rerank-result.ts +70 -0
  197. package/src/rerank/rerank.test.ts +516 -0
  198. package/src/rerank/rerank.ts +237 -0
  199. package/src/telemetry/assemble-operation-name.ts +21 -0
  200. package/src/telemetry/get-base-telemetry-attributes.ts +53 -0
  201. package/src/telemetry/get-tracer.ts +20 -0
  202. package/src/telemetry/noop-tracer.ts +69 -0
  203. package/src/telemetry/record-span.ts +63 -0
  204. package/src/telemetry/select-telemetry-attributes.ts +78 -0
  205. package/src/telemetry/select-temetry-attributes.test.ts +114 -0
  206. package/src/telemetry/stringify-for-telemetry.test.ts +114 -0
  207. package/src/telemetry/stringify-for-telemetry.ts +33 -0
  208. package/src/telemetry/telemetry-settings.ts +44 -0
  209. package/src/test/mock-embedding-model-v2.ts +35 -0
  210. package/src/test/mock-embedding-model-v3.ts +48 -0
  211. package/src/test/mock-image-model-v2.ts +28 -0
  212. package/src/test/mock-image-model-v3.ts +28 -0
  213. package/src/test/mock-language-model-v2.ts +72 -0
  214. package/src/test/mock-language-model-v3.ts +77 -0
  215. package/src/test/mock-provider-v2.ts +68 -0
  216. package/src/test/mock-provider-v3.ts +80 -0
  217. package/src/test/mock-reranking-model-v3.ts +25 -0
  218. package/src/test/mock-server-response.ts +69 -0
  219. package/src/test/mock-speech-model-v2.ts +24 -0
  220. package/src/test/mock-speech-model-v3.ts +24 -0
  221. package/src/test/mock-tracer.ts +156 -0
  222. package/src/test/mock-transcription-model-v2.ts +24 -0
  223. package/src/test/mock-transcription-model-v3.ts +24 -0
  224. package/src/test/mock-values.ts +4 -0
  225. package/src/test/not-implemented.ts +3 -0
  226. package/src/text-stream/create-text-stream-response.test.ts +38 -0
  227. package/src/text-stream/create-text-stream-response.ts +18 -0
  228. package/src/text-stream/index.ts +2 -0
  229. package/src/text-stream/pipe-text-stream-to-response.test.ts +38 -0
  230. package/src/text-stream/pipe-text-stream-to-response.ts +26 -0
  231. package/src/transcribe/index.ts +2 -0
  232. package/src/transcribe/transcribe-result.ts +60 -0
  233. package/src/transcribe/transcribe.test.ts +313 -0
  234. package/src/transcribe/transcribe.ts +173 -0
  235. package/src/types/embedding-model-middleware.ts +3 -0
  236. package/src/types/embedding-model.ts +18 -0
  237. package/src/types/image-model-middleware.ts +3 -0
  238. package/src/types/image-model-response-metadata.ts +16 -0
  239. package/src/types/image-model.ts +19 -0
  240. package/src/types/index.ts +29 -0
  241. package/src/types/json-value.ts +15 -0
  242. package/src/types/language-model-middleware.ts +3 -0
  243. package/src/types/language-model-request-metadata.ts +6 -0
  244. package/src/types/language-model-response-metadata.ts +21 -0
  245. package/src/types/language-model.ts +104 -0
  246. package/src/types/provider-metadata.ts +16 -0
  247. package/src/types/provider.ts +55 -0
  248. package/src/types/reranking-model.ts +6 -0
  249. package/src/types/speech-model-response-metadata.ts +21 -0
  250. package/src/types/speech-model.ts +6 -0
  251. package/src/types/transcription-model-response-metadata.ts +16 -0
  252. package/src/types/transcription-model.ts +9 -0
  253. package/src/types/usage.ts +200 -0
  254. package/src/types/warning.ts +7 -0
  255. package/src/ui/__snapshots__/append-response-messages.test.ts.snap +416 -0
  256. package/src/ui/__snapshots__/convert-to-model-messages.test.ts.snap +419 -0
  257. package/src/ui/__snapshots__/process-chat-text-response.test.ts.snap +142 -0
  258. package/src/ui/call-completion-api.ts +157 -0
  259. package/src/ui/chat-transport.ts +83 -0
  260. package/src/ui/chat.test-d.ts +233 -0
  261. package/src/ui/chat.test.ts +2695 -0
  262. package/src/ui/chat.ts +716 -0
  263. package/src/ui/convert-file-list-to-file-ui-parts.ts +36 -0
  264. package/src/ui/convert-to-model-messages.test.ts +2775 -0
  265. package/src/ui/convert-to-model-messages.ts +373 -0
  266. package/src/ui/default-chat-transport.ts +36 -0
  267. package/src/ui/direct-chat-transport.test.ts +446 -0
  268. package/src/ui/direct-chat-transport.ts +118 -0
  269. package/src/ui/http-chat-transport.test.ts +185 -0
  270. package/src/ui/http-chat-transport.ts +292 -0
  271. package/src/ui/index.ts +71 -0
  272. package/src/ui/last-assistant-message-is-complete-with-approval-responses.ts +44 -0
  273. package/src/ui/last-assistant-message-is-complete-with-tool-calls.test.ts +371 -0
  274. package/src/ui/last-assistant-message-is-complete-with-tool-calls.ts +39 -0
  275. package/src/ui/process-text-stream.test.ts +38 -0
  276. package/src/ui/process-text-stream.ts +16 -0
  277. package/src/ui/process-ui-message-stream.test.ts +8294 -0
  278. package/src/ui/process-ui-message-stream.ts +761 -0
  279. package/src/ui/text-stream-chat-transport.ts +23 -0
  280. package/src/ui/transform-text-to-ui-message-stream.test.ts +124 -0
  281. package/src/ui/transform-text-to-ui-message-stream.ts +27 -0
  282. package/src/ui/ui-messages.test.ts +48 -0
  283. package/src/ui/ui-messages.ts +534 -0
  284. package/src/ui/use-completion.ts +84 -0
  285. package/src/ui/validate-ui-messages.test.ts +1428 -0
  286. package/src/ui/validate-ui-messages.ts +476 -0
  287. package/src/ui-message-stream/create-ui-message-stream-response.test.ts +266 -0
  288. package/src/ui-message-stream/create-ui-message-stream-response.ts +32 -0
  289. package/src/ui-message-stream/create-ui-message-stream.test.ts +639 -0
  290. package/src/ui-message-stream/create-ui-message-stream.ts +124 -0
  291. package/src/ui-message-stream/get-response-ui-message-id.test.ts +55 -0
  292. package/src/ui-message-stream/get-response-ui-message-id.ts +24 -0
  293. package/src/ui-message-stream/handle-ui-message-stream-finish.test.ts +429 -0
  294. package/src/ui-message-stream/handle-ui-message-stream-finish.ts +135 -0
  295. package/src/ui-message-stream/index.ts +13 -0
  296. package/src/ui-message-stream/json-to-sse-transform-stream.ts +12 -0
  297. package/src/ui-message-stream/pipe-ui-message-stream-to-response.test.ts +90 -0
  298. package/src/ui-message-stream/pipe-ui-message-stream-to-response.ts +40 -0
  299. package/src/ui-message-stream/read-ui-message-stream.test.ts +122 -0
  300. package/src/ui-message-stream/read-ui-message-stream.ts +87 -0
  301. package/src/ui-message-stream/ui-message-chunks.test-d.ts +18 -0
  302. package/src/ui-message-stream/ui-message-chunks.ts +344 -0
  303. package/src/ui-message-stream/ui-message-stream-headers.ts +7 -0
  304. package/src/ui-message-stream/ui-message-stream-on-finish-callback.ts +32 -0
  305. package/src/ui-message-stream/ui-message-stream-response-init.ts +5 -0
  306. package/src/ui-message-stream/ui-message-stream-writer.ts +24 -0
  307. package/src/util/as-array.ts +3 -0
  308. package/src/util/async-iterable-stream.test.ts +241 -0
  309. package/src/util/async-iterable-stream.ts +94 -0
  310. package/src/util/consume-stream.ts +29 -0
  311. package/src/util/cosine-similarity.test.ts +57 -0
  312. package/src/util/cosine-similarity.ts +47 -0
  313. package/src/util/create-resolvable-promise.ts +30 -0
  314. package/src/util/create-stitchable-stream.test.ts +239 -0
  315. package/src/util/create-stitchable-stream.ts +112 -0
  316. package/src/util/data-url.ts +17 -0
  317. package/src/util/deep-partial.ts +84 -0
  318. package/src/util/detect-media-type.test.ts +670 -0
  319. package/src/util/detect-media-type.ts +184 -0
  320. package/src/util/download/download-function.ts +45 -0
  321. package/src/util/download/download.test.ts +69 -0
  322. package/src/util/download/download.ts +46 -0
  323. package/src/util/error-handler.ts +1 -0
  324. package/src/util/fix-json.test.ts +279 -0
  325. package/src/util/fix-json.ts +401 -0
  326. package/src/util/get-potential-start-index.test.ts +34 -0
  327. package/src/util/get-potential-start-index.ts +30 -0
  328. package/src/util/index.ts +11 -0
  329. package/src/util/is-deep-equal-data.test.ts +119 -0
  330. package/src/util/is-deep-equal-data.ts +48 -0
  331. package/src/util/is-non-empty-object.ts +5 -0
  332. package/src/util/job.ts +1 -0
  333. package/src/util/log-v2-compatibility-warning.ts +21 -0
  334. package/src/util/merge-abort-signals.test.ts +155 -0
  335. package/src/util/merge-abort-signals.ts +43 -0
  336. package/src/util/merge-objects.test.ts +118 -0
  337. package/src/util/merge-objects.ts +79 -0
  338. package/src/util/now.ts +4 -0
  339. package/src/util/parse-partial-json.test.ts +80 -0
  340. package/src/util/parse-partial-json.ts +30 -0
  341. package/src/util/prepare-headers.test.ts +51 -0
  342. package/src/util/prepare-headers.ts +14 -0
  343. package/src/util/prepare-retries.test.ts +10 -0
  344. package/src/util/prepare-retries.ts +47 -0
  345. package/src/util/retry-error.ts +41 -0
  346. package/src/util/retry-with-exponential-backoff.test.ts +446 -0
  347. package/src/util/retry-with-exponential-backoff.ts +154 -0
  348. package/src/util/serial-job-executor.test.ts +162 -0
  349. package/src/util/serial-job-executor.ts +36 -0
  350. package/src/util/simulate-readable-stream.test.ts +98 -0
  351. package/src/util/simulate-readable-stream.ts +39 -0
  352. package/src/util/split-array.test.ts +60 -0
  353. package/src/util/split-array.ts +20 -0
  354. package/src/util/value-of.ts +65 -0
  355. package/src/util/write-to-server-response.test.ts +266 -0
  356. package/src/util/write-to-server-response.ts +49 -0
  357. package/src/version.ts +5 -0
@@ -0,0 +1,2018 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { createDefaultDownloadFunction } from '../util/download/download-function';
3
+ import {
4
+ convertToLanguageModelMessage,
5
+ convertToLanguageModelPrompt,
6
+ } from './convert-to-language-model-prompt';
7
+
8
+ describe('convertToLanguageModelPrompt', () => {
9
+ describe('system message', () => {
10
+ it('should convert a string system message', async () => {
11
+ const result = await convertToLanguageModelPrompt({
12
+ prompt: {
13
+ system: 'INSTRUCTIONS',
14
+ messages: [{ role: 'user', content: 'Hello, world!' }],
15
+ },
16
+ supportedUrls: {},
17
+ download: undefined,
18
+ });
19
+
20
+ expect(result).toMatchInlineSnapshot(`
21
+ [
22
+ {
23
+ "content": "INSTRUCTIONS",
24
+ "role": "system",
25
+ },
26
+ {
27
+ "content": [
28
+ {
29
+ "text": "Hello, world!",
30
+ "type": "text",
31
+ },
32
+ ],
33
+ "providerOptions": undefined,
34
+ "role": "user",
35
+ },
36
+ ]
37
+ `);
38
+ });
39
+
40
+ it('should convert a SystemModelMessage system message', async () => {
41
+ const result = await convertToLanguageModelPrompt({
42
+ prompt: {
43
+ system: {
44
+ role: 'system',
45
+ content: 'INSTRUCTIONS',
46
+ providerOptions: { test: { value: 'test' } },
47
+ },
48
+ messages: [{ role: 'user', content: 'Hello, world!' }],
49
+ },
50
+ supportedUrls: {},
51
+ download: undefined,
52
+ });
53
+
54
+ expect(result).toMatchInlineSnapshot(`
55
+ [
56
+ {
57
+ "content": "INSTRUCTIONS",
58
+ "providerOptions": {
59
+ "test": {
60
+ "value": "test",
61
+ },
62
+ },
63
+ "role": "system",
64
+ },
65
+ {
66
+ "content": [
67
+ {
68
+ "text": "Hello, world!",
69
+ "type": "text",
70
+ },
71
+ ],
72
+ "providerOptions": undefined,
73
+ "role": "user",
74
+ },
75
+ ]
76
+ `);
77
+ });
78
+
79
+ it('should convert an array of SystemModelMessage system messages', async () => {
80
+ const result = await convertToLanguageModelPrompt({
81
+ prompt: {
82
+ system: [
83
+ { role: 'system', content: 'INSTRUCTIONS' },
84
+ { role: 'system', content: 'INSTRUCTIONS 2' },
85
+ ],
86
+ messages: [{ role: 'user', content: 'Hello, world!' }],
87
+ },
88
+ supportedUrls: {},
89
+ download: undefined,
90
+ });
91
+
92
+ expect(result).toMatchInlineSnapshot(`
93
+ [
94
+ {
95
+ "content": "INSTRUCTIONS",
96
+ "providerOptions": undefined,
97
+ "role": "system",
98
+ },
99
+ {
100
+ "content": "INSTRUCTIONS 2",
101
+ "providerOptions": undefined,
102
+ "role": "system",
103
+ },
104
+ {
105
+ "content": [
106
+ {
107
+ "text": "Hello, world!",
108
+ "type": "text",
109
+ },
110
+ ],
111
+ "providerOptions": undefined,
112
+ "role": "user",
113
+ },
114
+ ]
115
+ `);
116
+ });
117
+ });
118
+
119
+ describe('user message', () => {
120
+ describe('image parts', () => {
121
+ it('should download images for user image parts with URLs when model does not support image URLs', async () => {
122
+ const result = await convertToLanguageModelPrompt({
123
+ prompt: {
124
+ messages: [
125
+ {
126
+ role: 'user',
127
+ content: [
128
+ {
129
+ type: 'image',
130
+ image: new URL('https://example.com/image.png'),
131
+ },
132
+ ],
133
+ },
134
+ ],
135
+ },
136
+ supportedUrls: {},
137
+ download: createDefaultDownloadFunction(async ({ url }) => {
138
+ expect(url).toEqual(new URL('https://example.com/image.png'));
139
+ return {
140
+ data: new Uint8Array([0, 1, 2, 3]),
141
+ mediaType: 'image/png',
142
+ };
143
+ }),
144
+ });
145
+
146
+ expect(result).toEqual([
147
+ {
148
+ role: 'user',
149
+ content: [
150
+ {
151
+ type: 'file',
152
+ mediaType: 'image/png',
153
+ data: new Uint8Array([0, 1, 2, 3]),
154
+ },
155
+ ],
156
+ },
157
+ ]);
158
+ });
159
+
160
+ it('should download images for user image parts with string URLs when model does not support image URLs', async () => {
161
+ const result = await convertToLanguageModelPrompt({
162
+ prompt: {
163
+ messages: [
164
+ {
165
+ role: 'user',
166
+ content: [
167
+ {
168
+ type: 'image',
169
+ image: 'https://example.com/image.png',
170
+ },
171
+ ],
172
+ },
173
+ ],
174
+ },
175
+ supportedUrls: {},
176
+ download: createDefaultDownloadFunction(async ({ url }) => {
177
+ expect(url).toEqual(new URL('https://example.com/image.png'));
178
+ return {
179
+ data: new Uint8Array([0, 1, 2, 3]),
180
+ mediaType: 'image/png',
181
+ };
182
+ }),
183
+ });
184
+
185
+ expect(result).toEqual([
186
+ {
187
+ role: 'user',
188
+ content: [
189
+ {
190
+ type: 'file',
191
+ mediaType: 'image/png',
192
+ data: new Uint8Array([0, 1, 2, 3]),
193
+ },
194
+ ],
195
+ },
196
+ ]);
197
+ });
198
+ });
199
+
200
+ describe('file parts', () => {
201
+ it('should pass through URLs when the model supports a particular URL', async () => {
202
+ const result = await convertToLanguageModelPrompt({
203
+ prompt: {
204
+ messages: [
205
+ {
206
+ role: 'user',
207
+ content: [
208
+ {
209
+ type: 'file',
210
+ data: new URL('https://example.com/document.pdf'),
211
+ mediaType: 'application/pdf',
212
+ },
213
+ ],
214
+ },
215
+ ],
216
+ },
217
+ supportedUrls: {
218
+ '*': [/^https:\/\/.*$/],
219
+ },
220
+ download: undefined,
221
+ });
222
+
223
+ expect(result).toEqual([
224
+ {
225
+ role: 'user',
226
+ content: [
227
+ {
228
+ type: 'file',
229
+ data: new URL('https://example.com/document.pdf'),
230
+ mediaType: 'application/pdf',
231
+ },
232
+ ],
233
+ },
234
+ ]);
235
+ });
236
+
237
+ it('should download the URL as an asset when the model does not support a URL', async () => {
238
+ const result = await convertToLanguageModelPrompt({
239
+ prompt: {
240
+ messages: [
241
+ {
242
+ role: 'user',
243
+ content: [
244
+ {
245
+ type: 'file',
246
+ data: new URL('https://example.com/document.pdf'),
247
+ mediaType: 'application/pdf',
248
+ },
249
+ ],
250
+ },
251
+ ],
252
+ },
253
+ supportedUrls: {
254
+ // PDF is not supported, but image/* is
255
+ 'image/*': [/^https:\/\/.*$/],
256
+ },
257
+ download: createDefaultDownloadFunction(async ({ url }) => {
258
+ expect(url).toEqual(new URL('https://example.com/document.pdf'));
259
+ return {
260
+ data: new Uint8Array([0, 1, 2, 3]),
261
+ mediaType: 'application/pdf',
262
+ };
263
+ }),
264
+ });
265
+
266
+ expect(result).toEqual([
267
+ {
268
+ role: 'user',
269
+ content: [
270
+ {
271
+ type: 'file',
272
+ mediaType: 'application/pdf',
273
+ data: new Uint8Array([0, 1, 2, 3]),
274
+ },
275
+ ],
276
+ },
277
+ ]);
278
+ });
279
+
280
+ it('should handle file parts with base64 string data', async () => {
281
+ const base64Data = 'SGVsbG8sIFdvcmxkIQ=='; // "Hello, World!" in base64
282
+ const result = await convertToLanguageModelPrompt({
283
+ prompt: {
284
+ messages: [
285
+ {
286
+ role: 'user',
287
+ content: [
288
+ {
289
+ type: 'file',
290
+ data: base64Data,
291
+ mediaType: 'text/plain',
292
+ },
293
+ ],
294
+ },
295
+ ],
296
+ },
297
+ supportedUrls: {
298
+ 'image/*': [/^https:\/\/.*$/],
299
+ },
300
+ download: undefined,
301
+ });
302
+
303
+ expect(result).toEqual([
304
+ {
305
+ role: 'user',
306
+ content: [
307
+ {
308
+ type: 'file',
309
+ data: base64Data,
310
+ mediaType: 'text/plain',
311
+ },
312
+ ],
313
+ },
314
+ ]);
315
+ });
316
+
317
+ it('should handle file parts with Uint8Array data', async () => {
318
+ const uint8Data = new Uint8Array([72, 101, 108, 108, 111]); // "Hello" in ASCII
319
+ const result = await convertToLanguageModelPrompt({
320
+ prompt: {
321
+ messages: [
322
+ {
323
+ role: 'user',
324
+ content: [
325
+ {
326
+ type: 'file',
327
+ data: uint8Data,
328
+ mediaType: 'text/plain',
329
+ },
330
+ ],
331
+ },
332
+ ],
333
+ },
334
+ supportedUrls: {
335
+ 'image/*': [/^https:\/\/.*$/],
336
+ },
337
+ download: undefined,
338
+ });
339
+
340
+ expect(result).toEqual([
341
+ {
342
+ role: 'user',
343
+ content: [
344
+ {
345
+ type: 'file',
346
+ data: new Uint8Array([72, 101, 108, 108, 111]),
347
+ mediaType: 'text/plain',
348
+ },
349
+ ],
350
+ },
351
+ ]);
352
+ });
353
+
354
+ it('should download files for user file parts with URL objects when model does not support downloads', async () => {
355
+ const result = await convertToLanguageModelPrompt({
356
+ prompt: {
357
+ messages: [
358
+ {
359
+ role: 'user',
360
+ content: [
361
+ {
362
+ type: 'file',
363
+ data: new URL('https://example.com/document.pdf'),
364
+ mediaType: 'application/pdf',
365
+ },
366
+ ],
367
+ },
368
+ ],
369
+ },
370
+ supportedUrls: {},
371
+ download: createDefaultDownloadFunction(async ({ url }) => {
372
+ expect(url).toEqual(new URL('https://example.com/document.pdf'));
373
+ return {
374
+ data: new Uint8Array([0, 1, 2, 3]),
375
+ mediaType: 'application/pdf',
376
+ };
377
+ }),
378
+ });
379
+
380
+ expect(result).toEqual([
381
+ {
382
+ role: 'user',
383
+ content: [
384
+ {
385
+ type: 'file',
386
+ mediaType: 'application/pdf',
387
+ data: new Uint8Array([0, 1, 2, 3]),
388
+ },
389
+ ],
390
+ },
391
+ ]);
392
+ });
393
+
394
+ it('should download files for user file parts with string URLs when model does not support downloads', async () => {
395
+ const result = await convertToLanguageModelPrompt({
396
+ prompt: {
397
+ messages: [
398
+ {
399
+ role: 'user',
400
+ content: [
401
+ {
402
+ type: 'file',
403
+ data: 'https://example.com/document.pdf',
404
+ mediaType: 'application/pdf',
405
+ },
406
+ ],
407
+ },
408
+ ],
409
+ },
410
+ supportedUrls: {},
411
+ download: createDefaultDownloadFunction(async ({ url }) => {
412
+ expect(url).toEqual(new URL('https://example.com/document.pdf'));
413
+ return {
414
+ data: new Uint8Array([0, 1, 2, 3]),
415
+ mediaType: 'application/pdf',
416
+ };
417
+ }),
418
+ });
419
+
420
+ expect(result).toEqual([
421
+ {
422
+ role: 'user',
423
+ content: [
424
+ {
425
+ type: 'file',
426
+ mediaType: 'application/pdf',
427
+ data: new Uint8Array([0, 1, 2, 3]),
428
+ },
429
+ ],
430
+ },
431
+ ]);
432
+ });
433
+
434
+ it('should download files for user file parts with string URLs when model does not support the particular URL', async () => {
435
+ const result = await convertToLanguageModelPrompt({
436
+ prompt: {
437
+ messages: [
438
+ {
439
+ role: 'user',
440
+ content: [
441
+ {
442
+ type: 'file',
443
+ data: 'https://example.com/document.pdf',
444
+ mediaType: 'application/pdf',
445
+ },
446
+ ],
447
+ },
448
+ ],
449
+ },
450
+ supportedUrls: {
451
+ 'application/pdf': [
452
+ // everything except https://example.com/document.pdf
453
+ /^(?!https:\/\/example\.com\/document\.pdf$).*$/,
454
+ ],
455
+ },
456
+ download: createDefaultDownloadFunction(async ({ url }) => {
457
+ expect(url).toEqual(new URL('https://example.com/document.pdf'));
458
+ return {
459
+ data: new Uint8Array([0, 1, 2, 3]),
460
+ mediaType: 'application/pdf',
461
+ };
462
+ }),
463
+ });
464
+
465
+ expect(result).toEqual([
466
+ {
467
+ role: 'user',
468
+ content: [
469
+ {
470
+ type: 'file',
471
+ mediaType: 'application/pdf',
472
+ data: new Uint8Array([0, 1, 2, 3]),
473
+ },
474
+ ],
475
+ },
476
+ ]);
477
+ });
478
+
479
+ it('does not download URLs for user file parts for URL objects when model does support the URL', async () => {
480
+ const result = await convertToLanguageModelPrompt({
481
+ prompt: {
482
+ messages: [
483
+ {
484
+ role: 'user',
485
+ content: [
486
+ {
487
+ type: 'file',
488
+ data: new URL('https://example.com/document.pdf'),
489
+ mediaType: 'application/pdf',
490
+ },
491
+ ],
492
+ },
493
+ ],
494
+ },
495
+ supportedUrls: {
496
+ 'application/pdf': [
497
+ // match exactly https://example.com/document.pdf
498
+ /^https:\/\/example\.com\/document\.pdf$/,
499
+ ],
500
+ },
501
+ download: undefined,
502
+ });
503
+
504
+ expect(result).toEqual([
505
+ {
506
+ role: 'user',
507
+ content: [
508
+ {
509
+ type: 'file',
510
+ mediaType: 'application/pdf',
511
+ data: new URL('https://example.com/document.pdf'),
512
+ },
513
+ ],
514
+ },
515
+ ]);
516
+ });
517
+
518
+ it('it should default to downloading the URL when the model does not provider a supportsUrl function', async () => {
519
+ const result = await convertToLanguageModelPrompt({
520
+ prompt: {
521
+ messages: [
522
+ {
523
+ role: 'user',
524
+ content: [
525
+ {
526
+ type: 'file',
527
+ data: 'https://example.com/document.pdf',
528
+ mediaType: 'application/pdf',
529
+ },
530
+ ],
531
+ },
532
+ ],
533
+ },
534
+ supportedUrls: {},
535
+ download: createDefaultDownloadFunction(async ({ url }) => {
536
+ expect(url).toEqual(new URL('https://example.com/document.pdf'));
537
+ return {
538
+ data: new Uint8Array([0, 1, 2, 3]),
539
+ mediaType: 'application/pdf',
540
+ };
541
+ }),
542
+ });
543
+
544
+ expect(result).toEqual([
545
+ {
546
+ role: 'user',
547
+ content: [
548
+ {
549
+ type: 'file',
550
+ mediaType: 'application/pdf',
551
+ data: new Uint8Array([0, 1, 2, 3]),
552
+ },
553
+ ],
554
+ },
555
+ ]);
556
+ });
557
+
558
+ it('should handle file parts with filename', async () => {
559
+ const result = await convertToLanguageModelPrompt({
560
+ prompt: {
561
+ messages: [
562
+ {
563
+ role: 'user',
564
+ content: [
565
+ {
566
+ type: 'file',
567
+ data: 'SGVsbG8sIFdvcmxkIQ==', // "Hello, World!" in base64
568
+ mediaType: 'text/plain',
569
+ filename: 'hello.txt',
570
+ },
571
+ ],
572
+ },
573
+ ],
574
+ },
575
+ supportedUrls: {
576
+ 'image/*': [/^https:\/\/.*$/],
577
+ },
578
+ download: undefined,
579
+ });
580
+
581
+ expect(result).toEqual([
582
+ {
583
+ role: 'user',
584
+ content: [
585
+ {
586
+ type: 'file',
587
+ data: 'SGVsbG8sIFdvcmxkIQ==',
588
+ mediaType: 'text/plain',
589
+ filename: 'hello.txt',
590
+ },
591
+ ],
592
+ },
593
+ ]);
594
+ });
595
+
596
+ it('should preserve filename when downloading file from URL', async () => {
597
+ const result = await convertToLanguageModelPrompt({
598
+ prompt: {
599
+ messages: [
600
+ {
601
+ role: 'user',
602
+ content: [
603
+ {
604
+ type: 'file',
605
+ data: new URL('https://example.com/document.pdf'),
606
+ mediaType: 'application/pdf',
607
+ filename: 'important-document.pdf',
608
+ },
609
+ ],
610
+ },
611
+ ],
612
+ },
613
+ supportedUrls: {},
614
+ download: createDefaultDownloadFunction(async ({ url }) => {
615
+ expect(url).toEqual(new URL('https://example.com/document.pdf'));
616
+ return {
617
+ data: new Uint8Array([0, 1, 2, 3]),
618
+ mediaType: 'application/pdf',
619
+ };
620
+ }),
621
+ });
622
+
623
+ expect(result).toEqual([
624
+ {
625
+ role: 'user',
626
+ content: [
627
+ {
628
+ type: 'file',
629
+ mediaType: 'application/pdf',
630
+ data: new Uint8Array([0, 1, 2, 3]),
631
+ filename: 'important-document.pdf',
632
+ },
633
+ ],
634
+ },
635
+ ]);
636
+ });
637
+
638
+ it('should prioritize user-provided mediaType over downloaded file mediaType', async () => {
639
+ const result = await convertToLanguageModelPrompt({
640
+ prompt: {
641
+ messages: [
642
+ {
643
+ role: 'user',
644
+ content: [
645
+ {
646
+ type: 'file',
647
+ data: new URL('https://example.com/image.jpg'),
648
+ mediaType: 'image/jpeg',
649
+ },
650
+ ],
651
+ },
652
+ ],
653
+ },
654
+ supportedUrls: {},
655
+ download: createDefaultDownloadFunction(async ({ url }) => {
656
+ expect(url).toEqual(new URL('https://example.com/image.jpg'));
657
+ return {
658
+ data: new Uint8Array([0, 1, 2, 3]),
659
+ mediaType: 'application/octet-stream',
660
+ };
661
+ }),
662
+ });
663
+
664
+ expect(result).toMatchInlineSnapshot(`
665
+ [
666
+ {
667
+ "content": [
668
+ {
669
+ "data": Uint8Array [
670
+ 0,
671
+ 1,
672
+ 2,
673
+ 3,
674
+ ],
675
+ "filename": undefined,
676
+ "mediaType": "image/jpeg",
677
+ "providerOptions": undefined,
678
+ "type": "file",
679
+ },
680
+ ],
681
+ "providerOptions": undefined,
682
+ "role": "user",
683
+ },
684
+ ]
685
+ `);
686
+ });
687
+
688
+ it('should use downloaded file mediaType as fallback when user provides generic mediaType', async () => {
689
+ const result = await convertToLanguageModelPrompt({
690
+ prompt: {
691
+ messages: [
692
+ {
693
+ role: 'user',
694
+ content: [
695
+ {
696
+ type: 'file',
697
+ data: new URL('https://example.com/document.txt'),
698
+ mediaType: 'application/octet-stream',
699
+ },
700
+ ],
701
+ },
702
+ ],
703
+ },
704
+ supportedUrls: {},
705
+ download: createDefaultDownloadFunction(async ({ url }) => {
706
+ expect(url).toEqual(new URL('https://example.com/document.txt'));
707
+ return {
708
+ data: new Uint8Array([72, 101, 108, 108, 111]),
709
+ mediaType: 'text/plain',
710
+ };
711
+ }),
712
+ });
713
+
714
+ expect(result).toMatchInlineSnapshot(`
715
+ [
716
+ {
717
+ "content": [
718
+ {
719
+ "data": Uint8Array [
720
+ 72,
721
+ 101,
722
+ 108,
723
+ 108,
724
+ 111,
725
+ ],
726
+ "filename": undefined,
727
+ "mediaType": "application/octet-stream",
728
+ "providerOptions": undefined,
729
+ "type": "file",
730
+ },
731
+ ],
732
+ "providerOptions": undefined,
733
+ "role": "user",
734
+ },
735
+ ]
736
+ `);
737
+ });
738
+ });
739
+
740
+ describe('provider options', async () => {
741
+ it('should add provider options to messages', async () => {
742
+ const result = await convertToLanguageModelPrompt({
743
+ prompt: {
744
+ messages: [
745
+ {
746
+ role: 'user',
747
+ content: [
748
+ {
749
+ type: 'text',
750
+ text: 'hello, world!',
751
+ },
752
+ ],
753
+ providerOptions: {
754
+ 'test-provider': {
755
+ 'key-a': 'test-value-1',
756
+ 'key-b': 'test-value-2',
757
+ },
758
+ },
759
+ },
760
+ ],
761
+ },
762
+ supportedUrls: {},
763
+ download: undefined,
764
+ });
765
+
766
+ expect(result).toEqual([
767
+ {
768
+ role: 'user',
769
+ content: [
770
+ {
771
+ type: 'text',
772
+ text: 'hello, world!',
773
+ providerMetadata: undefined,
774
+ },
775
+ ],
776
+ providerOptions: {
777
+ 'test-provider': {
778
+ 'key-a': 'test-value-1',
779
+ 'key-b': 'test-value-2',
780
+ },
781
+ },
782
+ },
783
+ ]);
784
+ });
785
+ });
786
+
787
+ it('should download files when intermediate file cannot be downloaded', async () => {
788
+ const imageUrlA = `http://example.com/my-image-A.png`; // supported
789
+ const fileUrl = `http://127.0.0.1:3000/file`; // unsupported
790
+ const imageUrlB = `http://example.com/my-image-B.png`; // supported
791
+
792
+ const mockDownload = vi.fn().mockResolvedValue([
793
+ {
794
+ url: new URL(imageUrlA),
795
+ data: new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10, 0]), // empty png and 0
796
+ mediaType: 'image/png',
797
+ },
798
+ null,
799
+ {
800
+ url: new URL(imageUrlB),
801
+ data: new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10, 1]), // empty png and 1
802
+ mediaType: 'image/png',
803
+ },
804
+ ]);
805
+
806
+ const result = await convertToLanguageModelPrompt({
807
+ prompt: {
808
+ messages: [
809
+ {
810
+ role: 'user',
811
+ content: [
812
+ { type: 'image', image: imageUrlA, mediaType: 'image/png' },
813
+ {
814
+ type: 'file',
815
+ data: new URL(fileUrl),
816
+ mediaType: 'application/octet-stream',
817
+ },
818
+ { type: 'image', image: imageUrlB, mediaType: 'image/png' },
819
+ ],
820
+ },
821
+ ],
822
+ },
823
+ supportedUrls: {
824
+ '*': [/^https:\/\/.*$/],
825
+ },
826
+ download: mockDownload,
827
+ });
828
+
829
+ expect(result).toMatchInlineSnapshot(`
830
+ [
831
+ {
832
+ "content": [
833
+ {
834
+ "data": Uint8Array [
835
+ 137,
836
+ 80,
837
+ 78,
838
+ 71,
839
+ 13,
840
+ 10,
841
+ 26,
842
+ 10,
843
+ 0,
844
+ ],
845
+ "filename": undefined,
846
+ "mediaType": "image/png",
847
+ "providerOptions": undefined,
848
+ "type": "file",
849
+ },
850
+ {
851
+ "data": "http://127.0.0.1:3000/file",
852
+ "filename": undefined,
853
+ "mediaType": "application/octet-stream",
854
+ "providerOptions": undefined,
855
+ "type": "file",
856
+ },
857
+ {
858
+ "data": Uint8Array [
859
+ 137,
860
+ 80,
861
+ 78,
862
+ 71,
863
+ 13,
864
+ 10,
865
+ 26,
866
+ 10,
867
+ 1,
868
+ ],
869
+ "filename": undefined,
870
+ "mediaType": "image/png",
871
+ "providerOptions": undefined,
872
+ "type": "file",
873
+ },
874
+ ],
875
+ "providerOptions": undefined,
876
+ "role": "user",
877
+ },
878
+ ]
879
+ `);
880
+ });
881
+ });
882
+
883
+ describe('tool message', () => {
884
+ it('should combine 2 consecutive tool messages into a single tool message', async () => {
885
+ const result = await convertToLanguageModelPrompt({
886
+ prompt: {
887
+ messages: [
888
+ {
889
+ role: 'assistant',
890
+ content: [
891
+ {
892
+ type: 'tool-call',
893
+ toolCallId: 'toolCallId',
894
+ toolName: 'toolName',
895
+ input: {},
896
+ },
897
+ {
898
+ type: 'tool-approval-request',
899
+ approvalId: 'approvalId',
900
+ toolCallId: 'toolCallId',
901
+ },
902
+ ],
903
+ },
904
+ {
905
+ role: 'tool',
906
+ content: [
907
+ {
908
+ type: 'tool-approval-response',
909
+ approvalId: 'approvalId',
910
+ approved: true,
911
+ },
912
+ ],
913
+ },
914
+ {
915
+ role: 'tool',
916
+ content: [
917
+ {
918
+ type: 'tool-result',
919
+ toolName: 'toolName',
920
+ toolCallId: 'toolCallId',
921
+ output: { type: 'json', value: { some: 'result' } },
922
+ },
923
+ ],
924
+ },
925
+ ],
926
+ },
927
+ supportedUrls: {},
928
+ download: undefined,
929
+ });
930
+
931
+ expect(result).toMatchInlineSnapshot(`
932
+ [
933
+ {
934
+ "content": [
935
+ {
936
+ "input": {},
937
+ "providerExecuted": undefined,
938
+ "providerOptions": undefined,
939
+ "toolCallId": "toolCallId",
940
+ "toolName": "toolName",
941
+ "type": "tool-call",
942
+ },
943
+ ],
944
+ "providerOptions": undefined,
945
+ "role": "assistant",
946
+ },
947
+ {
948
+ "content": [
949
+ {
950
+ "output": {
951
+ "type": "json",
952
+ "value": {
953
+ "some": "result",
954
+ },
955
+ },
956
+ "providerOptions": undefined,
957
+ "toolCallId": "toolCallId",
958
+ "toolName": "toolName",
959
+ "type": "tool-result",
960
+ },
961
+ ],
962
+ "providerOptions": undefined,
963
+ "role": "tool",
964
+ },
965
+ ]
966
+ `);
967
+ });
968
+ });
969
+
970
+ describe('custom download function', () => {
971
+ it('should use custom download function to fetch URL content', async () => {
972
+ const mockDownload = vi.fn().mockResolvedValue([
973
+ {
974
+ url: new URL('https://example.com/test-file.txt'),
975
+ data: new Uint8Array([72, 101, 108, 108, 111]), // "Hello" in ASCII
976
+ mediaType: 'text/plain',
977
+ },
978
+ ]);
979
+
980
+ const result = await convertToLanguageModelPrompt({
981
+ prompt: {
982
+ messages: [
983
+ {
984
+ role: 'user',
985
+ content: [
986
+ {
987
+ type: 'file',
988
+ data: 'https://example.com/test-file.txt',
989
+ mediaType: 'text/plain',
990
+ },
991
+ ],
992
+ },
993
+ ],
994
+ },
995
+ supportedUrls: {}, // No URL support, so download should be triggered
996
+ download: mockDownload,
997
+ });
998
+
999
+ expect(mockDownload).toHaveBeenCalledOnce();
1000
+ expect(mockDownload).toHaveBeenCalledWith([
1001
+ {
1002
+ url: new URL('https://example.com/test-file.txt'),
1003
+ isUrlSupportedByModel: false,
1004
+ },
1005
+ ]);
1006
+
1007
+ expect(result).toEqual([
1008
+ {
1009
+ role: 'user',
1010
+ content: [
1011
+ {
1012
+ type: 'file',
1013
+ mediaType: 'text/plain',
1014
+ data: new Uint8Array([72, 101, 108, 108, 111]),
1015
+ },
1016
+ ],
1017
+ },
1018
+ ]);
1019
+ });
1020
+ });
1021
+ });
1022
+
1023
+ describe('convertToLanguageModelMessage', () => {
1024
+ describe('user message', () => {
1025
+ describe('text parts', () => {
1026
+ it('should filter out empty text parts', async () => {
1027
+ const result = convertToLanguageModelMessage({
1028
+ message: { role: 'user', content: [{ type: 'text', text: '' }] },
1029
+ downloadedAssets: {},
1030
+ });
1031
+
1032
+ expect(result).toEqual({
1033
+ role: 'user',
1034
+ content: [],
1035
+ });
1036
+ });
1037
+
1038
+ it('should pass through non-empty text parts', async () => {
1039
+ const result = convertToLanguageModelMessage({
1040
+ message: {
1041
+ role: 'user',
1042
+ content: [{ type: 'text', text: 'hello, world!' }],
1043
+ },
1044
+ downloadedAssets: {},
1045
+ });
1046
+
1047
+ expect(result).toEqual({
1048
+ role: 'user',
1049
+ content: [{ type: 'text', text: 'hello, world!' }],
1050
+ });
1051
+ });
1052
+ });
1053
+
1054
+ describe('image parts', () => {
1055
+ it('should convert image string https url to URL object', async () => {
1056
+ const result = convertToLanguageModelMessage({
1057
+ message: {
1058
+ role: 'user',
1059
+ content: [
1060
+ {
1061
+ type: 'image',
1062
+ image: 'https://example.com/image.jpg',
1063
+ },
1064
+ ],
1065
+ },
1066
+ downloadedAssets: {},
1067
+ });
1068
+
1069
+ expect(result).toEqual({
1070
+ role: 'user',
1071
+ content: [
1072
+ {
1073
+ type: 'file',
1074
+ data: new URL('https://example.com/image.jpg'),
1075
+ mediaType: 'image/*', // wildcard since we don't know the exact type
1076
+ },
1077
+ ],
1078
+ });
1079
+ });
1080
+
1081
+ it('should convert image string data url to base64 content', async () => {
1082
+ const result = convertToLanguageModelMessage({
1083
+ message: {
1084
+ role: 'user',
1085
+ content: [
1086
+ {
1087
+ type: 'image',
1088
+ image: 'data:image/jpg;base64,/9j/3Q==',
1089
+ },
1090
+ ],
1091
+ },
1092
+ downloadedAssets: {},
1093
+ });
1094
+
1095
+ expect(result).toEqual({
1096
+ role: 'user',
1097
+ content: [
1098
+ {
1099
+ type: 'file',
1100
+ data: '/9j/3Q==',
1101
+ mediaType: 'image/jpeg',
1102
+ },
1103
+ ],
1104
+ });
1105
+ });
1106
+
1107
+ it('should prefer detected mediaType', async () => {
1108
+ const result = convertToLanguageModelMessage({
1109
+ message: {
1110
+ role: 'user',
1111
+ content: [
1112
+ {
1113
+ type: 'image',
1114
+ // incorrect mediaType:
1115
+ image: 'data:image/png;base64,/9j/3Q==',
1116
+ },
1117
+ ],
1118
+ },
1119
+ downloadedAssets: {},
1120
+ });
1121
+
1122
+ expect(result).toEqual({
1123
+ role: 'user',
1124
+ content: [
1125
+ {
1126
+ type: 'file',
1127
+ data: '/9j/3Q==',
1128
+ mediaType: 'image/jpeg',
1129
+ },
1130
+ ],
1131
+ });
1132
+ });
1133
+ });
1134
+
1135
+ describe('file parts', () => {
1136
+ it('should convert file string https url to URL object', async () => {
1137
+ const result = convertToLanguageModelMessage({
1138
+ message: {
1139
+ role: 'user',
1140
+ content: [
1141
+ {
1142
+ type: 'file',
1143
+ data: 'https://example.com/image.jpg',
1144
+ mediaType: 'image/jpg',
1145
+ },
1146
+ ],
1147
+ },
1148
+ downloadedAssets: {},
1149
+ });
1150
+
1151
+ expect(result).toEqual({
1152
+ role: 'user',
1153
+ content: [
1154
+ {
1155
+ type: 'file',
1156
+ data: new URL('https://example.com/image.jpg'),
1157
+ mediaType: 'image/jpg',
1158
+ },
1159
+ ],
1160
+ });
1161
+ });
1162
+
1163
+ it('should convert file string data url to base64 content', async () => {
1164
+ const result = convertToLanguageModelMessage({
1165
+ message: {
1166
+ role: 'user',
1167
+ content: [
1168
+ {
1169
+ type: 'file',
1170
+ data: 'data:image/jpg;base64,dGVzdA==',
1171
+ mediaType: 'image/jpg',
1172
+ },
1173
+ ],
1174
+ },
1175
+ downloadedAssets: {},
1176
+ });
1177
+
1178
+ expect(result).toEqual({
1179
+ role: 'user',
1180
+ content: [
1181
+ {
1182
+ type: 'file',
1183
+ data: 'dGVzdA==',
1184
+ mediaType: 'image/jpg',
1185
+ },
1186
+ ],
1187
+ });
1188
+ });
1189
+ });
1190
+ });
1191
+
1192
+ describe('assistant message', () => {
1193
+ describe('text parts', () => {
1194
+ it('should ignore empty text parts when there are no provider options', async () => {
1195
+ const result = convertToLanguageModelMessage({
1196
+ message: {
1197
+ role: 'assistant',
1198
+ content: [
1199
+ {
1200
+ type: 'text',
1201
+ text: '',
1202
+ },
1203
+ {
1204
+ type: 'tool-call',
1205
+ toolName: 'toolName',
1206
+ toolCallId: 'toolCallId',
1207
+ input: {},
1208
+ },
1209
+ ],
1210
+ },
1211
+ downloadedAssets: {},
1212
+ });
1213
+
1214
+ expect(result).toEqual({
1215
+ role: 'assistant',
1216
+ content: [
1217
+ {
1218
+ type: 'tool-call',
1219
+ input: {},
1220
+ toolCallId: 'toolCallId',
1221
+ toolName: 'toolName',
1222
+ },
1223
+ ],
1224
+ });
1225
+ });
1226
+
1227
+ it('should include empty text parts when there are provider options', async () => {
1228
+ const result = convertToLanguageModelMessage({
1229
+ message: {
1230
+ role: 'assistant',
1231
+ content: [
1232
+ {
1233
+ type: 'text',
1234
+ text: '',
1235
+ providerOptions: {
1236
+ 'test-provider': {
1237
+ 'key-a': 'test-value-1',
1238
+ },
1239
+ },
1240
+ },
1241
+ {
1242
+ type: 'tool-call',
1243
+ toolName: 'toolName',
1244
+ toolCallId: 'toolCallId',
1245
+ input: {},
1246
+ },
1247
+ ],
1248
+ },
1249
+ downloadedAssets: {},
1250
+ });
1251
+
1252
+ expect(result).toMatchInlineSnapshot(`
1253
+ {
1254
+ "content": [
1255
+ {
1256
+ "providerOptions": {
1257
+ "test-provider": {
1258
+ "key-a": "test-value-1",
1259
+ },
1260
+ },
1261
+ "text": "",
1262
+ "type": "text",
1263
+ },
1264
+ {
1265
+ "input": {},
1266
+ "providerExecuted": undefined,
1267
+ "providerOptions": undefined,
1268
+ "toolCallId": "toolCallId",
1269
+ "toolName": "toolName",
1270
+ "type": "tool-call",
1271
+ },
1272
+ ],
1273
+ "providerOptions": undefined,
1274
+ "role": "assistant",
1275
+ }
1276
+ `);
1277
+ });
1278
+ });
1279
+
1280
+ describe('reasoning parts', () => {
1281
+ it('should pass through provider options', () => {
1282
+ const result = convertToLanguageModelMessage({
1283
+ message: {
1284
+ role: 'assistant',
1285
+ content: [
1286
+ {
1287
+ type: 'reasoning',
1288
+ text: 'hello, world!',
1289
+ providerOptions: {
1290
+ 'test-provider': {
1291
+ 'key-a': 'test-value-1',
1292
+ 'key-b': 'test-value-2',
1293
+ },
1294
+ },
1295
+ },
1296
+ ],
1297
+ },
1298
+ downloadedAssets: {},
1299
+ });
1300
+
1301
+ expect(result).toEqual({
1302
+ role: 'assistant',
1303
+ content: [
1304
+ {
1305
+ type: 'reasoning',
1306
+ text: 'hello, world!',
1307
+ providerOptions: {
1308
+ 'test-provider': {
1309
+ 'key-a': 'test-value-1',
1310
+ 'key-b': 'test-value-2',
1311
+ },
1312
+ },
1313
+ },
1314
+ ],
1315
+ });
1316
+ });
1317
+
1318
+ it('should support a mix of reasoning, redacted reasoning, and text parts', () => {
1319
+ const result = convertToLanguageModelMessage({
1320
+ message: {
1321
+ role: 'assistant',
1322
+ content: [
1323
+ {
1324
+ type: 'reasoning',
1325
+ text: `I'm thinking`,
1326
+ },
1327
+ {
1328
+ type: 'reasoning',
1329
+ text: 'redacted-reasoning-data',
1330
+ providerOptions: {
1331
+ 'test-provider': { redacted: true },
1332
+ },
1333
+ },
1334
+ {
1335
+ type: 'reasoning',
1336
+ text: 'more thinking',
1337
+ },
1338
+ {
1339
+ type: 'text',
1340
+ text: 'hello, world!',
1341
+ },
1342
+ ],
1343
+ },
1344
+ downloadedAssets: {},
1345
+ });
1346
+
1347
+ expect(result).toEqual({
1348
+ role: 'assistant',
1349
+ content: [
1350
+ {
1351
+ type: 'reasoning',
1352
+ text: `I'm thinking`,
1353
+ },
1354
+ {
1355
+ type: 'reasoning',
1356
+ text: 'redacted-reasoning-data',
1357
+ providerOptions: {
1358
+ 'test-provider': { redacted: true },
1359
+ },
1360
+ },
1361
+ {
1362
+ type: 'reasoning',
1363
+ text: 'more thinking',
1364
+ },
1365
+ {
1366
+ type: 'text',
1367
+ text: 'hello, world!',
1368
+ },
1369
+ ],
1370
+ });
1371
+ });
1372
+ });
1373
+
1374
+ describe('tool call parts', () => {
1375
+ it('should pass through provider options', () => {
1376
+ const result = convertToLanguageModelMessage({
1377
+ message: {
1378
+ role: 'assistant',
1379
+ content: [
1380
+ {
1381
+ type: 'tool-call',
1382
+ toolName: 'toolName',
1383
+ toolCallId: 'toolCallId',
1384
+ input: {},
1385
+ providerOptions: {
1386
+ 'test-provider': {
1387
+ 'key-a': 'test-value-1',
1388
+ 'key-b': 'test-value-2',
1389
+ },
1390
+ },
1391
+ },
1392
+ ],
1393
+ },
1394
+ downloadedAssets: {},
1395
+ });
1396
+
1397
+ expect(result).toEqual({
1398
+ role: 'assistant',
1399
+ content: [
1400
+ {
1401
+ type: 'tool-call',
1402
+ input: {},
1403
+ toolCallId: 'toolCallId',
1404
+ toolName: 'toolName',
1405
+ providerOptions: {
1406
+ 'test-provider': {
1407
+ 'key-a': 'test-value-1',
1408
+ 'key-b': 'test-value-2',
1409
+ },
1410
+ },
1411
+ },
1412
+ ],
1413
+ });
1414
+ });
1415
+
1416
+ it('should include providerExecuted flag', () => {
1417
+ const result = convertToLanguageModelMessage({
1418
+ message: {
1419
+ role: 'assistant',
1420
+ content: [
1421
+ {
1422
+ type: 'tool-call',
1423
+ toolName: 'toolName',
1424
+ toolCallId: 'toolCallId',
1425
+ input: {},
1426
+ providerExecuted: true,
1427
+ },
1428
+ ],
1429
+ },
1430
+ downloadedAssets: {},
1431
+ });
1432
+
1433
+ expect(result).toMatchInlineSnapshot(`
1434
+ {
1435
+ "content": [
1436
+ {
1437
+ "input": {},
1438
+ "providerExecuted": true,
1439
+ "providerOptions": undefined,
1440
+ "toolCallId": "toolCallId",
1441
+ "toolName": "toolName",
1442
+ "type": "tool-call",
1443
+ },
1444
+ ],
1445
+ "providerOptions": undefined,
1446
+ "role": "assistant",
1447
+ }
1448
+ `);
1449
+ });
1450
+ });
1451
+
1452
+ describe('tool result parts', () => {
1453
+ it('should include providerExecuted flag', () => {
1454
+ const result = convertToLanguageModelMessage({
1455
+ message: {
1456
+ role: 'assistant',
1457
+ content: [
1458
+ {
1459
+ type: 'tool-result',
1460
+ toolCallId: 'toolCallId',
1461
+ toolName: 'toolName',
1462
+ output: { type: 'json', value: { some: 'result' } },
1463
+ providerOptions: {
1464
+ 'test-provider': {
1465
+ 'key-a': 'test-value-1',
1466
+ 'key-b': 'test-value-2',
1467
+ },
1468
+ },
1469
+ },
1470
+ ],
1471
+ },
1472
+ downloadedAssets: {},
1473
+ });
1474
+
1475
+ expect(result).toMatchInlineSnapshot(`
1476
+ {
1477
+ "content": [
1478
+ {
1479
+ "output": {
1480
+ "type": "json",
1481
+ "value": {
1482
+ "some": "result",
1483
+ },
1484
+ },
1485
+ "providerOptions": {
1486
+ "test-provider": {
1487
+ "key-a": "test-value-1",
1488
+ "key-b": "test-value-2",
1489
+ },
1490
+ },
1491
+ "toolCallId": "toolCallId",
1492
+ "toolName": "toolName",
1493
+ "type": "tool-result",
1494
+ },
1495
+ ],
1496
+ "providerOptions": undefined,
1497
+ "role": "assistant",
1498
+ }
1499
+ `);
1500
+ });
1501
+ });
1502
+
1503
+ describe('provider-executed tool calls and results', () => {
1504
+ it('should include providerExecuted flag', () => {
1505
+ const result = convertToLanguageModelMessage({
1506
+ message: {
1507
+ role: 'assistant',
1508
+ content: [
1509
+ {
1510
+ type: 'tool-call',
1511
+ toolName: 'toolName',
1512
+ toolCallId: 'toolCallId',
1513
+ input: {},
1514
+ providerExecuted: true,
1515
+ providerOptions: {
1516
+ 'test-provider': {
1517
+ 'key-a': 'test-value-1',
1518
+ 'key-b': 'test-value-2',
1519
+ },
1520
+ },
1521
+ },
1522
+ {
1523
+ type: 'tool-result',
1524
+ toolCallId: 'toolCallId',
1525
+ toolName: 'toolName',
1526
+ output: { type: 'json', value: { some: 'result' } },
1527
+ providerOptions: {
1528
+ 'test-provider': {
1529
+ 'key-a': 'test-value-1',
1530
+ 'key-b': 'test-value-2',
1531
+ },
1532
+ },
1533
+ },
1534
+ ],
1535
+ },
1536
+ downloadedAssets: {},
1537
+ });
1538
+
1539
+ expect(result).toMatchInlineSnapshot(`
1540
+ {
1541
+ "content": [
1542
+ {
1543
+ "input": {},
1544
+ "providerExecuted": true,
1545
+ "providerOptions": {
1546
+ "test-provider": {
1547
+ "key-a": "test-value-1",
1548
+ "key-b": "test-value-2",
1549
+ },
1550
+ },
1551
+ "toolCallId": "toolCallId",
1552
+ "toolName": "toolName",
1553
+ "type": "tool-call",
1554
+ },
1555
+ {
1556
+ "output": {
1557
+ "type": "json",
1558
+ "value": {
1559
+ "some": "result",
1560
+ },
1561
+ },
1562
+ "providerOptions": {
1563
+ "test-provider": {
1564
+ "key-a": "test-value-1",
1565
+ "key-b": "test-value-2",
1566
+ },
1567
+ },
1568
+ "toolCallId": "toolCallId",
1569
+ "toolName": "toolName",
1570
+ "type": "tool-result",
1571
+ },
1572
+ ],
1573
+ "providerOptions": undefined,
1574
+ "role": "assistant",
1575
+ }
1576
+ `);
1577
+ });
1578
+ });
1579
+
1580
+ describe('file parts', () => {
1581
+ it('should convert file data correctly', async () => {
1582
+ const result = convertToLanguageModelMessage({
1583
+ message: {
1584
+ role: 'assistant',
1585
+ content: [
1586
+ {
1587
+ type: 'file',
1588
+ data: 'dGVzdA==', // "test" in base64
1589
+ mediaType: 'application/pdf',
1590
+ },
1591
+ ],
1592
+ },
1593
+ downloadedAssets: {},
1594
+ });
1595
+
1596
+ expect(result).toEqual({
1597
+ role: 'assistant',
1598
+ content: [
1599
+ {
1600
+ type: 'file',
1601
+ data: 'dGVzdA==',
1602
+ mediaType: 'application/pdf',
1603
+ },
1604
+ ],
1605
+ });
1606
+ });
1607
+
1608
+ it('should preserve filename when present', async () => {
1609
+ const result = convertToLanguageModelMessage({
1610
+ message: {
1611
+ role: 'assistant',
1612
+ content: [
1613
+ {
1614
+ type: 'file',
1615
+ data: 'dGVzdA==',
1616
+ mediaType: 'application/pdf',
1617
+ filename: 'test-document.pdf',
1618
+ },
1619
+ ],
1620
+ },
1621
+ downloadedAssets: {},
1622
+ });
1623
+
1624
+ expect(result).toEqual({
1625
+ role: 'assistant',
1626
+ content: [
1627
+ {
1628
+ type: 'file',
1629
+ data: 'dGVzdA==',
1630
+ mediaType: 'application/pdf',
1631
+ filename: 'test-document.pdf',
1632
+ },
1633
+ ],
1634
+ });
1635
+ });
1636
+
1637
+ it('should handle provider options', async () => {
1638
+ const result = convertToLanguageModelMessage({
1639
+ message: {
1640
+ role: 'assistant',
1641
+ content: [
1642
+ {
1643
+ type: 'file',
1644
+ data: 'dGVzdA==',
1645
+ mediaType: 'application/pdf',
1646
+ providerOptions: {
1647
+ 'test-provider': {
1648
+ 'key-a': 'test-value-1',
1649
+ 'key-b': 'test-value-2',
1650
+ },
1651
+ },
1652
+ },
1653
+ ],
1654
+ },
1655
+ downloadedAssets: {},
1656
+ });
1657
+
1658
+ expect(result).toEqual({
1659
+ role: 'assistant',
1660
+ content: [
1661
+ {
1662
+ type: 'file',
1663
+ data: 'dGVzdA==',
1664
+ mediaType: 'application/pdf',
1665
+ providerOptions: {
1666
+ 'test-provider': {
1667
+ 'key-a': 'test-value-1',
1668
+ 'key-b': 'test-value-2',
1669
+ },
1670
+ },
1671
+ },
1672
+ ],
1673
+ });
1674
+ });
1675
+ });
1676
+ });
1677
+
1678
+ describe('tool message', () => {
1679
+ it('should convert basic tool result message', () => {
1680
+ const result = convertToLanguageModelMessage({
1681
+ message: {
1682
+ role: 'tool',
1683
+ content: [
1684
+ {
1685
+ type: 'tool-result',
1686
+ toolName: 'toolName',
1687
+ toolCallId: 'toolCallId',
1688
+ output: { type: 'json', value: { some: 'result' } },
1689
+ },
1690
+ ],
1691
+ },
1692
+ downloadedAssets: {},
1693
+ });
1694
+
1695
+ expect(result).toMatchInlineSnapshot(`
1696
+ {
1697
+ "content": [
1698
+ {
1699
+ "output": {
1700
+ "type": "json",
1701
+ "value": {
1702
+ "some": "result",
1703
+ },
1704
+ },
1705
+ "providerOptions": undefined,
1706
+ "toolCallId": "toolCallId",
1707
+ "toolName": "toolName",
1708
+ "type": "tool-result",
1709
+ },
1710
+ ],
1711
+ "providerOptions": undefined,
1712
+ "role": "tool",
1713
+ }
1714
+ `);
1715
+ });
1716
+
1717
+ it('should convert tool result with provider metadata', () => {
1718
+ const result = convertToLanguageModelMessage({
1719
+ message: {
1720
+ role: 'tool',
1721
+ content: [
1722
+ {
1723
+ type: 'tool-result',
1724
+ toolName: 'toolName',
1725
+ toolCallId: 'toolCallId',
1726
+ output: { type: 'json', value: { some: 'result' } },
1727
+ providerOptions: {
1728
+ 'test-provider': {
1729
+ 'key-a': 'test-value-1',
1730
+ 'key-b': 'test-value-2',
1731
+ },
1732
+ },
1733
+ },
1734
+ ],
1735
+ },
1736
+ downloadedAssets: {},
1737
+ });
1738
+
1739
+ expect(result).toMatchInlineSnapshot(`
1740
+ {
1741
+ "content": [
1742
+ {
1743
+ "output": {
1744
+ "type": "json",
1745
+ "value": {
1746
+ "some": "result",
1747
+ },
1748
+ },
1749
+ "providerOptions": {
1750
+ "test-provider": {
1751
+ "key-a": "test-value-1",
1752
+ "key-b": "test-value-2",
1753
+ },
1754
+ },
1755
+ "toolCallId": "toolCallId",
1756
+ "toolName": "toolName",
1757
+ "type": "tool-result",
1758
+ },
1759
+ ],
1760
+ "providerOptions": undefined,
1761
+ "role": "tool",
1762
+ }
1763
+ `);
1764
+ });
1765
+
1766
+ it('should include error flag', () => {
1767
+ const result = convertToLanguageModelMessage({
1768
+ message: {
1769
+ role: 'tool',
1770
+ content: [
1771
+ {
1772
+ type: 'tool-result',
1773
+ toolName: 'toolName',
1774
+ toolCallId: 'toolCallId',
1775
+ output: { type: 'json', value: { some: 'result' } },
1776
+ },
1777
+ ],
1778
+ },
1779
+ downloadedAssets: {},
1780
+ });
1781
+
1782
+ expect(result).toMatchInlineSnapshot(`
1783
+ {
1784
+ "content": [
1785
+ {
1786
+ "output": {
1787
+ "type": "json",
1788
+ "value": {
1789
+ "some": "result",
1790
+ },
1791
+ },
1792
+ "providerOptions": undefined,
1793
+ "toolCallId": "toolCallId",
1794
+ "toolName": "toolName",
1795
+ "type": "tool-result",
1796
+ },
1797
+ ],
1798
+ "providerOptions": undefined,
1799
+ "role": "tool",
1800
+ }
1801
+ `);
1802
+ });
1803
+
1804
+ it('should include multipart content', () => {
1805
+ const result = convertToLanguageModelMessage({
1806
+ message: {
1807
+ role: 'tool',
1808
+ content: [
1809
+ {
1810
+ type: 'tool-result',
1811
+ toolName: 'toolName',
1812
+ toolCallId: 'toolCallId',
1813
+ output: {
1814
+ type: 'content',
1815
+ value: [
1816
+ { type: 'file-url', url: 'https://example.com/image.png' },
1817
+ {
1818
+ type: 'file-data',
1819
+ data: 'dGVzdA==',
1820
+ mediaType: 'image/png',
1821
+ },
1822
+ { type: 'file-id', fileId: 'fileId' },
1823
+ { type: 'file-id', fileId: { 'test-provider': 'fileId' } },
1824
+ {
1825
+ type: 'image-data',
1826
+ data: 'dGVzdA==',
1827
+ mediaType: 'image/png',
1828
+ },
1829
+ { type: 'image-url', url: 'https://example.com/image.png' },
1830
+ { type: 'image-file-id', fileId: 'fileId' },
1831
+ {
1832
+ type: 'image-file-id',
1833
+ fileId: { 'test-provider': 'fileId' },
1834
+ },
1835
+ {
1836
+ type: 'custom',
1837
+ providerOptions: {
1838
+ 'test-provider': {
1839
+ 'key-a': 'test-value-1',
1840
+ 'key-b': 'test-value-2',
1841
+ },
1842
+ },
1843
+ },
1844
+ ],
1845
+ },
1846
+ },
1847
+ ],
1848
+ },
1849
+ downloadedAssets: {},
1850
+ });
1851
+
1852
+ expect(result).toMatchInlineSnapshot(`
1853
+ {
1854
+ "content": [
1855
+ {
1856
+ "output": {
1857
+ "type": "content",
1858
+ "value": [
1859
+ {
1860
+ "type": "file-url",
1861
+ "url": "https://example.com/image.png",
1862
+ },
1863
+ {
1864
+ "data": "dGVzdA==",
1865
+ "mediaType": "image/png",
1866
+ "type": "file-data",
1867
+ },
1868
+ {
1869
+ "fileId": "fileId",
1870
+ "type": "file-id",
1871
+ },
1872
+ {
1873
+ "fileId": {
1874
+ "test-provider": "fileId",
1875
+ },
1876
+ "type": "file-id",
1877
+ },
1878
+ {
1879
+ "data": "dGVzdA==",
1880
+ "mediaType": "image/png",
1881
+ "type": "image-data",
1882
+ },
1883
+ {
1884
+ "type": "image-url",
1885
+ "url": "https://example.com/image.png",
1886
+ },
1887
+ {
1888
+ "fileId": "fileId",
1889
+ "type": "image-file-id",
1890
+ },
1891
+ {
1892
+ "fileId": {
1893
+ "test-provider": "fileId",
1894
+ },
1895
+ "type": "image-file-id",
1896
+ },
1897
+ {
1898
+ "providerOptions": {
1899
+ "test-provider": {
1900
+ "key-a": "test-value-1",
1901
+ "key-b": "test-value-2",
1902
+ },
1903
+ },
1904
+ "type": "custom",
1905
+ },
1906
+ ],
1907
+ },
1908
+ "providerOptions": undefined,
1909
+ "toolCallId": "toolCallId",
1910
+ "toolName": "toolName",
1911
+ "type": "tool-result",
1912
+ },
1913
+ ],
1914
+ "providerOptions": undefined,
1915
+ "role": "tool",
1916
+ }
1917
+ `);
1918
+ });
1919
+
1920
+ it('should map deprecated media type to image-data', () => {
1921
+ const result = convertToLanguageModelMessage({
1922
+ message: {
1923
+ role: 'tool',
1924
+ content: [
1925
+ {
1926
+ type: 'tool-result',
1927
+ toolName: 'toolName',
1928
+ toolCallId: 'toolCallId',
1929
+ output: {
1930
+ type: 'content',
1931
+ value: [
1932
+ { type: 'media', data: 'dGVzdA==', mediaType: 'image/png' },
1933
+ ],
1934
+ },
1935
+ },
1936
+ ],
1937
+ },
1938
+ downloadedAssets: {},
1939
+ });
1940
+
1941
+ expect(result).toMatchInlineSnapshot(`
1942
+ {
1943
+ "content": [
1944
+ {
1945
+ "output": {
1946
+ "type": "content",
1947
+ "value": [
1948
+ {
1949
+ "data": "dGVzdA==",
1950
+ "mediaType": "image/png",
1951
+ "type": "image-data",
1952
+ },
1953
+ ],
1954
+ },
1955
+ "providerOptions": undefined,
1956
+ "toolCallId": "toolCallId",
1957
+ "toolName": "toolName",
1958
+ "type": "tool-result",
1959
+ },
1960
+ ],
1961
+ "providerOptions": undefined,
1962
+ "role": "tool",
1963
+ }
1964
+ `);
1965
+ });
1966
+
1967
+ it('should map deprecated media type to file-data', () => {
1968
+ const result = convertToLanguageModelMessage({
1969
+ message: {
1970
+ role: 'tool',
1971
+ content: [
1972
+ {
1973
+ type: 'tool-result',
1974
+ toolName: 'toolName',
1975
+ toolCallId: 'toolCallId',
1976
+ output: {
1977
+ type: 'content',
1978
+ value: [
1979
+ {
1980
+ type: 'media',
1981
+ data: 'dGVzdA==',
1982
+ mediaType: 'application/pdf',
1983
+ },
1984
+ ],
1985
+ },
1986
+ },
1987
+ ],
1988
+ },
1989
+ downloadedAssets: {},
1990
+ });
1991
+
1992
+ expect(result).toMatchInlineSnapshot(`
1993
+ {
1994
+ "content": [
1995
+ {
1996
+ "output": {
1997
+ "type": "content",
1998
+ "value": [
1999
+ {
2000
+ "data": "dGVzdA==",
2001
+ "mediaType": "application/pdf",
2002
+ "type": "file-data",
2003
+ },
2004
+ ],
2005
+ },
2006
+ "providerOptions": undefined,
2007
+ "toolCallId": "toolCallId",
2008
+ "toolName": "toolName",
2009
+ "type": "tool-result",
2010
+ },
2011
+ ],
2012
+ "providerOptions": undefined,
2013
+ "role": "tool",
2014
+ }
2015
+ `);
2016
+ });
2017
+ });
2018
+ });