pipeai 0.8.2 → 0.8.4

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
@@ -4,7 +4,7 @@ A typed multi-agent workflow pipeline built on top of the [Vercel AI SDK v6](htt
4
4
 
5
5
  Agents are pure AI SDK wrappers that return native `GenerateTextResult` / `StreamTextResult`. Workflows chain agents into pipelines with automatic stream merging, deterministic agent routing, and typed output extraction.
6
6
 
7
- The library is ~1000 lines across 4 files. It's designed to be read, understood, and modified a thin composition layer over AI SDK, not a framework to learn around.
7
+ It's a lean composition layer over the Vercel AI SDK — not a framework to learn around. Agents return native AI SDK results and workflows merge native streams, so anything you can do with the AI SDK still works underneath; pipeai stays out of the way and stays fully compatible.
8
8
 
9
9
  ## Core Concepts
10
10
 
@@ -366,6 +366,31 @@ const pipeline = Workflow.create<Ctx>()
366
366
 
367
367
  Nested workflows can be arbitrarily deep — a workflow step can contain another workflow that itself contains nested workflows.
368
368
 
369
+ ### Conditional steps via `when` / `otherwise`
370
+
371
+ Any `step` form — agent, inline `step(id, fn)`, or nested `step(workflow)` — accepts a `when` predicate. When it returns false the step is **skipped** and its body never runs:
372
+
373
+ ```ts
374
+ const pipeline = Workflow.create<Ctx, Input>()
375
+ // skip → passthrough: input is forwarded unchanged
376
+ .step(enrichAgent, { when: ({ input }) => input.needsEnrichment })
377
+ // skip → `otherwise` produces the value
378
+ .step("search", runSearch, {
379
+ when: ({ input }) => input.intent === "search",
380
+ otherwise: ({ input }) => ({ ...input, results: [] }),
381
+ })
382
+ // conditionally run a whole sub-pipeline
383
+ .step(productPipeline, { when: ({ input }) => input.intent === "product" });
384
+ ```
385
+
386
+ The output type reflects what can actually happen — this is deliberate, so a skipped step can't be mistaken for one that always ran:
387
+
388
+ - **`when` + `otherwise`** → output stays `TNextOutput` (`otherwise` returns it).
389
+ - **`when` without `otherwise`** → output widens to `TOutput | TNextOutput` (skip passes the input through). For same-shape tap/enrich steps the union collapses to a single type; when the shapes differ and you'd rather keep a single type, supply `otherwise` to produce a default.
390
+ - **no `when`** → `TNextOutput`, exactly as before.
391
+
392
+ `when` / `otherwise` throwing propagates as a normal step error (a downstream `.catch()` can observe it). A skipped step still fires the `onStepStart` / `onStepFinish` observability events with its passthrough/`otherwise` value as `output`.
393
+
369
394
  ### Predicate branching via `branch()`
370
395
 
371
396
  Route to different agents based on runtime conditions. The first matching `when` wins. A case without `when` acts as the default:
@@ -494,7 +519,7 @@ const pipeline = Workflow.create<Ctx>()
494
519
 
495
520
  ### Array iteration via `foreach()`
496
521
 
497
- `foreach()` maps each element of an array output through an agent or workflow. Items run in generate mode to avoid interleaved streams:
522
+ `foreach()` maps each element of an array output through an agent or workflow. By default items run in generate mode — `foreach` never auto-merges, since merging N concurrent streams would interleave into a garbled message (see [Streaming items](#streaming-foreach--parallel-items) to opt in via `handleStream`):
498
523
 
499
524
  ```ts
500
525
  const summarizer = new Agent<Ctx, string, string>({
@@ -511,14 +536,16 @@ const pipeline = Workflow.create<Ctx>()
511
536
  .step("combine", ({ input }) => input.join("\n\n"));
512
537
  ```
513
538
 
514
- Concurrent processing with bounded parallelism:
539
+ By default `foreach` is **unbounded** — every item runs concurrently. Pass `concurrency` to throttle (e.g. against provider rate limits):
515
540
 
516
541
  ```ts
517
- // Up to 3 items run simultaneously; the next launches as soon as one finishes.
542
+ // Cap at 3 items in flight; the next launches as soon as one finishes.
518
543
  .foreach(summarizer, { concurrency: 3 })
519
544
  ```
520
545
 
521
- `concurrency` is the **maximum number of items in flight at any moment** — backed by a semaphore. There's no lockstep batching: a slow item never blocks a finished slot from picking up the next pending one.
546
+ `concurrency` is the **maximum number of items in flight at any moment** — backed by a worker pool. There's no lockstep batching: a slow item never blocks a finished slot from picking up the next pending one.
547
+
548
+ > **Rate-limit hazard:** the unbounded default fires all items at once. For large arrays against a rate-limited provider, set an explicit `concurrency`.
522
549
 
523
550
  Works with nested workflows too:
524
551
 
@@ -532,6 +559,33 @@ const pipeline = Workflow.create<Ctx>()
532
559
  .foreach(processItem, { concurrency: 5 });
533
560
  ```
534
561
 
562
+ #### Streaming `foreach` / `parallel` items
563
+
564
+ When the workflow is run with `.stream(...)`, pass `handleStream` to `foreach` or `parallel` to run each **agent** item/branch in stream mode and control how it surfaces to the writer — the same hook as a single `.step(agent)`, plus an `itemIndex`:
565
+
566
+ ```ts
567
+ // foreach: itemIndex is the numeric item index
568
+ .foreach(summarizer, {
569
+ handleStream: ({ result, writer, input, itemIndex }) => {
570
+ writer.write({ type: "data-item-start", data: { itemIndex } });
571
+ writer.merge(result.toUIMessageStream());
572
+ },
573
+ })
574
+
575
+ // parallel record form: itemIndex is the branch key
576
+ .parallel({ summary: summarizer, sentiment: classifier }, {
577
+ handleStream: ({ result, writer, itemIndex }) => {
578
+ if (itemIndex === "summary") writer.merge(result.toUIMessageStream());
579
+ },
580
+ })
581
+ ```
582
+
583
+ - **No `handleStream`** → agent items run in generate mode (no auto-merge). `foreach`/`parallel` never auto-merge; you opt into surfacing explicitly.
584
+ - **`SealedWorkflow` items/branches** stream transitively via their own steps when the parent streams — `handleStream` is not called for them.
585
+ - `itemIndex`: `number` for `foreach` and tuple `parallel`; the key (`string`) for record `parallel`. `branch` threads the matched key (select) / case index (predicate) into its existing `handleStream`.
586
+ - Both default to unbounded concurrency, so streamed parts **interleave** (id-keyed, non-corrupting, but nondeterministic order). Set `concurrency: 1` if you want each item/branch to stream sequentially in order.
587
+ - Generate-mode runs (`.generate(...)`) never call `handleStream`.
588
+
535
589
  #### Per-item error recovery via `onError`
536
590
 
537
591
  By default a single item's failure aborts the whole `foreach`. Pass an `onError` handler to recover individual items — return a substitute value, return `Workflow.SKIP` to drop the failed index from the output array, or rethrow to abort the step (the throw is catchable by a downstream `.catch()`):
@@ -572,7 +626,9 @@ const pipeline = Workflow.create<Ctx, string>()
572
626
  .parallel([researcher, critic] as const);
573
627
  ```
574
628
 
575
- The same input (`state.output`) is fed to each branch. Default concurrency is `min(branches.length, 5)` most users want fan-out, but the cap protects against rate-limit pressure. Pass `concurrency: Infinity` (or `branches.length`) to opt out.
629
+ The same input (`state.output`) is fed to each branch. By default `parallel` is **unbounded** all branches run concurrently. Pass an explicit `concurrency` to throttle against rate-limit pressure.
630
+
631
+ Like `foreach`, `parallel` runs agent branches in generate mode and never auto-merges; pass `handleStream` to surface branch streams in a `.stream(...)` run — see [Streaming items](#streaming-foreach--parallel-items).
576
632
 
577
633
  ```ts
578
634
  .parallel({ a, b, c, d, e, f, g, h }, { concurrency: 3 }) // explicit override
@@ -599,7 +655,7 @@ The same input (`state.output`) is fed to each branch. Default concurrency is `m
599
655
 
600
656
  Gates inside parallel branches throw `NestedGateUnsupportedError`, same as `foreach` concurrent. The lowest-index suspending branch wins the marker; others contribute to `siblingSuspensions`. Multi-branch suspension semantics are finalized in F0.6 alongside `cancelOnFirstSuspend` — until then, all branches run to completion (or sibling-failure) before the marker reaches the caller.
601
657
 
602
- > **Rate-limit hazard:** `parallel`'s default `min(N, 5)` assumes ≥5 RPS headroom on your model provider. Symptoms of overflow: 429s and stair-stepped latency.
658
+ > **Rate-limit hazard:** `parallel`'s unbounded default fires all branches at once. With many branches on a rate-limited provider, set an explicit `concurrency`. Symptoms of overflow: 429s and stair-stepped latency.
603
659
 
604
660
  > **Concurrent ctx-mutation hazard:** branches share the `ctx` object by reference. Treat `ctx` as immutable inside parallel branches.
605
661
 
@@ -636,6 +692,8 @@ Use `while` for the opposite condition (repeat while true, stop when false):
636
692
 
637
693
  The `until` and `while` options are mutually exclusive — TypeScript enforces this at compile time.
638
694
 
695
+ Both forms are **do-while**: the body always runs at least once, then the predicate is checked against its `output`. So `while: () => false` still runs the body once — it is not a pre-check.
696
+
639
697
  When `maxIterations` is exceeded, a `WorkflowLoopError` is thrown — catchable by `.catch()`:
640
698
 
641
699
  ```ts
@@ -692,7 +750,7 @@ const { stream, output } = pipeline.stream(ctx, initialInput, {
692
750
  | `.step(id, fn)` | Transform the output. `fn` receives `{ ctx, input }` and returns the new output. |
693
751
  | `.branch([...cases])` | Predicate routing. First `when` match wins; case without `when` is default. |
694
752
  | `.branch({ select, agents })` | Key routing. `select` returns a key, runs the matching agent. |
695
- | `.foreach(target, opts?)` | Map each array element through an agent or workflow. `opts.concurrency` is the max items in flight (default: 1). `opts.onError` recovers per-item failures; return `Workflow.SKIP` to drop an index. |
753
+ | `.foreach(target, opts?)` | Map each array element through an agent or workflow. `opts.concurrency` is the max items in flight (default: unbounded). `opts.onError` recovers per-item failures; return `Workflow.SKIP` to drop an index. |
696
754
  | `.repeat(target, opts)` | Loop an agent or workflow. Use `{ until }` or `{ while }` (mutually exclusive). `maxIterations` defaults to 10. |
697
755
  | `.gate(id, opts?)` | Human-in-the-loop suspension point. Returns a result with `status: "suspended"` carrying a serializable snapshot. Resume via `loadState(gateId, snapshot)`. |
698
756
  | `.catch(id, fn)` | Handle errors. `fn` receives `{ error, ctx, lastOutput, stepId }` and returns a recovery value. Bypassed on suspension. |
@@ -971,9 +1029,9 @@ const final = await resumed.generate(ctx); // no response arg — state is see
971
1029
 
972
1030
  ### Cadence
973
1031
 
974
- - `checkpointEvery: N` — fire every N executable steps. Defaults to `max(1, ceil(executableCount / 4))` — 4 checkpoints across the run, floor of every step on tiny pipelines.
1032
+ - `checkpointEvery: N` — fire every N executable steps. Defaults to `max(1, ceil(stepCount / 4))` — 4 checkpoints across the run, floor of every step on tiny pipelines.
975
1033
  - `checkpointWhen({ stepIndex, stepId, ctx }) => boolean` — predicate variant. Mutually exclusive with `checkpointEvery`.
976
- - `.catch()` and `.finally()` nodes are NOT counted as executable, so adding cleanup doesn't surprise you with extra checkpoints.
1034
+ - The default-cadence denominator counts only checkpointable steps (`step` / `branch` / `foreach` / `repeat` / `parallel` / nested). `gate` nodes suspend or skip and never checkpoint, and `.catch()` / `.finally()` are cleanup none of them count, so adding them doesn't shift the cadence.
977
1035
 
978
1036
  ### Timeout via `AbortSignal`
979
1037
 
@@ -1068,9 +1126,12 @@ Pass an `observability` object to `Workflow.create()` to receive lifecycle event
1068
1126
  ```ts
1069
1127
  import { Workflow, type WorkflowObservability } from "pipeai";
1070
1128
 
1071
- const obs: WorkflowObservability = {
1129
+ // `WorkflowObservability<Ctx>` types `ctx` in every hook as your context.
1130
+ // It defaults to `unknown`, so the bare `WorkflowObservability` form still
1131
+ // works for context-agnostic hooks.
1132
+ const obs: WorkflowObservability<Ctx> = {
1072
1133
  onStepStart: ({ stepId, type, ctx, input }) => {
1073
- console.log(`step ${stepId} (${type}) starting`);
1134
+ console.log(`[${ctx.requestId}] step ${stepId} (${type}) starting`);
1074
1135
  },
1075
1136
  onStepFinish: ({ stepId, type, output, durationMs, suspended }) => {
1076
1137
  console.log(`step ${stepId} (${type}) finished in ${durationMs}ms, suspended=${suspended}`);
@@ -1085,6 +1146,8 @@ const pipeline = Workflow.create<Ctx, string>({ observability: obs })
1085
1146
  .step("respond", responder);
1086
1147
  ```
1087
1148
 
1149
+ `ctx` is typed as the workflow's context: pass `WorkflowObservability<Ctx>` (or just inline the object into `Workflow.create<Ctx>({ observability: { ... } })` and let `Ctx` flow in). The `input` / `output` fields stay `unknown` — they differ at every step in the chain, so only `ctx` (constant across the run) can be typed.
1150
+
1088
1151
  The hooks are threaded through every builder return, so any chain following `Workflow.create({ observability })` keeps the same hooks. `ResumedWorkflow` (gate resume via `loadState`) and `CheckpointResumedWorkflow` (checkpoint resume via `resumeFrom`) ALSO inherit it — events fire on resumed runs without re-wiring.
1089
1152
 
1090
1153
  ### Per-node firing rules