AI.Tools behaviour (fnord v0.8.27)

View Source

This module defines the behaviour for tool calls. Defining a new tool requires implementing the spec/0 and call/2 functions.

The spec/0 function should return a map that describes the tool's capabilities and arguments, using a map to represent the OpenAPI spec.

The call/2 function generates the tool call response. It accepts the requesting AI.Completion's struct and a map derived from the parsed JSON provided by the agent, containing the tool call arguments. Note that, because the arguments are parsed from JSON, the keys will all be strings. Whether those are converted to symbols is between the tool implementation and the code it calls. What happens behind closed APIs is none of MY business.

Skeleton Implementation

defmodule AI.Tools.MyNewTool do
  @behaviour AI.Tools

  @impl AI.Tools
  def async?(), do: true

  @impl AI.Tools
  def ui_note_on_request(_args) do
    {"Doing something", "This tool is doing something."}
  end

  @impl AI.Tools
  def ui_note_on_result(_args, _result) do
    {"Did something", "This tool did something."}
  end

  @impl AI.Tools
  def read_args(args) do
    {:ok, args}
  end

  @impl AI.Tools
  def spec() do
    %{
      type: "function",
      function: %{
        name: "something_tool",
        description: "This tool does something.",
        strict: true,
        parameters: %{
          additionalProperties: false,
          type: "object",
          required: ["thing"],
          properties: %{
            thing: %{
              type: "string",
              description: "The thing to do."
            }
          }
        }
      }
    }
  end

  @impl AI.Tools
  def call(args) do
    {:ok, "IMPLEMENT ME"}
  end
end

Summary

Callbacks

Returns true if the tool is asynchronous, false otherwise. If false, when the LLM performs a multi-tool call, this tool will be called synchronously, after all other (asynchronous) tools have been called.

Calls the tool with the provided arguments and returns the response as an :ok tuple.

Returns true if the tool is available for use, false otherwise. This is used to determine whether the tool can be used in the current context, such as whether the tool is available in the current project or if it requires specific conditions to be met (e.g., a project being set, availability of an external tool like ripgrep, etc.).

Reads the arguments and returns a map of the arguments if they are valid. This is used to validate args before the tool is called. The result is what is passed to call/2, ui_note_on_request/1, and ui_note_on_result/2.

Returns the OpenAPI spec for the tool as an elixir map.

Return either a short string or a string tuple of label + detail to be displayed when the tool is called.

Return either a short string or a string tuple of label + detail to be displayed when the tool call is successful.

Functions

Returns a toolbox that includes all generally available tools and frobs.

Given a list of modules, returns a map from tool_name => module, using each module's spec().function.name value as the key.

Retrieves an argument from the parsed arguments map. If allow_empty? is true (default: false), an empty string or nil value is considered valid and will return {:ok, value}. If allow_empty? is false, an empty string or nil will return an error indicating a missing argument.

Generate a list of tool specs from a toolbox map.

Types

args_error()

@type args_error() :: missing_arg_error() | invalid_arg_error()

entry()

@type entry() :: Store.Project.Entry.t()

entry_not_found()

@type entry_not_found() :: {:error, :enoent}

frob_error()

@type frob_error() :: {:error, non_neg_integer(), binary()}

invalid_arg_error()

@type invalid_arg_error() :: {:error, :invalid_argument, binary()}

json_parse_error()

@type json_parse_error() :: {:error, Jason.DecodeError.t()}

missing_arg_error()

@type missing_arg_error() :: {:error, :missing_argument, binary()}

parsed_args()

@type parsed_args() :: %{required(binary()) => any()}

project()

@type project() :: Store.Project.t()

project_name()

@type project_name() :: binary() | nil

project_not_found()

@type project_not_found() :: {:error, :project_not_found} | {:error, :project_not_set}

raw_tool_result()

@type raw_tool_result() ::
  :ok | {:ok, any()} | {:error, any()} | :error | frob_error()

something_not_found()

@type something_not_found() :: project_not_found() | entry_not_found()

tool_error()

@type tool_error() :: {:error, binary()}

tool_name()

@type tool_name() :: binary()

tool_result()

@type tool_result() ::
  {:ok, binary()}
  | unknown_tool_error()
  | args_error()
  | tool_error()
  | frob_error()

tool_spec()

@type tool_spec() :: %{
  type: binary(),
  function: %{
    :name => binary(),
    :description => binary(),
    optional(:strict) => boolean(),
    parameters: %{
      optional(:additionalProperties) => boolean(),
      type: binary(),
      required: [binary()],
      properties: %{
        required(binary()) => %{
          :type => binary(),
          :description => binary(),
          optional(:default) => any()
        }
      }
    }
  }
}

toolbox()

@type toolbox() :: %{required(binary()) => module()}

unknown_tool_error()

@type unknown_tool_error() :: {:error, :unknown_tool, binary()}

unparsed_args()

@type unparsed_args() :: binary()

Callbacks

async?()

@callback async?() :: boolean()

Returns true if the tool is asynchronous, false otherwise. If false, when the LLM performs a multi-tool call, this tool will be called synchronously, after all other (asynchronous) tools have been called.

call(args)

@callback call(args :: map()) :: raw_tool_result()

Calls the tool with the provided arguments and returns the response as an :ok tuple.

is_available?()

@callback is_available?() :: boolean()

Returns true if the tool is available for use, false otherwise. This is used to determine whether the tool can be used in the current context, such as whether the tool is available in the current project or if it requires specific conditions to be met (e.g., a project being set, availability of an external tool like ripgrep, etc.).

read_args(args)

@callback read_args(args :: map()) :: {:ok, map()} | args_error()

Reads the arguments and returns a map of the arguments if they are valid. This is used to validate args before the tool is called. The result is what is passed to call/2, ui_note_on_request/1, and ui_note_on_result/2.

spec()

@callback spec() :: tool_spec()

Returns the OpenAPI spec for the tool as an elixir map.

ui_note_on_request(args)

@callback ui_note_on_request(args :: map()) :: {binary(), binary()} | binary() | nil

Return either a short string or a string tuple of label + detail to be displayed when the tool is called.

ui_note_on_result(args, result)

@callback ui_note_on_result(args :: map(), result :: any()) ::
  {binary(), binary()} | binary() | nil

Return either a short string or a string tuple of label + detail to be displayed when the tool call is successful.

Functions

all_tools()

@spec all_tools() :: toolbox()

Returns a toolbox that includes all generally available tools and frobs.

build_toolbox(modules)

@spec build_toolbox([module()] | %{required(binary()) => module()} | nil) :: toolbox()

Given a list of modules, returns a map from tool_name => module, using each module's spec().function.name value as the key.

get_arg(opts, key, allow_empty? \\ false)

@spec get_arg(parsed_args(), atom() | binary(), bool()) ::
  {:ok, any()} | {:error, binary()}

Retrieves an argument from the parsed arguments map. If allow_empty? is true (default: false), an empty string or nil value is considered valid and will return {:ok, value}. If allow_empty? is false, an empty string or nil will return an error indicating a missing argument.

get_entry(file)

@spec get_entry(binary()) :: {:ok, entry()} | something_not_found()

get_entry(project, file)

@spec get_entry(Store.Project.t(), binary()) :: {:ok, entry()} | entry_not_found()

get_file_contents(file)

@spec get_file_contents(binary()) :: {:ok, binary()} | something_not_found()

get_project()

@spec get_project() :: {:ok, project()} | project_not_found()

has_indexed_project()

@spec has_indexed_project() :: boolean()

is_async?(tool_name, tools \\ nil)

@spec is_async?(tool_name(), toolbox() | nil) :: boolean()

on_tool_request(tool, args, tools \\ nil)

@spec on_tool_request(tool_name(), parsed_args(), toolbox() | nil) ::
  {binary(), binary()} | binary() | nil

on_tool_result(tool, args, result, tools \\ nil)

@spec on_tool_result(tool_name(), parsed_args(), any(), toolbox() | nil) ::
  {binary(), binary()} | binary() | nil

perform_tool_call(tool, args, tools \\ nil)

@spec perform_tool_call(tool_name(), parsed_args(), toolbox() | nil) :: tool_result()

required_arg_error(key)

@spec required_arg_error(binary()) :: missing_arg_error()

tool_module(tool_name, tools \\ nil)

@spec tool_module(tool_name(), toolbox() | nil) ::
  {:ok, module()} | unknown_tool_error()

tool_spec(tool, tools \\ nil)

@spec tool_spec(tool_name(), toolbox() | nil) ::
  {:ok, tool_spec()} | {:error, :unknown_tool, binary()}

tool_spec!(tool, tools \\ nil)

@spec tool_spec!(tool_name(), toolbox() | nil) :: tool_spec()

toolbox_to_specs(toolbox)

@spec toolbox_to_specs(toolbox()) :: [tool_spec()]

Generate a list of tool specs from a toolbox map.

tools()

validate_required_args(tool, args, tools \\ nil)

@spec validate_required_args(tool_name(), parsed_args(), toolbox() | nil) ::
  :ok | args_error()

with_args(tool, args, fun, tools \\ nil)

@spec with_args(tool_name(), parsed_args(), (parsed_args() -> any()), toolbox() | nil) ::
  any()