优化目录结构
This commit is contained in:
+12
-21
@@ -2,7 +2,7 @@ import asyncio
|
||||
import json
|
||||
import time
|
||||
|
||||
from tools import CalculatorArguments, ToolRegistry, ToolSpec
|
||||
from tools import CurrentTimeArguments, ToolRegistry, ToolSpec
|
||||
|
||||
|
||||
def test_builtin_tool_definitions() -> None:
|
||||
@@ -11,37 +11,28 @@ def test_builtin_tool_definitions() -> None:
|
||||
definitions = registry.definitions()
|
||||
|
||||
assert [item["function"]["name"] for item in definitions] == [ # type: ignore[index]
|
||||
"get_current_time",
|
||||
"calculate",
|
||||
"get_current_time"
|
||||
]
|
||||
|
||||
|
||||
def test_calculator_and_time_tools() -> None:
|
||||
def test_current_time_tool() -> None:
|
||||
async def scenario() -> None:
|
||||
registry = ToolRegistry()
|
||||
calculation = await registry.execute(
|
||||
"calculate", '{"expression":"(2 + 3) * 4"}'
|
||||
)
|
||||
current_time = await registry.execute(
|
||||
"get_current_time", '{"timezone":"Asia/Shanghai"}'
|
||||
)
|
||||
|
||||
assert calculation.is_error is False
|
||||
assert json.loads(calculation.content)["result"] == 20
|
||||
assert current_time.is_error is False
|
||||
assert json.loads(current_time.content)["timezone"] == "Asia/Shanghai"
|
||||
|
||||
asyncio.run(scenario())
|
||||
|
||||
|
||||
def test_tool_argument_and_expression_errors_are_safe() -> None:
|
||||
def test_tool_argument_and_execution_errors_are_safe() -> None:
|
||||
async def scenario() -> None:
|
||||
registry = ToolRegistry()
|
||||
invalid_json = await registry.execute("calculate", "not-json")
|
||||
invalid_json = await registry.execute("get_current_time", "not-json")
|
||||
unknown = await registry.execute("missing", "{}")
|
||||
unsafe = await registry.execute(
|
||||
"calculate", '{"expression":"__import__(\\"os\\").system(\\"id\\")"}'
|
||||
)
|
||||
invalid_timezone = await registry.execute(
|
||||
"get_current_time", '{"timezone":"Not/A_Real_Zone"}'
|
||||
)
|
||||
@@ -50,8 +41,6 @@ def test_tool_argument_and_expression_errors_are_safe() -> None:
|
||||
assert "invalid_arguments" in invalid_json.content
|
||||
assert unknown.is_error is True
|
||||
assert "unknown_tool" in unknown.content
|
||||
assert unsafe.is_error is True
|
||||
assert "execution_failed" in unsafe.content
|
||||
assert invalid_timezone.is_error is True
|
||||
assert "execution_failed" in invalid_timezone.content
|
||||
|
||||
@@ -61,18 +50,20 @@ def test_tool_argument_and_expression_errors_are_safe() -> None:
|
||||
def test_tool_timeout_returns_error() -> None:
|
||||
def slow_handler(arguments):
|
||||
time.sleep(0.05)
|
||||
return {"result": 1}
|
||||
return {"timezone": "UTC"}
|
||||
|
||||
async def scenario() -> None:
|
||||
registry = ToolRegistry(timeout_seconds=0.01)
|
||||
registry._specs["calculate"] = ToolSpec(
|
||||
name="calculate",
|
||||
registry._specs["get_current_time"] = ToolSpec(
|
||||
name="get_current_time",
|
||||
description="slow",
|
||||
arguments_model=CalculatorArguments,
|
||||
arguments_model=CurrentTimeArguments,
|
||||
handler=slow_handler,
|
||||
)
|
||||
|
||||
result = await registry.execute("calculate", '{"expression":"1+1"}')
|
||||
result = await registry.execute(
|
||||
"get_current_time", '{"timezone":"UTC"}'
|
||||
)
|
||||
|
||||
assert result.is_error is True
|
||||
assert "timeout" in result.content
|
||||
|
||||
Reference in New Issue
Block a user