/*--------------------------------------------------------------------------------------------- * 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. *

* 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. * *

Example Usage

* *
{@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 args = invocation.getArguments();
 * 			// String location = (String) args.get("location");
 * 		});
 * }
* * @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. *

* 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 schema, ToolHandler handler) { return new ToolDefinition(name, description, schema, handler, null, null); } /** * Creates a tool definition that overrides a built-in CLI tool. *

* 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 schema, ToolHandler handler) { return new ToolDefinition(name, description, schema, handler, true, null); } /** * Creates a tool definition that skips the permission request. *

* 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 schema, ToolHandler handler) { return new ToolDefinition(name, description, schema, handler, null, true); } }