|
18 | 18 | import static com.google.common.collect.ImmutableList.toImmutableList; |
19 | 19 |
|
20 | 20 | import com.google.adk.agents.InvocationContext; |
| 21 | +import com.google.adk.events.Event; |
21 | 22 | import com.google.common.collect.ImmutableList; |
| 23 | +import com.google.common.collect.Iterables; |
22 | 24 | import com.google.genai.types.Content; |
23 | | -import io.a2a.spec.Message; |
| 25 | +import com.google.genai.types.FunctionResponse; |
24 | 26 | import io.a2a.spec.Part; |
25 | 27 | import java.util.Collection; |
| 28 | +import java.util.List; |
26 | 29 | import java.util.Optional; |
27 | 30 | import java.util.UUID; |
28 | | -import org.slf4j.Logger; |
29 | | -import org.slf4j.LoggerFactory; |
| 31 | +import org.jspecify.annotations.Nullable; |
30 | 32 |
|
31 | 33 | /** Converter for ADK Events to A2A Messages. */ |
32 | 34 | public final class EventConverter { |
33 | | - private static final Logger logger = LoggerFactory.getLogger(EventConverter.class); |
| 35 | + public static final String ADK_TASK_ID_KEY = "adk_task_id"; |
| 36 | + public static final String ADK_CONTEXT_ID_KEY = "adk_context_id"; |
34 | 37 |
|
35 | 38 | private EventConverter() {} |
36 | 39 |
|
37 | 40 | /** |
38 | | - * Converts an ADK InvocationContext to an A2A Message. |
| 41 | + * Returns the task ID from the event. |
39 | 42 | * |
40 | | - * <p>It combines all the events in the session, plus the user content, converted into A2A Parts, |
41 | | - * into a single A2A Message. |
| 43 | + * <p>Task ID is stored in the event's custom metadata with the key {@link #ADK_TASK_ID_KEY}. |
42 | 44 | * |
43 | | - * <p>If the context has no events, or no suitable content to build the message, an empty optional |
44 | | - * is returned. |
45 | | - * |
46 | | - * @param context The ADK InvocationContext to convert. |
47 | | - * @return The converted A2A Message. |
| 45 | + * @param event The event to get the task ID from. |
| 46 | + * @return The task ID, or an empty string if not found. |
48 | 47 | */ |
49 | | - public static Optional<Message> convertEventsToA2AMessage(InvocationContext context) { |
50 | | - if (context.session().events().isEmpty()) { |
51 | | - logger.warn("No events in session, cannot convert to A2A message."); |
52 | | - return Optional.empty(); |
53 | | - } |
54 | | - |
55 | | - ImmutableList.Builder<Part<?>> partsBuilder = ImmutableList.builder(); |
| 48 | + public static String taskId(Event event) { |
| 49 | + return metadataValue(event, ADK_TASK_ID_KEY); |
| 50 | + } |
56 | 51 |
|
57 | | - context |
58 | | - .session() |
59 | | - .events() |
60 | | - .forEach( |
61 | | - event -> |
62 | | - partsBuilder.addAll( |
63 | | - contentToParts(event.content(), event.partial().orElse(false)))); |
64 | | - partsBuilder.addAll(contentToParts(context.userContent(), false)); |
| 52 | + /** |
| 53 | + * Returns the context ID from the event. |
| 54 | + * |
| 55 | + * <p>Context ID is stored in the event's custom metadata with the key {@link |
| 56 | + * #ADK_CONTEXT_ID_KEY}. |
| 57 | + * |
| 58 | + * @param event The event to get the context ID from. |
| 59 | + * @return The context ID, or an empty string if not found. |
| 60 | + */ |
| 61 | + public static String contextId(Event event) { |
| 62 | + return metadataValue(event, ADK_CONTEXT_ID_KEY); |
| 63 | + } |
65 | 64 |
|
66 | | - ImmutableList<Part<?>> parts = partsBuilder.build(); |
| 65 | + /** |
| 66 | + * Returns the last user function call event from the list of events. |
| 67 | + * |
| 68 | + * @param events The list of events to find the user function call event from. |
| 69 | + * @return The user function call event, or null if not found. |
| 70 | + */ |
| 71 | + public static @Nullable Event findUserFunctionCall(List<Event> events) { |
| 72 | + Event candidate = Iterables.getLast(events); |
| 73 | + if (!candidate.author().equals("user")) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + FunctionResponse functionResponse = findUserFunctionResponse(candidate); |
| 77 | + if (functionResponse == null || functionResponse.id().isEmpty()) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + for (int i = events.size() - 2; i >= 0; i--) { |
| 81 | + Event event = events.get(i); |
| 82 | + if (isUserFunctionCall(event, functionResponse.id().get())) { |
| 83 | + return event; |
| 84 | + } |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
67 | 88 |
|
68 | | - if (parts.isEmpty()) { |
69 | | - logger.warn("No suitable content found to build A2A request message."); |
70 | | - return Optional.empty(); |
| 89 | + private static @Nullable FunctionResponse findUserFunctionResponse(Event candidate) { |
| 90 | + if (candidate.content().isEmpty() || candidate.content().get().parts().isEmpty()) { |
| 91 | + return null; |
71 | 92 | } |
| 93 | + return candidate.content().get().parts().get().stream() |
| 94 | + .filter(part -> part.functionResponse().isPresent()) |
| 95 | + .findFirst() |
| 96 | + .map(part -> part.functionResponse().get()) |
| 97 | + .orElse(null); |
| 98 | + } |
72 | 99 |
|
73 | | - return Optional.of( |
74 | | - new Message.Builder() |
75 | | - .messageId(UUID.randomUUID().toString()) |
76 | | - .parts(parts) |
77 | | - .role(Message.Role.USER) |
78 | | - .build()); |
| 100 | + private static boolean isUserFunctionCall(Event event, String functionResponseId) { |
| 101 | + if (event.content().isEmpty()) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + return event.content().get().parts().get().stream() |
| 105 | + .anyMatch( |
| 106 | + part -> |
| 107 | + part.functionCall().isPresent() |
| 108 | + && part.functionCall() |
| 109 | + .get() |
| 110 | + .id() |
| 111 | + .map(id -> id.equals(functionResponseId)) |
| 112 | + .orElse(false)); |
79 | 113 | } |
80 | 114 |
|
| 115 | + /** |
| 116 | + * Converts a GenAI Content object to a list of A2A Parts. |
| 117 | + * |
| 118 | + * @param content The GenAI Content object to convert. |
| 119 | + * @param isPartial Whether the content is partial. |
| 120 | + * @return A list of A2A Parts. |
| 121 | + */ |
81 | 122 | public static ImmutableList<Part<?>> contentToParts( |
82 | 123 | Optional<Content> content, boolean isPartial) { |
83 | 124 | return content.flatMap(Content::parts).stream() |
84 | 125 | .flatMap(Collection::stream) |
85 | 126 | .map(part -> PartConverter.fromGenaiPart(part, isPartial)) |
86 | 127 | .collect(toImmutableList()); |
87 | 128 | } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns the parts from the context events that should be sent to the agent. |
| 132 | + * |
| 133 | + * <p>All session events from the previous remote agent response (or the beginning of the session |
| 134 | + * in case of the first agent invocation) are included into the A2A message. Events from other |
| 135 | + * agents are presented as user messages and rephased as if a user was telling what happened in |
| 136 | + * the session up to the point. |
| 137 | + * |
| 138 | + * @param context The invocation context to get the parts from. |
| 139 | + * @return A list of A2A Parts. |
| 140 | + */ |
| 141 | + public static ImmutableList<Part<?>> messagePartsFromContext(InvocationContext context) { |
| 142 | + if (context.session().events().isEmpty()) { |
| 143 | + return ImmutableList.of(); |
| 144 | + } |
| 145 | + List<Event> events = context.session().events(); |
| 146 | + int lastResponseIndex = -1; |
| 147 | + String contextId = ""; |
| 148 | + for (int i = events.size() - 1; i >= 0; i--) { |
| 149 | + Event event = events.get(i); |
| 150 | + if (event.author().equals(context.agent().name())) { |
| 151 | + lastResponseIndex = i; |
| 152 | + contextId = contextId(event); |
| 153 | + break; |
| 154 | + } |
| 155 | + } |
| 156 | + ImmutableList.Builder<Part<?>> partsBuilder = ImmutableList.builder(); |
| 157 | + for (int i = lastResponseIndex + 1; i < events.size(); i++) { |
| 158 | + Event event = events.get(i); |
| 159 | + if (!event.author().equals("user") && !event.author().equals(context.agent().name())) { |
| 160 | + event = presentAsUserMessage(event, contextId); |
| 161 | + } |
| 162 | + contentToParts(event.content(), event.partial().orElse(false)).forEach(partsBuilder::add); |
| 163 | + } |
| 164 | + return partsBuilder.build(); |
| 165 | + } |
| 166 | + |
| 167 | + private static Event presentAsUserMessage(Event event, String contextId) { |
| 168 | + Event.Builder userEvent = |
| 169 | + new Event.Builder().id(UUID.randomUUID().toString()).invocationId(contextId).author("user"); |
| 170 | + ImmutableList<com.google.genai.types.Part> parts = |
| 171 | + event.content().flatMap(Content::parts).stream() |
| 172 | + .flatMap(Collection::stream) |
| 173 | + // convert only non-thought parts to user message parts, skip thought parts as they are |
| 174 | + // not meant to be shown to the user |
| 175 | + .filter(part -> !part.thought().orElse(false)) |
| 176 | + .map(part -> PartConverter.remoteCallAsUserPart(event.author(), part)) |
| 177 | + .collect(toImmutableList()); |
| 178 | + if (parts.isEmpty()) { |
| 179 | + return userEvent.build(); |
| 180 | + } |
| 181 | + com.google.genai.types.Part forContext = |
| 182 | + com.google.genai.types.Part.builder().text("For context:").build(); |
| 183 | + return userEvent |
| 184 | + .content( |
| 185 | + Content.builder() |
| 186 | + .parts( |
| 187 | + ImmutableList.<com.google.genai.types.Part>builder() |
| 188 | + .add(forContext) |
| 189 | + .addAll(parts) |
| 190 | + .build()) |
| 191 | + .build()) |
| 192 | + .build(); |
| 193 | + } |
| 194 | + |
| 195 | + private static String metadataValue(Event event, String key) { |
| 196 | + if (event.customMetadata().isEmpty()) { |
| 197 | + return ""; |
| 198 | + } |
| 199 | + return event.customMetadata().get().stream() |
| 200 | + .filter(m -> m.key().map(k -> k.equals(key)).orElse(false)) |
| 201 | + .findFirst() |
| 202 | + .flatMap(m -> m.stringValue()) |
| 203 | + .orElse(""); |
| 204 | + } |
88 | 205 | } |
0 commit comments