effect 4.1.3 → 4.1.5

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 (782) hide show
  1. package/THIRD_PARTY_NOTICES.md +88 -0
  2. package/ai-docs/package.json +2 -2
  3. package/dist/Arbitrary.d.ts +44 -2
  4. package/dist/Arbitrary.d.ts.map +1 -1
  5. package/dist/Arbitrary.js +9 -1
  6. package/dist/Arbitrary.js.map +1 -1
  7. package/dist/BigInt.d.ts +200 -1
  8. package/dist/BigInt.d.ts.map +1 -1
  9. package/dist/BigInt.js +105 -9
  10. package/dist/BigInt.js.map +1 -1
  11. package/dist/Cache.d.ts.map +1 -1
  12. package/dist/Cache.js +4 -2
  13. package/dist/Cache.js.map +1 -1
  14. package/dist/Cause.d.ts +3 -3
  15. package/dist/Cause.js +1 -1
  16. package/dist/Chunk.d.ts +5 -0
  17. package/dist/Chunk.d.ts.map +1 -1
  18. package/dist/Chunk.js +7 -2
  19. package/dist/Chunk.js.map +1 -1
  20. package/dist/Clock.d.ts +5 -2
  21. package/dist/Clock.d.ts.map +1 -1
  22. package/dist/Clock.js +2 -1
  23. package/dist/Clock.js.map +1 -1
  24. package/dist/Codegen.d.ts +295 -0
  25. package/dist/Codegen.d.ts.map +1 -0
  26. package/dist/Codegen.js +521 -0
  27. package/dist/Codegen.js.map +1 -0
  28. package/dist/Config.d.ts +31 -2
  29. package/dist/Config.d.ts.map +1 -1
  30. package/dist/Config.js +12 -1
  31. package/dist/Config.js.map +1 -1
  32. package/dist/ConfigProvider.d.ts +11 -1
  33. package/dist/ConfigProvider.d.ts.map +1 -1
  34. package/dist/ConfigProvider.js +17 -6
  35. package/dist/ConfigProvider.js.map +1 -1
  36. package/dist/Effect.d.ts +869 -109
  37. package/dist/Effect.d.ts.map +1 -1
  38. package/dist/Effect.js +400 -47
  39. package/dist/Effect.js.map +1 -1
  40. package/dist/Equivalence.d.ts +11 -9
  41. package/dist/Equivalence.d.ts.map +1 -1
  42. package/dist/Equivalence.js +15 -12
  43. package/dist/Equivalence.js.map +1 -1
  44. package/dist/Fiber.d.ts +58 -8
  45. package/dist/Fiber.d.ts.map +1 -1
  46. package/dist/Fiber.js +28 -4
  47. package/dist/Fiber.js.map +1 -1
  48. package/dist/FiberHandle.d.ts +81 -10
  49. package/dist/FiberHandle.d.ts.map +1 -1
  50. package/dist/FiberHandle.js +40 -8
  51. package/dist/FiberHandle.js.map +1 -1
  52. package/dist/FiberMap.d.ts +80 -10
  53. package/dist/FiberMap.d.ts.map +1 -1
  54. package/dist/FiberMap.js +37 -6
  55. package/dist/FiberMap.js.map +1 -1
  56. package/dist/FiberSet.d.ts +93 -8
  57. package/dist/FiberSet.d.ts.map +1 -1
  58. package/dist/FiberSet.js +46 -4
  59. package/dist/FiberSet.js.map +1 -1
  60. package/dist/Formatter.d.ts +2 -0
  61. package/dist/Formatter.d.ts.map +1 -1
  62. package/dist/Formatter.js +2 -0
  63. package/dist/Formatter.js.map +1 -1
  64. package/dist/LayerMap.d.ts +12 -3
  65. package/dist/LayerMap.d.ts.map +1 -1
  66. package/dist/LayerMap.js +12 -2
  67. package/dist/LayerMap.js.map +1 -1
  68. package/dist/LayerRef.d.ts +2 -2
  69. package/dist/Match.d.ts +12 -0
  70. package/dist/Match.d.ts.map +1 -1
  71. package/dist/Match.js +16 -0
  72. package/dist/Match.js.map +1 -1
  73. package/dist/Metric.js +1 -1
  74. package/dist/Metric.js.map +1 -1
  75. package/dist/Option.d.ts +4 -1
  76. package/dist/Option.d.ts.map +1 -1
  77. package/dist/Option.js +6 -2
  78. package/dist/Option.js.map +1 -1
  79. package/dist/Order.d.ts +7 -5
  80. package/dist/Order.d.ts.map +1 -1
  81. package/dist/Order.js +7 -2
  82. package/dist/Order.js.map +1 -1
  83. package/dist/Pool.d.ts +4 -3
  84. package/dist/Pool.d.ts.map +1 -1
  85. package/dist/Pool.js +21 -18
  86. package/dist/Pool.js.map +1 -1
  87. package/dist/Predicate.d.ts +4 -1
  88. package/dist/Predicate.d.ts.map +1 -1
  89. package/dist/Predicate.js +5 -1
  90. package/dist/Predicate.js.map +1 -1
  91. package/dist/Queue.d.ts +69 -22
  92. package/dist/Queue.d.ts.map +1 -1
  93. package/dist/Queue.js +40 -18
  94. package/dist/Queue.js.map +1 -1
  95. package/dist/Rational.d.ts +179 -0
  96. package/dist/Rational.d.ts.map +1 -1
  97. package/dist/Rational.js +63 -0
  98. package/dist/Rational.js.map +1 -1
  99. package/dist/Request.d.ts +20 -0
  100. package/dist/Request.d.ts.map +1 -1
  101. package/dist/Request.js +20 -0
  102. package/dist/Request.js.map +1 -1
  103. package/dist/Result.d.ts +3 -1
  104. package/dist/Result.d.ts.map +1 -1
  105. package/dist/Result.js +5 -2
  106. package/dist/Result.js.map +1 -1
  107. package/dist/Schema.d.ts +49 -19
  108. package/dist/Schema.d.ts.map +1 -1
  109. package/dist/Schema.js +44 -27
  110. package/dist/Schema.js.map +1 -1
  111. package/dist/SchemaAST.d.ts.map +1 -1
  112. package/dist/SchemaAST.js +11 -4
  113. package/dist/SchemaAST.js.map +1 -1
  114. package/dist/SchemaGetter.d.ts +5 -6
  115. package/dist/SchemaGetter.d.ts.map +1 -1
  116. package/dist/SchemaGetter.js +14 -10
  117. package/dist/SchemaGetter.js.map +1 -1
  118. package/dist/SchemaRepresentation.d.ts +36 -67
  119. package/dist/SchemaRepresentation.d.ts.map +1 -1
  120. package/dist/SchemaRepresentation.js +5 -30
  121. package/dist/SchemaRepresentation.js.map +1 -1
  122. package/dist/SchemaTransformation.d.ts +11 -10
  123. package/dist/SchemaTransformation.d.ts.map +1 -1
  124. package/dist/SchemaTransformation.js +12 -11
  125. package/dist/SchemaTransformation.js.map +1 -1
  126. package/dist/Scope.d.ts +12 -6
  127. package/dist/Scope.d.ts.map +1 -1
  128. package/dist/Scope.js +12 -6
  129. package/dist/Scope.js.map +1 -1
  130. package/dist/ScopedCache.d.ts.map +1 -1
  131. package/dist/ScopedCache.js +2 -1
  132. package/dist/ScopedCache.js.map +1 -1
  133. package/dist/Semaphore.d.ts.map +1 -1
  134. package/dist/Semaphore.js +18 -4
  135. package/dist/Semaphore.js.map +1 -1
  136. package/dist/Stream.d.ts.map +1 -1
  137. package/dist/Stream.js +55 -14
  138. package/dist/Stream.js.map +1 -1
  139. package/dist/String.d.ts +57 -3
  140. package/dist/String.d.ts.map +1 -1
  141. package/dist/String.js +62 -4
  142. package/dist/String.js.map +1 -1
  143. package/dist/Struct.d.ts +135 -1
  144. package/dist/Struct.d.ts.map +1 -1
  145. package/dist/Struct.js +64 -5
  146. package/dist/Struct.js.map +1 -1
  147. package/dist/TextDiff.d.ts +105 -0
  148. package/dist/TextDiff.d.ts.map +1 -1
  149. package/dist/TextDiff.js +137 -24
  150. package/dist/TextDiff.js.map +1 -1
  151. package/dist/TxPriorityQueue.js +2 -2
  152. package/dist/TxPriorityQueue.js.map +1 -1
  153. package/dist/ai/Chat.d.ts +1 -1
  154. package/dist/ai/Chat.d.ts.map +1 -1
  155. package/dist/ai/Chat.js.map +1 -1
  156. package/dist/ai/LanguageModel.d.ts +23 -4
  157. package/dist/ai/LanguageModel.d.ts.map +1 -1
  158. package/dist/ai/LanguageModel.js +9 -5
  159. package/dist/ai/LanguageModel.js.map +1 -1
  160. package/dist/ai/McpClient.d.ts +0 -5
  161. package/dist/ai/McpClient.d.ts.map +1 -1
  162. package/dist/ai/McpClient.js +1 -11
  163. package/dist/ai/McpClient.js.map +1 -1
  164. package/dist/ai/McpSchema.d.ts +128 -128
  165. package/dist/ai/McpSchema.d.ts.map +1 -1
  166. package/dist/ai/McpServer.d.ts +48 -5
  167. package/dist/ai/McpServer.d.ts.map +1 -1
  168. package/dist/ai/McpServer.js +136 -19
  169. package/dist/ai/McpServer.js.map +1 -1
  170. package/dist/ai/Tool.d.ts +3 -2
  171. package/dist/ai/Tool.d.ts.map +1 -1
  172. package/dist/ai/Tool.js +3 -2
  173. package/dist/ai/Tool.js.map +1 -1
  174. package/dist/ai/Toolkit.d.ts.map +1 -1
  175. package/dist/ai/Toolkit.js +5 -1
  176. package/dist/ai/Toolkit.js.map +1 -1
  177. package/dist/ai/WorkloadIdentity.d.ts +111 -0
  178. package/dist/ai/WorkloadIdentity.d.ts.map +1 -0
  179. package/dist/ai/WorkloadIdentity.js +227 -0
  180. package/dist/ai/WorkloadIdentity.js.map +1 -0
  181. package/dist/ai/index.d.ts +5 -0
  182. package/dist/ai/index.d.ts.map +1 -1
  183. package/dist/ai/index.js +5 -0
  184. package/dist/ai/index.js.map +1 -1
  185. package/dist/ai/internal/mcpProtocol/v2024_11_05.js +3 -1
  186. package/dist/ai/internal/mcpProtocol/v2024_11_05.js.map +1 -1
  187. package/dist/ai/internal/mcpProtocol/v2025_03_26.js +3 -1
  188. package/dist/ai/internal/mcpProtocol/v2025_03_26.js.map +1 -1
  189. package/dist/ai/internal/mcpProtocol/v2025_06_18.js +3 -1
  190. package/dist/ai/internal/mcpProtocol/v2025_06_18.js.map +1 -1
  191. package/dist/ai/internal/mcpProtocol/v2025_11_25.js +3 -1
  192. package/dist/ai/internal/mcpProtocol/v2025_11_25.js.map +1 -1
  193. package/dist/ai/internal/mcpRuntime.js +2 -4
  194. package/dist/ai/internal/mcpRuntime.js.map +1 -1
  195. package/dist/ai/internal/mcpSkills.js +80 -2
  196. package/dist/ai/internal/mcpSkills.js.map +1 -1
  197. package/dist/ai/internal/structured-output.js +5 -1
  198. package/dist/ai/internal/structured-output.js.map +1 -1
  199. package/dist/chat/Chat.d.ts +90 -0
  200. package/dist/chat/Chat.d.ts.map +1 -0
  201. package/dist/chat/Chat.js +127 -0
  202. package/dist/chat/Chat.js.map +1 -0
  203. package/dist/chat/ChatAdapter.d.ts +386 -0
  204. package/dist/chat/ChatAdapter.d.ts.map +1 -0
  205. package/dist/chat/ChatAdapter.js +110 -0
  206. package/dist/chat/ChatAdapter.js.map +1 -0
  207. package/dist/chat/ChatError.d.ts +500 -0
  208. package/dist/chat/ChatError.d.ts.map +1 -0
  209. package/dist/chat/ChatError.js +521 -0
  210. package/dist/chat/ChatError.js.map +1 -0
  211. package/dist/chat/ChatEvent.d.ts +321 -0
  212. package/dist/chat/ChatEvent.d.ts.map +1 -0
  213. package/dist/chat/ChatEvent.js +163 -0
  214. package/dist/chat/ChatEvent.js.map +1 -0
  215. package/dist/chat/ChatHandler.d.ts +81 -0
  216. package/dist/chat/ChatHandler.d.ts.map +1 -0
  217. package/dist/chat/ChatHandler.js +85 -0
  218. package/dist/chat/ChatHandler.js.map +1 -0
  219. package/dist/chat/ChatInteraction.d.ts +1519 -0
  220. package/dist/chat/ChatInteraction.d.ts.map +1 -0
  221. package/dist/chat/ChatInteraction.js +1052 -0
  222. package/dist/chat/ChatInteraction.js.map +1 -0
  223. package/dist/chat/ChatMarkdown.d.ts +280 -0
  224. package/dist/chat/ChatMarkdown.d.ts.map +1 -0
  225. package/dist/chat/ChatMarkdown.js +1172 -0
  226. package/dist/chat/ChatMarkdown.js.map +1 -0
  227. package/dist/chat/ChatMessage.d.ts +224 -0
  228. package/dist/chat/ChatMessage.d.ts.map +1 -0
  229. package/dist/chat/ChatMessage.js +172 -0
  230. package/dist/chat/ChatMessage.js.map +1 -0
  231. package/dist/chat/ChatTest.d.ts +407 -0
  232. package/dist/chat/ChatTest.d.ts.map +1 -0
  233. package/dist/chat/ChatTest.js +2145 -0
  234. package/dist/chat/ChatTest.js.map +1 -0
  235. package/dist/chat/ChatThread.d.ts +92 -0
  236. package/dist/chat/ChatThread.d.ts.map +1 -0
  237. package/dist/chat/ChatThread.js +116 -0
  238. package/dist/chat/ChatThread.js.map +1 -0
  239. package/dist/chat/ChatUi.d.ts +362 -0
  240. package/dist/chat/ChatUi.d.ts.map +1 -0
  241. package/dist/chat/ChatUi.js +209 -0
  242. package/dist/chat/ChatUi.js.map +1 -0
  243. package/dist/chat/index.d.ts +60 -0
  244. package/dist/chat/index.d.ts.map +1 -0
  245. package/dist/chat/index.js +61 -0
  246. package/dist/chat/index.js.map +1 -0
  247. package/dist/chat/internal/retries.d.ts +2 -0
  248. package/dist/chat/internal/retries.d.ts.map +1 -0
  249. package/dist/chat/internal/retries.js +7 -0
  250. package/dist/chat/internal/retries.js.map +1 -0
  251. package/dist/cli/CliOutput.d.ts.map +1 -1
  252. package/dist/cli/CliOutput.js +6 -7
  253. package/dist/cli/CliOutput.js.map +1 -1
  254. package/dist/cli/Command.d.ts +20 -2
  255. package/dist/cli/Command.d.ts.map +1 -1
  256. package/dist/cli/Command.js +7 -1
  257. package/dist/cli/Command.js.map +1 -1
  258. package/dist/cli/Prompt.d.ts +5 -6
  259. package/dist/cli/Prompt.d.ts.map +1 -1
  260. package/dist/cli/Prompt.js +9 -4
  261. package/dist/cli/Prompt.js.map +1 -1
  262. package/dist/cli/internal/command.d.ts +1 -1
  263. package/dist/cli/internal/command.d.ts.map +1 -1
  264. package/dist/cli/internal/config.d.ts +1 -1
  265. package/dist/cli/internal/config.d.ts.map +1 -1
  266. package/dist/cli/internal/config.js +3 -3
  267. package/dist/cli/internal/config.js.map +1 -1
  268. package/dist/cli/internal/lexer.js +13 -0
  269. package/dist/cli/internal/lexer.js.map +1 -1
  270. package/dist/cli/internal/parser.js +23 -16
  271. package/dist/cli/internal/parser.js.map +1 -1
  272. package/dist/cli/internal/wizard.d.ts.map +1 -1
  273. package/dist/cli/internal/wizard.js +4 -0
  274. package/dist/cli/internal/wizard.js.map +1 -1
  275. package/dist/cluster/ClusterCron.d.ts +21 -1
  276. package/dist/cluster/ClusterCron.d.ts.map +1 -1
  277. package/dist/cluster/ClusterCron.js +24 -6
  278. package/dist/cluster/ClusterCron.js.map +1 -1
  279. package/dist/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
  280. package/dist/cluster/ClusterWorkflowEngine.js +30 -22
  281. package/dist/cluster/ClusterWorkflowEngine.js.map +1 -1
  282. package/dist/cluster/Sharding.d.ts.map +1 -1
  283. package/dist/cluster/Sharding.js +15 -3
  284. package/dist/cluster/Sharding.js.map +1 -1
  285. package/dist/cluster/ShardingConfig.d.ts +6 -0
  286. package/dist/cluster/ShardingConfig.d.ts.map +1 -1
  287. package/dist/cluster/ShardingConfig.js.map +1 -1
  288. package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
  289. package/dist/cluster/SqlMessageStorage.js +13 -15
  290. package/dist/cluster/SqlMessageStorage.js.map +1 -1
  291. package/dist/cluster/SqlRunnerStorage.d.ts.map +1 -1
  292. package/dist/cluster/SqlRunnerStorage.js +18 -8
  293. package/dist/cluster/SqlRunnerStorage.js.map +1 -1
  294. package/dist/cluster/internal/entityManager.js +12 -8
  295. package/dist/cluster/internal/entityManager.js.map +1 -1
  296. package/dist/cluster/internal/entityReaper.js +1 -1
  297. package/dist/cluster/internal/entityReaper.js.map +1 -1
  298. package/dist/crypto/Kdf.d.ts +2 -1
  299. package/dist/crypto/Kdf.d.ts.map +1 -1
  300. package/dist/crypto/Kdf.js +2 -1
  301. package/dist/crypto/Kdf.js.map +1 -1
  302. package/dist/crypto/Password.d.ts +162 -0
  303. package/dist/crypto/Password.d.ts.map +1 -0
  304. package/dist/crypto/Password.js +217 -0
  305. package/dist/crypto/Password.js.map +1 -0
  306. package/dist/crypto/index.d.ts +5 -0
  307. package/dist/crypto/index.d.ts.map +1 -1
  308. package/dist/crypto/index.js +5 -0
  309. package/dist/crypto/index.js.map +1 -1
  310. package/dist/encoding/Base64.d.ts +63 -0
  311. package/dist/encoding/Base64.d.ts.map +1 -1
  312. package/dist/encoding/Base64.js +77 -0
  313. package/dist/encoding/Base64.js.map +1 -1
  314. package/dist/encoding/Base64Url.d.ts +1 -0
  315. package/dist/encoding/Base64Url.d.ts.map +1 -1
  316. package/dist/encoding/Base64Url.js +1 -0
  317. package/dist/encoding/Base64Url.js.map +1 -1
  318. package/dist/encoding/Csv.d.ts.map +1 -1
  319. package/dist/encoding/Csv.js +44 -16
  320. package/dist/encoding/Csv.js.map +1 -1
  321. package/dist/encoding/Hex.d.ts +5 -0
  322. package/dist/encoding/Hex.d.ts.map +1 -1
  323. package/dist/encoding/Hex.js +5 -0
  324. package/dist/encoding/Hex.js.map +1 -1
  325. package/dist/encoding/SchemaBinary.d.ts.map +1 -1
  326. package/dist/encoding/SchemaBinary.js +13 -124
  327. package/dist/encoding/SchemaBinary.js.map +1 -1
  328. package/dist/encoding/Sse.d.ts +1 -0
  329. package/dist/encoding/Sse.d.ts.map +1 -1
  330. package/dist/encoding/Sse.js +7 -5
  331. package/dist/encoding/Sse.js.map +1 -1
  332. package/dist/encoding/Xml.d.ts +117 -11
  333. package/dist/encoding/Xml.d.ts.map +1 -1
  334. package/dist/encoding/Xml.js +332 -42
  335. package/dist/encoding/Xml.js.map +1 -1
  336. package/dist/eventlog/EventJournal.d.ts.map +1 -1
  337. package/dist/eventlog/EventJournal.js +3 -8
  338. package/dist/eventlog/EventJournal.js.map +1 -1
  339. package/dist/eventlog/SqlEventLogServerEncrypted.js +6 -8
  340. package/dist/eventlog/SqlEventLogServerEncrypted.js.map +1 -1
  341. package/dist/http/FindMyWay/internal/router.js +163 -116
  342. package/dist/http/FindMyWay/internal/router.js.map +1 -1
  343. package/dist/http/FindMyWay.d.ts +99 -0
  344. package/dist/http/FindMyWay.d.ts.map +1 -1
  345. package/dist/http/FindMyWay.js +39 -0
  346. package/dist/http/FindMyWay.js.map +1 -1
  347. package/dist/http/HttpClient.d.ts.map +1 -1
  348. package/dist/http/HttpClient.js +1 -1
  349. package/dist/http/HttpClient.js.map +1 -1
  350. package/dist/http/HttpMiddleware.d.ts.map +1 -1
  351. package/dist/http/HttpMiddleware.js +18 -9
  352. package/dist/http/HttpMiddleware.js.map +1 -1
  353. package/dist/http/HttpRouter.d.ts +49 -1
  354. package/dist/http/HttpRouter.d.ts.map +1 -1
  355. package/dist/http/HttpRouter.js +70 -20
  356. package/dist/http/HttpRouter.js.map +1 -1
  357. package/dist/http/HttpServerResponse.d.ts.map +1 -1
  358. package/dist/http/HttpServerResponse.js +24 -58
  359. package/dist/http/HttpServerResponse.js.map +1 -1
  360. package/dist/http/Mime.d.ts.map +1 -1
  361. package/dist/http/Mime.js +3 -2
  362. package/dist/http/Mime.js.map +1 -1
  363. package/dist/http/internal/compression.d.ts.map +1 -1
  364. package/dist/http/internal/compression.js +2 -2
  365. package/dist/http/internal/compression.js.map +1 -1
  366. package/dist/http/internal/mediaType.d.ts.map +1 -0
  367. package/dist/{http-api → http}/internal/mediaType.js +4 -0
  368. package/dist/{http-api → http}/internal/mediaType.js.map +1 -1
  369. package/dist/http/internal/redactedPath.d.ts +2 -0
  370. package/dist/http/internal/redactedPath.d.ts.map +1 -0
  371. package/dist/http/internal/redactedPath.js +138 -0
  372. package/dist/http/internal/redactedPath.js.map +1 -0
  373. package/dist/http/internal/requestState.js +16 -0
  374. package/dist/http/internal/requestState.js.map +1 -1
  375. package/dist/http-api/HttpApi.d.ts +58 -0
  376. package/dist/http-api/HttpApi.d.ts.map +1 -1
  377. package/dist/http-api/HttpApi.js +80 -0
  378. package/dist/http-api/HttpApi.js.map +1 -1
  379. package/dist/http-api/HttpApiBuilder.d.ts.map +1 -1
  380. package/dist/http-api/HttpApiBuilder.js +12 -3
  381. package/dist/http-api/HttpApiBuilder.js.map +1 -1
  382. package/dist/http-api/HttpApiClient.d.ts +1 -1
  383. package/dist/http-api/HttpApiClient.d.ts.map +1 -1
  384. package/dist/http-api/HttpApiClient.js +11 -7
  385. package/dist/http-api/HttpApiClient.js.map +1 -1
  386. package/dist/http-api/HttpApiEndpoint.d.ts.map +1 -1
  387. package/dist/http-api/HttpApiEndpoint.js +1 -1
  388. package/dist/http-api/HttpApiEndpoint.js.map +1 -1
  389. package/dist/http-api/HttpApiScalar.d.ts +4 -0
  390. package/dist/http-api/HttpApiScalar.d.ts.map +1 -1
  391. package/dist/http-api/HttpApiScalar.js +10 -1
  392. package/dist/http-api/HttpApiScalar.js.map +1 -1
  393. package/dist/http-api/HttpApiSchema.d.ts +66 -0
  394. package/dist/http-api/HttpApiSchema.d.ts.map +1 -1
  395. package/dist/http-api/HttpApiSchema.js +67 -2
  396. package/dist/http-api/HttpApiSchema.js.map +1 -1
  397. package/dist/http-api/OpenApi.d.ts +2 -0
  398. package/dist/http-api/OpenApi.d.ts.map +1 -1
  399. package/dist/http-api/internal/httpApiScalar.js +5 -2
  400. package/dist/http-api/internal/httpApiScalar.js.map +1 -1
  401. package/dist/index.d.ts +5 -0
  402. package/dist/index.d.ts.map +1 -1
  403. package/dist/index.js +5 -0
  404. package/dist/index.js.map +1 -1
  405. package/dist/internal/arbitrary/model.js +0 -5
  406. package/dist/internal/arbitrary/model.js.map +1 -1
  407. package/dist/internal/arbitrary/regexp.js +2 -1
  408. package/dist/internal/arbitrary/regexp.js.map +1 -1
  409. package/dist/internal/arbitrary/runner.d.ts.map +1 -1
  410. package/dist/internal/arbitrary/runner.js +21 -16
  411. package/dist/internal/arbitrary/runner.js.map +1 -1
  412. package/dist/internal/arbitrary/schema.js +140 -23
  413. package/dist/internal/arbitrary/schema.js.map +1 -1
  414. package/dist/internal/codegen.d.ts +2 -0
  415. package/dist/internal/codegen.d.ts.map +1 -0
  416. package/dist/internal/codegen.js +59 -0
  417. package/dist/internal/codegen.js.map +1 -0
  418. package/dist/internal/core.js +3 -3
  419. package/dist/internal/core.js.map +1 -1
  420. package/dist/internal/dateTime.js +26 -12
  421. package/dist/internal/dateTime.js.map +1 -1
  422. package/dist/internal/effect.js +21 -7
  423. package/dist/internal/effect.js.map +1 -1
  424. package/dist/internal/hashMap.js +7 -7
  425. package/dist/internal/hashMap.js.map +1 -1
  426. package/dist/internal/matcher.js +2 -1
  427. package/dist/internal/matcher.js.map +1 -1
  428. package/dist/internal/record.js +18 -4
  429. package/dist/internal/record.js.map +1 -1
  430. package/dist/internal/result.js +1 -1
  431. package/dist/internal/result.js.map +1 -1
  432. package/dist/internal/schema/codeExpression.d.ts +4 -0
  433. package/dist/internal/schema/codeExpression.d.ts.map +1 -0
  434. package/dist/internal/schema/codeExpression.js +179 -0
  435. package/dist/internal/schema/codeExpression.js.map +1 -0
  436. package/dist/internal/schema/codeImports.d.ts +8 -0
  437. package/dist/internal/schema/codeImports.d.ts.map +1 -0
  438. package/dist/internal/schema/codeImports.js +135 -0
  439. package/dist/internal/schema/codeImports.js.map +1 -0
  440. package/dist/internal/schema/codeTokens.d.ts +8 -0
  441. package/dist/internal/schema/codeTokens.d.ts.map +1 -0
  442. package/dist/internal/schema/codeTokens.js +17 -0
  443. package/dist/internal/schema/codeTokens.js.map +1 -0
  444. package/dist/internal/schema/fromJsonSchemaDocument.js +141 -35
  445. package/dist/internal/schema/fromJsonSchemaDocument.js.map +1 -1
  446. package/dist/internal/schema/passthrough.d.ts +2 -0
  447. package/dist/internal/schema/passthrough.d.ts.map +1 -0
  448. package/dist/internal/schema/passthrough.js +8 -0
  449. package/dist/internal/schema/passthrough.js.map +1 -0
  450. package/dist/internal/schema/schemaCodegen.d.ts +2 -0
  451. package/dist/internal/schema/schemaCodegen.d.ts.map +1 -0
  452. package/dist/internal/schema/schemaCodegen.js +377 -0
  453. package/dist/internal/schema/schemaCodegen.js.map +1 -0
  454. package/dist/internal/schema/toJsonSchemaDocument.js +11 -4
  455. package/dist/internal/schema/toJsonSchemaDocument.js.map +1 -1
  456. package/dist/internal/trie.js +51 -64
  457. package/dist/internal/trie.js.map +1 -1
  458. package/dist/internal/utf8.d.ts +2 -0
  459. package/dist/internal/utf8.d.ts.map +1 -0
  460. package/dist/internal/utf8.js +151 -0
  461. package/dist/internal/utf8.js.map +1 -0
  462. package/dist/internal/version.js +1 -1
  463. package/dist/jose/Jwks.d.ts +13 -0
  464. package/dist/jose/Jwks.d.ts.map +1 -1
  465. package/dist/jose/Jwks.js +10 -2
  466. package/dist/jose/Jwks.js.map +1 -1
  467. package/dist/observability/OtlpExporter.d.ts.map +1 -1
  468. package/dist/observability/OtlpExporter.js +12 -10
  469. package/dist/observability/OtlpExporter.js.map +1 -1
  470. package/dist/observability/OtlpLogger.d.ts.map +1 -1
  471. package/dist/observability/OtlpLogger.js +5 -3
  472. package/dist/observability/OtlpLogger.js.map +1 -1
  473. package/dist/observability/internal/otlpProtobuf.d.ts +4 -2
  474. package/dist/observability/internal/otlpProtobuf.d.ts.map +1 -1
  475. package/dist/observability/internal/otlpProtobuf.js +238 -156
  476. package/dist/observability/internal/otlpProtobuf.js.map +1 -1
  477. package/dist/persistence/PersistedQueue.d.ts.map +1 -1
  478. package/dist/persistence/PersistedQueue.js +20 -3
  479. package/dist/persistence/PersistedQueue.js.map +1 -1
  480. package/dist/protobuf/ProtobufDescriptor.d.ts +377 -0
  481. package/dist/protobuf/ProtobufDescriptor.d.ts.map +1 -0
  482. package/dist/protobuf/ProtobufDescriptor.js +135 -0
  483. package/dist/protobuf/ProtobufDescriptor.js.map +1 -0
  484. package/dist/protobuf/ProtobufDescriptorProto.d.ts +2083 -0
  485. package/dist/protobuf/ProtobufDescriptorProto.d.ts.map +1 -0
  486. package/dist/protobuf/ProtobufDescriptorProto.js +2674 -0
  487. package/dist/protobuf/ProtobufDescriptorProto.js.map +1 -0
  488. package/dist/protobuf/ProtobufMessage.d.ts +183 -0
  489. package/dist/protobuf/ProtobufMessage.d.ts.map +1 -0
  490. package/dist/protobuf/ProtobufMessage.js +232 -0
  491. package/dist/protobuf/ProtobufMessage.js.map +1 -0
  492. package/dist/protobuf/ProtobufReader.d.ts +350 -0
  493. package/dist/protobuf/ProtobufReader.d.ts.map +1 -0
  494. package/dist/protobuf/ProtobufReader.js +698 -0
  495. package/dist/protobuf/ProtobufReader.js.map +1 -0
  496. package/dist/protobuf/ProtobufWriter.d.ts +224 -0
  497. package/dist/protobuf/ProtobufWriter.d.ts.map +1 -0
  498. package/dist/protobuf/ProtobufWriter.js +483 -0
  499. package/dist/protobuf/ProtobufWriter.js.map +1 -0
  500. package/dist/protobuf/index.d.ts +30 -0
  501. package/dist/protobuf/index.d.ts.map +1 -0
  502. package/dist/protobuf/index.js +31 -0
  503. package/dist/protobuf/index.js.map +1 -0
  504. package/dist/protobuf/internal/binary.d.ts +2 -0
  505. package/dist/protobuf/internal/binary.d.ts.map +1 -0
  506. package/dist/protobuf/internal/binary.js +966 -0
  507. package/dist/protobuf/internal/binary.js.map +1 -0
  508. package/dist/protobuf/internal/resolve.d.ts +2 -0
  509. package/dist/protobuf/internal/resolve.d.ts.map +1 -0
  510. package/dist/protobuf/internal/resolve.js +463 -0
  511. package/dist/protobuf/internal/resolve.js.map +1 -0
  512. package/dist/protobuf/internal/wireType.d.ts +2 -0
  513. package/dist/protobuf/internal/wireType.d.ts.map +1 -0
  514. package/dist/protobuf/internal/wireType.js +10 -0
  515. package/dist/protobuf/internal/wireType.js.map +1 -0
  516. package/dist/reactivity/AsyncResult.d.ts +3 -0
  517. package/dist/reactivity/AsyncResult.d.ts.map +1 -1
  518. package/dist/reactivity/AsyncResult.js +4 -1
  519. package/dist/reactivity/AsyncResult.js.map +1 -1
  520. package/dist/reactivity/Atom.d.ts +10 -4
  521. package/dist/reactivity/Atom.d.ts.map +1 -1
  522. package/dist/reactivity/Atom.js +38 -13
  523. package/dist/reactivity/Atom.js.map +1 -1
  524. package/dist/reactivity/AtomHttpApi.d.ts +5 -0
  525. package/dist/reactivity/AtomHttpApi.d.ts.map +1 -1
  526. package/dist/reactivity/AtomHttpApi.js +5 -1
  527. package/dist/reactivity/AtomHttpApi.js.map +1 -1
  528. package/dist/reactivity/AtomRegistry.js +13 -0
  529. package/dist/reactivity/AtomRegistry.js.map +1 -1
  530. package/dist/reactivity/AtomRpc.d.ts +5 -0
  531. package/dist/reactivity/AtomRpc.d.ts.map +1 -1
  532. package/dist/reactivity/AtomRpc.js +6 -1
  533. package/dist/reactivity/AtomRpc.js.map +1 -1
  534. package/dist/rpc/Rpc.d.ts +25 -0
  535. package/dist/rpc/Rpc.d.ts.map +1 -1
  536. package/dist/rpc/Rpc.js +18 -0
  537. package/dist/rpc/Rpc.js.map +1 -1
  538. package/dist/rpc/RpcClient.d.ts.map +1 -1
  539. package/dist/rpc/RpcClient.js +38 -21
  540. package/dist/rpc/RpcClient.js.map +1 -1
  541. package/dist/rpc/RpcGroup.d.ts +5 -0
  542. package/dist/rpc/RpcGroup.d.ts.map +1 -1
  543. package/dist/rpc/RpcGroup.js.map +1 -1
  544. package/dist/rpc/RpcMiddleware.d.ts +9 -0
  545. package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
  546. package/dist/rpc/RpcMiddleware.js.map +1 -1
  547. package/dist/rpc/RpcSerialization.d.ts +7 -1
  548. package/dist/rpc/RpcSerialization.d.ts.map +1 -1
  549. package/dist/rpc/RpcSerialization.js +22 -5
  550. package/dist/rpc/RpcSerialization.js.map +1 -1
  551. package/dist/rpc/RpcServer.d.ts.map +1 -1
  552. package/dist/rpc/RpcServer.js +33 -18
  553. package/dist/rpc/RpcServer.js.map +1 -1
  554. package/dist/schema/SchemaCompiler/runtime.js +2 -1
  555. package/dist/schema/SchemaCompiler/runtime.js.map +1 -1
  556. package/dist/schema/VariantSchema.d.ts +10 -2
  557. package/dist/schema/VariantSchema.d.ts.map +1 -1
  558. package/dist/schema/VariantSchema.js +7 -1
  559. package/dist/schema/VariantSchema.js.map +1 -1
  560. package/dist/socket/Socket.d.ts +24 -10
  561. package/dist/socket/Socket.d.ts.map +1 -1
  562. package/dist/socket/Socket.js +52 -17
  563. package/dist/socket/Socket.js.map +1 -1
  564. package/dist/sql/Migrator.d.ts +3 -0
  565. package/dist/sql/Migrator.d.ts.map +1 -1
  566. package/dist/sql/Migrator.js +10 -1
  567. package/dist/sql/Migrator.js.map +1 -1
  568. package/dist/sql/SqlModel.d.ts.map +1 -1
  569. package/dist/sql/SqlModel.js +1 -0
  570. package/dist/sql/SqlModel.js.map +1 -1
  571. package/dist/testing/TestClock.js +3 -1
  572. package/dist/testing/TestClock.js.map +1 -1
  573. package/dist/testing/TestConsole.d.ts +96 -0
  574. package/dist/testing/TestConsole.d.ts.map +1 -1
  575. package/dist/testing/TestConsole.js +78 -6
  576. package/dist/testing/TestConsole.js.map +1 -1
  577. package/dist/testing/TestSchema.d.ts +5 -1
  578. package/dist/testing/TestSchema.d.ts.map +1 -1
  579. package/dist/testing/TestSchema.js +7 -2
  580. package/dist/testing/TestSchema.js.map +1 -1
  581. package/dist/workers/SharedMemory.d.ts +115 -0
  582. package/dist/workers/SharedMemory.d.ts.map +1 -0
  583. package/dist/workers/SharedMemory.js +149 -0
  584. package/dist/workers/SharedMemory.js.map +1 -0
  585. package/dist/workers/Transferable.d.ts.map +1 -1
  586. package/dist/workers/Transferable.js +1 -4
  587. package/dist/workers/Transferable.js.map +1 -1
  588. package/dist/workers/index.d.ts +5 -0
  589. package/dist/workers/index.d.ts.map +1 -1
  590. package/dist/workers/index.js +5 -0
  591. package/dist/workers/index.js.map +1 -1
  592. package/dist/workflow/WorkflowEngine.d.ts.map +1 -1
  593. package/dist/workflow/WorkflowEngine.js +3 -1
  594. package/dist/workflow/WorkflowEngine.js.map +1 -1
  595. package/package.json +7 -4
  596. package/src/Arbitrary.ts +60 -3
  597. package/src/BigInt.ts +247 -9
  598. package/src/Cache.ts +4 -2
  599. package/src/Cause.ts +3 -3
  600. package/src/Chunk.ts +7 -3
  601. package/src/Clock.ts +5 -2
  602. package/src/Codegen.ts +741 -0
  603. package/src/Config.ts +35 -4
  604. package/src/ConfigProvider.ts +24 -7
  605. package/src/Effect.ts +871 -109
  606. package/src/Equivalence.ts +16 -13
  607. package/src/Fiber.ts +58 -8
  608. package/src/FiberHandle.ts +86 -12
  609. package/src/FiberMap.ts +83 -10
  610. package/src/FiberSet.ts +96 -8
  611. package/src/Formatter.ts +2 -0
  612. package/src/LayerMap.ts +14 -4
  613. package/src/LayerRef.ts +2 -2
  614. package/src/Match.ts +16 -0
  615. package/src/Metric.ts +1 -1
  616. package/src/Option.ts +9 -5
  617. package/src/Order.ts +9 -4
  618. package/src/Pool.ts +22 -17
  619. package/src/Predicate.ts +7 -3
  620. package/src/Queue.ts +84 -31
  621. package/src/Rational.ts +198 -0
  622. package/src/Request.ts +20 -0
  623. package/src/Result.ts +9 -6
  624. package/src/Schema.ts +69 -32
  625. package/src/SchemaAST.ts +15 -4
  626. package/src/SchemaGetter.ts +17 -10
  627. package/src/SchemaRepresentation.ts +37 -77
  628. package/src/SchemaTransformation.ts +12 -11
  629. package/src/Scope.ts +12 -6
  630. package/src/ScopedCache.ts +2 -1
  631. package/src/Semaphore.ts +18 -4
  632. package/src/Stream.ts +66 -20
  633. package/src/String.ts +83 -6
  634. package/src/Struct.ts +138 -5
  635. package/src/TextDiff.ts +186 -30
  636. package/src/TxPriorityQueue.ts +2 -2
  637. package/src/ai/Chat.ts +9 -3
  638. package/src/ai/LanguageModel.ts +72 -15
  639. package/src/ai/McpClient.ts +1 -11
  640. package/src/ai/McpServer.ts +146 -25
  641. package/src/ai/Tool.ts +3 -2
  642. package/src/ai/Toolkit.ts +3 -1
  643. package/src/ai/WorkloadIdentity.ts +318 -0
  644. package/src/ai/index.ts +6 -0
  645. package/src/ai/internal/mcpProtocol/v2024_11_05.ts +3 -1
  646. package/src/ai/internal/mcpProtocol/v2025_03_26.ts +3 -1
  647. package/src/ai/internal/mcpProtocol/v2025_06_18.ts +3 -1
  648. package/src/ai/internal/mcpProtocol/v2025_11_25.ts +3 -1
  649. package/src/ai/internal/mcpRuntime.ts +2 -6
  650. package/src/ai/internal/mcpSkills.ts +70 -2
  651. package/src/ai/internal/structured-output.ts +6 -1
  652. package/src/chat/Chat.ts +164 -0
  653. package/src/chat/ChatAdapter.ts +444 -0
  654. package/src/chat/ChatError.ts +684 -0
  655. package/src/chat/ChatEvent.ts +221 -0
  656. package/src/chat/ChatHandler.ts +102 -0
  657. package/src/chat/ChatInteraction.ts +1979 -0
  658. package/src/chat/ChatMarkdown.ts +1406 -0
  659. package/src/chat/ChatMessage.ts +213 -0
  660. package/src/chat/ChatTest.ts +2606 -0
  661. package/src/chat/ChatThread.ts +132 -0
  662. package/src/chat/ChatUi.ts +320 -0
  663. package/src/chat/index.ts +72 -0
  664. package/src/chat/internal/retries.ts +7 -0
  665. package/src/cli/CliOutput.ts +4 -5
  666. package/src/cli/Command.ts +25 -3
  667. package/src/cli/Prompt.ts +15 -9
  668. package/src/cli/internal/config.ts +8 -8
  669. package/src/cli/internal/lexer.ts +14 -0
  670. package/src/cli/internal/parser.ts +28 -20
  671. package/src/cli/internal/wizard.ts +4 -0
  672. package/src/cluster/ClusterCron.ts +33 -10
  673. package/src/cluster/ClusterWorkflowEngine.ts +42 -23
  674. package/src/cluster/Sharding.ts +17 -3
  675. package/src/cluster/ShardingConfig.ts +3 -0
  676. package/src/cluster/SqlMessageStorage.ts +16 -20
  677. package/src/cluster/SqlRunnerStorage.ts +21 -8
  678. package/src/cluster/internal/entityManager.ts +19 -10
  679. package/src/cluster/internal/entityReaper.ts +1 -1
  680. package/src/crypto/Kdf.ts +2 -1
  681. package/src/crypto/Password.ts +258 -0
  682. package/src/crypto/index.ts +6 -0
  683. package/src/encoding/Base64.ts +79 -0
  684. package/src/encoding/Base64Url.ts +1 -0
  685. package/src/encoding/Csv.ts +47 -20
  686. package/src/encoding/Hex.ts +5 -0
  687. package/src/encoding/SchemaBinary.ts +13 -79
  688. package/src/encoding/Sse.ts +7 -5
  689. package/src/encoding/Xml.ts +394 -47
  690. package/src/eventlog/EventJournal.ts +3 -8
  691. package/src/eventlog/SqlEventLogServerEncrypted.ts +9 -9
  692. package/src/http/FindMyWay/internal/router.ts +181 -129
  693. package/src/http/FindMyWay.ts +106 -0
  694. package/src/http/HttpClient.ts +2 -1
  695. package/src/http/HttpMiddleware.ts +34 -10
  696. package/src/http/HttpRouter.ts +84 -20
  697. package/src/http/HttpServerResponse.ts +33 -61
  698. package/src/http/Mime.ts +3 -2
  699. package/src/http/internal/compression.ts +2 -2
  700. package/src/{http-api → http}/internal/mediaType.ts +4 -0
  701. package/src/http/internal/redactedPath.ts +144 -0
  702. package/src/http/internal/requestState.ts +22 -0
  703. package/src/http-api/HttpApi.ts +103 -0
  704. package/src/http-api/HttpApiBuilder.ts +14 -3
  705. package/src/http-api/HttpApiClient.ts +23 -8
  706. package/src/http-api/HttpApiEndpoint.ts +1 -1
  707. package/src/http-api/HttpApiScalar.ts +9 -1
  708. package/src/http-api/HttpApiSchema.ts +69 -3
  709. package/src/http-api/OpenApi.ts +2 -0
  710. package/src/http-api/internal/httpApiScalar.ts +5 -1
  711. package/src/index.ts +6 -0
  712. package/src/internal/arbitrary/model.ts +3 -9
  713. package/src/internal/arbitrary/regexp.ts +3 -1
  714. package/src/internal/arbitrary/runner.ts +28 -14
  715. package/src/internal/arbitrary/schema.ts +140 -35
  716. package/src/internal/codegen.ts +53 -0
  717. package/src/internal/core.ts +3 -3
  718. package/src/internal/dateTime.ts +30 -13
  719. package/src/internal/effect.ts +25 -9
  720. package/src/internal/hashMap.ts +8 -7
  721. package/src/internal/matcher.ts +2 -1
  722. package/src/internal/record.ts +19 -4
  723. package/src/internal/result.ts +1 -1
  724. package/src/internal/schema/codeExpression.ts +179 -0
  725. package/src/internal/schema/codeImports.ts +142 -0
  726. package/src/internal/schema/codeTokens.ts +39 -0
  727. package/src/internal/schema/fromJsonSchemaDocument.ts +154 -34
  728. package/src/internal/schema/passthrough.ts +8 -0
  729. package/src/internal/schema/schemaCodegen.ts +543 -0
  730. package/src/internal/schema/toJsonSchemaDocument.ts +13 -4
  731. package/src/internal/trie.ts +46 -60
  732. package/src/internal/utf8.ts +106 -0
  733. package/src/internal/version.ts +1 -1
  734. package/src/jose/Jwks.ts +23 -4
  735. package/src/observability/OtlpExporter.ts +12 -10
  736. package/src/observability/OtlpLogger.ts +5 -3
  737. package/src/observability/internal/otlpProtobuf.ts +386 -457
  738. package/src/persistence/PersistedQueue.ts +21 -4
  739. package/src/protobuf/ProtobufDescriptor.ts +503 -0
  740. package/src/protobuf/ProtobufDescriptorProto.ts +3611 -0
  741. package/src/protobuf/ProtobufMessage.ts +277 -0
  742. package/src/protobuf/ProtobufReader.ts +756 -0
  743. package/src/protobuf/ProtobufWriter.ts +530 -0
  744. package/src/protobuf/index.ts +36 -0
  745. package/src/protobuf/internal/binary.ts +1215 -0
  746. package/src/protobuf/internal/resolve.ts +533 -0
  747. package/src/protobuf/internal/wireType.ts +9 -0
  748. package/src/reactivity/AsyncResult.ts +6 -1
  749. package/src/reactivity/Atom.ts +55 -16
  750. package/src/reactivity/AtomHttpApi.ts +10 -1
  751. package/src/reactivity/AtomRegistry.ts +16 -0
  752. package/src/reactivity/AtomRpc.ts +11 -1
  753. package/src/rpc/Rpc.ts +31 -0
  754. package/src/rpc/RpcClient.ts +30 -21
  755. package/src/rpc/RpcGroup.ts +5 -0
  756. package/src/rpc/RpcMiddleware.ts +9 -0
  757. package/src/rpc/RpcSerialization.ts +26 -6
  758. package/src/rpc/RpcServer.ts +33 -20
  759. package/src/schema/SchemaCompiler/runtime.ts +2 -3
  760. package/src/schema/VariantSchema.ts +12 -4
  761. package/src/socket/Socket.ts +67 -20
  762. package/src/sql/Migrator.ts +8 -1
  763. package/src/sql/SqlModel.ts +4 -0
  764. package/src/testing/TestClock.ts +1 -1
  765. package/src/testing/TestConsole.ts +119 -9
  766. package/src/testing/TestSchema.ts +8 -3
  767. package/src/workers/SharedMemory.ts +227 -0
  768. package/src/workers/Transferable.ts +1 -5
  769. package/src/workers/index.ts +6 -0
  770. package/src/workflow/WorkflowEngine.ts +3 -1
  771. package/dist/http-api/internal/mediaType.d.ts.map +0 -1
  772. package/dist/internal/schema/toCodeDocument.d.ts +0 -2
  773. package/dist/internal/schema/toCodeDocument.d.ts.map +0 -1
  774. package/dist/internal/schema/toCodeDocument.js +0 -444
  775. package/dist/internal/schema/toCodeDocument.js.map +0 -1
  776. package/dist/observability/internal/protobuf.d.ts +0 -2
  777. package/dist/observability/internal/protobuf.d.ts.map +0 -1
  778. package/dist/observability/internal/protobuf.js +0 -169
  779. package/dist/observability/internal/protobuf.js.map +0 -1
  780. package/src/internal/schema/toCodeDocument.ts +0 -570
  781. package/src/observability/internal/protobuf.ts +0 -225
  782. /package/dist/{http-api → http}/internal/mediaType.d.ts +0 -0
@@ -0,0 +1,1172 @@
1
+ /**
2
+ * Posting long and streamed Markdown through `Chat`.
3
+ *
4
+ * `post` splits Markdown that outgrows one message into parts at block
5
+ * boundaries. `stream` writes a stream of Markdown deltas as it arrives, by
6
+ * the adapter's native stream when it has one, otherwise by posting and then
7
+ * editing, and on an adapter without `edit` by posting each part once. Both
8
+ * sit above the adapter ops and use one splitter.
9
+ *
10
+ * **Example** (Streaming a reply)
11
+ *
12
+ * ```ts
13
+ * import { Effect, Stream } from "effect"
14
+ * import { ChatMarkdown, ChatThread } from "effect/chat"
15
+ *
16
+ * const reply = (deltas: Stream.Stream<string>) =>
17
+ * Effect.gen(function*() {
18
+ * const result = yield* ChatMarkdown.stream(ChatThread.make("slack", "T1/C1"), deltas)
19
+ * return result.parts.map((part) => part.messageId)
20
+ * })
21
+ * ```
22
+ *
23
+ * @stability unstable
24
+ * @since 4.1.5
25
+ */
26
+ import * as Cause from "../Cause.js";
27
+ import * as Clock from "../Clock.js";
28
+ import * as Data from "../Data.js";
29
+ import * as Duration from "../Duration.js";
30
+ import * as Effect from "../Effect.js";
31
+ import * as Exit from "../Exit.js";
32
+ import * as Fiber from "../Fiber.js";
33
+ import * as Latch from "../Latch.js";
34
+ import * as Option from "../Option.js";
35
+ import * as Scope from "../Scope.js";
36
+ import * as Stream from "../Stream.js";
37
+ import * as Chat from "./Chat.js";
38
+ import { Registry } from "./ChatAdapter.js";
39
+ import { ChatError, fileRefused, TooLarge } from "./ChatError.js";
40
+ /**
41
+ * Error for a call that did not deliver its whole input.
42
+ *
43
+ * **Details**
44
+ *
45
+ * - `posted` is text the platform confirmed, `uncertain` is text sent in a
46
+ * write that failed `uncertain` and was never confirmed since, and
47
+ * `remaining` was never sent. The `text` of `posted`, then `uncertain`,
48
+ * then `remaining` is the input, with nothing in two of them.
49
+ * - `remainingFence` is set when `remaining` starts inside a code fence:
50
+ * the line that opened it, with its info string. Text sent later starts
51
+ * with that line and a newline, then `remaining`.
52
+ * - `cause` is a `ChatError` from a write, the upstream's error `E`, or
53
+ * `Overflow` when the input outgrew `maxParts` and the overflow file.
54
+ *
55
+ * **Gotchas**
56
+ *
57
+ * It is not a `Schema.TaggedError`: it is generic in `E` and carries
58
+ * message text. `message` and `toJSON` leave the text out.
59
+ *
60
+ * @stability unstable
61
+ * @category errors
62
+ * @since 4.1.5
63
+ */
64
+ export class ChatMarkdownError extends /*#__PURE__*/Data.TaggedError("ChatMarkdownError") {
65
+ get message() {
66
+ const cause = this.cause._tag === "Chat" ? "Chat(" + this.cause.error.reason._tag + ")" : this.cause._tag;
67
+ return "ChatMarkdown failed after " + this.posted.length + " part(s): " + cause;
68
+ }
69
+ /**
70
+ * The error without its message text.
71
+ */
72
+ toJSON() {
73
+ return {
74
+ _tag: this._tag,
75
+ posted: this.posted.map(part => ({
76
+ messageId: part.messageId,
77
+ kind: part.kind,
78
+ transport: part.transport
79
+ })),
80
+ cause: this.cause._tag === "Chat" ? {
81
+ _tag: "Chat",
82
+ error: this.cause.error.toJSON()
83
+ } : {
84
+ _tag: this.cause._tag
85
+ }
86
+ };
87
+ }
88
+ }
89
+ const fencePattern = /^ {0,3}(`{3,}|~{3,})(.*)$/;
90
+ const fenceAfter = (fence, line) => {
91
+ const match = fencePattern.exec(line);
92
+ if (match === null) return {
93
+ fence,
94
+ closed: false
95
+ };
96
+ const marker = match[1];
97
+ if (fence === undefined) {
98
+ return marker[0] === "`" && match[2].includes("`") ? {
99
+ fence,
100
+ closed: false
101
+ } : {
102
+ fence: {
103
+ marker,
104
+ line: line.trimStart()
105
+ },
106
+ closed: false
107
+ };
108
+ }
109
+ return marker[0] === fence.marker[0] && marker.length >= fence.marker.length && match[2].trim() === "" ? {
110
+ fence: undefined,
111
+ closed: true
112
+ } : {
113
+ fence,
114
+ closed: false
115
+ };
116
+ };
117
+ /**
118
+ * The lines of `text`, read from a state of `fence`. Only complete lines
119
+ * count, unless `complete` says the text is finished.
120
+ */
121
+ const scanLines = (text, fence, complete) => {
122
+ const lines = [];
123
+ let start = 0;
124
+ while (start < text.length) {
125
+ const newline = text.indexOf("\n", start);
126
+ if (newline === -1 && !complete) break;
127
+ const end = newline === -1 ? text.length : newline + 1;
128
+ const line = text.slice(start, newline === -1 ? text.length : newline).replace(/\r$/, "");
129
+ const step = fenceAfter(fence, line);
130
+ fence = step.fence;
131
+ lines.push({
132
+ end,
133
+ fence,
134
+ closed: step.closed,
135
+ boundary: fence === undefined && (step.closed || line.trim() === "")
136
+ });
137
+ start = end;
138
+ }
139
+ return lines;
140
+ };
141
+ const fenceAt = (lines, initial, index) => {
142
+ let fence = initial;
143
+ for (const line of lines) {
144
+ if (line.end > index) break;
145
+ fence = line.fence;
146
+ }
147
+ return fence;
148
+ };
149
+ const closing = (fence, markdown) => fence === undefined ? "" : (markdown === "" || markdown.endsWith("\n") ? "" : "\n") + fence.marker;
150
+ const reopening = fence => fence === undefined ? "" : fence.line + "\n";
151
+ const isBlank = text => text.trim() === "";
152
+ /** Markdown for `text` written as a whole message: an open fence is closed. */
153
+ const render = (prefix, text, fence) => {
154
+ const markdown = prefix + text;
155
+ return markdown + closing(fenceAt(scanLines(text, fence, true), fence, text.length), markdown);
156
+ };
157
+ /**
158
+ * Where to cut `text` so that `prefix`, the cut text and a closing fence
159
+ * measure at most `room`: prefer a fitting block boundary, else cut after
160
+ * the last line, else after the last whitespace, else at the last character.
161
+ * Never inside a grapheme cluster, and never at 0.
162
+ */
163
+ const fitCut = (measure, text, fence, prefix, room) => {
164
+ const lines = scanLines(text, fence, false);
165
+ const size = cut => {
166
+ const markdown = prefix + text.slice(0, cut);
167
+ return measure(markdown + closing(fenceAt(lines, fence, cut), markdown));
168
+ };
169
+ const fits = cut => size(cut) <= room;
170
+ let low = 0;
171
+ let high = text.length;
172
+ while (low < high) {
173
+ const middle = Math.ceil((low + high) / 2);
174
+ if (fits(middle)) low = middle;else high = middle - 1;
175
+ }
176
+ const most = low;
177
+ // Closing a fence removes the synthetic suffix. Look ahead two real closes
178
+ // beyond the estimate, farthest first, to bound the extra measurements.
179
+ const closes = lines.filter(line => line.closed && line.end > most).slice(0, 2);
180
+ const candidates = closes.map(line => line.end).reverse();
181
+ for (let i = lines.length - 1; i >= 0; i--) {
182
+ if (lines[i].boundary && lines[i].end <= most) candidates.push(lines[i].end);
183
+ }
184
+ for (let i = lines.length - 1; i >= 0; i--) {
185
+ if (lines[i].end <= most) candidates.push(lines[i].end);
186
+ }
187
+ for (let i = most - 1; i > 0; i--) {
188
+ if (/\s/.test(text[i])) {
189
+ candidates.push(i + 1);
190
+ break;
191
+ }
192
+ }
193
+ candidates.push(most);
194
+ const clusters = graphemes(text);
195
+ for (const candidate of candidates) {
196
+ const cut = clusterStart(clusters, text, candidate);
197
+ if (cut > 0 && fits(cut)) return cut;
198
+ }
199
+ // Not even one cluster fits: the cut goes after the first one, and the write fails `TooLarge`.
200
+ return clusterStart(clusters, text, most) || clusterEnd(clusters, 0);
201
+ };
202
+ let segmenter;
203
+ /** The grapheme clusters of `text`. `Intl.Segmenter` is created on first use. */
204
+ const graphemes = text => (segmenter ??= new Intl.Segmenter(undefined, {
205
+ granularity: "grapheme"
206
+ })).segment(text);
207
+ /** The grapheme cluster boundary at or before `cut`. */
208
+ const clusterStart = (clusters, text, cut) => cut <= 0 || cut >= text.length ? cut : clusters.containing(cut).index;
209
+ /** The end of the grapheme cluster that contains `index`. */
210
+ const clusterEnd = (clusters, index) => {
211
+ const cluster = clusters.containing(index);
212
+ return cluster.index + cluster.segment.length;
213
+ };
214
+ /** Where the last grapheme cluster of `text` that is not whitespace starts, or -1 for blank text. */
215
+ const lastContentStart = text => {
216
+ const end = text.trimEnd().length;
217
+ return end === 0 ? -1 : graphemes(text).containing(end - 1).index;
218
+ };
219
+ /** Past this many held characters, a held line is released up to its last whitespace. */
220
+ const holdLimit = 300;
221
+ /**
222
+ * What an append may send of `pending` before the input is finished: up to
223
+ * the last complete line, unless inside a code fence.
224
+ */
225
+ const releasable = (pending, fence) => {
226
+ const lines = scanLines(pending, fence, false);
227
+ const complete = lines.length === 0 ? 0 : lines[lines.length - 1].end;
228
+ if (complete === pending.length) return pending;
229
+ if (fenceAt(lines, fence, complete) === undefined) {
230
+ const tail = pending.slice(complete);
231
+ if (tail.length <= holdLimit) return pending.slice(0, complete);
232
+ const space = tail.search(/\s\S*$/);
233
+ if (space !== -1) return pending.slice(0, complete + space + 1);
234
+ }
235
+ // The next delta may still extend the trailing cluster, even inside a fence.
236
+ // A split surrogate may complete a code point that extends the preceding cluster.
237
+ const text = /[\ud800-\udbff]$/.test(pending) ? pending.slice(0, -1) : pending;
238
+ let last = 0;
239
+ for (const cluster of graphemes(text)) last = cluster.index;
240
+ return pending.slice(0, last);
241
+ };
242
+ const utf8Length = text => {
243
+ let bytes = 0;
244
+ for (let i = 0; i < text.length; i++) {
245
+ const code = text.charCodeAt(i);
246
+ bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code >= 0xd800 && code <= 0xdfff ? 2 : 3;
247
+ }
248
+ return bytes;
249
+ };
250
+ // -----------------------------------------------------------------------------
251
+ // the reply
252
+ // -----------------------------------------------------------------------------
253
+ const defaultMaxParts = 5;
254
+ /** Consecutive `retry` failures of appends on one stream before the repair. Provisional. */
255
+ const appendRetryLimit = 3;
256
+ /** A native stream rolls over at the next block boundary past this share of `streamLength`. */
257
+ const softShare = 0.85;
258
+ /** A native stream rotates once it is this share of `streamAge` old. */
259
+ const ageShare = 0.8;
260
+ /** How long finishing a failed or interrupted reply may take. */
261
+ const finishTimeout = /*#__PURE__*/Duration.seconds(5);
262
+ const firstError = exit => Option.getOrUndefined(Exit.findErrorOption(exit));
263
+ /** Whether a write was cut off before the platform answered. */
264
+ const unanswered = exit => Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause);
265
+ /** Whether the adapter guarantees the files were refused and nothing remains posted. */
266
+ const isFileRefusal = exit => {
267
+ const reason = firstError(exit)?.reason;
268
+ return reason?.retry === "denied" && reason.detail === fileRefused;
269
+ };
270
+ const reply = options => Effect.suspend(() => {
271
+ const {
272
+ chat,
273
+ descriptor,
274
+ thread
275
+ } = options;
276
+ const measure = descriptor.measure;
277
+ const limits = descriptor.limits;
278
+ const interval = Duration.toMillis(options.minWriteInterval ?? limits.minWriteInterval);
279
+ let maxParts = Math.max(1, options.maxParts ?? defaultMaxParts);
280
+ const overflow = options.overflow ?? "fail";
281
+ const streamLength = Math.min(limits.streamLength ?? limits.messageLength, limits.messageLength);
282
+ const streamAge = limits.streamAge === undefined ? undefined : Duration.toMillis(limits.streamAge);
283
+ const onPart = options.onPart ?? (() => Effect.void);
284
+ const native = options.native;
285
+ const latch = Latch.makeUnsafe(false);
286
+ /** Opens when the upstream fails, which ends the loop even while a write is in flight. */
287
+ const upstreamFailed = Latch.makeUnsafe(false);
288
+ /** Holds the write fibers, so they outlive the loop and the finalizer can wait for them. */
289
+ const writes = Scope.makeUnsafe();
290
+ let input = options.initial;
291
+ let upstream = options.deltas === undefined ? Exit.void : undefined;
292
+ let state = {
293
+ _tag: "Running"
294
+ };
295
+ let finishing = false;
296
+ let nativeAllowed = native !== undefined && options.recipient !== undefined;
297
+ /** Without `edit`, a streamed part is posted once, when it is final. */
298
+ const buffered = options.deltas !== undefined && !descriptor.capabilities.has("edit");
299
+ let renderedNatively = false;
300
+ let lastWrite = Number.NEGATIVE_INFINITY;
301
+ let retryAt;
302
+ let overflowStart;
303
+ let overflowBytes = 0;
304
+ let overflowUncertain = false;
305
+ let overflowFilesRefused = false;
306
+ let note;
307
+ const parts = [];
308
+ const isRunning = () => state._tag === "Running";
309
+ const fail = cause => {
310
+ if (isRunning()) state = {
311
+ _tag: "Failed",
312
+ cause
313
+ };
314
+ };
315
+ const failChat = error => fail({
316
+ _tag: "Chat",
317
+ error
318
+ });
319
+ const done = stopped => {
320
+ if (isRunning()) state = {
321
+ _tag: "Done",
322
+ stopped
323
+ };
324
+ };
325
+ const newPart = (start, fence) => ({
326
+ start,
327
+ fence,
328
+ prefix: reopening(fence),
329
+ transport: options.deltas === undefined ? "post" : nativeAllowed ? "native" : buffered ? "post" : "edit",
330
+ native: nativeAllowed,
331
+ posted: undefined,
332
+ confirmed: start,
333
+ uncertain: start,
334
+ sent: start,
335
+ attempted: -1,
336
+ markdown: "",
337
+ startedAt: 0,
338
+ retries: 0,
339
+ closing: undefined,
340
+ stoppedAt: undefined
341
+ });
342
+ let current = newPart(0, undefined);
343
+ const partOf = (posted, markdown, text, kind, transport) => ({
344
+ messageId: posted.id,
345
+ timestamp: posted.timestamp,
346
+ markdown,
347
+ text,
348
+ kind,
349
+ transport
350
+ });
351
+ const toPart = (part, posted, markdown, end) => partOf(posted, markdown, input.slice(part.start, end), "text", part.transport);
352
+ /** Only the reply's first message replies to `replyTo`. */
353
+ const firstReplyTo = () => parts.length === 0 && options.replyTo !== undefined ? {
354
+ replyTo: options.replyTo
355
+ } : {};
356
+ /** The open fence at `index` of the input, read from the current part's complete lines. */
357
+ const fenceAtInput = (part, index) => {
358
+ const text = input.slice(part.start, index);
359
+ return fenceAt(scanLines(text, part.fence, false), part.fence, text.length);
360
+ };
361
+ const now = Clock.currentTimeMillis;
362
+ /** Waits for the floor since the previous write returned. */
363
+ const pace = Effect.flatMap(now, time => {
364
+ const wait = lastWrite + interval - time;
365
+ return wait > 0 ? Effect.sleep(Duration.millis(wait)) : Effect.void;
366
+ });
367
+ const wrote = Effect.flatMap(now, time => Effect.sync(() => void (lastWrite = time)));
368
+ /**
369
+ * Sends an edit or a stop after the floor. Both are safe to send again, so
370
+ * they stay interruptible. `commit` records the answer, or that there was
371
+ * none, before an interrupt can land.
372
+ */
373
+ const write = (effect, commit) => Effect.andThen(pace, Effect.uninterruptibleMask(restore => Effect.exit(restore(effect)).pipe(Effect.ensuring(wrote), Effect.flatMap(exit => Effect.sync(() => commit(exit))))));
374
+ /** The post, start or append in flight. */
375
+ let inflight;
376
+ /**
377
+ * Sends a post, a start or an append after the floor. Sending one again
378
+ * could repeat text, so neither an interrupt of the reply nor a failure of
379
+ * the upstream cuts it off: it runs in a fiber of its own, outside the
380
+ * loop, which commits the answer, and the finalizer waits for it. Only the
381
+ * finish timeout cuts it off, and `commit` then records that there was no
382
+ * answer.
383
+ */
384
+ const send = (effect, commit) => Effect.andThen(pace, Effect.uninterruptibleMask(restore => Effect.forkIn(Effect.uninterruptibleMask(restoreRequest => Effect.exit(restoreRequest(effect)).pipe(Effect.ensuring(wrote), Effect.tap(exit => Effect.flatMap(now, time => commit(exit, time))), Effect.ensuring(Effect.sync(() => void (inflight = undefined))))), writes).pipe(Effect.flatMap(fiber => {
385
+ inflight = fiber;
386
+ return restore(Fiber.join(fiber));
387
+ }))));
388
+ /**
389
+ * Creates a message, records it and runs `onPart` for it, in one commit,
390
+ * so an interrupt can neither lose an id nor make the finalizer create the
391
+ * message again.
392
+ */
393
+ const create = (effect, part, commit) => send(effect, (exit, time) => {
394
+ commit(exit, time);
395
+ return Exit.isSuccess(exit) ? onPart(part(exit.value)) : Effect.void;
396
+ });
397
+ const finishPart = end => {
398
+ const part = current;
399
+ parts.push(toPart(part, part.posted, part.markdown, end));
400
+ current = newPart(end, fenceAtInput(part, end));
401
+ if (parts.length >= maxParts) {
402
+ overflowStart = end;
403
+ overflowBytes = utf8Length(input.slice(end));
404
+ }
405
+ };
406
+ /** Ends the reply. Blank text left over is given to the last part, since it can't be posted alone. */
407
+ const finishReply = from => {
408
+ const rest = input.slice(from);
409
+ const last = parts.length - 1;
410
+ if (rest !== "" && last >= 0) parts[last] = {
411
+ ...parts[last],
412
+ text: parts[last].text + rest
413
+ };
414
+ done(false);
415
+ };
416
+ const hasFiles = (options.files?.length ?? 0) > 0;
417
+ const actions = options.actions ?? [];
418
+ const hasActions = actions.length > 0;
419
+ /** What the reply's last part costs, measured with the actions it carries. */
420
+ const measureLast = hasActions ? markdown => measure(markdown, actions) : measure;
421
+ /** The last part takes the files and the actions. */
422
+ const hasExtras = hasFiles || hasActions;
423
+ /**
424
+ * Set when not even the text's last grapheme cluster fits beside the
425
+ * actions: the text is posted without them, and the files and the
426
+ * actions follow in a message of their own.
427
+ */
428
+ let extrasAlone = false;
429
+ /** Whether the note posted on its own takes the actions. */
430
+ let noteActions = false;
431
+ /** Set when the actions fit beside neither the text nor the note: they follow in a message of their own. */
432
+ let actionsAfter = false;
433
+ // --- post-then-edit --------------------------------------------------------
434
+ /**
435
+ * Writes the current part as `input[start, end)`. `last` marks the part's
436
+ * final write, and `withFiles` the reply's last part, which takes the files.
437
+ */
438
+ const writeEdit = (end, last, withFiles) => Effect.suspend(() => {
439
+ const part = current;
440
+ const markdown = render(part.prefix, input.slice(part.start, end), part.fence);
441
+ part.attempted = input.length;
442
+ if (part.posted === undefined) return postPart(part, end, markdown, withFiles && hasExtras);
443
+ if (part.confirmed === end && part.markdown === markdown) return Effect.void;
444
+ const edit = chat.edit(thread, part.posted.id, {
445
+ markdown
446
+ });
447
+ return write(last ? edit : Chat.withoutRetries(edit), exit => {
448
+ if (Exit.isSuccess(exit)) {
449
+ part.confirmed = part.uncertain = end;
450
+ part.markdown = markdown;
451
+ return;
452
+ }
453
+ // Cut off, so it may have landed. The finalizer edits again.
454
+ if (unanswered(exit)) part.uncertain = Math.max(part.uncertain, end);
455
+ const error = firstError(exit);
456
+ if (error === undefined) return;
457
+ if (error.retry === "uncertain") part.uncertain = Math.max(part.uncertain, end);
458
+ if (last || error.retry === "denied") failChat(error);
459
+ });
460
+ });
461
+ const postPart = (part, end, markdown, withFiles) => {
462
+ const files = withFiles && hasFiles ? options.files : undefined;
463
+ const unattachedNote = files === undefined ? undefined : options.unattachedNote;
464
+ const withActions = withFiles && hasActions;
465
+ const settle = (exit, markdown) => {
466
+ if (Exit.isSuccess(exit)) {
467
+ part.posted = exit.value;
468
+ part.markdown = markdown;
469
+ part.confirmed = part.uncertain = end;
470
+ return;
471
+ }
472
+ if (unanswered(exit)) part.uncertain = end;
473
+ const error = firstError(exit);
474
+ if (error === undefined) return;
475
+ if (error.retry === "uncertain") part.uncertain = end;
476
+ failChat(error);
477
+ };
478
+ const attempt = (markdown, files, fallback, withActions) => create(chat.post(thread, {
479
+ markdown,
480
+ ...firstReplyTo(),
481
+ ...(files === undefined ? {} : {
482
+ files
483
+ }),
484
+ ...(withActions ? {
485
+ actions
486
+ } : {})
487
+ }), posted => toPart(part, posted, markdown, end), exit => {
488
+ // Left unsettled: the post goes out again without the files.
489
+ if (fallback && isFileRefusal(exit)) return;
490
+ settle(exit, markdown);
491
+ });
492
+ const withoutFiles = () => {
493
+ // Replace refused files with the unattached note.
494
+ const text = unattachedNote(files);
495
+ if (measureLast(markdown + text) <= limits.messageLength) {
496
+ return Effect.asVoid(attempt(markdown + text, undefined, false, withActions));
497
+ }
498
+ // The note is the last message now, so it takes the actions, or they follow it on their own.
499
+ noteActions = withActions && measure(text, actions) <= limits.messageLength;
500
+ actionsAfter = withActions && !noteActions;
501
+ // With no text to post, this part is the note.
502
+ if (isBlank(markdown)) return Effect.asVoid(attempt(text, undefined, false, noteActions));
503
+ note = text;
504
+ return Effect.asVoid(attempt(markdown, undefined, false, false));
505
+ };
506
+ if (overflowFilesRefused && files !== undefined) return withoutFiles();
507
+ return Effect.flatMap(attempt(markdown, files, unattachedNote !== undefined, withActions), exit => unattachedNote !== undefined && isFileRefusal(exit) ? withoutFiles() : Effect.void);
508
+ };
509
+ /** Posts a message after the text that is not part of it: the unattached note, or the actions alone. */
510
+ const postAfter = (text, withActions) => {
511
+ const part = posted => partOf(posted, text, "", "text", "post");
512
+ const post = chat.post(thread, {
513
+ markdown: text,
514
+ ...(withActions ? {
515
+ actions
516
+ } : {})
517
+ });
518
+ return Effect.flatMap(create(post, part, exit => {
519
+ if (Exit.isSuccess(exit)) return void parts.push(part(exit.value));
520
+ const error = firstError(exit);
521
+ if (error !== undefined) failChat(error);
522
+ }), () => isRunning() ? finishAfter : Effect.void);
523
+ };
524
+ /**
525
+ * Ends the reply once the text is posted: the unattached note that did
526
+ * not fit the last part follows, then the actions when they fit beside
527
+ * neither. The actions' message counts against `maxParts`, and with no
528
+ * part left the reply fails `Overflow`.
529
+ */
530
+ const finishAfter = Effect.suspend(() => {
531
+ if (note !== undefined) {
532
+ const text = note;
533
+ note = undefined;
534
+ return postAfter(text, noteActions);
535
+ }
536
+ if (actionsAfter) {
537
+ actionsAfter = false;
538
+ if (parts.length >= maxParts) {
539
+ fail({
540
+ _tag: "Overflow"
541
+ });
542
+ return Effect.void;
543
+ }
544
+ return postAfter("", true);
545
+ }
546
+ done(false);
547
+ return Effect.void;
548
+ });
549
+ /**
550
+ * Posts the files and the actions in a message of their own, after the
551
+ * text. It counts against `maxParts`, and there is no text left to put in
552
+ * an overflow file, so with no part left the reply fails `Overflow`.
553
+ */
554
+ const postExtras = Effect.suspend(() => {
555
+ extrasAlone = false;
556
+ if (parts.length >= maxParts) {
557
+ fail({
558
+ _tag: "Overflow"
559
+ });
560
+ return Effect.void;
561
+ }
562
+ const part = newPart(input.length, undefined);
563
+ current = part;
564
+ return Effect.flatMap(postPart(part, input.length, "", true), () => {
565
+ if (!isRunning()) return Effect.void;
566
+ finishPart(input.length);
567
+ return finishAfter;
568
+ });
569
+ });
570
+ const stepEdit = final => Effect.suspend(() => {
571
+ const part = current;
572
+ if (part.stoppedAt !== undefined) {
573
+ // A user clicked stop during a repair: settle what was sent, then end the reply.
574
+ return Effect.map(writeEdit(part.stoppedAt, false, false), () => {
575
+ if (!isRunning()) return;
576
+ finishPart(part.confirmed);
577
+ done(true);
578
+ });
579
+ }
580
+ const body = input.slice(part.start);
581
+ if (part.posted === undefined && isBlank(body) && !(final && hasExtras && parts.length === 0)) {
582
+ if (final) finishReply(part.start);
583
+ return Effect.void;
584
+ }
585
+ const end = last => {
586
+ if (!last || !isRunning()) return Effect.void;
587
+ return extrasAlone ? postExtras : finishAfter;
588
+ };
589
+ const cutAt = cut => {
590
+ // Only blank text follows the cut: this is the last part, so it takes the files, the actions and that text.
591
+ const last = final && isBlank(input.slice(cut));
592
+ return Effect.flatMap(writeEdit(cut, true, last && !extrasAlone), () => {
593
+ if (!isRunning()) return Effect.void;
594
+ finishPart(last ? input.length : cut);
595
+ return end(last);
596
+ });
597
+ };
598
+ /** A cut that would end the reply has to leave room for the actions. */
599
+ const lastFits = cut => !final || !hasActions || extrasAlone || !isBlank(input.slice(cut)) || measureLast(render(part.prefix, input.slice(part.start, cut), part.fence)) <= limits.messageLength;
600
+ const cut = measure(render(part.prefix, body, part.fence)) > limits.messageLength ? part.start + fitCut(measure, body, part.fence, part.prefix, limits.messageLength) : input.length;
601
+ if (!lastFits(cut)) {
602
+ // Reserve the last grapheme for a part with actions. If they cannot
603
+ // fit together, send the actions in their own message.
604
+ const keep = lastContentStart(body);
605
+ const tailStart = part.start + keep;
606
+ const tailFence = fenceAtInput(part, tailStart);
607
+ const tail = render(reopening(tailFence), input.slice(tailStart), tailFence);
608
+ if (keep > 0 && measureLast(tail) <= limits.messageLength) {
609
+ return cutAt(part.start + fitCut(measure, body.slice(0, keep), part.fence, part.prefix, limits.messageLength));
610
+ }
611
+ extrasAlone = true;
612
+ }
613
+ if (cut < input.length) return cutAt(cut);
614
+ if (buffered && !final) {
615
+ // Nothing to post until the part is final: it rolls over or the reply ends.
616
+ part.attempted = input.length;
617
+ return Effect.void;
618
+ }
619
+ return Effect.flatMap(writeEdit(input.length, final, final && !extrasAlone), () => {
620
+ if (final && isRunning()) finishPart(input.length);
621
+ return end(final);
622
+ });
623
+ });
624
+ // --- native ----------------------------------------------------------------
625
+ /** Sends the stop the current part is closing with. A stop carries no text, so its failures are ignored. */
626
+ const sendStop = (part, closing) => write(native.stop(thread, part.posted.id, {
627
+ end: closing.end
628
+ }), exit => {
629
+ // Cut off, so the stream may still be open. The finalizer sends the stop again.
630
+ if (unanswered(exit)) return;
631
+ part.closing = undefined;
632
+ part.native = false;
633
+ closing.onStop(Exit.isSuccess(exit) && exit.value.stopped);
634
+ });
635
+ /** Ends a native part at `end` once its stop is answered. The reply ends when `last`, or when a user had clicked stop. */
636
+ const rollNative = (part, end, last) => {
637
+ part.closing = {
638
+ end: last ? "done" : "rotating",
639
+ onStop: stopped => {
640
+ finishPart(end);
641
+ if (stopped) done(true);else if (last) finishReply(end);
642
+ }
643
+ };
644
+ };
645
+ /**
646
+ * Stops the stream and carries on by post-then-edit: the next edit writes
647
+ * the part's whole known text, the chunk that failed included.
648
+ */
649
+ const repair = part => {
650
+ part.closing = {
651
+ end: "rotating",
652
+ onStop: stopped => {
653
+ part.attempted = -1;
654
+ if (stopped) part.stoppedAt = Math.max(part.uncertain, part.sent);
655
+ }
656
+ };
657
+ };
658
+ const startNative = final => Effect.suspend(() => {
659
+ const part = current;
660
+ const pending = input.slice(part.start);
661
+ const text = final ? pending : releasable(pending, part.fence);
662
+ if (isBlank(text)) {
663
+ if (final) finishReply(part.start);
664
+ return Effect.void;
665
+ }
666
+ part.attempted = input.length;
667
+ // Measured with the closing fence, so a start leaves room to close it.
668
+ const whole = render(part.prefix, text, part.fence);
669
+ const cut = measure(whole) > streamLength ? fitCut(measure, text, part.fence, part.prefix, streamLength) : text.length;
670
+ const end = part.start + cut;
671
+ const roll = cut < text.length;
672
+ const markdown = roll || final ? render(part.prefix, text.slice(0, cut), part.fence) : part.prefix + text;
673
+ return Effect.asVoid(create(native.start(thread, {
674
+ markdown,
675
+ ...firstReplyTo(),
676
+ ...(parts.length === 0 ? {} : {
677
+ continues: parts[parts.length - 1].messageId
678
+ }),
679
+ recipient: options.recipient
680
+ }), posted => toPart(part, posted, markdown, end), (exit, time) => {
681
+ if (Exit.isSuccess(exit)) {
682
+ renderedNatively = true;
683
+ part.posted = exit.value;
684
+ part.markdown = markdown;
685
+ part.confirmed = part.uncertain = part.sent = end;
686
+ part.startedAt = time;
687
+ if (roll || final) rollNative(part, end, !roll);
688
+ return;
689
+ }
690
+ if (unanswered(exit)) part.uncertain = end;
691
+ const error = firstError(exit);
692
+ if (error === undefined) return;
693
+ if (error.reason._tag === "Unsupported" && !renderedNatively) {
694
+ nativeAllowed = false;
695
+ current = newPart(part.start, part.fence);
696
+ return;
697
+ }
698
+ if (error.retry === "uncertain") part.uncertain = end;
699
+ failChat(error);
700
+ }));
701
+ });
702
+ /**
703
+ * What the next append sends, and whether the part ends after it. Every
704
+ * append leaves room to close the code fence it ends in, so a part that
705
+ * rolls over on length always ends with its fence closed. `rotate` sends
706
+ * just that closing fence, for a stream about to rotate on age.
707
+ */
708
+ const nextAppend = (part, final, rotate) => {
709
+ const pending = input.slice(part.sent);
710
+ const fence = fenceAtInput(part, part.sent);
711
+ const lines = scanLines(pending, fence, false);
712
+ const size = text => measure(part.markdown + text);
713
+ const closeAt = piece => closing(fenceAt(lines, fence, piece.length), part.markdown + piece);
714
+ const closeOnly = {
715
+ piece: "",
716
+ extra: closeAt(""),
717
+ roll: true
718
+ };
719
+ if (rotate) return closeOnly;
720
+ const soft = softShare * streamLength;
721
+ const piece = final ? pending : releasable(pending, fence);
722
+ const reserve = final ? closing(fenceAt(scanLines(pending, fence, true), fence, pending.length), part.markdown + pending) : closeAt(piece);
723
+ const extra = final ? reserve : "";
724
+ if (size(piece + reserve) <= soft) return {
725
+ piece,
726
+ extra,
727
+ roll: false
728
+ };
729
+ for (const line of lines) {
730
+ if (!line.boundary || line.end > piece.length) continue;
731
+ const measured = size(pending.slice(0, line.end));
732
+ if (measured > streamLength) break;
733
+ if (measured >= soft) return {
734
+ piece: pending.slice(0, line.end),
735
+ extra: "",
736
+ roll: true
737
+ };
738
+ }
739
+ if (size(piece + reserve) <= streamLength) return {
740
+ piece,
741
+ extra,
742
+ roll: false
743
+ };
744
+ const cut = pending.slice(0, fitCut(measure, pending, fence, part.markdown, streamLength));
745
+ // Not even the smallest cut fits with its closing fence: send just the closing fence.
746
+ return size(cut + closeAt(cut)) <= streamLength ? {
747
+ piece: cut,
748
+ extra: closeAt(cut),
749
+ roll: true
750
+ } : closeOnly;
751
+ };
752
+ /**
753
+ * Appends the next piece. What follows, a rollover, a repair or a retry,
754
+ * is recorded in the commit, so an interrupt can't lose it.
755
+ */
756
+ const appendNative = (final, rotate) => Effect.suspend(() => {
757
+ const part = current;
758
+ const {
759
+ extra,
760
+ piece,
761
+ roll
762
+ } = nextAppend(part, final, rotate);
763
+ if (piece === "" && extra === "") {
764
+ if (roll || final) rollNative(part, part.sent, !roll);
765
+ return Effect.void;
766
+ }
767
+ part.attempted = input.length;
768
+ const markdown = piece + extra;
769
+ const commit = (exit, time) => {
770
+ if (Exit.isSuccess(exit)) {
771
+ const {
772
+ held,
773
+ state
774
+ } = exit.value;
775
+ if (state === "stopped") {
776
+ part.native = false;
777
+ finishPart(part.sent);
778
+ done(true);
779
+ } else if (state === "closed") {
780
+ part.native = false;
781
+ part.attempted = -1;
782
+ } else {
783
+ const added = Math.max(0, markdown.length - held);
784
+ part.retries = 0;
785
+ part.sent += Math.min(piece.length, added);
786
+ part.confirmed = part.uncertain = part.sent;
787
+ part.markdown += markdown.slice(0, added);
788
+ if (roll || final && part.sent === input.length) rollNative(part, part.sent, !roll);
789
+ }
790
+ return;
791
+ }
792
+ // Cut off by the finish timeout, so it may have landed.
793
+ if (unanswered(exit)) {
794
+ part.uncertain = part.sent + piece.length;
795
+ return;
796
+ }
797
+ const error = firstError(exit);
798
+ if (error === undefined) return;
799
+ if (error.reason._tag === "TooLarge") {
800
+ // Retry the chunk in the next part.
801
+ rollNative(part, part.sent, false);
802
+ } else if (error.retry === "retry") {
803
+ part.retries++;
804
+ if (part.retries >= appendRetryLimit) repair(part);else retryAt = time + (error.retryAfter === undefined ? interval : Duration.toMillis(error.retryAfter));
805
+ } else if (error.retry === "uncertain") {
806
+ part.uncertain = part.sent + piece.length;
807
+ repair(part);
808
+ } else {
809
+ failChat(error);
810
+ part.closing = {
811
+ end: "done",
812
+ onStop: () => {}
813
+ };
814
+ }
815
+ };
816
+ return Effect.asVoid(send(native.append(thread, part.posted.id, markdown, {
817
+ flush: final || roll
818
+ }), (exit, time) => Effect.sync(() => commit(exit, time))));
819
+ });
820
+ const stepNative = final => Effect.suspend(() => {
821
+ const part = current;
822
+ if (part.posted === undefined) return startNative(final);
823
+ if (final || streamAge === undefined) return appendNative(final, false);
824
+ return Effect.flatMap(now, time => appendNative(final, time - part.startedAt >= ageShare * streamAge));
825
+ });
826
+ // --- overflow --------------------------------------------------------------
827
+ const stepOverflow = final => Effect.suspend(() => {
828
+ const start = overflowStart;
829
+ const rest = input.slice(start);
830
+ if (isBlank(rest)) {
831
+ if (final) finishReply(start);
832
+ return Effect.void;
833
+ }
834
+ if (overflow === "fail" || overflowBytes > limits.uploadBytes) {
835
+ fail({
836
+ _tag: "Overflow"
837
+ });
838
+ return Effect.void;
839
+ }
840
+ // The upload is skipped when the reply failed or was interrupted.
841
+ if (!final || finishing) {
842
+ if (finishing) done(false);
843
+ return Effect.void;
844
+ }
845
+ const file = {
846
+ name: overflow.file.name,
847
+ contentType: "text/markdown",
848
+ content: new TextEncoder().encode(rest)
849
+ };
850
+ const part = markdown => posted => partOf(posted, markdown, rest, "overflowFile", "post");
851
+ // Attach consumer files to the final message.
852
+ const attempt = (markdown, files, fallback) => create(chat.post(thread, {
853
+ markdown,
854
+ files: [file, ...files],
855
+ ...(hasActions ? {
856
+ actions
857
+ } : {})
858
+ }), part(markdown), exit => {
859
+ if (Exit.isSuccess(exit)) {
860
+ parts.push(part(markdown)(exit.value));
861
+ overflowStart = input.length;
862
+ return done(false);
863
+ }
864
+ if (fallback && isFileRefusal(exit)) return;
865
+ if (unanswered(exit)) overflowUncertain = true;
866
+ const error = firstError(exit);
867
+ if (error === undefined) return;
868
+ overflowUncertain = error.retry === "uncertain";
869
+ failChat(error);
870
+ });
871
+ const files = options.files ?? [];
872
+ const unattachedNote = files.length === 0 ? undefined : options.unattachedNote;
873
+ return Effect.asVoid(Effect.map(attempt("", files, unattachedNote !== undefined), exit => {
874
+ if (unattachedNote === undefined || !isFileRefusal(exit)) return;
875
+ // The overflow file was refused too. Drain the remaining text through
876
+ // the normal splitter, without files or another maxParts overflow.
877
+ overflowFilesRefused = true;
878
+ overflowStart = undefined;
879
+ maxParts = Infinity;
880
+ nativeAllowed = false;
881
+ current.native = false;
882
+ }));
883
+ });
884
+ // --- the loop --------------------------------------------------------------
885
+ /** Whether there is a write to make. A stop is sent even after the reply failed. */
886
+ const active = () => isRunning() || current.closing !== undefined;
887
+ const hasWork = () => {
888
+ if (!isRunning() || upstream !== undefined || current.closing !== undefined) return true;
889
+ if (overflowStart !== undefined) {
890
+ return !isBlank(input.slice(overflowStart)) && (overflow === "fail" || overflowBytes > limits.uploadBytes);
891
+ }
892
+ const part = current;
893
+ if (input.length <= part.attempted) return false;
894
+ if (!part.native) return !isBlank(input.slice(part.start));
895
+ return part.posted === undefined ? !isBlank(releasable(input.slice(part.start), part.fence)) : releasable(input.slice(part.sent), fenceAtInput(part, part.sent)) !== "";
896
+ };
897
+ /** Waits until there is something to write, or a retry is due. */
898
+ const awaitWork = Effect.suspend(() => {
899
+ if (retryAt !== undefined) {
900
+ const at = retryAt;
901
+ retryAt = undefined;
902
+ return Effect.flatMap(now, time => time < at ? Effect.sleep(Duration.millis(at - time)) : Effect.void);
903
+ }
904
+ if (hasWork()) return Effect.void;
905
+ latch.closeUnsafe();
906
+ return Effect.andThen(latch.await, awaitWork);
907
+ });
908
+ const step = final => {
909
+ const closing = current.closing;
910
+ if (closing !== undefined) return sendStop(current, closing);
911
+ if (overflowStart !== undefined) return stepOverflow(final);
912
+ return current.native ? stepNative(final) : stepEdit(final);
913
+ };
914
+ const loop = Effect.suspend(() => active() ? Effect.flatMap(awaitWork, () => {
915
+ if (!active()) return Effect.void;
916
+ if (current.closing === undefined && upstream !== undefined && Exit.isFailure(upstream)) return Effect.void;
917
+ // The write takes whatever arrived while it waited for the floor.
918
+ return Effect.andThen(pace, Effect.suspend(() => step(upstream !== undefined))).pipe(Effect.andThen(loop));
919
+ }) : Effect.void);
920
+ /** Writes what was received as the end of the reply, closing an open fence, within a short timeout. */
921
+ const finish = Effect.suspend(() => {
922
+ finishing = true;
923
+ const drain = Effect.suspend(() => active() ? Effect.andThen(step(true), drain) : Effect.void);
924
+ // A write in flight commits first. Interruptible, so the timeout can cut it short in the interrupt finalizer.
925
+ const awaitInflight = Effect.suspend(() => inflight === undefined ? Effect.void : Fiber.await(inflight));
926
+ return Effect.timeoutOption(Effect.interruptible(Effect.andThen(awaitInflight, drain)), finishTimeout).pipe(Effect.andThen(Effect.suspend(() => inflight === undefined ? Effect.void : Fiber.interrupt(inflight))), Effect.asVoid);
927
+ });
928
+ const toError = cause => {
929
+ const posted = parts.slice();
930
+ let uncertain = "";
931
+ let from;
932
+ if (overflowStart !== undefined) {
933
+ from = overflowStart;
934
+ if (overflowUncertain) {
935
+ uncertain = input.slice(from);
936
+ from = input.length;
937
+ }
938
+ } else {
939
+ const part = current;
940
+ if (part.posted !== undefined) posted.push(toPart(part, part.posted, part.markdown, part.confirmed));
941
+ from = Math.max(part.confirmed, part.uncertain);
942
+ uncertain = input.slice(part.confirmed, from);
943
+ }
944
+ // Read from the start of the input, so it holds whichever part the cut fell in.
945
+ const fence = from === input.length ? undefined : fenceAt(scanLines(input.slice(0, from), undefined, false), undefined, from);
946
+ return new ChatMarkdownError({
947
+ posted,
948
+ remaining: input.slice(from),
949
+ ...(fence === undefined ? {} : {
950
+ remainingFence: fence.line
951
+ }),
952
+ ...(uncertain === "" ? {} : {
953
+ uncertain
954
+ }),
955
+ cause
956
+ });
957
+ };
958
+ const consume = options.deltas === undefined ? Effect.void : Effect.flatMap(Effect.exit(Stream.runForEach(options.deltas, chunk => Effect.sync(() => {
959
+ input += chunk;
960
+ if (overflowStart !== undefined) overflowBytes += utf8Length(chunk);
961
+ latch.openUnsafe();
962
+ }))), exit => Effect.sync(() => {
963
+ upstream = exit;
964
+ latch.openUnsafe();
965
+ if (Exit.isFailure(exit)) upstreamFailed.openUnsafe();
966
+ }));
967
+ return Effect.gen(function* () {
968
+ const fiber = yield* Effect.forkChild(consume);
969
+ // A failed upstream ends the loop at once, so the finish timeout starts even while a write is in flight.
970
+ yield* Effect.onInterrupt(Effect.raceFirst(loop, upstreamFailed.await), () => Effect.andThen(Fiber.interrupt(fiber), finish));
971
+ yield* Fiber.interrupt(fiber);
972
+ if (upstream !== undefined && Exit.isFailure(upstream) && (isRunning() || state._tag === "Done")) {
973
+ const cause = upstream.cause;
974
+ state = {
975
+ _tag: "Running"
976
+ };
977
+ yield* finish;
978
+ const error = Cause.findErrorOption(cause);
979
+ if (Option.isNone(error)) return yield* Effect.failCause(cause);
980
+ state = {
981
+ _tag: "Failed",
982
+ cause: {
983
+ _tag: "Upstream",
984
+ error: error.value
985
+ }
986
+ };
987
+ } else if (current.closing !== undefined || inflight !== undefined) {
988
+ // The reply failed already, and the race cut off its stop or write. The finish sends only that stop,
989
+ // within the timeout, since a failed reply writes no more text, and keeps the error it failed with.
990
+ yield* finish;
991
+ }
992
+ yield* Effect.annotateCurrentSpan("chat.markdown.transport", renderedNatively ? "native" : current.transport);
993
+ if (state._tag === "Failed") return yield* Effect.fail(toError(state.cause));
994
+ return {
995
+ parts,
996
+ stopped: state._tag === "Done" && state.stopped
997
+ };
998
+ }).pipe(Effect.ensuring(Scope.close(writes, Exit.void)));
999
+ });
1000
+ // -----------------------------------------------------------------------------
1001
+ // the calls
1002
+ // -----------------------------------------------------------------------------
1003
+ /**
1004
+ * Posts Markdown of any length, split into as many messages as it needs.
1005
+ *
1006
+ * **Details**
1007
+ *
1008
+ * - A part ends at a block boundary: after a blank line or a closed code
1009
+ * fence. A block too long for one message is cut at a line, closing the code
1010
+ * fence it is in and reopening it in the next part. Length is measured with
1011
+ * the adapter's `measure`.
1012
+ * - Each part is posted once and never edited. Parts are paced by
1013
+ * `minWriteInterval`.
1014
+ * - `files` go with the last part, or with the overflow file when there is
1015
+ * one. Blank text after the last cut stays with the last part.
1016
+ * - With `unattachedNote`, a post with files that fails `denied` with
1017
+ * detail `file_refused` is tried once more without them, ending in the note,
1018
+ * or followed by the note when it does not fit. Any failure of that post
1019
+ * fails the call. Without `unattachedNote` the first failure fails the
1020
+ * call. If the overflow file is refused too, the remaining text is split
1021
+ * into messages beyond `maxParts`, without files and ending in the note.
1022
+ * An `uncertain` failure records the part in `uncertain` and posts no note.
1023
+ * - `actions` go with the last part too, which is cut so that
1024
+ * `measure(part, actions) <= limits.messageLength`. The parts before it
1025
+ * are filled without room for them. When not even the text's last grapheme
1026
+ * cluster fits beside the actions, the files and the actions go in a
1027
+ * message of their own after the text. Either can add a part, which counts
1028
+ * against `maxParts`. When `measure("", actions)` is already over, the
1029
+ * call fails `TooLarge` before anything is posted.
1030
+ * - An unattached note posted on its own takes the actions. When they don't
1031
+ * fit beside it, they follow it in a message of their own, which counts
1032
+ * against `maxParts`.
1033
+ * - A `TooLarge` without detail `file_refused` fails the call, even with
1034
+ * `subject: "files"` or text that measured as fitting. Fallback requires
1035
+ * `denied` plus `file_refused`; `subject` never controls it.
1036
+ * - An empty input posts nothing and succeeds with no parts, unless there are
1037
+ * files or actions, which then go out in a message of their own.
1038
+ *
1039
+ * **Gotchas**
1040
+ *
1041
+ * A failure in the middle leaves the parts already posted in place. The error
1042
+ * says what was posted and what was not.
1043
+ *
1044
+ * @stability unstable
1045
+ * @category constructors
1046
+ * @since 4.1.5
1047
+ */
1048
+ export const post = (thread, markdown, options) => Effect.gen(function* () {
1049
+ const chat = yield* Chat.Chat;
1050
+ const failed = error => new ChatMarkdownError({
1051
+ posted: [],
1052
+ remaining: markdown,
1053
+ cause: {
1054
+ _tag: "Chat",
1055
+ error
1056
+ }
1057
+ });
1058
+ const descriptor = yield* Effect.mapError(chat.descriptor(thread.platform), failed);
1059
+ const actions = options?.actions ?? [];
1060
+ if (actions.length > 0 && descriptor.measure("", actions) > descriptor.limits.messageLength) {
1061
+ return yield* Effect.fail(failed(new ChatError({
1062
+ platform: thread.platform,
1063
+ op: "post",
1064
+ reason: new TooLarge({
1065
+ detail: "actions_too_long"
1066
+ })
1067
+ })));
1068
+ }
1069
+ return yield* reply({
1070
+ ...options,
1071
+ thread,
1072
+ initial: markdown,
1073
+ descriptor,
1074
+ chat
1075
+ });
1076
+ }).pipe(Effect.withSpan("ChatMarkdown.post", {
1077
+ attributes: {
1078
+ "chat.platform": thread.platform
1079
+ }
1080
+ }));
1081
+ /**
1082
+ * Writes a stream of Markdown deltas as one reply, as the text arrives.
1083
+ *
1084
+ * **Details**
1085
+ *
1086
+ * - With a `recipient` and an adapter that has a native stream, the reply is
1087
+ * written by native appends. Otherwise, or when the first native start fails
1088
+ * with `Unsupported`, it posts and then edits. The result and the span say
1089
+ * which ran.
1090
+ * - On an adapter without `edit` the reply is buffered instead: each part is
1091
+ * posted once, when it rolls over or the reply ends, with transport
1092
+ * `post`. Nothing shows until then. An upstream failure or an interrupt
1093
+ * posts what was received, as above.
1094
+ * - Text that outgrows a message rolls over into a new one at a block
1095
+ * boundary, and each new message counts against `maxParts`. A native stream
1096
+ * rolls over at the next block boundary past 85% of `limits.streamLength`,
1097
+ * and rotates once it is 80% of `limits.streamAge` old. Every append leaves
1098
+ * room to close the code fence it ends in, and a rollover or rotation closes
1099
+ * it.
1100
+ * - Writes are paced by `minWriteInterval`, on `Clock`. The next write is
1101
+ * scheduled only after the previous one returns, and takes all the text that
1102
+ * arrived meanwhile.
1103
+ * - An append holds back the unfinished last line, unless inside a code fence.
1104
+ * An edit closes an open code fence.
1105
+ * - An intermediate edit that fails `retry` or `uncertain` is dropped, since
1106
+ * the next edit carries the whole text. A `denied` failure, and any failure
1107
+ * of a post or of a part's last edit, fails the reply.
1108
+ * - A native append that fails `retry` is tried again after its
1109
+ * `retryAfter`, or the write interval, whether or not new text arrived.
1110
+ * Three in a row, or one `uncertain` failure, stop the stream, edit the part
1111
+ * to its whole known text, the failed chunk included, and carry on by
1112
+ * post-then-edit. `TooLarge` starts the next part with the chunk.
1113
+ * - `Result.stopped` is true when a user clicked stop on the reply. The
1114
+ * upstream is then interrupted.
1115
+ * - When the upstream fails or the call is interrupted, what was received is
1116
+ * written out, with an open code fence closed, within a short timeout, and
1117
+ * left posted. The timeout starts at once, even while a write is in
1118
+ * flight. A post, start or append in flight is not cut off by the failure
1119
+ * or the interrupt, since sending it again could repeat text: the finalizer
1120
+ * waits for it, then sends what follows, a stop cut off by the interrupt
1121
+ * included. Only the timeout cuts a write off, and its text is then
1122
+ * reported as `uncertain`.
1123
+ *
1124
+ * **Gotchas**
1125
+ *
1126
+ * - There are no files: post them after the result, when it is known whether
1127
+ * the reply stopped.
1128
+ * - There are no actions either. Add them after the result with
1129
+ * `chat.edit(thread, last.messageId, { markdown: last.markdown, actions })`,
1130
+ * where `last` is the final entry of `Result.parts`. Without `edit` they
1131
+ * can't be added to the reply: post them in a message of their own with
1132
+ * `chat.post(thread, { markdown, actions })`.
1133
+ * - To stop a reply, end `deltas` with `Stream.interruptWhen` or
1134
+ * `Stream.haltWhen`, so a `Result` still comes back. Interrupting the call
1135
+ * leaves only the ids collected through `onPart`.
1136
+ * - Native writes go to the adapter through the `Registry`, so a layer that
1137
+ * wraps `Chat` does not see them.
1138
+ * - A native part that is rolled over on `TooLarge` can end inside an open
1139
+ * code fence, since it can no longer be appended to. The next part reopens
1140
+ * the fence.
1141
+ *
1142
+ * @stability unstable
1143
+ * @category constructors
1144
+ * @since 4.1.5
1145
+ */
1146
+ export const stream = (thread, deltas, options) => Effect.gen(function* () {
1147
+ const chat = yield* Chat.Chat;
1148
+ const registry = yield* Registry;
1149
+ const adapter = yield* Effect.mapError(registry.get(thread.platform), error => new ChatMarkdownError({
1150
+ posted: [],
1151
+ remaining: "",
1152
+ cause: {
1153
+ _tag: "Chat",
1154
+ error
1155
+ }
1156
+ }));
1157
+ const native = adapter.descriptor.capabilities.has("nativeStream") ? adapter.nativeStream : undefined;
1158
+ return yield* reply({
1159
+ ...options,
1160
+ thread,
1161
+ initial: "",
1162
+ deltas,
1163
+ descriptor: adapter.descriptor,
1164
+ chat,
1165
+ native
1166
+ });
1167
+ }).pipe(Effect.withSpan("ChatMarkdown.stream", {
1168
+ attributes: {
1169
+ "chat.platform": thread.platform
1170
+ }
1171
+ }));
1172
+ //# sourceMappingURL=ChatMarkdown.js.map