|
| 1 | +.. _core_concepts: |
| 2 | + |
| 3 | +############################################################## |
| 4 | +Core Concepts |
| 5 | +############################################################## |
| 6 | + |
| 7 | +.. meta:: |
| 8 | + :description: Understand the core concepts of Junjo, including State, Store, Node, Edge, Condition, Graph, and Workflow. Learn how these components work together to build powerful and scalable Python workflows. |
| 9 | + :keywords: junjo, python, workflow, state management, node, edge, graph, core concepts |
| 10 | + |
| 11 | +This page breaks down the fundamental building blocks of the Junjo library. Understanding these concepts is key to effectively designing, building, and debugging your workflows. |
| 12 | + |
| 13 | +State |
| 14 | +===== |
| 15 | + |
| 16 | +**What is it?** |
| 17 | +A `BaseState` is a Pydantic model that defines the data structure for your workflow's state. It acts as a centralized, type-safe container for all the data that your workflow will operate on. |
| 18 | + |
| 19 | +**Key Characteristics:** |
| 20 | +- **Pydantic-Based:** Leverages Pydantic for data validation and type hinting. |
| 21 | +- **Immutable in Practice:** While the state object itself can be replaced, it is treated as immutable within the workflow. Nodes do not modify the state directly; they request changes through the store. |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + from junjo import BaseState |
| 26 | +
|
| 27 | + class MyWorkflowState(BaseState): |
| 28 | + user_input: str |
| 29 | + processed_data: dict | None = None |
| 30 | + is_complete: bool = False |
| 31 | +
|
| 32 | +Store |
| 33 | +===== |
| 34 | + |
| 35 | +**What is it?** |
| 36 | +A `BaseStore` is a class that manages the state of a workflow. It holds the `BaseState` and provides methods (often called "actions") to update the state in a controlled and predictable manner. |
| 37 | + |
| 38 | +**Key Characteristics:** |
| 39 | +- **State Management:** The single source of truth for the workflow's state. |
| 40 | +- **Redux-Inspired:** Follows a pattern where state is updated by dispatching actions, ensuring that state changes are explicit and traceable. |
| 41 | +- **Concurrency Safe:** Uses an `asyncio.Lock` to ensure that state updates are atomic, preventing race conditions. |
| 42 | + |
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + from junjo import BaseStore |
| 46 | +
|
| 47 | + class MyWorkflowStore(BaseStore[MyWorkflowState]): |
| 48 | + async def set_processed_data(self, data: dict) -> None: |
| 49 | + await self.set_state({"processed_data": data}) |
| 50 | +
|
| 51 | + async def mark_as_complete(self) -> None: |
| 52 | + await self.set_state({"is_complete": True}) |
| 53 | +
|
| 54 | +Node |
| 55 | +==== |
| 56 | + |
| 57 | +**What is it?** |
| 58 | +A `Node` represents a single unit of work in your workflow. It's where your business logic, API calls, or any other operations are executed. |
| 59 | + |
| 60 | +**Key Characteristics:** |
| 61 | +- **Atomic Unit of Work:** Each node should have a single, well-defined responsibility. |
| 62 | +- **Interacts with the Store:** Nodes receive the workflow's store as an argument to their `service` method, allowing them to read the current state and dispatch actions to update it. |
| 63 | +- **Asynchronous:** The `service` method is an `async` function, allowing for non-blocking I/O operations. |
| 64 | + |
| 65 | +.. code-block:: python |
| 66 | +
|
| 67 | + from junjo import Node |
| 68 | +
|
| 69 | + class ProcessDataNode(Node[MyWorkflowStore]): |
| 70 | + async def service(self, store: MyWorkflowStore) -> None: |
| 71 | + state = await store.get_state() |
| 72 | + # Perform some processing on state.user_input |
| 73 | + processed_data = {"result": "some_value"} |
| 74 | + await store.set_processed_data(processed_data) |
| 75 | +
|
| 76 | +Edge |
| 77 | +==== |
| 78 | + |
| 79 | +**What is it?** |
| 80 | +An `Edge` defines a directed connection between two nodes in a workflow graph. It represents a potential path of execution. |
| 81 | + |
| 82 | +**Key Characteristics:** |
| 83 | +- **Defines Flow:** Edges connect a `tail` node to a `head` node, establishing the sequence of operations. |
| 84 | +- **Can be Conditional:** An edge can have an associated `Condition` that determines whether the transition from the tail to the head should occur. |
| 85 | + |
| 86 | +.. code-block:: python |
| 87 | +
|
| 88 | + from junjo import Edge |
| 89 | +
|
| 90 | + edge = Edge(tail=node1, head=node2) |
| 91 | +
|
| 92 | +Condition |
| 93 | +========= |
| 94 | + |
| 95 | +**What is it?** |
| 96 | +A `Condition` is a class that contains logic to determine whether an `Edge` should be traversed. |
| 97 | + |
| 98 | +**Key Characteristics:** |
| 99 | +- **Pure Function of State:** A condition's `evaluate` method should only depend on the current state of the workflow. It should not have any side effects. |
| 100 | +- **Enables Branching:** Conditions are the primary mechanism for creating branching logic in your workflows. |
| 101 | + |
| 102 | +.. code-block:: python |
| 103 | +
|
| 104 | + from junjo import Condition |
| 105 | +
|
| 106 | + class DataIsProcessed(Condition[MyWorkflowState]): |
| 107 | + def evaluate(self, state: MyWorkflowState) -> bool: |
| 108 | + return state.processed_data is not None |
| 109 | +
|
| 110 | + edge = Edge(tail=node1, head=node2, condition=DataIsProcessed()) |
| 111 | +
|
| 112 | +Graph |
| 113 | +===== |
| 114 | + |
| 115 | +**What is it?** |
| 116 | +A `Graph` is a collection of nodes and edges that defines the complete structure of your workflow. |
| 117 | + |
| 118 | +**Key Characteristics:** |
| 119 | +- **Source and Sink:** A graph has a single entry point (`source`) and a single exit point (`sink`). |
| 120 | +- **Defines the Workflow Structure:** The graph is a complete representation of all possible paths of execution in your workflow. |
| 121 | + |
| 122 | +.. code-block:: python |
| 123 | +
|
| 124 | + from junjo import Graph |
| 125 | +
|
| 126 | + workflow_graph = Graph( |
| 127 | + source=start_node, |
| 128 | + sink=end_node, |
| 129 | + edges=[ |
| 130 | + Edge(tail=start_node, head=process_node), |
| 131 | + Edge(tail=process_node, head=end_node, condition=DataIsProcessed()) |
| 132 | + ] |
| 133 | + ) |
| 134 | +
|
| 135 | +Workflow |
| 136 | +======== |
| 137 | + |
| 138 | +**What is it?** |
| 139 | +A `Workflow` is the main executable component that takes a `graph_factory` and a `store_factory` and runs the defined process. |
| 140 | + |
| 141 | +**Key Characteristics:** |
| 142 | +- **Executable:** The `Workflow` class has an `execute` method that starts the workflow. |
| 143 | +- **Manages Execution:** It traverses the graph, executing nodes and evaluating conditions, until the `sink` node is reached. |
| 144 | +- **Isolated Execution:** Each call to `execute` uses the provided factories to create a fresh `Graph` and `Store`, ensuring that each execution is isolated and concurrency-safe. |
| 145 | + |
| 146 | +.. code-block:: python |
| 147 | +
|
| 148 | + from junjo import Workflow |
| 149 | +
|
| 150 | + def create_graph() -> Graph: |
| 151 | + # ... (graph creation logic) |
| 152 | + return workflow_graph |
| 153 | +
|
| 154 | + sample_workflow = Workflow[MyWorkflowState, MyWorkflowStore]( |
| 155 | + name="My First Workflow", |
| 156 | + graph_factory=create_graph, |
| 157 | + store_factory=lambda: MyWorkflowStore( |
| 158 | + initial_state=MyWorkflowState(user_input="hello") |
| 159 | + ) |
| 160 | + ) |
| 161 | +
|
| 162 | + await sample_workflow.execute() |
0 commit comments