Record Class ToolDefinition

java.lang.Object
java.lang.Record
com.github.copilot.sdk.json.ToolDefinition
Record Components:
name - the unique name of the tool
description - a description of what the tool does
parameters - the JSON Schema defining the tool's parameters
handler - the handler function to execute when invoked

public record ToolDefinition(String name, String description, Object parameters, ToolHandler handler) extends Record
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


 // 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");
 		});
 
Since:
1.0.0
See Also:
  • Constructor Details

    • ToolDefinition

      public ToolDefinition(String name, String description, Object parameters, ToolHandler handler)
      Creates an instance of a ToolDefinition record class.
      Parameters:
      name - the value for the name record component
      description - the value for the description record component
      parameters - the value for the parameters record component
      handler - the value for the handler record component
  • Method Details

    • create

      public static ToolDefinition create(String name, String description, Map<String,Object> schema, ToolHandler handler)
      Creates a tool definition with a JSON schema for parameters.

      This is a convenience factory method for creating tools with a Map-based parameter schema.

      Parameters:
      name - the unique name of the tool
      description - a description of what the tool does
      schema - the JSON Schema as a Map
      handler - the handler function to execute when invoked
      Returns:
      a new tool definition
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component
    • description

      public String description()
      Returns the value of the description record component.
      Returns:
      the value of the description record component
    • parameters

      public Object parameters()
      Returns the value of the parameters record component.
      Returns:
      the value of the parameters record component
    • handler

      public ToolHandler handler()
      Returns the value of the handler record component.
      Returns:
      the value of the handler record component