|
59 | 59 | * |
60 | 60 | * <pre>{@code |
61 | 61 | * // Create a session |
62 | | - * CopilotSession session = client.createSession(new SessionConfig().setModel("gpt-5")).get(); |
| 62 | + * var session = client.createSession(new SessionConfig().setModel("gpt-5")).get(); |
63 | 63 | * |
64 | | - * // Register event handlers |
65 | | - * session.on(evt -> { |
66 | | - * if (evt instanceof AssistantMessageEvent msg) { |
67 | | - * System.out.println(msg.getData().getContent()); |
68 | | - * } |
| 64 | + * // Register type-safe event handlers |
| 65 | + * session.on(AssistantMessageEvent.class, msg -> { |
| 66 | + * System.out.println(msg.getData().getContent()); |
| 67 | + * }); |
| 68 | + * session.on(SessionIdleEvent.class, idle -> { |
| 69 | + * System.out.println("Session is idle"); |
69 | 70 | * }); |
70 | 71 | * |
71 | 72 | * // Send messages |
@@ -288,28 +289,26 @@ public CompletableFuture<AssistantMessageEvent> sendAndWait(MessageOptions optio |
288 | 289 | } |
289 | 290 |
|
290 | 291 | /** |
291 | | - * Registers a callback for session events. |
| 292 | + * Registers a callback for all session events. |
292 | 293 | * <p> |
293 | | - * The handler will be invoked for all events in this session, including |
294 | | - * assistant messages, tool calls, and session state changes. |
| 294 | + * The handler will be invoked for every event in this session, including |
| 295 | + * assistant messages, tool calls, and session state changes. For type-safe |
| 296 | + * handling of specific event types, prefer {@link #on(Class, Consumer)} |
| 297 | + * instead. |
295 | 298 | * |
296 | 299 | * <p> |
297 | 300 | * <b>Example:</b> |
298 | 301 | * |
299 | 302 | * <pre>{@code |
300 | | - * Closeable subscription = session.on(evt -> { |
301 | | - * if (evt instanceof AssistantMessageEvent msg) { |
302 | | - * System.out.println(msg.getData().getContent()); |
303 | | - * } |
304 | | - * }); |
305 | | - * |
306 | | - * // Later, to unsubscribe: |
307 | | - * subscription.close(); |
| 303 | + * // Collect all events |
| 304 | + * var events = new ArrayList<AbstractSessionEvent>(); |
| 305 | + * session.on(events::add); |
308 | 306 | * }</pre> |
309 | 307 | * |
310 | 308 | * @param handler |
311 | 309 | * a callback to be invoked when a session event occurs |
312 | 310 | * @return a Closeable that, when closed, unsubscribes the handler |
| 311 | + * @see #on(Class, Consumer) |
313 | 312 | * @see AbstractSessionEvent |
314 | 313 | */ |
315 | 314 | public Closeable on(Consumer<AbstractSessionEvent> handler) { |
|
0 commit comments