From 0b63ca30294ea05572707c420306ae41bf7d60c7 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Tue, 27 Jan 2026 08:57:50 -0800 Subject: [PATCH] feat: Fix a handful of small changes related to headers, logging and javadoc 1. **License Headers**: Adds the Apache License 2.0 header to several Java files (`BaseToolset.java`, `GoogleMapsTool.java`, `LoadMemoryTool.java`, `NamedToolPredicate.java`, `ToolPredicate.java`, `IntegrationConnectorTool.java`, and `ModelNameUtils.java`). 2. **Logging**: Replaces `System.out.println` and `System.err.println` with `slf4j` logger calls (`logger.error`) in `ExampleUtils.java`, `GoogleSearchTool.java`, and `IntegrationConnectorTool.java` for better error handling and logging practices. 3. **JSON Handling**: In `ExampleUtils.java`, switches from a dedicated `ObjectMapper` instance to using `JsonBaseModel.getMapper()` for JSON serialization. 4. **Thread Safety Doc**: Adds a note to the javadoc for `EventStream.java` indicating that the class is not thread-safe. 5. **`ToolConfirmation.java`**: Makes the `create()` method package-private. 6. **`BuiltInCodeExecutionTool.java`**: Adds a public static `INSTANCE` field for easy access. 7. **`ModelNameUtils.java`**: Makes the class `final`. PiperOrigin-RevId: 861745391 --- .../com/google/adk/events/EventStream.java | 7 +++++- .../google/adk/events/ToolConfirmation.java | 2 +- .../com/google/adk/examples/ExampleUtils.java | 8 ++++++- .../com/google/adk/tools/BaseToolset.java | 16 +++++++++++++ .../adk/tools/BuiltInCodeExecutionTool.java | 1 + .../com/google/adk/tools/GoogleMapsTool.java | 16 +++++++++++++ .../google/adk/tools/GoogleSearchTool.java | 5 +++- .../com/google/adk/tools/LoadMemoryTool.java | 16 +++++++++++++ .../google/adk/tools/NamedToolPredicate.java | 16 +++++++++++++ .../com/google/adk/tools/ToolPredicate.java | 16 +++++++++++++ .../IntegrationConnectorTool.java | 24 +++++++++++++++++-- .../com/google/adk/utils/ModelNameUtils.java | 18 +++++++++++++- 12 files changed, 138 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/com/google/adk/events/EventStream.java b/core/src/main/java/com/google/adk/events/EventStream.java index ff3333d8f..c0c98543f 100644 --- a/core/src/main/java/com/google/adk/events/EventStream.java +++ b/core/src/main/java/com/google/adk/events/EventStream.java @@ -20,7 +20,12 @@ import java.util.NoSuchElementException; import java.util.function.Supplier; -/** Iterable stream of {@link Event} objects. */ +/** + * Iterable stream of {@link Event} objects. + * + *

NOTE: This class is not thread-safe. Concurrent iteration from multiple threads should be + * avoided or externally synchronized. + */ public class EventStream implements Iterable { private final Supplier eventSupplier; diff --git a/core/src/main/java/com/google/adk/events/ToolConfirmation.java b/core/src/main/java/com/google/adk/events/ToolConfirmation.java index 66b107eea..167286094 100644 --- a/core/src/main/java/com/google/adk/events/ToolConfirmation.java +++ b/core/src/main/java/com/google/adk/events/ToolConfirmation.java @@ -62,7 +62,7 @@ public abstract static class Builder { /** For internal usage. Please use `ToolConfirmation.builder()` for instantiation. */ @JsonCreator - private static Builder create() { + static Builder create() { return new AutoValue_ToolConfirmation.Builder(); } diff --git a/core/src/main/java/com/google/adk/examples/ExampleUtils.java b/core/src/main/java/com/google/adk/examples/ExampleUtils.java index b806dbd81..9cce535dc 100644 --- a/core/src/main/java/com/google/adk/examples/ExampleUtils.java +++ b/core/src/main/java/com/google/adk/examples/ExampleUtils.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.adk.JsonBaseModel; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.genai.types.Content; @@ -27,10 +28,14 @@ import com.google.genai.types.FunctionResponse; import com.google.genai.types.Part; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** Utility class for examples. */ public final class ExampleUtils { + private static final Logger logger = LoggerFactory.getLogger(ExampleUtils.class); + // Constant parts of the example string private static final String EXAMPLES_INTRO = "\nBegin few-shot\nThe following are examples of user queries and" @@ -50,7 +55,7 @@ public final class ExampleUtils { private static final String FUNCTION_RESPONSE_PREFIX = "```tool_outputs\n"; private static final String FUNCTION_RESPONSE_SUFFIX = "\n```\n"; - private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final ObjectMapper objectMapper = JsonBaseModel.getMapper(); /** * Converts a list of examples into a formatted few-shot prompt string. @@ -143,6 +148,7 @@ private static void appendFunctionResponse(FunctionResponse response, StringBuil .append(objectMapper.writeValueAsString(responseMap)) .append(FUNCTION_RESPONSE_SUFFIX); } catch (JsonProcessingException e) { + logger.error("Failed to serialize function response", e); builder.append(FUNCTION_RESPONSE_PREFIX).append(FUNCTION_RESPONSE_SUFFIX); } } diff --git a/core/src/main/java/com/google/adk/tools/BaseToolset.java b/core/src/main/java/com/google/adk/tools/BaseToolset.java index c7620f913..4d3482c57 100644 --- a/core/src/main/java/com/google/adk/tools/BaseToolset.java +++ b/core/src/main/java/com/google/adk/tools/BaseToolset.java @@ -1,3 +1,19 @@ +/* + * 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.tools; import com.google.adk.agents.ReadonlyContext; diff --git a/core/src/main/java/com/google/adk/tools/BuiltInCodeExecutionTool.java b/core/src/main/java/com/google/adk/tools/BuiltInCodeExecutionTool.java index 0a7b40112..060b3ffb8 100644 --- a/core/src/main/java/com/google/adk/tools/BuiltInCodeExecutionTool.java +++ b/core/src/main/java/com/google/adk/tools/BuiltInCodeExecutionTool.java @@ -31,6 +31,7 @@ * execution. */ public final class BuiltInCodeExecutionTool extends BaseTool { + public static final BuiltInCodeExecutionTool INSTANCE = new BuiltInCodeExecutionTool(); public BuiltInCodeExecutionTool() { super("code_execution", "code_execution"); diff --git a/core/src/main/java/com/google/adk/tools/GoogleMapsTool.java b/core/src/main/java/com/google/adk/tools/GoogleMapsTool.java index 0a9ea2c6b..8689849c2 100644 --- a/core/src/main/java/com/google/adk/tools/GoogleMapsTool.java +++ b/core/src/main/java/com/google/adk/tools/GoogleMapsTool.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 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.tools; import com.google.adk.models.LlmRequest; diff --git a/core/src/main/java/com/google/adk/tools/GoogleSearchTool.java b/core/src/main/java/com/google/adk/tools/GoogleSearchTool.java index 50743796e..ffd9601a8 100644 --- a/core/src/main/java/com/google/adk/tools/GoogleSearchTool.java +++ b/core/src/main/java/com/google/adk/tools/GoogleSearchTool.java @@ -24,6 +24,8 @@ import com.google.genai.types.Tool; import io.reactivex.rxjava3.core.Completable; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * A built-in tool that is automatically invoked by Gemini 2 models to retrieve search results from @@ -41,6 +43,7 @@ * } */ public final class GoogleSearchTool extends BaseTool { + private static final Logger logger = LoggerFactory.getLogger(GoogleSearchTool.class); public static final GoogleSearchTool INSTANCE = new GoogleSearchTool(); public GoogleSearchTool() { @@ -65,7 +68,7 @@ public Completable processLlmRequest( String model = llmRequestBuilder.build().model().get(); if (model != null && model.startsWith("gemini-1")) { if (!updatedToolsBuilder.build().isEmpty()) { - System.out.println(configBuilder.build().tools().get()); + logger.error("Tools already present: {}", configBuilder.build().tools().get()); return Completable.error( new IllegalArgumentException( "Google search tool cannot be used with other tools in Gemini 1.x.")); diff --git a/core/src/main/java/com/google/adk/tools/LoadMemoryTool.java b/core/src/main/java/com/google/adk/tools/LoadMemoryTool.java index d597bea38..beaf0d4f9 100644 --- a/core/src/main/java/com/google/adk/tools/LoadMemoryTool.java +++ b/core/src/main/java/com/google/adk/tools/LoadMemoryTool.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 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.tools; import com.google.adk.models.LlmRequest; diff --git a/core/src/main/java/com/google/adk/tools/NamedToolPredicate.java b/core/src/main/java/com/google/adk/tools/NamedToolPredicate.java index 94474fd30..a65b2f1ce 100644 --- a/core/src/main/java/com/google/adk/tools/NamedToolPredicate.java +++ b/core/src/main/java/com/google/adk/tools/NamedToolPredicate.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 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.tools; import com.google.adk.agents.ReadonlyContext; diff --git a/core/src/main/java/com/google/adk/tools/ToolPredicate.java b/core/src/main/java/com/google/adk/tools/ToolPredicate.java index 765f93d05..86d739e70 100644 --- a/core/src/main/java/com/google/adk/tools/ToolPredicate.java +++ b/core/src/main/java/com/google/adk/tools/ToolPredicate.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 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.tools; import com.google.adk.agents.ReadonlyContext; diff --git a/core/src/main/java/com/google/adk/tools/applicationintegrationtoolset/IntegrationConnectorTool.java b/core/src/main/java/com/google/adk/tools/applicationintegrationtoolset/IntegrationConnectorTool.java index d69a4d3e3..bc1357106 100644 --- a/core/src/main/java/com/google/adk/tools/applicationintegrationtoolset/IntegrationConnectorTool.java +++ b/core/src/main/java/com/google/adk/tools/applicationintegrationtoolset/IntegrationConnectorTool.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025 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.tools.applicationintegrationtoolset; import static com.google.common.base.Strings.isNullOrEmpty; @@ -26,10 +42,14 @@ import java.util.Map; import java.util.Optional; import org.jspecify.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** Application Integration Tool */ public class IntegrationConnectorTool extends BaseTool { + private static final Logger logger = LoggerFactory.getLogger(IntegrationConnectorTool.class); + private final String openApiSpec; private final String pathUrl; private final String connectionName; @@ -129,7 +149,7 @@ public Optional declaration() { .build(); return Optional.of(declaration); } catch (Exception e) { - System.err.println("Failed to get OpenAPI spec: " + e.getMessage()); + logger.error("Failed to get OpenAPI spec", e); return Optional.empty(); } } @@ -156,7 +176,7 @@ public Single> runAsync(Map args, ToolContex String response = executeIntegration(args); return ImmutableMap.of("result", response); } catch (Exception e) { - System.err.println("Failed to execute integration: " + e.getMessage()); + logger.error("Failed to execute integration", e); return ImmutableMap.of("error", e.getMessage()); } }); diff --git a/core/src/main/java/com/google/adk/utils/ModelNameUtils.java b/core/src/main/java/com/google/adk/utils/ModelNameUtils.java index d39572a79..9995f18b2 100644 --- a/core/src/main/java/com/google/adk/utils/ModelNameUtils.java +++ b/core/src/main/java/com/google/adk/utils/ModelNameUtils.java @@ -1,9 +1,25 @@ +/* + * Copyright 2025 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.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; -public class ModelNameUtils { +public final class ModelNameUtils { private static final Pattern GEMINI_2_PATTERN = Pattern.compile("^gemini-2\\..*"); private static final Pattern PATH_PATTERN = Pattern.compile("^projects/[^/]+/locations/[^/]+/publishers/[^/]+/models/(.+)$");