|
17 | 17 |
|
18 | 18 | from google.adk.agents.llm_agent import LlmAgent |
19 | 19 | from google.adk.events.event import Event |
| 20 | +from google.adk.events.event_actions import EventActions |
20 | 21 | from google.adk.flows.llm_flows import functions |
21 | 22 | from google.adk.flows.llm_flows.request_confirmation import request_processor |
22 | 23 | from google.adk.models.llm_request import LlmRequest |
| 24 | +from google.adk.tools.function_tool import FunctionTool |
23 | 25 | from google.adk.tools.tool_confirmation import ToolConfirmation |
24 | 26 | from google.genai import types |
25 | 27 | import pytest |
@@ -407,3 +409,141 @@ async def test_request_confirmation_processor_finds_user_confirmation_in_default |
407 | 409 |
|
408 | 410 | assert len(events) == 1 |
409 | 411 | assert events[0] == expected_event |
| 412 | + |
| 413 | + |
| 414 | +@pytest.mark.asyncio |
| 415 | +async def test_request_confirmation_processor_dynamic_success(): |
| 416 | + """Test successful processing of dynamic tool confirmation (require_confirmation=False).""" |
| 417 | + agent = LlmAgent( |
| 418 | + name="test_agent", |
| 419 | + tools=[FunctionTool(mock_tool, require_confirmation=False)], |
| 420 | + ) |
| 421 | + invocation_context = await testing_utils.create_invocation_context( |
| 422 | + agent=agent |
| 423 | + ) |
| 424 | + llm_request = LlmRequest() |
| 425 | + |
| 426 | + original_function_call = types.FunctionCall( |
| 427 | + name=MOCK_TOOL_NAME, args={"param1": "test"}, id=MOCK_FUNCTION_CALL_ID |
| 428 | + ) |
| 429 | + |
| 430 | + # 1. Event with the original tool call |
| 431 | + invocation_context.session.events.append( |
| 432 | + Event( |
| 433 | + author="agent", |
| 434 | + content=types.Content( |
| 435 | + parts=[types.Part(function_call=original_function_call)] |
| 436 | + ), |
| 437 | + ) |
| 438 | + ) |
| 439 | + |
| 440 | + # 2. Event with the tool's response requesting confirmation dynamically. |
| 441 | + # This event needs to have actions.requested_tool_confirmations. |
| 442 | + tool_confirmation_request = ToolConfirmation( |
| 443 | + confirmed=False, hint="dynamic hint" |
| 444 | + ) |
| 445 | + original_response_event = Event( |
| 446 | + author="user", |
| 447 | + content=types.Content( |
| 448 | + parts=[ |
| 449 | + types.Part( |
| 450 | + function_response=types.FunctionResponse( |
| 451 | + name=MOCK_TOOL_NAME, |
| 452 | + id=MOCK_FUNCTION_CALL_ID, |
| 453 | + response={"status": "waiting_for_confirm"}, |
| 454 | + ) |
| 455 | + ) |
| 456 | + ] |
| 457 | + ), |
| 458 | + actions=EventActions( |
| 459 | + requested_tool_confirmations={ |
| 460 | + MOCK_FUNCTION_CALL_ID: tool_confirmation_request |
| 461 | + } |
| 462 | + ), |
| 463 | + ) |
| 464 | + invocation_context.session.events.append(original_response_event) |
| 465 | + |
| 466 | + # 3. Confirmation request event from the agent to the client. |
| 467 | + tool_confirmation_args = { |
| 468 | + "originalFunctionCall": original_function_call.model_dump( |
| 469 | + exclude_none=True, by_alias=True |
| 470 | + ), |
| 471 | + "toolConfirmation": tool_confirmation_request.model_dump( |
| 472 | + by_alias=True, exclude_none=True |
| 473 | + ), |
| 474 | + } |
| 475 | + invocation_context.session.events.append( |
| 476 | + Event( |
| 477 | + author="agent", |
| 478 | + content=types.Content( |
| 479 | + parts=[ |
| 480 | + types.Part( |
| 481 | + function_call=types.FunctionCall( |
| 482 | + name=functions.REQUEST_CONFIRMATION_FUNCTION_CALL_NAME, |
| 483 | + args=tool_confirmation_args, |
| 484 | + id=MOCK_CONFIRMATION_FUNCTION_CALL_ID, |
| 485 | + ) |
| 486 | + ) |
| 487 | + ] |
| 488 | + ), |
| 489 | + ) |
| 490 | + ) |
| 491 | + |
| 492 | + # 4. Event with the user's confirmation response. |
| 493 | + user_confirmation = ToolConfirmation(confirmed=True) |
| 494 | + invocation_context.session.events.append( |
| 495 | + Event( |
| 496 | + author="user", |
| 497 | + content=types.Content( |
| 498 | + parts=[ |
| 499 | + types.Part( |
| 500 | + function_response=types.FunctionResponse( |
| 501 | + name=functions.REQUEST_CONFIRMATION_FUNCTION_CALL_NAME, |
| 502 | + id=MOCK_CONFIRMATION_FUNCTION_CALL_ID, |
| 503 | + response={ |
| 504 | + "response": user_confirmation.model_dump_json() |
| 505 | + }, |
| 506 | + ) |
| 507 | + ) |
| 508 | + ] |
| 509 | + ), |
| 510 | + ) |
| 511 | + ) |
| 512 | + |
| 513 | + expected_event = Event( |
| 514 | + author="agent", |
| 515 | + content=types.Content( |
| 516 | + parts=[ |
| 517 | + types.Part( |
| 518 | + function_response=types.FunctionResponse( |
| 519 | + name=MOCK_TOOL_NAME, |
| 520 | + id=MOCK_FUNCTION_CALL_ID, |
| 521 | + response={"result": "Mock tool result with test"}, |
| 522 | + ) |
| 523 | + ) |
| 524 | + ] |
| 525 | + ), |
| 526 | + ) |
| 527 | + |
| 528 | + with patch( |
| 529 | + "google.adk.flows.llm_flows.functions.handle_function_call_list_async" |
| 530 | + ) as mock_handle_function_call_list_async: |
| 531 | + mock_handle_function_call_list_async.return_value = expected_event |
| 532 | + |
| 533 | + events = [] |
| 534 | + async for event in request_processor.run_async( |
| 535 | + invocation_context, llm_request |
| 536 | + ): |
| 537 | + events.append(event) |
| 538 | + |
| 539 | + assert len(events) == 1 |
| 540 | + assert events[0] == expected_event |
| 541 | + |
| 542 | + mock_handle_function_call_list_async.assert_called_once() |
| 543 | + args, _ = mock_handle_function_call_list_async.call_args |
| 544 | + |
| 545 | + assert list(args[1]) == [original_function_call] # function_calls |
| 546 | + assert args[3] == {MOCK_FUNCTION_CALL_ID} # tools_to_confirm |
| 547 | + assert ( |
| 548 | + args[4][MOCK_FUNCTION_CALL_ID] == user_confirmation |
| 549 | + ) # tool_confirmation_dict |
0 commit comments