Jido.Agent.Directive (Jido v1.1.0-rc)
View SourceProvides a type-safe way to modify agent state through discrete, validated directives.
## Overview
Directives are immutable instructions that can be applied to an agent to modify its state in predefined ways. Each directive type is implemented as a separate struct with its own validation rules, helping ensure type safety and consistent state transitions.
## Available Directives
Enqueue
- Adds a new instruction to the agent's pending queue- Requires an action atom
- Supports optional params and context maps
- Supports optional opts keyword list
- Example:
%Enqueue{action: :move, params: %{location: :kitchen}}
RegisterAction
- Registers a new action module with the agent- Requires a valid module atom
- Example:
%RegisterAction{action_module: MyApp.Actions.Move}
DeregisterAction
- Removes an action module from the agent- Requires a valid module atom
- Example:
%DeregisterAction{action_module: MyApp.Actions.Move}
Spawn
- Spawns a child process under the agent's supervisor- Requires a module atom and arguments
- Example:
%Spawn{module: MyWorker, args: [id: 1]}
Kill
- Terminates a child process- Requires a valid PID
- Example:
%Kill{pid: #PID<0.123.0>}
## Usage
Directives can be applied to either an Agent or ServerState struct:
# Apply to Agent - returns updated agent
{:ok, updated_agent} = Directive.apply_directives(agent, directives)
# Apply to ServerState - returns updated state
{:ok, updated_state} = Directive.apply_directives(server_state, directives)
Each function validates directives before applying them and returns either:
{:ok, updated_state}
- Directives were successfully applied{:error, reason}
- Failed to apply directives
## Validation
Each directive type has its own validation rules:
Enqueue
requires a non-nil atom for the actionRegisterAction
requires a valid module atomDeregisterAction
requires a valid module atomSpawn
requires a valid module atom and argumentsKill
requires a valid PID
Failed validation results in an error tuple being returned and processing being halted.
## Error Handling
The module uses tagged tuples for error handling:
{:ok, updated_state}
- Successful application of directives{:error, reason}
- Failed validation or application
Common error reasons include:
:invalid_action
- The action specified in anEnqueue
is invalid:invalid_action_module
- The module specified in aRegister/DeregisterAction
is invalid:invalid_module
- The module specified in aSpawn
is invalid:invalid_pid
- The PID specified in aKill
is invalid:invalid_topic
- The topic specified in a broadcast/subscribe/unsubscribe directive is invalid
## Ideas Change Mode Change Verbosity Manage Router (add/remove/etc) Manage Skills (add/remove/etc) Manage Dispatchers (add/remove/etc)
Summary
Functions
Applies agent directives to an Agent struct.
Applies only server directives to a ServerState struct.
Types
@type directive_result() :: {:ok, Jido.Agent.t(), [t()]} | {:ok, Jido.Agent.Server.State.t(), [t()]} | {:error, term()}
Functions
@spec apply_agent_directive(Jido.Agent.t(), [t()], keyword()) :: directive_result()
Applies agent directives to an Agent struct.
Parameters
- agent: The Agent struct to modify
- directives: A list of directives to apply
- opts: Optional keyword list of options (default: [])
Returns
{:ok, updated_agent, unapplied_directives}
- Successfully applied agent directives{:error, reason}
- Failed to apply directives
@spec apply_server_directive(Jido.Agent.Server.State.t(), [t()], keyword()) :: directive_result()
Applies only server directives to a ServerState struct.
Parameters
- state: The ServerState struct to modify
- directives: A list of directives to apply
- opts: Optional keyword list of options (default: [])
Returns
{:ok, updated_state, unapplied_directives}
- Successfully applied server directives{:error, reason}
- Failed to apply directives