|
16 | 16 | import io.a2a.spec.A2AError; |
17 | 17 | import io.a2a.spec.Event; |
18 | 18 | import io.a2a.spec.EventKind; |
| 19 | +import io.a2a.spec.InternalError; |
19 | 20 | import io.a2a.spec.Message; |
20 | 21 | import io.a2a.spec.Task; |
21 | 22 | import io.a2a.spec.TaskState; |
@@ -96,6 +97,7 @@ public EventKind consumeAll(EventConsumer consumer) throws A2AError { |
96 | 97 | public EventTypeAndInterrupt consumeAndBreakOnInterrupt(EventConsumer consumer, boolean blocking) throws A2AError { |
97 | 98 | Flow.Publisher<EventQueueItem> allItems = consumer.consumeAll(); |
98 | 99 | AtomicReference<Message> message = new AtomicReference<>(); |
| 100 | + AtomicReference<Task> capturedTask = new AtomicReference<>(); // Capture Task events |
99 | 101 | AtomicBoolean interrupted = new AtomicBoolean(false); |
100 | 102 | AtomicReference<Throwable> errorRef = new AtomicReference<>(); |
101 | 103 | CompletableFuture<Void> completionFuture = new CompletableFuture<>(); |
@@ -129,6 +131,11 @@ public EventTypeAndInterrupt consumeAndBreakOnInterrupt(EventConsumer consumer, |
129 | 131 | return false; |
130 | 132 | } |
131 | 133 |
|
| 134 | + // Capture Task events (especially for new tasks where taskManager.getTask() would return null) |
| 135 | + if (event instanceof Task t) { |
| 136 | + capturedTask.set(t); |
| 137 | + } |
| 138 | + |
132 | 139 | // TaskStore update moved to MainEventBusProcessor |
133 | 140 |
|
134 | 141 | // Determine interrupt behavior |
@@ -229,20 +236,20 @@ else if (blocking) { |
229 | 236 | Utils.rethrow(error); |
230 | 237 | } |
231 | 238 |
|
232 | | - EventKind eventType; |
233 | | - Message msg = message.get(); |
234 | | - if (msg != null) { |
235 | | - eventType = msg; |
236 | | - } else { |
237 | | - Task task = taskManager.getTask(); |
238 | | - if (task == null) { |
239 | | - throw new io.a2a.spec.InternalError("No task or message available after consuming events"); |
240 | | - } |
241 | | - eventType = task; |
| 239 | + // Return Message if captured, otherwise Task if captured, otherwise fetch from TaskStore |
| 240 | + EventKind eventKind = message.get(); |
| 241 | + if (eventKind == null) { |
| 242 | + eventKind = capturedTask.get(); |
| 243 | + } |
| 244 | + if (eventKind == null) { |
| 245 | + eventKind = taskManager.getTask(); |
| 246 | + } |
| 247 | + if (eventKind == null) { |
| 248 | + throw new InternalError("Could not find a Task/Message for " + taskManager.getTaskId()); |
242 | 249 | } |
243 | 250 |
|
244 | 251 | return new EventTypeAndInterrupt( |
245 | | - eventType, |
| 252 | + eventKind, |
246 | 253 | interrupted.get(), |
247 | 254 | consumptionCompletionFuture); |
248 | 255 | } |
|
0 commit comments