57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
"""Unit tests for OpenAI tool schema generation in ToolRegistry."""
|
|
|
|
from homeagent.tools.base import tool
|
|
from homeagent.tools.registry import ToolRegistry
|
|
|
|
|
|
def test_openai_tools_conversion():
|
|
registry = ToolRegistry()
|
|
|
|
@tool(name="calc_add", description="Add two numbers")
|
|
def calc_add(a: int, b: int = 0) -> str:
|
|
"""Add two integers.
|
|
|
|
Args:
|
|
a: First number
|
|
b: Second number
|
|
"""
|
|
return str(a + b)
|
|
|
|
registry.register(calc_add)
|
|
|
|
tools = registry.get_openai_tools()
|
|
assert len(tools) == 1
|
|
t = tools[0]
|
|
assert t["type"] == "function"
|
|
assert t["function"]["name"] == "calc_add"
|
|
assert t["function"]["description"] == "Add two numbers"
|
|
|
|
params = t["function"]["parameters"]
|
|
assert params["type"] == "object"
|
|
assert "a" in params["properties"]
|
|
assert params["properties"]["a"]["type"] == "integer"
|
|
assert "b" in params["properties"]
|
|
assert params["required"] == ["a"]
|
|
|
|
|
|
def test_openai_tools_exclude_subagents():
|
|
registry = ToolRegistry()
|
|
|
|
@tool(name="spawn_subagent", description="Subagent tool")
|
|
def dummy_spawn(task: str) -> str:
|
|
return task
|
|
|
|
@tool(name="regular_tool", description="Regular tool")
|
|
def dummy_regular() -> str:
|
|
return "ok"
|
|
|
|
registry.register(dummy_spawn)
|
|
registry.register(dummy_regular)
|
|
|
|
all_tools = registry.get_openai_tools(exclude_subagents=False)
|
|
assert any(t["function"]["name"] == "spawn_subagent" for t in all_tools)
|
|
|
|
subagent_tools = registry.get_openai_tools(exclude_subagents=True)
|
|
assert not any(t["function"]["name"] == "spawn_subagent" for t in subagent_tools)
|
|
assert any(t["function"]["name"] == "regular_tool" for t in subagent_tools)
|