@fondation-io/ai 7.0.0-beta.45

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 (536) hide show
  1. package/CHANGELOG.md +7687 -0
  2. package/README.md +238 -0
  3. package/dist/index.d.mts +7056 -0
  4. package/dist/index.d.ts +7056 -0
  5. package/dist/index.js +14607 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +14578 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/dist/internal/index.d.mts +303 -0
  10. package/dist/internal/index.d.ts +303 -0
  11. package/dist/internal/index.js +1352 -0
  12. package/dist/internal/index.js.map +1 -0
  13. package/dist/internal/index.mjs +1336 -0
  14. package/dist/internal/index.mjs.map +1 -0
  15. package/dist/test/index.d.mts +265 -0
  16. package/dist/test/index.d.ts +265 -0
  17. package/dist/test/index.js +509 -0
  18. package/dist/test/index.js.map +1 -0
  19. package/dist/test/index.mjs +472 -0
  20. package/dist/test/index.mjs.map +1 -0
  21. package/docs/00-introduction/index.mdx +76 -0
  22. package/docs/02-foundations/01-overview.mdx +43 -0
  23. package/docs/02-foundations/02-providers-and-models.mdx +160 -0
  24. package/docs/02-foundations/03-prompts.mdx +616 -0
  25. package/docs/02-foundations/04-tools.mdx +251 -0
  26. package/docs/02-foundations/05-streaming.mdx +62 -0
  27. package/docs/02-foundations/06-provider-options.mdx +345 -0
  28. package/docs/02-foundations/index.mdx +49 -0
  29. package/docs/02-getting-started/00-choosing-a-provider.mdx +110 -0
  30. package/docs/02-getting-started/01-navigating-the-library.mdx +85 -0
  31. package/docs/02-getting-started/02-nextjs-app-router.mdx +559 -0
  32. package/docs/02-getting-started/03-nextjs-pages-router.mdx +542 -0
  33. package/docs/02-getting-started/04-svelte.mdx +627 -0
  34. package/docs/02-getting-started/05-nuxt.mdx +566 -0
  35. package/docs/02-getting-started/06-nodejs.mdx +512 -0
  36. package/docs/02-getting-started/07-expo.mdx +766 -0
  37. package/docs/02-getting-started/08-tanstack-start.mdx +583 -0
  38. package/docs/02-getting-started/09-coding-agents.mdx +179 -0
  39. package/docs/02-getting-started/index.mdx +44 -0
  40. package/docs/03-agents/01-overview.mdx +96 -0
  41. package/docs/03-agents/02-building-agents.mdx +449 -0
  42. package/docs/03-agents/03-workflows.mdx +386 -0
  43. package/docs/03-agents/04-loop-control.mdx +394 -0
  44. package/docs/03-agents/05-configuring-call-options.mdx +286 -0
  45. package/docs/03-agents/06-memory.mdx +222 -0
  46. package/docs/03-agents/06-subagents.mdx +362 -0
  47. package/docs/03-agents/index.mdx +46 -0
  48. package/docs/03-ai-sdk-core/01-overview.mdx +31 -0
  49. package/docs/03-ai-sdk-core/05-generating-text.mdx +707 -0
  50. package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +498 -0
  51. package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1148 -0
  52. package/docs/03-ai-sdk-core/16-mcp-tools.mdx +383 -0
  53. package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +146 -0
  54. package/docs/03-ai-sdk-core/25-settings.mdx +216 -0
  55. package/docs/03-ai-sdk-core/26-reasoning.mdx +190 -0
  56. package/docs/03-ai-sdk-core/30-embeddings.mdx +236 -0
  57. package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
  58. package/docs/03-ai-sdk-core/35-image-generation.mdx +341 -0
  59. package/docs/03-ai-sdk-core/36-transcription.mdx +227 -0
  60. package/docs/03-ai-sdk-core/37-speech.mdx +169 -0
  61. package/docs/03-ai-sdk-core/38-video-generation.mdx +366 -0
  62. package/docs/03-ai-sdk-core/40-middleware.mdx +485 -0
  63. package/docs/03-ai-sdk-core/45-provider-management.mdx +349 -0
  64. package/docs/03-ai-sdk-core/50-error-handling.mdx +149 -0
  65. package/docs/03-ai-sdk-core/55-testing.mdx +219 -0
  66. package/docs/03-ai-sdk-core/60-telemetry.mdx +391 -0
  67. package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
  68. package/docs/03-ai-sdk-core/65-event-listeners.mdx +1303 -0
  69. package/docs/03-ai-sdk-core/index.mdx +99 -0
  70. package/docs/04-ai-sdk-ui/01-overview.mdx +44 -0
  71. package/docs/04-ai-sdk-ui/02-chatbot.mdx +1320 -0
  72. package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +534 -0
  73. package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +263 -0
  74. package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +682 -0
  75. package/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx +389 -0
  76. package/docs/04-ai-sdk-ui/05-completion.mdx +181 -0
  77. package/docs/04-ai-sdk-ui/08-object-generation.mdx +344 -0
  78. package/docs/04-ai-sdk-ui/20-streaming-data.mdx +397 -0
  79. package/docs/04-ai-sdk-ui/21-error-handling.mdx +190 -0
  80. package/docs/04-ai-sdk-ui/21-transport.mdx +174 -0
  81. package/docs/04-ai-sdk-ui/24-reading-ui-message-streams.mdx +104 -0
  82. package/docs/04-ai-sdk-ui/25-message-metadata.mdx +152 -0
  83. package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +503 -0
  84. package/docs/04-ai-sdk-ui/index.mdx +64 -0
  85. package/docs/05-ai-sdk-rsc/01-overview.mdx +45 -0
  86. package/docs/05-ai-sdk-rsc/02-streaming-react-components.mdx +209 -0
  87. package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +279 -0
  88. package/docs/05-ai-sdk-rsc/03-saving-and-restoring-states.mdx +105 -0
  89. package/docs/05-ai-sdk-rsc/04-multistep-interfaces.mdx +282 -0
  90. package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +157 -0
  91. package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
  92. package/docs/05-ai-sdk-rsc/08-error-handling.mdx +94 -0
  93. package/docs/05-ai-sdk-rsc/09-authentication.mdx +42 -0
  94. package/docs/05-ai-sdk-rsc/10-migrating-to-ui.mdx +722 -0
  95. package/docs/05-ai-sdk-rsc/index.mdx +63 -0
  96. package/docs/06-advanced/01-prompt-engineering.mdx +96 -0
  97. package/docs/06-advanced/02-stopping-streams.mdx +184 -0
  98. package/docs/06-advanced/03-backpressure.mdx +173 -0
  99. package/docs/06-advanced/04-caching.mdx +169 -0
  100. package/docs/06-advanced/05-multiple-streamables.mdx +68 -0
  101. package/docs/06-advanced/06-rate-limiting.mdx +60 -0
  102. package/docs/06-advanced/07-rendering-ui-with-language-models.mdx +225 -0
  103. package/docs/06-advanced/08-model-as-router.mdx +120 -0
  104. package/docs/06-advanced/09-multistep-interfaces.mdx +115 -0
  105. package/docs/06-advanced/09-sequential-generations.mdx +55 -0
  106. package/docs/06-advanced/10-vercel-deployment-guide.mdx +117 -0
  107. package/docs/06-advanced/index.mdx +11 -0
  108. package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +2785 -0
  109. package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3752 -0
  110. package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +332 -0
  111. package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +330 -0
  112. package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +323 -0
  113. package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +251 -0
  114. package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +152 -0
  115. package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +221 -0
  116. package/docs/07-reference/01-ai-sdk-core/13-generate-video.mdx +264 -0
  117. package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +235 -0
  118. package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +973 -0
  119. package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +154 -0
  120. package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +173 -0
  121. package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +150 -0
  122. package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +209 -0
  123. package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +223 -0
  124. package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +423 -0
  125. package/docs/07-reference/01-ai-sdk-core/24-mcp-stdio-transport.mdx +68 -0
  126. package/docs/07-reference/01-ai-sdk-core/25-json-schema.mdx +94 -0
  127. package/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx +109 -0
  128. package/docs/07-reference/01-ai-sdk-core/27-valibot-schema.mdx +58 -0
  129. package/docs/07-reference/01-ai-sdk-core/28-output.mdx +342 -0
  130. package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +435 -0
  131. package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +264 -0
  132. package/docs/07-reference/01-ai-sdk-core/32-validate-ui-messages.mdx +101 -0
  133. package/docs/07-reference/01-ai-sdk-core/33-safe-validate-ui-messages.mdx +113 -0
  134. package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +198 -0
  135. package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +157 -0
  136. package/docs/07-reference/01-ai-sdk-core/50-cosine-similarity.mdx +52 -0
  137. package/docs/07-reference/01-ai-sdk-core/60-wrap-language-model.mdx +59 -0
  138. package/docs/07-reference/01-ai-sdk-core/61-wrap-image-model.mdx +64 -0
  139. package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +74 -0
  140. package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +68 -0
  141. package/docs/07-reference/01-ai-sdk-core/67-simulate-streaming-middleware.mdx +71 -0
  142. package/docs/07-reference/01-ai-sdk-core/68-default-settings-middleware.mdx +80 -0
  143. package/docs/07-reference/01-ai-sdk-core/69-add-tool-input-examples-middleware.mdx +155 -0
  144. package/docs/07-reference/01-ai-sdk-core/70-extract-json-middleware.mdx +147 -0
  145. package/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx +84 -0
  146. package/docs/07-reference/01-ai-sdk-core/71-has-tool-call.mdx +120 -0
  147. package/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx +94 -0
  148. package/docs/07-reference/01-ai-sdk-core/80-smooth-stream.mdx +145 -0
  149. package/docs/07-reference/01-ai-sdk-core/90-generate-id.mdx +30 -0
  150. package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
  151. package/docs/07-reference/01-ai-sdk-core/92-default-generated-file.mdx +68 -0
  152. package/docs/07-reference/01-ai-sdk-core/index.mdx +160 -0
  153. package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +493 -0
  154. package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +185 -0
  155. package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +196 -0
  156. package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +231 -0
  157. package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +108 -0
  158. package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +162 -0
  159. package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +119 -0
  160. package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +77 -0
  161. package/docs/07-reference/02-ai-sdk-ui/43-read-ui-message-stream.mdx +57 -0
  162. package/docs/07-reference/02-ai-sdk-ui/46-infer-ui-tools.mdx +99 -0
  163. package/docs/07-reference/02-ai-sdk-ui/47-infer-ui-tool.mdx +75 -0
  164. package/docs/07-reference/02-ai-sdk-ui/50-direct-chat-transport.mdx +333 -0
  165. package/docs/07-reference/02-ai-sdk-ui/index.mdx +89 -0
  166. package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +767 -0
  167. package/docs/07-reference/03-ai-sdk-rsc/02-create-ai.mdx +90 -0
  168. package/docs/07-reference/03-ai-sdk-rsc/03-create-streamable-ui.mdx +91 -0
  169. package/docs/07-reference/03-ai-sdk-rsc/04-create-streamable-value.mdx +78 -0
  170. package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +79 -0
  171. package/docs/07-reference/03-ai-sdk-rsc/06-get-ai-state.mdx +50 -0
  172. package/docs/07-reference/03-ai-sdk-rsc/07-get-mutable-ai-state.mdx +70 -0
  173. package/docs/07-reference/03-ai-sdk-rsc/08-use-ai-state.mdx +26 -0
  174. package/docs/07-reference/03-ai-sdk-rsc/09-use-actions.mdx +42 -0
  175. package/docs/07-reference/03-ai-sdk-rsc/10-use-ui-state.mdx +35 -0
  176. package/docs/07-reference/03-ai-sdk-rsc/11-use-streamable-value.mdx +46 -0
  177. package/docs/07-reference/03-ai-sdk-rsc/20-render.mdx +266 -0
  178. package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
  179. package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +31 -0
  180. package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +28 -0
  181. package/docs/07-reference/05-ai-sdk-errors/ai-empty-response-body-error.mdx +24 -0
  182. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-argument-error.mdx +26 -0
  183. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content-error.mdx +26 -0
  184. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-message-role-error.mdx +25 -0
  185. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +47 -0
  186. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-response-data-error.mdx +25 -0
  187. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-error.mdx +24 -0
  188. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-input-error.mdx +27 -0
  189. package/docs/07-reference/05-ai-sdk-errors/ai-json-parse-error.mdx +25 -0
  190. package/docs/07-reference/05-ai-sdk-errors/ai-load-api-key-error.mdx +24 -0
  191. package/docs/07-reference/05-ai-sdk-errors/ai-load-setting-error.mdx +24 -0
  192. package/docs/07-reference/05-ai-sdk-errors/ai-message-conversion-error.mdx +25 -0
  193. package/docs/07-reference/05-ai-sdk-errors/ai-no-content-generated-error.mdx +24 -0
  194. package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +36 -0
  195. package/docs/07-reference/05-ai-sdk-errors/ai-no-object-generated-error.mdx +43 -0
  196. package/docs/07-reference/05-ai-sdk-errors/ai-no-output-generated-error.mdx +25 -0
  197. package/docs/07-reference/05-ai-sdk-errors/ai-no-speech-generated-error.mdx +24 -0
  198. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +26 -0
  199. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +28 -0
  200. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-tool-error.mdx +26 -0
  201. package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +24 -0
  202. package/docs/07-reference/05-ai-sdk-errors/ai-no-video-generated-error.mdx +39 -0
  203. package/docs/07-reference/05-ai-sdk-errors/ai-retry-error.mdx +27 -0
  204. package/docs/07-reference/05-ai-sdk-errors/ai-too-many-embedding-values-for-call-error.mdx +27 -0
  205. package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-not-found-for-approval-error.mdx +25 -0
  206. package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-repair-error.mdx +28 -0
  207. package/docs/07-reference/05-ai-sdk-errors/ai-type-validation-error.mdx +25 -0
  208. package/docs/07-reference/05-ai-sdk-errors/ai-ui-message-stream-error.mdx +67 -0
  209. package/docs/07-reference/05-ai-sdk-errors/ai-unsupported-functionality-error.mdx +25 -0
  210. package/docs/07-reference/05-ai-sdk-errors/index.mdx +39 -0
  211. package/docs/07-reference/index.mdx +28 -0
  212. package/docs/08-migration-guides/00-versioning.mdx +46 -0
  213. package/docs/08-migration-guides/23-migration-guide-7-0.mdx +95 -0
  214. package/docs/08-migration-guides/24-migration-guide-6-0.mdx +823 -0
  215. package/docs/08-migration-guides/25-migration-guide-5-0-data.mdx +882 -0
  216. package/docs/08-migration-guides/26-migration-guide-5-0.mdx +3427 -0
  217. package/docs/08-migration-guides/27-migration-guide-4-2.mdx +99 -0
  218. package/docs/08-migration-guides/28-migration-guide-4-1.mdx +14 -0
  219. package/docs/08-migration-guides/29-migration-guide-4-0.mdx +1157 -0
  220. package/docs/08-migration-guides/36-migration-guide-3-4.mdx +14 -0
  221. package/docs/08-migration-guides/37-migration-guide-3-3.mdx +64 -0
  222. package/docs/08-migration-guides/38-migration-guide-3-2.mdx +46 -0
  223. package/docs/08-migration-guides/39-migration-guide-3-1.mdx +168 -0
  224. package/docs/08-migration-guides/index.mdx +22 -0
  225. package/docs/09-troubleshooting/01-azure-stream-slow.mdx +33 -0
  226. package/docs/09-troubleshooting/03-server-actions-in-client-components.mdx +40 -0
  227. package/docs/09-troubleshooting/04-strange-stream-output.mdx +36 -0
  228. package/docs/09-troubleshooting/05-streamable-ui-errors.mdx +16 -0
  229. package/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx +106 -0
  230. package/docs/09-troubleshooting/06-streaming-not-working-when-deployed.mdx +31 -0
  231. package/docs/09-troubleshooting/06-streaming-not-working-when-proxied.mdx +31 -0
  232. package/docs/09-troubleshooting/06-timeout-on-vercel.mdx +60 -0
  233. package/docs/09-troubleshooting/07-unclosed-streams.mdx +34 -0
  234. package/docs/09-troubleshooting/08-use-chat-failed-to-parse-stream.mdx +26 -0
  235. package/docs/09-troubleshooting/09-client-stream-error.mdx +25 -0
  236. package/docs/09-troubleshooting/10-use-chat-tools-no-response.mdx +32 -0
  237. package/docs/09-troubleshooting/11-use-chat-custom-request-options.mdx +149 -0
  238. package/docs/09-troubleshooting/12-typescript-performance-zod.mdx +46 -0
  239. package/docs/09-troubleshooting/12-use-chat-an-error-occurred.mdx +59 -0
  240. package/docs/09-troubleshooting/13-repeated-assistant-messages.mdx +73 -0
  241. package/docs/09-troubleshooting/14-stream-abort-handling.mdx +73 -0
  242. package/docs/09-troubleshooting/14-tool-calling-with-structured-outputs.mdx +48 -0
  243. package/docs/09-troubleshooting/15-abort-breaks-resumable-streams.mdx +55 -0
  244. package/docs/09-troubleshooting/15-stream-text-not-working.mdx +33 -0
  245. package/docs/09-troubleshooting/16-streaming-status-delay.mdx +63 -0
  246. package/docs/09-troubleshooting/17-use-chat-stale-body-data.mdx +141 -0
  247. package/docs/09-troubleshooting/18-ontoolcall-type-narrowing.mdx +66 -0
  248. package/docs/09-troubleshooting/19-unsupported-model-version.mdx +50 -0
  249. package/docs/09-troubleshooting/20-no-object-generated-content-filter.mdx +76 -0
  250. package/docs/09-troubleshooting/21-missing-tool-results-error.mdx +82 -0
  251. package/docs/09-troubleshooting/30-model-is-not-assignable-to-type.mdx +21 -0
  252. package/docs/09-troubleshooting/40-typescript-cannot-find-namespace-jsx.mdx +24 -0
  253. package/docs/09-troubleshooting/50-react-maximum-update-depth-exceeded.mdx +39 -0
  254. package/docs/09-troubleshooting/60-jest-cannot-find-module-ai-rsc.mdx +22 -0
  255. package/docs/09-troubleshooting/70-high-memory-usage-with-images.mdx +108 -0
  256. package/docs/09-troubleshooting/index.mdx +11 -0
  257. package/internal.d.ts +1 -0
  258. package/package.json +120 -0
  259. package/src/agent/agent.ts +156 -0
  260. package/src/agent/create-agent-ui-stream-response.ts +61 -0
  261. package/src/agent/create-agent-ui-stream.ts +84 -0
  262. package/src/agent/index.ts +37 -0
  263. package/src/agent/infer-agent-tools.ts +7 -0
  264. package/src/agent/infer-agent-ui-message.ts +11 -0
  265. package/src/agent/pipe-agent-ui-stream-to-response.ts +64 -0
  266. package/src/agent/tool-loop-agent-settings.ts +252 -0
  267. package/src/agent/tool-loop-agent.ts +205 -0
  268. package/src/embed/embed-events.ts +181 -0
  269. package/src/embed/embed-many-result.ts +53 -0
  270. package/src/embed/embed-many.ts +428 -0
  271. package/src/embed/embed-result.ts +50 -0
  272. package/src/embed/embed.ts +266 -0
  273. package/src/embed/index.ts +5 -0
  274. package/src/error/index.ts +37 -0
  275. package/src/error/invalid-argument-error.ts +34 -0
  276. package/src/error/invalid-stream-part-error.ts +28 -0
  277. package/src/error/invalid-tool-approval-error.ts +26 -0
  278. package/src/error/invalid-tool-input-error.ts +33 -0
  279. package/src/error/missing-tool-result-error.ts +28 -0
  280. package/src/error/no-image-generated-error.ts +39 -0
  281. package/src/error/no-object-generated-error.ts +70 -0
  282. package/src/error/no-output-generated-error.ts +26 -0
  283. package/src/error/no-speech-generated-error.ts +28 -0
  284. package/src/error/no-such-tool-error.ts +35 -0
  285. package/src/error/no-transcript-generated-error.ts +30 -0
  286. package/src/error/no-video-generated-error.ts +57 -0
  287. package/src/error/tool-call-not-found-for-approval-error.ts +32 -0
  288. package/src/error/tool-call-repair-error.ts +30 -0
  289. package/src/error/ui-message-stream-error.ts +48 -0
  290. package/src/error/unsupported-model-version-error.ts +23 -0
  291. package/src/error/verify-no-object-generated-error.ts +27 -0
  292. package/src/generate-image/generate-image-result.ts +42 -0
  293. package/src/generate-image/generate-image.ts +361 -0
  294. package/src/generate-image/index.ts +18 -0
  295. package/src/generate-object/generate-object-result.ts +67 -0
  296. package/src/generate-object/generate-object.ts +514 -0
  297. package/src/generate-object/index.ts +9 -0
  298. package/src/generate-object/inject-json-instruction.ts +30 -0
  299. package/src/generate-object/output-strategy.ts +415 -0
  300. package/src/generate-object/parse-and-validate-object-result.ts +111 -0
  301. package/src/generate-object/repair-text.ts +12 -0
  302. package/src/generate-object/stream-object-result.ts +120 -0
  303. package/src/generate-object/stream-object.ts +984 -0
  304. package/src/generate-object/validate-object-generation-input.ts +144 -0
  305. package/src/generate-speech/generate-speech-result.ts +30 -0
  306. package/src/generate-speech/generate-speech.ts +191 -0
  307. package/src/generate-speech/generated-audio-file.ts +65 -0
  308. package/src/generate-speech/index.ts +3 -0
  309. package/src/generate-text/collect-tool-approvals.ts +116 -0
  310. package/src/generate-text/content-part.ts +31 -0
  311. package/src/generate-text/core-events.ts +390 -0
  312. package/src/generate-text/create-execute-tools-transformation.ts +144 -0
  313. package/src/generate-text/execute-tool-call.ts +190 -0
  314. package/src/generate-text/extract-reasoning-content.ts +17 -0
  315. package/src/generate-text/extract-text-content.ts +15 -0
  316. package/src/generate-text/generate-text-result.ts +168 -0
  317. package/src/generate-text/generate-text.ts +1445 -0
  318. package/src/generate-text/generated-file.ts +70 -0
  319. package/src/generate-text/index.ts +78 -0
  320. package/src/generate-text/invoke-tool-callbacks-from-stream.ts +81 -0
  321. package/src/generate-text/is-approval-needed.ts +29 -0
  322. package/src/generate-text/output-utils.ts +23 -0
  323. package/src/generate-text/output.ts +590 -0
  324. package/src/generate-text/parse-tool-call.ts +188 -0
  325. package/src/generate-text/prepare-step.ts +103 -0
  326. package/src/generate-text/prune-messages.ts +167 -0
  327. package/src/generate-text/reasoning-output.ts +99 -0
  328. package/src/generate-text/reasoning.ts +10 -0
  329. package/src/generate-text/response-message.ts +10 -0
  330. package/src/generate-text/smooth-stream.ts +162 -0
  331. package/src/generate-text/step-result.ts +310 -0
  332. package/src/generate-text/stop-condition.ts +29 -0
  333. package/src/generate-text/stream-model-call.ts +418 -0
  334. package/src/generate-text/stream-text-result.ts +536 -0
  335. package/src/generate-text/stream-text.ts +2696 -0
  336. package/src/generate-text/to-response-messages.ts +195 -0
  337. package/src/generate-text/tool-approval-request-output.ts +21 -0
  338. package/src/generate-text/tool-call-repair-function.ts +27 -0
  339. package/src/generate-text/tool-call.ts +47 -0
  340. package/src/generate-text/tool-error.ts +34 -0
  341. package/src/generate-text/tool-output-denied.ts +21 -0
  342. package/src/generate-text/tool-output.ts +7 -0
  343. package/src/generate-text/tool-result.ts +36 -0
  344. package/src/generate-text/tool-set.ts +14 -0
  345. package/src/generate-video/generate-video-result.ts +36 -0
  346. package/src/generate-video/generate-video.ts +402 -0
  347. package/src/generate-video/index.ts +3 -0
  348. package/src/global.ts +36 -0
  349. package/src/index.ts +49 -0
  350. package/src/logger/index.ts +6 -0
  351. package/src/logger/log-warnings.ts +140 -0
  352. package/src/middleware/add-tool-input-examples-middleware.ts +90 -0
  353. package/src/middleware/default-embedding-settings-middleware.ts +22 -0
  354. package/src/middleware/default-settings-middleware.ts +33 -0
  355. package/src/middleware/extract-json-middleware.ts +197 -0
  356. package/src/middleware/extract-reasoning-middleware.ts +249 -0
  357. package/src/middleware/index.ts +10 -0
  358. package/src/middleware/simulate-streaming-middleware.ts +79 -0
  359. package/src/middleware/wrap-embedding-model.ts +89 -0
  360. package/src/middleware/wrap-image-model.ts +92 -0
  361. package/src/middleware/wrap-language-model.ts +108 -0
  362. package/src/middleware/wrap-provider.ts +51 -0
  363. package/src/model/as-embedding-model-v3.ts +24 -0
  364. package/src/model/as-embedding-model-v4.ts +25 -0
  365. package/src/model/as-image-model-v3.ts +24 -0
  366. package/src/model/as-image-model-v4.ts +21 -0
  367. package/src/model/as-language-model-v3.ts +103 -0
  368. package/src/model/as-language-model-v4.ts +25 -0
  369. package/src/model/as-provider-v3.ts +36 -0
  370. package/src/model/as-provider-v4.ts +47 -0
  371. package/src/model/as-reranking-model-v4.ts +16 -0
  372. package/src/model/as-speech-model-v3.ts +24 -0
  373. package/src/model/as-speech-model-v4.ts +21 -0
  374. package/src/model/as-transcription-model-v3.ts +24 -0
  375. package/src/model/as-transcription-model-v4.ts +25 -0
  376. package/src/model/as-video-model-v4.ts +19 -0
  377. package/src/model/resolve-model.ts +172 -0
  378. package/src/prompt/call-settings.ts +169 -0
  379. package/src/prompt/content-part.ts +236 -0
  380. package/src/prompt/convert-to-language-model-prompt.ts +548 -0
  381. package/src/prompt/create-tool-model-output.ts +34 -0
  382. package/src/prompt/data-content.ts +134 -0
  383. package/src/prompt/index.ts +27 -0
  384. package/src/prompt/invalid-data-content-error.ts +29 -0
  385. package/src/prompt/invalid-message-role-error.ts +27 -0
  386. package/src/prompt/message-conversion-error.ts +28 -0
  387. package/src/prompt/message.ts +72 -0
  388. package/src/prompt/prepare-call-settings.ts +110 -0
  389. package/src/prompt/prepare-tools-and-tool-choice.ts +86 -0
  390. package/src/prompt/prompt.ts +43 -0
  391. package/src/prompt/split-data-url.ts +17 -0
  392. package/src/prompt/standardize-prompt.ts +99 -0
  393. package/src/prompt/wrap-gateway-error.ts +29 -0
  394. package/src/registry/custom-provider.ts +210 -0
  395. package/src/registry/index.ts +7 -0
  396. package/src/registry/no-such-provider-error.ts +41 -0
  397. package/src/registry/provider-registry.ts +331 -0
  398. package/src/rerank/index.ts +8 -0
  399. package/src/rerank/rerank-events.ts +189 -0
  400. package/src/rerank/rerank-result.ts +70 -0
  401. package/src/rerank/rerank.ts +348 -0
  402. package/src/telemetry/assemble-operation-name.ts +21 -0
  403. package/src/telemetry/get-base-telemetry-attributes.ts +45 -0
  404. package/src/telemetry/get-global-telemetry-integration.ts +126 -0
  405. package/src/telemetry/get-tracer.ts +20 -0
  406. package/src/telemetry/index.ts +4 -0
  407. package/src/telemetry/noop-tracer.ts +69 -0
  408. package/src/telemetry/open-telemetry-integration.ts +875 -0
  409. package/src/telemetry/record-span.ts +75 -0
  410. package/src/telemetry/select-telemetry-attributes.ts +78 -0
  411. package/src/telemetry/stringify-for-telemetry.ts +33 -0
  412. package/src/telemetry/telemetry-integration-registry.ts +22 -0
  413. package/src/telemetry/telemetry-integration.ts +139 -0
  414. package/src/telemetry/telemetry-settings.ts +55 -0
  415. package/src/test/mock-embedding-model-v2.ts +35 -0
  416. package/src/test/mock-embedding-model-v3.ts +48 -0
  417. package/src/test/mock-embedding-model-v4.ts +48 -0
  418. package/src/test/mock-image-model-v2.ts +28 -0
  419. package/src/test/mock-image-model-v3.ts +28 -0
  420. package/src/test/mock-image-model-v4.ts +28 -0
  421. package/src/test/mock-language-model-v2.ts +72 -0
  422. package/src/test/mock-language-model-v3.ts +77 -0
  423. package/src/test/mock-language-model-v4.ts +77 -0
  424. package/src/test/mock-provider-v2.ts +68 -0
  425. package/src/test/mock-provider-v3.ts +80 -0
  426. package/src/test/mock-provider-v4.ts +80 -0
  427. package/src/test/mock-reranking-model-v3.ts +25 -0
  428. package/src/test/mock-reranking-model-v4.ts +25 -0
  429. package/src/test/mock-server-response.ts +69 -0
  430. package/src/test/mock-speech-model-v2.ts +24 -0
  431. package/src/test/mock-speech-model-v3.ts +24 -0
  432. package/src/test/mock-speech-model-v4.ts +24 -0
  433. package/src/test/mock-tracer.ts +156 -0
  434. package/src/test/mock-transcription-model-v2.ts +24 -0
  435. package/src/test/mock-transcription-model-v3.ts +24 -0
  436. package/src/test/mock-transcription-model-v4.ts +24 -0
  437. package/src/test/mock-values.ts +4 -0
  438. package/src/test/mock-video-model-v3.ts +28 -0
  439. package/src/test/mock-video-model-v4.ts +28 -0
  440. package/src/test/not-implemented.ts +3 -0
  441. package/src/text-stream/create-text-stream-response.ts +30 -0
  442. package/src/text-stream/index.ts +2 -0
  443. package/src/text-stream/pipe-text-stream-to-response.ts +38 -0
  444. package/src/transcribe/index.ts +2 -0
  445. package/src/transcribe/transcribe-result.ts +60 -0
  446. package/src/transcribe/transcribe.ts +187 -0
  447. package/src/types/embedding-model-middleware.ts +15 -0
  448. package/src/types/embedding-model.ts +20 -0
  449. package/src/types/image-model-middleware.ts +15 -0
  450. package/src/types/image-model-response-metadata.ts +16 -0
  451. package/src/types/image-model.ts +19 -0
  452. package/src/types/index.ts +29 -0
  453. package/src/types/json-value.ts +15 -0
  454. package/src/types/language-model-middleware.ts +15 -0
  455. package/src/types/language-model-request-metadata.ts +6 -0
  456. package/src/types/language-model-response-metadata.ts +21 -0
  457. package/src/types/language-model.ts +106 -0
  458. package/src/types/provider-metadata.ts +16 -0
  459. package/src/types/provider.ts +55 -0
  460. package/src/types/reranking-model.ts +6 -0
  461. package/src/types/speech-model-response-metadata.ts +21 -0
  462. package/src/types/speech-model.ts +10 -0
  463. package/src/types/transcription-model-response-metadata.ts +16 -0
  464. package/src/types/transcription-model.ts +14 -0
  465. package/src/types/usage.ts +200 -0
  466. package/src/types/video-model-response-metadata.ts +28 -0
  467. package/src/types/video-model.ts +15 -0
  468. package/src/types/warning.ts +7 -0
  469. package/src/ui/call-completion-api.ts +157 -0
  470. package/src/ui/chat-transport.ts +83 -0
  471. package/src/ui/chat.ts +786 -0
  472. package/src/ui/convert-file-list-to-file-ui-parts.ts +36 -0
  473. package/src/ui/convert-to-model-messages.ts +403 -0
  474. package/src/ui/default-chat-transport.ts +36 -0
  475. package/src/ui/direct-chat-transport.ts +117 -0
  476. package/src/ui/http-chat-transport.ts +273 -0
  477. package/src/ui/index.ts +76 -0
  478. package/src/ui/last-assistant-message-is-complete-with-approval-responses.ts +44 -0
  479. package/src/ui/last-assistant-message-is-complete-with-tool-calls.ts +39 -0
  480. package/src/ui/process-text-stream.ts +16 -0
  481. package/src/ui/process-ui-message-stream.ts +858 -0
  482. package/src/ui/text-stream-chat-transport.ts +23 -0
  483. package/src/ui/transform-text-to-ui-message-stream.ts +27 -0
  484. package/src/ui/ui-messages.ts +602 -0
  485. package/src/ui/use-completion.ts +84 -0
  486. package/src/ui/validate-ui-messages.ts +521 -0
  487. package/src/ui-message-stream/create-ui-message-stream-response.ts +44 -0
  488. package/src/ui-message-stream/create-ui-message-stream.ts +145 -0
  489. package/src/ui-message-stream/get-response-ui-message-id.ts +35 -0
  490. package/src/ui-message-stream/handle-ui-message-stream-finish.ts +170 -0
  491. package/src/ui-message-stream/index.ts +14 -0
  492. package/src/ui-message-stream/json-to-sse-transform-stream.ts +17 -0
  493. package/src/ui-message-stream/pipe-ui-message-stream-to-response.ts +51 -0
  494. package/src/ui-message-stream/read-ui-message-stream.ts +87 -0
  495. package/src/ui-message-stream/ui-message-chunks.ts +372 -0
  496. package/src/ui-message-stream/ui-message-stream-headers.ts +7 -0
  497. package/src/ui-message-stream/ui-message-stream-on-finish-callback.ts +32 -0
  498. package/src/ui-message-stream/ui-message-stream-on-step-finish-callback.ts +25 -0
  499. package/src/ui-message-stream/ui-message-stream-response-init.ts +14 -0
  500. package/src/ui-message-stream/ui-message-stream-writer.ts +24 -0
  501. package/src/util/as-array.ts +3 -0
  502. package/src/util/async-iterable-stream.ts +94 -0
  503. package/src/util/consume-stream.ts +31 -0
  504. package/src/util/cosine-similarity.ts +46 -0
  505. package/src/util/create-resolvable-promise.ts +30 -0
  506. package/src/util/create-stitchable-stream.ts +112 -0
  507. package/src/util/data-url.ts +17 -0
  508. package/src/util/deep-partial.ts +84 -0
  509. package/src/util/detect-media-type.ts +226 -0
  510. package/src/util/download/create-download.ts +13 -0
  511. package/src/util/download/download-function.ts +45 -0
  512. package/src/util/download/download.ts +74 -0
  513. package/src/util/error-handler.ts +1 -0
  514. package/src/util/fix-json.ts +401 -0
  515. package/src/util/get-potential-start-index.ts +39 -0
  516. package/src/util/index.ts +12 -0
  517. package/src/util/is-deep-equal-data.ts +48 -0
  518. package/src/util/is-non-empty-object.ts +5 -0
  519. package/src/util/job.ts +1 -0
  520. package/src/util/log-v2-compatibility-warning.ts +21 -0
  521. package/src/util/merge-abort-signals.ts +43 -0
  522. package/src/util/merge-objects.ts +79 -0
  523. package/src/util/notify.ts +22 -0
  524. package/src/util/now.ts +4 -0
  525. package/src/util/parse-partial-json.ts +30 -0
  526. package/src/util/prepare-headers.ts +14 -0
  527. package/src/util/prepare-retries.ts +47 -0
  528. package/src/util/retry-error.ts +41 -0
  529. package/src/util/retry-with-exponential-backoff.ts +154 -0
  530. package/src/util/serial-job-executor.ts +36 -0
  531. package/src/util/simulate-readable-stream.ts +39 -0
  532. package/src/util/split-array.ts +20 -0
  533. package/src/util/value-of.ts +65 -0
  534. package/src/util/write-to-server-response.ts +49 -0
  535. package/src/version.ts +5 -0
  536. package/test.d.ts +1 -0
@@ -0,0 +1,1352 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name5 in all)
8
+ __defProp(target, name5, { get: all[name5], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // internal/index.ts
21
+ var internal_exports = {};
22
+ __export(internal_exports, {
23
+ asLanguageModelUsage: () => asLanguageModelUsage,
24
+ convertAsyncIteratorToReadableStream: () => import_provider_utils9.convertAsyncIteratorToReadableStream,
25
+ convertToLanguageModelPrompt: () => convertToLanguageModelPrompt,
26
+ prepareCallSettings: () => prepareCallSettings,
27
+ prepareRetries: () => prepareRetries,
28
+ prepareToolsAndToolChoice: () => prepareToolsAndToolChoice,
29
+ standardizePrompt: () => standardizePrompt
30
+ });
31
+ module.exports = __toCommonJS(internal_exports);
32
+ var import_provider_utils9 = require("@ai-sdk/provider-utils");
33
+
34
+ // src/prompt/convert-to-language-model-prompt.ts
35
+ var import_provider_utils5 = require("@ai-sdk/provider-utils");
36
+
37
+ // src/util/detect-media-type.ts
38
+ var import_provider_utils = require("@ai-sdk/provider-utils");
39
+ var imageMediaTypeSignatures = [
40
+ {
41
+ mediaType: "image/gif",
42
+ bytesPrefix: [71, 73, 70]
43
+ // GIF
44
+ },
45
+ {
46
+ mediaType: "image/png",
47
+ bytesPrefix: [137, 80, 78, 71]
48
+ // PNG
49
+ },
50
+ {
51
+ mediaType: "image/jpeg",
52
+ bytesPrefix: [255, 216]
53
+ // JPEG
54
+ },
55
+ {
56
+ mediaType: "image/webp",
57
+ bytesPrefix: [
58
+ 82,
59
+ 73,
60
+ 70,
61
+ 70,
62
+ // "RIFF"
63
+ null,
64
+ null,
65
+ null,
66
+ null,
67
+ // file size (variable)
68
+ 87,
69
+ 69,
70
+ 66,
71
+ 80
72
+ // "WEBP"
73
+ ]
74
+ },
75
+ {
76
+ mediaType: "image/bmp",
77
+ bytesPrefix: [66, 77]
78
+ },
79
+ {
80
+ mediaType: "image/tiff",
81
+ bytesPrefix: [73, 73, 42, 0]
82
+ },
83
+ {
84
+ mediaType: "image/tiff",
85
+ bytesPrefix: [77, 77, 0, 42]
86
+ },
87
+ {
88
+ mediaType: "image/avif",
89
+ bytesPrefix: [
90
+ 0,
91
+ 0,
92
+ 0,
93
+ 32,
94
+ 102,
95
+ 116,
96
+ 121,
97
+ 112,
98
+ 97,
99
+ 118,
100
+ 105,
101
+ 102
102
+ ]
103
+ },
104
+ {
105
+ mediaType: "image/heic",
106
+ bytesPrefix: [
107
+ 0,
108
+ 0,
109
+ 0,
110
+ 32,
111
+ 102,
112
+ 116,
113
+ 121,
114
+ 112,
115
+ 104,
116
+ 101,
117
+ 105,
118
+ 99
119
+ ]
120
+ }
121
+ ];
122
+ var stripID3 = (data) => {
123
+ const bytes = typeof data === "string" ? (0, import_provider_utils.convertBase64ToUint8Array)(data) : data;
124
+ const id3Size = (bytes[6] & 127) << 21 | (bytes[7] & 127) << 14 | (bytes[8] & 127) << 7 | bytes[9] & 127;
125
+ return bytes.slice(id3Size + 10);
126
+ };
127
+ function stripID3TagsIfPresent(data) {
128
+ const hasId3 = typeof data === "string" && data.startsWith("SUQz") || typeof data !== "string" && data.length > 10 && data[0] === 73 && // 'I'
129
+ data[1] === 68 && // 'D'
130
+ data[2] === 51;
131
+ return hasId3 ? stripID3(data) : data;
132
+ }
133
+ function detectMediaType({
134
+ data,
135
+ signatures
136
+ }) {
137
+ const processedData = stripID3TagsIfPresent(data);
138
+ const bytes = typeof processedData === "string" ? (0, import_provider_utils.convertBase64ToUint8Array)(
139
+ processedData.substring(0, Math.min(processedData.length, 24))
140
+ ) : processedData;
141
+ for (const signature of signatures) {
142
+ if (bytes.length >= signature.bytesPrefix.length && signature.bytesPrefix.every(
143
+ (byte, index) => byte === null || bytes[index] === byte
144
+ )) {
145
+ return signature.mediaType;
146
+ }
147
+ }
148
+ return void 0;
149
+ }
150
+
151
+ // src/util/download/download.ts
152
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
153
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
154
+
155
+ // src/version.ts
156
+ var VERSION = true ? "7.0.0-beta.45" : "0.0.0-test";
157
+
158
+ // src/util/download/download.ts
159
+ var download = async ({
160
+ url,
161
+ maxBytes,
162
+ abortSignal
163
+ }) => {
164
+ var _a5;
165
+ const urlText = url.toString();
166
+ (0, import_provider_utils2.validateDownloadUrl)(urlText);
167
+ try {
168
+ const response = await fetch(urlText, {
169
+ headers: (0, import_provider_utils3.withUserAgentSuffix)(
170
+ {},
171
+ `ai-sdk/${VERSION}`,
172
+ (0, import_provider_utils3.getRuntimeEnvironmentUserAgent)()
173
+ ),
174
+ signal: abortSignal
175
+ });
176
+ if (response.redirected) {
177
+ (0, import_provider_utils2.validateDownloadUrl)(response.url);
178
+ }
179
+ if (!response.ok) {
180
+ throw new import_provider_utils2.DownloadError({
181
+ url: urlText,
182
+ statusCode: response.status,
183
+ statusText: response.statusText
184
+ });
185
+ }
186
+ const data = await (0, import_provider_utils2.readResponseWithSizeLimit)({
187
+ response,
188
+ url: urlText,
189
+ maxBytes: maxBytes != null ? maxBytes : import_provider_utils2.DEFAULT_MAX_DOWNLOAD_SIZE
190
+ });
191
+ return {
192
+ data,
193
+ mediaType: (_a5 = response.headers.get("content-type")) != null ? _a5 : void 0
194
+ };
195
+ } catch (error) {
196
+ if (import_provider_utils2.DownloadError.isInstance(error)) {
197
+ throw error;
198
+ }
199
+ throw new import_provider_utils2.DownloadError({ url: urlText, cause: error });
200
+ }
201
+ };
202
+
203
+ // src/util/download/download-function.ts
204
+ var createDefaultDownloadFunction = (download2 = download) => (requestedDownloads) => Promise.all(
205
+ requestedDownloads.map(
206
+ async (requestedDownload) => requestedDownload.isUrlSupportedByModel ? null : download2(requestedDownload)
207
+ )
208
+ );
209
+
210
+ // src/prompt/data-content.ts
211
+ var import_provider = require("@ai-sdk/provider");
212
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
213
+ var import_v4 = require("zod/v4");
214
+
215
+ // src/prompt/split-data-url.ts
216
+ function splitDataUrl(dataUrl) {
217
+ try {
218
+ const [header, base64Content] = dataUrl.split(",");
219
+ return {
220
+ mediaType: header.split(";")[0].split(":")[1],
221
+ base64Content
222
+ };
223
+ } catch (error) {
224
+ return {
225
+ mediaType: void 0,
226
+ base64Content: void 0
227
+ };
228
+ }
229
+ }
230
+
231
+ // src/prompt/data-content.ts
232
+ var dataContentSchema = import_v4.z.union([
233
+ import_v4.z.string(),
234
+ import_v4.z.instanceof(Uint8Array),
235
+ import_v4.z.instanceof(ArrayBuffer),
236
+ import_v4.z.custom(
237
+ // Buffer might not be available in some environments such as CloudFlare:
238
+ (value) => {
239
+ var _a5, _b;
240
+ return (_b = (_a5 = globalThis.Buffer) == null ? void 0 : _a5.isBuffer(value)) != null ? _b : false;
241
+ },
242
+ { message: "Must be a Buffer" }
243
+ )
244
+ ]);
245
+ function convertToLanguageModelV4DataContent(content) {
246
+ if (content instanceof Uint8Array) {
247
+ return { data: content, mediaType: void 0 };
248
+ }
249
+ if (content instanceof ArrayBuffer) {
250
+ return { data: new Uint8Array(content), mediaType: void 0 };
251
+ }
252
+ if (typeof content === "string") {
253
+ try {
254
+ content = new URL(content);
255
+ } catch (error) {
256
+ }
257
+ }
258
+ if (content instanceof URL && content.protocol === "data:") {
259
+ const { mediaType: dataUrlMediaType, base64Content } = splitDataUrl(
260
+ content.toString()
261
+ );
262
+ if (dataUrlMediaType == null || base64Content == null) {
263
+ throw new import_provider.AISDKError({
264
+ name: "InvalidDataContentError",
265
+ message: `Invalid data URL format in content ${content.toString()}`
266
+ });
267
+ }
268
+ return { data: base64Content, mediaType: dataUrlMediaType };
269
+ }
270
+ return { data: content, mediaType: void 0 };
271
+ }
272
+
273
+ // src/prompt/invalid-message-role-error.ts
274
+ var import_provider2 = require("@ai-sdk/provider");
275
+ var name = "AI_InvalidMessageRoleError";
276
+ var marker = `vercel.ai.error.${name}`;
277
+ var symbol = Symbol.for(marker);
278
+ var _a;
279
+ var InvalidMessageRoleError = class extends import_provider2.AISDKError {
280
+ constructor({
281
+ role,
282
+ message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
283
+ }) {
284
+ super({ name, message });
285
+ this[_a] = true;
286
+ this.role = role;
287
+ }
288
+ static isInstance(error) {
289
+ return import_provider2.AISDKError.hasMarker(error, marker);
290
+ }
291
+ };
292
+ _a = symbol;
293
+
294
+ // src/util/as-array.ts
295
+ function asArray(value) {
296
+ return value === void 0 ? [] : Array.isArray(value) ? value : [value];
297
+ }
298
+
299
+ // src/error/missing-tool-result-error.ts
300
+ var import_provider3 = require("@ai-sdk/provider");
301
+ var name2 = "AI_MissingToolResultsError";
302
+ var marker2 = `vercel.ai.error.${name2}`;
303
+ var symbol2 = Symbol.for(marker2);
304
+ var _a2;
305
+ var MissingToolResultsError = class extends import_provider3.AISDKError {
306
+ constructor({ toolCallIds }) {
307
+ super({
308
+ name: name2,
309
+ message: `Tool result${toolCallIds.length > 1 ? "s are" : " is"} missing for tool call${toolCallIds.length > 1 ? "s" : ""} ${toolCallIds.join(
310
+ ", "
311
+ )}.`
312
+ });
313
+ this[_a2] = true;
314
+ this.toolCallIds = toolCallIds;
315
+ }
316
+ static isInstance(error) {
317
+ return import_provider3.AISDKError.hasMarker(error, marker2);
318
+ }
319
+ };
320
+ _a2 = symbol2;
321
+
322
+ // src/prompt/convert-to-language-model-prompt.ts
323
+ async function convertToLanguageModelPrompt({
324
+ prompt,
325
+ supportedUrls,
326
+ download: download2 = createDefaultDownloadFunction()
327
+ }) {
328
+ const downloadedAssets = await downloadAssets(
329
+ prompt.messages,
330
+ download2,
331
+ supportedUrls
332
+ );
333
+ const approvalIdToToolCallId = /* @__PURE__ */ new Map();
334
+ for (const message of prompt.messages) {
335
+ if (message.role === "assistant" && Array.isArray(message.content)) {
336
+ for (const part of message.content) {
337
+ if (part.type === "tool-approval-request" && "approvalId" in part && "toolCallId" in part) {
338
+ approvalIdToToolCallId.set(
339
+ part.approvalId,
340
+ part.toolCallId
341
+ );
342
+ }
343
+ }
344
+ }
345
+ }
346
+ const approvedToolCallIds = /* @__PURE__ */ new Set();
347
+ for (const message of prompt.messages) {
348
+ if (message.role === "tool") {
349
+ for (const part of message.content) {
350
+ if (part.type === "tool-approval-response") {
351
+ const toolCallId = approvalIdToToolCallId.get(part.approvalId);
352
+ if (toolCallId) {
353
+ approvedToolCallIds.add(toolCallId);
354
+ }
355
+ }
356
+ }
357
+ }
358
+ }
359
+ const messages = [
360
+ ...prompt.system != null ? typeof prompt.system === "string" ? [{ role: "system", content: prompt.system }] : asArray(prompt.system).map((message) => ({
361
+ role: "system",
362
+ content: message.content,
363
+ providerOptions: message.providerOptions
364
+ })) : [],
365
+ ...prompt.messages.map(
366
+ (message) => convertToLanguageModelMessage({ message, downloadedAssets })
367
+ )
368
+ ];
369
+ const combinedMessages = [];
370
+ for (const message of messages) {
371
+ if (message.role !== "tool") {
372
+ combinedMessages.push(message);
373
+ continue;
374
+ }
375
+ const lastCombinedMessage = combinedMessages.at(-1);
376
+ if ((lastCombinedMessage == null ? void 0 : lastCombinedMessage.role) === "tool") {
377
+ lastCombinedMessage.content.push(...message.content);
378
+ } else {
379
+ combinedMessages.push(message);
380
+ }
381
+ }
382
+ const toolCallIds = /* @__PURE__ */ new Set();
383
+ for (const message of combinedMessages) {
384
+ switch (message.role) {
385
+ case "assistant": {
386
+ for (const content of message.content) {
387
+ if (content.type === "tool-call" && !content.providerExecuted) {
388
+ toolCallIds.add(content.toolCallId);
389
+ }
390
+ }
391
+ break;
392
+ }
393
+ case "tool": {
394
+ for (const content of message.content) {
395
+ if (content.type === "tool-result") {
396
+ toolCallIds.delete(content.toolCallId);
397
+ }
398
+ }
399
+ break;
400
+ }
401
+ case "user":
402
+ case "system":
403
+ for (const id of approvedToolCallIds) {
404
+ toolCallIds.delete(id);
405
+ }
406
+ if (toolCallIds.size > 0) {
407
+ throw new MissingToolResultsError({
408
+ toolCallIds: Array.from(toolCallIds)
409
+ });
410
+ }
411
+ break;
412
+ }
413
+ }
414
+ for (const id of approvedToolCallIds) {
415
+ toolCallIds.delete(id);
416
+ }
417
+ if (toolCallIds.size > 0) {
418
+ throw new MissingToolResultsError({ toolCallIds: Array.from(toolCallIds) });
419
+ }
420
+ return combinedMessages.filter(
421
+ // Filter out empty tool messages (e.g. if they only contained
422
+ // tool-approval-response parts that were removed).
423
+ // This prevents sending invalid empty messages to the provider.
424
+ // Note: provider-executed tool-approval-response parts are preserved.
425
+ (message) => message.role !== "tool" || message.content.length > 0
426
+ );
427
+ }
428
+ function convertToLanguageModelMessage({
429
+ message,
430
+ downloadedAssets
431
+ }) {
432
+ const role = message.role;
433
+ switch (role) {
434
+ case "system": {
435
+ return {
436
+ role: "system",
437
+ content: message.content,
438
+ providerOptions: message.providerOptions
439
+ };
440
+ }
441
+ case "user": {
442
+ if (typeof message.content === "string") {
443
+ return {
444
+ role: "user",
445
+ content: [{ type: "text", text: message.content }],
446
+ providerOptions: message.providerOptions
447
+ };
448
+ }
449
+ return {
450
+ role: "user",
451
+ content: message.content.map((part) => convertPartToLanguageModelPart(part, downloadedAssets)).filter((part) => part.type !== "text" || part.text !== ""),
452
+ providerOptions: message.providerOptions
453
+ };
454
+ }
455
+ case "assistant": {
456
+ if (typeof message.content === "string") {
457
+ return {
458
+ role: "assistant",
459
+ content: [{ type: "text", text: message.content }],
460
+ providerOptions: message.providerOptions
461
+ };
462
+ }
463
+ return {
464
+ role: "assistant",
465
+ content: message.content.filter(
466
+ // remove empty text parts (no text, and no provider options):
467
+ (part) => part.type !== "text" || part.text !== "" || part.providerOptions != null
468
+ ).filter(
469
+ (part) => part.type !== "tool-approval-request"
470
+ ).map((part) => {
471
+ const providerOptions = part.providerOptions;
472
+ switch (part.type) {
473
+ case "custom": {
474
+ return {
475
+ type: "custom",
476
+ kind: part.kind,
477
+ providerOptions
478
+ };
479
+ }
480
+ case "file": {
481
+ const { data, mediaType } = convertToLanguageModelV4DataContent(
482
+ part.data
483
+ );
484
+ return {
485
+ type: "file",
486
+ data,
487
+ filename: part.filename,
488
+ mediaType: mediaType != null ? mediaType : part.mediaType,
489
+ providerOptions
490
+ };
491
+ }
492
+ case "reasoning": {
493
+ return {
494
+ type: "reasoning",
495
+ text: part.text,
496
+ providerOptions
497
+ };
498
+ }
499
+ case "reasoning-file": {
500
+ const { data, mediaType } = convertToLanguageModelV4DataContent(
501
+ part.data
502
+ );
503
+ return {
504
+ type: "reasoning-file",
505
+ data,
506
+ mediaType: mediaType != null ? mediaType : part.mediaType,
507
+ providerOptions
508
+ };
509
+ }
510
+ case "text": {
511
+ return {
512
+ type: "text",
513
+ text: part.text,
514
+ providerOptions
515
+ };
516
+ }
517
+ case "tool-call": {
518
+ return {
519
+ type: "tool-call",
520
+ toolCallId: part.toolCallId,
521
+ toolName: part.toolName,
522
+ input: part.input,
523
+ providerExecuted: part.providerExecuted,
524
+ providerOptions
525
+ };
526
+ }
527
+ case "tool-result": {
528
+ return {
529
+ type: "tool-result",
530
+ toolCallId: part.toolCallId,
531
+ toolName: part.toolName,
532
+ output: mapToolResultOutput(part.output),
533
+ providerOptions
534
+ };
535
+ }
536
+ }
537
+ }),
538
+ providerOptions: message.providerOptions
539
+ };
540
+ }
541
+ case "tool": {
542
+ return {
543
+ role: "tool",
544
+ content: message.content.filter(
545
+ // Only include tool-approval-response for provider-executed tools
546
+ (part) => part.type !== "tool-approval-response" || part.providerExecuted
547
+ ).map((part) => {
548
+ switch (part.type) {
549
+ case "tool-result": {
550
+ return {
551
+ type: "tool-result",
552
+ toolCallId: part.toolCallId,
553
+ toolName: part.toolName,
554
+ output: mapToolResultOutput(part.output),
555
+ providerOptions: part.providerOptions
556
+ };
557
+ }
558
+ case "tool-approval-response": {
559
+ return {
560
+ type: "tool-approval-response",
561
+ approvalId: part.approvalId,
562
+ approved: part.approved,
563
+ reason: part.reason
564
+ };
565
+ }
566
+ }
567
+ }),
568
+ providerOptions: message.providerOptions
569
+ };
570
+ }
571
+ default: {
572
+ const _exhaustiveCheck = role;
573
+ throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
574
+ }
575
+ }
576
+ }
577
+ async function downloadAssets(messages, download2, supportedUrls) {
578
+ const plannedDownloads = messages.filter((message) => message.role === "user").map((message) => message.content).filter(
579
+ (content) => Array.isArray(content)
580
+ ).flat().filter(
581
+ (part) => part.type === "image" || part.type === "file"
582
+ ).map((part) => {
583
+ var _a5;
584
+ const mediaType = (_a5 = part.mediaType) != null ? _a5 : part.type === "image" ? "image/*" : void 0;
585
+ let data = part.type === "image" ? part.image : part.data;
586
+ if (typeof data === "string") {
587
+ try {
588
+ data = new URL(data);
589
+ } catch (ignored) {
590
+ }
591
+ }
592
+ return { mediaType, data };
593
+ }).filter(
594
+ (part) => part.data instanceof URL
595
+ ).map((part) => ({
596
+ url: part.data,
597
+ isUrlSupportedByModel: part.mediaType != null && (0, import_provider_utils5.isUrlSupported)({
598
+ url: part.data.toString(),
599
+ mediaType: part.mediaType,
600
+ supportedUrls
601
+ })
602
+ }));
603
+ const downloadedFiles = await download2(plannedDownloads);
604
+ return Object.fromEntries(
605
+ downloadedFiles.map(
606
+ (file, index) => file == null ? null : [
607
+ plannedDownloads[index].url.toString(),
608
+ { data: file.data, mediaType: file.mediaType }
609
+ ]
610
+ ).filter((file) => file != null)
611
+ );
612
+ }
613
+ function convertPartToLanguageModelPart(part, downloadedAssets) {
614
+ var _a5;
615
+ if (part.type === "text") {
616
+ return {
617
+ type: "text",
618
+ text: part.text,
619
+ providerOptions: part.providerOptions
620
+ };
621
+ }
622
+ let originalData;
623
+ const type = part.type;
624
+ switch (type) {
625
+ case "image":
626
+ originalData = part.image;
627
+ break;
628
+ case "file":
629
+ originalData = part.data;
630
+ break;
631
+ default:
632
+ throw new Error(`Unsupported part type: ${type}`);
633
+ }
634
+ const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV4DataContent(originalData);
635
+ let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
636
+ let data = convertedData;
637
+ if (data instanceof URL) {
638
+ const downloadedFile = downloadedAssets[data.toString()];
639
+ if (downloadedFile) {
640
+ data = downloadedFile.data;
641
+ mediaType != null ? mediaType : mediaType = downloadedFile.mediaType;
642
+ }
643
+ }
644
+ switch (type) {
645
+ case "image": {
646
+ if (data instanceof Uint8Array || typeof data === "string") {
647
+ mediaType = (_a5 = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _a5 : mediaType;
648
+ }
649
+ return {
650
+ type: "file",
651
+ mediaType: mediaType != null ? mediaType : "image/*",
652
+ // any image
653
+ filename: void 0,
654
+ data,
655
+ providerOptions: part.providerOptions
656
+ };
657
+ }
658
+ case "file": {
659
+ if (mediaType == null) {
660
+ throw new Error(`Media type is missing for file part`);
661
+ }
662
+ return {
663
+ type: "file",
664
+ mediaType,
665
+ filename: part.filename,
666
+ data,
667
+ providerOptions: part.providerOptions
668
+ };
669
+ }
670
+ }
671
+ }
672
+ function mapToolResultOutput(output) {
673
+ if (output.type !== "content") {
674
+ return output;
675
+ }
676
+ return {
677
+ type: "content",
678
+ value: output.value.map((item) => {
679
+ if (item.type !== "media") {
680
+ return item;
681
+ }
682
+ if (item.mediaType.startsWith("image/")) {
683
+ return {
684
+ type: "image-data",
685
+ data: item.data,
686
+ mediaType: item.mediaType
687
+ };
688
+ }
689
+ return {
690
+ type: "file-data",
691
+ data: item.data,
692
+ mediaType: item.mediaType
693
+ };
694
+ })
695
+ };
696
+ }
697
+
698
+ // src/prompt/prepare-tools-and-tool-choice.ts
699
+ var import_provider_utils6 = require("@ai-sdk/provider-utils");
700
+
701
+ // src/util/is-non-empty-object.ts
702
+ function isNonEmptyObject(object) {
703
+ return object != null && Object.keys(object).length > 0;
704
+ }
705
+
706
+ // src/prompt/prepare-tools-and-tool-choice.ts
707
+ async function prepareToolsAndToolChoice({
708
+ tools,
709
+ toolChoice,
710
+ activeTools
711
+ }) {
712
+ if (!isNonEmptyObject(tools)) {
713
+ return {
714
+ tools: void 0,
715
+ toolChoice: void 0
716
+ };
717
+ }
718
+ const filteredTools = activeTools != null ? Object.entries(tools).filter(
719
+ ([name5]) => activeTools.includes(name5)
720
+ ) : Object.entries(tools);
721
+ const languageModelTools = [];
722
+ for (const [name5, tool] of filteredTools) {
723
+ const toolType = tool.type;
724
+ switch (toolType) {
725
+ case void 0:
726
+ case "dynamic":
727
+ case "function":
728
+ languageModelTools.push({
729
+ type: "function",
730
+ name: name5,
731
+ description: tool.description,
732
+ inputSchema: await (0, import_provider_utils6.asSchema)(tool.inputSchema).jsonSchema,
733
+ ...tool.inputExamples != null ? { inputExamples: tool.inputExamples } : {},
734
+ providerOptions: tool.providerOptions,
735
+ ...tool.strict != null ? { strict: tool.strict } : {}
736
+ });
737
+ break;
738
+ case "provider":
739
+ languageModelTools.push({
740
+ type: "provider",
741
+ name: name5,
742
+ id: tool.id,
743
+ args: tool.args
744
+ });
745
+ break;
746
+ default: {
747
+ const exhaustiveCheck = toolType;
748
+ throw new Error(`Unsupported tool type: ${exhaustiveCheck}`);
749
+ }
750
+ }
751
+ }
752
+ return {
753
+ tools: languageModelTools,
754
+ toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
755
+ };
756
+ }
757
+
758
+ // src/prompt/standardize-prompt.ts
759
+ var import_provider4 = require("@ai-sdk/provider");
760
+ var import_provider_utils7 = require("@ai-sdk/provider-utils");
761
+ var import_v46 = require("zod/v4");
762
+
763
+ // src/prompt/message.ts
764
+ var import_v45 = require("zod/v4");
765
+
766
+ // src/types/provider-metadata.ts
767
+ var import_v43 = require("zod/v4");
768
+
769
+ // src/types/json-value.ts
770
+ var import_v42 = require("zod/v4");
771
+ var jsonValueSchema = import_v42.z.lazy(
772
+ () => import_v42.z.union([
773
+ import_v42.z.null(),
774
+ import_v42.z.string(),
775
+ import_v42.z.number(),
776
+ import_v42.z.boolean(),
777
+ import_v42.z.record(import_v42.z.string(), jsonValueSchema.optional()),
778
+ import_v42.z.array(jsonValueSchema)
779
+ ])
780
+ );
781
+
782
+ // src/types/provider-metadata.ts
783
+ var providerMetadataSchema = import_v43.z.record(
784
+ import_v43.z.string(),
785
+ import_v43.z.record(import_v43.z.string(), jsonValueSchema.optional())
786
+ );
787
+
788
+ // src/prompt/content-part.ts
789
+ var import_v44 = require("zod/v4");
790
+ var textPartSchema = import_v44.z.object({
791
+ type: import_v44.z.literal("text"),
792
+ text: import_v44.z.string(),
793
+ providerOptions: providerMetadataSchema.optional()
794
+ });
795
+ var imagePartSchema = import_v44.z.object({
796
+ type: import_v44.z.literal("image"),
797
+ image: import_v44.z.union([dataContentSchema, import_v44.z.instanceof(URL)]),
798
+ mediaType: import_v44.z.string().optional(),
799
+ providerOptions: providerMetadataSchema.optional()
800
+ });
801
+ var filePartSchema = import_v44.z.object({
802
+ type: import_v44.z.literal("file"),
803
+ data: import_v44.z.union([dataContentSchema, import_v44.z.instanceof(URL)]),
804
+ filename: import_v44.z.string().optional(),
805
+ mediaType: import_v44.z.string(),
806
+ providerOptions: providerMetadataSchema.optional()
807
+ });
808
+ var reasoningPartSchema = import_v44.z.object({
809
+ type: import_v44.z.literal("reasoning"),
810
+ text: import_v44.z.string(),
811
+ providerOptions: providerMetadataSchema.optional()
812
+ });
813
+ var customPartSchema = import_v44.z.object({
814
+ type: import_v44.z.literal("custom"),
815
+ kind: import_v44.z.string().transform((value) => value),
816
+ providerOptions: providerMetadataSchema.optional()
817
+ });
818
+ var reasoningFilePartSchema = import_v44.z.object({
819
+ type: import_v44.z.literal("reasoning-file"),
820
+ data: import_v44.z.union([dataContentSchema, import_v44.z.instanceof(URL)]),
821
+ mediaType: import_v44.z.string(),
822
+ providerOptions: providerMetadataSchema.optional()
823
+ });
824
+ var toolCallPartSchema = import_v44.z.object({
825
+ type: import_v44.z.literal("tool-call"),
826
+ toolCallId: import_v44.z.string(),
827
+ toolName: import_v44.z.string(),
828
+ input: import_v44.z.unknown(),
829
+ providerOptions: providerMetadataSchema.optional(),
830
+ providerExecuted: import_v44.z.boolean().optional()
831
+ });
832
+ var outputSchema = import_v44.z.discriminatedUnion(
833
+ "type",
834
+ [
835
+ import_v44.z.object({
836
+ type: import_v44.z.literal("text"),
837
+ value: import_v44.z.string(),
838
+ providerOptions: providerMetadataSchema.optional()
839
+ }),
840
+ import_v44.z.object({
841
+ type: import_v44.z.literal("json"),
842
+ value: jsonValueSchema,
843
+ providerOptions: providerMetadataSchema.optional()
844
+ }),
845
+ import_v44.z.object({
846
+ type: import_v44.z.literal("execution-denied"),
847
+ reason: import_v44.z.string().optional(),
848
+ providerOptions: providerMetadataSchema.optional()
849
+ }),
850
+ import_v44.z.object({
851
+ type: import_v44.z.literal("error-text"),
852
+ value: import_v44.z.string(),
853
+ providerOptions: providerMetadataSchema.optional()
854
+ }),
855
+ import_v44.z.object({
856
+ type: import_v44.z.literal("error-json"),
857
+ value: jsonValueSchema,
858
+ providerOptions: providerMetadataSchema.optional()
859
+ }),
860
+ import_v44.z.object({
861
+ type: import_v44.z.literal("content"),
862
+ value: import_v44.z.array(
863
+ import_v44.z.union([
864
+ import_v44.z.object({
865
+ type: import_v44.z.literal("text"),
866
+ text: import_v44.z.string(),
867
+ providerOptions: providerMetadataSchema.optional()
868
+ }),
869
+ import_v44.z.object({
870
+ type: import_v44.z.literal("media"),
871
+ data: import_v44.z.string(),
872
+ mediaType: import_v44.z.string()
873
+ }),
874
+ import_v44.z.object({
875
+ type: import_v44.z.literal("file-data"),
876
+ data: import_v44.z.string(),
877
+ mediaType: import_v44.z.string(),
878
+ filename: import_v44.z.string().optional(),
879
+ providerOptions: providerMetadataSchema.optional()
880
+ }),
881
+ import_v44.z.object({
882
+ type: import_v44.z.literal("file-url"),
883
+ url: import_v44.z.string(),
884
+ providerOptions: providerMetadataSchema.optional()
885
+ }),
886
+ import_v44.z.object({
887
+ type: import_v44.z.literal("file-id"),
888
+ fileId: import_v44.z.union([import_v44.z.string(), import_v44.z.record(import_v44.z.string(), import_v44.z.string())]),
889
+ providerOptions: providerMetadataSchema.optional()
890
+ }),
891
+ import_v44.z.object({
892
+ type: import_v44.z.literal("image-data"),
893
+ data: import_v44.z.string(),
894
+ mediaType: import_v44.z.string(),
895
+ providerOptions: providerMetadataSchema.optional()
896
+ }),
897
+ import_v44.z.object({
898
+ type: import_v44.z.literal("image-url"),
899
+ url: import_v44.z.string(),
900
+ providerOptions: providerMetadataSchema.optional()
901
+ }),
902
+ import_v44.z.object({
903
+ type: import_v44.z.literal("image-file-id"),
904
+ fileId: import_v44.z.union([import_v44.z.string(), import_v44.z.record(import_v44.z.string(), import_v44.z.string())]),
905
+ providerOptions: providerMetadataSchema.optional()
906
+ }),
907
+ import_v44.z.object({
908
+ type: import_v44.z.literal("custom"),
909
+ providerOptions: providerMetadataSchema.optional()
910
+ })
911
+ ])
912
+ )
913
+ })
914
+ ]
915
+ );
916
+ var toolResultPartSchema = import_v44.z.object({
917
+ type: import_v44.z.literal("tool-result"),
918
+ toolCallId: import_v44.z.string(),
919
+ toolName: import_v44.z.string(),
920
+ output: outputSchema,
921
+ providerOptions: providerMetadataSchema.optional()
922
+ });
923
+ var toolApprovalRequestSchema = import_v44.z.object({
924
+ type: import_v44.z.literal("tool-approval-request"),
925
+ approvalId: import_v44.z.string(),
926
+ toolCallId: import_v44.z.string()
927
+ });
928
+ var toolApprovalResponseSchema = import_v44.z.object({
929
+ type: import_v44.z.literal("tool-approval-response"),
930
+ approvalId: import_v44.z.string(),
931
+ approved: import_v44.z.boolean(),
932
+ reason: import_v44.z.string().optional()
933
+ });
934
+
935
+ // src/prompt/message.ts
936
+ var systemModelMessageSchema = import_v45.z.object(
937
+ {
938
+ role: import_v45.z.literal("system"),
939
+ content: import_v45.z.string(),
940
+ providerOptions: providerMetadataSchema.optional()
941
+ }
942
+ );
943
+ var userModelMessageSchema = import_v45.z.object({
944
+ role: import_v45.z.literal("user"),
945
+ content: import_v45.z.union([
946
+ import_v45.z.string(),
947
+ import_v45.z.array(import_v45.z.union([textPartSchema, imagePartSchema, filePartSchema]))
948
+ ]),
949
+ providerOptions: providerMetadataSchema.optional()
950
+ });
951
+ var assistantModelMessageSchema = import_v45.z.object({
952
+ role: import_v45.z.literal("assistant"),
953
+ content: import_v45.z.union([
954
+ import_v45.z.string(),
955
+ import_v45.z.array(
956
+ import_v45.z.union([
957
+ textPartSchema,
958
+ customPartSchema,
959
+ filePartSchema,
960
+ reasoningPartSchema,
961
+ reasoningFilePartSchema,
962
+ toolCallPartSchema,
963
+ toolResultPartSchema,
964
+ toolApprovalRequestSchema
965
+ ])
966
+ )
967
+ ]),
968
+ providerOptions: providerMetadataSchema.optional()
969
+ });
970
+ var toolModelMessageSchema = import_v45.z.object({
971
+ role: import_v45.z.literal("tool"),
972
+ content: import_v45.z.array(import_v45.z.union([toolResultPartSchema, toolApprovalResponseSchema])),
973
+ providerOptions: providerMetadataSchema.optional()
974
+ });
975
+ var modelMessageSchema = import_v45.z.union([
976
+ systemModelMessageSchema,
977
+ userModelMessageSchema,
978
+ assistantModelMessageSchema,
979
+ toolModelMessageSchema
980
+ ]);
981
+
982
+ // src/prompt/standardize-prompt.ts
983
+ async function standardizePrompt(prompt) {
984
+ if (prompt.prompt == null && prompt.messages == null) {
985
+ throw new import_provider4.InvalidPromptError({
986
+ prompt,
987
+ message: "prompt or messages must be defined"
988
+ });
989
+ }
990
+ if (prompt.prompt != null && prompt.messages != null) {
991
+ throw new import_provider4.InvalidPromptError({
992
+ prompt,
993
+ message: "prompt and messages cannot be defined at the same time"
994
+ });
995
+ }
996
+ if (prompt.system != null && typeof prompt.system !== "string" && !asArray(prompt.system).every(
997
+ (message) => typeof message === "object" && message !== null && "role" in message && message.role === "system"
998
+ )) {
999
+ throw new import_provider4.InvalidPromptError({
1000
+ prompt,
1001
+ message: "system must be a string, SystemModelMessage, or array of SystemModelMessage"
1002
+ });
1003
+ }
1004
+ let messages;
1005
+ if (prompt.prompt != null && typeof prompt.prompt === "string") {
1006
+ messages = [{ role: "user", content: prompt.prompt }];
1007
+ } else if (prompt.prompt != null && Array.isArray(prompt.prompt)) {
1008
+ messages = prompt.prompt;
1009
+ } else if (prompt.messages != null) {
1010
+ messages = prompt.messages;
1011
+ } else {
1012
+ throw new import_provider4.InvalidPromptError({
1013
+ prompt,
1014
+ message: "prompt or messages must be defined"
1015
+ });
1016
+ }
1017
+ if (messages.length === 0) {
1018
+ throw new import_provider4.InvalidPromptError({
1019
+ prompt,
1020
+ message: "messages must not be empty"
1021
+ });
1022
+ }
1023
+ const validationResult = await (0, import_provider_utils7.safeValidateTypes)({
1024
+ value: messages,
1025
+ schema: import_v46.z.array(modelMessageSchema)
1026
+ });
1027
+ if (!validationResult.success) {
1028
+ throw new import_provider4.InvalidPromptError({
1029
+ prompt,
1030
+ message: "The messages do not match the ModelMessage[] schema.",
1031
+ cause: validationResult.error
1032
+ });
1033
+ }
1034
+ return {
1035
+ messages,
1036
+ system: prompt.system
1037
+ };
1038
+ }
1039
+
1040
+ // src/error/invalid-argument-error.ts
1041
+ var import_provider5 = require("@ai-sdk/provider");
1042
+ var name3 = "AI_InvalidArgumentError";
1043
+ var marker3 = `vercel.ai.error.${name3}`;
1044
+ var symbol3 = Symbol.for(marker3);
1045
+ var _a3;
1046
+ var InvalidArgumentError = class extends import_provider5.AISDKError {
1047
+ constructor({
1048
+ parameter,
1049
+ value,
1050
+ message
1051
+ }) {
1052
+ super({
1053
+ name: name3,
1054
+ message: `Invalid argument for parameter ${parameter}: ${message}`
1055
+ });
1056
+ this[_a3] = true;
1057
+ this.parameter = parameter;
1058
+ this.value = value;
1059
+ }
1060
+ static isInstance(error) {
1061
+ return import_provider5.AISDKError.hasMarker(error, marker3);
1062
+ }
1063
+ };
1064
+ _a3 = symbol3;
1065
+
1066
+ // src/prompt/prepare-call-settings.ts
1067
+ function prepareCallSettings({
1068
+ maxOutputTokens,
1069
+ temperature,
1070
+ topP,
1071
+ topK,
1072
+ presencePenalty,
1073
+ frequencyPenalty,
1074
+ seed,
1075
+ stopSequences,
1076
+ reasoning
1077
+ }) {
1078
+ if (maxOutputTokens != null) {
1079
+ if (!Number.isInteger(maxOutputTokens)) {
1080
+ throw new InvalidArgumentError({
1081
+ parameter: "maxOutputTokens",
1082
+ value: maxOutputTokens,
1083
+ message: "maxOutputTokens must be an integer"
1084
+ });
1085
+ }
1086
+ if (maxOutputTokens < 1) {
1087
+ throw new InvalidArgumentError({
1088
+ parameter: "maxOutputTokens",
1089
+ value: maxOutputTokens,
1090
+ message: "maxOutputTokens must be >= 1"
1091
+ });
1092
+ }
1093
+ }
1094
+ if (temperature != null) {
1095
+ if (typeof temperature !== "number") {
1096
+ throw new InvalidArgumentError({
1097
+ parameter: "temperature",
1098
+ value: temperature,
1099
+ message: "temperature must be a number"
1100
+ });
1101
+ }
1102
+ }
1103
+ if (topP != null) {
1104
+ if (typeof topP !== "number") {
1105
+ throw new InvalidArgumentError({
1106
+ parameter: "topP",
1107
+ value: topP,
1108
+ message: "topP must be a number"
1109
+ });
1110
+ }
1111
+ }
1112
+ if (topK != null) {
1113
+ if (typeof topK !== "number") {
1114
+ throw new InvalidArgumentError({
1115
+ parameter: "topK",
1116
+ value: topK,
1117
+ message: "topK must be a number"
1118
+ });
1119
+ }
1120
+ }
1121
+ if (presencePenalty != null) {
1122
+ if (typeof presencePenalty !== "number") {
1123
+ throw new InvalidArgumentError({
1124
+ parameter: "presencePenalty",
1125
+ value: presencePenalty,
1126
+ message: "presencePenalty must be a number"
1127
+ });
1128
+ }
1129
+ }
1130
+ if (frequencyPenalty != null) {
1131
+ if (typeof frequencyPenalty !== "number") {
1132
+ throw new InvalidArgumentError({
1133
+ parameter: "frequencyPenalty",
1134
+ value: frequencyPenalty,
1135
+ message: "frequencyPenalty must be a number"
1136
+ });
1137
+ }
1138
+ }
1139
+ if (seed != null) {
1140
+ if (!Number.isInteger(seed)) {
1141
+ throw new InvalidArgumentError({
1142
+ parameter: "seed",
1143
+ value: seed,
1144
+ message: "seed must be an integer"
1145
+ });
1146
+ }
1147
+ }
1148
+ return {
1149
+ maxOutputTokens,
1150
+ temperature,
1151
+ topP,
1152
+ topK,
1153
+ presencePenalty,
1154
+ frequencyPenalty,
1155
+ stopSequences,
1156
+ seed,
1157
+ reasoning
1158
+ };
1159
+ }
1160
+
1161
+ // src/util/retry-with-exponential-backoff.ts
1162
+ var import_provider7 = require("@ai-sdk/provider");
1163
+ var import_provider_utils8 = require("@ai-sdk/provider-utils");
1164
+
1165
+ // src/util/retry-error.ts
1166
+ var import_provider6 = require("@ai-sdk/provider");
1167
+ var name4 = "AI_RetryError";
1168
+ var marker4 = `vercel.ai.error.${name4}`;
1169
+ var symbol4 = Symbol.for(marker4);
1170
+ var _a4;
1171
+ var RetryError = class extends import_provider6.AISDKError {
1172
+ constructor({
1173
+ message,
1174
+ reason,
1175
+ errors
1176
+ }) {
1177
+ super({ name: name4, message });
1178
+ this[_a4] = true;
1179
+ this.reason = reason;
1180
+ this.errors = errors;
1181
+ this.lastError = errors[errors.length - 1];
1182
+ }
1183
+ static isInstance(error) {
1184
+ return import_provider6.AISDKError.hasMarker(error, marker4);
1185
+ }
1186
+ };
1187
+ _a4 = symbol4;
1188
+
1189
+ // src/util/retry-with-exponential-backoff.ts
1190
+ function getRetryDelayInMs({
1191
+ error,
1192
+ exponentialBackoffDelay
1193
+ }) {
1194
+ const headers = error.responseHeaders;
1195
+ if (!headers)
1196
+ return exponentialBackoffDelay;
1197
+ let ms;
1198
+ const retryAfterMs = headers["retry-after-ms"];
1199
+ if (retryAfterMs) {
1200
+ const timeoutMs = parseFloat(retryAfterMs);
1201
+ if (!Number.isNaN(timeoutMs)) {
1202
+ ms = timeoutMs;
1203
+ }
1204
+ }
1205
+ const retryAfter = headers["retry-after"];
1206
+ if (retryAfter && ms === void 0) {
1207
+ const timeoutSeconds = parseFloat(retryAfter);
1208
+ if (!Number.isNaN(timeoutSeconds)) {
1209
+ ms = timeoutSeconds * 1e3;
1210
+ } else {
1211
+ ms = Date.parse(retryAfter) - Date.now();
1212
+ }
1213
+ }
1214
+ if (ms != null && !Number.isNaN(ms) && 0 <= ms && (ms < 60 * 1e3 || ms < exponentialBackoffDelay)) {
1215
+ return ms;
1216
+ }
1217
+ return exponentialBackoffDelay;
1218
+ }
1219
+ var retryWithExponentialBackoffRespectingRetryHeaders = ({
1220
+ maxRetries = 2,
1221
+ initialDelayInMs = 2e3,
1222
+ backoffFactor = 2,
1223
+ abortSignal
1224
+ } = {}) => async (f) => _retryWithExponentialBackoff(f, {
1225
+ maxRetries,
1226
+ delayInMs: initialDelayInMs,
1227
+ backoffFactor,
1228
+ abortSignal
1229
+ });
1230
+ async function _retryWithExponentialBackoff(f, {
1231
+ maxRetries,
1232
+ delayInMs,
1233
+ backoffFactor,
1234
+ abortSignal
1235
+ }, errors = []) {
1236
+ try {
1237
+ return await f();
1238
+ } catch (error) {
1239
+ if ((0, import_provider_utils8.isAbortError)(error)) {
1240
+ throw error;
1241
+ }
1242
+ if (maxRetries === 0) {
1243
+ throw error;
1244
+ }
1245
+ const errorMessage = (0, import_provider_utils8.getErrorMessage)(error);
1246
+ const newErrors = [...errors, error];
1247
+ const tryNumber = newErrors.length;
1248
+ if (tryNumber > maxRetries) {
1249
+ throw new RetryError({
1250
+ message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
1251
+ reason: "maxRetriesExceeded",
1252
+ errors: newErrors
1253
+ });
1254
+ }
1255
+ if (error instanceof Error && import_provider7.APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
1256
+ await (0, import_provider_utils8.delay)(
1257
+ getRetryDelayInMs({
1258
+ error,
1259
+ exponentialBackoffDelay: delayInMs
1260
+ }),
1261
+ { abortSignal }
1262
+ );
1263
+ return _retryWithExponentialBackoff(
1264
+ f,
1265
+ {
1266
+ maxRetries,
1267
+ delayInMs: backoffFactor * delayInMs,
1268
+ backoffFactor,
1269
+ abortSignal
1270
+ },
1271
+ newErrors
1272
+ );
1273
+ }
1274
+ if (tryNumber === 1) {
1275
+ throw error;
1276
+ }
1277
+ throw new RetryError({
1278
+ message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
1279
+ reason: "errorNotRetryable",
1280
+ errors: newErrors
1281
+ });
1282
+ }
1283
+ }
1284
+
1285
+ // src/util/prepare-retries.ts
1286
+ function prepareRetries({
1287
+ maxRetries,
1288
+ abortSignal
1289
+ }) {
1290
+ if (maxRetries != null) {
1291
+ if (!Number.isInteger(maxRetries)) {
1292
+ throw new InvalidArgumentError({
1293
+ parameter: "maxRetries",
1294
+ value: maxRetries,
1295
+ message: "maxRetries must be an integer"
1296
+ });
1297
+ }
1298
+ if (maxRetries < 0) {
1299
+ throw new InvalidArgumentError({
1300
+ parameter: "maxRetries",
1301
+ value: maxRetries,
1302
+ message: "maxRetries must be >= 0"
1303
+ });
1304
+ }
1305
+ }
1306
+ const maxRetriesResult = maxRetries != null ? maxRetries : 2;
1307
+ return {
1308
+ maxRetries: maxRetriesResult,
1309
+ retry: retryWithExponentialBackoffRespectingRetryHeaders({
1310
+ maxRetries: maxRetriesResult,
1311
+ abortSignal
1312
+ })
1313
+ };
1314
+ }
1315
+
1316
+ // src/types/usage.ts
1317
+ function asLanguageModelUsage(usage) {
1318
+ return {
1319
+ inputTokens: usage.inputTokens.total,
1320
+ inputTokenDetails: {
1321
+ noCacheTokens: usage.inputTokens.noCache,
1322
+ cacheReadTokens: usage.inputTokens.cacheRead,
1323
+ cacheWriteTokens: usage.inputTokens.cacheWrite
1324
+ },
1325
+ outputTokens: usage.outputTokens.total,
1326
+ outputTokenDetails: {
1327
+ textTokens: usage.outputTokens.text,
1328
+ reasoningTokens: usage.outputTokens.reasoning
1329
+ },
1330
+ totalTokens: addTokenCounts(
1331
+ usage.inputTokens.total,
1332
+ usage.outputTokens.total
1333
+ ),
1334
+ raw: usage.raw,
1335
+ reasoningTokens: usage.outputTokens.reasoning,
1336
+ cachedInputTokens: usage.inputTokens.cacheRead
1337
+ };
1338
+ }
1339
+ function addTokenCounts(tokenCount1, tokenCount2) {
1340
+ return tokenCount1 == null && tokenCount2 == null ? void 0 : (tokenCount1 != null ? tokenCount1 : 0) + (tokenCount2 != null ? tokenCount2 : 0);
1341
+ }
1342
+ // Annotate the CommonJS export names for ESM import in node:
1343
+ 0 && (module.exports = {
1344
+ asLanguageModelUsage,
1345
+ convertAsyncIteratorToReadableStream,
1346
+ convertToLanguageModelPrompt,
1347
+ prepareCallSettings,
1348
+ prepareRetries,
1349
+ prepareToolsAndToolChoice,
1350
+ standardizePrompt
1351
+ });
1352
+ //# sourceMappingURL=index.js.map