From 11ce49ec456b7eba81b7a7a29461e9716d4e5260 Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Fri, 6 Mar 2026 10:01:08 -0800 Subject: [PATCH 01/25] chore: switch release please secret to use adk-java-releases-bot's token PiperOrigin-RevId: 879687277 --- .github/workflows/release-please.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release-please.yaml b/.github/workflows/release-please.yaml index 6d3142907..258cf90db 100644 --- a/.github/workflows/release-please.yaml +++ b/.github/workflows/release-please.yaml @@ -14,4 +14,4 @@ jobs: steps: - uses: googleapis/release-please-action@v4 with: - token: ${{ secrets.GITHUB_TOKEN }} + token: ${{ secrets.RELEASE_PLEASE_TOKEN }} From 67b602f245f564238ea22298a37bf70049e56a12 Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Fri, 6 Mar 2026 11:01:57 -0800 Subject: [PATCH 02/25] feat!: use @Nullable fields in Event class PiperOrigin-RevId: 879717644 --- .../com/google/adk/a2a/RemoteA2AAgent.java | 2 +- .../adk/a2a/converters/ResponseConverter.java | 2 +- .../adk/a2a/executor/AgentExecutorTest.java | 6 +- .../java/com/google/adk/agents/BaseAgent.java | 4 +- .../java/com/google/adk/events/Event.java | 224 ++++++------------ .../adk/flows/llmflows/BaseLlmFlow.java | 28 +-- .../adk/flows/llmflows/CodeExecution.java | 6 +- .../google/adk/flows/llmflows/Contents.java | 4 +- .../google/adk/flows/llmflows/Functions.java | 10 +- .../java/com/google/adk/runner/Runner.java | 4 +- .../adk/sessions/SessionJsonConverter.java | 9 +- .../java/com/google/adk/events/EventTest.java | 2 + .../adk/flows/llmflows/ContentsTest.java | 4 +- .../adk/flows/llmflows/FunctionsTest.java | 8 +- .../google/adk/plugins/LoggingPluginTest.java | 2 - .../sessions/SessionJsonConverterTest.java | 7 +- 16 files changed, 113 insertions(+), 209 deletions(-) diff --git a/a2a/src/main/java/com/google/adk/a2a/RemoteA2AAgent.java b/a2a/src/main/java/com/google/adk/a2a/RemoteA2AAgent.java index b8ff39808..b391f2985 100644 --- a/a2a/src/main/java/com/google/adk/a2a/RemoteA2AAgent.java +++ b/a2a/src/main/java/com/google/adk/a2a/RemoteA2AAgent.java @@ -436,7 +436,7 @@ private boolean mergeAggregatedContentIntoEvent(Event event) { } Content aggregatedContent = Content.builder().role("model").parts(parts).build(); - event.setContent(Optional.of(aggregatedContent)); + event.setContent(aggregatedContent); ImmutableList.Builder newMetadata = ImmutableList.builder(); event.customMetadata().ifPresent(newMetadata::addAll); diff --git a/a2a/src/main/java/com/google/adk/a2a/converters/ResponseConverter.java b/a2a/src/main/java/com/google/adk/a2a/converters/ResponseConverter.java index ccbb1b9cf..57a84b58f 100644 --- a/a2a/src/main/java/com/google/adk/a2a/converters/ResponseConverter.java +++ b/a2a/src/main/java/com/google/adk/a2a/converters/ResponseConverter.java @@ -76,7 +76,7 @@ private static Optional handleTaskUpdate( boolean isLastChunk = Objects.equals(artifactEvent.isLastChunk(), true); Event eventPart = artifactToEvent(artifactEvent.getArtifact(), context); - eventPart.setPartial(Optional.of(isAppend || !isLastChunk)); + eventPart.setPartial(isAppend || !isLastChunk); // append=true, lastChunk=false: emit as partial, update aggregation // append=false, lastChunk=false: emit as partial, reset aggregation // append=true, lastChunk=true: emit as partial, update aggregation and emit as non-partial diff --git a/a2a/src/test/java/com/google/adk/a2a/executor/AgentExecutorTest.java b/a2a/src/test/java/com/google/adk/a2a/executor/AgentExecutorTest.java index 5570f40d0..647aaf21f 100644 --- a/a2a/src/test/java/com/google/adk/a2a/executor/AgentExecutorTest.java +++ b/a2a/src/test/java/com/google/adk/a2a/executor/AgentExecutorTest.java @@ -361,7 +361,7 @@ private RequestContext createRequestContext() { public void process_statefulAggregation_tracksArtifactIdAndAppendForAuthor() { Event partial1 = Event.builder() - .partial(Optional.of(true)) + .partial(true) .author("agent_author") .content( Content.builder() @@ -370,7 +370,7 @@ public void process_statefulAggregation_tracksArtifactIdAndAppendForAuthor() { .build(); Event partial2 = Event.builder() - .partial(Optional.of(true)) + .partial(true) .author("agent_author") .content( Content.builder() @@ -379,7 +379,7 @@ public void process_statefulAggregation_tracksArtifactIdAndAppendForAuthor() { .build(); Event finalEvent = Event.builder() - .partial(Optional.of(false)) + .partial(false) .author("agent_author") .content( Content.builder() diff --git a/core/src/main/java/com/google/adk/agents/BaseAgent.java b/core/src/main/java/com/google/adk/agents/BaseAgent.java index 226e61abe..d74ba9ca5 100644 --- a/core/src/main/java/com/google/adk/agents/BaseAgent.java +++ b/core/src/main/java/com/google/adk/agents/BaseAgent.java @@ -409,7 +409,7 @@ private Single> callCallback( .id(Event.generateEventId()) .invocationId(invocationContext.invocationId()) .author(name()) - .branch(invocationContext.branch()) + .branch(invocationContext.branch().orElse(null)) .actions(callbackContext.eventActions()) .content(content) .build()); @@ -426,7 +426,7 @@ private Single> callCallback( .id(Event.generateEventId()) .invocationId(invocationContext.invocationId()) .author(name()) - .branch(invocationContext.branch()) + .branch(invocationContext.branch().orElse(null)) .actions(callbackContext.eventActions()); return Single.just(Optional.of(eventBuilder.build())); diff --git a/core/src/main/java/com/google/adk/events/Event.java b/core/src/main/java/com/google/adk/events/Event.java index 91dc79a56..2677b635d 100644 --- a/core/src/main/java/com/google/adk/events/Event.java +++ b/core/src/main/java/com/google/adk/events/Event.java @@ -49,21 +49,21 @@ public class Event extends JsonBaseModel { private String id; private String invocationId; private String author; - private Optional content = Optional.empty(); + private @Nullable Content content; private EventActions actions; - private Optional> longRunningToolIds = Optional.empty(); - private Optional partial = Optional.empty(); - private Optional turnComplete = Optional.empty(); - private Optional errorCode = Optional.empty(); - private Optional errorMessage = Optional.empty(); - private Optional finishReason = Optional.empty(); - private Optional usageMetadata = Optional.empty(); - private Optional avgLogprobs = Optional.empty(); - private Optional interrupted = Optional.empty(); - private Optional branch = Optional.empty(); - private Optional groundingMetadata = Optional.empty(); - private Optional> customMetadata = Optional.empty(); - private Optional modelVersion = Optional.empty(); + private @Nullable Set longRunningToolIds; + private @Nullable Boolean partial; + private @Nullable Boolean turnComplete; + private @Nullable FinishReason errorCode; + private @Nullable String errorMessage; + private @Nullable FinishReason finishReason; + private @Nullable GenerateContentResponseUsageMetadata usageMetadata; + private @Nullable Double avgLogprobs; + private @Nullable Boolean interrupted; + private @Nullable String branch; + private @Nullable GroundingMetadata groundingMetadata; + private @Nullable List customMetadata; + private @Nullable String modelVersion; private long timestamp; private Event() {} @@ -104,10 +104,10 @@ public void setAuthor(String author) { @JsonProperty("content") public Optional content() { - return content; + return Optional.ofNullable(content); } - public void setContent(Optional content) { + public void setContent(@Nullable Content content) { this.content = content; } @@ -126,10 +126,10 @@ public void setActions(EventActions actions) { */ @JsonProperty("longRunningToolIds") public Optional> longRunningToolIds() { - return longRunningToolIds; + return Optional.ofNullable(longRunningToolIds); } - public void setLongRunningToolIds(Optional> longRunningToolIds) { + public void setLongRunningToolIds(@Nullable Set longRunningToolIds) { this.longRunningToolIds = longRunningToolIds; } @@ -139,73 +139,79 @@ public void setLongRunningToolIds(Optional> longRunningToolIds) { */ @JsonProperty("partial") public Optional partial() { - return partial; + return Optional.ofNullable(partial); } - public void setPartial(Optional partial) { + public void setPartial(@Nullable Boolean partial) { this.partial = partial; } @JsonProperty("turnComplete") public Optional turnComplete() { - return turnComplete; + return Optional.ofNullable(turnComplete); } - public void setTurnComplete(Optional turnComplete) { + public void setTurnComplete(@Nullable Boolean turnComplete) { this.turnComplete = turnComplete; } @JsonProperty("errorCode") public Optional errorCode() { - return errorCode; + return Optional.ofNullable(errorCode); } @JsonProperty("finishReason") public Optional finishReason() { - return finishReason; + return Optional.ofNullable(finishReason); } - public void setErrorCode(Optional errorCode) { + public void setErrorCode(@Nullable FinishReason errorCode) { this.errorCode = errorCode; } + @Deprecated + @SuppressWarnings("checkstyle:IllegalType") public void setFinishReason(Optional finishReason) { + this.finishReason = finishReason.orElse(null); + } + + public void setFinishReason(@Nullable FinishReason finishReason) { this.finishReason = finishReason; } @JsonProperty("errorMessage") public Optional errorMessage() { - return errorMessage; + return Optional.ofNullable(errorMessage); } - public void setErrorMessage(Optional errorMessage) { + public void setErrorMessage(@Nullable String errorMessage) { this.errorMessage = errorMessage; } @JsonProperty("usageMetadata") public Optional usageMetadata() { - return usageMetadata; + return Optional.ofNullable(usageMetadata); } - public void setUsageMetadata(Optional usageMetadata) { + public void setUsageMetadata(@Nullable GenerateContentResponseUsageMetadata usageMetadata) { this.usageMetadata = usageMetadata; } @JsonProperty("avgLogprobs") public Optional avgLogprobs() { - return avgLogprobs; + return Optional.ofNullable(avgLogprobs); } - public void setAvgLogprobs(Optional avgLogprobs) { + public void setAvgLogprobs(@Nullable Double avgLogprobs) { this.avgLogprobs = avgLogprobs; } @JsonProperty("interrupted") public Optional interrupted() { - return interrupted; + return Optional.ofNullable(interrupted); } - public void setInterrupted(Optional interrupted) { + public void setInterrupted(@Nullable Boolean interrupted) { this.interrupted = interrupted; } @@ -216,7 +222,7 @@ public void setInterrupted(Optional interrupted) { */ @JsonProperty("branch") public Optional branch() { - return branch; + return Optional.ofNullable(branch); } /** @@ -227,40 +233,36 @@ public Optional branch() { * @param branch Branch identifier. */ public void branch(@Nullable String branch) { - this.branch = Optional.ofNullable(branch); - } - - public void branch(Optional branch) { this.branch = branch; } /** The grounding metadata of the event. */ @JsonProperty("groundingMetadata") public Optional groundingMetadata() { - return groundingMetadata; + return Optional.ofNullable(groundingMetadata); } - public void setGroundingMetadata(Optional groundingMetadata) { + public void setGroundingMetadata(@Nullable GroundingMetadata groundingMetadata) { this.groundingMetadata = groundingMetadata; } /** The custom metadata of the event. */ @JsonProperty("customMetadata") public Optional> customMetadata() { - return customMetadata; + return Optional.ofNullable(customMetadata); } public void setCustomMetadata(@Nullable List customMetadata) { - this.customMetadata = Optional.ofNullable(customMetadata); + this.customMetadata = customMetadata; } /** The model version used to generate the response. */ @JsonProperty("modelVersion") public Optional modelVersion() { - return modelVersion; + return Optional.ofNullable(modelVersion); } - public void setModelVersion(Optional modelVersion) { + public void setModelVersion(@Nullable String modelVersion) { this.modelVersion = modelVersion; } @@ -345,22 +347,22 @@ public static class Builder { private String id; private String invocationId; private String author; - private Optional content = Optional.empty(); + private @Nullable Content content; private EventActions actions; - private Optional> longRunningToolIds = Optional.empty(); - private Optional partial = Optional.empty(); - private Optional turnComplete = Optional.empty(); - private Optional errorCode = Optional.empty(); - private Optional errorMessage = Optional.empty(); - private Optional finishReason = Optional.empty(); - private Optional usageMetadata = Optional.empty(); - private Optional avgLogprobs = Optional.empty(); - private Optional interrupted = Optional.empty(); - private Optional branch = Optional.empty(); - private Optional groundingMetadata = Optional.empty(); - private Optional> customMetadata = Optional.empty(); - private Optional modelVersion = Optional.empty(); - private Optional timestamp = Optional.empty(); + private @Nullable Set longRunningToolIds; + private @Nullable Boolean partial; + private @Nullable Boolean turnComplete; + private @Nullable FinishReason errorCode; + private @Nullable String errorMessage; + private @Nullable FinishReason finishReason; + private @Nullable GenerateContentResponseUsageMetadata usageMetadata; + private @Nullable Double avgLogprobs; + private @Nullable Boolean interrupted; + private @Nullable String branch; + private @Nullable GroundingMetadata groundingMetadata; + private @Nullable List customMetadata; + private @Nullable String modelVersion; + private @Nullable Long timestamp; @JsonCreator private static Builder create() { @@ -391,12 +393,6 @@ public Builder author(String value) { @CanIgnoreReturnValue @JsonProperty("content") public Builder content(@Nullable Content value) { - this.content = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder content(Optional value) { this.content = value; return this; } @@ -415,12 +411,6 @@ Optional actions() { @CanIgnoreReturnValue @JsonProperty("longRunningToolIds") public Builder longRunningToolIds(@Nullable Set value) { - this.longRunningToolIds = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder longRunningToolIds(Optional> value) { this.longRunningToolIds = value; return this; } @@ -428,12 +418,6 @@ public Builder longRunningToolIds(Optional> value) { @CanIgnoreReturnValue @JsonProperty("partial") public Builder partial(@Nullable Boolean value) { - this.partial = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder partial(Optional value) { this.partial = value; return this; } @@ -441,12 +425,6 @@ public Builder partial(Optional value) { @CanIgnoreReturnValue @JsonProperty("turnComplete") public Builder turnComplete(@Nullable Boolean value) { - this.turnComplete = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder turnComplete(Optional value) { this.turnComplete = value; return this; } @@ -454,12 +432,6 @@ public Builder turnComplete(Optional value) { @CanIgnoreReturnValue @JsonProperty("errorCode") public Builder errorCode(@Nullable FinishReason value) { - this.errorCode = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder errorCode(Optional value) { this.errorCode = value; return this; } @@ -467,12 +439,6 @@ public Builder errorCode(Optional value) { @CanIgnoreReturnValue @JsonProperty("errorMessage") public Builder errorMessage(@Nullable String value) { - this.errorMessage = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder errorMessage(Optional value) { this.errorMessage = value; return this; } @@ -480,12 +446,6 @@ public Builder errorMessage(Optional value) { @CanIgnoreReturnValue @JsonProperty("finishReason") public Builder finishReason(@Nullable FinishReason value) { - this.finishReason = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder finishReason(Optional value) { this.finishReason = value; return this; } @@ -493,12 +453,6 @@ public Builder finishReason(Optional value) { @CanIgnoreReturnValue @JsonProperty("usageMetadata") public Builder usageMetadata(@Nullable GenerateContentResponseUsageMetadata value) { - this.usageMetadata = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder usageMetadata(Optional value) { this.usageMetadata = value; return this; } @@ -506,12 +460,6 @@ public Builder usageMetadata(Optional valu @CanIgnoreReturnValue @JsonProperty("avgLogprobs") public Builder avgLogprobs(@Nullable Double value) { - this.avgLogprobs = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder avgLogprobs(Optional value) { this.avgLogprobs = value; return this; } @@ -519,12 +467,6 @@ public Builder avgLogprobs(Optional value) { @CanIgnoreReturnValue @JsonProperty("interrupted") public Builder interrupted(@Nullable Boolean value) { - this.interrupted = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder interrupted(Optional value) { this.interrupted = value; return this; } @@ -532,84 +474,52 @@ public Builder interrupted(Optional value) { @CanIgnoreReturnValue @JsonProperty("timestamp") public Builder timestamp(long value) { - this.timestamp = Optional.of(value); - return this; - } - - @CanIgnoreReturnValue - public Builder timestamp(Optional value) { this.timestamp = value; return this; } // Getter for builder's timestamp, used in build() Optional timestamp() { - return timestamp; + return Optional.ofNullable(timestamp); } @CanIgnoreReturnValue @JsonProperty("branch") public Builder branch(@Nullable String value) { - this.branch = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder branch(Optional value) { this.branch = value; return this; } // Getter for builder's branch, used in build() Optional branch() { - return branch; + return Optional.ofNullable(branch); } @CanIgnoreReturnValue @JsonProperty("groundingMetadata") public Builder groundingMetadata(@Nullable GroundingMetadata value) { - this.groundingMetadata = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder groundingMetadata(Optional value) { this.groundingMetadata = value; return this; } Optional groundingMetadata() { - return groundingMetadata; + return Optional.ofNullable(groundingMetadata); } @CanIgnoreReturnValue @JsonProperty("customMetadata") public Builder customMetadata(@Nullable List value) { - this.customMetadata = Optional.ofNullable(value); + this.customMetadata = value; return this; } - Optional> customMetadata() { - return customMetadata; - } - @CanIgnoreReturnValue @JsonProperty("modelVersion") public Builder modelVersion(@Nullable String value) { - this.modelVersion = Optional.ofNullable(value); - return this; - } - - @CanIgnoreReturnValue - public Builder modelVersion(Optional value) { this.modelVersion = value; return this; } - Optional modelVersion() { - return modelVersion; - } - public Event build() { Event event = new Event(); event.setId(id); @@ -627,7 +537,7 @@ public Event build() { event.setInterrupted(interrupted); event.branch(branch); event.setGroundingMetadata(groundingMetadata); - event.setCustomMetadata(customMetadata.orElse(null)); + event.setCustomMetadata(customMetadata); event.setModelVersion(modelVersion); event.setActions(actions().orElseGet(() -> EventActions.builder().build())); event.setTimestamp(timestamp().orElseGet(() -> Instant.now().toEpochMilli())); @@ -664,7 +574,7 @@ public Builder toBuilder() { .interrupted(this.interrupted) .branch(this.branch) .groundingMetadata(this.groundingMetadata) - .customMetadata(this.customMetadata.orElse(null)) + .customMetadata(this.customMetadata) .modelVersion(this.modelVersion); if (this.timestamp != 0) { builder.timestamp(this.timestamp); diff --git a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java index 1249728d8..6ed9ccaa3 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java @@ -351,7 +351,7 @@ private Flowable runOneStep(InvocationContext context) { .id(Event.generateEventId()) .invocationId(context.invocationId()) .author(context.agent().name()) - .branch(context.branch()) + .branch(context.branch().orElse(null)) .build(); mutableEventTemplate.setTimestamp(0L); @@ -535,7 +535,7 @@ public void onError(Throwable e) { Event.builder() .invocationId(invocationContext.invocationId()) .author(invocationContext.agent().name()) - .branch(invocationContext.branch()); + .branch(invocationContext.branch().orElse(null)); Flowable receiveFlow = connection @@ -639,17 +639,17 @@ private Event buildModelResponseEvent( Event baseEventForLlmResponse, LlmRequest llmRequest, LlmResponse llmResponse) { Event.Builder eventBuilder = baseEventForLlmResponse.toBuilder() - .content(llmResponse.content()) - .partial(llmResponse.partial()) - .errorCode(llmResponse.errorCode()) - .errorMessage(llmResponse.errorMessage()) - .interrupted(llmResponse.interrupted()) - .turnComplete(llmResponse.turnComplete()) - .groundingMetadata(llmResponse.groundingMetadata()) - .avgLogprobs(llmResponse.avgLogprobs()) - .finishReason(llmResponse.finishReason()) - .usageMetadata(llmResponse.usageMetadata()) - .modelVersion(llmResponse.modelVersion()); + .content(llmResponse.content().orElse(null)) + .partial(llmResponse.partial().orElse(null)) + .errorCode(llmResponse.errorCode().orElse(null)) + .errorMessage(llmResponse.errorMessage().orElse(null)) + .interrupted(llmResponse.interrupted().orElse(null)) + .turnComplete(llmResponse.turnComplete().orElse(null)) + .groundingMetadata(llmResponse.groundingMetadata().orElse(null)) + .avgLogprobs(llmResponse.avgLogprobs().orElse(null)) + .finishReason(llmResponse.finishReason().orElse(null)) + .usageMetadata(llmResponse.usageMetadata().orElse(null)) + .modelVersion(llmResponse.modelVersion().orElse(null)); Event event = eventBuilder.build(); @@ -661,7 +661,7 @@ private Event buildModelResponseEvent( Functions.getLongRunningFunctionCalls(event.functionCalls(), llmRequest.tools()); logger.debug("longRunningToolIds: {}", longRunningToolIds); if (!longRunningToolIds.isEmpty()) { - event.setLongRunningToolIds(Optional.of(longRunningToolIds)); + event.setLongRunningToolIds(longRunningToolIds); } } return event; diff --git a/core/src/main/java/com/google/adk/flows/llmflows/CodeExecution.java b/core/src/main/java/com/google/adk/flows/llmflows/CodeExecution.java index be0504dd4..f7c3c51ef 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/CodeExecution.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/CodeExecution.java @@ -229,7 +229,7 @@ private static Flowable runPreProcessor( Event.builder() .invocationId(invocationContext.invocationId()) .author(llmAgent.name()) - .content(Optional.of(codeContent)) + .content(codeContent) .build(); return Flowable.defer( @@ -309,7 +309,7 @@ private static Flowable runPostProcessor( Event.builder() .invocationId(invocationContext.invocationId()) .author(llmAgent.name()) - .content(Optional.of(responseContent)) + .content(responseContent) .actions(EventActions.builder().build()) .build(); @@ -456,7 +456,7 @@ private static Single postProcessCodeExecutionResult( return Event.builder() .invocationId(invocationContext.invocationId()) .author(invocationContext.agent().name()) - .content(Optional.of(resultContent)) + .content(resultContent) .actions(eventActionsBuilder.build()) .build(); }); diff --git a/core/src/main/java/com/google/adk/flows/llmflows/Contents.java b/core/src/main/java/com/google/adk/flows/llmflows/Contents.java index a770808d4..ca8e0a051 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/Contents.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/Contents.java @@ -760,9 +760,7 @@ private static Event mergeFunctionResponseEvents(List functionResponseEve } return baseEvent.toBuilder() - .content( - Optional.of( - Content.builder().role(baseContent.role().get()).parts(partsInMergedEvent).build())) + .content(Content.builder().role(baseContent.role().get()).parts(partsInMergedEvent).build()) .build(); } diff --git a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java index 269764046..ecc2bb412 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java @@ -122,7 +122,7 @@ public static void populateClientFunctionCallId(Event modelResponseEvent) { new IllegalStateException( "Content role is missing in event: " + modelResponseEvent.id())); Content newContent = Content.builder().role(role).parts(newParts).build(); - modelResponseEvent.setContent(Optional.of(newContent)); + modelResponseEvent.setContent(newContent); } } @@ -468,8 +468,8 @@ private static Optional mergeParallelFunctionResponseEvents( .id(Event.generateEventId()) .invocationId(baseEvent.invocationId()) .author(baseEvent.author()) - .branch(baseEvent.branch()) - .content(Optional.of(Content.builder().role("user").parts(mergedParts).build())) + .branch(baseEvent.branch().orElse(null)) + .content(Content.builder().role("user").parts(mergedParts).build()) .actions(mergedActionsBuilder.build()) .timestamp(baseEvent.timestamp()) .build()); @@ -624,7 +624,7 @@ private static Event buildResponseEvent( .id(Event.generateEventId()) .invocationId(invocationContext.invocationId()) .author(invocationContext.agent().name()) - .branch(invocationContext.branch()) + .branch(invocationContext.branch().orElse(null)) .content(Content.builder().role("user").parts(partFunctionResponse).build()) .actions(toolContext.eventActions()) .build(); @@ -684,7 +684,7 @@ public static Optional generateRequestConfirmationEvent( Event.builder() .invocationId(invocationContext.invocationId()) .author(invocationContext.agent().name()) - .branch(invocationContext.branch()) + .branch(invocationContext.branch().orElse(null)) .content(contentBuilder.build()) .longRunningToolIds(longRunningToolIds) .build()); diff --git a/core/src/main/java/com/google/adk/runner/Runner.java b/core/src/main/java/com/google/adk/runner/Runner.java index f2cb5b9d5..4371300fb 100644 --- a/core/src/main/java/com/google/adk/runner/Runner.java +++ b/core/src/main/java/com/google/adk/runner/Runner.java @@ -340,7 +340,7 @@ private Single appendNewMessageToSession( .id(Event.generateEventId()) .invocationId(invocationContext.invocationId()) .author("user") - .content(Optional.of(newMessage)); + .content(newMessage); // Add state delta if provided if (stateDelta != null && !stateDelta.isEmpty()) { @@ -540,7 +540,7 @@ private Flowable runAgentWithFreshSession( .id(Event.generateEventId()) .invocationId(contextWithUpdatedSession.invocationId()) .author("model") - .content(Optional.of(content)) + .content(content) .build()); // Agent execution diff --git a/core/src/main/java/com/google/adk/sessions/SessionJsonConverter.java b/core/src/main/java/com/google/adk/sessions/SessionJsonConverter.java index 71b072695..97cc0f56d 100644 --- a/core/src/main/java/com/google/adk/sessions/SessionJsonConverter.java +++ b/core/src/main/java/com/google/adk/sessions/SessionJsonConverter.java @@ -208,9 +208,12 @@ static Event fromApiEvent(Map apiEvent) { .timestamp(convertToInstant(apiEvent.get("timestamp")).toEpochMilli()) .errorCode( Optional.ofNullable(apiEvent.get("errorCode")) - .map(value -> new FinishReason((String) value))) + .map(value -> new FinishReason((String) value)) + .orElse(null)) .errorMessage( - Optional.ofNullable(apiEvent.get("errorMessage")).map(value -> (String) value)) + Optional.ofNullable(apiEvent.get("errorMessage")) + .map(value -> (String) value) + .orElse(null)) .build(); Map eventMetadata = (Map) apiEvent.get("eventMetadata"); if (eventMetadata != null) { @@ -236,7 +239,7 @@ static Event fromApiEvent(Map apiEvent) { Optional.ofNullable((Boolean) eventMetadata.get("turnComplete")).orElse(false)) .interrupted( Optional.ofNullable((Boolean) eventMetadata.get("interrupted")).orElse(false)) - .branch(Optional.ofNullable((String) eventMetadata.get("branch"))) + .branch((String) eventMetadata.get("branch")) .groundingMetadata(groundingMetadata) .usageMetadata(usageMetadata) .longRunningToolIds( diff --git a/core/src/test/java/com/google/adk/events/EventTest.java b/core/src/test/java/com/google/adk/events/EventTest.java index cbfb6ef0b..a4feab5c1 100644 --- a/core/src/test/java/com/google/adk/events/EventTest.java +++ b/core/src/test/java/com/google/adk/events/EventTest.java @@ -76,6 +76,7 @@ public final class EventTest { .avgLogprobs(0.5) .interrupted(true) .timestamp(123456789L) + .modelVersion("model_version") .build(); @Test @@ -99,6 +100,7 @@ public void event_builder_works() { assertThat(EVENT.interrupted()).hasValue(true); assertThat(EVENT.timestamp()).isEqualTo(123456789L); assertThat(EVENT.actions()).isEqualTo(EVENT_ACTIONS); + assertThat(EVENT.modelVersion()).hasValue("model_version"); } @Test diff --git a/core/src/test/java/com/google/adk/flows/llmflows/ContentsTest.java b/core/src/test/java/com/google/adk/flows/llmflows/ContentsTest.java index 3041a855b..85e78666d 100644 --- a/core/src/test/java/com/google/adk/flows/llmflows/ContentsTest.java +++ b/core/src/test/java/com/google/adk/flows/llmflows/ContentsTest.java @@ -784,7 +784,7 @@ private static Event createUserEvent(String id, String text) { return Event.builder() .id(id) .author(USER) - .content(Optional.of(Content.fromParts(Part.fromText(text)))) + .content(Content.fromParts(Part.fromText(text))) .invocationId("invocationId") .build(); } @@ -794,7 +794,7 @@ private static Event createUserEvent( return Event.builder() .id(id) .author(USER) - .content(Optional.of(Content.fromParts(Part.fromText(text)))) + .content(Content.fromParts(Part.fromText(text))) .invocationId(invocationId) .timestamp(timestamp) .build(); diff --git a/core/src/test/java/com/google/adk/flows/llmflows/FunctionsTest.java b/core/src/test/java/com/google/adk/flows/llmflows/FunctionsTest.java index 97092f68c..d5db4d4b3 100644 --- a/core/src/test/java/com/google/adk/flows/llmflows/FunctionsTest.java +++ b/core/src/test/java/com/google/adk/flows/llmflows/FunctionsTest.java @@ -33,7 +33,6 @@ import com.google.genai.types.FunctionCall; import com.google.genai.types.FunctionResponse; import com.google.genai.types.Part; -import java.util.Optional; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -43,12 +42,7 @@ public final class FunctionsTest { private static final Event EVENT_WITH_NO_CONTENT = - Event.builder() - .id("event1") - .invocationId("invocation1") - .author("agent") - .content(Optional.empty()) - .build(); + Event.builder().id("event1").invocationId("invocation1").author("agent").build(); private static final Event EVENT_WITH_NO_PARTS = Event.builder() diff --git a/core/src/test/java/com/google/adk/plugins/LoggingPluginTest.java b/core/src/test/java/com/google/adk/plugins/LoggingPluginTest.java index 764525ff0..a08599c9a 100644 --- a/core/src/test/java/com/google/adk/plugins/LoggingPluginTest.java +++ b/core/src/test/java/com/google/adk/plugins/LoggingPluginTest.java @@ -65,9 +65,7 @@ public class LoggingPluginTest { Event.builder() .id("event_id") .author("author") - .content(Optional.empty()) .actions(EventActions.builder().build()) - .longRunningToolIds(Optional.empty()) .build(); private final LlmRequest.Builder llmRequestBuilder = LlmRequest.builder().model("default").contents(ImmutableList.of()); diff --git a/core/src/test/java/com/google/adk/sessions/SessionJsonConverterTest.java b/core/src/test/java/com/google/adk/sessions/SessionJsonConverterTest.java index f6120cf08..335f7f1d0 100644 --- a/core/src/test/java/com/google/adk/sessions/SessionJsonConverterTest.java +++ b/core/src/test/java/com/google/adk/sessions/SessionJsonConverterTest.java @@ -22,7 +22,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.junit.Test; @@ -49,12 +48,12 @@ public void convertEventToJson_fullEvent_success() throws JsonProcessingExceptio .author("user") .invocationId("inv-123") .timestamp(Instant.parse("2023-01-01T00:00:00Z").toEpochMilli()) - .errorCode(Optional.of(new FinishReason("OTHER"))) - .errorMessage(Optional.of("Something was not found")) + .errorCode(new FinishReason("OTHER")) + .errorMessage("Something was not found") .partial(true) .turnComplete(true) .interrupted(false) - .branch(Optional.of("branch-1")) + .branch("branch-1") .content(Content.fromParts(Part.fromText("Hello"))) .actions(actions) .build(); From 82ef5ac2689e01676aa95d2616e3b4d8463e573e Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Mon, 9 Mar 2026 03:45:34 -0700 Subject: [PATCH 03/25] feat!: remove McpAsyncToolset constructors PiperOrigin-RevId: 880768893 --- .../google/adk/tools/mcp/McpAsyncToolset.java | 46 +++++++------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/com/google/adk/tools/mcp/McpAsyncToolset.java b/core/src/main/java/com/google/adk/tools/mcp/McpAsyncToolset.java index 73af9cc6a..bcc786d69 100644 --- a/core/src/main/java/com/google/adk/tools/mcp/McpAsyncToolset.java +++ b/core/src/main/java/com/google/adk/tools/mcp/McpAsyncToolset.java @@ -22,6 +22,8 @@ import com.google.adk.tools.BaseTool; import com.google.adk.tools.BaseToolset; import com.google.adk.tools.NamedToolPredicate; +import com.google.adk.tools.ToolPredicate; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.errorprone.annotations.CanIgnoreReturnValue; import io.modelcontextprotocol.client.McpAsyncClient; @@ -32,8 +34,8 @@ import java.time.Duration; import java.util.List; import java.util.Objects; -import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; +import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; @@ -59,14 +61,14 @@ public class McpAsyncToolset implements BaseToolset { private final McpSessionManager mcpSessionManager; private final ObjectMapper objectMapper; - private final Optional toolFilter; + private final @Nullable Object toolFilter; private final AtomicReference>> mcpTools = new AtomicReference<>(); /** Builder for McpAsyncToolset */ public static class Builder { private Object connectionParams = null; private ObjectMapper objectMapper = null; - private Optional toolFilter = null; + private @Nullable Object toolFilter = null; @CanIgnoreReturnValue public Builder connectionParams(ServerParameters connectionParams) { @@ -87,14 +89,14 @@ public Builder objectMapper(ObjectMapper objectMapper) { } @CanIgnoreReturnValue - public Builder toolFilter(Optional toolFilter) { - this.toolFilter = toolFilter; + public Builder toolFilter(List toolNames) { + this.toolFilter = new NamedToolPredicate(Preconditions.checkNotNull(toolNames)); return this; } @CanIgnoreReturnValue - public Builder toolFilter(List toolNames) { - this.toolFilter = Optional.of(new NamedToolPredicate(toolNames)); + public Builder toolFilter(@Nullable ToolPredicate toolPredicate) { + this.toolFilter = toolPredicate; return this; } @@ -118,12 +120,12 @@ public McpAsyncToolset build() { * * @param connectionParams The SSE connection parameters to the MCP server. * @param objectMapper An ObjectMapper instance for parsing schemas. - * @param toolFilter An Optional containing either a ToolPredicate or a List of tool names. + * @param toolFilter Either a ToolPredicate or a List of tool names. */ - public McpAsyncToolset( + McpAsyncToolset( SseServerParameters connectionParams, ObjectMapper objectMapper, - Optional toolFilter) { + @Nullable Object toolFilter) { Objects.requireNonNull(connectionParams); Objects.requireNonNull(objectMapper); this.objectMapper = objectMapper; @@ -136,10 +138,10 @@ public McpAsyncToolset( * * @param connectionParams The local server connection parameters to the MCP server. * @param objectMapper An ObjectMapper instance for parsing schemas. - * @param toolFilter An Optional containing either a ToolPredicate or a List of tool names. + * @param toolFilter Either a ToolPredicate or a List of tool names or null. */ - public McpAsyncToolset( - ServerParameters connectionParams, ObjectMapper objectMapper, Optional toolFilter) { + McpAsyncToolset( + ServerParameters connectionParams, ObjectMapper objectMapper, @Nullable Object toolFilter) { Objects.requireNonNull(connectionParams); Objects.requireNonNull(objectMapper); this.objectMapper = objectMapper; @@ -147,22 +149,6 @@ public McpAsyncToolset( this.toolFilter = toolFilter; } - /** - * Initializes the McpAsyncToolset with a provided McpSessionManager. - * - * @param mcpSessionManager The session manager for MCP connections. - * @param objectMapper An ObjectMapper instance for parsing schemas. - * @param toolFilter An Optional containing either a ToolPredicate or a List of tool names. - */ - public McpAsyncToolset( - McpSessionManager mcpSessionManager, ObjectMapper objectMapper, Optional toolFilter) { - Objects.requireNonNull(mcpSessionManager); - Objects.requireNonNull(objectMapper); - this.objectMapper = objectMapper; - this.mcpSessionManager = mcpSessionManager; - this.toolFilter = toolFilter; - } - @Override public Flowable getTools(ReadonlyContext readonlyContext) { return Maybe.defer(() -> Maybe.fromCompletionStage(this.initAndGetTools().toFuture())) @@ -170,7 +156,7 @@ public Flowable getTools(ReadonlyContext readonlyContext) { .map( tools -> tools.stream() - .filter(tool -> isToolSelected(tool, toolFilter.orElse(null), readonlyContext)) + .filter(tool -> isToolSelected(tool, toolFilter, readonlyContext)) .toList()) .onErrorResumeNext( err -> { From 5e4eaa4805f10e03e88c9433c30dbe844c0161ec Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Mon, 9 Mar 2026 04:26:08 -0700 Subject: [PATCH 04/25] refactor: remove use of Optional params in Contents class PiperOrigin-RevId: 880784641 --- .../google/adk/flows/llmflows/Contents.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/com/google/adk/flows/llmflows/Contents.java b/core/src/main/java/com/google/adk/flows/llmflows/Contents.java index ca8e0a051..6ebd39a9c 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/Contents.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/Contents.java @@ -25,6 +25,7 @@ import com.google.adk.events.Event; import com.google.adk.events.EventCompaction; import com.google.adk.models.LlmRequest; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -41,6 +42,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import javax.annotation.Nullable; /** {@link RequestProcessor} that populates content in request for LLM flows. */ public final class Contents implements RequestProcessor { @@ -68,7 +70,7 @@ public Single processRequest( request.toBuilder() .contents( getCurrentTurnContents( - context.branch(), + context.branch().orElse(null), context.session().events(), context.agent().name(), modelName)) @@ -78,7 +80,10 @@ public Single processRequest( ImmutableList contents = getContents( - context.branch(), context.session().events(), context.agent().name(), modelName); + context.branch().orElse(null), + context.session().events(), + context.agent().name(), + modelName); return Single.just( RequestProcessor.RequestProcessingResult.create( @@ -87,7 +92,7 @@ public Single processRequest( /** Gets contents for the current turn only (no conversation history). */ private ImmutableList getCurrentTurnContents( - Optional currentBranch, List events, String agentName, String modelName) { + @Nullable String currentBranch, List events, String agentName, String modelName) { // Find the latest event that starts the current turn and process from there. for (int i = events.size() - 1; i >= 0; i--) { Event event = events.get(i); @@ -99,7 +104,7 @@ private ImmutableList getCurrentTurnContents( } private ImmutableList getContents( - Optional currentBranch, List events, String agentName, String modelName) { + @Nullable String currentBranch, List events, String agentName, String modelName) { List filteredEvents = new ArrayList<>(); boolean hasCompactEvent = false; @@ -414,16 +419,12 @@ private static String convertMapToJson(Map struct) { } } - private static boolean isEventBelongsToBranch(Optional invocationBranchOpt, Event event) { - Optional eventBranchOpt = event.branch(); + private static boolean isEventBelongsToBranch(@Nullable String invocationBranch, Event event) { + @Nullable String eventBranch = event.branch().orElse(null); - if (invocationBranchOpt.isEmpty() || invocationBranchOpt.get().isEmpty()) { - return true; - } - if (eventBranchOpt.isEmpty() || eventBranchOpt.get().isEmpty()) { - return true; - } - return invocationBranchOpt.get().startsWith(eventBranchOpt.get()); + return Strings.isNullOrEmpty(invocationBranch) + || Strings.isNullOrEmpty(eventBranch) + || invocationBranch.startsWith(eventBranch); } /** From 1cb4d431ca62573a8071b244e64971e6d60a42ad Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Mon, 9 Mar 2026 05:28:00 -0700 Subject: [PATCH 05/25] refactor: Move RemoteA2AAgent to agent package; remove EXPERIMENTAL annotation; add LICENSE header PiperOrigin-RevId: 880804505 --- .../adk/a2a/{ => agent}/RemoteA2AAgent.java | 40 ++++++++++++------- .../google/adk/a2a/common/A2AClientError.java | 15 +++++++ .../google/adk/a2a/common/A2AMetadata.java | 15 +++++++ .../common/GenAiFieldMissingException.java | 15 +++++++ .../converters/A2ADataPartMetadataType.java | 15 +++++++ .../adk/a2a/converters/EventConverter.java | 22 +++++++--- .../adk/a2a/converters/PartConverter.java | 22 +++++++--- .../adk/a2a/converters/ResponseConverter.java | 22 +++++++--- .../adk/a2a/executor/AgentExecutor.java | 22 +++++++--- .../adk/a2a/executor/AgentExecutorConfig.java | 15 +++++++ .../google/adk/a2a/executor/Callbacks.java | 15 +++++++ .../a2a/{ => agent}/RemoteA2AAgentTest.java | 2 +- contrib/samples/a2a_basic/A2AAgent.java | 2 +- 13 files changed, 181 insertions(+), 41 deletions(-) rename a2a/src/main/java/com/google/adk/a2a/{ => agent}/RemoteA2AAgent.java (93%) rename a2a/src/test/java/com/google/adk/a2a/{ => agent}/RemoteA2AAgentTest.java (99%) diff --git a/a2a/src/main/java/com/google/adk/a2a/RemoteA2AAgent.java b/a2a/src/main/java/com/google/adk/a2a/agent/RemoteA2AAgent.java similarity index 93% rename from a2a/src/main/java/com/google/adk/a2a/RemoteA2AAgent.java rename to a2a/src/main/java/com/google/adk/a2a/agent/RemoteA2AAgent.java index b391f2985..021786162 100644 --- a/a2a/src/main/java/com/google/adk/a2a/RemoteA2AAgent.java +++ b/a2a/src/main/java/com/google/adk/a2a/agent/RemoteA2AAgent.java @@ -1,4 +1,19 @@ -package com.google.adk.a2a; +/* + * 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; @@ -44,26 +59,21 @@ import org.slf4j.LoggerFactory; /** - * Agent that communicates with a remote A2A agent via A2A client. - * - *

This agent supports multiple ways to specify the remote agent: + * Agent that communicates with a remote A2A agent via an A2A client. * - *

    - *
  1. Direct AgentCard object - *
  2. URL to agent card JSON - *
  3. File path to agent card JSON - *
+ *

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. * - *

The agent handles: + *

Key responsibilities of this agent include: * *

    *
  • Agent card resolution and validation - *
  • A2A message conversion and error handling - *
  • Session state management across requests + *
  • Converting ADK session history events into A2A requests ({@link io.a2a.spec.Message}) + *
  • Handling streaming and non-streaming responses from the A2A client + *
  • Buffering and aggregating streamed response chunks into ADK {@link + * com.google.adk.events.Event}s + *
  • Converting A2A client responses back into ADK format *
- * - *

**EXPERIMENTAL:** Subject to change, rename, or removal in any future patch release. Do not - * use in production code. */ public class RemoteA2AAgent extends BaseAgent { diff --git a/a2a/src/main/java/com/google/adk/a2a/common/A2AClientError.java b/a2a/src/main/java/com/google/adk/a2a/common/A2AClientError.java index 8e8282742..466c89223 100644 --- a/a2a/src/main/java/com/google/adk/a2a/common/A2AClientError.java +++ b/a2a/src/main/java/com/google/adk/a2a/common/A2AClientError.java @@ -1,3 +1,18 @@ +/* + * 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.common; /** Exception thrown when the A2A client encounters an error. */ diff --git a/a2a/src/main/java/com/google/adk/a2a/common/A2AMetadata.java b/a2a/src/main/java/com/google/adk/a2a/common/A2AMetadata.java index 5c75faeac..a5faeff2a 100644 --- a/a2a/src/main/java/com/google/adk/a2a/common/A2AMetadata.java +++ b/a2a/src/main/java/com/google/adk/a2a/common/A2AMetadata.java @@ -1,3 +1,18 @@ +/* + * 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.common; /** Constants and utilities for A2A metadata keys. */ diff --git a/a2a/src/main/java/com/google/adk/a2a/common/GenAiFieldMissingException.java b/a2a/src/main/java/com/google/adk/a2a/common/GenAiFieldMissingException.java index a5947dcb8..0ac56fc01 100644 --- a/a2a/src/main/java/com/google/adk/a2a/common/GenAiFieldMissingException.java +++ b/a2a/src/main/java/com/google/adk/a2a/common/GenAiFieldMissingException.java @@ -1,3 +1,18 @@ +/* + * 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.common; /** Exception thrown when the the genai class has an empty field. */ diff --git a/a2a/src/main/java/com/google/adk/a2a/converters/A2ADataPartMetadataType.java b/a2a/src/main/java/com/google/adk/a2a/converters/A2ADataPartMetadataType.java index b5b53c49a..e0e97c8e9 100644 --- a/a2a/src/main/java/com/google/adk/a2a/converters/A2ADataPartMetadataType.java +++ b/a2a/src/main/java/com/google/adk/a2a/converters/A2ADataPartMetadataType.java @@ -1,3 +1,18 @@ +/* + * 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.converters; /** Enum for the type of A2A DataPart metadata. */ diff --git a/a2a/src/main/java/com/google/adk/a2a/converters/EventConverter.java b/a2a/src/main/java/com/google/adk/a2a/converters/EventConverter.java index 1a49b0070..d823e3817 100644 --- a/a2a/src/main/java/com/google/adk/a2a/converters/EventConverter.java +++ b/a2a/src/main/java/com/google/adk/a2a/converters/EventConverter.java @@ -1,3 +1,18 @@ +/* + * 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.converters; import static com.google.common.collect.ImmutableList.toImmutableList; @@ -13,12 +28,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Converter for ADK Events to A2A Messages. - * - *

**EXPERIMENTAL:** Subject to change, rename, or removal in any future patch release. Do not - * use in production code. - */ +/** Converter for ADK Events to A2A Messages. */ public final class EventConverter { private static final Logger logger = LoggerFactory.getLogger(EventConverter.class); diff --git a/a2a/src/main/java/com/google/adk/a2a/converters/PartConverter.java b/a2a/src/main/java/com/google/adk/a2a/converters/PartConverter.java index 05125d170..96ef66bc8 100644 --- a/a2a/src/main/java/com/google/adk/a2a/converters/PartConverter.java +++ b/a2a/src/main/java/com/google/adk/a2a/converters/PartConverter.java @@ -1,3 +1,18 @@ +/* + * 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.converters; import static com.google.common.collect.ImmutableList.toImmutableList; @@ -32,12 +47,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Utility class for converting between Google GenAI Parts and A2A DataParts. - * - *

**EXPERIMENTAL:** Subject to change, rename, or removal in any future patch release. Do not - * use in production code. - */ +/** Utility class for converting between Google GenAI Parts and A2A DataParts. */ public final class PartConverter { private static final Logger logger = LoggerFactory.getLogger(PartConverter.class); diff --git a/a2a/src/main/java/com/google/adk/a2a/converters/ResponseConverter.java b/a2a/src/main/java/com/google/adk/a2a/converters/ResponseConverter.java index 57a84b58f..f3be48c1b 100644 --- a/a2a/src/main/java/com/google/adk/a2a/converters/ResponseConverter.java +++ b/a2a/src/main/java/com/google/adk/a2a/converters/ResponseConverter.java @@ -1,3 +1,18 @@ +/* + * 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.converters; import static com.google.common.collect.ImmutableList.toImmutableList; @@ -27,12 +42,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Utility for converting ADK events to A2A spec messages (and back). - * - *

**EXPERIMENTAL:** Subject to change, rename, or removal in any future patch release. Do not - * use in production code. - */ +/** Utility for converting ADK events to A2A spec messages (and back). */ public final class ResponseConverter { private static final Logger logger = LoggerFactory.getLogger(ResponseConverter.class); private static final ImmutableSet PENDING_STATES = diff --git a/a2a/src/main/java/com/google/adk/a2a/executor/AgentExecutor.java b/a2a/src/main/java/com/google/adk/a2a/executor/AgentExecutor.java index b7b4e9953..7252cdec1 100644 --- a/a2a/src/main/java/com/google/adk/a2a/executor/AgentExecutor.java +++ b/a2a/src/main/java/com/google/adk/a2a/executor/AgentExecutor.java @@ -1,3 +1,18 @@ +/* + * 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.executor; import static java.util.Objects.requireNonNull; @@ -44,12 +59,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Implementation of the A2A AgentExecutor interface that uses ADK to execute agent tasks. - * - *

**EXPERIMENTAL:** Subject to change, rename, or removal in any future patch release. Do not - * use in production code. - */ +/** Implementation of the A2A AgentExecutor interface that uses ADK to execute agent tasks. */ public class AgentExecutor implements io.a2a.server.agentexecution.AgentExecutor { private static final Logger logger = LoggerFactory.getLogger(AgentExecutor.class); private static final String USER_ID_PREFIX = "A2A_USER_"; diff --git a/a2a/src/main/java/com/google/adk/a2a/executor/AgentExecutorConfig.java b/a2a/src/main/java/com/google/adk/a2a/executor/AgentExecutorConfig.java index ba0177dc4..3ee8656d2 100644 --- a/a2a/src/main/java/com/google/adk/a2a/executor/AgentExecutorConfig.java +++ b/a2a/src/main/java/com/google/adk/a2a/executor/AgentExecutorConfig.java @@ -1,3 +1,18 @@ +/* + * 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.executor; import com.google.adk.a2a.executor.Callbacks.AfterEventCallback; diff --git a/a2a/src/main/java/com/google/adk/a2a/executor/Callbacks.java b/a2a/src/main/java/com/google/adk/a2a/executor/Callbacks.java index 666f1d8a0..3483c527f 100644 --- a/a2a/src/main/java/com/google/adk/a2a/executor/Callbacks.java +++ b/a2a/src/main/java/com/google/adk/a2a/executor/Callbacks.java @@ -1,3 +1,18 @@ +/* + * 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.executor; import com.google.adk.events.Event; diff --git a/a2a/src/test/java/com/google/adk/a2a/RemoteA2AAgentTest.java b/a2a/src/test/java/com/google/adk/a2a/agent/RemoteA2AAgentTest.java similarity index 99% rename from a2a/src/test/java/com/google/adk/a2a/RemoteA2AAgentTest.java rename to a2a/src/test/java/com/google/adk/a2a/agent/RemoteA2AAgentTest.java index 87eaa2321..e75da64ba 100644 --- a/a2a/src/test/java/com/google/adk/a2a/RemoteA2AAgentTest.java +++ b/a2a/src/test/java/com/google/adk/a2a/agent/RemoteA2AAgentTest.java @@ -1,4 +1,4 @@ -package com.google.adk.a2a; +package com.google.adk.a2a.agent; import static com.google.common.truth.Truth.assertThat; import static java.util.concurrent.TimeUnit.SECONDS; diff --git a/contrib/samples/a2a_basic/A2AAgent.java b/contrib/samples/a2a_basic/A2AAgent.java index e4e79a4eb..e08a87a67 100644 --- a/contrib/samples/a2a_basic/A2AAgent.java +++ b/contrib/samples/a2a_basic/A2AAgent.java @@ -1,6 +1,6 @@ package com.example.a2a_basic; -import com.google.adk.a2a.RemoteA2AAgent; +import com.google.adk.a2a.agent.RemoteA2AAgent; import com.google.adk.agents.BaseAgent; import com.google.adk.agents.LlmAgent; import com.google.adk.tools.FunctionTool; From 5e1e1d434fa1f3931af30194422800757de96cb6 Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Mon, 9 Mar 2026 06:57:20 -0700 Subject: [PATCH 06/25] feat!: Remove deprecated create method in ResponseProcessor PiperOrigin-RevId: 880834921 --- .../com/google/adk/flows/llmflows/CodeExecution.java | 5 ++--- .../google/adk/flows/llmflows/ResponseProcessor.java | 11 ++++++++--- .../google/adk/flows/llmflows/BaseLlmFlowTest.java | 9 ++------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/com/google/adk/flows/llmflows/CodeExecution.java b/core/src/main/java/com/google/adk/flows/llmflows/CodeExecution.java index f7c3c51ef..f2cbe967e 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/CodeExecution.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/CodeExecution.java @@ -159,8 +159,7 @@ public Single processResponse( InvocationContext invocationContext, LlmResponse llmResponse) { if (llmResponse.partial().orElse(false)) { return Single.just( - ResponseProcessor.ResponseProcessingResult.create( - llmResponse, ImmutableList.of(), Optional.empty())); + ResponseProcessor.ResponseProcessingResult.create(llmResponse, ImmutableList.of())); } var llmResponseBuilder = llmResponse.toBuilder(); return runPostProcessor(invocationContext, llmResponseBuilder) @@ -168,7 +167,7 @@ public Single processResponse( .map( events -> ResponseProcessor.ResponseProcessingResult.create( - llmResponseBuilder.build(), events, Optional.empty())); + llmResponseBuilder.build(), events)); } } diff --git a/core/src/main/java/com/google/adk/flows/llmflows/ResponseProcessor.java b/core/src/main/java/com/google/adk/flows/llmflows/ResponseProcessor.java index 4baa29523..d8e5ce3ab 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/ResponseProcessor.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/ResponseProcessor.java @@ -50,11 +50,16 @@ public abstract static class ResponseProcessingResult { */ public abstract Optional transferToAgent(); - /** Creates a new {@link ResponseProcessingResult}. */ public static ResponseProcessingResult create( - LlmResponse updatedResponse, Iterable events, Optional transferToAgent) { + LlmResponse updatedResponse, Iterable events, String transferToAgent) { return new AutoValue_ResponseProcessor_ResponseProcessingResult( - updatedResponse, events, transferToAgent); + updatedResponse, events, Optional.of(transferToAgent)); + } + + public static ResponseProcessingResult create( + LlmResponse updatedResponse, Iterable events) { + return new AutoValue_ResponseProcessor_ResponseProcessingResult( + updatedResponse, events, /* transferToAgent= */ Optional.empty()); } } diff --git a/core/src/test/java/com/google/adk/flows/llmflows/BaseLlmFlowTest.java b/core/src/test/java/com/google/adk/flows/llmflows/BaseLlmFlowTest.java index ff151a0b2..4a0b345c6 100644 --- a/core/src/test/java/com/google/adk/flows/llmflows/BaseLlmFlowTest.java +++ b/core/src/test/java/com/google/adk/flows/llmflows/BaseLlmFlowTest.java @@ -524,19 +524,14 @@ private static RequestProcessor createRequestProcessor( private static ResponseProcessor createResponseProcessor() { return (context, response) -> - Single.just( - ResponseProcessingResult.create( - response, ImmutableList.of(), /* transferToAgent= */ Optional.empty())); + Single.just(ResponseProcessingResult.create(response, ImmutableList.of())); } private static ResponseProcessor createResponseProcessor( Function responseUpdater) { return (context, response) -> Single.just( - ResponseProcessingResult.create( - responseUpdater.apply(response), - ImmutableList.of(), - /* transferToAgent= */ Optional.empty())); + ResponseProcessingResult.create(responseUpdater.apply(response), ImmutableList.of())); } private static class TestTool extends BaseTool { From a86ede007c3442ed73ee08a5c6ad0e2efa12998a Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Mon, 9 Mar 2026 07:08:54 -0700 Subject: [PATCH 07/25] feat!: remove deprecated url method in ComputerState.Builder PiperOrigin-RevId: 880839286 --- .../adk/tools/computeruse/ComputerState.java | 17 +++----- .../computeruse/ComputerUseToolTest.java | 9 +--- .../computeruse/ComputerUseToolsetTest.java | 42 +++++++------------ 3 files changed, 22 insertions(+), 46 deletions(-) diff --git a/core/src/main/java/com/google/adk/tools/computeruse/ComputerState.java b/core/src/main/java/com/google/adk/tools/computeruse/ComputerState.java index 4f3be46c2..b3d0f73bb 100644 --- a/core/src/main/java/com/google/adk/tools/computeruse/ComputerState.java +++ b/core/src/main/java/com/google/adk/tools/computeruse/ComputerState.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.Objects; import java.util.Optional; +import org.jspecify.annotations.Nullable; /** * Represents the current state of the computer environment. @@ -31,11 +32,11 @@ */ public final class ComputerState { private final byte[] screenshot; - private final Optional url; + private final @Nullable String url; @JsonCreator private ComputerState( - @JsonProperty("screenshot") byte[] screenshot, @JsonProperty("url") Optional url) { + @JsonProperty("screenshot") byte[] screenshot, @JsonProperty("url") @Nullable String url) { this.screenshot = screenshot.clone(); this.url = url; } @@ -47,7 +48,7 @@ public byte[] screenshot() { @JsonProperty("url") public Optional url() { - return url; + return Optional.ofNullable(url); } public static Builder builder() { @@ -57,7 +58,7 @@ public static Builder builder() { /** Builder for {@link ComputerState}. */ public static final class Builder { private byte[] screenshot; - private Optional url = Optional.empty(); + private @Nullable String url; @CanIgnoreReturnValue public Builder screenshot(byte[] screenshot) { @@ -66,17 +67,11 @@ public Builder screenshot(byte[] screenshot) { } @CanIgnoreReturnValue - public Builder url(Optional url) { + public Builder url(@Nullable String url) { this.url = url; return this; } - @CanIgnoreReturnValue - public Builder url(String url) { - this.url = Optional.ofNullable(url); - return this; - } - public ComputerState build() { return new ComputerState(screenshot, url); } diff --git a/core/src/test/java/com/google/adk/tools/computeruse/ComputerUseToolTest.java b/core/src/test/java/com/google/adk/tools/computeruse/ComputerUseToolTest.java index 20fb146cf..236172b27 100644 --- a/core/src/test/java/com/google/adk/tools/computeruse/ComputerUseToolTest.java +++ b/core/src/test/java/com/google/adk/tools/computeruse/ComputerUseToolTest.java @@ -30,7 +30,6 @@ import java.lang.reflect.Method; import java.util.Base64; import java.util.Map; -import java.util.Optional; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -128,10 +127,7 @@ public void testNormalizeDragAndDrop() throws NoSuchMethodException { public void testResultFormatting() throws NoSuchMethodException { byte[] screenshot = new byte[] {1, 2, 3}; computerMock.nextState = - ComputerState.builder() - .screenshot(screenshot) - .url(Optional.of("https://example.com")) - .build(); + ComputerState.builder().screenshot(screenshot).url("https://example.com").build(); Method method = ComputerMock.class.getMethod("clickAt", int.class, int.class); ComputerUseTool tool = @@ -226,8 +222,7 @@ public static class ComputerMock { public int lastY; public int lastDestX; public int lastDestY; - public ComputerState nextState = - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build(); + public ComputerState nextState = ComputerState.builder().screenshot(new byte[0]).build(); public Single clickAt(@Schema(name = "x") int x, @Schema(name = "y") int y) { this.lastX = x; diff --git a/core/src/test/java/com/google/adk/tools/computeruse/ComputerUseToolsetTest.java b/core/src/test/java/com/google/adk/tools/computeruse/ComputerUseToolsetTest.java index 1ed49419e..8051a018d 100644 --- a/core/src/test/java/com/google/adk/tools/computeruse/ComputerUseToolsetTest.java +++ b/core/src/test/java/com/google/adk/tools/computeruse/ComputerUseToolsetTest.java @@ -173,87 +173,73 @@ public Single environment() { @Override public Single openWebBrowser() { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single clickAt(int x, int y) { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single hoverAt(int x, int y) { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single typeTextAt( int x, int y, String text, Boolean pressEnter, Boolean clearBeforeTyping) { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single scrollDocument(String direction) { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single scrollAt(int x, int y, String direction, int magnitude) { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single wait(Duration duration) { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single goBack() { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single goForward() { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single search() { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single navigate(String url) { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.of(url)).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).url(url).build()); } @Override public Single keyCombination(List keys) { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single dragAndDrop(int x, int y, int destinationX, int destinationY) { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override public Single currentState() { - return Single.just( - ComputerState.builder().screenshot(new byte[0]).url(Optional.empty()).build()); + return Single.just(ComputerState.builder().screenshot(new byte[0]).build()); } @Override From 143b656949d61363d135e0b74ef5696e78eb270a Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Mon, 9 Mar 2026 09:47:49 -0700 Subject: [PATCH 08/25] feat: update return type for requestedToolConfirmations getter and setter to Map from ConcurrentMap PiperOrigin-RevId: 880903703 --- .../com/google/adk/events/EventActions.java | 25 +++++++++++++++---- .../google/adk/events/EventActionsTest.java | 24 ++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/adk/events/EventActions.java b/core/src/main/java/com/google/adk/events/EventActions.java index bf25acfc7..31b096930 100644 --- a/core/src/main/java/com/google/adk/events/EventActions.java +++ b/core/src/main/java/com/google/adk/events/EventActions.java @@ -165,13 +165,20 @@ public void setRequestedAuthConfigs( } @JsonProperty("requestedToolConfirmations") - public ConcurrentMap requestedToolConfirmations() { + public Map requestedToolConfirmations() { return requestedToolConfirmations; } public void setRequestedToolConfirmations( - ConcurrentMap requestedToolConfirmations) { - this.requestedToolConfirmations = requestedToolConfirmations; + Map requestedToolConfirmations) { + if (requestedToolConfirmations == null) { + this.requestedToolConfirmations = new ConcurrentHashMap<>(); + } else if (requestedToolConfirmations instanceof ConcurrentMap) { + this.requestedToolConfirmations = + (ConcurrentMap) requestedToolConfirmations; + } else { + this.requestedToolConfirmations = new ConcurrentHashMap<>(requestedToolConfirmations); + } } @JsonProperty("endOfAgent") @@ -351,8 +358,16 @@ public Builder requestedAuthConfigs( @CanIgnoreReturnValue @JsonProperty("requestedToolConfirmations") - public Builder requestedToolConfirmations(ConcurrentMap value) { - this.requestedToolConfirmations = value; + public Builder requestedToolConfirmations(@Nullable Map value) { + if (value == null) { + this.requestedToolConfirmations = new ConcurrentHashMap<>(); + return this; + } + if (value instanceof ConcurrentMap) { + this.requestedToolConfirmations = (ConcurrentMap) value; + } else { + this.requestedToolConfirmations = new ConcurrentHashMap<>(value); + } return this; } diff --git a/core/src/test/java/com/google/adk/events/EventActionsTest.java b/core/src/test/java/com/google/adk/events/EventActionsTest.java index 28123bab8..2975ca83f 100644 --- a/core/src/test/java/com/google/adk/events/EventActionsTest.java +++ b/core/src/test/java/com/google/adk/events/EventActionsTest.java @@ -26,6 +26,7 @@ import com.google.genai.types.Part; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -165,4 +166,27 @@ public void merge_failsOnMismatchedKeyTypesNestedInStateDelta() { assertThrows( IllegalArgumentException.class, () -> eventActions1.toBuilder().merge(eventActions2)); } + + @Test + public void setRequestedToolConfirmations_withConcurrentMap_usesSameInstance() { + ConcurrentHashMap map = new ConcurrentHashMap<>(); + map.put("tool", TOOL_CONFIRMATION); + + EventActions actions = new EventActions(); + actions.setRequestedToolConfirmations(map); + + assertThat(actions.requestedToolConfirmations()).isSameInstanceAs(map); + } + + @Test + public void setRequestedToolConfirmations_withRegularMap_createsConcurrentMap() { + ImmutableMap map = ImmutableMap.of("tool", TOOL_CONFIRMATION); + + EventActions actions = new EventActions(); + actions.setRequestedToolConfirmations(map); + + assertThat(actions.requestedToolConfirmations()).isNotSameInstanceAs(map); + assertThat(actions.requestedToolConfirmations()).isInstanceOf(ConcurrentMap.class); + assertThat(actions.requestedToolConfirmations()).containsExactly("tool", TOOL_CONFIRMATION); + } } From 973f88743cabebcd2e6e7a8d5f141142b596dbbb Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Mon, 9 Mar 2026 13:13:51 -0700 Subject: [PATCH 09/25] feat: Fixing the spans produced by agent calls to have the right parent spans PiperOrigin-RevId: 881003835 --- .../java/com/google/adk/agents/BaseAgent.java | 107 ++-- .../adk/flows/llmflows/BaseLlmFlow.java | 390 ++++++++------ .../google/adk/flows/llmflows/Functions.java | 227 ++++---- .../com/google/adk/plugins/PluginManager.java | 16 +- .../java/com/google/adk/runner/Runner.java | 167 +++--- .../com/google/adk/telemetry/Tracing.java | 488 +++++++++++++----- 6 files changed, 887 insertions(+), 508 deletions(-) diff --git a/core/src/main/java/com/google/adk/agents/BaseAgent.java b/core/src/main/java/com/google/adk/agents/BaseAgent.java index d74ba9ca5..c527eeab3 100644 --- a/core/src/main/java/com/google/adk/agents/BaseAgent.java +++ b/core/src/main/java/com/google/adk/agents/BaseAgent.java @@ -29,10 +29,10 @@ import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.DoNotCall; import com.google.genai.types.Content; +import io.opentelemetry.context.Context; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; -import io.reactivex.rxjava3.core.Single; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -312,38 +312,47 @@ public Flowable runAsync(InvocationContext parentContext) { private Flowable run( InvocationContext parentContext, Function> runImplementation) { + Context otelParentContext = Context.current(); + InvocationContext invocationContext = createInvocationContext(parentContext); + return Flowable.defer( - () -> { - InvocationContext invocationContext = createInvocationContext(parentContext); - - return callCallback( - beforeCallbacksToFunctions( - invocationContext.pluginManager(), beforeAgentCallback), - invocationContext) - .flatMapPublisher( - beforeEventOpt -> { - if (invocationContext.endInvocation()) { - return Flowable.fromOptional(beforeEventOpt); - } - - Flowable beforeEvents = Flowable.fromOptional(beforeEventOpt); - Flowable mainEvents = - Flowable.defer(() -> runImplementation.apply(invocationContext)); - Flowable afterEvents = - Flowable.defer( - () -> - callCallback( - afterCallbacksToFunctions( - invocationContext.pluginManager(), afterAgentCallback), - invocationContext) - .flatMapPublisher(Flowable::fromOptional)); - - return Flowable.concat(beforeEvents, mainEvents, afterEvents); - }) - .compose( - Tracing.traceAgent( - "invoke_agent " + name(), name(), description(), invocationContext)); - }); + () -> { + return callCallback( + beforeCallbacksToFunctions( + invocationContext.pluginManager(), beforeAgentCallback), + invocationContext) + .flatMapPublisher( + beforeEvent -> { + if (invocationContext.endInvocation()) { + return Flowable.just(beforeEvent); + } + + return Flowable.just(beforeEvent) + .concatWith(runMainAndAfter(invocationContext, runImplementation)); + }) + .switchIfEmpty( + Flowable.defer(() -> runMainAndAfter(invocationContext, runImplementation))); + }) + .compose( + Tracing.traceAgent( + otelParentContext, + "invoke_agent " + name(), + name(), + description(), + invocationContext)); + } + + private Flowable runMainAndAfter( + InvocationContext invocationContext, + Function> runImplementation) { + Flowable mainEvents = runImplementation.apply(invocationContext); + Flowable afterEvents = + callCallback( + afterCallbacksToFunctions(invocationContext.pluginManager(), afterAgentCallback), + invocationContext) + .flatMapPublisher(Flowable::just); + + return Flowable.concat(mainEvents, afterEvents); } /** @@ -383,13 +392,13 @@ private ImmutableList>> callbacksTo * * @param agentCallbacks Callback functions. * @param invocationContext Current invocation context. - * @return single emitting first event, or empty if none. + * @return Maybe emitting first event, or empty if none. */ - private Single> callCallback( + private Maybe callCallback( List>> agentCallbacks, InvocationContext invocationContext) { if (agentCallbacks.isEmpty()) { - return Single.just(Optional.empty()); + return Maybe.empty(); } CallbackContext callbackContext = @@ -398,27 +407,25 @@ private Single> callCallback( return Flowable.fromIterable(agentCallbacks) .concatMap( callback -> { - Maybe maybeContent = callback.apply(callbackContext); - - return maybeContent + return callback + .apply(callbackContext) .map( content -> { invocationContext.setEndInvocation(true); - return Optional.of( - Event.builder() - .id(Event.generateEventId()) - .invocationId(invocationContext.invocationId()) - .author(name()) - .branch(invocationContext.branch().orElse(null)) - .actions(callbackContext.eventActions()) - .content(content) - .build()); + return Event.builder() + .id(Event.generateEventId()) + .invocationId(invocationContext.invocationId()) + .author(name()) + .branch(invocationContext.branch().orElse(null)) + .actions(callbackContext.eventActions()) + .content(content) + .build(); }) .toFlowable(); }) .firstElement() .switchIfEmpty( - Single.defer( + Maybe.defer( () -> { if (callbackContext.state().hasDelta()) { Event.Builder eventBuilder = @@ -429,9 +436,9 @@ private Single> callCallback( .branch(invocationContext.branch().orElse(null)) .actions(callbackContext.eventActions()); - return Single.just(Optional.of(eventBuilder.build())); + return Maybe.just(eventBuilder.build()); } else { - return Single.just(Optional.empty()); + return Maybe.empty(); } })); } diff --git a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java index 6ed9ccaa3..fba7f10e0 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java @@ -92,7 +92,9 @@ public BaseLlmFlow( * events generated by them. */ protected Flowable preprocess( - InvocationContext context, AtomicReference llmRequestRef) { + InvocationContext context, + AtomicReference llmRequestRef, + Context otelParentContext) { LlmAgent agent = (LlmAgent) context.agent(); RequestProcessor toolsProcessor = @@ -104,7 +106,8 @@ protected Flowable preprocess( tool -> tool.processLlmRequest(builder, ToolContext.builder(ctx).build())) .andThen( Single.fromCallable( - () -> RequestProcessingResult.create(builder.build(), ImmutableList.of()))); + () -> RequestProcessingResult.create(builder.build(), ImmutableList.of()))) + .compose(Tracing.withContext(otelParentContext)); }; Iterable allProcessors = @@ -113,7 +116,9 @@ protected Flowable preprocess( return Flowable.fromIterable(allProcessors) .concatMap( processor -> - Single.defer(() -> processor.processRequest(context, llmRequestRef.get())) + processor + .processRequest(context, llmRequestRef.get()) + .compose(Tracing.withContext(otelParentContext)) .doOnSuccess(result -> llmRequestRef.set(result.updatedRequest())) .flattenAsFlowable( result -> result.events() != null ? result.events() : ImmutableList.of())); @@ -129,13 +134,32 @@ protected Flowable postprocess( Event baseEventForLlmResponse, LlmRequest llmRequest, LlmResponse llmResponse) { + return postprocess( + context, baseEventForLlmResponse, llmRequest, llmResponse, Context.current()); + } + + /** + * Post-processes the LLM response after receiving it from the LLM. Executes all registered {@link + * ResponseProcessor} instances. Emits events for the model response and any subsequent function + * calls. + */ + private Flowable postprocess( + InvocationContext context, + Event baseEventForLlmResponse, + LlmRequest llmRequest, + LlmResponse llmResponse, + Context otelParentContext) { List> eventIterables = new ArrayList<>(); Single currentLlmResponse = Single.just(llmResponse); for (ResponseProcessor processor : responseProcessors) { currentLlmResponse = currentLlmResponse - .flatMap(response -> processor.processResponse(context, response)) + .flatMap( + response -> + processor + .processResponse(context, response) + .compose(Tracing.withContext(otelParentContext))) .doOnSuccess( result -> { if (result.events() != null) { @@ -144,15 +168,16 @@ protected Flowable postprocess( }) .map(ResponseProcessingResult::updatedResponse); } - Context parentContext = Context.current(); return currentLlmResponse.flatMapPublisher( - updatedResponse -> { - try (Scope scope = parentContext.makeCurrent()) { - return buildPostprocessingEvents( - updatedResponse, eventIterables, context, baseEventForLlmResponse, llmRequest); - } - }); + updatedResponse -> + buildPostprocessingEvents( + updatedResponse, + eventIterables, + context, + baseEventForLlmResponse, + llmRequest, + otelParentContext)); } /** @@ -164,84 +189,100 @@ protected Flowable postprocess( * callbacks. Callbacks should not rely on its ID if they create their own separate events. */ private Flowable callLlm( - InvocationContext context, LlmRequest llmRequest, Event eventForCallbackUsage) { + InvocationContext context, + LlmRequest llmRequest, + Event eventForCallbackUsage, + Context otelParentContext) { LlmAgent agent = (LlmAgent) context.agent(); LlmRequest.Builder llmRequestBuilder = llmRequest.toBuilder(); - return handleBeforeModelCallback(context, llmRequestBuilder, eventForCallbackUsage) - .flatMapPublisher( - beforeResponse -> { - if (beforeResponse.isPresent()) { - return Flowable.just(beforeResponse.get()); - } - BaseLlm llm = - agent.resolvedModel().model().isPresent() - ? agent.resolvedModel().model().get() - : LlmRegistry.getLlm(agent.resolvedModel().modelName().get()); - return llm.generateContent( - llmRequestBuilder.build(), - context.runConfig().streamingMode() == StreamingMode.SSE) - .onErrorResumeNext( - exception -> - handleOnModelErrorCallback( - context, llmRequestBuilder, eventForCallbackUsage, exception) - .switchIfEmpty(Single.error(exception)) - .toFlowable()) - .doOnNext( - llmResp -> - Tracing.traceCallLlm( - context, - eventForCallbackUsage.id(), - llmRequestBuilder.build(), - llmResp)) - .doOnError( - error -> { - Span span = Span.current(); - span.setStatus(StatusCode.ERROR, error.getMessage()); - span.recordException(error); - }) - .compose(Tracing.trace("call_llm")) - .concatMap( - llmResp -> - handleAfterModelCallback(context, llmResp, eventForCallbackUsage) - .toFlowable()); - }); + return handleBeforeModelCallback( + context, llmRequestBuilder, eventForCallbackUsage, otelParentContext) + .flatMapPublisher(Flowable::just) + .switchIfEmpty( + Flowable.defer( + () -> { + BaseLlm llm = + agent.resolvedModel().model().isPresent() + ? agent.resolvedModel().model().get() + : LlmRegistry.getLlm(agent.resolvedModel().modelName().get()); + return llm.generateContent( + llmRequestBuilder.build(), + context.runConfig().streamingMode() == StreamingMode.SSE) + .onErrorResumeNext( + exception -> + handleOnModelErrorCallback( + context, + llmRequestBuilder, + eventForCallbackUsage, + exception, + otelParentContext) + .switchIfEmpty(Single.error(exception)) + .toFlowable()) + .compose( + Tracing.trace("call_llm", otelParentContext) + .onSuccess( + (span, llmResp) -> + Tracing.traceCallLlm( + span, + context, + eventForCallbackUsage.id(), + llmRequestBuilder.build(), + llmResp))) + .doOnError( + error -> { + Span span = Span.current(); + span.setStatus(StatusCode.ERROR, error.getMessage()); + span.recordException(error); + }) + .concatMap( + llmResp -> + handleAfterModelCallback( + context, llmResp, eventForCallbackUsage, otelParentContext) + .toFlowable()); + })); } /** * Invokes {@link BeforeModelCallback}s. If any returns a response, it's used instead of calling * the LLM. * - * @return A {@link Single} with the callback result or {@link Optional#empty()}. + * @return A {@link Maybe} with the callback result. */ - private Single> handleBeforeModelCallback( - InvocationContext context, LlmRequest.Builder llmRequestBuilder, Event modelResponseEvent) { - Event callbackEvent = modelResponseEvent.toBuilder().build(); - CallbackContext callbackContext = - new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); + private Maybe handleBeforeModelCallback( + InvocationContext context, + LlmRequest.Builder llmRequestBuilder, + Event modelResponseEvent, + Context otelParentContext) { + try (Scope scope = otelParentContext.makeCurrent()) { + Event callbackEvent = modelResponseEvent.toBuilder().build(); + CallbackContext callbackContext = + new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); - Maybe pluginResult = - context.pluginManager().beforeModelCallback(callbackContext, llmRequestBuilder); + Maybe pluginResult = + context.pluginManager().beforeModelCallback(callbackContext, llmRequestBuilder); - LlmAgent agent = (LlmAgent) context.agent(); + LlmAgent agent = (LlmAgent) context.agent(); - List callbacks = agent.canonicalBeforeModelCallbacks(); - if (callbacks.isEmpty()) { - return pluginResult.map(Optional::of).defaultIfEmpty(Optional.empty()); - } + List callbacks = agent.canonicalBeforeModelCallbacks(); + if (callbacks.isEmpty()) { + return pluginResult; + } - Maybe callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe(callback -> callback.call(callbackContext, llmRequestBuilder)) - .firstElement()); - - return pluginResult - .switchIfEmpty(callbackResult) - .map(Optional::of) - .defaultIfEmpty(Optional.empty()); + Maybe callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe( + callback -> + callback + .call(callbackContext, llmRequestBuilder) + .compose(Tracing.withContext(otelParentContext))) + .firstElement()); + + return pluginResult.switchIfEmpty(callbackResult); + } } /** @@ -254,32 +295,41 @@ private Maybe handleOnModelErrorCallback( InvocationContext context, LlmRequest.Builder llmRequestBuilder, Event modelResponseEvent, - Throwable throwable) { - Event callbackEvent = modelResponseEvent.toBuilder().build(); - CallbackContext callbackContext = - new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); - Exception ex = throwable instanceof Exception e ? e : new Exception(throwable); - - Maybe pluginResult = - context.pluginManager().onModelErrorCallback(callbackContext, llmRequestBuilder, throwable); - - LlmAgent agent = (LlmAgent) context.agent(); - List callbacks = agent.canonicalOnModelErrorCallbacks(); + Throwable throwable, + Context otelParentContext) { + + try (Scope scope = otelParentContext.makeCurrent()) { + Event callbackEvent = modelResponseEvent.toBuilder().build(); + CallbackContext callbackContext = + new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); + Exception ex = throwable instanceof Exception e ? e : new Exception(throwable); + Maybe pluginResult = + context + .pluginManager() + .onModelErrorCallback(callbackContext, llmRequestBuilder, throwable); + + LlmAgent agent = (LlmAgent) context.agent(); + List callbacks = agent.canonicalOnModelErrorCallbacks(); + + if (callbacks.isEmpty()) { + return pluginResult; + } - if (callbacks.isEmpty()) { - return pluginResult; + Maybe callbackResult = + Maybe.defer( + () -> { + LlmRequest llmRequest = llmRequestBuilder.build(); + return Flowable.fromIterable(callbacks) + .concatMapMaybe( + callback -> + callback + .call(callbackContext, llmRequest, ex) + .compose(Tracing.withContext(otelParentContext))) + .firstElement(); + }); + + return pluginResult.switchIfEmpty(callbackResult); } - - Maybe callbackResult = - Maybe.defer( - () -> { - LlmRequest llmRequest = llmRequestBuilder.build(); - return Flowable.fromIterable(callbacks) - .concatMapMaybe(callback -> callback.call(callbackContext, llmRequest, ex)) - .firstElement(); - }); - - return pluginResult.switchIfEmpty(callbackResult); } /** @@ -289,29 +339,39 @@ private Maybe handleOnModelErrorCallback( * @return A {@link Single} with the final {@link LlmResponse}. */ private Single handleAfterModelCallback( - InvocationContext context, LlmResponse llmResponse, Event modelResponseEvent) { - Event callbackEvent = modelResponseEvent.toBuilder().build(); - CallbackContext callbackContext = - new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); + InvocationContext context, + LlmResponse llmResponse, + Event modelResponseEvent, + Context otelParentContext) { - Maybe pluginResult = - context.pluginManager().afterModelCallback(callbackContext, llmResponse); + try (Scope scope = otelParentContext.makeCurrent()) { + Event callbackEvent = modelResponseEvent.toBuilder().build(); + CallbackContext callbackContext = + new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); - LlmAgent agent = (LlmAgent) context.agent(); - List callbacks = agent.canonicalAfterModelCallbacks(); + Maybe pluginResult = + context.pluginManager().afterModelCallback(callbackContext, llmResponse); - if (callbacks.isEmpty()) { - return pluginResult.defaultIfEmpty(llmResponse); - } + LlmAgent agent = (LlmAgent) context.agent(); + List callbacks = agent.canonicalAfterModelCallbacks(); - Maybe callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe(callback -> callback.call(callbackContext, llmResponse)) - .firstElement()); + if (callbacks.isEmpty()) { + return pluginResult.defaultIfEmpty(llmResponse); + } - return pluginResult.switchIfEmpty(callbackResult).defaultIfEmpty(llmResponse); + Maybe callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe( + callback -> + callback + .call(callbackContext, llmResponse) + .compose(Tracing.withContext(otelParentContext))) + .firstElement()); + + return pluginResult.switchIfEmpty(callbackResult).defaultIfEmpty(llmResponse); + } } /** @@ -323,13 +383,12 @@ private Single handleAfterModelCallback( * @throws LlmCallsLimitExceededException if the agent exceeds allowed LLM invocations. * @throws IllegalStateException if a transfer agent is specified but not found. */ - private Flowable runOneStep(InvocationContext context) { + private Flowable runOneStep(InvocationContext context, Context otelParentContext) { AtomicReference llmRequestRef = new AtomicReference<>(LlmRequest.builder().build()); return Flowable.defer( () -> { - Context currentContext = Context.current(); - return preprocess(context, llmRequestRef) + return preprocess(context, llmRequestRef, otelParentContext) .concatWith( Flowable.defer( () -> { @@ -355,15 +414,19 @@ private Flowable runOneStep(InvocationContext context) { .build(); mutableEventTemplate.setTimestamp(0L); - return callLlm(context, llmRequestAfterPreprocess, mutableEventTemplate) + return callLlm( + context, + llmRequestAfterPreprocess, + mutableEventTemplate, + otelParentContext) .concatMap( - llmResponse -> { - try (Scope postScope = currentContext.makeCurrent()) { - return postprocess( + llmResponse -> + postprocess( context, mutableEventTemplate, llmRequestAfterPreprocess, - llmResponse) + llmResponse, + otelParentContext) .doFinally( () -> { String oldId = mutableEventTemplate.id(); @@ -371,9 +434,7 @@ private Flowable runOneStep(InvocationContext context) { logger.debug( "Resetting event ID from {} to {}", oldId, newId); mutableEventTemplate.setId(newId); - }); - } - }) + })) .concatMap( event -> { Flowable postProcessedEvents = Flowable.just(event); @@ -407,11 +468,12 @@ private Flowable runOneStep(InvocationContext context) { */ @Override public Flowable run(InvocationContext invocationContext) { - return run(invocationContext, 0); + return run(invocationContext, Context.current(), 0); } - private Flowable run(InvocationContext invocationContext, int stepsCompleted) { - Flowable currentStepEvents = runOneStep(invocationContext).cache(); + private Flowable run( + InvocationContext invocationContext, Context otelParentContext, int stepsCompleted) { + Flowable currentStepEvents = runOneStep(invocationContext, otelParentContext).cache(); if (stepsCompleted + 1 >= maxSteps) { logger.debug("Ending flow execution because max steps reached."); return currentStepEvents; @@ -431,7 +493,7 @@ private Flowable run(InvocationContext invocationContext, int stepsComple return Flowable.empty(); } else { logger.debug("Continuing to next step of the flow."); - return run(invocationContext, stepsCompleted + 1); + return run(invocationContext, otelParentContext, stepsCompleted + 1); } })); } @@ -446,8 +508,10 @@ private Flowable run(InvocationContext invocationContext, int stepsComple */ @Override public Flowable runLive(InvocationContext invocationContext) { + Context otelParentContext = Context.current(); AtomicReference llmRequestRef = new AtomicReference<>(LlmRequest.builder().build()); - Flowable preprocessEvents = preprocess(invocationContext, llmRequestRef); + Flowable preprocessEvents = + preprocess(invocationContext, llmRequestRef, otelParentContext); return preprocessEvents.concatWith( Flowable.defer( @@ -469,6 +533,7 @@ public Flowable runLive(InvocationContext invocationContext) { ? Completable.complete() : connection .sendHistory(llmRequestAfterPreprocess.contents()) + .compose(Tracing.trace("send_data", otelParentContext)) .doOnComplete( () -> Tracing.traceSendData( @@ -484,8 +549,7 @@ public Flowable runLive(InvocationContext invocationContext) { invocationContext, eventIdForSendData, llmRequestAfterPreprocess.contents()); - }) - .compose(Tracing.trace("send_data")); + }); Flowable liveRequests = invocationContext @@ -542,13 +606,16 @@ public void onError(Throwable e) { .receive() .flatMap( llmResponse -> { - Event baseEventForThisLlmResponse = - liveEventBuilderTemplate.id(Event.generateEventId()).build(); - return postprocess( - invocationContext, - baseEventForThisLlmResponse, - llmRequestAfterPreprocess, - llmResponse); + try (Scope scope = otelParentContext.makeCurrent()) { + Event baseEventForLlmResponse = + liveEventBuilderTemplate.id(Event.generateEventId()).build(); + return postprocess( + invocationContext, + baseEventForLlmResponse, + llmRequestAfterPreprocess, + llmResponse, + otelParentContext); + } }) .flatMap( event -> { @@ -600,7 +667,8 @@ private Flowable buildPostprocessingEvents( List> eventIterables, InvocationContext context, Event baseEventForLlmResponse, - LlmRequest llmRequest) { + LlmRequest llmRequest, + Context otelParentContext) { Flowable processorEvents = Flowable.fromIterable(Iterables.concat(eventIterables)); if (updatedResponse.content().isEmpty() && updatedResponse.errorCode().isEmpty() @@ -616,23 +684,27 @@ private Flowable buildPostprocessingEvents( return processorEvents.concatWith(Flowable.just(modelResponseEvent)); } - Maybe maybeFunctionResponseEvent = - context.runConfig().streamingMode() == StreamingMode.BIDI - ? Functions.handleFunctionCallsLive(context, modelResponseEvent, llmRequest.tools()) - : Functions.handleFunctionCalls(context, modelResponseEvent, llmRequest.tools()); - - Flowable functionEvents = - maybeFunctionResponseEvent.flatMapPublisher( - functionResponseEvent -> { - Optional toolConfirmationEvent = - Functions.generateRequestConfirmationEvent( - context, modelResponseEvent, functionResponseEvent); - return toolConfirmationEvent.isPresent() - ? Flowable.just(toolConfirmationEvent.get(), functionResponseEvent) - : Flowable.just(functionResponseEvent); - }); - - return processorEvents.concatWith(Flowable.just(modelResponseEvent)).concatWith(functionEvents); + try (Scope scope = otelParentContext.makeCurrent()) { + Maybe maybeFunctionResponseEvent = + context.runConfig().streamingMode() == StreamingMode.BIDI + ? Functions.handleFunctionCallsLive(context, modelResponseEvent, llmRequest.tools()) + : Functions.handleFunctionCalls(context, modelResponseEvent, llmRequest.tools()); + + Flowable functionEvents = + maybeFunctionResponseEvent.flatMapPublisher( + functionResponseEvent -> { + Optional toolConfirmationEvent = + Functions.generateRequestConfirmationEvent( + context, modelResponseEvent, functionResponseEvent); + return toolConfirmationEvent.isPresent() + ? Flowable.just(toolConfirmationEvent.get(), functionResponseEvent) + : Flowable.just(functionResponseEvent); + }); + + return processorEvents + .concatWith(Flowable.just(modelResponseEvent)) + .concatWith(functionEvents); + } } private Event buildModelResponseEvent( diff --git a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java index ecc2bb412..f8b9e180d 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java @@ -257,7 +257,8 @@ private static Function> getFunctionCallMapper( functionCall.args().map(HashMap::new).orElse(new HashMap<>()); Maybe> maybeFunctionResult = - maybeInvokeBeforeToolCall(invocationContext, tool, functionArgs, toolContext) + maybeInvokeBeforeToolCall( + invocationContext, tool, functionArgs, toolContext, parentContext) .switchIfEmpty( Maybe.defer( () -> { @@ -395,48 +396,49 @@ private static Maybe postProcessFunctionResult( .defaultIfEmpty(Optional.empty()) .onErrorResumeNext( t -> { - Maybe> errorCallbackResult = - handleOnToolErrorCallback(invocationContext, tool, functionArgs, toolContext, t); - Maybe>> mappedResult; - if (isLive) { - // In live mode, handle null results from the error callback gracefully. - mappedResult = errorCallbackResult.map(Optional::ofNullable); - } else { - // In non-live mode, a null result from the error callback will cause an NPE - // when wrapped with Optional.of(), potentially matching prior behavior. - mappedResult = errorCallbackResult.map(Optional::of); + try (Scope scope = parentContext.makeCurrent()) { + Maybe> errorCallbackResult = + handleOnToolErrorCallback( + invocationContext, tool, functionArgs, toolContext, t, parentContext); + Maybe>> mappedResult; + if (isLive) { + // In live mode, handle null results from the error callback gracefully. + mappedResult = errorCallbackResult.map(Optional::ofNullable); + } else { + // In non-live mode, a null result from the error callback will cause an NPE + // when wrapped with Optional.of(), potentially matching prior behavior. + mappedResult = errorCallbackResult.map(Optional::of); + } + return mappedResult.switchIfEmpty(Single.error(t)); } - return mappedResult.switchIfEmpty(Single.error(t)); }) .flatMapMaybe( optionalInitialResult -> { - try (Scope scope = parentContext.makeCurrent()) { - Map initialFunctionResult = optionalInitialResult.orElse(null); - - return maybeInvokeAfterToolCall( - invocationContext, tool, functionArgs, toolContext, initialFunctionResult) - .map(Optional::of) - .defaultIfEmpty(Optional.ofNullable(initialFunctionResult)) - .flatMapMaybe( - finalOptionalResult -> { - Map finalFunctionResult = - finalOptionalResult.orElse(null); - if (tool.longRunning() && finalFunctionResult == null) { - return Maybe.empty(); - } - return Maybe.fromCallable( - () -> - buildResponseEvent( - tool, - finalFunctionResult, - toolContext, - invocationContext)) - .compose( - Tracing.trace( - "tool_response [" + tool.name() + "]", parentContext)) - .doOnSuccess(event -> Tracing.traceToolResponse(event.id(), event)); - }); - } + Map initialFunctionResult = optionalInitialResult.orElse(null); + + return maybeInvokeAfterToolCall( + invocationContext, + tool, + functionArgs, + toolContext, + initialFunctionResult, + parentContext) + .map(Optional::of) + .defaultIfEmpty(Optional.ofNullable(initialFunctionResult)) + .flatMapMaybe( + finalOptionalResult -> { + Map finalFunctionResult = finalOptionalResult.orElse(null); + if (tool.longRunning() && finalFunctionResult == null) { + return Maybe.empty(); + } + return Maybe.fromCallable( + () -> + buildResponseEvent( + tool, finalFunctionResult, toolContext, invocationContext)) + .compose( + Tracing.trace("tool_response [" + tool.name() + "]", parentContext)) + .doOnSuccess(event -> Tracing.traceToolResponse(event.id(), event)); + }); }); } @@ -479,28 +481,32 @@ private static Maybe> maybeInvokeBeforeToolCall( InvocationContext invocationContext, BaseTool tool, Map functionArgs, - ToolContext toolContext) { - if (invocationContext.agent() instanceof LlmAgent) { - LlmAgent agent = (LlmAgent) invocationContext.agent(); + ToolContext toolContext, + Context parentContext) { + if (invocationContext.agent() instanceof LlmAgent agent) { + try (Scope scope = parentContext.makeCurrent()) { - Maybe> pluginResult = - invocationContext.pluginManager().beforeToolCallback(tool, functionArgs, toolContext); + Maybe> pluginResult = + invocationContext.pluginManager().beforeToolCallback(tool, functionArgs, toolContext); - List callbacks = agent.canonicalBeforeToolCallbacks(); - if (callbacks.isEmpty()) { - return pluginResult; - } - - Maybe> callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe( - callback -> - callback.call(invocationContext, tool, functionArgs, toolContext)) - .firstElement()); + List callbacks = agent.canonicalBeforeToolCallbacks(); + if (callbacks.isEmpty()) { + return pluginResult; + } - return pluginResult.switchIfEmpty(callbackResult); + Maybe> callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe( + callback -> + callback + .call(invocationContext, tool, functionArgs, toolContext) + .compose(Tracing.withContext(parentContext))) + .firstElement()); + + return pluginResult.switchIfEmpty(callbackResult); + } } return Maybe.empty(); } @@ -516,34 +522,39 @@ private static Maybe> handleOnToolErrorCallback( BaseTool tool, Map functionArgs, ToolContext toolContext, - Throwable throwable) { + Throwable throwable, + Context parentContext) { Exception ex = throwable instanceof Exception exception ? exception : new Exception(throwable); - Maybe> pluginResult = - invocationContext - .pluginManager() - .onToolErrorCallback(tool, functionArgs, toolContext, throwable); - - if (invocationContext.agent() instanceof LlmAgent) { - LlmAgent agent = (LlmAgent) invocationContext.agent(); + try (Scope scope = parentContext.makeCurrent()) { + Maybe> pluginResult = + invocationContext + .pluginManager() + .onToolErrorCallback(tool, functionArgs, toolContext, throwable); - List callbacks = agent.canonicalOnToolErrorCallbacks(); - if (callbacks.isEmpty()) { - return pluginResult; - } + if (invocationContext.agent() instanceof LlmAgent) { + LlmAgent agent = (LlmAgent) invocationContext.agent(); - Maybe> callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe( - callback -> - callback.call(invocationContext, tool, functionArgs, toolContext, ex)) - .firstElement()); + List callbacks = agent.canonicalOnToolErrorCallbacks(); + if (callbacks.isEmpty()) { + return pluginResult; + } - return pluginResult.switchIfEmpty(callbackResult); + Maybe> callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe( + callback -> + callback + .call(invocationContext, tool, functionArgs, toolContext, ex) + .compose(Tracing.withContext(parentContext))) + .firstElement()); + + return pluginResult.switchIfEmpty(callbackResult); + } + return pluginResult; } - return pluginResult; } private static Maybe> maybeInvokeAfterToolCall( @@ -551,35 +562,39 @@ private static Maybe> maybeInvokeAfterToolCall( BaseTool tool, Map functionArgs, ToolContext toolContext, - Map functionResult) { - if (invocationContext.agent() instanceof LlmAgent) { - LlmAgent agent = (LlmAgent) invocationContext.agent(); + Map functionResult, + Context parentContext) { + if (invocationContext.agent() instanceof LlmAgent agent) { - Maybe> pluginResult = - invocationContext - .pluginManager() - .afterToolCallback(tool, functionArgs, toolContext, functionResult); + try (Scope scope = parentContext.makeCurrent()) { + Maybe> pluginResult = + invocationContext + .pluginManager() + .afterToolCallback(tool, functionArgs, toolContext, functionResult); - List callbacks = agent.canonicalAfterToolCallbacks(); - if (callbacks.isEmpty()) { - return pluginResult; - } + List callbacks = agent.canonicalAfterToolCallbacks(); + if (callbacks.isEmpty()) { + return pluginResult; + } - Maybe> callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe( - callback -> - callback.call( - invocationContext, - tool, - functionArgs, - toolContext, - functionResult)) - .firstElement()); - - return pluginResult.switchIfEmpty(callbackResult); + Maybe> callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe( + callback -> + callback + .call( + invocationContext, + tool, + functionArgs, + toolContext, + functionResult) + .compose(Tracing.withContext(parentContext))) + .firstElement()); + + return pluginResult.switchIfEmpty(callbackResult); + } } return Maybe.empty(); } diff --git a/core/src/main/java/com/google/adk/plugins/PluginManager.java b/core/src/main/java/com/google/adk/plugins/PluginManager.java index 56dea936a..4d90ca7b5 100644 --- a/core/src/main/java/com/google/adk/plugins/PluginManager.java +++ b/core/src/main/java/com/google/adk/plugins/PluginManager.java @@ -21,11 +21,13 @@ import com.google.adk.events.Event; import com.google.adk.models.LlmRequest; import com.google.adk.models.LlmResponse; +import com.google.adk.telemetry.Tracing; import com.google.adk.tools.BaseTool; import com.google.adk.tools.ToolContext; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.genai.types.Content; +import io.opentelemetry.context.Context; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; @@ -129,6 +131,7 @@ public Completable runAfterRunCallback(InvocationContext invocationContext) { @Override public Completable afterRunCallback(InvocationContext invocationContext) { + Context capturedContext = Context.current(); return Flowable.fromIterable(plugins) .concatMapCompletable( plugin -> @@ -139,11 +142,13 @@ public Completable afterRunCallback(InvocationContext invocationContext) { logger.error( "[{}] Error during callback 'afterRunCallback'", plugin.getName(), - e))); + e)) + .compose(Tracing.withContext(capturedContext))); } @Override public Completable close() { + Context capturedContext = Context.current(); return Flowable.fromIterable(plugins) .concatMapCompletableDelayError( plugin -> @@ -151,8 +156,8 @@ public Completable close() { .close() .doOnError( e -> - logger.error( - "[{}] Error during callback 'close'", plugin.getName(), e))); + logger.error("[{}] Error during callback 'close'", plugin.getName(), e)) + .compose(Tracing.withContext(capturedContext))); } public Maybe runOnEventCallback(InvocationContext invocationContext, Event event) { @@ -275,7 +280,7 @@ public Maybe> onToolErrorCallback( */ private Maybe runMaybeCallbacks( Function> callbackExecutor, String callbackName) { - + Context capturedContext = Context.current(); return Flowable.fromIterable(this.plugins) .concatMapMaybe( plugin -> @@ -294,7 +299,8 @@ private Maybe runMaybeCallbacks( "[{}] Error during callback '{}'", plugin.getName(), callbackName, - e))) + e)) + .compose(Tracing.withContext(capturedContext))) .firstElement(); } } diff --git a/core/src/main/java/com/google/adk/runner/Runner.java b/core/src/main/java/com/google/adk/runner/Runner.java index 4371300fb..e35f5c33d 100644 --- a/core/src/main/java/com/google/adk/runner/Runner.java +++ b/core/src/main/java/com/google/adk/runner/Runner.java @@ -52,6 +52,7 @@ import com.google.genai.types.Part; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.context.Context; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; @@ -375,20 +376,25 @@ public Flowable runAsync( Content newMessage, RunConfig runConfig, @Nullable Map stateDelta) { - Maybe maybeSession = - this.sessionService.getSession(appName, userId, sessionId, Optional.empty()); - return maybeSession - .switchIfEmpty( - Single.defer( - () -> { - if (runConfig.autoCreateSession()) { - return this.sessionService.createSession(appName, userId, null, sessionId); - } - return Single.error( - new IllegalArgumentException( - String.format("Session not found: %s for user %s", sessionId, userId))); - })) - .flatMapPublisher(session -> this.runAsyncImpl(session, newMessage, runConfig, stateDelta)); + return Flowable.defer( + () -> + this.sessionService + .getSession(appName, userId, sessionId, Optional.empty()) + .switchIfEmpty( + Single.defer( + () -> { + if (runConfig.autoCreateSession()) { + return this.sessionService.createSession( + appName, userId, (Map) null, sessionId); + } + return Single.error( + new IllegalArgumentException( + String.format( + "Session not found: %s for user %s", sessionId, userId))); + })) + .flatMapPublisher( + session -> this.runAsyncImpl(session, newMessage, runConfig, stateDelta))) + .compose(Tracing.trace("invocation")); } /** See {@link #runAsync(String, String, Content, RunConfig, Map)}. */ @@ -441,7 +447,8 @@ public Flowable runAsync( Content newMessage, RunConfig runConfig, @Nullable Map stateDelta) { - return runAsyncImpl(session, newMessage, runConfig, stateDelta); + return runAsyncImpl(session, newMessage, runConfig, stateDelta) + .compose(Tracing.trace("invocation")); } /** @@ -460,6 +467,7 @@ protected Flowable runAsyncImpl( @Nullable Map stateDelta) { return Flowable.defer( () -> { + Context capturedContext = Context.current(); BaseAgent rootAgent = this.agent; String invocationId = InvocationContext.newInvocationContextId(); @@ -473,6 +481,7 @@ protected Flowable runAsyncImpl( return this.pluginManager .onUserMessageCallback(initialContext, newMessage) + .compose(Tracing.withContext(capturedContext)) .defaultIfEmpty(newMessage) .flatMap( content -> @@ -484,6 +493,7 @@ protected Flowable runAsyncImpl( runConfig.saveInputBlobsAsArtifacts(), stateDelta) : Single.just(null)) + .compose(Tracing.withContext(capturedContext)) .flatMapPublisher( event -> { if (event == null) { @@ -494,15 +504,17 @@ protected Flowable runAsyncImpl( return this.sessionService .getSession( session.appName(), session.userId(), session.id(), Optional.empty()) + .compose(Tracing.withContext(capturedContext)) .flatMapPublisher( updatedSession -> runAgentWithFreshSession( - session, - updatedSession, - event, - invocationId, - runConfig, - rootAgent)); + session, + updatedSession, + event, + invocationId, + runConfig, + rootAgent) + .compose(Tracing.withContext(capturedContext))); }); }) .doOnError( @@ -510,8 +522,7 @@ protected Flowable runAsyncImpl( Span span = Span.current(); span.setStatus(StatusCode.ERROR, "Error in runAsync Flowable execution"); span.recordException(throwable); - }) - .compose(Tracing.trace("invocation")); + }); } private Flowable runAgentWithFreshSession( @@ -568,7 +579,7 @@ private Flowable runAgentWithFreshSession( .toFlowable() .switchIfEmpty(agentEvents) .concatWith( - Completable.defer(() -> pluginManager.runAfterRunCallback(contextWithUpdatedSession))) + Completable.defer(() -> pluginManager.afterRunCallback(contextWithUpdatedSession))) .concatWith(Completable.defer(() -> compactEvents(updatedSession))); } @@ -641,39 +652,51 @@ private InvocationContext.Builder newInvocationContextBuilder(Session session) { */ public Flowable runLive( Session session, LiveRequestQueue liveRequestQueue, RunConfig runConfig) { + return runLiveImpl(session, liveRequestQueue, runConfig).compose(Tracing.trace("invocation")); + } + + /** + * Runs the agent in live mode, appending generated events to the session. + * + * @return stream of events from the agent. + */ + protected Flowable runLiveImpl( + Session session, @Nullable LiveRequestQueue liveRequestQueue, RunConfig runConfig) { return Flowable.defer( - () -> { - InvocationContext invocationContext = - newInvocationContextForLive(session, liveRequestQueue, runConfig); - - Single invocationContextSingle; - if (invocationContext.agent() instanceof LlmAgent agent) { - invocationContextSingle = - agent - .tools() - .map( - tools -> { - this.addActiveStreamingTools(invocationContext, tools); - return invocationContext; - }); - } else { - invocationContextSingle = Single.just(invocationContext); - } - return invocationContextSingle - .flatMapPublisher( - updatedInvocationContext -> - updatedInvocationContext - .agent() - .runLive(updatedInvocationContext) - .doOnNext(event -> this.sessionService.appendEvent(session, event))) - .doOnError( - throwable -> { - Span span = Span.current(); - span.setStatus(StatusCode.ERROR, "Error in runLive Flowable execution"); - span.recordException(throwable); - }); - }) - .compose(Tracing.trace("invocation")); + () -> { + Context capturedContext = Context.current(); + InvocationContext invocationContext = + newInvocationContextForLive(session, liveRequestQueue, runConfig); + + Single invocationContextSingle; + if (invocationContext.agent() instanceof LlmAgent agent) { + invocationContextSingle = + agent + .tools() + .map( + tools -> { + this.addActiveStreamingTools(invocationContext, tools); + return invocationContext; + }); + } else { + invocationContextSingle = Single.just(invocationContext); + } + return invocationContextSingle + .compose(Tracing.withContext(capturedContext)) + .flatMapPublisher( + updatedInvocationContext -> + updatedInvocationContext + .agent() + .runLive(updatedInvocationContext) + .compose(Tracing.withContext(capturedContext)) + .doOnNext(event -> this.sessionService.appendEvent(session, event))) + .doOnError( + throwable -> { + Span span = Span.current(); + span.setStatus(StatusCode.ERROR, "Error in runLive Flowable execution"); + span.recordException(throwable); + }); + }); } /** @@ -684,19 +707,25 @@ public Flowable runLive( */ public Flowable runLive( String userId, String sessionId, LiveRequestQueue liveRequestQueue, RunConfig runConfig) { - return this.sessionService - .getSession(appName, userId, sessionId, Optional.empty()) - .switchIfEmpty( - Single.defer( - () -> { - if (runConfig.autoCreateSession()) { - return this.sessionService.createSession(appName, userId, null, sessionId); - } - return Single.error( - new IllegalArgumentException( - String.format("Session not found: %s for user %s", sessionId, userId))); - })) - .flatMapPublisher(session -> this.runLive(session, liveRequestQueue, runConfig)); + return Flowable.defer( + () -> + this.sessionService + .getSession(appName, userId, sessionId, Optional.empty()) + .switchIfEmpty( + Single.defer( + () -> { + if (runConfig.autoCreateSession()) { + return this.sessionService.createSession( + appName, userId, (Map) null, sessionId); + } + return Single.error( + new IllegalArgumentException( + String.format( + "Session not found: %s for user %s", sessionId, userId))); + })) + .flatMapPublisher( + session -> this.runLiveImpl(session, liveRequestQueue, runConfig))) + .compose(Tracing.trace("invocation")); } /** diff --git a/core/src/main/java/com/google/adk/telemetry/Tracing.java b/core/src/main/java/com/google/adk/telemetry/Tracing.java index 07a640c37..9fa68ee00 100644 --- a/core/src/main/java/com/google/adk/telemetry/Tracing.java +++ b/core/src/main/java/com/google/adk/telemetry/Tracing.java @@ -37,16 +37,20 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.reactivex.rxjava3.core.Completable; +import io.reactivex.rxjava3.core.CompletableObserver; import io.reactivex.rxjava3.core.CompletableSource; import io.reactivex.rxjava3.core.CompletableTransformer; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.FlowableTransformer; import io.reactivex.rxjava3.core.Maybe; +import io.reactivex.rxjava3.core.MaybeObserver; import io.reactivex.rxjava3.core.MaybeSource; import io.reactivex.rxjava3.core.MaybeTransformer; import io.reactivex.rxjava3.core.Single; +import io.reactivex.rxjava3.core.SingleObserver; import io.reactivex.rxjava3.core.SingleSource; import io.reactivex.rxjava3.core.SingleTransformer; +import io.reactivex.rxjava3.disposables.Disposable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -54,9 +58,12 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Supplier; import org.reactivestreams.Publisher; +import org.reactivestreams.Subscriber; +import org.reactivestreams.Subscription; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -136,6 +143,10 @@ private static Optional getValidCurrentSpan(String methodName) { return Optional.of(span); } + private static void traceWithSpan(String methodName, Consumer action) { + getValidCurrentSpan(methodName).ifPresent(action); + } + private static void setInvocationAttributes( Span span, InvocationContext invocationContext, String eventId) { span.setAttribute(ADK_INVOCATION_ID, invocationContext.invocationId()); @@ -206,16 +217,16 @@ public static void traceAgentInvocation( */ public static void traceToolCall( String toolName, String toolDescription, String toolType, Map args) { - getValidCurrentSpan("traceToolCall") - .ifPresent( - span -> { - setToolExecutionAttributes(span); - span.setAttribute(GEN_AI_TOOL_NAME, toolName); - span.setAttribute(GEN_AI_TOOL_DESCRIPTION, toolDescription); - span.setAttribute(GEN_AI_TOOL_TYPE, toolType); - - setJsonAttribute(span, ADK_TOOL_CALL_ARGS, args); - }); + traceWithSpan( + "traceToolCall", + span -> { + setToolExecutionAttributes(span); + span.setAttribute(GEN_AI_TOOL_NAME, toolName); + span.setAttribute(GEN_AI_TOOL_DESCRIPTION, toolDescription); + span.setAttribute(GEN_AI_TOOL_TYPE, toolType); + + setJsonAttribute(span, ADK_TOOL_CALL_ARGS, args); + }); } /** @@ -225,33 +236,32 @@ public static void traceToolCall( * @param functionResponseEvent The function response event. */ public static void traceToolResponse(String eventId, Event functionResponseEvent) { - getValidCurrentSpan("traceToolResponse") - .ifPresent( - span -> { - setToolExecutionAttributes(span); - span.setAttribute(ADK_EVENT_ID, eventId); - - FunctionResponse functionResponse = - functionResponseEvent.functionResponses().stream().findFirst().orElse(null); - - String toolCallId = ""; - Object toolResponse = ""; - if (functionResponse != null) { - toolCallId = functionResponse.id().orElse(toolCallId); - if (functionResponse.response().isPresent()) { - toolResponse = functionResponse.response().get(); - } - } - - span.setAttribute(GEN_AI_TOOL_CALL_ID, toolCallId); - - Object finalToolResponse = - (toolResponse instanceof Map) - ? toolResponse - : ImmutableMap.of("result", toolResponse); - - setJsonAttribute(span, ADK_TOOL_RESPONSE, finalToolResponse); - }); + traceWithSpan( + "traceToolResponse", + span -> { + setToolExecutionAttributes(span); + span.setAttribute(ADK_EVENT_ID, eventId); + + Optional functionResponse = + functionResponseEvent.functionResponses().stream().findFirst(); + + String toolCallId = + functionResponse.flatMap(FunctionResponse::id).orElse(""); + Object toolResponse = + functionResponse + .flatMap(FunctionResponse::response) + .map(Object.class::cast) + .orElse(""); + + span.setAttribute(GEN_AI_TOOL_CALL_ID, toolCallId); + + Object finalToolResponse = + (toolResponse instanceof Map) + ? toolResponse + : ImmutableMap.of("result", toolResponse); + + setJsonAttribute(span, ADK_TOOL_RESPONSE, finalToolResponse); + }); } /** @@ -296,58 +306,63 @@ public static void traceCallLlm( String eventId, LlmRequest llmRequest, LlmResponse llmResponse) { - getValidCurrentSpan("traceCallLlm") - .ifPresent( - span -> { - span.setAttribute(GEN_AI_SYSTEM, "gcp.vertex.agent"); - llmRequest - .model() - .ifPresent(modelName -> span.setAttribute(GEN_AI_REQUEST_MODEL, modelName)); - - setInvocationAttributes(span, invocationContext, eventId); + traceWithSpan( + "traceCallLlm", + span -> traceCallLlm(span, invocationContext, eventId, llmRequest, llmResponse)); + } - setJsonAttribute(span, ADK_LLM_REQUEST, buildLlmRequestForTrace(llmRequest)); - setJsonAttribute(span, ADK_LLM_RESPONSE, llmResponse); + /** + * Traces a call to the LLM. + * + * @param span The span to end when the stream completes + * @param invocationContext The invocation context. + * @param eventId The ID of the event associated with this LLM call/response. + * @param llmRequest The LLM request object. + * @param llmResponse The LLM response object. + */ + public static void traceCallLlm( + Span span, + InvocationContext invocationContext, + String eventId, + LlmRequest llmRequest, + LlmResponse llmResponse) { + span.setAttribute(GEN_AI_OPERATION_NAME, "call_llm"); + span.setAttribute(GEN_AI_SYSTEM, "gcp.vertex.agent"); + llmRequest.model().ifPresent(modelName -> span.setAttribute(GEN_AI_REQUEST_MODEL, modelName)); + + setInvocationAttributes(span, invocationContext, eventId); + + setJsonAttribute(span, ADK_LLM_REQUEST, buildLlmRequestForTrace(llmRequest)); + setJsonAttribute(span, ADK_LLM_RESPONSE, llmResponse); + + llmRequest + .config() + .flatMap(config -> config.topP()) + .ifPresent(topP -> span.setAttribute(GEN_AI_REQUEST_TOP_P, topP.doubleValue())); + llmRequest + .config() + .flatMap(config -> config.maxOutputTokens()) + .ifPresent( + maxTokens -> span.setAttribute(GEN_AI_REQUEST_MAX_TOKENS, maxTokens.longValue())); - llmRequest - .config() - .ifPresent( - config -> { - config - .topP() - .ifPresent( - topP -> - span.setAttribute(GEN_AI_REQUEST_TOP_P, topP.doubleValue())); - config - .maxOutputTokens() - .ifPresent( - maxTokens -> - span.setAttribute( - GEN_AI_REQUEST_MAX_TOKENS, maxTokens.longValue())); - }); - llmResponse - .usageMetadata() - .ifPresent( - usage -> { - usage - .promptTokenCount() - .ifPresent( - tokens -> - span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, (long) tokens)); - usage - .candidatesTokenCount() - .ifPresent( - tokens -> - span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, (long) tokens)); - }); - llmResponse - .finishReason() - .map(reason -> reason.knownEnum().name().toLowerCase(Locale.ROOT)) + llmResponse + .usageMetadata() + .ifPresent( + usage -> { + usage + .promptTokenCount() + .ifPresent(tokens -> span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, (long) tokens)); + usage + .candidatesTokenCount() .ifPresent( - reason -> - span.setAttribute( - GEN_AI_RESPONSE_FINISH_REASONS, ImmutableList.of(reason))); + tokens -> span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, (long) tokens)); }); + + llmResponse + .finishReason() + .map(reason -> reason.knownEnum().name().toLowerCase(Locale.ROOT)) + .ifPresent( + reason -> span.setAttribute(GEN_AI_RESPONSE_FINISH_REASONS, ImmutableList.of(reason))); } /** @@ -359,17 +374,18 @@ public static void traceCallLlm( */ public static void traceSendData( InvocationContext invocationContext, String eventId, List data) { - getValidCurrentSpan("traceSendData") - .ifPresent( - span -> { - setInvocationAttributes(span, invocationContext, eventId); - - ImmutableList safeData = - Optional.ofNullable(data).orElse(ImmutableList.of()).stream() - .filter(Objects::nonNull) - .collect(toImmutableList()); - setJsonAttribute(span, ADK_DATA, safeData); - }); + traceWithSpan( + "traceSendData", + span -> { + span.setAttribute(GEN_AI_OPERATION_NAME, "send_data"); + setInvocationAttributes(span, invocationContext, eventId); + + ImmutableList safeData = + Optional.ofNullable(data).orElse(ImmutableList.of()).stream() + .filter(Objects::nonNull) + .collect(toImmutableList()); + setJsonAttribute(span, ADK_DATA, safeData); + }); } /** @@ -405,14 +421,17 @@ public static Tracer getTracer() { @SuppressWarnings("MustBeClosedChecker") // Scope lifecycle managed by RxJava doFinally public static Flowable traceFlowable( Context spanContext, Span span, Supplier> flowableSupplier) { - Scope scope = spanContext.makeCurrent(); - return flowableSupplier - .get() - .doFinally( - () -> { - scope.close(); - span.end(); - }); + return Flowable.defer( + () -> { + Scope scope = spanContext.makeCurrent(); + return flowableSupplier + .get() + .doFinally( + () -> { + scope.close(); + span.end(); + }); + }); } /** @@ -450,15 +469,66 @@ public static TracerProvider trace(String spanName, Context parentContext * @return A TracerProvider configured for agent invocation. */ public static TracerProvider traceAgent( + Context parent, String spanName, String agentName, String agentDescription, InvocationContext invocationContext) { return new TracerProvider(spanName) + .setParent(parent) .configure( span -> traceAgentInvocation(span, agentName, agentDescription, invocationContext)); } + /** + * Returns a transformer that re-activates a given context for the duration of the stream's + * subscription. + * + * @param context The context to re-activate. + * @param The type of the stream. + * @return A transformer that re-activates the context. + */ + public static ContextTransformer withContext(Context context) { + return new ContextTransformer<>(context); + } + + /** + * A transformer that re-activates a given context for the duration of the stream's subscription. + * + * @param The type of the stream. + */ + public static final class ContextTransformer + implements FlowableTransformer, + SingleTransformer, + MaybeTransformer, + CompletableTransformer { + private final Context context; + + private ContextTransformer(Context context) { + this.context = context; + } + + @Override + public Publisher apply(Flowable upstream) { + return upstream.lift(subscriber -> TracingObserver.wrap(context, subscriber)); + } + + @Override + public SingleSource apply(Single upstream) { + return upstream.lift(observer -> TracingObserver.wrap(context, observer)); + } + + @Override + public MaybeSource apply(Maybe upstream) { + return upstream.lift(observer -> TracingObserver.wrap(context, observer)); + } + + @Override + public CompletableSource apply(Completable upstream) { + return upstream.lift(observer -> TracingObserver.wrap(context, observer)); + } + } + /** * A transformer that manages an OpenTelemetry span and scope for RxJava streams. * @@ -472,6 +542,7 @@ public static final class TracerProvider private final String spanName; private Context explicitParentContext; private final List> spanConfigurers = new ArrayList<>(); + private BiConsumer onSuccessConsumer; private TracerProvider(String spanName) { this.spanName = spanName; @@ -491,27 +562,38 @@ public TracerProvider setParent(Context parentContext) { return this; } + /** + * Registers a callback to be executed with the span and the result item when the stream emits a + * success value. + */ + @CanIgnoreReturnValue + public TracerProvider onSuccess(BiConsumer consumer) { + this.onSuccessConsumer = consumer; + return this; + } + private Context getParentContext() { return explicitParentContext != null ? explicitParentContext : Context.current(); } private final class TracingLifecycle { - private Span span; - private Scope scope; + private final Span span; + private final Context context; - @SuppressWarnings("MustBeClosedChecker") - void start() { - span = tracer.spanBuilder(spanName).setParent(getParentContext()).startSpan(); + TracingLifecycle() { + Context parentContext = getParentContext(); + span = tracer.spanBuilder(spanName).setParent(parentContext).startSpan(); spanConfigurers.forEach(c -> c.accept(span)); - scope = span.makeCurrent(); + context = parentContext.with(span); } void end() { - if (scope != null) { - scope.close(); - } - if (span != null) { - span.end(); + span.end(); + } + + void run(O observer, Consumer subscribeAction) { + try (Scope scope = context.makeCurrent()) { + subscribeAction.accept(observer); } } } @@ -521,7 +603,18 @@ public Publisher apply(Flowable upstream) { return Flowable.defer( () -> { TracingLifecycle lifecycle = new TracingLifecycle(); - return upstream.doOnSubscribe(s -> lifecycle.start()).doFinally(lifecycle::end); + return Flowable.fromPublisher( + observer -> + lifecycle.run( + observer, + o -> { + Flowable chain = upstream.compose(withContext(lifecycle.context)); + if (onSuccessConsumer != null) { + chain = + chain.doOnNext(t -> onSuccessConsumer.accept(lifecycle.span, t)); + } + chain.doFinally(lifecycle::end).subscribe(o); + })); }); } @@ -530,7 +623,18 @@ public SingleSource apply(Single upstream) { return Single.defer( () -> { TracingLifecycle lifecycle = new TracingLifecycle(); - return upstream.doOnSubscribe(s -> lifecycle.start()).doFinally(lifecycle::end); + return Single.wrap( + observer -> + lifecycle.run( + observer, + o -> { + Single chain = upstream.compose(withContext(lifecycle.context)); + if (onSuccessConsumer != null) { + chain = + chain.doOnSuccess(t -> onSuccessConsumer.accept(lifecycle.span, t)); + } + chain.doFinally(lifecycle::end).subscribe(o); + })); }); } @@ -539,7 +643,18 @@ public MaybeSource apply(Maybe upstream) { return Maybe.defer( () -> { TracingLifecycle lifecycle = new TracingLifecycle(); - return upstream.doOnSubscribe(s -> lifecycle.start()).doFinally(lifecycle::end); + return Maybe.wrap( + observer -> + lifecycle.run( + observer, + o -> { + Maybe chain = upstream.compose(withContext(lifecycle.context)); + if (onSuccessConsumer != null) { + chain = + chain.doOnSuccess(t -> onSuccessConsumer.accept(lifecycle.span, t)); + } + chain.doFinally(lifecycle::end).subscribe(o); + })); }); } @@ -548,7 +663,142 @@ public CompletableSource apply(Completable upstream) { return Completable.defer( () -> { TracingLifecycle lifecycle = new TracingLifecycle(); - return upstream.doOnSubscribe(s -> lifecycle.start()).doFinally(lifecycle::end); + return Completable.wrap( + observer -> + lifecycle.run( + observer, + o -> { + Completable chain = upstream.compose(withContext(lifecycle.context)); + // Completable does not emit items, so onSuccessConsumer is not + // applicable. + chain.doFinally(lifecycle::end).subscribe(o); + })); + }); + } + } + + /** + * An observer that wraps another observer and ensures that the OpenTelemetry context is active + * during all callback methods. + * + * @param The type of the items emitted by the stream. + */ + private static final class TracingObserver + implements Subscriber, SingleObserver, MaybeObserver, CompletableObserver { + private final Context context; + private final Subscriber subscriber; + private final SingleObserver singleObserver; + private final MaybeObserver maybeObserver; + private final CompletableObserver completableObserver; + + private TracingObserver( + Context context, + Subscriber subscriber, + SingleObserver singleObserver, + MaybeObserver maybeObserver, + CompletableObserver completableObserver) { + this.context = context; + this.subscriber = subscriber; + this.singleObserver = singleObserver; + this.maybeObserver = maybeObserver; + this.completableObserver = completableObserver; + } + + static TracingObserver wrap(Context context, Subscriber subscriber) { + return new TracingObserver<>(context, subscriber, null, null, null); + } + + static TracingObserver wrap(Context context, SingleObserver observer) { + return new TracingObserver<>(context, null, observer, null, null); + } + + static TracingObserver wrap(Context context, MaybeObserver observer) { + return new TracingObserver<>(context, null, null, observer, null); + } + + static TracingObserver wrap(Context context, CompletableObserver observer) { + return new TracingObserver<>(context, null, null, null, observer); + } + + private void runInContext(Runnable action) { + try (Scope scope = context.makeCurrent()) { + action.run(); + } + } + + @Override + public void onSubscribe(Subscription s) { + runInContext( + () -> { + if (subscriber != null) { + subscriber.onSubscribe(s); + } + }); + } + + @Override + public void onSubscribe(Disposable d) { + runInContext( + () -> { + if (singleObserver != null) { + singleObserver.onSubscribe(d); + } else if (maybeObserver != null) { + maybeObserver.onSubscribe(d); + } else if (completableObserver != null) { + completableObserver.onSubscribe(d); + } + }); + } + + @Override + public void onNext(T t) { + runInContext( + () -> { + if (subscriber != null) { + subscriber.onNext(t); + } + }); + } + + @Override + public void onSuccess(T t) { + runInContext( + () -> { + if (singleObserver != null) { + singleObserver.onSuccess(t); + } else if (maybeObserver != null) { + maybeObserver.onSuccess(t); + } + }); + } + + @Override + public void onError(Throwable t) { + runInContext( + () -> { + if (subscriber != null) { + subscriber.onError(t); + } else if (singleObserver != null) { + singleObserver.onError(t); + } else if (maybeObserver != null) { + maybeObserver.onError(t); + } else if (completableObserver != null) { + completableObserver.onError(t); + } + }); + } + + @Override + public void onComplete() { + runInContext( + () -> { + if (subscriber != null) { + subscriber.onComplete(); + } else if (maybeObserver != null) { + maybeObserver.onComplete(); + } else if (completableObserver != null) { + completableObserver.onComplete(); + } }); } } From 3c8f4886f0e4c76abdbeb64a348bfccd5c16120e Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Mon, 9 Mar 2026 18:45:28 -0700 Subject: [PATCH 10/25] feat: Fixing the spans produced by agent calls to have the right parent spans PiperOrigin-RevId: 881142814 --- .../java/com/google/adk/agents/BaseAgent.java | 107 ++-- .../adk/flows/llmflows/BaseLlmFlow.java | 390 ++++++-------- .../google/adk/flows/llmflows/Functions.java | 227 ++++---- .../com/google/adk/plugins/PluginManager.java | 16 +- .../java/com/google/adk/runner/Runner.java | 167 +++--- .../com/google/adk/telemetry/Tracing.java | 488 +++++------------- 6 files changed, 508 insertions(+), 887 deletions(-) diff --git a/core/src/main/java/com/google/adk/agents/BaseAgent.java b/core/src/main/java/com/google/adk/agents/BaseAgent.java index c527eeab3..d74ba9ca5 100644 --- a/core/src/main/java/com/google/adk/agents/BaseAgent.java +++ b/core/src/main/java/com/google/adk/agents/BaseAgent.java @@ -29,10 +29,10 @@ import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.DoNotCall; import com.google.genai.types.Content; -import io.opentelemetry.context.Context; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; +import io.reactivex.rxjava3.core.Single; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -312,47 +312,38 @@ public Flowable runAsync(InvocationContext parentContext) { private Flowable run( InvocationContext parentContext, Function> runImplementation) { - Context otelParentContext = Context.current(); - InvocationContext invocationContext = createInvocationContext(parentContext); - return Flowable.defer( - () -> { - return callCallback( - beforeCallbacksToFunctions( - invocationContext.pluginManager(), beforeAgentCallback), - invocationContext) - .flatMapPublisher( - beforeEvent -> { - if (invocationContext.endInvocation()) { - return Flowable.just(beforeEvent); - } - - return Flowable.just(beforeEvent) - .concatWith(runMainAndAfter(invocationContext, runImplementation)); - }) - .switchIfEmpty( - Flowable.defer(() -> runMainAndAfter(invocationContext, runImplementation))); - }) - .compose( - Tracing.traceAgent( - otelParentContext, - "invoke_agent " + name(), - name(), - description(), - invocationContext)); - } - - private Flowable runMainAndAfter( - InvocationContext invocationContext, - Function> runImplementation) { - Flowable mainEvents = runImplementation.apply(invocationContext); - Flowable afterEvents = - callCallback( - afterCallbacksToFunctions(invocationContext.pluginManager(), afterAgentCallback), - invocationContext) - .flatMapPublisher(Flowable::just); - - return Flowable.concat(mainEvents, afterEvents); + () -> { + InvocationContext invocationContext = createInvocationContext(parentContext); + + return callCallback( + beforeCallbacksToFunctions( + invocationContext.pluginManager(), beforeAgentCallback), + invocationContext) + .flatMapPublisher( + beforeEventOpt -> { + if (invocationContext.endInvocation()) { + return Flowable.fromOptional(beforeEventOpt); + } + + Flowable beforeEvents = Flowable.fromOptional(beforeEventOpt); + Flowable mainEvents = + Flowable.defer(() -> runImplementation.apply(invocationContext)); + Flowable afterEvents = + Flowable.defer( + () -> + callCallback( + afterCallbacksToFunctions( + invocationContext.pluginManager(), afterAgentCallback), + invocationContext) + .flatMapPublisher(Flowable::fromOptional)); + + return Flowable.concat(beforeEvents, mainEvents, afterEvents); + }) + .compose( + Tracing.traceAgent( + "invoke_agent " + name(), name(), description(), invocationContext)); + }); } /** @@ -392,13 +383,13 @@ private ImmutableList>> callbacksTo * * @param agentCallbacks Callback functions. * @param invocationContext Current invocation context. - * @return Maybe emitting first event, or empty if none. + * @return single emitting first event, or empty if none. */ - private Maybe callCallback( + private Single> callCallback( List>> agentCallbacks, InvocationContext invocationContext) { if (agentCallbacks.isEmpty()) { - return Maybe.empty(); + return Single.just(Optional.empty()); } CallbackContext callbackContext = @@ -407,25 +398,27 @@ private Maybe callCallback( return Flowable.fromIterable(agentCallbacks) .concatMap( callback -> { - return callback - .apply(callbackContext) + Maybe maybeContent = callback.apply(callbackContext); + + return maybeContent .map( content -> { invocationContext.setEndInvocation(true); - return Event.builder() - .id(Event.generateEventId()) - .invocationId(invocationContext.invocationId()) - .author(name()) - .branch(invocationContext.branch().orElse(null)) - .actions(callbackContext.eventActions()) - .content(content) - .build(); + return Optional.of( + Event.builder() + .id(Event.generateEventId()) + .invocationId(invocationContext.invocationId()) + .author(name()) + .branch(invocationContext.branch().orElse(null)) + .actions(callbackContext.eventActions()) + .content(content) + .build()); }) .toFlowable(); }) .firstElement() .switchIfEmpty( - Maybe.defer( + Single.defer( () -> { if (callbackContext.state().hasDelta()) { Event.Builder eventBuilder = @@ -436,9 +429,9 @@ private Maybe callCallback( .branch(invocationContext.branch().orElse(null)) .actions(callbackContext.eventActions()); - return Maybe.just(eventBuilder.build()); + return Single.just(Optional.of(eventBuilder.build())); } else { - return Maybe.empty(); + return Single.just(Optional.empty()); } })); } diff --git a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java index fba7f10e0..6ed9ccaa3 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java @@ -92,9 +92,7 @@ public BaseLlmFlow( * events generated by them. */ protected Flowable preprocess( - InvocationContext context, - AtomicReference llmRequestRef, - Context otelParentContext) { + InvocationContext context, AtomicReference llmRequestRef) { LlmAgent agent = (LlmAgent) context.agent(); RequestProcessor toolsProcessor = @@ -106,8 +104,7 @@ protected Flowable preprocess( tool -> tool.processLlmRequest(builder, ToolContext.builder(ctx).build())) .andThen( Single.fromCallable( - () -> RequestProcessingResult.create(builder.build(), ImmutableList.of()))) - .compose(Tracing.withContext(otelParentContext)); + () -> RequestProcessingResult.create(builder.build(), ImmutableList.of()))); }; Iterable allProcessors = @@ -116,9 +113,7 @@ protected Flowable preprocess( return Flowable.fromIterable(allProcessors) .concatMap( processor -> - processor - .processRequest(context, llmRequestRef.get()) - .compose(Tracing.withContext(otelParentContext)) + Single.defer(() -> processor.processRequest(context, llmRequestRef.get())) .doOnSuccess(result -> llmRequestRef.set(result.updatedRequest())) .flattenAsFlowable( result -> result.events() != null ? result.events() : ImmutableList.of())); @@ -134,32 +129,13 @@ protected Flowable postprocess( Event baseEventForLlmResponse, LlmRequest llmRequest, LlmResponse llmResponse) { - return postprocess( - context, baseEventForLlmResponse, llmRequest, llmResponse, Context.current()); - } - - /** - * Post-processes the LLM response after receiving it from the LLM. Executes all registered {@link - * ResponseProcessor} instances. Emits events for the model response and any subsequent function - * calls. - */ - private Flowable postprocess( - InvocationContext context, - Event baseEventForLlmResponse, - LlmRequest llmRequest, - LlmResponse llmResponse, - Context otelParentContext) { List> eventIterables = new ArrayList<>(); Single currentLlmResponse = Single.just(llmResponse); for (ResponseProcessor processor : responseProcessors) { currentLlmResponse = currentLlmResponse - .flatMap( - response -> - processor - .processResponse(context, response) - .compose(Tracing.withContext(otelParentContext))) + .flatMap(response -> processor.processResponse(context, response)) .doOnSuccess( result -> { if (result.events() != null) { @@ -168,16 +144,15 @@ private Flowable postprocess( }) .map(ResponseProcessingResult::updatedResponse); } + Context parentContext = Context.current(); return currentLlmResponse.flatMapPublisher( - updatedResponse -> - buildPostprocessingEvents( - updatedResponse, - eventIterables, - context, - baseEventForLlmResponse, - llmRequest, - otelParentContext)); + updatedResponse -> { + try (Scope scope = parentContext.makeCurrent()) { + return buildPostprocessingEvents( + updatedResponse, eventIterables, context, baseEventForLlmResponse, llmRequest); + } + }); } /** @@ -189,100 +164,84 @@ private Flowable postprocess( * callbacks. Callbacks should not rely on its ID if they create their own separate events. */ private Flowable callLlm( - InvocationContext context, - LlmRequest llmRequest, - Event eventForCallbackUsage, - Context otelParentContext) { + InvocationContext context, LlmRequest llmRequest, Event eventForCallbackUsage) { LlmAgent agent = (LlmAgent) context.agent(); LlmRequest.Builder llmRequestBuilder = llmRequest.toBuilder(); - return handleBeforeModelCallback( - context, llmRequestBuilder, eventForCallbackUsage, otelParentContext) - .flatMapPublisher(Flowable::just) - .switchIfEmpty( - Flowable.defer( - () -> { - BaseLlm llm = - agent.resolvedModel().model().isPresent() - ? agent.resolvedModel().model().get() - : LlmRegistry.getLlm(agent.resolvedModel().modelName().get()); - return llm.generateContent( - llmRequestBuilder.build(), - context.runConfig().streamingMode() == StreamingMode.SSE) - .onErrorResumeNext( - exception -> - handleOnModelErrorCallback( - context, - llmRequestBuilder, - eventForCallbackUsage, - exception, - otelParentContext) - .switchIfEmpty(Single.error(exception)) - .toFlowable()) - .compose( - Tracing.trace("call_llm", otelParentContext) - .onSuccess( - (span, llmResp) -> - Tracing.traceCallLlm( - span, - context, - eventForCallbackUsage.id(), - llmRequestBuilder.build(), - llmResp))) - .doOnError( - error -> { - Span span = Span.current(); - span.setStatus(StatusCode.ERROR, error.getMessage()); - span.recordException(error); - }) - .concatMap( - llmResp -> - handleAfterModelCallback( - context, llmResp, eventForCallbackUsage, otelParentContext) - .toFlowable()); - })); + return handleBeforeModelCallback(context, llmRequestBuilder, eventForCallbackUsage) + .flatMapPublisher( + beforeResponse -> { + if (beforeResponse.isPresent()) { + return Flowable.just(beforeResponse.get()); + } + BaseLlm llm = + agent.resolvedModel().model().isPresent() + ? agent.resolvedModel().model().get() + : LlmRegistry.getLlm(agent.resolvedModel().modelName().get()); + return llm.generateContent( + llmRequestBuilder.build(), + context.runConfig().streamingMode() == StreamingMode.SSE) + .onErrorResumeNext( + exception -> + handleOnModelErrorCallback( + context, llmRequestBuilder, eventForCallbackUsage, exception) + .switchIfEmpty(Single.error(exception)) + .toFlowable()) + .doOnNext( + llmResp -> + Tracing.traceCallLlm( + context, + eventForCallbackUsage.id(), + llmRequestBuilder.build(), + llmResp)) + .doOnError( + error -> { + Span span = Span.current(); + span.setStatus(StatusCode.ERROR, error.getMessage()); + span.recordException(error); + }) + .compose(Tracing.trace("call_llm")) + .concatMap( + llmResp -> + handleAfterModelCallback(context, llmResp, eventForCallbackUsage) + .toFlowable()); + }); } /** * Invokes {@link BeforeModelCallback}s. If any returns a response, it's used instead of calling * the LLM. * - * @return A {@link Maybe} with the callback result. + * @return A {@link Single} with the callback result or {@link Optional#empty()}. */ - private Maybe handleBeforeModelCallback( - InvocationContext context, - LlmRequest.Builder llmRequestBuilder, - Event modelResponseEvent, - Context otelParentContext) { - try (Scope scope = otelParentContext.makeCurrent()) { - Event callbackEvent = modelResponseEvent.toBuilder().build(); - CallbackContext callbackContext = - new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); + private Single> handleBeforeModelCallback( + InvocationContext context, LlmRequest.Builder llmRequestBuilder, Event modelResponseEvent) { + Event callbackEvent = modelResponseEvent.toBuilder().build(); + CallbackContext callbackContext = + new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); - Maybe pluginResult = - context.pluginManager().beforeModelCallback(callbackContext, llmRequestBuilder); + Maybe pluginResult = + context.pluginManager().beforeModelCallback(callbackContext, llmRequestBuilder); - LlmAgent agent = (LlmAgent) context.agent(); - - List callbacks = agent.canonicalBeforeModelCallbacks(); - if (callbacks.isEmpty()) { - return pluginResult; - } + LlmAgent agent = (LlmAgent) context.agent(); - Maybe callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe( - callback -> - callback - .call(callbackContext, llmRequestBuilder) - .compose(Tracing.withContext(otelParentContext))) - .firstElement()); - - return pluginResult.switchIfEmpty(callbackResult); + List callbacks = agent.canonicalBeforeModelCallbacks(); + if (callbacks.isEmpty()) { + return pluginResult.map(Optional::of).defaultIfEmpty(Optional.empty()); } + + Maybe callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe(callback -> callback.call(callbackContext, llmRequestBuilder)) + .firstElement()); + + return pluginResult + .switchIfEmpty(callbackResult) + .map(Optional::of) + .defaultIfEmpty(Optional.empty()); } /** @@ -295,41 +254,32 @@ private Maybe handleOnModelErrorCallback( InvocationContext context, LlmRequest.Builder llmRequestBuilder, Event modelResponseEvent, - Throwable throwable, - Context otelParentContext) { - - try (Scope scope = otelParentContext.makeCurrent()) { - Event callbackEvent = modelResponseEvent.toBuilder().build(); - CallbackContext callbackContext = - new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); - Exception ex = throwable instanceof Exception e ? e : new Exception(throwable); - Maybe pluginResult = - context - .pluginManager() - .onModelErrorCallback(callbackContext, llmRequestBuilder, throwable); - - LlmAgent agent = (LlmAgent) context.agent(); - List callbacks = agent.canonicalOnModelErrorCallbacks(); - - if (callbacks.isEmpty()) { - return pluginResult; - } + Throwable throwable) { + Event callbackEvent = modelResponseEvent.toBuilder().build(); + CallbackContext callbackContext = + new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); + Exception ex = throwable instanceof Exception e ? e : new Exception(throwable); + + Maybe pluginResult = + context.pluginManager().onModelErrorCallback(callbackContext, llmRequestBuilder, throwable); - Maybe callbackResult = - Maybe.defer( - () -> { - LlmRequest llmRequest = llmRequestBuilder.build(); - return Flowable.fromIterable(callbacks) - .concatMapMaybe( - callback -> - callback - .call(callbackContext, llmRequest, ex) - .compose(Tracing.withContext(otelParentContext))) - .firstElement(); - }); - - return pluginResult.switchIfEmpty(callbackResult); + LlmAgent agent = (LlmAgent) context.agent(); + List callbacks = agent.canonicalOnModelErrorCallbacks(); + + if (callbacks.isEmpty()) { + return pluginResult; } + + Maybe callbackResult = + Maybe.defer( + () -> { + LlmRequest llmRequest = llmRequestBuilder.build(); + return Flowable.fromIterable(callbacks) + .concatMapMaybe(callback -> callback.call(callbackContext, llmRequest, ex)) + .firstElement(); + }); + + return pluginResult.switchIfEmpty(callbackResult); } /** @@ -339,39 +289,29 @@ private Maybe handleOnModelErrorCallback( * @return A {@link Single} with the final {@link LlmResponse}. */ private Single handleAfterModelCallback( - InvocationContext context, - LlmResponse llmResponse, - Event modelResponseEvent, - Context otelParentContext) { + InvocationContext context, LlmResponse llmResponse, Event modelResponseEvent) { + Event callbackEvent = modelResponseEvent.toBuilder().build(); + CallbackContext callbackContext = + new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); - try (Scope scope = otelParentContext.makeCurrent()) { - Event callbackEvent = modelResponseEvent.toBuilder().build(); - CallbackContext callbackContext = - new CallbackContext(context, callbackEvent.actions(), callbackEvent.id()); + Maybe pluginResult = + context.pluginManager().afterModelCallback(callbackContext, llmResponse); - Maybe pluginResult = - context.pluginManager().afterModelCallback(callbackContext, llmResponse); + LlmAgent agent = (LlmAgent) context.agent(); + List callbacks = agent.canonicalAfterModelCallbacks(); - LlmAgent agent = (LlmAgent) context.agent(); - List callbacks = agent.canonicalAfterModelCallbacks(); + if (callbacks.isEmpty()) { + return pluginResult.defaultIfEmpty(llmResponse); + } - if (callbacks.isEmpty()) { - return pluginResult.defaultIfEmpty(llmResponse); - } + Maybe callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe(callback -> callback.call(callbackContext, llmResponse)) + .firstElement()); - Maybe callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe( - callback -> - callback - .call(callbackContext, llmResponse) - .compose(Tracing.withContext(otelParentContext))) - .firstElement()); - - return pluginResult.switchIfEmpty(callbackResult).defaultIfEmpty(llmResponse); - } + return pluginResult.switchIfEmpty(callbackResult).defaultIfEmpty(llmResponse); } /** @@ -383,12 +323,13 @@ private Single handleAfterModelCallback( * @throws LlmCallsLimitExceededException if the agent exceeds allowed LLM invocations. * @throws IllegalStateException if a transfer agent is specified but not found. */ - private Flowable runOneStep(InvocationContext context, Context otelParentContext) { + private Flowable runOneStep(InvocationContext context) { AtomicReference llmRequestRef = new AtomicReference<>(LlmRequest.builder().build()); return Flowable.defer( () -> { - return preprocess(context, llmRequestRef, otelParentContext) + Context currentContext = Context.current(); + return preprocess(context, llmRequestRef) .concatWith( Flowable.defer( () -> { @@ -414,19 +355,15 @@ private Flowable runOneStep(InvocationContext context, Context otelParent .build(); mutableEventTemplate.setTimestamp(0L); - return callLlm( - context, - llmRequestAfterPreprocess, - mutableEventTemplate, - otelParentContext) + return callLlm(context, llmRequestAfterPreprocess, mutableEventTemplate) .concatMap( - llmResponse -> - postprocess( + llmResponse -> { + try (Scope postScope = currentContext.makeCurrent()) { + return postprocess( context, mutableEventTemplate, llmRequestAfterPreprocess, - llmResponse, - otelParentContext) + llmResponse) .doFinally( () -> { String oldId = mutableEventTemplate.id(); @@ -434,7 +371,9 @@ private Flowable runOneStep(InvocationContext context, Context otelParent logger.debug( "Resetting event ID from {} to {}", oldId, newId); mutableEventTemplate.setId(newId); - })) + }); + } + }) .concatMap( event -> { Flowable postProcessedEvents = Flowable.just(event); @@ -468,12 +407,11 @@ private Flowable runOneStep(InvocationContext context, Context otelParent */ @Override public Flowable run(InvocationContext invocationContext) { - return run(invocationContext, Context.current(), 0); + return run(invocationContext, 0); } - private Flowable run( - InvocationContext invocationContext, Context otelParentContext, int stepsCompleted) { - Flowable currentStepEvents = runOneStep(invocationContext, otelParentContext).cache(); + private Flowable run(InvocationContext invocationContext, int stepsCompleted) { + Flowable currentStepEvents = runOneStep(invocationContext).cache(); if (stepsCompleted + 1 >= maxSteps) { logger.debug("Ending flow execution because max steps reached."); return currentStepEvents; @@ -493,7 +431,7 @@ private Flowable run( return Flowable.empty(); } else { logger.debug("Continuing to next step of the flow."); - return run(invocationContext, otelParentContext, stepsCompleted + 1); + return run(invocationContext, stepsCompleted + 1); } })); } @@ -508,10 +446,8 @@ private Flowable run( */ @Override public Flowable runLive(InvocationContext invocationContext) { - Context otelParentContext = Context.current(); AtomicReference llmRequestRef = new AtomicReference<>(LlmRequest.builder().build()); - Flowable preprocessEvents = - preprocess(invocationContext, llmRequestRef, otelParentContext); + Flowable preprocessEvents = preprocess(invocationContext, llmRequestRef); return preprocessEvents.concatWith( Flowable.defer( @@ -533,7 +469,6 @@ public Flowable runLive(InvocationContext invocationContext) { ? Completable.complete() : connection .sendHistory(llmRequestAfterPreprocess.contents()) - .compose(Tracing.trace("send_data", otelParentContext)) .doOnComplete( () -> Tracing.traceSendData( @@ -549,7 +484,8 @@ public Flowable runLive(InvocationContext invocationContext) { invocationContext, eventIdForSendData, llmRequestAfterPreprocess.contents()); - }); + }) + .compose(Tracing.trace("send_data")); Flowable liveRequests = invocationContext @@ -606,16 +542,13 @@ public void onError(Throwable e) { .receive() .flatMap( llmResponse -> { - try (Scope scope = otelParentContext.makeCurrent()) { - Event baseEventForLlmResponse = - liveEventBuilderTemplate.id(Event.generateEventId()).build(); - return postprocess( - invocationContext, - baseEventForLlmResponse, - llmRequestAfterPreprocess, - llmResponse, - otelParentContext); - } + Event baseEventForThisLlmResponse = + liveEventBuilderTemplate.id(Event.generateEventId()).build(); + return postprocess( + invocationContext, + baseEventForThisLlmResponse, + llmRequestAfterPreprocess, + llmResponse); }) .flatMap( event -> { @@ -667,8 +600,7 @@ private Flowable buildPostprocessingEvents( List> eventIterables, InvocationContext context, Event baseEventForLlmResponse, - LlmRequest llmRequest, - Context otelParentContext) { + LlmRequest llmRequest) { Flowable processorEvents = Flowable.fromIterable(Iterables.concat(eventIterables)); if (updatedResponse.content().isEmpty() && updatedResponse.errorCode().isEmpty() @@ -684,27 +616,23 @@ private Flowable buildPostprocessingEvents( return processorEvents.concatWith(Flowable.just(modelResponseEvent)); } - try (Scope scope = otelParentContext.makeCurrent()) { - Maybe maybeFunctionResponseEvent = - context.runConfig().streamingMode() == StreamingMode.BIDI - ? Functions.handleFunctionCallsLive(context, modelResponseEvent, llmRequest.tools()) - : Functions.handleFunctionCalls(context, modelResponseEvent, llmRequest.tools()); - - Flowable functionEvents = - maybeFunctionResponseEvent.flatMapPublisher( - functionResponseEvent -> { - Optional toolConfirmationEvent = - Functions.generateRequestConfirmationEvent( - context, modelResponseEvent, functionResponseEvent); - return toolConfirmationEvent.isPresent() - ? Flowable.just(toolConfirmationEvent.get(), functionResponseEvent) - : Flowable.just(functionResponseEvent); - }); - - return processorEvents - .concatWith(Flowable.just(modelResponseEvent)) - .concatWith(functionEvents); - } + Maybe maybeFunctionResponseEvent = + context.runConfig().streamingMode() == StreamingMode.BIDI + ? Functions.handleFunctionCallsLive(context, modelResponseEvent, llmRequest.tools()) + : Functions.handleFunctionCalls(context, modelResponseEvent, llmRequest.tools()); + + Flowable functionEvents = + maybeFunctionResponseEvent.flatMapPublisher( + functionResponseEvent -> { + Optional toolConfirmationEvent = + Functions.generateRequestConfirmationEvent( + context, modelResponseEvent, functionResponseEvent); + return toolConfirmationEvent.isPresent() + ? Flowable.just(toolConfirmationEvent.get(), functionResponseEvent) + : Flowable.just(functionResponseEvent); + }); + + return processorEvents.concatWith(Flowable.just(modelResponseEvent)).concatWith(functionEvents); } private Event buildModelResponseEvent( diff --git a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java index f8b9e180d..ecc2bb412 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java @@ -257,8 +257,7 @@ private static Function> getFunctionCallMapper( functionCall.args().map(HashMap::new).orElse(new HashMap<>()); Maybe> maybeFunctionResult = - maybeInvokeBeforeToolCall( - invocationContext, tool, functionArgs, toolContext, parentContext) + maybeInvokeBeforeToolCall(invocationContext, tool, functionArgs, toolContext) .switchIfEmpty( Maybe.defer( () -> { @@ -396,49 +395,48 @@ private static Maybe postProcessFunctionResult( .defaultIfEmpty(Optional.empty()) .onErrorResumeNext( t -> { - try (Scope scope = parentContext.makeCurrent()) { - Maybe> errorCallbackResult = - handleOnToolErrorCallback( - invocationContext, tool, functionArgs, toolContext, t, parentContext); - Maybe>> mappedResult; - if (isLive) { - // In live mode, handle null results from the error callback gracefully. - mappedResult = errorCallbackResult.map(Optional::ofNullable); - } else { - // In non-live mode, a null result from the error callback will cause an NPE - // when wrapped with Optional.of(), potentially matching prior behavior. - mappedResult = errorCallbackResult.map(Optional::of); - } - return mappedResult.switchIfEmpty(Single.error(t)); + Maybe> errorCallbackResult = + handleOnToolErrorCallback(invocationContext, tool, functionArgs, toolContext, t); + Maybe>> mappedResult; + if (isLive) { + // In live mode, handle null results from the error callback gracefully. + mappedResult = errorCallbackResult.map(Optional::ofNullable); + } else { + // In non-live mode, a null result from the error callback will cause an NPE + // when wrapped with Optional.of(), potentially matching prior behavior. + mappedResult = errorCallbackResult.map(Optional::of); } + return mappedResult.switchIfEmpty(Single.error(t)); }) .flatMapMaybe( optionalInitialResult -> { - Map initialFunctionResult = optionalInitialResult.orElse(null); - - return maybeInvokeAfterToolCall( - invocationContext, - tool, - functionArgs, - toolContext, - initialFunctionResult, - parentContext) - .map(Optional::of) - .defaultIfEmpty(Optional.ofNullable(initialFunctionResult)) - .flatMapMaybe( - finalOptionalResult -> { - Map finalFunctionResult = finalOptionalResult.orElse(null); - if (tool.longRunning() && finalFunctionResult == null) { - return Maybe.empty(); - } - return Maybe.fromCallable( - () -> - buildResponseEvent( - tool, finalFunctionResult, toolContext, invocationContext)) - .compose( - Tracing.trace("tool_response [" + tool.name() + "]", parentContext)) - .doOnSuccess(event -> Tracing.traceToolResponse(event.id(), event)); - }); + try (Scope scope = parentContext.makeCurrent()) { + Map initialFunctionResult = optionalInitialResult.orElse(null); + + return maybeInvokeAfterToolCall( + invocationContext, tool, functionArgs, toolContext, initialFunctionResult) + .map(Optional::of) + .defaultIfEmpty(Optional.ofNullable(initialFunctionResult)) + .flatMapMaybe( + finalOptionalResult -> { + Map finalFunctionResult = + finalOptionalResult.orElse(null); + if (tool.longRunning() && finalFunctionResult == null) { + return Maybe.empty(); + } + return Maybe.fromCallable( + () -> + buildResponseEvent( + tool, + finalFunctionResult, + toolContext, + invocationContext)) + .compose( + Tracing.trace( + "tool_response [" + tool.name() + "]", parentContext)) + .doOnSuccess(event -> Tracing.traceToolResponse(event.id(), event)); + }); + } }); } @@ -481,32 +479,28 @@ private static Maybe> maybeInvokeBeforeToolCall( InvocationContext invocationContext, BaseTool tool, Map functionArgs, - ToolContext toolContext, - Context parentContext) { - if (invocationContext.agent() instanceof LlmAgent agent) { - try (Scope scope = parentContext.makeCurrent()) { - - Maybe> pluginResult = - invocationContext.pluginManager().beforeToolCallback(tool, functionArgs, toolContext); + ToolContext toolContext) { + if (invocationContext.agent() instanceof LlmAgent) { + LlmAgent agent = (LlmAgent) invocationContext.agent(); - List callbacks = agent.canonicalBeforeToolCallbacks(); - if (callbacks.isEmpty()) { - return pluginResult; - } + Maybe> pluginResult = + invocationContext.pluginManager().beforeToolCallback(tool, functionArgs, toolContext); - Maybe> callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe( - callback -> - callback - .call(invocationContext, tool, functionArgs, toolContext) - .compose(Tracing.withContext(parentContext))) - .firstElement()); - - return pluginResult.switchIfEmpty(callbackResult); + List callbacks = agent.canonicalBeforeToolCallbacks(); + if (callbacks.isEmpty()) { + return pluginResult; } + + Maybe> callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe( + callback -> + callback.call(invocationContext, tool, functionArgs, toolContext)) + .firstElement()); + + return pluginResult.switchIfEmpty(callbackResult); } return Maybe.empty(); } @@ -522,39 +516,34 @@ private static Maybe> handleOnToolErrorCallback( BaseTool tool, Map functionArgs, ToolContext toolContext, - Throwable throwable, - Context parentContext) { + Throwable throwable) { Exception ex = throwable instanceof Exception exception ? exception : new Exception(throwable); - try (Scope scope = parentContext.makeCurrent()) { - Maybe> pluginResult = - invocationContext - .pluginManager() - .onToolErrorCallback(tool, functionArgs, toolContext, throwable); - - if (invocationContext.agent() instanceof LlmAgent) { - LlmAgent agent = (LlmAgent) invocationContext.agent(); + Maybe> pluginResult = + invocationContext + .pluginManager() + .onToolErrorCallback(tool, functionArgs, toolContext, throwable); - List callbacks = agent.canonicalOnToolErrorCallbacks(); - if (callbacks.isEmpty()) { - return pluginResult; - } + if (invocationContext.agent() instanceof LlmAgent) { + LlmAgent agent = (LlmAgent) invocationContext.agent(); - Maybe> callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe( - callback -> - callback - .call(invocationContext, tool, functionArgs, toolContext, ex) - .compose(Tracing.withContext(parentContext))) - .firstElement()); - - return pluginResult.switchIfEmpty(callbackResult); + List callbacks = agent.canonicalOnToolErrorCallbacks(); + if (callbacks.isEmpty()) { + return pluginResult; } - return pluginResult; + + Maybe> callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe( + callback -> + callback.call(invocationContext, tool, functionArgs, toolContext, ex)) + .firstElement()); + + return pluginResult.switchIfEmpty(callbackResult); } + return pluginResult; } private static Maybe> maybeInvokeAfterToolCall( @@ -562,39 +551,35 @@ private static Maybe> maybeInvokeAfterToolCall( BaseTool tool, Map functionArgs, ToolContext toolContext, - Map functionResult, - Context parentContext) { - if (invocationContext.agent() instanceof LlmAgent agent) { + Map functionResult) { + if (invocationContext.agent() instanceof LlmAgent) { + LlmAgent agent = (LlmAgent) invocationContext.agent(); - try (Scope scope = parentContext.makeCurrent()) { - Maybe> pluginResult = - invocationContext - .pluginManager() - .afterToolCallback(tool, functionArgs, toolContext, functionResult); - - List callbacks = agent.canonicalAfterToolCallbacks(); - if (callbacks.isEmpty()) { - return pluginResult; - } + Maybe> pluginResult = + invocationContext + .pluginManager() + .afterToolCallback(tool, functionArgs, toolContext, functionResult); - Maybe> callbackResult = - Maybe.defer( - () -> - Flowable.fromIterable(callbacks) - .concatMapMaybe( - callback -> - callback - .call( - invocationContext, - tool, - functionArgs, - toolContext, - functionResult) - .compose(Tracing.withContext(parentContext))) - .firstElement()); - - return pluginResult.switchIfEmpty(callbackResult); + List callbacks = agent.canonicalAfterToolCallbacks(); + if (callbacks.isEmpty()) { + return pluginResult; } + + Maybe> callbackResult = + Maybe.defer( + () -> + Flowable.fromIterable(callbacks) + .concatMapMaybe( + callback -> + callback.call( + invocationContext, + tool, + functionArgs, + toolContext, + functionResult)) + .firstElement()); + + return pluginResult.switchIfEmpty(callbackResult); } return Maybe.empty(); } diff --git a/core/src/main/java/com/google/adk/plugins/PluginManager.java b/core/src/main/java/com/google/adk/plugins/PluginManager.java index 4d90ca7b5..56dea936a 100644 --- a/core/src/main/java/com/google/adk/plugins/PluginManager.java +++ b/core/src/main/java/com/google/adk/plugins/PluginManager.java @@ -21,13 +21,11 @@ import com.google.adk.events.Event; import com.google.adk.models.LlmRequest; import com.google.adk.models.LlmResponse; -import com.google.adk.telemetry.Tracing; import com.google.adk.tools.BaseTool; import com.google.adk.tools.ToolContext; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.genai.types.Content; -import io.opentelemetry.context.Context; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; @@ -131,7 +129,6 @@ public Completable runAfterRunCallback(InvocationContext invocationContext) { @Override public Completable afterRunCallback(InvocationContext invocationContext) { - Context capturedContext = Context.current(); return Flowable.fromIterable(plugins) .concatMapCompletable( plugin -> @@ -142,13 +139,11 @@ public Completable afterRunCallback(InvocationContext invocationContext) { logger.error( "[{}] Error during callback 'afterRunCallback'", plugin.getName(), - e)) - .compose(Tracing.withContext(capturedContext))); + e))); } @Override public Completable close() { - Context capturedContext = Context.current(); return Flowable.fromIterable(plugins) .concatMapCompletableDelayError( plugin -> @@ -156,8 +151,8 @@ public Completable close() { .close() .doOnError( e -> - logger.error("[{}] Error during callback 'close'", plugin.getName(), e)) - .compose(Tracing.withContext(capturedContext))); + logger.error( + "[{}] Error during callback 'close'", plugin.getName(), e))); } public Maybe runOnEventCallback(InvocationContext invocationContext, Event event) { @@ -280,7 +275,7 @@ public Maybe> onToolErrorCallback( */ private Maybe runMaybeCallbacks( Function> callbackExecutor, String callbackName) { - Context capturedContext = Context.current(); + return Flowable.fromIterable(this.plugins) .concatMapMaybe( plugin -> @@ -299,8 +294,7 @@ private Maybe runMaybeCallbacks( "[{}] Error during callback '{}'", plugin.getName(), callbackName, - e)) - .compose(Tracing.withContext(capturedContext))) + e))) .firstElement(); } } diff --git a/core/src/main/java/com/google/adk/runner/Runner.java b/core/src/main/java/com/google/adk/runner/Runner.java index e35f5c33d..4371300fb 100644 --- a/core/src/main/java/com/google/adk/runner/Runner.java +++ b/core/src/main/java/com/google/adk/runner/Runner.java @@ -52,7 +52,6 @@ import com.google.genai.types.Part; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.StatusCode; -import io.opentelemetry.context.Context; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; @@ -376,25 +375,20 @@ public Flowable runAsync( Content newMessage, RunConfig runConfig, @Nullable Map stateDelta) { - return Flowable.defer( - () -> - this.sessionService - .getSession(appName, userId, sessionId, Optional.empty()) - .switchIfEmpty( - Single.defer( - () -> { - if (runConfig.autoCreateSession()) { - return this.sessionService.createSession( - appName, userId, (Map) null, sessionId); - } - return Single.error( - new IllegalArgumentException( - String.format( - "Session not found: %s for user %s", sessionId, userId))); - })) - .flatMapPublisher( - session -> this.runAsyncImpl(session, newMessage, runConfig, stateDelta))) - .compose(Tracing.trace("invocation")); + Maybe maybeSession = + this.sessionService.getSession(appName, userId, sessionId, Optional.empty()); + return maybeSession + .switchIfEmpty( + Single.defer( + () -> { + if (runConfig.autoCreateSession()) { + return this.sessionService.createSession(appName, userId, null, sessionId); + } + return Single.error( + new IllegalArgumentException( + String.format("Session not found: %s for user %s", sessionId, userId))); + })) + .flatMapPublisher(session -> this.runAsyncImpl(session, newMessage, runConfig, stateDelta)); } /** See {@link #runAsync(String, String, Content, RunConfig, Map)}. */ @@ -447,8 +441,7 @@ public Flowable runAsync( Content newMessage, RunConfig runConfig, @Nullable Map stateDelta) { - return runAsyncImpl(session, newMessage, runConfig, stateDelta) - .compose(Tracing.trace("invocation")); + return runAsyncImpl(session, newMessage, runConfig, stateDelta); } /** @@ -467,7 +460,6 @@ protected Flowable runAsyncImpl( @Nullable Map stateDelta) { return Flowable.defer( () -> { - Context capturedContext = Context.current(); BaseAgent rootAgent = this.agent; String invocationId = InvocationContext.newInvocationContextId(); @@ -481,7 +473,6 @@ protected Flowable runAsyncImpl( return this.pluginManager .onUserMessageCallback(initialContext, newMessage) - .compose(Tracing.withContext(capturedContext)) .defaultIfEmpty(newMessage) .flatMap( content -> @@ -493,7 +484,6 @@ protected Flowable runAsyncImpl( runConfig.saveInputBlobsAsArtifacts(), stateDelta) : Single.just(null)) - .compose(Tracing.withContext(capturedContext)) .flatMapPublisher( event -> { if (event == null) { @@ -504,17 +494,15 @@ protected Flowable runAsyncImpl( return this.sessionService .getSession( session.appName(), session.userId(), session.id(), Optional.empty()) - .compose(Tracing.withContext(capturedContext)) .flatMapPublisher( updatedSession -> runAgentWithFreshSession( - session, - updatedSession, - event, - invocationId, - runConfig, - rootAgent) - .compose(Tracing.withContext(capturedContext))); + session, + updatedSession, + event, + invocationId, + runConfig, + rootAgent)); }); }) .doOnError( @@ -522,7 +510,8 @@ protected Flowable runAsyncImpl( Span span = Span.current(); span.setStatus(StatusCode.ERROR, "Error in runAsync Flowable execution"); span.recordException(throwable); - }); + }) + .compose(Tracing.trace("invocation")); } private Flowable runAgentWithFreshSession( @@ -579,7 +568,7 @@ private Flowable runAgentWithFreshSession( .toFlowable() .switchIfEmpty(agentEvents) .concatWith( - Completable.defer(() -> pluginManager.afterRunCallback(contextWithUpdatedSession))) + Completable.defer(() -> pluginManager.runAfterRunCallback(contextWithUpdatedSession))) .concatWith(Completable.defer(() -> compactEvents(updatedSession))); } @@ -652,51 +641,39 @@ private InvocationContext.Builder newInvocationContextBuilder(Session session) { */ public Flowable runLive( Session session, LiveRequestQueue liveRequestQueue, RunConfig runConfig) { - return runLiveImpl(session, liveRequestQueue, runConfig).compose(Tracing.trace("invocation")); - } - - /** - * Runs the agent in live mode, appending generated events to the session. - * - * @return stream of events from the agent. - */ - protected Flowable runLiveImpl( - Session session, @Nullable LiveRequestQueue liveRequestQueue, RunConfig runConfig) { return Flowable.defer( - () -> { - Context capturedContext = Context.current(); - InvocationContext invocationContext = - newInvocationContextForLive(session, liveRequestQueue, runConfig); - - Single invocationContextSingle; - if (invocationContext.agent() instanceof LlmAgent agent) { - invocationContextSingle = - agent - .tools() - .map( - tools -> { - this.addActiveStreamingTools(invocationContext, tools); - return invocationContext; - }); - } else { - invocationContextSingle = Single.just(invocationContext); - } - return invocationContextSingle - .compose(Tracing.withContext(capturedContext)) - .flatMapPublisher( - updatedInvocationContext -> - updatedInvocationContext - .agent() - .runLive(updatedInvocationContext) - .compose(Tracing.withContext(capturedContext)) - .doOnNext(event -> this.sessionService.appendEvent(session, event))) - .doOnError( - throwable -> { - Span span = Span.current(); - span.setStatus(StatusCode.ERROR, "Error in runLive Flowable execution"); - span.recordException(throwable); - }); - }); + () -> { + InvocationContext invocationContext = + newInvocationContextForLive(session, liveRequestQueue, runConfig); + + Single invocationContextSingle; + if (invocationContext.agent() instanceof LlmAgent agent) { + invocationContextSingle = + agent + .tools() + .map( + tools -> { + this.addActiveStreamingTools(invocationContext, tools); + return invocationContext; + }); + } else { + invocationContextSingle = Single.just(invocationContext); + } + return invocationContextSingle + .flatMapPublisher( + updatedInvocationContext -> + updatedInvocationContext + .agent() + .runLive(updatedInvocationContext) + .doOnNext(event -> this.sessionService.appendEvent(session, event))) + .doOnError( + throwable -> { + Span span = Span.current(); + span.setStatus(StatusCode.ERROR, "Error in runLive Flowable execution"); + span.recordException(throwable); + }); + }) + .compose(Tracing.trace("invocation")); } /** @@ -707,25 +684,19 @@ protected Flowable runLiveImpl( */ public Flowable runLive( String userId, String sessionId, LiveRequestQueue liveRequestQueue, RunConfig runConfig) { - return Flowable.defer( - () -> - this.sessionService - .getSession(appName, userId, sessionId, Optional.empty()) - .switchIfEmpty( - Single.defer( - () -> { - if (runConfig.autoCreateSession()) { - return this.sessionService.createSession( - appName, userId, (Map) null, sessionId); - } - return Single.error( - new IllegalArgumentException( - String.format( - "Session not found: %s for user %s", sessionId, userId))); - })) - .flatMapPublisher( - session -> this.runLiveImpl(session, liveRequestQueue, runConfig))) - .compose(Tracing.trace("invocation")); + return this.sessionService + .getSession(appName, userId, sessionId, Optional.empty()) + .switchIfEmpty( + Single.defer( + () -> { + if (runConfig.autoCreateSession()) { + return this.sessionService.createSession(appName, userId, null, sessionId); + } + return Single.error( + new IllegalArgumentException( + String.format("Session not found: %s for user %s", sessionId, userId))); + })) + .flatMapPublisher(session -> this.runLive(session, liveRequestQueue, runConfig)); } /** diff --git a/core/src/main/java/com/google/adk/telemetry/Tracing.java b/core/src/main/java/com/google/adk/telemetry/Tracing.java index 9fa68ee00..07a640c37 100644 --- a/core/src/main/java/com/google/adk/telemetry/Tracing.java +++ b/core/src/main/java/com/google/adk/telemetry/Tracing.java @@ -37,20 +37,16 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.reactivex.rxjava3.core.Completable; -import io.reactivex.rxjava3.core.CompletableObserver; import io.reactivex.rxjava3.core.CompletableSource; import io.reactivex.rxjava3.core.CompletableTransformer; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.FlowableTransformer; import io.reactivex.rxjava3.core.Maybe; -import io.reactivex.rxjava3.core.MaybeObserver; import io.reactivex.rxjava3.core.MaybeSource; import io.reactivex.rxjava3.core.MaybeTransformer; import io.reactivex.rxjava3.core.Single; -import io.reactivex.rxjava3.core.SingleObserver; import io.reactivex.rxjava3.core.SingleSource; import io.reactivex.rxjava3.core.SingleTransformer; -import io.reactivex.rxjava3.disposables.Disposable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -58,12 +54,9 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Supplier; import org.reactivestreams.Publisher; -import org.reactivestreams.Subscriber; -import org.reactivestreams.Subscription; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -143,10 +136,6 @@ private static Optional getValidCurrentSpan(String methodName) { return Optional.of(span); } - private static void traceWithSpan(String methodName, Consumer action) { - getValidCurrentSpan(methodName).ifPresent(action); - } - private static void setInvocationAttributes( Span span, InvocationContext invocationContext, String eventId) { span.setAttribute(ADK_INVOCATION_ID, invocationContext.invocationId()); @@ -217,16 +206,16 @@ public static void traceAgentInvocation( */ public static void traceToolCall( String toolName, String toolDescription, String toolType, Map args) { - traceWithSpan( - "traceToolCall", - span -> { - setToolExecutionAttributes(span); - span.setAttribute(GEN_AI_TOOL_NAME, toolName); - span.setAttribute(GEN_AI_TOOL_DESCRIPTION, toolDescription); - span.setAttribute(GEN_AI_TOOL_TYPE, toolType); - - setJsonAttribute(span, ADK_TOOL_CALL_ARGS, args); - }); + getValidCurrentSpan("traceToolCall") + .ifPresent( + span -> { + setToolExecutionAttributes(span); + span.setAttribute(GEN_AI_TOOL_NAME, toolName); + span.setAttribute(GEN_AI_TOOL_DESCRIPTION, toolDescription); + span.setAttribute(GEN_AI_TOOL_TYPE, toolType); + + setJsonAttribute(span, ADK_TOOL_CALL_ARGS, args); + }); } /** @@ -236,32 +225,33 @@ public static void traceToolCall( * @param functionResponseEvent The function response event. */ public static void traceToolResponse(String eventId, Event functionResponseEvent) { - traceWithSpan( - "traceToolResponse", - span -> { - setToolExecutionAttributes(span); - span.setAttribute(ADK_EVENT_ID, eventId); - - Optional functionResponse = - functionResponseEvent.functionResponses().stream().findFirst(); - - String toolCallId = - functionResponse.flatMap(FunctionResponse::id).orElse(""); - Object toolResponse = - functionResponse - .flatMap(FunctionResponse::response) - .map(Object.class::cast) - .orElse(""); - - span.setAttribute(GEN_AI_TOOL_CALL_ID, toolCallId); - - Object finalToolResponse = - (toolResponse instanceof Map) - ? toolResponse - : ImmutableMap.of("result", toolResponse); - - setJsonAttribute(span, ADK_TOOL_RESPONSE, finalToolResponse); - }); + getValidCurrentSpan("traceToolResponse") + .ifPresent( + span -> { + setToolExecutionAttributes(span); + span.setAttribute(ADK_EVENT_ID, eventId); + + FunctionResponse functionResponse = + functionResponseEvent.functionResponses().stream().findFirst().orElse(null); + + String toolCallId = ""; + Object toolResponse = ""; + if (functionResponse != null) { + toolCallId = functionResponse.id().orElse(toolCallId); + if (functionResponse.response().isPresent()) { + toolResponse = functionResponse.response().get(); + } + } + + span.setAttribute(GEN_AI_TOOL_CALL_ID, toolCallId); + + Object finalToolResponse = + (toolResponse instanceof Map) + ? toolResponse + : ImmutableMap.of("result", toolResponse); + + setJsonAttribute(span, ADK_TOOL_RESPONSE, finalToolResponse); + }); } /** @@ -306,63 +296,58 @@ public static void traceCallLlm( String eventId, LlmRequest llmRequest, LlmResponse llmResponse) { - traceWithSpan( - "traceCallLlm", - span -> traceCallLlm(span, invocationContext, eventId, llmRequest, llmResponse)); - } - - /** - * Traces a call to the LLM. - * - * @param span The span to end when the stream completes - * @param invocationContext The invocation context. - * @param eventId The ID of the event associated with this LLM call/response. - * @param llmRequest The LLM request object. - * @param llmResponse The LLM response object. - */ - public static void traceCallLlm( - Span span, - InvocationContext invocationContext, - String eventId, - LlmRequest llmRequest, - LlmResponse llmResponse) { - span.setAttribute(GEN_AI_OPERATION_NAME, "call_llm"); - span.setAttribute(GEN_AI_SYSTEM, "gcp.vertex.agent"); - llmRequest.model().ifPresent(modelName -> span.setAttribute(GEN_AI_REQUEST_MODEL, modelName)); - - setInvocationAttributes(span, invocationContext, eventId); - - setJsonAttribute(span, ADK_LLM_REQUEST, buildLlmRequestForTrace(llmRequest)); - setJsonAttribute(span, ADK_LLM_RESPONSE, llmResponse); - - llmRequest - .config() - .flatMap(config -> config.topP()) - .ifPresent(topP -> span.setAttribute(GEN_AI_REQUEST_TOP_P, topP.doubleValue())); - llmRequest - .config() - .flatMap(config -> config.maxOutputTokens()) + getValidCurrentSpan("traceCallLlm") .ifPresent( - maxTokens -> span.setAttribute(GEN_AI_REQUEST_MAX_TOKENS, maxTokens.longValue())); + span -> { + span.setAttribute(GEN_AI_SYSTEM, "gcp.vertex.agent"); + llmRequest + .model() + .ifPresent(modelName -> span.setAttribute(GEN_AI_REQUEST_MODEL, modelName)); - llmResponse - .usageMetadata() - .ifPresent( - usage -> { - usage - .promptTokenCount() - .ifPresent(tokens -> span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, (long) tokens)); - usage - .candidatesTokenCount() + setInvocationAttributes(span, invocationContext, eventId); + + setJsonAttribute(span, ADK_LLM_REQUEST, buildLlmRequestForTrace(llmRequest)); + setJsonAttribute(span, ADK_LLM_RESPONSE, llmResponse); + + llmRequest + .config() + .ifPresent( + config -> { + config + .topP() + .ifPresent( + topP -> + span.setAttribute(GEN_AI_REQUEST_TOP_P, topP.doubleValue())); + config + .maxOutputTokens() + .ifPresent( + maxTokens -> + span.setAttribute( + GEN_AI_REQUEST_MAX_TOKENS, maxTokens.longValue())); + }); + llmResponse + .usageMetadata() + .ifPresent( + usage -> { + usage + .promptTokenCount() + .ifPresent( + tokens -> + span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, (long) tokens)); + usage + .candidatesTokenCount() + .ifPresent( + tokens -> + span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, (long) tokens)); + }); + llmResponse + .finishReason() + .map(reason -> reason.knownEnum().name().toLowerCase(Locale.ROOT)) .ifPresent( - tokens -> span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, (long) tokens)); + reason -> + span.setAttribute( + GEN_AI_RESPONSE_FINISH_REASONS, ImmutableList.of(reason))); }); - - llmResponse - .finishReason() - .map(reason -> reason.knownEnum().name().toLowerCase(Locale.ROOT)) - .ifPresent( - reason -> span.setAttribute(GEN_AI_RESPONSE_FINISH_REASONS, ImmutableList.of(reason))); } /** @@ -374,18 +359,17 @@ public static void traceCallLlm( */ public static void traceSendData( InvocationContext invocationContext, String eventId, List data) { - traceWithSpan( - "traceSendData", - span -> { - span.setAttribute(GEN_AI_OPERATION_NAME, "send_data"); - setInvocationAttributes(span, invocationContext, eventId); - - ImmutableList safeData = - Optional.ofNullable(data).orElse(ImmutableList.of()).stream() - .filter(Objects::nonNull) - .collect(toImmutableList()); - setJsonAttribute(span, ADK_DATA, safeData); - }); + getValidCurrentSpan("traceSendData") + .ifPresent( + span -> { + setInvocationAttributes(span, invocationContext, eventId); + + ImmutableList safeData = + Optional.ofNullable(data).orElse(ImmutableList.of()).stream() + .filter(Objects::nonNull) + .collect(toImmutableList()); + setJsonAttribute(span, ADK_DATA, safeData); + }); } /** @@ -421,17 +405,14 @@ public static Tracer getTracer() { @SuppressWarnings("MustBeClosedChecker") // Scope lifecycle managed by RxJava doFinally public static Flowable traceFlowable( Context spanContext, Span span, Supplier> flowableSupplier) { - return Flowable.defer( - () -> { - Scope scope = spanContext.makeCurrent(); - return flowableSupplier - .get() - .doFinally( - () -> { - scope.close(); - span.end(); - }); - }); + Scope scope = spanContext.makeCurrent(); + return flowableSupplier + .get() + .doFinally( + () -> { + scope.close(); + span.end(); + }); } /** @@ -469,66 +450,15 @@ public static TracerProvider trace(String spanName, Context parentContext * @return A TracerProvider configured for agent invocation. */ public static TracerProvider traceAgent( - Context parent, String spanName, String agentName, String agentDescription, InvocationContext invocationContext) { return new TracerProvider(spanName) - .setParent(parent) .configure( span -> traceAgentInvocation(span, agentName, agentDescription, invocationContext)); } - /** - * Returns a transformer that re-activates a given context for the duration of the stream's - * subscription. - * - * @param context The context to re-activate. - * @param The type of the stream. - * @return A transformer that re-activates the context. - */ - public static ContextTransformer withContext(Context context) { - return new ContextTransformer<>(context); - } - - /** - * A transformer that re-activates a given context for the duration of the stream's subscription. - * - * @param The type of the stream. - */ - public static final class ContextTransformer - implements FlowableTransformer, - SingleTransformer, - MaybeTransformer, - CompletableTransformer { - private final Context context; - - private ContextTransformer(Context context) { - this.context = context; - } - - @Override - public Publisher apply(Flowable upstream) { - return upstream.lift(subscriber -> TracingObserver.wrap(context, subscriber)); - } - - @Override - public SingleSource apply(Single upstream) { - return upstream.lift(observer -> TracingObserver.wrap(context, observer)); - } - - @Override - public MaybeSource apply(Maybe upstream) { - return upstream.lift(observer -> TracingObserver.wrap(context, observer)); - } - - @Override - public CompletableSource apply(Completable upstream) { - return upstream.lift(observer -> TracingObserver.wrap(context, observer)); - } - } - /** * A transformer that manages an OpenTelemetry span and scope for RxJava streams. * @@ -542,7 +472,6 @@ public static final class TracerProvider private final String spanName; private Context explicitParentContext; private final List> spanConfigurers = new ArrayList<>(); - private BiConsumer onSuccessConsumer; private TracerProvider(String spanName) { this.spanName = spanName; @@ -562,38 +491,27 @@ public TracerProvider setParent(Context parentContext) { return this; } - /** - * Registers a callback to be executed with the span and the result item when the stream emits a - * success value. - */ - @CanIgnoreReturnValue - public TracerProvider onSuccess(BiConsumer consumer) { - this.onSuccessConsumer = consumer; - return this; - } - private Context getParentContext() { return explicitParentContext != null ? explicitParentContext : Context.current(); } private final class TracingLifecycle { - private final Span span; - private final Context context; + private Span span; + private Scope scope; - TracingLifecycle() { - Context parentContext = getParentContext(); - span = tracer.spanBuilder(spanName).setParent(parentContext).startSpan(); + @SuppressWarnings("MustBeClosedChecker") + void start() { + span = tracer.spanBuilder(spanName).setParent(getParentContext()).startSpan(); spanConfigurers.forEach(c -> c.accept(span)); - context = parentContext.with(span); + scope = span.makeCurrent(); } void end() { - span.end(); - } - - void run(O observer, Consumer subscribeAction) { - try (Scope scope = context.makeCurrent()) { - subscribeAction.accept(observer); + if (scope != null) { + scope.close(); + } + if (span != null) { + span.end(); } } } @@ -603,18 +521,7 @@ public Publisher apply(Flowable upstream) { return Flowable.defer( () -> { TracingLifecycle lifecycle = new TracingLifecycle(); - return Flowable.fromPublisher( - observer -> - lifecycle.run( - observer, - o -> { - Flowable chain = upstream.compose(withContext(lifecycle.context)); - if (onSuccessConsumer != null) { - chain = - chain.doOnNext(t -> onSuccessConsumer.accept(lifecycle.span, t)); - } - chain.doFinally(lifecycle::end).subscribe(o); - })); + return upstream.doOnSubscribe(s -> lifecycle.start()).doFinally(lifecycle::end); }); } @@ -623,18 +530,7 @@ public SingleSource apply(Single upstream) { return Single.defer( () -> { TracingLifecycle lifecycle = new TracingLifecycle(); - return Single.wrap( - observer -> - lifecycle.run( - observer, - o -> { - Single chain = upstream.compose(withContext(lifecycle.context)); - if (onSuccessConsumer != null) { - chain = - chain.doOnSuccess(t -> onSuccessConsumer.accept(lifecycle.span, t)); - } - chain.doFinally(lifecycle::end).subscribe(o); - })); + return upstream.doOnSubscribe(s -> lifecycle.start()).doFinally(lifecycle::end); }); } @@ -643,18 +539,7 @@ public MaybeSource apply(Maybe upstream) { return Maybe.defer( () -> { TracingLifecycle lifecycle = new TracingLifecycle(); - return Maybe.wrap( - observer -> - lifecycle.run( - observer, - o -> { - Maybe chain = upstream.compose(withContext(lifecycle.context)); - if (onSuccessConsumer != null) { - chain = - chain.doOnSuccess(t -> onSuccessConsumer.accept(lifecycle.span, t)); - } - chain.doFinally(lifecycle::end).subscribe(o); - })); + return upstream.doOnSubscribe(s -> lifecycle.start()).doFinally(lifecycle::end); }); } @@ -663,142 +548,7 @@ public CompletableSource apply(Completable upstream) { return Completable.defer( () -> { TracingLifecycle lifecycle = new TracingLifecycle(); - return Completable.wrap( - observer -> - lifecycle.run( - observer, - o -> { - Completable chain = upstream.compose(withContext(lifecycle.context)); - // Completable does not emit items, so onSuccessConsumer is not - // applicable. - chain.doFinally(lifecycle::end).subscribe(o); - })); - }); - } - } - - /** - * An observer that wraps another observer and ensures that the OpenTelemetry context is active - * during all callback methods. - * - * @param The type of the items emitted by the stream. - */ - private static final class TracingObserver - implements Subscriber, SingleObserver, MaybeObserver, CompletableObserver { - private final Context context; - private final Subscriber subscriber; - private final SingleObserver singleObserver; - private final MaybeObserver maybeObserver; - private final CompletableObserver completableObserver; - - private TracingObserver( - Context context, - Subscriber subscriber, - SingleObserver singleObserver, - MaybeObserver maybeObserver, - CompletableObserver completableObserver) { - this.context = context; - this.subscriber = subscriber; - this.singleObserver = singleObserver; - this.maybeObserver = maybeObserver; - this.completableObserver = completableObserver; - } - - static TracingObserver wrap(Context context, Subscriber subscriber) { - return new TracingObserver<>(context, subscriber, null, null, null); - } - - static TracingObserver wrap(Context context, SingleObserver observer) { - return new TracingObserver<>(context, null, observer, null, null); - } - - static TracingObserver wrap(Context context, MaybeObserver observer) { - return new TracingObserver<>(context, null, null, observer, null); - } - - static TracingObserver wrap(Context context, CompletableObserver observer) { - return new TracingObserver<>(context, null, null, null, observer); - } - - private void runInContext(Runnable action) { - try (Scope scope = context.makeCurrent()) { - action.run(); - } - } - - @Override - public void onSubscribe(Subscription s) { - runInContext( - () -> { - if (subscriber != null) { - subscriber.onSubscribe(s); - } - }); - } - - @Override - public void onSubscribe(Disposable d) { - runInContext( - () -> { - if (singleObserver != null) { - singleObserver.onSubscribe(d); - } else if (maybeObserver != null) { - maybeObserver.onSubscribe(d); - } else if (completableObserver != null) { - completableObserver.onSubscribe(d); - } - }); - } - - @Override - public void onNext(T t) { - runInContext( - () -> { - if (subscriber != null) { - subscriber.onNext(t); - } - }); - } - - @Override - public void onSuccess(T t) { - runInContext( - () -> { - if (singleObserver != null) { - singleObserver.onSuccess(t); - } else if (maybeObserver != null) { - maybeObserver.onSuccess(t); - } - }); - } - - @Override - public void onError(Throwable t) { - runInContext( - () -> { - if (subscriber != null) { - subscriber.onError(t); - } else if (singleObserver != null) { - singleObserver.onError(t); - } else if (maybeObserver != null) { - maybeObserver.onError(t); - } else if (completableObserver != null) { - completableObserver.onError(t); - } - }); - } - - @Override - public void onComplete() { - runInContext( - () -> { - if (subscriber != null) { - subscriber.onComplete(); - } else if (maybeObserver != null) { - maybeObserver.onComplete(); - } else if (completableObserver != null) { - completableObserver.onComplete(); - } + return upstream.doOnSubscribe(s -> lifecycle.start()).doFinally(lifecycle::end); }); } } From 305299fd3a009b24d415ae8f3f052e1bf0d477e4 Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Tue, 10 Mar 2026 02:18:44 -0700 Subject: [PATCH 11/25] refactor: remove the Optional param in VertexAiCodeExecutor method PiperOrigin-RevId: 881301261 --- .../adk/codeexecutors/VertexAiCodeExecutor.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/adk/codeexecutors/VertexAiCodeExecutor.java b/core/src/main/java/com/google/adk/codeexecutors/VertexAiCodeExecutor.java index 5268edf39..af2219d18 100644 --- a/core/src/main/java/com/google/adk/codeexecutors/VertexAiCodeExecutor.java +++ b/core/src/main/java/com/google/adk/codeexecutors/VertexAiCodeExecutor.java @@ -36,7 +36,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Optional; +import org.jspecify.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -140,7 +140,7 @@ public CodeExecutionResult executeCode( executeCodeInterpreter( getCodeWithImports(codeExecutionInput.code()), codeExecutionInput.inputFiles(), - codeExecutionInput.executionId()); + codeExecutionInput.executionId().orElse(null)); // Save output file as artifacts. List savedFiles = new ArrayList<>(); @@ -173,7 +173,7 @@ public CodeExecutionResult executeCode( } private Map executeCodeInterpreter( - String code, List inputFiles, Optional sessionId) { + String code, List inputFiles, @Nullable String sessionId) { ExtensionExecutionServiceClient codeInterpreterExtension = getCodeInterpreterExtension(); if (codeInterpreterExtension == null) { logger.warn("Vertex AI Code Interpreter execution is not available. Returning empty result."); @@ -196,8 +196,9 @@ private Map executeCodeInterpreter( paramsBuilder.putFields( "files", Value.newBuilder().setListValue(listBuilder.build()).build()); } - sessionId.ifPresent( - s -> paramsBuilder.putFields("session_id", Value.newBuilder().setStringValue(s).build())); + if (sessionId != null) { + paramsBuilder.putFields("session_id", Value.newBuilder().setStringValue(sessionId).build()); + } ExecuteExtensionRequest request = ExecuteExtensionRequest.newBuilder() From b71900f08cbab1a89b775356db2294440945a605 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Tue, 10 Mar 2026 05:56:51 -0700 Subject: [PATCH 12/25] refactor: Use Maybe instead of Single PiperOrigin-RevId: 881382533 --- .../java/com/google/adk/agents/BaseAgent.java | 61 +++++++------- .../adk/flows/llmflows/BaseLlmFlow.java | 84 +++++++++---------- 2 files changed, 69 insertions(+), 76 deletions(-) diff --git a/core/src/main/java/com/google/adk/agents/BaseAgent.java b/core/src/main/java/com/google/adk/agents/BaseAgent.java index d74ba9ca5..00676ec31 100644 --- a/core/src/main/java/com/google/adk/agents/BaseAgent.java +++ b/core/src/main/java/com/google/adk/agents/BaseAgent.java @@ -32,7 +32,6 @@ import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; -import io.reactivex.rxjava3.core.Single; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -316,30 +315,29 @@ private Flowable run( () -> { InvocationContext invocationContext = createInvocationContext(parentContext); + Flowable mainAndAfterEvents = + Flowable.defer(() -> runImplementation.apply(invocationContext)) + .concatWith( + Flowable.defer( + () -> + callCallback( + afterCallbacksToFunctions( + invocationContext.pluginManager(), afterAgentCallback), + invocationContext) + .toFlowable())); + return callCallback( beforeCallbacksToFunctions( invocationContext.pluginManager(), beforeAgentCallback), invocationContext) .flatMapPublisher( - beforeEventOpt -> { + beforeEvent -> { if (invocationContext.endInvocation()) { - return Flowable.fromOptional(beforeEventOpt); + return Flowable.just(beforeEvent); } - - Flowable beforeEvents = Flowable.fromOptional(beforeEventOpt); - Flowable mainEvents = - Flowable.defer(() -> runImplementation.apply(invocationContext)); - Flowable afterEvents = - Flowable.defer( - () -> - callCallback( - afterCallbacksToFunctions( - invocationContext.pluginManager(), afterAgentCallback), - invocationContext) - .flatMapPublisher(Flowable::fromOptional)); - - return Flowable.concat(beforeEvents, mainEvents, afterEvents); + return Flowable.just(beforeEvent).concatWith(mainAndAfterEvents); }) + .switchIfEmpty(mainAndAfterEvents) .compose( Tracing.traceAgent( "invoke_agent " + name(), name(), description(), invocationContext)); @@ -383,13 +381,13 @@ private ImmutableList>> callbacksTo * * @param agentCallbacks Callback functions. * @param invocationContext Current invocation context. - * @return single emitting first event, or empty if none. + * @return maybe emitting first event, or empty if none. */ - private Single> callCallback( + private Maybe callCallback( List>> agentCallbacks, InvocationContext invocationContext) { if (agentCallbacks.isEmpty()) { - return Single.just(Optional.empty()); + return Maybe.empty(); } CallbackContext callbackContext = @@ -404,21 +402,20 @@ private Single> callCallback( .map( content -> { invocationContext.setEndInvocation(true); - return Optional.of( - Event.builder() - .id(Event.generateEventId()) - .invocationId(invocationContext.invocationId()) - .author(name()) - .branch(invocationContext.branch().orElse(null)) - .actions(callbackContext.eventActions()) - .content(content) - .build()); + return Event.builder() + .id(Event.generateEventId()) + .invocationId(invocationContext.invocationId()) + .author(name()) + .branch(invocationContext.branch().orElse(null)) + .actions(callbackContext.eventActions()) + .content(content) + .build(); }) .toFlowable(); }) .firstElement() .switchIfEmpty( - Single.defer( + Maybe.defer( () -> { if (callbackContext.state().hasDelta()) { Event.Builder eventBuilder = @@ -429,9 +426,9 @@ private Single> callCallback( .branch(invocationContext.branch().orElse(null)) .actions(callbackContext.eventActions()); - return Single.just(Optional.of(eventBuilder.build())); + return Maybe.just(eventBuilder.build()); } else { - return Single.just(Optional.empty()); + return Maybe.empty(); } })); } diff --git a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java index 6ed9ccaa3..e1afca2b1 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java @@ -170,52 +170,51 @@ private Flowable callLlm( LlmRequest.Builder llmRequestBuilder = llmRequest.toBuilder(); return handleBeforeModelCallback(context, llmRequestBuilder, eventForCallbackUsage) - .flatMapPublisher( - beforeResponse -> { - if (beforeResponse.isPresent()) { - return Flowable.just(beforeResponse.get()); - } - BaseLlm llm = - agent.resolvedModel().model().isPresent() - ? agent.resolvedModel().model().get() - : LlmRegistry.getLlm(agent.resolvedModel().modelName().get()); - return llm.generateContent( - llmRequestBuilder.build(), - context.runConfig().streamingMode() == StreamingMode.SSE) - .onErrorResumeNext( - exception -> - handleOnModelErrorCallback( - context, llmRequestBuilder, eventForCallbackUsage, exception) - .switchIfEmpty(Single.error(exception)) - .toFlowable()) - .doOnNext( - llmResp -> - Tracing.traceCallLlm( - context, - eventForCallbackUsage.id(), - llmRequestBuilder.build(), - llmResp)) - .doOnError( - error -> { - Span span = Span.current(); - span.setStatus(StatusCode.ERROR, error.getMessage()); - span.recordException(error); - }) - .compose(Tracing.trace("call_llm")) - .concatMap( - llmResp -> - handleAfterModelCallback(context, llmResp, eventForCallbackUsage) - .toFlowable()); - }); + .toFlowable() + .switchIfEmpty( + Flowable.defer( + () -> { + BaseLlm llm = + agent.resolvedModel().model().isPresent() + ? agent.resolvedModel().model().get() + : LlmRegistry.getLlm(agent.resolvedModel().modelName().get()); + return llm.generateContent( + llmRequestBuilder.build(), + context.runConfig().streamingMode() == StreamingMode.SSE) + .onErrorResumeNext( + exception -> + handleOnModelErrorCallback( + context, llmRequestBuilder, eventForCallbackUsage, exception) + .switchIfEmpty(Single.error(exception)) + .toFlowable()) + .doOnNext( + llmResp -> + Tracing.traceCallLlm( + context, + eventForCallbackUsage.id(), + llmRequestBuilder.build(), + llmResp)) + .doOnError( + error -> { + Span span = Span.current(); + span.setStatus(StatusCode.ERROR, error.getMessage()); + span.recordException(error); + }) + .compose(Tracing.trace("call_llm")) + .concatMap( + llmResp -> + handleAfterModelCallback(context, llmResp, eventForCallbackUsage) + .toFlowable()); + })); } /** * Invokes {@link BeforeModelCallback}s. If any returns a response, it's used instead of calling * the LLM. * - * @return A {@link Single} with the callback result or {@link Optional#empty()}. + * @return A {@link Maybe} with the callback result. */ - private Single> handleBeforeModelCallback( + private Maybe handleBeforeModelCallback( InvocationContext context, LlmRequest.Builder llmRequestBuilder, Event modelResponseEvent) { Event callbackEvent = modelResponseEvent.toBuilder().build(); CallbackContext callbackContext = @@ -228,7 +227,7 @@ private Single> handleBeforeModelCallback( List callbacks = agent.canonicalBeforeModelCallbacks(); if (callbacks.isEmpty()) { - return pluginResult.map(Optional::of).defaultIfEmpty(Optional.empty()); + return pluginResult; } Maybe callbackResult = @@ -238,10 +237,7 @@ private Single> handleBeforeModelCallback( .concatMapMaybe(callback -> callback.call(callbackContext, llmRequestBuilder)) .firstElement()); - return pluginResult - .switchIfEmpty(callbackResult) - .map(Optional::of) - .defaultIfEmpty(Optional.empty()); + return pluginResult.switchIfEmpty(callbackResult); } /** From d1d5539ef763b6bfd5057c6ea0f2591225a98535 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Tue, 10 Mar 2026 06:15:15 -0700 Subject: [PATCH 13/25] feat: update return type for artifactDelta getter and setter to Map from ConcurrentMap PiperOrigin-RevId: 881389219 --- .../main/java/com/google/adk/events/EventActions.java | 10 +++++----- .../java/com/google/adk/events/EventActionsTest.java | 10 ++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/adk/events/EventActions.java b/core/src/main/java/com/google/adk/events/EventActions.java index 31b096930..4873a5f49 100644 --- a/core/src/main/java/com/google/adk/events/EventActions.java +++ b/core/src/main/java/com/google/adk/events/EventActions.java @@ -110,12 +110,12 @@ public void removeStateByKey(String key) { } @JsonProperty("artifactDelta") - public ConcurrentMap artifactDelta() { + public Map artifactDelta() { return artifactDelta; } - public void setArtifactDelta(ConcurrentMap artifactDelta) { - this.artifactDelta = artifactDelta; + public void setArtifactDelta(Map artifactDelta) { + this.artifactDelta = new ConcurrentHashMap<>(artifactDelta); } @JsonProperty("deletedArtifactIds") @@ -322,8 +322,8 @@ public Builder stateDelta(ConcurrentMap value) { @CanIgnoreReturnValue @JsonProperty("artifactDelta") - public Builder artifactDelta(ConcurrentMap value) { - this.artifactDelta = value; + public Builder artifactDelta(Map value) { + this.artifactDelta = new ConcurrentHashMap<>(value); return this; } diff --git a/core/src/test/java/com/google/adk/events/EventActionsTest.java b/core/src/test/java/com/google/adk/events/EventActionsTest.java index 2975ca83f..22bb94e64 100644 --- a/core/src/test/java/com/google/adk/events/EventActionsTest.java +++ b/core/src/test/java/com/google/adk/events/EventActionsTest.java @@ -110,6 +110,16 @@ public void merge_mergesAllFields() { assertThat(merged.compaction()).hasValue(COMPACTION); } + @Test + public void setArtifactDelta_copiesRegularMap() { + EventActions eventActions = new EventActions(); + ImmutableMap artifactDelta = ImmutableMap.of("artifact1", 1); + + eventActions.setArtifactDelta(artifactDelta); + + assertThat(eventActions.artifactDelta()).containsExactly("artifact1", 1); + } + @Test public void removeStateByKey_marksKeyAsRemoved() { EventActions eventActions = new EventActions(); From b8316b1944ce17cc9208963cc09d900c379444c6 Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Tue, 10 Mar 2026 07:12:02 -0700 Subject: [PATCH 14/25] feat!: Remove Optional parameters in EventActions PiperOrigin-RevId: 881410767 --- .../com/google/adk/events/EventActions.java | 84 +++++++------------ 1 file changed, 28 insertions(+), 56 deletions(-) diff --git a/core/src/main/java/com/google/adk/events/EventActions.java b/core/src/main/java/com/google/adk/events/EventActions.java index 4873a5f49..0b167de93 100644 --- a/core/src/main/java/com/google/adk/events/EventActions.java +++ b/core/src/main/java/com/google/adk/events/EventActions.java @@ -28,36 +28,32 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import javax.annotation.Nullable; +import org.jspecify.annotations.Nullable; /** Represents the actions attached to an event. */ // TODO - b/414081262 make json wire camelCase @JsonDeserialize(builder = EventActions.Builder.class) public class EventActions extends JsonBaseModel { - private Optional skipSummarization; + private @Nullable Boolean skipSummarization; private ConcurrentMap stateDelta; private ConcurrentMap artifactDelta; private Set deletedArtifactIds; - private Optional transferToAgent; - private Optional escalate; + private @Nullable String transferToAgent; + private @Nullable Boolean escalate; private ConcurrentMap> requestedAuthConfigs; private ConcurrentMap requestedToolConfirmations; private boolean endOfAgent; - private Optional compaction; + private @Nullable EventCompaction compaction; /** Default constructor for Jackson. */ public EventActions() { - this.skipSummarization = Optional.empty(); this.stateDelta = new ConcurrentHashMap<>(); this.artifactDelta = new ConcurrentHashMap<>(); this.deletedArtifactIds = new HashSet<>(); - this.transferToAgent = Optional.empty(); - this.escalate = Optional.empty(); this.requestedAuthConfigs = new ConcurrentHashMap<>(); this.requestedToolConfirmations = new ConcurrentHashMap<>(); this.endOfAgent = false; - this.compaction = Optional.empty(); } private EventActions(Builder builder) { @@ -75,19 +71,15 @@ private EventActions(Builder builder) { @JsonProperty("skipSummarization") public Optional skipSummarization() { - return skipSummarization; + return Optional.ofNullable(skipSummarization); } public void setSkipSummarization(@Nullable Boolean skipSummarization) { - this.skipSummarization = Optional.ofNullable(skipSummarization); - } - - public void setSkipSummarization(Optional skipSummarization) { this.skipSummarization = skipSummarization; } public void setSkipSummarization(boolean skipSummarization) { - this.skipSummarization = Optional.of(skipSummarization); + this.skipSummarization = skipSummarization; } @JsonProperty("stateDelta") @@ -130,30 +122,22 @@ public void setDeletedArtifactIds(Set deletedArtifactIds) { @JsonProperty("transferToAgent") public Optional transferToAgent() { - return transferToAgent; + return Optional.ofNullable(transferToAgent); } - public void setTransferToAgent(Optional transferToAgent) { + public void setTransferToAgent(@Nullable String transferToAgent) { this.transferToAgent = transferToAgent; } - public void setTransferToAgent(String transferToAgent) { - this.transferToAgent = Optional.ofNullable(transferToAgent); - } - @JsonProperty("escalate") public Optional escalate() { - return escalate; + return Optional.ofNullable(escalate); } - public void setEscalate(Optional escalate) { + public void setEscalate(@Nullable Boolean escalate) { this.escalate = escalate; } - public void setEscalate(boolean escalate) { - this.escalate = Optional.of(escalate); - } - @JsonProperty("requestedAuthConfigs") public ConcurrentMap> requestedAuthConfigs() { return requestedAuthConfigs; @@ -199,14 +183,6 @@ public Optional endInvocation() { return endOfAgent ? Optional.of(true) : Optional.empty(); } - /** - * @deprecated Use {@link #setEndOfAgent(boolean)} instead. - */ - @Deprecated - public void setEndInvocation(Optional endInvocation) { - this.endOfAgent = endInvocation.orElse(false); - } - /** * @deprecated Use {@link #setEndOfAgent(boolean)} instead. */ @@ -217,10 +193,10 @@ public void setEndInvocation(boolean endInvocation) { @JsonProperty("compaction") public Optional compaction() { - return compaction; + return Optional.ofNullable(compaction); } - public void setCompaction(Optional compaction) { + public void setCompaction(@Nullable EventCompaction compaction) { this.compaction = compaction; } @@ -269,47 +245,43 @@ public int hashCode() { /** Builder for {@link EventActions}. */ public static class Builder { - private Optional skipSummarization; + private @Nullable Boolean skipSummarization; private ConcurrentMap stateDelta; private ConcurrentMap artifactDelta; private Set deletedArtifactIds; - private Optional transferToAgent; - private Optional escalate; + private @Nullable String transferToAgent; + private @Nullable Boolean escalate; private ConcurrentMap> requestedAuthConfigs; private ConcurrentMap requestedToolConfirmations; private boolean endOfAgent = false; - private Optional compaction; + private @Nullable EventCompaction compaction; public Builder() { - this.skipSummarization = Optional.empty(); this.stateDelta = new ConcurrentHashMap<>(); this.artifactDelta = new ConcurrentHashMap<>(); this.deletedArtifactIds = new HashSet<>(); - this.transferToAgent = Optional.empty(); - this.escalate = Optional.empty(); this.requestedAuthConfigs = new ConcurrentHashMap<>(); this.requestedToolConfirmations = new ConcurrentHashMap<>(); - this.compaction = Optional.empty(); } private Builder(EventActions eventActions) { - this.skipSummarization = eventActions.skipSummarization(); + this.skipSummarization = eventActions.skipSummarization; this.stateDelta = new ConcurrentHashMap<>(eventActions.stateDelta()); this.artifactDelta = new ConcurrentHashMap<>(eventActions.artifactDelta()); this.deletedArtifactIds = new HashSet<>(eventActions.deletedArtifactIds()); - this.transferToAgent = eventActions.transferToAgent(); - this.escalate = eventActions.escalate(); + this.transferToAgent = eventActions.transferToAgent; + this.escalate = eventActions.escalate; this.requestedAuthConfigs = new ConcurrentHashMap<>(eventActions.requestedAuthConfigs()); this.requestedToolConfirmations = new ConcurrentHashMap<>(eventActions.requestedToolConfirmations()); - this.endOfAgent = eventActions.endOfAgent(); - this.compaction = eventActions.compaction(); + this.endOfAgent = eventActions.endOfAgent; + this.compaction = eventActions.compaction; } @CanIgnoreReturnValue @JsonProperty("skipSummarization") public Builder skipSummarization(boolean skipSummarization) { - this.skipSummarization = Optional.of(skipSummarization); + this.skipSummarization = skipSummarization; return this; } @@ -336,15 +308,15 @@ public Builder deletedArtifactIds(Set value) { @CanIgnoreReturnValue @JsonProperty("transferToAgent") - public Builder transferToAgent(String agentId) { - this.transferToAgent = Optional.ofNullable(agentId); + public Builder transferToAgent(@Nullable String agentId) { + this.transferToAgent = agentId; return this; } @CanIgnoreReturnValue @JsonProperty("escalate") public Builder escalate(boolean escalate) { - this.escalate = Optional.of(escalate); + this.escalate = escalate; return this; } @@ -391,8 +363,8 @@ public Builder endInvocation(boolean endInvocation) { @CanIgnoreReturnValue @JsonProperty("compaction") - public Builder compaction(EventCompaction value) { - this.compaction = Optional.ofNullable(value); + public Builder compaction(@Nullable EventCompaction value) { + this.compaction = value; return this; } From 14ee28ba593a9f6f5f7b9bb6003441539fe33a18 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Tue, 10 Mar 2026 08:01:55 -0700 Subject: [PATCH 15/25] fix: Make sure that `InvocationContext.callbackContextData` remains the same instance `InvocationContext.callbackContextData` is used by plugins to keep track of things like invocation start times in `before` and then read in `after` callbacks PiperOrigin-RevId: 881432816 --- .../java/com/google/adk/agents/InvocationContext.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/google/adk/agents/InvocationContext.java b/core/src/main/java/com/google/adk/agents/InvocationContext.java index 7602ca9f2..7f0e49d0c 100644 --- a/core/src/main/java/com/google/adk/agents/InvocationContext.java +++ b/core/src/main/java/com/google/adk/agents/InvocationContext.java @@ -75,7 +75,10 @@ protected InvocationContext(Builder builder) { this.eventsCompactionConfig = builder.eventsCompactionConfig; this.contextCacheConfig = builder.contextCacheConfig; this.invocationCostManager = builder.invocationCostManager; - this.callbackContextData = new ConcurrentHashMap<>(builder.callbackContextData); + // Don't copy the callback context data. This should be the same instance for the full + // invocation invocation so that Plugins can access the same data it during the invocation + // across all types of callbacks. + this.callbackContextData = builder.callbackContextData; } /** @@ -345,7 +348,10 @@ private Builder(InvocationContext context) { this.eventsCompactionConfig = context.eventsCompactionConfig; this.contextCacheConfig = context.contextCacheConfig; this.invocationCostManager = context.invocationCostManager; - this.callbackContextData = new ConcurrentHashMap<>(context.callbackContextData); + // Don't copy the callback context data. This should be the same instance for the full + // invocation invocation so that Plugins can access the same data it during the invocation + // across all types of callbacks. + this.callbackContextData = context.callbackContextData; } private BaseSessionService sessionService; From d66c31d5e7ef75b994c5ee3efbc3cf89f393776c Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Tue, 10 Mar 2026 08:51:34 -0700 Subject: [PATCH 16/25] refactor: Removing unnecessary PluginManager.runX() methods PiperOrigin-RevId: 881456806 --- .../com/google/adk/plugins/PluginManager.java | 52 +------------------ .../java/com/google/adk/runner/Runner.java | 2 +- 2 files changed, 3 insertions(+), 51 deletions(-) diff --git a/core/src/main/java/com/google/adk/plugins/PluginManager.java b/core/src/main/java/com/google/adk/plugins/PluginManager.java index 56dea936a..e534da787 100644 --- a/core/src/main/java/com/google/adk/plugins/PluginManager.java +++ b/core/src/main/java/com/google/adk/plugins/PluginManager.java @@ -34,6 +34,7 @@ import java.util.Map; import java.util.Optional; import java.util.function.Function; +import org.jspecify.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +48,7 @@ public class PluginManager extends BasePlugin { private static final Logger logger = LoggerFactory.getLogger(PluginManager.class); private final List plugins = new ArrayList<>(); - public PluginManager(List plugins) { + public PluginManager(@Nullable List plugins) { super("PluginManager"); if (plugins != null) { plugins.forEach(this::registerPlugin); @@ -123,10 +124,6 @@ public Maybe beforeRunCallback(InvocationContext invocationContext) { plugin -> plugin.beforeRunCallback(invocationContext), "beforeRunCallback"); } - public Completable runAfterRunCallback(InvocationContext invocationContext) { - return afterRunCallback(invocationContext); - } - @Override public Completable afterRunCallback(InvocationContext invocationContext) { return Flowable.fromIterable(plugins) @@ -155,41 +152,24 @@ public Completable close() { "[{}] Error during callback 'close'", plugin.getName(), e))); } - public Maybe runOnEventCallback(InvocationContext invocationContext, Event event) { - return onEventCallback(invocationContext, event); - } - @Override public Maybe onEventCallback(InvocationContext invocationContext, Event event) { return runMaybeCallbacks( plugin -> plugin.onEventCallback(invocationContext, event), "onEventCallback"); } - public Maybe runBeforeAgentCallback(BaseAgent agent, CallbackContext callbackContext) { - return beforeAgentCallback(agent, callbackContext); - } - @Override public Maybe beforeAgentCallback(BaseAgent agent, CallbackContext callbackContext) { return runMaybeCallbacks( plugin -> plugin.beforeAgentCallback(agent, callbackContext), "beforeAgentCallback"); } - public Maybe runAfterAgentCallback(BaseAgent agent, CallbackContext callbackContext) { - return afterAgentCallback(agent, callbackContext); - } - @Override public Maybe afterAgentCallback(BaseAgent agent, CallbackContext callbackContext) { return runMaybeCallbacks( plugin -> plugin.afterAgentCallback(agent, callbackContext), "afterAgentCallback"); } - public Maybe runBeforeModelCallback( - CallbackContext callbackContext, LlmRequest.Builder llmRequest) { - return beforeModelCallback(callbackContext, llmRequest); - } - @Override public Maybe beforeModelCallback( CallbackContext callbackContext, LlmRequest.Builder llmRequest) { @@ -197,11 +177,6 @@ public Maybe beforeModelCallback( plugin -> plugin.beforeModelCallback(callbackContext, llmRequest), "beforeModelCallback"); } - public Maybe runAfterModelCallback( - CallbackContext callbackContext, LlmResponse llmResponse) { - return afterModelCallback(callbackContext, llmResponse); - } - @Override public Maybe afterModelCallback( CallbackContext callbackContext, LlmResponse llmResponse) { @@ -209,11 +184,6 @@ public Maybe afterModelCallback( plugin -> plugin.afterModelCallback(callbackContext, llmResponse), "afterModelCallback"); } - public Maybe runOnModelErrorCallback( - CallbackContext callbackContext, LlmRequest.Builder llmRequest, Throwable error) { - return onModelErrorCallback(callbackContext, llmRequest, error); - } - @Override public Maybe onModelErrorCallback( CallbackContext callbackContext, LlmRequest.Builder llmRequest, Throwable error) { @@ -222,11 +192,6 @@ public Maybe onModelErrorCallback( "onModelErrorCallback"); } - public Maybe> runBeforeToolCallback( - BaseTool tool, Map toolArgs, ToolContext toolContext) { - return beforeToolCallback(tool, toolArgs, toolContext); - } - @Override public Maybe> beforeToolCallback( BaseTool tool, Map toolArgs, ToolContext toolContext) { @@ -234,14 +199,6 @@ public Maybe> beforeToolCallback( plugin -> plugin.beforeToolCallback(tool, toolArgs, toolContext), "beforeToolCallback"); } - public Maybe> runAfterToolCallback( - BaseTool tool, - Map toolArgs, - ToolContext toolContext, - Map result) { - return afterToolCallback(tool, toolArgs, toolContext, result); - } - @Override public Maybe> afterToolCallback( BaseTool tool, @@ -253,11 +210,6 @@ public Maybe> afterToolCallback( "afterToolCallback"); } - public Maybe> runOnToolErrorCallback( - BaseTool tool, Map toolArgs, ToolContext toolContext, Throwable error) { - return onToolErrorCallback(tool, toolArgs, toolContext, error); - } - @Override public Maybe> onToolErrorCallback( BaseTool tool, Map toolArgs, ToolContext toolContext, Throwable error) { diff --git a/core/src/main/java/com/google/adk/runner/Runner.java b/core/src/main/java/com/google/adk/runner/Runner.java index 4371300fb..29b2b76d3 100644 --- a/core/src/main/java/com/google/adk/runner/Runner.java +++ b/core/src/main/java/com/google/adk/runner/Runner.java @@ -568,7 +568,7 @@ private Flowable runAgentWithFreshSession( .toFlowable() .switchIfEmpty(agentEvents) .concatWith( - Completable.defer(() -> pluginManager.runAfterRunCallback(contextWithUpdatedSession))) + Completable.defer(() -> pluginManager.afterRunCallback(contextWithUpdatedSession))) .concatWith(Completable.defer(() -> compactEvents(updatedSession))); } From 72c98045088ff49d4b45b645b3ad31100e6d1fa8 Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Tue, 10 Mar 2026 08:55:40 -0700 Subject: [PATCH 17/25] refactor: suppress warnings for Optional param in LlmRequest.addInstructions PiperOrigin-RevId: 881458806 --- .../com/google/adk/models/LlmRequest.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/com/google/adk/models/LlmRequest.java b/core/src/main/java/com/google/adk/models/LlmRequest.java index e35969147..1a45c3a95 100644 --- a/core/src/main/java/com/google/adk/models/LlmRequest.java +++ b/core/src/main/java/com/google/adk/models/LlmRequest.java @@ -17,7 +17,6 @@ package com.google.adk.models; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.ImmutableMap.toImmutableMap; @@ -172,29 +171,33 @@ public final Builder appendInstructions(List instructions) { return liveConnectConfig(liveCfg.toBuilder().systemInstruction(newLiveSi).build()); } + // In this particular case we can keep the Optional as a type of a + // parameter, since the function is private and used in only one place while + // the Optional type plays nicely with flatMaps in the code (if we had a + // nullable here, we'd wrap it in the Optional anyway) private Content addInstructions( - Optional currentSystemInstruction, List additionalInstructions) { + @SuppressWarnings("checkstyle:IllegalType") Optional currentSystemInstruction, + List additionalInstructions) { checkArgument( - currentSystemInstruction.isEmpty() - || currentSystemInstruction.get().parts().map(parts -> parts.size()).orElse(0) <= 1, + currentSystemInstruction.flatMap(Content::parts).map(parts -> parts.size()).orElse(0) + <= 1, "At most one instruction is supported."); // Either append to the existing instruction, or create a new one. String instructions = String.join("\n\n", additionalInstructions); - Optional part = - currentSystemInstruction - .flatMap(Content::parts) - .flatMap(parts -> parts.stream().findFirst()); - if (part.isEmpty() || part.get().text().isEmpty()) { - part = Optional.of(Part.fromText(instructions)); - } else { - part = Optional.of(Part.fromText(part.get().text().get() + "\n\n" + instructions)); - } - checkState(part.isPresent(), "Failed to create instruction."); + Part part = + Part.fromText( + currentSystemInstruction + .flatMap(Content::parts) + .flatMap(parts -> parts.stream().findFirst()) + .flatMap(Part::text) + .map(text -> text + "\n\n" + instructions) + .orElse(instructions)); String role = currentSystemInstruction.flatMap(Content::role).orElse("user"); - return Content.builder().parts(part.get()).role(role).build(); + + return Content.builder().parts(part).role(role).build(); } @CanIgnoreReturnValue From aa0e06c535eb65c9008564f177e2590b6d29d30e Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Tue, 10 Mar 2026 11:25:07 -0700 Subject: [PATCH 18/25] refactor: update ApiClient.createHttpClient Optional timeout param to @Nullable PiperOrigin-RevId: 881536165 --- .../main/java/com/google/adk/sessions/ApiClient.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/adk/sessions/ApiClient.java b/core/src/main/java/com/google/adk/sessions/ApiClient.java index 6bf69ee47..e850199e9 100644 --- a/core/src/main/java/com/google/adk/sessions/ApiClient.java +++ b/core/src/main/java/com/google/adk/sessions/ApiClient.java @@ -67,7 +67,7 @@ abstract class ApiClient { applyHttpOptions(customHttpOptions.get()); } - this.httpClient = createHttpClient(httpOptions.timeout()); + this.httpClient = createHttpClient(httpOptions.timeout().orElse(null)); } ApiClient( @@ -113,13 +113,13 @@ abstract class ApiClient { } this.apiKey = Optional.empty(); this.vertexAI = true; - this.httpClient = createHttpClient(httpOptions.timeout()); + this.httpClient = createHttpClient(httpOptions.timeout().orElse(null)); } - private OkHttpClient createHttpClient(Optional timeout) { + private OkHttpClient createHttpClient(@Nullable Integer timeout) { OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); - if (timeout.isPresent()) { - builder.connectTimeout(Duration.ofMillis(timeout.get())); + if (timeout != null) { + builder.connectTimeout(Duration.ofMillis(timeout)); } return builder.build(); } From 444e0f0b4d02481bdb82213f8a87828188fbb89c Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Tue, 10 Mar 2026 11:26:24 -0700 Subject: [PATCH 19/25] refactor: delete SessionJsonConverter.putIfEmpty method with Optional collection param PiperOrigin-RevId: 881536906 --- .../java/com/google/adk/sessions/SessionJsonConverter.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/core/src/main/java/com/google/adk/sessions/SessionJsonConverter.java b/core/src/main/java/com/google/adk/sessions/SessionJsonConverter.java index 97cc0f56d..0c2b33704 100644 --- a/core/src/main/java/com/google/adk/sessions/SessionJsonConverter.java +++ b/core/src/main/java/com/google/adk/sessions/SessionJsonConverter.java @@ -73,7 +73,7 @@ static String convertEventToJson(Event event, boolean useIsoString) { event.turnComplete().ifPresent(v -> metadataJson.put("turnComplete", v)); event.interrupted().ifPresent(v -> metadataJson.put("interrupted", v)); event.branch().ifPresent(v -> metadataJson.put("branch", v)); - putIfNotEmpty(metadataJson, "longRunningToolIds", event.longRunningToolIds()); + event.longRunningToolIds().ifPresent(v -> putIfNotEmpty(metadataJson, "longRunningToolIds", v)); event.groundingMetadata().ifPresent(v -> metadataJson.put("groundingMetadata", v)); event.usageMetadata().ifPresent(v -> metadataJson.put("usageMetadata", v)); Map eventJson = new HashMap<>(); @@ -355,11 +355,6 @@ private static void putIfNotEmpty(Map map, String key, Map } } - private static void putIfNotEmpty( - Map map, String key, Optional> values) { - values.ifPresent(v -> putIfNotEmpty(map, key, v)); - } - private static void putIfNotEmpty( Map map, String key, @Nullable Collection values) { if (values != null && !values.isEmpty()) { From b857f010a0f51df0eb25ecdc364465ffdd9fef65 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Tue, 10 Mar 2026 13:55:10 -0700 Subject: [PATCH 20/25] fix: Removing deprecated methods in Runner PiperOrigin-RevId: 881608694 --- .../adk/models/langchain4j/RunLoop.java | 3 +- .../springai/SpringAIIntegrationTest.java | 21 ++++++---- .../google/adk/models/springai/TestUtils.java | 17 +++++--- .../java/com/google/adk/runner/Runner.java | 41 ------------------- 4 files changed, 26 insertions(+), 56 deletions(-) diff --git a/contrib/langchain4j/src/test/java/com/google/adk/models/langchain4j/RunLoop.java b/contrib/langchain4j/src/test/java/com/google/adk/models/langchain4j/RunLoop.java index 04a2aa585..ede7300fe 100644 --- a/contrib/langchain4j/src/test/java/com/google/adk/models/langchain4j/RunLoop.java +++ b/contrib/langchain4j/src/test/java/com/google/adk/models/langchain4j/RunLoop.java @@ -53,7 +53,8 @@ public static List runLoop(BaseAgent agent, boolean streaming, Object... allEvents.addAll( runner .runAsync( - session, + session.userId(), + session.id(), messageContent, RunConfig.builder() .setStreamingMode( diff --git a/contrib/spring-ai/src/test/java/com/google/adk/models/springai/SpringAIIntegrationTest.java b/contrib/spring-ai/src/test/java/com/google/adk/models/springai/SpringAIIntegrationTest.java index 6843c8eaa..11b17ebf1 100644 --- a/contrib/spring-ai/src/test/java/com/google/adk/models/springai/SpringAIIntegrationTest.java +++ b/contrib/spring-ai/src/test/java/com/google/adk/models/springai/SpringAIIntegrationTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.*; import com.google.adk.agents.LlmAgent; +import com.google.adk.agents.RunConfig; import com.google.adk.events.Event; import com.google.adk.models.springai.integrations.tools.WeatherTool; import com.google.adk.runner.InMemoryRunner; @@ -73,14 +74,15 @@ public ChatResponse call(Prompt prompt) { // when Runner runner = new InMemoryRunner(agent); - Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); + Session session = + runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); Content userMessage = Content.builder().role("user").parts(List.of(Part.fromText("What is a qubit?"))).build(); List events = runner - .runAsync(session, userMessage, com.google.adk.agents.RunConfig.builder().build()) + .runAsync(session.userId(), session.id(), userMessage, RunConfig.builder().build()) .toList() .blockingGet(); @@ -149,7 +151,8 @@ public ChatResponse call(Prompt prompt) { // when Runner runner = new InMemoryRunner(agent); - Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); + Session session = + runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); Content userMessage = Content.builder() @@ -159,7 +162,7 @@ public ChatResponse call(Prompt prompt) { List events = runner - .runAsync(session, userMessage, com.google.adk.agents.RunConfig.builder().build()) + .runAsync(session.userId(), session.id(), userMessage, RunConfig.builder().build()) .toList() .blockingGet(); @@ -217,7 +220,8 @@ public Flux stream(Prompt prompt) { // when Runner runner = new InMemoryRunner(agent); - Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); + Session session = + runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); Content userMessage = Content.builder() @@ -228,11 +232,10 @@ public Flux stream(Prompt prompt) { List events = runner .runAsync( - session, + session.userId(), + session.id(), userMessage, - com.google.adk.agents.RunConfig.builder() - .setStreamingMode(com.google.adk.agents.RunConfig.StreamingMode.SSE) - .build()) + RunConfig.builder().setStreamingMode(RunConfig.StreamingMode.SSE).build()) .toList() .blockingGet(); diff --git a/contrib/spring-ai/src/test/java/com/google/adk/models/springai/TestUtils.java b/contrib/spring-ai/src/test/java/com/google/adk/models/springai/TestUtils.java index f18ded055..891dcd62d 100644 --- a/contrib/spring-ai/src/test/java/com/google/adk/models/springai/TestUtils.java +++ b/contrib/spring-ai/src/test/java/com/google/adk/models/springai/TestUtils.java @@ -46,7 +46,8 @@ public static List askAgent(BaseAgent agent, boolean streaming, Object... allEvents.addAll( runner .runAsync( - session, + session.userId(), + session.id(), messageContent, RunConfig.builder() .setStreamingMode( @@ -67,13 +68,17 @@ public static List askBlockingAgent(BaseAgent agent, Object... messages) } Runner runner = new InMemoryRunner(agent); - Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); + Session session = + runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); List events = new ArrayList<>(); for (Content content : contents) { List batchEvents = - runner.runAsync(session, content, RunConfig.builder().build()).toList().blockingGet(); + runner + .runAsync(session.userId(), session.id(), content, RunConfig.builder().build()) + .toList() + .blockingGet(); events.addAll(batchEvents); } @@ -88,7 +93,8 @@ public static List askAgentStreaming(BaseAgent agent, Object... messages) } Runner runner = new InMemoryRunner(agent); - Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); + Session session = + runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); List events = new ArrayList<>(); @@ -96,7 +102,8 @@ public static List askAgentStreaming(BaseAgent agent, Object... messages) List batchEvents = runner .runAsync( - session, + session.userId(), + session.id(), content, RunConfig.builder().setStreamingMode(RunConfig.StreamingMode.SSE).build()) .toList() diff --git a/core/src/main/java/com/google/adk/runner/Runner.java b/core/src/main/java/com/google/adk/runner/Runner.java index 29b2b76d3..0fd8bb92e 100644 --- a/core/src/main/java/com/google/adk/runner/Runner.java +++ b/core/src/main/java/com/google/adk/runner/Runner.java @@ -415,35 +415,6 @@ public Flowable runAsync(String userId, String sessionId, Content newMess return runAsync(userId, sessionId, newMessage, RunConfig.builder().build()); } - /** - * See {@link #runAsync(Session, Content, RunConfig, Map)}. - * - * @deprecated Use runAsync with sessionId. - */ - @Deprecated(since = "0.4.0", forRemoval = true) - public Flowable runAsync(Session session, Content newMessage, RunConfig runConfig) { - return runAsync(session, newMessage, runConfig, /* stateDelta= */ null); - } - - /** - * Runs the agent asynchronously using a provided Session object. - * - * @param session The session to run the agent in. - * @param newMessage The new message from the user to process. - * @param runConfig Configuration for the agent run. - * @param stateDelta Optional map of state updates to merge into the session for this run. - * @return A Flowable stream of {@link Event} objects generated by the agent during execution. - * @deprecated Use runAsync with sessionId. - */ - @Deprecated(since = "0.4.0", forRemoval = true) - public Flowable runAsync( - Session session, - Content newMessage, - RunConfig runConfig, - @Nullable Map stateDelta) { - return runAsyncImpl(session, newMessage, runConfig, stateDelta); - } - /** * Runs the agent asynchronously using a provided Session object. * @@ -710,18 +681,6 @@ public Flowable runLive( return runLive(sessionKey.userId(), sessionKey.id(), liveRequestQueue, runConfig); } - /** - * Runs the agent asynchronously with a default user ID. - * - * @return stream of generated events. - */ - @Deprecated(since = "0.5.0", forRemoval = true) - public Flowable runWithSessionId( - String sessionId, Content newMessage, RunConfig runConfig) { - // TODO(b/410859954): Add user_id to getter or method signature. Assuming "tmp-user" for now. - return this.runAsync("tmp-user", sessionId, newMessage, runConfig); - } - /** * Checks if the agent and its parent chain allow transfer up the tree. * From 0d8e22d6e9fe4e8d29c87d485915ba51a22eb350 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Tue, 10 Mar 2026 14:58:35 -0700 Subject: [PATCH 21/25] fix: Removing deprecated methods in Runner PiperOrigin-RevId: 881637295 --- .../adk/models/langchain4j/RunLoop.java | 3 +- .../springai/SpringAIIntegrationTest.java | 21 ++++------ .../google/adk/models/springai/TestUtils.java | 17 +++----- .../java/com/google/adk/runner/Runner.java | 41 +++++++++++++++++++ 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/contrib/langchain4j/src/test/java/com/google/adk/models/langchain4j/RunLoop.java b/contrib/langchain4j/src/test/java/com/google/adk/models/langchain4j/RunLoop.java index ede7300fe..04a2aa585 100644 --- a/contrib/langchain4j/src/test/java/com/google/adk/models/langchain4j/RunLoop.java +++ b/contrib/langchain4j/src/test/java/com/google/adk/models/langchain4j/RunLoop.java @@ -53,8 +53,7 @@ public static List runLoop(BaseAgent agent, boolean streaming, Object... allEvents.addAll( runner .runAsync( - session.userId(), - session.id(), + session, messageContent, RunConfig.builder() .setStreamingMode( diff --git a/contrib/spring-ai/src/test/java/com/google/adk/models/springai/SpringAIIntegrationTest.java b/contrib/spring-ai/src/test/java/com/google/adk/models/springai/SpringAIIntegrationTest.java index 11b17ebf1..6843c8eaa 100644 --- a/contrib/spring-ai/src/test/java/com/google/adk/models/springai/SpringAIIntegrationTest.java +++ b/contrib/spring-ai/src/test/java/com/google/adk/models/springai/SpringAIIntegrationTest.java @@ -18,7 +18,6 @@ import static org.junit.jupiter.api.Assertions.*; import com.google.adk.agents.LlmAgent; -import com.google.adk.agents.RunConfig; import com.google.adk.events.Event; import com.google.adk.models.springai.integrations.tools.WeatherTool; import com.google.adk.runner.InMemoryRunner; @@ -74,15 +73,14 @@ public ChatResponse call(Prompt prompt) { // when Runner runner = new InMemoryRunner(agent); - Session session = - runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); + Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); Content userMessage = Content.builder().role("user").parts(List.of(Part.fromText("What is a qubit?"))).build(); List events = runner - .runAsync(session.userId(), session.id(), userMessage, RunConfig.builder().build()) + .runAsync(session, userMessage, com.google.adk.agents.RunConfig.builder().build()) .toList() .blockingGet(); @@ -151,8 +149,7 @@ public ChatResponse call(Prompt prompt) { // when Runner runner = new InMemoryRunner(agent); - Session session = - runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); + Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); Content userMessage = Content.builder() @@ -162,7 +159,7 @@ public ChatResponse call(Prompt prompt) { List events = runner - .runAsync(session.userId(), session.id(), userMessage, RunConfig.builder().build()) + .runAsync(session, userMessage, com.google.adk.agents.RunConfig.builder().build()) .toList() .blockingGet(); @@ -220,8 +217,7 @@ public Flux stream(Prompt prompt) { // when Runner runner = new InMemoryRunner(agent); - Session session = - runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); + Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); Content userMessage = Content.builder() @@ -232,10 +228,11 @@ public Flux stream(Prompt prompt) { List events = runner .runAsync( - session.userId(), - session.id(), + session, userMessage, - RunConfig.builder().setStreamingMode(RunConfig.StreamingMode.SSE).build()) + com.google.adk.agents.RunConfig.builder() + .setStreamingMode(com.google.adk.agents.RunConfig.StreamingMode.SSE) + .build()) .toList() .blockingGet(); diff --git a/contrib/spring-ai/src/test/java/com/google/adk/models/springai/TestUtils.java b/contrib/spring-ai/src/test/java/com/google/adk/models/springai/TestUtils.java index 891dcd62d..f18ded055 100644 --- a/contrib/spring-ai/src/test/java/com/google/adk/models/springai/TestUtils.java +++ b/contrib/spring-ai/src/test/java/com/google/adk/models/springai/TestUtils.java @@ -46,8 +46,7 @@ public static List askAgent(BaseAgent agent, boolean streaming, Object... allEvents.addAll( runner .runAsync( - session.userId(), - session.id(), + session, messageContent, RunConfig.builder() .setStreamingMode( @@ -68,17 +67,13 @@ public static List askBlockingAgent(BaseAgent agent, Object... messages) } Runner runner = new InMemoryRunner(agent); - Session session = - runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); + Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); List events = new ArrayList<>(); for (Content content : contents) { List batchEvents = - runner - .runAsync(session.userId(), session.id(), content, RunConfig.builder().build()) - .toList() - .blockingGet(); + runner.runAsync(session, content, RunConfig.builder().build()).toList().blockingGet(); events.addAll(batchEvents); } @@ -93,8 +88,7 @@ public static List askAgentStreaming(BaseAgent agent, Object... messages) } Runner runner = new InMemoryRunner(agent); - Session session = - runner.sessionService().createSession(agent.name(), "test-user").blockingGet(); + Session session = runner.sessionService().createSession("test-app", "test-user").blockingGet(); List events = new ArrayList<>(); @@ -102,8 +96,7 @@ public static List askAgentStreaming(BaseAgent agent, Object... messages) List batchEvents = runner .runAsync( - session.userId(), - session.id(), + session, content, RunConfig.builder().setStreamingMode(RunConfig.StreamingMode.SSE).build()) .toList() diff --git a/core/src/main/java/com/google/adk/runner/Runner.java b/core/src/main/java/com/google/adk/runner/Runner.java index 0fd8bb92e..29b2b76d3 100644 --- a/core/src/main/java/com/google/adk/runner/Runner.java +++ b/core/src/main/java/com/google/adk/runner/Runner.java @@ -415,6 +415,35 @@ public Flowable runAsync(String userId, String sessionId, Content newMess return runAsync(userId, sessionId, newMessage, RunConfig.builder().build()); } + /** + * See {@link #runAsync(Session, Content, RunConfig, Map)}. + * + * @deprecated Use runAsync with sessionId. + */ + @Deprecated(since = "0.4.0", forRemoval = true) + public Flowable runAsync(Session session, Content newMessage, RunConfig runConfig) { + return runAsync(session, newMessage, runConfig, /* stateDelta= */ null); + } + + /** + * Runs the agent asynchronously using a provided Session object. + * + * @param session The session to run the agent in. + * @param newMessage The new message from the user to process. + * @param runConfig Configuration for the agent run. + * @param stateDelta Optional map of state updates to merge into the session for this run. + * @return A Flowable stream of {@link Event} objects generated by the agent during execution. + * @deprecated Use runAsync with sessionId. + */ + @Deprecated(since = "0.4.0", forRemoval = true) + public Flowable runAsync( + Session session, + Content newMessage, + RunConfig runConfig, + @Nullable Map stateDelta) { + return runAsyncImpl(session, newMessage, runConfig, stateDelta); + } + /** * Runs the agent asynchronously using a provided Session object. * @@ -681,6 +710,18 @@ public Flowable runLive( return runLive(sessionKey.userId(), sessionKey.id(), liveRequestQueue, runConfig); } + /** + * Runs the agent asynchronously with a default user ID. + * + * @return stream of generated events. + */ + @Deprecated(since = "0.5.0", forRemoval = true) + public Flowable runWithSessionId( + String sessionId, Content newMessage, RunConfig runConfig) { + // TODO(b/410859954): Add user_id to getter or method signature. Assuming "tmp-user" for now. + return this.runAsync("tmp-user", sessionId, newMessage, runConfig); + } + /** * Checks if the agent and its parent chain allow transfer up the tree. * From 20f863f716f653979551c481d85d4e7fa56a35da Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Tue, 10 Mar 2026 17:01:00 -0700 Subject: [PATCH 22/25] fix: Explicitly setting the otel parent spans in agents, llm flow and function calls PiperOrigin-RevId: 881688036 --- .../java/com/google/adk/agents/BaseAgent.java | 10 +++++-- .../adk/flows/llmflows/BaseLlmFlow.java | 27 ++++++++++++------- .../google/adk/flows/llmflows/Functions.java | 10 ++++--- .../com/google/adk/telemetry/Tracing.java | 13 --------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/com/google/adk/agents/BaseAgent.java b/core/src/main/java/com/google/adk/agents/BaseAgent.java index 00676ec31..ed6631c50 100644 --- a/core/src/main/java/com/google/adk/agents/BaseAgent.java +++ b/core/src/main/java/com/google/adk/agents/BaseAgent.java @@ -29,6 +29,7 @@ import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.DoNotCall; import com.google.genai.types.Content; +import io.opentelemetry.context.Context; import io.reactivex.rxjava3.core.Completable; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; @@ -311,6 +312,7 @@ public Flowable runAsync(InvocationContext parentContext) { private Flowable run( InvocationContext parentContext, Function> runImplementation) { + Context parentSpanContext = Context.current(); return Flowable.defer( () -> { InvocationContext invocationContext = createInvocationContext(parentContext); @@ -339,8 +341,12 @@ private Flowable run( }) .switchIfEmpty(mainAndAfterEvents) .compose( - Tracing.traceAgent( - "invoke_agent " + name(), name(), description(), invocationContext)); + Tracing.trace("invoke_agent " + name()) + .setParent(parentSpanContext) + .configure( + span -> + Tracing.traceAgentInvocation( + span, name(), description(), invocationContext))); }); } diff --git a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java index e1afca2b1..79066b213 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java @@ -164,7 +164,10 @@ protected Flowable postprocess( * callbacks. Callbacks should not rely on its ID if they create their own separate events. */ private Flowable callLlm( - InvocationContext context, LlmRequest llmRequest, Event eventForCallbackUsage) { + Context spanContext, + InvocationContext context, + LlmRequest llmRequest, + Event eventForCallbackUsage) { LlmAgent agent = (LlmAgent) context.agent(); LlmRequest.Builder llmRequestBuilder = llmRequest.toBuilder(); @@ -200,7 +203,7 @@ private Flowable callLlm( span.setStatus(StatusCode.ERROR, error.getMessage()); span.recordException(error); }) - .compose(Tracing.trace("call_llm")) + .compose(Tracing.trace("call_llm").setParent(spanContext)) .concatMap( llmResp -> handleAfterModelCallback(context, llmResp, eventForCallbackUsage) @@ -319,7 +322,7 @@ private Single handleAfterModelCallback( * @throws LlmCallsLimitExceededException if the agent exceeds allowed LLM invocations. * @throws IllegalStateException if a transfer agent is specified but not found. */ - private Flowable runOneStep(InvocationContext context) { + private Flowable runOneStep(Context spanContext, InvocationContext context) { AtomicReference llmRequestRef = new AtomicReference<>(LlmRequest.builder().build()); return Flowable.defer( @@ -351,7 +354,11 @@ private Flowable runOneStep(InvocationContext context) { .build(); mutableEventTemplate.setTimestamp(0L); - return callLlm(context, llmRequestAfterPreprocess, mutableEventTemplate) + return callLlm( + spanContext, + context, + llmRequestAfterPreprocess, + mutableEventTemplate) .concatMap( llmResponse -> { try (Scope postScope = currentContext.makeCurrent()) { @@ -403,11 +410,12 @@ private Flowable runOneStep(InvocationContext context) { */ @Override public Flowable run(InvocationContext invocationContext) { - return run(invocationContext, 0); + return run(Context.current(), invocationContext, 0); } - private Flowable run(InvocationContext invocationContext, int stepsCompleted) { - Flowable currentStepEvents = runOneStep(invocationContext).cache(); + private Flowable run( + Context spanContext, InvocationContext invocationContext, int stepsCompleted) { + Flowable currentStepEvents = runOneStep(spanContext, invocationContext).cache(); if (stepsCompleted + 1 >= maxSteps) { logger.debug("Ending flow execution because max steps reached."); return currentStepEvents; @@ -427,7 +435,7 @@ private Flowable run(InvocationContext invocationContext, int stepsComple return Flowable.empty(); } else { logger.debug("Continuing to next step of the flow."); - return run(invocationContext, stepsCompleted + 1); + return run(spanContext, invocationContext, stepsCompleted + 1); } })); } @@ -444,6 +452,7 @@ private Flowable run(InvocationContext invocationContext, int stepsComple public Flowable runLive(InvocationContext invocationContext) { AtomicReference llmRequestRef = new AtomicReference<>(LlmRequest.builder().build()); Flowable preprocessEvents = preprocess(invocationContext, llmRequestRef); + Context spanContext = Context.current(); return preprocessEvents.concatWith( Flowable.defer( @@ -481,7 +490,7 @@ public Flowable runLive(InvocationContext invocationContext) { eventIdForSendData, llmRequestAfterPreprocess.contents()); }) - .compose(Tracing.trace("send_data")); + .compose(Tracing.trace("send_data").setParent(spanContext)); Flowable liveRequests = invocationContext diff --git a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java index ecc2bb412..c1a996064 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/Functions.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/Functions.java @@ -178,7 +178,7 @@ public static Maybe handleFunctionCalls( if (events.size() > 1) { return Maybe.just(mergedEvent) .doOnSuccess(event -> Tracing.traceToolResponse(event.id(), event)) - .compose(Tracing.trace("tool_response", parentContext)); + .compose(Tracing.trace("tool_response").setParent(parentContext)); } return Maybe.just(mergedEvent); }); @@ -432,8 +432,8 @@ private static Maybe postProcessFunctionResult( toolContext, invocationContext)) .compose( - Tracing.trace( - "tool_response [" + tool.name() + "]", parentContext)) + Tracing.trace("tool_response [" + tool.name() + "]") + .setParent(parentContext)) .doOnSuccess(event -> Tracing.traceToolResponse(event.id(), event)); }); } @@ -593,7 +593,9 @@ private static Maybe> callTool( Tracing.traceToolCall( tool.name(), tool.description(), tool.getClass().getSimpleName(), args)) .doOnError(t -> Span.current().recordException(t)) - .compose(Tracing.trace("tool_call [" + tool.name() + "]", parentContext)) + .compose( + Tracing.>trace("tool_call [" + tool.name() + "]") + .setParent(parentContext)) .onErrorResumeNext( e -> Maybe.error( diff --git a/core/src/main/java/com/google/adk/telemetry/Tracing.java b/core/src/main/java/com/google/adk/telemetry/Tracing.java index 07a640c37..fc2ca3abf 100644 --- a/core/src/main/java/com/google/adk/telemetry/Tracing.java +++ b/core/src/main/java/com/google/adk/telemetry/Tracing.java @@ -426,19 +426,6 @@ public static TracerProvider trace(String spanName) { return new TracerProvider<>(spanName); } - /** - * Returns a transformer that traces the execution of an RxJava stream with an explicit parent - * context. - * - * @param spanName The name of the span to create. - * @param parentContext The explicit parent context for the span. - * @param The type of the stream. - * @return A TracerProvider that can be used with .compose(). - */ - public static TracerProvider trace(String spanName, Context parentContext) { - return new TracerProvider(spanName).setParent(parentContext); - } - /** * Returns a transformer that traces an agent invocation. * From c6fdb63c92e2f3481a01cfeafa946b6dce728c51 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Wed, 11 Mar 2026 02:33:45 -0700 Subject: [PATCH 23/25] feat: update State constructors to accept general Map types PiperOrigin-RevId: 881890323 --- .../java/com/google/adk/sessions/State.java | 24 ++++++--- .../com/google/adk/sessions/StateTest.java | 50 +++++++++++++++++++ 2 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 core/src/test/java/com/google/adk/sessions/StateTest.java diff --git a/core/src/main/java/com/google/adk/sessions/State.java b/core/src/main/java/com/google/adk/sessions/State.java index ec23857d9..70d2dfbf2 100644 --- a/core/src/main/java/com/google/adk/sessions/State.java +++ b/core/src/main/java/com/google/adk/sessions/State.java @@ -24,6 +24,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import javax.annotation.Nullable; /** A {@link State} object that also keeps track of the changes to the state. */ @SuppressWarnings("ShouldNotSubclass") @@ -39,13 +40,22 @@ public final class State implements ConcurrentMap { private final ConcurrentMap state; private final ConcurrentMap delta; - public State(ConcurrentMap state) { - this(state, new ConcurrentHashMap<>()); - } - - public State(ConcurrentMap state, ConcurrentMap delta) { - this.state = Objects.requireNonNull(state); - this.delta = delta; + public State(Map state) { + this(state, null); + } + + public State(Map state, @Nullable Map delta) { + Objects.requireNonNull(state, "state is null"); + this.state = + state instanceof ConcurrentMap + ? (ConcurrentMap) state + : new ConcurrentHashMap<>(state); + this.delta = + delta == null + ? new ConcurrentHashMap<>() + : delta instanceof ConcurrentMap + ? (ConcurrentMap) delta + : new ConcurrentHashMap<>(delta); } @Override diff --git a/core/src/test/java/com/google/adk/sessions/StateTest.java b/core/src/test/java/com/google/adk/sessions/StateTest.java new file mode 100644 index 000000000..e1fcaeadc --- /dev/null +++ b/core/src/test/java/com/google/adk/sessions/StateTest.java @@ -0,0 +1,50 @@ +package com.google.adk.sessions; + +import static com.google.common.truth.Truth.assertThat; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class StateTest { + @Test + public void constructor_nullDelta_createsEmptyConcurrentHashMap() { + ConcurrentMap stateMap = new ConcurrentHashMap<>(); + State state = new State(stateMap, null); + assertThat(state.hasDelta()).isFalse(); + state.put("key", "value"); + assertThat(state.hasDelta()).isTrue(); + } + + @Test + public void constructor_nullState_throwsException() { + Assert.assertThrows(NullPointerException.class, () -> new State(null, new HashMap<>())); + } + + @Test + public void constructor_regularMapState() { + Map stateMap = new HashMap<>(); + stateMap.put("initial", "val"); + State state = new State(stateMap, null); + // It should have copied the contents + assertThat(state).containsEntry("initial", "val"); + state.put("key", "value"); + // The original map should NOT be updated because a copy was created + assertThat(stateMap).doesNotContainKey("key"); + } + + @Test + public void constructor_singleArgument() { + ConcurrentMap stateMap = new ConcurrentHashMap<>(); + State state = new State(stateMap); + assertThat(state.hasDelta()).isFalse(); + state.put("key", "value"); + assertThat(state.hasDelta()).isTrue(); + } +} From e0d833b337e958e299d0d11a03f6bfa1468731bc Mon Sep 17 00:00:00 2001 From: Maciej Szwaja Date: Wed, 11 Mar 2026 04:25:00 -0700 Subject: [PATCH 24/25] feat!: update LoopAgent's maxIteration field and methods to be @Nullable instead of Optional PiperOrigin-RevId: 881935126 --- .../java/com/google/adk/agents/LoopAgent.java | 22 +++++++++---------- .../com/google/adk/agents/LoopAgentTest.java | 7 +----- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/com/google/adk/agents/LoopAgent.java b/core/src/main/java/com/google/adk/agents/LoopAgent.java index d9d049f80..743d569b9 100644 --- a/core/src/main/java/com/google/adk/agents/LoopAgent.java +++ b/core/src/main/java/com/google/adk/agents/LoopAgent.java @@ -21,7 +21,7 @@ import com.google.errorprone.annotations.CanIgnoreReturnValue; import io.reactivex.rxjava3.core.Flowable; import java.util.List; -import java.util.Optional; +import org.jspecify.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,7 +34,7 @@ public class LoopAgent extends BaseAgent { private static final Logger logger = LoggerFactory.getLogger(LoopAgent.class); - private final Optional maxIterations; + private final @Nullable Integer maxIterations; /** * Constructor for LoopAgent. @@ -50,7 +50,7 @@ private LoopAgent( String name, String description, List subAgents, - Optional maxIterations, + @Nullable Integer maxIterations, List beforeAgentCallback, List afterAgentCallback) { @@ -60,16 +60,10 @@ private LoopAgent( /** Builder for {@link LoopAgent}. */ public static class Builder extends BaseAgent.Builder { - private Optional maxIterations = Optional.empty(); + private @Nullable Integer maxIterations; @CanIgnoreReturnValue - public Builder maxIterations(int maxIterations) { - this.maxIterations = Optional.of(maxIterations); - return this; - } - - @CanIgnoreReturnValue - public Builder maxIterations(Optional maxIterations) { + public Builder maxIterations(@Nullable Integer maxIterations) { this.maxIterations = maxIterations; return this; } @@ -124,7 +118,7 @@ protected Flowable runAsyncImpl(InvocationContext invocationContext) { return Flowable.fromIterable(subAgents) .concatMap(subAgent -> subAgent.runAsync(invocationContext)) - .repeat(maxIterations.orElse(Integer.MAX_VALUE)) + .repeat(maxIterations != null ? maxIterations : Integer.MAX_VALUE) .takeUntil(LoopAgent::hasEscalateAction); } @@ -137,4 +131,8 @@ protected Flowable runLiveImpl(InvocationContext invocationContext) { private static boolean hasEscalateAction(Event event) { return event.actions().escalate().orElse(false); } + + public @Nullable Integer maxIterations() { + return maxIterations; + } } diff --git a/core/src/test/java/com/google/adk/agents/LoopAgentTest.java b/core/src/test/java/com/google/adk/agents/LoopAgentTest.java index 5c04ac74b..b2d0778c6 100644 --- a/core/src/test/java/com/google/adk/agents/LoopAgentTest.java +++ b/core/src/test/java/com/google/adk/agents/LoopAgentTest.java @@ -33,7 +33,6 @@ import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Maybe; import java.util.List; -import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import org.junit.runner.RunWith; @@ -165,11 +164,7 @@ public void runAsync_withNoMaxIterations_keepsLooping() { Event event2 = createEvent("event2"); TestBaseAgent subAgent = createSubAgent("subAgent", () -> Flowable.just(event1, event2)); LoopAgent loopAgent = - LoopAgent.builder() - .name("loopAgent") - .subAgents(ImmutableList.of(subAgent)) - .maxIterations(Optional.empty()) - .build(); + LoopAgent.builder().name("loopAgent").subAgents(ImmutableList.of(subAgent)).build(); InvocationContext invocationContext = createInvocationContext(loopAgent); Iterable result = loopAgent.runAsync(invocationContext).blockingIterable(); From 1a871141d1c4e659ec90fe4e9f29342bea305255 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Wed, 11 Mar 2026 05:34:14 -0700 Subject: [PATCH 25/25] refactor: Simplifying Tracing code Replacing `getValidCurrentSpan.ifPresent` with a simpler `traceWithSpan` method. PiperOrigin-RevId: 881960190 --- .../com/google/adk/telemetry/Tracing.java | 202 +++++++++--------- 1 file changed, 99 insertions(+), 103 deletions(-) diff --git a/core/src/main/java/com/google/adk/telemetry/Tracing.java b/core/src/main/java/com/google/adk/telemetry/Tracing.java index fc2ca3abf..7f338fdcf 100644 --- a/core/src/main/java/com/google/adk/telemetry/Tracing.java +++ b/core/src/main/java/com/google/adk/telemetry/Tracing.java @@ -127,13 +127,13 @@ public class Tracing { private Tracing() {} - private static Optional getValidCurrentSpan(String methodName) { + private static void traceWithSpan(String methodName, Consumer traceAction) { Span span = Span.current(); if (!span.getSpanContext().isValid()) { log.trace("{}: No valid span in current context.", methodName); - return Optional.empty(); + return; } - return Optional.of(span); + traceAction.accept(span); } private static void setInvocationAttributes( @@ -206,16 +206,16 @@ public static void traceAgentInvocation( */ public static void traceToolCall( String toolName, String toolDescription, String toolType, Map args) { - getValidCurrentSpan("traceToolCall") - .ifPresent( - span -> { - setToolExecutionAttributes(span); - span.setAttribute(GEN_AI_TOOL_NAME, toolName); - span.setAttribute(GEN_AI_TOOL_DESCRIPTION, toolDescription); - span.setAttribute(GEN_AI_TOOL_TYPE, toolType); - - setJsonAttribute(span, ADK_TOOL_CALL_ARGS, args); - }); + traceWithSpan( + "traceToolCall", + span -> { + setToolExecutionAttributes(span); + span.setAttribute(GEN_AI_TOOL_NAME, toolName); + span.setAttribute(GEN_AI_TOOL_DESCRIPTION, toolDescription); + span.setAttribute(GEN_AI_TOOL_TYPE, toolType); + + setJsonAttribute(span, ADK_TOOL_CALL_ARGS, args); + }); } /** @@ -225,33 +225,33 @@ public static void traceToolCall( * @param functionResponseEvent The function response event. */ public static void traceToolResponse(String eventId, Event functionResponseEvent) { - getValidCurrentSpan("traceToolResponse") - .ifPresent( - span -> { - setToolExecutionAttributes(span); - span.setAttribute(ADK_EVENT_ID, eventId); - - FunctionResponse functionResponse = - functionResponseEvent.functionResponses().stream().findFirst().orElse(null); - - String toolCallId = ""; - Object toolResponse = ""; - if (functionResponse != null) { - toolCallId = functionResponse.id().orElse(toolCallId); - if (functionResponse.response().isPresent()) { - toolResponse = functionResponse.response().get(); - } - } - - span.setAttribute(GEN_AI_TOOL_CALL_ID, toolCallId); - - Object finalToolResponse = - (toolResponse instanceof Map) - ? toolResponse - : ImmutableMap.of("result", toolResponse); - - setJsonAttribute(span, ADK_TOOL_RESPONSE, finalToolResponse); - }); + traceWithSpan( + "traceToolResponse", + span -> { + setToolExecutionAttributes(span); + span.setAttribute(ADK_EVENT_ID, eventId); + + FunctionResponse functionResponse = + functionResponseEvent.functionResponses().stream().findFirst().orElse(null); + + String toolCallId = ""; + Object toolResponse = ""; + if (functionResponse != null) { + toolCallId = functionResponse.id().orElse(toolCallId); + if (functionResponse.response().isPresent()) { + toolResponse = functionResponse.response().get(); + } + } + + span.setAttribute(GEN_AI_TOOL_CALL_ID, toolCallId); + + Object finalToolResponse = + (toolResponse instanceof Map) + ? toolResponse + : ImmutableMap.of("result", toolResponse); + + setJsonAttribute(span, ADK_TOOL_RESPONSE, finalToolResponse); + }); } /** @@ -296,58 +296,54 @@ public static void traceCallLlm( String eventId, LlmRequest llmRequest, LlmResponse llmResponse) { - getValidCurrentSpan("traceCallLlm") - .ifPresent( - span -> { - span.setAttribute(GEN_AI_SYSTEM, "gcp.vertex.agent"); - llmRequest - .model() - .ifPresent(modelName -> span.setAttribute(GEN_AI_REQUEST_MODEL, modelName)); - - setInvocationAttributes(span, invocationContext, eventId); - - setJsonAttribute(span, ADK_LLM_REQUEST, buildLlmRequestForTrace(llmRequest)); - setJsonAttribute(span, ADK_LLM_RESPONSE, llmResponse); - - llmRequest - .config() - .ifPresent( - config -> { - config - .topP() - .ifPresent( - topP -> - span.setAttribute(GEN_AI_REQUEST_TOP_P, topP.doubleValue())); - config - .maxOutputTokens() - .ifPresent( - maxTokens -> - span.setAttribute( - GEN_AI_REQUEST_MAX_TOKENS, maxTokens.longValue())); - }); - llmResponse - .usageMetadata() - .ifPresent( - usage -> { - usage - .promptTokenCount() - .ifPresent( - tokens -> - span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, (long) tokens)); - usage - .candidatesTokenCount() - .ifPresent( - tokens -> - span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, (long) tokens)); - }); - llmResponse - .finishReason() - .map(reason -> reason.knownEnum().name().toLowerCase(Locale.ROOT)) - .ifPresent( - reason -> - span.setAttribute( - GEN_AI_RESPONSE_FINISH_REASONS, ImmutableList.of(reason))); - }); + traceWithSpan( + "traceCallLlm", + span -> { + span.setAttribute(GEN_AI_SYSTEM, "gcp.vertex.agent"); + llmRequest + .model() + .ifPresent(modelName -> span.setAttribute(GEN_AI_REQUEST_MODEL, modelName)); + + setInvocationAttributes(span, invocationContext, eventId); + + setJsonAttribute(span, ADK_LLM_REQUEST, buildLlmRequestForTrace(llmRequest)); + setJsonAttribute(span, ADK_LLM_RESPONSE, llmResponse); + + llmRequest + .config() + .ifPresent( + config -> { + config + .topP() + .ifPresent( + topP -> span.setAttribute(GEN_AI_REQUEST_TOP_P, topP.doubleValue())); + config + .maxOutputTokens() + .ifPresent( + maxTokens -> + span.setAttribute( + GEN_AI_REQUEST_MAX_TOKENS, maxTokens.longValue())); + }); + llmResponse + .usageMetadata() + .ifPresent( + usage -> { + usage + .promptTokenCount() + .ifPresent( + tokens -> span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, (long) tokens)); + usage + .candidatesTokenCount() + .ifPresent( + tokens -> span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, (long) tokens)); + }); + llmResponse + .finishReason() + .map(reason -> reason.knownEnum().name().toLowerCase(Locale.ROOT)) + .ifPresent( + reason -> + span.setAttribute(GEN_AI_RESPONSE_FINISH_REASONS, ImmutableList.of(reason))); + }); } /** @@ -359,17 +355,17 @@ public static void traceCallLlm( */ public static void traceSendData( InvocationContext invocationContext, String eventId, List data) { - getValidCurrentSpan("traceSendData") - .ifPresent( - span -> { - setInvocationAttributes(span, invocationContext, eventId); - - ImmutableList safeData = - Optional.ofNullable(data).orElse(ImmutableList.of()).stream() - .filter(Objects::nonNull) - .collect(toImmutableList()); - setJsonAttribute(span, ADK_DATA, safeData); - }); + traceWithSpan( + "traceSendData", + span -> { + setInvocationAttributes(span, invocationContext, eventId); + + ImmutableList safeData = + Optional.ofNullable(data).orElse(ImmutableList.of()).stream() + .filter(Objects::nonNull) + .collect(toImmutableList()); + setJsonAttribute(span, ADK_DATA, safeData); + }); } /**