ranuts 0.4.1-alpha.1 → 0.5.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 (331) hide show
  1. package/CLAUDE.md +178 -26
  2. package/README.de.md +62 -0
  3. package/README.es.md +62 -0
  4. package/README.fa.md +62 -0
  5. package/README.ja.md +62 -0
  6. package/README.ko.md +62 -0
  7. package/README.md +35 -90
  8. package/README.pt.md +62 -0
  9. package/README.zh-CN.md +35 -89
  10. package/dist/iife/index.iife.js +4 -0
  11. package/dist/iife/utils.iife.js +4 -0
  12. package/dist/index.d.ts +1 -1
  13. package/dist/index.js +306 -1
  14. package/dist/src/_virtual/_rolldown/runtime/index.js +12 -0
  15. package/dist/src/conversation/engine.d.ts +109 -0
  16. package/dist/src/conversation/engine.js +101 -0
  17. package/dist/src/conversation/index.d.ts +12 -0
  18. package/dist/src/conversation/index.js +4 -0
  19. package/dist/src/conversation/types.d.ts +99 -0
  20. package/dist/src/stream/accumulator.d.ts +51 -0
  21. package/dist/src/stream/accumulator.js +79 -0
  22. package/dist/src/stream/budget.d.ts +89 -0
  23. package/dist/src/stream/budget.js +45 -0
  24. package/dist/src/stream/index.d.ts +25 -0
  25. package/dist/src/stream/index.js +11 -0
  26. package/dist/src/stream/sse.d.ts +51 -0
  27. package/dist/src/stream/sse.js +61 -0
  28. package/dist/src/stream/types.d.ts +106 -0
  29. package/dist/src/sw/index.js +52 -1
  30. package/dist/src/utils/async.js +49 -0
  31. package/dist/src/utils/audioRecorder.js +1121 -0
  32. package/dist/src/utils/behavior.js +9 -0
  33. package/dist/src/utils/binary.js +89 -0
  34. package/dist/src/utils/bom.d.ts +31 -9
  35. package/dist/src/utils/bom.js +165 -0
  36. package/dist/src/utils/bridge.js +310 -0
  37. package/dist/src/utils/canvas.d.ts +1 -0
  38. package/dist/src/utils/canvas.js +103 -0
  39. package/dist/src/utils/chain.js +76 -0
  40. package/dist/src/utils/color.d.ts +0 -1
  41. package/dist/src/utils/color.js +312 -0
  42. package/dist/src/utils/compose.js +22 -0
  43. package/dist/src/utils/console.js +22 -0
  44. package/dist/src/utils/debounce.js +18 -0
  45. package/dist/src/utils/device.d.ts +0 -9
  46. package/dist/src/utils/device.js +31 -0
  47. package/dist/src/utils/diff.d.ts +51 -0
  48. package/dist/src/utils/diff.js +110 -0
  49. package/dist/src/utils/dom.d.ts +1 -1
  50. package/dist/src/utils/dom.js +89 -0
  51. package/dist/src/utils/error.js +11 -0
  52. package/dist/src/utils/event.d.ts +3 -3
  53. package/dist/src/utils/event.js +39 -0
  54. package/dist/src/utils/file.js +14 -0
  55. package/dist/src/utils/func.js +4 -0
  56. package/dist/src/utils/i18n/index.js +79 -1
  57. package/dist/src/utils/idb.js +177 -0
  58. package/dist/src/utils/img.d.ts +1 -1
  59. package/dist/src/utils/img.js +68 -0
  60. package/dist/src/utils/index.d.ts +81 -77
  61. package/dist/src/utils/index.js +306 -1
  62. package/dist/src/utils/lang.d.ts +23 -6
  63. package/dist/src/utils/lang.js +42 -0
  64. package/dist/src/utils/localePath.js +48 -0
  65. package/dist/src/utils/memoize.d.ts +1 -7
  66. package/dist/src/utils/memoize.js +16 -0
  67. package/dist/src/utils/mimeType.js +841 -0
  68. package/dist/src/utils/monitor.js +84 -0
  69. package/dist/src/utils/network.js +117 -0
  70. package/dist/src/utils/noop.js +5 -0
  71. package/dist/src/utils/number.d.ts +20 -0
  72. package/dist/src/utils/number.js +185 -0
  73. package/dist/src/utils/obj.d.ts +2 -2
  74. package/dist/src/utils/obj.js +140 -0
  75. package/dist/src/utils/paginate.js +93 -0
  76. package/dist/src/utils/performance.js +26 -0
  77. package/dist/src/utils/placement.d.ts +42 -5
  78. package/dist/src/utils/placement.js +37 -0
  79. package/dist/src/utils/prefetch.js +69 -0
  80. package/dist/src/utils/queue.js +46 -0
  81. package/dist/src/utils/report.js +44 -0
  82. package/dist/src/utils/request.js +35 -0
  83. package/dist/src/utils/script.d.ts +1 -1
  84. package/dist/src/utils/script.js +46 -0
  85. package/dist/src/utils/scroll.d.ts +93 -0
  86. package/dist/src/utils/scroll.js +62 -0
  87. package/dist/src/utils/secure.js +36 -0
  88. package/dist/src/utils/segment.js +43 -0
  89. package/dist/src/utils/signal.d.ts +1 -1
  90. package/dist/src/utils/signal.js +13 -0
  91. package/dist/src/utils/speech.js +59 -0
  92. package/dist/src/utils/storage.js +53 -0
  93. package/dist/src/utils/str.d.ts +0 -6
  94. package/dist/src/utils/str.js +395 -0
  95. package/dist/src/utils/style.js +29 -0
  96. package/dist/src/utils/subscribe.js +58 -0
  97. package/dist/src/utils/throttle.js +17 -0
  98. package/dist/src/utils/time.d.ts +0 -21
  99. package/dist/src/utils/time.js +97 -0
  100. package/dist/src/utils/totp/sha/common.d.ts +1 -1
  101. package/dist/src/utils/totp/sha/common.js +201 -0
  102. package/dist/src/utils/totp/sha/converters.d.ts +1 -1
  103. package/dist/src/utils/totp/sha/converters.js +188 -0
  104. package/dist/src/utils/totp/sha/primitives_32.js +55 -0
  105. package/dist/src/utils/totp/sha/primitives_64.js +85 -0
  106. package/dist/src/utils/totp/sha/sha.d.ts +1 -1
  107. package/dist/src/utils/totp/sha/sha.js +30 -0
  108. package/dist/src/utils/totp/sha/sha1.d.ts +2 -2
  109. package/dist/src/utils/totp/sha/sha1.js +51 -0
  110. package/dist/src/utils/totp/sha/sha256.d.ts +2 -2
  111. package/dist/src/utils/totp/sha/sha256.js +56 -0
  112. package/dist/src/utils/totp/sha/sha3.d.ts +3 -3
  113. package/dist/src/utils/totp/sha/sha3.js +253 -0
  114. package/dist/src/utils/totp/sha/sha512.d.ts +3 -3
  115. package/dist/src/utils/totp/sha/sha512.js +177 -0
  116. package/dist/src/utils/totp/totp.js +49 -0
  117. package/dist/src/utils/tween.js +59 -0
  118. package/dist/src/utils/visual/application.d.ts +4 -4
  119. package/dist/src/utils/visual/application.js +42 -0
  120. package/dist/src/utils/visual/enums.js +19 -0
  121. package/dist/src/utils/visual/event/boundary.d.ts +2 -2
  122. package/dist/src/utils/visual/event/boundary.js +93 -0
  123. package/dist/src/utils/visual/event/event.d.ts +1 -1
  124. package/dist/src/utils/visual/event/event.js +34 -0
  125. package/dist/src/utils/visual/event/index.d.ts +4 -4
  126. package/dist/src/utils/visual/event/types.d.ts +2 -2
  127. package/dist/src/utils/visual/event/types.js +23 -0
  128. package/dist/src/utils/visual/graphics/graphics.d.ts +8 -8
  129. package/dist/src/utils/visual/graphics/graphics.js +168 -0
  130. package/dist/src/utils/visual/graphics/graphicsData.d.ts +3 -3
  131. package/dist/src/utils/visual/graphics/graphicsData.js +12 -0
  132. package/dist/src/utils/visual/graphics/graphicsGeometry.d.ts +7 -7
  133. package/dist/src/utils/visual/graphics/graphicsGeometry.js +39 -0
  134. package/dist/src/utils/visual/graphics/index.d.ts +3 -3
  135. package/dist/src/utils/visual/index.d.ts +8 -8
  136. package/dist/src/utils/visual/index.js +20 -1
  137. package/dist/src/utils/visual/math/bezier.js +15 -0
  138. package/dist/src/utils/visual/math/enums.js +6 -0
  139. package/dist/src/utils/visual/math/index.d.ts +4 -4
  140. package/dist/src/utils/visual/math/matrix.d.ts +1 -1
  141. package/dist/src/utils/visual/math/matrix.js +59 -0
  142. package/dist/src/utils/visual/math/transform.d.ts +2 -2
  143. package/dist/src/utils/visual/math/transform.js +50 -0
  144. package/dist/src/utils/visual/render/batchRenderer.d.ts +4 -4
  145. package/dist/src/utils/visual/render/batchRenderer.js +59 -0
  146. package/dist/src/utils/visual/render/canvasRenderer.d.ts +3 -3
  147. package/dist/src/utils/visual/render/canvasRenderer.js +17 -0
  148. package/dist/src/utils/visual/render/filter.d.ts +1 -1
  149. package/dist/src/utils/visual/render/filter.js +92 -0
  150. package/dist/src/utils/visual/render/index.d.ts +2 -2
  151. package/dist/src/utils/visual/render/index.js +20 -0
  152. package/dist/src/utils/visual/render/render.d.ts +4 -4
  153. package/dist/src/utils/visual/render/render.js +19 -0
  154. package/dist/src/utils/visual/render/renderTarget.js +28 -0
  155. package/dist/src/utils/visual/render/utils/batch/index.d.ts +4 -4
  156. package/dist/src/utils/visual/render/utils/batch/index.js +88 -0
  157. package/dist/src/utils/visual/render/utils/earcut.js +328 -0
  158. package/dist/src/utils/visual/render/utils/float.js +65 -0
  159. package/dist/src/utils/visual/render/utils/index.js +50 -0
  160. package/dist/src/utils/visual/render/utils/verticy.d.ts +2 -2
  161. package/dist/src/utils/visual/render/utils/verticy.js +162 -0
  162. package/dist/src/utils/visual/render/utils/webgl/initShader.d.ts +1 -1
  163. package/dist/src/utils/visual/render/utils/webgl/initShader.js +31 -0
  164. package/dist/src/utils/visual/render/utils/webgl/shaders.js +22 -0
  165. package/dist/src/utils/visual/render/utils/webgpu/shaders.js +32 -0
  166. package/dist/src/utils/visual/render/webGPURenderer.d.ts +2 -2
  167. package/dist/src/utils/visual/render/webGPURenderer.js +185 -0
  168. package/dist/src/utils/visual/render/webGlRenderer.d.ts +2 -2
  169. package/dist/src/utils/visual/render/webGlRenderer.js +88 -0
  170. package/dist/src/utils/visual/shape/circle.d.ts +3 -3
  171. package/dist/src/utils/visual/shape/circle.js +17 -0
  172. package/dist/src/utils/visual/shape/ellipse.d.ts +3 -3
  173. package/dist/src/utils/visual/shape/ellipse.js +18 -0
  174. package/dist/src/utils/visual/shape/index.d.ts +6 -6
  175. package/dist/src/utils/visual/shape/polygon.d.ts +3 -3
  176. package/dist/src/utils/visual/shape/polygon.js +29 -0
  177. package/dist/src/utils/visual/shape/rectangle.d.ts +3 -3
  178. package/dist/src/utils/visual/shape/rectangle.js +18 -0
  179. package/dist/src/utils/visual/shape/roundedRectangle.d.ts +3 -3
  180. package/dist/src/utils/visual/shape/roundedRectangle.js +32 -0
  181. package/dist/src/utils/visual/shape/shape.d.ts +2 -2
  182. package/dist/src/utils/visual/shape/shape.js +5 -0
  183. package/dist/src/utils/visual/style/fill.js +18 -0
  184. package/dist/src/utils/visual/style/index.d.ts +2 -2
  185. package/dist/src/utils/visual/style/line.d.ts +2 -2
  186. package/dist/src/utils/visual/style/line.js +18 -0
  187. package/dist/src/utils/visual/types.d.ts +1 -1
  188. package/dist/src/utils/visual/vertex/container.d.ts +5 -5
  189. package/dist/src/utils/visual/vertex/container.js +54 -0
  190. package/dist/src/utils/visual/vertex/point.js +37 -0
  191. package/dist/src/utils/visual/vertex/vertex.d.ts +7 -7
  192. package/dist/src/utils/visual/vertex/vertex.js +76 -0
  193. package/dist/src/utils/worker.js +89 -0
  194. package/dist/src/utils/zip.js +161 -0
  195. package/dist/src/vnode/h.d.ts +1 -1
  196. package/dist/src/vnode/h.js +22 -0
  197. package/dist/src/vnode/hooks.d.ts +1 -1
  198. package/dist/src/vnode/htmlDomApi.js +94 -0
  199. package/dist/src/vnode/index.d.ts +16 -16
  200. package/dist/src/vnode/index.js +28 -1
  201. package/dist/src/vnode/init.d.ts +1 -1
  202. package/dist/src/vnode/init.js +104 -0
  203. package/dist/src/vnode/is.d.ts +1 -1
  204. package/dist/src/vnode/is.js +20 -0
  205. package/dist/src/vnode/modules/attributes.d.ts +1 -1
  206. package/dist/src/vnode/modules/attributes.js +21 -0
  207. package/dist/src/vnode/modules/class.d.ts +1 -1
  208. package/dist/src/vnode/modules/class.js +18 -0
  209. package/dist/src/vnode/modules/index.js +15 -0
  210. package/dist/src/vnode/modules/listeners.d.ts +1 -1
  211. package/dist/src/vnode/modules/listeners.js +36 -0
  212. package/dist/src/vnode/modules/props.d.ts +1 -1
  213. package/dist/src/vnode/modules/props.js +17 -0
  214. package/dist/src/vnode/modules/style.d.ts +1 -1
  215. package/dist/src/vnode/modules/style.js +52 -0
  216. package/dist/src/vnode/vnode.d.ts +6 -6
  217. package/dist/src/vnode/vnode.js +13 -0
  218. package/dist/third-party/jschardet/LICENSE.txt +504 -0
  219. package/dist/third-party/jschardet/NOTICE.txt +33 -0
  220. package/docs/API.md +86 -65
  221. package/package.json +20 -15
  222. package/dist/bin/generate-api-docs.d.ts +0 -1
  223. package/dist/build/build.es.d.ts +0 -2
  224. package/dist/build/build.umd.d.ts +0 -2
  225. package/dist/build/build.umd.node.d.ts +0 -2
  226. package/dist/build/build.umd.utils.d.ts +0 -2
  227. package/dist/chain-CTywmh_H.js +0 -1
  228. package/dist/color-B_KMM8UX.js +0 -1
  229. package/dist/enums-BAgbyGdH.js +0 -1
  230. package/dist/examples/clone-deep-example.d.ts +0 -1
  231. package/dist/examples/is-equal-example.d.ts +0 -1
  232. package/dist/i18n-BJIS1fR1.js +0 -1
  233. package/dist/plugins/vite-plugins-banner.d.ts +0 -2
  234. package/dist/rolldown-runtime-I3TIJKkN.js +0 -1
  235. package/dist/src/node/appendFile.d.ts +0 -7
  236. package/dist/src/node/body.d.ts +0 -9
  237. package/dist/src/node/color.d.ts +0 -7
  238. package/dist/src/node/command.d.ts +0 -8
  239. package/dist/src/node/ctx2req.d.ts +0 -5
  240. package/dist/src/node/fileInfo.d.ts +0 -7
  241. package/dist/src/node/fs.d.ts +0 -7
  242. package/dist/src/node/get.d.ts +0 -10
  243. package/dist/src/node/getIPAdress.d.ts +0 -1
  244. package/dist/src/node/index.d.ts +0 -25
  245. package/dist/src/node/index.js +0 -1
  246. package/dist/src/node/isColorSupported.d.ts +0 -2
  247. package/dist/src/node/paresUrl.d.ts +0 -19
  248. package/dist/src/node/readDir.d.ts +0 -6
  249. package/dist/src/node/readFile.d.ts +0 -9
  250. package/dist/src/node/router.d.ts +0 -42
  251. package/dist/src/node/send.d.ts +0 -7
  252. package/dist/src/node/server.d.ts +0 -18
  253. package/dist/src/node/startTask.d.ts +0 -2
  254. package/dist/src/node/stream.d.ts +0 -15
  255. package/dist/src/node/taskEnd.d.ts +0 -2
  256. package/dist/src/node/traverse.d.ts +0 -17
  257. package/dist/src/node/watchFile.d.ts +0 -8
  258. package/dist/src/node/writeFile.d.ts +0 -8
  259. package/dist/src/node/ws.d.ts +0 -8
  260. package/dist/src/utils/visual/render/utils/batch/index.test.d.ts +0 -1
  261. package/dist/src/utils/visual/render/utils/index.test.d.ts +0 -1
  262. package/dist/test/binary.test.d.ts +0 -1
  263. package/dist/test/bridge.test.d.ts +0 -1
  264. package/dist/test/canvas.test.d.ts +0 -1
  265. package/dist/test/clearBr.test.d.ts +0 -1
  266. package/dist/test/cloneDeep.test.d.ts +0 -1
  267. package/dist/test/color.test.d.ts +0 -1
  268. package/dist/test/command.test.d.ts +0 -1
  269. package/dist/test/encodeUrl.test.d.ts +0 -1
  270. package/dist/test/escapeHtml.test.d.ts +0 -1
  271. package/dist/test/is-equal.test.d.ts +0 -1
  272. package/dist/test/mimeType.test.d.ts +0 -1
  273. package/dist/test/package-exports.test.d.ts +0 -1
  274. package/dist/test/server.test.d.ts +0 -1
  275. package/dist/test/status.test.d.ts +0 -1
  276. package/dist/test/sw.test.d.ts +0 -1
  277. package/dist/test/totp.test.d.ts +0 -1
  278. package/dist/test/tween.test.d.ts +0 -1
  279. package/dist/test/utils/async.test.d.ts +0 -1
  280. package/dist/test/utils/autosize.test.d.ts +0 -1
  281. package/dist/test/utils/color-grade.test.d.ts +0 -1
  282. package/dist/test/utils/compose.test.d.ts +0 -1
  283. package/dist/test/utils/event.test.d.ts +0 -1
  284. package/dist/test/utils/file-read.test.d.ts +0 -1
  285. package/dist/test/utils/format.test.d.ts +0 -1
  286. package/dist/test/utils/i18n.test.d.ts +0 -1
  287. package/dist/test/utils/idb-collection.test.d.ts +0 -1
  288. package/dist/test/utils/idb.test.d.ts +0 -1
  289. package/dist/test/utils/instrumentation.test.d.ts +0 -1
  290. package/dist/test/utils/interp.test.d.ts +0 -1
  291. package/dist/test/utils/lang.test.d.ts +0 -1
  292. package/dist/test/utils/locale-path.test.d.ts +0 -1
  293. package/dist/test/utils/media-query.test.d.ts +0 -1
  294. package/dist/test/utils/mergeObj.test.d.ts +0 -1
  295. package/dist/test/utils/number-parse.test.d.ts +0 -1
  296. package/dist/test/utils/paginate.test.d.ts +0 -1
  297. package/dist/test/utils/placement.test.d.ts +0 -1
  298. package/dist/test/utils/prefetch.test.d.ts +0 -1
  299. package/dist/test/utils/query-storage.test.d.ts +0 -1
  300. package/dist/test/utils/queue.test.d.ts +0 -1
  301. package/dist/test/utils/report-config.test.d.ts +0 -1
  302. package/dist/test/utils/report-image.test.d.ts +0 -1
  303. package/dist/test/utils/secure.test.d.ts +0 -1
  304. package/dist/test/utils/segment.test.d.ts +0 -1
  305. package/dist/test/utils/signal-once.test.d.ts +0 -1
  306. package/dist/test/utils/single-flight.test.d.ts +0 -1
  307. package/dist/test/utils/speech.test.d.ts +0 -1
  308. package/dist/test/utils/text-boundary.test.d.ts +0 -1
  309. package/dist/test/utils/time.test.d.ts +0 -1
  310. package/dist/test/utils/timing.test.d.ts +0 -1
  311. package/dist/test/utils/truncate.test.d.ts +0 -1
  312. package/dist/test/utils/visual/filter.test.d.ts +0 -1
  313. package/dist/test/utils/worker-client.test.d.ts +0 -1
  314. package/dist/test/utils/worker-serve.test.d.ts +0 -1
  315. package/dist/test/utils/zip.test.d.ts +0 -1
  316. package/dist/test/visual/math.test.d.ts +0 -1
  317. package/dist/test/visual-application.test.d.ts +0 -1
  318. package/dist/test/visual-batch.test.d.ts +0 -1
  319. package/dist/test/visual-math.test.d.ts +0 -1
  320. package/dist/test/vnode/index.d.ts +0 -1
  321. package/dist/test/vnode/server.d.ts +0 -2
  322. package/dist/test/vnode/vnode.d.ts +0 -1
  323. package/dist/test/vnode.test.d.ts +0 -1
  324. package/dist/test/websocket.test.d.ts +0 -1
  325. package/dist/test/writeFile.test.d.ts +0 -1
  326. package/dist/umd/index.umd.cjs +0 -1
  327. package/dist/umd/node/node.umd.cjs +0 -1
  328. package/dist/umd/utils/utils.umd.cjs +0 -1
  329. package/dist/utils-BNwhohXq.js +0 -1
  330. package/dist/vite.config.d.ts +0 -8
  331. package/dist/vitest.config.d.ts +0 -2
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Folds a {@link StreamChunk} sequence into the blocks it describes.
3
+ *
4
+ * A renderer wants "the current text", not "every delta since the start", and rebuilding
5
+ * that by concatenating in the view is where streaming UIs leak: the view ends up owning
6
+ * ordering, interleaving, and the block-end/delta reconciliation. This owns all three, so
7
+ * a view reads a snapshot and renders it.
8
+ */
9
+ import type { ContentBlock, FinishReason, StreamChunk, TokenUsage } from "./types.js";
10
+ /** Immutable view of everything received so far. */
11
+ export interface StreamSnapshot {
12
+ /** Completed and in-progress blocks, in `index` order. */
13
+ readonly blocks: readonly ContentBlock[];
14
+ /** Latest usage report, once one has arrived. */
15
+ readonly usage: TokenUsage | undefined;
16
+ /** Terminal reason, once `finish` has arrived. */
17
+ readonly finishReason: FinishReason | undefined;
18
+ /** True once `finish` has arrived. */
19
+ readonly done: boolean;
20
+ }
21
+ /** Stateful fold over one response's chunks. */
22
+ export interface StreamAccumulator {
23
+ /**
24
+ * Applies one chunk.
25
+ *
26
+ * A delta for an unseen `index` opens its block, because several providers omit
27
+ * `block-start` entirely. A `block-end` replaces whatever was accumulated at that index:
28
+ * the assembled block the provider sent is authoritative.
29
+ *
30
+ * @param chunk The chunk to fold in.
31
+ */
32
+ push(chunk: StreamChunk): void;
33
+ /** @returns An immutable view of the current state. */
34
+ snapshot(): StreamSnapshot;
35
+ /** @returns Every text block concatenated, in index order. */
36
+ text(): string;
37
+ /** @returns Every reasoning block concatenated, in index order. */
38
+ reasoning(): string;
39
+ /** @returns Completed tool calls, in index order. */
40
+ toolCalls(): readonly Extract<ContentBlock, {
41
+ type: 'tool-call';
42
+ }>[];
43
+ /** Discards all state so the instance can fold another response. */
44
+ reset(): void;
45
+ }
46
+ /**
47
+ * Creates a fold over one streamed response.
48
+ *
49
+ * @returns A fresh accumulator with no state.
50
+ */
51
+ export declare function createStreamAccumulator(): StreamAccumulator;
@@ -0,0 +1,79 @@
1
+ function d(r) {
2
+ return r === "text" ? {
3
+ type: "text",
4
+ text: ""
5
+ } : r === "reasoning" ? {
6
+ type: "reasoning",
7
+ text: ""
8
+ } : {
9
+ type: "tool-call",
10
+ id: "",
11
+ name: "",
12
+ arguments: ""
13
+ };
14
+ }
15
+ function c() {
16
+ let r = /* @__PURE__ */ new Map(), a, n;
17
+ const o = (e, t) => {
18
+ const i = r.get(e);
19
+ if (i !== void 0 && i.type === t) return i;
20
+ const l = d(t);
21
+ return r.set(e, l), l;
22
+ }, s = () => [...r.entries()].sort(([e], [t]) => e - t).map(([, e]) => e);
23
+ return {
24
+ push(e) {
25
+ switch (e.type) {
26
+ case "block-start":
27
+ o(e.index, e.blockType);
28
+ return;
29
+ case "text-delta": {
30
+ const t = o(e.index, "text");
31
+ t.text += e.text;
32
+ return;
33
+ }
34
+ case "reasoning-delta": {
35
+ const t = o(e.index, "reasoning");
36
+ t.text += e.text;
37
+ return;
38
+ }
39
+ case "tool-call-delta": {
40
+ const t = o(e.index, "tool-call");
41
+ e.id !== "" && (t.id = e.id), e.name !== void 0 && e.name !== "" && (t.name = e.name), t.arguments += e.argumentsDelta;
42
+ return;
43
+ }
44
+ case "block-end":
45
+ r.set(e.index, { ...e.block });
46
+ return;
47
+ case "usage":
48
+ a = e.usage;
49
+ return;
50
+ case "finish":
51
+ n = e.reason;
52
+ return;
53
+ }
54
+ },
55
+ snapshot() {
56
+ return Object.freeze({
57
+ blocks: Object.freeze(s().map((e) => Object.freeze({ ...e }))),
58
+ usage: a === void 0 ? void 0 : Object.freeze({ ...a }),
59
+ finishReason: n,
60
+ done: n !== void 0
61
+ });
62
+ },
63
+ text() {
64
+ return s().reduce((e, t) => t.type === "text" ? e + t.text : e, "");
65
+ },
66
+ reasoning() {
67
+ return s().reduce((e, t) => t.type === "reasoning" ? e + t.text : e, "");
68
+ },
69
+ toolCalls() {
70
+ return s().filter((e) => e.type === "tool-call");
71
+ },
72
+ reset() {
73
+ r = /* @__PURE__ */ new Map(), a = void 0, n = void 0;
74
+ }
75
+ };
76
+ }
77
+ export {
78
+ c as createStreamAccumulator
79
+ };
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Counting what a conversation costs, and deciding what to drop when it stops fitting.
3
+ *
4
+ * A chat client that never does this works for a week and then stops working: every turn
5
+ * carries the whole history, so the request grows monotonically until the provider refuses
6
+ * it. The refusal arrives as a wall — the conversation someone was having simply stops.
7
+ *
8
+ * Two pieces, deliberately separate:
9
+ *
10
+ * - **{@link estimateTokens}** answers "how big is this text" without a tokenizer. It is an
11
+ * estimate and named as one. A real tokenizer is a per-model dependency measured in
12
+ * megabytes, and this is used to decide *when to act*, not to bill anyone.
13
+ * - **{@link planCompaction}** answers "what has to go" given sizes and a ceiling. It knows
14
+ * nothing about messages, models, or summaries — it takes a list of sizes and returns how
15
+ * many leading entries no longer fit. What replaces them is the caller's business.
16
+ *
17
+ * @module ranuts/stream
18
+ */
19
+ import type { TokenUsage } from "./types.js";
20
+ /**
21
+ * Estimates how many tokens a string costs.
22
+ *
23
+ * Deliberately not a tokenizer. Every model's is different, shipping one is megabytes, and
24
+ * the number is used to decide when to compact rather than to charge anyone. It is
25
+ * calibrated to be roughly right per script rather than exactly right for one model.
26
+ *
27
+ * @param text The text to measure.
28
+ * @returns An estimated token count, never below one for a non-empty string.
29
+ */
30
+ export declare function estimateTokens(text: string): number;
31
+ /**
32
+ * Adds two usage reports.
33
+ *
34
+ * Every field is optional and providers differ in which they send, so an absent field on
35
+ * either side is zero rather than a reason to drop the field: a running total that lost
36
+ * `outputTokens` the moment one response omitted it would be worse than no total.
37
+ *
38
+ * @param total The running total.
39
+ * @param next The report to add.
40
+ * @returns A new total.
41
+ */
42
+ export declare function addUsage(total: TokenUsage | undefined, next: TokenUsage | undefined): TokenUsage;
43
+ /** What a compaction has to fit under, and what it may not touch. */
44
+ export interface CompactionLimits {
45
+ /** Token ceiling the next request must fit under, including everything the caller adds. */
46
+ limit: number;
47
+ /**
48
+ * How many trailing entries are never compacted.
49
+ *
50
+ * The recent turns are what the conversation is currently about; summarizing them is how
51
+ * a client starts answering the wrong question. Zero is legal and means the caller
52
+ * accepts that.
53
+ */
54
+ keepRecent: number;
55
+ /**
56
+ * Estimated size of whatever replaces the compacted entries.
57
+ *
58
+ * Required, not defaulted: a summary is not free, and a plan that assumed it was can
59
+ * return "this fits" for a request that does not.
60
+ */
61
+ summaryTokens: number;
62
+ }
63
+ /** What to do with a history that has grown. */
64
+ export interface CompactionPlan {
65
+ /** How many leading entries to replace with one summary; zero means nothing to do. */
66
+ compact: number;
67
+ /** Estimated tokens the request will cost once the plan is applied. */
68
+ projected: number;
69
+ /**
70
+ * Whether {@link CompactionPlan.projected} is under the limit.
71
+ *
72
+ * False means the kept entries alone exceed it — a single message longer than the whole
73
+ * context window, most often. The caller has to decide, and there is nothing this
74
+ * function can silently do that would not lose the user's own words.
75
+ */
76
+ fits: boolean;
77
+ }
78
+ /**
79
+ * Decides how much of a history no longer fits.
80
+ *
81
+ * Leading entries only. A conversation is a sequence, and dropping from the middle leaves
82
+ * the model reading an exchange with a hole in it; dropping from the front is what a summary
83
+ * can stand in for.
84
+ *
85
+ * @param sizes Estimated tokens per entry, in conversation order.
86
+ * @param limits The ceiling, the protected tail, and the summary's own cost.
87
+ * @returns How many leading entries to compact, and whether the result fits.
88
+ */
89
+ export declare function planCompaction(sizes: readonly number[], limits: CompactionLimits): CompactionPlan;
@@ -0,0 +1,45 @@
1
+ var k = 4, d = /[ -〿぀-ヿ㐀-䶿一-鿿豈-﫿＀-￯가-힯]/u;
2
+ function f(t) {
3
+ if (t === "") return 0;
4
+ let n = 0, e = 0;
5
+ for (const o of t) d.test(o) ? n += 1 : e += 1;
6
+ return Math.max(1, n + Math.ceil(e / k));
7
+ }
8
+ function m(t, n) {
9
+ const e = (o, c) => o === void 0 && c === void 0 ? void 0 : (o ?? 0) + (c ?? 0);
10
+ return {
11
+ inputTokens: e(t?.inputTokens, n?.inputTokens),
12
+ outputTokens: e(t?.outputTokens, n?.outputTokens),
13
+ reasoningTokens: e(t?.reasoningTokens, n?.reasoningTokens),
14
+ cachedInputTokens: e(t?.cachedInputTokens, n?.cachedInputTokens),
15
+ totalTokens: e(t?.totalTokens, n?.totalTokens)
16
+ };
17
+ }
18
+ function l(t, n) {
19
+ const { limit: e, keepRecent: o, summaryTokens: c } = n, s = t.reduce((p, T) => p + T, 0);
20
+ if (s <= e) return {
21
+ compact: 0,
22
+ projected: s,
23
+ fits: !0
24
+ };
25
+ const i = Math.max(0, t.length - o);
26
+ let r = 0, u = 0;
27
+ for (; r < i && c + s - u > e; )
28
+ u += t[r] ?? 0, r += 1;
29
+ if (r === 0) return {
30
+ compact: 0,
31
+ projected: s,
32
+ fits: s <= e
33
+ };
34
+ const a = c + s - u;
35
+ return {
36
+ compact: r,
37
+ projected: a,
38
+ fits: a <= e
39
+ };
40
+ }
41
+ export {
42
+ m as addUsage,
43
+ f as estimateTokens,
44
+ l as planCompaction
45
+ };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * `ranuts/stream` — streaming model responses, without a vendor.
3
+ *
4
+ * Three layers, each usable alone:
5
+ *
6
+ * 1. {@link parseEventStream} turns bytes into Server-Sent Events. Transport only.
7
+ * 2. {@link StreamChunk} is the provider-neutral vocabulary one response streams in.
8
+ * 3. {@link createStreamAccumulator} folds those chunks into renderable blocks.
9
+ * 4. {@link estimateTokens} and {@link planCompaction} decide when a history stops fitting.
10
+ *
11
+ * Mapping a vendor's event onto {@link StreamChunk} stays with the caller — that mapping
12
+ * is the only vendor-specific part, and baking one provider's wire format in here would
13
+ * make the other two layers unusable for anyone else. {@link mapEventStream} is the seam.
14
+ *
15
+ * Runtime: browser and node. No DOM — a stream can be folded in a test or on a server.
16
+ *
17
+ * @module ranuts/stream
18
+ */
19
+ export type { ContentBlock, ContentBlockType, FinishReason, ReasoningBlock, StreamChunk, TextBlock, TokenUsage, ToolCallBlock, } from "./types.js";
20
+ export type { ByteSource, ServerSentEvent } from "./sse.js";
21
+ export { mapEventStream, parseEventStream } from "./sse.js";
22
+ export type { StreamAccumulator, StreamSnapshot } from "./accumulator.js";
23
+ export { createStreamAccumulator } from "./accumulator.js";
24
+ export type { CompactionLimits, CompactionPlan } from "./budget.js";
25
+ export { addUsage, estimateTokens, planCompaction } from "./budget.js";
@@ -0,0 +1,11 @@
1
+ import { mapEventStream as e, parseEventStream as m } from "./sse.js";
2
+ import { createStreamAccumulator as a } from "./accumulator.js";
3
+ import { addUsage as p, estimateTokens as i, planCompaction as n } from "./budget.js";
4
+ export {
5
+ p as addUsage,
6
+ a as createStreamAccumulator,
7
+ i as estimateTokens,
8
+ e as mapEventStream,
9
+ m as parseEventStream,
10
+ n as planCompaction
11
+ };
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Server-Sent Events parsing, to the WHATWG event-stream rules.
3
+ *
4
+ * This is the transport half of `ranuts/stream` and knows nothing about models: it turns
5
+ * bytes into {@link ServerSentEvent} records. Mapping an event's `data` onto
6
+ * {@link StreamChunk} is the caller's job, because that mapping is vendor-specific while
7
+ * the framing is not.
8
+ *
9
+ * The parts worth not re-deriving: a chunk boundary can fall anywhere, including inside a
10
+ * multi-byte character and between the two halves of a `\r\n`; `data:` repeats join with
11
+ * `\n`; exactly one space after the colon is stripped; a line starting with `:` is a
12
+ * comment (which is how servers keep a connection warm); and a trailing block with no
13
+ * terminating blank line is still an event.
14
+ */
15
+ import type { StreamChunk } from "./types.js";
16
+ /** One parsed `text/event-stream` event. */
17
+ export interface ServerSentEvent {
18
+ /** Joined `data:` payload; `\n` between repeats, no trailing newline. */
19
+ data: string;
20
+ /** `event:` field, when the server sent one. */
21
+ event?: string;
22
+ /** `id:` field, when the server sent one. */
23
+ id?: string;
24
+ /** `retry:` field in milliseconds, when the server sent a valid integer. */
25
+ retry?: number;
26
+ }
27
+ /** Anything `for await` can walk that yields byte chunks — a `fetch` body, or a fake. */
28
+ export type ByteSource = AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>;
29
+ /**
30
+ * Parses a byte stream as `text/event-stream`.
31
+ *
32
+ * Decoding is streaming, so a multi-byte character split across two network chunks is
33
+ * reassembled rather than replaced. A final block with no terminating blank line is still
34
+ * yielded, which is what a server that closes the connection mid-event produces.
35
+ *
36
+ * @param source Byte source, typically `response.body`.
37
+ * @returns Each event in arrival order.
38
+ */
39
+ export declare function parseEventStream(source: ByteSource): AsyncGenerator<ServerSentEvent>;
40
+ /**
41
+ * Maps SSE events onto {@link StreamChunk} values using a caller-supplied vendor mapping.
42
+ *
43
+ * The mapping returns an array because one wire event commonly carries several normalized
44
+ * chunks — a delta plus a usage report, or two concurrent tool calls — and returning an
45
+ * empty array is how a keep-alive or a sentinel such as `[DONE]` is dropped.
46
+ *
47
+ * @param source Byte source, typically `response.body`.
48
+ * @param map Vendor mapping from one event to zero or more chunks.
49
+ * @returns Each normalized chunk in arrival order.
50
+ */
51
+ export declare function mapEventStream(source: ByteSource, map: (event: ServerSentEvent) => readonly StreamChunk[]): AsyncGenerator<StreamChunk>;
@@ -0,0 +1,61 @@
1
+ async function* f(r) {
2
+ if (typeof r[Symbol.asyncIterator] == "function") {
3
+ yield* r;
4
+ return;
5
+ }
6
+ const t = r.getReader();
7
+ try {
8
+ for (; ; ) {
9
+ const { done: e, value: i } = await t.read();
10
+ if (e) return;
11
+ i !== void 0 && (yield i);
12
+ }
13
+ } finally {
14
+ t.releaseLock();
15
+ }
16
+ }
17
+ function c(r) {
18
+ const t = [];
19
+ let e, i, l;
20
+ for (const n of r.split(`
21
+ `)) {
22
+ if (n === "" || n.startsWith(":")) continue;
23
+ const d = n.indexOf(":"), s = d === -1 ? n : n.slice(0, d);
24
+ let o = d === -1 ? "" : n.slice(d + 1);
25
+ o.startsWith(" ") && (o = o.slice(1)), s === "data" ? t.push(o) : s === "event" ? e = o : s === "id" && !o.includes("\0") ? i = o : s === "retry" && /^\d+$/.test(o) && (l = Number(o));
26
+ }
27
+ if (t.length === 0 && e === void 0 && i === void 0 && l === void 0) return null;
28
+ const a = { data: t.join(`
29
+ `) };
30
+ return e !== void 0 && (a.event = e), i !== void 0 && (a.id = i), l !== void 0 && (a.retry = l), a;
31
+ }
32
+ async function* u(r) {
33
+ const t = new TextDecoder();
34
+ let e = "", i = !1;
35
+ for await (const a of f(r)) {
36
+ e += t.decode(a, { stream: !0 }), e = e.replace(/\r\n?/g, `
37
+ `), i || (e.startsWith("\uFEFF") && (e = e.slice(1)), i = !0);
38
+ let n = e.indexOf(`
39
+
40
+ `);
41
+ for (; n !== -1; ) {
42
+ const d = e.slice(0, n);
43
+ e = e.slice(n + 2);
44
+ const s = c(d);
45
+ s !== null && (yield s), n = e.indexOf(`
46
+
47
+ `);
48
+ }
49
+ }
50
+ e += t.decode();
51
+ const l = c(e.replace(/\r\n?/g, `
52
+ `));
53
+ l !== null && (yield l);
54
+ }
55
+ async function* y(r, t) {
56
+ for await (const e of u(r)) yield* t(e);
57
+ }
58
+ export {
59
+ y as mapEventStream,
60
+ u as parseEventStream
61
+ };
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Provider-neutral vocabulary for one streamed model response.
3
+ *
4
+ * Every mainstream chat completion API streams the same four things — assistant text,
5
+ * separately-billed reasoning text, tool calls, and a token count — but each names them
6
+ * differently and interleaves them differently. Normalizing to one union at the edge means
7
+ * a renderer, an accumulator, and a test fixture never learn a vendor's wire format.
8
+ *
9
+ * The union is the contract; the shape below is what makes it usable:
10
+ *
11
+ * - **`index` correlates interleaved deltas.** Reasoning and text can arrive interleaved,
12
+ * and a response can open several tool calls at once. Consumers group by `index` rather
13
+ * than assuming a delta belongs to whatever arrived last.
14
+ * - **`block-end` carries the assembled block.** A consumer that accumulated the deltas
15
+ * itself can adopt or verify it; a consumer that only wants finished blocks can ignore
16
+ * every delta.
17
+ * - **Tool arguments stay raw JSON text.** `argumentsDelta` is never parsed mid-stream:
18
+ * half a JSON document is not a value, and pretending otherwise is where streaming tool
19
+ * calls usually break.
20
+ * - **`finish` terminates.** `usage` arrives before it, and nothing follows it. A consumer
21
+ * can treat `finish` as the point where its state is complete.
22
+ *
23
+ * @module ranuts/stream
24
+ */
25
+ /** Discriminator for the content a block accumulates. */
26
+ export type ContentBlockType = 'text' | 'reasoning' | 'tool-call';
27
+ /** Assistant prose addressed to the user. */
28
+ export interface TextBlock {
29
+ type: 'text';
30
+ text: string;
31
+ }
32
+ /**
33
+ * Model reasoning, when the provider exposes it. Kept separate from {@link TextBlock}
34
+ * because it is billed, displayed, and retained differently — a UI usually collapses it,
35
+ * and it is normally dropped from the history sent back on the next request.
36
+ */
37
+ export interface ReasoningBlock {
38
+ type: 'reasoning';
39
+ text: string;
40
+ }
41
+ /** One tool invocation requested by the model. */
42
+ export interface ToolCallBlock {
43
+ type: 'tool-call';
44
+ /** Provider-assigned call id, echoed back with the result. */
45
+ id: string;
46
+ /** Tool name; empty until the provider has sent it. */
47
+ name: string;
48
+ /** Raw JSON text of the arguments — deliberately unparsed. */
49
+ arguments: string;
50
+ }
51
+ /** One completed unit of assistant output. */
52
+ export type ContentBlock = TextBlock | ReasoningBlock | ToolCallBlock;
53
+ /**
54
+ * Why the response ended.
55
+ *
56
+ * `tool-calls` is not a failure: the model finished its turn by asking for tools, and the
57
+ * caller is expected to run them and continue. `aborted` means the consumer stopped the
58
+ * stream, which is why it is distinct from `error`.
59
+ */
60
+ export type FinishReason = 'stop' | 'length' | 'tool-calls' | 'content-filter' | 'error' | 'aborted';
61
+ /** Token counts reported for one response. Every field is optional: providers differ. */
62
+ export interface TokenUsage {
63
+ inputTokens?: number;
64
+ outputTokens?: number;
65
+ /** Reasoning tokens, where the provider bills them separately from output. */
66
+ reasoningTokens?: number;
67
+ /** Input tokens served from a provider-side cache, where reported. */
68
+ cachedInputTokens?: number;
69
+ totalTokens?: number;
70
+ }
71
+ /**
72
+ * One normalized event from a streamed response.
73
+ *
74
+ * `block-start` is optional in practice — several providers begin a block with its first
75
+ * delta — so consumers must tolerate a delta for an index they have not seen.
76
+ * {@link createStreamAccumulator} does.
77
+ */
78
+ export type StreamChunk = {
79
+ type: 'block-start';
80
+ index: number;
81
+ blockType: ContentBlockType;
82
+ } | {
83
+ type: 'text-delta';
84
+ index: number;
85
+ text: string;
86
+ } | {
87
+ type: 'reasoning-delta';
88
+ index: number;
89
+ text: string;
90
+ } | {
91
+ type: 'tool-call-delta';
92
+ index: number;
93
+ id: string;
94
+ name?: string;
95
+ argumentsDelta: string;
96
+ } | {
97
+ type: 'block-end';
98
+ index: number;
99
+ block: ContentBlock;
100
+ } | {
101
+ type: 'usage';
102
+ usage: TokenUsage;
103
+ } | {
104
+ type: 'finish';
105
+ reason: FinishReason;
106
+ };
@@ -1 +1,52 @@
1
- var e=()=>globalThis,t=()=>new Response("Network error happened",{status:408,headers:{"Content-Type":"text/plain"}}),c=(e,t)=>"GET"===e.method&&200===t.status,a=(e,t,c,a)=>{const s=a.clone();return e.caches.open(t).then(e=>e.put(c.url,s)).catch(()=>{})},s=async(s,n)=>{const r=n.scope??e(),o=n.shouldCache??c;try{const e=await r.caches.match(s.url);if(e)return e;const t=await r.fetch(s);return o(s,t)&&a(r,n.cacheName,s,t),t}catch{return t()}},n=async(s,n)=>{const r=n.scope??e(),o=n.shouldCache??c;try{const e=await r.fetch(s);return o(s,e)&&a(r,n.cacheName,s,e),e}catch{return await r.caches.match(s.url).catch(()=>{})??t()}},r=async(t,c,a={})=>{const s=a.scope??e(),n=await s.caches.open(t);for(const e of c)try{if(await n.match(e))continue;const t=await s.fetch(e,{cache:"force-cache"});t.ok&&await n.put(e,t.clone())}catch{}},o=async(t,c={})=>{const a=c.scope??e(),s=(await a.caches.keys()).filter(e=>!t.includes(e));return await Promise.all(s.map(e=>a.caches.delete(e))),s},h=t=>{const c=t.scope??e(),a=e=>{const a=e.data;a&&a.type===t.type&&Array.isArray(a.urls)&&e.waitUntil(r(t.cacheName,a.urls,{scope:c}))};return c.addEventListener("message",a),()=>c.removeEventListener("message",a)};export{s as cacheFirst,o as dropCachesExcept,n as networkFirst,r as precache,h as servePrecache};
1
+ var n = () => globalThis, h = () => new Response("Network error happened", {
2
+ status: 408,
3
+ headers: { "Content-Type": "text/plain" }
4
+ }), l = (c, a) => c.method === "GET" && a.status === 200, p = (c, a, t, s) => {
5
+ const e = s.clone();
6
+ return c.caches.open(a).then((r) => r.put(t.url, e)).catch(() => {
7
+ });
8
+ }, u = async (c, a) => {
9
+ const t = a.scope ?? n(), s = a.shouldCache ?? l;
10
+ try {
11
+ const e = await t.caches.open(a.cacheName).then((o) => o.match(c.url)).catch(() => {
12
+ });
13
+ if (e) return e;
14
+ const r = await t.fetch(c);
15
+ return s(c, r) && p(t, a.cacheName, c, r), r;
16
+ } catch {
17
+ return h();
18
+ }
19
+ }, d = async (c, a) => {
20
+ const t = a.scope ?? n(), s = a.shouldCache ?? l;
21
+ try {
22
+ const e = await t.fetch(c);
23
+ return s(c, e) && p(t, a.cacheName, c, e), e;
24
+ } catch {
25
+ return await t.caches.open(a.cacheName).then((e) => e.match(c.url)).catch(() => {
26
+ }) ?? h();
27
+ }
28
+ }, i = async (c, a, t = {}) => {
29
+ const s = t.scope ?? n(), e = await s.caches.open(c);
30
+ for (const r of a) try {
31
+ if (await e.match(r)) continue;
32
+ const o = await s.fetch(r, { cache: "force-cache" });
33
+ o.ok && await e.put(r, o.clone());
34
+ } catch {
35
+ }
36
+ }, f = async (c, a = {}) => {
37
+ const t = a.scope ?? n(), s = (await t.caches.keys()).filter((e) => !c.includes(e));
38
+ return await Promise.all(s.map((e) => t.caches.delete(e))), s;
39
+ }, m = (c) => {
40
+ const a = c.scope ?? n(), t = (s) => {
41
+ const e = s.data;
42
+ !e || e.type !== c.type || !Array.isArray(e.urls) || s.waitUntil(i(c.cacheName, e.urls, { scope: a }));
43
+ };
44
+ return a.addEventListener("message", t), () => a.removeEventListener("message", t);
45
+ };
46
+ export {
47
+ u as cacheFirst,
48
+ f as dropCachesExcept,
49
+ d as networkFirst,
50
+ i as precache,
51
+ m as servePrecache
52
+ };
@@ -0,0 +1,49 @@
1
+ var c = () => {
2
+ let e, r;
3
+ return {
4
+ promise: new Promise((o, i) => {
5
+ e = o, r = i;
6
+ }),
7
+ resolve: e,
8
+ reject: r
9
+ };
10
+ }, s = class extends Error {
11
+ constructor(e) {
12
+ super(e), this.name = "TimeoutError";
13
+ }
14
+ }, T = (e, r, o = {}) => {
15
+ const { message: i = `operation timed out after ${r}ms`, onTimeout: m } = o;
16
+ return new Promise((u, a) => {
17
+ const t = setTimeout(() => {
18
+ m?.(), a(new s(i));
19
+ }, r);
20
+ e.then((n) => {
21
+ clearTimeout(t), u(n);
22
+ }, (n) => {
23
+ clearTimeout(t), a(n);
24
+ });
25
+ });
26
+ }, l = (e, r, o, i) => new Promise((m, u) => {
27
+ const a = setTimeout(() => {
28
+ i?.(), m(o);
29
+ }, r);
30
+ e.then((t) => {
31
+ clearTimeout(a), m(t);
32
+ }, (t) => {
33
+ clearTimeout(a), u(t);
34
+ });
35
+ }), w = (e) => new Promise((r) => setTimeout(r, e)), d = () => {
36
+ let e = 0;
37
+ return {
38
+ start: () => ++e,
39
+ isCurrent: (r) => r === e
40
+ };
41
+ };
42
+ export {
43
+ s as TimeoutError,
44
+ d as createRaceGuard,
45
+ c as deferred,
46
+ w as delay,
47
+ T as withTimeout,
48
+ l as withTimeoutFallback
49
+ };