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
136 lines (129 loc) · 5.44 KB
/
ToolDefinition.java
File metadata and controls
136 lines (129 loc) · 5.44 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk.json;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnore;
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>
*
* @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
* @param overridesBuiltInTool
* when {@code true}, indicates that this tool intentionally
* overrides a built-in CLI tool with the same name; {@code null} or
* {@code false} means the tool is purely custom
* @param skipPermission
* when {@code true}, the CLI skips the permission request for this
* tool invocation; {@code null} or {@code false} uses normal
* permission handling
* @see SessionConfig#setTools(java.util.List)
* @see ToolHandler
* @since 1.0.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ToolDefinition(@JsonProperty("name") String name, @JsonProperty("description") String description,
@JsonProperty("parameters") Object parameters, @JsonIgnore ToolHandler handler,
@JsonProperty("overridesBuiltInTool") Boolean overridesBuiltInTool,
@JsonProperty("skipPermission") Boolean skipPermission) {
/**
* 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, null, null);
}
/**
* Creates a tool definition that overrides a built-in CLI tool.
* <p>
* Use this factory method when you want your custom tool to replace a built-in
* tool (e.g., {@code grep}, {@code read_file}) with the same name. Setting
* {@code overridesBuiltInTool} to {@code true} signals to the CLI that this is
* intentional.
*
* @param name
* the name of the built-in tool to override
* @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 with the override flag set
* @since 1.0.11
*/
public static ToolDefinition createOverride(String name, String description, Map<String, Object> schema,
ToolHandler handler) {
return new ToolDefinition(name, description, schema, handler, true, null);
}
/**
* Creates a tool definition that skips the permission request.
* <p>
* Use this factory method when the tool is safe to invoke without user
* permission confirmation. Setting {@code skipPermission} to {@code true}
* signals to the CLI that no permission check is needed.
*
* @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 with permission skipping enabled
* @since 1.2.0
*/
public static ToolDefinition createSkipPermission(String name, String description, Map<String, Object> schema,
ToolHandler handler) {
return new ToolDefinition(name, description, schema, handler, null, true);
}
}