-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRpcHandlerDispatcher.java.html
More file actions
515 lines (471 loc) · 36.9 KB
/
Copy pathRpcHandlerDispatcher.java.html
File metadata and controls
515 lines (471 loc) · 36.9 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>RpcHandlerDispatcher.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">GitHub Copilot SDK :: Java</a> > <a href="index.source.html" class="el_package">com.github.copilot</a> > <span class="el_source">RpcHandlerDispatcher.java</span></div><h1>RpcHandlerDispatcher.java</h1><pre class="source lang-java linenums">/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.copilot.generated.SessionEvent;
import com.github.copilot.rpc.AutoModeSwitchRequest;
import com.github.copilot.rpc.ExitPlanModeRequest;
import com.github.copilot.rpc.PermissionRequestResult;
import com.github.copilot.rpc.PermissionRequestResultKind;
import com.github.copilot.rpc.SessionLifecycleEvent;
import com.github.copilot.rpc.SessionLifecycleEventMetadata;
import com.github.copilot.rpc.ToolDefinition;
import com.github.copilot.rpc.ToolInvocation;
import com.github.copilot.rpc.ToolResultObject;
import com.github.copilot.rpc.UserInputRequest;
/**
* Dispatches incoming JSON-RPC method calls to the appropriate handlers.
* <p>
* This class handles all server-to-client RPC calls including:
* <ul>
* <li>Session events</li>
* <li>Tool calls</li>
* <li>Permission requests</li>
* <li>User input requests</li>
* <li>Hooks invocations</li>
* <li>Lifecycle events</li>
* </ul>
*/
final class RpcHandlerDispatcher {
<span class="nc" id="L46"> private static final Logger LOG = Logger.getLogger(RpcHandlerDispatcher.class.getName());</span>
<span class="nc" id="L47"> private static final ObjectMapper MAPPER = JsonRpcClient.getObjectMapper();</span>
private final Map<String, CopilotSession> sessions;
private final LifecycleEventDispatcher lifecycleDispatcher;
private final Executor executor;
/**
* Creates a dispatcher with session registry and lifecycle dispatcher.
*
* @param sessions
* the session registry to look up sessions by ID
* @param lifecycleDispatcher
* callback for dispatching lifecycle events
* @param executor
* the executor for async dispatch, or {@code null} for default
*/
RpcHandlerDispatcher(Map<String, CopilotSession> sessions, LifecycleEventDispatcher lifecycleDispatcher,
<span class="nc" id="L64"> Executor executor) {</span>
<span class="nc" id="L65"> this.sessions = sessions;</span>
<span class="nc" id="L66"> this.lifecycleDispatcher = lifecycleDispatcher;</span>
<span class="nc" id="L67"> this.executor = executor;</span>
<span class="nc" id="L68"> }</span>
/**
* Registers all RPC method handlers with the given JSON-RPC client.
*
* @param rpc
* the JSON-RPC client to register handlers with
*/
void registerHandlers(JsonRpcClient rpc) {
<span class="nc" id="L77"> rpc.registerMethodHandler("session.event", (requestId, params) -> handleSessionEvent(params));</span>
<span class="nc" id="L78"> rpc.registerMethodHandler("session.lifecycle", (requestId, params) -> handleLifecycleEvent(params));</span>
<span class="nc" id="L79"> rpc.registerMethodHandler("tool.call", (requestId, params) -> handleToolCall(rpc, requestId, params));</span>
<span class="nc" id="L80"> rpc.registerMethodHandler("permission.request",</span>
<span class="nc" id="L81"> (requestId, params) -> handlePermissionRequest(rpc, requestId, params));</span>
<span class="nc" id="L82"> rpc.registerMethodHandler("userInput.request",</span>
<span class="nc" id="L83"> (requestId, params) -> handleUserInputRequest(rpc, requestId, params));</span>
<span class="nc" id="L84"> rpc.registerMethodHandler("exitPlanMode.request",</span>
<span class="nc" id="L85"> (requestId, params) -> handleExitPlanModeRequest(rpc, requestId, params));</span>
<span class="nc" id="L86"> rpc.registerMethodHandler("autoModeSwitch.request",</span>
<span class="nc" id="L87"> (requestId, params) -> handleAutoModeSwitchRequest(rpc, requestId, params));</span>
<span class="nc" id="L88"> rpc.registerMethodHandler("hooks.invoke", (requestId, params) -> handleHooksInvoke(rpc, requestId, params));</span>
<span class="nc" id="L89"> rpc.registerMethodHandler("systemMessage.transform",</span>
<span class="nc" id="L90"> (requestId, params) -> handleSystemMessageTransform(rpc, requestId, params));</span>
<span class="nc" id="L91"> }</span>
private void handleSessionEvent(JsonNode params) {
try {
<span class="nc" id="L95"> String sessionId = params.get("sessionId").asText();</span>
<span class="nc" id="L96"> JsonNode eventNode = params.get("event");</span>
<span class="nc" id="L97"> LOG.fine("Received session.event: " + eventNode);</span>
<span class="nc" id="L99"> CopilotSession session = sessions.get(sessionId);</span>
<span class="nc bnc" id="L100" title="All 4 branches missed."> if (session != null && eventNode != null) {</span>
<span class="nc" id="L101"> SessionEvent event = MAPPER.treeToValue(eventNode, SessionEvent.class);</span>
<span class="nc bnc" id="L102" title="All 2 branches missed."> if (event != null) {</span>
<span class="nc" id="L103"> session.dispatchEvent(event);</span>
}
}
<span class="nc" id="L106"> } catch (Exception e) {</span>
<span class="nc" id="L107"> LOG.log(Level.SEVERE, "Error handling session event", e);</span>
<span class="nc" id="L108"> }</span>
<span class="nc" id="L109"> }</span>
private void handleLifecycleEvent(JsonNode params) {
try {
<span class="nc bnc" id="L113" title="All 2 branches missed."> String type = params.has("type") ? params.get("type").asText() : "";</span>
<span class="nc bnc" id="L114" title="All 2 branches missed."> String sessionId = params.has("sessionId") ? params.get("sessionId").asText() : "";</span>
<span class="nc" id="L116"> SessionLifecycleEvent event = new SessionLifecycleEvent();</span>
<span class="nc" id="L117"> event.setType(type);</span>
<span class="nc" id="L118"> event.setSessionId(sessionId);</span>
<span class="nc bnc" id="L120" title="All 4 branches missed."> if (params.has("metadata") && !params.get("metadata").isNull()) {</span>
<span class="nc" id="L121"> SessionLifecycleEventMetadata metadata = MAPPER.treeToValue(params.get("metadata"),</span>
SessionLifecycleEventMetadata.class);
<span class="nc" id="L123"> event.setMetadata(metadata);</span>
}
<span class="nc" id="L126"> lifecycleDispatcher.dispatch(event);</span>
<span class="nc" id="L127"> } catch (Exception e) {</span>
<span class="nc" id="L128"> LOG.log(Level.SEVERE, "Error handling session lifecycle event", e);</span>
<span class="nc" id="L129"> }</span>
<span class="nc" id="L130"> }</span>
private void handleToolCall(JsonRpcClient rpc, String requestId, JsonNode params) {
<span class="nc" id="L133"> runAsync(() -> {</span>
<span class="nc" id="L134"> final long requestIdLong = parseRequestId(requestId, "tool.call");</span>
<span class="nc bnc" id="L135" title="All 2 branches missed."> if (requestIdLong == -1) {</span>
<span class="nc" id="L136"> return;</span>
}
try {
<span class="nc" id="L139"> String sessionId = params.get("sessionId").asText();</span>
<span class="nc" id="L140"> String toolCallId = params.get("toolCallId").asText();</span>
<span class="nc" id="L141"> String toolName = params.get("toolName").asText();</span>
<span class="nc" id="L142"> JsonNode arguments = params.get("arguments");</span>
<span class="nc" id="L144"> CopilotSession session = sessions.get(sessionId);</span>
<span class="nc bnc" id="L145" title="All 2 branches missed."> if (session == null) {</span>
<span class="nc" id="L146"> rpc.sendErrorResponse(requestIdLong, -32602, "Unknown session " + sessionId);</span>
<span class="nc" id="L147"> return;</span>
}
<span class="nc" id="L150"> ToolDefinition tool = session.getTool(toolName);</span>
<span class="nc bnc" id="L151" title="All 4 branches missed."> if (tool == null || tool.handler() == null) {</span>
<span class="nc" id="L152"> var result = ToolResultObject.failure("Tool '" + toolName + "' is not supported.",</span>
"tool '" + toolName + "' not supported");
<span class="nc" id="L154"> rpc.sendResponse(requestIdLong, Map.of("result", result));</span>
<span class="nc" id="L155"> return;</span>
}
<span class="nc" id="L158"> var invocation = new ToolInvocation().setSessionId(sessionId).setToolCallId(toolCallId)</span>
<span class="nc" id="L159"> .setToolName(toolName).setArguments(arguments);</span>
<span class="nc" id="L161"> tool.handler().invoke(invocation).thenAccept(result -> {</span>
try {
ToolResultObject toolResult;
<span class="nc bnc" id="L164" title="All 2 branches missed."> if (result instanceof ToolResultObject tr) {</span>
<span class="nc" id="L165"> toolResult = tr;</span>
} else {
toolResult = ToolResultObject
<span class="nc bnc" id="L168" title="All 2 branches missed."> .success(result instanceof String s ? s : MAPPER.writeValueAsString(result));</span>
}
<span class="nc" id="L170"> rpc.sendResponse(requestIdLong, Map.of("result", toolResult));</span>
<span class="nc" id="L171"> } catch (Exception e) {</span>
<span class="nc" id="L172"> LOG.log(Level.SEVERE, "Error sending tool result", e);</span>
<span class="nc" id="L173"> }</span>
<span class="nc" id="L174"> }).exceptionally(ex -> {</span>
try {
<span class="nc" id="L176"> var result = ToolResultObject.failure(</span>
"Invoking this tool produced an error. Detailed information is not available.",
<span class="nc" id="L178"> ex.getMessage());</span>
<span class="nc" id="L179"> rpc.sendResponse(requestIdLong, Map.of("result", result));</span>
<span class="nc" id="L180"> } catch (Exception e) {</span>
<span class="nc" id="L181"> LOG.log(Level.SEVERE, "Error sending tool error", e);</span>
<span class="nc" id="L182"> }</span>
<span class="nc" id="L183"> return null;</span>
});
<span class="nc" id="L185"> } catch (Exception e) {</span>
<span class="nc" id="L186"> LOG.log(Level.SEVERE, "Error handling tool call", e);</span>
try {
<span class="nc" id="L188"> rpc.sendErrorResponse(requestIdLong, -32603, e.getMessage());</span>
<span class="nc" id="L189"> } catch (IOException ioe) {</span>
<span class="nc" id="L190"> LOG.log(Level.SEVERE, "Failed to send error response", ioe);</span>
<span class="nc" id="L191"> }</span>
<span class="nc" id="L192"> }</span>
<span class="nc" id="L193"> });</span>
<span class="nc" id="L194"> }</span>
private void handlePermissionRequest(JsonRpcClient rpc, String requestId, JsonNode params) {
<span class="nc" id="L197"> runAsync(() -> {</span>
<span class="nc" id="L198"> final long requestIdLong = parseRequestId(requestId, "permission.request");</span>
<span class="nc bnc" id="L199" title="All 2 branches missed."> if (requestIdLong == -1) {</span>
<span class="nc" id="L200"> return;</span>
}
try {
<span class="nc" id="L203"> String sessionId = params.get("sessionId").asText();</span>
<span class="nc" id="L204"> JsonNode permissionRequest = params.get("permissionRequest");</span>
<span class="nc" id="L206"> CopilotSession session = sessions.get(sessionId);</span>
<span class="nc bnc" id="L207" title="All 2 branches missed."> if (session == null) {</span>
<span class="nc" id="L208"> var result = new PermissionRequestResult()</span>
<span class="nc" id="L209"> .setKind(PermissionRequestResultKind.DENIED_COULD_NOT_REQUEST_FROM_USER);</span>
<span class="nc" id="L210"> rpc.sendResponse(requestIdLong, Map.of("result", result));</span>
<span class="nc" id="L211"> return;</span>
}
<span class="nc" id="L214"> session.handlePermissionRequest(permissionRequest).thenAccept(result -> {</span>
try {
<span class="nc bnc" id="L216" title="All 2 branches missed."> if (PermissionRequestResultKind.NO_RESULT.getValue().equalsIgnoreCase(result.getKind())) {</span>
// Protocol v2 does not support NO_RESULT — the server
// expects exactly one response per request, so abstaining
// would leave it hanging.
<span class="nc" id="L220"> throw new IllegalStateException(</span>
"Permission handlers cannot return 'no-result' when connected to a protocol v2 server.");
}
<span class="nc" id="L223"> rpc.sendResponse(requestIdLong, Map.of("result", result));</span>
<span class="nc" id="L224"> } catch (IOException e) {</span>
<span class="nc" id="L225"> LOG.log(Level.SEVERE, "Error sending permission result", e);</span>
<span class="nc" id="L226"> }</span>
<span class="nc" id="L227"> }).exceptionally(ex -> {</span>
try {
<span class="nc" id="L229"> var result = new PermissionRequestResult()</span>
<span class="nc" id="L230"> .setKind(PermissionRequestResultKind.DENIED_COULD_NOT_REQUEST_FROM_USER);</span>
<span class="nc" id="L231"> rpc.sendResponse(requestIdLong, Map.of("result", result));</span>
<span class="nc" id="L232"> } catch (IOException e) {</span>
<span class="nc" id="L233"> LOG.log(Level.SEVERE, "Error sending permission denied", e);</span>
<span class="nc" id="L234"> }</span>
<span class="nc" id="L235"> return null;</span>
});
<span class="nc" id="L237"> } catch (Exception e) {</span>
<span class="nc" id="L238"> LOG.log(Level.SEVERE, "Error handling permission request", e);</span>
<span class="nc" id="L239"> }</span>
<span class="nc" id="L240"> });</span>
<span class="nc" id="L241"> }</span>
private void handleUserInputRequest(JsonRpcClient rpc, String requestId, JsonNode params) {
<span class="nc" id="L244"> LOG.fine("Received userInput.request: " + params);</span>
<span class="nc" id="L245"> runAsync(() -> {</span>
<span class="nc" id="L246"> final long requestIdLong = parseRequestId(requestId, "userInput.request");</span>
<span class="nc bnc" id="L247" title="All 2 branches missed."> if (requestIdLong == -1) {</span>
<span class="nc" id="L248"> return;</span>
}
try {
<span class="nc" id="L251"> String sessionId = params.get("sessionId").asText();</span>
<span class="nc" id="L252"> String question = params.get("question").asText();</span>
<span class="nc" id="L253"> LOG.fine("Processing userInput for session " + sessionId + ", question: " + question);</span>
<span class="nc" id="L254"> JsonNode choicesNode = params.get("choices");</span>
<span class="nc" id="L255"> JsonNode allowFreeformNode = params.get("allowFreeform");</span>
<span class="nc" id="L257"> CopilotSession session = sessions.get(sessionId);</span>
<span class="nc bnc" id="L258" title="All 2 branches missed."> LOG.fine("Found session: " + (session != null));</span>
<span class="nc bnc" id="L259" title="All 2 branches missed."> if (session == null) {</span>
<span class="nc" id="L260"> LOG.fine("Session not found, sending error");</span>
<span class="nc" id="L261"> rpc.sendErrorResponse(requestIdLong, -32602, "Unknown session " + sessionId);</span>
<span class="nc" id="L262"> return;</span>
}
<span class="nc" id="L265"> var request = new UserInputRequest().setQuestion(question);</span>
<span class="nc bnc" id="L266" title="All 4 branches missed."> if (choicesNode != null && choicesNode.isArray()) {</span>
<span class="nc" id="L267"> var choices = new ArrayList<String>();</span>
<span class="nc bnc" id="L268" title="All 2 branches missed."> for (JsonNode choice : choicesNode) {</span>
<span class="nc" id="L269"> choices.add(choice.asText());</span>
<span class="nc" id="L270"> }</span>
<span class="nc" id="L271"> request.setChoices(choices);</span>
}
<span class="nc bnc" id="L273" title="All 2 branches missed."> if (allowFreeformNode != null) {</span>
<span class="nc" id="L274"> request.setAllowFreeform(allowFreeformNode.asBoolean());</span>
}
<span class="nc" id="L277"> session.handleUserInputRequest(request).thenAccept(response -> {</span>
try {
// Ensure answer is never null - CLI requires a non-null string
<span class="nc bnc" id="L280" title="All 2 branches missed."> String answer = response.getAnswer() != null ? response.getAnswer() : "";</span>
<span class="nc" id="L281"> LOG.fine("Sending userInput response: answer=" + answer + ", wasFreeform="</span>
<span class="nc" id="L282"> + response.isWasFreeform());</span>
<span class="nc" id="L283"> rpc.sendResponse(requestIdLong,</span>
<span class="nc" id="L284"> Map.of("answer", answer, "wasFreeform", response.isWasFreeform()));</span>
<span class="nc" id="L285"> } catch (IOException e) {</span>
<span class="nc" id="L286"> LOG.log(Level.SEVERE, "Error sending user input response", e);</span>
<span class="nc" id="L287"> }</span>
<span class="nc" id="L288"> }).exceptionally(ex -> {</span>
<span class="nc" id="L289"> LOG.log(Level.WARNING, "User input handler exception", ex);</span>
try {
<span class="nc" id="L291"> rpc.sendErrorResponse(requestIdLong, -32603, "User input handler error: " + ex.getMessage());</span>
<span class="nc" id="L292"> } catch (IOException e) {</span>
<span class="nc" id="L293"> LOG.log(Level.SEVERE, "Error sending user input error", e);</span>
<span class="nc" id="L294"> }</span>
<span class="nc" id="L295"> return null;</span>
});
<span class="nc" id="L297"> } catch (Exception e) {</span>
<span class="nc" id="L298"> LOG.log(Level.SEVERE, "Error handling user input request", e);</span>
<span class="nc" id="L299"> }</span>
<span class="nc" id="L300"> });</span>
<span class="nc" id="L301"> }</span>
private void handleExitPlanModeRequest(JsonRpcClient rpc, String requestId, JsonNode params) {
<span class="nc" id="L304"> runAsync(() -> {</span>
<span class="nc" id="L305"> final long requestIdLong = parseRequestId(requestId, "exitPlanMode.request");</span>
<span class="nc bnc" id="L306" title="All 2 branches missed."> if (requestIdLong == -1) {</span>
<span class="nc" id="L307"> return;</span>
}
try {
<span class="nc" id="L310"> String sessionId = params.get("sessionId").asText();</span>
<span class="nc" id="L312"> CopilotSession session = sessions.get(sessionId);</span>
<span class="nc bnc" id="L313" title="All 2 branches missed."> if (session == null) {</span>
<span class="nc" id="L314"> rpc.sendErrorResponse(requestIdLong, -32602, "Unknown session " + sessionId);</span>
<span class="nc" id="L315"> return;</span>
}
<span class="nc" id="L318"> var request = new ExitPlanModeRequest();</span>
<span class="nc bnc" id="L319" title="All 2 branches missed."> if (params.has("summary")) {</span>
<span class="nc" id="L320"> request.setSummary(params.get("summary").asText());</span>
}
<span class="nc bnc" id="L322" title="All 4 branches missed."> if (params.has("planContent") && !params.get("planContent").isNull()) {</span>
<span class="nc" id="L323"> request.setPlanContent(params.get("planContent").asText());</span>
}
<span class="nc bnc" id="L325" title="All 4 branches missed."> if (params.has("actions") && params.get("actions").isArray()) {</span>
<span class="nc" id="L326"> var actions = new ArrayList<String>();</span>
<span class="nc bnc" id="L327" title="All 2 branches missed."> for (JsonNode action : params.get("actions")) {</span>
<span class="nc" id="L328"> actions.add(action.asText());</span>
<span class="nc" id="L329"> }</span>
<span class="nc" id="L330"> request.setActions(actions);</span>
}
<span class="nc bnc" id="L332" title="All 4 branches missed."> if (params.has("recommendedAction") && !params.get("recommendedAction").isNull()) {</span>
<span class="nc" id="L333"> request.setRecommendedAction(params.get("recommendedAction").asText());</span>
}
<span class="nc" id="L336"> session.handleExitPlanModeRequest(request).thenAccept(result -> {</span>
try {
<span class="nc" id="L338"> rpc.sendResponse(requestIdLong, MAPPER.valueToTree(result));</span>
<span class="nc" id="L339"> } catch (IOException e) {</span>
<span class="nc" id="L340"> LOG.log(Level.SEVERE, "Error sending exit plan mode response", e);</span>
<span class="nc" id="L341"> }</span>
<span class="nc" id="L342"> }).exceptionally(ex -> {</span>
try {
<span class="nc" id="L344"> rpc.sendErrorResponse(requestIdLong, -32603,</span>
<span class="nc" id="L345"> "Exit plan mode handler error: " + ex.getMessage());</span>
<span class="nc" id="L346"> } catch (IOException e) {</span>
<span class="nc" id="L347"> LOG.log(Level.SEVERE, "Error sending exit plan mode error", e);</span>
<span class="nc" id="L348"> }</span>
<span class="nc" id="L349"> return null;</span>
});
<span class="nc" id="L351"> } catch (Exception e) {</span>
<span class="nc" id="L352"> LOG.log(Level.SEVERE, "Error handling exit plan mode request", e);</span>
<span class="nc" id="L353"> }</span>
<span class="nc" id="L354"> });</span>
<span class="nc" id="L355"> }</span>
private void handleAutoModeSwitchRequest(JsonRpcClient rpc, String requestId, JsonNode params) {
<span class="nc" id="L358"> runAsync(() -> {</span>
<span class="nc" id="L359"> final long requestIdLong = parseRequestId(requestId, "autoModeSwitch.request");</span>
<span class="nc bnc" id="L360" title="All 2 branches missed."> if (requestIdLong == -1) {</span>
<span class="nc" id="L361"> return;</span>
}
try {
<span class="nc" id="L364"> String sessionId = params.get("sessionId").asText();</span>
<span class="nc" id="L366"> CopilotSession session = sessions.get(sessionId);</span>
<span class="nc bnc" id="L367" title="All 2 branches missed."> if (session == null) {</span>
<span class="nc" id="L368"> rpc.sendErrorResponse(requestIdLong, -32602, "Unknown session " + sessionId);</span>
<span class="nc" id="L369"> return;</span>
}
<span class="nc" id="L372"> var request = new AutoModeSwitchRequest();</span>
<span class="nc bnc" id="L373" title="All 4 branches missed."> if (params.has("errorCode") && !params.get("errorCode").isNull()) {</span>
<span class="nc" id="L374"> request.setErrorCode(params.get("errorCode").asText());</span>
}
<span class="nc bnc" id="L376" title="All 4 branches missed."> if (params.has("retryAfterSeconds") && !params.get("retryAfterSeconds").isNull()) {</span>
<span class="nc" id="L377"> request.setRetryAfterSeconds(params.get("retryAfterSeconds").asDouble());</span>
}
<span class="nc" id="L380"> session.handleAutoModeSwitchRequest(request).thenAccept(response -> {</span>
try {
<span class="nc" id="L382"> rpc.sendResponse(requestIdLong, Map.of("response", response));</span>
<span class="nc" id="L383"> } catch (IOException e) {</span>
<span class="nc" id="L384"> LOG.log(Level.SEVERE, "Error sending auto mode switch response", e);</span>
<span class="nc" id="L385"> }</span>
<span class="nc" id="L386"> }).exceptionally(ex -> {</span>
try {
<span class="nc" id="L388"> rpc.sendErrorResponse(requestIdLong, -32603,</span>
<span class="nc" id="L389"> "Auto mode switch handler error: " + ex.getMessage());</span>
<span class="nc" id="L390"> } catch (IOException e) {</span>
<span class="nc" id="L391"> LOG.log(Level.SEVERE, "Error sending auto mode switch error", e);</span>
<span class="nc" id="L392"> }</span>
<span class="nc" id="L393"> return null;</span>
});
<span class="nc" id="L395"> } catch (Exception e) {</span>
<span class="nc" id="L396"> LOG.log(Level.SEVERE, "Error handling auto mode switch request", e);</span>
<span class="nc" id="L397"> }</span>
<span class="nc" id="L398"> });</span>
<span class="nc" id="L399"> }</span>
private void handleHooksInvoke(JsonRpcClient rpc, String requestId, JsonNode params) {
<span class="nc" id="L402"> runAsync(() -> {</span>
<span class="nc" id="L403"> final long requestIdLong = parseRequestId(requestId, "hooks.invoke");</span>
<span class="nc bnc" id="L404" title="All 2 branches missed."> if (requestIdLong == -1) {</span>
<span class="nc" id="L405"> return;</span>
}
try {
<span class="nc" id="L408"> String sessionId = params.get("sessionId").asText();</span>
<span class="nc" id="L409"> String hookType = params.get("hookType").asText();</span>
<span class="nc" id="L410"> JsonNode input = params.get("input");</span>
<span class="nc" id="L412"> CopilotSession session = sessions.get(sessionId);</span>
<span class="nc bnc" id="L413" title="All 2 branches missed."> if (session == null) {</span>
<span class="nc" id="L414"> rpc.sendErrorResponse(requestIdLong, -32602, "Unknown session " + sessionId);</span>
<span class="nc" id="L415"> return;</span>
}
<span class="nc" id="L418"> session.handleHooksInvoke(hookType, input).thenAccept(output -> {</span>
try {
<span class="nc" id="L420"> rpc.sendResponse(requestIdLong, Collections.singletonMap("output", output));</span>
<span class="nc" id="L421"> } catch (IOException e) {</span>
<span class="nc" id="L422"> LOG.log(Level.SEVERE, "Error sending hooks response", e);</span>
<span class="nc" id="L423"> }</span>
<span class="nc" id="L424"> }).exceptionally(ex -> {</span>
try {
<span class="nc" id="L426"> rpc.sendErrorResponse(requestIdLong, -32603, "Hooks handler error: " + ex.getMessage());</span>
<span class="nc" id="L427"> } catch (IOException e) {</span>
<span class="nc" id="L428"> LOG.log(Level.SEVERE, "Error sending hooks error", e);</span>
<span class="nc" id="L429"> }</span>
<span class="nc" id="L430"> return null;</span>
});
<span class="nc" id="L432"> } catch (Exception e) {</span>
<span class="nc" id="L433"> LOG.log(Level.SEVERE, "Error handling hooks invoke", e);</span>
<span class="nc" id="L434"> }</span>
<span class="nc" id="L435"> });</span>
<span class="nc" id="L436"> }</span>
/**
* Functional interface for dispatching lifecycle events.
*/
@FunctionalInterface
interface LifecycleEventDispatcher {
void dispatch(SessionLifecycleEvent event);
}
private void handleSystemMessageTransform(JsonRpcClient rpc, String requestId, JsonNode params) {
<span class="nc" id="L448"> runAsync(() -> {</span>
<span class="nc" id="L449"> final long requestIdLong = parseRequestId(requestId, "systemMessage.transform");</span>
<span class="nc bnc" id="L450" title="All 2 branches missed."> if (requestIdLong == -1) {</span>
<span class="nc" id="L451"> return;</span>
}
try {
<span class="nc bnc" id="L454" title="All 2 branches missed."> String sessionId = params.has("sessionId") ? params.get("sessionId").asText() : null;</span>
<span class="nc" id="L455"> JsonNode sections = params.get("sections");</span>
<span class="nc bnc" id="L457" title="All 2 branches missed."> CopilotSession session = sessionId != null ? sessions.get(sessionId) : null;</span>
<span class="nc bnc" id="L458" title="All 2 branches missed."> if (session == null) {</span>
<span class="nc" id="L459"> rpc.sendErrorResponse(requestIdLong, -32602, "Unknown session " + sessionId);</span>
<span class="nc" id="L460"> return;</span>
}
<span class="nc" id="L463"> session.handleSystemMessageTransform(sections).thenAccept(result -> {</span>
try {
<span class="nc" id="L465"> rpc.sendResponse(requestIdLong, result);</span>
<span class="nc" id="L466"> } catch (IOException e) {</span>
<span class="nc" id="L467"> LOG.log(Level.SEVERE, "Error sending systemMessage.transform response", e);</span>
<span class="nc" id="L468"> }</span>
<span class="nc" id="L469"> }).exceptionally(ex -> {</span>
try {
<span class="nc" id="L471"> rpc.sendErrorResponse(requestIdLong, -32603, "Transform error: " + ex.getMessage());</span>
<span class="nc" id="L472"> } catch (IOException e) {</span>
<span class="nc" id="L473"> LOG.log(Level.SEVERE, "Error sending transform error response", e);</span>
<span class="nc" id="L474"> }</span>
<span class="nc" id="L475"> return null;</span>
});
<span class="nc" id="L477"> } catch (Exception e) {</span>
<span class="nc" id="L478"> LOG.log(Level.SEVERE, "Error handling systemMessage.transform", e);</span>
<span class="nc" id="L479"> }</span>
<span class="nc" id="L480"> });</span>
<span class="nc" id="L481"> }</span>
/**
* Parses a JSON-RPC request ID string into a {@code long}.
*
* @param requestId
* the request ID string received from the JSON-RPC layer
* @param methodName
* the RPC method name, used in the log message on failure
* @return the parsed request ID, or {@code -1} if the string is not a valid
* long
*/
private static long parseRequestId(String requestId, String methodName) {
try {
<span class="nc" id="L495"> return Long.parseLong(requestId);</span>
<span class="nc" id="L496"> } catch (NumberFormatException nfe) {</span>
<span class="nc" id="L497"> LOG.log(Level.SEVERE, "Invalid requestId for " + methodName + ": " + requestId, nfe);</span>
<span class="nc" id="L498"> return -1;</span>
}
}
private void runAsync(Runnable task) {
try {
<span class="nc bnc" id="L504" title="All 2 branches missed."> if (executor != null) {</span>
<span class="nc" id="L505"> CompletableFuture.runAsync(task, executor);</span>
} else {
<span class="nc" id="L507"> CompletableFuture.runAsync(task);</span>
}
<span class="nc" id="L509"> } catch (RejectedExecutionException e) {</span>
<span class="nc" id="L510"> LOG.log(Level.WARNING, "Executor rejected handler task; running inline", e);</span>
<span class="nc" id="L511"> task.run();</span>
<span class="nc" id="L512"> }</span>
<span class="nc" id="L513"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.14.202510111229</span></div></body></html>