สภาพแวดล้อมคือแซนด์บ็อกซ์ Linux ที่มีการจัดการซึ่งช่วยให้เอเจนต์มีพื้นที่แยกต่างหากสำหรับ เรียกใช้โค้ดและจัดเก็บไฟล์ โดยจะแยกออกจากบริบทของการโต้ตอบ คุณจึงนำสภาพแวดล้อมเดียวกันกลับมาใช้ซ้ำในการโต้ตอบหลายครั้งหรือเริ่มใหม่ได้ทุกเมื่อ
ตัวอย่างต่อไปนี้แสดงวิธีสร้างการโต้ตอบกับสภาพแวดล้อมระยะไกลใหม่ และดึงข้อมูลรหัส
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Install pandas and matplotlib, verify the imports, and print the versions.",
environment="remote",
)
print(f"Environment ID: {interaction.environment_id}")
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Install pandas and matplotlib, verify the imports, and print the versions.",
environment: "remote",
});
console.log(`Environment ID: ${interaction.environment_id}`);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
Client client = new Client();
CreateAgentInteraction params = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Install pandas and matplotlib, verify the imports, and print the versions."))
.environment(CreateAgentInteractionEnvironment.of("remote"))
.build();
Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println("Environment ID: " + interaction.environmentId().orElse(""));
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": "Install pandas and matplotlib, verify the imports, and print the versions.",
"environment": "remote"
}'
พารามิเตอร์ environment
พารามิเตอร์ environment ยอมรับ 3 รูปแบบ ได้แก่
| แบบฟอร์ม | ตัวอย่าง | กรณีที่ควรใช้ |
|---|---|---|
"remote" |
environment="remote" |
จัดสรรแซนด์บ็อกซ์ใหม่ |
| รหัสสภาพแวดล้อม | environment="env_abc123" |
นำแซนด์บ็อกซ์ที่มีอยู่พร้อมไฟล์และแพ็กเกจทั้งหมดกลับมาใช้ซ้ำ |
| ออบเจ็กต์การกำหนดค่า | environment={...} |
จัดสรรแซนด์บ็อกซ์ใหม่ที่มีแหล่งที่มา กฎเครือข่าย ตัวแปรสภาพแวดล้อม หรือการผสมผสาน |
ตัวอย่างต่อไปนี้แสดงวิธีใช้พารามิเตอร์ environment
ทั้ง 3 วิธี
Python
from google import genai
client = genai.Client()
# Fresh sandbox
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Write a hello world script.",
environment="remote",
)
# Reuse an existing sandbox
interaction_2 = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Modify the script to accept a name argument.",
environment=interaction.environment_id,
previous_interaction_id=interaction.id,
)
# New sandbox with sources
interaction_3 = client.interactions.create(
agent="antigravity-preview-09-2026",
input="List all files and summarize the project.",
environment={
"type": "remote",
"sources": [
{
"type": "repository",
"source": "https://github.com/octocat/Spoon-Knife",
"target": "/workspace/spoon-knife",
}
],
},
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
// Fresh sandbox
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Write a hello world script.",
environment: "remote",
});
// Reuse an existing sandbox
const interaction2 = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Modify the script to accept a name argument.",
environment: interaction.environment_id,
previous_interaction_id: interaction.id,
});
// New sandbox with sources
const interaction3 = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "List all files and summarize the project.",
environment: {
type: "remote",
sources: [
{
type: "repository",
source: "https://github.com/octocat/Spoon-Knife",
target: "/workspace/spoon-knife",
},
],
},
});
console.log(interaction.output_text);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Environment;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Source;
import com.google.genai.gaos.models.interactions.SourceType;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.List;
Client client = new Client();
// Fresh sandbox
CreateAgentInteraction params1 = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Write a hello world script."))
.environment(CreateAgentInteractionEnvironment.of("remote"))
.build();
Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params1)).interaction().get();
// Reuse an existing sandbox
CreateAgentInteraction params2 = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Modify the script to accept a name argument."))
.environment(CreateAgentInteractionEnvironment.of(interaction.environmentId().orElse("")))
.previousInteractionId(interaction.id().orElse(""))
.build();
Interaction interaction2 = client.interactions.create(CreateInteractionRequestBody.of(params2)).interaction().get();
// New sandbox with sources
Environment env3 = Environment.builder()
.sources(List.of(
Source.builder()
.type(SourceType.REPOSITORY)
.source("https://github.com/octocat/Spoon-Knife")
.target("/workspace/spoon-knife")
.build()
))
.build();
CreateAgentInteraction params3 = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("List all files and summarize the project."))
.environment(CreateAgentInteractionEnvironment.of(env3))
.build();
Interaction interaction3 = client.interactions.create(CreateInteractionRequestBody.of(params3)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
# Fresh sandbox
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": [{"type": "text", "text": "Write a hello world script."}],
"environment": "remote"
}'
# Reuse an existing sandbox (replace $ENV_ID and $INTERACTION_ID with values from the previous response)
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d "{
\"agent\": \"antigravity-preview-09-2026\",
\"input\": [{\"type\": \"text\", \"text\": \"Modify the script to accept a name argument.\"}],
\"environment\": \"$ENV_ID\",
\"previous_interaction_id\": \"$INTERACTION_ID\"
}"
# New sandbox with sources
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": [{"type": "text", "text": "List all files and summarize the project."}],
"environment": {
"type": "remote",
"sources": [
{
"type": "repository",
"source": "https://github.com/octocat/Spoon-Knife",
"target": "/workspace/spoon-knife"
}
]
}
}'
กำหนดค่าสภาพแวดล้อม
วิธีหนึ่งในการตั้งค่าสภาพแวดล้อมคือการบอก Agent ว่าคุณต้องการติดตั้งอะไร
โดยจะจัดการการแก้ปัญหาและการแก้ปัญหาการขึ้นต่อกัน เมื่อสภาพแวดล้อมพร้อมแล้ว ให้บันทึก environment_id แล้วนำไปใช้ซ้ำ
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Install pandas, matplotlib, and seaborn. Verify all imports work and print the installed versions.",
environment="remote",
)
# Reuse the configured environment
interaction_2 = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Clone https://github.com/octocat/Spoon-Knife into /workspace/tools. Run the test suite and fix any missing dependencies.",
environment=interaction.environment_id,
previous_interaction_id=interaction.id,
)
# Reuse the configured environment
interaction_3 = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Using the tools in /workspace/tools, list the files.",
environment=interaction.environment_id,
previous_interaction_id=interaction_2.id,
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Install pandas, matplotlib, and seaborn. Verify all imports work and print the installed versions.",
environment: "remote",
});
const interaction2 = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Clone https://github.com/octocat/Spoon-Knife into /workspace/tools. Run the test suite and fix any missing dependencies.",
environment: interaction.environment_id,
previous_interaction_id: interaction.id,
});
const interaction3 = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Using the tools in /workspace/tools, list the files.",
environment: interaction.environment_id,
previous_interaction_id: interaction2.id,
});
console.log(interaction.output_text);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
Client client = new Client();
CreateAgentInteraction params1 = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Install pandas, matplotlib, and seaborn. Verify all imports work and print the installed versions."))
.environment(CreateAgentInteractionEnvironment.of("remote"))
.build();
Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params1)).interaction().get();
// Reuse the configured environment
CreateAgentInteraction params2 = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Clone https://github.com/octocat/Spoon-Knife into /workspace/tools. Run the test suite and fix any missing dependencies."))
.environment(CreateAgentInteractionEnvironment.of(interaction.environmentId().orElse("")))
.previousInteractionId(interaction.id().orElse(""))
.build();
Interaction interaction2 = client.interactions.create(CreateInteractionRequestBody.of(params2)).interaction().get();
// Reuse the configured environment
CreateAgentInteraction params3 = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Using the tools in /workspace/tools, list the files."))
.environment(CreateAgentInteractionEnvironment.of(interaction.environmentId().orElse("")))
.previousInteractionId(interaction2.id().orElse(""))
.build();
Interaction interaction3 = client.interactions.create(CreateInteractionRequestBody.of(params3)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
# Create interaction
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": "Install pandas, matplotlib, and seaborn. Verify all imports work and print the installed versions.",
"environment": "remote"
}'
เมานต์จากแหล่งข้อมูล
หากทราบว่าเอเจนต์ต้องการไฟล์ใดบ้าง ให้ติดตั้งไฟล์เหล่านั้นในการเรียกใช้ครั้งเดียว
แทนที่จะทำซ้ำ ออบเจ็กต์การกำหนดค่า environment รับอาร์เรย์ sources
ที่มี 3 ประเภท ดังนี้
| ประเภทแหล่งที่มา | ค่า type |
คำอธิบาย | ขีดจำกัด |
|---|---|---|---|
| ที่เก็บ Git | repository |
โคลนที่เก็บจาก URL ลงในแซนด์บ็อกซ์ที่ target |
500 MB |
| Cloud Storage | gcs |
คัดลอกไฟล์หรือไดเรกทอรีจาก Cloud Storage ไปยังแซนด์บ็อกซ์ที่ target |
2 GB |
| เนื้อหาในบรรทัด | inline |
เขียนเนื้อหาข้อความดิบลงในไฟล์ในแซนด์บ็อกซ์ที่ target |
1 MB ต่อไฟล์ รวม 2 MB |
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="List all files under /workspace and describe what you find.",
environment={
"type": "remote",
"sources": [
{
"type": "repository",
"source": "https://github.com/octocat/Spoon-Knife",
"target": "/workspace/spoon-knife",
},
{
"type": "gcs",
"source": "gs://cloud-samples-data/bigquery/us-states/",
"target": "/workspace/gcs-data",
},
{
"type": "inline",
"content": "# Project Notes\n\n- Analyze state population data\n- Create visualizations\n",
"target": "/workspace/notes/readme.md",
},
],
},
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "List all files under /workspace and describe what you find.",
environment: {
type: "remote",
sources: [
{
type: "repository",
source: "https://github.com/octocat/Spoon-Knife",
target: "/workspace/spoon-knife",
},
{
type: "gcs",
source: "gs://cloud-samples-data/bigquery/us-states/",
target: "/workspace/gcs-data",
},
{
type: "inline",
content: "# Project Notes\n\n- Analyze state population data\n- Create visualizations\n",
target: "/workspace/notes/readme.md",
},
],
},
});
console.log(interaction.output_text);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Environment;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Source;
import com.google.genai.gaos.models.interactions.SourceType;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.List;
Client client = new Client();
Environment env = Environment.builder()
.sources(List.of(
Source.builder()
.type(SourceType.REPOSITORY)
.source("https://github.com/octocat/Spoon-Knife")
.target("/workspace/spoon-knife")
.build(),
Source.builder()
.type(SourceType.GCS)
.source("gs://cloud-samples-data/bigquery/us-states/")
.target("/workspace/gcs-data")
.build(),
Source.builder()
.type(SourceType.INLINE)
.content("# Project Notes\n\n- Analyze state population data\n- Create visualizations\n")
.target("/workspace/notes/readme.md")
.build()
))
.build();
CreateAgentInteraction params = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("List all files under /workspace and describe what you find."))
.environment(CreateAgentInteractionEnvironment.of(env))
.build();
Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
# Create interaction with sources
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": "List all files under /workspace and describe what you find.",
"environment": {
"type": "remote",
"sources": [
{
"type": "repository",
"source": "https://github.com/octocat/Spoon-Knife",
"target": "/workspace/spoon-knife"
},
{
"type": "gcs",
"source": "gs://cloud-samples-data/bigquery/us-states/",
"target": "/workspace/gcs-data"
},
{
"type": "inline",
"content": "# Project Notes\n\n- Analyze state population data\n- Create visualizations\n",
"target": "/workspace/notes/readme.md"
}
]
}
}'
คุณสามารถใช้ทั้ง 2 วิธีร่วมกันได้ โดยติดตั้งแหล่งที่มาที่รู้จักแบบประกาศ จากนั้นทำซ้ำ
ด้วยการโต้ตอบติดตามผลเพื่อติดตั้งแพ็กเกจหรือเรียกใช้สคริปต์การตั้งค่า คุณไม่สามารถ
ตั้งค่ารูท (/) เป็นเป้าหมายเมื่อเพิ่มแหล่งที่มาที่กำหนดเองได้ คุณต้องระบุ
ไดเรกทอรีย่อยเสมอ
ฮุก
นอกจากนี้ คุณยังติดตั้ง.agents/hooks.jsonไฟล์กำหนดค่าและสคริปต์การสกัดกั้นที่กำหนดเองลงในแซนด์บ็อกซ์เพื่อบังคับใช้แนวทางด้านความปลอดภัยหรือเรียกใช้การตรวจสอบอัตโนมัติเมื่อใดก็ตามที่เครื่องมือทำงานได้ด้วย ดูคำจำกัดความของสคีมาและตัวอย่างโค้ดได้ที่Hook
แหล่งข้อมูลส่วนตัว
นอกจากนี้ คุณยังดาวน์โหลดจากที่เก็บ GitHub ส่วนตัวหรือที่เก็บข้อมูล Cloud Storage ส่วนตัวได้ด้วยโดยการตรวจสอบสิทธิ์โดเมนแหล่งที่มาในการกำหนดค่าเครือข่าย
ตัวเลือกหนึ่งคือข้อมูลเข้าสู่ระบบที่จัดเก็บ ซึ่งอ้างอิงตามรหัส เพื่อให้คุณจัดเก็บข้อมูลลับเพียงครั้งเดียวและทุกสภาพแวดล้อมที่ต้องการ แหล่งข้อมูลนั้นจะอ้างอิงข้อมูลลับได้
"network": {
"allowlist": [
{ "domain": "github.com", "credential": "github-production" },
{ "domain": "*" }
]
}
คุณยังตั้งค่าส่วนหัวแบบอินไลน์ด้วย transform ได้ดังตัวอย่างต่อไปนี้
พร็อกซีขาออกจะใช้ทั้ง 2 รูปแบบในลักษณะเดียวกัน และในทั้ง 2 กรณี
ข้อมูลลับจะไม่ไปอยู่ในแซนด์บ็อกซ์
สำหรับที่เก็บ Git ส่วนตัว ให้ใช้การตรวจสอบสิทธิ์ Basic ด้วย
โทเค็นการเข้าถึงส่วนตัวของ GitHub
(PAT)
เข้ารหัสโทเค็นโดยใช้ x-oauth-basic เป็นชื่อผู้ใช้
echo -n "x-oauth-basic:ghp_YourPATHere" | base64
Python
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Run the test for my backend app and fix any issue.",
environment={
"type": "remote",
"sources": [
{
"type": "repository",
"source": "https://github.com/your-org/backend",
"target": "/backend-app"
}
],
"network": {
"allowlist": [
{
"domain": "github.com",
"transform": {
"Authorization": "Basic YOUR_BASE64_TOKEN"
}
},
{
"domain": "*"
}
]
}
}
)
JavaScript
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Run the test for my backend app and fix any issue.",
environment: {
type: "remote",
sources: [
{
type: "repository",
source: "https://github.com/your-org/backend",
target: "/backend-app"
}
],
network: {
allowlist: [
{
domain: "github.com",
transform: {
"Authorization": "Basic YOUR_BASE64_TOKEN"
}
},
{
domain: "*"
}
]
}
},
});
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.Allowlist;
import com.google.genai.gaos.models.interactions.AllowlistEntry;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Environment;
import com.google.genai.gaos.models.interactions.EnvironmentNetworkEgressAllowlist;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Network;
import com.google.genai.gaos.models.interactions.Source;
import com.google.genai.gaos.models.interactions.SourceType;
import com.google.genai.gaos.models.interactions.Transform;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.List;
import java.util.Map;
Client client = new Client();
Environment env = Environment.builder()
.sources(List.of(
Source.builder()
.type(SourceType.REPOSITORY)
.source("https://github.com/your-org/backend")
.target("/backend-app")
.build()
))
.network(Network.of(EnvironmentNetworkEgressAllowlist.of(
Allowlist.builder()
.allowlist(List.of(
AllowlistEntry.builder()
.domain("github.com")
.transform(Transform.of(Map.of(
"Authorization", "Basic YOUR_BASE64_TOKEN"
)))
.build(),
AllowlistEntry.builder()
.domain("*")
.build()
))
.build()
)))
.build();
CreateAgentInteraction params = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Run the test for my backend app and fix any issue."))
.environment(CreateAgentInteractionEnvironment.of(env))
.build();
Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": "Run the test for my backend app and fix any issue.",
"environment": {
"type": "remote",
"sources": [
{
"type": "repository",
"source": "https://github.com/your-org/backend",
"target": "/backend-app"
}
],
"network": {
"allowlist": [
{
"domain": "github.com",
"transform": {
"Authorization": "Basic YOUR_BASE64_TOKEN"
}
},
{
"domain": "*"
}
]
}
}
}'
สำหรับที่เก็บข้อมูล Cloud Storage แบบส่วนตัว ให้ใช้โทเค็นสำหรับผู้ถือ OAuth 2.0 มาตรฐาน
gcloud auth print-access-token
Python
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Analyze the discrepancies across the data in workspace",
environment={
"type": "remote",
"sources": [
{
"type": "gcs",
"source": "gs://my-private-bucket/data",
"target": "/workspace",
}
],
"network": {
"allowlist": [
{
"domain": "*.googleapis.com",
"transform": {
"Authorization": "Bearer YOUR_GCS_TOKEN"
}
},
{
"domain": "*"
}
]
}
},
)
JavaScript
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Analyze the discrepancies across the data in workspace",
environment: {
type: "remote",
sources: [
{
type: "gcs",
source: "gs://my-private-bucket/data",
target: "/workspace",
}
],
network: {
allowlist: [
{
domain: "storage.googleapis.com",
transform: {
"Authorization": "Bearer YOUR_GCS_TOKEN"
}
},
{
domain: "*"
}
]
}
},
});
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.Allowlist;
import com.google.genai.gaos.models.interactions.AllowlistEntry;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Environment;
import com.google.genai.gaos.models.interactions.EnvironmentNetworkEgressAllowlist;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Network;
import com.google.genai.gaos.models.interactions.Source;
import com.google.genai.gaos.models.interactions.SourceType;
import com.google.genai.gaos.models.interactions.Transform;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.List;
import java.util.Map;
Client client = new Client();
Environment env = Environment.builder()
.sources(List.of(
Source.builder()
.type(SourceType.GCS)
.source("gs://my-private-bucket/data")
.target("/workspace")
.build()
))
.network(Network.of(EnvironmentNetworkEgressAllowlist.of(
Allowlist.builder()
.allowlist(List.of(
AllowlistEntry.builder()
.domain("*.googleapis.com")
.transform(Transform.of(Map.of(
"Authorization", "Bearer YOUR_GCS_TOKEN"
)))
.build(),
AllowlistEntry.builder()
.domain("*")
.build()
))
.build()
)))
.build();
CreateAgentInteraction params = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Analyze the discrepancies across the data in workspace"))
.environment(CreateAgentInteractionEnvironment.of(env))
.build();
Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": "Analyze the discrepancies across the data in workspace",
"environment": {
"type": "remote",
"sources": [
{
"type": "gcs",
"source": "gs://my-private-bucket/data",
"target": "/workspace"
}
],
"network": {
"allowlist": [
{
"domain": "storage.googleapis.com",
"transform": {
"Authorization": "Bearer YOUR_GCS_TOKEN"
}
},
{
"domain": "*"
}
]
}
}
}'
ซอฟต์แวร์ที่ติดตั้งไว้ล่วงหน้า
แซนด์บ็อกซ์ทำงานบน Ubuntu และมาพร้อมกับรันไทม์และแพ็กเกจทั่วไปที่
ติดตั้งไว้ล่วงหน้า เอเจนต์สามารถติดตั้งแพ็กเกจเพิ่มเติมได้ขณะรันไทม์โดยใช้ pip
install หรือ npm install แพ็กเกจที่ติดตั้งระหว่างการโต้ตอบจะยังคงอยู่เมื่อ
คุณใช้ environment_id เดียวกันซ้ำ
| หมวดหมู่ | แพ็กเกจที่ติดตั้งไว้ล่วงหน้า |
|---|---|
| เครื่องมือ UNIX | curl, wget, git, rsync, unzip, ripgrep, fd-find, gawk, bc, tree, which, lsof, htop, jq, iproute2, procps, gcloud CLI |
| Python 3.12 | numpy, pandas, requests, google-genai, beautifulsoup4, pyyaml, ast-grep-cli |
| Node.js 22 | create-next-app, create-vite, typescript |
ตัวแปรสภาพแวดล้อม
ใช้ฟิลด์ env เพื่อตั้งค่าตัวแปรสภาพแวดล้อมภายในแซนด์บ็อกซ์ แต่ละรายการ
จะแมปชื่อตัวแปรกับสตริงอักษรตามที่กำหนดค่า หรือ
การอ้างอิงถึงข้อมูลเข้าสู่ระบบที่จัดเก็บไว้สำหรับ
Secret เอเจนต์จะเห็นตัวแปรเหล่านั้นในลักษณะเดียวกับที่เห็นในเชลล์ใดๆ ดังนั้นเครื่องมือและสคริปต์
ที่อ่านจากสภาพแวดล้อมของกระบวนการจึงจะเลือกตัวแปรเหล่านั้นได้โดยไม่ต้องมีการเชื่อมต่อเพิ่มเติม
| ช่อง | ประเภท | คำอธิบาย |
|---|---|---|
env |
object |
แผนที่ของชื่อตัวแปรกับค่า ค่าจะเป็น string ตามตัวอักษรหรือการอ้างอิงข้อมูลเข้าสู่ระบบในรูปแบบ {"credential": "credential-id"} |
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Build the project and run the test suite.",
environment={
"type": "remote",
"env": {
"NODE_ENV": "production",
"LOG_LEVEL": "debug",
"API_TOKEN": {"credential": "my-api-token"},
},
},
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Build the project and run the test suite.",
environment: {
type: "remote",
env: {
NODE_ENV: "production",
LOG_LEVEL: "debug",
API_TOKEN: { credential: "my-api-token" },
},
},
});
console.log(interaction.output_text);
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": [{"type": "text", "text": "Build the project and run the test suite."}],
"environment": {
"type": "remote",
"env": {
"NODE_ENV": "production",
"LOG_LEVEL": "debug",
"API_TOKEN": {"credential": "my-api-token"}
}
}
}'
ตัวแปรจะมีผลกับทุกคำสั่งที่ Agent เรียกใช้ในการโต้ตอบนั้น ซึ่งรวมถึง คำสั่งเชลล์ ขั้นตอนการสร้าง และกระบวนการใดๆ ที่ Agent เริ่มต้น
ค่าทั้ง 2 ประเภทจะทำงานแตกต่างกัน ระบบจะเขียนสตริงตามตัวอักษรลงใน คอนเทนเนอร์เป็นข้อความธรรมดา การอ้างอิงข้อมูลเข้าสู่ระบบไม่ใช่ตัวแปรที่ได้รับตัวยึดตำแหน่ง และพร็อกซีขาออกจะแทนที่ข้อมูลลับจริงเฉพาะในคำขอขาออกไปยังโดเมนที่เชื่อถือของข้อมูลเข้าสู่ระบบนั้น ดูวิธีดำเนินการได้ที่ ใช้ข้อมูลเข้าสู่ระบบเป็นตัวแปรสภาพแวดล้อม
การกำหนดค่าเครือข่าย
โดยค่าเริ่มต้น สภาพแวดล้อมจะมีสิทธิ์เข้าถึงเครือข่ายขาออกแบบไม่จำกัด ใช้ฟิลด์
network เพื่อจำกัดการรับส่งขาออกไปยังโดเมนที่เฉพาะเจาะจง แต่ละกฎ
จะระบุ domain รวมถึง credential ที่ไม่บังคับเพื่อแทรกข้อมูลลับที่จัดเก็บไว้
และออบเจ็กต์ transform ที่ไม่บังคับเพื่อแทรกส่วนหัวลงในคำขอที่ตรงกัน
ส่วนหัวเหล่านี้อาจไม่ซ้ำกันต่อการโต้ตอบ และคุณสามารถอัปเดตส่วนหัวสำหรับสภาพแวดล้อมเดียวกันได้
| ช่อง | ประเภท | คำอธิบาย |
|---|---|---|
domain |
string |
โดเมนที่จะจับคู่ ใช้ชื่อโฮสต์ที่แน่นอนหรือ * สำหรับโดเมนทั้งหมด |
credential |
string |
รหัสของข้อมูลเข้าสู่ระบบที่จัดเก็บไว้ พร็อกซีขาออกจะแก้ไขและแทรกส่วนหัวการตรวจสอบสิทธิ์ในเวลาที่ส่งคำขอ |
transform |
object |
ออบเจ็กต์ที่มีคู่คีย์-ค่าแบบเรียบซึ่งแสดงถึงส่วนหัวที่จะแทรกลงในคำขอที่ตรงกัน เช่น {"Authorization": "Bearer ..."} |
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Fetch the latest issues from the GitHub API for my-org/my-repo.",
environment={
"type": "remote",
"network": {
"allowlist": [
{
"domain": "api.github.com",
"transform": {
"Authorization": "Bearer ghp_your_github_token"
},
},
{"domain": "pypi.org"},
{"domain": "*"},
]
},
},
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Fetch the latest issues from the GitHub API for my-org/my-repo.",
environment: {
type: "remote",
network: {
allowlist: [
{
domain: "api.github.com",
transform: {
"Authorization": "Bearer ghp_your_github_token"
},
},
{ domain: "pypi.org" },
{ domain: "*" },
]
}
},
});
console.log(interaction.output_text);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.Allowlist;
import com.google.genai.gaos.models.interactions.AllowlistEntry;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Environment;
import com.google.genai.gaos.models.interactions.EnvironmentNetworkEgressAllowlist;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Network;
import com.google.genai.gaos.models.interactions.Transform;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.List;
import java.util.Map;
Client client = new Client();
Environment env = Environment.builder()
.network(Network.of(EnvironmentNetworkEgressAllowlist.of(
Allowlist.builder()
.allowlist(List.of(
AllowlistEntry.builder()
.domain("api.github.com")
.transform(Transform.of(Map.of(
"Authorization", "Bearer ghp_your_github_token"
)))
.build(),
AllowlistEntry.builder().domain("pypi.org").build(),
AllowlistEntry.builder().domain("*").build()
))
.build()
)))
.build();
CreateAgentInteraction params = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Fetch the latest issues from the GitHub API for my-org/my-repo."))
.environment(CreateAgentInteractionEnvironment.of(env))
.build();
Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": [{"type": "text", "text": "Fetch the latest issues from the GitHub API for my-org/my-repo."}],
"environment": {
"type": "remote",
"network": {
"allowlist": [
{
"domain": "api.github.com",
"transform": {
"Authorization": "Bearer ghp_your_github_token"
}
},
{"domain": "pypi.org"},
{"domain": "*"}
]
}
}
}'
เมื่อตั้งค่ารายการที่อนุญาตแล้ว จะอนุญาตเฉพาะคำขอไปยังโดเมนที่ระบุไว้อย่างชัดเจนเท่านั้น คุณใช้ไวลด์การ์ดเพื่อจับคู่โดเมนย่อยได้ (เช่น {"domain":
"*.example.com"}) แต่โปรดทราบว่าไวลด์การ์ดนี้จะไม่จับคู่โดเมนราก
example.com ซึ่งต้องเพิ่มแยกต่างหาก หากต้องการอนุญาตการรับส่งอื่นๆ ทั้งหมด เช่น การกำหนดเส้นทางโดเมนที่ไม่ได้อยู่ในรายการโดยไม่มีส่วนหัวที่แทรก ให้เพิ่ม {"domain": "*"} เป็นรายการที่ครอบคลุมทั้งหมด
ข้อมูลเข้าสู่ระบบ
การตรวจสอบสิทธิ์การรับส่งข้อมูลขาออกทำได้ 2 วิธี ได้แก่ ข้อมูลเข้าสู่ระบบที่จัดเก็บซึ่งอ้างอิงตามรหัส และtransformในกฎรายการที่อนุญาต พร็อกซีขาออก
จะใช้ทั้งในสาย ดังนั้นในทั้ง 2 กรณี ความลับจะไม่เข้าสู่
แซนด์บ็อกซ์และจะไม่ปรากฏในเพย์โหลดการโต้ตอบ
ข้อมูลเข้าสู่ระบบที่มีการจัดการคือข้อมูลเข้าสู่ระบบที่ใช้เมื่อคุณต้องการจัดเก็บข้อมูลลับเพียงครั้งเดียวและนำกลับมาใช้ใหม่ สภาพแวดล้อม เอเจนต์ และทริกเกอร์ทุกรายการในโปรเจ็กต์สามารถอ้างอิงรหัสเดียวกันได้ และคุณจะหมุนเวียนรหัส ได้ในที่เดียว
Python
from google import genai
client = genai.Client()
# Store the secret once
client.credentials.create(
id="github-production",
type="bearer_token",
token="ghp_your_github_token",
)
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Fetch the latest issues from the GitHub API for my-org/my-repo.",
environment={
"type": "remote",
"network": {
"allowlist": [
{"domain": "api.github.com", "credential": "github-production"},
{"domain": "*"},
]
},
},
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
// Store the secret once
await client.credentials.create({
id: "github-production",
type: "bearer_token",
token: "ghp_your_github_token",
});
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Fetch the latest issues from the GitHub API for my-org/my-repo.",
environment: {
type: "remote",
network: {
allowlist: [
{ domain: "api.github.com", credential: "github-production" },
{ domain: "*" },
]
}
},
});
console.log(interaction.output_text);
REST
# Store the secret once
curl -X POST "https://generativelanguage.googleapis.com/v1beta/credentials" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"id": "github-production",
"type": "bearer_token",
"token": "ghp_your_github_token"
}'
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": "Fetch the latest issues from the GitHub API for my-org/my-repo.",
"environment": {
"type": "remote",
"network": {
"allowlist": [
{ "domain": "api.github.com", "credential": "github-production" },
{ "domain": "*" }
]
}
}
}'
oauth2 ยังรีเฟรชโทเค็นเพื่อการเข้าถึงด้วยตัวเองด้วย ดังนั้น
การโต้ตอบที่ใช้เวลานานจึงไม่หยุดชะงักเมื่อโทเค็นหมดอายุ ดูรายการประเภทข้อมูลเข้าสู่ระบบและการดำเนินการจัดการทั้งหมดได้ที่
ข้อมูลเข้าสู่ระบบ
นอกจากนี้ คุณยังตั้งค่าส่วนหัวในบรรทัดด้วย transform ได้ด้วย วิธีนี้เหมาะสำหรับกรณีที่ค่า
เป็นของการเรียกครั้งเดียว เช่น โทเค็นที่คุณสร้างก่อนสร้าง
การโต้ตอบ ส่วนหัวที่ตั้งค่าด้วยวิธีนี้จะได้รับการแทรกโดยพร็อกซีขาออกเดียวกัน
และจะไม่แสดงภายในแซนด์บ็อกซ์เป็นตัวแปรสภาพแวดล้อมหรือไฟล์
Python
import subprocess
from google import genai
# Fetch a short-lived access token from your local gcloud CLI
gcloud_token = subprocess.check_output(
["gcloud", "auth", "print-access-token"], text=True
).strip()
client = genai.Client()
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="List the files in gs://my-bucket/reports/ using the GCS JSON API.",
environment={
"type": "remote",
"network": {
"allowlist": [
{
"domain": "storage.googleapis.com",
"transform": {
"Authorization": f"Bearer {gcloud_token}"
},
}
]
},
},
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
import { execSync } from "child_process";
const gcloudToken = execSync("gcloud auth print-access-token").toString().trim();
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "List the files in gs://my-bucket/reports/ using the GCS JSON API.",
environment: {
type: "remote",
network: {
allowlist: [
{
domain: "storage.googleapis.com",
transform: {
"Authorization": `Bearer ${gcloudToken}`
},
}
]
}
},
});
console.log(interaction.output_text);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.Allowlist;
import com.google.genai.gaos.models.interactions.AllowlistEntry;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Environment;
import com.google.genai.gaos.models.interactions.EnvironmentNetworkEgressAllowlist;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Network;
import com.google.genai.gaos.models.interactions.Transform;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
// Fetch a short-lived access token from your local gcloud CLI
Process process = new ProcessBuilder("gcloud", "auth", "print-access-token").start();
String gcloudToken = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8).trim();
Client client = new Client();
Environment env = Environment.builder()
.network(Network.of(EnvironmentNetworkEgressAllowlist.of(
Allowlist.builder()
.allowlist(List.of(
AllowlistEntry.builder()
.domain("storage.googleapis.com")
.transform(Transform.of(Map.of(
"Authorization", "Bearer " + gcloudToken
)))
.build()
))
.build()
)))
.build();
CreateAgentInteraction params = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("List the files in gs://my-bucket/reports/ using the GCS JSON API."))
.environment(CreateAgentInteractionEnvironment.of(env))
.build();
Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": "List the files in gs://my-bucket/reports/ using the GCS JSON API.",
"environment": {
"type": "remote",
"network": {
"allowlist": [
{
"domain": "storage.googleapis.com",
"transform": {
"Authorization": "Bearer <YOUR_GCLOUD_TOKEN>"
}
}
]
}
}
}'
credential และ transform สามารถปรากฏในกฎเดียวกันได้ ระบบจะใช้ข้อมูลเข้าสู่ระบบก่อนและ transform จะผสานที่ด้านบน ดังนั้นส่วนหัว transform ที่ชัดเจนจะชนะหากทั้ง 2 รายการตั้งค่าคีย์เดียวกัน รูปแบบทั่วไปคือข้อมูลเข้าสู่ระบบสำหรับ
ส่วนหัวการตรวจสอบสิทธิ์และ transform สำหรับส่วนหัวเพิ่มเติมที่บริการ
คาดหวังควบคู่กัน
ปิดใช้การเข้าถึงเครือข่าย
หากต้องการบล็อกการเข้าถึงเครือข่ายขาออกทั้งหมด ให้ตั้งค่า network เป็น disabled
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Analyze the local files only.",
environment={
"type": "remote",
"network": "disabled",
},
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Analyze the local files only.",
environment: {
type: "remote",
network: "disabled",
},
});
console.log(interaction.output_text);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Environment;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Network;
import com.google.genai.gaos.models.interactions.NetworkEnum;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
Client client = new Client();
Environment env = Environment.builder()
.network(Network.of(NetworkEnum.DISABLED))
.build();
CreateAgentInteraction params = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Analyze the local files only."))
.environment(CreateAgentInteractionEnvironment.of(env))
.build();
Interaction interaction = client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": "Analyze the local files only.",
"environment": {
"type": "remote",
"network": "disabled"
}
}'
รีเฟรชข้อมูลเข้าสู่ระบบ
โทเค็นแบบอินไลน์ เช่น โทเค็นเพื่อการเข้าถึงและคีย์ API ที่มีอายุสั้นจะหมดอายุ
คุณรีเฟรชได้โดยส่ง environment_id ที่มีอยู่พร้อมกับการกำหนดค่า network ใหม่ในการโต้ตอบครั้งถัดไป กฎเครือข่ายใหม่จะแทนที่กฎก่อนหน้าทั้งหมด ในขณะที่สถานะระบบไฟล์ของสภาพแวดล้อม (แพ็กเกจ ไฟล์ ที่เก็บที่ติดตั้ง) จะยังคงอยู่
หากใช้ข้อมูลเข้าสู่ระบบที่จัดเก็บไว้แทน
คุณก็ไม่จำเป็นต้องทำขั้นตอนนี้ oauth2 จะรีเฟรชตัวเอง และการหมุนเวียนข้อมูลเข้าสู่ระบบใดๆ ก็ตามจะเป็นPATCH ในข้อมูลเข้าสู่ระบบที่ทำให้กฎรายการที่อนุญาตทั้งหมดที่อ้างอิงถึงข้อมูลเข้าสู่ระบบนั้นไม่เปลี่ยนแปลง
Python
from google import genai
client = genai.Client()
# First interaction: use an initial token
first = client.interactions.create(
agent="antigravity-preview-09-2026",
input="List the files in gs://my-bucket/reports/ using the GCS JSON API.",
environment={
"type": "remote",
"network": {
"allowlist": [
{
"domain": "storage.googleapis.com",
"transform": {
"Authorization": "Bearer INITIAL_TOKEN"
},
}
]
},
},
)
# Later: refresh the token on the same environment
result = client.interactions.create(
agent="antigravity-preview-09-2026",
input="Now download the file reports/q1.csv from the same bucket.",
environment={
"type": "remote",
"environment_id": first.environment_id,
"network": {
"allowlist": [
{
"domain": "storage.googleapis.com",
"transform": {
"Authorization": "Bearer REFRESHED_TOKEN"
},
}
]
},
},
)
print(result.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
// First interaction: use an initial token
const first = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "List the files in gs://my-bucket/reports/ using the GCS JSON API.",
environment: {
type: "remote",
network: {
allowlist: [
{
domain: "storage.googleapis.com",
transform: {
"Authorization": "Bearer INITIAL_TOKEN"
},
}
]
}
},
});
// Later: refresh the token on the same environment
const result = await client.interactions.create({
agent: "antigravity-preview-09-2026",
input: "Now download the file reports/q1.csv from the same bucket.",
environment: {
type: "remote",
environment_id: first.environment_id,
network: {
allowlist: [
{
domain: "storage.googleapis.com",
transform: {
"Authorization": "Bearer REFRESHED_TOKEN"
},
}
]
}
},
});
console.log(result.output_text);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.Allowlist;
import com.google.genai.gaos.models.interactions.AllowlistEntry;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.Environment;
import com.google.genai.gaos.models.interactions.EnvironmentNetworkEgressAllowlist;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Network;
import com.google.genai.gaos.models.interactions.Transform;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.List;
import java.util.Map;
Client client = new Client();
// First interaction: use an initial token
Environment initialEnv = Environment.builder()
.network(Network.of(EnvironmentNetworkEgressAllowlist.of(
Allowlist.builder()
.allowlist(List.of(
AllowlistEntry.builder()
.domain("storage.googleapis.com")
.transform(Transform.of(Map.of(
"Authorization", "Bearer INITIAL_TOKEN"
)))
.build()
))
.build()
)))
.build();
CreateAgentInteraction firstParams = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("List the files in gs://my-bucket/reports/ using the GCS JSON API."))
.environment(CreateAgentInteractionEnvironment.of(initialEnv))
.build();
Interaction first = client.interactions.create(CreateInteractionRequestBody.of(firstParams)).interaction().get();
// Later: refresh the token on the same environment
Environment refreshedEnv = Environment.builder()
.environmentId(first.environmentId().orElse(""))
.network(Network.of(EnvironmentNetworkEgressAllowlist.of(
Allowlist.builder()
.allowlist(List.of(
AllowlistEntry.builder()
.domain("storage.googleapis.com")
.transform(Transform.of(Map.of(
"Authorization", "Bearer REFRESHED_TOKEN"
)))
.build()
))
.build()
)))
.build();
CreateAgentInteraction secondParams = CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-09-2026"))
.input(InteractionsInput.of("Now download the file reports/q1.csv from the same bucket."))
.environment(CreateAgentInteractionEnvironment.of(refreshedEnv))
.build();
Interaction result = client.interactions.create(CreateInteractionRequestBody.of(secondParams)).interaction().get();
System.out.println(result.outputText().orElse(""));
REST
# Use the environment_id from a previous interaction
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"agent": "antigravity-preview-09-2026",
"input": "Now download the file reports/q1.csv from the same bucket.",
"environment": {
"type": "remote",
"environment_id": "<ENVIRONMENT_ID_FROM_PREVIOUS_INTERACTION>",
"network": {
"allowlist": [
{
"domain": "storage.googleapis.com",
"transform": {
"Authorization": "Bearer REFRESHED_TOKEN"
}
}
]
}
}
}'
วงจรการใช้งานสภาพแวดล้อม
สภาพแวดล้อมมีวงจรดังนี้
| รัฐ | พฤติกรรม |
|---|---|
| สร้างแล้ว | จัดสรรเมื่อการโต้ตอบระบุ environment: "remote" หรือออบเจ็กต์การกำหนดค่า |
| ใช้งานอยู่ | การเรียกใช้ขณะที่การโต้ตอบอยู่ระหว่างดำเนินการ |
| ไม่มีการใช้งาน | ถ่ายภาพอัตโนมัติและหยุดหลังจากไม่มีการใช้งานเป็นเวลา 15 นาที |
| ออฟไลน์ | เก็บไว้เป็นเวลา 7 วันนับตั้งแต่ใช้งานครั้งล่าสุด สามารถกลับมาทำงานต่อได้โดยส่งรหัส |
| ลบแล้ว | ระบบจะนำออกจากระบบโดยอัตโนมัติหลังจากที่การเก็บรักษา TTL 7 วันหมดอายุหรือเมื่อมีการลบด้วยตนเอง |
Environments API
คุณสามารถใช้ Environments API เพื่อจัดการเซสชันแซนด์บ็อกซ์แบบเป็นโปรแกรมได้ การแจงนับสภาพแวดล้อมช่วยให้คุณค้นพบรหัสเซสชันที่ใช้งานอยู่และกู้คืนสถานะได้ หากการเชื่อมต่อไคลเอ็นต์สิ้นสุดลงในระหว่างงานที่ใช้เวลานาน นอกจากนี้ คุณยัง ตรวจสอบข้อมูลเมตาของเซสชันและลบสภาพแวดล้อมอย่างชัดเจนเมื่อเวิร์กโฟลว์ สิ้นสุดลงได้ด้วย แทนที่จะรอให้ TTL อัตโนมัติหมดอายุ
แสดงรายการสภาพแวดล้อม
แสดงรายการสภาพแวดล้อมที่ใช้งานอยู่ซึ่งเป็นของโปรเจ็กต์ ใช้พารามิเตอร์การแบ่งหน้า เพื่อควบคุมขนาดกลุ่มการตอบกลับ
Python
from google import genai
client = genai.Client()
response = client.environments.list(page_size=10)
for env in response.environments:
print(f"Environment ID: {env.id}, Status: {env.status}")
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const response = await client.environments.list({ page_size: 10 });
for (const env of response.environments) {
console.log(`Environment ID: ${env.id}, Status: ${env.status}`);
}
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.environments.Environment;
import com.google.genai.gaos.models.environments.ListEnvironmentsResponse;
import java.util.List;
Client client = new Client();
ListEnvironmentsResponse response = client.environments.listEnvironments()
.pageSize(10)
.call()
.listEnvironmentsResponse()
.get();
for (Environment env : response.environments().orElse(List.of())) {
System.out.println("Environment ID: " + env.id().orElse("") + ", Status: " + env.status().orElse(null));
}
REST
curl -X GET "https://generativelanguage.googleapis.com/v1beta/environments?pageSize=10" \
-H "x-goog-api-key: $GEMINI_API_KEY"
การตอบกลับจะมีลักษณะคล้ายกับตัวอย่างต่อไปนี้
{
"environments": [
{
"id": "140128b2a13c12c00a5a0d8cf7af9469",
"status": "active"
},
{
"id": "362b738275a1d74af6f1c62bc050da73",
"status": "active"
}
],
"next_page_token": "Cj...5aE="
}
รับสภาพแวดล้อม
ดึงข้อมูลเมตาและรายละเอียดการกำหนดค่าสำหรับสภาพแวดล้อมที่เฉพาะเจาะจงตามชื่อทรัพยากร
Python
from google import genai
client = genai.Client()
env = client.environments.get(id="YOUR_ENVIRONMENT_ID")
print(f"Environment ID: {env.id}, Status: {env.status}")
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const env = await client.environments.get("YOUR_ENVIRONMENT_ID");
console.log(`Environment ID: ${env.id}, Status: ${env.status}`);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.environments.Environment;
Client client = new Client();
Environment env = client.environments.getEnvironment("YOUR_ENVIRONMENT_ID").environment().get();
System.out.println("Environment ID: " + env.id().orElse("") + ", Status: " + env.status().orElse(null));
REST
curl -X GET "https://generativelanguage.googleapis.com/v1beta/environments/YOUR_ENVIRONMENT_ID" \
-H "x-goog-api-key: $GEMINI_API_KEY"
การตอบกลับจะมีลักษณะคล้ายกับตัวอย่างต่อไปนี้
{
"id": "140128b2a13c12c00a5a0d8cf7af9469",
"status": "active",
"sources": [
{
"type": "repository",
"source": "https://github.com/octocat/Spoon-Knife",
"target": "/workspace/spoon-knife"
}
],
"network": {
"allowlist": [
{
"domain": "api.github.com"
},
{
"domain": "github.com"
}
]
}
}
ลบสภาพแวดล้อม
สิ้นสุดและลบสภาพแวดล้อมอย่างชัดเจนเพื่อล้างข้อมูลทรัพยากรแซนด์บ็อกซ์ เมื่องานหรือไปป์ไลน์เสร็จสิ้น
Python
from google import genai
client = genai.Client()
client.environments.delete(id="YOUR_ENVIRONMENT_ID")
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
await client.environments.delete("YOUR_ENVIRONMENT_ID");
Java
import com.google.genai.Client;
Client client = new Client();
client.environments.deleteEnvironment("YOUR_ENVIRONMENT_ID");
REST
curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/environments/YOUR_ENVIRONMENT_ID" \
-H "x-goog-api-key: $GEMINI_API_KEY"
จัดการไฟล์ในสภาพแวดล้อม
Agent จะสร้างและแก้ไขไฟล์ภายในแซนด์บ็อกซ์ในระหว่างการดำเนินการ คุณ สามารถเรียกดูเนื้อหาไดเรกทอรี รับข้อมูลเมตาของไฟล์ ดาวน์โหลดไฟล์แต่ละไฟล์หรือ ทั้งไดเรกทอรีเป็นไฟล์เก็บถาวร tar และอัปโหลดไฟล์หรือแยกไฟล์เก็บถาวร ลงในสภาพแวดล้อมได้โดยตรง พื้นที่เก็บข้อมูลในสภาพแวดล้อมแซนด์บ็อกซ์จะอยู่ภายใต้ โควต้าการใช้งานอย่างยุติธรรม
แสดงรายการไฟล์ในไดเรกทอรี
แสดงรายการเนื้อหาของไดเรกทอรีในสภาพแวดล้อม โดยค่าเริ่มต้นจะแสดงรายการไดเรกทอรีรูท
พารามิเตอร์การค้นหา
| พารามิเตอร์ | ประเภท | คำอธิบาย |
|---|---|---|
recursive |
บูลีน | เมื่อ true จะแสดงรายการไฟล์และไดเรกทอรีทั้งหมดแบบเรียกซ้ำ ค่าเริ่มต้น: false |
Python
from google import genai
client = genai.Client()
# List root directory
response = client.environments.files.list(
environment="YOUR_ENVIRONMENT_ID",
path="",
)
for file in response.files:
print(f"{file.name} ({file.type}) - {file.path}")
# List a subdirectory recursively
response = client.environments.files.list(
environment="YOUR_ENVIRONMENT_ID",
path="src",
recursive=True,
)
for file in response.files:
print(f"{file.name} ({file.type}) - {file.path}")
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
// List root directory
const response = await client.environments.files.list({
environment: "YOUR_ENVIRONMENT_ID",
path: "",
});
for (const file of response.files) {
console.log(`${file.name} (${file.type}) - ${file.path}`);
}
// List a subdirectory recursively
const srcResponse = await client.environments.files.list({
environment: "YOUR_ENVIRONMENT_ID",
path: "src",
recursive: true,
});
for (const file of srcResponse.files) {
console.log(`${file.name} (${file.type}) - ${file.path}`);
}
REST
# List root directory
curl -X GET "https://generativelanguage.googleapis.com/v1beta/environments/$ENV_ID/files" \
-H "x-goog-api-key: $GEMINI_API_KEY"
# List a subdirectory
curl -X GET "https://generativelanguage.googleapis.com/v1beta/environments/$ENV_ID/files/src" \
-H "x-goog-api-key: $GEMINI_API_KEY"
# List all files recursively
curl -X GET "https://generativelanguage.googleapis.com/v1beta/environments/$ENV_ID/files?recursive=true" \
-H "x-goog-api-key: $GEMINI_API_KEY"
การตอบกลับจะแสดงผลอาร์เรย์ files พร้อมข้อมูลเมตาสำหรับแต่ละรายการ ดังนี้
{
"files": [
{
"name": "config",
"path": "config",
"type": "DIRECTORY",
"created": "2026-08-12T07:44:18Z",
"modified": "2026-08-12T07:44:18Z"
},
{
"name": "main.py",
"path": "src/main.py",
"type": "FILE",
"size_bytes": "15",
"mime_type": "text/x-python; charset=utf-8",
"created": "2026-08-12T07:44:20Z",
"modified": "2026-08-12T07:44:20Z"
}
]
}
ฟิลด์รายการไฟล์
| ช่อง | ประเภท | คำอธิบาย |
|---|---|---|
name |
สตริง | ชื่อไฟล์หรือไดเรกทอรี |
path |
สตริง | เส้นทางแบบเต็มที่สัมพันธ์กับรูทของสภาพแวดล้อม |
type |
สตริง | FILE หรือ DIRECTORY ก็ได้ |
size_bytes |
สตริง | ขนาดไฟล์ในหน่วยไบต์ (ไฟล์เท่านั้น) |
mime_type |
สตริง | ประเภท MIME (ไฟล์เท่านั้น) |
created |
สตริง | การประทับเวลาการสร้าง ISO 8601 |
modified |
สตริง | การประทับเวลาการแก้ไขล่าสุดตามมาตรฐาน ISO 8601 |
รับข้อมูลเมตาของไฟล์
รับข้อมูลเมตาสำหรับไฟล์ที่เฉพาะเจาะจงตามเส้นทาง
Python
from google import genai
client = genai.Client()
response = client.environments.files.list(
environment="YOUR_ENVIRONMENT_ID",
path="src/main.py",
)
file = response.files[0]
print(f"Name: {file.name}, Size: {file.size_bytes} bytes, Type: {file.mime_type}")
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const response = await client.environments.files.list({
environment: "YOUR_ENVIRONMENT_ID",
path: "src/main.py",
});
const file = response.files[0];
console.log(`Name: ${file.name}, Size: ${file.size_bytes} bytes, Type: ${file.mime_type}`);
REST
curl -X GET "https://generativelanguage.googleapis.com/v1beta/environments/$ENV_ID/files/src/main.py" \
-H "x-goog-api-key: $GEMINI_API_KEY"
การตอบกลับจะแสดงข้อมูลเมตาของไฟล์ที่อยู่ในอาร์เรย์ files ดังนี้
{
"files": [
{
"name": "main.py",
"path": "src/main.py",
"type": "FILE",
"size_bytes": "15",
"mime_type": "text/x-python; charset=utf-8",
"created": "2026-08-12T07:44:20Z",
"modified": "2026-08-12T07:44:20Z"
}
]
}
หากไม่มีไฟล์ดังกล่าว API จะแสดงข้อผิดพลาด 404
{
"error": {
"message": "Path 'nonexistent.txt' not found in environment 'ENV_ID'.",
"code": "not_found"
}
}
ดาวน์โหลดไฟล์เดียว
ดาวน์โหลดเนื้อหาของไฟล์ที่เฉพาะเจาะจง ใน SDK ให้ใช้เมธอด download()
ในคำขอ REST ให้เพิ่มพารามิเตอร์การค้นหา ?alt=media ลงในเส้นทาง
ไฟล์ เซิร์ฟเวอร์จะตอบกลับด้วย 200 OK และสตรีมเนื้อหาไฟล์ดิบ
Python
from google import genai
client = genai.Client()
content = client.environments.files.download(
environment="YOUR_ENVIRONMENT_ID",
path="src/main.py",
)
with open("main.py", "wb") as f:
f.write(content)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "fs";
const client = new GoogleGenAI({});
const bytes = await client.environments.files.download({
environment: "YOUR_ENVIRONMENT_ID",
path: "src/main.py",
});
fs.writeFileSync("main.py", Buffer.from(bytes));
REST
curl -L -X GET "https://generativelanguage.googleapis.com/v1beta/environments/$ENV_ID/files/src/main.py?alt=media" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-o main.py
ดาวน์โหลดไดเรกทอรีเป็นไฟล์เก็บถาวร tar
ดาวน์โหลดทั้งไดเรกทอรีเป็นที่เก็บถาวร tar โดยขอเส้นทางไดเรกทอรี
ด้วย ?alt=media ซึ่งจะแสดงผลไฟล์ POSIX tar (ไม่ใช่ gzipped) ใช้
recursive=true เพื่อรวมไดเรกทอรีย่อยที่ซ้อนกัน
Python
import tarfile
from google import genai
client = genai.Client()
# Download a subdirectory archive
archive = client.environments.files.download(
environment="YOUR_ENVIRONMENT_ID",
path="src",
)
with open("src.tar", "wb") as f:
f.write(archive)
with tarfile.open("src.tar") as tar:
tar.extractall(path="./extracted")
JavaScript
import { GoogleGenAI } from "@google/genai";
import { execSync } from "child_process";
import * as fs from "fs";
const client = new GoogleGenAI({});
// Download a subdirectory archive
const bytes = await client.environments.files.download({
environment: "YOUR_ENVIRONMENT_ID",
path: "src",
});
fs.writeFileSync("src.tar", Buffer.from(bytes));
execSync("tar -xf src.tar -C ./extracted");
REST
# Download a subdirectory (top-level files only)
curl -L -X GET "https://generativelanguage.googleapis.com/v1beta/environments/$ENV_ID/files/src?alt=media" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-o src.tar
# Download a subdirectory recursively (includes nested directories)
curl -L -X GET "https://generativelanguage.googleapis.com/v1beta/environments/$ENV_ID/files/config?alt=media&recursive=true" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-o config.tar
# Download root directory
curl -L -X GET "https://generativelanguage.googleapis.com/v1beta/environments/$ENV_ID/files?alt=media" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-o snapshot.tar
# Extract the archive
tar xf snapshot.tar -C ./extracted
เมทริกซ์พฤติกรรม
ตารางพฤติกรรมต่อไปนี้สรุปการตอบกลับและพฤติกรรมการเก็บถาวรที่คาดไว้ในปลายทางของไฟล์และไดเรกทอรี, วิธี HTTP และพารามิเตอร์การค้นหา
| ส่งคำขอ | alt |
recursive |
extract |
overwrite |
คำตอบ |
|---|---|---|---|---|---|
GET /files |
(ไม่มี) | (ไม่มี) | - | - | รายการ JSON ของไดเรกทอรีราก |
GET /files/{path} (ไฟล์) |
(ไม่มี) | - | - | - | ข้อมูลเมตา JSON สำหรับไฟล์ |
GET /files/{path} (dir) |
(ไม่มี) | false |
- | - | รายการ JSON ขององค์ประกอบย่อยที่อยู่ติดกัน |
GET /files/{path} (dir) |
(ไม่มี) | true |
- | - | รายการ JSON ขององค์ประกอบสืบทอดทั้งหมด |
GET /files/{path}?alt=media (ไฟล์) |
media |
- | - | - | เนื้อหาไฟล์ดิบ |
GET /files/{path}?alt=media (dir) |
media |
false |
- | - | ที่เก็บถาวร Tar ของไฟล์ที่อยู่ในไดเรกทอรี |
GET /files/{path}?alt=media (dir) |
media |
true |
- | - | ที่เก็บถาวร Tar ของไฟล์ทั้งหมดแบบเรียกซ้ำ |
GET /files?alt=media |
media |
false |
- | - | ไฟล์เก็บถาวร Tar ของไฟล์ระดับรูทเท่านั้น |
PUT /files/{path} (ไฟล์) |
- | - | false |
false |
เขียนไฟล์ที่เส้นทาง แสดงผล 409 Conflict หากมีอยู่แล้ว |
PUT /files/{path}?overwrite=true |
- | - | false |
true |
เขียนหรือเขียนทับไฟล์ในเส้นทาง |
PUT /files/{path}?extract=true |
- | - | true |
false |
แตกไฟล์ที่เก็บถาวรลงในไดเรกทอรีปลายทาง แสดงผล 409 Conflict หากมีไฟล์เป้าหมาย |
PUT /files/{path}?extract=true&overwrite=true |
- | - | true |
true |
คลายแพ็กไฟล์เก็บถาวรและแทนที่ไฟล์ที่มีอยู่ |
อัปโหลดไฟล์ไปยังสภาพแวดล้อม
อัปโหลดไฟล์แต่ละไฟล์หรือที่เก็บถาวรของไดเรกทอรีไปยังสภาพแวดล้อม Sandbox ที่มีอยู่โดยตรงโดยใช้ HTTP PUT ระบบจะสร้างไดเรกทอรีระดับบนโดยอัตโนมัติหากยังไม่มี พื้นที่เก็บข้อมูลในสภาพแวดล้อมจะขึ้นอยู่กับโควต้าการใช้งานอย่างเหมาะสม
อัปโหลดไฟล์เดียว
Python
from google import genai
client = genai.Client()
with open("local_file.txt", "rb") as f:
result = client.environments.files.upload(
environment="YOUR_ENVIRONMENT_ID",
path="workspace/data/file.txt",
file=f,
mime_type="text/plain",
overwrite=True,
)
file = result.files[0]
print(f"Uploaded: {file.name} ({file.size_bytes} bytes)")
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "fs";
const client = new GoogleGenAI({});
const content = fs.readFileSync("local_file.txt");
const result = await client.environments.files.upload({
environment: "YOUR_ENVIRONMENT_ID",
path: "workspace/data/file.txt",
file: content,
mime_type: "text/plain",
overwrite: true,
});
const file = result.files[0];
console.log(`Uploaded: ${file.name} (${file.size_bytes} bytes)`);
REST
curl -X PUT "https://generativelanguage.googleapis.com/upload/v1beta/environments/$ENV_ID/files/workspace/data/file.txt" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: text/plain" \
--data-binary @local_file.txt
การตอบกลับจะแสดงข้อมูลเมตาสำหรับไฟล์ที่อัปโหลด ซึ่งอยู่ในอาร์เรย์ files
เพื่อให้สอดคล้องกับปลายทางของรายการและรับ
{
"files": [
{
"name": "file.txt",
"path": "workspace/data/file.txt",
"type": "FILE",
"size_bytes": "1024",
"mime_type": "text/plain"
}
]
}
อัปโหลดและแตกไฟล์ที่เก็บถาวรของไดเรกทอรี
หากต้องการเริ่มต้นโค้ดเบสหรือโครงสร้างไดเรกทอรีทั้งหมดในคำขอเดียว ให้อัปโหลดไฟล์ .tar หรือ .tar.gz ที่เก็บถาวรด้วย extract=true
Python
from google import genai
client = genai.Client()
with open("source.tar.gz", "rb") as f:
result = client.environments.files.upload(
environment="YOUR_ENVIRONMENT_ID",
path="workspace/src/",
file=f,
extract=True,
)
for entry in result.files:
print(f"Extracted: {entry.path}")
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "fs";
const client = new GoogleGenAI({});
const archive = fs.readFileSync("source.tar.gz");
const result = await client.environments.files.upload({
environment: "YOUR_ENVIRONMENT_ID",
path: "workspace/src/",
file: archive,
extract: true,
});
for (const entry of result.files) {
console.log(`Extracted: ${entry.path}`);
}
REST
curl -X PUT "https://generativelanguage.googleapis.com/upload/v1beta/environments/$ENV_ID/files/workspace/src/?extract=true" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/x-tar" \
--data-binary @source.tar.gz
การตอบกลับจะแสดงรายการไฟล์ทั้งหมดที่เก็บถาวรเขียน
{
"files": [
{
"name": "app.py",
"path": "workspace/src/app.py",
"type": "FILE",
"size_bytes": "15",
"mime_type": "text/x-python"
},
{
"name": "requirements.txt",
"path": "workspace/src/requirements.txt",
"type": "FILE",
"size_bytes": "17",
"mime_type": "text/plain"
}
]
}
อัปโหลดไฟล์ขนาดใหญ่ด้วยเซสชันที่สามารถอัปโหลดต่อได้
สำหรับเพย์โหลดขนาดใหญ่หรือเมื่ออัปโหลดผ่านการเชื่อมต่อที่ไม่น่าเชื่อถือ ให้ใช้ เซสชันที่อัปโหลดต่อได้แทนที่จะส่งทั้งเนื้อหาในคำขอเดียว การอัปโหลดต่อได้ จะแบ่งการโอนเป็นก้อนที่ลองใหม่ได้ทีละก้อน ดังนั้น หากการโอนล้มเหลวกลางคัน คุณก็ไม่ต้องเริ่มใหม่
เริ่มต้นด้วยการเริ่มเซสชันกับ uploadType=resumable ส่งเนื้อหาที่ว่างเปล่า
และใช้ส่วนหัว X-Upload-Content-Type และ X-Upload-Content-Length เพื่อ
ประกาศประเภทสื่อและขนาดทั้งหมดของเพย์โหลดที่คุณต้องการอัปโหลด
PUT /upload/v1beta/environments/$ENV_ID/files/workspace/data/large_dataset.bin?uploadType=resumable HTTP/1.1
Host: generativelanguage.googleapis.com
X-Upload-Content-Type: application/octet-stream
X-Upload-Content-Length: 20971520
Content-Length: 0
x-goog-api-key: $GEMINI_API_KEY
การตอบกลับจะมี URL ของเซสชันในส่วนหัว Location URL นี้มี upload_id อยู่แล้ว จึงไม่จำเป็นต้องใช้คีย์ API อีก
HTTP/1.1 200 OK
Location: https://generativelanguage.googleapis.com/upload/v1beta/environments/$ENV_ID/files/workspace/data/large_dataset.bin?uploadType=resumable&upload_id=AJjja9bfHjiYlGi60pUazCaTuPY
Content-Length: 0
อัปโหลดเพย์โหลดไปยัง URL นั้นเป็นกลุ่ม แต่ละก้อนจะประกาศช่วงไบต์และ
ขนาดทั้งหมดด้วยส่วนหัว Content-Range ดังนี้
PUT /upload/v1beta/environments/$ENV_ID/files/workspace/data/large_dataset.bin?uploadType=resumable&upload_id=AJjja9bfHjiYlGi60pUazCaTuPY HTTP/1.1
Host: generativelanguage.googleapis.com
Content-Type: application/octet-stream
Content-Range: bytes 0-10485759/20971520
Content-Length: 10485760
<10 MB binary payload>
ทุกๆ ก้อนข้อมูลจะส่งคืน 308 Resume Incomplete ยกเว้นก้อนข้อมูลสุดท้าย ส่วนหัว Range
จะบอกจำนวนไบต์ที่เซิร์ฟเวอร์ส่ง ซึ่งเป็นจุดที่คุณจะกลับมาดำเนินการต่อ
หากก้อนข้อมูลล้มเหลว
HTTP/1.1 308 Resume Incomplete
Range: bytes=0-10485759
Content-Length: 0
ส่งก้อนข้อมูลที่เหลือด้วยวิธีเดียวกัน
PUT /upload/v1beta/environments/$ENV_ID/files/workspace/data/large_dataset.bin?uploadType=resumable&upload_id=AJjja9bfHjiYlGi60pUazCaTuPY HTTP/1.1
Host: generativelanguage.googleapis.com
Content-Type: application/octet-stream
Content-Range: bytes 10485760-20971519/20971520
Content-Length: 10485760
<remaining 10 MB binary payload>
ก้อนข้อมูลสุดท้ายจะทำให้การอัปโหลดเสร็จสมบูรณ์และส่งคืนข้อมูลเมตาของไฟล์ใน
filesซองเดียวกับการอัปโหลดแบบครั้งเดียว
{
"files": [
{
"name": "large_dataset.bin",
"path": "workspace/data/large_dataset.bin",
"type": "FILE",
"size_bytes": "20971520",
"mime_type": "application/octet-stream"
}
]
}
เซสชันที่กลับมาทำงานต่อได้จะใช้ได้กับ extract และ overwrite ด้วย ตั้งค่าพารามิเตอร์การค้นหาเหล่านั้นในคำขอเริ่มต้น ไม่ใช่ในแต่ละก้อน
การป้องกันการเขียนทับ
โดยค่าเริ่มต้น overwrite จะเป็น false หากมีเส้นทางปลายทางอยู่แล้ว คำขอจะแสดงข้อผิดพลาด 409 Conflict และจะไม่มีการเขียนข้อมูลใดๆ
{
"error": {
"message": "Requested entity already exists",
"code": "aborted"
}
}
หากต้องการแทนที่ไฟล์หรือไดเรกทอรีที่มีอยู่ ให้ตั้งค่า overwrite=true (หรือ append
?overwrite=true ใน REST) เมื่อใช้ extract=true การตรวจสอบความขัดแย้งจะมีผลกับ
ทุกไฟล์ในที่เก็บ ดังนั้นคำขอจะล้มเหลวหากมีไฟล์เป้าหมาย
ดาวน์โหลดสแนปชอตแบบเต็ม (เลิกใช้งานแล้ว)
วิธีย้ายข้อมูลโค้ดที่มีอยู่ไปยัง API ของไฟล์สภาพแวดล้อม
Python: แทนที่คำขอการดาวน์โหลดไฟล์เดิมด้วยคำขอต่อไปนี้
archive = client.environments.files.download( environment="YOUR_ENVIRONMENT_ID", path="workspace", ) with open("snapshot.tar", "wb") as f: f.write(archive)JavaScript: แทนที่คำขอดาวน์โหลดไฟล์เดิมด้วยคำขอต่อไปนี้
const bytes = await client.environments.files.download({ environment: "YOUR_ENVIRONMENT_ID", path: "workspace", }); fs.writeFileSync("snapshot.tar", Buffer.from(bytes));REST: แทนที่
GET /v1beta/files/environment-$ENV_ID:download?alt=mediaด้วยcurl -L -X GET "https://generativelanguage.googleapis.com/v1beta/environments/$ENV_ID/files?alt=media" \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -o snapshot.tar
ราคาและแหล่งข้อมูล
แต่ละสภาพแวดล้อมจะทำงานโดยมีการจัดสรรทรัพยากรแบบคงที่ ดังนี้
| ทรัพยากร | ค่า |
|---|---|
| CPU | 4 แกน |
| หน่วยความจำ | 16 GB |
ระบบจะไม่เรียกเก็บเงินสำหรับการประมวลผลสภาพแวดล้อม (CPU, หน่วยความจำ, การดำเนินการในแซนด์บ็อกซ์) ในช่วงระยะเวลาแสดงตัวอย่าง ดูราคาสำหรับ ค่าใช้จ่ายของโทเค็นตัวแทน
ข้อจำกัด
- สถานะเวอร์ชันตัวอย่าง: สภาพแวดล้อมและเอเจนต์ที่มีการจัดการอยู่ในเวอร์ชันตัวอย่าง ฟีเจอร์และสคีมาอาจมีการเปลี่ยนแปลง
- ขนาดแหล่งข้อมูลในบรรทัด: แหล่งข้อมูลในบรรทัดจำกัดไว้ที่ 1 MB ต่อไฟล์ และ 2 MB โดยรวมในทุกไฟล์
- ขนาดแหล่งข้อมูล: ที่เก็บ Git มีขนาดได้ไม่เกิน 500 MB และที่เก็บ Cloud Storage มีขนาดได้ไม่เกิน 2 GB
- การเริ่มต้นสภาพแวดล้อม: การจัดสรรสภาพแวดล้อมใหม่จะใช้เวลาไม่เกิน 5 วินาทีโดยประมาณ ที่เก็บแหล่งข้อมูลขนาดใหญ่อาจทำให้เวลาในการดำเนินการนานขึ้น
- การหมดอายุของสภาพแวดล้อม: ระบบจะเก็บรักษาสภาพแวดล้อมออฟไลน์ที่ไม่ได้ใช้งานไว้เป็นเวลา 7 วันก่อนที่จะหมดอายุโดยใช้การล้างข้อมูล TTL อัตโนมัติ การส่งรหัสสภาพแวดล้อมที่หมดอายุหรือ
ไม่ถูกต้องจะแสดงข้อผิดพลาด
404 Not Found - การรองรับไฟล์: ปัจจุบันเอเจนต์อ่านได้เฉพาะไฟล์ข้อความและรูปภาพ ยังไม่พร้อมให้บริการรองรับไฟล์ไบนารี
- ห้ามติดตั้งจากรูท: คุณไม่สามารถตั้งค่ารูท (
/) เป็นเป้าหมายเมื่อเพิ่มแหล่งข้อมูลที่กำหนดเองได้ คุณต้องระบุไดเรกทอรีย่อยเสมอ
ขั้นตอนถัดไป
- ภาพรวมของ Agent: ดูข้อมูลเกี่ยวกับแนวคิดหลักของ Agent ที่มีการจัดการ
- เริ่มต้นใช้งานฉบับย่อ: เริ่มสร้างด้วยการสนทนาไปมาและการสตรีม
- Antigravity Agent: สำรวจความสามารถ เครื่องมือ การเลือกรุ่น และราคาของเอเจนต์เริ่มต้น
- การสร้าง Agent ที่กำหนดเอง: กำหนด Agent ของคุณเองโดยใช้
AGENTS.mdและSKILL.md - Hook: บังคับใช้ขอบเขตการใช้งานด้านความปลอดภัยและเรียกใช้การตรวจสอบผลข้างเคียงภายในแซนด์บ็อกซ์