AgiCLI / agicli /tools.py
AGofficial's picture
Upload 24 files
b2863d5 verified
Raw History Blame Contribute Delete
1.75 kB
import inspect
import functools
# Global registry for AGI tools
TOOL_REGISTRY = {}
def agi_tool(func):
"""
Decorator to register a function as an AGI tool.
The function's docstring will be used as the tool description.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
# Get function signature
sig = inspect.signature(func)
params = list(sig.parameters.keys())
# Store tool info
TOOL_REGISTRY[func.__name__] = {
"func": func,
"description": func.__doc__.strip() if func.__doc__ else "No description provided.",
"parameters": params
}
return wrapper
def get_tools_prompt():
"""
Generates the XML-style prompt for all registered tools.
"""
prompt_lines = []
for name, info in TOOL_REGISTRY.items():
attr_part = " ".join([f'{p}=""' for p in info['parameters']])
line = f'<{name} {attr_part} /> - {info["description"]}'
prompt_lines.append(line)
return "\n".join(prompt_lines)
def handle_custom_tool(name, attrs):
"""
Calls a registered tool with the provided attributes.
"""
if name in TOOL_REGISTRY:
info = TOOL_REGISTRY[name]
func = info["func"]
# Filter attributes to match function parameters and handle type conversion (optional)
# For now, pass all attrs as strings, letting the function handle them
try:
# We only pass attributes that the function expects
valid_args = {k: v for k, v in attrs.items() if k in info['parameters']}
return func(**valid_args)
except Exception as e:
return f"Error executing tool {name}: {str(e)}"
return None