R6 class representing the orchestration layer for multi-agent systems.
Manages the call stack, context switching, and delegation between agents.
The Flow acts as the "conductor" - it suspends the calling agent,
activates the delegate agent, and resumes the caller with the result.
Methods
Method new()
Initialize a new Flow.
Usage
Flow$new(
session,
model,
registry = NULL,
max_depth = 5,
max_steps_per_agent = 10
)
Arguments
session
A ChatSession object for shared state between agents.
model
The default model ID to use (e.g., "openai:gpt-4o").
registry
Optional AgentRegistry for agent lookup.
max_depth
Maximum delegation depth (prevents infinite loops). Default 5.
max_steps_per_agent
Maximum ReAct steps per agent. Default 10.
Method depth()
Get the current call stack depth.
Method current()
Get the current active agent.
Returns
The currently active Agent, or NULL.
Get the shared session.
Returns
The ChatSession object.
Method set_global_context()
Set the global context (the user's original goal).
Usage
Flow$set_global_context(context)
Arguments
context
Character string describing the overall goal.
Returns
Invisible self for chaining.
Method global_context()
Get the global context.
Returns
The global context string.
Method delegate()
Delegate a task to another agent.
Usage
Flow$delegate(agent, task, context = NULL)
Arguments
agent
The Agent to delegate to.
task
The task instruction.
context
Optional additional context.
Details
This is the core orchestration method. It:
Checks depth limit
Pushes current agent to stack (if any)
Builds recursive context
Executes the delegate agent
Pops and returns result
Returns
The text result from the delegate agent.
Method run()
Run a primary agent (the Manager).
Arguments
agent
The primary/manager Agent to run.
task
The user's task/input.
Details
Entry point for a multi-agent flow. The primary agent is run with
delegation tools automatically injected from the registry.
Returns
The final result from the primary agent.
Method clone()
The objects of this class are cloneable with this method.
Arguments
deep
Whether to make a deep clone.