Skip to content

Commit 4f30838

Browse files
committed
refactor: update tool handling to use ToolDefinition and simplify SessionRequestBuilder
1 parent b27117b commit 4f30838

8 files changed

Lines changed: 23 additions & 268 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ void registerTools(List<ToolDefinition> tools) {
546546
toolHandlers.clear();
547547
if (tools != null) {
548548
for (ToolDefinition tool : tools) {
549-
toolHandlers.put(tool.getName(), tool);
549+
toolHandlers.put(tool.name(), tool);
550550
}
551551
}
552552
}

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

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

131131
ToolDefinition tool = session.getTool(toolName);
132-
if (tool == null || tool.getHandler() == null) {
132+
if (tool == null || tool.handler() == null) {
133133
var result = new ToolResultObject().setTextResultForLlm("Tool '" + toolName + "' is not supported.")
134134
.setResultType("failure").setError("tool '" + toolName + "' not supported");
135135
rpc.sendResponse(Long.parseLong(requestId), Map.of("result", result));
@@ -139,7 +139,7 @@ private void handleToolCall(JsonRpcClient rpc, String requestId, JsonNode params
139139
var invocation = new ToolInvocation().setSessionId(sessionId).setToolCallId(toolCallId)
140140
.setToolName(toolName).setArguments(arguments);
141141

142-
tool.getHandler().invoke(invocation).thenAccept(result -> {
142+
tool.handler().invoke(invocation).thenAccept(result -> {
143143
try {
144144
ToolResultObject toolResult;
145145
if (result instanceof ToolResultObject tr) {

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
package com.github.copilot.sdk;
66

7-
import java.util.stream.Collectors;
8-
97
import com.github.copilot.sdk.json.CreateSessionRequest;
108
import com.github.copilot.sdk.json.ResumeSessionConfig;
119
import com.github.copilot.sdk.json.ResumeSessionRequest;
1210
import com.github.copilot.sdk.json.SessionConfig;
13-
import com.github.copilot.sdk.json.ToolDef;
1411

1512
/**
1613
* Builds JSON-RPC request objects from session configuration.
@@ -41,10 +38,7 @@ static CreateSessionRequest buildCreateRequest(SessionConfig config) {
4138
request.setModel(config.getModel());
4239
request.setSessionId(config.getSessionId());
4340
request.setReasoningEffort(config.getReasoningEffort());
44-
request.setTools(config.getTools() != null
45-
? config.getTools().stream().map(t -> new ToolDef(t.getName(), t.getDescription(), t.getParameters()))
46-
.collect(Collectors.toList())
47-
: null);
41+
request.setTools(config.getTools());
4842
request.setSystemMessage(config.getSystemMessage());
4943
request.setAvailableTools(config.getAvailableTools());
5044
request.setExcludedTools(config.getExcludedTools());
@@ -83,10 +77,7 @@ static ResumeSessionRequest buildResumeRequest(String sessionId, ResumeSessionCo
8377

8478
request.setModel(config.getModel());
8579
request.setReasoningEffort(config.getReasoningEffort());
86-
request.setTools(config.getTools() != null
87-
? config.getTools().stream().map(t -> new ToolDef(t.getName(), t.getDescription(), t.getParameters()))
88-
.collect(Collectors.toList())
89-
: null);
80+
request.setTools(config.getTools());
9081
request.setSystemMessage(config.getSystemMessage());
9182
request.setAvailableTools(config.getAvailableTools());
9283
request.setExcludedTools(config.getExcludedTools());

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final class CreateSessionRequest {
3535
private String reasoningEffort;
3636

3737
@JsonProperty("tools")
38-
private List<ToolDef> tools;
38+
private List<ToolDefinition> tools;
3939

4040
@JsonProperty("systemMessage")
4141
private SystemMessageConfig systemMessage;
@@ -115,12 +115,12 @@ public void setReasoningEffort(String reasoningEffort) {
115115
}
116116

117117
/** Gets the tools. @return the tool definitions */
118-
public List<ToolDef> getTools() {
118+
public List<ToolDefinition> getTools() {
119119
return tools == null ? null : Collections.unmodifiableList(tools);
120120
}
121121

122122
/** Sets the tools. @param tools the tool definitions */
123-
public void setTools(List<ToolDef> tools) {
123+
public void setTools(List<ToolDefinition> tools) {
124124
this.tools = tools;
125125
}
126126

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public final class ResumeSessionRequest {
3636
private String reasoningEffort;
3737

3838
@JsonProperty("tools")
39-
private List<ToolDef> tools;
39+
private List<ToolDefinition> tools;
4040

4141
@JsonProperty("systemMessage")
4242
private SystemMessageConfig systemMessage;
@@ -119,12 +119,12 @@ public void setReasoningEffort(String reasoningEffort) {
119119
}
120120

121121
/** Gets the tools. @return the tool definitions */
122-
public List<ToolDef> getTools() {
122+
public List<ToolDefinition> getTools() {
123123
return tools == null ? null : Collections.unmodifiableList(tools);
124124
}
125125

126126
/** Sets the tools. @param tools the tool definitions */
127-
public void setTools(List<ToolDef> tools) {
127+
public void setTools(List<ToolDefinition> tools) {
128128
this.tools = tools;
129129
}
130130

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

Lines changed: 0 additions & 109 deletions
This file was deleted.

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

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

77
import java.util.Map;
88

9+
import com.fasterxml.jackson.annotation.JsonIgnore;
910
import com.fasterxml.jackson.annotation.JsonInclude;
1011
import com.fasterxml.jackson.annotation.JsonProperty;
1112

@@ -39,149 +40,21 @@
3940
* });
4041
* }</pre>
4142
*
43+
* @param name
44+
* the unique name of the tool
45+
* @param description
46+
* a description of what the tool does
47+
* @param parameters
48+
* the JSON Schema defining the tool's parameters
49+
* @param handler
50+
* the handler function to execute when invoked
4251
* @see SessionConfig#setTools(java.util.List)
4352
* @see ToolHandler
4453
* @since 1.0.0
4554
*/
4655
@JsonInclude(JsonInclude.Include.NON_NULL)
47-
public class ToolDefinition {
48-
49-
@JsonProperty("name")
50-
private String name;
51-
52-
@JsonProperty("description")
53-
private String description;
54-
55-
@JsonProperty("parameters")
56-
private Object parameters;
57-
58-
private transient ToolHandler handler;
59-
60-
/**
61-
* Creates an empty tool definition.
62-
* <p>
63-
* Use the setter methods to configure the tool.
64-
*/
65-
public ToolDefinition() {
66-
}
67-
68-
/**
69-
* Creates a tool definition with all properties.
70-
*
71-
* @param name
72-
* the unique name of the tool
73-
* @param description
74-
* a description of what the tool does
75-
* @param parameters
76-
* the JSON Schema defining the tool's parameters
77-
* @param handler
78-
* the handler function to execute when invoked
79-
*/
80-
public ToolDefinition(String name, String description, Object parameters, ToolHandler handler) {
81-
this.name = name;
82-
this.description = description;
83-
this.parameters = parameters;
84-
this.handler = handler;
85-
}
86-
87-
/**
88-
* Gets the tool name.
89-
*
90-
* @return the unique name of the tool
91-
*/
92-
public String getName() {
93-
return name;
94-
}
95-
96-
/**
97-
* Sets the tool name.
98-
* <p>
99-
* The name should be unique within a session and follow naming conventions
100-
* similar to function names (e.g., "get_user", "search_files").
101-
*
102-
* @param name
103-
* the unique name of the tool
104-
* @return this tool definition for method chaining
105-
*/
106-
public ToolDefinition setName(String name) {
107-
this.name = name;
108-
return this;
109-
}
110-
111-
/**
112-
* Gets the tool description.
113-
*
114-
* @return the description of what the tool does
115-
*/
116-
public String getDescription() {
117-
return description;
118-
}
119-
120-
/**
121-
* Sets the tool description.
122-
* <p>
123-
* The description helps the AI understand when and how to use the tool. Be
124-
* clear and specific about the tool's purpose and any constraints.
125-
*
126-
* @param description
127-
* the description of what the tool does
128-
* @return this tool definition for method chaining
129-
*/
130-
public ToolDefinition setDescription(String description) {
131-
this.description = description;
132-
return this;
133-
}
134-
135-
/**
136-
* Gets the parameter schema.
137-
*
138-
* @return the JSON Schema for the tool's parameters
139-
*/
140-
public Object getParameters() {
141-
return parameters;
142-
}
143-
144-
/**
145-
* Sets the parameter schema.
146-
* <p>
147-
* The schema should follow JSON Schema format and define the structure of
148-
* arguments the tool accepts. This is typically a {@code Map} with "type",
149-
* "properties", and "required" fields.
150-
*
151-
* @param parameters
152-
* the JSON Schema for the tool's parameters
153-
* @return this tool definition for method chaining
154-
*/
155-
public ToolDefinition setParameters(Object parameters) {
156-
this.parameters = parameters;
157-
return this;
158-
}
159-
160-
/**
161-
* Gets the tool handler.
162-
*
163-
* @return the handler function that executes when the tool is invoked
164-
*/
165-
public ToolHandler getHandler() {
166-
return handler;
167-
}
168-
169-
/**
170-
* Sets the tool handler.
171-
* <p>
172-
* The handler is called when the assistant invokes this tool. It receives a
173-
* {@link ToolInvocation} with the arguments and should return a
174-
* {@code CompletableFuture} with the result.
175-
*
176-
* @param handler
177-
* the handler function
178-
* @return this tool definition for method chaining
179-
* @see ToolHandler
180-
*/
181-
public ToolDefinition setHandler(ToolHandler handler) {
182-
this.handler = handler;
183-
return this;
184-
}
56+
public record ToolDefinition(@JsonProperty("name") String name, @JsonProperty("description") String description,
57+
@JsonProperty("parameters") Object parameters, @JsonIgnore ToolHandler handler) {
18558

18659
/**
18760
* Creates a tool definition with a JSON schema for parameters.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void testBuildResumeRequestWithTools() {
8080

8181
assertNotNull(request.getTools());
8282
assertEquals(1, request.getTools().size());
83-
assertEquals("my_tool", request.getTools().get(0).getName());
83+
assertEquals("my_tool", request.getTools().get(0).name());
8484
}
8585

8686
@Test

0 commit comments

Comments
 (0)