Sagents.AgentsDynamicSupervisor (Sagents v0.8.0-rc.2)
Copy MarkdownDynamic supervisor for managing AgentSupervisor instances.
Each AgentSupervisor manages a single agent (conversation) and its supporting infrastructure. This supervisor allows agents to be started and stopped dynamically as conversations are created and terminated.
Scope
Unlike FileSystemSupervisor which scopes by user/project, this supervisor manages individual
agent instances identified by agent_id (typically "conversation-{id}").
Usage
# Start the supervisor (typically in your Application)
{:ok, pid} = AgentsDynamicSupervisor.start_link(name: MyApp.AgentsDynamicSupervisor)
# Start an agent supervisor
{:ok, agent_sup_pid} = AgentsDynamicSupervisor.start_agent(
agent_id: "conversation-123",
agent: agent,
initial_state: state,
pubsub: {Phoenix.PubSub, :my_app_pubsub}
)
# Stop an agent supervisor
:ok = AgentsDynamicSupervisor.stop_agent("conversation-123")
# List all running agents
agent_ids = AgentsDynamicSupervisor.list_agents()
Summary
Functions
Returns a specification to start this module under a supervisor.
Count running agents.
List all running agent IDs.
Start a new agent supervisor as a child of this dynamic supervisor.
Start a new agent supervisor and wait for it to be ready.
Start the AgentsDynamicSupervisor.
Stop an agent supervisor.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec count_agents(atom() | pid()) :: non_neg_integer()
Count running agents.
Parameters
supervisor- Supervisor reference (optional, defaults to__MODULE__)
Examples
count = AgentsDynamicSupervisor.count_agents()
# => 5
List all running agent IDs.
Parameters
supervisor- Supervisor reference (optional, defaults to__MODULE__)
Returns
List of agent_id strings
Examples
agent_ids = AgentsDynamicSupervisor.list_agents()
# => ["conversation-1", "conversation-2"]
@spec start_agent(keyword()) :: DynamicSupervisor.on_start_child()
Start a new agent supervisor as a child of this dynamic supervisor.
Parameters
opts- Keyword list of options passed to AgentSupervisor.start_link/1:agent_id- Agent identifier (required, will be extracted to set supervisor name):agent- The Agent struct (required):initial_state- Initial State for AgentServer (optional):pubsub- PubSub configuration as{module(), atom()}tuple (optional, used only forPhoenix.Presencewiring):inactivity_timeout- Timeout in milliseconds (optional):shutdown_delay- Shutdown delay in milliseconds (optional):presence_tracking- Presence tracking configuration (optional):conversation_id- Conversation identifier (optional):agent_persistence- Module implementingSagents.AgentPersistence(optional):display_message_persistence- Module implementingSagents.DisplayMessagePersistence(optional):supervisor- Supervisor reference (optional, defaults to__MODULE__)
Returns
{:ok, pid}- Agent supervisor started successfully{:ok, pid, info}- Agent supervisor already running (idempotent){:error, reason}- Failed to start
Examples
{:ok, agent_sup_pid} = AgentsDynamicSupervisor.start_agent(
agent_id: "conversation-123",
agent: agent,
initial_state: state,
pubsub: {Phoenix.PubSub, :my_app_pubsub}
)
@spec start_agent_sync(keyword()) :: {:ok, pid()} | {:ok, pid(), :already_started} | {:error, term()}
Start a new agent supervisor and wait for it to be ready.
This is a synchronous version that waits for the AgentServer child to be
fully registered before returning. See AgentSupervisor.start_link_sync/1
for details on why this is needed.
Parameters
Same as start_agent/1, plus:
:startup_timeout- Maximum time to wait for AgentServer to be ready (default: 5000ms)
Examples
{:ok, agent_sup_pid} = AgentsDynamicSupervisor.start_agent_sync(
agent_id: "conversation-123",
agent: agent,
initial_state: state,
pubsub: {Phoenix.PubSub, :my_app_pubsub},
startup_timeout: 10_000
)
@spec start_link(keyword()) :: Supervisor.on_start()
Start the AgentsDynamicSupervisor.
Options
:name- Registered name for the supervisor (optional, defaults to__MODULE__)
Examples
{:ok, pid} = start_link(name: MyApp.AgentsDynamicSupervisor)
Stop an agent supervisor.
Parameters
agent_id- Agent identifieropts- Options (optional):supervisor- Supervisor reference (defaults to__MODULE__):reason- Shutdown reason (defaults to:normal):timeout- Shutdown timeout in milliseconds (defaults to:infinity)
Examples
:ok = AgentsDynamicSupervisor.stop_agent("conversation-123")
:ok = AgentsDynamicSupervisor.stop_agent("conversation-123", reason: :shutdown, timeout: 5000)