Raxol.Agent.Action.ToolConverter (Raxol Agent v2.6.0)

Copy Markdown View Source

Converts Action modules to LLM tool definitions and dispatches tool calls.

Use to_tool_definitions/1 to generate the tools parameter for OpenAI/Anthropic API calls, then dispatch_tool_call/3 to route the LLM's tool call response back to the matching Action module.

Summary

Functions

Dispatch an LLM tool call to the matching action module.

Build a tool result message for feeding back to the LLM.

Convert action modules to tool definitions for LLM API calls.

Types

effect()

@type effect() :: Raxol.Agent.Directive.t()

Functions

dispatch_tool_call(tool_call, action_modules, context \\ %{})

@spec dispatch_tool_call(map(), [module()], map()) ::
  {:ok, map()} | {:ok, map(), [effect()]} | {:error, term()}

Dispatch an LLM tool call to the matching action module.

The tool_call map should have "name" and "arguments" keys (standard OpenAI/Anthropic function calling format). Arguments can be a string (JSON) or a pre-parsed map with string keys.

Validates argument depth, key count, and value sizes before dispatch to prevent resource exhaustion from malformed LLM output.

Returns {:ok, result} or {:error, reason}.

format_tool_result(tool_call_id, result)

@spec format_tool_result(String.t(), map()) :: map()

Build a tool result message for feeding back to the LLM.

Encodes the result as a JSON string suitable for the tool role message.

to_tool_definitions(action_modules)

@spec to_tool_definitions([module()]) :: [map()]

Convert action modules to tool definitions for LLM API calls.

Returns a list of JSON Schema function tool definitions.