effect 4.1.5 → 4.1.6

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 (309) hide show
  1. package/ai-docs/package.json +2 -2
  2. package/dist/Channel.d.ts +104 -7
  3. package/dist/Channel.d.ts.map +1 -1
  4. package/dist/Channel.js +73 -4
  5. package/dist/Channel.js.map +1 -1
  6. package/dist/Codegen.d.ts +7 -2
  7. package/dist/Codegen.d.ts.map +1 -1
  8. package/dist/Codegen.js +7 -2
  9. package/dist/Codegen.js.map +1 -1
  10. package/dist/Effect.d.ts +139 -18
  11. package/dist/Effect.d.ts.map +1 -1
  12. package/dist/Effect.js +10 -2
  13. package/dist/Effect.js.map +1 -1
  14. package/dist/Equal.d.ts +4 -4
  15. package/dist/Equal.d.ts.map +1 -1
  16. package/dist/Equal.js +78 -65
  17. package/dist/Equal.js.map +1 -1
  18. package/dist/Hash.d.ts.map +1 -1
  19. package/dist/Hash.js +24 -17
  20. package/dist/Hash.js.map +1 -1
  21. package/dist/LayerRef.d.ts +1 -1
  22. package/dist/LayerRef.d.ts.map +1 -1
  23. package/dist/LayerRef.js +3 -3
  24. package/dist/LayerRef.js.map +1 -1
  25. package/dist/Logger.d.ts +37 -13
  26. package/dist/Logger.d.ts.map +1 -1
  27. package/dist/Logger.js +39 -10
  28. package/dist/Logger.js.map +1 -1
  29. package/dist/Match.d.ts +23 -23
  30. package/dist/Match.d.ts.map +1 -1
  31. package/dist/Match.js.map +1 -1
  32. package/dist/Order.js +1 -1
  33. package/dist/Order.js.map +1 -1
  34. package/dist/Schema.d.ts +41 -0
  35. package/dist/Schema.d.ts.map +1 -1
  36. package/dist/Schema.js +51 -390
  37. package/dist/Schema.js.map +1 -1
  38. package/dist/SchemaAST.d.ts.map +1 -1
  39. package/dist/SchemaAST.js +0 -3
  40. package/dist/SchemaAST.js.map +1 -1
  41. package/dist/ScopedCache.d.ts.map +1 -1
  42. package/dist/ScopedCache.js +6 -4
  43. package/dist/ScopedCache.js.map +1 -1
  44. package/dist/Stream.d.ts +128 -5
  45. package/dist/Stream.d.ts.map +1 -1
  46. package/dist/Stream.js +124 -29
  47. package/dist/Stream.js.map +1 -1
  48. package/dist/String.js +1 -1
  49. package/dist/String.js.map +1 -1
  50. package/dist/Tracer.d.ts +10 -0
  51. package/dist/Tracer.d.ts.map +1 -1
  52. package/dist/Tracer.js +2 -0
  53. package/dist/Tracer.js.map +1 -1
  54. package/dist/ai/McpSchema.d.ts +88 -88
  55. package/dist/ai/McpSchema.d.ts.map +1 -1
  56. package/dist/ai/McpServer.d.ts +152 -2
  57. package/dist/ai/McpServer.d.ts.map +1 -1
  58. package/dist/ai/McpServer.js +8 -2
  59. package/dist/ai/McpServer.js.map +1 -1
  60. package/dist/ai/Prompt.d.ts +3 -1
  61. package/dist/ai/Prompt.d.ts.map +1 -1
  62. package/dist/ai/Prompt.js +17 -3
  63. package/dist/ai/Prompt.js.map +1 -1
  64. package/dist/ai/Skill.d.ts +172 -0
  65. package/dist/ai/Skill.d.ts.map +1 -0
  66. package/dist/ai/Skill.js +209 -0
  67. package/dist/ai/Skill.js.map +1 -0
  68. package/dist/ai/index.d.ts +5 -0
  69. package/dist/ai/index.d.ts.map +1 -1
  70. package/dist/ai/index.js +5 -0
  71. package/dist/ai/index.js.map +1 -1
  72. package/dist/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
  73. package/dist/cluster/ClusterWorkflowEngine.js +155 -83
  74. package/dist/cluster/ClusterWorkflowEngine.js.map +1 -1
  75. package/dist/cluster/MessageStorage.d.ts +56 -0
  76. package/dist/cluster/MessageStorage.d.ts.map +1 -1
  77. package/dist/cluster/MessageStorage.js +18 -2
  78. package/dist/cluster/MessageStorage.js.map +1 -1
  79. package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
  80. package/dist/cluster/SqlMessageStorage.js +45 -22
  81. package/dist/cluster/SqlMessageStorage.js.map +1 -1
  82. package/dist/cluster/internal/entityActivation.js +2 -0
  83. package/dist/cluster/internal/entityActivation.js.map +1 -1
  84. package/dist/cluster/internal/entityManager.js +8 -5
  85. package/dist/cluster/internal/entityManager.js.map +1 -1
  86. package/dist/encoding/Base64.js +5 -1
  87. package/dist/encoding/Base64.js.map +1 -1
  88. package/dist/encoding/Base64Url.d.ts.map +1 -1
  89. package/dist/encoding/Base64Url.js +6 -1
  90. package/dist/encoding/Base64Url.js.map +1 -1
  91. package/dist/encoding/Xml.d.ts +38 -9
  92. package/dist/encoding/Xml.d.ts.map +1 -1
  93. package/dist/encoding/Xml.js +63 -31
  94. package/dist/encoding/Xml.js.map +1 -1
  95. package/dist/http/FetchHttpClient.d.ts +2 -1
  96. package/dist/http/FetchHttpClient.d.ts.map +1 -1
  97. package/dist/http/FetchHttpClient.js +14 -10
  98. package/dist/http/FetchHttpClient.js.map +1 -1
  99. package/dist/http/Headers.d.ts +201 -3
  100. package/dist/http/Headers.d.ts.map +1 -1
  101. package/dist/http/Headers.js +205 -3
  102. package/dist/http/Headers.js.map +1 -1
  103. package/dist/http/HttpClient.d.ts.map +1 -1
  104. package/dist/http/HttpClient.js +11 -7
  105. package/dist/http/HttpClient.js.map +1 -1
  106. package/dist/http/HttpClientError.d.ts +1 -0
  107. package/dist/http/HttpClientError.d.ts.map +1 -1
  108. package/dist/http/HttpClientError.js +32 -9
  109. package/dist/http/HttpClientError.js.map +1 -1
  110. package/dist/http/HttpClientRequest.d.ts +46 -0
  111. package/dist/http/HttpClientRequest.d.ts.map +1 -1
  112. package/dist/http/HttpClientRequest.js +15 -0
  113. package/dist/http/HttpClientRequest.js.map +1 -1
  114. package/dist/http/HttpClientResponse.d.ts +132 -0
  115. package/dist/http/HttpClientResponse.d.ts.map +1 -1
  116. package/dist/http/HttpClientResponse.js +83 -0
  117. package/dist/http/HttpClientResponse.js.map +1 -1
  118. package/dist/http/HttpMiddleware.js +1 -1
  119. package/dist/http/HttpMiddleware.js.map +1 -1
  120. package/dist/http/HttpServerResponse.d.ts +118 -0
  121. package/dist/http/HttpServerResponse.d.ts.map +1 -1
  122. package/dist/http/HttpServerResponse.js +43 -0
  123. package/dist/http/HttpServerResponse.js.map +1 -1
  124. package/dist/http/Multipart.d.ts.map +1 -1
  125. package/dist/http/Multipart.js +0 -5
  126. package/dist/http/Multipart.js.map +1 -1
  127. package/dist/http/MultipartParser/internal/contentType.d.ts +6 -0
  128. package/dist/http/MultipartParser/internal/contentType.d.ts.map +1 -1
  129. package/dist/http/MultipartParser/internal/contentType.js +1 -1
  130. package/dist/http/MultipartParser/internal/contentType.js.map +1 -1
  131. package/dist/http/internal/clientPath.d.ts +2 -0
  132. package/dist/http/internal/clientPath.d.ts.map +1 -0
  133. package/dist/http/internal/clientPath.js +24 -0
  134. package/dist/http/internal/clientPath.js.map +1 -0
  135. package/dist/http/internal/redactedPath.js +2 -0
  136. package/dist/http/internal/redactedPath.js.map +1 -1
  137. package/dist/http/internal/tracing.js +5 -3
  138. package/dist/http/internal/tracing.js.map +1 -1
  139. package/dist/http-api/HttpApi.d.ts +2 -2
  140. package/dist/http-api/HttpApi.d.ts.map +1 -1
  141. package/dist/http-api/HttpApi.js +30 -7
  142. package/dist/http-api/HttpApi.js.map +1 -1
  143. package/dist/http-api/HttpApiBuilder.d.ts.map +1 -1
  144. package/dist/http-api/HttpApiBuilder.js +5 -2
  145. package/dist/http-api/HttpApiBuilder.js.map +1 -1
  146. package/dist/http-api/HttpApiClient.d.ts.map +1 -1
  147. package/dist/http-api/HttpApiClient.js +23 -6
  148. package/dist/http-api/HttpApiClient.js.map +1 -1
  149. package/dist/http-api/internal/redactedKeys.d.ts +2 -0
  150. package/dist/http-api/internal/redactedKeys.d.ts.map +1 -0
  151. package/dist/http-api/internal/redactedKeys.js +37 -0
  152. package/dist/http-api/internal/redactedKeys.js.map +1 -0
  153. package/dist/internal/base64.js +34 -2
  154. package/dist/internal/base64.js.map +1 -1
  155. package/dist/internal/dateTime.js +35 -17
  156. package/dist/internal/dateTime.js.map +1 -1
  157. package/dist/internal/effect.js +9 -1
  158. package/dist/internal/effect.js.map +1 -1
  159. package/dist/internal/equal.js +14 -13
  160. package/dist/internal/equal.js.map +1 -1
  161. package/dist/internal/hash.js +4 -1
  162. package/dist/internal/hash.js.map +1 -1
  163. package/dist/internal/matcher.js +17 -17
  164. package/dist/internal/matcher.js.map +1 -1
  165. package/dist/internal/schema/schemaCodegen.js +3 -2
  166. package/dist/internal/schema/schemaCodegen.js.map +1 -1
  167. package/dist/internal/schema/schemaCodegenBuiltIns.d.ts +2 -0
  168. package/dist/internal/schema/schemaCodegenBuiltIns.d.ts.map +1 -0
  169. package/dist/internal/schema/schemaCodegenBuiltIns.js +425 -0
  170. package/dist/internal/schema/schemaCodegenBuiltIns.js.map +1 -0
  171. package/dist/internal/schema/toEquivalence.js +3 -4
  172. package/dist/internal/schema/toEquivalence.js.map +1 -1
  173. package/dist/internal/version.js +1 -1
  174. package/dist/jose/JoseError.d.ts +51 -1
  175. package/dist/jose/JoseError.d.ts.map +1 -1
  176. package/dist/jose/JoseError.js +25 -18
  177. package/dist/jose/JoseError.js.map +1 -1
  178. package/dist/jose/internal/compact.js +10 -5
  179. package/dist/jose/internal/compact.js.map +1 -1
  180. package/dist/observability/Otlp.d.ts +19 -0
  181. package/dist/observability/Otlp.d.ts.map +1 -1
  182. package/dist/observability/Otlp.js +10 -0
  183. package/dist/observability/Otlp.js.map +1 -1
  184. package/dist/observability/OtlpExporter.d.ts +49 -13
  185. package/dist/observability/OtlpExporter.d.ts.map +1 -1
  186. package/dist/observability/OtlpExporter.js +135 -54
  187. package/dist/observability/OtlpExporter.js.map +1 -1
  188. package/dist/observability/OtlpLogger.d.ts +14 -0
  189. package/dist/observability/OtlpLogger.d.ts.map +1 -1
  190. package/dist/observability/OtlpLogger.js +10 -2
  191. package/dist/observability/OtlpLogger.js.map +1 -1
  192. package/dist/observability/OtlpMetrics.d.ts.map +1 -1
  193. package/dist/observability/OtlpMetrics.js +2 -0
  194. package/dist/observability/OtlpMetrics.js.map +1 -1
  195. package/dist/observability/OtlpTracer.d.ts +14 -0
  196. package/dist/observability/OtlpTracer.d.ts.map +1 -1
  197. package/dist/observability/OtlpTracer.js +10 -2
  198. package/dist/observability/OtlpTracer.js.map +1 -1
  199. package/dist/persistence/KeyValueStore.d.ts.map +1 -1
  200. package/dist/persistence/KeyValueStore.js +6 -4
  201. package/dist/persistence/KeyValueStore.js.map +1 -1
  202. package/dist/reactivity/Atom.d.ts.map +1 -1
  203. package/dist/reactivity/Atom.js +10 -9
  204. package/dist/reactivity/Atom.js.map +1 -1
  205. package/dist/rpc/RpcClient.d.ts.map +1 -1
  206. package/dist/rpc/RpcClient.js +32 -1
  207. package/dist/rpc/RpcClient.js.map +1 -1
  208. package/dist/rpc/RpcGroup.d.ts +1 -1
  209. package/dist/rpc/RpcGroup.d.ts.map +1 -1
  210. package/dist/rpc/RpcGroup.js +4 -0
  211. package/dist/rpc/RpcGroup.js.map +1 -1
  212. package/dist/sql/Migrator.d.ts.map +1 -1
  213. package/dist/sql/Migrator.js +2 -5
  214. package/dist/sql/Migrator.js.map +1 -1
  215. package/dist/sql/SqlClient.d.ts.map +1 -1
  216. package/dist/sql/SqlClient.js +5 -1
  217. package/dist/sql/SqlClient.js.map +1 -1
  218. package/dist/workflow/Activity.d.ts +18 -0
  219. package/dist/workflow/Activity.d.ts.map +1 -1
  220. package/dist/workflow/Activity.js +13 -4
  221. package/dist/workflow/Activity.js.map +1 -1
  222. package/dist/workflow/Workflow.d.ts +10 -9
  223. package/dist/workflow/Workflow.d.ts.map +1 -1
  224. package/dist/workflow/Workflow.js +33 -22
  225. package/dist/workflow/Workflow.js.map +1 -1
  226. package/dist/workflow/WorkflowEngine.d.ts.map +1 -1
  227. package/dist/workflow/WorkflowEngine.js +26 -12
  228. package/dist/workflow/WorkflowEngine.js.map +1 -1
  229. package/dist/workflow/internal/retry.d.ts +2 -0
  230. package/dist/workflow/internal/retry.d.ts.map +1 -0
  231. package/dist/workflow/internal/retry.js +11 -0
  232. package/dist/workflow/internal/retry.js.map +1 -0
  233. package/dist/workflow/internal/scope.d.ts +2 -0
  234. package/dist/workflow/internal/scope.d.ts.map +1 -0
  235. package/dist/workflow/internal/scope.js +26 -0
  236. package/dist/workflow/internal/scope.js.map +1 -0
  237. package/package.json +3 -3
  238. package/src/Channel.ts +105 -8
  239. package/src/Codegen.ts +7 -2
  240. package/src/Effect.ts +224 -48
  241. package/src/Equal.ts +85 -79
  242. package/src/Hash.ts +30 -22
  243. package/src/LayerRef.ts +6 -4
  244. package/src/Logger.ts +68 -25
  245. package/src/Match.ts +78 -50
  246. package/src/Order.ts +1 -1
  247. package/src/Schema.ts +79 -296
  248. package/src/SchemaAST.ts +0 -1
  249. package/src/ScopedCache.ts +6 -4
  250. package/src/Stream.ts +167 -33
  251. package/src/String.ts +1 -1
  252. package/src/Tracer.ts +10 -0
  253. package/src/ai/McpServer.ts +161 -4
  254. package/src/ai/Prompt.ts +20 -3
  255. package/src/ai/Skill.ts +236 -0
  256. package/src/ai/index.ts +6 -0
  257. package/src/cluster/ClusterWorkflowEngine.ts +172 -96
  258. package/src/cluster/MessageStorage.ts +51 -2
  259. package/src/cluster/SqlMessageStorage.ts +81 -34
  260. package/src/cluster/internal/entityActivation.ts +6 -0
  261. package/src/cluster/internal/entityManager.ts +8 -4
  262. package/src/encoding/Base64.ts +5 -1
  263. package/src/encoding/Base64Url.ts +6 -1
  264. package/src/encoding/Xml.ts +96 -26
  265. package/src/http/FetchHttpClient.ts +8 -5
  266. package/src/http/Headers.ts +309 -3
  267. package/src/http/HttpClient.ts +19 -7
  268. package/src/http/HttpClientError.ts +42 -9
  269. package/src/http/HttpClientRequest.ts +51 -0
  270. package/src/http/HttpClientResponse.ts +156 -0
  271. package/src/http/HttpMiddleware.ts +1 -1
  272. package/src/http/HttpServerResponse.ts +126 -0
  273. package/src/http/Multipart.ts +0 -5
  274. package/src/http/MultipartParser/internal/contentType.ts +1 -1
  275. package/src/http/internal/clientPath.ts +39 -0
  276. package/src/http/internal/redactedPath.ts +4 -0
  277. package/src/http/internal/tracing.ts +6 -3
  278. package/src/http-api/HttpApi.ts +29 -8
  279. package/src/http-api/HttpApiBuilder.ts +7 -2
  280. package/src/http-api/HttpApiClient.ts +30 -7
  281. package/src/http-api/internal/redactedKeys.ts +40 -0
  282. package/src/internal/base64.ts +57 -2
  283. package/src/internal/dateTime.ts +43 -24
  284. package/src/internal/effect.ts +31 -102
  285. package/src/internal/equal.ts +37 -17
  286. package/src/internal/hash.ts +5 -1
  287. package/src/internal/matcher.ts +34 -113
  288. package/src/internal/schema/schemaCodegen.ts +5 -4
  289. package/src/internal/schema/schemaCodegenBuiltIns.ts +343 -0
  290. package/src/internal/schema/toEquivalence.ts +3 -5
  291. package/src/internal/version.ts +1 -1
  292. package/src/jose/JoseError.ts +34 -0
  293. package/src/jose/internal/compact.ts +14 -5
  294. package/src/observability/Otlp.ts +29 -0
  295. package/src/observability/OtlpExporter.ts +206 -75
  296. package/src/observability/OtlpLogger.ts +24 -2
  297. package/src/observability/OtlpMetrics.ts +2 -0
  298. package/src/observability/OtlpTracer.ts +24 -3
  299. package/src/persistence/KeyValueStore.ts +17 -14
  300. package/src/reactivity/Atom.ts +12 -18
  301. package/src/rpc/RpcClient.ts +26 -1
  302. package/src/rpc/RpcGroup.ts +4 -1
  303. package/src/sql/Migrator.ts +2 -11
  304. package/src/sql/SqlClient.ts +14 -8
  305. package/src/workflow/Activity.ts +28 -4
  306. package/src/workflow/Workflow.ts +68 -44
  307. package/src/workflow/WorkflowEngine.ts +38 -15
  308. package/src/workflow/internal/retry.ts +12 -0
  309. package/src/workflow/internal/scope.ts +36 -0
package/src/SchemaAST.ts CHANGED
@@ -4288,7 +4288,6 @@ export function isFinite(annotations?: Schema.Annotations.Filter) {
4288
4288
  payload: null
4289
4289
  },
4290
4290
  toJsonSchema: () => ({ type: "number" }),
4291
- toCode: () => ({ runtime: "Schema.isFinite()" }),
4292
4291
  arbitraryConstraint: {
4293
4292
  number: "finite"
4294
4293
  },
@@ -339,11 +339,13 @@ export const get: {
339
339
  return effect.interrupt
340
340
  }
341
341
  const oentry = MutableHashMap.get(state.map, key)
342
- if (Option.isSome(oentry) && !hasExpired(oentry.value, fiber)) {
343
- // Move the entry to the end of the map to keep it fresh
342
+ if (Option.isSome(oentry)) {
344
343
  MutableHashMap.remove(state.map, key)
345
- MutableHashMap.set(state.map, key, oentry.value)
346
- return awaitEntry(self, key, oentry.value, restore)
344
+ if (!hasExpired(oentry.value, fiber)) {
345
+ // Move the entry to the end of the map to keep it fresh
346
+ MutableHashMap.set(state.map, key, oentry.value)
347
+ return awaitEntry(self, key, oentry.value, restore)
348
+ }
347
349
  }
348
350
  const clock = fiber.getRef(effect.ClockRef)
349
351
  const scope = Scope.makeUnsafe()
package/src/Stream.ts CHANGED
@@ -8,6 +8,26 @@
8
8
  * stream lifecycle: create a stream, transform or combine it, control buffering
9
9
  * and timing, handle failures, and finally consume it.
10
10
  *
11
+ * Every stream runs in a scope supplied by its consumer, called the stream's
12
+ * scope. The run functions create that scope and close it before they return,
13
+ * however the stream ends: completion, failure, defect, an early stop by a
14
+ * downstream operator such as `take`, or interruption. `toPull` uses the scope
15
+ * it is given and leaves closing it to its owner. Operators that run inner
16
+ * streams, such as `flatMap` and `concat`, may give each inner stream a child
17
+ * scope that closes sooner.
18
+ *
19
+ * `ensuring`, `onExit`, and `scoped` finalize when their stream ends on its own
20
+ * (completion, failure, or defect), before downstream observes that end.
21
+ * `callback` and `unwrap` register finalizers in the stream's scope, so they
22
+ * run when that scope closes. A stream that is abandoned because a downstream
23
+ * operator such as `take` stopped pulling from it is finalized no later than
24
+ * when its scope closes. Under `toPull`, that can be after the pull has
25
+ * returned its final result, so close the scope to release what the stream
26
+ * still holds. `toQueue` and `toPubSub` use the given scope for the queue or
27
+ * pubsub and for the fiber that feeds it. That fiber runs the stream like a run
28
+ * function, so the stream's resources are released when it ends, without
29
+ * waiting for the scope to close.
30
+ *
11
31
  * @stability stable
12
32
  * @since 2.0.0
13
33
  */
@@ -695,9 +715,15 @@ export const toChannel = <A, E, R>(
695
715
  * Use when you need callback-based code to emit stream values by offering to a
696
716
  * `Queue`, or signal stream completion through the `Queue` module APIs.
697
717
  *
698
- * By default it uses an "unbounded" buffer size.
699
- * You can customize the buffer size and strategy by passing an object as the
700
- * second argument with the `bufferSize` and `strategy` fields.
718
+ * **Details**
719
+ *
720
+ * The callback effect runs in the stream's scope. Finalizers it registers, for
721
+ * example with `Effect.acquireRelease`, run when that scope closes, not when
722
+ * the queue ends.
723
+ *
724
+ * By default it uses an "unbounded" buffer size. You can customize the buffer
725
+ * size and strategy by passing an object as the second argument with the
726
+ * `bufferSize` and `strategy` fields.
701
727
  *
702
728
  * **Example** (Creating a stream from a callback that can emit values into a queue)
703
729
  *
@@ -718,6 +744,29 @@ export const toChannel = <A, E, R>(
718
744
  * await Effect.runPromise(Stream.runCollect(stream)) // => [1, 2, 3]
719
745
  * ```
720
746
  *
747
+ * **Example** (Unregistering a listener when the stream scope closes)
748
+ *
749
+ * ```ts import.meta.vitest
750
+ * import { Effect, Queue, Stream } from "effect"
751
+ *
752
+ * const listeners = new Set<(value: number) => void>()
753
+ *
754
+ * const stream = Stream.callback<number>(Effect.fnUntraced(function*(queue) {
755
+ * const listener = (value: number) => {
756
+ * Queue.offerUnsafe(queue, value)
757
+ * }
758
+ * yield* Effect.acquireRelease(
759
+ * Effect.sync(() => listeners.add(listener)),
760
+ * () => Effect.sync(() => listeners.delete(listener))
761
+ * )
762
+ * listeners.forEach((notify) => notify(1))
763
+ * }))
764
+ *
765
+ * const values = await Effect.runPromise(Stream.runCollect(stream.pipe(Stream.take(1))))
766
+ * values // => [1]
767
+ * listeners.size // => 0
768
+ * ```
769
+ *
721
770
  * @stability stable
722
771
  * @category constructors
723
772
  * @since 4.0.0
@@ -1692,6 +1741,11 @@ export const never: Stream<never> = fromChannel(Channel.never)
1692
1741
  /**
1693
1742
  * Creates a stream produced from an `Effect`.
1694
1743
  *
1744
+ * **Details**
1745
+ *
1746
+ * The effect runs in the stream's scope, so finalizers it registers run when
1747
+ * that scope closes.
1748
+ *
1695
1749
  * **Example** (Unwrapping a stream effect)
1696
1750
  *
1697
1751
  * ```ts import.meta.vitest
@@ -1720,6 +1774,14 @@ export const unwrap = <A, E2, R2, E, R>(
1720
1774
  * Runs a stream that requires `Scope` in a managed scope, ensuring its
1721
1775
  * finalizers are run when the stream completes.
1722
1776
  *
1777
+ * **Details**
1778
+ *
1779
+ * The provided scope closes when the stream ends on its own (completion,
1780
+ * failure, or defect), before downstream observes that end. If a downstream
1781
+ * operator stops pulling first, it closes no later than when the stream's scope
1782
+ * closes. It satisfies the `Scope` service requirement only; constructors that
1783
+ * register finalizers in the stream's scope, such as `callback`, keep using it.
1784
+ *
1723
1785
  * **Example** (Scoping a stream)
1724
1786
  *
1725
1787
  * ```ts import.meta.vitest
@@ -17677,6 +17739,12 @@ export const haltWhen: {
17677
17739
  /**
17678
17740
  * Runs the provided finalizer when the stream exits, passing the exit value.
17679
17741
  *
17742
+ * **Details**
17743
+ *
17744
+ * The finalizer runs when the stream ends on its own (completion, failure, or
17745
+ * defect), before downstream observes that end. If a downstream operator stops
17746
+ * pulling first, it runs no later than when the stream's scope closes.
17747
+ *
17680
17748
  * **Example** (Running a finalizer on exit)
17681
17749
  *
17682
17750
  * ```ts import.meta.vitest
@@ -17705,6 +17773,12 @@ export const onExit: {
17705
17773
  /**
17706
17774
  * Runs the provided finalizer when the stream exits, passing the exit value.
17707
17775
  *
17776
+ * **Details**
17777
+ *
17778
+ * The finalizer runs when the stream ends on its own (completion, failure, or
17779
+ * defect), before downstream observes that end. If a downstream operator stops
17780
+ * pulling first, it runs no later than when the stream's scope closes.
17781
+ *
17708
17782
  * **Example** (Running a finalizer on exit)
17709
17783
  *
17710
17784
  * ```ts import.meta.vitest
@@ -17735,6 +17809,12 @@ export const onExit: {
17735
17809
  /**
17736
17810
  * Runs the provided finalizer when the stream exits, passing the exit value.
17737
17811
  *
17812
+ * **Details**
17813
+ *
17814
+ * The finalizer runs when the stream ends on its own (completion, failure, or
17815
+ * defect), before downstream observes that end. If a downstream operator stops
17816
+ * pulling first, it runs no later than when the stream's scope closes.
17817
+ *
17738
17818
  * **Example** (Running a finalizer on exit)
17739
17819
  *
17740
17820
  * ```ts import.meta.vitest
@@ -18121,6 +18201,12 @@ export const onEnd: {
18121
18201
  /**
18122
18202
  * Executes the provided finalizer after this stream's finalizers run.
18123
18203
  *
18204
+ * **Details**
18205
+ *
18206
+ * The finalizer runs when the stream ends on its own (completion, failure, or
18207
+ * defect), before downstream observes that end. If a downstream operator stops
18208
+ * pulling first, it runs no later than when the stream's scope closes.
18209
+ *
18124
18210
  * **Example** (Ensuring finalization)
18125
18211
  *
18126
18212
  * ```ts import.meta.vitest
@@ -18148,6 +18234,12 @@ export const ensuring: {
18148
18234
  /**
18149
18235
  * Executes the provided finalizer after this stream's finalizers run.
18150
18236
  *
18237
+ * **Details**
18238
+ *
18239
+ * The finalizer runs when the stream ends on its own (completion, failure, or
18240
+ * defect), before downstream observes that end. If a downstream operator stops
18241
+ * pulling first, it runs no later than when the stream's scope closes.
18242
+ *
18151
18243
  * **Example** (Ensuring finalization)
18152
18244
  *
18153
18245
  * ```ts import.meta.vitest
@@ -18175,6 +18267,12 @@ export const ensuring: {
18175
18267
  /**
18176
18268
  * Executes the provided finalizer after this stream's finalizers run.
18177
18269
  *
18270
+ * **Details**
18271
+ *
18272
+ * The finalizer runs when the stream ends on its own (completion, failure, or
18273
+ * defect), before downstream observes that end. If a downstream operator stops
18274
+ * pulling first, it runs no later than when the stream's scope closes.
18275
+ *
18178
18276
  * **Example** (Ensuring finalization)
18179
18277
  *
18180
18278
  * ```ts import.meta.vitest
@@ -20062,8 +20160,9 @@ export const runDrain = <A, E, R>(self: Stream<A, E, R>): Effect.Effect<void, E,
20062
20160
  *
20063
20161
  * **Details**
20064
20162
  *
20065
- * The pull fails with `Cause.Done` when the stream ends and with the stream
20066
- * error on failure.
20163
+ * The stream runs in the provided scope, which the pull leaves open; close it
20164
+ * to release any resources the stream still holds. The pull fails with
20165
+ * `Cause.Done` when the stream ends and with the stream error on failure.
20067
20166
  *
20068
20167
  * **Example** (Creating a scoped pull)
20069
20168
  *
@@ -20621,11 +20720,13 @@ export const toAsyncIterableWith: {
20621
20720
  ): AsyncIterable<A> => ({
20622
20721
  [Symbol.asyncIterator]() {
20623
20722
  const runPromise = Effect.runPromiseWith(context)
20624
- const runFork = Effect.runForkWith(context)
20723
+ const runPromiseExit = Effect.runPromiseExitWith(context)
20625
20724
  const scope = Scope.makeUnsafe()
20626
20725
  let pull: Pull.Pull<Arr.NonEmptyReadonlyArray<A>, E, void, R> | undefined
20627
20726
  let currentIter: Iterator<A> | undefined
20628
- let currentFiber: Fiber.Fiber<Arr.NonEmptyReadonlyArray<A>, E | Cause.Done<void>> | undefined
20727
+ let currentFiber: Fiber.Fiber<unknown, unknown> | undefined
20728
+ let reading = false
20729
+ const queuedReads: Array<() => void> = []
20629
20730
  let closePromise: Promise<IteratorResult<A>> | undefined
20630
20731
  const close = (exit: Exit.Exit<unknown, unknown>): Promise<IteratorResult<A>> => {
20631
20732
  if (closePromise) return closePromise
@@ -20646,37 +20747,46 @@ export const toAsyncIterableWith: {
20646
20747
  await runPromise(Effect.logError("Suppressed error while closing Stream async iterator", error))
20647
20748
  }
20648
20749
  }
20750
+ // Register the fiber before pulling so a reentrant close can interrupt it.
20751
+ const pullChunk = Effect.withFiber((fiber) => {
20752
+ currentFiber = fiber
20753
+ return pull ??
20754
+ Effect.flatMap(Channel.toPullScoped(self.channel, scope), (nextPull) => {
20755
+ pull = nextPull
20756
+ return nextPull
20757
+ })
20758
+ })
20649
20759
  return {
20650
20760
  async next(): Promise<IteratorResult<A>> {
20651
- if (closePromise) return closePromise
20652
- if (currentIter) {
20653
- const next = currentIter.next()
20654
- if (!next.done) return next
20655
- currentIter = undefined
20761
+ if (reading) {
20762
+ await new Promise<void>((resolve) => queuedReads.push(resolve))
20656
20763
  }
20657
- const fiber = runFork(
20658
- pull ??
20659
- Effect.flatMap(Channel.toPullScoped(self.channel, scope), (nextPull) => {
20660
- pull = nextPull
20661
- return nextPull
20662
- })
20663
- )
20664
- currentFiber = fiber
20665
- const exit = await runPromise(Fiber.await(fiber))
20666
- if (currentFiber === fiber) {
20764
+ reading = true
20765
+ try {
20766
+ if (closePromise) return closePromise
20767
+ if (currentIter) {
20768
+ const next = currentIter.next()
20769
+ if (!next.done) return next
20770
+ currentIter = undefined
20771
+ }
20772
+ const exit = await runPromiseExit(pullChunk)
20667
20773
  currentFiber = undefined
20774
+ if (Exit.isSuccess(exit)) {
20775
+ currentIter = exit.value[Symbol.iterator]()
20776
+ return currentIter.next()
20777
+ } else if (Pull.isDoneCause(exit.cause)) {
20778
+ return close(Exit.void)
20779
+ }
20780
+ if (closePromise && Cause.hasInterruptsOnly(exit.cause)) {
20781
+ return closePromise
20782
+ }
20783
+ await closeAndReportError(exit)
20784
+ throw Cause.squash(exit.cause)
20785
+ } finally {
20786
+ const resume = queuedReads.shift()
20787
+ if (resume) resume()
20788
+ else reading = false
20668
20789
  }
20669
- if (Exit.isSuccess(exit)) {
20670
- currentIter = exit.value[Symbol.iterator]()
20671
- return currentIter.next()
20672
- } else if (Pull.isDoneCause(exit.cause)) {
20673
- return close(Exit.void)
20674
- }
20675
- if (closePromise && Cause.hasInterruptsOnly(exit.cause)) {
20676
- return closePromise
20677
- }
20678
- await closeAndReportError(exit)
20679
- throw Cause.squash(exit.cause)
20680
20790
  },
20681
20791
  return() {
20682
20792
  return close(Exit.void)
@@ -20869,6 +20979,10 @@ export const runIntoPubSub: {
20869
20979
  *
20870
20980
  * **Details**
20871
20981
  *
20982
+ * The surrounding scope owns the PubSub and the fiber that feeds it. The stream
20983
+ * runs like a run function inside that fiber, so its finalizers run when it
20984
+ * ends, or when the fiber is interrupted by the scope closing.
20985
+ *
20872
20986
  * `shutdownOnEnd` indicates whether the PubSub should be shut down when the
20873
20987
  * stream ends. By default this is `true`.
20874
20988
  *
@@ -20899,6 +21013,10 @@ export const toPubSub: {
20899
21013
  *
20900
21014
  * **Details**
20901
21015
  *
21016
+ * The surrounding scope owns the PubSub and the fiber that feeds it. The stream
21017
+ * runs like a run function inside that fiber, so its finalizers run when it
21018
+ * ends, or when the fiber is interrupted by the scope closing.
21019
+ *
20902
21020
  * `shutdownOnEnd` indicates whether the PubSub should be shut down when the
20903
21021
  * stream ends. By default this is `true`.
20904
21022
  *
@@ -20940,6 +21058,10 @@ export const toPubSub: {
20940
21058
  *
20941
21059
  * **Details**
20942
21060
  *
21061
+ * The surrounding scope owns the PubSub and the fiber that feeds it. The stream
21062
+ * runs like a run function inside that fiber, so its finalizers run when it
21063
+ * ends, or when the fiber is interrupted by the scope closing.
21064
+ *
20943
21065
  * `shutdownOnEnd` indicates whether the PubSub should be shut down when the
20944
21066
  * stream ends. By default this is `true`.
20945
21067
  *
@@ -21134,6 +21256,10 @@ export const toPubSubTake: {
21134
21256
  *
21135
21257
  * **Details**
21136
21258
  *
21259
+ * The surrounding scope owns the queue and the fiber that feeds it. The stream
21260
+ * runs like a run function inside that fiber, so its finalizers run when it
21261
+ * ends, or when the fiber is interrupted by the scope closing.
21262
+ *
21137
21263
  * Elements are offered to the queue as the stream runs. Stream completion is
21138
21264
  * signaled with `Cause.Done`, stream failures fail the queue, and the queue is
21139
21265
  * shut down when the surrounding scope closes.
@@ -21162,6 +21288,10 @@ export const toQueue: {
21162
21288
  *
21163
21289
  * **Details**
21164
21290
  *
21291
+ * The surrounding scope owns the queue and the fiber that feeds it. The stream
21292
+ * runs like a run function inside that fiber, so its finalizers run when it
21293
+ * ends, or when the fiber is interrupted by the scope closing.
21294
+ *
21165
21295
  * Elements are offered to the queue as the stream runs. Stream completion is
21166
21296
  * signaled with `Cause.Done`, stream failures fail the queue, and the queue is
21167
21297
  * shut down when the surrounding scope closes.
@@ -21197,6 +21327,10 @@ export const toQueue: {
21197
21327
  *
21198
21328
  * **Details**
21199
21329
  *
21330
+ * The surrounding scope owns the queue and the fiber that feeds it. The stream
21331
+ * runs like a run function inside that fiber, so its finalizers run when it
21332
+ * ends, or when the fiber is interrupted by the scope closing.
21333
+ *
21200
21334
  * Elements are offered to the queue as the stream runs. Stream completion is
21201
21335
  * signaled with `Cause.Done`, stream failures fail the queue, and the queue is
21202
21336
  * shut down when the surrounding scope closes.
package/src/String.ts CHANGED
@@ -1678,7 +1678,7 @@ class LinesIterator implements IterableIterator<string> {
1678
1678
  }
1679
1679
 
1680
1680
  [Symbol.iterator](): IterableIterator<string> {
1681
- return new LinesIterator(this.s, this.stripped)
1681
+ return this
1682
1682
  }
1683
1683
 
1684
1684
  private get done(): boolean {
package/src/Tracer.ts CHANGED
@@ -182,6 +182,14 @@ export class ParentSpan extends Context.Service<ParentSpan, AnySpan>()(ParentSpa
182
182
  * identifiers, sampling state, and annotations so it can be used as a parent or
183
183
  * link in Effect tracing.
184
184
  *
185
+ * **Details**
186
+ *
187
+ * An external span identifies a parent or link only. Effect never records on
188
+ * it: it has no attributes, events, or end. While the nearest parent span is
189
+ * external, `Effect.currentSpan` fails, `Effect.annotateCurrentSpan` does
190
+ * nothing, and `Logger.tracerLogger` records nothing. Open a span with
191
+ * `Effect.withSpan` to get one that records.
192
+ *
185
193
  * **Example** (Creating an external span value)
186
194
  *
187
195
  * ```ts import.meta.vitest
@@ -514,6 +522,8 @@ export const make = (options: Tracer): Tracer => options
514
522
  * spans.map((span) => Option.getOrUndefined(span.parent)?.spanId) // => ["span-abc-123"]
515
523
  * ```
516
524
  *
525
+ * @see {@link ExternalSpan}
526
+ *
517
527
  * @stability stable
518
528
  * @category constructors
519
529
  * @since 2.0.0
@@ -97,6 +97,7 @@ import type {
97
97
  ServerCapabilities
98
98
  } from "./McpSchema.ts"
99
99
  import * as AiPrompt from "./Prompt.ts"
100
+ import * as Skill from "./Skill.ts"
100
101
  import * as Tool from "./Tool.ts"
101
102
  import * as Toolkit from "./Toolkit.ts"
102
103
 
@@ -4011,6 +4012,16 @@ export interface SkillOptions {
4011
4012
  readonly annotations?: Context.Context<never> | undefined
4012
4013
  }
4013
4014
 
4015
+ /**
4016
+ * Options for registering a {@link Skill.Skill} with {@link registerSkill} or
4017
+ * {@link skill}: {@link SkillOptions} without `files`, which come from the skill.
4018
+ *
4019
+ * @stability unstable
4020
+ * @category models
4021
+ * @since 4.1.6
4022
+ */
4023
+ export type SkillRegistration = Omit<SkillOptions, "files">
4024
+
4014
4025
  const skillFileEncoder = new TextEncoder()
4015
4026
 
4016
4027
  // Bytes are copied so that later changes by the caller cannot alter what is
@@ -4025,6 +4036,9 @@ const toSkillSourceFile = (path: string, file: SkillFile): McpSkills.SourceFile
4025
4036
  }
4026
4037
  }
4027
4038
 
4039
+ const toSkillOptions = (skillOrOptions: Skill.Skill | SkillOptions, registration?: SkillRegistration): SkillOptions =>
4040
+ skillOrOptions instanceof Skill.Skill ? { ...registration!, files: Skill.toFiles(skillOrOptions) } : skillOrOptions
4041
+
4028
4042
  /**
4029
4043
  * Registers an Agent Skill under the MCP Skills extension (SEP-2640).
4030
4044
  *
@@ -4052,8 +4066,68 @@ const toSkillSourceFile = (path: string, file: SkillFile): McpSkills.SourceFile
4052
4066
  * @category handlers
4053
4067
  * @since 4.1.1
4054
4068
  */
4055
- export const registerSkill: (options: SkillOptions) => Effect.Effect<void, Cause.IllegalArgumentError, McpServer> =
4056
- Effect.fnUntraced(function*(options: SkillOptions) {
4069
+ export const registerSkill: {
4070
+ /**
4071
+ * Registers an Agent Skill under the MCP Skills extension (SEP-2640).
4072
+ *
4073
+ * **Details**
4074
+ *
4075
+ * Each file is registered as a resource, and the skill is listed by
4076
+ * `skills/list` and `skills/get` with a manifest of every file's SHA-256
4077
+ * digest and size, computed from the bytes served. Every directory of the
4078
+ * skill namespace is readable with `resources/directory/read`. The server
4079
+ * declares the extension once any skill is registered.
4080
+ *
4081
+ * Fails when the skill breaks the extension's rules: a name that does not
4082
+ * match the URI, a `SKILL.md` without YAML frontmatter, frontmatter that is
4083
+ * not valid {@link McpSchema.SkillFrontmatter}, a file path that leaves the
4084
+ * skill, or more than 512 files or 16 MiB in all. It also fails when the
4085
+ * skill's directory contains or lies inside another skill's, or one of its
4086
+ * files is already registered as a resource. Registering a resource at the
4087
+ * URI of a skill's file afterwards is a defect.
4088
+ *
4089
+ * SHA-256 runs on `crypto.subtle`.
4090
+ *
4091
+ * @see {@link skill} for the layer-based skill registration wrapper
4092
+ *
4093
+ * @stability unstable
4094
+ * @category handlers
4095
+ * @since 4.1.1
4096
+ */
4097
+ (skill: Skill.Skill, options: SkillRegistration): Effect.Effect<void, Cause.IllegalArgumentError, McpServer>
4098
+ /**
4099
+ * Registers an Agent Skill under the MCP Skills extension (SEP-2640).
4100
+ *
4101
+ * **Details**
4102
+ *
4103
+ * Each file is registered as a resource, and the skill is listed by
4104
+ * `skills/list` and `skills/get` with a manifest of every file's SHA-256
4105
+ * digest and size, computed from the bytes served. Every directory of the
4106
+ * skill namespace is readable with `resources/directory/read`. The server
4107
+ * declares the extension once any skill is registered.
4108
+ *
4109
+ * Fails when the skill breaks the extension's rules: a name that does not
4110
+ * match the URI, a `SKILL.md` without YAML frontmatter, frontmatter that is
4111
+ * not valid {@link McpSchema.SkillFrontmatter}, a file path that leaves the
4112
+ * skill, or more than 512 files or 16 MiB in all. It also fails when the
4113
+ * skill's directory contains or lies inside another skill's, or one of its
4114
+ * files is already registered as a resource. Registering a resource at the
4115
+ * URI of a skill's file afterwards is a defect.
4116
+ *
4117
+ * SHA-256 runs on `crypto.subtle`.
4118
+ *
4119
+ * @see {@link skill} for the layer-based skill registration wrapper
4120
+ *
4121
+ * @stability unstable
4122
+ * @category handlers
4123
+ * @since 4.1.1
4124
+ */
4125
+ (options: SkillOptions): Effect.Effect<void, Cause.IllegalArgumentError, McpServer>
4126
+ } = (skillOrOptions: Skill.Skill | SkillOptions, registration?: SkillRegistration) =>
4127
+ registerSkillFiles(toSkillOptions(skillOrOptions, registration))
4128
+
4129
+ const registerSkillFiles: (options: SkillOptions) => Effect.Effect<void, Cause.IllegalArgumentError, McpServer> = Effect
4130
+ .fnUntraced(function*(options: SkillOptions) {
4057
4131
  const files = Object.entries(options.files).map(([path, file]) => toSkillSourceFile(path, file))
4058
4132
  const skill = yield* McpSkills.makeStaticSkill({ uri: options.uri, files }).pipe(
4059
4133
  Effect.provide(Digest.layerWebCrypto)
@@ -4131,8 +4205,91 @@ export const registerSkill: (options: SkillOptions) => Effect.Effect<void, Cause
4131
4205
  * @category layers
4132
4206
  * @since 4.1.1
4133
4207
  */
4134
- export const skill = (options: SkillOptions): Layer.Layer<never, Cause.IllegalArgumentError, McpServer> =>
4135
- Layer.effectDiscard(registerSkill(options))
4208
+ export const skill: {
4209
+ /**
4210
+ * Creates a layer that registers an Agent Skill under the MCP Skills extension
4211
+ * (SEP-2640).
4212
+ *
4213
+ * **Example** (Publishing a skill with a supporting file)
4214
+ *
4215
+ * ```ts
4216
+ * import { Layer } from "effect"
4217
+ * import { McpProtocol, McpServer } from "effect/ai"
4218
+ *
4219
+ * const RefundsSkill = McpServer.skill({
4220
+ * uri: "skill://billing/refunds/SKILL.md",
4221
+ * files: {
4222
+ * "SKILL.md": `---
4223
+ * name: refunds
4224
+ * description: Process refunds using the billing tools
4225
+ * ---
4226
+ *
4227
+ * Read references/policy.md before issuing a refund.
4228
+ * `,
4229
+ * "references/policy.md": "Refunds above CHF 500 require manager approval.\n"
4230
+ * }
4231
+ * })
4232
+ *
4233
+ * const ServerLayer = RefundsSkill.pipe(
4234
+ * Layer.provide(McpServer.layerHttp({
4235
+ * name: "billing",
4236
+ * version: "1.0.0",
4237
+ * path: "/mcp",
4238
+ * protocols: [McpProtocol.v2026_07_28, McpProtocol.v2025_11_25]
4239
+ * }))
4240
+ * )
4241
+ * ```
4242
+ *
4243
+ * @see {@link registerSkill} for the Effect-level skill registration API
4244
+ *
4245
+ * @stability unstable
4246
+ * @category layers
4247
+ * @since 4.1.1
4248
+ */
4249
+ (skill: Skill.Skill, options: SkillRegistration): Layer.Layer<never, Cause.IllegalArgumentError, McpServer>
4250
+ /**
4251
+ * Creates a layer that registers an Agent Skill under the MCP Skills extension
4252
+ * (SEP-2640).
4253
+ *
4254
+ * **Example** (Publishing a skill with a supporting file)
4255
+ *
4256
+ * ```ts
4257
+ * import { Layer } from "effect"
4258
+ * import { McpProtocol, McpServer } from "effect/ai"
4259
+ *
4260
+ * const RefundsSkill = McpServer.skill({
4261
+ * uri: "skill://billing/refunds/SKILL.md",
4262
+ * files: {
4263
+ * "SKILL.md": `---
4264
+ * name: refunds
4265
+ * description: Process refunds using the billing tools
4266
+ * ---
4267
+ *
4268
+ * Read references/policy.md before issuing a refund.
4269
+ * `,
4270
+ * "references/policy.md": "Refunds above CHF 500 require manager approval.\n"
4271
+ * }
4272
+ * })
4273
+ *
4274
+ * const ServerLayer = RefundsSkill.pipe(
4275
+ * Layer.provide(McpServer.layerHttp({
4276
+ * name: "billing",
4277
+ * version: "1.0.0",
4278
+ * path: "/mcp",
4279
+ * protocols: [McpProtocol.v2026_07_28, McpProtocol.v2025_11_25]
4280
+ * }))
4281
+ * )
4282
+ * ```
4283
+ *
4284
+ * @see {@link registerSkill} for the Effect-level skill registration API
4285
+ *
4286
+ * @stability unstable
4287
+ * @category layers
4288
+ * @since 4.1.1
4289
+ */
4290
+ (options: SkillOptions): Layer.Layer<never, Cause.IllegalArgumentError, McpServer>
4291
+ } = (skillOrOptions: Skill.Skill | SkillOptions, registration?: SkillRegistration) =>
4292
+ Layer.effectDiscard(registerSkillFiles(toSkillOptions(skillOrOptions, registration)))
4136
4293
 
4137
4294
  /**
4138
4295
  * Registers an MCP prompt from an Effect program.
package/src/ai/Prompt.ts CHANGED
@@ -2204,7 +2204,9 @@ const mergeOptions = (left: ProviderOptions, right: ProviderOptions): ProviderOp
2204
2204
  * reasoning streams into assistant parts, preserving provider metadata as
2205
2205
  * prompt options, placing tool calls and approval requests in an assistant
2206
2206
  * message, and placing non-preliminary tool results in a tool message using
2207
- * their encoded results.
2207
+ * their encoded results. Results for tool calls from an earlier message, such
2208
+ * as resolved approvals, are placed in a tool message before the assistant
2209
+ * message.
2208
2210
  *
2209
2211
  * **Example** (Creating prompts from response parts)
2210
2212
  *
@@ -2248,6 +2250,13 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
2248
2250
 
2249
2251
  const assistantParts: Array<AssistantMessagePart | undefined> = []
2250
2252
  const toolParts: Array<ToolMessagePart> = []
2253
+ const priorToolParts: Array<ToolMessagePart> = []
2254
+ const toolCallIds = new Set<string>()
2255
+ for (const part of parts) {
2256
+ if (part.type === "tool-call") {
2257
+ toolCallIds.add(part.id)
2258
+ }
2259
+ }
2251
2260
 
2252
2261
  const activeTextDeltas = new Map<string, { index: number; text: string; options: ProviderOptions }>()
2253
2262
  const activeReasoningDeltas = new Map<string, { index: number; text: string; options: ProviderOptions }>()
@@ -2339,7 +2348,11 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
2339
2348
  // Tool Result Parts (skip preliminary results)
2340
2349
  case "tool-result": {
2341
2350
  if (part.preliminary !== true) {
2342
- const target = part.providerExecuted === true ? assistantParts : toolParts
2351
+ const target = part.providerExecuted === true
2352
+ ? assistantParts
2353
+ : toolCallIds.has(part.id)
2354
+ ? toolParts
2355
+ : priorToolParts
2343
2356
  target.push(makePart("tool-result", {
2344
2357
  id: part.id,
2345
2358
  name: part.name,
@@ -2374,12 +2387,16 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
2374
2387
 
2375
2388
  const assistantContent = assistantParts.filter((part) => part !== undefined)
2376
2389
 
2377
- if (assistantContent.length === 0 && toolParts.length === 0) {
2390
+ if (assistantContent.length === 0 && toolParts.length === 0 && priorToolParts.length === 0) {
2378
2391
  return empty
2379
2392
  }
2380
2393
 
2381
2394
  const messages: Array<Message> = []
2382
2395
 
2396
+ if (priorToolParts.length > 0) {
2397
+ messages.push(makeMessage("tool", { content: priorToolParts }))
2398
+ }
2399
+
2383
2400
  if (assistantContent.length > 0) {
2384
2401
  messages.push(makeMessage("assistant", { content: assistantContent }))
2385
2402
  }