Repository navigation
Expand file tree
/
Copy pathJavaSamplesTest.java
More file actions
1035 lines (924 loc) · 41.7 KB
/
Copy pathJavaSamplesTest.java
File metadata and controls
1035 lines (924 loc) · 41.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2026 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
*
* http://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.
*/
package com.google.genai.examples;
import com.google.genai.Client;
import com.google.genai.gaos.models.agents.Agent;
import com.google.genai.gaos.models.agents.AgentListResponse;
import com.google.genai.gaos.models.agents.BaseEnvironment;
import com.google.genai.gaos.models.environments.CreateEnvironmentRequest;
import com.google.genai.gaos.models.environments.EnvironmentFile;
import com.google.genai.gaos.models.environments.GetEnvironmentFilesResponse;
import com.google.genai.gaos.models.interactions.AgentOption;
import com.google.genai.gaos.models.interactions.CodeExecution;
import com.google.genai.gaos.models.interactions.ComputerUse;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateAgentInteraction;
import com.google.genai.gaos.models.interactions.CreateAgentInteractionEnvironment;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.FileSearch;
import com.google.genai.gaos.models.interactions.Function;
import com.google.genai.gaos.models.interactions.GoogleMaps;
import com.google.genai.gaos.models.interactions.GoogleSearch;
import com.google.genai.gaos.models.interactions.ImageContent;
import com.google.genai.gaos.models.interactions.ImageContentMimeType;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionStatus;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.MCPServer;
import com.google.genai.gaos.models.interactions.ModelOutputStep;
import com.google.genai.gaos.models.interactions.Source;
import com.google.genai.gaos.models.interactions.SourceType;
import com.google.genai.gaos.models.interactions.Step;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.interactions.URLContext;
import com.google.genai.gaos.models.interactions.UserInputStep;
import com.google.genai.gaos.models.operations.CancelInteractionByIdResponse;
import com.google.genai.gaos.models.operations.CreateAgentResponse;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import com.google.genai.gaos.models.operations.CreateInteractionResponse;
import com.google.genai.gaos.models.operations.CreateTriggerResponse;
import com.google.genai.gaos.models.operations.GetAgentResponse;
import com.google.genai.gaos.models.operations.GetEnvironmentFilesRequest;
import com.google.genai.gaos.models.operations.GetInteractionByIdRequest;
import com.google.genai.gaos.models.operations.GetInteractionByIdResponse;
import com.google.genai.gaos.models.operations.ListAgentsResponse;
import com.google.genai.gaos.models.operations.ListEnvironmentsResponse;
import com.google.genai.gaos.models.triggers.Trigger;
import com.google.genai.gaos.models.triggers.TriggerCreateParams;
import com.google.genai.types.CreateFileSearchStoreConfig;
import com.google.genai.types.FileSearchStore;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class JavaSamplesTest {
@Test
public void testApiVersionAgentsGetList() throws Exception {
// [START paths['/{api_version}/agents'].get:list]
Client client = new Client();
ListAgentsResponse response = client.agents.list().call();
List<Agent> agents =
response.agentListResponse().flatMap(AgentListResponse::agents).orElse(List.of());
for (Agent agent : agents) {
agent.id().ifPresent(System.out::println);
}
// [END paths['/{api_version}/agents'].get:list]
}
@Test
public void testApiVersionAgentsPostCreate() throws Exception {
// [START paths['/{api_version}/agents'].post:create]
Client client = new Client();
String agentId = "research-assistant-" + UUID.randomUUID().toString().substring(0, 8);
Agent agent =
Agent.builder()
.id(agentId)
.baseAgent("antigravity-preview-05-2026")
.description("A helpful research assistant.")
.systemInstruction("You are a helpful research assistant.")
.baseEnvironment(BaseEnvironment.of("remote"))
.tools(List.of(new GoogleSearch()))
.build();
CreateAgentResponse response = client.agents.create(agent);
System.out.println(response.agent().flatMap(Agent::id).orElse(""));
// [cleanup]
client.agents.delete(agentId);
// [/cleanup]
// [END paths['/{api_version}/agents'].post:create]
}
@Test
public void testApiVersionAgentsPostWithSources() throws Exception {
// [START paths['/{api_version}/agents'].post:with_sources]
Client client = new Client();
String agentId = "data-analyst-" + UUID.randomUUID().toString().substring(0, 8);
com.google.genai.gaos.models.interactions.Environment env =
com.google.genai.gaos.models.interactions.Environment.builder()
.sources(
List.of(
Source.builder()
.type(SourceType.INLINE)
.target(".agents/AGENTS.md")
.content(
"Always use matplotlib for charts. Include a summary table in every"
+ " report.")
.build(),
Source.builder()
.type(SourceType.REPOSITORY)
.source("https://github.com/my-org/analysis-templates")
.target("/workspace/templates")
.build()))
.build();
Agent agent =
Agent.builder()
.id(agentId)
.baseAgent("antigravity-preview-05-2026")
.systemInstruction(
"You are a data analyst. Always include visualizations and export results as PDF.")
.baseEnvironment(BaseEnvironment.of(env))
.build();
CreateAgentResponse response = client.agents.create(agent);
System.out.println("Created agent: " + response.agent().flatMap(Agent::id).orElse(""));
// [cleanup]
client.agents.delete(agentId);
// [/cleanup]
// [END paths['/{api_version}/agents'].post:with_sources]
}
@Test
public void testApiVersionAgentsPostForkFromEnv() throws Exception {
// [START paths['/{api_version}/agents'].post:fork_from_env]
Client client = new Client();
// Step 1: Set up the environment interactively.
CreateAgentInteraction params =
CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-05-2026"))
.input(
InteractionsInput.of(
"Write a basic Hello World template to /workspace/template.py."))
.environment(CreateAgentInteractionEnvironment.of("remote"))
.build();
CreateInteractionResponse interactionResponse =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
interactionResponse
.interaction()
.orElseThrow(() -> new RuntimeException("No interaction returned"));
// Step 2: Fork that environment into a named agent.
String agentId = "my-data-analyst-" + UUID.randomUUID().toString().substring(0, 8);
Agent agent =
Agent.builder()
.id(agentId)
.baseAgent("antigravity-preview-05-2026")
.systemInstruction(
"You are a data analyst. Use the template at /workspace/template.py for all"
+ " reports.")
.baseEnvironment(BaseEnvironment.of(interaction.environmentId().orElse("")))
.build();
CreateAgentResponse agentResponse = client.agents.create(agent);
System.out.println("Forked agent: " + agentResponse.agent().flatMap(Agent::id).orElse(""));
// [cleanup]
client.agents.delete(agentId);
// [/cleanup]
// [END paths['/{api_version}/agents'].post:fork_from_env]
}
@Test
public void testApiVersionAgentsIdDeleteDelete() throws Exception {
// [START paths['/{api_version}/agents/{id}'].delete:delete]
Client client = new Client();
// [setup]
String agentId = "delete-test-" + UUID.randomUUID().toString().substring(0, 8);
client.agents.create(
Agent.builder()
.id(agentId)
.baseAgent("antigravity-preview-05-2026")
.description("Temporary agent for deletion.")
.baseEnvironment(BaseEnvironment.of("remote"))
.build());
// [/setup]
client.agents.delete(agentId);
System.out.println("Agent deleted successfully.");
// [END paths['/{api_version}/agents/{id}'].delete:delete]
}
@Test
public void testApiVersionAgentsIdGetGet() throws Exception {
// [START paths['/{api_version}/agents/{id}'].get:get]
Client client = new Client();
// [setup]
String agentId = "test-agent-" + UUID.randomUUID().toString().substring(0, 8);
client.agents.create(
Agent.builder()
.id(agentId)
.baseAgent("antigravity-preview-05-2026")
.description("A test agent.")
.baseEnvironment(BaseEnvironment.of("remote"))
.build());
// [/setup]
GetAgentResponse response = client.agents.get(agentId);
Agent agent = response.agent().orElseThrow(() -> new RuntimeException("No agent returned"));
System.out.println(agent.id().orElse(""));
// [cleanup]
client.agents.delete(agentId);
// [/cleanup]
// [END paths['/{api_version}/agents/{id}'].get:get]
}
@Test
public void testApiVersionEnvironmentsGetList() throws Exception {
// [START paths['/{api_version}/environments'].get:list]
Client client = new Client();
ListEnvironmentsResponse response = client.environments.listEnvironmentsDirect();
for (com.google.genai.gaos.models.environments.Environment env :
response.listEnvironmentsResponse().flatMap(res -> res.environments()).orElse(List.of())) {
System.out.println(env.id().orElse(""));
}
// [END paths['/{api_version}/environments'].get:list]
}
@Test
public void testApiVersionEnvironmentsPostCreate() throws Exception {
// [START paths['/{api_version}/environments'].post:create]
Client client = new Client();
CreateEnvironmentRequest request =
CreateEnvironmentRequest.builder()
.sources(
List.of(
Source.builder()
.type(SourceType.INLINE)
.target("main.py")
.content("print('Hello, World!')")
.build()))
.build();
com.google.genai.gaos.models.environments.Environment environment =
client.environments.createEnvironment(request).environment().orElseThrow();
System.out.println(environment.id().orElse(""));
// [cleanup]
environment.id().ifPresent(id -> client.environments.deleteEnvironment(id));
// [/cleanup]
// [END paths['/{api_version}/environments'].post:create]
}
@Test
public void testApiVersionEnvironmentsPostCopy() throws Exception {
// [START paths['/{api_version}/environments'].post:copy]
Client client = new Client();
// [setup]
CreateEnvironmentRequest setupReq =
CreateEnvironmentRequest.builder()
.sources(
List.of(
Source.builder()
.type(SourceType.INLINE)
.target("main.py")
.content("print('Hello')")
.build()))
.build();
com.google.genai.gaos.models.environments.Environment sourceEnv =
client.environments.createEnvironment(setupReq).environment().orElseThrow();
String sourceEnvId = sourceEnv.id().orElseThrow();
// [/setup]
CreateEnvironmentRequest request =
CreateEnvironmentRequest.builder().fromEnvironment(sourceEnvId).build();
com.google.genai.gaos.models.environments.Environment environment =
client.environments.createEnvironment(request).environment().orElseThrow();
System.out.println(environment.id().orElse(""));
// [cleanup]
environment.id().ifPresent(id -> client.environments.deleteEnvironment(id));
client.environments.deleteEnvironment(sourceEnvId);
// [/cleanup]
// [END paths['/{api_version}/environments'].post:copy]
}
@Test
public void testApiVersionEnvironmentsEnvironmentFilesPathGetListFiles() throws Exception {
// [START paths['/{api_version}/environments/{environment}/files/{path}'].get:list_files]
Client client = new Client();
// [setup]
CreateEnvironmentRequest createReq =
CreateEnvironmentRequest.builder()
.sources(
List.of(
Source.builder()
.type(SourceType.INLINE)
.target("src/main.py")
.content("print('Hello')")
.build()))
.build();
com.google.genai.gaos.models.environments.Environment created =
client.environments.createEnvironment(createReq).environment().orElseThrow();
String envId = created.id().orElseThrow();
// [/setup]
com.google.genai.gaos.models.operations.GetEnvironmentFilesResponse response =
client
.environments
.files()
.list(GetEnvironmentFilesRequest.builder().environment(envId).path("src").build());
GetEnvironmentFilesResponse filesResponse =
response.getEnvironmentFilesResponse().orElseThrow();
for (EnvironmentFile file : filesResponse.files().orElse(List.of())) {
System.out.println(
file.name().orElse("")
+ " "
+ file.type().map(Object::toString).orElse("")
+ " "
+ file.sizeBytes().orElse(""));
}
// [cleanup]
client.environments.deleteEnvironment(envId);
// [/cleanup]
// [END paths['/{api_version}/environments/{environment}/files/{path}'].get:list_files]
}
@Test
public void testApiVersionEnvironmentsEnvironmentFilesPathGetGetFile() throws Exception {
// [START paths['/{api_version}/environments/{environment}/files/{path}'].get:get_file]
Client client = new Client();
// [setup]
CreateEnvironmentRequest createReq =
CreateEnvironmentRequest.builder()
.sources(
List.of(
Source.builder()
.type(SourceType.INLINE)
.target("main.py")
.content("print('Hello')")
.build()))
.build();
com.google.genai.gaos.models.environments.Environment created =
client.environments.createEnvironment(createReq).environment().orElseThrow();
String envId = created.id().orElseThrow();
// [/setup]
com.google.genai.gaos.models.operations.GetEnvironmentFilesResponse response =
client
.environments
.files()
.list(GetEnvironmentFilesRequest.builder().environment(envId).path("main.py").build());
GetEnvironmentFilesResponse filesResponse =
response.getEnvironmentFilesResponse().orElseThrow();
for (EnvironmentFile file : filesResponse.files().orElse(List.of())) {
System.out.println(file.name().orElse("") + " " + file.sizeBytes().orElse(""));
}
// [cleanup]
client.environments.deleteEnvironment(envId);
// [/cleanup]
// [END paths['/{api_version}/environments/{environment}/files/{path}'].get:get_file]
}
@Test
public void testApiVersionEnvironmentsIdDeleteDelete() throws Exception {
// [START paths['/{api_version}/environments/{id}'].delete:delete]
Client client = new Client();
// [setup]
CreateEnvironmentRequest request =
CreateEnvironmentRequest.builder()
.sources(
List.of(
Source.builder()
.type(SourceType.INLINE)
.target("main.py")
.content("print('Hello')")
.build()))
.build();
com.google.genai.gaos.models.environments.Environment created =
client.environments.createEnvironment(request).environment().orElseThrow();
String envId = created.id().orElseThrow();
// [/setup]
client.environments.deleteEnvironment(envId);
System.out.println(
"com.google.genai.gaos.models.environments.Environment deleted successfully.");
// [END paths['/{api_version}/environments/{id}'].delete:delete]
}
@Test
public void testApiVersionEnvironmentsIdGetGet() throws Exception {
// [START paths['/{api_version}/environments/{id}'].get:get]
Client client = new Client();
// [setup]
CreateEnvironmentRequest request =
CreateEnvironmentRequest.builder()
.sources(
List.of(
Source.builder()
.type(SourceType.INLINE)
.target("main.py")
.content("print('Hello')")
.build()))
.build();
com.google.genai.gaos.models.environments.Environment created =
client.environments.createEnvironment(request).environment().orElseThrow();
String envId = created.id().orElseThrow();
// [/setup]
com.google.genai.gaos.models.environments.Environment environment =
client.environments.getEnvironment(envId).environment().orElseThrow();
System.out.println(environment.id().orElse(""));
// [cleanup]
client.environments.deleteEnvironment(envId);
// [/cleanup]
// [END paths['/{api_version}/environments/{id}'].get:get]
}
@Test
public void testApiVersionInteractionsPostSimple() throws Exception {
// [START paths['/{api_version}/interactions'].post:simple]
Client client = new Client();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.input(InteractionsInput.of("Hello, how are you?"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END paths['/{api_version}/interactions'].post:simple]
}
@Test
public void testApiVersionInteractionsPostMultiTurn() throws Exception {
// [START paths['/{api_version}/interactions'].post:multi_turn]
Client client = new Client();
List<Step> conversation =
List.of(
UserInputStep.builder()
.content(List.of(TextContent.builder().text("Hello!").build()))
.build(),
ModelOutputStep.builder()
.content(
List.of(
TextContent.builder().text("Hi there! How can I help you today?").build()))
.build(),
UserInputStep.builder()
.content(
List.of(TextContent.builder().text("What is the capital of France?").build()))
.build());
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.input(InteractionsInput.ofStep(conversation))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END paths['/{api_version}/interactions'].post:multi_turn]
}
@Test
public void testApiVersionInteractionsPostMultimodalImage() throws Exception {
// [START paths['/{api_version}/interactions'].post:multimodal_image]
Client client = new Client();
List<Content> content =
List.of(
TextContent.builder().text("What is in this picture?").build(),
ImageContent.builder()
.data("BASE64_ENCODED_IMAGE")
.mimeType(ImageContentMimeType.IMAGE_PNG)
.build());
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.input(InteractionsInput.ofContent(content))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END paths['/{api_version}/interactions'].post:multimodal_image]
}
@Test
public void testApiVersionInteractionsPostFunctionCalling() throws Exception {
// [START paths['/{api_version}/interactions'].post:function_calling]
Client client = new Client();
Map<String, Object> parameters =
Map.of(
"type", "object",
"properties",
Map.of(
"location",
Map.of(
"type", "string",
"description", "The city and state, e.g. San Francisco, CA")),
"required", List.of("location"));
Function functionTool =
Function.builder()
.name("get_weather")
.description("Get the current weather in a given location")
.parameters(parameters)
.build();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.tools(List.of(functionTool))
.input(InteractionsInput.of("What is the weather like in Boston, MA?"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
List<Step> steps = interaction.steps().orElse(List.of());
if (!steps.isEmpty()) {
System.out.println(steps.get(steps.size() - 1));
}
// [END paths['/{api_version}/interactions'].post:function_calling]
}
@Test
public void testApiVersionInteractionsPostDeepResearch() throws Exception {
// [START paths['/{api_version}/interactions'].post:deep_research]
Client client = new Client();
CreateAgentInteraction params =
CreateAgentInteraction.builder()
.agent(AgentOption.of("deep-research-pro-preview-12-2025"))
.input(InteractionsInput.of("find a cure to cancer"))
.background(true)
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.status().map(InteractionStatus::value).orElse(""));
// [END paths['/{api_version}/interactions'].post:deep_research]
}
@Test
public void testApiVersionInteractionsPostAntigravity() throws Exception {
// [START paths['/{api_version}/interactions'].post:antigravity]
Client client = new Client();
CreateAgentInteraction params =
CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-05-2026"))
.input(
InteractionsInput.of(
"Read Hacker News, summarize the top 5 stories, and save results as a markdown"
+ " file."))
.environment(CreateAgentInteractionEnvironment.of("remote"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END paths['/{api_version}/interactions'].post:antigravity]
}
@Test
public void testApiVersionInteractionsPostReuseEnv() throws Exception {
// [START paths['/{api_version}/interactions'].post:reuse_env]
Client client = new Client();
// Step 1: Create an interaction with a fresh remote environment.
CreateAgentInteraction params1 =
CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-05-2026"))
.input(InteractionsInput.of("Write a hello world script at /workspace/hello.py."))
.environment(CreateAgentInteractionEnvironment.of("remote"))
.build();
CreateInteractionResponse response1 =
client.interactions.create(CreateInteractionRequestBody.of(params1));
Interaction interaction1 =
response1.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println("Environment ID: " + interaction1.environmentId().orElse(""));
// Step 2: Reuse the same environment in a follow-up interaction.
CreateAgentInteraction params2 =
CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-05-2026"))
.input(
InteractionsInput.of(
"Modify the script to accept a name argument and greet the user."))
.environment(
CreateAgentInteractionEnvironment.of(interaction1.environmentId().orElse("")))
.previousInteractionId(interaction1.id().orElse(null))
.build();
CreateInteractionResponse response2 =
client.interactions.create(CreateInteractionRequestBody.of(params2));
Interaction interaction2 =
response2.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction2.outputText().orElse(""));
// [END paths['/{api_version}/interactions'].post:reuse_env]
}
@Test
public void testApiVersionInteractionsPostWithSources() throws Exception {
// [START paths['/{api_version}/interactions'].post:with_sources]
Client client = new Client();
com.google.genai.gaos.models.interactions.Environment env =
com.google.genai.gaos.models.interactions.Environment.builder()
.sources(
List.of(
Source.builder()
.type(SourceType.REPOSITORY)
.source("https://github.com/octocat/Spoon-Knife")
.target("/workspace/repo")
.build(),
Source.builder()
.type(SourceType.INLINE)
.content("Focus on Python files only.")
.target("/workspace/notes.txt")
.build()))
.build();
CreateAgentInteraction params =
CreateAgentInteraction.builder()
.agent(AgentOption.of("antigravity-preview-05-2026"))
.input(
InteractionsInput.of(
"List all files under /workspace and summarize what you find."))
.environment(CreateAgentInteractionEnvironment.of(env))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END paths['/{api_version}/interactions'].post:with_sources]
}
@Test
public void testApiVersionInteractionsPostCustomAgent() throws Exception {
// [START paths['/{api_version}/interactions'].post:custom_agent]
Client client = new Client();
// Step 1: Create a custom agent.
String agentId = "code-reviewer-" + UUID.randomUUID().toString().substring(0, 8);
com.google.genai.gaos.models.interactions.Environment baseEnv =
com.google.genai.gaos.models.interactions.Environment.builder()
.sources(
List.of(
Source.builder()
.type(SourceType.REPOSITORY)
.source("https://github.com/octocat/Spoon-Knife")
.target("/workspace/repo")
.build()))
.build();
Agent customAgent =
Agent.builder()
.id(agentId)
.baseAgent("antigravity-preview-05-2026")
.systemInstruction(
"You are a senior code reviewer. Check every file for bugs, style issues, and"
+ " security vulnerabilities.")
.baseEnvironment(BaseEnvironment.of(baseEnv))
.build();
client.agents.create(customAgent);
// Step 2: Use the custom agent.
CreateAgentInteraction params =
CreateAgentInteraction.builder()
.agent(AgentOption.of(agentId))
.input(
InteractionsInput.of(
"Review the latest changes in /workspace/repo/src and file a summary."))
.environment(CreateAgentInteractionEnvironment.of("remote"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [cleanup]
client.agents.delete(agentId);
// [/cleanup]
// [END paths['/{api_version}/interactions'].post:custom_agent]
}
@Test
public void testApiVersionInteractionsIdDeleteDelete() throws Exception {
// [START paths['/{api_version}/interactions/{id}'].delete:delete]
Client client = new Client();
// [setup]
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.input(InteractionsInput.of("Hello"))
.build();
CreateInteractionResponse created =
client.interactions.create(CreateInteractionRequestBody.of(params));
String interactionId = created.interaction().flatMap(Interaction::id).orElseThrow();
// [/setup]
client.interactions.delete(interactionId);
System.out.println("Interaction deleted successfully.");
// [END paths['/{api_version}/interactions/{id}'].delete:delete]
}
@Test
public void testApiVersionInteractionsIdGetGet() throws Exception {
// [START paths['/{api_version}/interactions/{id}'].get:get]
Client client = new Client();
// [setup]
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.input(InteractionsInput.of("Say hello."))
.build();
CreateInteractionResponse created =
client.interactions.create(CreateInteractionRequestBody.of(params));
String interactionId = created.interaction().flatMap(Interaction::id).orElseThrow();
// [/setup]
GetInteractionByIdResponse getResponse =
client.interactions.get(new GetInteractionByIdRequest(interactionId));
Interaction interaction =
getResponse
.interaction()
.orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.status().map(InteractionStatus::value).orElse(""));
// [END paths['/{api_version}/interactions/{id}'].get:get]
}
@Test
public void testApiVersionInteractionsIdCancelPostCancel() throws Exception {
// [START paths['/{api_version}/interactions/{id}/cancel'].post:cancel]
Client client = new Client();
// Start a background interaction so it stays in-progress.
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.input(InteractionsInput.of("Write a long essay about the history of computing."))
.tools(List.of(new ComputerUse()))
.background(true)
.build();
CreateInteractionResponse created =
client.interactions.create(CreateInteractionRequestBody.of(params));
String interactionId = created.interaction().flatMap(Interaction::id).orElseThrow();
// Cancel the in-progress interaction.
CancelInteractionByIdResponse cancelResponse = client.interactions.cancel(interactionId);
Interaction interaction =
cancelResponse
.interaction()
.orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.status().map(InteractionStatus::value).orElse(""));
// [END paths['/{api_version}/interactions/{id}/cancel'].post:cancel]
}
@Test
public void testSchemaCodeexecutionCodeExecution() throws Exception {
// [START components.schemas.CodeExecution:code_execution]
Client client = new Client();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.tools(List.of(new CodeExecution()))
.input(InteractionsInput.of("Calculate the first 10 Fibonacci numbers"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END components.schemas.CodeExecution:code_execution]
}
@Test
public void testSchemaComputeruseComputerUse() throws Exception {
// [START components.schemas.ComputerUse:computer_use]
Client client = new Client();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-2.5-computer-use-preview-10-2025")
.tools(List.of(new ComputerUse()))
.input(InteractionsInput.of("Find a flight to Tokyo"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END components.schemas.ComputerUse:computer_use]
}
@Test
public void testSchemaFilesearchFileSearch() throws Exception {
// [START components.schemas.FileSearch:file_search]
Client client = new Client();
// Create a file search store so we have a valid one to use.
FileSearchStore store =
client.fileSearchStores.create(CreateFileSearchStoreConfig.builder().build());
String storeName = store.name().orElseThrow();
FileSearch tool = FileSearch.builder().fileSearchStoreNames(List.of(storeName)).build();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.tools(List.of(tool))
.input(InteractionsInput.of("What documents are available?"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [cleanup]
client.fileSearchStores.delete(storeName, null);
// [/cleanup]
// [END components.schemas.FileSearch:file_search]
}
@Test
public void testSchemaFunctionFunctionCalling() throws Exception {
// [START components.schemas.Function:function_calling]
Client client = new Client();
Map<String, Object> parameters =
Map.of(
"type", "object",
"properties",
Map.of(
"location",
Map.of(
"type", "string",
"description", "The city and state, e.g. San Francisco, CA")),
"required", List.of("location"));
Function functionTool =
Function.builder()
.name("get_weather")
.description("Get the current weather in a given location")
.parameters(parameters)
.build();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.tools(List.of(functionTool))
.input(InteractionsInput.of("What is the weather like in Boston?"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
List<Step> steps = interaction.steps().orElse(List.of());
if (!steps.isEmpty()) {
System.out.println(steps.get(steps.size() - 1));
}
// [END components.schemas.Function:function_calling]
}
@Test
public void testSchemaGooglemapsGoogleMaps() throws Exception {
// [START components.schemas.GoogleMaps:google_maps]
Client client = new Client();
GoogleMaps tool = GoogleMaps.builder().latitude(37.7749).longitude(-122.4194).build();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.tools(List.of(tool))
.input(InteractionsInput.of("What is the best food near me?"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END components.schemas.GoogleMaps:google_maps]
}
@Test
public void testSchemaGooglesearchGoogleSearch() throws Exception {
// [START components.schemas.GoogleSearch:google_search]
Client client = new Client();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.tools(List.of(new GoogleSearch()))
.input(InteractionsInput.of("Who is the current president of France?"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END components.schemas.GoogleSearch:google_search]
}
@Test
public void testSchemaMcpserverMcpServer() throws Exception {
// [START components.schemas.McpServer:mcp_server]
Client client = new Client();
MCPServer mcpTool =
MCPServer.builder()
.name("weather_service")
.url("https://gemini-api-demos.uc.r.appspot.com/mcp")
.build();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.tools(List.of(mcpTool))
.input(
InteractionsInput.of(
"Today is 12-05-2025, what is the temperature today in London?"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));
System.out.println(interaction.outputText().orElse(""));
// [END components.schemas.McpServer:mcp_server]
}
@Test
public void testSchemaUrlcontextUrlContext() throws Exception {
// [START components.schemas.UrlContext:url_context]
Client client = new Client();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model("gemini-3.6-flash")
.tools(List.of(new URLContext()))
.input(InteractionsInput.of("Summarize https://www.example.com"))
.build();
CreateInteractionResponse response =
client.interactions.create(CreateInteractionRequestBody.of(params));
Interaction interaction =
response.interaction().orElseThrow(() -> new RuntimeException("No interaction returned"));