77import java .util .logging .Logger ;
88
99import com .fasterxml .jackson .core .JsonProcessingException ;
10- import com .fasterxml .jackson .databind .JsonNode ;
1110import io .a2a .spec .JSONRPCError ;
11+ import io .a2a .spec .JSONRPCResponse ;
12+ import io .a2a .spec .SendStreamingMessageResponse ;
1213import io .a2a .spec .StreamingEventKind ;
1314import io .a2a .spec .TaskStatusUpdateEvent ;
1415
@@ -26,36 +27,34 @@ public SSEEventListener(Consumer<StreamingEventKind> eventHandler, Consumer<JSON
2627
2728 public void onMessage (String message , Future <Void > completableFuture ) {
2829 try {
29- handleMessage (OBJECT_MAPPER .readTree (message ),completableFuture );
30+ SendStreamingMessageResponse sendStreamingMessageResponse = OBJECT_MAPPER .readValue (message , SendStreamingMessageResponse .class );
31+ handleMessage (sendStreamingMessageResponse ,completableFuture );
3032 } catch (JsonProcessingException e ) {
3133 log .warning ("Failed to parse JSON message: " + message );
3234 }
3335 }
3436
37+ public void onMessage (JSONRPCResponse <?> response , Future <Void > completableFuture ) {
38+ handleMessage (response ,completableFuture );
39+ }
40+
3541 public void onError (Throwable throwable , Future <Void > future ) {
3642 failureHandler .run ();
3743 future .cancel (true ); // close SSE channel
3844 }
3945
40- private void handleMessage (JsonNode jsonNode , Future <Void > future ) {
41- try {
42- if (jsonNode .has ("error" )) {
43- JSONRPCError error = OBJECT_MAPPER .treeToValue (jsonNode .get ("error" ), JSONRPCError .class );
44- errorHandler .accept (error );
45- } else if (jsonNode .has ("result" )) {
46- // result can be a Task, Message, TaskStatusUpdateEvent, or TaskArtifactUpdateEvent
47- JsonNode result = jsonNode .path ("result" );
48- StreamingEventKind event = OBJECT_MAPPER .treeToValue (result , StreamingEventKind .class );
49- eventHandler .accept (event );
50- if (event instanceof TaskStatusUpdateEvent && ((TaskStatusUpdateEvent ) event ).isFinal ()) {
51- future .cancel (true ); // close SSE channel
52- }
53- } else {
54- throw new IllegalArgumentException ("Unknown message type" );
46+ private void handleMessage (JSONRPCResponse <?> response , Future <Void > future ) {
47+ if (null != response .getError ()) {
48+ errorHandler .accept (response .getError ());
49+ } else if (null != response .getResult ()) {
50+ // result can be a Task, Message, TaskStatusUpdateEvent, or TaskArtifactUpdateEvent
51+ StreamingEventKind event = (StreamingEventKind ) response .getResult ();
52+ eventHandler .accept (event );
53+ if (event instanceof TaskStatusUpdateEvent && ((TaskStatusUpdateEvent ) event ).isFinal ()) {
54+ future .cancel (true ); // close SSE channel
5555 }
56- } catch ( JsonProcessingException e ) {
57- throw new RuntimeException ( e );
56+ } else {
57+ throw new IllegalArgumentException ( "Unknown message type" );
5858 }
5959 }
60-
6160}
0 commit comments