-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCopilotSession.java
More file actions
422 lines (391 loc) · 15.6 KB
/
CopilotSession.java
File metadata and controls
422 lines (391 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.copilot.sdk.events.AbstractSessionEvent;
import com.github.copilot.sdk.events.AssistantMessageEvent;
import com.github.copilot.sdk.events.SessionErrorEvent;
import com.github.copilot.sdk.events.SessionEventParser;
import com.github.copilot.sdk.events.SessionIdleEvent;
import com.github.copilot.sdk.json.GetMessagesResponse;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.PermissionInvocation;
import com.github.copilot.sdk.json.PermissionRequest;
import com.github.copilot.sdk.json.PermissionRequestResult;
import com.github.copilot.sdk.json.SendMessageRequest;
import com.github.copilot.sdk.json.SendMessageResponse;
import com.github.copilot.sdk.json.ToolDefinition;
/**
* Represents a single conversation session with the Copilot CLI.
* <p>
* A session maintains conversation state, handles events, and manages tool
* execution. Sessions are created via {@link CopilotClient#createSession} or
* resumed via {@link CopilotClient#resumeSession}.
*
* <h2>Example Usage</h2>
*
* <pre>{@code
* // Create a session
* CopilotSession session = client.createSession(new SessionConfig().setModel("gpt-5")).get();
*
* // Register event handlers
* session.on(evt -> {
* if (evt instanceof AssistantMessageEvent msg) {
* System.out.println(msg.getData().getContent());
* }
* });
*
* // Send messages
* session.sendAndWait(new MessageOptions().setPrompt("Hello!")).get();
*
* // Clean up
* session.close();
* }</pre>
*
* @see CopilotClient#createSession(com.github.copilot.sdk.json.SessionConfig)
* @see CopilotClient#resumeSession(String,
* com.github.copilot.sdk.json.ResumeSessionConfig)
* @see AbstractSessionEvent
*/
public final class CopilotSession implements AutoCloseable {
private static final Logger LOG = Logger.getLogger(CopilotSession.class.getName());
private static final ObjectMapper MAPPER = JsonRpcClient.getObjectMapper();
private final String sessionId;
private final JsonRpcClient rpc;
private final Set<Consumer<AbstractSessionEvent>> eventHandlers = ConcurrentHashMap.newKeySet();
private final Map<String, ToolDefinition> toolHandlers = new ConcurrentHashMap<>();
private final AtomicReference<PermissionHandler> permissionHandler = new AtomicReference<>();
/**
* Creates a new session with the given ID and RPC client.
* <p>
* This constructor is package-private. Sessions should be created via
* {@link CopilotClient#createSession} or {@link CopilotClient#resumeSession}.
*
* @param sessionId
* the unique session identifier
* @param rpc
* the JSON-RPC client for communication
*/
CopilotSession(String sessionId, JsonRpcClient rpc) {
this.sessionId = sessionId;
this.rpc = rpc;
}
/**
* Gets the unique identifier for this session.
*
* @return the session ID
*/
public String getSessionId() {
return sessionId;
}
/**
* Sends a simple text message to the Copilot session.
* <p>
* This is a convenience method equivalent to
* {@code send(new MessageOptions().setPrompt(prompt))}.
*
* @param prompt
* the message text to send
* @return a future that resolves with the message ID assigned by the server
* @see #send(MessageOptions)
*/
public CompletableFuture<String> send(String prompt) {
return send(new MessageOptions().setPrompt(prompt));
}
/**
* Sends a simple text message and waits until the session becomes idle.
* <p>
* This is a convenience method equivalent to
* {@code sendAndWait(new MessageOptions().setPrompt(prompt))}.
*
* @param prompt
* the message text to send
* @return a future that resolves with the final assistant message event, or
* {@code null} if no assistant message was received
* @see #sendAndWait(MessageOptions)
*/
public CompletableFuture<AssistantMessageEvent> sendAndWait(String prompt) {
return sendAndWait(new MessageOptions().setPrompt(prompt));
}
/**
* Sends a message to the Copilot session.
* <p>
* This method sends a message asynchronously and returns immediately. Use
* {@link #sendAndWait(MessageOptions)} to wait for the response.
*
* @param options
* the message options containing the prompt and attachments
* @return a future that resolves with the message ID assigned by the server
* @see #sendAndWait(MessageOptions)
* @see #send(String)
*/
public CompletableFuture<String> send(MessageOptions options) {
SendMessageRequest request = new SendMessageRequest();
request.setSessionId(sessionId);
request.setPrompt(options.getPrompt());
request.setAttachments(options.getAttachments());
request.setMode(options.getMode());
return rpc.invoke("session.send", request, SendMessageResponse.class)
.thenApply(SendMessageResponse::getMessageId);
}
/**
* Sends a message and waits until the session becomes idle.
* <p>
* This method blocks until the assistant finishes processing the message or
* until the timeout expires. It's suitable for simple request/response
* interactions where you don't need to process streaming events.
*
* @param options
* the message options containing the prompt and attachments
* @param timeoutMs
* timeout in milliseconds (0 or negative for no timeout)
* @return a future that resolves with the final assistant message event, or
* {@code null} if no assistant message was received. The future
* completes exceptionally with a TimeoutException if the timeout
* expires.
* @see #sendAndWait(MessageOptions)
* @see #send(MessageOptions)
*/
public CompletableFuture<AssistantMessageEvent> sendAndWait(MessageOptions options, long timeoutMs) {
CompletableFuture<AssistantMessageEvent> future = new CompletableFuture<>();
AtomicReference<AssistantMessageEvent> lastAssistantMessage = new AtomicReference<>();
Consumer<AbstractSessionEvent> handler = evt -> {
if (evt instanceof AssistantMessageEvent msg) {
lastAssistantMessage.set(msg);
} else if (evt instanceof SessionIdleEvent) {
future.complete(lastAssistantMessage.get());
} else if (evt instanceof SessionErrorEvent errorEvent) {
String message = errorEvent.getData() != null ? errorEvent.getData().getMessage() : "session error";
future.completeExceptionally(new RuntimeException("Session error: " + message));
}
};
Closeable subscription = on(handler);
send(options).exceptionally(ex -> {
try {
subscription.close();
} catch (Exception e) {
LOG.log(Level.SEVERE, "Error closing subscription", e);
}
future.completeExceptionally(ex);
return null;
});
// Set up timeout
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(() -> {
if (!future.isDone()) {
future.completeExceptionally(new TimeoutException("sendAndWait timed out after " + timeoutMs + "ms"));
}
scheduler.shutdown();
}, timeoutMs, TimeUnit.MILLISECONDS);
return future.whenComplete((result, ex) -> {
try {
subscription.close();
} catch (IOException e) {
LOG.log(Level.SEVERE, "Error closing subscription", e);
}
scheduler.shutdown();
});
}
/**
* Sends a message and waits until the session becomes idle with default 60
* second timeout.
*
* @param options
* the message options containing the prompt and attachments
* @return a future that resolves with the final assistant message event, or
* {@code null} if no assistant message was received
* @see #sendAndWait(MessageOptions, long)
*/
public CompletableFuture<AssistantMessageEvent> sendAndWait(MessageOptions options) {
return sendAndWait(options, 60000);
}
/**
* Registers a callback for session events.
* <p>
* The handler will be invoked for all events in this session, including
* assistant messages, tool calls, and session state changes.
*
* <p>
* <b>Example:</b>
*
* <pre>{@code
* Closeable subscription = session.on(evt -> {
* if (evt instanceof AssistantMessageEvent msg) {
* System.out.println(msg.getData().getContent());
* }
* });
*
* // Later, to unsubscribe:
* subscription.close();
* }</pre>
*
* @param handler
* a callback to be invoked when a session event occurs
* @return a Closeable that, when closed, unsubscribes the handler
* @see AbstractSessionEvent
*/
public Closeable on(Consumer<AbstractSessionEvent> handler) {
eventHandlers.add(handler);
return () -> eventHandlers.remove(handler);
}
/**
* Dispatches an event to all registered handlers.
* <p>
* This is called internally when events are received from the server.
*
* @param event
* the event to dispatch
*/
void dispatchEvent(AbstractSessionEvent event) {
for (Consumer<AbstractSessionEvent> handler : eventHandlers) {
try {
handler.accept(event);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Error in event handler", e);
}
}
}
/**
* Registers custom tool handlers for this session.
* <p>
* Called internally when creating or resuming a session with tools.
*
* @param tools
* the list of tool definitions with handlers
*/
void registerTools(List<ToolDefinition> tools) {
toolHandlers.clear();
if (tools != null) {
for (ToolDefinition tool : tools) {
toolHandlers.put(tool.getName(), tool);
}
}
}
/**
* Retrieves a registered tool by name.
*
* @param name
* the tool name
* @return the tool definition, or {@code null} if not found
*/
ToolDefinition getTool(String name) {
return toolHandlers.get(name);
}
/**
* Registers a handler for permission requests.
* <p>
* Called internally when creating or resuming a session with permission
* handling.
*
* @param handler
* the permission handler
*/
void registerPermissionHandler(PermissionHandler handler) {
permissionHandler.set(handler);
}
/**
* Handles a permission request from the Copilot CLI.
* <p>
* Called internally when the server requests permission for an operation.
*
* @param permissionRequestData
* the JSON data for the permission request
* @return a future that resolves with the permission result
*/
CompletableFuture<PermissionRequestResult> handlePermissionRequest(JsonNode permissionRequestData) {
PermissionHandler handler = permissionHandler.get();
if (handler == null) {
PermissionRequestResult result = new PermissionRequestResult();
result.setKind("denied-no-approval-rule-and-could-not-request-from-user");
return CompletableFuture.completedFuture(result);
}
try {
PermissionRequest request = MAPPER.treeToValue(permissionRequestData, PermissionRequest.class);
PermissionInvocation invocation = new PermissionInvocation();
invocation.setSessionId(sessionId);
return handler.handle(request, invocation);
} catch (JsonProcessingException e) {
LOG.log(Level.SEVERE, "Failed to parse permission request", e);
PermissionRequestResult result = new PermissionRequestResult();
result.setKind("denied-no-approval-rule-and-could-not-request-from-user");
return CompletableFuture.completedFuture(result);
}
}
/**
* Gets the complete list of messages and events in the session.
* <p>
* This retrieves the full conversation history, including all user messages,
* assistant responses, tool invocations, and other session events.
*
* @return a future that resolves with a list of all session events
* @see AbstractSessionEvent
*/
public CompletableFuture<List<AbstractSessionEvent>> getMessages() {
return rpc.invoke("session.getMessages", Map.of("sessionId", sessionId), GetMessagesResponse.class)
.thenApply(response -> {
List<AbstractSessionEvent> events = new ArrayList<>();
if (response.getEvents() != null) {
for (JsonNode eventNode : response.getEvents()) {
try {
AbstractSessionEvent event = SessionEventParser.parse(eventNode.toString());
if (event != null) {
events.add(event);
}
} catch (Exception e) {
LOG.log(Level.WARNING, "Failed to parse event", e);
}
}
}
return events;
});
}
/**
* Aborts the currently processing message in this session.
* <p>
* Use this to cancel a long-running operation or stop the assistant from
* continuing to generate a response.
*
* @return a future that completes when the abort is acknowledged
*/
public CompletableFuture<Void> abort() {
return rpc.invoke("session.abort", Map.of("sessionId", sessionId), Void.class);
}
/**
* Disposes the session and releases all associated resources.
* <p>
* This destroys the session on the server, clears all event handlers, and
* releases tool and permission handlers. After calling this method, the session
* cannot be used again.
*/
@Override
public void close() {
try {
rpc.invoke("session.destroy", Map.of("sessionId", sessionId), Void.class).get(5, TimeUnit.SECONDS);
} catch (Exception e) {
LOG.log(Level.FINE, "Error destroying session", e);
}
eventHandlers.clear();
toolHandlers.clear();
permissionHandler.set(null);
}
}