haive.core.graph.node.state_driven_node¶
State-driven nodes that get behavior from state.
This module implements nodes whose behavior is determined at runtime from the state, enabling true dynamic execution without recompilation.
Classes¶
Factory for creating state-driven nodes. |
|
Branch node that determines routing from state. |
|
Node that executes behavior from state. |
|
Schema for defining state-driven nodes in configuration. |
Module Contents¶
- class haive.core.graph.node.state_driven_node.DynamicNodeFactory¶
Factory for creating state-driven nodes.
This factory simplifies the creation of state-driven nodes and provides patterns for common use cases.
- static create_aggregator_node(name, aggregation_key='results')¶
Create a node that aggregates results.
- Parameters:
- Returns:
StateDrivenNode that aggregates parallel results
- Return type:
- static create_branch(name, default=None)¶
Create a state-driven branch.
- Parameters:
- Returns:
StateDrivenBranch instance
- Return type:
- static create_node(name, **kwargs)¶
Create a state-driven node.
- Parameters:
name (str) – Node name
**kwargs – Additional node configuration
- Returns:
StateDrivenNode instance
- Return type:
- static create_parallel_node(name, nodes)¶
Create a node that triggers parallel execution.
- Parameters:
- Returns:
StateDrivenNode that sets up parallel execution
- Return type:
- class haive.core.graph.node.state_driven_node.StateDrivenBranch(name, default_route=None)¶
Branch node that determines routing from state.
Unlike StateDrivenNode which executes behavior, this node specifically handles conditional routing based on state.
Examples
>>> branch = StateDrivenBranch("router") >>> >>> # In state, define routing logic >>> state.branches["router"] = lambda s: "path_a" if s.score > 0.5 else "path_b" >>> >>> # Branch will route based on state >>> next_node = branch(state)
Initialize state-driven branch.
- class haive.core.graph.node.state_driven_node.StateDrivenNode(name, fallback=None)¶
Node that executes behavior from state.
Instead of having fixed behavior at compile time, this node retrieves its behavior from the state at runtime. This enables:
Runtime behavior modification
Dynamic routing updates
Hot-swappable logic
No recompilation needed
The state must have a ‘nodes’ dictionary containing callables and a ‘routing_table’ for dynamic routing.
Examples
>>> # In state schema >>> class DynamicState(StateSchema): ... nodes: dict[str, Callable] = Field(default_factory=dict) ... routing_table: dict[str, list[str]] = Field(default_factory=dict) >>> >>> # Create state-driven node >>> node = StateDrivenNode("processor") >>> >>> # Add behavior to state >>> state.nodes["processor"] = lambda x: x.upper() >>> state.routing_table["processor"] = ["validator"] >>> >>> # Execute - behavior comes from state! >>> result = node(state)
Initialize state-driven node.
- Parameters:
name (str) – Node identifier used to lookup behavior in state
fallback (Optional[Callable]) – Optional fallback behavior if not found in state
- reset_execution_count()¶
Reset execution counter.
- Return type:
None
- class haive.core.graph.node.state_driven_node.StateNodeSchema(/, **data)¶
Bases:
pydantic.BaseModelSchema for defining state-driven nodes in configuration.
This allows nodes to be defined declaratively and created from configuration rather than code.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (Any)
- create_node()¶
Create the actual node from this schema.
- Returns:
StateDrivenNode or StateDrivenBranch instance
- Return type:
Any