Skip to content

Commit b1afb3f

Browse files
committed
refactor: convert simple POJO classes to records for improved readability and simplify method usage
1 parent 5db1d09 commit b1afb3f

16 files changed

Lines changed: 111 additions & 776 deletions

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

Lines changed: 10 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,100 +11,24 @@
1111
* Represents a file attachment to include with a message.
1212
* <p>
1313
* Attachments provide additional context to the AI assistant, such as source
14-
* code files, documents, or other relevant content. All setter methods return
15-
* {@code this} for method chaining.
14+
* code files, documents, or other relevant content.
1615
*
1716
* <h2>Example Usage</h2>
1817
*
1918
* <pre>{@code
20-
* var attachment = new Attachment().setType("file").setPath("/path/to/source.java").setDisplayName("Main Source File");
19+
* var attachment = new Attachment("file", "/path/to/source.java", "Main Source File");
2120
* }</pre>
2221
*
22+
* @param type
23+
* the attachment type (e.g., "file")
24+
* @param path
25+
* the absolute path to the file on the filesystem
26+
* @param displayName
27+
* a human-readable display name for the attachment
2328
* @see MessageOptions#setAttachments(java.util.List)
2429
* @since 1.0.0
2530
*/
2631
@JsonInclude(JsonInclude.Include.NON_NULL)
27-
public class Attachment {
28-
29-
@JsonProperty("type")
30-
private String type;
31-
32-
@JsonProperty("path")
33-
private String path;
34-
35-
@JsonProperty("displayName")
36-
private String displayName;
37-
38-
/**
39-
* Gets the attachment type.
40-
*
41-
* @return the type (e.g., "file")
42-
*/
43-
public String getType() {
44-
return type;
45-
}
46-
47-
/**
48-
* Sets the attachment type.
49-
* <p>
50-
* Currently supported types:
51-
* <ul>
52-
* <li>"file" - A file from the filesystem</li>
53-
* </ul>
54-
*
55-
* @param type
56-
* the attachment type
57-
* @return this attachment for method chaining
58-
*/
59-
public Attachment setType(String type) {
60-
this.type = type;
61-
return this;
62-
}
63-
64-
/**
65-
* Gets the file path.
66-
*
67-
* @return the absolute path to the file
68-
*/
69-
public String getPath() {
70-
return path;
71-
}
72-
73-
/**
74-
* Sets the file path.
75-
* <p>
76-
* This should be an absolute path to the file on the filesystem.
77-
*
78-
* @param path
79-
* the absolute file path
80-
* @return this attachment for method chaining
81-
*/
82-
public Attachment setPath(String path) {
83-
this.path = path;
84-
return this;
85-
}
86-
87-
/**
88-
* Gets the display name.
89-
*
90-
* @return the display name for the attachment
91-
*/
92-
public String getDisplayName() {
93-
return displayName;
94-
}
95-
96-
/**
97-
* Sets a human-readable display name for the attachment.
98-
* <p>
99-
* This name is shown to the assistant and may be used when referring to the
100-
* file in responses.
101-
*
102-
* @param displayName
103-
* the display name
104-
* @return this attachment for method chaining
105-
*/
106-
public Attachment setDisplayName(String displayName) {
107-
this.displayName = displayName;
108-
return this;
109-
}
32+
public record Attachment(@JsonProperty("type") String type, @JsonProperty("path") String path,
33+
@JsonProperty("displayName") String displayName) {
11034
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* <pre>{@code
2121
* var options = new MessageOptions().setPrompt("Explain this code")
22-
* .setAttachments(List.of(new Attachment().setType("file").setPath("/path/to/file.java")));
22+
* .setAttachments(List.of(new Attachment("file", "/path/to/file.java", null)));
2323
*
2424
* session.send(options).get();
2525
* }</pre>

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

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,80 +11,16 @@
1111
/**
1212
* Output for a post-tool-use hook.
1313
*
14+
* @param modifiedResult
15+
* the modified tool result, or {@code null} to use original
16+
* @param additionalContext
17+
* additional context to provide to the model
18+
* @param suppressOutput
19+
* {@code true} to suppress output
1420
* @since 1.0.6
1521
*/
1622
@JsonInclude(JsonInclude.Include.NON_NULL)
17-
public class PostToolUseHookOutput {
18-
19-
@JsonProperty("modifiedResult")
20-
private JsonNode modifiedResult;
21-
22-
@JsonProperty("additionalContext")
23-
private String additionalContext;
24-
25-
@JsonProperty("suppressOutput")
26-
private Boolean suppressOutput;
27-
28-
/**
29-
* Gets the modified tool result.
30-
*
31-
* @return the modified result, or {@code null} to use original
32-
*/
33-
public JsonNode getModifiedResult() {
34-
return modifiedResult;
35-
}
36-
37-
/**
38-
* Sets the modified tool result.
39-
*
40-
* @param modifiedResult
41-
* the modified result
42-
* @return this instance for method chaining
43-
*/
44-
public PostToolUseHookOutput setModifiedResult(JsonNode modifiedResult) {
45-
this.modifiedResult = modifiedResult;
46-
return this;
47-
}
48-
49-
/**
50-
* Gets additional context to provide to the model.
51-
*
52-
* @return the additional context
53-
*/
54-
public String getAdditionalContext() {
55-
return additionalContext;
56-
}
57-
58-
/**
59-
* Sets additional context to provide to the model.
60-
*
61-
* @param additionalContext
62-
* the additional context
63-
* @return this instance for method chaining
64-
*/
65-
public PostToolUseHookOutput setAdditionalContext(String additionalContext) {
66-
this.additionalContext = additionalContext;
67-
return this;
68-
}
69-
70-
/**
71-
* Returns whether to suppress output.
72-
*
73-
* @return {@code true} to suppress output
74-
*/
75-
public Boolean getSuppressOutput() {
76-
return suppressOutput;
77-
}
78-
79-
/**
80-
* Sets whether to suppress output.
81-
*
82-
* @param suppressOutput
83-
* {@code true} to suppress output
84-
* @return this instance for method chaining
85-
*/
86-
public PostToolUseHookOutput setSuppressOutput(Boolean suppressOutput) {
87-
this.suppressOutput = suppressOutput;
88-
return this;
89-
}
23+
public record PostToolUseHookOutput(@JsonProperty("modifiedResult") JsonNode modifiedResult,
24+
@JsonProperty("additionalContext") String additionalContext,
25+
@JsonProperty("suppressOutput") Boolean suppressOutput) {
9026
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
*
1717
* <pre>{@code
1818
* SessionEndHandler handler = (input, invocation) -> {
19-
* System.out.println("Session ended: " + input.getReason());
20-
* return CompletableFuture
21-
* .completedFuture(new SessionEndHookOutput().setSessionSummary("Session completed successfully"));
19+
* System.out.println("Session ended: " + input.reason());
20+
* return CompletableFuture.completedFuture(new SessionEndHookOutput(null, null, "Session completed successfully"));
2221
* };
2322
* }</pre>
2423
*

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

Lines changed: 14 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -13,129 +13,21 @@
1313
* This hook is invoked when a session ends, allowing you to perform cleanup or
1414
* logging.
1515
*
16+
* @param timestamp
17+
* the timestamp in milliseconds since epoch when the session ended
18+
* @param cwd
19+
* the current working directory
20+
* @param reason
21+
* the reason: "complete", "error", "abort", "timeout", or
22+
* "user_exit"
23+
* @param finalMessage
24+
* the final message, or {@code null}
25+
* @param error
26+
* the error message, or {@code null}
1627
* @since 1.0.7
1728
*/
1829
@JsonIgnoreProperties(ignoreUnknown = true)
19-
public class SessionEndHookInput {
20-
21-
@JsonProperty("timestamp")
22-
private long timestamp;
23-
24-
@JsonProperty("cwd")
25-
private String cwd;
26-
27-
@JsonProperty("reason")
28-
private String reason;
29-
30-
@JsonProperty("finalMessage")
31-
private String finalMessage;
32-
33-
@JsonProperty("error")
34-
private String error;
35-
36-
/**
37-
* Gets the timestamp when the session ended.
38-
*
39-
* @return the timestamp in milliseconds since epoch
40-
*/
41-
public long getTimestamp() {
42-
return timestamp;
43-
}
44-
45-
/**
46-
* Sets the timestamp when the session ended.
47-
*
48-
* @param timestamp
49-
* the timestamp in milliseconds since epoch
50-
* @return this instance for method chaining
51-
*/
52-
public SessionEndHookInput setTimestamp(long timestamp) {
53-
this.timestamp = timestamp;
54-
return this;
55-
}
56-
57-
/**
58-
* Gets the current working directory.
59-
*
60-
* @return the current working directory
61-
*/
62-
public String getCwd() {
63-
return cwd;
64-
}
65-
66-
/**
67-
* Sets the current working directory.
68-
*
69-
* @param cwd
70-
* the current working directory
71-
* @return this instance for method chaining
72-
*/
73-
public SessionEndHookInput setCwd(String cwd) {
74-
this.cwd = cwd;
75-
return this;
76-
}
77-
78-
/**
79-
* Gets the reason for session end.
80-
*
81-
* @return the reason: "complete", "error", "abort", "timeout", or "user_exit"
82-
*/
83-
public String getReason() {
84-
return reason;
85-
}
86-
87-
/**
88-
* Sets the reason for session end.
89-
*
90-
* @param reason
91-
* the reason: "complete", "error", "abort", "timeout", or
92-
* "user_exit"
93-
* @return this instance for method chaining
94-
*/
95-
public SessionEndHookInput setReason(String reason) {
96-
this.reason = reason;
97-
return this;
98-
}
99-
100-
/**
101-
* Gets the final message, if any.
102-
*
103-
* @return the final message, or {@code null}
104-
*/
105-
public String getFinalMessage() {
106-
return finalMessage;
107-
}
108-
109-
/**
110-
* Sets the final message.
111-
*
112-
* @param finalMessage
113-
* the final message
114-
* @return this instance for method chaining
115-
*/
116-
public SessionEndHookInput setFinalMessage(String finalMessage) {
117-
this.finalMessage = finalMessage;
118-
return this;
119-
}
120-
121-
/**
122-
* Gets the error message, if the session ended due to an error.
123-
*
124-
* @return the error message, or {@code null}
125-
*/
126-
public String getError() {
127-
return error;
128-
}
129-
130-
/**
131-
* Sets the error message.
132-
*
133-
* @param error
134-
* the error message
135-
* @return this instance for method chaining
136-
*/
137-
public SessionEndHookInput setError(String error) {
138-
this.error = error;
139-
return this;
140-
}
30+
public record SessionEndHookInput(@JsonProperty("timestamp") long timestamp, @JsonProperty("cwd") String cwd,
31+
@JsonProperty("reason") String reason, @JsonProperty("finalMessage") String finalMessage,
32+
@JsonProperty("error") String error) {
14133
}

0 commit comments

Comments
 (0)