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
18 changes: 18 additions & 0 deletions src/main/java/com/google/cloud/agentplatform/AsyncMemoryBanks.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.cloud.agentplatform.types.MemoryGenerationTriggerConfig;
import com.google.cloud.agentplatform.types.ReasoningEngine;
import com.google.cloud.agentplatform.types.ReasoningEngineContextSpecMemoryBankConfig;
import com.google.cloud.agentplatform.types.UpdateMemoryBankConfig;
import com.google.genai.ApiClient;
import com.google.genai.ApiResponse;
import com.google.genai.Common.BuiltRequest;
Expand Down Expand Up @@ -66,6 +67,23 @@ CompletableFuture<MemoryBankOperation> privateCreate(
});
}

CompletableFuture<MemoryBankOperation> privateUpdate(
String name,
ReasoningEngineContextSpecMemoryBankConfig memoryBankConfig,
UpdateMemoryBankConfig config) {

BuiltRequest builtRequest =
memoryBanks.buildRequestForPrivateUpdate(name, memoryBankConfig, config);
return this.apiClient
.asyncRequest("patch", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
return memoryBanks.processResponseForPrivateUpdate(res, config);
}
});
}

CompletableFuture<DeleteMemoryBankOperation> privateDelete(
String name, boolean force, DeleteMemoryBankConfig config) {

Expand Down
141 changes: 141 additions & 0 deletions src/main/java/com/google/cloud/agentplatform/MemoryBanks.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import com.google.cloud.agentplatform.types.MemoryGenerationTriggerConfig;
import com.google.cloud.agentplatform.types.ReasoningEngine;
import com.google.cloud.agentplatform.types.ReasoningEngineContextSpecMemoryBankConfig;
import com.google.cloud.agentplatform.types.UpdateMemoryBankConfig;
import com.google.cloud.agentplatform.types.UpdateMemoryBankRequestParameters;
import com.google.genai.ApiClient;
import com.google.genai.ApiResponse;
import com.google.genai.Common;
Expand Down Expand Up @@ -813,6 +815,59 @@ ObjectNode structuredMemorySchemaConfigToVertex(JsonNode fromObject, ObjectNode
return toObject;
}

@ExcludeFromGeneratedCoverageReport
ObjectNode updateMemoryBankConfigToVertex(JsonNode fromObject, ObjectNode parentObject) {
ObjectNode toObject = JsonSerializable.objectMapper().createObjectNode();

if (Common.getValueByPath(fromObject, new String[] {"displayName"}) != null) {
Common.setValueByPath(
parentObject,
new String[] {"displayName"},
Common.getValueByPath(fromObject, new String[] {"displayName"}));
}

if (Common.getValueByPath(fromObject, new String[] {"description"}) != null) {
Common.setValueByPath(
parentObject,
new String[] {"description"},
Common.getValueByPath(fromObject, new String[] {"description"}));
}

return toObject;
}

@ExcludeFromGeneratedCoverageReport
ObjectNode updateMemoryBankRequestParametersToVertex(
JsonNode fromObject, ObjectNode parentObject) {
ObjectNode toObject = JsonSerializable.objectMapper().createObjectNode();
if (Common.getValueByPath(fromObject, new String[] {"name"}) != null) {
Common.setValueByPath(
toObject,
new String[] {"_url", "name"},
Common.getValueByPath(fromObject, new String[] {"name"}));
}

if (Common.getValueByPath(fromObject, new String[] {"memoryBankConfig"}) != null) {
Common.setValueByPath(
toObject,
new String[] {"context_spec", "memory_bank_config"},
reasoningEngineContextSpecMemoryBankConfigToVertex(
JsonSerializable.toJsonNode(
Common.getValueByPath(fromObject, new String[] {"memoryBankConfig"})),
toObject));
}

if (Common.getValueByPath(fromObject, new String[] {"config"}) != null) {
JsonNode unused =
updateMemoryBankConfigToVertex(
JsonSerializable.toJsonNode(
Common.getValueByPath(fromObject, new String[] {"config"})),
toObject);
}

return toObject;
}

/** A shared buildRequest method for both sync and async methods. */
BuiltRequest buildRequestForPrivateCreate(
ReasoningEngineContextSpecMemoryBankConfig memoryBankConfig, CreateMemoryBankConfig config) {
Expand Down Expand Up @@ -892,6 +947,92 @@ public MemoryBankOperation privateCreate(
}
}

/** A shared buildRequest method for both sync and async methods. */
BuiltRequest buildRequestForPrivateUpdate(
String name,
ReasoningEngineContextSpecMemoryBankConfig memoryBankConfig,
UpdateMemoryBankConfig config) {

UpdateMemoryBankRequestParameters.Builder parameterBuilder =
UpdateMemoryBankRequestParameters.builder();

if (!Common.isZero(name)) {
parameterBuilder.name(name);
}
if (!Common.isZero(memoryBankConfig)) {
parameterBuilder.memoryBankConfig(memoryBankConfig);
}
if (!Common.isZero(config)) {
parameterBuilder.config(config);
}
JsonNode parameterNode = JsonSerializable.toJsonNode(parameterBuilder.build());

ObjectNode body;
String path;
if (this.apiClient.vertexAI()) {
body = updateMemoryBankRequestParametersToVertex(parameterNode, null);
path = Common.formatMap("{name}", body.get("_url"));
} else {
throw new UnsupportedOperationException(
"This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini"
+ " Developer API mode.");
}
body.remove("_url");

JsonNode queryParams = body.get("_query");
if (queryParams != null) {
body.remove("_query");
path = String.format("%s?%s", path, Common.urlEncode((ObjectNode) queryParams));
}

// TODO: Remove the hack that removes config.
Optional<HttpOptions> requestHttpOptions = Optional.empty();
if (config != null) {
requestHttpOptions = config.httpOptions();
}

return new BuiltRequest(path, JsonSerializable.toJsonString(body), requestHttpOptions);
}

/** A shared processResponse function for both sync and async methods. */
MemoryBankOperation processResponseForPrivateUpdate(
ApiResponse response, UpdateMemoryBankConfig config) {
ResponseBody responseBody = response.getBody();
String responseString;
try {
responseString = responseBody.string();
} catch (IOException e) {
throw new GenAiIOException("Failed to read HTTP response.", e);
}

JsonNode responseNode = JsonSerializable.stringToJsonNode(responseString);

if (this.apiClient.vertexAI()) {
responseNode = memoryBankOperationFromVertex(responseNode, null);
}

if (!this.apiClient.vertexAI()) {
throw new UnsupportedOperationException(
"This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini"
+ " Developer API mode.");
}

return JsonSerializable.fromJsonNode(responseNode, MemoryBankOperation.class);
}

public MemoryBankOperation privateUpdate(
String name,
ReasoningEngineContextSpecMemoryBankConfig memoryBankConfig,
UpdateMemoryBankConfig config) {
BuiltRequest builtRequest = buildRequestForPrivateUpdate(name, memoryBankConfig, config);

try (ApiResponse response =
this.apiClient.request(
"patch", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())) {
return processResponseForPrivateUpdate(response, config);
}
}

/** A shared buildRequest method for both sync and async methods. */
BuiltRequest buildRequestForPrivateDelete(
String name, boolean force, DeleteMemoryBankConfig config) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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
*
* https://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.
*/

// Auto-generated code. Do not edit.

package com.google.cloud.agentplatform.types;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.genai.JsonSerializable;
import com.google.genai.types.HttpOptions;
import java.util.Optional;

/** Config for updating a memory bank. */
@AutoValue
@JsonDeserialize(builder = UpdateMemoryBankConfig.Builder.class)
public abstract class UpdateMemoryBankConfig extends JsonSerializable {
/** Used to override HTTP request options. */
@JsonProperty("httpOptions")
public abstract Optional<HttpOptions> httpOptions();

/**
* The user-defined name of the Memory Bank.
*
* <p>The display name can be up to 128 characters long and can comprise any UTF-8 characters.
*/
@JsonProperty("displayName")
public abstract Optional<String> displayName();

/** The description of the Memory Bank. */
@JsonProperty("description")
public abstract Optional<String> description();

/** Instantiates a builder for UpdateMemoryBankConfig. */
@ExcludeFromGeneratedCoverageReport
public static Builder builder() {
return new AutoValue_UpdateMemoryBankConfig.Builder();
}

/** Creates a builder with the same values as this instance. */
public abstract Builder toBuilder();

/** Builder for UpdateMemoryBankConfig. */
@AutoValue.Builder
public abstract static class Builder {
/** For internal usage. Please use `UpdateMemoryBankConfig.builder()` for instantiation. */
@JsonCreator
private static Builder create() {
return new AutoValue_UpdateMemoryBankConfig.Builder();
}

/**
* Setter for httpOptions.
*
* <p>httpOptions: Used to override HTTP request options.
*/
@JsonProperty("httpOptions")
public abstract Builder httpOptions(HttpOptions httpOptions);

@ExcludeFromGeneratedCoverageReport
abstract Builder httpOptions(Optional<HttpOptions> httpOptions);

/** Clears the value of httpOptions field. */
@ExcludeFromGeneratedCoverageReport
@CanIgnoreReturnValue
public Builder clearHttpOptions() {
return httpOptions(Optional.empty());
}

/**
* Setter for displayName.
*
* <p>displayName: The user-defined name of the Memory Bank.
*
* <p>The display name can be up to 128 characters long and can comprise any UTF-8 characters.
*/
@JsonProperty("displayName")
public abstract Builder displayName(String displayName);

@ExcludeFromGeneratedCoverageReport
abstract Builder displayName(Optional<String> displayName);

/** Clears the value of displayName field. */
@ExcludeFromGeneratedCoverageReport
@CanIgnoreReturnValue
public Builder clearDisplayName() {
return displayName(Optional.empty());
}

/**
* Setter for description.
*
* <p>description: The description of the Memory Bank.
*/
@JsonProperty("description")
public abstract Builder description(String description);

@ExcludeFromGeneratedCoverageReport
abstract Builder description(Optional<String> description);

/** Clears the value of description field. */
@ExcludeFromGeneratedCoverageReport
@CanIgnoreReturnValue
public Builder clearDescription() {
return description(Optional.empty());
}

public abstract UpdateMemoryBankConfig build();
}

/** Deserializes a JSON string to a UpdateMemoryBankConfig object. */
@ExcludeFromGeneratedCoverageReport
public static UpdateMemoryBankConfig fromJson(String jsonString) {
return JsonSerializable.fromJsonString(jsonString, UpdateMemoryBankConfig.class);
}
}
Loading
Loading