From bdd93d57ff5952ba9bcbdd64e2d8998ad3f935ac Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Thu, 6 Aug 2026 13:07:39 -0700 Subject: [PATCH] feat: Add a top-level `client.sandboxes` accessor to the Vertex AI GenAI SDK for Java, with `environments`, `templates`, and `snapshots` submodules. PiperOrigin-RevId: 960460418 --- ...entEngineSandboxTemplatesAndSnapshots.java | 195 +++-- .../cloud/agentplatform/AgentEngines.java | 49 ++ .../AsyncSandboxEnvironments.java | 139 +++ .../cloud/agentplatform/AsyncSandboxes.java | 2 + .../google/cloud/agentplatform/Client.java | 22 +- .../agentplatform/SandboxEnvironments.java | 800 ++++++++++++++++++ .../cloud/agentplatform/SandboxSnapshots.java | 60 ++ .../cloud/agentplatform/SandboxTemplates.java | 73 ++ .../google/cloud/agentplatform/Sandboxes.java | 98 +++ 9 files changed, 1344 insertions(+), 94 deletions(-) create mode 100644 src/main/java/com/google/cloud/agentplatform/AsyncSandboxEnvironments.java create mode 100644 src/main/java/com/google/cloud/agentplatform/SandboxEnvironments.java diff --git a/examples/src/main/java/com/google/cloud/agentplatform/examples/AgentEngineSandboxTemplatesAndSnapshots.java b/examples/src/main/java/com/google/cloud/agentplatform/examples/AgentEngineSandboxTemplatesAndSnapshots.java index 613225d..c84c561 100644 --- a/examples/src/main/java/com/google/cloud/agentplatform/examples/AgentEngineSandboxTemplatesAndSnapshots.java +++ b/examples/src/main/java/com/google/cloud/agentplatform/examples/AgentEngineSandboxTemplatesAndSnapshots.java @@ -36,32 +36,36 @@ package com.google.cloud.agentplatform.examples; import com.google.cloud.agentplatform.Client; -import com.google.cloud.agentplatform.types.AgentEngineOperation; import com.google.cloud.agentplatform.types.AgentEngineSandboxOperation; import com.google.cloud.agentplatform.types.AgentEngineSandboxSnapshotOperation; -import com.google.cloud.agentplatform.types.CreateAgentEngineConfig; +import com.google.cloud.agentplatform.types.CreateAgentEngineSandboxConfig; import com.google.cloud.agentplatform.types.CreateAgentEngineSandboxSnapshotConfig; import com.google.cloud.agentplatform.types.CreateSandboxEnvironmentTemplateConfig; -import com.google.cloud.agentplatform.types.DeleteAgentEngineOperation; import com.google.cloud.agentplatform.types.DeleteAgentEngineSandboxOperation; import com.google.cloud.agentplatform.types.DeleteSandboxEnvironmentSnapshotOperation; import com.google.cloud.agentplatform.types.DeleteSandboxEnvironmentTemplateOperation; import com.google.cloud.agentplatform.types.ListSandboxEnvironmentSnapshotsResponse; import com.google.cloud.agentplatform.types.ListSandboxEnvironmentTemplatesResponse; import com.google.cloud.agentplatform.types.SandboxEnvironmentSnapshot; -import com.google.cloud.agentplatform.types.SandboxEnvironmentSpec; -import com.google.cloud.agentplatform.types.SandboxEnvironmentSpecCodeExecutionEnvironment; import com.google.cloud.agentplatform.types.SandboxEnvironmentTemplate; import com.google.cloud.agentplatform.types.SandboxEnvironmentTemplateDefaultContainerEnvironment; import com.google.cloud.agentplatform.types.SandboxEnvironmentTemplateEgressControlConfig; import com.google.cloud.agentplatform.types.SandboxEnvironmentTemplateOperation; -import com.google.common.collect.ImmutableMap; import java.time.Duration; import java.util.List; /** - * An example of using the Java SDK to perform operations on Agent Engine sandbox environment - * templates and snapshots. + * An example of using the Java SDK to perform operations on Agent Platform sandbox environments, + * templates, and snapshots, using only the public API surface. + * + *

The sandbox API is reached from the top-level {@code client.sandboxes} accessor, with its + * {@code environments}, {@code templates}, and {@code snapshots} submodules. The parent agent + * engine name is omitted, so the SDK lazily reuses a shared default agent engine for this project + + * location. + * + *

The flow mirrors the canonical usage: create a computer-use sandbox template, create a sandbox + * environment from that template, snapshot the running sandbox, then restore a new sandbox from the + * snapshot. Snapshots can only be taken of template-based (e.g. computer-use) sandboxes. */ public final class AgentEngineSandboxTemplatesAndSnapshots { public static void main(String[] args) { @@ -69,58 +73,25 @@ public static void main(String[] args) { // `GOOGLE_CLOUD_PROJECT`. Client client = new Client(); - System.out.println("Creating a temporary Agent Engine..."); - CreateAgentEngineConfig config = - CreateAgentEngineConfig.builder() - .labels(ImmutableMap.of("test-label", "test-value")) - .build(); - AgentEngineOperation createOperation = client.agentEngines.privateCreate(config); - while (!createOperation.done().filter(Boolean::booleanValue).isPresent()) { - sleep(10000); - createOperation = - client.agentEngines.privateGetAgentOperation(createOperation.name().get(), null); - } - String agentEngineName = createOperation.response().get().name().get(); - System.out.println("Created Agent Engine: " + agentEngineName); - - try { - runSandboxTemplatesExample(client, agentEngineName); - runSandboxSnapshotsExample(client, agentEngineName); - } finally { - System.out.println("\nCleaning up Agent Engine..."); - DeleteAgentEngineOperation deleteOperation = - client.agentEngines.privateDelete(agentEngineName, true, null); - System.out.println("Delete operation started: " + deleteOperation.name().orElse("")); - } + runSandboxTemplatesExample(client); + runSandboxSnapshotsExample(client); } /** Demonstrates the sandbox templates submodule: create, get, list, and delete. */ - private static void runSandboxTemplatesExample(Client client, String agentEngineName) { + private static void runSandboxTemplatesExample(Client client) { System.out.println("\n=== Sandbox Templates ==="); - // 1. Create a sandbox environment template. + // 1. Create a computer-use sandbox environment template (no agent engine name needed). System.out.println("\n--- Creating a sandbox template ---"); SandboxEnvironmentTemplateOperation templateOp = - client.agentEngines.sandboxes.templates.privateCreate( - agentEngineName, - "Example Sandbox Template", - CreateSandboxEnvironmentTemplateConfig.builder() - .defaultContainerEnvironment( - SandboxEnvironmentTemplateDefaultContainerEnvironment.builder() - .defaultContainerCategory("DEFAULT_CONTAINER_CATEGORY_COMPUTER_USE") - .build()) - .egressControlConfig( - SandboxEnvironmentTemplateEgressControlConfig.builder() - .internetAccess(true) - .build()) - .build()); + client.sandboxes.templates.create("Example Sandbox Template", computerUseTemplateConfig()); System.out.println("Create template operation: " + templateOp.name().orElse("")); // Wait for the template creation to complete. while (!templateOp.done().filter(Boolean::booleanValue).isPresent()) { sleep(10000); templateOp = - client.agentEngines.sandboxes.templates.getSandboxEnvironmentTemplateOperation( + client.sandboxes.templates.getSandboxEnvironmentTemplateOperation( templateOp.name().get(), null); } String templateName = templateOp.response().get().name().get(); @@ -128,101 +99,141 @@ private static void runSandboxTemplatesExample(Client client, String agentEngine // 2. Get the template. System.out.println("\n--- Getting the sandbox template ---"); - SandboxEnvironmentTemplate template = - client.agentEngines.sandboxes.templates.privateGet(templateName, null); + SandboxEnvironmentTemplate template = client.sandboxes.templates.get(templateName, null); System.out.println("Template display name: " + template.displayName().orElse("")); - // 3. List templates. + // 3. List templates (no agent engine name needed). System.out.println("\n--- Listing sandbox templates ---"); - ListSandboxEnvironmentTemplatesResponse listResponse = - client.agentEngines.sandboxes.templates.privateList(agentEngineName, null); + ListSandboxEnvironmentTemplatesResponse listResponse = client.sandboxes.templates.list(null); System.out.println( "Templates found: " + listResponse.sandboxEnvironmentTemplates().map(List::size).orElse(0)); // 4. Delete the template. System.out.println("\n--- Deleting the sandbox template ---"); DeleteSandboxEnvironmentTemplateOperation deleteOp = - client.agentEngines.sandboxes.templates.privateDelete(templateName, null); + client.sandboxes.templates.delete(templateName, null); System.out.println("Delete template operation: " + deleteOp.name().orElse("")); } /** - * Demonstrates the sandbox snapshots submodule: create a sandbox, snapshot it, get, list, and - * delete. + * Demonstrates the sandbox environments and snapshots submodules: create a template-based + * sandbox, snapshot it, get, list, restore a new sandbox from the snapshot, then clean up. */ - private static void runSandboxSnapshotsExample(Client client, String agentEngineName) { + private static void runSandboxSnapshotsExample(Client client) { System.out.println("\n=== Sandbox Snapshots ==="); - // A snapshot captures the state of an existing sandbox environment, so first create one. - System.out.println("\n--- Creating a source sandbox ---"); - AgentEngineSandboxOperation sandboxOp = - client.agentEngines.sandboxes.privateCreate( - agentEngineName, - SandboxEnvironmentSpec.builder() - .codeExecutionEnvironment( - SandboxEnvironmentSpecCodeExecutionEnvironment.builder() - .machineConfig("MACHINE_CONFIG_VCPU4_RAM4GIB") - .build()) - .build(), - null); - while (!sandboxOp.done().filter(Boolean::booleanValue).isPresent()) { + // Snapshots can only be taken of template-based sandboxes, so create a computer-use template + // and then a sandbox environment from it. + System.out.println("\n--- Creating a computer-use template ---"); + SandboxEnvironmentTemplateOperation templateOp = + client.sandboxes.templates.create("Snapshot Source Template", computerUseTemplateConfig()); + while (!templateOp.done().filter(Boolean::booleanValue).isPresent()) { sleep(10000); - sandboxOp = - client.agentEngines.sandboxes.privateGetSandboxOperation(sandboxOp.name().get(), null); + templateOp = + client.sandboxes.templates.getSandboxEnvironmentTemplateOperation( + templateOp.name().get(), null); } - String sandboxName = sandboxOp.response().get().name().get(); - System.out.println("Created source Sandbox: " + sandboxName); + String templateName = templateOp.response().get().name().get(); + System.out.println("Created Sandbox Template: " + templateName); + String sandboxName = null; + String restoredSandboxName = null; try { - // 1. Create a snapshot of the sandbox. + // Create a sandbox environment from the template (no agent engine name needed). + System.out.println("\n--- Creating a sandbox environment from the template ---"); + AgentEngineSandboxOperation sandboxOp = + client.sandboxes.environments.create( + /* spec= */ null, + CreateAgentEngineSandboxConfig.builder() + .displayName("Snapshot Source Sandbox") + .sandboxEnvironmentTemplate(templateName) + .build()); + while (!sandboxOp.done().filter(Boolean::booleanValue).isPresent()) { + sleep(10000); + sandboxOp = client.sandboxes.environments.getSandboxOperation(sandboxOp.name().get(), null); + } + sandboxName = sandboxOp.response().get().name().get(); + System.out.println("Created source Sandbox: " + sandboxName); + + // 1. Create a snapshot of the running sandbox. System.out.println("\n--- Creating a sandbox snapshot ---"); AgentEngineSandboxSnapshotOperation snapshotOp = - client.agentEngines.sandboxes.snapshots.privateCreate( + client.sandboxes.snapshots.create( sandboxName, CreateAgentEngineSandboxSnapshotConfig.builder() .displayName("Example Sandbox Snapshot") .ttl(Duration.ofSeconds(3600)) .build()); - System.out.println("Create snapshot operation: " + snapshotOp.name().orElse("")); - - // Wait for the snapshot creation to complete. while (!snapshotOp.done().filter(Boolean::booleanValue).isPresent()) { sleep(10000); snapshotOp = - client.agentEngines.sandboxes.snapshots.getSandboxSnapshotOperation( - snapshotOp.name().get(), null); + client.sandboxes.snapshots.getSandboxSnapshotOperation(snapshotOp.name().get(), null); } String snapshotName = snapshotOp.response().get().name().get(); System.out.println("Created Sandbox Snapshot: " + snapshotName); // 2. Get the snapshot. System.out.println("\n--- Getting the sandbox snapshot ---"); - SandboxEnvironmentSnapshot snapshot = - client.agentEngines.sandboxes.snapshots.privateGet(snapshotName, null); + SandboxEnvironmentSnapshot snapshot = client.sandboxes.snapshots.get(snapshotName, null); System.out.println("Snapshot Name: " + snapshot.name().orElse("")); - // 3. List snapshots. + // 3. List snapshots (no agent engine name needed). System.out.println("\n--- Listing sandbox snapshots ---"); - ListSandboxEnvironmentSnapshotsResponse listResponse = - client.agentEngines.sandboxes.snapshots.privateList(agentEngineName, null); + ListSandboxEnvironmentSnapshotsResponse listResponse = client.sandboxes.snapshots.list(null); System.out.println( "Snapshots found: " + listResponse.sandboxEnvironmentSnapshots().map(List::size).orElse(0)); - // 4. Delete the snapshot. + // 4. Restore a new sandbox environment from the snapshot. + System.out.println("\n--- Restoring a sandbox from the snapshot ---"); + AgentEngineSandboxOperation restoreOp = + client.sandboxes.environments.create( + /* spec= */ null, + CreateAgentEngineSandboxConfig.builder() + .displayName("Restored Sandbox") + .sandboxEnvironmentSnapshot(snapshotName) + .build()); + while (!restoreOp.done().filter(Boolean::booleanValue).isPresent()) { + sleep(10000); + restoreOp = client.sandboxes.environments.getSandboxOperation(restoreOp.name().get(), null); + } + restoredSandboxName = restoreOp.response().get().name().get(); + System.out.println("Restored Sandbox: " + restoredSandboxName); + + // 5. Delete the snapshot. System.out.println("\n--- Deleting the sandbox snapshot ---"); DeleteSandboxEnvironmentSnapshotOperation deleteOp = - client.agentEngines.sandboxes.snapshots.privateDelete(snapshotName, null); + client.sandboxes.snapshots.delete(snapshotName, null); System.out.println("Delete snapshot operation: " + deleteOp.name().orElse("")); } finally { - // Clean up the source sandbox. - System.out.println("\n--- Deleting the source sandbox ---"); - DeleteAgentEngineSandboxOperation deleteSandboxOp = - client.agentEngines.sandboxes.privateDelete(sandboxName, null); - System.out.println("Delete sandbox operation: " + deleteSandboxOp.name().orElse("")); + // Clean up the restored sandbox, source sandbox, and template. + if (restoredSandboxName != null) { + System.out.println("\n--- Deleting the restored sandbox ---"); + client.sandboxes.environments.delete(restoredSandboxName, null); + } + if (sandboxName != null) { + System.out.println("\n--- Deleting the source sandbox ---"); + DeleteAgentEngineSandboxOperation deleteSandboxOp = + client.sandboxes.environments.delete(sandboxName, null); + System.out.println("Delete sandbox operation: " + deleteSandboxOp.name().orElse("")); + } + System.out.println("\n--- Deleting the source template ---"); + client.sandboxes.templates.delete(templateName, null); } } + /** Returns the config for a computer-use sandbox template with internet access. */ + private static CreateSandboxEnvironmentTemplateConfig computerUseTemplateConfig() { + return CreateSandboxEnvironmentTemplateConfig.builder() + .defaultContainerEnvironment( + SandboxEnvironmentTemplateDefaultContainerEnvironment.builder() + .defaultContainerCategory("DEFAULT_CONTAINER_CATEGORY_COMPUTER_USE") + .build()) + .egressControlConfig( + SandboxEnvironmentTemplateEgressControlConfig.builder().internetAccess(true).build()) + .build(); + } + private static void sleep(long millis) { try { Thread.sleep(millis); diff --git a/src/main/java/com/google/cloud/agentplatform/AgentEngines.java b/src/main/java/com/google/cloud/agentplatform/AgentEngines.java index bd1a171..0b65e53 100644 --- a/src/main/java/com/google/cloud/agentplatform/AgentEngines.java +++ b/src/main/java/com/google/cloud/agentplatform/AgentEngines.java @@ -1401,4 +1401,53 @@ public AgentEngineOperation privateUpdate(String name, UpdateAgentEngineConfig c return processResponseForPrivateUpdate(response, config); } } + + private static final java.util.concurrent.ConcurrentHashMap + DEFAULT_AGENT_ENGINE_BY_LOCATION = new java.util.concurrent.ConcurrentHashMap<>(); + + private static final String DEFAULT_AGENT_ENGINE_DISPLAY_NAME = + "default-agent-platform-sandbox-host"; + + /** + * Returns the resource name of a shared "default" agent engine for the client's project and + * location, creating it lazily if it does not exist. Backs sandbox calls where the caller does + * not supply an agent engine name, so repeated calls reuse one agent engine instead of creating + * many. + */ + static String ensureDefaultAgentEngine(ApiClient apiClient) { + String key = apiClient.project() + "/" + apiClient.location(); + return DEFAULT_AGENT_ENGINE_BY_LOCATION.computeIfAbsent( + key, unused -> new AgentEngines(apiClient).resolveDefaultAgentEngine()); + } + + private String resolveDefaultAgentEngine() { + ListReasoningEnginesResponse listResponse = privateList(null); + if (listResponse.reasoningEngines().isPresent()) { + for (ReasoningEngine reasoningEngine : listResponse.reasoningEngines().get()) { + if (DEFAULT_AGENT_ENGINE_DISPLAY_NAME.equals(reasoningEngine.displayName().orElse(null)) + && reasoningEngine.name().isPresent()) { + return reasoningEngine.name().get(); + } + } + } + AgentEngineOperation operation = + privateCreate( + CreateAgentEngineConfig.builder() + .displayName(DEFAULT_AGENT_ENGINE_DISPLAY_NAME) + .build()); + while (!operation.done().filter(Boolean::booleanValue).isPresent()) { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("Interrupted while creating default agent engine.", e); + } + operation = privateGetAgentOperation(operation.name().get(), null); + } + if (operation.error().isPresent()) { + throw new IllegalStateException( + "Failed to create default agent engine: " + operation.error().get()); + } + return operation.response().get().name().get(); + } } diff --git a/src/main/java/com/google/cloud/agentplatform/AsyncSandboxEnvironments.java b/src/main/java/com/google/cloud/agentplatform/AsyncSandboxEnvironments.java new file mode 100644 index 0000000..a5af57e --- /dev/null +++ b/src/main/java/com/google/cloud/agentplatform/AsyncSandboxEnvironments.java @@ -0,0 +1,139 @@ +/* + * 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; + +import com.google.cloud.agentplatform.types.AgentEngineSandboxOperation; +import com.google.cloud.agentplatform.types.Chunk; +import com.google.cloud.agentplatform.types.CreateAgentEngineSandboxConfig; +import com.google.cloud.agentplatform.types.DeleteAgentEngineSandboxConfig; +import com.google.cloud.agentplatform.types.DeleteAgentEngineSandboxOperation; +import com.google.cloud.agentplatform.types.ExecuteCodeAgentEngineSandboxConfig; +import com.google.cloud.agentplatform.types.ExecuteSandboxEnvironmentResponse; +import com.google.cloud.agentplatform.types.GetAgentEngineOperationConfig; +import com.google.cloud.agentplatform.types.GetAgentEngineSandboxConfig; +import com.google.cloud.agentplatform.types.ListAgentEngineSandboxesConfig; +import com.google.cloud.agentplatform.types.ListAgentEngineSandboxesResponse; +import com.google.cloud.agentplatform.types.SandboxEnvironment; +import com.google.cloud.agentplatform.types.SandboxEnvironmentSpec; +import com.google.genai.ApiClient; +import com.google.genai.ApiResponse; +import com.google.genai.Common.BuiltRequest; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** Async module of {@link SandboxEnvironments} */ +public final class AsyncSandboxEnvironments { + + SandboxEnvironments sandboxEnvironments; + ApiClient apiClient; + + public AsyncSandboxEnvironments(ApiClient apiClient) { + this.apiClient = apiClient; + this.sandboxEnvironments = new SandboxEnvironments(apiClient); + } + + CompletableFuture privateCreate( + String name, SandboxEnvironmentSpec spec, CreateAgentEngineSandboxConfig config) { + + BuiltRequest builtRequest = + sandboxEnvironments.buildRequestForPrivateCreate(name, spec, config); + return this.apiClient + .asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions()) + .thenApplyAsync( + response -> { + try (ApiResponse res = response) { + return sandboxEnvironments.processResponseForPrivateCreate(res, config); + } + }); + } + + CompletableFuture privateDelete( + String name, DeleteAgentEngineSandboxConfig config) { + + BuiltRequest builtRequest = sandboxEnvironments.buildRequestForPrivateDelete(name, config); + return this.apiClient + .asyncRequest( + "delete", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions()) + .thenApplyAsync( + response -> { + try (ApiResponse res = response) { + return sandboxEnvironments.processResponseForPrivateDelete(res, config); + } + }); + } + + CompletableFuture privateExecuteCode( + String name, List inputs, ExecuteCodeAgentEngineSandboxConfig config) { + + BuiltRequest builtRequest = + sandboxEnvironments.buildRequestForPrivateExecuteCode(name, inputs, config); + return this.apiClient + .asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions()) + .thenApplyAsync( + response -> { + try (ApiResponse res = response) { + return sandboxEnvironments.processResponseForPrivateExecuteCode(res, config); + } + }); + } + + CompletableFuture privateGet( + String name, GetAgentEngineSandboxConfig config) { + + BuiltRequest builtRequest = sandboxEnvironments.buildRequestForPrivateGet(name, config); + return this.apiClient + .asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions()) + .thenApplyAsync( + response -> { + try (ApiResponse res = response) { + return sandboxEnvironments.processResponseForPrivateGet(res, config); + } + }); + } + + CompletableFuture privateList( + String name, ListAgentEngineSandboxesConfig config) { + + BuiltRequest builtRequest = sandboxEnvironments.buildRequestForPrivateList(name, config); + return this.apiClient + .asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions()) + .thenApplyAsync( + response -> { + try (ApiResponse res = response) { + return sandboxEnvironments.processResponseForPrivateList(res, config); + } + }); + } + + CompletableFuture privateGetSandboxOperation( + String operationName, GetAgentEngineOperationConfig config) { + + BuiltRequest builtRequest = + sandboxEnvironments.buildRequestForPrivateGetSandboxOperation(operationName, config); + return this.apiClient + .asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions()) + .thenApplyAsync( + response -> { + try (ApiResponse res = response) { + return sandboxEnvironments.processResponseForPrivateGetSandboxOperation( + res, config); + } + }); + } +} diff --git a/src/main/java/com/google/cloud/agentplatform/AsyncSandboxes.java b/src/main/java/com/google/cloud/agentplatform/AsyncSandboxes.java index 4777717..d8be9ad 100644 --- a/src/main/java/com/google/cloud/agentplatform/AsyncSandboxes.java +++ b/src/main/java/com/google/cloud/agentplatform/AsyncSandboxes.java @@ -39,6 +39,7 @@ /** Async module of {@link Sandboxes} */ public final class AsyncSandboxes { + public final AsyncSandboxEnvironments environments; public final AsyncSandboxTemplates templates; public final AsyncSandboxSnapshots snapshots; @@ -49,6 +50,7 @@ public AsyncSandboxes(ApiClient apiClient) { this.apiClient = apiClient; this.sandboxes = new Sandboxes(apiClient); + this.environments = new AsyncSandboxEnvironments(apiClient); this.templates = new AsyncSandboxTemplates(apiClient); this.snapshots = new AsyncSandboxSnapshots(apiClient); } diff --git a/src/main/java/com/google/cloud/agentplatform/Client.java b/src/main/java/com/google/cloud/agentplatform/Client.java index 2e09523..7b62285 100644 --- a/src/main/java/com/google/cloud/agentplatform/Client.java +++ b/src/main/java/com/google/cloud/agentplatform/Client.java @@ -37,10 +37,19 @@ public final class Client implements AutoCloseable { /** Async class for GenAI. */ public final class Async { - public final AsyncAgentEngines agentEngines; + // Package-private: the Agent Engine brand is not part of the public API surface. Callers reach + // sandboxes via the top-level `sandboxes` accessor below. + final AsyncAgentEngines agentEngines; + + /** + * Provides access to the Agent Platform Sandboxes API, including its {@code environments}, + * {@code templates}, and {@code snapshots} submodules. + */ + public final AsyncSandboxes sandboxes; public Async(ApiClient apiClient) { this.agentEngines = new AsyncAgentEngines(apiClient); + this.sandboxes = this.agentEngines.sandboxes; } } @@ -62,7 +71,15 @@ public Async(ApiClient apiClient) { private final DebugConfig debugConfig; private final ApiClient apiClient; - public final AgentEngines agentEngines; + // Package-private: the Agent Engine brand is not part of the public API surface. Callers reach + // sandboxes via the top-level `sandboxes` accessor below. + final AgentEngines agentEngines; + + /** + * Provides access to the Agent Platform Sandboxes API, including its {@code environments}, {@code + * templates}, and {@code snapshots} submodules. + */ + public final Sandboxes sandboxes; /** Builder for {@link Client}. */ public static class Builder { @@ -222,6 +239,7 @@ private Client( /* clientOptions= */ clientOptions); } agentEngines = new AgentEngines(this.apiClient); + sandboxes = agentEngines.sandboxes; } /** Returns the project ID for Vertex AI APIs. */ diff --git a/src/main/java/com/google/cloud/agentplatform/SandboxEnvironments.java b/src/main/java/com/google/cloud/agentplatform/SandboxEnvironments.java new file mode 100644 index 0000000..5c68ac3 --- /dev/null +++ b/src/main/java/com/google/cloud/agentplatform/SandboxEnvironments.java @@ -0,0 +1,800 @@ +/* + * 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; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.cloud.agentplatform.types.AgentEngineSandboxOperation; +import com.google.cloud.agentplatform.types.Chunk; +import com.google.cloud.agentplatform.types.CreateAgentEngineSandboxConfig; +import com.google.cloud.agentplatform.types.CreateAgentEngineSandboxRequestParameters; +import com.google.cloud.agentplatform.types.DeleteAgentEngineSandboxConfig; +import com.google.cloud.agentplatform.types.DeleteAgentEngineSandboxOperation; +import com.google.cloud.agentplatform.types.DeleteAgentEngineSandboxRequestParameters; +import com.google.cloud.agentplatform.types.ExecuteCodeAgentEngineSandboxConfig; +import com.google.cloud.agentplatform.types.ExecuteCodeAgentEngineSandboxRequestParameters; +import com.google.cloud.agentplatform.types.ExecuteSandboxEnvironmentResponse; +import com.google.cloud.agentplatform.types.GetAgentEngineOperationConfig; +import com.google.cloud.agentplatform.types.GetAgentEngineSandboxConfig; +import com.google.cloud.agentplatform.types.GetAgentEngineSandboxOperationParameters; +import com.google.cloud.agentplatform.types.GetAgentEngineSandboxRequestParameters; +import com.google.cloud.agentplatform.types.ListAgentEngineSandboxesConfig; +import com.google.cloud.agentplatform.types.ListAgentEngineSandboxesRequestParameters; +import com.google.cloud.agentplatform.types.ListAgentEngineSandboxesResponse; +import com.google.cloud.agentplatform.types.SandboxEnvironment; +import com.google.cloud.agentplatform.types.SandboxEnvironmentSpec; +import com.google.genai.ApiClient; +import com.google.genai.ApiResponse; +import com.google.genai.Common; +import com.google.genai.Common.BuiltRequest; +import com.google.genai.JsonSerializable; +import com.google.genai.errors.GenAiIOException; +import com.google.genai.types.HttpOptions; +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import okhttp3.ResponseBody; + +public final class SandboxEnvironments { + + final ApiClient apiClient; + + public SandboxEnvironments(ApiClient apiClient) { + this.apiClient = apiClient; + } + + @ExcludeFromGeneratedCoverageReport + ObjectNode createAgentEngineSandboxConfigToVertex(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"})); + } + + if (Common.getValueByPath(fromObject, new String[] {"ttl"}) != null) { + Common.setValueByPath( + parentObject, + new String[] {"ttl"}, + Common.getValueByPath(fromObject, new String[] {"ttl"})); + } + + if (Common.getValueByPath(fromObject, new String[] {"sandboxEnvironmentTemplate"}) != null) { + Common.setValueByPath( + parentObject, + new String[] {"sandboxEnvironmentTemplate"}, + Common.getValueByPath(fromObject, new String[] {"sandboxEnvironmentTemplate"})); + } + + if (Common.getValueByPath(fromObject, new String[] {"sandboxEnvironmentSnapshot"}) != null) { + Common.setValueByPath( + parentObject, + new String[] {"sandboxEnvironmentSnapshot"}, + Common.getValueByPath(fromObject, new String[] {"sandboxEnvironmentSnapshot"})); + } + + if (Common.getValueByPath(fromObject, new String[] {"owner"}) != null) { + Common.setValueByPath( + parentObject, + new String[] {"owner"}, + Common.getValueByPath(fromObject, new String[] {"owner"})); + } + + return toObject; + } + + @ExcludeFromGeneratedCoverageReport + ObjectNode createAgentEngineSandboxRequestParametersToVertex( + 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[] {"spec"}) != null) { + Common.setValueByPath( + toObject, + new String[] {"spec"}, + Common.getValueByPath(fromObject, new String[] {"spec"})); + } + + if (Common.getValueByPath(fromObject, new String[] {"config"}) != null) { + JsonNode unused = + createAgentEngineSandboxConfigToVertex( + JsonSerializable.toJsonNode( + Common.getValueByPath(fromObject, new String[] {"config"})), + toObject); + } + + return toObject; + } + + @ExcludeFromGeneratedCoverageReport + ObjectNode deleteAgentEngineSandboxRequestParametersToVertex( + 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"})); + } + + return toObject; + } + + @ExcludeFromGeneratedCoverageReport + ObjectNode executeCodeAgentEngineSandboxRequestParametersToVertex( + 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[] {"inputs"}) != null) { + Common.setValueByPath( + toObject, + new String[] {"inputs"}, + Common.getValueByPath(fromObject, new String[] {"inputs"})); + } + + return toObject; + } + + @ExcludeFromGeneratedCoverageReport + ObjectNode getAgentEngineSandboxOperationParametersToVertex( + JsonNode fromObject, ObjectNode parentObject) { + ObjectNode toObject = JsonSerializable.objectMapper().createObjectNode(); + if (Common.getValueByPath(fromObject, new String[] {"operationName"}) != null) { + Common.setValueByPath( + toObject, + new String[] {"_url", "operationName"}, + Common.getValueByPath(fromObject, new String[] {"operationName"})); + } + + return toObject; + } + + @ExcludeFromGeneratedCoverageReport + ObjectNode getAgentEngineSandboxRequestParametersToVertex( + 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"})); + } + + return toObject; + } + + @ExcludeFromGeneratedCoverageReport + ObjectNode listAgentEngineSandboxesConfigToVertex(JsonNode fromObject, ObjectNode parentObject) { + ObjectNode toObject = JsonSerializable.objectMapper().createObjectNode(); + + if (Common.getValueByPath(fromObject, new String[] {"pageSize"}) != null) { + Common.setValueByPath( + parentObject, + new String[] {"_query", "pageSize"}, + Common.getValueByPath(fromObject, new String[] {"pageSize"})); + } + + if (Common.getValueByPath(fromObject, new String[] {"pageToken"}) != null) { + Common.setValueByPath( + parentObject, + new String[] {"_query", "pageToken"}, + Common.getValueByPath(fromObject, new String[] {"pageToken"})); + } + + if (Common.getValueByPath(fromObject, new String[] {"filter"}) != null) { + Common.setValueByPath( + parentObject, + new String[] {"_query", "filter"}, + Common.getValueByPath(fromObject, new String[] {"filter"})); + } + + return toObject; + } + + @ExcludeFromGeneratedCoverageReport + ObjectNode listAgentEngineSandboxesRequestParametersToVertex( + 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[] {"config"}) != null) { + JsonNode unused = + listAgentEngineSandboxesConfigToVertex( + JsonSerializable.toJsonNode( + Common.getValueByPath(fromObject, new String[] {"config"})), + toObject); + } + + return toObject; + } + + /** A shared buildRequest method for both sync and async methods. */ + BuiltRequest buildRequestForPrivateCreate( + String name, SandboxEnvironmentSpec spec, CreateAgentEngineSandboxConfig config) { + + CreateAgentEngineSandboxRequestParameters.Builder parameterBuilder = + CreateAgentEngineSandboxRequestParameters.builder(); + + if (!Common.isZero(name)) { + parameterBuilder.name(name); + } + if (!Common.isZero(spec)) { + parameterBuilder.spec(spec); + } + if (!Common.isZero(config)) { + parameterBuilder.config(config); + } + JsonNode parameterNode = JsonSerializable.toJsonNode(parameterBuilder.build()); + + ObjectNode body; + String path; + if (this.apiClient.vertexAI()) { + body = createAgentEngineSandboxRequestParametersToVertex(parameterNode, null); + path = Common.formatMap("{name}/sandboxEnvironments", 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 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. */ + AgentEngineSandboxOperation processResponseForPrivateCreate( + ApiResponse response, CreateAgentEngineSandboxConfig 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()) { + throw new UnsupportedOperationException( + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini" + + " Developer API mode."); + } + + return JsonSerializable.fromJsonNode(responseNode, AgentEngineSandboxOperation.class); + } + + public AgentEngineSandboxOperation privateCreate( + String name, SandboxEnvironmentSpec spec, CreateAgentEngineSandboxConfig config) { + BuiltRequest builtRequest = buildRequestForPrivateCreate(name, spec, config); + + try (ApiResponse response = + this.apiClient.request( + "post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())) { + return processResponseForPrivateCreate(response, config); + } + } + + /** A shared buildRequest method for both sync and async methods. */ + BuiltRequest buildRequestForPrivateDelete(String name, DeleteAgentEngineSandboxConfig config) { + + DeleteAgentEngineSandboxRequestParameters.Builder parameterBuilder = + DeleteAgentEngineSandboxRequestParameters.builder(); + + if (!Common.isZero(name)) { + parameterBuilder.name(name); + } + if (!Common.isZero(config)) { + parameterBuilder.config(config); + } + JsonNode parameterNode = JsonSerializable.toJsonNode(parameterBuilder.build()); + + ObjectNode body; + String path; + if (this.apiClient.vertexAI()) { + body = deleteAgentEngineSandboxRequestParametersToVertex(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 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. */ + DeleteAgentEngineSandboxOperation processResponseForPrivateDelete( + ApiResponse response, DeleteAgentEngineSandboxConfig 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()) { + throw new UnsupportedOperationException( + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini" + + " Developer API mode."); + } + + return JsonSerializable.fromJsonNode(responseNode, DeleteAgentEngineSandboxOperation.class); + } + + public DeleteAgentEngineSandboxOperation privateDelete( + String name, DeleteAgentEngineSandboxConfig config) { + BuiltRequest builtRequest = buildRequestForPrivateDelete(name, config); + + try (ApiResponse response = + this.apiClient.request( + "delete", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())) { + return processResponseForPrivateDelete(response, config); + } + } + + /** A shared buildRequest method for both sync and async methods. */ + BuiltRequest buildRequestForPrivateExecuteCode( + String name, List inputs, ExecuteCodeAgentEngineSandboxConfig config) { + + ExecuteCodeAgentEngineSandboxRequestParameters.Builder parameterBuilder = + ExecuteCodeAgentEngineSandboxRequestParameters.builder(); + + if (!Common.isZero(name)) { + parameterBuilder.name(name); + } + if (!Common.isZero(inputs)) { + parameterBuilder.inputs(inputs); + } + if (!Common.isZero(config)) { + parameterBuilder.config(config); + } + JsonNode parameterNode = JsonSerializable.toJsonNode(parameterBuilder.build()); + + ObjectNode body; + String path; + if (this.apiClient.vertexAI()) { + body = executeCodeAgentEngineSandboxRequestParametersToVertex(parameterNode, null); + path = Common.formatMap("{name}/:execute", 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 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. */ + ExecuteSandboxEnvironmentResponse processResponseForPrivateExecuteCode( + ApiResponse response, ExecuteCodeAgentEngineSandboxConfig 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()) { + throw new UnsupportedOperationException( + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini" + + " Developer API mode."); + } + + return JsonSerializable.fromJsonNode(responseNode, ExecuteSandboxEnvironmentResponse.class); + } + + public ExecuteSandboxEnvironmentResponse privateExecuteCode( + String name, List inputs, ExecuteCodeAgentEngineSandboxConfig config) { + BuiltRequest builtRequest = buildRequestForPrivateExecuteCode(name, inputs, config); + + try (ApiResponse response = + this.apiClient.request( + "post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())) { + return processResponseForPrivateExecuteCode(response, config); + } + } + + /** A shared buildRequest method for both sync and async methods. */ + BuiltRequest buildRequestForPrivateGet(String name, GetAgentEngineSandboxConfig config) { + + GetAgentEngineSandboxRequestParameters.Builder parameterBuilder = + GetAgentEngineSandboxRequestParameters.builder(); + + if (!Common.isZero(name)) { + parameterBuilder.name(name); + } + if (!Common.isZero(config)) { + parameterBuilder.config(config); + } + JsonNode parameterNode = JsonSerializable.toJsonNode(parameterBuilder.build()); + + ObjectNode body; + String path; + if (this.apiClient.vertexAI()) { + body = getAgentEngineSandboxRequestParametersToVertex(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 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. */ + SandboxEnvironment processResponseForPrivateGet( + ApiResponse response, GetAgentEngineSandboxConfig 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()) { + throw new UnsupportedOperationException( + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini" + + " Developer API mode."); + } + + return JsonSerializable.fromJsonNode(responseNode, SandboxEnvironment.class); + } + + public SandboxEnvironment privateGet(String name, GetAgentEngineSandboxConfig config) { + BuiltRequest builtRequest = buildRequestForPrivateGet(name, config); + + try (ApiResponse response = + this.apiClient.request( + "get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())) { + return processResponseForPrivateGet(response, config); + } + } + + /** A shared buildRequest method for both sync and async methods. */ + BuiltRequest buildRequestForPrivateList(String name, ListAgentEngineSandboxesConfig config) { + + ListAgentEngineSandboxesRequestParameters.Builder parameterBuilder = + ListAgentEngineSandboxesRequestParameters.builder(); + + if (!Common.isZero(name)) { + parameterBuilder.name(name); + } + if (!Common.isZero(config)) { + parameterBuilder.config(config); + } + JsonNode parameterNode = JsonSerializable.toJsonNode(parameterBuilder.build()); + + ObjectNode body; + String path; + if (this.apiClient.vertexAI()) { + body = listAgentEngineSandboxesRequestParametersToVertex(parameterNode, null); + path = Common.formatMap("{name}/sandboxEnvironments", 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 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. */ + ListAgentEngineSandboxesResponse processResponseForPrivateList( + ApiResponse response, ListAgentEngineSandboxesConfig 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()) { + throw new UnsupportedOperationException( + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini" + + " Developer API mode."); + } + + return JsonSerializable.fromJsonNode(responseNode, ListAgentEngineSandboxesResponse.class); + } + + public ListAgentEngineSandboxesResponse privateList( + String name, ListAgentEngineSandboxesConfig config) { + BuiltRequest builtRequest = buildRequestForPrivateList(name, config); + + try (ApiResponse response = + this.apiClient.request( + "get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())) { + return processResponseForPrivateList(response, config); + } + } + + /** A shared buildRequest method for both sync and async methods. */ + BuiltRequest buildRequestForPrivateGetSandboxOperation( + String operationName, GetAgentEngineOperationConfig config) { + + GetAgentEngineSandboxOperationParameters.Builder parameterBuilder = + GetAgentEngineSandboxOperationParameters.builder(); + + if (!Common.isZero(operationName)) { + parameterBuilder.operationName(operationName); + } + if (!Common.isZero(config)) { + parameterBuilder.config(config); + } + JsonNode parameterNode = JsonSerializable.toJsonNode(parameterBuilder.build()); + + ObjectNode body; + String path; + if (this.apiClient.vertexAI()) { + body = getAgentEngineSandboxOperationParametersToVertex(parameterNode, null); + path = Common.formatMap("{operationName}", 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 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. */ + AgentEngineSandboxOperation processResponseForPrivateGetSandboxOperation( + ApiResponse response, GetAgentEngineOperationConfig 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()) { + throw new UnsupportedOperationException( + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini" + + " Developer API mode."); + } + + return JsonSerializable.fromJsonNode(responseNode, AgentEngineSandboxOperation.class); + } + + public AgentEngineSandboxOperation privateGetSandboxOperation( + String operationName, GetAgentEngineOperationConfig config) { + BuiltRequest builtRequest = buildRequestForPrivateGetSandboxOperation(operationName, config); + + try (ApiResponse response = + this.apiClient.request( + "get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())) { + return processResponseForPrivateGetSandboxOperation(response, config); + } + } + + /** + * Creates a sandbox environment. + * + * @param name The resource name of the agent engine. + * @param spec The specification of the sandbox environment. + * @param config The configuration for creating the sandbox. + * @return The operation representing the creation process. + */ + public AgentEngineSandboxOperation create( + String name, SandboxEnvironmentSpec spec, CreateAgentEngineSandboxConfig config) { + return privateCreate(name, spec, config); + } + + /** + * Creates a sandbox environment under a shared default agent engine (created lazily), for callers + * that do not manage an agent engine directly. + * + * @param spec The specification of the sandbox environment. + * @param config The configuration for creating the sandbox. + * @return The operation representing the creation process. + */ + public AgentEngineSandboxOperation create( + SandboxEnvironmentSpec spec, CreateAgentEngineSandboxConfig config) { + return create(AgentEngines.ensureDefaultAgentEngine(apiClient), spec, config); + } + + /** + * Deletes the sandbox environment with the specified name. + * + * @param name The resource name of the sandbox environment. + * @param config The configuration for deleting the sandbox. + * @return The operation representing the deletion process. + */ + public DeleteAgentEngineSandboxOperation delete( + String name, DeleteAgentEngineSandboxConfig config) { + return privateDelete(name, config); + } + + /** + * Executes code in the sandbox environment. + * + * @param name The resource name of the sandbox environment. + * @param inputs The code inputs to execute. + * @param config The configuration for executing the code. + * @return The response from executing the code. + */ + public ExecuteSandboxEnvironmentResponse executeCode( + String name, List inputs, ExecuteCodeAgentEngineSandboxConfig config) { + return privateExecuteCode(name, inputs, config); + } + + /** + * Returns the sandbox environment with the specified name. + * + * @param name The resource name of the sandbox environment. + * @param config The configuration for getting the sandbox. + * @return The sandbox environment. + */ + public SandboxEnvironment get(String name, GetAgentEngineSandboxConfig config) { + return privateGet(name, config); + } + + /** + * Lists the sandbox environments for the given agent engine. + * + * @param name The resource name of the agent engine. + * @param config The configuration for listing the sandboxes. + * @return The list of sandbox environments. + */ + public ListAgentEngineSandboxesResponse list(String name, ListAgentEngineSandboxesConfig config) { + return privateList(name, config); + } + + /** + * Lists the sandbox environments under a shared default agent engine (created lazily), for + * callers that do not manage an agent engine directly. + * + * @param config The configuration for listing the sandboxes. + * @return The list of sandbox environments. + */ + public ListAgentEngineSandboxesResponse list(ListAgentEngineSandboxesConfig config) { + return list(AgentEngines.ensureDefaultAgentEngine(apiClient), config); + } + + /** + * Returns the sandbox operation with the specified name. + * + * @param operationName The name of the sandbox operation. + * @param config The configuration for getting the operation. + * @return The sandbox operation. + */ + public AgentEngineSandboxOperation getSandboxOperation( + String operationName, GetAgentEngineOperationConfig config) { + return privateGetSandboxOperation(operationName, config); + } +} diff --git a/src/main/java/com/google/cloud/agentplatform/SandboxSnapshots.java b/src/main/java/com/google/cloud/agentplatform/SandboxSnapshots.java index 670f408..f77082d 100644 --- a/src/main/java/com/google/cloud/agentplatform/SandboxSnapshots.java +++ b/src/main/java/com/google/cloud/agentplatform/SandboxSnapshots.java @@ -572,4 +572,64 @@ public AgentEngineSandboxSnapshotOperation getSandboxSnapshotOperation( return processResponseForGetSandboxSnapshotOperation(response, config); } } + + /** + * Creates a snapshot of a running sandbox environment. + * + * @param sourceSandboxEnvironmentName The resource name of the source sandbox environment to + * snapshot. + * @param config The configuration for creating the snapshot. + * @return The operation representing the creation process. + */ + public AgentEngineSandboxSnapshotOperation create( + String sourceSandboxEnvironmentName, CreateAgentEngineSandboxSnapshotConfig config) { + return privateCreate(sourceSandboxEnvironmentName, config); + } + + /** + * Returns the sandbox environment snapshot with the specified name. + * + * @param name The resource name of the sandbox environment snapshot. + * @param config The configuration for getting the snapshot. + * @return The sandbox environment snapshot. + */ + public SandboxEnvironmentSnapshot get(String name, GetSandboxEnvironmentSnapshotConfig config) { + return privateGet(name, config); + } + + /** + * Lists the sandbox environment snapshots for the given agent engine. + * + * @param name The resource name of the agent engine. + * @param config The configuration for listing the snapshots. + * @return The list of sandbox environment snapshots. + */ + public ListSandboxEnvironmentSnapshotsResponse list( + String name, ListSandboxEnvironmentSnapshotsConfig config) { + return privateList(name, config); + } + + /** + * Lists the sandbox environment snapshots under a shared default agent engine (created lazily), + * for callers that do not manage an agent engine directly. + * + * @param config The configuration for listing the snapshots. + * @return The list of sandbox environment snapshots. + */ + public ListSandboxEnvironmentSnapshotsResponse list( + ListSandboxEnvironmentSnapshotsConfig config) { + return list(AgentEngines.ensureDefaultAgentEngine(apiClient), config); + } + + /** + * Deletes the sandbox environment snapshot with the specified name. + * + * @param name The resource name of the sandbox environment snapshot. + * @param config The configuration for deleting the snapshot. + * @return The operation representing the deletion process. + */ + public DeleteSandboxEnvironmentSnapshotOperation delete( + String name, DeleteSandboxEnvironmentSnapshotConfig config) { + return privateDelete(name, config); + } } diff --git a/src/main/java/com/google/cloud/agentplatform/SandboxTemplates.java b/src/main/java/com/google/cloud/agentplatform/SandboxTemplates.java index 4f02a6b..1dc4593 100644 --- a/src/main/java/com/google/cloud/agentplatform/SandboxTemplates.java +++ b/src/main/java/com/google/cloud/agentplatform/SandboxTemplates.java @@ -583,4 +583,77 @@ public SandboxEnvironmentTemplateOperation getSandboxEnvironmentTemplateOperatio return processResponseForGetSandboxEnvironmentTemplateOperation(response, config); } } + + /** + * Creates a sandbox environment template. + * + * @param name The resource name of the agent engine. + * @param displayName The display name of the template. + * @param config The configuration for creating the template. + * @return The operation representing the creation process. + */ + public SandboxEnvironmentTemplateOperation create( + String name, String displayName, CreateSandboxEnvironmentTemplateConfig config) { + return privateCreate(name, displayName, config); + } + + /** + * Creates a sandbox environment template under a shared default agent engine (created lazily), + * for callers that do not manage an agent engine directly. + * + * @param displayName The display name of the template. + * @param config The configuration for creating the template. + * @return The operation representing the creation process. + */ + public SandboxEnvironmentTemplateOperation create( + String displayName, CreateSandboxEnvironmentTemplateConfig config) { + return create(AgentEngines.ensureDefaultAgentEngine(apiClient), displayName, config); + } + + /** + * Returns the sandbox environment template with the specified name. + * + * @param name The resource name of the sandbox environment template. + * @param config The configuration for getting the template. + * @return The sandbox environment template. + */ + public SandboxEnvironmentTemplate get(String name, GetSandboxEnvironmentTemplateConfig config) { + return privateGet(name, config); + } + + /** + * Lists the sandbox environment templates for the given agent engine. + * + * @param name The resource name of the agent engine. + * @param config The configuration for listing the templates. + * @return The list of sandbox environment templates. + */ + public ListSandboxEnvironmentTemplatesResponse list( + String name, ListSandboxEnvironmentTemplatesConfig config) { + return privateList(name, config); + } + + /** + * Lists the sandbox environment templates under a shared default agent engine (created lazily), + * for callers that do not manage an agent engine directly. + * + * @param config The configuration for listing the templates. + * @return The list of sandbox environment templates. + */ + public ListSandboxEnvironmentTemplatesResponse list( + ListSandboxEnvironmentTemplatesConfig config) { + return list(AgentEngines.ensureDefaultAgentEngine(apiClient), config); + } + + /** + * Deletes the sandbox environment template with the specified name. + * + * @param name The resource name of the sandbox environment template. + * @param config The configuration for deleting the template. + * @return The operation representing the deletion process. + */ + public DeleteSandboxEnvironmentTemplateOperation delete( + String name, DeleteSandboxEnvironmentTemplateConfig config) { + return privateDelete(name, config); + } } diff --git a/src/main/java/com/google/cloud/agentplatform/Sandboxes.java b/src/main/java/com/google/cloud/agentplatform/Sandboxes.java index d49c0af..c094869 100644 --- a/src/main/java/com/google/cloud/agentplatform/Sandboxes.java +++ b/src/main/java/com/google/cloud/agentplatform/Sandboxes.java @@ -52,6 +52,7 @@ import okhttp3.ResponseBody; public final class Sandboxes { + public final SandboxEnvironments environments; public final SandboxTemplates templates; public final SandboxSnapshots snapshots; @@ -59,6 +60,7 @@ public final class Sandboxes { public Sandboxes(ApiClient apiClient) { this.apiClient = apiClient; + this.environments = new SandboxEnvironments(apiClient); this.templates = new SandboxTemplates(apiClient); this.snapshots = new SandboxSnapshots(apiClient); } @@ -705,4 +707,100 @@ public AgentEngineSandboxOperation privateGetSandboxOperation( return processResponseForPrivateGetSandboxOperation(response, config); } } + + /** + * Creates a sandbox environment. + * + * @param name The resource name of the agent engine. + * @param spec The specification of the sandbox environment. + * @param config The configuration for creating the sandbox. + * @return The operation representing the creation process. + */ + public AgentEngineSandboxOperation create( + String name, SandboxEnvironmentSpec spec, CreateAgentEngineSandboxConfig config) { + return privateCreate(name, spec, config); + } + + /** + * Creates a sandbox environment under a shared default agent engine (created lazily), for callers + * that do not manage an agent engine directly. + * + * @param spec The specification of the sandbox environment. + * @param config The configuration for creating the sandbox. + * @return The operation representing the creation process. + */ + public AgentEngineSandboxOperation create( + SandboxEnvironmentSpec spec, CreateAgentEngineSandboxConfig config) { + return create(AgentEngines.ensureDefaultAgentEngine(apiClient), spec, config); + } + + /** + * Deletes the sandbox environment with the specified name. + * + * @param name The resource name of the sandbox environment. + * @param config The configuration for deleting the sandbox. + * @return The operation representing the deletion process. + */ + public DeleteAgentEngineSandboxOperation delete( + String name, DeleteAgentEngineSandboxConfig config) { + return privateDelete(name, config); + } + + /** + * Executes code in the sandbox environment. + * + * @param name The resource name of the sandbox environment. + * @param inputs The code inputs to execute. + * @param config The configuration for executing the code. + * @return The response from executing the code. + */ + public ExecuteSandboxEnvironmentResponse executeCode( + String name, List inputs, ExecuteCodeAgentEngineSandboxConfig config) { + return privateExecuteCode(name, inputs, config); + } + + /** + * Returns the sandbox environment with the specified name. + * + * @param name The resource name of the sandbox environment. + * @param config The configuration for getting the sandbox. + * @return The sandbox environment. + */ + public SandboxEnvironment get(String name, GetAgentEngineSandboxConfig config) { + return privateGet(name, config); + } + + /** + * Lists the sandbox environments for the given agent engine. + * + * @param name The resource name of the agent engine. + * @param config The configuration for listing the sandboxes. + * @return The list of sandbox environments. + */ + public ListAgentEngineSandboxesResponse list(String name, ListAgentEngineSandboxesConfig config) { + return privateList(name, config); + } + + /** + * Lists the sandbox environments under a shared default agent engine (created lazily), for + * callers that do not manage an agent engine directly. + * + * @param config The configuration for listing the sandboxes. + * @return The list of sandbox environments. + */ + public ListAgentEngineSandboxesResponse list(ListAgentEngineSandboxesConfig config) { + return list(AgentEngines.ensureDefaultAgentEngine(apiClient), config); + } + + /** + * Returns the sandbox operation with the specified name. + * + * @param operationName The name of the sandbox operation. + * @param config The configuration for getting the operation. + * @return The sandbox operation. + */ + public AgentEngineSandboxOperation getSandboxOperation( + String operationName, GetAgentEngineOperationConfig config) { + return privateGetSandboxOperation(operationName, config); + } }