Skip to content

Commit caba4f0

Browse files
committed
Revert "Merge pull request #1314 from github/fix/java-cookbook-sdk-0.2.1"
This reverts commit d14dc51, reversing changes made to 7c6c0be.
1 parent d14dc51 commit caba4f0

File tree

5 files changed

+36
-53
lines changed

5 files changed

+36
-53
lines changed

cookbook/copilot-sdk/java/accessibility-report.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ public class AccessibilityReport {
6767
client.start().get();
6868
6969
// Configure Playwright MCP server for browser automation
70-
Map<String, Object> mcpConfig = Map.of(
71-
"type", "local",
72-
"command", "npx",
73-
"args", List.of("@playwright/mcp@latest"),
74-
"tools", List.of("*")
75-
);
70+
var mcpConfig = new McpServerConfig()
71+
.setType("local")
72+
.setCommand("npx")
73+
.setArgs(List.of("@playwright/mcp@latest"))
74+
.setTools(List.of("*"));
7675
7776
var session = client.createSession(
7877
new SessionConfig()
@@ -168,12 +167,11 @@ public class AccessibilityReport {
168167
The recipe configures a local MCP server that runs alongside the session:
169168
170169
```java
171-
Map<String, Object> mcpConfig = Map.of(
172-
"type", "local",
173-
"command", "npx",
174-
"args", List.of("@playwright/mcp@latest"),
175-
"tools", List.of("*")
176-
);
170+
var mcpConfig = new McpServerConfig()
171+
.setType("local")
172+
.setCommand("npx")
173+
.setArgs(List.of("@playwright/mcp@latest"))
174+
.setTools(List.of("*"));
177175
178176
var session = client.createSession(
179177
new SessionConfig()

cookbook/copilot-sdk/java/error-handling.md

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -150,42 +150,27 @@ try (var client = new CopilotClient()) {
150150
151151
## Handling tool errors
152152
153-
When defining tools, return an error string to signal a failure back to the model instead of throwing.
153+
When defining tools, return an error result to signal a failure back to the model instead of throwing.
154154
155155
```java
156-
import com.github.copilot.sdk.json.ToolDefinition;
157-
import java.util.concurrent.CompletableFuture;
158-
159-
var readFileTool = ToolDefinition.create(
160-
"read_file",
161-
"Read a file from disk",
162-
Map.of(
163-
"type", "object",
164-
"properties", Map.of(
165-
"path", Map.of("type", "string", "description", "File path")
166-
),
167-
"required", List.of("path")
168-
),
169-
invocation -> {
156+
import com.github.copilot.sdk.json.ToolResultObject;
157+
158+
session.addTool(
159+
ToolDefinition.builder()
160+
.name("read_file")
161+
.description("Read a file from disk")
162+
.parameter("path", "string", "File path", true)
163+
.build(),
164+
(args) -> {
170165
try {
171-
var path = (String) invocation.getArguments().get("path");
172166
var content = java.nio.file.Files.readString(
173-
java.nio.file.Path.of(path));
174-
return CompletableFuture.completedFuture(content);
175-
} catch (java.io.IOException ex) {
176-
return CompletableFuture.completedFuture(
177-
"Error: Failed to read file: " + ex.getMessage());
167+
java.nio.file.Path.of(args.get("path").toString()));
168+
return ToolResultObject.success(content);
169+
} catch (IOException ex) {
170+
return ToolResultObject.error("Failed to read file: " + ex.getMessage());
178171
}
179172
}
180173
);
181-
182-
// Register tools when creating the session
183-
var session = client.createSession(
184-
new SessionConfig()
185-
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
186-
.setModel("gpt-5")
187-
.setTools(List.of(readFileTool))
188-
).get();
189174
```
190175
191176
## Best practices
@@ -194,5 +179,5 @@ var session = client.createSession(
194179
2. **Unwrap `ExecutionException`**: Call `getCause()` to inspect the real error — the outer `ExecutionException` is just a `CompletableFuture` wrapper.
195180
3. **Restore interrupt flag**: When catching `InterruptedException`, call `Thread.currentThread().interrupt()` to preserve the interrupted status.
196181
4. **Set timeouts**: Use `get(timeout, TimeUnit)` instead of bare `get()` for any call that could block indefinitely.
197-
5. **Return tool errors, don't throw**: Return an error string from the `CompletableFuture` so the model can recover gracefully.
182+
5. **Return tool errors, don't throw**: Use `ToolResultObject.error()` so the model can recover gracefully.
198183
6. **Log errors**: Capture error details for debugging — consider a logging framework like SLF4J for production applications.

cookbook/copilot-sdk/java/multiple-sessions.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public class MultipleSessions {
4848
session3.sendAndWait(new MessageOptions().setPrompt("How do I initialize a module?")).get();
4949
5050
// Clean up all sessions
51-
session1.close();
52-
session2.close();
53-
session3.close();
51+
session1.destroy().get();
52+
session2.destroy().get();
53+
session3.destroy().get();
5454
}
5555
}
5656
}
@@ -136,7 +136,7 @@ var session = client.createSession(new SessionConfig()
136136
137137
session.sendAndWait(new MessageOptions().setPrompt("Hello!")).get();
138138
139-
session.close();
139+
session.destroy().get();
140140
client.stop().get();
141141
executor.shutdown();
142142
```

cookbook/copilot-sdk/java/recipe/AccessibilityReport.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ public static void main(String[] args) throws Exception {
4343
client.start().get();
4444

4545
// Configure Playwright MCP server for browser automation
46-
Map<String, Object> mcpConfig = Map.of(
47-
"type", "local",
48-
"command", "npx",
49-
"args", List.of("@playwright/mcp@latest"),
50-
"tools", List.of("*")
51-
);
46+
var mcpConfig = new McpServerConfig()
47+
.setType("local")
48+
.setCommand("npx")
49+
.setArgs(List.of("@playwright/mcp@latest"))
50+
.setTools(List.of("*"));
5251

5352
var session = client.createSession(
5453
new SessionConfig()

cookbook/copilot-sdk/java/recipe/MultipleSessions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public static void main(String[] args) throws Exception {
3030
System.out.println("S3: " + s3.sendAndWait(new MessageOptions().setPrompt("Explain pattern matching")).get().getData().content());
3131

3232
// Clean up
33-
s1.close(); s2.close(); s3.close();
33+
s1.destroy().get(); s2.destroy().get(); s3.destroy().get();
34+
client.stop().get();
3435
}
3536
}
3637
}

0 commit comments

Comments
 (0)