|
4 | 4 |
|
5 | 5 | package com.github.copilot.sdk.json; |
6 | 6 |
|
7 | | -import java.util.Collections; |
8 | 7 | import java.util.List; |
9 | 8 | import java.util.Map; |
10 | 9 |
|
|
14 | 13 | /** |
15 | 14 | * Result object returned from a tool execution. |
16 | 15 | * <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 |
18 | 17 | * text output, binary data, error information, and telemetry. |
19 | 18 | * |
20 | 19 | * <h2>Example: Success Result</h2> |
21 | 20 | * |
22 | 21 | * <pre>{@code |
23 | | - * return new ToolResultObject().setResultType("success").setTextResultForLlm("File contents: " + content); |
| 22 | + * return ToolResultObject.success("File contents: " + content); |
24 | 23 | * }</pre> |
25 | 24 | * |
26 | 25 | * <h2>Example: Error Result</h2> |
27 | 26 | * |
28 | 27 | * <pre>{@code |
29 | | - * return new ToolResultObject().setResultType("error").setError("File not found: " + path); |
| 28 | + * return ToolResultObject.error("File not found: " + path); |
30 | 29 | * }</pre> |
31 | 30 | * |
| 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 |
32 | 49 | * @see ToolHandler |
33 | 50 | * @see ToolBinaryResult |
34 | 51 | * @since 1.0.0 |
35 | 52 | */ |
36 | 53 | @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) { |
56 | 59 |
|
57 | 60 | /** |
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. |
68 | 62 | * |
69 | 63 | * @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 |
72 | 66 | */ |
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); |
76 | 69 | } |
77 | 70 |
|
78 | 71 | /** |
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. |
131 | 73 | * |
132 | 74 | * @param error |
133 | 75 | * the error message |
134 | | - * @return this result for method chaining |
| 76 | + * @return an error result |
135 | 77 | */ |
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); |
139 | 80 | } |
140 | 81 |
|
141 | 82 | /** |
142 | | - * Gets the session log entry. |
| 83 | + * Creates an error result with both a text result and error message. |
143 | 84 | * |
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 |
156 | 90 | */ |
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); |
160 | 93 | } |
161 | 94 |
|
162 | 95 | /** |
163 | | - * Gets the tool telemetry data. |
| 96 | + * Creates a failure result with the given text and error message. |
164 | 97 | * |
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 |
166 | 103 | */ |
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); |
174 | 106 | } |
175 | 107 | } |
0 commit comments