brass-runtime 1.13.8 → 1.14.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 (36) hide show
  1. package/dist/agent/cli/main.cjs +43 -43
  2. package/dist/agent/cli/main.js +2 -2
  3. package/dist/agent/cli/main.mjs +2 -2
  4. package/dist/agent/index.cjs +3 -3
  5. package/dist/agent/index.d.ts +1 -1
  6. package/dist/agent/index.js +2 -2
  7. package/dist/agent/index.mjs +2 -2
  8. package/dist/chunk-4N2JEK4H.mjs +3897 -0
  9. package/dist/chunk-BKBFSOGT.cjs +3897 -0
  10. package/dist/{chunk-XNOTJSMZ.mjs → chunk-BMRF4FN6.js} +268 -8
  11. package/dist/chunk-JT7D6M5H.js +3897 -0
  12. package/dist/{chunk-3R7ZYRK2.mjs → chunk-MQF7HZ7Y.mjs} +1 -1
  13. package/dist/chunk-SKVY72E5.cjs +667 -0
  14. package/dist/{chunk-ATHSSDUF.js → chunk-UWMMYKVK.mjs} +268 -8
  15. package/dist/{chunk-INZBKOHY.js → chunk-WJESVBWN.js} +1 -1
  16. package/dist/{chunk-XDINDYNA.cjs → chunk-XTMZTVIT.cjs} +134 -134
  17. package/dist/{effect-ISvXPLgc.d.ts → effect-DM56H743.d.ts} +191 -21
  18. package/dist/http/index.cjs +808 -140
  19. package/dist/http/index.d.ts +181 -8
  20. package/dist/http/index.js +793 -125
  21. package/dist/http/index.mjs +793 -125
  22. package/dist/index.cjs +1785 -137
  23. package/dist/index.d.ts +979 -36
  24. package/dist/index.js +1675 -27
  25. package/dist/index.mjs +1675 -27
  26. package/dist/stream-Oqe6WeLE.d.ts +173 -0
  27. package/package.json +1 -1
  28. package/wasm/pkg/brass_runtime_wasm_engine.d.ts +95 -16
  29. package/wasm/pkg/brass_runtime_wasm_engine.js +715 -15
  30. package/wasm/pkg/brass_runtime_wasm_engine_bg.wasm +0 -0
  31. package/wasm/pkg/brass_runtime_wasm_engine_bg.wasm.d.ts +78 -7
  32. package/dist/chunk-2P4PD6D7.cjs +0 -2557
  33. package/dist/chunk-7F2R7A2V.mjs +0 -2557
  34. package/dist/chunk-L6KKKM66.js +0 -2557
  35. package/dist/chunk-ZTDK2DLG.cjs +0 -407
  36. package/dist/stream-BvukHxCv.d.ts +0 -66
@@ -11,7 +11,7 @@ import {
11
11
  fromPromiseAbortable,
12
12
  race,
13
13
  withScopeAsync
14
- } from "./chunk-7F2R7A2V.mjs";
14
+ } from "./chunk-4N2JEK4H.mjs";
15
15
 
16
16
  // src/agent/core/state.ts
17
17
  var initialAgentState = (goal) => ({
@@ -0,0 +1,667 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+ var _chunkBKBFSOGTcjs = require('./chunk-BKBFSOGT.cjs');
26
+
27
+ // src/core/runtime/combinators.ts
28
+ function sleep(ms) {
29
+ return _chunkBKBFSOGTcjs.async.call(void 0, (_env, cb) => {
30
+ const delay = Math.max(0, Math.floor(ms));
31
+ const id = setTimeout(() => cb({ _tag: "Success", value: void 0 }), delay);
32
+ return () => clearTimeout(id);
33
+ });
34
+ }
35
+ function timeout(effect, ms) {
36
+ return _chunkBKBFSOGTcjs.async.call(void 0, (env, cb) => {
37
+ let done = false;
38
+ let timerId;
39
+ let effectRunning = true;
40
+ timerId = setTimeout(() => {
41
+ if (done) return;
42
+ done = true;
43
+ effectRunning = false;
44
+ cb({
45
+ _tag: "Failure",
46
+ cause: { _tag: "Fail", error: { _tag: "TimeoutError", ms } }
47
+ });
48
+ }, Math.max(0, Math.floor(ms)));
49
+ const runtime = _chunkBKBFSOGTcjs.unsafeGetCurrentRuntime.call(void 0, );
50
+ if (runtime) {
51
+ const fiber = runtime.fork(effect);
52
+ fiber.join((exit) => {
53
+ if (done) return;
54
+ done = true;
55
+ clearTimeout(timerId);
56
+ cb(exit);
57
+ });
58
+ return () => {
59
+ if (done) return;
60
+ done = true;
61
+ clearTimeout(timerId);
62
+ fiber.interrupt();
63
+ };
64
+ }
65
+ return () => {
66
+ if (done) return;
67
+ done = true;
68
+ clearTimeout(timerId);
69
+ };
70
+ });
71
+ }
72
+ function retry(effect, policy) {
73
+ const shouldRetry = _nullishCoalesce(policy.shouldRetry, () => ( (() => true)));
74
+ const jitter = _nullishCoalesce(policy.jitter, () => ( "full"));
75
+ const maxElapsedMs = policy.maxElapsedMs;
76
+ const computeDelay = (attempt) => {
77
+ const exp = policy.baseDelayMs * Math.pow(2, attempt);
78
+ const capped = Math.min(exp, policy.maxDelayMs);
79
+ if (jitter === "none") return capped;
80
+ return Math.floor(Math.random() * capped);
81
+ };
82
+ const loop = (attempt, startedAt) => _chunkBKBFSOGTcjs.asyncFold.call(void 0,
83
+ effect,
84
+ (error) => {
85
+ if (attempt >= policy.maxRetries) return _chunkBKBFSOGTcjs.asyncFail.call(void 0, error);
86
+ if (!shouldRetry(error, attempt)) return _chunkBKBFSOGTcjs.asyncFail.call(void 0, error);
87
+ if (maxElapsedMs !== void 0) {
88
+ const elapsed = performance.now() - startedAt;
89
+ if (elapsed >= maxElapsedMs) return _chunkBKBFSOGTcjs.asyncFail.call(void 0, error);
90
+ }
91
+ const delay = computeDelay(attempt);
92
+ return _chunkBKBFSOGTcjs.asyncFlatMap.call(void 0, sleep(delay), () => loop(attempt + 1, startedAt));
93
+ },
94
+ (value) => _chunkBKBFSOGTcjs.asyncSucceed.call(void 0, value)
95
+ );
96
+ return _chunkBKBFSOGTcjs.asyncFlatMap.call(void 0,
97
+ { _tag: "Sync", thunk: () => performance.now() },
98
+ (startedAt) => loop(0, startedAt)
99
+ );
100
+ }
101
+ function retryN(effect, n) {
102
+ return retry(effect, {
103
+ maxRetries: n,
104
+ baseDelayMs: 0,
105
+ maxDelayMs: 0,
106
+ jitter: "none"
107
+ });
108
+ }
109
+ function retryWithBackoff(effect, opts = {}) {
110
+ return retry(effect, {
111
+ maxRetries: _nullishCoalesce(opts.maxRetries, () => ( 3)),
112
+ baseDelayMs: _nullishCoalesce(opts.baseDelayMs, () => ( 100)),
113
+ maxDelayMs: _nullishCoalesce(opts.maxDelayMs, () => ( 1e4)),
114
+ maxElapsedMs: opts.maxElapsedMs,
115
+ shouldRetry: opts.shouldRetry,
116
+ jitter: "full"
117
+ });
118
+ }
119
+
120
+ // src/core/runtime/circuitBreaker.ts
121
+ function makeCircuitBreaker(config = {}) {
122
+ const failureThreshold = _nullishCoalesce(config.failureThreshold, () => ( 5));
123
+ const resetTimeoutMs = _nullishCoalesce(config.resetTimeoutMs, () => ( 3e4));
124
+ const successThreshold = _nullishCoalesce(config.successThreshold, () => ( 1));
125
+ const isFailure = _nullishCoalesce(config.isFailure, () => ( (() => true)));
126
+ const onStateChange = config.onStateChange;
127
+ let currentState = "closed";
128
+ let consecutiveFailures = 0;
129
+ let consecutiveSuccesses = 0;
130
+ let openedAt = 0;
131
+ let totalRequests = 0;
132
+ let totalFailures = 0;
133
+ let totalSuccesses = 0;
134
+ let totalRejected = 0;
135
+ let lastFailureTime = null;
136
+ let lastSuccessTime = null;
137
+ const transition = (to) => {
138
+ if (currentState === to) return;
139
+ const from = currentState;
140
+ currentState = to;
141
+ _optionalChain([onStateChange, 'optionalCall', _2 => _2(from, to)]);
142
+ };
143
+ const onSuccess = () => {
144
+ totalSuccesses++;
145
+ lastSuccessTime = Date.now();
146
+ consecutiveFailures = 0;
147
+ if (currentState === "half-open") {
148
+ consecutiveSuccesses++;
149
+ if (consecutiveSuccesses >= successThreshold) {
150
+ consecutiveSuccesses = 0;
151
+ transition("closed");
152
+ }
153
+ }
154
+ };
155
+ const onFailure = (error) => {
156
+ if (!isFailure(error)) {
157
+ onSuccess();
158
+ return;
159
+ }
160
+ totalFailures++;
161
+ lastFailureTime = Date.now();
162
+ consecutiveSuccesses = 0;
163
+ consecutiveFailures++;
164
+ if (currentState === "half-open") {
165
+ openedAt = Date.now();
166
+ transition("open");
167
+ } else if (currentState === "closed" && consecutiveFailures >= failureThreshold) {
168
+ openedAt = Date.now();
169
+ transition("open");
170
+ }
171
+ };
172
+ const shouldAllow = () => {
173
+ switch (currentState) {
174
+ case "closed":
175
+ return true;
176
+ case "open": {
177
+ const elapsed = Date.now() - openedAt;
178
+ if (elapsed >= resetTimeoutMs) {
179
+ transition("half-open");
180
+ return true;
181
+ }
182
+ return false;
183
+ }
184
+ case "half-open":
185
+ return true;
186
+ }
187
+ };
188
+ const protect = (effect) => {
189
+ totalRequests++;
190
+ if (!shouldAllow()) {
191
+ totalRejected++;
192
+ return _chunkBKBFSOGTcjs.asyncFail.call(void 0, {
193
+ _tag: "CircuitBreakerOpen",
194
+ openSince: openedAt,
195
+ failures: consecutiveFailures
196
+ });
197
+ }
198
+ return _chunkBKBFSOGTcjs.asyncFold.call(void 0,
199
+ effect,
200
+ (error) => {
201
+ onFailure(error);
202
+ return _chunkBKBFSOGTcjs.asyncFail.call(void 0, error);
203
+ },
204
+ (value) => {
205
+ onSuccess();
206
+ return _chunkBKBFSOGTcjs.asyncSucceed.call(void 0, value);
207
+ }
208
+ );
209
+ };
210
+ const stats = () => ({
211
+ state: currentState,
212
+ failures: consecutiveFailures,
213
+ successes: consecutiveSuccesses,
214
+ totalRequests,
215
+ totalFailures,
216
+ totalSuccesses,
217
+ totalRejected,
218
+ lastFailureTime,
219
+ lastSuccessTime
220
+ });
221
+ const reset = () => {
222
+ consecutiveFailures = 0;
223
+ consecutiveSuccesses = 0;
224
+ transition("closed");
225
+ };
226
+ return {
227
+ state: () => currentState,
228
+ protect,
229
+ stats,
230
+ reset
231
+ };
232
+ }
233
+
234
+ // src/core/stream/stream.ts
235
+ var widenOpt = (opt) => opt._tag === "None" ? _chunkBKBFSOGTcjs.none : _chunkBKBFSOGTcjs.some.call(void 0, opt.value);
236
+ var fromPull = (pull) => ({
237
+ _tag: "FromPull",
238
+ pull
239
+ });
240
+ var unwrapScoped = (acquire, release) => ({
241
+ _tag: "Scoped",
242
+ acquire,
243
+ release
244
+ });
245
+ var managedStream = (acquire) => ({
246
+ _tag: "Managed",
247
+ acquire
248
+ });
249
+ var mergeStream = (left, right, flip = true) => ({
250
+ _tag: "Merge",
251
+ left,
252
+ right,
253
+ flip
254
+ });
255
+ var EMPTY_STREAM = { _tag: "Empty" };
256
+ var emptyStream = () => EMPTY_STREAM;
257
+ var emitStream = (value) => ({
258
+ _tag: "Emit",
259
+ value
260
+ });
261
+ var concatStream = (left, right) => ({
262
+ _tag: "Concat",
263
+ left,
264
+ right
265
+ });
266
+ var flattenStream = (stream) => ({
267
+ _tag: "Flatten",
268
+ stream
269
+ });
270
+ function streamToRaceWithHandler(winnerSide, leftStream, rightStream, flip, id) {
271
+ return (exit, otherFiber, scope) => {
272
+ if (exit._tag === "Failure") {
273
+ }
274
+ if (exit._tag === "Success") {
275
+ }
276
+ if (exit._tag === "Success") {
277
+ const [a, tailWin] = exit.value;
278
+ otherFiber.interrupt();
279
+ const next = winnerSide === "L" ? fromPull(makeMergePull(tailWin, rightStream, !flip, id)) : fromPull(makeMergePull(leftStream, tailWin, !flip, id));
280
+ return _chunkBKBFSOGTcjs.asyncSucceed.call(void 0, [a, next]);
281
+ }
282
+ if (exit.cause._tag === "Interrupt") {
283
+ return _chunkBKBFSOGTcjs.async.call(void 0, (_env, cb) => {
284
+ cb(_chunkBKBFSOGTcjs.Exit.failCause(_chunkBKBFSOGTcjs.Cause.interrupt()));
285
+ });
286
+ }
287
+ const opt = exit.cause.error;
288
+ if (opt._tag === "None") {
289
+ return winnerSide === "L" ? uncons(rightStream) : uncons(leftStream);
290
+ }
291
+ return _chunkBKBFSOGTcjs.asyncFail.call(void 0, opt);
292
+ };
293
+ }
294
+ function makeMergePull(onLeft, onRight, flip, mergePullId) {
295
+ const id = ++mergePullId;
296
+ const onLeftHandler = streamToRaceWithHandler("L", onLeft, onRight, flip, id);
297
+ const onRightHandler = streamToRaceWithHandler("R", onLeft, onRight, flip, id);
298
+ return _chunkBKBFSOGTcjs.async.call(void 0, (_env, cb) => {
299
+ const runtime = _chunkBKBFSOGTcjs.unsafeGetCurrentRuntime.call(void 0, );
300
+ const scope = new (0, _chunkBKBFSOGTcjs.Scope)(runtime);
301
+ const handler = _chunkBKBFSOGTcjs.raceWith.call(void 0,
302
+ uncons(onLeft),
303
+ uncons(onRight),
304
+ scope,
305
+ onLeftHandler,
306
+ onRightHandler
307
+ );
308
+ scope.fork(handler).join((ex) => {
309
+ scope.close(ex);
310
+ cb(ex);
311
+ });
312
+ });
313
+ }
314
+ function merge(left, right) {
315
+ return fromPull(makeMergePull(left, right, true, 0));
316
+ }
317
+ function uncons(self) {
318
+ switch (self._tag) {
319
+ case "Empty":
320
+ return _chunkBKBFSOGTcjs.fail.call(void 0, _chunkBKBFSOGTcjs.none);
321
+ case "FromArray": {
322
+ const arr = self.values;
323
+ if (arr.length === 0) return _chunkBKBFSOGTcjs.fail.call(void 0, _chunkBKBFSOGTcjs.none);
324
+ const tail = arr.length === 1 ? EMPTY_STREAM : { _tag: "FromArray", values: arr.slice(1) };
325
+ return _chunkBKBFSOGTcjs.succeed.call(void 0, [arr[0], tail]);
326
+ }
327
+ case "Emit":
328
+ return _chunkBKBFSOGTcjs.map.call(void 0,
329
+ _chunkBKBFSOGTcjs.mapError.call(void 0, self.value, (e) => _chunkBKBFSOGTcjs.some.call(void 0, e)),
330
+ (a) => [a, EMPTY_STREAM]
331
+ );
332
+ case "FromPull":
333
+ return self.pull;
334
+ case "Concat":
335
+ return _chunkBKBFSOGTcjs.orElseOptional.call(void 0,
336
+ _chunkBKBFSOGTcjs.map.call(void 0,
337
+ uncons(self.left),
338
+ ([a, tail]) => [a, concatStream(tail, self.right)]
339
+ ),
340
+ () => uncons(self.right)
341
+ );
342
+ case "Flatten":
343
+ return _chunkBKBFSOGTcjs.flatMap.call(void 0,
344
+ uncons(self.stream),
345
+ ([head, tail]) => _chunkBKBFSOGTcjs.orElseOptional.call(void 0,
346
+ _chunkBKBFSOGTcjs.map.call(void 0,
347
+ uncons(head),
348
+ ([a, as]) => [
349
+ a,
350
+ concatStream(as, flattenStream(tail))
351
+ ]
352
+ ),
353
+ () => uncons(flattenStream(tail))
354
+ )
355
+ );
356
+ case "Merge":
357
+ return makeMergePull(self.left, self.right, self.flip, 0);
358
+ case "Scoped":
359
+ return _chunkBKBFSOGTcjs.async.call(void 0, (env, cb) => {
360
+ const runtime = _chunkBKBFSOGTcjs.unsafeGetCurrentRuntime.call(void 0, );
361
+ const scope = new (0, _chunkBKBFSOGTcjs.Scope)(runtime);
362
+ const fiber = _chunkBKBFSOGTcjs.getCurrentFiber.call(void 0, );
363
+ _optionalChain([fiber, 'optionalAccess', _3 => _3.addFinalizer, 'call', _4 => _4((exit) => {
364
+ try {
365
+ scope.close(exit);
366
+ } catch (e3) {
367
+ }
368
+ })]);
369
+ const closeWith = (exit) => {
370
+ if (exit._tag === "Failure") {
371
+ const err = exit.cause.error;
372
+ if (err && typeof err === "object" && err._tag === "None") {
373
+ scope.close({ _tag: "Success", value: void 0 });
374
+ return;
375
+ }
376
+ }
377
+ scope.close(exit);
378
+ };
379
+ const wrap = (s) => fromPull(
380
+ _chunkBKBFSOGTcjs.async.call(void 0, (env2, cb2) => {
381
+ const pull = uncons(s);
382
+ _chunkBKBFSOGTcjs.unsafeRunFoldWithEnv.call(void 0,
383
+ pull,
384
+ env2,
385
+ (cause) => {
386
+ const ex = _chunkBKBFSOGTcjs.Exit.failCause(cause);
387
+ closeWith(ex);
388
+ cb2(ex);
389
+ },
390
+ ([a, tail]) => {
391
+ cb2(_chunkBKBFSOGTcjs.Exit.succeed([a, wrap(tail)]));
392
+ }
393
+ );
394
+ })
395
+ );
396
+ scope.fork(self.acquire).join((ex) => {
397
+ if (ex._tag === "Failure") {
398
+ closeWith(ex);
399
+ cb({ _tag: "Failure", cause: { _tag: "Fail", error: _chunkBKBFSOGTcjs.some.call(void 0, ex.cause.error) } });
400
+ return;
401
+ }
402
+ scope.addFinalizer((exit) => self.release(exit));
403
+ const inner = ex.value;
404
+ _chunkBKBFSOGTcjs.unsafeGetCurrentRuntime.call(void 0, ).fork(uncons(wrap(inner))).join(cb);
405
+ });
406
+ });
407
+ case "Managed":
408
+ return _chunkBKBFSOGTcjs.async.call(void 0, (env, cb) => {
409
+ const runtime = _chunkBKBFSOGTcjs.unsafeGetCurrentRuntime.call(void 0, );
410
+ const scope = new (0, _chunkBKBFSOGTcjs.Scope)(runtime);
411
+ _optionalChain([_chunkBKBFSOGTcjs.getCurrentFiber.call(void 0, ), 'optionalAccess', _5 => _5.addFinalizer, 'call', _6 => _6((exit) => {
412
+ try {
413
+ scope.close(exit);
414
+ } catch (e4) {
415
+ }
416
+ })]);
417
+ const closeWith = (exit) => {
418
+ if (exit._tag === "Failure") {
419
+ const err = exit.cause.error;
420
+ if (err && typeof err === "object" && err._tag === "None") {
421
+ scope.close({ _tag: "Success", value: void 0 });
422
+ return;
423
+ }
424
+ }
425
+ scope.close(exit);
426
+ };
427
+ scope.fork(self.acquire).join((ex) => {
428
+ if (ex._tag === "Failure") {
429
+ scope.close(ex);
430
+ cb({ _tag: "Failure", cause: { _tag: "Fail", error: _chunkBKBFSOGTcjs.some.call(void 0, ex.cause.error) } });
431
+ return;
432
+ }
433
+ const { stream: inner, release } = ex.value;
434
+ scope.addFinalizer((exit) => release(exit));
435
+ const wrap = (s) => fromPull(
436
+ _chunkBKBFSOGTcjs.async.call(void 0, (env2, cb2) => {
437
+ uncons(s)(env2, (ex2) => {
438
+ if (ex2._tag === "Failure") {
439
+ closeWith(ex2);
440
+ cb2(ex2);
441
+ return;
442
+ }
443
+ const [a, tail] = ex2.value;
444
+ cb2(_chunkBKBFSOGTcjs.Exit.succeed([a, wrap(tail)]));
445
+ });
446
+ })
447
+ );
448
+ _chunkBKBFSOGTcjs.unsafeGetCurrentRuntime.call(void 0, ).fork(uncons(wrap(inner))).join(cb);
449
+ });
450
+ });
451
+ }
452
+ }
453
+ function assertNever(x, msg) {
454
+ throw new Error(_nullishCoalesce(msg, () => ( `Unexpected value: ${String(x)}`)));
455
+ }
456
+ function mapStream(self, f) {
457
+ switch (self._tag) {
458
+ case "Empty":
459
+ return emptyStream();
460
+ case "FromArray":
461
+ return { _tag: "FromArray", values: self.values.map(f) };
462
+ case "Emit":
463
+ return emitStream(_chunkBKBFSOGTcjs.map.call(void 0, self.value, f));
464
+ case "FromPull":
465
+ return fromPull(
466
+ _chunkBKBFSOGTcjs.map.call(void 0, self.pull, ([a, tail]) => [f(a), mapStream(tail, f)])
467
+ );
468
+ case "Concat":
469
+ return concatStream(mapStream(self.left, f), mapStream(self.right, f));
470
+ case "Flatten": {
471
+ const mappedOuter = mapStream(
472
+ self.stream,
473
+ (inner) => mapStream(inner, f)
474
+ );
475
+ return flattenStream(mappedOuter);
476
+ }
477
+ case "Merge":
478
+ return mergeStream(
479
+ mapStream(self.left, f),
480
+ mapStream(self.right, f),
481
+ self.flip
482
+ );
483
+ case "Scoped":
484
+ return unwrapScoped(
485
+ _chunkBKBFSOGTcjs.map.call(void 0, self.acquire, (s) => mapStream(s, f)),
486
+ self.release
487
+ );
488
+ case "Managed":
489
+ return managedStream(
490
+ _chunkBKBFSOGTcjs.map.call(void 0, self.acquire, ({ stream, release }) => ({
491
+ stream: mapStream(stream, f),
492
+ release
493
+ }))
494
+ );
495
+ default:
496
+ return assertNever(self);
497
+ }
498
+ }
499
+ function rangeStream(start, end) {
500
+ const go = (i) => fromPull(
501
+ i > end ? _chunkBKBFSOGTcjs.asyncFail.call(void 0, _chunkBKBFSOGTcjs.none) : _chunkBKBFSOGTcjs.asyncSucceed.call(void 0, [i, go(i + 1)])
502
+ );
503
+ return go(start);
504
+ }
505
+ function zip(left, right) {
506
+ const pull = _chunkBKBFSOGTcjs.asyncFold.call(void 0,
507
+ _chunkBKBFSOGTcjs.asyncMapError.call(void 0, uncons(left), (opt) => widenOpt(opt)),
508
+ // si left termina o falla, el zip termina/falla igual
509
+ (opt) => _chunkBKBFSOGTcjs.asyncFail.call(void 0, opt),
510
+ ([a, tailL]) => _chunkBKBFSOGTcjs.asyncFold.call(void 0,
511
+ _chunkBKBFSOGTcjs.asyncMapError.call(void 0, uncons(right), (opt) => widenOpt(opt)),
512
+ (opt) => _chunkBKBFSOGTcjs.asyncFail.call(void 0, opt),
513
+ ([b, tailR]) => _chunkBKBFSOGTcjs.asyncSucceed.call(void 0, [
514
+ [a, b],
515
+ zip(tailL, tailR)
516
+ ])
517
+ )
518
+ );
519
+ return fromPull(pull);
520
+ }
521
+ function foreachStream(stream, f) {
522
+ const loop = (cur) => _chunkBKBFSOGTcjs.asyncFold.call(void 0,
523
+ // uncons: Option<E> -> Option<E|E2>
524
+ _chunkBKBFSOGTcjs.asyncMapError.call(void 0, uncons(cur), (opt) => widenOpt(opt)),
525
+ (opt) => {
526
+ if (opt._tag === "None") return _chunkBKBFSOGTcjs.asyncSucceed.call(void 0, void 0);
527
+ return _chunkBKBFSOGTcjs.asyncFail.call(void 0, opt);
528
+ },
529
+ ([a, tail]) => _chunkBKBFSOGTcjs.asyncFlatMap.call(void 0,
530
+ // f(a): E2 -> Option<E|E2>
531
+ _chunkBKBFSOGTcjs.asyncMapError.call(void 0, f(a), (e2) => _chunkBKBFSOGTcjs.some.call(void 0, e2)),
532
+ () => loop(tail)
533
+ )
534
+ );
535
+ return _chunkBKBFSOGTcjs.asyncFold.call(void 0,
536
+ loop(stream),
537
+ (opt) => {
538
+ if (opt._tag === "None") return _chunkBKBFSOGTcjs.asyncSucceed.call(void 0, void 0);
539
+ return _chunkBKBFSOGTcjs.asyncFail.call(void 0, opt.value);
540
+ },
541
+ () => _chunkBKBFSOGTcjs.asyncSucceed.call(void 0, void 0)
542
+ );
543
+ }
544
+ function fromArray(values) {
545
+ if (values.length === 0) return emptyStream();
546
+ return { _tag: "FromArray", values };
547
+ }
548
+ function collectStream(stream) {
549
+ const syncResult = drainStreamSyncFull(stream);
550
+ if (syncResult !== null) {
551
+ return _chunkBKBFSOGTcjs.asyncSucceed.call(void 0, syncResult);
552
+ }
553
+ const loop = (cur, acc) => _chunkBKBFSOGTcjs.asyncFold.call(void 0,
554
+ uncons(cur),
555
+ (opt) => {
556
+ if (opt._tag === "None") return _chunkBKBFSOGTcjs.succeed.call(void 0, acc);
557
+ return _chunkBKBFSOGTcjs.fail.call(void 0, opt);
558
+ },
559
+ ([a, tail]) => {
560
+ acc.push(a);
561
+ return loop(tail, acc);
562
+ }
563
+ );
564
+ return _chunkBKBFSOGTcjs.mapError.call(void 0, loop(stream, []), (opt) => {
565
+ if (opt._tag === "Some") return opt.value;
566
+ throw new Error("unreachable: stream end handled as success");
567
+ });
568
+ }
569
+ function drainStreamSyncFull(stream) {
570
+ const result = [];
571
+ let cur = stream;
572
+ while (true) {
573
+ switch (cur._tag) {
574
+ case "Empty":
575
+ return result;
576
+ case "FromArray": {
577
+ const arr = cur.values;
578
+ for (let i = 0; i < arr.length; i++) {
579
+ result.push(arr[i]);
580
+ }
581
+ return result;
582
+ }
583
+ case "Emit": {
584
+ const zio = cur.value;
585
+ if (zio._tag === "Succeed") {
586
+ result.push(zio.value);
587
+ return result;
588
+ }
589
+ return null;
590
+ }
591
+ case "Concat": {
592
+ const leftItems = drainStreamSyncFull(cur.left);
593
+ if (leftItems === null) return null;
594
+ for (let i = 0; i < leftItems.length; i++) {
595
+ result.push(leftItems[i]);
596
+ }
597
+ cur = cur.right;
598
+ break;
599
+ }
600
+ default:
601
+ return null;
602
+ }
603
+ }
604
+ }
605
+ function readerStream(reader, normalizeError) {
606
+ const pull = _chunkBKBFSOGTcjs.async.call(void 0, (_, cb) => {
607
+ reader.read().then(({ done, value }) => {
608
+ if (done) {
609
+ cb({ _tag: "Failure", cause: { _tag: "Fail", error: _chunkBKBFSOGTcjs.none } });
610
+ return;
611
+ }
612
+ cb({
613
+ _tag: "Success",
614
+ value: [value, fromPull(pull)]
615
+ });
616
+ }).catch((e) => {
617
+ cb({ _tag: "Failure", cause: { _tag: "Fail", error: _chunkBKBFSOGTcjs.some.call(void 0, normalizeError(e)) } });
618
+ });
619
+ });
620
+ return fromPull(pull);
621
+ }
622
+ function streamFromReadableStream(body, normalizeError) {
623
+ if (!body) return emptyStream();
624
+ let reader;
625
+ return unwrapScoped(
626
+ // acquire: produce un ZStream
627
+ _chunkBKBFSOGTcjs.sync.call(void 0, () => {
628
+ reader = body.getReader();
629
+ return readerStream(reader, normalizeError);
630
+ }),
631
+ // release: se corre en fin / error / interrupción
632
+ () => _chunkBKBFSOGTcjs.asyncSync.call(void 0, () => {
633
+ try {
634
+ _optionalChain([reader, 'optionalAccess', _7 => _7.cancel, 'call', _8 => _8()]);
635
+ } catch (e5) {
636
+ }
637
+ })
638
+ );
639
+ }
640
+
641
+
642
+
643
+
644
+
645
+
646
+
647
+
648
+
649
+
650
+
651
+
652
+
653
+
654
+
655
+
656
+
657
+
658
+
659
+
660
+
661
+
662
+
663
+
664
+
665
+
666
+
667
+ exports.sleep = sleep; exports.timeout = timeout; exports.retry = retry; exports.retryN = retryN; exports.retryWithBackoff = retryWithBackoff; exports.makeCircuitBreaker = makeCircuitBreaker; exports.widenOpt = widenOpt; exports.fromPull = fromPull; exports.unwrapScoped = unwrapScoped; exports.managedStream = managedStream; exports.mergeStream = mergeStream; exports.emptyStream = emptyStream; exports.emitStream = emitStream; exports.concatStream = concatStream; exports.flattenStream = flattenStream; exports.merge = merge; exports.uncons = uncons; exports.assertNever = assertNever; exports.mapStream = mapStream; exports.rangeStream = rangeStream; exports.zip = zip; exports.foreachStream = foreachStream; exports.fromArray = fromArray; exports.collectStream = collectStream; exports.streamFromReadableStream = streamFromReadableStream;