Skip to content

Commit 09f69ed

Browse files
committed
Add session hooks documentation and update site navigation
1 parent 8208056 commit 09f69ed

4 files changed

Lines changed: 594 additions & 8 deletions

File tree

src/site/markdown/advanced.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,32 @@ var session = client.createSession(
296296

297297
---
298298

299+
## Session Hooks
300+
301+
Intercept tool execution and session lifecycle events using hooks.
302+
303+
```java
304+
var hooks = new SessionHooks()
305+
.setOnPreToolUse((input, invocation) -> {
306+
System.out.println("Tool: " + input.getToolName());
307+
return CompletableFuture.completedFuture(
308+
new PreToolUseHookOutput().setPermissionDecision("allow")
309+
);
310+
})
311+
.setOnPostToolUse((input, invocation) -> {
312+
System.out.println("Result: " + input.getToolResult());
313+
return CompletableFuture.completedFuture(null);
314+
});
315+
316+
var session = client.createSession(
317+
new SessionConfig().setHooks(hooks)
318+
).get();
319+
```
320+
321+
📖 **[Full Session Hooks documentation →](hooks.html)** for all 5 hook types, inputs/outputs, and examples.
322+
323+
---
324+
299325
## Manual Server Control
300326

301327
Control the CLI lifecycle yourself instead of auto-start.

src/site/markdown/documentation.md

Lines changed: 149 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ This guide covers common use cases for the Copilot SDK for Java. For complete AP
88

99
- [Basic Usage](#Basic_Usage)
1010
- [Handling Responses](#Handling_Responses)
11+
- [Event Types Reference](#Event_Types_Reference)
1112
- [Streaming Responses](#Streaming_Responses)
13+
- [Session Operations](#Session_Operations)
1214
- [Choosing a Model](#Choosing_a_Model)
1315
- [Session Management](#Session_Management)
1416

@@ -99,15 +101,83 @@ session.on(event -> {
99101
| `AssistantMessageDeltaEvent` | Streaming chunk (when streaming enabled) |
100102
| `SessionIdleEvent` | Session finished processing |
101103
| `SessionErrorEvent` | An error occurred |
102-
| `SessionShutdownEvent` | Session is shutting down (with reason and exit code) |
103-
| `ToolExecutionStartEvent` | Tool invocation started |
104-
| `ToolExecutionCompleteEvent` | Tool invocation completed |
105-
| `SkillInvokedEvent` | A skill was invoked |
106-
| `SessionCompactionStartEvent` | Context compaction started (infinite sessions) |
107-
| `SessionCompactionCompleteEvent` | Context compaction completed |
108-
| `SessionUsageInfoEvent` | Token usage information |
109104

110-
See the [events package Javadoc](apidocs/com/github/copilot/sdk/events/package-summary.html) for all event types.
105+
For the complete list of all 33 event types, see [Event Types Reference](#Event_Types_Reference) below.
106+
107+
---
108+
109+
## Event Types Reference
110+
111+
The SDK supports 33 event types organized by category. All events extend `AbstractSessionEvent`.
112+
113+
### Session Events
114+
115+
| Event | Type String | Description |
116+
|-------|-------------|-------------|
117+
| `SessionStartEvent` | `session.start` | Session has started |
118+
| `SessionResumeEvent` | `session.resume` | Session was resumed |
119+
| `SessionIdleEvent` | `session.idle` | Session finished processing, ready for input |
120+
| `SessionErrorEvent` | `session.error` | An error occurred in the session |
121+
| `SessionInfoEvent` | `session.info` | Informational message from the session |
122+
| `SessionShutdownEvent` | `session.shutdown` | Session is shutting down (includes reason and exit code) |
123+
| `SessionModelChangeEvent` | `session.model_change` | The model was changed mid-session |
124+
| `SessionHandoffEvent` | `session.handoff` | Session handed off to another agent |
125+
| `SessionTruncationEvent` | `session.truncation` | Context was truncated due to limits |
126+
| `SessionSnapshotRewindEvent` | `session.snapshot_rewind` | Session rewound to a previous snapshot |
127+
| `SessionUsageInfoEvent` | `session.usage_info` | Token usage information |
128+
| `SessionCompactionStartEvent` | `session.compaction_start` | Context compaction started (infinite sessions) |
129+
| `SessionCompactionCompleteEvent` | `session.compaction_complete` | Context compaction completed |
130+
131+
### Assistant Events
132+
133+
| Event | Type String | Description |
134+
|-------|-------------|-------------|
135+
| `AssistantTurnStartEvent` | `assistant.turn_start` | Assistant began processing |
136+
| `AssistantIntentEvent` | `assistant.intent` | Assistant's detected intent |
137+
| `AssistantReasoningEvent` | `assistant.reasoning` | Full reasoning content (reasoning models) |
138+
| `AssistantReasoningDeltaEvent` | `assistant.reasoning_delta` | Streaming reasoning chunk |
139+
| `AssistantMessageEvent` | `assistant.message` | Complete assistant message |
140+
| `AssistantMessageDeltaEvent` | `assistant.message_delta` | Streaming message chunk |
141+
| `AssistantTurnEndEvent` | `assistant.turn_end` | Assistant finished processing |
142+
| `AssistantUsageEvent` | `assistant.usage` | Token usage for this turn |
143+
144+
### Tool Events
145+
146+
| Event | Type String | Description |
147+
|-------|-------------|-------------|
148+
| `ToolUserRequestedEvent` | `tool.user_requested` | User requested a tool invocation |
149+
| `ToolExecutionStartEvent` | `tool.execution_start` | Tool execution started |
150+
| `ToolExecutionProgressEvent` | `tool.execution_progress` | Tool execution progress update |
151+
| `ToolExecutionPartialResultEvent` | `tool.execution_partial_result` | Partial result from tool |
152+
| `ToolExecutionCompleteEvent` | `tool.execution_complete` | Tool execution completed |
153+
154+
### User Events
155+
156+
| Event | Type String | Description |
157+
|-------|-------------|-------------|
158+
| `UserMessageEvent` | `user.message` | User sent a message |
159+
| `PendingMessagesModifiedEvent` | `pending_messages.modified` | Pending message queue changed |
160+
161+
### Subagent Events
162+
163+
| Event | Type String | Description |
164+
|-------|-------------|-------------|
165+
| `SubagentStartedEvent` | `subagent.started` | Subagent was spawned |
166+
| `SubagentSelectedEvent` | `subagent.selected` | Subagent was selected for task |
167+
| `SubagentCompletedEvent` | `subagent.completed` | Subagent completed its task |
168+
| `SubagentFailedEvent` | `subagent.failed` | Subagent failed |
169+
170+
### Other Events
171+
172+
| Event | Type String | Description |
173+
|-------|-------------|-------------|
174+
| `AbortEvent` | `abort` | Operation was aborted |
175+
| `HookStartEvent` | `hook.start` | Hook execution started |
176+
| `HookEndEvent` | `hook.end` | Hook execution completed |
177+
| `SystemMessageEvent` | `system.message` | System-level message |
178+
| `SkillInvokedEvent` | `skill.invoked` | A skill was invoked |
179+
180+
See the [events package Javadoc](apidocs/com/github/copilot/sdk/events/package-summary.html) for detailed event data structures.
111181

112182
---
113183

@@ -140,6 +210,76 @@ done.get();
140210

141211
---
142212

213+
## Session Operations
214+
215+
### Get Conversation History
216+
217+
Retrieve all messages and events from a session using `getMessages()`:
218+
219+
```java
220+
List<AbstractSessionEvent> history = session.getMessages().get();
221+
222+
for (AbstractSessionEvent event : history) {
223+
switch (event) {
224+
case UserMessageEvent user ->
225+
System.out.println("User: " + user.getData().getContent());
226+
case AssistantMessageEvent assistant ->
227+
System.out.println("Assistant: " + assistant.getData().getContent());
228+
case ToolExecutionCompleteEvent tool ->
229+
System.out.println("Tool: " + tool.getData().getToolName());
230+
default -> { }
231+
}
232+
}
233+
```
234+
235+
This is useful for:
236+
- Displaying conversation history in a UI
237+
- Persisting conversations for later review
238+
- Debugging and logging session state
239+
240+
### Abort Current Operation
241+
242+
Cancel a long-running operation using `abort()`:
243+
244+
```java
245+
// Start a potentially long operation
246+
var messageFuture = session.send("Analyze this large codebase...");
247+
248+
// User clicks cancel button
249+
session.abort().get();
250+
251+
// The session will emit an AbortEvent
252+
session.on(AbortEvent.class, evt -> {
253+
System.out.println("Operation was cancelled");
254+
});
255+
```
256+
257+
Use cases:
258+
- User-initiated cancellation in interactive applications
259+
- Timeout handling in automated pipelines
260+
- Graceful shutdown when application is closing
261+
262+
### Custom Timeout
263+
264+
Use `sendAndWait` with a custom timeout for CI/CD pipelines:
265+
266+
```java
267+
try {
268+
// 30-second timeout
269+
var response = session.sendAndWait(
270+
new MessageOptions().setPrompt("Quick question"),
271+
30000 // timeout in milliseconds
272+
).get();
273+
} catch (ExecutionException e) {
274+
if (e.getCause() instanceof TimeoutException) {
275+
System.err.println("Request timed out");
276+
session.abort().get();
277+
}
278+
}
279+
```
280+
281+
---
282+
143283
## Choosing a Model
144284

145285
### List Available Models
@@ -210,5 +350,6 @@ client.deleteSession(sessionId).get();
210350
## Next Steps
211351

212352
- 📖 **[Advanced Usage](advanced.html)** - Tools, BYOK, MCP Servers, System Messages, Infinite Sessions, Skills
353+
- 📖 **[Session Hooks](hooks.html)** - Intercept tool execution and session lifecycle events
213354
- 📖 **[MCP Servers](mcp.html)** - Integrate external tools via Model Context Protocol
214355
- 📖 **[API Javadoc](apidocs/index.html)** - Complete API reference

0 commit comments

Comments
 (0)