View Source Jido behaviour (Jido v1.0.0)

自動 (Jido) - A foundational framework for building autonomous, distributed agent systems in Elixir.

This module provides the main interface for interacting with Jido components, including:

  • Managing and interacting with Agents through a high-level API
  • Listing and retrieving Actions, Sensors, and Domains
  • Filtering and paginating results
  • Generating unique slugs for components

Agent Interaction Examples

# Find and act on an agent
"agent-id"
|> Jido.get_agent_by_id()
|> Jido.act(:command, %{param: "value"})

# Act asynchronously
{:ok, agent} = Jido.get_agent_by_id("agent-id")
Jido.act_async(agent, :command)

# Send management commands
{:ok, agent} = Jido.get_agent_by_id("agent-id")
Jido.manage(agent, :pause)

# Subscribe to agent events
{:ok, topic} = Jido.get_agent_topic("agent-id")
Phoenix.PubSub.subscribe(MyApp.PubSub, topic)

Summary

Functions

Clones an existing agent with a new ID.

Callback used by the generated start_link/0 function. This is where we actually call Jido.Supervisor.start_link.

Retrieves a running Agent by its ID.

Pipe-friendly version of get_agent that raises on errors.

Gets the current state of an agent.

Gets the status of an agent.

Gets the supervisor for an agent.

Gets the PubSub topic for an agent.

Retrieves a prompt file from the priv/prompts directory by its name.

Types

component_metadata()

@type component_metadata() :: %{
  module: module(),
  name: String.t(),
  description: String.t(),
  slug: String.t(),
  category: atom() | nil,
  tags: [atom()] | nil
}

Callbacks

config()

@callback config() :: keyword()

Functions

clone_agent(source_id, new_id, opts \\ [])

@spec clone_agent(String.t() | atom(), String.t() | atom(), keyword()) ::
  {:ok, pid()} | {:error, term()}

Clones an existing agent with a new ID.

Parameters

  • source_id: ID of the agent to clone
  • new_id: ID for the new cloned agent
  • opts: Optional keyword list of options to override for the new agent

Returns

  • {:ok, pid} with the new agent's process ID
  • {:error, reason} if cloning fails

Examples

iex> {:ok, new_pid} = Jido.clone_agent("source-agent", "cloned-agent")
{:ok, #PID<0.125.0>}

cmd(pid_or_tuple, action \\ :default, args \\ %{}, opts \\ [])

@spec cmd(pid() | {:ok, pid()}, atom(), map(), keyword()) :: any()

Sends a command to an agent.

Parameters

  • agent: Agent pid or return value from get_agent
  • action: The action to execute
  • args: Optional map of action arguments
  • opts: Optional keyword list of options

Returns

Returns the result of command execution.

Examples

iex> {:ok, agent} = Jido.get_agent("my-agent")
iex> Jido.cmd(agent, :generate_response, %{prompt: "Hello"})
{:ok, %{response: "Hi there!"}}

ensure_started(jido_module)

@spec ensure_started(module()) :: Supervisor.on_start()

Callback used by the generated start_link/0 function. This is where we actually call Jido.Supervisor.start_link.

get_action_by_slug(slug)

See Jido.Discovery.get_action_by_slug/1.

get_agent(id, opts \\ [])

@spec get_agent(
  String.t() | atom(),
  keyword()
) :: {:ok, pid()} | {:error, :not_found}

Retrieves a running Agent by its ID.

Parameters

  • id: String or atom ID of the agent to retrieve
  • opts: Optional keyword list of options:
    • :registry: Override the default agent registry

Returns

  • {:ok, pid} if agent is found and running
  • {:error, :not_found} if agent doesn't exist

Examples

iex> {:ok, agent} = Jido.get_agent("my-agent")
{:ok, #PID<0.123.0>}

# Using a custom registry
iex> {:ok, agent} = Jido.get_agent("my-agent", registry: MyApp.Registry)
{:ok, #PID<0.123.0>}

get_agent!(id, opts \\ [])

@spec get_agent!(
  String.t() | atom(),
  keyword()
) :: pid()

Pipe-friendly version of get_agent that raises on errors.

Parameters

  • id: String or atom ID of the agent to retrieve
  • opts: Optional keyword list of options:
    • :registry: Override the default agent registry

Returns

Examples

iex> "my-agent" |> Jido.get_agent!() |> Jido.cmd(:command)
:ok

get_agent_by_slug(slug)

See Jido.Discovery.get_agent_by_slug/1.

get_agent_state(pid)

@spec get_agent_state(pid() | {:ok, pid()} | String.t()) ::
  {:ok, term()} | {:error, term()}

Gets the current state of an agent.

Parameters

  • agent_or_id: Agent pid, ID, or return value from get_agent

Returns

  • {:ok, state} with the agent's current state
  • {:error, reason} if state couldn't be retrieved

Examples

iex> {:ok, state} = Jido.get_agent_state("my-agent")
{:ok, %Jido.Agent.Server.State{...}}

get_agent_status(pid)

@spec get_agent_status(pid() | {:ok, pid()} | String.t()) ::
  {:ok, atom()} | {:error, term()}

Gets the status of an agent.

Parameters

  • agent_or_id: Agent pid, ID, or return value from get_agent

Returns

  • {:ok, status} with the agent's status
  • {:error, reason} if status couldn't be retrieved

Examples

iex> {:ok, status} = Jido.get_agent_status("my-agent")
{:ok, :idle}

get_agent_supervisor(pid)

@spec get_agent_supervisor(pid() | {:ok, pid()} | String.t()) ::
  {:ok, pid()} | {:error, term()}

Gets the supervisor for an agent.

Parameters

  • agent_or_id: Agent pid, ID, or return value from get_agent

Returns

  • {:ok, supervisor_pid} with the agent's supervisor pid
  • {:error, reason} if supervisor couldn't be retrieved

Examples

iex> {:ok, supervisor} = Jido.get_agent_supervisor("my-agent")
{:ok, #PID<0.124.0>}

get_agent_topic(pid)

@spec get_agent_topic(pid() | {:ok, pid()} | String.t()) ::
  {:ok, String.t()} | {:error, term()}

Gets the PubSub topic for an agent.

Parameters

  • agent_or_id: Agent pid, ID, or return value from get_agent

Returns

  • {:ok, topic} with the agent's topic string
  • {:error, reason} if topic couldn't be retrieved

Examples

iex> {:ok, topic} = Jido.get_agent_topic("my-agent")
{:ok, "jido.agent.my-agent"}

iex> {:ok, agent} = Jido.get_agent("my-agent")
iex> {:ok, topic} = Jido.get_agent_topic(agent)
{:ok, "jido.agent.my-agent"}

get_sensor_by_slug(slug)

See Jido.Discovery.get_sensor_by_slug/1.

list_actions(opts \\ [])

See Jido.Discovery.list_actions/1.

list_agents(opts \\ [])

See Jido.Discovery.list_agents/1.

list_sensors(opts \\ [])

See Jido.Discovery.list_sensors/1.

prompt(name)

@spec prompt(atom()) :: String.t()

Retrieves a prompt file from the priv/prompts directory by its name.

Parameters

  • name: An atom representing the name of the prompt file (without .txt extension)

Returns

The contents of the prompt file as a string if found, otherwise raises an error.

Examples

iex> Jido.prompt(:system)
"You are a helpful AI assistant..."

iex> Jido.prompt(:nonexistent)
** (File.Error) could not read file priv/prompts/nonexistent.txt