Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions core/src/main/java/com/google/adk/agents/CallbackPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.google.adk.agents;

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.adk.agents.Callbacks.AfterAgentCallback;
import com.google.adk.agents.Callbacks.AfterAgentCallbackBase;
import com.google.adk.agents.Callbacks.AfterAgentCallbackSync;
Expand Down Expand Up @@ -74,7 +72,7 @@ public String getName() {

@SuppressWarnings("unchecked") // The builder ensures that the type is correct.
private <T> ImmutableList<T> getCallbacks(Class<T> type) {
return callbacks.get(type).stream().map(callback -> (T) callback).collect(toImmutableList());
return (ImmutableList<T>) callbacks.get(type);
}

public ImmutableList<? extends Callbacks.BeforeAgentCallback> getBeforeAgentCallback() {
Expand Down
126 changes: 63 additions & 63 deletions core/src/main/java/com/google/adk/events/EventActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,40 @@
@JsonDeserialize(builder = EventActions.Builder.class)
public class EventActions {

private Optional<Boolean> skipSummarization = Optional.empty();
private ConcurrentMap<String, Object> stateDelta = new ConcurrentHashMap<>();
private ConcurrentMap<String, Part> artifactDelta = new ConcurrentHashMap<>();
private Optional<String> transferToAgent = Optional.empty();
private Optional<Boolean> escalate = Optional.empty();
private ConcurrentMap<String, ConcurrentMap<String, Object>> requestedAuthConfigs =
new ConcurrentHashMap<>();
private ConcurrentMap<String, ToolConfirmation> requestedToolConfirmations =
new ConcurrentHashMap<>();
private Optional<Boolean> endInvocation = Optional.empty();
private Optional<EventCompaction> compaction = Optional.empty();
private Optional<Boolean> skipSummarization;
private ConcurrentMap<String, Object> stateDelta;
private ConcurrentMap<String, Part> artifactDelta;
private Optional<String> transferToAgent;
private Optional<Boolean> escalate;
private ConcurrentMap<String, ConcurrentMap<String, Object>> requestedAuthConfigs;
private ConcurrentMap<String, ToolConfirmation> requestedToolConfirmations;
private Optional<Boolean> endInvocation;
private Optional<EventCompaction> compaction;

/** Default constructor for Jackson. */
public EventActions() {}
public EventActions() {
this.skipSummarization = Optional.empty();
this.stateDelta = new ConcurrentHashMap<>();
this.artifactDelta = new ConcurrentHashMap<>();
this.transferToAgent = Optional.empty();
this.escalate = Optional.empty();
this.requestedAuthConfigs = new ConcurrentHashMap<>();
this.requestedToolConfirmations = new ConcurrentHashMap<>();
this.endInvocation = Optional.empty();
this.compaction = Optional.empty();
}

private EventActions(Builder builder) {
this.skipSummarization = builder.skipSummarization;
this.stateDelta = builder.stateDelta;
this.artifactDelta = builder.artifactDelta;
this.transferToAgent = builder.transferToAgent;
this.escalate = builder.escalate;
this.requestedAuthConfigs = builder.requestedAuthConfigs;
this.requestedToolConfirmations = builder.requestedToolConfirmations;
this.endInvocation = builder.endInvocation;
this.compaction = builder.compaction;
}

@JsonProperty("skipSummarization")
public Optional<Boolean> skipSummarization() {
Expand Down Expand Up @@ -191,19 +211,27 @@ public int hashCode() {

/** Builder for {@link EventActions}. */
public static class Builder {
private Optional<Boolean> skipSummarization = Optional.empty();
private ConcurrentMap<String, Object> stateDelta = new ConcurrentHashMap<>();
private ConcurrentMap<String, Part> artifactDelta = new ConcurrentHashMap<>();
private Optional<String> transferToAgent = Optional.empty();
private Optional<Boolean> escalate = Optional.empty();
private ConcurrentMap<String, ConcurrentMap<String, Object>> requestedAuthConfigs =
new ConcurrentHashMap<>();
private ConcurrentMap<String, ToolConfirmation> requestedToolConfirmations =
new ConcurrentHashMap<>();
private Optional<Boolean> endInvocation = Optional.empty();
private Optional<EventCompaction> compaction = Optional.empty();

public Builder() {}
private Optional<Boolean> skipSummarization;
private ConcurrentMap<String, Object> stateDelta;
private ConcurrentMap<String, Part> artifactDelta;
private Optional<String> transferToAgent;
private Optional<Boolean> escalate;
private ConcurrentMap<String, ConcurrentMap<String, Object>> requestedAuthConfigs;
private ConcurrentMap<String, ToolConfirmation> requestedToolConfirmations;
private Optional<Boolean> endInvocation;
private Optional<EventCompaction> compaction;

public Builder() {
this.skipSummarization = Optional.empty();
this.stateDelta = new ConcurrentHashMap<>();
this.artifactDelta = new ConcurrentHashMap<>();
this.transferToAgent = Optional.empty();
this.escalate = Optional.empty();
this.requestedAuthConfigs = new ConcurrentHashMap<>();
this.requestedToolConfirmations = new ConcurrentHashMap<>();
this.endInvocation = Optional.empty();
this.compaction = Optional.empty();
}

private Builder(EventActions eventActions) {
this.skipSummarization = eventActions.skipSummarization();
Expand Down Expand Up @@ -284,48 +312,20 @@ public Builder compaction(EventCompaction value) {

@CanIgnoreReturnValue
public Builder merge(EventActions other) {
if (other.skipSummarization().isPresent()) {
this.skipSummarization = other.skipSummarization();
}
if (other.stateDelta() != null) {
this.stateDelta.putAll(other.stateDelta());
}
if (other.artifactDelta() != null) {
this.artifactDelta.putAll(other.artifactDelta());
}
if (other.transferToAgent().isPresent()) {
this.transferToAgent = other.transferToAgent();
}
if (other.escalate().isPresent()) {
this.escalate = other.escalate();
}
if (other.requestedAuthConfigs() != null) {
this.requestedAuthConfigs.putAll(other.requestedAuthConfigs());
}
if (other.requestedToolConfirmations() != null) {
this.requestedToolConfirmations.putAll(other.requestedToolConfirmations());
}
if (other.endInvocation().isPresent()) {
this.endInvocation = other.endInvocation();
}
if (other.compaction().isPresent()) {
this.compaction = other.compaction();
}
other.skipSummarization().ifPresent(this::skipSummarization);
this.stateDelta.putAll(other.stateDelta());
this.artifactDelta.putAll(other.artifactDelta());
other.transferToAgent().ifPresent(this::transferToAgent);
other.escalate().ifPresent(this::escalate);
this.requestedAuthConfigs.putAll(other.requestedAuthConfigs());
this.requestedToolConfirmations.putAll(other.requestedToolConfirmations());
other.endInvocation().ifPresent(this::endInvocation);
other.compaction().ifPresent(this::compaction);
return this;
}

public EventActions build() {
EventActions eventActions = new EventActions();
eventActions.setSkipSummarization(this.skipSummarization);
eventActions.setStateDelta(this.stateDelta);
eventActions.setArtifactDelta(this.artifactDelta);
eventActions.setTransferToAgent(this.transferToAgent);
eventActions.setEscalate(this.escalate);
eventActions.setRequestedAuthConfigs(this.requestedAuthConfigs);
eventActions.setRequestedToolConfirmations(this.requestedToolConfirmations);
eventActions.setEndInvocation(this.endInvocation);
eventActions.setCompaction(this.compaction);
return eventActions;
return new EventActions(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public static Set<String> getLongRunningFunctionCalls(
continue;
}
BaseTool tool = tools.get(functionCall.name().get());
if (tool.longRunning()) {
if (tool != null && tool.longRunning()) {
longRunningFunctionCalls.add(functionCall.id().orElse(""));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
InvocationContext invocationContext, LlmRequest llmRequest) {
ImmutableList<Event> events = ImmutableList.copyOf(invocationContext.session().events());
if (events.isEmpty()) {
logger.info(
logger.trace(
"No events are present in the session. Skipping request confirmation processing.");
return Single.just(RequestProcessingResult.create(llmRequest, ImmutableList.of()));
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
}
}
if (responses.isEmpty()) {
logger.info("No request confirmation function responses found.");
logger.trace("No request confirmation function responses found.");
return Single.just(RequestProcessingResult.create(llmRequest, ImmutableList.of()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public Maybe<Session> getSession(
Session sessionCopy = copySession(storedSession);

// Apply filtering based on config directly to the mutable list in the copy
GetSessionConfig config = configOpt.orElse(GetSessionConfig.builder().build());
GetSessionConfig config = configOpt.orElseGet(() -> GetSessionConfig.builder().build());
List<Event> eventsInCopy = sessionCopy.events();

config
Expand Down Expand Up @@ -257,8 +257,8 @@ public Single<Event> appendEvent(Session session, Event event) {

// --- Update the session stored in this service ---
sessions
.getOrDefault(appName, new ConcurrentHashMap<>())
.getOrDefault(userId, new ConcurrentHashMap<>())
.computeIfAbsent(appName, k -> new ConcurrentHashMap<>())
.computeIfAbsent(userId, k -> new ConcurrentHashMap<>())
.put(sessionId, session);

mergeWithGlobalState(appName, userId, session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ static String convertEventToJson(Event event) {
actionsJson.put("transferAgent", event.actions().transferToAgent());
actionsJson.put("escalate", event.actions().escalate());
actionsJson.put("requestedAuthConfigs", event.actions().requestedAuthConfigs());
actionsJson.put("requestedToolConfirmations", event.actions().requestedToolConfirmations());
actionsJson.put("endInvocation", event.actions().endInvocation());
actionsJson.put("compaction", event.actions().compaction());
eventJson.put("actions", actionsJson);
}
if (event.content().isPresent()) {
Expand Down Expand Up @@ -146,26 +149,25 @@ private static Content convertMapToContent(Object rawContentValue) {
*/
@SuppressWarnings("unchecked")
static Event fromApiEvent(Map<String, Object> apiEvent) {
EventActions eventActions = new EventActions();
EventActions.Builder eventActionsBuilder = EventActions.builder();
if (apiEvent.get("actions") != null) {
Map<String, Object> actionsMap = (Map<String, Object>) apiEvent.get("actions");
eventActions.setSkipSummarization(
Optional.ofNullable(actionsMap.get("skipSummarization")).map(value -> (Boolean) value));
eventActions.setStateDelta(
if (actionsMap.get("skipSummarization") != null) {
eventActionsBuilder.skipSummarization((Boolean) actionsMap.get("skipSummarization"));
}
eventActionsBuilder.stateDelta(
actionsMap.get("stateDelta") != null
? new ConcurrentHashMap<>((Map<String, Object>) actionsMap.get("stateDelta"))
: new ConcurrentHashMap<>());
eventActions.setArtifactDelta(
eventActionsBuilder.artifactDelta(
actionsMap.get("artifactDelta") != null
? convertToArtifactDeltaMap(actionsMap.get("artifactDelta"))
: new ConcurrentHashMap<>());
eventActions.setTransferToAgent(
actionsMap.get("transferAgent") != null
? (String) actionsMap.get("transferAgent")
: null);
eventActions.setEscalate(
Optional.ofNullable(actionsMap.get("escalate")).map(value -> (Boolean) value));
eventActions.setRequestedAuthConfigs(
eventActionsBuilder.transferToAgent((String) actionsMap.get("transferAgent"));
if (actionsMap.get("escalate") != null) {
eventActionsBuilder.escalate((Boolean) actionsMap.get("escalate"));
}
eventActionsBuilder.requestedAuthConfigs(
Optional.ofNullable(actionsMap.get("requestedAuthConfigs"))
.map(SessionJsonConverter::asConcurrentMapOfConcurrentMaps)
.orElse(new ConcurrentHashMap<>()));
Expand All @@ -176,7 +178,7 @@ static Event fromApiEvent(Map<String, Object> apiEvent) {
.id((String) Iterables.getLast(Splitter.on('/').split(apiEvent.get("name").toString())))
.invocationId((String) apiEvent.get("invocationId"))
.author((String) apiEvent.get("author"))
.actions(eventActions)
.actions(eventActionsBuilder.build())
.content(
Optional.ofNullable(apiEvent.get("content"))
.map(SessionJsonConverter::convertMapToContent)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/google/adk/tools/AgentTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Optional<FunctionDeclaration> declaration() {
public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContext toolContext) {

if (this.skipSummarization) {
toolContext.actions().setSkipSummarization(true);
toolContext.setActions(toolContext.actions().toBuilder().skipSummarization(true).build());
}

Optional<Schema> agentInputSchema = Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ public final class SessionJsonConverterTest {

@Test
public void convertEventToJson_fullEvent_success() throws JsonProcessingException {
EventActions actions = new EventActions();
actions.setSkipSummarization(Optional.of(true));
actions.setStateDelta(new ConcurrentHashMap<>(ImmutableMap.of("key", "value")));
actions.setArtifactDelta(
new ConcurrentHashMap<>(ImmutableMap.of("artifact", Part.fromText("artifact_text"))));
actions.setTransferToAgent("agent");
actions.setEscalate(Optional.of(true));
EventActions actions =
EventActions.builder()
.skipSummarization(true)
.stateDelta(new ConcurrentHashMap<>(ImmutableMap.of("key", "value")))
.artifactDelta(
new ConcurrentHashMap<>(
ImmutableMap.of("artifact", Part.fromText("artifact_text"))))
.transferToAgent("agent")
.escalate(true)
.build();

Event event =
Event.builder()
Expand Down