|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// Auto-generated code. Do not edit. |
| 18 | + |
| 19 | +package com.google.cloud.vertexai.genai; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.databind.JsonNode; |
| 22 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | +import com.google.cloud.vertexai.genai.types.GetAgentEngineMemoryRevisionConfig; |
| 24 | +import com.google.cloud.vertexai.genai.types.GetAgentEngineMemoryRevisionRequestParameters; |
| 25 | +import com.google.cloud.vertexai.genai.types.ListAgentEngineMemoryRevisionsConfig; |
| 26 | +import com.google.cloud.vertexai.genai.types.ListAgentEngineMemoryRevisionsRequestParameters; |
| 27 | +import com.google.cloud.vertexai.genai.types.ListAgentEngineMemoryRevisionsResponse; |
| 28 | +import com.google.cloud.vertexai.genai.types.MemoryRevision; |
| 29 | +import com.google.genai.ApiClient; |
| 30 | +import com.google.genai.ApiResponse; |
| 31 | +import com.google.genai.Common; |
| 32 | +import com.google.genai.Common.BuiltRequest; |
| 33 | +import com.google.genai.JsonSerializable; |
| 34 | +import com.google.genai.errors.GenAiIOException; |
| 35 | +import com.google.genai.types.HttpOptions; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.Optional; |
| 38 | +import okhttp3.ResponseBody; |
| 39 | + |
| 40 | +public final class MemoryRevisions { |
| 41 | + |
| 42 | + final ApiClient apiClient; |
| 43 | + |
| 44 | + public MemoryRevisions(ApiClient apiClient) { |
| 45 | + this.apiClient = apiClient; |
| 46 | + } |
| 47 | + |
| 48 | + @ExcludeFromGeneratedCoverageReport |
| 49 | + ObjectNode getAgentEngineMemoryRevisionRequestParametersToVertex( |
| 50 | + JsonNode fromObject, ObjectNode parentObject) { |
| 51 | + ObjectNode toObject = JsonSerializable.objectMapper().createObjectNode(); |
| 52 | + if (Common.getValueByPath(fromObject, new String[] {"name"}) != null) { |
| 53 | + Common.setValueByPath( |
| 54 | + toObject, |
| 55 | + new String[] {"_url", "name"}, |
| 56 | + Common.getValueByPath(fromObject, new String[] {"name"})); |
| 57 | + } |
| 58 | + |
| 59 | + return toObject; |
| 60 | + } |
| 61 | + |
| 62 | + @ExcludeFromGeneratedCoverageReport |
| 63 | + ObjectNode listAgentEngineMemoryRevisionsConfigToVertex( |
| 64 | + JsonNode fromObject, ObjectNode parentObject) { |
| 65 | + ObjectNode toObject = JsonSerializable.objectMapper().createObjectNode(); |
| 66 | + |
| 67 | + if (Common.getValueByPath(fromObject, new String[] {"pageSize"}) != null) { |
| 68 | + Common.setValueByPath( |
| 69 | + parentObject, |
| 70 | + new String[] {"_query", "pageSize"}, |
| 71 | + Common.getValueByPath(fromObject, new String[] {"pageSize"})); |
| 72 | + } |
| 73 | + |
| 74 | + if (Common.getValueByPath(fromObject, new String[] {"pageToken"}) != null) { |
| 75 | + Common.setValueByPath( |
| 76 | + parentObject, |
| 77 | + new String[] {"_query", "pageToken"}, |
| 78 | + Common.getValueByPath(fromObject, new String[] {"pageToken"})); |
| 79 | + } |
| 80 | + |
| 81 | + if (Common.getValueByPath(fromObject, new String[] {"filter"}) != null) { |
| 82 | + Common.setValueByPath( |
| 83 | + parentObject, |
| 84 | + new String[] {"_query", "filter"}, |
| 85 | + Common.getValueByPath(fromObject, new String[] {"filter"})); |
| 86 | + } |
| 87 | + |
| 88 | + return toObject; |
| 89 | + } |
| 90 | + |
| 91 | + @ExcludeFromGeneratedCoverageReport |
| 92 | + ObjectNode listAgentEngineMemoryRevisionsRequestParametersToVertex( |
| 93 | + JsonNode fromObject, ObjectNode parentObject) { |
| 94 | + ObjectNode toObject = JsonSerializable.objectMapper().createObjectNode(); |
| 95 | + if (Common.getValueByPath(fromObject, new String[] {"name"}) != null) { |
| 96 | + Common.setValueByPath( |
| 97 | + toObject, |
| 98 | + new String[] {"_url", "name"}, |
| 99 | + Common.getValueByPath(fromObject, new String[] {"name"})); |
| 100 | + } |
| 101 | + |
| 102 | + if (Common.getValueByPath(fromObject, new String[] {"config"}) != null) { |
| 103 | + JsonNode unused = |
| 104 | + listAgentEngineMemoryRevisionsConfigToVertex( |
| 105 | + JsonSerializable.toJsonNode( |
| 106 | + Common.getValueByPath(fromObject, new String[] {"config"})), |
| 107 | + toObject); |
| 108 | + } |
| 109 | + |
| 110 | + return toObject; |
| 111 | + } |
| 112 | + |
| 113 | + /** A shared buildRequest method for both sync and async methods. */ |
| 114 | + BuiltRequest buildRequestForGet(String name, GetAgentEngineMemoryRevisionConfig config) { |
| 115 | + |
| 116 | + GetAgentEngineMemoryRevisionRequestParameters.Builder parameterBuilder = |
| 117 | + GetAgentEngineMemoryRevisionRequestParameters.builder(); |
| 118 | + |
| 119 | + if (!Common.isZero(name)) { |
| 120 | + parameterBuilder.name(name); |
| 121 | + } |
| 122 | + if (!Common.isZero(config)) { |
| 123 | + parameterBuilder.config(config); |
| 124 | + } |
| 125 | + JsonNode parameterNode = JsonSerializable.toJsonNode(parameterBuilder.build()); |
| 126 | + |
| 127 | + ObjectNode body; |
| 128 | + String path; |
| 129 | + if (this.apiClient.vertexAI()) { |
| 130 | + body = getAgentEngineMemoryRevisionRequestParametersToVertex(parameterNode, null); |
| 131 | + path = Common.formatMap("{name}", body.get("_url")); |
| 132 | + } else { |
| 133 | + throw new UnsupportedOperationException( |
| 134 | + "This method is only supported in the Vertex AI client."); |
| 135 | + } |
| 136 | + body.remove("_url"); |
| 137 | + |
| 138 | + JsonNode queryParams = body.get("_query"); |
| 139 | + if (queryParams != null) { |
| 140 | + body.remove("_query"); |
| 141 | + path = String.format("%s?%s", path, Common.urlEncode((ObjectNode) queryParams)); |
| 142 | + } |
| 143 | + |
| 144 | + // TODO: Remove the hack that removes config. |
| 145 | + Optional<HttpOptions> requestHttpOptions = Optional.empty(); |
| 146 | + if (config != null) { |
| 147 | + requestHttpOptions = config.httpOptions(); |
| 148 | + } |
| 149 | + |
| 150 | + return new BuiltRequest(path, JsonSerializable.toJsonString(body), requestHttpOptions); |
| 151 | + } |
| 152 | + |
| 153 | + /** A shared processResponse function for both sync and async methods. */ |
| 154 | + MemoryRevision processResponseForGet( |
| 155 | + ApiResponse response, GetAgentEngineMemoryRevisionConfig config) { |
| 156 | + ResponseBody responseBody = response.getBody(); |
| 157 | + String responseString; |
| 158 | + try { |
| 159 | + responseString = responseBody.string(); |
| 160 | + } catch (IOException e) { |
| 161 | + throw new GenAiIOException("Failed to read HTTP response.", e); |
| 162 | + } |
| 163 | + |
| 164 | + JsonNode responseNode = JsonSerializable.stringToJsonNode(responseString); |
| 165 | + |
| 166 | + if (!this.apiClient.vertexAI()) { |
| 167 | + throw new UnsupportedOperationException( |
| 168 | + "This method is only supported in the Vertex AI client."); |
| 169 | + } |
| 170 | + |
| 171 | + return JsonSerializable.fromJsonNode(responseNode, MemoryRevision.class); |
| 172 | + } |
| 173 | + |
| 174 | + public MemoryRevision get(String name, GetAgentEngineMemoryRevisionConfig config) { |
| 175 | + BuiltRequest builtRequest = buildRequestForGet(name, config); |
| 176 | + |
| 177 | + try (ApiResponse response = |
| 178 | + this.apiClient.request( |
| 179 | + "get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())) { |
| 180 | + return processResponseForGet(response, config); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** A shared buildRequest method for both sync and async methods. */ |
| 185 | + BuiltRequest buildRequestForPrivateList( |
| 186 | + String name, ListAgentEngineMemoryRevisionsConfig config) { |
| 187 | + |
| 188 | + ListAgentEngineMemoryRevisionsRequestParameters.Builder parameterBuilder = |
| 189 | + ListAgentEngineMemoryRevisionsRequestParameters.builder(); |
| 190 | + |
| 191 | + if (!Common.isZero(name)) { |
| 192 | + parameterBuilder.name(name); |
| 193 | + } |
| 194 | + if (!Common.isZero(config)) { |
| 195 | + parameterBuilder.config(config); |
| 196 | + } |
| 197 | + JsonNode parameterNode = JsonSerializable.toJsonNode(parameterBuilder.build()); |
| 198 | + |
| 199 | + ObjectNode body; |
| 200 | + String path; |
| 201 | + if (this.apiClient.vertexAI()) { |
| 202 | + body = listAgentEngineMemoryRevisionsRequestParametersToVertex(parameterNode, null); |
| 203 | + path = Common.formatMap("{name}/revisions", body.get("_url")); |
| 204 | + } else { |
| 205 | + throw new UnsupportedOperationException( |
| 206 | + "This method is only supported in the Vertex AI client."); |
| 207 | + } |
| 208 | + body.remove("_url"); |
| 209 | + |
| 210 | + JsonNode queryParams = body.get("_query"); |
| 211 | + if (queryParams != null) { |
| 212 | + body.remove("_query"); |
| 213 | + path = String.format("%s?%s", path, Common.urlEncode((ObjectNode) queryParams)); |
| 214 | + } |
| 215 | + |
| 216 | + // TODO: Remove the hack that removes config. |
| 217 | + Optional<HttpOptions> requestHttpOptions = Optional.empty(); |
| 218 | + if (config != null) { |
| 219 | + requestHttpOptions = config.httpOptions(); |
| 220 | + } |
| 221 | + |
| 222 | + return new BuiltRequest(path, JsonSerializable.toJsonString(body), requestHttpOptions); |
| 223 | + } |
| 224 | + |
| 225 | + /** A shared processResponse function for both sync and async methods. */ |
| 226 | + ListAgentEngineMemoryRevisionsResponse processResponseForPrivateList( |
| 227 | + ApiResponse response, ListAgentEngineMemoryRevisionsConfig config) { |
| 228 | + ResponseBody responseBody = response.getBody(); |
| 229 | + String responseString; |
| 230 | + try { |
| 231 | + responseString = responseBody.string(); |
| 232 | + } catch (IOException e) { |
| 233 | + throw new GenAiIOException("Failed to read HTTP response.", e); |
| 234 | + } |
| 235 | + |
| 236 | + JsonNode responseNode = JsonSerializable.stringToJsonNode(responseString); |
| 237 | + |
| 238 | + if (!this.apiClient.vertexAI()) { |
| 239 | + throw new UnsupportedOperationException( |
| 240 | + "This method is only supported in the Vertex AI client."); |
| 241 | + } |
| 242 | + |
| 243 | + return JsonSerializable.fromJsonNode( |
| 244 | + responseNode, ListAgentEngineMemoryRevisionsResponse.class); |
| 245 | + } |
| 246 | + |
| 247 | + public ListAgentEngineMemoryRevisionsResponse privateList( |
| 248 | + String name, ListAgentEngineMemoryRevisionsConfig config) { |
| 249 | + BuiltRequest builtRequest = buildRequestForPrivateList(name, config); |
| 250 | + |
| 251 | + try (ApiResponse response = |
| 252 | + this.apiClient.request( |
| 253 | + "get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())) { |
| 254 | + return processResponseForPrivateList(response, config); |
| 255 | + } |
| 256 | + } |
| 257 | +} |
0 commit comments