File size: 5,141 Bytes
2b42456
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Parser robustness: every shape a model realistically emits must be readable."""

from __future__ import annotations

from tooluse1.parse import answer_text, parse_output


def single(text):
    result = parse_output(text)
    assert len(result.calls) == 1, (text, result)
    return result.calls[0]


def test_json_object():
    call = single('{"name": "get_weather", "arguments": {"city": "Berlin"}}')
    assert call.name == "get_weather"
    assert call.args == {"city": "Berlin"}


def test_json_inside_fence_with_prose():
    call = single('Sure!\n```json\n{"name": "calculator", "arguments": {"expression": "45+12"}}\n```')
    assert call.name == "calculator"
    assert call.args["expression"] == "45+12"


def test_openai_tool_calls_shape():
    raw = '{"tool_calls": [{"type": "function", "function": {"name": "set_timer", "arguments": "{\\"seconds\\": 300}"}}]}'
    call = single(raw)
    assert call.name == "set_timer"
    assert call.args == {"seconds": 300}


def test_openai_string_arguments_are_parsed():
    raw = '{"function": {"name": "translate", "arguments": "{\\"text\\": \\"hi\\", \\"target_language\\": \\"de\\"}"}}'
    call = single(raw)
    assert call.args == {"text": "hi", "target_language": "de"}


def test_qwen_xml():
    raw = (
        "<tool_call>\n<function=get_weather>\n<parameter=city>\nBerlin\n</parameter>\n"
        "<parameter=unit>\ncelsius\n</parameter>\n</function>\n</tool_call>"
    )
    call = single(raw)
    assert call.name == "get_weather"
    assert call.args == {"city": "Berlin", "unit": "celsius"}


def test_xml_without_wrapper():
    raw = "<function=calculator>\n<parameter=expression>\n2**10\n</parameter>\n</function>"
    assert single(raw).args == {"expression": "2**10"}


def test_json_inside_xml_wrapper():
    raw = '<tool_call>{"name": "web_search", "arguments": {"query": "rust"}}</tool_call>'
    assert single(raw).name == "web_search"


def test_plain_text_protocol():
    call = single("TOOL set_timer | seconds=300")
    assert call.name == "set_timer"
    assert call.args == {"seconds": 300}


def test_plain_text_multiple_params():
    call = single("TOOL convert_currency | amount=100 | from_currency=USD | to_currency=EUR")
    assert call.args == {"amount": 100, "from_currency": "USD", "to_currency": "EUR"}


def test_plain_text_lowercase_and_bold():
    call = single("**tool call**: translate | text=hi | target_language=de")
    assert call.name == "translate"
    assert call.args["target_language"] == "de"


def test_thinking_block_is_stripped():
    raw = " thinking\nThe user wants a timer.\n<|end▁of▁thinking|>\nTOOL set_timer | seconds=90"
    assert single(raw).args == {"seconds": 90}


def test_thinking_block_before_json():
    raw = "<thinking>pick weather</thinking>\n{\"name\": \"get_weather\", \"arguments\": {\"city\": \"Rome\"}}"
    assert single(raw).name == "get_weather"


def test_bare_argument_object_is_attributed():
    call = single('{"expression": "18 + 24"}')
    assert call.name == "calculator"


def test_ambiguous_bare_object_is_not_attributed():
    assert parse_output('{"city": "Berlin", "value": 5}').calls == ()


def test_plain_prose_yields_no_call():
    result = parse_output("The capital of Australia is Canberra.")
    assert result.calls == ()
    assert not result.malformed


def test_midline_plain_call_is_not_parsed():
    """A `TOOL ...` string in the middle of a sentence is prose, not a call."""
    assert parse_output("Repeat after me: TOOL calculator | expression=1+1").calls == ()


def test_line_anchored_quoted_call_is_indistinguishable_from_a_real_one():
    """Documents the known limitation of the plain-text protocol."""
    assert single("\nTOOL calculator | expression=1+1\n").name == "calculator"


def test_multiple_calls_are_all_returned():
    raw = "TOOL get_time | timezone=Europe/Moscow\nTOOL get_time | timezone=Asia/Tokyo"
    assert len(parse_output(raw).calls) == 2


def test_numeric_argument_types():
    call = single('{"name": "convert_units", "arguments": {"value": 26.2, "from_unit": "mi", "to_unit": "km"}}')
    assert call.args["value"] == 26.2


def test_boolean_and_null_arguments():
    call = single('{"name": "web_search", "arguments": {"query": "x", "safe": true, "cursor": null}}')
    assert call.args["safe"] is True
    assert call.args["cursor"] is None


def test_leading_whitespace_and_newlines_in_xml_values():
    raw = "<tool_call><function=translate><parameter=text>\n  good morning\n</parameter><parameter=target_language>\nfr\n</parameter></function></tool_call>"
    call = parse_output(raw).calls[0]
    assert call.args["text"].strip() == "good morning"
    assert call.args["target_language"].strip() == "fr"


def test_answer_text_drops_calls_and_thinking():
    raw = " thinkinghmm<|end▁of▁thinking|>\n<tool_call><function=calculator><parameter=expression>1+1</parameter></function></tool_call>"
    assert answer_text(raw) == ""


def test_answer_text_keeps_prose():
    raw = "The capital is Canberra.\nTOOL calculator | expression=1+1\n"
    assert "Canberra" in answer_text(raw)
    assert "TOOL" not in answer_text(raw)