99import jakarta .inject .Inject ;
1010
1111import java .util .List ;
12+ import java .util .concurrent .CompletableFuture ;
1213import java .util .concurrent .Flow ;
1314import java .util .regex .Matcher ;
1415import java .util .regex .Pattern ;
@@ -143,7 +144,7 @@ private HTTPRestResponse handlePostRequest(String path, String body, ServerCallC
143144 public HTTPRestResponse sendMessage (String body , ServerCallContext context ) {
144145 io .a2a .grpc .SendMessageRequest .Builder request = io .a2a .grpc .SendMessageRequest .newBuilder ();
145146 parseRequestBody (body , request );
146- EventKind result = requestHandler .onMessageSend (ProtoUtils .FromProto .messageSendParams (request ), context );
147+ EventKind result = requestHandler .onMessageSend (ProtoUtils .FromProto .messageSendParams (request . build () ), context );
147148 return createSuccessResponse (200 , io .a2a .grpc .SendMessageResponse .newBuilder (ProtoUtils .ToProto .taskOrMessage (result )));
148149 }
149150
@@ -154,7 +155,7 @@ public HTTPRestStreamingResponse sendStreamingMessage(String body, ServerCallCon
154155 try {
155156 io .a2a .grpc .SendMessageRequest .Builder request = io .a2a .grpc .SendMessageRequest .newBuilder ();
156157 parseRequestBody (body , request );
157- Flow .Publisher <StreamingEventKind > publisher = requestHandler .onMessageSendStream (ProtoUtils .FromProto .messageSendParams (request ), context );
158+ Flow .Publisher <StreamingEventKind > publisher = requestHandler .onMessageSendStream (ProtoUtils .FromProto .messageSendParams (request . build () ), context );
158159 return createStreamingResponse (publisher );
159160 } catch (JSONRPCError e ) {
160161 return new HTTPRestStreamingResponse (ZeroPublisher .fromItems (new HTTPRestErrorResponse (e ).toJson ()));
@@ -221,7 +222,7 @@ public HTTPRestResponse listTaskPushNotificationConfigurations(String taskId, Se
221222 }
222223 ListTaskPushNotificationConfigParams params = new ListTaskPushNotificationConfigParams (taskId );
223224 List <TaskPushNotificationConfig > configs = requestHandler .onListTaskPushNotificationConfig (params , context );
224- return createSuccessResponse (200 , io . a2a . grpc . ListTaskPushNotificationConfigResponse . newBuilder ( ProtoUtils .ToProto .listTaskPushNotificationConfigResponse (configs )));
225+ return createSuccessResponse (200 , ProtoUtils .ToProto .listTaskPushNotificationConfigResponse (configs ). toBuilder ( ));
225226 }
226227
227228 public HTTPRestResponse deleteTaskPushNotificationConfiguration (String taskId , String configId , ServerCallContext context ) {
@@ -289,43 +290,45 @@ private Flow.Publisher<String> convertToSendStreamingMessageResponse(
289290 // We can't use the normal convertingProcessor since that propagates any errors as an error handled
290291 // via Subscriber.onError() rather than as part of the SendStreamingResponse payload
291292 return ZeroPublisher .create (createTubeConfig (), tube -> {
292- publisher .subscribe (new Flow .Subscriber <StreamingEventKind >() {
293- Flow .Subscription subscription ;
294-
295- @ Override
296- public void onSubscribe (Flow .Subscription subscription ) {
297- this .subscription = subscription ;
298- subscription .request (1 );
299- }
300-
301- @ Override
302- public void onNext (StreamingEventKind item ) {
303- try {
304- String payload = JsonFormat .printer ().omittingInsignificantWhitespace ().print (ProtoUtils .ToProto .taskOrMessageStream (item ));
305- System .out .println ("############## Sending event " + payload );
306- tube .send (payload );
293+ CompletableFuture .runAsync (() -> {
294+ publisher .subscribe (new Flow .Subscriber <StreamingEventKind >() {
295+ Flow .Subscription subscription ;
296+
297+ @ Override
298+ public void onSubscribe (Flow .Subscription subscription ) {
299+ this .subscription = subscription ;
307300 subscription .request (1 );
308- } catch (InvalidProtocolBufferException ex ) {
309- onError (ex );
310301 }
311- }
312-
313- @ Override
314- public void onError (Throwable throwable ) {
315- System .out .println ("############## Sending error " + throwable );
316- throwable .printStackTrace ();
317- if (throwable instanceof JSONRPCError jsonrpcError ) {
318- tube .send (new HTTPRestErrorResponse (jsonrpcError ).toJson ());
319- } else {
320- tube .send (new HTTPRestErrorResponse (new InternalError (throwable .getMessage ())).toJson ());
302+
303+ @ Override
304+ public void onNext (StreamingEventKind item ) {
305+ try {
306+ String payload = JsonFormat .printer ().omittingInsignificantWhitespace ().print (ProtoUtils .ToProto .taskOrMessageStream (item ));
307+ System .out .println ("############## Sending event " + payload );
308+ tube .send (payload );
309+ subscription .request (1 );
310+ } catch (InvalidProtocolBufferException ex ) {
311+ onError (ex );
312+ }
321313 }
322- onComplete ();
323- }
324314
325- @ Override
326- public void onComplete () {
327- tube .complete ();
328- }
315+ @ Override
316+ public void onError (Throwable throwable ) {
317+ System .out .println ("############## Sending error " + throwable );
318+ throwable .printStackTrace ();
319+ if (throwable instanceof JSONRPCError jsonrpcError ) {
320+ tube .send (new HTTPRestErrorResponse (jsonrpcError ).toJson ());
321+ } else {
322+ tube .send (new HTTPRestErrorResponse (new InternalError (throwable .getMessage ())).toJson ());
323+ }
324+ onComplete ();
325+ }
326+
327+ @ Override
328+ public void onComplete () {
329+ tube .complete ();
330+ }
331+ });
329332 });
330333 });
331334 }
0 commit comments