File size: 4,470 Bytes
ad51766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
"""Unit tests for core.chat conversation primitives."""

from core.chat import (
    build_api_kwargs,
    build_messages_for_api,
    finalize_response,
    flush_tool_results,
    init_state,
    record_tool_result,
)


def test_init_state_has_all_keys():
    state = init_state()
    assert state["messages"] == []
    assert state["context_start_index"] == 0
    assert state["pending_tool_calls"] == []
    assert state["pending_assistant_msg"] is None
    assert state["submitted_tool_results"] == []


def test_build_messages_for_api_prepends_system_prompt():
    state = init_state()
    state["messages"] = [{"role": "user", "content": "hi"}]
    out = build_messages_for_api(state, "be helpful")
    assert out[0] == {"role": "system", "content": "be helpful"}
    assert out[-1]["content"] == "hi"


def test_build_messages_for_api_skips_blank_system():
    state = init_state()
    state["messages"] = [{"role": "user", "content": "hi"}]
    assert build_messages_for_api(state, "   ") == state["messages"]


def test_build_messages_for_api_respects_context_start():
    state = init_state()
    state["messages"] = [
        {"role": "user", "content": "old"},
        {"role": "assistant", "content": "older reply"},
        {"role": "user", "content": "new"},
    ]
    state["context_start_index"] = 2
    out = build_messages_for_api(state, "")
    assert out == [{"role": "user", "content": "new"}]


def test_build_api_kwargs_defaults():
    state = init_state()
    state["messages"] = [{"role": "user", "content": "hi"}]
    kwargs = build_api_kwargs(
        state,
        system_prompt="",
        functions_json_str=None,
        think_level="low",
        temperature=0.9,
        max_tokens=512,
        top_p=0.95,
    )
    assert kwargs["stream"] is True
    assert kwargs["max_tokens"] == 512
    assert kwargs["reasoning_effort"] == "low"
    assert kwargs["temperature"] == 0.7
    assert kwargs["top_p"] == 0.95
    assert "extra_body" not in kwargs
    assert "tools" not in kwargs


def test_build_api_kwargs_omits_unset_sampling_knobs():
    state = init_state()
    kwargs = build_api_kwargs(
        state,
        system_prompt="",
        functions_json_str=None,
        think_level="high",
        temperature=None,
        max_tokens=0,
        top_p=0,
    )
    assert "max_tokens" not in kwargs
    assert "temperature" not in kwargs
    assert "top_p" not in kwargs
    assert "extra_body" not in kwargs


def test_build_api_kwargs_sends_explicit_zero_temperature():
    """``temperature=0`` is greedy decoding and must reach the wire — only
    ``temperature=None`` should be treated as "use the server default"."""
    state = init_state()
    kwargs = build_api_kwargs(
        state,
        system_prompt="",
        functions_json_str=None,
        think_level="high",
        temperature=0,
        max_tokens=0,
        top_p=0,
    )
    assert kwargs["temperature"] == 0.0


def test_finalize_response_without_tool_calls():
    state = init_state()
    has_tools, pending = finalize_response(
        state=state,
        assistant_content="hello",
        reasoning_content="",
        tool_calls_acc=[],
    )
    assert has_tools is False
    assert pending == []
    assert state["messages"] == [{"role": "assistant", "content": "hello"}]


def test_finalize_response_with_tool_calls_records_pending():
    state = init_state()
    tc = {
        "id": "call_1",
        "type": "function",
        "function": {"name": "ping", "arguments": "{}"},
    }
    has_tools, pending = finalize_response(
        state=state,
        assistant_content="",
        reasoning_content="",
        tool_calls_acc=[tc],
    )
    assert has_tools is True
    assert pending == [tc]
    assert state["pending_tool_calls"] == [tc]
    assert state["pending_assistant_msg"]["tool_calls"] == [tc]


def test_record_tool_result_then_flush_appends_to_messages():
    state = init_state()
    tc = {"id": "call_1", "type": "function", "function": {"name": "ping", "arguments": "{}"}}
    record_tool_result(state, tc, "pong")
    assert state["submitted_tool_results"] == [{
        "role": "tool",
        "tool_call_id": "call_1",
        "content": "pong",
    }]
    flush_tool_results(state)
    assert state["submitted_tool_results"] == []
    assert state["messages"][-1] == {
        "role": "tool",
        "tool_call_id": "call_1",
        "content": "pong",
    }
    assert state["pending_assistant_msg"] is None