|
13 | 13 | from openhands.sdk.llm import LLM |
14 | 14 | from openhands.sdk.mcp.client import MCPClient |
15 | 15 | from openhands.sdk.mcp.tool import MCPToolDefinition |
| 16 | +from openhands.sdk.tool import FinishTool |
16 | 17 | from openhands.sdk.tool.tool import ToolDefinition |
17 | 18 | from openhands.sdk.utils.models import OpenHandsModel |
18 | 19 |
|
@@ -221,3 +222,140 @@ class TestModel(BaseModel): |
221 | 222 | assert isinstance(deserialized_model.agent, Agent) |
222 | 223 | assert deserialized_model.agent.model_dump() == agent.model_dump() |
223 | 224 | assert deserialized_model.model_dump() == model.model_dump() |
| 225 | + |
| 226 | + |
| 227 | +def test_include_default_tools_serialization_default() -> None: |
| 228 | + """Test that include_default_tools serializes correctly with default value.""" |
| 229 | + llm = LLM(model="test-model", usage_id="test-llm") |
| 230 | + agent = Agent(llm=llm, tools=[]) |
| 231 | + |
| 232 | + # Serialize to JSON |
| 233 | + agent_json = agent.model_dump_json() |
| 234 | + agent_dict = json.loads(agent_json) |
| 235 | + |
| 236 | + # Default should include both FinishTool and ThinkTool as strings |
| 237 | + assert "include_default_tools" in agent_dict |
| 238 | + assert set(agent_dict["include_default_tools"]) == {"FinishTool", "ThinkTool"} |
| 239 | + |
| 240 | + |
| 241 | +def test_include_default_tools_serialization_empty() -> None: |
| 242 | + """Test that include_default_tools serializes correctly when empty.""" |
| 243 | + llm = LLM(model="test-model", usage_id="test-llm") |
| 244 | + agent = Agent(llm=llm, tools=[], include_default_tools=[]) |
| 245 | + |
| 246 | + # Serialize to JSON |
| 247 | + agent_json = agent.model_dump_json() |
| 248 | + agent_dict = json.loads(agent_json) |
| 249 | + |
| 250 | + # Should be empty list |
| 251 | + assert agent_dict["include_default_tools"] == [] |
| 252 | + |
| 253 | + |
| 254 | +def test_include_default_tools_serialization_partial() -> None: |
| 255 | + """Test that include_default_tools serializes correctly with partial list.""" |
| 256 | + llm = LLM(model="test-model", usage_id="test-llm") |
| 257 | + agent = Agent(llm=llm, tools=[], include_default_tools=["FinishTool"]) |
| 258 | + |
| 259 | + # Serialize to JSON |
| 260 | + agent_json = agent.model_dump_json() |
| 261 | + agent_dict = json.loads(agent_json) |
| 262 | + |
| 263 | + # Should be serialized as string |
| 264 | + assert agent_dict["include_default_tools"] == ["FinishTool"] |
| 265 | + |
| 266 | + |
| 267 | +def test_include_default_tools_deserialization_roundtrip() -> None: |
| 268 | + """Test that include_default_tools deserializes correctly after round-trip.""" |
| 269 | + llm = LLM(model="test-model", usage_id="test-llm") |
| 270 | + agent = Agent(llm=llm, tools=[], include_default_tools=["FinishTool"]) |
| 271 | + |
| 272 | + # Serialize to JSON |
| 273 | + agent_json = agent.model_dump_json() |
| 274 | + |
| 275 | + # Deserialize from JSON |
| 276 | + deserialized_agent = AgentBase.model_validate_json(agent_json) |
| 277 | + |
| 278 | + # Should have the same include_default_tools |
| 279 | + assert isinstance(deserialized_agent, Agent) |
| 280 | + assert deserialized_agent.include_default_tools == ["FinishTool"] |
| 281 | + |
| 282 | + |
| 283 | +def test_include_default_tools_deserialization_all_tools() -> None: |
| 284 | + """Test that include_default_tools deserializes correctly with all tools.""" |
| 285 | + llm = LLM(model="test-model", usage_id="test-llm") |
| 286 | + agent = Agent(llm=llm, tools=[], include_default_tools=["FinishTool", "ThinkTool"]) |
| 287 | + |
| 288 | + # Serialize to JSON |
| 289 | + agent_json = agent.model_dump_json() |
| 290 | + |
| 291 | + # Deserialize from JSON |
| 292 | + deserialized_agent = AgentBase.model_validate_json(agent_json) |
| 293 | + |
| 294 | + # Should have both tools |
| 295 | + assert isinstance(deserialized_agent, Agent) |
| 296 | + assert set(deserialized_agent.include_default_tools) == {"FinishTool", "ThinkTool"} |
| 297 | + |
| 298 | + |
| 299 | +def test_include_default_tools_deserialization_empty() -> None: |
| 300 | + """Test that include_default_tools deserializes correctly when empty.""" |
| 301 | + llm = LLM(model="test-model", usage_id="test-llm") |
| 302 | + agent = Agent(llm=llm, tools=[], include_default_tools=[]) |
| 303 | + |
| 304 | + # Serialize to JSON |
| 305 | + agent_json = agent.model_dump_json() |
| 306 | + |
| 307 | + # Deserialize from JSON |
| 308 | + deserialized_agent = AgentBase.model_validate_json(agent_json) |
| 309 | + |
| 310 | + # Should be empty |
| 311 | + assert isinstance(deserialized_agent, Agent) |
| 312 | + assert deserialized_agent.include_default_tools == [] |
| 313 | + |
| 314 | + |
| 315 | +def test_include_default_tools_deserialization_from_dict() -> None: |
| 316 | + """Test that include_default_tools deserializes correctly from dict.""" |
| 317 | + agent_dict = { |
| 318 | + "llm": {"model": "test-model", "usage_id": "test-llm"}, |
| 319 | + "tools": [], |
| 320 | + "include_default_tools": ["ThinkTool"], |
| 321 | + "kind": "Agent", |
| 322 | + } |
| 323 | + |
| 324 | + # Deserialize from dict |
| 325 | + agent = AgentBase.model_validate(agent_dict) |
| 326 | + |
| 327 | + # Should have ThinkTool |
| 328 | + assert isinstance(agent, Agent) |
| 329 | + assert agent.include_default_tools == ["ThinkTool"] |
| 330 | + |
| 331 | + |
| 332 | +def test_include_default_tools_invalid_tool_name() -> None: |
| 333 | + """Test that include_default_tools raises error for invalid tool name.""" |
| 334 | + agent_dict = { |
| 335 | + "llm": {"model": "test-model", "usage_id": "test-llm"}, |
| 336 | + "tools": [], |
| 337 | + "include_default_tools": ["InvalidTool"], |
| 338 | + "kind": "Agent", |
| 339 | + } |
| 340 | + |
| 341 | + # Should raise ValueError |
| 342 | + with pytest.raises(ValueError, match="Unknown built-in tool class: 'InvalidTool'"): |
| 343 | + AgentBase.model_validate(agent_dict) |
| 344 | + |
| 345 | + |
| 346 | +def test_include_default_tools_accepts_tool_class_via_validator() -> None: |
| 347 | + """Test that include_default_tools validator converts tool classes to strings.""" |
| 348 | + # The validator accepts tool classes and converts them to strings |
| 349 | + # This is tested via model_validate to bypass type checking |
| 350 | + agent_dict = { |
| 351 | + "llm": {"model": "test-model", "usage_id": "test-llm"}, |
| 352 | + "tools": [], |
| 353 | + "include_default_tools": [FinishTool], # Pass class, not string |
| 354 | + "kind": "Agent", |
| 355 | + } |
| 356 | + |
| 357 | + agent = AgentBase.model_validate(agent_dict) |
| 358 | + |
| 359 | + # Should be converted to string |
| 360 | + assert isinstance(agent, Agent) |
| 361 | + assert agent.include_default_tools == ["FinishTool"] |
0 commit comments