mirror of
https://github.com/github/awesome-copilot.git
synced 2026-04-11 02:35:55 +00:00
Add Java SDK cookbook with 7 recipes
Add complete Java cookbook matching the pattern of existing .NET, Go, Node.js, and Python cookbooks. All 7 recipes included: - Ralph Loop: Autonomous AI task loops with JBang - Error Handling: try-with-resources, ExecutionException, timeouts - Multiple Sessions: Parallel sessions with CompletableFuture - Managing Local Files: AI-powered file organization - PR Visualization: Interactive PR age charts - Persisting Sessions: Save/resume with custom IDs - Accessibility Report: WCAG reports via Playwright MCP Each recipe includes both markdown documentation and a standalone JBang-runnable Java file in recipe/.
This commit is contained in:
55
cookbook/copilot-sdk/java/recipe/RalphLoop.java
Normal file
55
cookbook/copilot-sdk/java/recipe/RalphLoop.java
Normal file
@@ -0,0 +1,55 @@
|
||||
///usr/bin/env jbang "$0" "$@" ; exit $?
|
||||
//DEPS com.github:copilot-sdk-java:0.2.1-java.1
|
||||
|
||||
import com.github.copilot.sdk.*;
|
||||
import com.github.copilot.sdk.events.*;
|
||||
import com.github.copilot.sdk.json.*;
|
||||
import java.nio.file.*;
|
||||
|
||||
/**
|
||||
* Simple Ralph Loop — reads PROMPT.md and runs it in a fresh session each iteration.
|
||||
*
|
||||
* Usage:
|
||||
* jbang RalphLoop.java # defaults: PROMPT.md, 50 iterations
|
||||
* jbang RalphLoop.java PROMPT.md 20 # custom prompt file, 20 iterations
|
||||
*/
|
||||
public class RalphLoop {
|
||||
public static void main(String[] args) throws Exception {
|
||||
String promptFile = args.length > 0 ? args[0] : "PROMPT.md";
|
||||
int maxIterations = args.length > 1 ? Integer.parseInt(args[1]) : 50;
|
||||
|
||||
System.out.printf("Ralph Loop — prompt: %s, max iterations: %d%n", promptFile, maxIterations);
|
||||
|
||||
try (var client = new CopilotClient()) {
|
||||
client.start().get();
|
||||
|
||||
String prompt = Files.readString(Path.of(promptFile));
|
||||
|
||||
for (int i = 1; i <= maxIterations; i++) {
|
||||
System.out.printf("%n=== Iteration %d/%d ===%n", i, maxIterations);
|
||||
|
||||
// Fresh session each iteration — context isolation is the point
|
||||
var session = client.createSession(
|
||||
new SessionConfig()
|
||||
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
|
||||
.setModel("gpt-5.1-codex-mini")
|
||||
.setWorkingDirectory(System.getProperty("user.dir"))
|
||||
).get();
|
||||
|
||||
// Log tool usage for visibility
|
||||
session.on(ToolExecutionStartEvent.class,
|
||||
ev -> System.out.printf(" ⚙ %s%n", ev.getData().toolName()));
|
||||
|
||||
try {
|
||||
session.sendAndWait(new MessageOptions().setPrompt(prompt)).get();
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
|
||||
System.out.printf("Iteration %d complete.%n", i);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\nAll iterations complete.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user