Object Tool.Companion
-
- All Implemented Interfaces:
public class Tool.Companion
-
-
Field Summary
Fields Modifier and Type Field Description public final static Tool.CompanionINSTANCE
-
Method Summary
Modifier and Type Method Description final Toolof(String name, String description, Tool.InputSchema inputSchema, Tool.Metadata metadata, Tool.Function function)Create a tool from a function. final Toolof(String name, String description, Tool.Metadata metadata, Tool.Function function)Create a tool with no parameters. final static Toolcreate(String name, String description, Tool.Handler handler)Create a tool with no parameters (Java-friendly). final static Toolcreate(String name, String description, Tool.Metadata metadata, Tool.Handler handler)Create a tool with custom metadata (Java-friendly). final static Toolcreate(String name, String description, Tool.InputSchema inputSchema, Tool.Handler handler)Create a tool with input schema (Java-friendly). final static Toolcreate(String name, String description, Tool.InputSchema inputSchema, Tool.Metadata metadata, Tool.Handler handler)Create a fully configured tool (Java-friendly). final ToolfromMethod(Object instance, KFunction<?> method, ObjectMapper objectMapper)Create a Tool from a method annotated with com.embabel.agent.api.annotation.LlmTool. final static List<Tool>fromInstance(Object instance, ObjectMapper objectMapper)Create Tools from all methods annotated with LlmTool on an instance. final static List<Tool>fromInstance(Object instance)Create Tools from all methods annotated with LlmTool on an instance. final static List<Tool>safelyFromInstance(Object instance, ObjectMapper objectMapper)Safely create Tools from an instance, returning empty list if no annotated methods found. final static List<Tool>safelyFromInstance(Object instance)Safely create Tools from an instance, returning empty list if no annotated methods found. final static ToolreplanAlways(Tool tool)Make this tool always replan after execution, adding the artifact to the blackboard. final static <T extends Any> DelegatingToolconditionalReplan(Tool tool, Function2<T, ReplanContext, ReplanDecision> decider)When the decider returns a ReplanDecision, replan after execution, adding the artifact to the blackboard along with any additional updates from the decision. final static <T extends Any> DelegatingToolreplanWhen(Tool tool, Function1<T, Boolean> predicate)When the predicate matches the tool result artifact, replan, adding the artifact to the blackboard. final static <T extends Any> DelegatingToolreplanAndAdd(Tool tool, Function1<T, Object> valueComputer)Replan and add the object returned by the predicate to the blackboard. final static StringformatToolTree(String name, List<Tool> tools)Format a list of tools as an ASCII tree structure. -
-
Method Detail
-
of
final Tool of(String name, String description, Tool.InputSchema inputSchema, Tool.Metadata metadata, Tool.Function function)
Create a tool from a function.
-
of
final Tool of(String name, String description, Tool.Metadata metadata, Tool.Function function)
Create a tool with no parameters.
-
create
final static Tool create(String name, String description, Tool.Handler handler)
Create a tool with no parameters (Java-friendly). This method is easier to call from Java as it uses the Handler interface.
Example:
Tool tool = Tool.create("greet", "Greets user", input -> Tool.Result.text("Hello!"));- Parameters:
name- Tool namedescription- Tool descriptionhandler- Handler that processes input and returns a result- Returns:
A new Tool instance
-
create
final static Tool create(String name, String description, Tool.Metadata metadata, Tool.Handler handler)
Create a tool with custom metadata (Java-friendly).
- Parameters:
name- Tool namedescription- Tool descriptionmetadata- Tool metadata (e.g.handler- Handler that processes input and returns a result- Returns:
A new Tool instance
-
create
final static Tool create(String name, String description, Tool.InputSchema inputSchema, Tool.Handler handler)
Create a tool with input schema (Java-friendly).
- Parameters:
name- Tool namedescription- Tool descriptioninputSchema- Schema describing the input parametershandler- Handler that processes input and returns a result- Returns:
A new Tool instance
-
create
final static Tool create(String name, String description, Tool.InputSchema inputSchema, Tool.Metadata metadata, Tool.Handler handler)
Create a fully configured tool (Java-friendly).
- Parameters:
name- Tool namedescription- Tool descriptioninputSchema- Schema describing the input parametersmetadata- Tool metadatahandler- Handler that processes input and returns a result- Returns:
A new Tool instance
-
fromMethod
final Tool fromMethod(Object instance, KFunction<?> method, ObjectMapper objectMapper)
Create a Tool from a method annotated with com.embabel.agent.api.annotation.LlmTool.
- Parameters:
instance- The object instance containing the methodmethod- The method to wrap as a toolobjectMapper- ObjectMapper for JSON parsing (optional)- Returns:
A Tool that invokes the method
-
fromInstance
@JvmOverloads() final static List<Tool> fromInstance(Object instance, ObjectMapper objectMapper)
Create Tools from all methods annotated with LlmTool on an instance.
If the instance's class is annotated with @MatryoshkaTools, returns a single MatryoshkaTool containing all the inner tools. Otherwise, returns individual tools for each annotated method.
- Parameters:
instance- The object instance to scan for annotated methodsobjectMapper- ObjectMapper for JSON parsing (optional)- Returns:
List of Tools, one for each annotated method (or single MatryoshkaTool if @MatryoshkaTools present)
-
fromInstance
@JvmOverloads() final static List<Tool> fromInstance(Object instance)
Create Tools from all methods annotated with LlmTool on an instance.
If the instance's class is annotated with @MatryoshkaTools, returns a single MatryoshkaTool containing all the inner tools. Otherwise, returns individual tools for each annotated method.
- Parameters:
instance- The object instance to scan for annotated methods- Returns:
List of Tools, one for each annotated method (or single MatryoshkaTool if @MatryoshkaTools present)
-
safelyFromInstance
@JvmOverloads() final static List<Tool> safelyFromInstance(Object instance, ObjectMapper objectMapper)
Safely create Tools from an instance, returning empty list if no annotated methods found. This is useful when you want to scan an object that may or may not have tool methods.
- Parameters:
instance- The object instance to scan for annotated methodsobjectMapper- ObjectMapper for JSON parsing (optional)- Returns:
List of Tools, or empty list if no annotated methods found
-
safelyFromInstance
@JvmOverloads() final static List<Tool> safelyFromInstance(Object instance)
Safely create Tools from an instance, returning empty list if no annotated methods found. This is useful when you want to scan an object that may or may not have tool methods.
- Parameters:
instance- The object instance to scan for annotated methods- Returns:
List of Tools, or empty list if no annotated methods found
-
replanAlways
final static Tool replanAlways(Tool tool)
Make this tool always replan after execution, adding the artifact to the blackboard.
-
conditionalReplan
final static <T extends Any> DelegatingTool conditionalReplan(Tool tool, Function2<T, ReplanContext, ReplanDecision> decider)
When the decider returns a ReplanDecision, replan after execution, adding the artifact to the blackboard along with any additional updates from the decision. The decider receives the artifact cast to type T and the replan context. If the artifact is null or cannot be cast to T, the decider is not called.
-
replanWhen
final static <T extends Any> DelegatingTool replanWhen(Tool tool, Function1<T, Boolean> predicate)
When the predicate matches the tool result artifact, replan, adding the artifact to the blackboard. The predicate receives the artifact cast to type T. If the artifact is null or cannot be cast to T, returns normally.
-
replanAndAdd
final static <T extends Any> DelegatingTool replanAndAdd(Tool tool, Function1<T, Object> valueComputer)
Replan and add the object returned by the predicate to the blackboard.
- Parameters:
tool- The tool to wrapvalueComputer- Function that takes the artifact of type T and returns an object to add to the blackboard, or null to not replan
-
formatToolTree
final static String formatToolTree(String name, List<Tool> tools)
Format a list of tools as an ASCII tree structure. MatryoshkaTools are expanded recursively to show their inner tools.
- Parameters:
name- The name to display at the root of the treetools- The list of tools to format- Returns:
A formatted tree string, or a message if no tools are present
-
-
-
-