Class AgentHelpers

java.lang.Object
dev.langchain4j.agentic.internal.AgentHelpers

public final class AgentHelpers extends Object
Locates the helper methods of a declarative agent: the static methods of the agent type, as declared on it or inherited, and the instance methods of its helper objects, searched in the class hierarchy of each object.

For one annotation an agent can have a single helper, unless the annotation is allowed several times: a static method on the type together with an instance method on a helper object, or two instance methods, are a configuration error.

A helper declared by the agent type itself that is not static needs one of the helper objects to be invoked on, an instance of its declaring class, typically the agent bean of a container: checkNonStaticHelpers(Class, List) rejects the agent otherwise, instead of skipping the helper silently. An instance helper can have any visibility but private, and cannot be final unless its class is final too: a container invokes it through the proxy of its bean, which cannot override such a method, so it would run on the proxy itself, without the injected fields of the bean, while no proxy subclasses a final class.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final List<Class<? extends Annotation>>
    The annotations marking the helper methods of a declarative agent, as opposed to the ones marking its agent method and the parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
     
    static void
    checkNonStaticHelpers(Class<?> agentType, List<Object> helperObjects)
    Checks that every non-static method of the agent type annotated with a helper annotation has an object to be invoked on: one of the helper objects must be an instance of its declaring class, typically the agent itself when it is the bean of a container.
    static List<Method>
    methodsAnnotatedWith(Class<?> type, Collection<Class<? extends Annotation>> annotations)
    The methods, static or not, annotated with any of the given annotations declared by the given type, its superclasses and its interfaces, made accessible, with the same rule as instanceMethodsAnnotatedWith(Class, Class) for the overridden methods: an override without the annotation does not hide the annotated method it overrides, so that the method of a class is found on a proxy of the class too.
    of(Class<?> agentType, List<Object> helperObjects)
    The helpers of an agent: its static methods and the instance methods of the given helper objects, which are all the ones of the agent, so a non-static helper of the type without an object to be invoked on is rejected.
    select(Class<? extends Annotation> annotation)
    Selects the single helper method annotated with the given annotation.
    select(Class<? extends Annotation> annotation, Predicate<Method> filter)
    Selects the single helper method annotated with the given annotation and matching the given filter.
    selectAll(Class<? extends Annotation> annotation)
    Selects all the helper methods annotated with the given annotation, for the annotations allowed several times on one agent.
    staticOnly(Class<?> agentType)
    The helpers of an agent without helper objects: its static methods only.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HELPER_ANNOTATIONS

      public static final List<Class<? extends Annotation>> HELPER_ANNOTATIONS
      The annotations marking the helper methods of a declarative agent, as opposed to the ones marking its agent method and the parameters.
  • Method Details

    • staticOnly

      public static AgentHelpers staticOnly(Class<?> agentType)
      The helpers of an agent without helper objects: its static methods only.
    • of

      public static AgentHelpers of(Class<?> agentType, List<Object> helperObjects)
      The helpers of an agent: its static methods and the instance methods of the given helper objects, which are all the ones of the agent, so a non-static helper of the type without an object to be invoked on is rejected.
    • checkNonStaticHelpers

      public static void checkNonStaticHelpers(Class<?> agentType, List<Object> helperObjects)
      Checks that every non-static method of the agent type annotated with a helper annotation has an object to be invoked on: one of the helper objects must be an instance of its declaring class, typically the agent itself when it is the bean of a container.
      Throws:
      AgenticSystemConfigurationException - if such a method has none, instead of skipping it silently
    • agentType

      public Class<?> agentType()
    • select

      public Optional<HelperMethod> select(Class<? extends Annotation> annotation)
      Selects the single helper method annotated with the given annotation.
      Throws:
      AgenticSystemConfigurationException - if there is more than one such method
    • select

      public Optional<HelperMethod> select(Class<? extends Annotation> annotation, Predicate<Method> filter)
      Selects the single helper method annotated with the given annotation and matching the given filter.
      Throws:
      AgenticSystemConfigurationException - if there is more than one such method
    • selectAll

      public List<HelperMethod> selectAll(Class<? extends Annotation> annotation)
      Selects all the helper methods annotated with the given annotation, for the annotations allowed several times on one agent.
    • methodsAnnotatedWith

      public static List<Method> methodsAnnotatedWith(Class<?> type, Collection<Class<? extends Annotation>> annotations)
      The methods, static or not, annotated with any of the given annotations declared by the given type, its superclasses and its interfaces, made accessible, with the same rule as instanceMethodsAnnotatedWith(Class, Class) for the overridden methods: an override without the annotation does not hide the annotated method it overrides, so that the method of a class is found on a proxy of the class too.