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
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
@@ -134,6 +135,80 @@ def test_prepare_call_parses_json_string_arguments() -> None:
|
||||
assert error is None
|
||||
|
||||
|
||||
def test_prepare_call_decodes_json_string_for_nested_object_schema() -> None:
|
||||
registry = ToolRegistry()
|
||||
registry.register(
|
||||
_FakeTool(
|
||||
"read_note",
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"target": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {"type": "string"},
|
||||
"path": {"type": "string"},
|
||||
},
|
||||
"required": ["type", "path"],
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {"type": "string"},
|
||||
"name": {"type": "string"},
|
||||
},
|
||||
"required": ["type", "name"],
|
||||
},
|
||||
],
|
||||
},
|
||||
"format": {"type": "string"},
|
||||
},
|
||||
"required": ["target", "format"],
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
tool, params, error = registry.prepare_call(
|
||||
"read_note",
|
||||
{
|
||||
"target": '{"type":"path","path":"Projects/nanobot.md"}',
|
||||
"format": "content",
|
||||
},
|
||||
)
|
||||
|
||||
assert tool is not None
|
||||
assert error is None
|
||||
assert params == {
|
||||
"target": {"type": "path", "path": "Projects/nanobot.md"},
|
||||
"format": "content",
|
||||
}
|
||||
|
||||
|
||||
def test_prepare_call_preserves_json_like_text_for_string_schema() -> None:
|
||||
registry = ToolRegistry()
|
||||
registry.register(
|
||||
_FakeTool(
|
||||
"echo_text",
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {"value": {"type": "string"}},
|
||||
"required": ["value"],
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
tool, params, error = registry.prepare_call(
|
||||
"echo_text",
|
||||
{"value": '{"type":"path"}'},
|
||||
)
|
||||
|
||||
assert tool is not None
|
||||
assert error is None
|
||||
assert params == {"value": '{"type":"path"}'}
|
||||
|
||||
|
||||
def test_prepare_call_rejects_malformed_json_string_arguments() -> None:
|
||||
registry = ToolRegistry()
|
||||
registry.register(_FakeTool("read_file"))
|
||||
@@ -146,6 +221,67 @@ def test_prepare_call_rejects_malformed_json_string_arguments() -> None:
|
||||
assert "parameters must be a JSON object" in error
|
||||
|
||||
|
||||
@pytest.mark.parametrize("union", ["oneOf", "anyOf"])
|
||||
@pytest.mark.parametrize("branches", [
|
||||
[{"type": "object"}, {"type": "string"}],
|
||||
[{"type": "string"}, {"type": "object"}],
|
||||
[{"type": "object"}, {}],
|
||||
[{"type": "object"}, True],
|
||||
])
|
||||
def test_nested_json_cast_preserves_unions_that_can_accept_strings(union, branches):
|
||||
registry = ToolRegistry()
|
||||
registry.register(_FakeTool("echo", {
|
||||
"type": "object", "properties": {"value": {union: branches}},
|
||||
}))
|
||||
original = {"value": '{"mode":"text"}'}
|
||||
_, params, error = registry.prepare_call("echo", original)
|
||||
assert error is None
|
||||
assert params == original
|
||||
|
||||
|
||||
@pytest.mark.parametrize("encoded", [True, False])
|
||||
def test_nested_json_cast_does_not_select_the_first_object_branch(encoded):
|
||||
registry = ToolRegistry()
|
||||
registry.register(_FakeTool("echo", {
|
||||
"type": "object", "properties": {"value": {"oneOf": [
|
||||
{"type": "object", "properties": {"count": {"type": "string"}}},
|
||||
{"type": "object", "properties": {"count": {"type": "integer"}}},
|
||||
]}},
|
||||
}))
|
||||
value = {"count": 12}
|
||||
_, params, error = registry.prepare_call("echo", {"value": json.dumps(value) if encoded else value})
|
||||
assert error is None
|
||||
assert params == {"value": value}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("schema, value, expected", [
|
||||
({"type": "object"}, '{"count":12}', {"count": 12}),
|
||||
({"type": "array"}, '[{"count":12}]', [{"count": 12}]),
|
||||
({"type": ["object", "null"]}, '{"count":12}', {"count": 12}),
|
||||
({"type": ["object", "array"]}, '[1,2]', [1, 2]),
|
||||
({"anyOf": [{"type": "object"}, {"type": "array"}]}, '[1,2]', [1, 2]),
|
||||
({"type": ["string", "object"]}, '{"count":12}', '{"count":12}'),
|
||||
({"type": ["object", "string"]}, '{"count":12}', '{"count":12}'),
|
||||
({"type": "object"}, '{count:12}', '{count:12}'),
|
||||
({"type": "object"}, '[1,2]', '[1,2]'),
|
||||
({"type": "array"}, '{"count":12}', '{"count":12}'),
|
||||
({"$ref": "#/$defs/options"}, '{"count":12}', '{"count":12}'),
|
||||
])
|
||||
def test_nested_container_decoding_is_lossless_and_schema_directed(schema, value, expected):
|
||||
registry = ToolRegistry()
|
||||
registry.register(_FakeTool("echo", {
|
||||
"type": "object", "properties": {"value": schema},
|
||||
"$defs": {"options": {"type": "object"}},
|
||||
}))
|
||||
original = {"value": value}
|
||||
schemas_before = json.dumps(registry.get_definitions())
|
||||
arguments_before = json.dumps(original)
|
||||
_, params, _ = registry.prepare_call("echo", original)
|
||||
assert params == {"value": expected}
|
||||
assert json.dumps(original) == arguments_before
|
||||
assert json.dumps(registry.get_definitions()) == schemas_before
|
||||
|
||||
|
||||
def test_prepare_call_rejects_scalar_for_single_required_parameter() -> None:
|
||||
registry = ToolRegistry()
|
||||
registry.register(_FakeTool(
|
||||
|
||||
Reference in new issue
Block a user