forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolDefinition.java
More file actions
206 lines (187 loc) · 6.08 KB
/
ToolDefinition.java
File metadata and controls
206 lines (187 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk.json;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Defines a tool that can be invoked by the AI assistant.
* <p>
* Tools extend the assistant's capabilities by allowing it to call back into
* your application to perform actions or retrieve information. Each tool has a
* name, description, parameter schema, and a handler function that executes
* when the tool is invoked.
*
* <h2>Example Usage</h2>
*
* <pre>{@code
* // Define a record for your tool's arguments
* record WeatherArgs(String location) {
* }
*
* var tool = ToolDefinition.create("get_weather", "Get the current weather for a location",
* Map.of("type", "object", "properties",
* Map.of("location", Map.of("type", "string", "description", "City name")), "required",
* List.of("location")),
* invocation -> {
* // Type-safe access with records (recommended)
* WeatherArgs args = invocation.getArgumentsAs(WeatherArgs.class);
* return CompletableFuture.completedFuture(getWeatherData(args.location()));
*
* // Or use Map-based access
* // Map<String, Object> args = invocation.getArguments();
* // String location = (String) args.get("location");
* });
* }</pre>
*
* @see SessionConfig#setTools(java.util.List)
* @see ToolHandler
* @since 1.0.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ToolDefinition {
@JsonProperty("name")
private String name;
@JsonProperty("description")
private String description;
@JsonProperty("parameters")
private Object parameters;
private transient ToolHandler handler;
/**
* Creates an empty tool definition.
* <p>
* Use the setter methods to configure the tool.
*/
public ToolDefinition() {
}
/**
* Creates a tool definition with all properties.
*
* @param name
* the unique name of the tool
* @param description
* a description of what the tool does
* @param parameters
* the JSON Schema defining the tool's parameters
* @param handler
* the handler function to execute when invoked
*/
public ToolDefinition(String name, String description, Object parameters, ToolHandler handler) {
this.name = name;
this.description = description;
this.parameters = parameters;
this.handler = handler;
}
/**
* Gets the tool name.
*
* @return the unique name of the tool
*/
public String getName() {
return name;
}
/**
* Sets the tool name.
* <p>
* The name should be unique within a session and follow naming conventions
* similar to function names (e.g., "get_user", "search_files").
*
* @param name
* the unique name of the tool
* @return this tool definition for method chaining
*/
public ToolDefinition setName(String name) {
this.name = name;
return this;
}
/**
* Gets the tool description.
*
* @return the description of what the tool does
*/
public String getDescription() {
return description;
}
/**
* Sets the tool description.
* <p>
* The description helps the AI understand when and how to use the tool. Be
* clear and specific about the tool's purpose and any constraints.
*
* @param description
* the description of what the tool does
* @return this tool definition for method chaining
*/
public ToolDefinition setDescription(String description) {
this.description = description;
return this;
}
/**
* Gets the parameter schema.
*
* @return the JSON Schema for the tool's parameters
*/
public Object getParameters() {
return parameters;
}
/**
* Sets the parameter schema.
* <p>
* The schema should follow JSON Schema format and define the structure of
* arguments the tool accepts. This is typically a {@code Map} with "type",
* "properties", and "required" fields.
*
* @param parameters
* the JSON Schema for the tool's parameters
* @return this tool definition for method chaining
*/
public ToolDefinition setParameters(Object parameters) {
this.parameters = parameters;
return this;
}
/**
* Gets the tool handler.
*
* @return the handler function that executes when the tool is invoked
*/
public ToolHandler getHandler() {
return handler;
}
/**
* Sets the tool handler.
* <p>
* The handler is called when the assistant invokes this tool. It receives a
* {@link ToolInvocation} with the arguments and should return a
* {@code CompletableFuture} with the result.
*
* @param handler
* the handler function
* @return this tool definition for method chaining
* @see ToolHandler
*/
public ToolDefinition setHandler(ToolHandler handler) {
this.handler = handler;
return this;
}
/**
* Creates a tool definition with a JSON schema for parameters.
* <p>
* This is a convenience factory method for creating tools with a
* {@code Map}-based parameter schema.
*
* @param name
* the unique name of the tool
* @param description
* a description of what the tool does
* @param schema
* the JSON Schema as a {@code Map}
* @param handler
* the handler function to execute when invoked
* @return a new tool definition
*/
public static ToolDefinition create(String name, String description, Map<String, Object> schema,
ToolHandler handler) {
return new ToolDefinition(name, description, schema, handler);
}
}