Skip to content

Commit f6133fe

Browse files
committed
Enhance Java SDK examples and models; add JBang support and improve session handling
1 parent 78ba87e commit f6133fe

5 files changed

Lines changed: 158 additions & 34 deletions

File tree

java/README.md

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,59 @@ import com.github.copilot.sdk.json.*;
3636

3737
import java.util.concurrent.CompletableFuture;
3838

39-
void main() throws Exception {
40-
// Create and start client
41-
try (var client = new CopilotClient()) {
42-
client.start().get();
43-
44-
// Create a session
45-
var session = client.createSession(
46-
new SessionConfig().setModel("gpt-5")
47-
).get();
48-
49-
// Wait for response using session.idle event
50-
var done = new CompletableFuture<Void>();
51-
52-
session.on(evt -> {
53-
if (evt instanceof AssistantMessageEvent msg) {
54-
System.out.println(msg.getData().getContent());
55-
} else if (evt instanceof SessionIdleEvent) {
56-
done.complete(null);
57-
}
58-
});
59-
60-
// Send a message and wait for completion
61-
session.send(new MessageOptions().setPrompt("What is 2+2?")).get();
62-
done.get();
39+
public class Example {
40+
public static void main(String[] args) throws Exception {
41+
// Create and start client
42+
try (var client = new CopilotClient()) {
43+
client.start().get();
44+
45+
// Create a session
46+
var session = client.createSession(
47+
new SessionConfig().setModel(CopilotModel.CLAUDE_SONNET_4_5.toString())
48+
).get();
49+
50+
// Wait for response using session.idle event
51+
var done = new CompletableFuture<Void>();
52+
53+
session.on(evt -> {
54+
if (evt instanceof AssistantMessageEvent msg) {
55+
System.out.println(msg.getData().getContent());
56+
} else if (evt instanceof SessionIdleEvent) {
57+
done.complete(null);
58+
}
59+
});
60+
61+
// Send a message and wait for completion
62+
session.send(new MessageOptions().setPrompt("What is 2+2?")).get();
63+
done.get();
64+
}
6365
}
6466
}
6567
```
6668

69+
## Try it with JBang
70+
71+
You can quickly try the SDK without setting up a full project using [JBang](https://www.jbang.dev/):
72+
73+
```bash
74+
# Assuming you are in the `java/` directory of this repository
75+
# Install the SDK locally first (not yet on Maven Central)
76+
mvn install
77+
78+
# Install JBang (if not already installed)
79+
# macOS: brew install jbang
80+
# Linux/Windows: curl -Ls https://sh.jbang.dev | bash -s - app setup
81+
82+
# Run the example
83+
jbang jbang-example.java
84+
```
85+
86+
The `jbang-example.java` file includes the dependency declaration and can be run directly:
87+
88+
```java
89+
//DEPS com.github.copilot:copilot-sdk:0.1.0
90+
```
91+
6792
## API Reference
6893

6994
### CopilotClient

java/jbang-example.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
//DEPS com.github.copilot:copilot-sdk:0.1.0
3+
import com.github.copilot.sdk.*;
4+
import com.github.copilot.sdk.events.*;
5+
import com.github.copilot.sdk.json.*;
6+
import java.util.concurrent.CompletableFuture;
7+
8+
class CopilotSDK {
9+
public static void main(String[] args) throws Exception {
10+
// Create and start client
11+
try (var client = new CopilotClient()) {
12+
client.start().get();
13+
14+
// Create a session
15+
var session = client.createSession(
16+
new SessionConfig().setModel(CopilotModel.CLAUDE_SONNET_4_5.toString())).get();
17+
18+
// Wait for response using session.idle event
19+
var done = new CompletableFuture<Void>();
20+
21+
session.on(evt -> {
22+
if (evt instanceof AssistantMessageEvent msg) {
23+
System.out.println(msg.getData().getContent());
24+
} else if (evt instanceof SessionIdleEvent) {
25+
done.complete(null);
26+
}
27+
});
28+
29+
// Send a message and wait for completion
30+
session.send(new MessageOptions().setPrompt("What is 2+2?")).get();
31+
done.get();
32+
}
33+
}
34+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.github.copilot.sdk;
2+
3+
/**
4+
* Available Copilot models.
5+
*
6+
* <p>
7+
* The actual availability of models depends on your GitHub Copilot
8+
* subscription.
9+
*/
10+
public enum CopilotModel {
11+
/** Claude Sonnet 4.5 */
12+
CLAUDE_SONNET_4_5("claude-sonnet-4.5"),
13+
/** Claude Haiku 4.5 */
14+
CLAUDE_HAIKU_4_5("claude-haiku-4.5"),
15+
/** Claude Opus 4.5 */
16+
CLAUDE_OPUS_4_5("claude-opus-4.5"),
17+
/** Claude Sonnet 4 */
18+
CLAUDE_SONNET_4("claude-sonnet-4"),
19+
/** GPT-5.2 Codex */
20+
GPT_5_2_CODEX("gpt-5.2-codex"),
21+
/** GPT-5.1 Codex Max */
22+
GPT_5_1_CODEX_MAX("gpt-5.1-codex-max"),
23+
/** GPT-5.1 Codex */
24+
GPT_5_1_CODEX("gpt-5.1-codex"),
25+
/** GPT-5.2 */
26+
GPT_5_2("gpt-5.2"),
27+
/** GPT-5.1 */
28+
GPT_5_1("gpt-5.1"),
29+
/** GPT-5 */
30+
GPT_5("gpt-5"),
31+
/** GPT-5.1 Codex Mini */
32+
GPT_5_1_CODEX_MINI("gpt-5.1-codex-mini"),
33+
/** GPT-5 Mini */
34+
GPT_5_MINI("gpt-5-mini"),
35+
/** GPT-4.1 */
36+
GPT_4_1("gpt-4.1"),
37+
/** Gemini 3 Pro Preview */
38+
GEMINI_3_PRO_PREVIEW("gemini-3-pro-preview");
39+
40+
private final String value;
41+
42+
CopilotModel(String value) {
43+
this.value = value;
44+
}
45+
46+
/**
47+
* Returns the model identifier string to use with the API.
48+
*
49+
* @return the model identifier
50+
*/
51+
public String getValue() {
52+
return value;
53+
}
54+
55+
/**
56+
* Returns the string representation of the model.
57+
*
58+
* @return the model identifier string
59+
*/
60+
@Override
61+
public String toString() {
62+
return value;
63+
}
64+
}

java/src/main/java/com/github/copilot/sdk/CopilotSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
* com.github.copilot.sdk.json.ResumeSessionConfig)
7272
* @see AbstractSessionEvent
7373
*/
74-
class CopilotSession implements AutoCloseable {
74+
public final class CopilotSession implements AutoCloseable {
7575

7676
private static final Logger LOG = Logger.getLogger(CopilotSession.class.getName());
7777
private static final ObjectMapper MAPPER = JsonRpcClient.getObjectMapper();

java/src/main/java/com/github/copilot/sdk/json/MessageOptions.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,6 @@ public MessageOptions setAttachments(List<Attachment> attachments) {
7878
return this;
7979
}
8080

81-
/**
82-
* Gets the delivery mode.
83-
*
84-
* @return the delivery mode
85-
*/
86-
public String getMode() {
87-
return mode;
88-
}
89-
9081
/**
9182
* Sets the message delivery mode.
9283
* <p>
@@ -104,4 +95,14 @@ public MessageOptions setMode(String mode) {
10495
this.mode = mode;
10596
return this;
10697
}
98+
99+
/**
100+
* Gets the delivery mode.
101+
*
102+
* @return the delivery mode
103+
*/
104+
public String getMode() {
105+
return mode;
106+
}
107+
107108
}

0 commit comments

Comments
 (0)