|
20 | 20 | from agent_framework._clients import BaseChatClient |
21 | 21 | from agent_framework._middleware import ChatMiddlewareLayer |
22 | 22 | from agent_framework._tools import FunctionInvocationLayer |
23 | | -from agent_framework.orchestrations import HandoffAgentUserRequest, HandoffBuilder |
| 23 | +from agent_framework.orchestrations import HandoffAgentUserRequest, HandoffBuilder, create_handoff_tools, get_handoff_tool_name |
24 | 24 |
|
25 | 25 |
|
26 | 26 | class MockChatClient(ChatMiddlewareLayer[Any], FunctionInvocationLayer[Any], BaseChatClient[Any]): |
@@ -364,3 +364,105 @@ def test_handoff_builder_accepts_all_instances_in_add_handoff(): |
364 | 364 | assert "triage" in workflow.executors |
365 | 365 | assert "specialist_a" in workflow.executors |
366 | 366 | assert "specialist_b" in workflow.executors |
| 367 | + |
| 368 | + |
| 369 | +def test_create_handoff_tools_creates_correct_tools(): |
| 370 | + """Test that create_handoff_tools creates tools with correct names and default descriptions.""" |
| 371 | + target_ids = ["specialist", "escalation"] |
| 372 | + tools = create_handoff_tools(target_ids) |
| 373 | + |
| 374 | + assert len(tools) == 2 |
| 375 | + assert tools[0].name == "handoff_to_specialist" |
| 376 | + assert tools[1].name == "handoff_to_escalation" |
| 377 | + assert tools[0].description == "Handoff to the specialist agent." |
| 378 | + assert tools[1].description == "Handoff to the escalation agent." |
| 379 | + |
| 380 | + |
| 381 | +def test_create_handoff_tools_with_custom_descriptions(): |
| 382 | + """Test that create_handoff_tools uses custom descriptions when provided.""" |
| 383 | + target_ids = ["billing", "tech"] |
| 384 | + descriptions = { |
| 385 | + "billing": "Transfer the customer to the billing department.", |
| 386 | + "tech": "Transfer the customer to technical support.", |
| 387 | + } |
| 388 | + tools = create_handoff_tools(target_ids, descriptions=descriptions) |
| 389 | + |
| 390 | + assert len(tools) == 2 |
| 391 | + assert tools[0].name == "handoff_to_billing" |
| 392 | + assert tools[1].name == "handoff_to_tech" |
| 393 | + assert tools[0].description == "Transfer the customer to the billing department." |
| 394 | + assert tools[1].description == "Transfer the customer to technical support." |
| 395 | + |
| 396 | + |
| 397 | +def test_create_handoff_tools_empty(): |
| 398 | + """Test that create_handoff_tools returns empty list for empty input.""" |
| 399 | + tools = create_handoff_tools([]) |
| 400 | + assert tools == [] |
| 401 | + |
| 402 | + |
| 403 | +async def test_pre_registered_tools_no_conflict(): |
| 404 | + """Test that pre-registered handoff tools do not raise ValueError when building a workflow. |
| 405 | +
|
| 406 | + This verifies the Azure AI Agent Service compatibility fix: when a user creates |
| 407 | + handoff tools upfront and attaches them to the agent, _apply_auto_tools should |
| 408 | + skip the duplicates instead of raising. |
| 409 | + """ |
| 410 | + pre_registered_tools = create_handoff_tools(["specialist"]) |
| 411 | + mock_client = MockChatClient(name="triage") |
| 412 | + agent = Agent( |
| 413 | + client=mock_client, |
| 414 | + name="triage", |
| 415 | + id="triage", |
| 416 | + default_options={"tools": pre_registered_tools}, # type: ignore |
| 417 | + ) |
| 418 | + |
| 419 | + specialist = MockHandoffAgent(name="specialist") |
| 420 | + # This should NOT raise ValueError for duplicate tool names |
| 421 | + workflow = ( |
| 422 | + HandoffBuilder(participants=[agent, specialist]) |
| 423 | + .with_start_agent(agent) |
| 424 | + .add_handoff(agent, [specialist]) |
| 425 | + .build() |
| 426 | + ) |
| 427 | + |
| 428 | + assert "triage" in workflow.executors |
| 429 | + assert "specialist" in workflow.executors |
| 430 | + |
| 431 | + |
| 432 | +def test_get_handoff_tool_name(): |
| 433 | + """Test that get_handoff_tool_name returns the expected format.""" |
| 434 | + assert get_handoff_tool_name("specialist") == "handoff_to_specialist" |
| 435 | + assert get_handoff_tool_name("billing_agent") == "handoff_to_billing_agent" |
| 436 | + |
| 437 | + |
| 438 | +async def test_mesh_topology_with_pre_registered_tools(): |
| 439 | + """Test that pre-registered tools work in a mesh topology where each agent can route to all others.""" |
| 440 | + # Create agents with pre-registered handoff tools (Azure AI Agent Service pattern) |
| 441 | + mock_client_a = MockChatClient(name="agent_a", handoff_to="agent_b") |
| 442 | + tools_a = create_handoff_tools(["agent_b", "agent_c"]) |
| 443 | + agent_a = Agent( |
| 444 | + client=mock_client_a, |
| 445 | + name="agent_a", |
| 446 | + id="agent_a", |
| 447 | + default_options={"tools": tools_a}, # type: ignore |
| 448 | + ) |
| 449 | + |
| 450 | + agent_b = MockHandoffAgent(name="agent_b") |
| 451 | + agent_c = MockHandoffAgent(name="agent_c") |
| 452 | + |
| 453 | + workflow = ( |
| 454 | + HandoffBuilder(participants=[agent_a, agent_b, agent_c]) |
| 455 | + .with_start_agent(agent_a) |
| 456 | + .build() |
| 457 | + ) |
| 458 | + |
| 459 | + assert "agent_a" in workflow.executors |
| 460 | + assert "agent_b" in workflow.executors |
| 461 | + assert "agent_c" in workflow.executors |
| 462 | + |
| 463 | + # Run the workflow to verify it works end-to-end |
| 464 | + events = await _drain(workflow.run("Test message", stream=True)) |
| 465 | + # agent_a should hand off to agent_b, which has no further handoff |
| 466 | + # so it should request user input |
| 467 | + requests = [ev for ev in events if ev.type == "request_info"] |
| 468 | + assert requests |
0 commit comments