Object Tool.Companion

    • Constructor Detail

    • Method Detail

      • 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 name
        description - Tool description
        handler - 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 name
        description - Tool description
        metadata - 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 name
        description - Tool description
        inputSchema - Schema describing the input parameters
        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.Metadata metadata, Tool.Handler handler)

        Create a fully configured tool (Java-friendly).

        Parameters:
        name - Tool name
        description - Tool description
        inputSchema - Schema describing the input parameters
        metadata - Tool metadata
        handler - 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 method
        method - The method to wrap as a tool
        objectMapper - 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 methods
        objectMapper - 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 methods
        objectMapper - 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 wrap
        valueComputer - 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 tree
        tools - The list of tools to format
        Returns:

        A formatted tree string, or a message if no tools are present