Record Class AgenticServices.AgentConfigurator

java.lang.Object
java.lang.Record
dev.langchain4j.agentic.AgenticServices.AgentConfigurator
Enclosing class:
AgenticServices

public static record AgenticServices.AgentConfigurator(Consumer<AgenticServices.DeclarativeAgentCreationContext<?>> configurator, Function<Class<?>,Object> subAgentResolver, Function<InternalAgent, Object> agentInstanceFactory, Supplier<Object> defaultMemoryIdSupplier, Function<Class<?>,List<?>> helperObjectsResolver) extends Record
The hooks through which an integration takes part in the creation of a declarative agentic system.

The configurator is called for every agent of the system, AI agents and decision agents alike, with a AgenticServices.DeclarativeAgentCreationContext. For an AI agent its agentBuilder() is set and its decisionAgentBuilder() is null, for a decision agent it is the other way round, so a configurator must check which builder it received before using it. The agentInstanceFactory is applied to both kinds of agents.

The helperObjectsResolver is called with the type of every agent of the system, the root agent and each nested agent, and returns its helper objects: the objects whose instance methods, annotated with the declarative annotations like @ExitCondition or @ChatModelSupplier, configure the agent instead of, or next to, the static methods of the agent type. It can return null or an empty list for an agent without helper objects. It is how a dependency injection container provides the helpers of an agent as methods of a managed bean, typically an instance of the agent type itself.

The resolver is called once for each agent, when the agentic system is built. The returned objects are then shared by all the invocations of the agent, also by the ones running at the same time, so they must be thread-safe, or be proxies finding the current instance at each call, like the client proxies of the normal-scoped beans of a CDI container. Their annotated methods are found and invoked through reflection, on the class of each object, its superclasses and its interfaces. They can have any visibility but private, and cannot be final unless their class is final too: a container invokes them through the proxy of its bean, which cannot override such methods, so they would run on the proxy, without its injected fields, while no proxy subclasses a final class. A helper method 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: the agentic system fails to build otherwise, instead of skipping the helper silently.

Create a configurator with builder(), setting only the hooks needed: the constructors are kept, but each hook added to the record adds a positional component to them.

  • Constructor Details

  • Method Details

    • empty

      public static AgenticServices.AgentConfigurator empty()
    • builder

      A builder of a configurator: every hook is optional, and the configurator does nothing by default.
    • helperObjects

      public List<Object> helperObjects(Class<?> agentType)
      The helper objects of the given agent type returned by the helperObjectsResolver, or an empty list when there is no resolver or it returns nothing.
    • 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.
    • configurator

      Returns the value of the configurator record component.
      Returns:
      the value of the configurator record component
    • subAgentResolver

      public Function<Class<?>,Object> subAgentResolver()
      Returns the value of the subAgentResolver record component.
      Returns:
      the value of the subAgentResolver record component
    • agentInstanceFactory

      public Function<InternalAgent, Object> agentInstanceFactory()
      Returns the value of the agentInstanceFactory record component.
      Returns:
      the value of the agentInstanceFactory record component
    • defaultMemoryIdSupplier

      public Supplier<Object> defaultMemoryIdSupplier()
      Returns the value of the defaultMemoryIdSupplier record component.
      Returns:
      the value of the defaultMemoryIdSupplier record component
    • helperObjectsResolver

      public Function<Class<?>,List<?>> helperObjectsResolver()
      Returns the value of the helperObjectsResolver record component.
      Returns:
      the value of the helperObjectsResolver record component