Files
YiemAgent/docs/loadtools.md
T
2026-08-07 23:13:13 +07:00

3.0 KiB

Dynamic Tool Loading

Tools can be loaded dynamically from .jl files in the src/tools/ directory without hardcoding filenames in the main module.

How It Works

  1. src/tools/registry.jl defines a load_tools(dir::String) function that scans a directory for .jl files
  2. Each tool file must define a single function: get_tool() :: agentTool
  3. load_tools() sorts files alphabetically, includes each one, calls get_tool(), and registers the result
  4. Loaded tools are returned as Vector{agentTool} for use when constructing a yiemAgent

Directory Structure

src/
├── tools/
│   ├── registry.jl      # Tool loader (do not edit)
│   ├── get_weather.jl   # Your tool
│   └── query_db.jl      # Another tool
├── type.jl
├── utils.jl
├── agentCore.jl
├── api.jl
└── YiemAgent.jl

Creating a Tool

Each .jl file in src/tools/ must define get_tool() returning an agentTool:

# src/tools/get_weather.jl
function get_tool() :: agentTool
    return agentTool(
        name = "get_weather",
        label = "Weather Lookup",
        description = "Fetch current weather and forecast for a given city.",
        inputSchema = Dict{String,Any}(
            "type" => "object",
            "properties" => Dict(
                "city" => Dict("type" => "string", "description" => "City and country"),
                "units" => Dict("type" => "string", "enum" => ["celsius", "fahrenheit"], "default" => "celsius")
            ),
            "required" => ["city"]
        ),
        execute = (toolCallId, args, signal, onPartialResult) -> begin
            city = args["city"]
            return agentToolResult(
                [textContent("Weather in $(city): Sunny, 22C")],
                Dict{Any,Any}(), nothing, false
            )
        end,
        prepareArguments = nothing,
        parallelToolExecute = false
    )
end

No module wrapper needed — the registry includes each file in the current module scope so all types (agentTool, textContent, agentToolResult, etc.) resolve correctly.

Loading Tools

using .YiemAgent
using .YiemAgent: toolRegistry

# Load all tool files from src/tools/
tools = load_tools(joinpath(@__DIR__, "src", "tools"))

# Create agent with loaded tools
agent = yiemAgent(
    systemPrompt = "You are a helpful assistant.",
    model = my_model,
    tools = tools,
    llmCall = my_llm_call,
    agentEventSink = my_event_sink
)

Available Functions

Function Description
load_tools(dir::String) Scan directory and load all .jl tool files
register_tool(tool::agentTool) Register a single tool into the global registry
get_tools() Get deep copy of all registered tools
list_tools() List all registered tools as (name, label) pairs
clear_tools() Clear the global registry

File Loading Order

Files are sorted alphabetically before loading, so 01_database.jl loads before 02_weather.jl. This ensures deterministic registration order.