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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
*/
public final class PartConverter {
private static final Logger logger = LoggerFactory.getLogger(PartConverter.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final ObjectMapper objectMapper = new ObjectMapper();

// Constants for metadata types. By convention metadata keys are prefixed with "adk_" to align
// with the Python and Golang libraries.
public static final String A2A_DATA_PART_METADATA_TYPE_KEY = "adk_type";
Expand Down Expand Up @@ -172,7 +173,7 @@ private static Optional<com.google.genai.types.Part> convertDataPartToGenAiPart(
}

try {
String json = OBJECT_MAPPER.writeValueAsString(data);
String json = objectMapper.writeValueAsString(data);
return Optional.of(com.google.genai.types.Part.builder().text(json).build());
} catch (JsonProcessingException e) {
logger.warn("Failed to serialize DataPart payload", e);
Expand Down Expand Up @@ -254,7 +255,7 @@ public static Optional<io.a2a.spec.Part<?>> fromGenaiPart(Part part) {
return Optional.empty();
}

@SuppressWarnings("unchecked") // safe conversion from OBJECT_MAPPER.readValue
@SuppressWarnings("unchecked") // safe conversion from objectMapper.readValue
private static Map<String, Object> coerceToMap(Object value) {
if (value == null) {
return new HashMap<>();
Expand All @@ -272,7 +273,7 @@ private static Map<String, Object> coerceToMap(Object value) {
return new HashMap<>();
}
try {
return OBJECT_MAPPER.readValue(str, Map.class);
return objectMapper.readValue(str, Map.class);
} catch (JsonProcessingException e) {
logger.warn("Failed to parse map from string payload", e);
Map<String, Object> fallback = new HashMap<>();
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/google/adk/examples/ExampleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 OBJECT_MAPPER = new ObjectMapper();
private static final ObjectMapper objectMapper = new ObjectMapper();

/**
* Converts a list of examples into a formatted few-shot prompt string.
Expand Down Expand Up @@ -140,7 +140,7 @@ private static void appendFunctionResponse(FunctionResponse response, StringBuil
Object responseMap = response.response().orElse(ImmutableMap.of());
builder
.append(FUNCTION_RESPONSE_PREFIX)
.append(OBJECT_MAPPER.writeValueAsString(responseMap))
.append(objectMapper.writeValueAsString(responseMap))
.append(FUNCTION_RESPONSE_SUFFIX);
} catch (JsonProcessingException e) {
builder.append(FUNCTION_RESPONSE_PREFIX).append(FUNCTION_RESPONSE_SUFFIX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
public class RequestConfirmationLlmRequestProcessor implements RequestProcessor {
private static final Logger logger =
LoggerFactory.getLogger(RequestConfirmationLlmRequestProcessor.class);
private static final ObjectMapper OBJECT_MAPPER = JsonBaseModel.getMapper();
private static final ObjectMapper objectMapper = JsonBaseModel.getMapper();
private static final String ORIGINAL_FUNCTION_CALL = "originalFunctionCall";

@Override
Expand Down Expand Up @@ -177,7 +177,7 @@ private Optional<FunctionCall> getOriginalFunctionCall(FunctionCall functionCall
}
try {
FunctionCall originalFunctionCall =
OBJECT_MAPPER.convertValue(
objectMapper.convertValue(
functionCall.args().get().get(ORIGINAL_FUNCTION_CALL), FunctionCall.class);
if (originalFunctionCall.id().isEmpty()) {
return Optional.empty();
Expand Down Expand Up @@ -229,14 +229,14 @@ private static Optional<Map.Entry<String, ToolConfirmation>> maybeCreateToolConf
return Optional.of(
Map.entry(
functionResponse.id().get(),
OBJECT_MAPPER.convertValue(responseMap, ToolConfirmation.class)));
objectMapper.convertValue(responseMap, ToolConfirmation.class)));
}

try {
return Optional.of(
Map.entry(
functionResponse.id().get(),
OBJECT_MAPPER.readValue(
objectMapper.readValue(
(String) responseMap.get("response"), ToolConfirmation.class)));
} catch (JsonProcessingException e) {
logger.error("Failed to parse tool confirmation response", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public final class FunctionCallingUtils {

private static final Logger logger = LoggerFactory.getLogger(FunctionCallingUtils.class);
private static final ObjectMapper OBJECT_MAPPER = JsonBaseModel.getMapper();
private static final ObjectMapper objectMapper = JsonBaseModel.getMapper();

/** Holds the state during a single schema generation process to handle caching and recursion. */
private static class SchemaGenerationContext {
Expand Down Expand Up @@ -162,7 +162,7 @@ private static Schema buildSchemaFromParameter(Parameter param) {
* @throws IllegalArgumentException if a type is encountered that cannot be serialized by Jackson.
*/
public static Schema buildSchemaFromType(Type type) {
return buildSchemaRecursive(OBJECT_MAPPER.constructType(type), new SchemaGenerationContext());
return buildSchemaRecursive(objectMapper.constructType(type), new SchemaGenerationContext());
}

/**
Expand Down Expand Up @@ -217,7 +217,7 @@ private static Schema buildSchemaRecursive(JavaType javaType, SchemaGenerationCo
}
builder.enum_(enumValues).type("STRING").format("enum");
} else { // POJO
if (!OBJECT_MAPPER.canSerialize(rawClass)) {
if (!objectMapper.canSerialize(rawClass)) {
throw new IllegalArgumentException(
"Unsupported type: "
+ rawClass.getName()
Expand All @@ -226,7 +226,7 @@ private static Schema buildSchemaRecursive(JavaType javaType, SchemaGenerationCo
+ " directly.");
}
BeanDescription beanDescription =
OBJECT_MAPPER.getSerializationConfig().introspect(javaType);
objectMapper.getSerializationConfig().introspect(javaType);
Map<String, Schema> properties = new LinkedHashMap<>();
List<String> required = new ArrayList<>();
for (BeanPropertyDefinition property : beanDescription.findProperties()) {
Expand Down
16 changes: 7 additions & 9 deletions core/src/main/java/com/google/adk/tools/FunctionTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public class FunctionTool extends BaseTool {

private static final Logger logger = LoggerFactory.getLogger(FunctionTool.class);
private static final ObjectMapper OBJECT_MAPPER = JsonBaseModel.getMapper();
private static final ObjectMapper objectMapper = JsonBaseModel.getMapper();

private final @Nullable Object instance;
private final Method func;
Expand Down Expand Up @@ -241,17 +241,15 @@ private Maybe<Map<String, Object>> call(Map<String, Object> args, ToolContext to
} else if (result instanceof Maybe) {
return ((Maybe<?>) result)
.map(
data ->
OBJECT_MAPPER.convertValue(data, new TypeReference<Map<String, Object>>() {}));
data -> objectMapper.convertValue(data, new TypeReference<Map<String, Object>>() {}));
} else if (result instanceof Single) {
return ((Single<?>) result)
.map(
data -> OBJECT_MAPPER.convertValue(data, new TypeReference<Map<String, Object>>() {}))
.map(data -> objectMapper.convertValue(data, new TypeReference<Map<String, Object>>() {}))
.toMaybe();
} else {
try {
return Maybe.just(
OBJECT_MAPPER.convertValue(result, new TypeReference<Map<String, Object>>() {}));
objectMapper.convertValue(result, new TypeReference<Map<String, Object>>() {}));
} catch (IllegalArgumentException e) {
// Conversion to map failed, in this case we follow
// https://google.github.io/adk-docs/tools-custom/function-tools/#return-type and return
Expand Down Expand Up @@ -326,7 +324,7 @@ private Object[] buildArguments(
continue;
}
} else if (argValue instanceof Map) {
arguments[i] = OBJECT_MAPPER.convertValue(argValue, paramType);
arguments[i] = objectMapper.convertValue(argValue, paramType);
continue;
}
arguments[i] = castValue(argValue, paramType);
Expand Down Expand Up @@ -363,7 +361,7 @@ private static List<Object> createList(List<Object> values, Class<?> type) {
|| cls == String.class) {
list.add(castValue(value, cls));
} else {
list.add(OBJECT_MAPPER.convertValue(value, type));
list.add(objectMapper.convertValue(value, type));
}
}
return list;
Expand Down Expand Up @@ -414,6 +412,6 @@ private static Object castValue(Object value, Class<?> type) {
return value;
}
}
return OBJECT_MAPPER.convertValue(value, type);
return objectMapper.convertValue(value, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ApplicationIntegrationToolset implements BaseToolset {
String serviceAccountJson;
@Nullable String toolNamePrefix;
@Nullable String toolInstructions;
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static final ObjectMapper objectMapper = new ObjectMapper();
private final HttpClient httpClient;
private final CredentialsHelper credentialsHelper;

Expand Down Expand Up @@ -118,13 +118,13 @@ public ApplicationIntegrationToolset(

List<String> getPathUrl(String openApiSchemaString) throws Exception {
List<String> pathUrls = new ArrayList<>();
JsonNode topLevelNode = OBJECT_MAPPER.readTree(openApiSchemaString);
JsonNode topLevelNode = objectMapper.readTree(openApiSchemaString);
JsonNode specNode = topLevelNode.path("openApiSpec");
if (specNode.isMissingNode() || !specNode.isTextual()) {
throw new IllegalArgumentException(
"Failed to get OpenApiSpec, please check the project and region for the integration.");
}
JsonNode rootNode = OBJECT_MAPPER.readTree(specNode.asText());
JsonNode rootNode = objectMapper.readTree(specNode.asText());
JsonNode pathsNode = rootNode.path("paths");
Iterator<Map.Entry<String, JsonNode>> paths = pathsNode.fields();
while (paths.hasNext()) {
Expand Down Expand Up @@ -184,12 +184,12 @@ private List<BaseTool> getAllTools() throws Exception {
this.serviceAccountJson,
this.httpClient,
this.credentialsHelper);
ObjectNode parentOpenApiSpec = OBJECT_MAPPER.createObjectNode();
ObjectNode parentOpenApiSpec = objectMapper.createObjectNode();
ObjectNode openApiSpec =
integrationClient.getOpenApiSpecForConnection(toolNamePrefix, toolInstructions);
String openApiSpecString = OBJECT_MAPPER.writeValueAsString(openApiSpec);
String openApiSpecString = objectMapper.writeValueAsString(openApiSpec);
parentOpenApiSpec.put("openApiSpec", openApiSpecString);
openApiSchemaString = OBJECT_MAPPER.writeValueAsString(parentOpenApiSpec);
openApiSchemaString = objectMapper.writeValueAsString(parentOpenApiSpec);
List<String> pathUrls = getPathUrl(openApiSchemaString);
for (String pathUrl : pathUrls) {
String toolName = integrationClient.getOperationIdFromPathUrl(openApiSchemaString, pathUrl);
Expand All @@ -202,7 +202,7 @@ private List<BaseTool> getAllTools() throws Exception {
this.serviceAccountJson,
this.httpClient,
this.credentialsHelper,
OBJECT_MAPPER);
objectMapper);

ConnectionsClient.ConnectionDetails connectionDetails =
connectionsClient.getConnectionDetails();
Expand Down
Loading