View Source SwarmEx.Agent behaviour (SwarmEx v0.1.0)

Defines the behavior and implementation for SwarmEx agents.

Each agent in the SwarmEx system is a process that can:

  • Process messages from other agents or clients
  • Execute tools and handle their results
  • Maintain internal state
  • Participate in agent networks

Example

defmodule MyAgent do
  use SwarmEx.Agent

  def init(opts) do
    # Initialize agent state
    {:ok, opts}
  end

  def handle_message(msg, state) do
    # Handle incoming message
    {:ok, response, state}
  end

  def handle_tool(tool_name, args, state) do
    # Execute tool functionality
    {:ok, result, state}
  end
end

Summary

Functions

Creates a new agent process with the given module and options.

Stops an agent process.

Validates the agent implementation to ensure all required callbacks are implemented correctly.

Types

@type error() :: {:error, term()}
@type message() :: term()
@type response() :: {:ok, term(), state()} | {:error, term()}
@type state() :: term()
@type tool() :: atom()
@type tool_args() :: term()

Callbacks

Link to this callback

handle_handoff(target, state)

View Source (optional)
@callback handle_handoff(target :: pid(), state()) :: {:ok, state()} | error()
Link to this callback

handle_message(message, state)

View Source
@callback handle_message(message(), state()) :: response()
Link to this callback

handle_tool(tool, tool_args, state)

View Source (optional)
@callback handle_tool(tool(), tool_args(), state()) :: response()

Functions

Link to this function

create(module, opts \\ [])

View Source
@spec create(
  module(),
  keyword()
) :: {:ok, pid()} | {:error, term()}

Creates a new agent process with the given module and options.

Link to this function

stop(agent, reason \\ :normal)

View Source
@spec stop(pid() | atom() | binary(), term()) :: :ok | {:error, term()}

Stops an agent process.

@spec validate_agent(module()) :: :ok | {:error, term()}

Validates the agent implementation to ensure all required callbacks are implemented correctly.