Skip to content

Commit 46b38cc

Browse files
committed
refactor: convert response classes to records and update method references for improved readability
1 parent 4aad552 commit 46b38cc

14 files changed

Lines changed: 70 additions & 356 deletions

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ private void verifyProtocolVersion(Connection connection) throws Exception {
179179
params.put("message", null);
180180
PingResponse pingResponse = connection.rpc.invoke("ping", params, PingResponse.class).get(30, TimeUnit.SECONDS);
181181

182-
if (pingResponse.getProtocolVersion() == null) {
182+
if (pingResponse.protocolVersion() == null) {
183183
throw new RuntimeException("SDK protocol version mismatch: SDK expects version " + expectedVersion
184184
+ ", but server does not report a protocol version. "
185185
+ "Please update your server to ensure compatibility.");
186186
}
187187

188-
if (pingResponse.getProtocolVersion() != expectedVersion) {
188+
if (pingResponse.protocolVersion() != expectedVersion) {
189189
throw new RuntimeException("SDK protocol version mismatch: SDK expects version " + expectedVersion
190-
+ ", but server reports version " + pingResponse.getProtocolVersion() + ". "
190+
+ ", but server reports version " + pingResponse.protocolVersion() + ". "
191191
+ "Please update your SDK or server to ensure compatibility.");
192192
}
193193
}
@@ -272,9 +272,9 @@ public CompletableFuture<CopilotSession> createSession(SessionConfig config) {
272272
var request = SessionRequestBuilder.buildCreateRequest(config);
273273

274274
return connection.rpc.invoke("session.create", request, CreateSessionResponse.class).thenApply(response -> {
275-
var session = new CopilotSession(response.getSessionId(), connection.rpc, response.getWorkspacePath());
275+
var session = new CopilotSession(response.sessionId(), connection.rpc, response.workspacePath());
276276
SessionRequestBuilder.configureSession(session, config);
277-
sessions.put(response.getSessionId(), session);
277+
sessions.put(response.sessionId(), session);
278278
return session;
279279
});
280280
});
@@ -310,9 +310,9 @@ public CompletableFuture<CopilotSession> resumeSession(String sessionId, ResumeS
310310
var request = SessionRequestBuilder.buildResumeRequest(sessionId, config);
311311

312312
return connection.rpc.invoke("session.resume", request, ResumeSessionResponse.class).thenApply(response -> {
313-
var session = new CopilotSession(response.getSessionId(), connection.rpc, response.getWorkspacePath());
313+
var session = new CopilotSession(response.sessionId(), connection.rpc, response.workspacePath());
314314
SessionRequestBuilder.configureSession(session, config);
315-
sessions.put(response.getSessionId(), session);
315+
sessions.put(response.sessionId(), session);
316316
return session;
317317
});
318318
});
@@ -432,7 +432,7 @@ public CompletableFuture<List<ModelInfo>> listModels() {
432432
public CompletableFuture<String> getLastSessionId() {
433433
return ensureConnected().thenCompose(
434434
connection -> connection.rpc.invoke("session.getLastId", Map.of(), GetLastSessionIdResponse.class)
435-
.thenApply(GetLastSessionIdResponse::getSessionId));
435+
.thenApply(GetLastSessionIdResponse::sessionId));
436436
}
437437

438438
/**
@@ -450,9 +450,8 @@ public CompletableFuture<Void> deleteSession(String sessionId) {
450450
return ensureConnected().thenCompose(connection -> connection.rpc
451451
.invoke("session.delete", Map.of("sessionId", sessionId), DeleteSessionResponse.class)
452452
.thenAccept(response -> {
453-
if (!response.isSuccess()) {
454-
throw new RuntimeException(
455-
"Failed to delete session " + sessionId + ": " + response.getError());
453+
if (!response.success()) {
454+
throw new RuntimeException("Failed to delete session " + sessionId + ": " + response.error());
456455
}
457456
sessions.remove(sessionId);
458457
}));
@@ -471,7 +470,7 @@ public CompletableFuture<Void> deleteSession(String sessionId) {
471470
public CompletableFuture<List<SessionMetadata>> listSessions() {
472471
return ensureConnected()
473472
.thenCompose(connection -> connection.rpc.invoke("session.list", Map.of(), ListSessionsResponse.class)
474-
.thenApply(ListSessionsResponse::getSessions));
473+
.thenApply(ListSessionsResponse::sessions));
475474
}
476475

477476
/**
@@ -487,7 +486,7 @@ public CompletableFuture<String> getForegroundSessionId() {
487486
return ensureConnected().thenCompose(connection -> connection.rpc
488487
.invoke("session.getForeground", Map.of(),
489488
com.github.copilot.sdk.json.GetForegroundSessionResponse.class)
490-
.thenApply(com.github.copilot.sdk.json.GetForegroundSessionResponse::getSessionId));
489+
.thenApply(com.github.copilot.sdk.json.GetForegroundSessionResponse::sessionId));
491490
}
492491

493492
/**
@@ -509,9 +508,9 @@ public CompletableFuture<Void> setForegroundSessionId(String sessionId) {
509508
.invoke("session.setForeground", Map.of("sessionId", sessionId),
510509
com.github.copilot.sdk.json.SetForegroundSessionResponse.class)
511510
.thenAccept(response -> {
512-
if (!response.isSuccess()) {
513-
throw new RuntimeException(response.getError() != null
514-
? response.getError()
511+
if (!response.success()) {
512+
throw new RuntimeException(response.error() != null
513+
? response.error()
515514
: "Failed to set foreground session");
516515
}
517516
}));

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,7 @@ public CompletableFuture<String> send(MessageOptions options) {
303303
request.setAttachments(options.getAttachments());
304304
request.setMode(options.getMode());
305305

306-
return rpc.invoke("session.send", request, SendMessageResponse.class)
307-
.thenApply(SendMessageResponse::getMessageId);
306+
return rpc.invoke("session.send", request, SendMessageResponse.class).thenApply(SendMessageResponse::messageId);
308307
}
309308

310309
/**
@@ -747,8 +746,8 @@ public CompletableFuture<List<AbstractSessionEvent>> getMessages() {
747746
return rpc.invoke("session.getMessages", Map.of("sessionId", sessionId), GetMessagesResponse.class)
748747
.thenApply(response -> {
749748
var events = new ArrayList<AbstractSessionEvent>();
750-
if (response.getEvents() != null) {
751-
for (JsonNode eventNode : response.getEvents()) {
749+
if (response.events() != null) {
750+
for (JsonNode eventNode : response.events()) {
752751
try {
753752
AbstractSessionEvent event = SessionEventParser.parse(eventNode);
754753
if (event != null) {

src/main/java/com/github/copilot/sdk/json/CreateSessionResponse.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,14 @@
66
/**
77
* Internal response object from creating a session.
88
*
9+
* @param sessionId
10+
* the session ID assigned by the server
11+
* @param workspacePath
12+
* the workspace path, or {@code null} if infinite sessions are
13+
* disabled
914
* @since 1.0.0
1015
*/
1116
@JsonInclude(JsonInclude.Include.NON_NULL)
12-
public final class CreateSessionResponse {
13-
@JsonProperty("sessionId")
14-
private String sessionId;
15-
16-
@JsonProperty("workspacePath")
17-
private String workspacePath;
18-
19-
public String getSessionId() {
20-
return sessionId;
21-
}
22-
public void setSessionId(String sessionId) {
23-
this.sessionId = sessionId;
24-
}
25-
26-
/**
27-
* Gets the workspace path when infinite sessions are enabled.
28-
*
29-
* @return the workspace path, or {@code null} if infinite sessions are disabled
30-
*/
31-
public String getWorkspacePath() {
32-
return workspacePath;
33-
}
34-
public void setWorkspacePath(String workspacePath) {
35-
this.workspacePath = workspacePath;
36-
}
17+
public record CreateSessionResponse(@JsonProperty("sessionId") String sessionId,
18+
@JsonProperty("workspacePath") String workspacePath) {
3719
}

src/main/java/com/github/copilot/sdk/json/DeleteSessionResponse.java

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,9 @@
1717
* @since 1.0.0
1818
*/
1919
@JsonInclude(JsonInclude.Include.NON_NULL)
20-
public final class DeleteSessionResponse {
21-
22-
@JsonProperty("success")
23-
private boolean success;
24-
25-
@JsonProperty("error")
26-
private String error;
27-
28-
/**
29-
* Returns whether the deletion was successful.
30-
*
31-
* @return {@code true} if the session was deleted successfully
32-
*/
33-
public boolean isSuccess() {
34-
return success;
35-
}
36-
37-
/**
38-
* Sets whether the deletion was successful.
39-
*
40-
* @param success
41-
* {@code true} if successful
42-
*/
43-
public void setSuccess(boolean success) {
44-
this.success = success;
45-
}
46-
47-
/**
48-
* Gets the error message if the deletion failed.
49-
*
50-
* @return the error message, or {@code null} if successful
51-
*/
52-
public String getError() {
53-
return error;
54-
}
55-
56-
/**
57-
* Sets the error message.
58-
*
59-
* @param error
60-
* the error message
61-
*/
62-
public void setError(String error) {
63-
this.error = error;
64-
}
20+
public record DeleteSessionResponse(
21+
/** Whether the deletion was successful. */
22+
@JsonProperty("success") boolean success,
23+
/** The error message, or {@code null} if successful. */
24+
@JsonProperty("error") String error) {
6525
}

src/main/java/com/github/copilot/sdk/json/GetForegroundSessionResponse.java

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,9 @@
1616
* @since 1.0.0
1717
*/
1818
@JsonIgnoreProperties(ignoreUnknown = true)
19-
public class GetForegroundSessionResponse {
20-
21-
@JsonProperty("sessionId")
22-
private String sessionId;
23-
24-
@JsonProperty("workspacePath")
25-
private String workspacePath;
26-
27-
/**
28-
* Gets the session ID currently displayed in the TUI.
29-
*
30-
* @return the session ID, or null if no foreground session
31-
*/
32-
public String getSessionId() {
33-
return sessionId;
34-
}
35-
36-
public void setSessionId(String sessionId) {
37-
this.sessionId = sessionId;
38-
}
39-
40-
/**
41-
* Gets the workspace path of the foreground session.
42-
*
43-
* @return the workspace path, or null
44-
*/
45-
public String getWorkspacePath() {
46-
return workspacePath;
47-
}
48-
49-
public void setWorkspacePath(String workspacePath) {
50-
this.workspacePath = workspacePath;
51-
}
19+
public record GetForegroundSessionResponse(
20+
/** The session ID currently displayed in the TUI, or null if none. */
21+
@JsonProperty("sessionId") String sessionId,
22+
/** The workspace path of the foreground session, or null. */
23+
@JsonProperty("workspacePath") String workspacePath) {
5224
}

src/main/java/com/github/copilot/sdk/json/GetLastSessionIdResponse.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,5 @@
99
* @since 1.0.0
1010
*/
1111
@JsonInclude(JsonInclude.Include.NON_NULL)
12-
public final class GetLastSessionIdResponse {
13-
@JsonProperty("sessionId")
14-
private String sessionId;
15-
16-
public String getSessionId() {
17-
return sessionId;
18-
}
19-
public void setSessionId(String sessionId) {
20-
this.sessionId = sessionId;
21-
}
12+
public record GetLastSessionIdResponse(@JsonProperty("sessionId") String sessionId) {
2213
}

src/main/java/com/github/copilot/sdk/json/GetMessagesResponse.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,5 @@
1212
* @since 1.0.0
1313
*/
1414
@JsonInclude(JsonInclude.Include.NON_NULL)
15-
public final class GetMessagesResponse {
16-
17-
@JsonProperty("events")
18-
private List<JsonNode> events;
19-
20-
public List<JsonNode> getEvents() {
21-
return events;
22-
}
23-
24-
public void setEvents(List<JsonNode> events) {
25-
this.events = events;
26-
}
15+
public record GetMessagesResponse(@JsonProperty("events") List<JsonNode> events) {
2716
}

src/main/java/com/github/copilot/sdk/json/ListSessionsResponse.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,7 @@
2020
* @since 1.0.0
2121
*/
2222
@JsonInclude(JsonInclude.Include.NON_NULL)
23-
public final class ListSessionsResponse {
24-
25-
@JsonProperty("sessions")
26-
private List<SessionMetadata> sessions;
27-
28-
/**
29-
* Gets the list of sessions.
30-
*
31-
* @return the list of session metadata
32-
*/
33-
public List<SessionMetadata> getSessions() {
34-
return sessions;
35-
}
36-
37-
/**
38-
* Sets the list of sessions.
39-
*
40-
* @param sessions
41-
* the list of session metadata
42-
*/
43-
public void setSessions(List<SessionMetadata> sessions) {
44-
this.sessions = sessions;
45-
}
23+
public record ListSessionsResponse(
24+
/** The list of session metadata. */
25+
@JsonProperty("sessions") List<SessionMetadata> sessions) {
4626
}

0 commit comments

Comments
 (0)