@depup/ai 6.0.116-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (515) hide show
  1. package/CHANGELOG.md +7350 -0
  2. package/LICENSE +13 -0
  3. package/README.md +25 -0
  4. package/changes.json +5 -0
  5. package/dist/index.d.mts +6429 -0
  6. package/dist/index.d.ts +6429 -0
  7. package/dist/index.js +13548 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/index.mjs +13514 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/dist/internal/index.d.mts +314 -0
  12. package/dist/internal/index.d.ts +314 -0
  13. package/dist/internal/index.js +1316 -0
  14. package/dist/internal/index.js.map +1 -0
  15. package/dist/internal/index.mjs +1300 -0
  16. package/dist/internal/index.mjs.map +1 -0
  17. package/dist/test/index.d.mts +133 -0
  18. package/dist/test/index.d.ts +133 -0
  19. package/dist/test/index.js +275 -0
  20. package/dist/test/index.js.map +1 -0
  21. package/dist/test/index.mjs +245 -0
  22. package/dist/test/index.mjs.map +1 -0
  23. package/docs/00-introduction/index.mdx +76 -0
  24. package/docs/02-foundations/01-overview.mdx +43 -0
  25. package/docs/02-foundations/02-providers-and-models.mdx +156 -0
  26. package/docs/02-foundations/03-prompts.mdx +616 -0
  27. package/docs/02-foundations/04-tools.mdx +251 -0
  28. package/docs/02-foundations/05-streaming.mdx +62 -0
  29. package/docs/02-foundations/06-provider-options.mdx +337 -0
  30. package/docs/02-foundations/index.mdx +49 -0
  31. package/docs/02-getting-started/00-choosing-a-provider.mdx +110 -0
  32. package/docs/02-getting-started/01-navigating-the-library.mdx +85 -0
  33. package/docs/02-getting-started/02-nextjs-app-router.mdx +559 -0
  34. package/docs/02-getting-started/03-nextjs-pages-router.mdx +542 -0
  35. package/docs/02-getting-started/04-svelte.mdx +627 -0
  36. package/docs/02-getting-started/05-nuxt.mdx +566 -0
  37. package/docs/02-getting-started/06-nodejs.mdx +512 -0
  38. package/docs/02-getting-started/07-expo.mdx +766 -0
  39. package/docs/02-getting-started/08-tanstack-start.mdx +583 -0
  40. package/docs/02-getting-started/09-coding-agents.mdx +179 -0
  41. package/docs/02-getting-started/index.mdx +44 -0
  42. package/docs/03-agents/01-overview.mdx +96 -0
  43. package/docs/03-agents/02-building-agents.mdx +449 -0
  44. package/docs/03-agents/03-workflows.mdx +386 -0
  45. package/docs/03-agents/04-loop-control.mdx +394 -0
  46. package/docs/03-agents/05-configuring-call-options.mdx +286 -0
  47. package/docs/03-agents/06-memory.mdx +222 -0
  48. package/docs/03-agents/06-subagents.mdx +362 -0
  49. package/docs/03-agents/index.mdx +46 -0
  50. package/docs/03-ai-sdk-core/01-overview.mdx +31 -0
  51. package/docs/03-ai-sdk-core/05-generating-text.mdx +707 -0
  52. package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +498 -0
  53. package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1144 -0
  54. package/docs/03-ai-sdk-core/16-mcp-tools.mdx +377 -0
  55. package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +146 -0
  56. package/docs/03-ai-sdk-core/25-settings.mdx +198 -0
  57. package/docs/03-ai-sdk-core/30-embeddings.mdx +245 -0
  58. package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
  59. package/docs/03-ai-sdk-core/35-image-generation.mdx +342 -0
  60. package/docs/03-ai-sdk-core/36-transcription.mdx +227 -0
  61. package/docs/03-ai-sdk-core/37-speech.mdx +169 -0
  62. package/docs/03-ai-sdk-core/38-video-generation.mdx +366 -0
  63. package/docs/03-ai-sdk-core/40-middleware.mdx +485 -0
  64. package/docs/03-ai-sdk-core/45-provider-management.mdx +349 -0
  65. package/docs/03-ai-sdk-core/50-error-handling.mdx +149 -0
  66. package/docs/03-ai-sdk-core/55-testing.mdx +219 -0
  67. package/docs/03-ai-sdk-core/60-telemetry.mdx +391 -0
  68. package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
  69. package/docs/03-ai-sdk-core/65-event-listeners.mdx +915 -0
  70. package/docs/03-ai-sdk-core/index.mdx +93 -0
  71. package/docs/04-ai-sdk-ui/01-overview.mdx +44 -0
  72. package/docs/04-ai-sdk-ui/02-chatbot.mdx +1313 -0
  73. package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +535 -0
  74. package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +263 -0
  75. package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +682 -0
  76. package/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx +389 -0
  77. package/docs/04-ai-sdk-ui/05-completion.mdx +181 -0
  78. package/docs/04-ai-sdk-ui/08-object-generation.mdx +344 -0
  79. package/docs/04-ai-sdk-ui/20-streaming-data.mdx +397 -0
  80. package/docs/04-ai-sdk-ui/21-error-handling.mdx +190 -0
  81. package/docs/04-ai-sdk-ui/21-transport.mdx +174 -0
  82. package/docs/04-ai-sdk-ui/24-reading-ui-message-streams.mdx +104 -0
  83. package/docs/04-ai-sdk-ui/25-message-metadata.mdx +152 -0
  84. package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +477 -0
  85. package/docs/04-ai-sdk-ui/index.mdx +64 -0
  86. package/docs/05-ai-sdk-rsc/01-overview.mdx +45 -0
  87. package/docs/05-ai-sdk-rsc/02-streaming-react-components.mdx +209 -0
  88. package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +279 -0
  89. package/docs/05-ai-sdk-rsc/03-saving-and-restoring-states.mdx +105 -0
  90. package/docs/05-ai-sdk-rsc/04-multistep-interfaces.mdx +282 -0
  91. package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +157 -0
  92. package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
  93. package/docs/05-ai-sdk-rsc/08-error-handling.mdx +94 -0
  94. package/docs/05-ai-sdk-rsc/09-authentication.mdx +42 -0
  95. package/docs/05-ai-sdk-rsc/10-migrating-to-ui.mdx +722 -0
  96. package/docs/05-ai-sdk-rsc/index.mdx +63 -0
  97. package/docs/06-advanced/01-prompt-engineering.mdx +96 -0
  98. package/docs/06-advanced/02-stopping-streams.mdx +184 -0
  99. package/docs/06-advanced/03-backpressure.mdx +173 -0
  100. package/docs/06-advanced/04-caching.mdx +169 -0
  101. package/docs/06-advanced/05-multiple-streamables.mdx +68 -0
  102. package/docs/06-advanced/06-rate-limiting.mdx +60 -0
  103. package/docs/06-advanced/07-rendering-ui-with-language-models.mdx +225 -0
  104. package/docs/06-advanced/08-model-as-router.mdx +120 -0
  105. package/docs/06-advanced/09-multistep-interfaces.mdx +115 -0
  106. package/docs/06-advanced/09-sequential-generations.mdx +55 -0
  107. package/docs/06-advanced/10-vercel-deployment-guide.mdx +117 -0
  108. package/docs/06-advanced/index.mdx +11 -0
  109. package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +2715 -0
  110. package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3656 -0
  111. package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +197 -0
  112. package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +191 -0
  113. package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +309 -0
  114. package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +251 -0
  115. package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +152 -0
  116. package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +221 -0
  117. package/docs/07-reference/01-ai-sdk-core/13-generate-video.mdx +264 -0
  118. package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +235 -0
  119. package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +973 -0
  120. package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +154 -0
  121. package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +173 -0
  122. package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +150 -0
  123. package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +209 -0
  124. package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +223 -0
  125. package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +416 -0
  126. package/docs/07-reference/01-ai-sdk-core/24-mcp-stdio-transport.mdx +68 -0
  127. package/docs/07-reference/01-ai-sdk-core/25-json-schema.mdx +94 -0
  128. package/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx +109 -0
  129. package/docs/07-reference/01-ai-sdk-core/27-valibot-schema.mdx +58 -0
  130. package/docs/07-reference/01-ai-sdk-core/28-output.mdx +342 -0
  131. package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +415 -0
  132. package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +246 -0
  133. package/docs/07-reference/01-ai-sdk-core/32-validate-ui-messages.mdx +101 -0
  134. package/docs/07-reference/01-ai-sdk-core/33-safe-validate-ui-messages.mdx +113 -0
  135. package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +198 -0
  136. package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +157 -0
  137. package/docs/07-reference/01-ai-sdk-core/50-cosine-similarity.mdx +52 -0
  138. package/docs/07-reference/01-ai-sdk-core/60-wrap-language-model.mdx +59 -0
  139. package/docs/07-reference/01-ai-sdk-core/61-wrap-image-model.mdx +64 -0
  140. package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +74 -0
  141. package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +68 -0
  142. package/docs/07-reference/01-ai-sdk-core/67-simulate-streaming-middleware.mdx +71 -0
  143. package/docs/07-reference/01-ai-sdk-core/68-default-settings-middleware.mdx +80 -0
  144. package/docs/07-reference/01-ai-sdk-core/69-add-tool-input-examples-middleware.mdx +155 -0
  145. package/docs/07-reference/01-ai-sdk-core/70-extract-json-middleware.mdx +147 -0
  146. package/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx +84 -0
  147. package/docs/07-reference/01-ai-sdk-core/71-has-tool-call.mdx +120 -0
  148. package/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx +94 -0
  149. package/docs/07-reference/01-ai-sdk-core/80-smooth-stream.mdx +145 -0
  150. package/docs/07-reference/01-ai-sdk-core/90-generate-id.mdx +30 -0
  151. package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
  152. package/docs/07-reference/01-ai-sdk-core/92-default-generated-file.mdx +68 -0
  153. package/docs/07-reference/01-ai-sdk-core/index.mdx +160 -0
  154. package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +493 -0
  155. package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +185 -0
  156. package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +196 -0
  157. package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +231 -0
  158. package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +108 -0
  159. package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +162 -0
  160. package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +119 -0
  161. package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +77 -0
  162. package/docs/07-reference/02-ai-sdk-ui/43-read-ui-message-stream.mdx +57 -0
  163. package/docs/07-reference/02-ai-sdk-ui/46-infer-ui-tools.mdx +99 -0
  164. package/docs/07-reference/02-ai-sdk-ui/47-infer-ui-tool.mdx +75 -0
  165. package/docs/07-reference/02-ai-sdk-ui/50-direct-chat-transport.mdx +333 -0
  166. package/docs/07-reference/02-ai-sdk-ui/index.mdx +89 -0
  167. package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +767 -0
  168. package/docs/07-reference/03-ai-sdk-rsc/02-create-ai.mdx +90 -0
  169. package/docs/07-reference/03-ai-sdk-rsc/03-create-streamable-ui.mdx +91 -0
  170. package/docs/07-reference/03-ai-sdk-rsc/04-create-streamable-value.mdx +78 -0
  171. package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +79 -0
  172. package/docs/07-reference/03-ai-sdk-rsc/06-get-ai-state.mdx +50 -0
  173. package/docs/07-reference/03-ai-sdk-rsc/07-get-mutable-ai-state.mdx +70 -0
  174. package/docs/07-reference/03-ai-sdk-rsc/08-use-ai-state.mdx +26 -0
  175. package/docs/07-reference/03-ai-sdk-rsc/09-use-actions.mdx +42 -0
  176. package/docs/07-reference/03-ai-sdk-rsc/10-use-ui-state.mdx +35 -0
  177. package/docs/07-reference/03-ai-sdk-rsc/11-use-streamable-value.mdx +46 -0
  178. package/docs/07-reference/03-ai-sdk-rsc/20-render.mdx +266 -0
  179. package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
  180. package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +31 -0
  181. package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +28 -0
  182. package/docs/07-reference/05-ai-sdk-errors/ai-empty-response-body-error.mdx +24 -0
  183. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-argument-error.mdx +26 -0
  184. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content-error.mdx +26 -0
  185. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-message-role-error.mdx +25 -0
  186. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +47 -0
  187. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-response-data-error.mdx +25 -0
  188. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-error.mdx +24 -0
  189. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-input-error.mdx +27 -0
  190. package/docs/07-reference/05-ai-sdk-errors/ai-json-parse-error.mdx +25 -0
  191. package/docs/07-reference/05-ai-sdk-errors/ai-load-api-key-error.mdx +24 -0
  192. package/docs/07-reference/05-ai-sdk-errors/ai-load-setting-error.mdx +24 -0
  193. package/docs/07-reference/05-ai-sdk-errors/ai-message-conversion-error.mdx +25 -0
  194. package/docs/07-reference/05-ai-sdk-errors/ai-no-content-generated-error.mdx +24 -0
  195. package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +36 -0
  196. package/docs/07-reference/05-ai-sdk-errors/ai-no-object-generated-error.mdx +43 -0
  197. package/docs/07-reference/05-ai-sdk-errors/ai-no-output-generated-error.mdx +25 -0
  198. package/docs/07-reference/05-ai-sdk-errors/ai-no-speech-generated-error.mdx +24 -0
  199. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +26 -0
  200. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +28 -0
  201. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-tool-error.mdx +26 -0
  202. package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +24 -0
  203. package/docs/07-reference/05-ai-sdk-errors/ai-no-video-generated-error.mdx +39 -0
  204. package/docs/07-reference/05-ai-sdk-errors/ai-retry-error.mdx +27 -0
  205. package/docs/07-reference/05-ai-sdk-errors/ai-too-many-embedding-values-for-call-error.mdx +27 -0
  206. package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-not-found-for-approval-error.mdx +25 -0
  207. package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-repair-error.mdx +28 -0
  208. package/docs/07-reference/05-ai-sdk-errors/ai-type-validation-error.mdx +25 -0
  209. package/docs/07-reference/05-ai-sdk-errors/ai-ui-message-stream-error.mdx +67 -0
  210. package/docs/07-reference/05-ai-sdk-errors/ai-unsupported-functionality-error.mdx +25 -0
  211. package/docs/07-reference/05-ai-sdk-errors/index.mdx +39 -0
  212. package/docs/07-reference/index.mdx +28 -0
  213. package/docs/08-migration-guides/00-versioning.mdx +46 -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 +126 -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 +244 -0
  267. package/src/agent/tool-loop-agent.ts +206 -0
  268. package/src/embed/embed-many-result.ts +53 -0
  269. package/src/embed/embed-many.ts +386 -0
  270. package/src/embed/embed-result.ts +50 -0
  271. package/src/embed/embed.ts +217 -0
  272. package/src/embed/index.ts +4 -0
  273. package/src/error/index.ts +37 -0
  274. package/src/error/invalid-argument-error.ts +34 -0
  275. package/src/error/invalid-stream-part-error.ts +28 -0
  276. package/src/error/invalid-tool-approval-error.ts +26 -0
  277. package/src/error/invalid-tool-input-error.ts +33 -0
  278. package/src/error/missing-tool-result-error.ts +28 -0
  279. package/src/error/no-image-generated-error.ts +39 -0
  280. package/src/error/no-object-generated-error.ts +70 -0
  281. package/src/error/no-output-generated-error.ts +26 -0
  282. package/src/error/no-speech-generated-error.ts +28 -0
  283. package/src/error/no-such-tool-error.ts +35 -0
  284. package/src/error/no-transcript-generated-error.ts +30 -0
  285. package/src/error/no-video-generated-error.ts +57 -0
  286. package/src/error/tool-call-not-found-for-approval-error.ts +32 -0
  287. package/src/error/tool-call-repair-error.ts +30 -0
  288. package/src/error/ui-message-stream-error.ts +48 -0
  289. package/src/error/unsupported-model-version-error.ts +23 -0
  290. package/src/error/verify-no-object-generated-error.ts +27 -0
  291. package/src/generate-image/generate-image-result.ts +42 -0
  292. package/src/generate-image/generate-image.ts +361 -0
  293. package/src/generate-image/index.ts +18 -0
  294. package/src/generate-object/generate-object-result.ts +67 -0
  295. package/src/generate-object/generate-object.ts +517 -0
  296. package/src/generate-object/index.ts +9 -0
  297. package/src/generate-object/inject-json-instruction.ts +30 -0
  298. package/src/generate-object/output-strategy.ts +415 -0
  299. package/src/generate-object/parse-and-validate-object-result.ts +111 -0
  300. package/src/generate-object/repair-text.ts +12 -0
  301. package/src/generate-object/stream-object-result.ts +120 -0
  302. package/src/generate-object/stream-object.ts +985 -0
  303. package/src/generate-object/validate-object-generation-input.ts +144 -0
  304. package/src/generate-speech/generate-speech-result.ts +30 -0
  305. package/src/generate-speech/generate-speech.ts +191 -0
  306. package/src/generate-speech/generated-audio-file.ts +65 -0
  307. package/src/generate-speech/index.ts +3 -0
  308. package/src/generate-text/callback-events.ts +332 -0
  309. package/src/generate-text/collect-tool-approvals.ts +116 -0
  310. package/src/generate-text/content-part.ts +25 -0
  311. package/src/generate-text/execute-tool-call.ts +197 -0
  312. package/src/generate-text/extract-reasoning-content.ts +17 -0
  313. package/src/generate-text/extract-text-content.ts +15 -0
  314. package/src/generate-text/generate-text-result.ts +168 -0
  315. package/src/generate-text/generate-text.ts +1535 -0
  316. package/src/generate-text/generated-file.ts +70 -0
  317. package/src/generate-text/index.ts +74 -0
  318. package/src/generate-text/is-approval-needed.ts +29 -0
  319. package/src/generate-text/output-utils.ts +23 -0
  320. package/src/generate-text/output.ts +590 -0
  321. package/src/generate-text/parse-tool-call.ts +188 -0
  322. package/src/generate-text/prepare-step.ts +103 -0
  323. package/src/generate-text/prune-messages.ts +167 -0
  324. package/src/generate-text/reasoning-output.ts +20 -0
  325. package/src/generate-text/reasoning.ts +8 -0
  326. package/src/generate-text/response-message.ts +10 -0
  327. package/src/generate-text/run-tools-transformation.ts +442 -0
  328. package/src/generate-text/smooth-stream.ts +162 -0
  329. package/src/generate-text/step-result.ts +290 -0
  330. package/src/generate-text/stop-condition.ts +29 -0
  331. package/src/generate-text/stream-text-result.ts +463 -0
  332. package/src/generate-text/stream-text.ts +2807 -0
  333. package/src/generate-text/to-response-messages.ts +163 -0
  334. package/src/generate-text/tool-approval-request-output.ts +21 -0
  335. package/src/generate-text/tool-call-repair-function.ts +27 -0
  336. package/src/generate-text/tool-call.ts +47 -0
  337. package/src/generate-text/tool-error.ts +34 -0
  338. package/src/generate-text/tool-output-denied.ts +21 -0
  339. package/src/generate-text/tool-output.ts +7 -0
  340. package/src/generate-text/tool-result.ts +36 -0
  341. package/src/generate-text/tool-set.ts +14 -0
  342. package/src/generate-video/generate-video-result.ts +36 -0
  343. package/src/generate-video/generate-video.ts +402 -0
  344. package/src/generate-video/index.ts +3 -0
  345. package/src/global.ts +36 -0
  346. package/src/index.ts +49 -0
  347. package/src/logger/index.ts +6 -0
  348. package/src/logger/log-warnings.ts +140 -0
  349. package/src/middleware/add-tool-input-examples-middleware.ts +90 -0
  350. package/src/middleware/default-embedding-settings-middleware.ts +22 -0
  351. package/src/middleware/default-settings-middleware.ts +33 -0
  352. package/src/middleware/extract-json-middleware.ts +197 -0
  353. package/src/middleware/extract-reasoning-middleware.ts +249 -0
  354. package/src/middleware/index.ts +10 -0
  355. package/src/middleware/simulate-streaming-middleware.ts +79 -0
  356. package/src/middleware/wrap-embedding-model.ts +86 -0
  357. package/src/middleware/wrap-image-model.ts +85 -0
  358. package/src/middleware/wrap-language-model.ts +104 -0
  359. package/src/middleware/wrap-provider.ts +51 -0
  360. package/src/model/as-embedding-model-v3.ts +24 -0
  361. package/src/model/as-image-model-v3.ts +24 -0
  362. package/src/model/as-language-model-v3.ts +103 -0
  363. package/src/model/as-provider-v3.ts +36 -0
  364. package/src/model/as-speech-model-v3.ts +24 -0
  365. package/src/model/as-transcription-model-v3.ts +24 -0
  366. package/src/model/resolve-model.ts +159 -0
  367. package/src/prompt/call-settings.ts +148 -0
  368. package/src/prompt/content-part.ts +215 -0
  369. package/src/prompt/convert-to-language-model-prompt.ts +526 -0
  370. package/src/prompt/create-tool-model-output.ts +34 -0
  371. package/src/prompt/data-content.ts +134 -0
  372. package/src/prompt/index.ts +27 -0
  373. package/src/prompt/invalid-data-content-error.ts +29 -0
  374. package/src/prompt/invalid-message-role-error.ts +27 -0
  375. package/src/prompt/message-conversion-error.ts +28 -0
  376. package/src/prompt/message.ts +68 -0
  377. package/src/prompt/prepare-call-settings.ts +108 -0
  378. package/src/prompt/prepare-tools-and-tool-choice.ts +86 -0
  379. package/src/prompt/prompt.ts +43 -0
  380. package/src/prompt/split-data-url.ts +17 -0
  381. package/src/prompt/standardize-prompt.ts +99 -0
  382. package/src/prompt/wrap-gateway-error.ts +29 -0
  383. package/src/registry/custom-provider.ts +176 -0
  384. package/src/registry/index.ts +7 -0
  385. package/src/registry/no-such-provider-error.ts +41 -0
  386. package/src/registry/provider-registry.ts +328 -0
  387. package/src/rerank/index.ts +2 -0
  388. package/src/rerank/rerank-result.ts +70 -0
  389. package/src/rerank/rerank.ts +239 -0
  390. package/src/telemetry/assemble-operation-name.ts +21 -0
  391. package/src/telemetry/get-base-telemetry-attributes.ts +53 -0
  392. package/src/telemetry/get-global-telemetry-integration.ts +85 -0
  393. package/src/telemetry/get-tracer.ts +20 -0
  394. package/src/telemetry/index.ts +4 -0
  395. package/src/telemetry/noop-tracer.ts +69 -0
  396. package/src/telemetry/record-span.ts +75 -0
  397. package/src/telemetry/select-telemetry-attributes.ts +78 -0
  398. package/src/telemetry/stringify-for-telemetry.ts +33 -0
  399. package/src/telemetry/telemetry-integration-registry.ts +17 -0
  400. package/src/telemetry/telemetry-integration.ts +24 -0
  401. package/src/telemetry/telemetry-settings.ts +53 -0
  402. package/src/test/mock-embedding-model-v2.ts +35 -0
  403. package/src/test/mock-embedding-model-v3.ts +48 -0
  404. package/src/test/mock-image-model-v2.ts +28 -0
  405. package/src/test/mock-image-model-v3.ts +28 -0
  406. package/src/test/mock-language-model-v2.ts +72 -0
  407. package/src/test/mock-language-model-v3.ts +77 -0
  408. package/src/test/mock-provider-v2.ts +68 -0
  409. package/src/test/mock-provider-v3.ts +80 -0
  410. package/src/test/mock-reranking-model-v3.ts +25 -0
  411. package/src/test/mock-server-response.ts +69 -0
  412. package/src/test/mock-speech-model-v2.ts +24 -0
  413. package/src/test/mock-speech-model-v3.ts +24 -0
  414. package/src/test/mock-tracer.ts +156 -0
  415. package/src/test/mock-transcription-model-v2.ts +24 -0
  416. package/src/test/mock-transcription-model-v3.ts +24 -0
  417. package/src/test/mock-values.ts +4 -0
  418. package/src/test/mock-video-model-v3.ts +28 -0
  419. package/src/test/not-implemented.ts +3 -0
  420. package/src/text-stream/create-text-stream-response.ts +30 -0
  421. package/src/text-stream/index.ts +2 -0
  422. package/src/text-stream/pipe-text-stream-to-response.ts +38 -0
  423. package/src/transcribe/index.ts +2 -0
  424. package/src/transcribe/transcribe-result.ts +60 -0
  425. package/src/transcribe/transcribe.ts +187 -0
  426. package/src/types/embedding-model-middleware.ts +3 -0
  427. package/src/types/embedding-model.ts +18 -0
  428. package/src/types/image-model-middleware.ts +3 -0
  429. package/src/types/image-model-response-metadata.ts +16 -0
  430. package/src/types/image-model.ts +19 -0
  431. package/src/types/index.ts +29 -0
  432. package/src/types/json-value.ts +15 -0
  433. package/src/types/language-model-middleware.ts +3 -0
  434. package/src/types/language-model-request-metadata.ts +6 -0
  435. package/src/types/language-model-response-metadata.ts +21 -0
  436. package/src/types/language-model.ts +104 -0
  437. package/src/types/provider-metadata.ts +16 -0
  438. package/src/types/provider.ts +55 -0
  439. package/src/types/reranking-model.ts +6 -0
  440. package/src/types/speech-model-response-metadata.ts +21 -0
  441. package/src/types/speech-model.ts +6 -0
  442. package/src/types/transcription-model-response-metadata.ts +16 -0
  443. package/src/types/transcription-model.ts +9 -0
  444. package/src/types/usage.ts +200 -0
  445. package/src/types/video-model-response-metadata.ts +28 -0
  446. package/src/types/video-model.ts +11 -0
  447. package/src/types/warning.ts +7 -0
  448. package/src/ui/call-completion-api.ts +157 -0
  449. package/src/ui/chat-transport.ts +83 -0
  450. package/src/ui/chat.ts +751 -0
  451. package/src/ui/convert-file-list-to-file-ui-parts.ts +36 -0
  452. package/src/ui/convert-to-model-messages.ts +376 -0
  453. package/src/ui/default-chat-transport.ts +36 -0
  454. package/src/ui/direct-chat-transport.ts +118 -0
  455. package/src/ui/http-chat-transport.ts +284 -0
  456. package/src/ui/index.ts +71 -0
  457. package/src/ui/last-assistant-message-is-complete-with-approval-responses.ts +44 -0
  458. package/src/ui/last-assistant-message-is-complete-with-tool-calls.ts +39 -0
  459. package/src/ui/process-text-stream.ts +16 -0
  460. package/src/ui/process-ui-message-stream.ts +793 -0
  461. package/src/ui/text-stream-chat-transport.ts +23 -0
  462. package/src/ui/transform-text-to-ui-message-stream.ts +27 -0
  463. package/src/ui/ui-messages.ts +536 -0
  464. package/src/ui/use-completion.ts +84 -0
  465. package/src/ui/validate-ui-messages.ts +506 -0
  466. package/src/ui-message-stream/create-ui-message-stream-response.ts +44 -0
  467. package/src/ui-message-stream/create-ui-message-stream.ts +145 -0
  468. package/src/ui-message-stream/get-response-ui-message-id.ts +35 -0
  469. package/src/ui-message-stream/handle-ui-message-stream-finish.ts +170 -0
  470. package/src/ui-message-stream/index.ts +14 -0
  471. package/src/ui-message-stream/json-to-sse-transform-stream.ts +17 -0
  472. package/src/ui-message-stream/pipe-ui-message-stream-to-response.ts +51 -0
  473. package/src/ui-message-stream/read-ui-message-stream.ts +87 -0
  474. package/src/ui-message-stream/ui-message-chunks.ts +346 -0
  475. package/src/ui-message-stream/ui-message-stream-headers.ts +7 -0
  476. package/src/ui-message-stream/ui-message-stream-on-finish-callback.ts +32 -0
  477. package/src/ui-message-stream/ui-message-stream-on-step-finish-callback.ts +25 -0
  478. package/src/ui-message-stream/ui-message-stream-response-init.ts +14 -0
  479. package/src/ui-message-stream/ui-message-stream-writer.ts +24 -0
  480. package/src/util/as-array.ts +3 -0
  481. package/src/util/async-iterable-stream.ts +94 -0
  482. package/src/util/consume-stream.ts +31 -0
  483. package/src/util/cosine-similarity.ts +46 -0
  484. package/src/util/create-resolvable-promise.ts +30 -0
  485. package/src/util/create-stitchable-stream.ts +112 -0
  486. package/src/util/data-url.ts +17 -0
  487. package/src/util/deep-partial.ts +84 -0
  488. package/src/util/detect-media-type.ts +226 -0
  489. package/src/util/download/create-download.ts +13 -0
  490. package/src/util/download/download-function.ts +45 -0
  491. package/src/util/download/download.ts +69 -0
  492. package/src/util/error-handler.ts +1 -0
  493. package/src/util/fix-json.ts +401 -0
  494. package/src/util/get-potential-start-index.ts +39 -0
  495. package/src/util/index.ts +12 -0
  496. package/src/util/is-deep-equal-data.ts +48 -0
  497. package/src/util/is-non-empty-object.ts +5 -0
  498. package/src/util/job.ts +1 -0
  499. package/src/util/log-v2-compatibility-warning.ts +21 -0
  500. package/src/util/merge-abort-signals.ts +43 -0
  501. package/src/util/merge-objects.ts +79 -0
  502. package/src/util/notify.ts +22 -0
  503. package/src/util/now.ts +4 -0
  504. package/src/util/parse-partial-json.ts +30 -0
  505. package/src/util/prepare-headers.ts +14 -0
  506. package/src/util/prepare-retries.ts +47 -0
  507. package/src/util/retry-error.ts +41 -0
  508. package/src/util/retry-with-exponential-backoff.ts +154 -0
  509. package/src/util/serial-job-executor.ts +36 -0
  510. package/src/util/simulate-readable-stream.ts +39 -0
  511. package/src/util/split-array.ts +20 -0
  512. package/src/util/value-of.ts +65 -0
  513. package/src/util/write-to-server-response.ts +49 -0
  514. package/src/version.ts +5 -0
  515. package/test.d.ts +1 -0
@@ -0,0 +1,2807 @@
1
+ import {
2
+ getErrorMessage,
3
+ LanguageModelV3,
4
+ LanguageModelV3ToolChoice,
5
+ SharedV3Warning,
6
+ UnsupportedFunctionalityError,
7
+ } from '@ai-sdk/provider';
8
+ import {
9
+ createIdGenerator,
10
+ DelayedPromise,
11
+ IdGenerator,
12
+ isAbortError,
13
+ ModelMessage,
14
+ ProviderOptions,
15
+ SystemModelMessage,
16
+ ToolApprovalResponse,
17
+ ToolContent,
18
+ } from '@ai-sdk/provider-utils';
19
+ import { Span } from '@opentelemetry/api';
20
+ import { ServerResponse } from 'node:http';
21
+ import { NoOutputGeneratedError } from '../error';
22
+ import { Listener, notify } from '../util/notify';
23
+ import { logWarnings } from '../logger/log-warnings';
24
+ import { resolveLanguageModel } from '../model/resolve-model';
25
+ import {
26
+ CallSettings,
27
+ getChunkTimeoutMs,
28
+ getStepTimeoutMs,
29
+ getTotalTimeoutMs,
30
+ TimeoutConfiguration,
31
+ } from '../prompt/call-settings';
32
+ import { convertToLanguageModelPrompt } from '../prompt/convert-to-language-model-prompt';
33
+ import { createToolModelOutput } from '../prompt/create-tool-model-output';
34
+ import { prepareCallSettings } from '../prompt/prepare-call-settings';
35
+ import { prepareToolsAndToolChoice } from '../prompt/prepare-tools-and-tool-choice';
36
+ import { Prompt } from '../prompt/prompt';
37
+ import { standardizePrompt } from '../prompt/standardize-prompt';
38
+ import { wrapGatewayError } from '../prompt/wrap-gateway-error';
39
+ import { assembleOperationName } from '../telemetry/assemble-operation-name';
40
+ import { getBaseTelemetryAttributes } from '../telemetry/get-base-telemetry-attributes';
41
+ import { getTracer } from '../telemetry/get-tracer';
42
+ import { recordSpan } from '../telemetry/record-span';
43
+ import { selectTelemetryAttributes } from '../telemetry/select-telemetry-attributes';
44
+ import { stringifyForTelemetry } from '../telemetry/stringify-for-telemetry';
45
+ import { getGlobalTelemetryIntegration } from '../telemetry/get-global-telemetry-integration';
46
+ import { TelemetrySettings } from '../telemetry/telemetry-settings';
47
+ import { createTextStreamResponse } from '../text-stream/create-text-stream-response';
48
+ import { pipeTextStreamToResponse } from '../text-stream/pipe-text-stream-to-response';
49
+ import { LanguageModelRequestMetadata } from '../types';
50
+ import {
51
+ CallWarning,
52
+ FinishReason,
53
+ LanguageModel,
54
+ ToolChoice,
55
+ } from '../types/language-model';
56
+ import { ProviderMetadata } from '../types/provider-metadata';
57
+ import {
58
+ addLanguageModelUsage,
59
+ createNullLanguageModelUsage,
60
+ LanguageModelUsage,
61
+ } from '../types/usage';
62
+ import { UIMessage } from '../ui';
63
+ import { createUIMessageStreamResponse } from '../ui-message-stream/create-ui-message-stream-response';
64
+ import { getResponseUIMessageId } from '../ui-message-stream/get-response-ui-message-id';
65
+ import { handleUIMessageStreamFinish } from '../ui-message-stream/handle-ui-message-stream-finish';
66
+ import { pipeUIMessageStreamToResponse } from '../ui-message-stream/pipe-ui-message-stream-to-response';
67
+ import {
68
+ InferUIMessageChunk,
69
+ UIMessageChunk,
70
+ } from '../ui-message-stream/ui-message-chunks';
71
+ import { UIMessageStreamResponseInit } from '../ui-message-stream/ui-message-stream-response-init';
72
+ import { InferUIMessageData, InferUIMessageMetadata } from '../ui/ui-messages';
73
+ import { asArray } from '../util/as-array';
74
+ import {
75
+ AsyncIterableStream,
76
+ createAsyncIterableStream,
77
+ } from '../util/async-iterable-stream';
78
+ import { consumeStream } from '../util/consume-stream';
79
+ import { createStitchableStream } from '../util/create-stitchable-stream';
80
+ import { DownloadFunction } from '../util/download/download-function';
81
+ import { mergeAbortSignals } from '../util/merge-abort-signals';
82
+ import { mergeObjects } from '../util/merge-objects';
83
+ import { now as originalNow } from '../util/now';
84
+ import { prepareRetries } from '../util/prepare-retries';
85
+ import { collectToolApprovals } from './collect-tool-approvals';
86
+ import type {
87
+ OnFinishEvent,
88
+ OnStartEvent,
89
+ OnStepFinishEvent,
90
+ OnStepStartEvent,
91
+ OnToolCallFinishEvent,
92
+ OnToolCallStartEvent,
93
+ } from './callback-events';
94
+ import { ContentPart } from './content-part';
95
+ import { executeToolCall } from './execute-tool-call';
96
+ import { Output, text } from './output';
97
+ import {
98
+ InferCompleteOutput,
99
+ InferElementOutput,
100
+ InferPartialOutput,
101
+ } from './output-utils';
102
+ import { PrepareStepFunction } from './prepare-step';
103
+ import { ResponseMessage } from './response-message';
104
+ import {
105
+ runToolsTransformation,
106
+ SingleRequestTextStreamPart,
107
+ } from './run-tools-transformation';
108
+ import { DefaultStepResult, StepResult } from './step-result';
109
+ import {
110
+ isStopConditionMet,
111
+ stepCountIs,
112
+ StopCondition,
113
+ } from './stop-condition';
114
+ import {
115
+ ConsumeStreamOptions,
116
+ StreamTextResult,
117
+ TextStreamPart,
118
+ UIMessageStreamOptions,
119
+ } from './stream-text-result';
120
+ import { toResponseMessages } from './to-response-messages';
121
+ import { TypedToolCall } from './tool-call';
122
+ import { ToolCallRepairFunction } from './tool-call-repair-function';
123
+ import { ToolOutput } from './tool-output';
124
+ import { StaticToolOutputDenied } from './tool-output-denied';
125
+ import { ToolSet } from './tool-set';
126
+
127
+ const originalGenerateId = createIdGenerator({
128
+ prefix: 'aitxt',
129
+ size: 24,
130
+ });
131
+
132
+ /**
133
+ * A transformation that is applied to the stream.
134
+ *
135
+ * @param stopStream - A function that stops the source stream.
136
+ * @param tools - The tools that are accessible to and can be called by the model. The model needs to support calling tools.
137
+ */
138
+ export type StreamTextTransform<TOOLS extends ToolSet> = (options: {
139
+ tools: TOOLS; // for type inference
140
+ stopStream: () => void;
141
+ }) => TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>;
142
+
143
+ /**
144
+ * Callback that is set using the `onError` option.
145
+ *
146
+ * @param event - The event that is passed to the callback.
147
+ */
148
+ export type StreamTextOnErrorCallback = (event: {
149
+ error: unknown;
150
+ }) => PromiseLike<void> | void;
151
+
152
+ /**
153
+ * Callback that is set using the `onStepFinish` option.
154
+ *
155
+ * @param stepResult - The result of the step.
156
+ */
157
+ export type StreamTextOnStepFinishCallback<TOOLS extends ToolSet> = (
158
+ event: OnStepFinishEvent<TOOLS>,
159
+ ) => PromiseLike<void> | void;
160
+
161
+ /**
162
+ * Callback that is set using the `onChunk` option.
163
+ *
164
+ * @param event - The event that is passed to the callback.
165
+ */
166
+ export type StreamTextOnChunkCallback<TOOLS extends ToolSet> = (event: {
167
+ chunk: Extract<
168
+ TextStreamPart<TOOLS>,
169
+ {
170
+ type:
171
+ | 'text-delta'
172
+ | 'reasoning-delta'
173
+ | 'source'
174
+ | 'tool-call'
175
+ | 'tool-input-start'
176
+ | 'tool-input-delta'
177
+ | 'tool-result'
178
+ | 'raw';
179
+ }
180
+ >;
181
+ }) => PromiseLike<void> | void;
182
+
183
+ /**
184
+ * Callback that is set using the `onFinish` option.
185
+ *
186
+ * @param event - The event that is passed to the callback.
187
+ */
188
+ export type StreamTextOnFinishCallback<TOOLS extends ToolSet> = (
189
+ event: OnFinishEvent<TOOLS>,
190
+ ) => PromiseLike<void> | void;
191
+
192
+ /**
193
+ * Callback that is set using the `onAbort` option.
194
+ *
195
+ * @param event - The event that is passed to the callback.
196
+ */
197
+ export type StreamTextOnAbortCallback<TOOLS extends ToolSet> = (event: {
198
+ /**
199
+ * Details for all previously finished steps.
200
+ */
201
+ readonly steps: StepResult<TOOLS>[];
202
+ }) => PromiseLike<void> | void;
203
+
204
+ /**
205
+ * Include settings for streamText (requestBody only).
206
+ */
207
+ type StreamTextIncludeSettings = { requestBody?: boolean };
208
+
209
+ /**
210
+ * Callback that is set using the `experimental_onStart` option.
211
+ *
212
+ * Called when the streamText operation begins, before any LLM calls.
213
+ * Use this callback for logging, analytics, or initializing state at the
214
+ * start of a generation.
215
+ *
216
+ * @param event - The event object containing generation configuration.
217
+ */
218
+ export type StreamTextOnStartCallback<
219
+ TOOLS extends ToolSet = ToolSet,
220
+ OUTPUT extends Output = Output,
221
+ > = (
222
+ event: OnStartEvent<TOOLS, OUTPUT, StreamTextIncludeSettings>,
223
+ ) => PromiseLike<void> | void;
224
+
225
+ /**
226
+ * Callback that is set using the `experimental_onStepStart` option.
227
+ *
228
+ * Called when a step (LLM call) begins, before the provider is called.
229
+ * Each step represents a single LLM invocation. Multiple steps occur when
230
+ * using tool calls (the model may be called multiple times in a loop).
231
+ *
232
+ * @param event - The event object containing step configuration.
233
+ */
234
+ export type StreamTextOnStepStartCallback<
235
+ TOOLS extends ToolSet = ToolSet,
236
+ OUTPUT extends Output = Output,
237
+ > = (
238
+ event: OnStepStartEvent<TOOLS, OUTPUT, StreamTextIncludeSettings>,
239
+ ) => PromiseLike<void> | void;
240
+
241
+ export type StreamTextOnToolCallStartCallback<TOOLS extends ToolSet = ToolSet> =
242
+ (event: OnToolCallStartEvent<TOOLS>) => PromiseLike<void> | void;
243
+
244
+ export type StreamTextOnToolCallFinishCallback<
245
+ TOOLS extends ToolSet = ToolSet,
246
+ > = (event: OnToolCallFinishEvent<TOOLS>) => PromiseLike<void> | void;
247
+
248
+ /**
249
+ * Generate a text and call tools for a given prompt using a language model.
250
+ *
251
+ * This function streams the output. If you do not want to stream the output, use `generateText` instead.
252
+ *
253
+ * @param model - The language model to use.
254
+ * @param tools - Tools that are accessible to and can be called by the model. The model needs to support calling tools.
255
+ *
256
+ * @param system - A system message that will be part of the prompt.
257
+ * @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
258
+ * @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
259
+ *
260
+ * @param maxOutputTokens - Maximum number of tokens to generate.
261
+ * @param temperature - Temperature setting.
262
+ * The value is passed through to the provider. The range depends on the provider and model.
263
+ * It is recommended to set either `temperature` or `topP`, but not both.
264
+ * @param topP - Nucleus sampling.
265
+ * The value is passed through to the provider. The range depends on the provider and model.
266
+ * It is recommended to set either `temperature` or `topP`, but not both.
267
+ * @param topK - Only sample from the top K options for each subsequent token.
268
+ * Used to remove "long tail" low probability responses.
269
+ * Recommended for advanced use cases only. You usually only need to use temperature.
270
+ * @param presencePenalty - Presence penalty setting.
271
+ * It affects the likelihood of the model to repeat information that is already in the prompt.
272
+ * The value is passed through to the provider. The range depends on the provider and model.
273
+ * @param frequencyPenalty - Frequency penalty setting.
274
+ * It affects the likelihood of the model to repeatedly use the same words or phrases.
275
+ * The value is passed through to the provider. The range depends on the provider and model.
276
+ * @param stopSequences - Stop sequences.
277
+ * If set, the model will stop generating text when one of the stop sequences is generated.
278
+ * @param seed - The seed (integer) to use for random sampling.
279
+ * If set and supported by the model, calls will generate deterministic results.
280
+ *
281
+ * @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
282
+ * @param abortSignal - An optional abort signal that can be used to cancel the call.
283
+ * @param timeout - An optional timeout in milliseconds. The call will be aborted if it takes longer than the specified timeout.
284
+ * @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
285
+ *
286
+ * @param onChunk - Callback that is called for each chunk of the stream. The stream processing will pause until the callback promise is resolved.
287
+ * @param onError - Callback that is called when an error occurs during streaming. You can use it to log errors.
288
+ * @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
289
+ * @param onFinish - Callback that is called when all steps are finished and the response is complete.
290
+ *
291
+ * @returns
292
+ * A result object for accessing different stream types and additional information.
293
+ */
294
+ export function streamText<
295
+ TOOLS extends ToolSet,
296
+ OUTPUT extends Output = Output<string, string, never>,
297
+ >({
298
+ model,
299
+ tools,
300
+ toolChoice,
301
+ system,
302
+ prompt,
303
+ messages,
304
+ maxRetries,
305
+ abortSignal,
306
+ timeout,
307
+ headers,
308
+ stopWhen = stepCountIs(1),
309
+ experimental_output,
310
+ output = experimental_output,
311
+ experimental_telemetry: telemetry,
312
+ prepareStep,
313
+ providerOptions,
314
+ experimental_activeTools,
315
+ activeTools = experimental_activeTools,
316
+ experimental_repairToolCall: repairToolCall,
317
+ experimental_transform: transform,
318
+ experimental_download: download,
319
+ includeRawChunks = false,
320
+ onChunk,
321
+ onError = ({ error }) => {
322
+ console.error(error);
323
+ },
324
+ onFinish,
325
+ onAbort,
326
+ onStepFinish,
327
+ experimental_onStart: onStart,
328
+ experimental_onStepStart: onStepStart,
329
+ experimental_onToolCallStart: onToolCallStart,
330
+ experimental_onToolCallFinish: onToolCallFinish,
331
+ experimental_context,
332
+ experimental_include: include,
333
+ _internal: { now = originalNow, generateId = originalGenerateId } = {},
334
+ ...settings
335
+ }: CallSettings &
336
+ Prompt & {
337
+ /**
338
+ * The language model to use.
339
+ */
340
+ model: LanguageModel;
341
+
342
+ /**
343
+ * The tools that the model can call. The model needs to support calling tools.
344
+ */
345
+ tools?: TOOLS;
346
+
347
+ /**
348
+ * The tool choice strategy. Default: 'auto'.
349
+ */
350
+ toolChoice?: ToolChoice<TOOLS>;
351
+
352
+ /**
353
+ * Condition for stopping the generation when there are tool results in the last step.
354
+ * When the condition is an array, any of the conditions can be met to stop the generation.
355
+ *
356
+ * @default stepCountIs(1)
357
+ */
358
+ stopWhen?:
359
+ | StopCondition<NoInfer<TOOLS>>
360
+ | Array<StopCondition<NoInfer<TOOLS>>>;
361
+
362
+ /**
363
+ * Optional telemetry configuration (experimental).
364
+ */
365
+ experimental_telemetry?: TelemetrySettings;
366
+
367
+ /**
368
+ * Additional provider-specific options. They are passed through
369
+ * to the provider from the AI SDK and enable provider-specific
370
+ * functionality that can be fully encapsulated in the provider.
371
+ */
372
+ providerOptions?: ProviderOptions;
373
+
374
+ /**
375
+ * @deprecated Use `activeTools` instead.
376
+ */
377
+ experimental_activeTools?: Array<keyof NoInfer<TOOLS>>;
378
+
379
+ /**
380
+ * Limits the tools that are available for the model to call without
381
+ * changing the tool call and result types in the result.
382
+ */
383
+ activeTools?: Array<keyof NoInfer<TOOLS>>;
384
+
385
+ /**
386
+ * Optional specification for parsing structured outputs from the LLM response.
387
+ */
388
+ output?: OUTPUT;
389
+
390
+ /**
391
+ * Optional specification for parsing structured outputs from the LLM response.
392
+ *
393
+ * @deprecated Use `output` instead.
394
+ */
395
+ experimental_output?: OUTPUT;
396
+
397
+ /**
398
+ * Optional function that you can use to provide different settings for a step.
399
+ *
400
+ * @param options - The options for the step.
401
+ * @param options.steps - The steps that have been executed so far.
402
+ * @param options.stepNumber - The number of the step that is being executed.
403
+ * @param options.model - The model that is being used.
404
+ *
405
+ * @returns An object that contains the settings for the step.
406
+ * If you return undefined (or for undefined settings), the settings from the outer level will be used.
407
+ */
408
+ prepareStep?: PrepareStepFunction<NoInfer<TOOLS>>;
409
+
410
+ /**
411
+ * A function that attempts to repair a tool call that failed to parse.
412
+ */
413
+ experimental_repairToolCall?: ToolCallRepairFunction<TOOLS>;
414
+
415
+ /**
416
+ * Optional stream transformations.
417
+ * They are applied in the order they are provided.
418
+ * The stream transformations must maintain the stream structure for streamText to work correctly.
419
+ */
420
+ experimental_transform?:
421
+ | StreamTextTransform<TOOLS>
422
+ | Array<StreamTextTransform<TOOLS>>;
423
+
424
+ /**
425
+ * Custom download function to use for URLs.
426
+ *
427
+ * By default, files are downloaded if the model does not support the URL for the given media type.
428
+ */
429
+ experimental_download?: DownloadFunction | undefined;
430
+
431
+ /**
432
+ * Whether to include raw chunks from the provider in the stream.
433
+ * When enabled, you will receive raw chunks with type 'raw' that contain the unprocessed data from the provider.
434
+ * This allows access to cutting-edge provider features not yet wrapped by the AI SDK.
435
+ * Defaults to false.
436
+ */
437
+ includeRawChunks?: boolean;
438
+
439
+ /**
440
+ * Callback that is called for each chunk of the stream.
441
+ * The stream processing will pause until the callback promise is resolved.
442
+ */
443
+ onChunk?: StreamTextOnChunkCallback<TOOLS>;
444
+
445
+ /**
446
+ * Callback that is invoked when an error occurs during streaming.
447
+ * You can use it to log errors.
448
+ * The stream processing will pause until the callback promise is resolved.
449
+ */
450
+ onError?: StreamTextOnErrorCallback;
451
+
452
+ /**
453
+ * Callback that is called when the LLM response and all request tool executions
454
+ * (for tools that have an `execute` function) are finished.
455
+ *
456
+ * The usage is the combined usage of all steps.
457
+ */
458
+ onFinish?: StreamTextOnFinishCallback<TOOLS>;
459
+
460
+ onAbort?: StreamTextOnAbortCallback<TOOLS>;
461
+
462
+ /**
463
+ * Callback that is called when each step (LLM call) is finished, including intermediate steps.
464
+ */
465
+ onStepFinish?: StreamTextOnStepFinishCallback<TOOLS>;
466
+
467
+ /**
468
+ * Callback that is called when the streamText operation begins,
469
+ * before any LLM calls are made.
470
+ */
471
+ experimental_onStart?: StreamTextOnStartCallback<NoInfer<TOOLS>, OUTPUT>;
472
+
473
+ /**
474
+ * Callback that is called when a step (LLM call) begins,
475
+ * before the provider is called.
476
+ */
477
+ experimental_onStepStart?: StreamTextOnStepStartCallback<
478
+ NoInfer<TOOLS>,
479
+ OUTPUT
480
+ >;
481
+
482
+ /**
483
+ * Callback that is called right before a tool's execute function runs.
484
+ */
485
+ experimental_onToolCallStart?: StreamTextOnToolCallStartCallback<
486
+ NoInfer<TOOLS>
487
+ >;
488
+
489
+ /**
490
+ * Callback that is called right after a tool's execute function completes (or errors).
491
+ */
492
+ experimental_onToolCallFinish?: StreamTextOnToolCallFinishCallback<
493
+ NoInfer<TOOLS>
494
+ >;
495
+
496
+ /**
497
+ * Context that is passed into tool execution.
498
+ *
499
+ * Experimental (can break in patch releases).
500
+ *
501
+ * @default undefined
502
+ */
503
+ experimental_context?: unknown;
504
+
505
+ /**
506
+ * Settings for controlling what data is included in step results.
507
+ * Disabling inclusion can help reduce memory usage when processing
508
+ * large payloads like images.
509
+ *
510
+ * By default, all data is included for backwards compatibility.
511
+ */
512
+ experimental_include?: {
513
+ /**
514
+ * Whether to retain the request body in step results.
515
+ * The request body can be large when sending images or files.
516
+ * @default true
517
+ */
518
+ requestBody?: boolean;
519
+ };
520
+
521
+ /**
522
+ * Internal. For test use only. May change without notice.
523
+ */
524
+ _internal?: {
525
+ now?: () => number;
526
+ generateId?: IdGenerator;
527
+ };
528
+ }): StreamTextResult<TOOLS, OUTPUT> {
529
+ const totalTimeoutMs = getTotalTimeoutMs(timeout);
530
+ const stepTimeoutMs = getStepTimeoutMs(timeout);
531
+ const chunkTimeoutMs = getChunkTimeoutMs(timeout);
532
+ const stepAbortController =
533
+ stepTimeoutMs != null ? new AbortController() : undefined;
534
+ const chunkAbortController =
535
+ chunkTimeoutMs != null ? new AbortController() : undefined;
536
+ return new DefaultStreamTextResult<TOOLS, OUTPUT>({
537
+ model: resolveLanguageModel(model),
538
+ telemetry,
539
+ headers,
540
+ settings,
541
+ maxRetries,
542
+ abortSignal: mergeAbortSignals(
543
+ abortSignal,
544
+ totalTimeoutMs != null ? AbortSignal.timeout(totalTimeoutMs) : undefined,
545
+ stepAbortController?.signal,
546
+ chunkAbortController?.signal,
547
+ ),
548
+ stepTimeoutMs,
549
+ stepAbortController,
550
+ chunkTimeoutMs,
551
+ chunkAbortController,
552
+ system,
553
+ prompt,
554
+ messages,
555
+ tools,
556
+ toolChoice,
557
+ transforms: asArray(transform),
558
+ activeTools,
559
+ repairToolCall,
560
+ stopConditions: asArray(stopWhen),
561
+ output,
562
+ providerOptions,
563
+ prepareStep,
564
+ includeRawChunks,
565
+ timeout,
566
+ stopWhen,
567
+ originalAbortSignal: abortSignal,
568
+ onChunk,
569
+ onError,
570
+ onFinish,
571
+ onAbort,
572
+ onStepFinish,
573
+ onStart,
574
+ onStepStart,
575
+ onToolCallStart,
576
+ onToolCallFinish,
577
+ now,
578
+ generateId,
579
+ experimental_context,
580
+ download,
581
+ include,
582
+ });
583
+ }
584
+
585
+ export type EnrichedStreamPart<TOOLS extends ToolSet, PARTIAL_OUTPUT> = {
586
+ part: TextStreamPart<TOOLS>;
587
+ partialOutput: PARTIAL_OUTPUT | undefined;
588
+ };
589
+
590
+ function createOutputTransformStream<
591
+ TOOLS extends ToolSet,
592
+ OUTPUT extends Output,
593
+ >(
594
+ output: OUTPUT,
595
+ ): TransformStream<
596
+ TextStreamPart<TOOLS>,
597
+ EnrichedStreamPart<TOOLS, InferPartialOutput<OUTPUT>>
598
+ > {
599
+ let firstTextChunkId: string | undefined = undefined;
600
+ let text = '';
601
+ let textChunk = '';
602
+ let textProviderMetadata: ProviderMetadata | undefined = undefined;
603
+ let lastPublishedJson = '';
604
+
605
+ function publishTextChunk({
606
+ controller,
607
+ partialOutput = undefined,
608
+ }: {
609
+ controller: TransformStreamDefaultController<
610
+ EnrichedStreamPart<TOOLS, InferPartialOutput<OUTPUT>>
611
+ >;
612
+ partialOutput?: InferPartialOutput<OUTPUT>;
613
+ }) {
614
+ controller.enqueue({
615
+ part: {
616
+ type: 'text-delta',
617
+ id: firstTextChunkId!,
618
+ text: textChunk,
619
+ providerMetadata: textProviderMetadata,
620
+ },
621
+ partialOutput,
622
+ });
623
+ textChunk = '';
624
+ }
625
+
626
+ return new TransformStream<
627
+ TextStreamPart<TOOLS>,
628
+ EnrichedStreamPart<TOOLS, InferPartialOutput<OUTPUT>>
629
+ >({
630
+ async transform(chunk, controller) {
631
+ // ensure that we publish the last text chunk before the step finish:
632
+ if (chunk.type === 'finish-step' && textChunk.length > 0) {
633
+ publishTextChunk({ controller });
634
+ }
635
+
636
+ if (
637
+ chunk.type !== 'text-delta' &&
638
+ chunk.type !== 'text-start' &&
639
+ chunk.type !== 'text-end'
640
+ ) {
641
+ controller.enqueue({ part: chunk, partialOutput: undefined });
642
+ return;
643
+ }
644
+
645
+ // we have to pick a text chunk which contains the json text
646
+ // since we are streaming, we have to pick the first text chunk
647
+ if (firstTextChunkId == null) {
648
+ firstTextChunkId = chunk.id;
649
+ } else if (chunk.id !== firstTextChunkId) {
650
+ controller.enqueue({ part: chunk, partialOutput: undefined });
651
+ return;
652
+ }
653
+
654
+ if (chunk.type === 'text-start') {
655
+ controller.enqueue({ part: chunk, partialOutput: undefined });
656
+ return;
657
+ }
658
+
659
+ if (chunk.type === 'text-end') {
660
+ if (textChunk.length > 0) {
661
+ publishTextChunk({ controller });
662
+ }
663
+ controller.enqueue({ part: chunk, partialOutput: undefined });
664
+ return;
665
+ }
666
+
667
+ text += chunk.text;
668
+ textChunk += chunk.text;
669
+ textProviderMetadata = chunk.providerMetadata ?? textProviderMetadata;
670
+
671
+ // only publish if partial json can be parsed:
672
+ const result = await output.parsePartialOutput({ text });
673
+
674
+ // null should be allowed (valid JSON value) but undefined should not:
675
+ if (result !== undefined) {
676
+ // only send new json if it has changed:
677
+ const currentJson = JSON.stringify(result.partial);
678
+ if (currentJson !== lastPublishedJson) {
679
+ publishTextChunk({ controller, partialOutput: result.partial });
680
+ lastPublishedJson = currentJson;
681
+ }
682
+ }
683
+ },
684
+ });
685
+ }
686
+
687
+ class DefaultStreamTextResult<TOOLS extends ToolSet, OUTPUT extends Output>
688
+ implements StreamTextResult<TOOLS, OUTPUT>
689
+ {
690
+ private readonly _totalUsage = new DelayedPromise<
691
+ Awaited<StreamTextResult<TOOLS, OUTPUT>['usage']>
692
+ >();
693
+ private readonly _finishReason = new DelayedPromise<
694
+ Awaited<StreamTextResult<TOOLS, OUTPUT>['finishReason']>
695
+ >();
696
+ private readonly _rawFinishReason = new DelayedPromise<
697
+ Awaited<StreamTextResult<TOOLS, OUTPUT>['rawFinishReason']>
698
+ >();
699
+ private readonly _steps = new DelayedPromise<
700
+ Awaited<StreamTextResult<TOOLS, OUTPUT>['steps']>
701
+ >();
702
+
703
+ private readonly addStream: (
704
+ stream: ReadableStream<TextStreamPart<TOOLS>>,
705
+ ) => void;
706
+
707
+ private readonly closeStream: () => void;
708
+
709
+ private baseStream: ReadableStream<
710
+ EnrichedStreamPart<TOOLS, InferPartialOutput<OUTPUT>>
711
+ >;
712
+
713
+ private outputSpecification: OUTPUT | undefined;
714
+
715
+ private includeRawChunks: boolean;
716
+
717
+ private tools: TOOLS | undefined;
718
+
719
+ constructor({
720
+ model,
721
+ telemetry,
722
+ headers,
723
+ settings,
724
+ maxRetries: maxRetriesArg,
725
+ abortSignal,
726
+ stepTimeoutMs,
727
+ stepAbortController,
728
+ chunkTimeoutMs,
729
+ chunkAbortController,
730
+ system,
731
+ prompt,
732
+ messages,
733
+ tools,
734
+ toolChoice,
735
+ transforms,
736
+ activeTools,
737
+ repairToolCall,
738
+ stopConditions,
739
+ output,
740
+ providerOptions,
741
+ prepareStep,
742
+ includeRawChunks,
743
+ now,
744
+ generateId,
745
+ timeout,
746
+ stopWhen,
747
+ originalAbortSignal,
748
+ onChunk,
749
+ onError,
750
+ onFinish,
751
+ onAbort,
752
+ onStepFinish,
753
+ onStart,
754
+ onStepStart,
755
+ onToolCallStart,
756
+ onToolCallFinish,
757
+ experimental_context,
758
+ download,
759
+ include,
760
+ }: {
761
+ model: LanguageModelV3;
762
+ telemetry: TelemetrySettings | undefined;
763
+ headers: Record<string, string | undefined> | undefined;
764
+ settings: Omit<CallSettings, 'abortSignal' | 'headers'>;
765
+ maxRetries: number | undefined;
766
+ abortSignal: AbortSignal | undefined;
767
+ stepTimeoutMs: number | undefined;
768
+ stepAbortController: AbortController | undefined;
769
+ chunkTimeoutMs: number | undefined;
770
+ chunkAbortController: AbortController | undefined;
771
+ system: Prompt['system'];
772
+ prompt: Prompt['prompt'];
773
+ messages: Prompt['messages'];
774
+ tools: TOOLS | undefined;
775
+ toolChoice: ToolChoice<TOOLS> | undefined;
776
+ transforms: Array<StreamTextTransform<TOOLS>>;
777
+ activeTools: Array<keyof TOOLS> | undefined;
778
+ repairToolCall: ToolCallRepairFunction<TOOLS> | undefined;
779
+ stopConditions: Array<StopCondition<NoInfer<TOOLS>>>;
780
+ output: OUTPUT | undefined;
781
+ providerOptions: ProviderOptions | undefined;
782
+ prepareStep: PrepareStepFunction<NoInfer<TOOLS>> | undefined;
783
+ includeRawChunks: boolean;
784
+ now: () => number;
785
+ generateId: () => string;
786
+ timeout: TimeoutConfiguration | undefined;
787
+ stopWhen:
788
+ | StopCondition<NoInfer<TOOLS>>
789
+ | Array<StopCondition<NoInfer<TOOLS>>>
790
+ | undefined;
791
+ originalAbortSignal: AbortSignal | undefined;
792
+ experimental_context: unknown;
793
+ download: DownloadFunction | undefined;
794
+ include: { requestBody?: boolean } | undefined;
795
+
796
+ // callbacks:
797
+ onChunk: undefined | StreamTextOnChunkCallback<TOOLS>;
798
+ onError: StreamTextOnErrorCallback;
799
+ onFinish: undefined | StreamTextOnFinishCallback<TOOLS>;
800
+ onAbort: undefined | StreamTextOnAbortCallback<TOOLS>;
801
+ onStepFinish: undefined | StreamTextOnStepFinishCallback<TOOLS>;
802
+ onStart: undefined | StreamTextOnStartCallback<TOOLS, OUTPUT>;
803
+ onStepStart: undefined | StreamTextOnStepStartCallback<TOOLS, OUTPUT>;
804
+ onToolCallStart: undefined | StreamTextOnToolCallStartCallback<TOOLS>;
805
+ onToolCallFinish: undefined | StreamTextOnToolCallFinishCallback<TOOLS>;
806
+ }) {
807
+ this.outputSpecification = output;
808
+ this.includeRawChunks = includeRawChunks;
809
+ this.tools = tools;
810
+
811
+ const createGlobalTelemetry = getGlobalTelemetryIntegration<
812
+ TOOLS,
813
+ OUTPUT
814
+ >();
815
+ const globalTelemetry = createGlobalTelemetry(telemetry?.integrations);
816
+
817
+ // promise to ensure that the step has been fully processed by the event processor
818
+ // before a new step is started. This is required because the continuation condition
819
+ // needs the updated steps to determine if another step is needed.
820
+ let stepFinish!: DelayedPromise<void>;
821
+
822
+ let recordedContent: Array<ContentPart<TOOLS>> = [];
823
+ const recordedResponseMessages: Array<ResponseMessage> = [];
824
+ let recordedFinishReason: FinishReason | undefined = undefined;
825
+ let recordedRawFinishReason: string | undefined = undefined;
826
+ let recordedTotalUsage: LanguageModelUsage | undefined = undefined;
827
+ let recordedRequest: LanguageModelRequestMetadata = {};
828
+ let recordedWarnings: Array<CallWarning> = [];
829
+ const recordedSteps: StepResult<TOOLS>[] = [];
830
+
831
+ // Track provider-executed tool calls that support deferred results
832
+ // (e.g., code_execution in programmatic tool calling scenarios).
833
+ // These tools may not return their results in the same turn as their call.
834
+ const pendingDeferredToolCalls = new Map<string, { toolName: string }>();
835
+
836
+ let rootSpan!: Span;
837
+
838
+ let activeTextContent: Record<
839
+ string,
840
+ {
841
+ type: 'text';
842
+ text: string;
843
+ providerMetadata: ProviderMetadata | undefined;
844
+ }
845
+ > = {};
846
+
847
+ let activeReasoningContent: Record<
848
+ string,
849
+ {
850
+ type: 'reasoning';
851
+ text: string;
852
+ providerMetadata: ProviderMetadata | undefined;
853
+ }
854
+ > = {};
855
+
856
+ const eventProcessor = new TransformStream<
857
+ EnrichedStreamPart<TOOLS, InferPartialOutput<OUTPUT>>,
858
+ EnrichedStreamPart<TOOLS, InferPartialOutput<OUTPUT>>
859
+ >({
860
+ async transform(chunk, controller) {
861
+ controller.enqueue(chunk); // forward the chunk to the next stream
862
+
863
+ const { part } = chunk;
864
+
865
+ if (
866
+ part.type === 'text-delta' ||
867
+ part.type === 'reasoning-delta' ||
868
+ part.type === 'source' ||
869
+ part.type === 'tool-call' ||
870
+ part.type === 'tool-result' ||
871
+ part.type === 'tool-input-start' ||
872
+ part.type === 'tool-input-delta' ||
873
+ part.type === 'raw'
874
+ ) {
875
+ await onChunk?.({ chunk: part });
876
+ }
877
+
878
+ if (part.type === 'error') {
879
+ await onError({ error: wrapGatewayError(part.error) });
880
+ }
881
+
882
+ if (part.type === 'text-start') {
883
+ activeTextContent[part.id] = {
884
+ type: 'text',
885
+ text: '',
886
+ providerMetadata: part.providerMetadata,
887
+ };
888
+
889
+ recordedContent.push(activeTextContent[part.id]);
890
+ }
891
+
892
+ if (part.type === 'text-delta') {
893
+ const activeText = activeTextContent[part.id];
894
+
895
+ if (activeText == null) {
896
+ controller.enqueue({
897
+ part: {
898
+ type: 'error',
899
+ error: `text part ${part.id} not found`,
900
+ },
901
+ partialOutput: undefined,
902
+ });
903
+ return;
904
+ }
905
+
906
+ activeText.text += part.text;
907
+ activeText.providerMetadata =
908
+ part.providerMetadata ?? activeText.providerMetadata;
909
+ }
910
+
911
+ if (part.type === 'text-end') {
912
+ const activeText = activeTextContent[part.id];
913
+
914
+ if (activeText == null) {
915
+ controller.enqueue({
916
+ part: {
917
+ type: 'error',
918
+ error: `text part ${part.id} not found`,
919
+ },
920
+ partialOutput: undefined,
921
+ });
922
+ return;
923
+ }
924
+
925
+ activeText.providerMetadata =
926
+ part.providerMetadata ?? activeText.providerMetadata;
927
+
928
+ delete activeTextContent[part.id];
929
+ }
930
+
931
+ if (part.type === 'reasoning-start') {
932
+ activeReasoningContent[part.id] = {
933
+ type: 'reasoning',
934
+ text: '',
935
+ providerMetadata: part.providerMetadata,
936
+ };
937
+
938
+ recordedContent.push(activeReasoningContent[part.id]);
939
+ }
940
+
941
+ if (part.type === 'reasoning-delta') {
942
+ const activeReasoning = activeReasoningContent[part.id];
943
+
944
+ if (activeReasoning == null) {
945
+ controller.enqueue({
946
+ part: {
947
+ type: 'error',
948
+ error: `reasoning part ${part.id} not found`,
949
+ },
950
+ partialOutput: undefined,
951
+ });
952
+ return;
953
+ }
954
+
955
+ activeReasoning.text += part.text;
956
+ activeReasoning.providerMetadata =
957
+ part.providerMetadata ?? activeReasoning.providerMetadata;
958
+ }
959
+
960
+ if (part.type === 'reasoning-end') {
961
+ const activeReasoning = activeReasoningContent[part.id];
962
+
963
+ if (activeReasoning == null) {
964
+ controller.enqueue({
965
+ part: {
966
+ type: 'error',
967
+ error: `reasoning part ${part.id} not found`,
968
+ },
969
+ partialOutput: undefined,
970
+ });
971
+ return;
972
+ }
973
+
974
+ activeReasoning.providerMetadata =
975
+ part.providerMetadata ?? activeReasoning.providerMetadata;
976
+
977
+ delete activeReasoningContent[part.id];
978
+ }
979
+
980
+ if (part.type === 'file') {
981
+ recordedContent.push({
982
+ type: 'file',
983
+ file: part.file,
984
+ ...(part.providerMetadata != null
985
+ ? { providerMetadata: part.providerMetadata }
986
+ : {}),
987
+ });
988
+ }
989
+
990
+ if (part.type === 'source') {
991
+ recordedContent.push(part);
992
+ }
993
+
994
+ if (part.type === 'tool-call') {
995
+ recordedContent.push(part);
996
+ }
997
+
998
+ if (part.type === 'tool-result' && !part.preliminary) {
999
+ recordedContent.push(part);
1000
+ }
1001
+
1002
+ if (part.type === 'tool-approval-request') {
1003
+ recordedContent.push(part);
1004
+ }
1005
+
1006
+ if (part.type === 'tool-error') {
1007
+ recordedContent.push(part);
1008
+ }
1009
+
1010
+ if (part.type === 'start-step') {
1011
+ // reset the recorded data when a new step starts:
1012
+ recordedContent = [];
1013
+ activeReasoningContent = {};
1014
+ activeTextContent = {};
1015
+
1016
+ recordedRequest = part.request;
1017
+ recordedWarnings = part.warnings;
1018
+ }
1019
+
1020
+ if (part.type === 'finish-step') {
1021
+ const stepMessages = await toResponseMessages({
1022
+ content: recordedContent,
1023
+ tools,
1024
+ });
1025
+
1026
+ // Add step information (after response messages are updated):
1027
+ const currentStepResult: StepResult<TOOLS> = new DefaultStepResult({
1028
+ stepNumber: recordedSteps.length,
1029
+ model: modelInfo,
1030
+ ...callbackTelemetryProps,
1031
+ experimental_context,
1032
+ content: recordedContent,
1033
+ finishReason: part.finishReason,
1034
+ rawFinishReason: part.rawFinishReason,
1035
+ usage: part.usage,
1036
+ warnings: recordedWarnings,
1037
+ request: recordedRequest,
1038
+ response: {
1039
+ ...part.response,
1040
+ messages: [...recordedResponseMessages, ...stepMessages],
1041
+ },
1042
+ providerMetadata: part.providerMetadata,
1043
+ });
1044
+
1045
+ await notify({
1046
+ event: currentStepResult,
1047
+ callbacks: [onStepFinish, globalTelemetry.onStepFinish],
1048
+ });
1049
+
1050
+ logWarnings({
1051
+ warnings: recordedWarnings,
1052
+ provider: modelInfo.provider,
1053
+ model: modelInfo.modelId,
1054
+ });
1055
+
1056
+ recordedSteps.push(currentStepResult);
1057
+
1058
+ recordedResponseMessages.push(...stepMessages);
1059
+
1060
+ // resolve the promise to signal that the step has been fully processed
1061
+ // by the event processor:
1062
+ stepFinish.resolve();
1063
+ }
1064
+
1065
+ if (part.type === 'finish') {
1066
+ recordedTotalUsage = part.totalUsage;
1067
+ recordedFinishReason = part.finishReason;
1068
+ recordedRawFinishReason = part.rawFinishReason;
1069
+ }
1070
+ },
1071
+
1072
+ async flush(controller) {
1073
+ try {
1074
+ if (recordedSteps.length === 0) {
1075
+ const error = abortSignal?.aborted
1076
+ ? abortSignal.reason
1077
+ : new NoOutputGeneratedError({
1078
+ message: 'No output generated. Check the stream for errors.',
1079
+ });
1080
+
1081
+ self._finishReason.reject(error);
1082
+ self._rawFinishReason.reject(error);
1083
+ self._totalUsage.reject(error);
1084
+ self._steps.reject(error);
1085
+
1086
+ return; // no steps recorded (e.g. in error scenario)
1087
+ }
1088
+
1089
+ // derived:
1090
+ const finishReason = recordedFinishReason ?? 'other';
1091
+ const totalUsage =
1092
+ recordedTotalUsage ?? createNullLanguageModelUsage();
1093
+
1094
+ // from finish:
1095
+ self._finishReason.resolve(finishReason);
1096
+ self._rawFinishReason.resolve(recordedRawFinishReason);
1097
+ self._totalUsage.resolve(totalUsage);
1098
+
1099
+ // aggregate results:
1100
+ self._steps.resolve(recordedSteps);
1101
+
1102
+ // call onFinish callback:
1103
+ const finalStep = recordedSteps[recordedSteps.length - 1];
1104
+
1105
+ await notify({
1106
+ event: {
1107
+ stepNumber: finalStep.stepNumber,
1108
+ model: finalStep.model,
1109
+ functionId: finalStep.functionId,
1110
+ metadata: finalStep.metadata,
1111
+ experimental_context: finalStep.experimental_context,
1112
+ finishReason: finalStep.finishReason,
1113
+ rawFinishReason: finalStep.rawFinishReason,
1114
+ totalUsage,
1115
+ usage: finalStep.usage,
1116
+ content: finalStep.content,
1117
+ text: finalStep.text,
1118
+ reasoningText: finalStep.reasoningText,
1119
+ reasoning: finalStep.reasoning,
1120
+ files: finalStep.files,
1121
+ sources: finalStep.sources,
1122
+ toolCalls: finalStep.toolCalls,
1123
+ staticToolCalls: finalStep.staticToolCalls,
1124
+ dynamicToolCalls: finalStep.dynamicToolCalls,
1125
+ toolResults: finalStep.toolResults,
1126
+ staticToolResults: finalStep.staticToolResults,
1127
+ dynamicToolResults: finalStep.dynamicToolResults,
1128
+ request: finalStep.request,
1129
+ response: finalStep.response,
1130
+ warnings: finalStep.warnings,
1131
+ providerMetadata: finalStep.providerMetadata,
1132
+ steps: recordedSteps,
1133
+ },
1134
+ callbacks: [
1135
+ onFinish,
1136
+ globalTelemetry.onFinish as
1137
+ | undefined
1138
+ | StreamTextOnFinishCallback<TOOLS>,
1139
+ ],
1140
+ });
1141
+
1142
+ // Add response information to the root span:
1143
+ rootSpan.setAttributes(
1144
+ await selectTelemetryAttributes({
1145
+ telemetry,
1146
+ attributes: {
1147
+ 'ai.response.finishReason': finishReason,
1148
+ 'ai.response.text': { output: () => finalStep.text },
1149
+ 'ai.response.reasoning': {
1150
+ output: () => finalStep.reasoningText,
1151
+ },
1152
+ 'ai.response.toolCalls': {
1153
+ output: () =>
1154
+ finalStep.toolCalls?.length
1155
+ ? JSON.stringify(finalStep.toolCalls)
1156
+ : undefined,
1157
+ },
1158
+ 'ai.response.providerMetadata': JSON.stringify(
1159
+ finalStep.providerMetadata,
1160
+ ),
1161
+
1162
+ 'ai.usage.inputTokens': totalUsage.inputTokens,
1163
+ 'ai.usage.outputTokens': totalUsage.outputTokens,
1164
+ 'ai.usage.totalTokens': totalUsage.totalTokens,
1165
+ 'ai.usage.reasoningTokens': totalUsage.reasoningTokens,
1166
+ 'ai.usage.cachedInputTokens': totalUsage.cachedInputTokens,
1167
+ },
1168
+ }),
1169
+ );
1170
+ } catch (error) {
1171
+ controller.error(error);
1172
+ } finally {
1173
+ rootSpan.end();
1174
+ }
1175
+ },
1176
+ });
1177
+
1178
+ // initialize the stitchable stream and the transformed stream:
1179
+ const stitchableStream = createStitchableStream<TextStreamPart<TOOLS>>();
1180
+ this.addStream = stitchableStream.addStream;
1181
+ this.closeStream = stitchableStream.close;
1182
+
1183
+ // resilient stream that handles abort signals and errors:
1184
+ const reader = stitchableStream.stream.getReader();
1185
+ let stream = new ReadableStream<TextStreamPart<TOOLS>>({
1186
+ async start(controller) {
1187
+ // send start event:
1188
+ controller.enqueue({ type: 'start' });
1189
+ },
1190
+
1191
+ async pull(controller) {
1192
+ // abort handling:
1193
+ function abort() {
1194
+ onAbort?.({ steps: recordedSteps });
1195
+ controller.enqueue({
1196
+ type: 'abort',
1197
+ // The `reason` is usually of type DOMException, but it can also be of any type,
1198
+ // so we use getErrorMessage for serialization because it is already designed to accept values of the unknown type.
1199
+ // See: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/reason
1200
+ ...(abortSignal?.reason !== undefined
1201
+ ? { reason: getErrorMessage(abortSignal.reason) }
1202
+ : {}),
1203
+ });
1204
+ controller.close();
1205
+ }
1206
+
1207
+ try {
1208
+ const { done, value } = await reader.read();
1209
+
1210
+ if (done) {
1211
+ controller.close();
1212
+ return;
1213
+ }
1214
+
1215
+ if (abortSignal?.aborted) {
1216
+ abort();
1217
+ return;
1218
+ }
1219
+
1220
+ controller.enqueue(value);
1221
+ } catch (error) {
1222
+ if (isAbortError(error) && abortSignal?.aborted) {
1223
+ abort();
1224
+ } else {
1225
+ controller.error(error);
1226
+ }
1227
+ }
1228
+ },
1229
+
1230
+ cancel(reason) {
1231
+ return stitchableStream.stream.cancel(reason);
1232
+ },
1233
+ });
1234
+
1235
+ // transform the stream before output parsing
1236
+ // to enable replacement of stream segments:
1237
+ for (const transform of transforms) {
1238
+ stream = stream.pipeThrough(
1239
+ transform({
1240
+ tools: tools as TOOLS,
1241
+ stopStream() {
1242
+ stitchableStream.terminate();
1243
+ },
1244
+ }),
1245
+ );
1246
+ }
1247
+
1248
+ this.baseStream = stream
1249
+ .pipeThrough(createOutputTransformStream(output ?? text()))
1250
+ .pipeThrough(eventProcessor);
1251
+
1252
+ const { maxRetries, retry } = prepareRetries({
1253
+ maxRetries: maxRetriesArg,
1254
+ abortSignal,
1255
+ });
1256
+
1257
+ const tracer = getTracer(telemetry);
1258
+
1259
+ const callSettings = prepareCallSettings(settings);
1260
+
1261
+ const baseTelemetryAttributes = getBaseTelemetryAttributes({
1262
+ model,
1263
+ telemetry,
1264
+ headers,
1265
+ settings: { ...callSettings, maxRetries },
1266
+ });
1267
+
1268
+ const self = this;
1269
+
1270
+ const modelInfo = { provider: model.provider, modelId: model.modelId };
1271
+ const callbackTelemetryProps = {
1272
+ functionId: telemetry?.functionId,
1273
+ metadata: telemetry?.metadata as Record<string, unknown> | undefined,
1274
+ };
1275
+
1276
+ recordSpan({
1277
+ name: 'ai.streamText',
1278
+ attributes: selectTelemetryAttributes({
1279
+ telemetry,
1280
+ attributes: {
1281
+ ...assembleOperationName({ operationId: 'ai.streamText', telemetry }),
1282
+ ...baseTelemetryAttributes,
1283
+ // specific settings that only make sense on the outer level:
1284
+ 'ai.prompt': {
1285
+ input: () => JSON.stringify({ system, prompt, messages }),
1286
+ },
1287
+ },
1288
+ }),
1289
+ tracer,
1290
+ endWhenDone: false,
1291
+ fn: async rootSpanArg => {
1292
+ rootSpan = rootSpanArg;
1293
+
1294
+ const initialPrompt = await standardizePrompt({
1295
+ system,
1296
+ prompt,
1297
+ messages,
1298
+ } as Prompt);
1299
+
1300
+ await notify({
1301
+ event: {
1302
+ model: modelInfo,
1303
+ system,
1304
+ prompt,
1305
+ messages,
1306
+ tools,
1307
+ toolChoice,
1308
+ activeTools,
1309
+ maxOutputTokens: callSettings.maxOutputTokens,
1310
+ temperature: callSettings.temperature,
1311
+ topP: callSettings.topP,
1312
+ topK: callSettings.topK,
1313
+ presencePenalty: callSettings.presencePenalty,
1314
+ frequencyPenalty: callSettings.frequencyPenalty,
1315
+ stopSequences: callSettings.stopSequences,
1316
+ seed: callSettings.seed,
1317
+ maxRetries,
1318
+ timeout,
1319
+ headers,
1320
+ providerOptions,
1321
+ stopWhen,
1322
+ output,
1323
+ abortSignal: originalAbortSignal,
1324
+ include,
1325
+ ...callbackTelemetryProps,
1326
+ experimental_context,
1327
+ },
1328
+ callbacks: [
1329
+ onStart,
1330
+ globalTelemetry.onStart as
1331
+ | undefined
1332
+ | StreamTextOnStartCallback<TOOLS, OUTPUT>,
1333
+ ],
1334
+ });
1335
+
1336
+ const initialMessages = initialPrompt.messages;
1337
+ const initialResponseMessages: Array<ResponseMessage> = [];
1338
+
1339
+ const { approvedToolApprovals, deniedToolApprovals } =
1340
+ collectToolApprovals<TOOLS>({ messages: initialMessages });
1341
+
1342
+ // initial tool execution step stream
1343
+ if (
1344
+ deniedToolApprovals.length > 0 ||
1345
+ approvedToolApprovals.length > 0
1346
+ ) {
1347
+ const providerExecutedToolApprovals = [
1348
+ ...approvedToolApprovals,
1349
+ ...deniedToolApprovals,
1350
+ ].filter(toolApproval => toolApproval.toolCall.providerExecuted);
1351
+
1352
+ const localApprovedToolApprovals = approvedToolApprovals.filter(
1353
+ toolApproval => !toolApproval.toolCall.providerExecuted,
1354
+ );
1355
+ const localDeniedToolApprovals = deniedToolApprovals.filter(
1356
+ toolApproval => !toolApproval.toolCall.providerExecuted,
1357
+ );
1358
+
1359
+ const deniedProviderExecutedToolApprovals =
1360
+ deniedToolApprovals.filter(
1361
+ toolApproval => toolApproval.toolCall.providerExecuted,
1362
+ );
1363
+
1364
+ let toolExecutionStepStreamController:
1365
+ | ReadableStreamDefaultController<TextStreamPart<TOOLS>>
1366
+ | undefined;
1367
+ const toolExecutionStepStream = new ReadableStream<
1368
+ TextStreamPart<TOOLS>
1369
+ >({
1370
+ start(controller) {
1371
+ toolExecutionStepStreamController = controller;
1372
+ },
1373
+ });
1374
+
1375
+ self.addStream(toolExecutionStepStream);
1376
+
1377
+ try {
1378
+ for (const toolApproval of [
1379
+ ...localDeniedToolApprovals,
1380
+ ...deniedProviderExecutedToolApprovals,
1381
+ ]) {
1382
+ toolExecutionStepStreamController?.enqueue({
1383
+ type: 'tool-output-denied',
1384
+ toolCallId: toolApproval.toolCall.toolCallId,
1385
+ toolName: toolApproval.toolCall.toolName,
1386
+ } as StaticToolOutputDenied<TOOLS>);
1387
+ }
1388
+
1389
+ const toolOutputs: Array<ToolOutput<TOOLS>> = [];
1390
+
1391
+ await Promise.all(
1392
+ localApprovedToolApprovals.map(async toolApproval => {
1393
+ const result = await executeToolCall({
1394
+ toolCall: toolApproval.toolCall,
1395
+ tools,
1396
+ tracer,
1397
+ telemetry,
1398
+ messages: initialMessages,
1399
+ abortSignal,
1400
+ experimental_context,
1401
+ stepNumber: recordedSteps.length,
1402
+ model: modelInfo,
1403
+ onToolCallStart: [
1404
+ onToolCallStart,
1405
+ globalTelemetry.onToolCallStart as
1406
+ | undefined
1407
+ | StreamTextOnToolCallStartCallback<TOOLS>,
1408
+ ],
1409
+ onToolCallFinish: [
1410
+ onToolCallFinish,
1411
+ globalTelemetry.onToolCallFinish,
1412
+ ],
1413
+ onPreliminaryToolResult: result => {
1414
+ toolExecutionStepStreamController?.enqueue(result);
1415
+ },
1416
+ });
1417
+
1418
+ if (result != null) {
1419
+ toolExecutionStepStreamController?.enqueue(result);
1420
+ toolOutputs.push(result);
1421
+ }
1422
+ }),
1423
+ );
1424
+
1425
+ // forward provider-executed approval responses to the provider (do not execute locally):
1426
+ if (providerExecutedToolApprovals.length > 0) {
1427
+ initialResponseMessages.push({
1428
+ role: 'tool',
1429
+ content: providerExecutedToolApprovals.map(
1430
+ toolApproval =>
1431
+ ({
1432
+ type: 'tool-approval-response',
1433
+ approvalId: toolApproval.approvalResponse.approvalId,
1434
+ approved: toolApproval.approvalResponse.approved,
1435
+ reason: toolApproval.approvalResponse.reason,
1436
+ providerExecuted: true,
1437
+ }) satisfies ToolApprovalResponse,
1438
+ ),
1439
+ });
1440
+ }
1441
+
1442
+ // Local tool results (approved + denied) are sent as tool results:
1443
+ if (toolOutputs.length > 0 || localDeniedToolApprovals.length > 0) {
1444
+ const localToolContent: ToolContent = [];
1445
+
1446
+ // add regular tool results for approved tool calls:
1447
+ for (const output of toolOutputs) {
1448
+ localToolContent.push({
1449
+ type: 'tool-result' as const,
1450
+ toolCallId: output.toolCallId,
1451
+ toolName: output.toolName,
1452
+ output: await createToolModelOutput({
1453
+ toolCallId: output.toolCallId,
1454
+ input: output.input,
1455
+ tool: tools?.[output.toolName],
1456
+ output:
1457
+ output.type === 'tool-result'
1458
+ ? output.output
1459
+ : output.error,
1460
+ errorMode: output.type === 'tool-error' ? 'text' : 'none',
1461
+ }),
1462
+ });
1463
+ }
1464
+
1465
+ // add execution denied tool results for denied local tool approvals:
1466
+ for (const toolApproval of localDeniedToolApprovals) {
1467
+ localToolContent.push({
1468
+ type: 'tool-result' as const,
1469
+ toolCallId: toolApproval.toolCall.toolCallId,
1470
+ toolName: toolApproval.toolCall.toolName,
1471
+ output: {
1472
+ type: 'execution-denied' as const,
1473
+ reason: toolApproval.approvalResponse.reason,
1474
+ },
1475
+ });
1476
+ }
1477
+
1478
+ initialResponseMessages.push({
1479
+ role: 'tool',
1480
+ content: localToolContent,
1481
+ });
1482
+ }
1483
+ } finally {
1484
+ toolExecutionStepStreamController?.close();
1485
+ }
1486
+ }
1487
+
1488
+ recordedResponseMessages.push(...initialResponseMessages);
1489
+
1490
+ async function streamStep({
1491
+ currentStep,
1492
+ responseMessages,
1493
+ usage,
1494
+ }: {
1495
+ currentStep: number;
1496
+ responseMessages: Array<ResponseMessage>;
1497
+ usage: LanguageModelUsage;
1498
+ }) {
1499
+ const includeRawChunks = self.includeRawChunks;
1500
+
1501
+ // Set up step timeout if configured
1502
+ const stepTimeoutId =
1503
+ stepTimeoutMs != null
1504
+ ? setTimeout(() => stepAbortController!.abort(), stepTimeoutMs)
1505
+ : undefined;
1506
+
1507
+ // Set up chunk timeout tracking (will be reset on each chunk)
1508
+ let chunkTimeoutId: ReturnType<typeof setTimeout> | undefined =
1509
+ undefined;
1510
+
1511
+ function resetChunkTimeout() {
1512
+ if (chunkTimeoutMs != null) {
1513
+ if (chunkTimeoutId != null) {
1514
+ clearTimeout(chunkTimeoutId);
1515
+ }
1516
+ chunkTimeoutId = setTimeout(
1517
+ () => chunkAbortController!.abort(),
1518
+ chunkTimeoutMs,
1519
+ );
1520
+ }
1521
+ }
1522
+
1523
+ function clearChunkTimeout() {
1524
+ if (chunkTimeoutId != null) {
1525
+ clearTimeout(chunkTimeoutId);
1526
+ chunkTimeoutId = undefined;
1527
+ }
1528
+ }
1529
+
1530
+ function clearStepTimeout() {
1531
+ if (stepTimeoutId != null) {
1532
+ clearTimeout(stepTimeoutId);
1533
+ }
1534
+ }
1535
+
1536
+ try {
1537
+ stepFinish = new DelayedPromise<void>();
1538
+
1539
+ const stepInputMessages = [...initialMessages, ...responseMessages];
1540
+
1541
+ const prepareStepResult = await prepareStep?.({
1542
+ model,
1543
+ steps: recordedSteps,
1544
+ stepNumber: recordedSteps.length,
1545
+ messages: stepInputMessages,
1546
+ experimental_context,
1547
+ });
1548
+
1549
+ const stepModel = resolveLanguageModel(
1550
+ prepareStepResult?.model ?? model,
1551
+ );
1552
+ const stepModelInfo = {
1553
+ provider: stepModel.provider,
1554
+ modelId: stepModel.modelId,
1555
+ };
1556
+
1557
+ const promptMessages = await convertToLanguageModelPrompt({
1558
+ prompt: {
1559
+ system: prepareStepResult?.system ?? initialPrompt.system,
1560
+ messages: prepareStepResult?.messages ?? stepInputMessages,
1561
+ },
1562
+ supportedUrls: await stepModel.supportedUrls,
1563
+ download,
1564
+ });
1565
+
1566
+ const stepActiveTools =
1567
+ prepareStepResult?.activeTools ?? activeTools;
1568
+
1569
+ const { toolChoice: stepToolChoice, tools: stepTools } =
1570
+ await prepareToolsAndToolChoice({
1571
+ tools,
1572
+ toolChoice: prepareStepResult?.toolChoice ?? toolChoice,
1573
+ activeTools: stepActiveTools,
1574
+ });
1575
+
1576
+ experimental_context =
1577
+ prepareStepResult?.experimental_context ?? experimental_context;
1578
+
1579
+ const stepMessages =
1580
+ prepareStepResult?.messages ?? stepInputMessages;
1581
+
1582
+ const stepSystem =
1583
+ prepareStepResult?.system ?? initialPrompt.system;
1584
+
1585
+ const stepProviderOptions = mergeObjects(
1586
+ providerOptions,
1587
+ prepareStepResult?.providerOptions,
1588
+ );
1589
+
1590
+ await notify({
1591
+ event: {
1592
+ stepNumber: recordedSteps.length,
1593
+ model: stepModelInfo,
1594
+ system: stepSystem,
1595
+ messages: stepMessages,
1596
+ tools,
1597
+ toolChoice: stepToolChoice,
1598
+ activeTools: stepActiveTools,
1599
+ steps: [...recordedSteps],
1600
+ providerOptions: stepProviderOptions,
1601
+ timeout,
1602
+ headers,
1603
+ stopWhen,
1604
+ output,
1605
+ abortSignal: originalAbortSignal,
1606
+ include,
1607
+ ...callbackTelemetryProps,
1608
+ experimental_context,
1609
+ },
1610
+ callbacks: [
1611
+ onStepStart,
1612
+ globalTelemetry.onStepStart as
1613
+ | undefined
1614
+ | StreamTextOnStepStartCallback<TOOLS, OUTPUT>,
1615
+ ],
1616
+ });
1617
+
1618
+ const {
1619
+ result: { stream, response, request },
1620
+ doStreamSpan,
1621
+ startTimestampMs,
1622
+ } = await retry(() =>
1623
+ recordSpan({
1624
+ name: 'ai.streamText.doStream',
1625
+ attributes: selectTelemetryAttributes({
1626
+ telemetry,
1627
+ attributes: {
1628
+ ...assembleOperationName({
1629
+ operationId: 'ai.streamText.doStream',
1630
+ telemetry,
1631
+ }),
1632
+ ...baseTelemetryAttributes,
1633
+ // model:
1634
+ 'ai.model.provider': stepModel.provider,
1635
+ 'ai.model.id': stepModel.modelId,
1636
+ // prompt:
1637
+ 'ai.prompt.messages': {
1638
+ input: () => stringifyForTelemetry(promptMessages),
1639
+ },
1640
+ 'ai.prompt.tools': {
1641
+ // convert the language model level tools:
1642
+ input: () => stepTools?.map(tool => JSON.stringify(tool)),
1643
+ },
1644
+ 'ai.prompt.toolChoice': {
1645
+ input: () =>
1646
+ stepToolChoice != null
1647
+ ? JSON.stringify(stepToolChoice)
1648
+ : undefined,
1649
+ },
1650
+
1651
+ // standardized gen-ai llm span attributes:
1652
+ 'gen_ai.system': stepModel.provider,
1653
+ 'gen_ai.request.model': stepModel.modelId,
1654
+ 'gen_ai.request.frequency_penalty':
1655
+ callSettings.frequencyPenalty,
1656
+ 'gen_ai.request.max_tokens': callSettings.maxOutputTokens,
1657
+ 'gen_ai.request.presence_penalty':
1658
+ callSettings.presencePenalty,
1659
+ 'gen_ai.request.stop_sequences': callSettings.stopSequences,
1660
+ 'gen_ai.request.temperature': callSettings.temperature,
1661
+ 'gen_ai.request.top_k': callSettings.topK,
1662
+ 'gen_ai.request.top_p': callSettings.topP,
1663
+ },
1664
+ }),
1665
+ tracer,
1666
+ endWhenDone: false,
1667
+ fn: async doStreamSpan => ({
1668
+ startTimestampMs: now(), // get before the call
1669
+ doStreamSpan,
1670
+ result: await stepModel.doStream({
1671
+ ...callSettings,
1672
+ tools: stepTools,
1673
+ toolChoice: stepToolChoice,
1674
+ responseFormat: await output?.responseFormat,
1675
+ prompt: promptMessages,
1676
+ providerOptions: stepProviderOptions,
1677
+ abortSignal,
1678
+ headers,
1679
+ includeRawChunks,
1680
+ }),
1681
+ }),
1682
+ }),
1683
+ );
1684
+
1685
+ const streamWithToolResults = runToolsTransformation({
1686
+ tools,
1687
+ generatorStream: stream,
1688
+ tracer,
1689
+ telemetry,
1690
+ system,
1691
+ messages: stepInputMessages,
1692
+ repairToolCall,
1693
+ abortSignal,
1694
+ experimental_context,
1695
+ generateId,
1696
+ stepNumber: recordedSteps.length,
1697
+ model: stepModelInfo,
1698
+ onToolCallStart: [
1699
+ onToolCallStart,
1700
+ globalTelemetry.onToolCallStart as
1701
+ | undefined
1702
+ | StreamTextOnToolCallStartCallback<TOOLS>,
1703
+ ],
1704
+ onToolCallFinish: [
1705
+ onToolCallFinish,
1706
+ globalTelemetry.onToolCallFinish,
1707
+ ],
1708
+ });
1709
+
1710
+ // Conditionally include request.body based on include settings.
1711
+ // Large payloads (e.g., base64-encoded images) can cause memory issues.
1712
+ const stepRequest: LanguageModelRequestMetadata =
1713
+ (include?.requestBody ?? true)
1714
+ ? (request ?? {})
1715
+ : { ...request, body: undefined };
1716
+ const stepToolCalls: TypedToolCall<TOOLS>[] = [];
1717
+ const stepToolOutputs: ToolOutput<TOOLS>[] = [];
1718
+ let warnings: SharedV3Warning[] | undefined;
1719
+
1720
+ const activeToolCallToolNames: Record<string, string> = {};
1721
+
1722
+ let stepFinishReason: FinishReason = 'other';
1723
+ let stepRawFinishReason: string | undefined = undefined;
1724
+
1725
+ let stepUsage: LanguageModelUsage = createNullLanguageModelUsage();
1726
+ let stepProviderMetadata: ProviderMetadata | undefined;
1727
+ let stepFirstChunk = true;
1728
+ let stepResponse: { id: string; timestamp: Date; modelId: string } =
1729
+ {
1730
+ id: generateId(),
1731
+ timestamp: new Date(),
1732
+ modelId: modelInfo.modelId,
1733
+ };
1734
+
1735
+ // raw text as it comes from the provider. recorded for telemetry.
1736
+ let activeText = '';
1737
+
1738
+ self.addStream(
1739
+ streamWithToolResults.pipeThrough(
1740
+ new TransformStream<
1741
+ SingleRequestTextStreamPart<TOOLS>,
1742
+ TextStreamPart<TOOLS>
1743
+ >({
1744
+ async transform(chunk, controller): Promise<void> {
1745
+ resetChunkTimeout();
1746
+
1747
+ if (chunk.type === 'stream-start') {
1748
+ warnings = chunk.warnings;
1749
+ return; // stream start chunks are sent immediately and do not count as first chunk
1750
+ }
1751
+
1752
+ if (stepFirstChunk) {
1753
+ // Telemetry for first chunk:
1754
+ const msToFirstChunk = now() - startTimestampMs;
1755
+
1756
+ stepFirstChunk = false;
1757
+
1758
+ doStreamSpan.addEvent('ai.stream.firstChunk', {
1759
+ 'ai.response.msToFirstChunk': msToFirstChunk,
1760
+ });
1761
+
1762
+ doStreamSpan.setAttributes({
1763
+ 'ai.response.msToFirstChunk': msToFirstChunk,
1764
+ });
1765
+
1766
+ // Step start:
1767
+ controller.enqueue({
1768
+ type: 'start-step',
1769
+ request: stepRequest,
1770
+ warnings: warnings ?? [],
1771
+ });
1772
+ }
1773
+
1774
+ const chunkType = chunk.type;
1775
+ switch (chunkType) {
1776
+ case 'tool-approval-request':
1777
+ case 'text-start':
1778
+ case 'text-end': {
1779
+ controller.enqueue(chunk);
1780
+ break;
1781
+ }
1782
+
1783
+ case 'text-delta': {
1784
+ if (chunk.delta.length > 0) {
1785
+ controller.enqueue({
1786
+ type: 'text-delta',
1787
+ id: chunk.id,
1788
+ text: chunk.delta,
1789
+ providerMetadata: chunk.providerMetadata,
1790
+ });
1791
+ activeText += chunk.delta;
1792
+ }
1793
+ break;
1794
+ }
1795
+
1796
+ case 'reasoning-start':
1797
+ case 'reasoning-end': {
1798
+ controller.enqueue(chunk);
1799
+ break;
1800
+ }
1801
+
1802
+ case 'reasoning-delta': {
1803
+ controller.enqueue({
1804
+ type: 'reasoning-delta',
1805
+ id: chunk.id,
1806
+ text: chunk.delta,
1807
+ providerMetadata: chunk.providerMetadata,
1808
+ });
1809
+ break;
1810
+ }
1811
+
1812
+ case 'tool-call': {
1813
+ controller.enqueue(chunk);
1814
+ // store tool calls for onFinish callback and toolCalls promise:
1815
+ stepToolCalls.push(chunk);
1816
+ break;
1817
+ }
1818
+
1819
+ case 'tool-result': {
1820
+ controller.enqueue(chunk);
1821
+
1822
+ if (!chunk.preliminary) {
1823
+ stepToolOutputs.push(chunk);
1824
+ }
1825
+
1826
+ break;
1827
+ }
1828
+
1829
+ case 'tool-error': {
1830
+ controller.enqueue(chunk);
1831
+ stepToolOutputs.push(chunk);
1832
+ break;
1833
+ }
1834
+
1835
+ case 'response-metadata': {
1836
+ stepResponse = {
1837
+ id: chunk.id ?? stepResponse.id,
1838
+ timestamp: chunk.timestamp ?? stepResponse.timestamp,
1839
+ modelId: chunk.modelId ?? stepResponse.modelId,
1840
+ };
1841
+ break;
1842
+ }
1843
+
1844
+ case 'finish': {
1845
+ // Note: tool executions might not be finished yet when the finish event is emitted.
1846
+ // store usage and finish reason for promises and onFinish callback:
1847
+ stepUsage = chunk.usage;
1848
+ stepFinishReason = chunk.finishReason;
1849
+ stepRawFinishReason = chunk.rawFinishReason;
1850
+ stepProviderMetadata = chunk.providerMetadata;
1851
+
1852
+ // Telemetry for finish event timing
1853
+ // (since tool executions can take longer and distort calculations)
1854
+ const msToFinish = now() - startTimestampMs;
1855
+ doStreamSpan.addEvent('ai.stream.finish');
1856
+ doStreamSpan.setAttributes({
1857
+ 'ai.response.msToFinish': msToFinish,
1858
+ 'ai.response.avgOutputTokensPerSecond':
1859
+ (1000 * (stepUsage.outputTokens ?? 0)) / msToFinish,
1860
+ });
1861
+
1862
+ break;
1863
+ }
1864
+
1865
+ case 'file': {
1866
+ controller.enqueue(chunk);
1867
+ break;
1868
+ }
1869
+
1870
+ case 'source': {
1871
+ controller.enqueue(chunk);
1872
+ break;
1873
+ }
1874
+
1875
+ case 'tool-input-start': {
1876
+ activeToolCallToolNames[chunk.id] = chunk.toolName;
1877
+
1878
+ const tool = tools?.[chunk.toolName];
1879
+ if (tool?.onInputStart != null) {
1880
+ await tool.onInputStart({
1881
+ toolCallId: chunk.id,
1882
+ messages: stepInputMessages,
1883
+ abortSignal,
1884
+ experimental_context,
1885
+ });
1886
+ }
1887
+
1888
+ controller.enqueue({
1889
+ ...chunk,
1890
+ dynamic: chunk.dynamic ?? tool?.type === 'dynamic',
1891
+ title: tool?.title,
1892
+ });
1893
+ break;
1894
+ }
1895
+
1896
+ case 'tool-input-end': {
1897
+ delete activeToolCallToolNames[chunk.id];
1898
+ controller.enqueue(chunk);
1899
+ break;
1900
+ }
1901
+
1902
+ case 'tool-input-delta': {
1903
+ const toolName = activeToolCallToolNames[chunk.id];
1904
+ const tool = tools?.[toolName];
1905
+
1906
+ if (tool?.onInputDelta != null) {
1907
+ await tool.onInputDelta({
1908
+ inputTextDelta: chunk.delta,
1909
+ toolCallId: chunk.id,
1910
+ messages: stepInputMessages,
1911
+ abortSignal,
1912
+ experimental_context,
1913
+ });
1914
+ }
1915
+
1916
+ controller.enqueue(chunk);
1917
+ break;
1918
+ }
1919
+
1920
+ case 'error': {
1921
+ controller.enqueue(chunk);
1922
+ stepFinishReason = 'error';
1923
+ break;
1924
+ }
1925
+
1926
+ case 'raw': {
1927
+ if (includeRawChunks) {
1928
+ controller.enqueue(chunk);
1929
+ }
1930
+ break;
1931
+ }
1932
+
1933
+ default: {
1934
+ const exhaustiveCheck: never = chunkType;
1935
+ throw new Error(
1936
+ `Unknown chunk type: ${exhaustiveCheck}`,
1937
+ );
1938
+ }
1939
+ }
1940
+ },
1941
+
1942
+ // invoke onFinish callback and resolve toolResults promise when the stream is about to close:
1943
+ async flush(controller) {
1944
+ const stepToolCallsJson =
1945
+ stepToolCalls.length > 0
1946
+ ? JSON.stringify(stepToolCalls)
1947
+ : undefined;
1948
+
1949
+ // record telemetry information first to ensure best effort timing
1950
+ try {
1951
+ doStreamSpan.setAttributes(
1952
+ await selectTelemetryAttributes({
1953
+ telemetry,
1954
+ attributes: {
1955
+ 'ai.response.finishReason': stepFinishReason,
1956
+ 'ai.response.text': {
1957
+ output: () => activeText,
1958
+ },
1959
+ 'ai.response.reasoning': {
1960
+ output: () => {
1961
+ const reasoningParts = recordedContent.filter(
1962
+ (
1963
+ c,
1964
+ ): c is { type: 'reasoning'; text: string } =>
1965
+ c.type === 'reasoning',
1966
+ );
1967
+ return reasoningParts.length > 0
1968
+ ? reasoningParts.map(r => r.text).join('\n')
1969
+ : undefined;
1970
+ },
1971
+ },
1972
+ 'ai.response.toolCalls': {
1973
+ output: () => stepToolCallsJson,
1974
+ },
1975
+ 'ai.response.id': stepResponse.id,
1976
+ 'ai.response.model': stepResponse.modelId,
1977
+ 'ai.response.timestamp':
1978
+ stepResponse.timestamp.toISOString(),
1979
+ 'ai.response.providerMetadata':
1980
+ JSON.stringify(stepProviderMetadata),
1981
+
1982
+ 'ai.usage.inputTokens': stepUsage.inputTokens,
1983
+ 'ai.usage.outputTokens': stepUsage.outputTokens,
1984
+ 'ai.usage.totalTokens': stepUsage.totalTokens,
1985
+ 'ai.usage.reasoningTokens':
1986
+ stepUsage.reasoningTokens,
1987
+ 'ai.usage.cachedInputTokens':
1988
+ stepUsage.cachedInputTokens,
1989
+
1990
+ // standardized gen-ai llm span attributes:
1991
+ 'gen_ai.response.finish_reasons': [
1992
+ stepFinishReason,
1993
+ ],
1994
+ 'gen_ai.response.id': stepResponse.id,
1995
+ 'gen_ai.response.model': stepResponse.modelId,
1996
+ 'gen_ai.usage.input_tokens': stepUsage.inputTokens,
1997
+ 'gen_ai.usage.output_tokens':
1998
+ stepUsage.outputTokens,
1999
+ },
2000
+ }),
2001
+ );
2002
+ } catch (error) {
2003
+ // ignore error setting telemetry attributes
2004
+ } finally {
2005
+ // finish doStreamSpan before other operations for correct timing:
2006
+ doStreamSpan.end();
2007
+ }
2008
+
2009
+ controller.enqueue({
2010
+ type: 'finish-step',
2011
+ finishReason: stepFinishReason,
2012
+ rawFinishReason: stepRawFinishReason,
2013
+ usage: stepUsage,
2014
+ providerMetadata: stepProviderMetadata,
2015
+ response: {
2016
+ ...stepResponse,
2017
+ headers: response?.headers,
2018
+ },
2019
+ });
2020
+
2021
+ const combinedUsage = addLanguageModelUsage(
2022
+ usage,
2023
+ stepUsage,
2024
+ );
2025
+
2026
+ // wait for the step to be fully processed by the event processor
2027
+ // to ensure that the recorded steps are complete:
2028
+ await stepFinish.promise;
2029
+
2030
+ const clientToolCalls = stepToolCalls.filter(
2031
+ toolCall => toolCall.providerExecuted !== true,
2032
+ );
2033
+ const clientToolOutputs = stepToolOutputs.filter(
2034
+ toolOutput => toolOutput.providerExecuted !== true,
2035
+ );
2036
+
2037
+ // Track provider-executed tool calls that support deferred results.
2038
+ // In programmatic tool calling, a server tool (e.g., code_execution) may
2039
+ // trigger a client tool, and the server tool's result is deferred until
2040
+ // the client tool's result is sent back.
2041
+ for (const toolCall of stepToolCalls) {
2042
+ if (toolCall.providerExecuted !== true) continue;
2043
+ const tool = tools?.[toolCall.toolName];
2044
+ if (
2045
+ tool?.type === 'provider' &&
2046
+ tool.supportsDeferredResults
2047
+ ) {
2048
+ // Check if this tool call already has a result in the current step
2049
+ const hasResultInStep = stepToolOutputs.some(
2050
+ output =>
2051
+ (output.type === 'tool-result' ||
2052
+ output.type === 'tool-error') &&
2053
+ output.toolCallId === toolCall.toolCallId,
2054
+ );
2055
+ if (!hasResultInStep) {
2056
+ pendingDeferredToolCalls.set(toolCall.toolCallId, {
2057
+ toolName: toolCall.toolName,
2058
+ });
2059
+ }
2060
+ }
2061
+ }
2062
+
2063
+ // Mark deferred tool calls as resolved when we receive their results
2064
+ for (const output of stepToolOutputs) {
2065
+ if (
2066
+ output.type === 'tool-result' ||
2067
+ output.type === 'tool-error'
2068
+ ) {
2069
+ pendingDeferredToolCalls.delete(output.toolCallId);
2070
+ }
2071
+ }
2072
+
2073
+ // Clear the step and chunk timeouts before the next step is started
2074
+ clearStepTimeout();
2075
+ clearChunkTimeout();
2076
+
2077
+ if (
2078
+ // Continue if:
2079
+ // 1. There are client tool calls that have all been executed, OR
2080
+ // 2. There are pending deferred results from provider-executed tools
2081
+ ((clientToolCalls.length > 0 &&
2082
+ clientToolOutputs.length === clientToolCalls.length) ||
2083
+ pendingDeferredToolCalls.size > 0) &&
2084
+ // continue until a stop condition is met:
2085
+ !(await isStopConditionMet({
2086
+ stopConditions,
2087
+ steps: recordedSteps,
2088
+ }))
2089
+ ) {
2090
+ // append to messages for the next step:
2091
+ responseMessages.push(
2092
+ ...(await toResponseMessages({
2093
+ content:
2094
+ // use transformed content to create the messages for the next step:
2095
+ recordedSteps[recordedSteps.length - 1].content,
2096
+ tools,
2097
+ })),
2098
+ );
2099
+
2100
+ try {
2101
+ await streamStep({
2102
+ currentStep: currentStep + 1,
2103
+ responseMessages,
2104
+ usage: combinedUsage,
2105
+ });
2106
+ } catch (error) {
2107
+ controller.enqueue({
2108
+ type: 'error',
2109
+ error,
2110
+ });
2111
+
2112
+ self.closeStream();
2113
+ }
2114
+ } else {
2115
+ controller.enqueue({
2116
+ type: 'finish',
2117
+ finishReason: stepFinishReason,
2118
+ rawFinishReason: stepRawFinishReason,
2119
+ totalUsage: combinedUsage,
2120
+ });
2121
+
2122
+ self.closeStream(); // close the stitchable stream
2123
+ }
2124
+ },
2125
+ }),
2126
+ ),
2127
+ );
2128
+ } finally {
2129
+ clearStepTimeout();
2130
+ clearChunkTimeout();
2131
+ }
2132
+ }
2133
+
2134
+ // add the initial stream to the stitchable stream
2135
+ await streamStep({
2136
+ currentStep: 0,
2137
+ responseMessages: initialResponseMessages,
2138
+ usage: createNullLanguageModelUsage(),
2139
+ });
2140
+ },
2141
+ }).catch(error => {
2142
+ // add an error stream part and close the streams:
2143
+ self.addStream(
2144
+ new ReadableStream({
2145
+ start(controller) {
2146
+ controller.enqueue({ type: 'error', error });
2147
+ controller.close();
2148
+ },
2149
+ }),
2150
+ );
2151
+ self.closeStream();
2152
+ });
2153
+ }
2154
+
2155
+ get steps() {
2156
+ // when any of the promises are accessed, the stream is consumed
2157
+ // so it resolves without needing to consume the stream separately
2158
+ this.consumeStream();
2159
+
2160
+ return this._steps.promise;
2161
+ }
2162
+
2163
+ private get finalStep() {
2164
+ return this.steps.then(steps => steps[steps.length - 1]);
2165
+ }
2166
+
2167
+ get content() {
2168
+ return this.finalStep.then(step => step.content);
2169
+ }
2170
+
2171
+ get warnings() {
2172
+ return this.finalStep.then(step => step.warnings);
2173
+ }
2174
+
2175
+ get providerMetadata() {
2176
+ return this.finalStep.then(step => step.providerMetadata);
2177
+ }
2178
+
2179
+ get text() {
2180
+ return this.finalStep.then(step => step.text);
2181
+ }
2182
+
2183
+ get reasoningText() {
2184
+ return this.finalStep.then(step => step.reasoningText);
2185
+ }
2186
+
2187
+ get reasoning() {
2188
+ return this.finalStep.then(step => step.reasoning);
2189
+ }
2190
+
2191
+ get sources() {
2192
+ return this.finalStep.then(step => step.sources);
2193
+ }
2194
+
2195
+ get files() {
2196
+ return this.finalStep.then(step => step.files);
2197
+ }
2198
+
2199
+ get toolCalls() {
2200
+ return this.finalStep.then(step => step.toolCalls);
2201
+ }
2202
+
2203
+ get staticToolCalls() {
2204
+ return this.finalStep.then(step => step.staticToolCalls);
2205
+ }
2206
+
2207
+ get dynamicToolCalls() {
2208
+ return this.finalStep.then(step => step.dynamicToolCalls);
2209
+ }
2210
+
2211
+ get toolResults() {
2212
+ return this.finalStep.then(step => step.toolResults);
2213
+ }
2214
+
2215
+ get staticToolResults() {
2216
+ return this.finalStep.then(step => step.staticToolResults);
2217
+ }
2218
+
2219
+ get dynamicToolResults() {
2220
+ return this.finalStep.then(step => step.dynamicToolResults);
2221
+ }
2222
+
2223
+ get usage() {
2224
+ return this.finalStep.then(step => step.usage);
2225
+ }
2226
+
2227
+ get request() {
2228
+ return this.finalStep.then(step => step.request);
2229
+ }
2230
+
2231
+ get response() {
2232
+ return this.finalStep.then(step => step.response);
2233
+ }
2234
+
2235
+ get totalUsage() {
2236
+ // when any of the promises are accessed, the stream is consumed
2237
+ // so it resolves without needing to consume the stream separately
2238
+ this.consumeStream();
2239
+
2240
+ return this._totalUsage.promise;
2241
+ }
2242
+
2243
+ get finishReason() {
2244
+ // when any of the promises are accessed, the stream is consumed
2245
+ // so it resolves without needing to consume the stream separately
2246
+ this.consumeStream();
2247
+
2248
+ return this._finishReason.promise;
2249
+ }
2250
+
2251
+ get rawFinishReason() {
2252
+ // when any of the promises are accessed, the stream is consumed
2253
+ // so it resolves without needing to consume the stream separately
2254
+ this.consumeStream();
2255
+
2256
+ return this._rawFinishReason.promise;
2257
+ }
2258
+
2259
+ /**
2260
+ * Split out a new stream from the original stream.
2261
+ * The original stream is replaced to allow for further splitting,
2262
+ * since we do not know how many times the stream will be split.
2263
+ *
2264
+ * Note: this leads to buffering the stream content on the server.
2265
+ * However, the LLM results are expected to be small enough to not cause issues.
2266
+ */
2267
+ private teeStream() {
2268
+ const [stream1, stream2] = this.baseStream.tee();
2269
+ this.baseStream = stream2;
2270
+ return stream1;
2271
+ }
2272
+
2273
+ get textStream(): AsyncIterableStream<string> {
2274
+ return createAsyncIterableStream(
2275
+ this.teeStream().pipeThrough(
2276
+ new TransformStream<
2277
+ EnrichedStreamPart<TOOLS, InferPartialOutput<OUTPUT>>,
2278
+ string
2279
+ >({
2280
+ transform({ part }, controller) {
2281
+ if (part.type === 'text-delta') {
2282
+ controller.enqueue(part.text);
2283
+ }
2284
+ },
2285
+ }),
2286
+ ),
2287
+ );
2288
+ }
2289
+
2290
+ get fullStream(): AsyncIterableStream<TextStreamPart<TOOLS>> {
2291
+ return createAsyncIterableStream(
2292
+ this.teeStream().pipeThrough(
2293
+ new TransformStream<
2294
+ EnrichedStreamPart<TOOLS, InferPartialOutput<OUTPUT>>,
2295
+ TextStreamPart<TOOLS>
2296
+ >({
2297
+ transform({ part }, controller) {
2298
+ controller.enqueue(part);
2299
+ },
2300
+ }),
2301
+ ),
2302
+ );
2303
+ }
2304
+
2305
+ async consumeStream(options?: ConsumeStreamOptions): Promise<void> {
2306
+ try {
2307
+ await consumeStream({
2308
+ stream: this.fullStream,
2309
+ onError: options?.onError,
2310
+ });
2311
+ } catch (error) {
2312
+ options?.onError?.(error);
2313
+ }
2314
+ }
2315
+
2316
+ get experimental_partialOutputStream(): AsyncIterableStream<
2317
+ InferPartialOutput<OUTPUT>
2318
+ > {
2319
+ return this.partialOutputStream;
2320
+ }
2321
+
2322
+ get partialOutputStream(): AsyncIterableStream<InferPartialOutput<OUTPUT>> {
2323
+ return createAsyncIterableStream(
2324
+ this.teeStream().pipeThrough(
2325
+ new TransformStream<
2326
+ EnrichedStreamPart<TOOLS, InferPartialOutput<OUTPUT>>,
2327
+ InferPartialOutput<OUTPUT>
2328
+ >({
2329
+ transform({ partialOutput }, controller) {
2330
+ if (partialOutput != null) {
2331
+ controller.enqueue(partialOutput);
2332
+ }
2333
+ },
2334
+ }),
2335
+ ),
2336
+ );
2337
+ }
2338
+
2339
+ get elementStream(): AsyncIterableStream<InferElementOutput<OUTPUT>> {
2340
+ const transform = this.outputSpecification?.createElementStreamTransform();
2341
+
2342
+ if (transform == null) {
2343
+ throw new UnsupportedFunctionalityError({
2344
+ functionality: `element streams in ${this.outputSpecification?.name ?? 'text'} mode`,
2345
+ });
2346
+ }
2347
+
2348
+ return createAsyncIterableStream(this.teeStream().pipeThrough(transform));
2349
+ }
2350
+
2351
+ get output(): Promise<InferCompleteOutput<OUTPUT>> {
2352
+ return this.finalStep.then(step => {
2353
+ const output = this.outputSpecification ?? text();
2354
+ return output.parseCompleteOutput(
2355
+ { text: step.text },
2356
+ {
2357
+ response: step.response,
2358
+ usage: step.usage,
2359
+ finishReason: step.finishReason,
2360
+ },
2361
+ );
2362
+ });
2363
+ }
2364
+
2365
+ toUIMessageStream<UI_MESSAGE extends UIMessage>({
2366
+ originalMessages,
2367
+ generateMessageId,
2368
+ onFinish,
2369
+ messageMetadata,
2370
+ sendReasoning = true,
2371
+ sendSources = false,
2372
+ sendStart = true,
2373
+ sendFinish = true,
2374
+ onError = getErrorMessage,
2375
+ }: UIMessageStreamOptions<UI_MESSAGE> = {}): AsyncIterableStream<
2376
+ InferUIMessageChunk<UI_MESSAGE>
2377
+ > {
2378
+ const responseMessageId =
2379
+ generateMessageId != null
2380
+ ? getResponseUIMessageId({
2381
+ originalMessages,
2382
+ responseMessageId: generateMessageId,
2383
+ })
2384
+ : undefined;
2385
+
2386
+ // TODO simplify once dynamic is no longer needed for invalid tool inputs
2387
+ const isDynamic = (part: { toolName: string; dynamic?: boolean }) => {
2388
+ const tool = this.tools?.[part.toolName];
2389
+
2390
+ // provider-executed, dynamic tools are not listed in the tools object
2391
+ if (tool == null) {
2392
+ return part.dynamic;
2393
+ }
2394
+
2395
+ return tool?.type === 'dynamic' ? true : undefined;
2396
+ };
2397
+
2398
+ const baseStream = this.fullStream.pipeThrough(
2399
+ new TransformStream<
2400
+ TextStreamPart<TOOLS>,
2401
+ UIMessageChunk<
2402
+ InferUIMessageMetadata<UI_MESSAGE>,
2403
+ InferUIMessageData<UI_MESSAGE>
2404
+ >
2405
+ >({
2406
+ transform: async (part, controller) => {
2407
+ const messageMetadataValue = messageMetadata?.({ part });
2408
+
2409
+ const partType = part.type;
2410
+ switch (partType) {
2411
+ case 'text-start': {
2412
+ controller.enqueue({
2413
+ type: 'text-start',
2414
+ id: part.id,
2415
+ ...(part.providerMetadata != null
2416
+ ? { providerMetadata: part.providerMetadata }
2417
+ : {}),
2418
+ });
2419
+ break;
2420
+ }
2421
+
2422
+ case 'text-delta': {
2423
+ controller.enqueue({
2424
+ type: 'text-delta',
2425
+ id: part.id,
2426
+ delta: part.text,
2427
+ ...(part.providerMetadata != null
2428
+ ? { providerMetadata: part.providerMetadata }
2429
+ : {}),
2430
+ });
2431
+ break;
2432
+ }
2433
+
2434
+ case 'text-end': {
2435
+ controller.enqueue({
2436
+ type: 'text-end',
2437
+ id: part.id,
2438
+ ...(part.providerMetadata != null
2439
+ ? { providerMetadata: part.providerMetadata }
2440
+ : {}),
2441
+ });
2442
+ break;
2443
+ }
2444
+
2445
+ case 'reasoning-start': {
2446
+ controller.enqueue({
2447
+ type: 'reasoning-start',
2448
+ id: part.id,
2449
+ ...(part.providerMetadata != null
2450
+ ? { providerMetadata: part.providerMetadata }
2451
+ : {}),
2452
+ });
2453
+ break;
2454
+ }
2455
+
2456
+ case 'reasoning-delta': {
2457
+ if (sendReasoning) {
2458
+ controller.enqueue({
2459
+ type: 'reasoning-delta',
2460
+ id: part.id,
2461
+ delta: part.text,
2462
+ ...(part.providerMetadata != null
2463
+ ? { providerMetadata: part.providerMetadata }
2464
+ : {}),
2465
+ });
2466
+ }
2467
+ break;
2468
+ }
2469
+
2470
+ case 'reasoning-end': {
2471
+ controller.enqueue({
2472
+ type: 'reasoning-end',
2473
+ id: part.id,
2474
+ ...(part.providerMetadata != null
2475
+ ? { providerMetadata: part.providerMetadata }
2476
+ : {}),
2477
+ });
2478
+ break;
2479
+ }
2480
+
2481
+ case 'file': {
2482
+ controller.enqueue({
2483
+ type: 'file',
2484
+ mediaType: part.file.mediaType,
2485
+ url: `data:${part.file.mediaType};base64,${part.file.base64}`,
2486
+ ...(part.providerMetadata != null
2487
+ ? { providerMetadata: part.providerMetadata }
2488
+ : {}),
2489
+ });
2490
+ break;
2491
+ }
2492
+
2493
+ case 'source': {
2494
+ if (sendSources && part.sourceType === 'url') {
2495
+ controller.enqueue({
2496
+ type: 'source-url',
2497
+ sourceId: part.id,
2498
+ url: part.url,
2499
+ title: part.title,
2500
+ ...(part.providerMetadata != null
2501
+ ? { providerMetadata: part.providerMetadata }
2502
+ : {}),
2503
+ });
2504
+ }
2505
+
2506
+ if (sendSources && part.sourceType === 'document') {
2507
+ controller.enqueue({
2508
+ type: 'source-document',
2509
+ sourceId: part.id,
2510
+ mediaType: part.mediaType,
2511
+ title: part.title,
2512
+ filename: part.filename,
2513
+ ...(part.providerMetadata != null
2514
+ ? { providerMetadata: part.providerMetadata }
2515
+ : {}),
2516
+ });
2517
+ }
2518
+ break;
2519
+ }
2520
+
2521
+ case 'tool-input-start': {
2522
+ const dynamic = isDynamic(part);
2523
+
2524
+ controller.enqueue({
2525
+ type: 'tool-input-start',
2526
+ toolCallId: part.id,
2527
+ toolName: part.toolName,
2528
+ ...(part.providerExecuted != null
2529
+ ? { providerExecuted: part.providerExecuted }
2530
+ : {}),
2531
+ ...(part.providerMetadata != null
2532
+ ? { providerMetadata: part.providerMetadata }
2533
+ : {}),
2534
+ ...(dynamic != null ? { dynamic } : {}),
2535
+ ...(part.title != null ? { title: part.title } : {}),
2536
+ });
2537
+ break;
2538
+ }
2539
+
2540
+ case 'tool-input-delta': {
2541
+ controller.enqueue({
2542
+ type: 'tool-input-delta',
2543
+ toolCallId: part.id,
2544
+ inputTextDelta: part.delta,
2545
+ });
2546
+ break;
2547
+ }
2548
+
2549
+ case 'tool-call': {
2550
+ const dynamic = isDynamic(part);
2551
+
2552
+ if (part.invalid) {
2553
+ controller.enqueue({
2554
+ type: 'tool-input-error',
2555
+ toolCallId: part.toolCallId,
2556
+ toolName: part.toolName,
2557
+ input: part.input,
2558
+ ...(part.providerExecuted != null
2559
+ ? { providerExecuted: part.providerExecuted }
2560
+ : {}),
2561
+ ...(part.providerMetadata != null
2562
+ ? { providerMetadata: part.providerMetadata }
2563
+ : {}),
2564
+ ...(dynamic != null ? { dynamic } : {}),
2565
+ errorText: onError(part.error),
2566
+ ...(part.title != null ? { title: part.title } : {}),
2567
+ });
2568
+ } else {
2569
+ controller.enqueue({
2570
+ type: 'tool-input-available',
2571
+ toolCallId: part.toolCallId,
2572
+ toolName: part.toolName,
2573
+ input: part.input,
2574
+ ...(part.providerExecuted != null
2575
+ ? { providerExecuted: part.providerExecuted }
2576
+ : {}),
2577
+ ...(part.providerMetadata != null
2578
+ ? { providerMetadata: part.providerMetadata }
2579
+ : {}),
2580
+ ...(dynamic != null ? { dynamic } : {}),
2581
+ ...(part.title != null ? { title: part.title } : {}),
2582
+ });
2583
+ }
2584
+
2585
+ break;
2586
+ }
2587
+
2588
+ case 'tool-approval-request': {
2589
+ controller.enqueue({
2590
+ type: 'tool-approval-request',
2591
+ approvalId: part.approvalId,
2592
+ toolCallId: part.toolCall.toolCallId,
2593
+ });
2594
+ break;
2595
+ }
2596
+
2597
+ case 'tool-result': {
2598
+ const dynamic = isDynamic(part);
2599
+
2600
+ controller.enqueue({
2601
+ type: 'tool-output-available',
2602
+ toolCallId: part.toolCallId,
2603
+ output: part.output,
2604
+ ...(part.providerExecuted != null
2605
+ ? { providerExecuted: part.providerExecuted }
2606
+ : {}),
2607
+ ...(part.preliminary != null
2608
+ ? { preliminary: part.preliminary }
2609
+ : {}),
2610
+ ...(dynamic != null ? { dynamic } : {}),
2611
+ });
2612
+ break;
2613
+ }
2614
+
2615
+ case 'tool-error': {
2616
+ const dynamic = isDynamic(part);
2617
+
2618
+ controller.enqueue({
2619
+ type: 'tool-output-error',
2620
+ toolCallId: part.toolCallId,
2621
+ errorText: onError(part.error),
2622
+ ...(part.providerExecuted != null
2623
+ ? { providerExecuted: part.providerExecuted }
2624
+ : {}),
2625
+ ...(dynamic != null ? { dynamic } : {}),
2626
+ });
2627
+ break;
2628
+ }
2629
+
2630
+ case 'tool-output-denied': {
2631
+ controller.enqueue({
2632
+ type: 'tool-output-denied',
2633
+ toolCallId: part.toolCallId,
2634
+ });
2635
+ break;
2636
+ }
2637
+
2638
+ case 'error': {
2639
+ controller.enqueue({
2640
+ type: 'error',
2641
+ errorText: onError(part.error),
2642
+ });
2643
+ break;
2644
+ }
2645
+
2646
+ case 'start-step': {
2647
+ controller.enqueue({ type: 'start-step' });
2648
+ break;
2649
+ }
2650
+
2651
+ case 'finish-step': {
2652
+ controller.enqueue({ type: 'finish-step' });
2653
+ break;
2654
+ }
2655
+
2656
+ case 'start': {
2657
+ if (sendStart) {
2658
+ controller.enqueue({
2659
+ type: 'start',
2660
+ ...(messageMetadataValue != null
2661
+ ? { messageMetadata: messageMetadataValue }
2662
+ : {}),
2663
+ ...(responseMessageId != null
2664
+ ? { messageId: responseMessageId }
2665
+ : {}),
2666
+ });
2667
+ }
2668
+ break;
2669
+ }
2670
+
2671
+ case 'finish': {
2672
+ if (sendFinish) {
2673
+ controller.enqueue({
2674
+ type: 'finish',
2675
+ finishReason: part.finishReason,
2676
+ ...(messageMetadataValue != null
2677
+ ? { messageMetadata: messageMetadataValue }
2678
+ : {}),
2679
+ });
2680
+ }
2681
+ break;
2682
+ }
2683
+
2684
+ case 'abort': {
2685
+ controller.enqueue(part);
2686
+ break;
2687
+ }
2688
+
2689
+ case 'tool-input-end': {
2690
+ break;
2691
+ }
2692
+
2693
+ case 'raw': {
2694
+ // Raw chunks are not included in UI message streams
2695
+ // as they contain provider-specific data for developer use
2696
+ break;
2697
+ }
2698
+
2699
+ default: {
2700
+ const exhaustiveCheck: never = partType;
2701
+ throw new Error(`Unknown chunk type: ${exhaustiveCheck}`);
2702
+ }
2703
+ }
2704
+
2705
+ // start and finish events already have metadata
2706
+ // so we only need to send metadata for other parts
2707
+ if (
2708
+ messageMetadataValue != null &&
2709
+ partType !== 'start' &&
2710
+ partType !== 'finish'
2711
+ ) {
2712
+ controller.enqueue({
2713
+ type: 'message-metadata',
2714
+ messageMetadata: messageMetadataValue,
2715
+ });
2716
+ }
2717
+ },
2718
+ }),
2719
+ );
2720
+
2721
+ return createAsyncIterableStream(
2722
+ handleUIMessageStreamFinish<UI_MESSAGE>({
2723
+ stream: baseStream,
2724
+ messageId: responseMessageId ?? generateMessageId?.(),
2725
+ originalMessages,
2726
+ onFinish,
2727
+ onError,
2728
+ }),
2729
+ );
2730
+ }
2731
+
2732
+ pipeUIMessageStreamToResponse<UI_MESSAGE extends UIMessage>(
2733
+ response: ServerResponse,
2734
+ {
2735
+ originalMessages,
2736
+ generateMessageId,
2737
+ onFinish,
2738
+ messageMetadata,
2739
+ sendReasoning,
2740
+ sendSources,
2741
+ sendFinish,
2742
+ sendStart,
2743
+ onError,
2744
+ ...init
2745
+ }: UIMessageStreamResponseInit & UIMessageStreamOptions<UI_MESSAGE> = {},
2746
+ ) {
2747
+ pipeUIMessageStreamToResponse({
2748
+ response,
2749
+ stream: this.toUIMessageStream({
2750
+ originalMessages,
2751
+ generateMessageId,
2752
+ onFinish,
2753
+ messageMetadata,
2754
+ sendReasoning,
2755
+ sendSources,
2756
+ sendFinish,
2757
+ sendStart,
2758
+ onError,
2759
+ }),
2760
+ ...init,
2761
+ });
2762
+ }
2763
+
2764
+ pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit) {
2765
+ pipeTextStreamToResponse({
2766
+ response,
2767
+ textStream: this.textStream,
2768
+ ...init,
2769
+ });
2770
+ }
2771
+
2772
+ toUIMessageStreamResponse<UI_MESSAGE extends UIMessage>({
2773
+ originalMessages,
2774
+ generateMessageId,
2775
+ onFinish,
2776
+ messageMetadata,
2777
+ sendReasoning,
2778
+ sendSources,
2779
+ sendFinish,
2780
+ sendStart,
2781
+ onError,
2782
+ ...init
2783
+ }: UIMessageStreamResponseInit &
2784
+ UIMessageStreamOptions<UI_MESSAGE> = {}): Response {
2785
+ return createUIMessageStreamResponse({
2786
+ stream: this.toUIMessageStream({
2787
+ originalMessages,
2788
+ generateMessageId,
2789
+ onFinish,
2790
+ messageMetadata,
2791
+ sendReasoning,
2792
+ sendSources,
2793
+ sendFinish,
2794
+ sendStart,
2795
+ onError,
2796
+ }),
2797
+ ...init,
2798
+ });
2799
+ }
2800
+
2801
+ toTextStreamResponse(init?: ResponseInit): Response {
2802
+ return createTextStreamResponse({
2803
+ textStream: this.textStream,
2804
+ ...init,
2805
+ });
2806
+ }
2807
+ }