|
29 | 29 | import com.google.adk.agents.RunConfig; |
30 | 30 | import com.google.adk.agents.SequentialAgent; |
31 | 31 | import com.google.adk.events.Event; |
| 32 | +import com.google.adk.models.LlmRequest; |
32 | 33 | import com.google.adk.runner.InMemoryRunner; |
33 | 34 | import com.google.adk.runner.Runner; |
34 | 35 | import com.google.adk.sessions.Session; |
@@ -411,6 +412,85 @@ public void testAutoToLoop() { |
411 | 412 | assertThat(simplifyEvents(actualEvents)).containsExactly("root_agent: response5"); |
412 | 413 | } |
413 | 414 |
|
| 415 | + @Test |
| 416 | + public void testLegacyTransferToAgent() { |
| 417 | + Content transferCallContent = |
| 418 | + Content.fromParts( |
| 419 | + Part.fromFunctionCall("transferToAgent", ImmutableMap.of("agentName", "sub_agent_1"))); |
| 420 | + Content response1 = Content.fromParts(Part.fromText("response1")); |
| 421 | + Content response2 = Content.fromParts(Part.fromText("response2")); |
| 422 | + |
| 423 | + TestLlm testLlm = |
| 424 | + createTestLlm( |
| 425 | + Flowable.just(createLlmResponse(transferCallContent)), |
| 426 | + Flowable.just(createLlmResponse(response1)), |
| 427 | + Flowable.just(createLlmResponse(response2))); |
| 428 | + |
| 429 | + LlmAgent subAgent1 = createTestAgentBuilder(testLlm).name("sub_agent_1").build(); |
| 430 | + LlmAgent rootAgent = |
| 431 | + createTestAgentBuilder(testLlm) |
| 432 | + .name("root_agent") |
| 433 | + .subAgents(ImmutableList.of(subAgent1)) |
| 434 | + .build(); |
| 435 | + InvocationContext invocationContext = createInvocationContext(rootAgent); |
| 436 | + |
| 437 | + Runner runner = getRunnerAndCreateSession(rootAgent, invocationContext.session()); |
| 438 | + List<Event> actualEvents = runRunner(runner, invocationContext); |
| 439 | + |
| 440 | + assertThat(simplifyEvents(actualEvents)) |
| 441 | + .containsExactly( |
| 442 | + "root_agent: FunctionCall(name=transferToAgent, args={agentName=sub_agent_1})", |
| 443 | + "root_agent: FunctionResponse(name=transferToAgent, response={})", |
| 444 | + "sub_agent_1: response1") |
| 445 | + .inOrder(); |
| 446 | + |
| 447 | + actualEvents = runRunner(runner, invocationContext); |
| 448 | + |
| 449 | + assertThat(simplifyEvents(actualEvents)).containsExactly("sub_agent_1: response2"); |
| 450 | + } |
| 451 | + |
| 452 | + @Test |
| 453 | + public void testAgentTransferDoesNotExposeLegacyTransferToAgent() { |
| 454 | + Content transferCallContent = |
| 455 | + Content.fromParts( |
| 456 | + Part.fromFunctionCall("transferToAgent", ImmutableMap.of("agentName", "sub_agent_1"))); |
| 457 | + Content response1 = Content.fromParts(Part.fromText("response1")); |
| 458 | + Content response2 = Content.fromParts(Part.fromText("response2")); |
| 459 | + TestLlm testLlm = |
| 460 | + createTestLlm( |
| 461 | + Flowable.just(createLlmResponse(transferCallContent)), |
| 462 | + Flowable.just(createLlmResponse(response1)), |
| 463 | + Flowable.just(createLlmResponse(response2))); |
| 464 | + LlmAgent subAgent1 = createTestAgentBuilder(testLlm).name("sub_agent_1").build(); |
| 465 | + LlmAgent rootAgent = |
| 466 | + createTestAgentBuilder(testLlm) |
| 467 | + .name("root_agent") |
| 468 | + .subAgents(ImmutableList.of(subAgent1)) |
| 469 | + .build(); |
| 470 | + InvocationContext invocationContext = createInvocationContext(rootAgent); |
| 471 | + AgentTransfer processor = new AgentTransfer(); |
| 472 | + LlmRequest request = LlmRequest.builder().build(); |
| 473 | + |
| 474 | + var processed = processor.processRequest(invocationContext, request); |
| 475 | + |
| 476 | + assertThat(processed.blockingGet().updatedRequest().config().get().tools()).isPresent(); |
| 477 | + assertThat(processed.blockingGet().updatedRequest().config().get().tools().get()).hasSize(1); |
| 478 | + assertThat( |
| 479 | + processed |
| 480 | + .blockingGet() |
| 481 | + .updatedRequest() |
| 482 | + .config() |
| 483 | + .get() |
| 484 | + .tools() |
| 485 | + .get() |
| 486 | + .get(0) |
| 487 | + .functionDeclarations() |
| 488 | + .get() |
| 489 | + .get(0) |
| 490 | + .name()) |
| 491 | + .hasValue("transfer_to_agent"); |
| 492 | + } |
| 493 | + |
414 | 494 | private Runner getRunnerAndCreateSession(LlmAgent agent, Session session) { |
415 | 495 | Runner runner = new InMemoryRunner(agent, session.appName()); |
416 | 496 | // Ensure the session exists before running the agent. |
|
0 commit comments