// Track cancellation per session
Map<String, Boolean> cancelledSessions = new ConcurrentHashMap<>();
AcpSyncAgent agent = AcpAgent.sync(transport)
.cancelHandler(notification -> {
// Set flag — no response needed (notification, not request)
cancelledSessions.put(notification.sessionId(), true);
})
.promptHandler((req, context) -> {
cancelledSessions.put(req.sessionId(), false);
for (int i = 1; i <= 10; i++) {
// Check flag before each step
if (cancelledSessions.getOrDefault(req.sessionId(), false)) {
context.sendMessage("[Cancelled at step " + i + "]");
return PromptResponse.endTurn();
}
context.sendMessage("Step " + i + "/10... ");
Thread.sleep(500);
}
context.sendMessage("All steps completed!");
return PromptResponse.endTurn();
})
.build();