fix: create default agent tools on first use (#1175)

This commit is contained in:
LHMQ878
2026-08-12 07:30:37 -04:00
committed by GitHub
parent 5570dbc7b6
commit 99ebbdfea2
4 changed files with 64 additions and 18 deletions
+1
View File
@@ -99,6 +99,7 @@ The following shortcut strings load tools directly. Passing a Tool instance is a
| write | Writes content to file |
| *.md | Loads a [`skill.md`](https://agentskills.io/specification) file |
## instructions
```yaml
+1 -1
View File
@@ -53,7 +53,7 @@ extras["dev"] = [
"pylint",
]
extras["agent"] = ["jinja2>=3.1.6", "mcp<2.0", "mcpadapt>=0.1.0", "smolagents>=1.23"]
extras["agent"] = ["beautifulsoup4>=4.9.3", "jinja2>=3.1.6", "mcp<2.0", "mcpadapt>=0.1.0", "smolagents>=1.23"]
extras["ann"] = [
"annoy>=1.16.3",
+45 -17
View File
@@ -30,22 +30,50 @@ class ToolFactory:
Methods to create tools.
"""
# Default toolkit
DEFAULTS = {
"bash": BashTool(),
"edit": EditTool(),
"glob": GlobTool(),
"grep": GrepTool(),
"python": PythonInterpreterTool(),
"question": UserInputTool(),
"read": ReadTool(),
"todowrite": TodoWriteTool(),
"websearch": WebSearchTool(),
"write": WriteTool(),
}
# Names in the default toolkit.
DEFAULTS = (
"bash",
"edit",
"glob",
"grep",
"python",
"question",
"read",
"todowrite",
"websearch",
"webview",
"write",
)
# Backwards compatible mappings
DEFAULTS["webview"] = DEFAULTS["read"]
@staticmethod
def default(name):
"""
Creates a default tool by alias name.
Args:
name: default tool alias name
Returns:
Tool
"""
tools = {
"bash": BashTool,
"edit": EditTool,
"glob": GlobTool,
"grep": GrepTool,
"python": PythonInterpreterTool,
"question": UserInputTool,
"read": ReadTool,
"webview": ReadTool,
"todowrite": TodoWriteTool,
"websearch": WebSearchTool,
"write": WriteTool,
}
if name not in tools:
raise KeyError(name)
return tools[name]()
@staticmethod
def create(config):
@@ -81,11 +109,11 @@ class ToolFactory:
# Get default tool, if applicable
elif isinstance(tool, str) and tool in ToolFactory.DEFAULTS:
tool = ToolFactory.DEFAULTS[tool]
tool = ToolFactory.default(tool)
# Get ALL default tools, if applicable
elif isinstance(tool, str) and tool == "defaults":
tools.extend(set(ToolFactory.DEFAULTS.values()))
tools.extend(ToolFactory.default(name) for name in ToolFactory.DEFAULTS if name != "webview")
tool = None
# Support importing MCP tool collections
+17
View File
@@ -3,6 +3,7 @@ Agent module tests
"""
import os
import subprocess
import tempfile
import unittest
@@ -13,6 +14,7 @@ from datetime import datetime
from smolagents import CodeAgent, PythonInterpreterTool
from txtai.agent import Agent
from txtai.agent.tool import ToolFactory
from txtai.embeddings import Embeddings
# agents.md content
@@ -227,3 +229,18 @@ class TestAgent(unittest.TestCase):
agent = Agent(tools=["http://localhost:8000/mcp"], llm="hf-internal-testing/tiny-random-LlamaForCausalLM", max_steps=1)
self.assertEqual(len(agent.tools), 2)
class TestToolFactory(unittest.TestCase):
"""
ToolFactory tests.
"""
def testDefaultToolCreation(self):
"""
Test default tools are created on demand.
"""
tool = ToolFactory.default("bash")
self.assertEqual(tool.name, "bash")
self.assertIsNot(tool, ToolFactory.default("bash"))