Repository navigation
docs: add Java language tabs across all SDK documentation #1021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c9f79c9
c6e8fdc
00f828e
905c877
07af152
63a36a9
692f764
4a7a32d
a843134
5929102
afbc006
236901b
e1fbc2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -205,6 +205,41 @@ await using var session = await client.CreateSessionAsync(new SessionConfig | |
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary><strong>Java</strong></summary> | ||
|
|
||
| ```java | ||
| import com.github.copilot.sdk.CopilotClient; | ||
| import com.github.copilot.sdk.events.*; | ||
| import com.github.copilot.sdk.json.*; | ||
|
|
||
| try (var client = new CopilotClient()) { | ||
| client.start().get(); | ||
|
|
||
| var session = client.createSession( | ||
| new SessionConfig() | ||
| .setModel("gpt-4.1") | ||
| .setCustomAgents(List.of( | ||
| new CustomAgentConfig() | ||
| .setName("researcher") | ||
| .setDisplayName("Research Agent") | ||
| .setDescription("Explores codebases and answers questions using read-only tools") | ||
| .setTools(List.of("grep", "glob", "view")) | ||
| .setPrompt("You are a research assistant. Analyze code and answer questions. Do not modify any files."), | ||
|
Comment on lines
+221
to
+228
|
||
| new CustomAgentConfig() | ||
| .setName("editor") | ||
| .setDisplayName("Editor Agent") | ||
| .setDescription("Makes targeted code changes") | ||
| .setTools(List.of("view", "edit", "bash")) | ||
| .setPrompt("You are a code editor. Make minimal, surgical changes to files as requested.") | ||
| )) | ||
| .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) | ||
| ).get(); | ||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ## Configuration Reference | ||
|
|
||
| | Property | Type | Required | Description | | ||
|
|
@@ -316,6 +351,28 @@ var session = await client.CreateSessionAsync(new SessionConfig | |
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary><strong>Java</strong></summary> | ||
|
|
||
| <!-- docs-validate: skip --> | ||
| ```java | ||
| var session = client.createSession( | ||
| new SessionConfig() | ||
| .setCustomAgents(List.of( | ||
| new CustomAgentConfig() | ||
| .setName("researcher") | ||
| .setPrompt("You are a research assistant. Analyze code and answer questions."), | ||
| new CustomAgentConfig() | ||
| .setName("editor") | ||
| .setPrompt("You are a code editor. Make minimal, surgical changes.") | ||
| )) | ||
| .setAgent("researcher") // Pre-select the researcher agent | ||
| .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) | ||
| ).get(); | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ## How Sub-Agent Delegation Works | ||
|
|
||
| When you send a prompt to a session with custom agents, the runtime evaluates whether to delegate to a sub-agent: | ||
|
|
@@ -561,6 +618,34 @@ await session.SendAndWaitAsync(new MessageOptions | |
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary><strong>Java</strong></summary> | ||
|
|
||
| ```java | ||
| session.on(event -> { | ||
| if (event instanceof SubagentStartedEvent e) { | ||
| System.out.println("▶ Sub-agent started: " + e.getData().agentDisplayName()); | ||
| System.out.println(" Description: " + e.getData().agentDescription()); | ||
| System.out.println(" Tool call ID: " + e.getData().toolCallId()); | ||
| } else if (event instanceof SubagentCompletedEvent e) { | ||
| System.out.println("✅ Sub-agent completed: " + e.getData().agentName()); | ||
| } else if (event instanceof SubagentFailedEvent e) { | ||
| System.out.println("❌ Sub-agent failed: " + e.getData().agentName()); | ||
| System.out.println(" Error: " + e.getData().error()); | ||
| } else if (event instanceof SubagentSelectedEvent e) { | ||
| System.out.println("🎯 Agent selected: " + e.getData().agentDisplayName()); | ||
| } else if (event instanceof SubagentDeselectedEvent e) { | ||
| System.out.println("↩ Agent deselected, returning to parent"); | ||
| } | ||
| }); | ||
|
|
||
| var response = session.sendAndWait( | ||
| new MessageOptions().setPrompt("Research how authentication works in this codebase") | ||
| ).get(); | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ## Building an Agent Tree UI | ||
|
|
||
| Sub-agent events include `toolCallId` fields that let you reconstruct the execution tree. Here's a pattern for tracking agent activity: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,6 +195,33 @@ var session = await client.CreateSessionAsync(new SessionConfig | |
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary><strong>Java</strong></summary> | ||
|
|
||
| ```java | ||
| import com.github.copilot.sdk.CopilotClient; | ||
| import com.github.copilot.sdk.events.*; | ||
| import com.github.copilot.sdk.json.*; | ||
|
|
||
| try (var client = new CopilotClient()) { | ||
| client.start().get(); | ||
|
|
||
| var hooks = new SessionHooks() | ||
| .setOnSessionStart((input, inv) -> CompletableFuture.completedFuture(null)) | ||
| .setOnPreToolUse((input, inv) -> CompletableFuture.completedFuture(null)) | ||
| .setOnPostToolUse((input, inv) -> CompletableFuture.completedFuture(null)); | ||
|
Comment on lines
+206
to
+212
|
||
| // ... add only the hooks you need | ||
|
|
||
| var session = client.createSession( | ||
| new SessionConfig() | ||
| .setHooks(hooks) | ||
| .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) | ||
| ).get(); | ||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| > **Tip:** Every hook handler receives an `invocation` parameter containing the `sessionId`, which is useful for correlating logs and maintaining per-session state. | ||
| --- | ||
|
|
@@ -380,6 +407,32 @@ var session = await client.CreateSessionAsync(new SessionConfig | |
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary><strong>Java</strong></summary> | ||
|
|
||
| ```java | ||
| var readOnlyTools = Set.of("read_file", "glob", "grep", "view"); | ||
|
|
||
| var hooks = new SessionHooks() | ||
| .setOnPreToolUse((input, invocation) -> { | ||
| if (!readOnlyTools.contains(input.getToolName())) { | ||
| return CompletableFuture.completedFuture( | ||
| PreToolUseHookOutput.deny( | ||
| "Only read-only tools are allowed. \"" + input.getToolName() + "\" was blocked.") | ||
| ); | ||
| } | ||
| return CompletableFuture.completedFuture(PreToolUseHookOutput.allow()); | ||
| }); | ||
|
|
||
| var session = client.createSession( | ||
| new SessionConfig() | ||
| .setHooks(hooks) | ||
| .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) | ||
| ).get(); | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ### Restrict file access to specific directories | ||
|
|
||
| ```typescript | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -219,6 +219,34 @@ await session.SendAsync(new MessageOptions | |
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary><strong>Java</strong></summary> | ||
|
|
||
| ```java | ||
| import com.github.copilot.sdk.CopilotClient; | ||
| import com.github.copilot.sdk.events.*; | ||
| import com.github.copilot.sdk.json.*; | ||
|
|
||
| try (var client = new CopilotClient()) { | ||
| client.start().get(); | ||
|
|
||
| var session = client.createSession( | ||
| new SessionConfig() | ||
| .setModel("gpt-4.1") | ||
| .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) | ||
| ).get(); | ||
|
|
||
| session.send(new MessageOptions() | ||
| .setPrompt("Describe what you see in this image") | ||
| .setAttachments(List.of( | ||
| new Attachment("file", "/absolute/path/to/screenshot.png", "screenshot.png") | ||
| )) | ||
| ).get(); | ||
|
Comment on lines
+239
to
+244
|
||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ## Quick Start — Blob Attachment | ||
|
|
||
| When you already have image data in memory (e.g., a screenshot captured by your app, or an image fetched from an API), use a blob attachment to send it directly without writing to disk. | ||
|
|
@@ -400,6 +428,38 @@ await session.SendAsync(new MessageOptions | |
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary><strong>Java</strong></summary> | ||
|
|
||
| ```java | ||
| import com.github.copilot.sdk.CopilotClient; | ||
| import com.github.copilot.sdk.events.*; | ||
| import com.github.copilot.sdk.json.*; | ||
|
|
||
| try (var client = new CopilotClient()) { | ||
| client.start().get(); | ||
|
|
||
| var session = client.createSession( | ||
| new SessionConfig() | ||
| .setModel("gpt-4.1") | ||
| .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) | ||
| ).get(); | ||
|
|
||
| var base64ImageData = "..."; // your base64-encoded image | ||
| session.send(new MessageOptions() | ||
| .setPrompt("Describe what you see in this image") | ||
| .setAttachments(List.of( | ||
| new BlobAttachment() | ||
| .setData(base64ImageData) | ||
| .setMimeType("image/png") | ||
| .setDisplayName("screenshot.png") | ||
| )) | ||
| ).get(); | ||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ## Supported Formats | ||
|
|
||
| Supported image formats include JPG, PNG, GIF, and other common image types. For file attachments, the runtime reads the image from disk and converts it as needed. For blob attachments, you provide the base64 data and MIME type directly. Use PNG or JPEG for best results, as these are the most widely supported formats. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Java
onListModelsexample referencesListand several model types (ModelInfo,ModelCapabilities, etc.) but doesn't importjava.util.Listor the SDK JSON types package. As written, it won't compile; please add the missing imports to make the snippet copy/pasteable.