ai 6.0.32 → 6.0.34

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