Tools is broken

This commit is contained in:
eweeman
2026-01-15 17:01:02 -08:00
parent 4a6e2b898f
commit fcc86b52c2
11 changed files with 596 additions and 109 deletions

View File

@@ -1,6 +1,21 @@
import importlib
import inspect
import logging
from pathlib import Path
from types import SimpleNamespace
class ToolWrapper:
"""
Wraps a legacy tool function to provide a standard run() interface.
"""
def __init__(self, definition, func):
self.definition = definition
self.function = func
def run(self, **kwargs):
return self.function(**kwargs)
def load_tools(tools_path: Path):
registry = {}
@@ -14,27 +29,52 @@ def load_tools(tools_path: Path):
try:
module = importlib.import_module(module_name)
# Check if TOOL_DEFINITION exists
# --------------------------------------------------
# TOOL_DEFINITION is mandatory
# --------------------------------------------------
if not hasattr(module, "TOOL_DEFINITION"):
logging.warning(f"Tool {file.name} missing TOOL_DEFINITION, skipping")
continue
# Check if run function exists
if not hasattr(module, "run"):
logging.warning(f"Tool {file.name} missing 'run' function, skipping")
tool_def = module.TOOL_DEFINITION
tool_name = tool_def.get("name", file.stem)
# --------------------------------------------------
# Preferred: explicit run()
# --------------------------------------------------
if hasattr(module, "run") and callable(module.run):
registry[tool_name] = {
"definition": tool_def,
"function": module.run,
}
logging.info(f"Loaded tool (run): {tool_name}")
continue
name = module.TOOL_DEFINITION["name"]
func = getattr(module, "run")
# --------------------------------------------------
# Legacy auto-wrap (single public function)
# --------------------------------------------------
public_funcs = [
obj for name, obj in inspect.getmembers(module, inspect.isfunction)
if not name.startswith("_")
]
registry[name] = {
"definition": module.TOOL_DEFINITION,
"function": func,
}
if len(public_funcs) == 1:
wrapped = ToolWrapper(tool_def, public_funcs[0])
registry[tool_name] = {
"definition": tool_def,
"function": wrapped.run,
}
logging.info(f"Wrapped legacy tool: {tool_name}")
continue
logging.info(f"Loaded tool: {name}")
# --------------------------------------------------
# Failure case
# --------------------------------------------------
logging.error(
f"Tool {file.name} has no run() and multiple public functions; skipping"
)
except Exception as e:
except Exception:
logging.error(f"Failed to load tool {file.name}", exc_info=True)
return registry
return registry