File size: 9,389 Bytes
59b2038 d408a51 59b2038 754345f 59b2038 d408a51 6155b26 d408a51 6155b26 d408a51 6155b26 d408a51 754345f d408a51 6155b26 d408a51 6155b26 d408a51 59b2038 754345f 59b2038 | 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | from types import SimpleNamespace
import pytest
from litellm import ChatCompletionMessageToolCall, Message
from agent.core import agent_loop
from agent.core.agent_loop import (
LLMResult,
_call_llm_streaming,
_assistant_message_from_result,
_extract_thinking_state,
)
def test_extract_thinking_state_from_litellm_message():
message = Message(
role="assistant",
content="working",
thinking_blocks=[{"type": "thinking", "thinking": "reasoned"}],
reasoning_content="reasoned",
)
thinking_blocks, reasoning_content = _extract_thinking_state(message)
assert thinking_blocks == [{"type": "thinking", "thinking": "reasoned"}]
assert reasoning_content == "reasoned"
def test_extract_thinking_state_from_provider_fields():
message = SimpleNamespace(
provider_specific_fields={
"thinking_blocks": [{"type": "thinking", "thinking": "reasoned"}],
"reasoning_content": "reasoned",
},
)
thinking_blocks, reasoning_content = _extract_thinking_state(message)
assert thinking_blocks == [{"type": "thinking", "thinking": "reasoned"}]
assert reasoning_content == "reasoned"
def test_assistant_message_from_result_preserves_thinking_with_tool_calls():
tool_call = ChatCompletionMessageToolCall(
id="call_1",
type="function",
function={"name": "bash", "arguments": '{"command": "date"}'},
)
result = LLMResult(
content=None,
tool_calls_acc={},
token_count=12,
finish_reason="tool_calls",
thinking_blocks=[{"type": "thinking", "thinking": "reasoned"}],
reasoning_content="reasoned",
)
message = _assistant_message_from_result(
result,
model_name="anthropic/claude-opus-4-6",
tool_calls=[tool_call],
)
assert message.tool_calls == [tool_call]
assert message.thinking_blocks == [{"type": "thinking", "thinking": "reasoned"}]
assert message.reasoning_content == "reasoned"
def test_assistant_message_from_result_strips_non_anthropic_reasoning_content():
result = LLMResult(
content=None,
tool_calls_acc={},
token_count=12,
finish_reason="tool_calls",
thinking_blocks=[{"type": "thinking", "thinking": "reasoned"}],
reasoning_content="reasoned",
)
message = _assistant_message_from_result(
result,
model_name="openai/Qwen/Qwen3-Next-80B-A3B-Instruct",
)
assert getattr(message, "thinking_blocks", None) is None
assert getattr(message, "reasoning_content", None) is None
def test_assistant_message_from_result_omits_absent_thinking_fields():
result = LLMResult(
content="done",
tool_calls_acc={},
token_count=12,
finish_reason="stop",
)
message = _assistant_message_from_result(
result,
model_name="anthropic/claude-opus-4-6",
)
assert message.content == "done"
assert getattr(message, "thinking_blocks", None) is None
assert getattr(message, "reasoning_content", None) is None
@pytest.mark.asyncio
async def test_streaming_call_rebuilds_anthropic_thinking_state(monkeypatch):
async def fake_stream():
yield SimpleNamespace(
choices=[
SimpleNamespace(
delta=SimpleNamespace(content="done", tool_calls=None),
finish_reason="stop",
)
],
)
yield SimpleNamespace(choices=[], usage=SimpleNamespace(total_tokens=3))
async def fake_acompletion(**_kwargs):
return fake_stream()
def fake_chunk_builder(chunks, **_kwargs):
assert len(chunks) == 2
return SimpleNamespace(
choices=[
SimpleNamespace(
message=Message(
role="assistant",
content="done",
thinking_blocks=[{"type": "thinking", "thinking": "reasoned"}],
reasoning_content="reasoned",
)
)
]
)
events = []
async def send_event(event):
events.append(event)
session = SimpleNamespace(
config=SimpleNamespace(model_name="anthropic/claude-opus-4-6"),
is_cancelled=False,
send_event=send_event,
)
monkeypatch.setattr(agent_loop, "acompletion", fake_acompletion)
monkeypatch.setattr(agent_loop, "stream_chunk_builder", fake_chunk_builder)
result = await _call_llm_streaming(
session,
messages=[Message(role="user", content="hi")],
tools=[],
llm_params={"model": "anthropic/claude-opus-4-6"},
)
assert result.content == "done"
assert result.thinking_blocks == [{"type": "thinking", "thinking": "reasoned"}]
assert result.reasoning_content == "reasoned"
@pytest.mark.asyncio
async def test_streaming_call_rebuilds_anthropic_delta_thinking_state(monkeypatch):
async def fake_stream():
yield SimpleNamespace(
choices=[
SimpleNamespace(
delta=SimpleNamespace(
content=None,
tool_calls=None,
thinking_blocks=[
{
"type": "thinking",
"thinking": "reasoned",
"signature": "",
}
],
),
finish_reason=None,
)
],
)
yield SimpleNamespace(
choices=[
SimpleNamespace(
delta=SimpleNamespace(
content=None,
tool_calls=None,
thinking_blocks=[
{
"type": "thinking",
"thinking": "",
"signature": "signed",
}
],
),
finish_reason=None,
)
],
)
yield SimpleNamespace(
choices=[
SimpleNamespace(
delta=SimpleNamespace(content="done", tool_calls=None),
finish_reason="stop",
)
],
)
yield SimpleNamespace(choices=[], usage=SimpleNamespace(total_tokens=3))
async def fake_acompletion(**_kwargs):
return fake_stream()
def fake_chunk_builder(chunks, **_kwargs):
assert len(chunks) == 4
return SimpleNamespace(
choices=[
SimpleNamespace(
message=Message(
role="assistant",
content="done",
thinking_blocks=[
{
"type": "thinking",
"thinking": "reasoned",
"signature": "signed",
}
],
reasoning_content="reasoned",
)
)
]
)
events = []
async def send_event(event):
events.append(event)
session = SimpleNamespace(
config=SimpleNamespace(model_name="anthropic/claude-opus-4-7"),
is_cancelled=False,
send_event=send_event,
)
monkeypatch.setattr(agent_loop, "acompletion", fake_acompletion)
monkeypatch.setattr(agent_loop, "stream_chunk_builder", fake_chunk_builder)
result = await _call_llm_streaming(
session,
messages=[Message(role="user", content="hi")],
tools=[],
llm_params={"model": "anthropic/claude-opus-4-7"},
)
assert result.content == "done"
assert result.thinking_blocks == [
{"type": "thinking", "thinking": "reasoned", "signature": "signed"}
]
assert result.reasoning_content == "reasoned"
@pytest.mark.asyncio
async def test_streaming_call_skips_chunk_rebuild_for_non_anthropic(monkeypatch):
async def fake_stream():
yield SimpleNamespace(
choices=[
SimpleNamespace(
delta=SimpleNamespace(content="done", tool_calls=None),
finish_reason="stop",
)
],
)
async def fake_acompletion(**_kwargs):
return fake_stream()
def fail_chunk_builder(*_args, **_kwargs):
raise AssertionError("stream_chunk_builder should not run")
events = []
async def send_event(event):
events.append(event)
session = SimpleNamespace(
config=SimpleNamespace(model_name="openai/Qwen/Qwen3"),
is_cancelled=False,
send_event=send_event,
)
monkeypatch.setattr(agent_loop, "acompletion", fake_acompletion)
monkeypatch.setattr(agent_loop, "stream_chunk_builder", fail_chunk_builder)
result = await _call_llm_streaming(
session,
messages=[Message(role="user", content="hi")],
tools=[],
llm_params={"model": "openai/Qwen/Qwen3"},
)
assert result.content == "done"
assert result.thinking_blocks is None
assert result.reasoning_content is None
|