-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathRemoteA2AAgent.java
More file actions
550 lines (496 loc) · 19.4 KB
/
RemoteA2AAgent.java
File metadata and controls
550 lines (496 loc) · 19.4 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
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.adk.a2a.agent;
import static com.google.common.base.Strings.nullToEmpty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.adk.a2a.common.A2AClientError;
import com.google.adk.a2a.common.A2AMetadata;
import com.google.adk.a2a.converters.EventConverter;
import com.google.adk.a2a.converters.ResponseConverter;
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.Callbacks;
import com.google.adk.agents.InvocationContext;
import com.google.adk.events.Event;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.genai.types.Content;
import com.google.genai.types.CustomMetadata;
import com.google.genai.types.Part;
import io.a2a.client.Client;
import io.a2a.client.ClientEvent;
import io.a2a.client.MessageEvent;
import io.a2a.client.TaskEvent;
import io.a2a.client.TaskUpdateEvent;
import io.a2a.spec.A2AClientException;
import io.a2a.spec.AgentCard;
import io.a2a.spec.Message;
import io.a2a.spec.TaskArtifactUpdateEvent;
import io.a2a.spec.TaskState;
import io.a2a.spec.TaskStatusUpdateEvent;
import io.reactivex.rxjava3.core.BackpressureStrategy;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableEmitter;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiConsumer;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Agent that communicates with a remote A2A agent via an A2A client.
*
* <p>The remote agent can be specified directly by providing an {@link AgentCard} to the builder,
* or it can be resolved automatically using the provided A2A client.
*
* <p>Key responsibilities of this agent include:
*
* <ul>
* <li>Agent card resolution and validation
* <li>Converting ADK session history events into A2A requests ({@link io.a2a.spec.Message})
* <li>Handling streaming and non-streaming responses from the A2A client
* <li>Buffering and aggregating streamed response chunks into ADK {@link
* com.google.adk.events.Event}s
* <li>Converting A2A client responses back into ADK format
* </ul>
*/
public class RemoteA2AAgent extends BaseAgent {
private static final Logger logger = LoggerFactory.getLogger(RemoteA2AAgent.class);
private static final ObjectMapper objectMapper =
new ObjectMapper().registerModule(new JavaTimeModule());
private final AgentCard agentCard;
private final Client a2aClient;
private String description;
private final boolean streaming;
// Internal constructor used by builder
private RemoteA2AAgent(Builder builder) {
super(
builder.name,
builder.description,
builder.subAgents,
builder.beforeAgentCallback,
builder.afterAgentCallback);
if (builder.a2aClient == null) {
throw new IllegalArgumentException("a2aClient cannot be null");
}
this.a2aClient = builder.a2aClient;
if (builder.agentCard != null) {
this.agentCard = builder.agentCard;
} else {
try {
this.agentCard = this.a2aClient.getAgentCard();
} catch (A2AClientException e) {
throw new AgentCardResolutionError("Failed to resolve agent card", e);
}
}
if (this.agentCard == null) {
throw new IllegalArgumentException("agentCard cannot be null");
}
this.description = nullToEmpty(builder.description);
// If builder description is empty, use the one from AgentCard
if (this.description.isEmpty() && this.agentCard.description() != null) {
this.description = this.agentCard.description();
}
this.streaming = this.agentCard.capabilities().streaming();
}
public static Builder builder() {
return new Builder();
}
/** Builder for {@link RemoteA2AAgent}. */
public static class Builder {
private String name;
private AgentCard agentCard;
private Client a2aClient;
private String description = "";
private List<? extends BaseAgent> subAgents;
private List<Callbacks.BeforeAgentCallback> beforeAgentCallback;
private List<Callbacks.AfterAgentCallback> afterAgentCallback;
@CanIgnoreReturnValue
public Builder name(String name) {
this.name = name;
return this;
}
@CanIgnoreReturnValue
public Builder agentCard(AgentCard agentCard) {
this.agentCard = agentCard;
return this;
}
@CanIgnoreReturnValue
public Builder description(String description) {
this.description = description;
return this;
}
@CanIgnoreReturnValue
public Builder subAgents(List<? extends BaseAgent> subAgents) {
this.subAgents = subAgents;
return this;
}
@CanIgnoreReturnValue
public Builder beforeAgentCallback(List<Callbacks.BeforeAgentCallback> beforeAgentCallback) {
this.beforeAgentCallback = beforeAgentCallback;
return this;
}
@CanIgnoreReturnValue
public Builder afterAgentCallback(List<Callbacks.AfterAgentCallback> afterAgentCallback) {
this.afterAgentCallback = afterAgentCallback;
return this;
}
@CanIgnoreReturnValue
public Builder a2aClient(Client a2aClient) {
this.a2aClient = a2aClient;
return this;
}
public RemoteA2AAgent build() {
return new RemoteA2AAgent(this);
}
}
private Message.Builder newA2AMessage(Message.Role role, List<io.a2a.spec.Part<?>> parts) {
return new Message.Builder().messageId(UUID.randomUUID().toString()).role(role).parts(parts);
}
private Message prepareMessage(InvocationContext invocationContext) {
Event userCall = EventConverter.findUserFunctionCall(invocationContext.session().events());
if (userCall != null) {
ImmutableList<io.a2a.spec.Part<?>> parts =
EventConverter.contentToParts(userCall.content(), userCall.partial().orElse(false));
return newA2AMessage(Message.Role.USER, parts)
.taskId(EventConverter.taskId(userCall))
.contextId(EventConverter.contextId(userCall))
.build();
}
return newA2AMessage(
Message.Role.USER, EventConverter.messagePartsFromContext(invocationContext))
.build();
}
@Override
protected Flowable<Event> runAsyncImpl(InvocationContext invocationContext) {
// Construct A2A Message from the last ADK event
List<Event> sessionEvents = invocationContext.session().events();
if (sessionEvents.isEmpty()) {
logger.warn("No events in session, cannot send message to remote agent.");
return Flowable.empty();
}
Message originalMessage = prepareMessage(invocationContext);
String requestJson = serializeMessageToJson(originalMessage);
return Flowable.create(
emitter -> {
StreamHandler handler =
new StreamHandler(
emitter.serialize(), invocationContext, requestJson, streaming, name());
ImmutableList<BiConsumer<ClientEvent, AgentCard>> consumers =
ImmutableList.of(handler::handleEvent);
a2aClient.sendMessage(originalMessage, consumers, handler::handleError, null);
},
BackpressureStrategy.BUFFER);
}
private @Nullable String serializeMessageToJson(Message message) {
try {
return objectMapper.writeValueAsString(message);
} catch (JsonProcessingException e) {
logger.warn("Failed to serialize request", e);
return null;
}
}
private static class StreamHandler {
private final FlowableEmitter<Event> emitter;
private final InvocationContext invocationContext;
private final String requestJson;
private final boolean streaming;
private final String agentName;
private boolean done = false;
private final StringBuilder textBuffer = new StringBuilder();
private final StringBuilder thoughtsBuffer = new StringBuilder();
StreamHandler(
FlowableEmitter<Event> emitter,
InvocationContext invocationContext,
String requestJson,
boolean streaming,
String agentName) {
this.emitter = emitter;
this.invocationContext = invocationContext;
this.requestJson = requestJson;
this.streaming = streaming;
this.agentName = agentName;
}
synchronized void handleError(Throwable e) {
// Mark the flow as done if it is already cancelled.
if (!done) {
done = emitter.isCancelled();
}
// If the flow is already done, stop processing.
if (done) {
return;
}
// If the error is raised, complete the flow with an error.
done = true;
emitter.tryOnError(new A2AClientError("Failed to communicate with the remote agent", e));
}
// TODO: b/483038527 - The synchronized block might block the thread, we should optimize for
// performance in the future.
synchronized void handleEvent(ClientEvent clientEvent, AgentCard unused) {
// Mark the flow as done if it is already cancelled.
if (!done) {
done = emitter.isCancelled();
}
// If the flow is already done, stop processing.
if (done) {
return;
}
Optional<Event> eventOpt =
ResponseConverter.clientEventToEvent(clientEvent, invocationContext);
eventOpt.ifPresent(
event -> {
addMetadata(event, clientEvent);
if (isCompleted(clientEvent)) {
// Terminal event, check if we can merge.
boolean mergeResult = mergeAggregatedContentIntoEvent(event);
if (!mergeResult) {
emitAggregatedEventAndClearBuffer(null);
}
} else {
boolean isPartial = event.partial().orElse(false);
if (isPartial) {
if (shouldResetBuffer(clientEvent)) {
clearBuffer();
}
boolean addedToBuffer = bufferContent(event, clientEvent);
if (!addedToBuffer) {
// Partial event with no content to buffer (e.g. tool call).
// Flush buffer before emitting this event.
emitAggregatedEventAndClearBuffer(null);
}
} else {
// Intermediate non-partial.
emitAggregatedEventAndClearBuffer(null);
}
}
emitter.onNext(event);
});
// For non-streaming communication, complete the flow; for streaming, wait until the client
// marks the completion.
if (isCompleted(clientEvent) || !streaming) {
// Only complete the flow once.
if (!done) {
emitAggregatedEventAndClearBuffer(clientEvent);
done = true;
emitter.onComplete();
}
}
}
private void addMetadata(Event event, ClientEvent clientEvent) {
ImmutableList.Builder<CustomMetadata> eventMetadataBuilder = ImmutableList.builder();
event.customMetadata().ifPresent(eventMetadataBuilder::addAll);
if (requestJson != null) {
eventMetadataBuilder.add(
CustomMetadata.builder()
.key(A2AMetadata.Key.REQUEST.getValue())
.stringValue(requestJson)
.build());
}
try {
if (clientEvent != null) {
eventMetadataBuilder.add(
CustomMetadata.builder()
.key(A2AMetadata.Key.RESPONSE.getValue())
.stringValue(objectMapper.writeValueAsString(clientEvent))
.build());
}
} catch (JsonProcessingException e) {
// metadata serialization is not critical for agent execution, so we just log and continue.
logger.warn("Failed to serialize response metadata", e);
}
event.setCustomMetadata(eventMetadataBuilder.build());
}
/**
* Buffers the content from the event into the text and thoughts buffers.
*
* @return true if the event has content that was added to the buffer, false otherwise.
*/
private boolean bufferContent(Event event, ClientEvent clientEvent) {
if (!shouldBuffer(clientEvent)) {
return false;
}
boolean updated = false;
for (Part part : eventParts(event)) {
if (part.text().isPresent()) {
String t = part.text().get();
if (part.thought().orElse(false)) {
thoughtsBuffer.append(t);
updated = true;
} else {
textBuffer.append(t);
updated = true;
}
}
}
return updated;
}
/**
* Determines if the event should be buffered.
*
* <p>Buffering is used to aggregate content from partial events. We buffer events that can
* contain content which is streamed in chunks, like {@link MessageEvent} or {@link
* TaskArtifactUpdateEvent}. Events that do not contain content to be aggregated, like {@link
* TaskStatusUpdateEvent} or {@link TaskEvent} without artifacts, should not be buffered.
*/
private boolean shouldBuffer(ClientEvent event) {
if (event instanceof TaskUpdateEvent taskUpdateEvent) {
Object innerEvent = taskUpdateEvent.getUpdateEvent();
return !(innerEvent instanceof TaskStatusUpdateEvent);
}
if (event instanceof TaskEvent taskEvent) {
return !taskEvent.getTask().getArtifacts().isEmpty();
}
return true;
}
/**
* Determines if text buffers should be reset before processing new content.
*
* <p>When receiving artifact updates via {@link TaskArtifactUpdateEvent}, if {@code append} is
* false, it indicates the new content should replace any prior chunks. If this is not the
* {@code last_chunk}, it means we are at the beginning of receiving a new set of chunks, so we
* need to reset buffers to avoid appending to stale content from a prior update.
*/
private boolean shouldResetBuffer(ClientEvent event) {
if (event instanceof TaskUpdateEvent taskUpdateEvent) {
Object innerEvent = taskUpdateEvent.getUpdateEvent();
if (innerEvent instanceof TaskArtifactUpdateEvent artifactEvent) {
return Objects.equals(artifactEvent.isAppend(), false)
&& Objects.equals(artifactEvent.isLastChunk(), false);
}
}
return false;
}
private void clearBuffer() {
thoughtsBuffer.setLength(0);
textBuffer.setLength(0);
}
private void emitAggregatedEventAndClearBuffer(@Nullable ClientEvent triggerEvent) {
if (thoughtsBuffer.length() > 0 || textBuffer.length() > 0) {
List<Part> parts = new ArrayList<>();
if (thoughtsBuffer.length() > 0) {
parts.add(Part.builder().thought(true).text(thoughtsBuffer.toString()).build());
}
if (textBuffer.length() > 0) {
parts.add(Part.builder().text(textBuffer.toString()).build());
}
Content aggregatedContent = Content.builder().role("model").parts(parts).build();
emitter.onNext(createAggregatedEvent(aggregatedContent, triggerEvent));
clearBuffer();
}
}
private boolean mergeAggregatedContentIntoEvent(Event event) {
if (thoughtsBuffer.isEmpty() && textBuffer.isEmpty()) {
return false;
}
boolean hasContent =
event.content().isPresent()
&& !event.content().get().parts().orElse(ImmutableList.of()).isEmpty();
if (hasContent) {
return false;
}
List<Part> parts = new ArrayList<>();
if (thoughtsBuffer.length() > 0) {
parts.add(Part.builder().thought(true).text(thoughtsBuffer.toString()).build());
}
if (textBuffer.length() > 0) {
parts.add(Part.builder().text(textBuffer.toString()).build());
}
Content aggregatedContent = Content.builder().role("model").parts(parts).build();
event.setContent(aggregatedContent);
ImmutableList.Builder<CustomMetadata> newMetadata = ImmutableList.builder();
event.customMetadata().ifPresent(newMetadata::addAll);
newMetadata.add(
CustomMetadata.builder()
.key(A2AMetadata.Key.AGGREGATED.getValue())
.stringValue("true")
.build());
event.setCustomMetadata(newMetadata.build());
clearBuffer();
return true;
}
private Event createAggregatedEvent(Content content, @Nullable ClientEvent triggerEvent) {
ImmutableList.Builder<CustomMetadata> aggMetadataBuilder = ImmutableList.builder();
aggMetadataBuilder.add(
CustomMetadata.builder()
.key(A2AMetadata.Key.AGGREGATED.getValue())
.stringValue("true")
.build());
if (requestJson != null) {
aggMetadataBuilder.add(
CustomMetadata.builder()
.key(A2AMetadata.Key.REQUEST.getValue())
.stringValue(requestJson)
.build());
}
if (triggerEvent != null) {
try {
aggMetadataBuilder.add(
CustomMetadata.builder()
.key(A2AMetadata.Key.RESPONSE.getValue())
.stringValue(objectMapper.writeValueAsString(triggerEvent))
.build());
} catch (JsonProcessingException e) {
logger.warn("Failed to serialize response metadata for aggregated event", e);
}
}
return Event.builder()
.id(UUID.randomUUID().toString())
.invocationId(invocationContext.invocationId())
.author(agentName)
.content(content)
.timestamp(Instant.now().toEpochMilli())
.customMetadata(aggMetadataBuilder.build())
.build();
}
}
private static boolean isCompleted(ClientEvent event) {
TaskState executionState = TaskState.UNKNOWN;
if (event instanceof TaskEvent taskEvent) {
executionState = taskEvent.getTask().getStatus().state();
} else if (event instanceof TaskUpdateEvent updateEvent) {
executionState = updateEvent.getTask().getStatus().state();
}
return executionState.equals(TaskState.COMPLETED);
}
private static ImmutableList<Part> eventParts(Event event) {
return ImmutableList.copyOf(event.content().flatMap(Content::parts).orElse(ImmutableList.of()));
}
@Override
protected Flowable<Event> runLiveImpl(InvocationContext invocationContext) {
throw new UnsupportedOperationException(
"runLiveImpl for " + getClass() + " via A2A is not implemented.");
}
/** Exception thrown when the agent card cannot be resolved. */
public static class AgentCardResolutionError extends RuntimeException {
public AgentCardResolutionError(String message) {
super(message);
}
public AgentCardResolutionError(String message, Throwable cause) {
super(message, cause);
}
}
/** Exception thrown when a type error occurs. */
public static class TypeError extends RuntimeException {
public TypeError(String message) {
super(message);
}
}
}