fix: decode nested JSON tool arguments by schema
This commit is contained in:
1 parent
c9bd5ebb45
commit
6b930432cb
4 files changed
+244
No files matched your search
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
@@ -24,6 +25,68 @@ from nanobot.providers.openai_responses.parsing import parse_response_output
|
||||
_MAX_TOOL_RESULT_CHARS = AgentDefaults().max_tool_result_chars
|
||||
|
||||
|
||||
@pytest.mark.parametrize("schema, emitted, expected", [
|
||||
({"oneOf": [
|
||||
{"type": "object", "properties": {"count": {"type": "string"}}, "required": ["count"]},
|
||||
{"type": "object", "properties": {"count": {"type": "integer"}}, "required": ["count"]},
|
||||
]}, '{"count":12}', {"count": 12}),
|
||||
({"anyOf": [{"type": "object"}, {"type": "string"}]}, '{"count":12}', '{"count":12}'),
|
||||
({"type": "array", "items": {"type": "integer"}}, '["1","2"]', [1, 2]),
|
||||
])
|
||||
async def test_nested_arguments_reach_mcp_without_rewriting_model_history(schema, emitted, expected):
|
||||
from mcp.server.lowlevel import Server
|
||||
from mcp.shared.memory import create_connected_server_and_client_session
|
||||
from mcp.types import TextContent
|
||||
from mcp.types import Tool as MCPTool
|
||||
|
||||
from nanobot.agent.loop import AgentLoop
|
||||
from nanobot.agent.tools.mcp import MCPToolWrapper
|
||||
from nanobot.session.manager import Session
|
||||
|
||||
server = Server("nested-arguments-test")
|
||||
received = []
|
||||
|
||||
@server.list_tools()
|
||||
async def list_tools():
|
||||
return [MCPTool(name="echo", inputSchema={
|
||||
"type": "object", "properties": {"value": schema}, "required": ["value"],
|
||||
})]
|
||||
|
||||
@server.call_tool()
|
||||
async def call_tool(name, arguments):
|
||||
received.append((name, arguments))
|
||||
return [TextContent(type="text", text="accepted")]
|
||||
|
||||
async with create_connected_server_and_client_session(server) as client:
|
||||
definition = (await client.list_tools()).tools[0]
|
||||
registry = ToolRegistry()
|
||||
tool = MCPToolWrapper(client, "test", definition)
|
||||
registry.register(tool)
|
||||
schemas_before = json.dumps(registry.get_definitions())
|
||||
call = ToolCallRequest(id="nested", name=tool.name, arguments={"value": emitted})
|
||||
original_call = call.to_openai_tool_call()
|
||||
provider = MagicMock()
|
||||
provider.chat_stream_with_retry = AsyncMock(side_effect=[
|
||||
LLMResponse(content=None, tool_calls=[call]), LLMResponse(content="done"),
|
||||
])
|
||||
result = await AgentRunner().run(make_run_spec(
|
||||
provider, tools=registry, model="test-model", max_iterations=2,
|
||||
initial_messages=[{"role": "user", "content": "echo the value"}],
|
||||
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
|
||||
))
|
||||
assert result.final_content == "done"
|
||||
assert received == [("echo", {"value": expected})]
|
||||
requests = provider.chat_stream_with_retry.await_args_list
|
||||
model_call = next(m for m in requests[1].kwargs["messages"] if m.get("tool_calls"))
|
||||
assert model_call["tool_calls"] == [original_call]
|
||||
assert call.to_openai_tool_call() == original_call
|
||||
assert all(json.dumps(r.kwargs["tools"]) == schemas_before for r in requests)
|
||||
session = Session(key="test:nested-arguments")
|
||||
AgentLoop.__new__(AgentLoop)._save_turn(session, result.messages, skip=0)
|
||||
replay = next(m for m in session.get_history() if m.get("tool_calls"))
|
||||
assert replay["tool_calls"] == [original_call]
|
||||
|
||||
|
||||
class _DelayTool(Tool):
|
||||
def __init__(
|
||||
self,
|
||||
|
||||
Reference in new issue
Block a user