Skip to content

Commit c9b4c00

Browse files
Copilotbrunoborges
andcommitted
Convert ToolResultObject from class to record with factory methods
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent 2de4006 commit c9b4c00

File tree

3 files changed

+59
-129
lines changed

3 files changed

+59
-129
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ private void handleToolCall(JsonRpcClient rpc, String requestId, JsonNode params
130130

131131
ToolDefinition tool = session.getTool(toolName);
132132
if (tool == null || tool.handler() == null) {
133-
var result = new ToolResultObject().setTextResultForLlm("Tool '" + toolName + "' is not supported.")
134-
.setResultType("failure").setError("tool '" + toolName + "' not supported");
133+
var result = ToolResultObject.failure("Tool '" + toolName + "' is not supported.",
134+
"tool '" + toolName + "' not supported");
135135
rpc.sendResponse(Long.parseLong(requestId), Map.of("result", result));
136136
return;
137137
}
@@ -145,19 +145,18 @@ private void handleToolCall(JsonRpcClient rpc, String requestId, JsonNode params
145145
if (result instanceof ToolResultObject tr) {
146146
toolResult = tr;
147147
} else {
148-
toolResult = new ToolResultObject().setResultType("success").setTextResultForLlm(
149-
result instanceof String s ? s : MAPPER.writeValueAsString(result));
148+
toolResult = ToolResultObject
149+
.success(result instanceof String s ? s : MAPPER.writeValueAsString(result));
150150
}
151151
rpc.sendResponse(Long.parseLong(requestId), Map.of("result", toolResult));
152152
} catch (Exception e) {
153153
LOG.log(Level.SEVERE, "Error sending tool result", e);
154154
}
155155
}).exceptionally(ex -> {
156156
try {
157-
var result = new ToolResultObject()
158-
.setTextResultForLlm(
159-
"Invoking this tool produced an error. Detailed information is not available.")
160-
.setResultType("failure").setError(ex.getMessage());
157+
var result = ToolResultObject.failure(
158+
"Invoking this tool produced an error. Detailed information is not available.",
159+
ex.getMessage());
161160
rpc.sendResponse(Long.parseLong(requestId), Map.of("result", result));
162161
} catch (Exception e) {
163162
LOG.log(Level.SEVERE, "Error sending tool error", e);

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

Lines changed: 51 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package com.github.copilot.sdk.json;
66

7-
import java.util.Collections;
87
import java.util.List;
98
import java.util.Map;
109

@@ -14,162 +13,95 @@
1413
/**
1514
* Result object returned from a tool execution.
1615
* <p>
17-
* This class represents the structured result of a tool invocation, including
16+
* This record represents the structured result of a tool invocation, including
1817
* text output, binary data, error information, and telemetry.
1918
*
2019
* <h2>Example: Success Result</h2>
2120
*
2221
* <pre>{@code
23-
* return new ToolResultObject().setResultType("success").setTextResultForLlm("File contents: " + content);
22+
* return ToolResultObject.success("File contents: " + content);
2423
* }</pre>
2524
*
2625
* <h2>Example: Error Result</h2>
2726
*
2827
* <pre>{@code
29-
* return new ToolResultObject().setResultType("error").setError("File not found: " + path);
28+
* return ToolResultObject.error("File not found: " + path);
3029
* }</pre>
3130
*
31+
* <h2>Example: Custom Result</h2>
32+
*
33+
* <pre>{@code
34+
* return new ToolResultObject("success", "Result text", null, null, null, null);
35+
* }</pre>
36+
*
37+
* @param resultType
38+
* the result type ("success" or "error"), defaults to "success"
39+
* @param textResultForLlm
40+
* the text result to be sent to the LLM
41+
* @param binaryResultsForLlm
42+
* the list of binary results to be sent to the LLM
43+
* @param error
44+
* the error message, or {@code null} if successful
45+
* @param sessionLog
46+
* the session log text
47+
* @param toolTelemetry
48+
* the tool telemetry data
3249
* @see ToolHandler
3350
* @see ToolBinaryResult
3451
* @since 1.0.0
3552
*/
3653
@JsonInclude(JsonInclude.Include.NON_NULL)
37-
public final class ToolResultObject {
38-
39-
@JsonProperty("textResultForLlm")
40-
private String textResultForLlm;
41-
42-
@JsonProperty("binaryResultsForLlm")
43-
private List<ToolBinaryResult> binaryResultsForLlm;
44-
45-
@JsonProperty("resultType")
46-
private String resultType = "success";
47-
48-
@JsonProperty("error")
49-
private String error;
50-
51-
@JsonProperty("sessionLog")
52-
private String sessionLog;
53-
54-
@JsonProperty("toolTelemetry")
55-
private Map<String, Object> toolTelemetry;
54+
public record ToolResultObject(@JsonProperty("resultType") String resultType,
55+
@JsonProperty("textResultForLlm") String textResultForLlm,
56+
@JsonProperty("binaryResultsForLlm") List<ToolBinaryResult> binaryResultsForLlm,
57+
@JsonProperty("error") String error, @JsonProperty("sessionLog") String sessionLog,
58+
@JsonProperty("toolTelemetry") Map<String, Object> toolTelemetry) {
5659

5760
/**
58-
* Gets the text result to be sent to the LLM.
59-
*
60-
* @return the text result
61-
*/
62-
public String getTextResultForLlm() {
63-
return textResultForLlm;
64-
}
65-
66-
/**
67-
* Sets the text result to be sent to the LLM.
61+
* Creates a success result with the given text.
6862
*
6963
* @param textResultForLlm
70-
* the text result
71-
* @return this result for method chaining
64+
* the text result to be sent to the LLM
65+
* @return a success result
7266
*/
73-
public ToolResultObject setTextResultForLlm(String textResultForLlm) {
74-
this.textResultForLlm = textResultForLlm;
75-
return this;
67+
public static ToolResultObject success(String textResultForLlm) {
68+
return new ToolResultObject("success", textResultForLlm, null, null, null, null);
7669
}
7770

7871
/**
79-
* Gets the binary results to be sent to the LLM.
80-
*
81-
* @return the list of binary results
82-
*/
83-
public List<ToolBinaryResult> getBinaryResultsForLlm() {
84-
return binaryResultsForLlm == null ? null : Collections.unmodifiableList(binaryResultsForLlm);
85-
}
86-
87-
/**
88-
* Sets binary results (images, files) to be sent to the LLM.
89-
*
90-
* @param binaryResultsForLlm
91-
* the list of binary results
92-
* @return this result for method chaining
93-
*/
94-
public ToolResultObject setBinaryResultsForLlm(List<ToolBinaryResult> binaryResultsForLlm) {
95-
this.binaryResultsForLlm = binaryResultsForLlm;
96-
return this;
97-
}
98-
99-
/**
100-
* Gets the result type.
101-
*
102-
* @return the result type ("success" or "error")
103-
*/
104-
public String getResultType() {
105-
return resultType;
106-
}
107-
108-
/**
109-
* Sets the result type.
110-
*
111-
* @param resultType
112-
* "success" or "error"
113-
* @return this result for method chaining
114-
*/
115-
public ToolResultObject setResultType(String resultType) {
116-
this.resultType = resultType;
117-
return this;
118-
}
119-
120-
/**
121-
* Gets the error message.
122-
*
123-
* @return the error message, or {@code null} if successful
124-
*/
125-
public String getError() {
126-
return error;
127-
}
128-
129-
/**
130-
* Sets an error message for failed tool execution.
72+
* Creates an error result with the given error message.
13173
*
13274
* @param error
13375
* the error message
134-
* @return this result for method chaining
76+
* @return an error result
13577
*/
136-
public ToolResultObject setError(String error) {
137-
this.error = error;
138-
return this;
78+
public static ToolResultObject error(String error) {
79+
return new ToolResultObject("error", "An error occurred.", null, error, null, null);
13980
}
14081

14182
/**
142-
* Gets the session log entry.
83+
* Creates an error result with both a text result and error message.
14384
*
144-
* @return the session log text
145-
*/
146-
public String getSessionLog() {
147-
return sessionLog;
148-
}
149-
150-
/**
151-
* Sets a log entry to be recorded in the session.
152-
*
153-
* @param sessionLog
154-
* the log entry
155-
* @return this result for method chaining
85+
* @param textResultForLlm
86+
* the text result to be sent to the LLM
87+
* @param error
88+
* the error message
89+
* @return an error result
15690
*/
157-
public ToolResultObject setSessionLog(String sessionLog) {
158-
this.sessionLog = sessionLog;
159-
return this;
91+
public static ToolResultObject error(String textResultForLlm, String error) {
92+
return new ToolResultObject("error", textResultForLlm, null, error, null, null);
16093
}
16194

16295
/**
163-
* Gets the tool telemetry data.
96+
* Creates a failure result with the given text and error message.
16497
*
165-
* @return the telemetry map
98+
* @param textResultForLlm
99+
* the text result to be sent to the LLM
100+
* @param error
101+
* the error message
102+
* @return a failure result
166103
*/
167-
public Map<String, Object> getToolTelemetry() {
168-
return toolTelemetry == null ? null : Collections.unmodifiableMap(toolTelemetry);
169-
}
170-
171-
public ToolResultObject setToolTelemetry(Map<String, Object> toolTelemetry) {
172-
this.toolTelemetry = toolTelemetry;
173-
return this;
104+
public static ToolResultObject failure(String textResultForLlm, String error) {
105+
return new ToolResultObject("failure", textResultForLlm, null, error, null, null);
174106
}
175107
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ void toolCallWithUnknownTool() throws Exception {
232232
void toolCallReturnsToolResultObjectDirectly() throws Exception {
233233
CopilotSession session = createSession("s1");
234234
var tool = ToolDefinition.create("my_tool", "A test tool", Map.of("type", "object"),
235-
invocation -> CompletableFuture.completedFuture(
236-
new ToolResultObject().setResultType("success").setTextResultForLlm("direct result")));
235+
invocation -> CompletableFuture.completedFuture(ToolResultObject.success("direct result")));
237236
session.registerTools(List.of(tool));
238237

239238
ObjectNode params = MAPPER.createObjectNode();

0 commit comments

Comments
 (0)