@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,239 @@
1
+ import { JSONObject, RerankingModelV3CallOptions } from '@ai-sdk/provider';
2
+ import { ProviderOptions } from '@ai-sdk/provider-utils';
3
+ import { prepareRetries } from '../../src/util/prepare-retries';
4
+ import { assembleOperationName } from '../telemetry/assemble-operation-name';
5
+ import { getBaseTelemetryAttributes } from '../telemetry/get-base-telemetry-attributes';
6
+ import { getTracer } from '../telemetry/get-tracer';
7
+ import { recordSpan } from '../telemetry/record-span';
8
+ import { selectTelemetryAttributes } from '../telemetry/select-telemetry-attributes';
9
+ import { TelemetrySettings } from '../telemetry/telemetry-settings';
10
+ import { RerankingModel } from '../types';
11
+ import { RerankResult } from './rerank-result';
12
+ import { logWarnings } from '../logger/log-warnings';
13
+
14
+ /**
15
+ * Rerank documents using a reranking model. The type of the value is defined by the reranking model.
16
+ *
17
+ * @param model - The reranking model to use.
18
+ * @param documents - The documents that should be reranked.
19
+ * @param query - The query to rerank the documents against.
20
+ * @param topN - Number of top documents to return.
21
+ *
22
+ * @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
23
+ * @param abortSignal - An optional abort signal that can be used to cancel the call.
24
+ * @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
25
+ * @param providerOptions - Additional provider-specific options.
26
+ * @param experimental_telemetry - Optional telemetry configuration (experimental).
27
+ *
28
+ * @returns A result object that contains the reranked documents, the reranked indices, and additional information.
29
+ */
30
+ export async function rerank<VALUE extends JSONObject | string>({
31
+ model,
32
+ documents,
33
+ query,
34
+ topN,
35
+ maxRetries: maxRetriesArg,
36
+ abortSignal,
37
+ headers,
38
+ providerOptions,
39
+ experimental_telemetry: telemetry,
40
+ }: {
41
+ /**
42
+ * The reranking model to use.
43
+ */
44
+ model: RerankingModel;
45
+
46
+ /**
47
+ * The documents that should be reranked.
48
+ */
49
+ documents: Array<VALUE>;
50
+
51
+ /**
52
+ * The query to rerank the documents against.
53
+ */
54
+ query: string;
55
+
56
+ /**
57
+ * Number of top documents to return.
58
+ */
59
+ topN?: number;
60
+
61
+ /**
62
+ * Maximum number of retries per reranking model call. Set to 0 to disable retries.
63
+ *
64
+ * @default 2
65
+ */
66
+ maxRetries?: number;
67
+
68
+ /**
69
+ * Abort signal.
70
+ */
71
+ abortSignal?: AbortSignal;
72
+
73
+ /**
74
+ * Additional headers to include in the request.
75
+ * Only applicable for HTTP-based providers.
76
+ */
77
+ headers?: Record<string, string>;
78
+
79
+ /**
80
+ * Optional telemetry configuration (experimental).
81
+ */
82
+ experimental_telemetry?: TelemetrySettings;
83
+
84
+ /**
85
+ * Additional provider-specific options. They are passed through
86
+ * to the provider from the AI SDK and enable provider-specific
87
+ * functionality that can be fully encapsulated in the provider.
88
+ */
89
+ providerOptions?: ProviderOptions;
90
+ }): Promise<RerankResult<VALUE>> {
91
+ if (documents.length === 0) {
92
+ return new DefaultRerankResult({
93
+ originalDocuments: [],
94
+ ranking: [],
95
+ providerMetadata: undefined,
96
+ response: {
97
+ timestamp: new Date(),
98
+ modelId: model.modelId,
99
+ },
100
+ });
101
+ }
102
+
103
+ const { maxRetries, retry } = prepareRetries({
104
+ maxRetries: maxRetriesArg,
105
+ abortSignal,
106
+ });
107
+
108
+ // detect the type of the documents:
109
+ const documentsToSend: RerankingModelV3CallOptions['documents'] =
110
+ typeof documents[0] === 'string'
111
+ ? { type: 'text', values: documents as string[] }
112
+ : { type: 'object', values: documents as JSONObject[] };
113
+
114
+ const baseTelemetryAttributes = getBaseTelemetryAttributes({
115
+ model,
116
+ telemetry,
117
+ headers,
118
+ settings: { maxRetries },
119
+ });
120
+
121
+ const tracer = getTracer(telemetry);
122
+
123
+ return recordSpan({
124
+ name: 'ai.rerank',
125
+ attributes: selectTelemetryAttributes({
126
+ telemetry,
127
+ attributes: {
128
+ ...assembleOperationName({ operationId: 'ai.rerank', telemetry }),
129
+ ...baseTelemetryAttributes,
130
+ 'ai.documents': {
131
+ input: () => documents.map(document => JSON.stringify(document)),
132
+ },
133
+ },
134
+ }),
135
+ tracer,
136
+ fn: async () => {
137
+ const { ranking, response, providerMetadata, warnings } = await retry(
138
+ () =>
139
+ recordSpan({
140
+ name: 'ai.rerank.doRerank',
141
+ attributes: selectTelemetryAttributes({
142
+ telemetry,
143
+ attributes: {
144
+ ...assembleOperationName({
145
+ operationId: 'ai.rerank.doRerank',
146
+ telemetry,
147
+ }),
148
+ ...baseTelemetryAttributes,
149
+ // specific settings that only make sense on the outer level:
150
+ 'ai.documents': {
151
+ input: () =>
152
+ documents.map(document => JSON.stringify(document)),
153
+ },
154
+ },
155
+ }),
156
+ tracer,
157
+ fn: async doRerankSpan => {
158
+ const modelResponse = await model.doRerank({
159
+ documents: documentsToSend,
160
+ query,
161
+ topN,
162
+ providerOptions,
163
+ abortSignal,
164
+ headers,
165
+ });
166
+
167
+ const ranking = modelResponse.ranking;
168
+
169
+ doRerankSpan.setAttributes(
170
+ await selectTelemetryAttributes({
171
+ telemetry,
172
+ attributes: {
173
+ 'ai.ranking.type': documentsToSend.type,
174
+ 'ai.ranking': {
175
+ output: () =>
176
+ ranking.map(ranking => JSON.stringify(ranking)),
177
+ },
178
+ },
179
+ }),
180
+ );
181
+
182
+ return {
183
+ ranking,
184
+ providerMetadata: modelResponse.providerMetadata,
185
+ response: modelResponse.response,
186
+ warnings: modelResponse.warnings,
187
+ };
188
+ },
189
+ }),
190
+ );
191
+
192
+ logWarnings({
193
+ warnings: warnings ?? [],
194
+ provider: model.provider,
195
+ model: model.modelId,
196
+ });
197
+
198
+ return new DefaultRerankResult({
199
+ originalDocuments: documents,
200
+ ranking: ranking.map(ranking => ({
201
+ originalIndex: ranking.index,
202
+ score: ranking.relevanceScore,
203
+ document: documents[ranking.index],
204
+ })),
205
+ providerMetadata,
206
+ response: {
207
+ id: response?.id,
208
+ timestamp: response?.timestamp ?? new Date(),
209
+ modelId: response?.modelId ?? model.modelId,
210
+ headers: response?.headers,
211
+ body: response?.body,
212
+ },
213
+ });
214
+ },
215
+ });
216
+ }
217
+
218
+ class DefaultRerankResult<VALUE> implements RerankResult<VALUE> {
219
+ readonly originalDocuments: RerankResult<VALUE>['originalDocuments'];
220
+ readonly ranking: RerankResult<VALUE>['ranking'];
221
+ readonly response: RerankResult<VALUE>['response'];
222
+ readonly providerMetadata: RerankResult<VALUE>['providerMetadata'];
223
+
224
+ constructor(options: {
225
+ originalDocuments: RerankResult<VALUE>['originalDocuments'];
226
+ ranking: RerankResult<VALUE>['ranking'];
227
+ providerMetadata?: RerankResult<VALUE>['providerMetadata'];
228
+ response: RerankResult<VALUE>['response'];
229
+ }) {
230
+ this.originalDocuments = options.originalDocuments;
231
+ this.ranking = options.ranking;
232
+ this.response = options.response;
233
+ this.providerMetadata = options.providerMetadata;
234
+ }
235
+
236
+ get rerankedDocuments(): RerankResult<VALUE>['rerankedDocuments'] {
237
+ return this.ranking.map(ranking => ranking.document);
238
+ }
239
+ }
@@ -0,0 +1,21 @@
1
+ import { TelemetrySettings } from './telemetry-settings';
2
+
3
+ export function assembleOperationName({
4
+ operationId,
5
+ telemetry,
6
+ }: {
7
+ operationId: string;
8
+ telemetry?: TelemetrySettings;
9
+ }) {
10
+ return {
11
+ // standardized operation and resource name:
12
+ 'operation.name': `${operationId}${
13
+ telemetry?.functionId != null ? ` ${telemetry.functionId}` : ''
14
+ }`,
15
+ 'resource.name': telemetry?.functionId,
16
+
17
+ // detailed, AI SDK specific data:
18
+ 'ai.operationId': operationId,
19
+ 'ai.telemetry.functionId': telemetry?.functionId,
20
+ };
21
+ }
@@ -0,0 +1,53 @@
1
+ import { Attributes, AttributeValue } from '@opentelemetry/api';
2
+ import { CallSettings, getTotalTimeoutMs } from '../prompt/call-settings';
3
+ import { TelemetrySettings } from './telemetry-settings';
4
+
5
+ export function getBaseTelemetryAttributes({
6
+ model,
7
+ settings,
8
+ telemetry,
9
+ headers,
10
+ }: {
11
+ model: { modelId: string; provider: string };
12
+ settings: Omit<CallSettings, 'abortSignal' | 'headers' | 'temperature'>;
13
+ telemetry: TelemetrySettings | undefined;
14
+ headers: Record<string, string | undefined> | undefined;
15
+ }): Attributes {
16
+ return {
17
+ 'ai.model.provider': model.provider,
18
+ 'ai.model.id': model.modelId,
19
+
20
+ // settings:
21
+ ...Object.entries(settings).reduce((attributes, [key, value]) => {
22
+ // Handle timeout specially since it can be a number or object
23
+ if (key === 'timeout') {
24
+ const totalTimeoutMs = getTotalTimeoutMs(
25
+ value as Parameters<typeof getTotalTimeoutMs>[0],
26
+ );
27
+ if (totalTimeoutMs != null) {
28
+ attributes[`ai.settings.${key}`] = totalTimeoutMs;
29
+ }
30
+ } else {
31
+ attributes[`ai.settings.${key}`] = value as AttributeValue;
32
+ }
33
+ return attributes;
34
+ }, {} as Attributes),
35
+
36
+ // add metadata as attributes:
37
+ ...Object.entries(telemetry?.metadata ?? {}).reduce(
38
+ (attributes, [key, value]) => {
39
+ attributes[`ai.telemetry.metadata.${key}`] = value;
40
+ return attributes;
41
+ },
42
+ {} as Attributes,
43
+ ),
44
+
45
+ // request headers
46
+ ...Object.entries(headers ?? {}).reduce((attributes, [key, value]) => {
47
+ if (value !== undefined) {
48
+ attributes[`ai.request.headers.${key}`] = value;
49
+ }
50
+ return attributes;
51
+ }, {} as Attributes),
52
+ };
53
+ }
@@ -0,0 +1,85 @@
1
+ import type { Output } from '../generate-text/output';
2
+ import type { ToolSet } from '../generate-text/tool-set';
3
+ import { asArray } from '../util/as-array';
4
+ import type { TelemetryIntegration } from './telemetry-integration';
5
+ import { getGlobalTelemetryIntegrations } from './telemetry-integration-registry';
6
+
7
+ /**
8
+ * Wraps a telemetry integration with bound methods.
9
+ * Use this when creating class-based integrations to ensure methods
10
+ * work correctly when passed as callbacks.
11
+ */
12
+ export function bindTelemetryIntegration(
13
+ integration: TelemetryIntegration,
14
+ ): TelemetryIntegration {
15
+ return {
16
+ onStart: integration.onStart?.bind(integration),
17
+ onStepStart: integration.onStepStart?.bind(integration),
18
+ onToolCallStart: integration.onToolCallStart?.bind(integration),
19
+ onToolCallFinish: integration.onToolCallFinish?.bind(integration),
20
+ onStepFinish: integration.onStepFinish?.bind(integration),
21
+ onFinish: integration.onFinish?.bind(integration),
22
+ };
23
+ }
24
+
25
+ /**
26
+ * Creates a factory that merges globally registered integrations
27
+ * (via `registerTelemetryIntegration`) with per-call integrations
28
+ * into a single composite integration.
29
+ *
30
+ * Returns a factory function that accepts local integrations and
31
+ * returns the merged TelemetryIntegration.
32
+ */
33
+ export function getGlobalTelemetryIntegration<
34
+ TOOLS extends ToolSet = ToolSet,
35
+ OUTPUT extends Output = Output,
36
+ >(): (
37
+ integrations: TelemetryIntegration | Array<TelemetryIntegration> | undefined,
38
+ ) => TelemetryIntegration {
39
+ const globalIntegrations = getGlobalTelemetryIntegrations();
40
+
41
+ return (
42
+ integrations:
43
+ | TelemetryIntegration
44
+ | Array<TelemetryIntegration>
45
+ | undefined,
46
+ ): TelemetryIntegration => {
47
+ const localIntegrations = asArray(integrations);
48
+ const allIntegrations = [...globalIntegrations, ...localIntegrations];
49
+
50
+ function createTelemetryComposite<EVENT>(
51
+ getListenerFromIntegration: (
52
+ integration: TelemetryIntegration,
53
+ ) => ((event: EVENT) => PromiseLike<void> | void) | undefined,
54
+ ): ((event: EVENT) => Promise<void>) | undefined {
55
+ const listeners = allIntegrations
56
+ .map(getListenerFromIntegration)
57
+ .filter(Boolean) as Array<(event: EVENT) => PromiseLike<void> | void>;
58
+
59
+ return async (event: EVENT) => {
60
+ for (const listener of listeners) {
61
+ try {
62
+ await listener(event);
63
+ } catch (_ignored) {}
64
+ }
65
+ };
66
+ }
67
+
68
+ return {
69
+ onStart: createTelemetryComposite(integration => integration.onStart),
70
+ onStepStart: createTelemetryComposite(
71
+ integration => integration.onStepStart,
72
+ ),
73
+ onToolCallStart: createTelemetryComposite(
74
+ integration => integration.onToolCallStart,
75
+ ),
76
+ onToolCallFinish: createTelemetryComposite(
77
+ integration => integration.onToolCallFinish,
78
+ ),
79
+ onStepFinish: createTelemetryComposite(
80
+ integration => integration.onStepFinish,
81
+ ),
82
+ onFinish: createTelemetryComposite(integration => integration.onFinish),
83
+ };
84
+ };
85
+ }
@@ -0,0 +1,20 @@
1
+ import { Tracer, trace } from '@opentelemetry/api';
2
+ import { noopTracer } from './noop-tracer';
3
+
4
+ export function getTracer({
5
+ isEnabled = false,
6
+ tracer,
7
+ }: {
8
+ isEnabled?: boolean;
9
+ tracer?: Tracer;
10
+ } = {}): Tracer {
11
+ if (!isEnabled) {
12
+ return noopTracer;
13
+ }
14
+
15
+ if (tracer) {
16
+ return tracer;
17
+ }
18
+
19
+ return trace.getTracer('ai');
20
+ }
@@ -0,0 +1,4 @@
1
+ export type { TelemetrySettings } from './telemetry-settings';
2
+ export type { TelemetryIntegration } from './telemetry-integration';
3
+ export { bindTelemetryIntegration } from './get-global-telemetry-integration';
4
+ export { registerTelemetryIntegration } from './telemetry-integration-registry';
@@ -0,0 +1,69 @@
1
+ import { Span, SpanContext, Tracer } from '@opentelemetry/api';
2
+
3
+ /**
4
+ * Tracer implementation that does nothing (null object).
5
+ */
6
+ export const noopTracer: Tracer = {
7
+ startSpan(): Span {
8
+ return noopSpan;
9
+ },
10
+
11
+ startActiveSpan<F extends (span: Span) => unknown>(
12
+ name: unknown,
13
+ arg1: unknown,
14
+ arg2?: unknown,
15
+ arg3?: F,
16
+ ): ReturnType<any> {
17
+ if (typeof arg1 === 'function') {
18
+ return arg1(noopSpan);
19
+ }
20
+ if (typeof arg2 === 'function') {
21
+ return arg2(noopSpan);
22
+ }
23
+ if (typeof arg3 === 'function') {
24
+ return arg3(noopSpan);
25
+ }
26
+ },
27
+ };
28
+
29
+ const noopSpan: Span = {
30
+ spanContext() {
31
+ return noopSpanContext;
32
+ },
33
+ setAttribute() {
34
+ return this;
35
+ },
36
+ setAttributes() {
37
+ return this;
38
+ },
39
+ addEvent() {
40
+ return this;
41
+ },
42
+ addLink() {
43
+ return this;
44
+ },
45
+ addLinks() {
46
+ return this;
47
+ },
48
+ setStatus() {
49
+ return this;
50
+ },
51
+ updateName() {
52
+ return this;
53
+ },
54
+ end() {
55
+ return this;
56
+ },
57
+ isRecording() {
58
+ return false;
59
+ },
60
+ recordException() {
61
+ return this;
62
+ },
63
+ };
64
+
65
+ const noopSpanContext: SpanContext = {
66
+ traceId: '',
67
+ spanId: '',
68
+ traceFlags: 0,
69
+ };
@@ -0,0 +1,75 @@
1
+ import {
2
+ Attributes,
3
+ Span,
4
+ Tracer,
5
+ SpanStatusCode,
6
+ context,
7
+ } from '@opentelemetry/api';
8
+
9
+ export async function recordSpan<T>({
10
+ name,
11
+ tracer,
12
+ attributes,
13
+ fn,
14
+ endWhenDone = true,
15
+ }: {
16
+ name: string;
17
+ tracer: Tracer;
18
+ attributes: Attributes | PromiseLike<Attributes>;
19
+ fn: (span: Span) => Promise<T>;
20
+ endWhenDone?: boolean;
21
+ }) {
22
+ return tracer.startActiveSpan(
23
+ name,
24
+ { attributes: await attributes },
25
+ async span => {
26
+ // Capture the current context to maintain it across async generator yields
27
+ const ctx = context.active();
28
+
29
+ try {
30
+ // Execute within the captured context to ensure async generators
31
+ // don't lose the active span when they yield
32
+ const result = await context.with(ctx, () => fn(span));
33
+
34
+ if (endWhenDone) {
35
+ span.end();
36
+ }
37
+
38
+ return result;
39
+ } catch (error) {
40
+ try {
41
+ recordErrorOnSpan(span, error);
42
+ } finally {
43
+ // always stop the span when there is an error:
44
+ span.end();
45
+ }
46
+
47
+ throw error;
48
+ }
49
+ },
50
+ );
51
+ }
52
+
53
+ /**
54
+ * Record an error on a span. Sets the span status to error. If the error is
55
+ * an instance of Error, an exception event with name, message, and stack
56
+ * will also be recorded.
57
+ *
58
+ * @param span - The span to record the error on.
59
+ * @param error - The error to record on the span.
60
+ */
61
+ export function recordErrorOnSpan(span: Span, error: unknown) {
62
+ if (error instanceof Error) {
63
+ span.recordException({
64
+ name: error.name,
65
+ message: error.message,
66
+ stack: error.stack,
67
+ });
68
+ span.setStatus({
69
+ code: SpanStatusCode.ERROR,
70
+ message: error.message,
71
+ });
72
+ } else {
73
+ span.setStatus({ code: SpanStatusCode.ERROR });
74
+ }
75
+ }
@@ -0,0 +1,78 @@
1
+ import type { Attributes, AttributeValue } from '@opentelemetry/api';
2
+ import type { TelemetrySettings } from './telemetry-settings';
3
+
4
+ type ResolvableAttributeValue = () =>
5
+ | AttributeValue
6
+ | PromiseLike<AttributeValue>
7
+ | undefined;
8
+
9
+ export async function selectTelemetryAttributes({
10
+ telemetry,
11
+ attributes,
12
+ }: {
13
+ telemetry?: TelemetrySettings;
14
+ attributes: {
15
+ [attributeKey: string]:
16
+ | AttributeValue
17
+ | { input: ResolvableAttributeValue }
18
+ | { output: ResolvableAttributeValue }
19
+ | undefined;
20
+ };
21
+ }): Promise<Attributes> {
22
+ // when telemetry is disabled, return an empty object to avoid serialization overhead:
23
+ if (telemetry?.isEnabled !== true) {
24
+ return {};
25
+ }
26
+
27
+ const resultAttributes: Attributes = {};
28
+
29
+ for (const [key, value] of Object.entries(attributes)) {
30
+ if (value == null) {
31
+ continue;
32
+ }
33
+
34
+ // input value, check if it should be recorded:
35
+ if (
36
+ typeof value === 'object' &&
37
+ 'input' in value &&
38
+ typeof value.input === 'function'
39
+ ) {
40
+ // default to true:
41
+ if (telemetry?.recordInputs === false) {
42
+ continue;
43
+ }
44
+
45
+ const result = await value.input();
46
+
47
+ if (result != null) {
48
+ resultAttributes[key] = result;
49
+ }
50
+
51
+ continue;
52
+ }
53
+
54
+ // output value, check if it should be recorded:
55
+ if (
56
+ typeof value === 'object' &&
57
+ 'output' in value &&
58
+ typeof value.output === 'function'
59
+ ) {
60
+ // default to true:
61
+ if (telemetry?.recordOutputs === false) {
62
+ continue;
63
+ }
64
+
65
+ const result = await value.output();
66
+
67
+ if (result != null) {
68
+ resultAttributes[key] = result;
69
+ }
70
+ continue;
71
+ }
72
+
73
+ // value is an attribute value already:
74
+ resultAttributes[key] = value as AttributeValue;
75
+ }
76
+
77
+ return resultAttributes;
78
+ }
@@ -0,0 +1,33 @@
1
+ import {
2
+ LanguageModelV3Message,
3
+ LanguageModelV3Prompt,
4
+ } from '@ai-sdk/provider';
5
+ import { convertDataContentToBase64String } from '../prompt/data-content';
6
+
7
+ /**
8
+ * Helper utility to serialize prompt content for OpenTelemetry tracing.
9
+ * It is initially created because normalized LanguageModelV3Prompt carries
10
+ * images as Uint8Arrays, on which JSON.stringify acts weirdly, converting
11
+ * them to objects with stringified indices as keys, e.g. {"0": 42, "1": 69 }.
12
+ */
13
+ export function stringifyForTelemetry(prompt: LanguageModelV3Prompt): string {
14
+ return JSON.stringify(
15
+ prompt.map((message: LanguageModelV3Message) => ({
16
+ ...message,
17
+ content:
18
+ typeof message.content === 'string'
19
+ ? message.content
20
+ : message.content.map(part =>
21
+ part.type === 'file'
22
+ ? {
23
+ ...part,
24
+ data:
25
+ part.data instanceof Uint8Array
26
+ ? convertDataContentToBase64String(part.data)
27
+ : part.data,
28
+ }
29
+ : part,
30
+ ),
31
+ })),
32
+ );
33
+ }
@@ -0,0 +1,17 @@
1
+ import type { TelemetryIntegration } from './telemetry-integration';
2
+
3
+ /**
4
+ * Registers a telemetry integration globally.
5
+ */
6
+ export function registerTelemetryIntegration(
7
+ integration: TelemetryIntegration,
8
+ ): void {
9
+ if (!globalThis.AI_SDK_TELEMETRY_INTEGRATIONS) {
10
+ globalThis.AI_SDK_TELEMETRY_INTEGRATIONS = [];
11
+ }
12
+ globalThis.AI_SDK_TELEMETRY_INTEGRATIONS.push(integration);
13
+ }
14
+
15
+ export function getGlobalTelemetryIntegrations(): TelemetryIntegration[] {
16
+ return globalThis.AI_SDK_TELEMETRY_INTEGRATIONS ?? [];
17
+ }