Skip to content

Commit e1f966d

Browse files
authored
Merge pull request #1316 from github/cookbook/java-to-staged
Add Java SDK cookbook with 7 recipes (re-targeted to staged)
2 parents 6dd2453 + e447f7d commit e1f966d

18 files changed

+2239
-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: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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+
Map<String, Object> mcpConfig = Map.of(
71+
"type", "local",
72+
"command", "npx",
73+
"args", List.of("@playwright/mcp@latest"),
74+
"tools", List.of("*")
75+
);
76+
77+
var session = client.createSession(
78+
new SessionConfig()
79+
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
80+
.setModel("claude-opus-4.6")
81+
.setStreaming(true)
82+
.setMcpServers(Map.of("playwright", mcpConfig))
83+
).get();
84+
85+
// Stream output token-by-token
86+
var idleLatch = new CountDownLatch(1);
87+
88+
session.on(AssistantMessageDeltaEvent.class,
89+
ev -> System.out.print(ev.getData().deltaContent()));
90+
91+
session.on(SessionIdleEvent.class,
92+
ev -> idleLatch.countDown());
93+
94+
session.on(SessionErrorEvent.class, ev -> {
95+
System.err.printf("%nError: %s%n", ev.getData().message());
96+
idleLatch.countDown();
97+
});
98+
99+
String prompt = """
100+
Use the Playwright MCP server to analyze the accessibility of this webpage: %s
101+
102+
Please:
103+
1. Navigate to the URL using playwright-browser_navigate
104+
2. Take an accessibility snapshot using playwright-browser_snapshot
105+
3. Analyze the snapshot and provide a detailed accessibility report
106+
107+
Format the report with emoji indicators:
108+
- 📊 Accessibility Report header
109+
- ✅ What's Working Well (table with Category, Status, Details)
110+
- ⚠️ Issues Found (table with Severity, Issue, WCAG Criterion, Recommendation)
111+
- 📋 Stats Summary (links, headings, focusable elements, landmarks)
112+
- ⚙️ Priority Recommendations
113+
114+
Use ✅ for pass, 🔴 for high severity issues, 🟡 for medium severity, ❌ for missing items.
115+
Include actual findings from the page analysis.
116+
""".formatted(url);
117+
118+
session.send(new MessageOptions().setPrompt(prompt));
119+
idleLatch.await();
120+
121+
System.out.println("\n\n=== Report Complete ===\n");
122+
123+
// Prompt user for test generation
124+
System.out.print("Would you like to generate Playwright accessibility tests? (y/n): ");
125+
String generateTests = reader.readLine().trim();
126+
127+
if (generateTests.equalsIgnoreCase("y") || generateTests.equalsIgnoreCase("yes")) {
128+
var testLatch = new CountDownLatch(1);
129+
130+
session.on(SessionIdleEvent.class,
131+
ev -> testLatch.countDown());
132+
133+
String testPrompt = """
134+
Based on the accessibility report you just generated for %s,
135+
create Playwright accessibility tests in Java.
136+
137+
Include tests for: lang attribute, title, heading hierarchy, alt text,
138+
landmarks, skip navigation, focus indicators, and touch targets.
139+
Use Playwright's accessibility testing features with helpful comments.
140+
Output the complete test file.
141+
""".formatted(url);
142+
143+
System.out.println("\nGenerating accessibility tests...\n");
144+
session.send(new MessageOptions().setPrompt(testPrompt));
145+
testLatch.await();
146+
147+
System.out.println("\n\n=== Tests Generated ===");
148+
}
149+
150+
session.close();
151+
}
152+
}
153+
}
154+
```
155+
156+
## How it works
157+
158+
1. **Playwright MCP server**: Configures a local MCP server running `@playwright/mcp` to provide browser automation tools
159+
2. **Streaming output**: Uses `streaming: true` and `AssistantMessageDeltaEvent` for real-time token-by-token output
160+
3. **Accessibility snapshot**: Playwright's `browser_snapshot` tool captures the full accessibility tree of the page
161+
4. **Structured report**: The prompt engineers a consistent WCAG-aligned report format with emoji severity indicators
162+
5. **Test generation**: Optionally generates Playwright accessibility tests based on the analysis
163+
164+
## Key concepts
165+
166+
### MCP server configuration
167+
168+
The recipe configures a local MCP server that runs alongside the session:
169+
170+
```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+
);
177+
178+
var session = client.createSession(
179+
new SessionConfig()
180+
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
181+
.setMcpServers(Map.of("playwright", mcpConfig))
182+
).get();
183+
```
184+
185+
This gives the model access to Playwright browser tools like `browser_navigate`, `browser_snapshot`, and `browser_click`.
186+
187+
### Streaming with events
188+
189+
Unlike `sendAndWait`, this recipe uses streaming for real-time output:
190+
191+
```java
192+
session.on(AssistantMessageDeltaEvent.class,
193+
ev -> System.out.print(ev.getData().deltaContent()));
194+
195+
session.on(SessionIdleEvent.class,
196+
ev -> idleLatch.countDown());
197+
```
198+
199+
A `CountDownLatch` synchronizes the main thread with the async event stream — when the session becomes idle, the latch releases and the program continues.
200+
201+
## Sample interaction
202+
203+
```
204+
=== Accessibility Report Generator ===
205+
206+
Enter URL to analyze: github.com
207+
208+
Analyzing: https://github.com
209+
Please wait...
210+
211+
📊 Accessibility Report: GitHub (github.com)
212+
213+
✅ What's Working Well
214+
| Category | Status | Details |
215+
|----------|--------|---------|
216+
| Language | ✅ Pass | lang="en" properly set |
217+
| Page Title | ✅ Pass | "GitHub" is recognizable |
218+
| Heading Hierarchy | ✅ Pass | Proper H1/H2 structure |
219+
| Images | ✅ Pass | All images have alt text |
220+
221+
⚠️ Issues Found
222+
| Severity | Issue | WCAG Criterion | Recommendation |
223+
|----------|-------|----------------|----------------|
224+
| 🟡 Medium | Some links lack descriptive text | 2.4.4 | Add aria-label to icon-only links |
225+
226+
📋 Stats Summary
227+
- Total Links: 47
228+
- Total Headings: 8 (1× H1, proper hierarchy)
229+
- Focusable Elements: 52
230+
- Landmarks Found: banner ✅, navigation ✅, main ✅, footer ✅
231+
232+
=== Report Complete ===
233+
234+
Would you like to generate Playwright accessibility tests? (y/n): y
235+
236+
Generating accessibility tests...
237+
[Generated test file output...]
238+
239+
=== Tests Generated ===
240+
```

0 commit comments

Comments
 (0)