View Source SwarmEx.Tool behaviour (SwarmEx v0.2.0)
DEPRECATED: This module is deprecated. Tools should be implemented as regular modules with functions instead.
Migration Guide
Instead of using the Tool behavior, define your tools as regular modules with functions:
Before:
defmodule MyTool do
@behaviour SwarmEx.Tool
@impl true
def execute(args) do
# Perform tool operation
{:ok, result}
end
@impl true
def validate(args) do
# Validate incoming arguments
:ok
end
@impl true
def cleanup(args) do
# Cleanup any resources
:ok
end
end
After:
defmodule MyTool do
def process(args) do
# Validate args if needed
with :ok <- validate_args(args),
{:ok, result} <- do_process(args) do
cleanup(args)
{:ok, result}
end
end
defp validate_args(args) do
# Optional validation
:ok
end
defp do_process(args) do
# Main processing logic
{:ok, result}
end
defp cleanup(args) do
# Optional cleanup
:ok
end
end
The new approach:
- Is simpler and more idiomatic Elixir
- Gives more flexibility in function naming and implementation
- Allows for better integration with other libraries
- Reduces boilerplate code
- Makes testing easier
Summary
Functions
DEPRECATED: Use direct function calls instead.
DEPRECATED: Use regular module configuration instead.
DEPRECATED: Use regular module configuration instead.
DEPRECATED: Use regular module definition instead.
Types
@type args() :: term()
@type cleanup_result() :: :ok | {:error, term()}
@type options() :: [ timeout: non_neg_integer(), retries: non_neg_integer(), validate_args: boolean() ]
@type t() :: module()
@type validation_result() :: :ok | {:error, term()}
Callbacks
Functions
DEPRECATED: Use direct function calls instead.
Safely executes a tool with the given arguments and options.
Options
:timeout
- Maximum time in milliseconds to wait (default: 5000):retries
- Number of retry attempts (default: 3):validate_args
- Whether to validate arguments (default: true)
DEPRECATED: Use regular module configuration instead.
Retrieves the configuration for a registered tool.
DEPRECATED: Use regular module configuration instead.
Registers a tool configuration in the runtime.
DEPRECATED: Use regular module definition instead.
Validates that a module implements the Tool behavior correctly.