AI.Tools behaviour (fnord v0.8.27)
View SourceThis 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
@type args_error() :: missing_arg_error() | invalid_arg_error()
@type entry() :: Store.Project.Entry.t()
@type entry_not_found() :: {:error, :enoent}
@type frob_error() :: {:error, non_neg_integer(), binary()}
@type invalid_arg_error() :: {:error, :invalid_argument, binary()}
@type json_parse_error() :: {:error, Jason.DecodeError.t()}
@type missing_arg_error() :: {:error, :missing_argument, binary()}
@type project() :: Store.Project.t()
@type project_name() :: binary() | nil
@type project_not_found() :: {:error, :project_not_found} | {:error, :project_not_set}
@type raw_tool_result() :: :ok | {:ok, any()} | {:error, any()} | :error | frob_error()
@type something_not_found() :: project_not_found() | entry_not_found()
@type tool_error() :: {:error, binary()}
@type tool_name() :: binary()
@type tool_result() :: {:ok, binary()} | unknown_tool_error() | args_error() | tool_error() | frob_error()
@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() } } } } }
@type unknown_tool_error() :: {:error, :unknown_tool, binary()}
@type unparsed_args() :: binary()
Callbacks
@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.
@callback call(args :: map()) :: raw_tool_result()
Calls the tool with the provided arguments and returns the response as an :ok tuple.
@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.).
@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
.
@callback spec() :: tool_spec()
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.
@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
@spec all_tools() :: toolbox()
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.
@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.
@spec get_entry(binary()) :: {:ok, entry()} | something_not_found()
@spec get_entry(Store.Project.t(), binary()) :: {:ok, entry()} | entry_not_found()
@spec get_file_contents(binary()) :: {:ok, binary()} | something_not_found()
@spec get_project() :: {:ok, project()} | project_not_found()
@spec has_indexed_project() :: boolean()
@spec perform_tool_call(tool_name(), parsed_args(), toolbox() | nil) :: tool_result()
@spec required_arg_error(binary()) :: missing_arg_error()
@spec tool_module(tool_name(), toolbox() | nil) :: {:ok, module()} | unknown_tool_error()
Generate a list of tool specs from a toolbox map.
@spec validate_required_args(tool_name(), parsed_args(), toolbox() | nil) :: :ok | args_error()
@spec with_args(tool_name(), parsed_args(), (parsed_args() -> any()), toolbox() | nil) :: any()