Normandy.Tools.Registry (normandy v0.6.1)

View Source

Manages a collection of tools available to an agent.

The Registry provides a structured way to register, retrieve, and list tools that an agent can use during execution.

Summary

Functions

Returns the number of tools in the registry.

Filters tools by parameter type.

Filters tools by whether they have required parameters.

Retrieves a tool by name from the registry.

Retrieves a tool by name, raising if not found.

Gets metadata for a specific tool by name.

Checks if a tool with the given name exists in the registry.

Gets schema introspection data for a tool if available.

Returns a list of all registered tools.

Gets metadata for all tools in the registry.

Returns a list of all tool names in the registry.

Creates a new empty tool registry.

Creates a new tool registry with the given tools.

Registers a tool in the registry.

Generates a list of tool schemas for LLM consumption.

Gets tools that support a specific constraint type.

Removes a tool from the registry by name.

Types

t()

@type t() :: %Normandy.Tools.Registry{tools: %{required(String.t()) => struct()}}

Functions

count(registry)

@spec count(t()) :: non_neg_integer()

Returns the number of tools in the registry.

Examples

iex> registry = Normandy.Tools.Registry.new([%Tool1{}, %Tool2{}])
iex> Normandy.Tools.Registry.count(registry)
2

filter_by_param_type(registry, param_type)

@spec filter_by_param_type(t(), String.t()) :: t()

Filters tools by parameter type.

Returns a new registry containing only tools that have a parameter of the given type.

Examples

iex> registry = Normandy.Tools.Registry.new([%Calculator{}, %StringTool{}])
iex> Normandy.Tools.Registry.filter_by_param_type(registry, "number")
%Normandy.Tools.Registry{tools: %{"calculator" => %Calculator{}}}

filter_by_required_params(registry, has_required?)

@spec filter_by_required_params(t(), boolean()) :: t()

Filters tools by whether they have required parameters.

Returns a new registry containing only tools that match the criteria.

Examples

iex> registry = Normandy.Tools.Registry.new([%Tool1{}, %Tool2{}])
iex> Normandy.Tools.Registry.filter_by_required_params(registry, true)
%Normandy.Tools.Registry{tools: %{"tool1" => %Tool1{}}}

get(registry, tool_name)

@spec get(t(), String.t()) :: {:ok, struct()} | :error

Retrieves a tool by name from the registry.

Returns {:ok, tool} if found, :error otherwise.

Examples

iex> registry = Normandy.Tools.Registry.new([%CalculatorTool{}])
iex> Normandy.Tools.Registry.get(registry, "calculator")
{:ok, %CalculatorTool{}}

iex> Normandy.Tools.Registry.get(registry, "nonexistent")
:error

get!(registry, tool_name)

@spec get!(t(), String.t()) :: struct()

Retrieves a tool by name, raising if not found.

Examples

iex> registry = Normandy.Tools.Registry.new([%CalculatorTool{}])
iex> Normandy.Tools.Registry.get!(registry, "calculator")
%CalculatorTool{}

get_metadata(registry, tool_name)

@spec get_metadata(t(), String.t()) :: {:ok, map()} | :error

Gets metadata for a specific tool by name.

Returns detailed metadata including schema introspection if available.

Examples

iex> registry = Normandy.Tools.Registry.new([%CalculatorTool{}])
iex> Normandy.Tools.Registry.get_metadata(registry, "calculator")
{:ok, %{
  name: "calculator",
  description: "Performs arithmetic operations",
  input_schema: %{...},
  fields: [...]
}}

has_tool?(registry, tool_name)

@spec has_tool?(t(), String.t()) :: boolean()

Checks if a tool with the given name exists in the registry.

Examples

iex> registry = Normandy.Tools.Registry.new([%CalculatorTool{}])
iex> Normandy.Tools.Registry.has_tool?(registry, "calculator")
true

iex> Normandy.Tools.Registry.has_tool?(registry, "nonexistent")
false

introspect_schema(registry, tool_name)

@spec introspect_schema(t(), String.t()) :: {:ok, [map()]} | {:ok, nil} | :error

Gets schema introspection data for a tool if available.

Returns schema field information for schema-based tools, or nil for legacy tools.

Examples

iex> registry = Normandy.Tools.Registry.new([%EnhancedCalculator{}])
iex> Normandy.Tools.Registry.introspect_schema(registry, "enhanced_calculator")
{:ok, [
  %{name: :operation, type: :string, required: true, ...},
  ...
]}

list(registry)

@spec list(t()) :: [struct()]

Returns a list of all registered tools.

Examples

iex> registry = Normandy.Tools.Registry.new([%Tool1{}, %Tool2{}])
iex> Normandy.Tools.Registry.list(registry)
[%Tool1{}, %Tool2{}]

list_metadata(registry)

@spec list_metadata(t()) :: [map()]

Gets metadata for all tools in the registry.

Returns a list of metadata maps for each registered tool.

Examples

iex> registry = Normandy.Tools.Registry.new([%Tool1{}, %Tool2{}])
iex> Normandy.Tools.Registry.list_metadata(registry)
[%{name: "tool1", ...}, %{name: "tool2", ...}]

list_names(registry)

@spec list_names(t()) :: [String.t()]

Returns a list of all tool names in the registry.

Examples

iex> registry = Normandy.Tools.Registry.new([%CalculatorTool{}])
iex> Normandy.Tools.Registry.list_names(registry)
["calculator"]

new()

@spec new() :: t()

Creates a new empty tool registry.

Examples

iex> Normandy.Tools.Registry.new()
%Normandy.Tools.Registry{tools: %{}}

new(tools)

@spec new([struct()]) :: t()

Creates a new tool registry with the given tools.

Examples

iex> tools = [%MyTool{}, %OtherTool{}]
iex> Normandy.Tools.Registry.new(tools)
%Normandy.Tools.Registry{tools: %{"my_tool" => %MyTool{}, ...}}

register(registry, tool)

@spec register(
  t(),
  struct()
) :: t()

Registers a tool in the registry.

The tool is indexed by its tool_name.

Examples

iex> registry = Normandy.Tools.Registry.new()
iex> tool = %CalculatorTool{}
iex> Normandy.Tools.Registry.register(registry, tool)
%Normandy.Tools.Registry{tools: %{"calculator" => %CalculatorTool{}}}

to_tool_schemas(registry)

@spec to_tool_schemas(t()) :: [map()]

Generates a list of tool schemas for LLM consumption.

Returns a list of maps containing tool metadata (name, description, schema).

Examples

iex> registry = Normandy.Tools.Registry.new([%CalculatorTool{}])
iex> Normandy.Tools.Registry.to_tool_schemas(registry)
[
  %{
    name: "calculator",
    description: "Performs arithmetic operations",
    input_schema: %{type: "object", ...}
  }
]

tools_with_constraint(registry, constraint_type)

@spec tools_with_constraint(t(), atom()) :: [String.t()]

Gets tools that support a specific constraint type.

Returns a list of tool names that have parameters with the given constraint.

Examples

iex> registry = Normandy.Tools.Registry.new([%Calculator{}, %StringTool{}])
iex> Normandy.Tools.Registry.tools_with_constraint(registry, :enum)
["calculator", "string_tool"]

unregister(registry, tool_name)

@spec unregister(t(), String.t()) :: t()

Removes a tool from the registry by name.

Examples

iex> registry = Normandy.Tools.Registry.new([%CalculatorTool{}])
iex> Normandy.Tools.Registry.unregister(registry, "calculator")
%Normandy.Tools.Registry{tools: %{}}