-
Notifications
You must be signed in to change notification settings - Fork 257
fix : enable service injection in A2ASendMessageExecutor for A2A runner #704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prasadskarmarkar
wants to merge
1
commit into
google:main
Choose a base branch
from
prasadskarmarkar:feat/a2a-configurable-services
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
a2a/src/test/java/com/google/adk/a2a/A2ASendMessageExecutorAdvancedTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| package com.google.adk.a2a; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import com.google.adk.agents.BaseAgent; | ||
| import com.google.adk.agents.InvocationContext; | ||
| import com.google.adk.artifacts.InMemoryArtifactService; | ||
| import com.google.adk.events.Event; | ||
| import com.google.adk.memory.InMemoryMemoryService; | ||
| import com.google.adk.sessions.InMemorySessionService; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.genai.types.Content; | ||
| import com.google.genai.types.Part; | ||
| import io.a2a.spec.Message; | ||
| import io.a2a.spec.TextPart; | ||
| import io.reactivex.rxjava3.core.Flowable; | ||
| import io.reactivex.rxjava3.core.Single; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class A2ASendMessageExecutorAdvancedTest { | ||
|
|
||
| private InMemorySessionService sessionService; | ||
|
|
||
| @Test | ||
| public void execute_withCustomStrategy_usesStrategy() { | ||
| InMemorySessionService sessionService = new InMemorySessionService(); | ||
|
|
||
| A2ASendMessageExecutor executor = new A2ASendMessageExecutor(sessionService, "test-app"); | ||
|
|
||
| A2ASendMessageExecutor.AgentExecutionStrategy customStrategy = | ||
| (userId, sessionId, userContent, runConfig, invocationId) -> { | ||
| Event customEvent = | ||
| Event.builder() | ||
| .id(UUID.randomUUID().toString()) | ||
| .invocationId(invocationId) | ||
| .author("agent") | ||
| .content( | ||
| Content.builder() | ||
| .role("model") | ||
| .parts( | ||
| ImmutableList.of( | ||
| Part.builder().text("Custom strategy response").build())) | ||
| .build()) | ||
| .build(); | ||
| return Single.just(ImmutableList.of(customEvent)); | ||
| }; | ||
|
|
||
| Message request = | ||
| new Message.Builder() | ||
| .messageId("msg-1") | ||
| .contextId("ctx-1") | ||
| .role(Message.Role.USER) | ||
| .parts(List.of(new TextPart("Test"))) | ||
| .build(); | ||
|
|
||
| Message response = executor.execute(request, customStrategy).blockingGet(); | ||
|
|
||
| assertThat(response).isNotNull(); | ||
| assertThat(response.getParts()).isNotEmpty(); | ||
| assertThat(((TextPart) response.getParts().get(0)).getText()) | ||
| .contains("Custom strategy response"); | ||
| } | ||
|
|
||
| private A2ASendMessageExecutor createExecutorWithAgent() { | ||
| BaseAgent agent = createSimpleAgent(); | ||
| sessionService = new InMemorySessionService(); | ||
| return new A2ASendMessageExecutor( | ||
| agent, | ||
| "test-app", | ||
| Duration.ofSeconds(30), | ||
| sessionService, | ||
| new InMemoryArtifactService(), | ||
| new InMemoryMemoryService()); | ||
| } | ||
|
|
||
| @Test | ||
| public void execute_withNullMessage_generatesDefaultContext() { | ||
| A2ASendMessageExecutor executor = createExecutorWithAgent(); | ||
|
|
||
| Message response = executor.execute(null).blockingGet(); | ||
|
|
||
| assertThat(response).isNotNull(); | ||
| assertThat(response.getContextId()).isNotNull(); | ||
| assertThat(response.getContextId()).isNotEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void execute_withEmptyContextId_generatesNewContext() { | ||
| A2ASendMessageExecutor executor = createExecutorWithAgent(); | ||
|
|
||
| Message request = | ||
| new Message.Builder() | ||
| .messageId("msg-1") | ||
| .role(Message.Role.USER) | ||
| .parts(List.of(new TextPart("Test"))) | ||
| .build(); | ||
|
|
||
| Message response = executor.execute(request).blockingGet(); | ||
|
|
||
| assertThat(response).isNotNull(); | ||
| assertThat(response.getContextId()).isNotNull(); | ||
| assertThat(response.getContextId()).isNotEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void execute_withProvidedContextId_preservesContext() { | ||
| A2ASendMessageExecutor executor = createExecutorWithAgent(); | ||
|
|
||
| String contextId = "my-custom-context"; | ||
| Message request = | ||
| new Message.Builder() | ||
| .messageId("msg-1") | ||
| .contextId(contextId) | ||
| .role(Message.Role.USER) | ||
| .parts(List.of(new TextPart("Test"))) | ||
| .build(); | ||
|
|
||
| Message response = executor.execute(request).blockingGet(); | ||
|
|
||
| assertThat(response).isNotNull(); | ||
| assertThat(response.getContextId()).isEqualTo(contextId); | ||
| } | ||
|
|
||
| @Test | ||
| public void execute_multipleRequests_maintainsSession() { | ||
| A2ASendMessageExecutor executor = createExecutorWithAgent(); | ||
|
|
||
| String contextId = "persistent-context"; | ||
|
|
||
| Message request1 = | ||
| new Message.Builder() | ||
| .messageId("msg-1") | ||
| .contextId(contextId) | ||
| .role(Message.Role.USER) | ||
| .parts(List.of(new TextPart("First message"))) | ||
| .build(); | ||
|
|
||
| Message response1 = executor.execute(request1).blockingGet(); | ||
| assertThat(response1.getContextId()).isEqualTo(contextId); | ||
|
|
||
| Message request2 = | ||
| new Message.Builder() | ||
| .messageId("msg-2") | ||
| .contextId(contextId) | ||
| .role(Message.Role.USER) | ||
| .parts(List.of(new TextPart("Second message"))) | ||
| .build(); | ||
|
|
||
| Message response2 = executor.execute(request2).blockingGet(); | ||
| assertThat(response2.getContextId()).isEqualTo(contextId); | ||
| } | ||
|
|
||
| @Test | ||
| public void execute_withoutRunnerConfig_throwsException() { | ||
| InMemorySessionService sessionService = new InMemorySessionService(); | ||
|
|
||
| A2ASendMessageExecutor executor = new A2ASendMessageExecutor(sessionService, "test-app"); | ||
|
|
||
| Message request = | ||
| new Message.Builder() | ||
| .messageId("msg-1") | ||
| .contextId("ctx-1") | ||
| .role(Message.Role.USER) | ||
| .parts(List.of(new TextPart("Test"))) | ||
| .build(); | ||
|
|
||
| try { | ||
| executor.execute(request).blockingGet(); | ||
| assertThat(false).isTrue(); | ||
| } catch (IllegalStateException e) { | ||
| assertThat(e.getMessage()).contains("Runner-based handle invoked without configured runner"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void execute_errorInStrategy_returnsErrorResponse() { | ||
| InMemorySessionService sessionService = new InMemorySessionService(); | ||
|
|
||
| A2ASendMessageExecutor executor = new A2ASendMessageExecutor(sessionService, "test-app"); | ||
|
|
||
| A2ASendMessageExecutor.AgentExecutionStrategy failingStrategy = | ||
| (userId, sessionId, userContent, runConfig, invocationId) -> { | ||
| return Single.error(new RuntimeException("Strategy failed")); | ||
| }; | ||
|
|
||
| Message request = | ||
| new Message.Builder() | ||
| .messageId("msg-1") | ||
| .contextId("ctx-1") | ||
| .role(Message.Role.USER) | ||
| .parts(List.of(new TextPart("Test"))) | ||
| .build(); | ||
|
|
||
| Message response = executor.execute(request, failingStrategy).blockingGet(); | ||
|
|
||
| assertThat(response).isNotNull(); | ||
| assertThat(response.getParts()).isNotEmpty(); | ||
| assertThat(((TextPart) response.getParts().get(0)).getText()).contains("Error:"); | ||
| assertThat(((TextPart) response.getParts().get(0)).getText()).contains("Strategy failed"); | ||
| } | ||
|
|
||
| private BaseAgent createSimpleAgent() { | ||
| return new BaseAgent("test", "test agent", ImmutableList.of(), null, null) { | ||
| @Override | ||
| protected Flowable<Event> runAsyncImpl(InvocationContext ctx) { | ||
| return Flowable.just( | ||
| Event.builder() | ||
| .content( | ||
| Content.builder() | ||
| .role("model") | ||
| .parts( | ||
| ImmutableList.of( | ||
| com.google.genai.types.Part.builder().text("Response").build())) | ||
| .build()) | ||
| .build()); | ||
| } | ||
|
|
||
| @Override | ||
| protected Flowable<Event> runLiveImpl(InvocationContext ctx) { | ||
| return Flowable.empty(); | ||
| } | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.