Object Tool.Companion
-
- All Implemented Interfaces:
-
com.embabel.agent.api.tool.ArtifactSinkFactory,com.embabel.agent.api.tool.MethodToolFactory,com.embabel.agent.api.tool.ReplanningToolFactory,com.embabel.agent.api.tool.TypedToolFactory
public class Tool.Companion implements TypedToolFactory, MethodToolFactory, ReplanningToolFactory, ArtifactSinkFactory
-
-
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). static <I extends Any, O extends Any> ToolfromFunction(String name, String description, Class<I> inputType, Class<O> outputType, Function<I, O> function)Create a tool with strongly typed input and output (Java-friendly). static <I extends Any, O extends Any> ToolfromFunction(String name, String description, Class<I> inputType, Class<O> outputType, Tool.Metadata metadata, Function<I, O> function)Create a tool with strongly typed input and output with custom metadata (Java-friendly). static <I extends Any, O extends Any> ToolfromFunction(String name, String description, Class<I> inputType, Class<O> outputType, Tool.Metadata metadata, ObjectMapper objectMapper, Function<I, O> function)Create a tool with strongly typed input and output with custom ObjectMapper (Java-friendly). final static List<Tool>fromInstance(Object instance)static List<Tool>fromInstance(Object instance, ObjectMapper objectMapper)Create Tools from all methods annotated with LlmTool on an instance. final static List<Tool>safelyFromInstance(Object instance)static List<Tool>safelyFromInstance(Object instance, ObjectMapper objectMapper)Safely create Tools from an instance, returning empty list if no annotated methods found. static ToolreplanAlways(Tool tool)Make this tool always replan after execution, adding the artifact to the blackboard. 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. 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. static <T extends Any> DelegatingToolreplanAndAdd(Tool tool, Function1<T, Object> valueComputer)Replan and add the object returned by the valueComputer to the blackboard. final static StringformatToolTree(String name, List<Tool> tools)Format a list of tools as an ASCII tree structure. static <T extends Any> ToolsinkArtifacts(Tool tool, Class<T> clazz, ArtifactSink sink)Wrap a tool to sink artifacts of the specified type to the given sink. static <T extends Any> ToolsinkArtifacts(Tool tool, Class<T> clazz, ArtifactSink sink, Function1<T, Boolean> filter, Function1<T, Object> transform)Wrap a tool to sink artifacts of the specified type to the given sink, with optional filtering and transformation. static ToolpublishToBlackboard(Tool tool)Wrap a tool to publish all artifacts to the blackboard. static <T extends Any> ToolpublishToBlackboard(Tool tool, Class<T> clazz)Wrap a tool to publish artifacts of the specified type to the blackboard. static <T extends Any> ToolpublishToBlackboard(Tool tool, Class<T> clazz, Function1<T, Boolean> filter, Function1<T, Object> transform)Wrap a tool to publish artifacts of the specified type to the blackboard, with optional filtering and transformation. -
-
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
-
fromFunction
static <I extends Any, O extends Any> Tool fromFunction(String name, String description, Class<I> inputType, Class<O> outputType, Function<I, O> function)
Create a tool with strongly typed input and output (Java-friendly).
- Parameters:
name- Tool namedescription- Tool descriptioninputType- Class of the input typeoutputType- Class of the output typefunction- Function that processes typed input and returns typed output- Returns:
A new Tool instance
-
fromFunction
static <I extends Any, O extends Any> Tool fromFunction(String name, String description, Class<I> inputType, Class<O> outputType, Tool.Metadata metadata, Function<I, O> function)
Create a tool with strongly typed input and output with custom metadata (Java-friendly).
- Parameters:
name- Tool namedescription- Tool descriptioninputType- Class of the input typeoutputType- Class of the output typemetadata- Tool metadatafunction- Function that processes typed input and returns typed output- Returns:
A new Tool instance
-
fromFunction
static <I extends Any, O extends Any> Tool fromFunction(String name, String description, Class<I> inputType, Class<O> outputType, Tool.Metadata metadata, ObjectMapper objectMapper, Function<I, O> function)
Create a tool with strongly typed input and output with custom ObjectMapper (Java-friendly).
- Parameters:
name- Tool namedescription- Tool descriptioninputType- Class of the input typeoutputType- Class of the output typemetadata- Tool metadataobjectMapper- ObjectMapper for JSON serialization/deserializationfunction- Function that processes typed input and returns typed output- Returns:
A new Tool instance
-
fromInstance
final static List<Tool> fromInstance(Object instance)
-
fromInstance
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 UnfoldingTools or MatryoshkaTools, returns a single UnfoldingTool 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 UnfoldingTool if @UnfoldingTools/@MatryoshkaTools present)
-
safelyFromInstance
final static List<Tool> safelyFromInstance(Object instance)
-
safelyFromInstance
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
-
replanAlways
static Tool replanAlways(Tool tool)
Make this tool always replan after execution, adding the artifact to the blackboard.
-
conditionalReplan
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
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
static <T extends Any> DelegatingTool replanAndAdd(Tool tool, Function1<T, Object> valueComputer)
Replan and add the object returned by the valueComputer 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. UnfoldingTools 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
-
sinkArtifacts
static <T extends Any> Tool sinkArtifacts(Tool tool, Class<T> clazz, ArtifactSink sink)
Wrap a tool to sink artifacts of the specified type to the given sink. Handles both single artifacts and Iterables.
- Parameters:
tool- The tool to wrapclazz- The class of artifacts to capturesink- Where to send captured artifacts
-
sinkArtifacts
static <T extends Any> Tool sinkArtifacts(Tool tool, Class<T> clazz, ArtifactSink sink, Function1<T, Boolean> filter, Function1<T, Object> transform)
Wrap a tool to sink artifacts of the specified type to the given sink, with optional filtering and transformation.
- Parameters:
tool- The tool to wrapclazz- The class of artifacts to capturesink- Where to send captured artifactsfilter- Predicate to filter which artifacts to capturetransform- Function to transform artifacts before sinking
-
publishToBlackboard
static Tool publishToBlackboard(Tool tool)
Wrap a tool to publish all artifacts to the blackboard.
- Parameters:
tool- The tool to wrap
-
publishToBlackboard
static <T extends Any> Tool publishToBlackboard(Tool tool, Class<T> clazz)
Wrap a tool to publish artifacts of the specified type to the blackboard.
- Parameters:
tool- The tool to wrapclazz- The class of artifacts to publish
-
publishToBlackboard
static <T extends Any> Tool publishToBlackboard(Tool tool, Class<T> clazz, Function1<T, Boolean> filter, Function1<T, Object> transform)
Wrap a tool to publish artifacts of the specified type to the blackboard, with optional filtering and transformation.
- Parameters:
tool- The tool to wrapclazz- The class of artifacts to publishfilter- Predicate to filter which artifacts to publishtransform- Function to transform artifacts before publishing
-
-
-
-