Core Abstractions

The Pocket Flow Framework is built around three core abstractions: Nodes, Flows, and BatchFlows. These abstractions allow you to define modular, reusable, and scalable workflows.

1. Nodes

A Node is the fundamental building block of a workflow. It represents a single unit of work and is implemented by extending the BaseNode class. Nodes have three primary methods:

Nodes can be chained together using successors, which define transitions based on the post method's return value.

2. Flows

A Flow orchestrates the execution of a sequence of nodes. It starts with a single entry point (the start node) and follows transitions defined by the post method of each node. The Flow class extends BaseNode and provides methods for orchestrating the execution of nodes.

3. BatchFlows

A BatchFlow extends the Flow class to handle workflows that process multiple items in parallel. It prepares a list of items, orchestrates their execution, and aggregates the results.

Example: Simple Flow

Here’s an example of creating a simple flow with two nodes:

import { BaseNode, Flow, DEFAULT_ACTION } from "../src/pocket";

class NodeA extends BaseNode {
  async prep(sharedState: any): Promise<void> {}
  async execCore(prepResult: any): Promise<void> {}
  async post(prepResult: any, execResult: any, sharedState: any): Promise<string> {
    return "default";
  }
}

class NodeB extends BaseNode {
  async prep(sharedState: any): Promise<void> {}
  async execCore(prepResult: any): Promise<void> {}
  async post(prepResult: any, execResult: any, sharedState: any): Promise<string> {
    return DEFAULT_ACTION;
  }
}

const nodeA = new NodeA();
const nodeB = new NodeB();
nodeA.addSuccessor(nodeB, "default");

const flow = new Flow(nodeA);
flow.run({});

This flow starts with NodeA and transitions to NodeB when NodeA.post() returns "default".