Skip to content

Commit 7c6c0be

Browse files
authored
Merge pull request #1313 from brunoborges/cookbook/java
Add Java SDK cookbook with 7 recipes
2 parents 0c3c5bb + c330a7d commit 7c6c0be

18 files changed

+2223
-2
lines changed

cookbook/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The cookbook is organized by tool or product, with recipes collected by language
1010

1111
Ready-to-use recipes for building with the GitHub Copilot SDK across multiple languages.
1212

13-
- **[Copilot SDK Cookbook](copilot-sdk/)** - Recipes for .NET, Go, Node.js, and Python
13+
- **[Copilot SDK Cookbook](copilot-sdk/)** - Recipes for .NET, Go, Java, Node.js, and Python
1414
- Error handling, session management, file operations, and more
1515
- Runnable examples for each language
1616
- Best practices and complete implementation guides

cookbook/copilot-sdk/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ This cookbook collects small, focused recipes showing how to accomplish common t
4444
- [Persisting Sessions](go/persisting-sessions.md): Save and resume sessions across restarts.
4545
- [Accessibility Report](go/accessibility-report.md): Generate WCAG accessibility reports using the Playwright MCP server.
4646

47+
### Java
48+
49+
- [Ralph Loop](java/ralph-loop.md): Build autonomous AI coding loops with fresh context per iteration, planning/building modes, and backpressure.
50+
- [Error Handling](java/error-handling.md): Handle errors gracefully including connection failures, timeouts, and cleanup.
51+
- [Multiple Sessions](java/multiple-sessions.md): Manage multiple independent conversations simultaneously.
52+
- [Managing Local Files](java/managing-local-files.md): Organize files by metadata using AI-powered grouping strategies.
53+
- [PR Visualization](java/pr-visualization.md): Generate interactive PR age charts using GitHub MCP Server.
54+
- [Persisting Sessions](java/persisting-sessions.md): Save and resume sessions across restarts.
55+
- [Accessibility Report](java/accessibility-report.md): Generate WCAG accessibility reports using the Playwright MCP server.
56+
4757
## How to Use
4858

4959
- Browse your language section above and open the recipe links
@@ -84,11 +94,18 @@ cd go/cookbook/recipe
8494
go run <filename>.go
8595
```
8696

97+
### Java
98+
99+
```bash
100+
cd java/recipe
101+
jbang <FileName>.java
102+
```
103+
87104
## Contributing
88105

89106
- Propose or add a new recipe by creating a markdown file in your language's `cookbook/` folder and a runnable example in `recipe/`
90107
- Follow repository guidance in [CONTRIBUTING.md](../../CONTRIBUTING.md)
91108

92109
## Status
93110

94-
Cookbook structure is complete with 7 recipes across all 4 supported languages. Each recipe includes both markdown documentation and runnable examples.
111+
Cookbook structure is complete with 7 recipes across all 5 supported languages. Each recipe includes both markdown documentation and runnable examples.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# GitHub Copilot SDK Cookbook — Java
2+
3+
This folder hosts short, practical recipes for using the GitHub Copilot SDK with Java. Each recipe is concise, copy‑pasteable, and points to fuller examples and tests. All examples can be run directly with [JBang](https://www.jbang.dev/).
4+
5+
## Recipes
6+
7+
- [Ralph Loop](ralph-loop.md): Build autonomous AI coding loops with fresh context per iteration, planning/building modes, and backpressure.
8+
- [Error Handling](error-handling.md): Handle errors gracefully including connection failures, timeouts, and cleanup.
9+
- [Multiple Sessions](multiple-sessions.md): Manage multiple independent conversations simultaneously.
10+
- [Managing Local Files](managing-local-files.md): Organize files by metadata using AI-powered grouping strategies.
11+
- [PR Visualization](pr-visualization.md): Generate interactive PR age charts using GitHub MCP Server.
12+
- [Persisting Sessions](persisting-sessions.md): Save and resume sessions across restarts.
13+
- [Accessibility Report](accessibility-report.md): Generate WCAG accessibility reports using the Playwright MCP server.
14+
15+
## Contributing
16+
17+
Add a new recipe by creating a markdown file in this folder and linking it above. Follow repository guidance in [CONTRIBUTING.md](../../../CONTRIBUTING.md).
18+
19+
## Status
20+
21+
These recipes are complete, practical examples and can be used directly or adapted for your own projects.
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
# Generating Accessibility Reports
2+
3+
Build a CLI tool that analyzes web page accessibility using the Playwright MCP server and generates detailed WCAG-compliant reports with optional test generation.
4+
5+
> **Runnable example:** [recipe/AccessibilityReport.java](recipe/AccessibilityReport.java)
6+
>
7+
> ```bash
8+
> jbang recipe/AccessibilityReport.java
9+
> ```
10+
11+
## Example scenario
12+
13+
You want to audit a website's accessibility compliance. This tool navigates to a URL using Playwright, captures an accessibility snapshot, and produces a structured report covering WCAG criteria like landmarks, heading hierarchy, focus management, and touch targets. It can also generate Playwright test files to automate future accessibility checks.
14+
15+
## Prerequisites
16+
17+
Install [JBang](https://www.jbang.dev/) and ensure `npx` is available (Node.js installed) for the Playwright MCP server:
18+
19+
```bash
20+
# macOS (using Homebrew)
21+
brew install jbangdev/tap/jbang
22+
23+
# Verify npx is available (needed for Playwright MCP)
24+
npx --version
25+
```
26+
27+
## Usage
28+
29+
```bash
30+
jbang recipe/AccessibilityReport.java
31+
# Enter a URL when prompted
32+
```
33+
34+
## Full example: AccessibilityReport.java
35+
36+
```java
37+
///usr/bin/env jbang "$0" "$@" ; exit $?
38+
//DEPS com.github:copilot-sdk-java:0.2.1-java.1
39+
40+
import com.github.copilot.sdk.*;
41+
import com.github.copilot.sdk.events.*;
42+
import com.github.copilot.sdk.json.*;
43+
import java.io.*;
44+
import java.util.*;
45+
import java.util.concurrent.*;
46+
47+
public class AccessibilityReport {
48+
public static void main(String[] args) throws Exception {
49+
System.out.println("=== Accessibility Report Generator ===\n");
50+
51+
var reader = new BufferedReader(new InputStreamReader(System.in));
52+
53+
System.out.print("Enter URL to analyze: ");
54+
String url = reader.readLine().trim();
55+
if (url.isEmpty()) {
56+
System.out.println("No URL provided. Exiting.");
57+
return;
58+
}
59+
if (!url.startsWith("http://") && !url.startsWith("https://")) {
60+
url = "https://" + url;
61+
}
62+
63+
System.out.printf("%nAnalyzing: %s%n", url);
64+
System.out.println("Please wait...\n");
65+
66+
try (var client = new CopilotClient()) {
67+
client.start().get();
68+
69+
// Configure Playwright MCP server for browser automation
70+
var mcpConfig = new McpServerConfig()
71+
.setType("local")
72+
.setCommand("npx")
73+
.setArgs(List.of("@playwright/mcp@latest"))
74+
.setTools(List.of("*"));
75+
76+
var session = client.createSession(
77+
new SessionConfig()
78+
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
79+
.setModel("claude-opus-4.6")
80+
.setStreaming(true)
81+
.setMcpServers(Map.of("playwright", mcpConfig))
82+
).get();
83+
84+
// Stream output token-by-token
85+
var idleLatch = new CountDownLatch(1);
86+
87+
session.on(AssistantMessageDeltaEvent.class,
88+
ev -> System.out.print(ev.getData().deltaContent()));
89+
90+
session.on(SessionIdleEvent.class,
91+
ev -> idleLatch.countDown());
92+
93+
session.on(SessionErrorEvent.class, ev -> {
94+
System.err.printf("%nError: %s%n", ev.getData().message());
95+
idleLatch.countDown();
96+
});
97+
98+
String prompt = """
99+
Use the Playwright MCP server to analyze the accessibility of this webpage: %s
100+
101+
Please:
102+
1. Navigate to the URL using playwright-browser_navigate
103+
2. Take an accessibility snapshot using playwright-browser_snapshot
104+
3. Analyze the snapshot and provide a detailed accessibility report
105+
106+
Format the report with emoji indicators:
107+
- 📊 Accessibility Report header
108+
- ✅ What's Working Well (table with Category, Status, Details)
109+
- ⚠️ Issues Found (table with Severity, Issue, WCAG Criterion, Recommendation)
110+
- 📋 Stats Summary (links, headings, focusable elements, landmarks)
111+
- ⚙️ Priority Recommendations
112+
113+
Use ✅ for pass, 🔴 for high severity issues, 🟡 for medium severity, ❌ for missing items.
114+
Include actual findings from the page analysis.
115+
""".formatted(url);
116+
117+
session.send(new MessageOptions().setPrompt(prompt));
118+
idleLatch.await();
119+
120+
System.out.println("\n\n=== Report Complete ===\n");
121+
122+
// Prompt user for test generation
123+
System.out.print("Would you like to generate Playwright accessibility tests? (y/n): ");
124+
String generateTests = reader.readLine().trim();
125+
126+
if (generateTests.equalsIgnoreCase("y") || generateTests.equalsIgnoreCase("yes")) {
127+
var testLatch = new CountDownLatch(1);
128+
129+
session.on(SessionIdleEvent.class,
130+
ev -> testLatch.countDown());
131+
132+
String testPrompt = """
133+
Based on the accessibility report you just generated for %s,
134+
create Playwright accessibility tests in Java.
135+
136+
Include tests for: lang attribute, title, heading hierarchy, alt text,
137+
landmarks, skip navigation, focus indicators, and touch targets.
138+
Use Playwright's accessibility testing features with helpful comments.
139+
Output the complete test file.
140+
""".formatted(url);
141+
142+
System.out.println("\nGenerating accessibility tests...\n");
143+
session.send(new MessageOptions().setPrompt(testPrompt));
144+
testLatch.await();
145+
146+
System.out.println("\n\n=== Tests Generated ===");
147+
}
148+
149+
session.close();
150+
}
151+
}
152+
}
153+
```
154+
155+
## How it works
156+
157+
1. **Playwright MCP server**: Configures a local MCP server running `@playwright/mcp` to provide browser automation tools
158+
2. **Streaming output**: Uses `streaming: true` and `AssistantMessageDeltaEvent` for real-time token-by-token output
159+
3. **Accessibility snapshot**: Playwright's `browser_snapshot` tool captures the full accessibility tree of the page
160+
4. **Structured report**: The prompt engineers a consistent WCAG-aligned report format with emoji severity indicators
161+
5. **Test generation**: Optionally generates Playwright accessibility tests based on the analysis
162+
163+
## Key concepts
164+
165+
### MCP server configuration
166+
167+
The recipe configures a local MCP server that runs alongside the session:
168+
169+
```java
170+
var mcpConfig = new McpServerConfig()
171+
.setType("local")
172+
.setCommand("npx")
173+
.setArgs(List.of("@playwright/mcp@latest"))
174+
.setTools(List.of("*"));
175+
176+
var session = client.createSession(
177+
new SessionConfig()
178+
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
179+
.setMcpServers(Map.of("playwright", mcpConfig))
180+
).get();
181+
```
182+
183+
This gives the model access to Playwright browser tools like `browser_navigate`, `browser_snapshot`, and `browser_click`.
184+
185+
### Streaming with events
186+
187+
Unlike `sendAndWait`, this recipe uses streaming for real-time output:
188+
189+
```java
190+
session.on(AssistantMessageDeltaEvent.class,
191+
ev -> System.out.print(ev.getData().deltaContent()));
192+
193+
session.on(SessionIdleEvent.class,
194+
ev -> idleLatch.countDown());
195+
```
196+
197+
A `CountDownLatch` synchronizes the main thread with the async event stream — when the session becomes idle, the latch releases and the program continues.
198+
199+
## Sample interaction
200+
201+
```
202+
=== Accessibility Report Generator ===
203+
204+
Enter URL to analyze: github.com
205+
206+
Analyzing: https://github.com
207+
Please wait...
208+
209+
📊 Accessibility Report: GitHub (github.com)
210+
211+
✅ What's Working Well
212+
| Category | Status | Details |
213+
|----------|--------|---------|
214+
| Language | ✅ Pass | lang="en" properly set |
215+
| Page Title | ✅ Pass | "GitHub" is recognizable |
216+
| Heading Hierarchy | ✅ Pass | Proper H1/H2 structure |
217+
| Images | ✅ Pass | All images have alt text |
218+
219+
⚠️ Issues Found
220+
| Severity | Issue | WCAG Criterion | Recommendation |
221+
|----------|-------|----------------|----------------|
222+
| 🟡 Medium | Some links lack descriptive text | 2.4.4 | Add aria-label to icon-only links |
223+
224+
📋 Stats Summary
225+
- Total Links: 47
226+
- Total Headings: 8 (1× H1, proper hierarchy)
227+
- Focusable Elements: 52
228+
- Landmarks Found: banner ✅, navigation ✅, main ✅, footer ✅
229+
230+
=== Report Complete ===
231+
232+
Would you like to generate Playwright accessibility tests? (y/n): y
233+
234+
Generating accessibility tests...
235+
[Generated test file output...]
236+
237+
=== Tests Generated ===
238+
```

0 commit comments

Comments
 (0)