Repository navigation
Expand file tree
/
Copy pathDefaultRequestHandler.java
More file actions
601 lines (516 loc) · 26 KB
/
Copy pathDefaultRequestHandler.java
File metadata and controls
601 lines (516 loc) · 26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package io.a2a.server.requesthandlers;
import static io.a2a.server.util.async.AsyncUtils.convertingProcessor;
import static io.a2a.server.util.async.AsyncUtils.createTubeConfig;
import static io.a2a.server.util.async.AsyncUtils.processor;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Flow;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import io.a2a.server.ServerCallContext;
import io.a2a.server.agentexecution.AgentExecutor;
import io.a2a.server.agentexecution.RequestContext;
import io.a2a.server.agentexecution.SimpleRequestContextBuilder;
import io.a2a.server.events.EnhancedRunnable;
import io.a2a.server.events.EventConsumer;
import io.a2a.server.events.EventQueue;
import io.a2a.server.events.NoTaskQueueException;
import io.a2a.server.events.QueueManager;
import io.a2a.server.events.TaskQueueExistsException;
import io.a2a.server.tasks.PushNotificationConfigStore;
import io.a2a.server.tasks.PushNotificationSender;
import io.a2a.server.tasks.ResultAggregator;
import io.a2a.server.tasks.TaskManager;
import io.a2a.server.tasks.TaskStore;
import io.a2a.server.util.async.Internal;
import io.a2a.spec.DeleteTaskPushNotificationConfigParams;
import io.a2a.spec.Event;
import io.a2a.spec.EventKind;
import io.a2a.spec.GetTaskPushNotificationConfigParams;
import io.a2a.spec.InternalError;
import io.a2a.spec.JSONRPCError;
import io.a2a.spec.ListTaskPushNotificationConfigParams;
import io.a2a.spec.Message;
import io.a2a.spec.MessageSendParams;
import io.a2a.spec.PushNotificationConfig;
import io.a2a.spec.StreamingEventKind;
import io.a2a.spec.Task;
import io.a2a.spec.TaskIdParams;
import io.a2a.spec.TaskNotCancelableError;
import io.a2a.spec.TaskState;
import io.a2a.spec.TaskNotFoundError;
import io.a2a.spec.TaskPushNotificationConfig;
import io.a2a.spec.TaskQueryParams;
import io.a2a.spec.UnsupportedOperationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ApplicationScoped
public class DefaultRequestHandler implements RequestHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRequestHandler.class);
private final AgentExecutor agentExecutor;
private final TaskStore taskStore;
private final QueueManager queueManager;
private final PushNotificationConfigStore pushConfigStore;
private final PushNotificationSender pushSender;
private final Supplier<RequestContext.Builder> requestContextBuilder;
private final ConcurrentMap<String, CompletableFuture<Void>> runningAgents = new ConcurrentHashMap<>();
private final Set<CompletableFuture<Void>> backgroundTasks = ConcurrentHashMap.newKeySet();
private final Executor executor;
@Inject
public DefaultRequestHandler(AgentExecutor agentExecutor, TaskStore taskStore,
QueueManager queueManager, PushNotificationConfigStore pushConfigStore,
PushNotificationSender pushSender, @Internal Executor executor) {
this.agentExecutor = agentExecutor;
this.taskStore = taskStore;
this.queueManager = queueManager;
this.pushConfigStore = pushConfigStore;
this.pushSender = pushSender;
this.executor = executor;
// TODO In Python this is also a constructor parameter defaulting to this SimpleRequestContextBuilder
// implementation if the parameter is null. Skip that for now, since otherwise I get CDI errors, and
// I am unsure about the correct scope.
// Also reworked to make a Supplier since otherwise the builder gets polluted with wrong tasks
this.requestContextBuilder = () -> new SimpleRequestContextBuilder(taskStore, false);
}
@Override
public Task onGetTask(TaskQueryParams params, ServerCallContext context) throws JSONRPCError {
LOGGER.debug("onGetTask {}", params.id());
Task task = taskStore.get(params.id());
if (task == null) {
LOGGER.debug("No task found for {}. Throwing TaskNotFoundError", params.id());
throw new TaskNotFoundError();
}
if (params.historyLength() != null && task.getHistory() != null && params.historyLength() < task.getHistory().size()) {
List<Message> history;
if (params.historyLength() <= 0) {
history = new ArrayList<>();
} else {
history = task.getHistory().subList(
task.getHistory().size() - params.historyLength(),
task.getHistory().size() - 1);
}
task = new Task.Builder(task)
.history(history)
.build();
}
LOGGER.debug("Task found {}", task);
return task;
}
@Override
public Task onCancelTask(TaskIdParams params, ServerCallContext context) throws JSONRPCError {
Task task = taskStore.get(params.id());
if (task == null) {
throw new TaskNotFoundError();
}
// Check if task is in a non-cancelable state (completed, canceled, failed, rejected)
if (task.getStatus().state().isFinal()) {
throw new TaskNotCancelableError(
"Task cannot be canceled - current state: " + task.getStatus().state().asString());
}
TaskManager taskManager = new TaskManager(
task.getId(),
task.getContextId(),
taskStore,
null);
ResultAggregator resultAggregator = new ResultAggregator(taskManager, null);
EventQueue queue = queueManager.tap(task.getId());
if (queue == null) {
queue = queueManager.getEventQueueBuilder(task.getId()).build();
}
agentExecutor.cancel(
requestContextBuilder.get()
.setTaskId(task.getId())
.setContextId(task.getContextId())
.setTask(task)
.setServerCallContext(context)
.build(),
queue);
Optional.ofNullable(runningAgents.get(task.getId()))
.ifPresent(cf -> cf.cancel(true));
EventConsumer consumer = new EventConsumer(queue);
EventKind type = resultAggregator.consumeAll(consumer);
if (!(type instanceof Task tempTask)) {
throw new InternalError("Agent did not return valid response for cancel");
}
// Verify task was actually canceled (not completed concurrently)
if (tempTask.getStatus().state() != TaskState.CANCELED) {
throw new TaskNotCancelableError(
"Task cannot be canceled - current state: " + tempTask.getStatus().state().asString());
}
return tempTask;
}
@Override
public EventKind onMessageSend(MessageSendParams params, ServerCallContext context) throws JSONRPCError {
LOGGER.debug("onMessageSend - task: {}; context {}", params.message().getTaskId(), params.message().getContextId());
MessageSendSetup mss = initMessageSend(params, context);
String taskId = mss.requestContext.getTaskId();
LOGGER.debug("Request context taskId: {}", taskId);
EventQueue queue = queueManager.createOrTap(taskId);
ResultAggregator resultAggregator = new ResultAggregator(mss.taskManager, null);
boolean blocking = true; // Default to blocking behavior
if (params.configuration() != null && Boolean.FALSE.equals(params.configuration().blocking())) {
blocking = false;
}
boolean interruptedOrNonBlocking = false;
EnhancedRunnable producerRunnable = registerAndExecuteAgentAsync(taskId, mss.requestContext, queue);
ResultAggregator.EventTypeAndInterrupt etai = null;
try {
// Create callback for push notifications during background event processing
Runnable pushNotificationCallback = () -> sendPushNotification(taskId, resultAggregator);
EventConsumer consumer = new EventConsumer(queue);
// This callback must be added before we start consuming. Otherwise,
// any errors thrown by the producerRunnable are not picked up by the consumer
producerRunnable.addDoneCallback(consumer.createAgentRunnableDoneCallback());
etai = resultAggregator.consumeAndBreakOnInterrupt(consumer, blocking, pushNotificationCallback);
if (etai == null) {
LOGGER.debug("No result, throwing InternalError");
throw new InternalError("No result");
}
interruptedOrNonBlocking = etai.interrupted();
LOGGER.debug("Was interrupted or non-blocking: {}", interruptedOrNonBlocking);
EventKind kind = etai.eventType();
if (kind instanceof Task taskResult && !taskId.equals(taskResult.getId())) {
throw new InternalError("Task ID mismatch in agent response");
}
// Send push notification after initial return (for both blocking and non-blocking)
pushNotificationCallback.run();
} finally {
if (interruptedOrNonBlocking) {
CompletableFuture<Void> cleanupTask = CompletableFuture.runAsync(() -> cleanupProducer(taskId, false), executor);
trackBackgroundTask(cleanupTask);
} else {
cleanupProducer(taskId, false);
}
}
LOGGER.debug("Returning: {}", etai.eventType());
return etai.eventType();
}
@Override
public Flow.Publisher<StreamingEventKind> onMessageSendStream(
MessageSendParams params, ServerCallContext context) throws JSONRPCError {
LOGGER.debug("onMessageSendStream - task: {}; context {}", params.message().getTaskId(), params.message().getContextId());
MessageSendSetup mss = initMessageSend(params, context);
AtomicReference<String> taskId = new AtomicReference<>(mss.requestContext.getTaskId());
EventQueue queue = queueManager.createOrTap(taskId.get());
ResultAggregator resultAggregator = new ResultAggregator(mss.taskManager, null);
EnhancedRunnable producerRunnable = registerAndExecuteAgentAsync(taskId.get(), mss.requestContext, queue);
// Move consumer creation and callback registration outside try block
// so consumer is available for background consumption on client disconnect
EventConsumer consumer = new EventConsumer(queue);
producerRunnable.addDoneCallback(consumer.createAgentRunnableDoneCallback());
AtomicBoolean backgroundConsumeStarted = new AtomicBoolean(false);
try {
Flow.Publisher<Event> results = resultAggregator.consumeAndEmit(consumer);
Flow.Publisher<Event> eventPublisher =
processor(createTubeConfig(), results, ((errorConsumer, event) -> {
if (event instanceof Task createdTask) {
if (!Objects.equals(taskId.get(), createdTask.getId())) {
errorConsumer.accept(new InternalError("Task ID mismatch in agent response"));
}
// TODO the Python implementation no longer has the following block but removing it causes
// failures here
try {
queueManager.add(createdTask.getId(), queue);
taskId.set(createdTask.getId());
} catch (TaskQueueExistsException e) {
// TODO Log
}
if (pushConfigStore != null &&
params.configuration() != null &&
params.configuration().pushNotificationConfig() != null) {
pushConfigStore.setInfo(
createdTask.getId(),
params.configuration().pushNotificationConfig());
}
}
if (pushSender != null && taskId.get() != null) {
EventKind latest = resultAggregator.getCurrentResult();
if (latest instanceof Task latestTask) {
pushSender.sendNotification(latestTask);
}
}
return true;
}));
Flow.Publisher<StreamingEventKind> finalPublisher = convertingProcessor(eventPublisher, event -> (StreamingEventKind) event);
// Wrap publisher to detect client disconnect and continue background consumption
return subscriber -> finalPublisher.subscribe(new Flow.Subscriber<StreamingEventKind>() {
private Flow.Subscription subscription;
@Override
public void onSubscribe(Flow.Subscription subscription) {
this.subscription = subscription;
// Wrap subscription to detect cancellation
subscriber.onSubscribe(new Flow.Subscription() {
@Override
public void request(long n) {
subscription.request(n);
}
@Override
public void cancel() {
LOGGER.debug("Client cancelled subscription for task {}, starting background consumption", taskId.get());
startBackgroundConsumption();
subscription.cancel();
}
});
}
@Override
public void onNext(StreamingEventKind item) {
subscriber.onNext(item);
}
@Override
public void onError(Throwable throwable) {
subscriber.onError(throwable);
}
@Override
public void onComplete() {
subscriber.onComplete();
}
private void startBackgroundConsumption() {
if (backgroundConsumeStarted.compareAndSet(false, true)) {
// Client disconnected: continue consuming and persisting events in background
CompletableFuture<Void> bgTask = CompletableFuture.runAsync(() -> {
try {
resultAggregator.consumeAll(consumer);
LOGGER.debug("Background consumption completed for task {}", taskId.get());
} catch (Exception e) {
LOGGER.error("Error during background consumption for task {}", taskId.get(), e);
}
}, executor);
trackBackgroundTask(bgTask);
}
}
});
} finally {
CompletableFuture<Void> cleanupTask = CompletableFuture.runAsync(() -> cleanupProducer(taskId.get(), true), executor);
trackBackgroundTask(cleanupTask);
}
}
@Override
public TaskPushNotificationConfig onSetTaskPushNotificationConfig(
TaskPushNotificationConfig params, ServerCallContext context) throws JSONRPCError {
if (pushConfigStore == null) {
throw new UnsupportedOperationError();
}
Task task = taskStore.get(params.taskId());
if (task == null) {
throw new TaskNotFoundError();
}
PushNotificationConfig pushNotificationConfig = pushConfigStore.setInfo(params.taskId(), params.pushNotificationConfig());
return new TaskPushNotificationConfig(params.taskId(), pushNotificationConfig);
}
@Override
public TaskPushNotificationConfig onGetTaskPushNotificationConfig(
GetTaskPushNotificationConfigParams params, ServerCallContext context) throws JSONRPCError {
if (pushConfigStore == null) {
throw new UnsupportedOperationError();
}
Task task = taskStore.get(params.id());
if (task == null) {
throw new TaskNotFoundError();
}
List<PushNotificationConfig> pushNotificationConfigList = pushConfigStore.getInfo(params.id());
if (pushNotificationConfigList == null || pushNotificationConfigList.isEmpty()) {
throw new InternalError("No push notification config found");
}
return new TaskPushNotificationConfig(params.id(), getPushNotificationConfig(pushNotificationConfigList, params.pushNotificationConfigId()));
}
private PushNotificationConfig getPushNotificationConfig(List<PushNotificationConfig> notificationConfigList,
String configId) {
if (configId != null) {
for (PushNotificationConfig notificationConfig : notificationConfigList) {
if (configId.equals(notificationConfig.id())) {
return notificationConfig;
}
}
}
return notificationConfigList.get(0);
}
@Override
public Flow.Publisher<StreamingEventKind> onResubscribeToTask(
TaskIdParams params, ServerCallContext context) throws JSONRPCError {
Task task = taskStore.get(params.id());
if (task == null) {
throw new TaskNotFoundError();
}
TaskManager taskManager = new TaskManager(task.getId(), task.getContextId(), taskStore, null);
ResultAggregator resultAggregator = new ResultAggregator(taskManager, null);
EventQueue queue = queueManager.tap(task.getId());
if (queue == null) {
throw new TaskNotFoundError();
}
EventConsumer consumer = new EventConsumer(queue);
Flow.Publisher<Event> results = resultAggregator.consumeAndEmit(consumer);
return convertingProcessor(results, e -> (StreamingEventKind) e);
}
@Override
public List<TaskPushNotificationConfig> onListTaskPushNotificationConfig(
ListTaskPushNotificationConfigParams params, ServerCallContext context) throws JSONRPCError {
if (pushConfigStore == null) {
throw new UnsupportedOperationError();
}
Task task = taskStore.get(params.id());
if (task == null) {
throw new TaskNotFoundError();
}
List<PushNotificationConfig> pushNotificationConfigList = pushConfigStore.getInfo(params.id());
List<TaskPushNotificationConfig> taskPushNotificationConfigList = new ArrayList<>();
if (pushNotificationConfigList != null) {
for (PushNotificationConfig pushNotificationConfig : pushNotificationConfigList) {
TaskPushNotificationConfig taskPushNotificationConfig = new TaskPushNotificationConfig(params.id(), pushNotificationConfig);
taskPushNotificationConfigList.add(taskPushNotificationConfig);
}
}
return taskPushNotificationConfigList;
}
@Override
public void onDeleteTaskPushNotificationConfig(
DeleteTaskPushNotificationConfigParams params, ServerCallContext context) {
if (pushConfigStore == null) {
throw new UnsupportedOperationError();
}
Task task = taskStore.get(params.id());
if (task == null) {
throw new TaskNotFoundError();
}
pushConfigStore.deleteInfo(params.id(), params.pushNotificationConfigId());
}
private boolean shouldAddPushInfo(MessageSendParams params) {
return pushConfigStore != null && params.configuration() != null && params.configuration().pushNotificationConfig() != null;
}
private EnhancedRunnable registerAndExecuteAgentAsync(String taskId, RequestContext requestContext, EventQueue queue) {
EnhancedRunnable runnable = new EnhancedRunnable() {
@Override
public void run() {
agentExecutor.execute(requestContext, queue);
try {
queueManager.awaitQueuePollerStart(queue);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
CompletableFuture<Void> cf = CompletableFuture.runAsync(runnable, executor)
.whenComplete((v, err) -> {
if (err != null) {
runnable.setError(err);
// Close queue on error
queue.close();
} else {
// Only close queue if task is in a final state
Task task = taskStore.get(taskId);
if (task != null && task.getStatus().state().isFinal()) {
queue.close();
} else {
LOGGER.debug("Task {} not in final state or not yet created, keeping queue open", taskId);
}
}
runnable.invokeDoneCallbacks();
});
runningAgents.put(taskId, cf);
return runnable;
}
private void trackBackgroundTask(CompletableFuture<Void> task) {
backgroundTasks.add(task);
task.whenComplete((result, throwable) -> {
try {
if (throwable != null) {
if (throwable instanceof java.util.concurrent.CancellationException) {
LOGGER.debug("Background task cancelled: {}", task);
} else {
LOGGER.error("Background task failed", throwable);
}
}
} finally {
backgroundTasks.remove(task);
}
});
}
private void cleanupProducer(String taskId, boolean isStreaming) {
LOGGER.debug("Starting cleanup for task {} (streaming={})", taskId, isStreaming);
// TODO the Python implementation waits for the producerRunnable
CompletableFuture<Void> agentFuture = runningAgents.get(taskId);
if (agentFuture == null) {
LOGGER.debug("No running agent found for task {}", taskId);
return;
}
agentFuture.whenComplete((v, t) -> {
LOGGER.debug("Agent completed for task {}", taskId);
boolean closeQueue = false;
if (isStreaming) {
// For streaming calls, always close queue when agent completes
// Each streaming call is independent and doesn't support multi-message reuse
LOGGER.debug("Streaming call, closing queue for task {}", taskId);
closeQueue = true;
} else {
// For non-streaming calls, only close queue if task is in final state
// For non-final states, queue must stay open for potential future messages to same taskId
// so we can handle the "fire and forget' case used e.g. in the TCK
Task task = taskStore.get(taskId);
if (task != null && task.getStatus().state().isFinal()) {
LOGGER.debug("Task in final state, closing queue for task {}", taskId);
closeQueue = true;
} else {
LOGGER.debug("Task not in final state, keeping queue open for task {}", taskId);
}
}
if (closeQueue) {
try {
queueManager.close(taskId);
} catch (NoTaskQueueException e) {
// This can happen if the queue was already closed and removed, which is not an error in this cleanup context.
LOGGER.debug("Queue for task {} was already closed or does not exist.", taskId);
} catch (Exception e) {
LOGGER.debug("Error closing queue for task {}: {}", taskId, e.getMessage());
}
}
// Always remove from running agents
runningAgents.remove(taskId);
});
}
private MessageSendSetup initMessageSend(MessageSendParams params, ServerCallContext context) {
TaskManager taskManager = new TaskManager(
params.message().getTaskId(),
params.message().getContextId(),
taskStore,
params.message());
Task task = taskManager.getTask();
if (task != null) {
LOGGER.debug("Found task updating with message {}", params.message());
task = taskManager.updateWithMessage(params.message(), task);
if (shouldAddPushInfo(params)) {
LOGGER.debug("Adding push info");
pushConfigStore.setInfo(task.getId(), params.configuration().pushNotificationConfig());
}
}
RequestContext requestContext = requestContextBuilder.get()
.setParams(params)
.setTaskId(task == null ? null : task.getId())
.setContextId(params.message().getContextId())
.setTask(task)
.setServerCallContext(context)
.build();
return new MessageSendSetup(taskManager, task, requestContext);
}
private void sendPushNotification(String taskId, ResultAggregator resultAggregator) {
if (pushSender != null && taskId != null) {
EventKind latest = resultAggregator.getCurrentResult();
if (latest instanceof Task latestTask) {
pushSender.sendNotification(latestTask);
}
}
}
private boolean isTaskFinalInTaskStore(String taskId) {
Task task = taskStore.get(taskId);
return task != null && task.getStatus().state().isFinal();
}
private record MessageSendSetup(TaskManager taskManager, Task task, RequestContext requestContext) {}
}