This commit is contained in:
2026-08-11 12:15:05 +07:00
parent ed91260468
commit 5a27630ccf
5 changed files with 132 additions and 60 deletions
+74 -46
View File
@@ -22,17 +22,17 @@ struct toolStore
end
"""
Create a new isolated tool store.
toolStore(; name="default") -> toolStore
Create a new empty tool store.
# Keyword Arguments
- `name::String`: Identifier for this store (default: "default")
- `name::String`: Display name for logging (default: `"default"`)
# Examples
# Example
```julia
store = toolStore(name="agent1")
tools = loadTools(store, "src/tools")
registerTool(store, my_tool)
agent = yiemAgent(tools=getTools(store), llmCall=..., _tool_store=store)
julia> store = toolStore(name="agent1")
toolStore(OrderedDict{String, agentTool}(), "agent1")
```
"""
function toolStore(; name::String="default")::toolStore
@@ -40,14 +40,32 @@ function toolStore(; name::String="default")::toolStore
end
"""
List tool definition — lets the agent query available tools for collision detection
when creating new tools via writeTool.
listTool(store::toolStore) -> agentTool
Return an `agentTool` definition for listing registered tools.
Each call produces a **new** tool object that captures (closes over)
`store`. `loadTools` auto-registers one so the LLM can discover tools
at runtime.
# Arguments
- `store::toolStore`: The tool store to list from
- `store`: The tool store whose tools will be listed when the tool runs
Each `toolStore` gets its own `listTool` instance bound to that store,
so each agent sees only its own tools.
# Example
```julia
julia> store = toolStore(name="agent1");
julia> loadTools(store, "src/tools") # auto-registers listTools
[toolRegistry:agent1] Loaded tool: getWeather (Weather Lookup)
[toolRegistry:agent1] Registered tool: listTools
julia> tools = getTools(store)
OrderedDict{String, agentTool} with 4 entries:
"getWeather" => agentTool(...)
"getTime" => agentTool(...)
"writeTool" => agentTool(...)
"listTools" => agentTool(...)
```
"""
function listTool(store::toolStore)::agentTool
return agentTool(
@@ -80,30 +98,31 @@ function listTool(store::toolStore)::agentTool
end
"""
Load all tool modules from a directory into a specific toolStore.
Load `.jl` tool files from `dir` into `store`, then auto-register
`listTool` so the LLM can discover available tools at runtime.
Scans `dir` for `.jl` files. Each file must define a function named
`getTool()::agentTool`. Files are sorted alphabetically so tool
registration order is deterministic.
Each `.jl` file is loaded into its own **submodule** so that all functions
defined in the file (`validateRequiredArgs`, `prepareArguments`, `executeTool`,
and any helper functions) are namespaced and never collide with other tools.
Each `.jl` file must define `function getTool()::agentTool ... end`.
Files are sorted alphabetically for deterministic registration order.
Each file is loaded into its own Julia submodule to avoid name collisions.
# Arguments
- `store::toolStore`: The tool store to register tools into
- `dir::String`: Directory path to scan for `.jl` tool files
- `store`: Tool store to populate
- `dir`: Directory containing `.jl` tool files
# Returns
- `OrderedDict{String, agentTool}`: All loaded tools keyed by name
- The same `store.tools` dict (modified in place)
# Errors
- Throws `ArgumentError` if a tool file does not define a `getTool` function
- Throws `ArgumentError` if `dir` does not exist or a file lacks `getTool()`
# Examples
# Example
```julia
julia> store = toolStore(name="agent1")
julia> tools = loadTools(store, "src/tools")
julia> store = toolStore(name="agent1");
julia> loadTools(store, "src/tools")
[toolRegistry:agent1] Loaded tool: getWeather (Weather Lookup)
[toolRegistry:agent1] Loaded tool: getTime (Time Lookup)
[toolRegistry:agent1] Registered tool: listTools
OrderedDict{String, agentTool} with 3 entries:
"getWeather" => agentTool(...)
"getTime" => agentTool(...)
@@ -168,24 +187,30 @@ function loadTools(store::toolStore, dir::String)::OrderedDict{String, agentTool
end
end
registerTool(store, listTool(store))
return store.tools
end
"""
Register a single agentTool into a specific toolStore.
registerTool(store::toolStore, tool::agentTool) -> OrderedDict{String, agentTool}
Add `tool` to `store`, overwriting any existing tool with the same name.
# Arguments
- `store::toolStore`: The tool store to register into
- `tool::agentTool`: The tool to register
- `store`: Tool store to modify
- `tool`: The `agentTool` to register
# Returns
- `OrderedDict{String, agentTool}`: Updated tool dict for this store
- The same `store.tools` dict (modified in place)
# Examples
# Example
```julia
julia> store = toolStore(name="agent1")
julia> registerTool(store, my_tool)
[toolRegistry:agent1] Registered tool: my_tool
julia> store = toolStore(name="agent1");
julia> registerTool(store, listTool(store))
[toolRegistry:agent1] Registered tool: listTools
OrderedDict{String, agentTool} with 1 entry:
"listTools" => agentTool(...)
```
"""
function registerTool(store::toolStore, tool::agentTool)::OrderedDict{String, agentTool}
@@ -195,22 +220,21 @@ function registerTool(store::toolStore, tool::agentTool)::OrderedDict{String, ag
end
"""
Get the registered tools from a specific toolStore.
Return the tools registered in `store`.
Returns the internal `OrderedDict` directly — O(1) lookup by name,
ordered iteration preserving registration order.
The returned dict is the **same object** stored inside `store` — mutations
to it (e.g. via `registerTool`) are visible through subsequent calls.
# Arguments
- `store::toolStore`: The tool store to query
- `store`: Tool store to query
# Returns
- `OrderedDict{String, agentTool}`: Tools keyed by name, in registration order
# Examples
# Example
```julia
julia> getTools(store)
OrderedDict{String, agentTool} with 3 entries:
"listTools" => agentTool(...)
julia> tools = getTools(store)
OrderedDict{String, agentTool} with 2 entries:
"getWeather" => agentTool(...)
"getTime" => agentTool(...)
```
@@ -220,18 +244,22 @@ function getTools(store::toolStore)::OrderedDict{String, agentTool}
end
"""
Clear all registered tools from a specific toolStore.
Remove all tools from `store`.
# Arguments
- `store::toolStore`: The tool store to clear
- `store`: Tool store to clear
# Returns
- `nothing`
# Examples
# Example
```julia
julia> clearTools(store)
[toolRegistry:agent1] Registry cleared
nothing
julia> getTools(store)
OrderedDict{String, agentTool} with 0 entries
```
"""
function clearTools(store::toolStore)::Nothing