@effect-uai/core 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +1 -1
  2. package/dist/{Items-BH8xUkoR.d.mts → Items-pXR5BAgW.d.mts} +12 -12
  3. package/dist/Items-pXR5BAgW.d.mts.map +1 -0
  4. package/dist/SandboxError-BvKypkJm.d.mts +158 -0
  5. package/dist/SandboxError-BvKypkJm.d.mts.map +1 -0
  6. package/dist/{Tool-87ViKCCO.d.mts → Tool-DP-AwKOf.d.mts} +11 -11
  7. package/dist/{Tool-87ViKCCO.d.mts.map → Tool-DP-AwKOf.d.mts.map} +1 -1
  8. package/dist/domain/Items.d.mts +2 -2
  9. package/dist/domain/Items.mjs +15 -15
  10. package/dist/domain/Items.mjs.map +1 -1
  11. package/dist/domain/Turn.d.mts +388 -2
  12. package/dist/domain/Turn.d.mts.map +1 -0
  13. package/dist/domain/Turn.mjs +13 -13
  14. package/dist/domain/Turn.mjs.map +1 -1
  15. package/dist/index.d.mts +12 -6
  16. package/dist/index.mjs +9 -3
  17. package/dist/language-model/LanguageModel.d.mts +29 -27
  18. package/dist/language-model/LanguageModel.d.mts.map +1 -1
  19. package/dist/language-model/LanguageModel.mjs +16 -25
  20. package/dist/language-model/LanguageModel.mjs.map +1 -1
  21. package/dist/language-model/LanguageModel.test.mjs +10 -81
  22. package/dist/language-model/LanguageModel.test.mjs.map +1 -1
  23. package/dist/loop/Loop.d.mts +37 -90
  24. package/dist/loop/Loop.d.mts.map +1 -1
  25. package/dist/loop/Loop.mjs +39 -93
  26. package/dist/loop/Loop.mjs.map +1 -1
  27. package/dist/loop/Loop.test.mjs +46 -54
  28. package/dist/loop/Loop.test.mjs.map +1 -1
  29. package/dist/retry/Retry.d.mts +64 -0
  30. package/dist/retry/Retry.d.mts.map +1 -0
  31. package/dist/retry/Retry.mjs +69 -0
  32. package/dist/retry/Retry.mjs.map +1 -0
  33. package/dist/retry/Retry.test.mjs +142 -0
  34. package/dist/retry/Retry.test.mjs.map +1 -0
  35. package/dist/sandbox/Memory.d.mts +53 -0
  36. package/dist/sandbox/Memory.d.mts.map +1 -0
  37. package/dist/sandbox/Memory.mjs +68 -0
  38. package/dist/sandbox/Memory.mjs.map +1 -0
  39. package/dist/sandbox/Sandbox.d.mts +717 -0
  40. package/dist/sandbox/Sandbox.d.mts.map +1 -0
  41. package/dist/sandbox/Sandbox.mjs +192 -0
  42. package/dist/sandbox/Sandbox.mjs.map +1 -0
  43. package/dist/sandbox/SandboxError.d.mts +2 -0
  44. package/dist/sandbox/SandboxError.mjs +89 -0
  45. package/dist/sandbox/SandboxError.mjs.map +1 -0
  46. package/dist/sandbox/SandboxImage.d.mts +28 -0
  47. package/dist/sandbox/SandboxImage.d.mts.map +1 -0
  48. package/dist/sandbox/SandboxImage.mjs +32 -0
  49. package/dist/sandbox/SandboxImage.mjs.map +1 -0
  50. package/dist/sandbox/SandboxNetwork.d.mts +34 -0
  51. package/dist/sandbox/SandboxNetwork.d.mts.map +1 -0
  52. package/dist/sandbox/SandboxNetwork.mjs +36 -0
  53. package/dist/sandbox/SandboxNetwork.mjs.map +1 -0
  54. package/dist/speech-synthesizer/SpeechSynthesizer.d.mts +98 -2
  55. package/dist/speech-synthesizer/SpeechSynthesizer.d.mts.map +1 -1
  56. package/dist/speech-synthesizer/SpeechSynthesizer.mjs +32 -2
  57. package/dist/speech-synthesizer/SpeechSynthesizer.mjs.map +1 -1
  58. package/dist/speech-synthesizer/SpeechSynthesizer.test.mjs +56 -2
  59. package/dist/speech-synthesizer/SpeechSynthesizer.test.mjs.map +1 -1
  60. package/dist/testing/MockProvider.d.mts +3 -3
  61. package/dist/testing/MockProvider.d.mts.map +1 -1
  62. package/dist/testing/MockProvider.mjs +14 -8
  63. package/dist/testing/MockProvider.mjs.map +1 -1
  64. package/dist/testing/MockSandbox.d.mts +604 -0
  65. package/dist/testing/MockSandbox.d.mts.map +1 -0
  66. package/dist/testing/MockSandbox.mjs +160 -0
  67. package/dist/testing/MockSandbox.mjs.map +1 -0
  68. package/dist/testing/MockSpeechSynthesizer.d.mts +26 -11
  69. package/dist/testing/MockSpeechSynthesizer.d.mts.map +1 -1
  70. package/dist/testing/MockSpeechSynthesizer.mjs +75 -51
  71. package/dist/testing/MockSpeechSynthesizer.mjs.map +1 -1
  72. package/dist/tool/{Resolvers.d.mts → Approval.d.mts} +27 -27
  73. package/dist/tool/Approval.d.mts.map +1 -0
  74. package/dist/tool/{Resolvers.mjs → Approval.mjs} +18 -18
  75. package/dist/tool/Approval.mjs.map +1 -0
  76. package/dist/tool/Approval.test.d.mts +1 -0
  77. package/dist/tool/{Resolvers.test.mjs → Approval.test.mjs} +40 -40
  78. package/dist/tool/Approval.test.mjs.map +1 -0
  79. package/dist/tool/HistoryCheck.d.mts +6 -6
  80. package/dist/tool/HistoryCheck.d.mts.map +1 -1
  81. package/dist/tool/HistoryCheck.mjs +5 -5
  82. package/dist/tool/HistoryCheck.mjs.map +1 -1
  83. package/dist/tool/Tool.d.mts +2 -2
  84. package/dist/tool/Tool.mjs +4 -4
  85. package/dist/tool/Tool.mjs.map +1 -1
  86. package/dist/tool/ToolEvent.d.mts +19 -19
  87. package/dist/tool/ToolEvent.d.mts.map +1 -1
  88. package/dist/tool/ToolEvent.mjs +8 -8
  89. package/dist/tool/ToolEvent.mjs.map +1 -1
  90. package/dist/tool/{Outcome.d.mts → ToolResult.d.mts} +29 -29
  91. package/dist/tool/ToolResult.d.mts.map +1 -0
  92. package/dist/tool/ToolResult.mjs +61 -0
  93. package/dist/tool/ToolResult.mjs.map +1 -0
  94. package/dist/tool/Toolkit.d.mts +58 -26
  95. package/dist/tool/Toolkit.d.mts.map +1 -1
  96. package/dist/tool/Toolkit.mjs +62 -34
  97. package/dist/tool/Toolkit.mjs.map +1 -1
  98. package/dist/tool/Toolkit.test.mjs +13 -13
  99. package/dist/tool/Toolkit.test.mjs.map +1 -1
  100. package/package.json +35 -7
  101. package/src/domain/Items.ts +9 -9
  102. package/src/domain/Turn.ts +16 -16
  103. package/src/index.ts +8 -2
  104. package/src/language-model/LanguageModel.test.ts +21 -102
  105. package/src/language-model/LanguageModel.ts +36 -59
  106. package/src/loop/Loop.test.ts +93 -91
  107. package/src/loop/Loop.ts +72 -170
  108. package/src/retry/Retry.test.ts +163 -0
  109. package/src/retry/Retry.ts +84 -0
  110. package/src/sandbox/Memory.ts +128 -0
  111. package/src/sandbox/Sandbox.ts +639 -0
  112. package/src/sandbox/SandboxError.ts +140 -0
  113. package/src/sandbox/SandboxImage.ts +26 -0
  114. package/src/sandbox/SandboxNetwork.ts +34 -0
  115. package/src/speech-synthesizer/SpeechSynthesizer.test.ts +89 -0
  116. package/src/speech-synthesizer/SpeechSynthesizer.ts +124 -0
  117. package/src/testing/MockProvider.ts +31 -25
  118. package/src/testing/MockSandbox.ts +342 -0
  119. package/src/testing/MockSpeechSynthesizer.ts +137 -77
  120. package/src/tool/{Resolvers.test.ts → Approval.test.ts} +45 -54
  121. package/src/tool/{Resolvers.ts → Approval.ts} +25 -25
  122. package/src/tool/HistoryCheck.ts +10 -13
  123. package/src/tool/Tool.ts +13 -15
  124. package/src/tool/ToolEvent.ts +7 -7
  125. package/src/tool/{Outcome.ts → ToolResult.ts} +20 -20
  126. package/src/tool/Toolkit.test.ts +13 -13
  127. package/src/tool/Toolkit.ts +97 -73
  128. package/dist/Items-BH8xUkoR.d.mts.map +0 -1
  129. package/dist/Turn-0CwCAyVe.d.mts +0 -388
  130. package/dist/Turn-0CwCAyVe.d.mts.map +0 -1
  131. package/dist/tool/Outcome.d.mts.map +0 -1
  132. package/dist/tool/Outcome.mjs +0 -61
  133. package/dist/tool/Outcome.mjs.map +0 -1
  134. package/dist/tool/Resolvers.d.mts.map +0 -1
  135. package/dist/tool/Resolvers.mjs.map +0 -1
  136. package/dist/tool/Resolvers.test.mjs.map +0 -1
  137. /package/dist/{tool/Resolvers.test.d.mts → retry/Retry.test.d.mts} +0 -0
@@ -6,10 +6,10 @@ import { Array, Cause, Channel, Data, Effect, Exit, Function, Match, Option, Ref
6
6
  /**
7
7
  * Pull-based `loop` for state-threaded sub-streams.
8
8
  *
9
- * Each iteration runs a body that returns a `Stream<Event<A, S>>`. The body
9
+ * Each iteration runs a body that returns a `Stream<Step<A, S>>`. The body
10
10
  * emits values via `Loop.value(a)` and signals iteration control via
11
- * `Loop.next(state)` (continue with new state) or `Loop.stop` (terminate).
12
- * The loop unwraps `Value` events back to `A` for downstream consumers, so
11
+ * `Loop.next(state)` (continue with new state) or `Loop.stop()` (terminate).
12
+ * The loop unwraps `Value` steps back to `A` for downstream consumers, so
13
13
  * the resulting stream is a plain `Stream<A>`.
14
14
  *
15
15
  * The next body stream is only pulled when downstream pulls the outer
@@ -17,98 +17,44 @@ import { Array, Cause, Channel, Data, Effect, Exit, Function, Match, Option, Ref
17
17
  * scoped resources, and backpressure stay aligned with normal Stream
18
18
  * semantics.
19
19
  *
20
- * Convention: a `Next` or `Stop` event is the terminal element of a body's
20
+ * Convention: a `Next` or `Stop` step is the terminal element of a body's
21
21
  * iteration. Values emitted in the same chunk after one are discarded
22
- * (their producing side effects may already have run). Prefer the
23
- * `Loop.nextAfter` / `Loop.stopAfter` helpers to terminate cleanly.
22
+ * (their producing side effects may already have run). The `Loop.next` /
23
+ * `Loop.stop` helpers each emit a single terminal step, so concatenate
24
+ * your values before them (`values.pipe(Stream.concat(Loop.next(state)))`).
24
25
  */
25
26
  var Loop_exports = /* @__PURE__ */ __exportAll({
26
27
  loop: () => loop,
27
- loopFrom: () => loopFrom,
28
+ loopOver: () => loopOver,
28
29
  loopWithState: () => loopWithState,
29
30
  next: () => next,
30
- nextAfter: () => nextAfter,
31
- nextAfterFold: () => nextAfterFold,
32
31
  onTurnComplete: () => onTurnComplete,
33
32
  stop: () => stop,
34
- stopAfter: () => stopAfter,
35
- stopEvent: () => stopEvent,
36
- stopWith: () => stopWith,
37
- stopWithAfter: () => stopWithAfter,
38
33
  value: () => value
39
34
  });
40
- const Event = Data.taggedEnum();
35
+ const Step = Data.taggedEnum();
41
36
  /** Wrap a value so it flows through the loop to downstream consumers. */
42
- const value = (a) => Event.Value({ value: a });
43
- /** End the current iteration and continue with a new state. */
44
- const next = (state) => Event.Next({ state });
37
+ const value = (a) => Step.Value({ value: a });
45
38
  /**
46
- * The terminal `Stop` event with no carried state. Use `stop` (the Stream)
47
- * to end a loop body without communicating a final state.
39
+ * End the current iteration and continue with a new state. Emits a single
40
+ * terminal `Next` step as a one-element stream, so it slots directly into a
41
+ * loop body or after a run of values: `values.pipe(Stream.concat(next(s)))`.
48
42
  */
49
- const stopEvent = Event.Stop();
43
+ const next = (state) => Stream.succeed(Step.Next({ state }));
50
44
  /**
51
- * Terminal event that ends the loop AND carries a final state. For
52
- * `loopFrom` this is the natural "this input is done, here's the state to
53
- * carry forward to the next input" signal — symmetric with `next(s)` but
54
- * ending the inner loop instead of continuing it. For `loopWithState` the
55
- * carried state is written to the `SubscriptionRef` before the loop ends.
45
+ * End the loop. Called with no argument it emits a bare `Stop`; called with
46
+ * a state it emits `StopWith(state)` — `loopOver` threads that final state
47
+ * to the next input and `loopWithState` writes it to its `SubscriptionRef`
48
+ * before the loop ends. Either way it returns a single-element stream, so
49
+ * return it directly from a loop body.
56
50
  */
57
- const stopWith = (state) => Event.StopWith({ state });
51
+ const stop = (state) => Stream.succeed(state === void 0 ? Step.Stop() : Step.StopWith({ state }));
58
52
  /**
59
- * A single-element stream that ends the loop. Return this from a body when
60
- * there's nothing else to emit; equivalent to `stopAfter(Stream.empty)` but
61
- * named for the common case.
62
- */
63
- const stop = Stream.succeed(stopEvent);
64
- /**
65
- * Pipe a raw `Stream<A>` into the loop's emit shape, then terminate the
66
- * iteration with `next(state)`. Common shape for "stream this turn's
67
- * deltas, then continue with updated history."
68
- *
69
- * Dual: data-first `nextAfter(stream, state)` and data-last
70
- * `stream.pipe(nextAfter(state))` both work.
71
- */
72
- const nextAfter = Function.dual(2, (stream, state) => Stream.concat(Stream.map(stream, value), Stream.fromIterable([next(state)])));
73
- /**
74
- * Pipe a raw `Stream<A>` into the loop's emit shape, then terminate the
75
- * loop. Common shape for "stream this turn's deltas, then we're done."
76
- *
77
- * Unary on the stream — already pipe-compatible via `stream.pipe(stopAfter)`.
78
- */
79
- const stopAfter = (stream) => Stream.concat(Stream.map(stream, value), Stream.fromIterable([stopEvent]));
80
- /**
81
- * Pipe a raw `Stream<A>` into the loop's emit shape, then terminate with
82
- * `stopWith(state)`. The natural "emit final outputs, advance state, end
83
- * this input's inner loop" shape for `loopFrom`.
84
- *
85
- * Dual: data-first `stopWithAfter(stream, state)` and data-last
86
- * `stream.pipe(stopWithAfter(state))` both work.
87
- */
88
- const stopWithAfter = Function.dual(2, (stream, state) => Stream.concat(Stream.map(stream, value), Stream.fromIterable([stopWith(state)])));
89
- /**
90
- * General `nextAfter` variant: drain `stream` to the consumer, fold elements
91
- * into an accumulator, and at end-of-stream emit one `next(build(finalAcc))`.
92
- *
93
- * Subsumes `nextAfter` when state is constant (`reduce: (s, _) => s`,
94
- * `build: (s) => s`). Used by `Toolkit.continueWith` to collect tool
95
- * results and build next state without exposing a Ref to recipes.
96
- *
97
- * Dual: data-first `nextAfterFold(stream, initial, reduce, build)` and
98
- * data-last `stream.pipe(nextAfterFold(initial, reduce, build))` both work.
99
- */
100
- const nextAfterFold = Function.dual(4, (stream, initial, reduce, build) => Stream.unwrap(Effect.gen(function* () {
101
- const ref = yield* Ref.make(initial);
102
- const tapped = stream.pipe(Stream.tap((a) => Ref.update(ref, (acc) => reduce(acc, a))), Stream.map(value));
103
- const continuation = Stream.fromEffect(Ref.get(ref).pipe(Effect.map((acc) => next(build(acc)))));
104
- return tapped.pipe(Stream.concat(continuation));
105
- })));
106
- /**
107
- * Lift a provider's `Stream<TurnEvent>` into a loop body's `Stream<Event<TurnEvent | A, S>>`.
53
+ * Lift a provider's `Stream<TurnEvent>` into a loop body's `Stream<Step<TurnEvent | A, S>>`.
108
54
  * Each delta passes through as `value(delta)` (including the terminal
109
55
  * `TurnComplete`, so the consumer sees turn boundaries naturally). Once
110
56
  * the terminal arrives, `then(turn)` runs and its returned stream of loop
111
- * events (typically tool outputs followed by `next(state)` or `stop`) is
57
+ * steps (typically tool outputs followed by `next(state)` or `stop()`) is
112
58
  * concatenated.
113
59
  *
114
60
  * Pre-pipe transforms (`Stream.tap` / `Stream.map` / `Stream.filter`) on
@@ -134,22 +80,22 @@ const onTurnComplete = Function.dual(2, (deltas, then) => Stream.unwrap(Effect.g
134
80
  const isNonEmpty = (array) => array.length > 0;
135
81
  const closeBody = (current, exit) => Scope.close(current.scope, exit);
136
82
  /**
137
- * Walk a chunk of `Event<A, S>` until a terminal `Next` or `Stop` is found.
83
+ * Walk a chunk of `Step<A, S>` until a terminal `Next` or `Stop` is found.
138
84
  * Returns the unwrapped values seen so far and (optionally) the terminal
139
- * event. Anything in the chunk after the terminal is discarded - its
85
+ * step. Anything in the chunk after the terminal is discarded - its
140
86
  * producing side effects may have run, but downstream never sees it.
141
87
  */
142
88
  const partitionChunk = (chunk) => {
143
- const [valueEvents, rest] = Array.span(chunk, (e) => e._tag === "Value");
89
+ const [valueSteps, rest] = Array.span(chunk, (e) => e._tag === "Value");
144
90
  return {
145
- values: valueEvents.map((e) => e.value),
91
+ values: valueSteps.map((e) => e.value),
146
92
  decision: Array.head(rest)
147
93
  };
148
94
  };
149
95
  /**
150
96
  * Drive a state-threaded loop body. Each iteration runs `body(state)` to get
151
- * a `Stream<Event<A, S>>`; values flow downstream, `next(s)` continues with
152
- * a new state, `stop` ends the loop. See the file header for the full
97
+ * a `Stream<Step<A, S>>`; values flow downstream, `next(s)` continues with
98
+ * a new state, `stop()` ends the loop. See the file header for the full
153
99
  * pull-based execution model.
154
100
  *
155
101
  * Dual: data-first `loop(initial, body)` and data-last `loop(body)(initial)`
@@ -198,31 +144,31 @@ const loop = Function.dual(2, (initial, body) => Stream.scoped(Stream.fromPull(E
198
144
  * stream, runs an inner seed-driven `loop` whose body is
199
145
  * `(s) => body(s, item)`. State is threaded across input items.
200
146
  *
201
- * **Per-input semantics — the body emits standard `Event<A, S>`:**
147
+ * **Per-input semantics — the body emits standard `Step<A, S>`:**
202
148
  * - `value(a)`: emit `a` downstream
203
149
  * - `next(s)`: re-run the body with the SAME input and new state `s`
204
150
  * (multi-turn within one input — e.g. multiple model turns + tool
205
151
  * calls for one document)
206
- * - `stop`: end this input's inner loop, advance to the next input
152
+ * - `stop()`: end this input's inner loop, advance to the next input
207
153
  * (state preserved)
208
- * - body stream ending without a decision: same as `stop` (advance)
154
+ * - body stream ending without a decision: same as `stop()` (advance)
209
155
  *
210
156
  * **Outer termination:** the input stream ending. To halt programmatically
211
157
  * from within, end the input stream upstream (`Stream.takeWhile`, a
212
- * `SubscriptionRef` gate, etc.). Reserving `stop` for per-item
158
+ * `SubscriptionRef` gate, etc.). Reserving `stop()` for per-item
213
159
  * advancement is what makes the common "stream of documents, multi-turn
214
160
  * conversation per document" shape readable.
215
161
  *
216
- * Dual: data-first `loopFrom(input, initial, body)` and data-last
217
- * `input.pipe(loopFrom(initial, body))` both work.
162
+ * Dual: data-first `loopOver(input, initial, body)` and data-last
163
+ * `input.pipe(loopOver(initial, body))` both work.
218
164
  */
219
- const loopFrom = Function.dual(3, (input, initial, body) => Stream.unwrap(Effect.gen(function* () {
165
+ const loopOver = Function.dual(3, (input, initial, body) => Stream.unwrap(Effect.gen(function* () {
220
166
  const stateRef = yield* Ref.make(initial);
221
167
  return input.pipe(Stream.flatMap((item) => Stream.unwrap(Effect.gen(function* () {
222
168
  const state = yield* Ref.get(stateRef);
223
169
  const wrappedBody = (s) => {
224
170
  const result = body(s, item);
225
- return (Effect.isEffect(result) ? Stream.unwrap(result) : result).pipe(Stream.tap((event) => Match.value(event).pipe(Match.tags({
171
+ return (Effect.isEffect(result) ? Stream.unwrap(result) : result).pipe(Stream.tap((step) => Match.value(step).pipe(Match.tags({
226
172
  Next: (e) => Ref.set(stateRef, e.state),
227
173
  StopWith: (e) => Ref.set(stateRef, e.state)
228
174
  }), Match.orElse(() => Effect.void))));
@@ -235,7 +181,7 @@ const loopFrom = Function.dual(3, (input, initial, body) => Stream.unwrap(Effect
235
181
  * alongside the value stream.
236
182
  *
237
183
  * Allocates one `SubscriptionRef<S>` seeded with `initial`, then runs the
238
- * loop with a wrapped body that taps every `Next(s)` event into the ref
184
+ * loop with a wrapped body that taps every `Next(s)` step into the ref
239
185
  * before forwarding it. The caller decides how to consume both channels:
240
186
  *
241
187
  * - **Final state**: drain the stream, then `SubscriptionRef.get(state)`
@@ -252,7 +198,7 @@ const loopFrom = Function.dual(3, (input, initial, body) => Stream.unwrap(Effect
252
198
  */
253
199
  const loopWithState = (initial, body) => Effect.gen(function* () {
254
200
  const stateRef = yield* SubscriptionRef.make(initial);
255
- const tap = (stream) => stream.pipe(Stream.tap((event) => Match.value(event).pipe(Match.tags({
201
+ const tap = (stream) => stream.pipe(Stream.tap((step) => Match.value(step).pipe(Match.tags({
256
202
  Next: (e) => SubscriptionRef.set(stateRef, e.state),
257
203
  StopWith: (e) => SubscriptionRef.set(stateRef, e.state)
258
204
  }), Match.orElse(() => Effect.void))));
@@ -266,6 +212,6 @@ const loopWithState = (initial, body) => Effect.gen(function* () {
266
212
  };
267
213
  });
268
214
  //#endregion
269
- export { loop, loopFrom, loopWithState, next, nextAfter, nextAfterFold, onTurnComplete, stop, stopAfter, stopEvent, stopWith, stopWithAfter, Loop_exports as t, value };
215
+ export { loop, loopOver, loopWithState, next, onTurnComplete, stop, Loop_exports as t, value };
270
216
 
271
217
  //# sourceMappingURL=Loop.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"Loop.mjs","names":["Arr"],"sources":["../../src/loop/Loop.ts"],"sourcesContent":["/**\n * Pull-based `loop` for state-threaded sub-streams.\n *\n * Each iteration runs a body that returns a `Stream<Event<A, S>>`. The body\n * emits values via `Loop.value(a)` and signals iteration control via\n * `Loop.next(state)` (continue with new state) or `Loop.stop` (terminate).\n * The loop unwraps `Value` events back to `A` for downstream consumers, so\n * the resulting stream is a plain `Stream<A>`.\n *\n * The next body stream is only pulled when downstream pulls the outer\n * stream - no producer fiber, no queue buffering. Cancellation, failures,\n * scoped resources, and backpressure stay aligned with normal Stream\n * semantics.\n *\n * Convention: a `Next` or `Stop` event is the terminal element of a body's\n * iteration. Values emitted in the same chunk after one are discarded\n * (their producing side effects may already have run). Prefer the\n * `Loop.nextAfter` / `Loop.stopAfter` helpers to terminate cleanly.\n */\nimport {\n Array as Arr,\n Cause,\n Channel,\n Data,\n Effect,\n Exit,\n Function,\n Match,\n Option,\n Ref,\n Result,\n Scope,\n Stream,\n SubscriptionRef,\n} from \"effect\"\nimport { IncompleteTurn } from \"../domain/AiError.js\"\nimport { isTurnComplete, type Turn, type TurnEvent } from \"../domain/Turn.js\"\n\n// ---------------------------------------------------------------------------\n// Event type - the body's emit shape\n// ---------------------------------------------------------------------------\n\n/**\n * The tagged union a body emits per pull. `Value` carries a payload that\n * flows downstream. `Next` ends the current iteration and continues with a\n * new state. `Stop` ends the loop entirely with no carried state.\n * `StopWith` also ends the loop but carries a final state that `loopFrom`\n * will thread to the next input and `loopWithState` will write to its\n * `SubscriptionRef` before the loop ends. Plain `loop` has no next\n * iteration to apply it to and treats `StopWith` like `Stop`.\n *\n * `Stop` is intentionally `{}` so the bare `stopEvent` / `stop` helpers\n * don't constrain `S` from a body's stream type — every body has a `Stop`\n * variant in its union, and forcing `S` to flow through it would break\n * inference whenever the body never uses `next` / `stopWith`.\n */\nexport type Event<A, S> = Data.TaggedEnum<{\n Value: { readonly value: A }\n Next: { readonly state: S }\n Stop: {}\n StopWith: { readonly state: S }\n}>\n\ninterface EventDef extends Data.TaggedEnum.WithGenerics<2> {\n readonly taggedEnum: Event<this[\"A\"], this[\"B\"]>\n}\n\nconst Event = Data.taggedEnum<EventDef>()\n\n/** Wrap a value so it flows through the loop to downstream consumers. */\nexport const value = <A>(a: A): Event<A, never> => Event.Value({ value: a })\n\n/** End the current iteration and continue with a new state. */\nexport const next = <S>(state: S): Event<never, S> => Event.Next({ state })\n\n/**\n * The terminal `Stop` event with no carried state. Use `stop` (the Stream)\n * to end a loop body without communicating a final state.\n */\nexport const stopEvent: Event<never, never> = Event.Stop()\n\n/**\n * Terminal event that ends the loop AND carries a final state. For\n * `loopFrom` this is the natural \"this input is done, here's the state to\n * carry forward to the next input\" signal — symmetric with `next(s)` but\n * ending the inner loop instead of continuing it. For `loopWithState` the\n * carried state is written to the `SubscriptionRef` before the loop ends.\n */\nexport const stopWith = <S>(state: S): Event<never, S> => Event.StopWith({ state })\n\n/**\n * A single-element stream that ends the loop. Return this from a body when\n * there's nothing else to emit; equivalent to `stopAfter(Stream.empty)` but\n * named for the common case.\n */\nexport const stop: Stream.Stream<Event<never, never>> = Stream.succeed(stopEvent)\n\n/**\n * Pipe a raw `Stream<A>` into the loop's emit shape, then terminate the\n * iteration with `next(state)`. Common shape for \"stream this turn's\n * deltas, then continue with updated history.\"\n *\n * Dual: data-first `nextAfter(stream, state)` and data-last\n * `stream.pipe(nextAfter(state))` both work.\n */\nexport const nextAfter: {\n <S>(state: S): <A, E, R>(stream: Stream.Stream<A, E, R>) => Stream.Stream<Event<A, S>, E, R>\n <S, A, E, R>(stream: Stream.Stream<A, E, R>, state: S): Stream.Stream<Event<A, S>, E, R>\n} = Function.dual(\n 2,\n <S, A, E, R>(stream: Stream.Stream<A, E, R>, state: S): Stream.Stream<Event<A, S>, E, R> =>\n Stream.concat(Stream.map(stream, value), Stream.fromIterable([next(state)])),\n)\n\n/**\n * Pipe a raw `Stream<A>` into the loop's emit shape, then terminate the\n * loop. Common shape for \"stream this turn's deltas, then we're done.\"\n *\n * Unary on the stream — already pipe-compatible via `stream.pipe(stopAfter)`.\n */\nexport const stopAfter = <A, E, R>(\n stream: Stream.Stream<A, E, R>,\n): Stream.Stream<Event<A, never>, E, R> =>\n Stream.concat(Stream.map(stream, value), Stream.fromIterable([stopEvent]))\n\n/**\n * Pipe a raw `Stream<A>` into the loop's emit shape, then terminate with\n * `stopWith(state)`. The natural \"emit final outputs, advance state, end\n * this input's inner loop\" shape for `loopFrom`.\n *\n * Dual: data-first `stopWithAfter(stream, state)` and data-last\n * `stream.pipe(stopWithAfter(state))` both work.\n */\nexport const stopWithAfter: {\n <S>(state: S): <A, E, R>(stream: Stream.Stream<A, E, R>) => Stream.Stream<Event<A, S>, E, R>\n <S, A, E, R>(stream: Stream.Stream<A, E, R>, state: S): Stream.Stream<Event<A, S>, E, R>\n} = Function.dual(\n 2,\n <S, A, E, R>(stream: Stream.Stream<A, E, R>, state: S): Stream.Stream<Event<A, S>, E, R> =>\n Stream.concat(Stream.map(stream, value), Stream.fromIterable([stopWith(state)])),\n)\n\n/**\n * General `nextAfter` variant: drain `stream` to the consumer, fold elements\n * into an accumulator, and at end-of-stream emit one `next(build(finalAcc))`.\n *\n * Subsumes `nextAfter` when state is constant (`reduce: (s, _) => s`,\n * `build: (s) => s`). Used by `Toolkit.continueWith` to collect tool\n * results and build next state without exposing a Ref to recipes.\n *\n * Dual: data-first `nextAfterFold(stream, initial, reduce, build)` and\n * data-last `stream.pipe(nextAfterFold(initial, reduce, build))` both work.\n */\nexport const nextAfterFold: {\n <A, B, S>(\n initial: B,\n reduce: (acc: B, a: A) => B,\n build: (b: B) => S,\n ): <E, R>(stream: Stream.Stream<A, E, R>) => Stream.Stream<Event<A, S>, E, R>\n <A, B, S, E, R>(\n stream: Stream.Stream<A, E, R>,\n initial: B,\n reduce: (acc: B, a: A) => B,\n build: (b: B) => S,\n ): Stream.Stream<Event<A, S>, E, R>\n} = Function.dual(\n 4,\n <A, B, S, E, R>(\n stream: Stream.Stream<A, E, R>,\n initial: B,\n reduce: (acc: B, a: A) => B,\n build: (b: B) => S,\n ): Stream.Stream<Event<A, S>, E, R> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const ref = yield* Ref.make(initial)\n const tapped = stream.pipe(\n Stream.tap((a) => Ref.update(ref, (acc) => reduce(acc, a))),\n Stream.map(value),\n )\n const continuation = Stream.fromEffect(\n Ref.get(ref).pipe(Effect.map((acc) => next(build(acc)))),\n )\n return tapped.pipe(Stream.concat(continuation))\n }),\n ),\n)\n\n// ---------------------------------------------------------------------------\n// onTurnComplete - turn-aware stream operator for loop bodies\n// ---------------------------------------------------------------------------\n\n/**\n * Lift a provider's `Stream<TurnEvent>` into a loop body's `Stream<Event<TurnEvent | A, S>>`.\n * Each delta passes through as `value(delta)` (including the terminal\n * `TurnComplete`, so the consumer sees turn boundaries naturally). Once\n * the terminal arrives, `then(turn)` runs and its returned stream of loop\n * events (typically tool outputs followed by `next(state)` or `stop`) is\n * concatenated.\n *\n * Pre-pipe transforms (`Stream.tap` / `Stream.map` / `Stream.filter`) on\n * the raw delta stream cover anything an `emit`-style callback would do.\n *\n * If the upstream ends without a `TurnComplete`, the resulting stream\n * fails with `AiError.IncompleteTurn`. Catch it via `Stream.catchTag` if\n * you want to recover.\n *\n * Dual: data-first `onTurnComplete(deltas, then)` and data-last\n * `deltas.pipe(onTurnComplete(then))` both work.\n */\nexport const onTurnComplete: {\n <S, A, E2 = never, R2 = never>(\n then: (turn: Turn) => Effect.Effect<Stream.Stream<Event<A, S>, E2, R2>, E2, R2>,\n ): <E, R>(\n deltas: Stream.Stream<TurnEvent, E, R>,\n ) => Stream.Stream<Event<TurnEvent | A, S>, E | E2 | IncompleteTurn, R | R2>\n <S, A, E, R, E2 = never, R2 = never>(\n deltas: Stream.Stream<TurnEvent, E, R>,\n then: (turn: Turn) => Effect.Effect<Stream.Stream<Event<A, S>, E2, R2>, E2, R2>,\n ): Stream.Stream<Event<TurnEvent | A, S>, E | E2 | IncompleteTurn, R | R2>\n} = Function.dual(\n 2,\n <S, A, E, R, E2, R2>(\n deltas: Stream.Stream<TurnEvent, E, R>,\n then: (turn: Turn) => Effect.Effect<Stream.Stream<Event<A, S>, E2, R2>, E2, R2>,\n ): Stream.Stream<Event<TurnEvent | A, S>, E | E2 | IncompleteTurn, R | R2> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const turnRef = yield* Ref.make<Option.Option<Turn>>(Option.none())\n\n const events: Stream.Stream<Event<TurnEvent, S>, E, R> = deltas.pipe(\n Stream.tap((delta) =>\n isTurnComplete(delta) ? Ref.set(turnRef, Option.some(delta.turn)) : Effect.void,\n ),\n Stream.map(value),\n )\n\n const continuation = Stream.unwrap(\n Effect.gen(function* () {\n const opt = yield* Ref.get(turnRef)\n if (Option.isNone(opt)) return yield* new IncompleteTurn({})\n return yield* then(opt.value)\n }),\n )\n\n return Stream.concat(events, continuation)\n }),\n ),\n)\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\nconst isNonEmpty = <A>(array: ReadonlyArray<A>): array is readonly [A, ...Array<A>] =>\n array.length > 0\n\ntype CurrentBody<S, A, E, R> = {\n readonly scope: Scope.Closeable\n readonly pull: Effect.Effect<ReadonlyArray<Event<A, S>>, E | Cause.Done<void>, R>\n}\n\nconst closeBody = <S, A, E, R>(\n current: CurrentBody<S, A, E, R>,\n exit: Exit.Exit<unknown, unknown>,\n) => Scope.close(current.scope, exit)\n\n/**\n * Walk a chunk of `Event<A, S>` until a terminal `Next` or `Stop` is found.\n * Returns the unwrapped values seen so far and (optionally) the terminal\n * event. Anything in the chunk after the terminal is discarded - its\n * producing side effects may have run, but downstream never sees it.\n */\nconst partitionChunk = <A, S>(\n chunk: ReadonlyArray<Event<A, S>>,\n): {\n readonly values: ReadonlyArray<A>\n readonly decision: Option.Option<Event<A, S>>\n} => {\n const [valueEvents, rest] = Arr.span(\n chunk,\n (e): e is Event<A, S> & { _tag: \"Value\" } => e._tag === \"Value\",\n )\n return {\n values: valueEvents.map((e) => e.value),\n decision: Arr.head(rest),\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\ntype LoopBody<S, A, E, R> = (\n state: S,\n) => Stream.Stream<Event<A, S>, E, R> | Effect.Effect<Stream.Stream<Event<A, S>, E, R>, E, R>\n\n/**\n * Drive a state-threaded loop body. Each iteration runs `body(state)` to get\n * a `Stream<Event<A, S>>`; values flow downstream, `next(s)` continues with\n * a new state, `stop` ends the loop. See the file header for the full\n * pull-based execution model.\n *\n * Dual: data-first `loop(initial, body)` and data-last `loop(body)(initial)`\n * (or `pipe(initial, loop(body))`) both work.\n */\nexport const loop: {\n <S, A, E, R>(body: LoopBody<S, A, E, R>): (initial: S) => Stream.Stream<A, E, R>\n <S, A, E, R>(initial: S, body: LoopBody<S, A, E, R>): Stream.Stream<A, E, R>\n} = Function.dual(\n 2,\n <S, A, E, R>(initial: S, body: LoopBody<S, A, E, R>): Stream.Stream<A, E, R> =>\n Stream.scoped(\n Stream.fromPull(\n Effect.gen(function* () {\n const outerScope = yield* Effect.scope\n let state = initial\n let current: CurrentBody<S, A, E, R> | undefined\n let done = false\n\n const closeActive = (\n active: CurrentBody<S, A, E, R>,\n exit: Exit.Exit<unknown, unknown>,\n ) => {\n const isActive = current === active\n if (isActive) current = undefined\n // Scope.close is idempotent. Multiple paths can race to close the\n // active body during cancellation/failure, so closing twice is safe.\n return closeBody(active, exit)\n }\n\n yield* Scope.addFinalizerExit(outerScope, (exit) =>\n current === undefined ? Effect.void : closeActive(current, exit),\n )\n\n const pull = Effect.gen(function* () {\n while (true) {\n if (done) return yield* Cause.done()\n\n if (current === undefined) {\n const result = body(state)\n const stream = Effect.isEffect(result) ? Stream.unwrap(result) : result\n const bodyScope = yield* Scope.fork(outerScope)\n const bodyPull = yield* Channel.toPullScoped(\n Stream.toChannel(stream),\n bodyScope,\n ).pipe(Effect.onError((cause) => Scope.close(bodyScope, Exit.failCause(cause))))\n current = { scope: bodyScope, pull: bodyPull }\n }\n\n const active = current\n const chunk = yield* active.pull.pipe(\n Effect.catchIf(Cause.isDone, () =>\n closeActive(active, Exit.void).pipe(\n Effect.as(undefined as ReadonlyArray<Event<A, S>> | undefined),\n ),\n ),\n Effect.onError((cause) => closeActive(active, Exit.failCause(cause))),\n )\n\n if (chunk === undefined) {\n done = true\n return yield* Cause.done()\n }\n\n const { values, decision } = partitionChunk(chunk)\n\n if (Option.isSome(decision)) {\n yield* closeActive(active, Exit.void)\n if (decision.value._tag === \"Stop\" || decision.value._tag === \"StopWith\") {\n // `loop` has no next iteration to apply StopWith's state to;\n // the state lands in `loopFrom`'s outer ref or\n // `loopWithState`'s SubscriptionRef via their taps.\n done = true\n } else if (decision.value._tag === \"Next\") {\n state = decision.value.state\n }\n }\n\n // Emit the values seen so far if any. Chunks from a Stream pull\n // are non-empty, so when `decision` is `None` every event was\n // a `Value` and `values` is non-empty here. With a decision and\n // no preceding values, fall through to the next iteration.\n if (isNonEmpty(values)) return values\n }\n })\n\n return pull\n }),\n ),\n ),\n)\n\n// ---------------------------------------------------------------------------\n// loopFrom - stream-driven sibling of loop. One input item runs a full\n// multi-turn inner loop.\n// ---------------------------------------------------------------------------\n\ntype LoopFromBody<S, I, A, E, R> = (\n state: S,\n input: I,\n) => Stream.Stream<Event<A, S>, E, R> | Effect.Effect<Stream.Stream<Event<A, S>, E, R>, E, R>\n\n/**\n * Input-driven sibling of `loop`. For each item pulled from the input\n * stream, runs an inner seed-driven `loop` whose body is\n * `(s) => body(s, item)`. State is threaded across input items.\n *\n * **Per-input semantics — the body emits standard `Event<A, S>`:**\n * - `value(a)`: emit `a` downstream\n * - `next(s)`: re-run the body with the SAME input and new state `s`\n * (multi-turn within one input — e.g. multiple model turns + tool\n * calls for one document)\n * - `stop`: end this input's inner loop, advance to the next input\n * (state preserved)\n * - body stream ending without a decision: same as `stop` (advance)\n *\n * **Outer termination:** the input stream ending. To halt programmatically\n * from within, end the input stream upstream (`Stream.takeWhile`, a\n * `SubscriptionRef` gate, etc.). Reserving `stop` for per-item\n * advancement is what makes the common \"stream of documents, multi-turn\n * conversation per document\" shape readable.\n *\n * Dual: data-first `loopFrom(input, initial, body)` and data-last\n * `input.pipe(loopFrom(initial, body))` both work.\n */\nexport const loopFrom: {\n <S, I, A, E, R>(\n initial: S,\n body: LoopFromBody<S, I, A, E, R>,\n ): <EI, RI>(input: Stream.Stream<I, EI, RI>) => Stream.Stream<A, E | EI, R | RI>\n <S, I, A, E, R, EI, RI>(\n input: Stream.Stream<I, EI, RI>,\n initial: S,\n body: LoopFromBody<S, I, A, E, R>,\n ): Stream.Stream<A, E | EI, R | RI>\n} = Function.dual(\n 3,\n <S, I, A, E, R, EI, RI>(\n input: Stream.Stream<I, EI, RI>,\n initial: S,\n body: LoopFromBody<S, I, A, E, R>,\n ): Stream.Stream<A, E | EI, R | RI> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const stateRef = yield* Ref.make<S>(initial)\n return input.pipe(\n Stream.flatMap((item) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const state = yield* Ref.get(stateRef)\n // Capture Next states (and stopWith's final state) into the\n // outer ref so the LAST state seen in this input's inner\n // loop is what the next input starts from.\n const wrappedBody = (s: S) => {\n const result = body(s, item)\n const stream = Effect.isEffect(result) ? Stream.unwrap(result) : result\n return stream.pipe(\n Stream.tap((event) =>\n Match.value(event).pipe(\n Match.tags({\n Next: (e) => Ref.set(stateRef, e.state),\n StopWith: (e) => Ref.set(stateRef, e.state),\n }),\n Match.orElse(() => Effect.void),\n ),\n ),\n )\n }\n return loop(state, wrappedBody)\n }),\n ),\n ),\n )\n }),\n ),\n)\n\n// ---------------------------------------------------------------------------\n// loopWithState - same body protocol, plus a live state observable.\n// ---------------------------------------------------------------------------\n\n/**\n * Like `loop`, but exposes the current loop state as a `SubscriptionRef`\n * alongside the value stream.\n *\n * Allocates one `SubscriptionRef<S>` seeded with `initial`, then runs the\n * loop with a wrapped body that taps every `Next(s)` event into the ref\n * before forwarding it. The caller decides how to consume both channels:\n *\n * - **Final state**: drain the stream, then `SubscriptionRef.get(state)`\n * - the ref holds the state from the last `Next` (or `initial` if the\n * loop ended without advancing).\n * - **Live transitions**: `SubscriptionRef.changes(state)` is a\n * `Stream<S>` of every state observed; subscribe alongside the value\n * stream.\n * - **Mid-iteration peek**: `SubscriptionRef.get(state)` at any time.\n *\n * The returned stream and ref are independent of each other - the ref\n * lives outside the stream's scope, so reading it after the stream\n * completes is safe.\n */\nexport const loopWithState = <S, A, E, R>(\n initial: S,\n body: LoopBody<S, A, E, R>,\n): Effect.Effect<{\n readonly stream: Stream.Stream<A, E, R>\n readonly state: SubscriptionRef.SubscriptionRef<S>\n}> =>\n Effect.gen(function* () {\n const stateRef = yield* SubscriptionRef.make(initial)\n\n const tap = (stream: Stream.Stream<Event<A, S>, E, R>): Stream.Stream<Event<A, S>, E, R> =>\n stream.pipe(\n Stream.tap((event) =>\n Match.value(event).pipe(\n Match.tags({\n Next: (e) => SubscriptionRef.set(stateRef, e.state),\n StopWith: (e) => SubscriptionRef.set(stateRef, e.state),\n }),\n Match.orElse(() => Effect.void),\n ),\n ),\n )\n\n const wrappedBody: LoopBody<S, A, E, R> = (s) => {\n const result = body(s)\n return Effect.isEffect(result) ? Effect.map(result, tap) : tap(result)\n }\n\n return {\n stream: loop(initial, wrappedBody),\n state: stateRef,\n }\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,MAAM,QAAQ,KAAK,YAAsB;;AAGzC,MAAa,SAAY,MAA0B,MAAM,MAAM,EAAE,OAAO,GAAG,CAAC;;AAG5E,MAAa,QAAW,UAA8B,MAAM,KAAK,EAAE,OAAO,CAAC;;;;;AAM3E,MAAa,YAAiC,MAAM,MAAM;;;;;;;;AAS1D,MAAa,YAAe,UAA8B,MAAM,SAAS,EAAE,OAAO,CAAC;;;;;;AAOnF,MAAa,OAA2C,OAAO,QAAQ,UAAU;;;;;;;;;AAUjF,MAAa,YAGT,SAAS,KACX,IACa,QAAgC,UAC3C,OAAO,OAAO,OAAO,IAAI,QAAQ,MAAM,EAAE,OAAO,aAAa,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC,CAC/E;;;;;;;AAQD,MAAa,aACX,WAEA,OAAO,OAAO,OAAO,IAAI,QAAQ,MAAM,EAAE,OAAO,aAAa,CAAC,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MAAM,WAAW,OAAO,gBAAgB,KAAK,QAAQ;CAErD,MAAM,OAAO,WACX,OAAO,KACL,OAAO,KAAK,UACV,MAAM,MAAM,MAAM,CAAC,KACjB,MAAM,KAAK;EACT,OAAO,MAAM,gBAAgB,IAAI,UAAU,EAAE,MAAM;EACnD,WAAW,MAAM,gBAAgB,IAAI,UAAU,EAAE,MAAM;EACxD,CAAC,EACF,MAAM,aAAa,OAAO,KAAK,CAChC,CACF,CACF;CAEH,MAAM,eAAqC,MAAM;EAC/C,MAAM,SAAS,KAAK,EAAE;AACtB,SAAO,OAAO,SAAS,OAAO,GAAG,OAAO,IAAI,QAAQ,IAAI,GAAG,IAAI,OAAO;;AAGxE,QAAO;EACL,QAAQ,KAAK,SAAS,YAAY;EAClC,OAAO;EACR;EACD"}
1
+ {"version":3,"file":"Loop.mjs","names":["Arr"],"sources":["../../src/loop/Loop.ts"],"sourcesContent":["/**\n * Pull-based `loop` for state-threaded sub-streams.\n *\n * Each iteration runs a body that returns a `Stream<Step<A, S>>`. The body\n * emits values via `Loop.value(a)` and signals iteration control via\n * `Loop.next(state)` (continue with new state) or `Loop.stop()` (terminate).\n * The loop unwraps `Value` steps back to `A` for downstream consumers, so\n * the resulting stream is a plain `Stream<A>`.\n *\n * The next body stream is only pulled when downstream pulls the outer\n * stream - no producer fiber, no queue buffering. Cancellation, failures,\n * scoped resources, and backpressure stay aligned with normal Stream\n * semantics.\n *\n * Convention: a `Next` or `Stop` step is the terminal element of a body's\n * iteration. Values emitted in the same chunk after one are discarded\n * (their producing side effects may already have run). The `Loop.next` /\n * `Loop.stop` helpers each emit a single terminal step, so concatenate\n * your values before them (`values.pipe(Stream.concat(Loop.next(state)))`).\n */\nimport {\n Array as Arr,\n Cause,\n Channel,\n Data,\n Effect,\n Exit,\n Function,\n Match,\n Option,\n Ref,\n Scope,\n Stream,\n SubscriptionRef,\n} from \"effect\"\nimport { IncompleteTurn } from \"../domain/AiError.js\"\nimport { isTurnComplete, type Turn, type TurnEvent } from \"../domain/Turn.js\"\n\n// ---------------------------------------------------------------------------\n// Step type - the body's emit shape\n// ---------------------------------------------------------------------------\n\n/**\n * The tagged union a body emits per pull. `Value` carries a payload that\n * flows downstream. `Next` ends the current iteration and continues with a\n * new state. `Stop` ends the loop entirely with no carried state.\n * `StopWith` also ends the loop but carries a final state that `loopOver`\n * will thread to the next input and `loopWithState` will write to its\n * `SubscriptionRef` before the loop ends. Plain `loop` has no next\n * iteration to apply it to and treats `StopWith` like `Stop`.\n *\n * `Stop` is intentionally `{}` so the bare `stop()` helper doesn't\n * constrain `S` from a body's stream type — every body has a `Stop`\n * variant in its union, and forcing `S` to flow through it would break\n * inference whenever the body never uses `next` / `stop(state)`.\n */\nexport type Step<A, S> = Data.TaggedEnum<{\n Value: { readonly value: A }\n Next: { readonly state: S }\n Stop: {}\n StopWith: { readonly state: S }\n}>\n\ninterface StepDef extends Data.TaggedEnum.WithGenerics<2> {\n readonly taggedEnum: Step<this[\"A\"], this[\"B\"]>\n}\n\nconst Step = Data.taggedEnum<StepDef>()\n\n/** Wrap a value so it flows through the loop to downstream consumers. */\nexport const value = <A>(a: A): Step<A, never> => Step.Value({ value: a })\n\n/**\n * End the current iteration and continue with a new state. Emits a single\n * terminal `Next` step as a one-element stream, so it slots directly into a\n * loop body or after a run of values: `values.pipe(Stream.concat(next(s)))`.\n */\nexport const next = <S>(state: S): Stream.Stream<Step<never, S>> =>\n Stream.succeed(Step.Next({ state }))\n\n/**\n * End the loop. Called with no argument it emits a bare `Stop`; called with\n * a state it emits `StopWith(state)` — `loopOver` threads that final state\n * to the next input and `loopWithState` writes it to its `SubscriptionRef`\n * before the loop ends. Either way it returns a single-element stream, so\n * return it directly from a loop body.\n */\nexport const stop = <S = never>(state?: S): Stream.Stream<Step<never, S>> =>\n Stream.succeed<Step<never, S>>(state === undefined ? Step.Stop() : Step.StopWith({ state }))\n\n// ---------------------------------------------------------------------------\n// onTurnComplete - turn-aware stream operator for loop bodies\n// ---------------------------------------------------------------------------\n\n/**\n * Lift a provider's `Stream<TurnEvent>` into a loop body's `Stream<Step<TurnEvent | A, S>>`.\n * Each delta passes through as `value(delta)` (including the terminal\n * `TurnComplete`, so the consumer sees turn boundaries naturally). Once\n * the terminal arrives, `then(turn)` runs and its returned stream of loop\n * steps (typically tool outputs followed by `next(state)` or `stop()`) is\n * concatenated.\n *\n * Pre-pipe transforms (`Stream.tap` / `Stream.map` / `Stream.filter`) on\n * the raw delta stream cover anything an `emit`-style callback would do.\n *\n * If the upstream ends without a `TurnComplete`, the resulting stream\n * fails with `AiError.IncompleteTurn`. Catch it via `Stream.catchTag` if\n * you want to recover.\n *\n * Dual: data-first `onTurnComplete(deltas, then)` and data-last\n * `deltas.pipe(onTurnComplete(then))` both work.\n */\nexport const onTurnComplete: {\n <S, A, E2 = never, R2 = never>(\n then: (turn: Turn) => Effect.Effect<Stream.Stream<Step<A, S>, E2, R2>, E2, R2>,\n ): <E, R>(\n deltas: Stream.Stream<TurnEvent, E, R>,\n ) => Stream.Stream<Step<TurnEvent | A, S>, E | E2 | IncompleteTurn, R | R2>\n <S, A, E, R, E2 = never, R2 = never>(\n deltas: Stream.Stream<TurnEvent, E, R>,\n then: (turn: Turn) => Effect.Effect<Stream.Stream<Step<A, S>, E2, R2>, E2, R2>,\n ): Stream.Stream<Step<TurnEvent | A, S>, E | E2 | IncompleteTurn, R | R2>\n} = Function.dual(\n 2,\n <S, A, E, R, E2, R2>(\n deltas: Stream.Stream<TurnEvent, E, R>,\n then: (turn: Turn) => Effect.Effect<Stream.Stream<Step<A, S>, E2, R2>, E2, R2>,\n ): Stream.Stream<Step<TurnEvent | A, S>, E | E2 | IncompleteTurn, R | R2> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const turnRef = yield* Ref.make<Option.Option<Turn>>(Option.none())\n\n const events: Stream.Stream<Step<TurnEvent, S>, E, R> = deltas.pipe(\n Stream.tap((delta) =>\n isTurnComplete(delta) ? Ref.set(turnRef, Option.some(delta.turn)) : Effect.void,\n ),\n Stream.map(value),\n )\n\n const continuation = Stream.unwrap(\n Effect.gen(function* () {\n const opt = yield* Ref.get(turnRef)\n if (Option.isNone(opt)) return yield* new IncompleteTurn({})\n return yield* then(opt.value)\n }),\n )\n\n return Stream.concat(events, continuation)\n }),\n ),\n)\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\nconst isNonEmpty = <A>(array: ReadonlyArray<A>): array is readonly [A, ...Array<A>] =>\n array.length > 0\n\ntype CurrentBody<S, A, E, R> = {\n readonly scope: Scope.Closeable\n readonly pull: Effect.Effect<ReadonlyArray<Step<A, S>>, E | Cause.Done<void>, R>\n}\n\nconst closeBody = <S, A, E, R>(\n current: CurrentBody<S, A, E, R>,\n exit: Exit.Exit<unknown, unknown>,\n) => Scope.close(current.scope, exit)\n\n/**\n * Walk a chunk of `Step<A, S>` until a terminal `Next` or `Stop` is found.\n * Returns the unwrapped values seen so far and (optionally) the terminal\n * step. Anything in the chunk after the terminal is discarded - its\n * producing side effects may have run, but downstream never sees it.\n */\nconst partitionChunk = <A, S>(\n chunk: ReadonlyArray<Step<A, S>>,\n): {\n readonly values: ReadonlyArray<A>\n readonly decision: Option.Option<Step<A, S>>\n} => {\n const [valueSteps, rest] = Arr.span(\n chunk,\n (e): e is Step<A, S> & { _tag: \"Value\" } => e._tag === \"Value\",\n )\n return {\n values: valueSteps.map((e) => e.value),\n decision: Arr.head(rest),\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\ntype LoopBody<S, A, E, R> = (\n state: S,\n) => Stream.Stream<Step<A, S>, E, R> | Effect.Effect<Stream.Stream<Step<A, S>, E, R>, E, R>\n\n/**\n * Drive a state-threaded loop body. Each iteration runs `body(state)` to get\n * a `Stream<Step<A, S>>`; values flow downstream, `next(s)` continues with\n * a new state, `stop()` ends the loop. See the file header for the full\n * pull-based execution model.\n *\n * Dual: data-first `loop(initial, body)` and data-last `loop(body)(initial)`\n * (or `pipe(initial, loop(body))`) both work.\n */\nexport const loop: {\n <S, A, E, R>(body: LoopBody<S, A, E, R>): (initial: S) => Stream.Stream<A, E, R>\n <S, A, E, R>(initial: S, body: LoopBody<S, A, E, R>): Stream.Stream<A, E, R>\n} = Function.dual(\n 2,\n <S, A, E, R>(initial: S, body: LoopBody<S, A, E, R>): Stream.Stream<A, E, R> =>\n Stream.scoped(\n Stream.fromPull(\n Effect.gen(function* () {\n const outerScope = yield* Effect.scope\n let state = initial\n let current: CurrentBody<S, A, E, R> | undefined\n let done = false\n\n const closeActive = (\n active: CurrentBody<S, A, E, R>,\n exit: Exit.Exit<unknown, unknown>,\n ) => {\n const isActive = current === active\n if (isActive) current = undefined\n // Scope.close is idempotent. Multiple paths can race to close the\n // active body during cancellation/failure, so closing twice is safe.\n return closeBody(active, exit)\n }\n\n yield* Scope.addFinalizerExit(outerScope, (exit) =>\n current === undefined ? Effect.void : closeActive(current, exit),\n )\n\n const pull = Effect.gen(function* () {\n while (true) {\n if (done) return yield* Cause.done()\n\n if (current === undefined) {\n const result = body(state)\n const stream = Effect.isEffect(result) ? Stream.unwrap(result) : result\n const bodyScope = yield* Scope.fork(outerScope)\n const bodyPull = yield* Channel.toPullScoped(\n Stream.toChannel(stream),\n bodyScope,\n ).pipe(Effect.onError((cause) => Scope.close(bodyScope, Exit.failCause(cause))))\n current = { scope: bodyScope, pull: bodyPull }\n }\n\n const active = current\n const chunk = yield* active.pull.pipe(\n Effect.catchIf(Cause.isDone, () =>\n closeActive(active, Exit.void).pipe(\n Effect.as(undefined as ReadonlyArray<Step<A, S>> | undefined),\n ),\n ),\n Effect.onError((cause) => closeActive(active, Exit.failCause(cause))),\n )\n\n if (chunk === undefined) {\n done = true\n return yield* Cause.done()\n }\n\n const { values, decision } = partitionChunk(chunk)\n\n if (Option.isSome(decision)) {\n yield* closeActive(active, Exit.void)\n if (decision.value._tag === \"Stop\" || decision.value._tag === \"StopWith\") {\n // `loop` has no next iteration to apply StopWith's state to;\n // the state lands in `loopOver`'s outer ref or\n // `loopWithState`'s SubscriptionRef via their taps.\n done = true\n } else if (decision.value._tag === \"Next\") {\n state = decision.value.state\n }\n }\n\n // Emit the values seen so far if any. Chunks from a Stream pull\n // are non-empty, so when `decision` is `None` every step was\n // a `Value` and `values` is non-empty here. With a decision and\n // no preceding values, fall through to the next iteration.\n if (isNonEmpty(values)) return values\n }\n })\n\n return pull\n }),\n ),\n ),\n)\n\n// ---------------------------------------------------------------------------\n// loopOver - stream-driven sibling of loop. One input item runs a full\n// multi-turn inner loop.\n// ---------------------------------------------------------------------------\n\ntype LoopOverBody<S, I, A, E, R> = (\n state: S,\n input: I,\n) => Stream.Stream<Step<A, S>, E, R> | Effect.Effect<Stream.Stream<Step<A, S>, E, R>, E, R>\n\n/**\n * Input-driven sibling of `loop`. For each item pulled from the input\n * stream, runs an inner seed-driven `loop` whose body is\n * `(s) => body(s, item)`. State is threaded across input items.\n *\n * **Per-input semantics — the body emits standard `Step<A, S>`:**\n * - `value(a)`: emit `a` downstream\n * - `next(s)`: re-run the body with the SAME input and new state `s`\n * (multi-turn within one input — e.g. multiple model turns + tool\n * calls for one document)\n * - `stop()`: end this input's inner loop, advance to the next input\n * (state preserved)\n * - body stream ending without a decision: same as `stop()` (advance)\n *\n * **Outer termination:** the input stream ending. To halt programmatically\n * from within, end the input stream upstream (`Stream.takeWhile`, a\n * `SubscriptionRef` gate, etc.). Reserving `stop()` for per-item\n * advancement is what makes the common \"stream of documents, multi-turn\n * conversation per document\" shape readable.\n *\n * Dual: data-first `loopOver(input, initial, body)` and data-last\n * `input.pipe(loopOver(initial, body))` both work.\n */\nexport const loopOver: {\n <S, I, A, E, R>(\n initial: S,\n body: LoopOverBody<S, I, A, E, R>,\n ): <EI, RI>(input: Stream.Stream<I, EI, RI>) => Stream.Stream<A, E | EI, R | RI>\n <S, I, A, E, R, EI, RI>(\n input: Stream.Stream<I, EI, RI>,\n initial: S,\n body: LoopOverBody<S, I, A, E, R>,\n ): Stream.Stream<A, E | EI, R | RI>\n} = Function.dual(\n 3,\n <S, I, A, E, R, EI, RI>(\n input: Stream.Stream<I, EI, RI>,\n initial: S,\n body: LoopOverBody<S, I, A, E, R>,\n ): Stream.Stream<A, E | EI, R | RI> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const stateRef = yield* Ref.make<S>(initial)\n return input.pipe(\n Stream.flatMap((item) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const state = yield* Ref.get(stateRef)\n // Capture Next states (and stop(state)'s final state) into the\n // outer ref so the LAST state seen in this input's inner\n // loop is what the next input starts from.\n const wrappedBody = (s: S) => {\n const result = body(s, item)\n const stream = Effect.isEffect(result) ? Stream.unwrap(result) : result\n return stream.pipe(\n Stream.tap((step) =>\n Match.value(step).pipe(\n Match.tags({\n Next: (e) => Ref.set(stateRef, e.state),\n StopWith: (e) => Ref.set(stateRef, e.state),\n }),\n Match.orElse(() => Effect.void),\n ),\n ),\n )\n }\n return loop(state, wrappedBody)\n }),\n ),\n ),\n )\n }),\n ),\n)\n\n// ---------------------------------------------------------------------------\n// loopWithState - same body protocol, plus a live state observable.\n// ---------------------------------------------------------------------------\n\n/**\n * Like `loop`, but exposes the current loop state as a `SubscriptionRef`\n * alongside the value stream.\n *\n * Allocates one `SubscriptionRef<S>` seeded with `initial`, then runs the\n * loop with a wrapped body that taps every `Next(s)` step into the ref\n * before forwarding it. The caller decides how to consume both channels:\n *\n * - **Final state**: drain the stream, then `SubscriptionRef.get(state)`\n * - the ref holds the state from the last `Next` (or `initial` if the\n * loop ended without advancing).\n * - **Live transitions**: `SubscriptionRef.changes(state)` is a\n * `Stream<S>` of every state observed; subscribe alongside the value\n * stream.\n * - **Mid-iteration peek**: `SubscriptionRef.get(state)` at any time.\n *\n * The returned stream and ref are independent of each other - the ref\n * lives outside the stream's scope, so reading it after the stream\n * completes is safe.\n */\nexport const loopWithState = <S, A, E, R>(\n initial: S,\n body: LoopBody<S, A, E, R>,\n): Effect.Effect<{\n readonly stream: Stream.Stream<A, E, R>\n readonly state: SubscriptionRef.SubscriptionRef<S>\n}> =>\n Effect.gen(function* () {\n const stateRef = yield* SubscriptionRef.make(initial)\n\n const tap = (stream: Stream.Stream<Step<A, S>, E, R>): Stream.Stream<Step<A, S>, E, R> =>\n stream.pipe(\n Stream.tap((step) =>\n Match.value(step).pipe(\n Match.tags({\n Next: (e) => SubscriptionRef.set(stateRef, e.state),\n StopWith: (e) => SubscriptionRef.set(stateRef, e.state),\n }),\n Match.orElse(() => Effect.void),\n ),\n ),\n )\n\n const wrappedBody: LoopBody<S, A, E, R> = (s) => {\n const result = body(s)\n return Effect.isEffect(result) ? Effect.map(result, tap) : tap(result)\n }\n\n return {\n stream: loop(initial, wrappedBody),\n state: stateRef,\n }\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,MAAM,OAAO,KAAK,YAAqB;;AAGvC,MAAa,SAAY,MAAyB,KAAK,MAAM,EAAE,OAAO,GAAG,CAAC;;;;;;AAO1E,MAAa,QAAW,UACtB,OAAO,QAAQ,KAAK,KAAK,EAAE,OAAO,CAAC,CAAC;;;;;;;;AAStC,MAAa,QAAmB,UAC9B,OAAO,QAAwB,UAAU,KAAA,IAAY,KAAK,MAAM,GAAG,KAAK,SAAS,EAAE,OAAO,CAAC,CAAC;;;;;;;;;;;;;;;;;;;AAwB9F,MAAa,iBAUT,SAAS,KACX,IAEE,QACA,SAEA,OAAO,OACL,OAAO,IAAI,aAAa;CACtB,MAAM,UAAU,OAAO,IAAI,KAA0B,OAAO,MAAM,CAAC;CAEnE,MAAM,SAAkD,OAAO,KAC7D,OAAO,KAAK,UACV,eAAe,MAAM,GAAG,IAAI,IAAI,SAAS,OAAO,KAAK,MAAM,KAAK,CAAC,GAAG,OAAO,KAC5E,EACD,OAAO,IAAI,MAAM,CAClB;CAED,MAAM,eAAe,OAAO,OAC1B,OAAO,IAAI,aAAa;EACtB,MAAM,MAAM,OAAO,IAAI,IAAI,QAAQ;AACnC,MAAI,OAAO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