-
Notifications
You must be signed in to change notification settings - Fork 120
refactor: extract shared SSE event handling into SPI module #624
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
Merged
Merged
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,4 +52,5 @@ nbactions.xml | |
| # Private Claude config | ||
| .claude/ | ||
| .serena/ | ||
| .bob/ | ||
| claudedocs | ||
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
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
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
87 changes: 0 additions & 87 deletions
87
...t/transport/rest/src/main/java/io/a2a/client/transport/rest/sse/RestSSEEventListener.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
client/transport/rest/src/main/java/io/a2a/client/transport/rest/sse/SSEEventListener.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,73 @@ | ||
| package io.a2a.client.transport.rest.sse; | ||
|
|
||
| import java.util.concurrent.Future; | ||
| import java.util.function.Consumer; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import com.google.protobuf.util.JsonFormat; | ||
| import io.a2a.client.transport.spi.sse.AbstractSSEEventListener; | ||
| import io.a2a.client.transport.rest.RestErrorMapper; | ||
| import io.a2a.grpc.StreamResponse; | ||
| import io.a2a.grpc.utils.ProtoUtils; | ||
| import io.a2a.spec.StreamingEventKind; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * REST transport implementation of SSE event listener. | ||
| * Handles parsing of JSON-formatted protobuf messages from REST SSE streams. | ||
| */ | ||
| public class SSEEventListener extends AbstractSSEEventListener { | ||
|
|
||
| private static final Logger log = Logger.getLogger(SSEEventListener.class.getName()); | ||
|
|
||
| public SSEEventListener(Consumer<StreamingEventKind> eventHandler, | ||
| @Nullable Consumer<Throwable> errorHandler) { | ||
| super(eventHandler, errorHandler); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessage(String message, @Nullable Future<Void> completableFuture) { | ||
| try { | ||
| log.fine("Streaming message received: " + message); | ||
| io.a2a.grpc.StreamResponse.Builder builder = io.a2a.grpc.StreamResponse.newBuilder(); | ||
| JsonFormat.parser().merge(message, builder); | ||
| parseAndHandleMessage(builder.build(), completableFuture); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| if (getErrorHandler() != null) { | ||
| getErrorHandler().accept(RestErrorMapper.mapRestError(message, 500)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parses a StreamResponse protobuf message and delegates to the base class for event handling. | ||
| * | ||
| * @param response The parsed StreamResponse | ||
| * @param future Optional future for controlling the SSE connection | ||
| */ | ||
| private void parseAndHandleMessage(StreamResponse response, @Nullable Future<Void> future) { | ||
| StreamingEventKind event; | ||
| switch (response.getPayloadCase()) { | ||
| case MESSAGE -> | ||
| event = ProtoUtils.FromProto.message(response.getMessage()); | ||
| case TASK -> | ||
| event = ProtoUtils.FromProto.task(response.getTask()); | ||
| case STATUS_UPDATE -> | ||
| event = ProtoUtils.FromProto.taskStatusUpdateEvent(response.getStatusUpdate()); | ||
| case ARTIFACT_UPDATE -> | ||
| event = ProtoUtils.FromProto.taskArtifactUpdateEvent(response.getArtifactUpdate()); | ||
| default -> { | ||
| log.warning("Invalid stream response " + response.getPayloadCase()); | ||
| if (getErrorHandler() != null) { | ||
| getErrorHandler().accept(new IllegalStateException("Invalid stream response from server: " + response.getPayloadCase())); | ||
| } | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Delegate to base class for common event handling and auto-close logic | ||
| handleEvent(event, future); | ||
| } | ||
|
|
||
| } | ||
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.