@deepagents/context 7.10.0 → 7.10.1

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.
package/README.md CHANGED
@@ -586,6 +586,8 @@ The package includes durable stream persistence utilities:
586
586
  - `persistedWriter` (low-level writer wrapper)
587
587
 
588
588
  ```typescript
589
+ import { Backoff } from '@zukhruf/async';
590
+
589
591
  import {
590
592
  PollingChangeSource,
591
593
  SqliteStreamStore,
@@ -595,13 +597,13 @@ import {
595
597
  const store = new SqliteStreamStore('./streams.db');
596
598
  const changeSource = new PollingChangeSource({
597
599
  reads: store,
598
- config: {
599
- minMs: 25,
600
- maxMs: 500,
600
+ backoff: new Backoff({
601
+ initial: 25,
601
602
  multiplier: 2,
602
- jitterRatio: 0.15,
603
- statusCheckEvery: 3,
604
- },
603
+ max: 500,
604
+ jitter: { kind: 'ratio', ratio: 0.15 },
605
+ }),
606
+ statusCheckEvery: 3,
605
607
  });
606
608
  const manager = new StreamManager({
607
609
  store,
package/dist/browser.js CHANGED
@@ -2412,60 +2412,94 @@ var StreamStore = class {
2412
2412
  };
2413
2413
 
2414
2414
  // packages/context/src/lib/stream-buffer.ts
2415
+ var MAX_QUEUED_CHUNKS = 128;
2415
2416
  async function persistedWriter(options) {
2416
- const {
2417
- writer,
2418
- store,
2419
- streamId,
2420
- strategy = "buffered",
2421
- flushSize = 20
2422
- } = options;
2417
+ const { writer, store, streamId } = options;
2423
2418
  let seq = 0;
2424
- let buffer = [];
2419
+ let queue = [];
2420
+ let appending;
2421
+ let appendSettled = Promise.resolve();
2422
+ let failure;
2425
2423
  let failedByChunk = false;
2426
2424
  let abortedByChunk = false;
2427
- async function flush() {
2428
- if (buffer.length === 0) return;
2429
- const batch = buffer;
2430
- buffer = [];
2431
- await appendBatch(batch);
2432
- }
2433
- function makeChunk(part) {
2434
- return {
2435
- streamId,
2436
- seq: seq++,
2437
- data: part,
2438
- createdAt: Date.now()
2439
- };
2440
- }
2441
- async function appendBatch(batch) {
2442
- await store.appendChunks(batch);
2443
- failedByChunk ||= collectStreamFailures(batch).length > 0;
2444
- }
2445
- async function persistChunk(chunk) {
2446
- if (chunk.data.type === "abort") {
2447
- abortedByChunk = true;
2448
- }
2449
- if (strategy === "immediate") {
2450
- await appendBatch([chunk]);
2451
- } else {
2452
- buffer.push(chunk);
2453
- if (buffer.length >= flushSize || chunk.data.type.startsWith("data-")) {
2454
- await flush();
2425
+ function enqueue(part, committed, failed) {
2426
+ if (failure) {
2427
+ failed(failure.error);
2428
+ return;
2429
+ }
2430
+ queue.push({
2431
+ chunk: { streamId, seq: seq++, data: part, createdAt: Date.now() },
2432
+ committed,
2433
+ failed
2434
+ });
2435
+ appending ??= appendQueued();
2436
+ }
2437
+ async function appendQueued() {
2438
+ while (queue.length > 0) {
2439
+ const batch = queue;
2440
+ queue = [];
2441
+ const chunks = batch.map(({ chunk }) => chunk);
2442
+ const append = Promise.try(() => store.appendChunks(chunks));
2443
+ appendSettled = append.then(
2444
+ () => void 0,
2445
+ () => void 0
2446
+ );
2447
+ try {
2448
+ await append;
2449
+ } catch (error) {
2450
+ failure = { error };
2451
+ const rejected = [...batch, ...queue];
2452
+ queue = [];
2453
+ for (const entry of rejected) entry.failed(error);
2454
+ break;
2455
2455
  }
2456
+ failedByChunk ||= collectStreamFailures(chunks).length > 0;
2457
+ abortedByChunk ||= chunks.some(({ data }) => data.type === "abort");
2458
+ for (const entry of batch) entry.committed();
2456
2459
  }
2460
+ appending = void 0;
2461
+ }
2462
+ async function settle() {
2463
+ while (appending) await appending;
2464
+ }
2465
+ async function flush() {
2466
+ await settle();
2467
+ if (failure) throw failure.error;
2457
2468
  }
2458
2469
  const wrappedWriter = {
2459
2470
  onError: writer.onError,
2460
- async write(part) {
2461
- await persistChunk(makeChunk(part));
2462
- writer.write(part);
2471
+ write(part) {
2472
+ const { promise, resolve, reject } = Promise.withResolvers();
2473
+ enqueue(
2474
+ part,
2475
+ () => {
2476
+ writer.write(part);
2477
+ resolve();
2478
+ },
2479
+ reject
2480
+ );
2481
+ return promise;
2463
2482
  },
2464
2483
  merge(stream) {
2484
+ let cancelled = false;
2465
2485
  const transform = new TransformStream({
2466
- async transform(chunk, controller) {
2467
- await persistChunk(makeChunk(chunk));
2468
- controller.enqueue(chunk);
2486
+ async transform(part, controller) {
2487
+ enqueue(
2488
+ part,
2489
+ () => {
2490
+ if (!cancelled) controller.enqueue(part);
2491
+ },
2492
+ (error) => {
2493
+ if (!cancelled) controller.error(error);
2494
+ }
2495
+ );
2496
+ while (queue.length >= MAX_QUEUED_CHUNKS && !failure) {
2497
+ await appendSettled;
2498
+ }
2499
+ },
2500
+ flush: () => flush(),
2501
+ cancel() {
2502
+ cancelled = true;
2469
2503
  }
2470
2504
  });
2471
2505
  writer.merge(stream.pipeThrough(transform));
@@ -2484,7 +2518,7 @@ async function persistedWriter(options) {
2484
2518
  );
2485
2519
  },
2486
2520
  async fail(error) {
2487
- await flush();
2521
+ await settle();
2488
2522
  if (failedByChunk) return;
2489
2523
  await store.updateStreamStatus(streamId, "failed", { error });
2490
2524
  },