Skip to content

Commit 1762525

Browse files
Copilotbrunoborges
andcommitted
Fix cookbook code issues to ensure examples compile and run correctly
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent 5b83323 commit 1762525

3 files changed

Lines changed: 40 additions & 23 deletions

File tree

src/site/markdown/cookbook/error-handling.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,28 @@ public class BasicErrorHandling {
6666
import com.github.copilot.sdk.*;
6767
import com.github.copilot.sdk.events.*;
6868
import com.github.copilot.sdk.json.*;
69-
import java.io.IOException;
7069
import java.util.concurrent.ExecutionException;
71-
import java.util.concurrent.TimeoutException;
7270

7371
public class SpecificErrorHandling {
7472
public static void startClient() {
7573
try (var client = new CopilotClient()) {
7674
client.start().get();
7775
// ... use client ...
78-
} catch (IOException ex) {
79-
System.err.println("Copilot CLI not found. Please install it first.");
80-
System.err.println("Details: " + ex.getMessage());
81-
} catch (TimeoutException ex) {
82-
System.err.println("Could not connect to Copilot CLI server.");
83-
System.err.println("Details: " + ex.getMessage());
8476
} catch (ExecutionException ex) {
85-
System.err.println("Unexpected error: " + ex.getCause().getMessage());
86-
ex.getCause().printStackTrace();
77+
Throwable cause = ex.getCause();
78+
if (cause instanceof java.io.IOException) {
79+
System.err.println("Copilot CLI not found. Please install it first.");
80+
System.err.println("Details: " + cause.getMessage());
81+
} else if (cause instanceof java.util.concurrent.TimeoutException) {
82+
System.err.println("Could not connect to Copilot CLI server.");
83+
System.err.println("Details: " + cause.getMessage());
84+
} else {
85+
System.err.println("Unexpected error: " + cause.getMessage());
86+
cause.printStackTrace();
87+
}
88+
} catch (InterruptedException ex) {
89+
Thread.currentThread().interrupt();
90+
System.err.println("Operation interrupted: " + ex.getMessage());
8791
} catch (Exception ex) {
8892
System.err.println("Unexpected error: " + ex.getMessage());
8993
ex.printStackTrace();

src/site/markdown/cookbook/managing-local-files.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ You have a folder with many files and want to organize them into subfolders base
2525

2626
**Usage:**
2727
```bash
28+
# Use with a specific folder (recommended)
29+
jbang ManagingLocalFiles.java /path/to/your/folder
30+
31+
# Or run without arguments to use a safe default (temp directory)
2832
jbang ManagingLocalFiles.java
2933
```
3034

@@ -63,19 +67,19 @@ public class ManagingLocalFiles {
6367

6468
session.on(SessionIdleEvent.class, evt -> done.countDown());
6569

66-
// Ask Copilot to organize files
67-
String userHome = System.getProperty("user.home");
68-
String targetFolder = Paths.get(userHome, "Downloads").toString();
70+
// Ask Copilot to organize files - using a safe example folder
71+
// For real use, replace with your target folder
72+
String targetFolder = args.length > 0 ? args[0] :
73+
System.getProperty("java.io.tmpdir") + "/example-files";
6974

7075
String prompt = String.format("""
71-
Analyze the files in "%s" and organize them into subfolders.
76+
Analyze the files in "%s" and show how you would organize them into subfolders.
7277
7378
1. First, list all files and their metadata
7479
2. Preview grouping by file extension
75-
3. Create appropriate subfolders (e.g., "images", "documents", "videos")
76-
4. Move each file to its appropriate subfolder
77-
78-
Please confirm before moving any files.
80+
3. Suggest appropriate subfolders (e.g., "images", "documents", "videos")
81+
82+
IMPORTANT: DO NOT move any files. Only show the plan.
7983
""", targetFolder);
8084

8185
session.send(new MessageOptions().setPrompt(prompt));

src/site/markdown/cookbook/persisting-sessions.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public class DeleteSession {
125125
## Getting session history
126126

127127
```java
128+
//DEPS io.github.copilot-community-sdk:copilot-sdk:${project.version}
129+
import com.github.copilot.sdk.*;
130+
import com.github.copilot.sdk.events.*;
131+
import com.github.copilot.sdk.json.*;
132+
128133
public class SessionHistory {
129134
public static void main(String[] args) throws Exception {
130135
try (var client = new CopilotClient()) {
@@ -133,11 +138,15 @@ public class SessionHistory {
133138
var session = client.resumeSession("user-123-conversation").get();
134139

135140
var messages = session.getMessages().get();
136-
for (var msg : messages) {
137-
System.out.printf("[%s] %s%n",
138-
msg.getType(),
139-
msg.getContent() != null ? msg.getContent() : "(no content)"
140-
);
141+
for (var event : messages) {
142+
// Print different event types appropriately
143+
if (event instanceof AssistantMessageEvent msg) {
144+
System.out.printf("[assistant] %s%n", msg.getData().getContent());
145+
} else if (event instanceof UserMessageEvent userMsg) {
146+
System.out.printf("[user] %s%n", userMsg.getData().getPrompt());
147+
} else {
148+
System.out.printf("[%s]%n", event.getType());
149+
}
141150
}
142151

143152
session.close();

0 commit comments

Comments
 (0)