-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdocumentation.html
More file actions
1222 lines (1152 loc) · 50.5 KB
/
Copy pathdocumentation.html
File metadata and controls
1222 lines (1152 loc) · 50.5 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
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 2.0.0 from target/filtered-site/markdown/documentation.md at 2026-05-05
| Rendered using Apache Maven Fluido Skin 2.1.0
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 2.0.0" />
<title>GitHub Copilot SDK for Java - Documentation – GitHub Copilot SDK :: Java</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-2.1.0.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script src="./js/apache-maven-fluido-2.1.0.min.js"></script>
<style>.github-fork-ribbon:before { background-color: gray; }</style>
</head>
<body class="topBarEnabled">
<a class="github-fork-ribbon right-top" href="https://github.com/github/copilot-sdk-java" data-ribbon="Fork me on GitHub">Fork me on GitHub</a>
<header id="topbar" class="navbar navbar-fixed-top ">
<div class="navbar-inner">
<div class="container-fluid">
<a data-target=".nav-collapse" data-toggle="collapse" class="btn btn-navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="index.html">GitHub Copilot SDK for Java
</a>
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Overview <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="index.html">Introduction</a></li>
<li><a href="getting-started.html">Getting Started</a></li>
<li><a href="setup.html">Setup</a></li>
<li><a>Documentation</a></li>
<li><a href="mcp.html">MCP Servers</a></li>
<li><a href="hooks.html">Session Hooks</a></li>
<li><a href="advanced.html">Advanced Usage</a></li>
<li><a href="apidocs/index.html">API Javadoc</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Guides <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="documentation.html#Basic_Usage">Basic Usage</a></li>
<li><a href="documentation.html#Handling_Responses">Handling Responses</a></li>
<li><a href="documentation.html#Streaming_Responses">Streaming</a></li>
<li><a href="documentation.html#Choosing_a_Model">Choosing a Model</a></li>
<li><a href="documentation.html#Session_Management">Session Management</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Advanced <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="advanced.html#Custom_Tools">Custom Tools</a></li>
<li><a href="advanced.html#System_Messages">System Messages</a></li>
<li><a href="advanced.html#File_Attachments">File Attachments</a></li>
<li><a href="advanced.html#Bring_Your_Own_Key_.28BYOK.29">BYOK</a></li>
<li><a href="advanced.html#Infinite_Sessions">Infinite Sessions</a></li>
<li><a href="advanced.html#MCP_Servers">MCP Servers</a></li>
<li><a href="advanced.html#Slash_Commands">Slash Commands</a></li>
<li><a href="advanced.html#Elicitation_UI_Dialogs">Elicitation</a></li>
<li><a href="advanced.html#Getting_Session_Metadata_by_ID">Session Metadata</a></li>
<li><a href="hooks.html">Session Hooks</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Cookbook <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="cookbook/README.html">Overview</a></li>
<li><a href="cookbook/error-handling.html">Error Handling</a></li>
<li><a href="cookbook/multiple-sessions.html">Multiple Sessions</a></li>
<li><a href="cookbook/managing-local-files.html">Managing Local Files</a></li>
<li><a href="cookbook/pr-visualization.html">PR Visualization</a></li>
<li><a href="cookbook/persisting-sessions.html">Persisting Sessions</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Project Documentation <b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-submenu">
<a href="project-info.html">Project Information</a>
<ul class="dropdown-menu">
<li><a href="dependencies.html">Dependencies</a></li>
<li><a href="dependency-info.html">Maven Coordinates</a></li>
<li><a href="distribution-management.html">Distribution Management</a></li>
<li><a href="index.html">About</a></li>
<li><a href="licenses.html">Licenses</a></li>
<li><a href="plugin-management.html">Plugin Management</a></li>
<li><a href="plugins.html">Plugins</a></li>
<li><a href="scm.html">Source Code Management</a></li>
<li><a href="summary.html">Summary</a></li>
<li><a href="team.html">Team</a></li>
</ul>
</li>
<li class="dropdown-submenu">
<a href="project-reports.html">Project Reports</a>
<ul class="dropdown-menu">
<li><a href="apidocs/index.html">Javadoc</a></li>
<li><a href="surefire.html">Surefire</a></li>
<li><a href="spotbugs.html">SpotBugs</a></li>
<li><a href="taglist.html">Tag List</a></li>
<li><a href="dependency-analysis.html">Dependency Analysis</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</header>
<div class="container-fluid container-fluid-top">
<header>
<div id="banner">
<div class="pull-left"></div>
<div class="pull-right"></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2026-05-05<span class="divider">|</span>
</li>
<li id="projectVersion">Version: 1.0.0-beta-java.1<span class="divider">|</span></li>
<li><a href="index.html">Home</a><span class="divider">/</span></li>
<li class="active">GitHub Copilot SDK for Java - Documentation</li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span2">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Overview</li>
<li><a href="index.html">Introduction</a></li>
<li><a href="getting-started.html">Getting Started</a></li>
<li><a href="setup.html">Setup</a></li>
<li class="active"><a>Documentation</a></li>
<li><a href="mcp.html">MCP Servers</a></li>
<li><a href="hooks.html">Session Hooks</a></li>
<li><a href="advanced.html">Advanced Usage</a></li>
<li><a href="apidocs/index.html">API Javadoc</a></li>
<li class="nav-header">Guides</li>
<li><a href="documentation.html#Basic_Usage">Basic Usage</a></li>
<li><a href="documentation.html#Handling_Responses">Handling Responses</a></li>
<li><a href="documentation.html#Streaming_Responses">Streaming</a></li>
<li><a href="documentation.html#Choosing_a_Model">Choosing a Model</a></li>
<li><a href="documentation.html#Session_Management">Session Management</a></li>
<li class="nav-header">Advanced</li>
<li><a href="advanced.html#Custom_Tools">Custom Tools</a></li>
<li><a href="advanced.html#System_Messages">System Messages</a></li>
<li><a href="advanced.html#File_Attachments">File Attachments</a></li>
<li><a href="advanced.html#Bring_Your_Own_Key_.28BYOK.29">BYOK</a></li>
<li><a href="advanced.html#Infinite_Sessions">Infinite Sessions</a></li>
<li><a href="advanced.html#MCP_Servers">MCP Servers</a></li>
<li><a href="advanced.html#Slash_Commands">Slash Commands</a></li>
<li><a href="advanced.html#Elicitation_UI_Dialogs">Elicitation</a></li>
<li><a href="advanced.html#Getting_Session_Metadata_by_ID">Session Metadata</a></li>
<li><a href="hooks.html">Session Hooks</a></li>
<li class="nav-header">Cookbook</li>
<li><a href="cookbook/README.html">Overview</a></li>
<li><a href="cookbook/error-handling.html">Error Handling</a></li>
<li><a href="cookbook/multiple-sessions.html">Multiple Sessions</a></li>
<li><a href="cookbook/managing-local-files.html">Managing Local Files</a></li>
<li><a href="cookbook/pr-visualization.html">PR Visualization</a></li>
<li><a href="cookbook/persisting-sessions.html">Persisting Sessions</a></li>
<li class="nav-header">Project Documentation</li>
<li><a href="project-info.html"><span class="icon-chevron-right"></span>Project Information</a></li>
<li><a href="project-reports.html"><span class="icon-chevron-right"></span>Project Reports</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<a href="https://maven.apache.org/" class="builtBy" target="_blank"><img class="builtBy" alt="Built by Maven" src="./images/logos/maven-feather.png" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span10">
<section><a id="GitHub_Copilot_SDK_for_Java_-_Documentation"></a>
<h1>GitHub Copilot SDK for Java - Documentation</h1>
<p>This guide covers common use cases for the GitHub Copilot SDK for Java. For complete API details, see the <a href="apidocs/index.html">Javadoc</a>.</p><section><a id="Table_of_Contents"></a>
<h2>Table of Contents</h2>
<ul>
<li><a href="#Basic_Usage">Basic Usage</a></li>
<li><a href="#Handling_Responses">Handling Responses</a></li>
<li><a href="#Troubleshooting_Event_Handling">Troubleshooting Event Handling</a></li>
<li><a href="#Event_Types_Reference">Event Types Reference</a></li>
<li><a href="#Streaming_Responses">Streaming Responses</a></li>
<li><a href="#Session_Operations">Session Operations</a></li>
<li><a href="#Choosing_a_Model">Choosing a Model</a></li>
<li><a href="#Reasoning_Effort">Reasoning Effort</a></li>
<li><a href="#Tool_Filtering">Tool Filtering</a></li>
<li><a href="#Working_Directory">Working Directory</a></li>
<li><a href="#Connection_State__Diagnostics">Connection State & Diagnostics</a></li>
<li><a href="#Message_Delivery_Mode">Message Delivery Mode</a></li>
<li><a href="#Session_Management">Session Management</a></li>
<li><a href="#SessionConfig_Reference">SessionConfig Reference</a>
<ul>
<li><a href="#Cloning_SessionConfig">Cloning SessionConfig</a></li>
</ul></li>
</ul><hr /></section><section><a id="Basic_Usage"></a>
<h2>Basic Usage</h2>
<p>Create a client, start a session, and send a message:</p>
<pre class="prettyprint"><code class="language-java">import com.github.copilot.sdk.CopilotClient;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.SessionConfig;
try (var client = new CopilotClient()) {
client.start().get();
var session = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setModel("gpt-4.1")
).get();
var response = session.sendAndWait("Explain Java records in one sentence").get();
System.out.println(response.getData().content());
session.close();
}
</code></pre>
<p>The client manages the connection to the Copilot CLI. Sessions are independent conversations that can run concurrently.</p><hr /></section><section><a id="Handling_Responses"></a>
<h2>Handling Responses</h2><section><a id="Simple_Request-Response"></a>
<h3>Simple Request-Response</h3>
<p>For straightforward interactions, use <code>sendAndWait()</code>:</p>
<pre class="prettyprint"><code class="language-java">var response = session.sendAndWait("What is the capital of France?").get();
System.out.println(response.getData().content());
</code></pre></section><section><a id="Event-Based_Processing"></a>
<h3>Event-Based Processing</h3>
<p>For more control, subscribe to events and use <code>send()</code>:</p>
<blockquote>
<p><strong>Exception isolation:</strong> If a handler throws an exception, the SDK logs the
error. By default, dispatch stops after the first handler error
(<code>PROPAGATE_AND_LOG_ERRORS</code>). To continue dispatching to remaining handlers,
set <code>EventErrorPolicy.SUPPRESS_AND_LOG_ERRORS</code>. You can customize error
handling with <code>session.setEventErrorHandler()</code> — see the
<a href="advanced.html#Custom_Event_Error_Handler">Advanced Usage</a> guide.</p>
</blockquote></section></section><section><a id="Troubleshooting_Event_Handling"></a>
<h2>Troubleshooting Event Handling</h2><section><a id="Symptoms_of_policy_misconfiguration"></a>
<h3>Symptoms of policy misconfiguration</h3>
<ul>
<li>You registered multiple <code>session.on(...)</code> handlers, but only the first one runs</li>
<li>A handler throws once and later handlers stop receiving events</li>
<li>You expected “best effort” fan-out, but dispatch halts on errors</li>
</ul></section><section><a id="Fix"></a>
<h3>Fix</h3>
<p>Set the event error policy to suppress-and-continue:</p>
<pre class="prettyprint"><code class="language-java">session.setEventErrorPolicy(EventErrorPolicy.SUPPRESS_AND_LOG_ERRORS);
</code></pre>
<p>Optionally add a custom error handler for observability:</p>
<pre class="prettyprint"><code class="language-java">session.setEventErrorHandler((event, exception) -> {
System.err.println("Handler failed for event " + event.getType() + ": " + exception.getMessage());
});
</code></pre>
<p>Use <code>PROPAGATE_AND_LOG_ERRORS</code> when you want fail-fast behavior.</p>
<pre class="prettyprint"><code class="language-java">var done = new CompletableFuture<Void>();
// Type-safe event handlers (recommended)
session.on(AssistantMessageEvent.class, msg -> {
System.out.println("Response: " + msg.getData().content());
});
session.on(SessionErrorEvent.class, err -> {
System.err.println("Error: " + err.getData().message());
});
session.on(SessionIdleEvent.class, idle -> {
done.complete(null);
});
session.send("Tell me a joke").get();
done.get(); // Wait for completion
</code></pre>
<p>You can also use a single handler for all events:</p>
<pre class="prettyprint"><code class="language-java">session.on(event -> {
switch (event) {
case AssistantMessageEvent msg ->
System.out.println("Response: " + msg.getData().content());
case SessionErrorEvent err ->
System.err.println("Error: " + err.getData().message());
case SessionIdleEvent idle ->
done.complete(null);
default -> { }
}
});
</code></pre></section><section><a id="Key_Event_Types"></a>
<h3>Key Event Types</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>AssistantMessageEvent</code></td>
<td>Complete assistant response</td></tr>
<tr class="a">
<td><code>AssistantMessageDeltaEvent</code></td>
<td>Streaming chunk (when streaming enabled)</td></tr>
<tr class="b">
<td><code>SessionIdleEvent</code></td>
<td>Session finished processing</td></tr>
<tr class="a">
<td><code>SessionErrorEvent</code></td>
<td>An error occurred</td></tr></tbody>
</table>
<p>For the complete list of all event types, see <a href="#Event_Types_Reference">Event Types Reference</a> below.</p><hr /></section></section><section><a id="Event_Types_Reference"></a>
<h2>Event Types Reference</h2>
<p>The SDK supports event types organized by category. All events extend <code>SessionEvent</code>.</p><section><a id="Session_Events"></a>
<h3>Session Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>SessionStartEvent</code></td>
<td><code>session.start</code></td>
<td>Session has started</td></tr>
<tr class="a">
<td><code>SessionResumeEvent</code></td>
<td><code>session.resume</code></td>
<td>Session was resumed</td></tr>
<tr class="b">
<td><code>SessionIdleEvent</code></td>
<td><code>session.idle</code></td>
<td>Session finished processing, ready for input</td></tr>
<tr class="a">
<td><code>SessionErrorEvent</code></td>
<td><code>session.error</code></td>
<td>An error occurred in the session</td></tr>
<tr class="b">
<td><code>SessionInfoEvent</code></td>
<td><code>session.info</code></td>
<td>Informational message from the session</td></tr>
<tr class="a">
<td><code>SessionShutdownEvent</code></td>
<td><code>session.shutdown</code></td>
<td>Session is shutting down (includes reason and exit code)</td></tr>
<tr class="b">
<td><code>SessionModelChangeEvent</code></td>
<td><code>session.model_change</code></td>
<td>The model was changed mid-session</td></tr>
<tr class="a">
<td><code>SessionModeChangedEvent</code></td>
<td><code>session.mode_changed</code></td>
<td>Session mode changed (e.g., plan mode)</td></tr>
<tr class="b">
<td><code>SessionPlanChangedEvent</code></td>
<td><code>session.plan_changed</code></td>
<td>Session plan was updated</td></tr>
<tr class="a">
<td><code>SessionWorkspaceFileChangedEvent</code></td>
<td><code>session.workspace_file_changed</code></td>
<td>A file in the workspace was modified</td></tr>
<tr class="b">
<td><code>SessionHandoffEvent</code></td>
<td><code>session.handoff</code></td>
<td>Session handed off to another agent</td></tr>
<tr class="a">
<td><code>SessionTruncationEvent</code></td>
<td><code>session.truncation</code></td>
<td>Context was truncated due to limits</td></tr>
<tr class="b">
<td><code>SessionSnapshotRewindEvent</code></td>
<td><code>session.snapshot_rewind</code></td>
<td>Session rewound to a previous snapshot</td></tr>
<tr class="a">
<td><code>SessionUsageInfoEvent</code></td>
<td><code>session.usage_info</code></td>
<td>Token usage information</td></tr>
<tr class="b">
<td><code>SessionCompactionStartEvent</code></td>
<td><code>session.compaction_start</code></td>
<td>Context compaction started (infinite sessions)</td></tr>
<tr class="a">
<td><code>SessionCompactionCompleteEvent</code></td>
<td><code>session.compaction_complete</code></td>
<td>Context compaction completed</td></tr>
<tr class="b">
<td><code>SessionContextChangedEvent</code></td>
<td><code>session.context_changed</code></td>
<td>Working directory context changed</td></tr>
<tr class="a">
<td><code>SessionTaskCompleteEvent</code></td>
<td><code>session.task_complete</code></td>
<td>Task completed with summary</td></tr></tbody>
</table>
</section><section><a id="Assistant_Events"></a>
<h3>Assistant Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>AssistantTurnStartEvent</code></td>
<td><code>assistant.turn_start</code></td>
<td>Assistant began processing</td></tr>
<tr class="a">
<td><code>AssistantIntentEvent</code></td>
<td><code>assistant.intent</code></td>
<td>Assistant's detected intent</td></tr>
<tr class="b">
<td><code>AssistantReasoningEvent</code></td>
<td><code>assistant.reasoning</code></td>
<td>Full reasoning content (reasoning models)</td></tr>
<tr class="a">
<td><code>AssistantReasoningDeltaEvent</code></td>
<td><code>assistant.reasoning_delta</code></td>
<td>Streaming reasoning chunk</td></tr>
<tr class="b">
<td><code>AssistantMessageEvent</code></td>
<td><code>assistant.message</code></td>
<td>Complete assistant message</td></tr>
<tr class="a">
<td><code>AssistantMessageDeltaEvent</code></td>
<td><code>assistant.message_delta</code></td>
<td>Streaming message chunk</td></tr>
<tr class="b">
<td><code>AssistantStreamingDeltaEvent</code></td>
<td><code>assistant.streaming_delta</code></td>
<td>Streaming progress with size metrics</td></tr>
<tr class="a">
<td><code>AssistantTurnEndEvent</code></td>
<td><code>assistant.turn_end</code></td>
<td>Assistant finished processing</td></tr>
<tr class="b">
<td><code>AssistantUsageEvent</code></td>
<td><code>assistant.usage</code></td>
<td>Token usage for this turn</td></tr></tbody>
</table>
</section><section><a id="Tool_Events"></a>
<h3>Tool Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>ToolUserRequestedEvent</code></td>
<td><code>tool.user_requested</code></td>
<td>User requested a tool invocation</td></tr>
<tr class="a">
<td><code>ToolExecutionStartEvent</code></td>
<td><code>tool.execution_start</code></td>
<td>Tool execution started</td></tr>
<tr class="b">
<td><code>ToolExecutionProgressEvent</code></td>
<td><code>tool.execution_progress</code></td>
<td>Tool execution progress update</td></tr>
<tr class="a">
<td><code>ToolExecutionPartialResultEvent</code></td>
<td><code>tool.execution_partial_result</code></td>
<td>Partial result from tool</td></tr>
<tr class="b">
<td><code>ToolExecutionCompleteEvent</code></td>
<td><code>tool.execution_complete</code></td>
<td>Tool execution completed</td></tr></tbody>
</table>
</section><section><a id="User_Events"></a>
<h3>User Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>UserMessageEvent</code></td>
<td><code>user.message</code></td>
<td>User sent a message</td></tr>
<tr class="a">
<td><code>PendingMessagesModifiedEvent</code></td>
<td><code>pending_messages.modified</code></td>
<td>Pending message queue changed</td></tr></tbody>
</table>
</section><section><a id="Subagent_Events"></a>
<h3>Subagent Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>SubagentStartedEvent</code></td>
<td><code>subagent.started</code></td>
<td>Subagent was spawned</td></tr>
<tr class="a">
<td><code>SubagentSelectedEvent</code></td>
<td><code>subagent.selected</code></td>
<td>Subagent was selected for task</td></tr>
<tr class="b">
<td><code>SubagentDeselectedEvent</code></td>
<td><code>subagent.deselected</code></td>
<td>Subagent was deselected</td></tr>
<tr class="a">
<td><code>SubagentCompletedEvent</code></td>
<td><code>subagent.completed</code></td>
<td>Subagent completed its task</td></tr>
<tr class="b">
<td><code>SubagentFailedEvent</code></td>
<td><code>subagent.failed</code></td>
<td>Subagent failed</td></tr></tbody>
</table>
</section><section><a id="Other_Events"></a>
<h3>Other Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>AbortEvent</code></td>
<td><code>abort</code></td>
<td>Operation was aborted</td></tr>
<tr class="a">
<td><code>HookStartEvent</code></td>
<td><code>hook.start</code></td>
<td>Hook execution started</td></tr>
<tr class="b">
<td><code>HookEndEvent</code></td>
<td><code>hook.end</code></td>
<td>Hook execution completed</td></tr>
<tr class="a">
<td><code>SystemMessageEvent</code></td>
<td><code>system.message</code></td>
<td>System-level message</td></tr>
<tr class="b">
<td><code>SystemNotificationEvent</code></td>
<td><code>system.notification</code></td>
<td>System notification (informational)</td></tr>
<tr class="a">
<td><code>SkillInvokedEvent</code></td>
<td><code>skill.invoked</code></td>
<td>A skill was invoked</td></tr></tbody>
</table>
</section><section><a id="External_Tool_Events"></a>
<h3>External Tool Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>ExternalToolRequestedEvent</code></td>
<td><code>external_tool.requested</code></td>
<td>An external tool invocation was requested</td></tr>
<tr class="a">
<td><code>ExternalToolCompletedEvent</code></td>
<td><code>external_tool.completed</code></td>
<td>An external tool invocation completed</td></tr></tbody>
</table>
</section><section><a id="Permission_Events"></a>
<h3>Permission Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>PermissionRequestedEvent</code></td>
<td><code>permission.requested</code></td>
<td>A permission request was issued</td></tr>
<tr class="a">
<td><code>PermissionCompletedEvent</code></td>
<td><code>permission.completed</code></td>
<td>A permission request was resolved</td></tr></tbody>
</table>
</section><section><a id="Command_Events"></a>
<h3>Command Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>CommandQueuedEvent</code></td>
<td><code>command.queued</code></td>
<td>A command was queued for execution</td></tr>
<tr class="a">
<td><code>CommandCompletedEvent</code></td>
<td><code>command.completed</code></td>
<td>A queued command completed</td></tr>
<tr class="b">
<td><code>CommandExecuteEvent</code></td>
<td><code>command.execute</code></td>
<td>A registered slash command was dispatched for execution</td></tr></tbody>
</table>
</section><section><a id="Elicitation_Events"></a>
<h3>Elicitation Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>ElicitationRequestedEvent</code></td>
<td><code>elicitation.requested</code></td>
<td>An elicitation (UI dialog) request was received</td></tr></tbody>
</table>
</section><section><a id="Capability_Events"></a>
<h3>Capability Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>CapabilitiesChangedEvent</code></td>
<td><code>capabilities.changed</code></td>
<td>Session capabilities were updated</td></tr></tbody>
</table>
</section><section><a id="Plan_Mode_Events"></a>
<h3>Plan Mode Events</h3>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Event</th>
<th>Type String</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>ExitPlanModeRequestedEvent</code></td>
<td><code>exit_plan_mode.requested</code></td>
<td>Exit from plan mode was requested</td></tr>
<tr class="a">
<td><code>ExitPlanModeCompletedEvent</code></td>
<td><code>exit_plan_mode.completed</code></td>
<td>Exit from plan mode completed</td></tr></tbody>
</table>
<p>See the <a href="apidocs/com/github/copilot/sdk/generated/package-summary.html">generated package Javadoc</a> for detailed event data structures.</p><hr /></section></section><section><a id="Streaming_Responses"></a>
<h2>Streaming Responses</h2>
<p>Enable streaming to receive response chunks as they're generated:</p>
<pre class="prettyprint"><code class="language-java">var session = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setModel("gpt-4.1")
.setStreaming(true)
).get();
var done = new CompletableFuture<Void>();
session.on(AssistantMessageDeltaEvent.class, delta -> {
// Print each chunk as it arrives
System.out.print(delta.getData().deltaContent());
});
session.on(SessionIdleEvent.class, idle -> {
System.out.println(); // Newline at end
done.complete(null);
});
session.send("Write a haiku about Java").get();
done.get();
</code></pre><hr /></section><section><a id="Session_Operations"></a>
<h2>Session Operations</h2><section><a id="Get_Conversation_History"></a>
<h3>Get Conversation History</h3>
<p>Retrieve all messages and events from a session using <code>getMessages()</code>:</p>
<pre class="prettyprint"><code class="language-java">var history = session.getMessages().get();
for (var event : history) {
switch (event) {
case UserMessageEvent user ->
System.out.println("User: " + user.getData().content());
case AssistantMessageEvent assistant ->
System.out.println("Assistant: " + assistant.getData().content());
case ToolExecutionCompleteEvent tool ->
System.out.println("Tool: " + tool.getData().toolCallId());
default -> { }
}
}
</code></pre>
<p>This is useful for:</p>
<ul>
<li>Displaying conversation history in a UI</li>
<li>Persisting conversations for later review</li>
<li>Debugging and logging session state</li>
</ul></section><section><a id="Abort_Current_Operation"></a>
<h3>Abort Current Operation</h3>
<p>Cancel a long-running operation using <code>abort()</code>:</p>
<pre class="prettyprint"><code class="language-java">// Start a potentially long operation
var messageFuture = session.send("Analyze this large codebase...");
// User clicks cancel button
session.abort().get();
// The session will emit an AbortEvent
session.on(AbortEvent.class, evt -> {
System.out.println("Operation was cancelled");
});
</code></pre>
<p>Use cases:</p>
<ul>
<li>User-initiated cancellation in interactive applications</li>
<li>Timeout handling in automated pipelines</li>
<li>Graceful shutdown when application is closing</li>
</ul></section><section><a id="Custom_Timeout"></a>
<h3>Custom Timeout</h3>
<p>Use <code>sendAndWait</code> with a custom timeout for CI/CD pipelines:</p>
<pre class="prettyprint"><code class="language-java">try {
// 30-second timeout
var response = session.sendAndWait(
new MessageOptions().setPrompt("Quick question"),
30000 // timeout in milliseconds
).get();
} catch (ExecutionException e) {
if (e.getCause() instanceof TimeoutException) {
System.err.println("Request timed out");
session.abort().get();
}
}
</code></pre><hr /></section></section><section><a id="Choosing_a_Model"></a>
<h2>Choosing a Model</h2><section><a id="List_Available_Models"></a>
<h3>List Available Models</h3>
<p>Query available models before creating a session:</p>
<pre class="prettyprint"><code class="language-java">var models = client.listModels().get();
for (var model : models) {
System.out.printf("%s (%s)%n", model.getId(), model.getName());
}
</code></pre></section><section><a id="Use_a_Specific_Model"></a>
<h3>Use a Specific Model</h3>
<pre class="prettyprint"><code class="language-java">var session = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setModel("claude-sonnet-4")
).get();
</code></pre><hr /></section></section><section><a id="Reasoning_Effort"></a>
<h2>Reasoning Effort</h2>
<p>For models that support it, control how much effort the model spends reasoning before responding:</p>
<pre class="prettyprint"><code class="language-java">var session = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setModel("o3-mini")
.setReasoningEffort("high")
).get();
</code></pre>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Level</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>"low"</code></td>
<td>Fastest responses, less detailed reasoning</td></tr>
<tr class="a">
<td><code>"medium"</code></td>
<td>Balanced speed and reasoning depth</td></tr>
<tr class="b">
<td><code>"high"</code></td>
<td>Thorough reasoning, slower responses</td></tr>
<tr class="a">
<td><code>"xhigh"</code></td>
<td>Maximum reasoning effort</td></tr></tbody>
</table>
<blockquote>
<p><strong>Note:</strong> Only applies to reasoning models (e.g., <code>o3-mini</code>). Non-reasoning models ignore this setting.</p>
</blockquote><hr /></section><section><a id="Tool_Filtering"></a>
<h2>Tool Filtering</h2>
<p>Control which built-in tools the assistant can use with allowlists and blocklists.</p><section><a id="Allowlist_.28Available_Tools.29"></a>
<h3>Allowlist (Available Tools)</h3>
<p>Restrict the session to only specific tools:</p>
<pre class="prettyprint"><code class="language-java">var session = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setAvailableTools(List.of("read_file", "search_code", "list_dir"))
).get();
</code></pre>
<p>When <code>availableTools</code> is set, the assistant can <strong>only</strong> use tools in this list.</p></section><section><a id="Blocklist_.28Excluded_Tools.29"></a>
<h3>Blocklist (Excluded Tools)</h3>
<p>Allow all tools except specific ones:</p>
<pre class="prettyprint"><code class="language-java">var session = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setExcludedTools(List.of("execute_command", "write_file"))
).get();
</code></pre>
<p>Tools in the <code>excludedTools</code> list will not be available to the assistant.</p></section><section><a id="Combining_with_Custom_Tools"></a>
<h3>Combining with Custom Tools</h3>
<p>Tool filtering applies to built-in tools. Your custom tools (registered via <code>setTools()</code>) are always available:</p>
<pre class="prettyprint"><code class="language-java">var lookupTool = ToolDefinition.create("lookup_issue", "Fetch issue", schema, handler);
var session = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setTools(List.of(lookupTool)) // Custom tool always available
.setAvailableTools(List.of("read_file")) // Only allow read_file from built-ins
).get();
</code></pre><hr /></section></section><section><a id="Working_Directory"></a>
<h2>Working Directory</h2>
<p>Set the working directory for file operations in the session:</p>
<pre class="prettyprint"><code class="language-java">var session = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setWorkingDirectory("/path/to/project")
).get();
</code></pre>
<p>This affects how the assistant resolves relative file paths when using tools like <code>read_file</code>, <code>write_file</code>, and <code>search_code</code>.</p><hr /></section><section><a id="Connection_State_.26_Diagnostics"></a>
<h2>Connection State & Diagnostics</h2><section><a id="Checking_Connection_State"></a>
<h3>Checking Connection State</h3>
<p>Query the client's connection state at any time without making a server call:</p>
<pre class="prettyprint"><code class="language-java">ConnectionState state = client.getState();
switch (state) {
case CONNECTED -> System.out.println("Ready");
case CONNECTING -> System.out.println("Starting up...");
case DISCONNECTED -> System.out.println("Not connected");
case ERROR -> System.out.println("Connection failed");
}
</code></pre>
<p>The four states are:</p>
<table class="table table-striped">
<thead>
<tr class="a">
<th>State</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>DISCONNECTED</code></td>
<td>Client has not been started yet</td></tr>
<tr class="a">
<td><code>CONNECTING</code></td>
<td><code>start()</code> was called but hasn't completed</td></tr>
<tr class="b">
<td><code>CONNECTED</code></td>
<td>Client is connected and ready</td></tr>
<tr class="a">
<td><code>ERROR</code></td>
<td>Connection failed (check logs for details)</td></tr></tbody>
</table>
</section><section><a id="State_Transitions"></a>
<h3>State Transitions</h3>
<pre><code class="nohighlight nocode"> start()
DISCONNECTED ──────────► CONNECTING
│
┌──────────┼──────────┐
│ success │ │ failure
▼ │ ▼
CONNECTED │ ERROR
│ │
stop() / │ │
forceStop() │ │
▼ │
DISCONNECTED ◄───┘
│
│ start() (retry)
▼
CONNECTING
</code></pre>
<ul>
<li><strong>DISCONNECTED → CONNECTING:</strong> <code>start()</code> was called</li>
<li><strong>CONNECTING → CONNECTED:</strong> Server connection and protocol handshake succeeded</li>
<li><strong>CONNECTING → ERROR:</strong> Connection or protocol verification failed</li>
<li><strong>CONNECTED → DISCONNECTED:</strong> <code>stop()</code> or <code>forceStop()</code> was called, or <code>close()</code> via try-with-resources</li>
<li><strong>DISCONNECTED → CONNECTING:</strong> <code>start()</code> can be called again to retry</li>
</ul></section><section><a id="Server_Status"></a>
<h3>Server Status</h3>
<p>Get CLI version and protocol information:</p>
<pre class="prettyprint"><code class="language-java">var status = client.getStatus().get();
System.out.println("CLI version: " + status.getVersion());
System.out.println("Protocol version: " + status.getProtocolVersion());
</code></pre></section><section><a id="Authentication_Status"></a>
<h3>Authentication Status</h3>
<p>Check whether the current connection is authenticated and how:</p>
<pre class="prettyprint"><code class="language-java">var auth = client.getAuthStatus().get();
if (auth.isAuthenticated()) {
System.out.println("Logged in as: " + auth.getLogin());
System.out.println("Auth type: " + auth.getAuthType());
System.out.println("Host: " + auth.getHost());
} else {
System.out.println("Not authenticated: " + auth.getStatusMessage());
}
</code></pre></section><section><a id="Ping"></a>
<h3>Ping</h3>
<p>Verify server connectivity:</p>
<pre class="prettyprint"><code class="language-java">var pong = client.ping("hello").get();
System.out.println("Server responded, protocol version: " + pong.protocolVersion());
</code></pre>
<p>See <a href="apidocs/com/github/copilot/sdk/ConnectionState.html">ConnectionState</a>, <a href="apidocs/com/github/copilot/sdk/json/GetStatusResponse.html">GetStatusResponse</a>, and <a href="apidocs/com/github/copilot/sdk/json/GetAuthStatusResponse.html">GetAuthStatusResponse</a> Javadoc for details.</p><hr /></section></section><section><a id="Message_Delivery_Mode"></a>
<h2>Message Delivery Mode</h2>
<p>Control how messages are delivered to the session:</p>
<pre class="prettyprint"><code class="language-java">// Default: message is enqueued for processing
session.send(new MessageOptions()
.setPrompt("Analyze this codebase")
).get();
// Immediate: process the message right away
session.send(new MessageOptions()
.setPrompt("Quick question")
.setMode("immediate")
).get();
</code></pre>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Mode</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>"enqueue"</code></td>
<td>Queue the message for processing (default)</td></tr>
<tr class="a">
<td><code>"immediate"</code></td>
<td>Process the message immediately</td></tr></tbody>
</table>
<hr /></section><section><a id="Session_Management"></a>
<h2>Session Management</h2><section><a id="Multiple_Concurrent_Sessions"></a>
<h3>Multiple Concurrent Sessions</h3>
<pre class="prettyprint"><code class="language-java">var session1 = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setModel("gpt-4.1")
).get();
var session2 = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setModel("claude-sonnet-4")
).get();
// Send messages concurrently
var future1 = session1.sendAndWait("Summarize REST APIs");
var future2 = session2.sendAndWait("Summarize GraphQL");
System.out.println("GPT: " + future1.get().getData().content());
System.out.println("Claude: " + future2.get().getData().content());
</code></pre></section><section><a id="Resume_a_Previous_Session"></a>
<h3>Resume a Previous Session</h3>
<pre class="prettyprint"><code class="language-java">// Get the last session ID
var lastSessionId = client.getLastSessionId().get();
// Or list all sessions
var sessions = client.listSessions().get();
// Resume a session
var session = client.resumeSession(lastSessionId, new ResumeSessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
</code></pre></section><section><a id="Resume_Options"></a>
<h3>Resume Options</h3>
<p>When resuming a session, you can optionally reconfigure many settings. This is useful when you need to change the model, update tool configurations, or modify behavior.</p>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Option</th>
<th>Description</th></tr></thead><tbody>
<tr class="b">
<td><code>model</code></td>
<td>Change the model for the resumed session</td></tr>
<tr class="a">
<td><code>systemMessage</code></td>
<td>Override or extend the system prompt</td></tr>
<tr class="b">
<td><code>availableTools</code></td>
<td>Restrict which tools are available</td></tr>
<tr class="a">
<td><code>excludedTools</code></td>
<td>Disable specific tools</td></tr>
<tr class="b">
<td><code>provider</code></td>
<td>Re-provide BYOK credentials (required for BYOK sessions)</td></tr>
<tr class="a">
<td><code>reasoningEffort</code></td>
<td>Adjust reasoning effort level</td></tr>
<tr class="b">
<td><code>streaming</code></td>
<td>Enable/disable streaming responses</td></tr>