Skip to content

Commit d6a09cf

Browse files
committed
refactor: simplify response handling in RpcHandlerDispatcher and update tests for null output
1 parent ec936f0 commit d6a09cf

2 files changed

Lines changed: 6 additions & 13 deletions

File tree

src/main/java/com/github/copilot/sdk/RpcHandlerDispatcher.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.IOException;
88
import java.util.ArrayList;
9+
import java.util.Collections;
910
import java.util.Map;
1011
import java.util.concurrent.CompletableFuture;
1112
import java.util.logging.Level;
@@ -282,11 +283,7 @@ private void handleHooksInvoke(JsonRpcClient rpc, String requestId, JsonNode par
282283

283284
session.handleHooksInvoke(hookType, input).thenAccept(output -> {
284285
try {
285-
if (output != null) {
286-
rpc.sendResponse(Long.parseLong(requestId), Map.of("output", output));
287-
} else {
288-
rpc.sendResponse(Long.parseLong(requestId), Map.of("output", (Object) null));
289-
}
286+
rpc.sendResponse(Long.parseLong(requestId), Collections.singletonMap("output", output));
290287
} catch (IOException e) {
291288
LOG.log(Level.SEVERE, "Error sending hooks response", e);
292289
}

src/test/java/com/github/copilot/sdk/RpcHandlerDispatcherTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,8 @@ void hooksInvokeWithNullOutput() throws Exception {
461461
invokeHandler("hooks.invoke", "31", params);
462462

463463
JsonNode response = readResponse();
464-
// Null output triggers NPE in Map.of() → falls to .exceptionally() → error
465-
// response
466-
assertNotNull(response.get("error"));
467-
assertEquals(-32603, response.get("error").get("code").asInt());
464+
JsonNode output = response.get("result").get("output");
465+
assertTrue(output == null || output.isNull(), "Output should be null when no hook handler is set");
468466
}
469467

470468
@Test
@@ -522,9 +520,7 @@ void hooksInvokeWithNoHooksRegistered() throws Exception {
522520
invokeHandler("hooks.invoke", "34", params);
523521

524522
JsonNode response = readResponse();
525-
// Null output triggers NPE in Map.of() → falls to .exceptionally() → error
526-
// response
527-
assertNotNull(response.get("error"));
528-
assertEquals(-32603, response.get("error").get("code").asInt());
523+
JsonNode output = response.get("result").get("output");
524+
assertTrue(output == null || output.isNull(), "Output should be null when no hooks registered");
529525
}
530526
}

0 commit comments

Comments
 (0)