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 AI agent and decision agent of the system, and for every agent orchestrating subagents through a planner: a sequence, a loop, a conditional, a parallel or a parallel mapper agent, a supervisor and a planner agent. It is called with a AgenticServices.DeclarativeAgentCreationContext, after the declarative configuration of the agent has been applied and before the agent is built. The context provides the builder of the agent through one of three methods, and the other two return null: agentBuilder() for an AI agent, decisionAgentBuilder() for a decision agent and agenticService() for an agent orchestrating subagents. So a configurator must check which builder it received before using it, and do nothing with a context providing none of the builders it knows, since other kinds of agents can be added to these. The agentInstanceFactory is applied to the same agents, and to an A2A or MCP client agent whose type is a class, which cannot be created without it, while one whose type is an interface is always a JDK proxy. What the factory must return for such an agent is described on A2AClientBuilder.agentInstanceFactory(Function) and McpClientBuilder.agentInstanceFactory(Function).

It is how an integration registers something on every such agent, like an AgentListener: a listener is notified of the events of the agent it is registered on, and of the agents below it when it is inherited, but never of the events of the agents containing it, like the model calls that a supervisor makes to choose its subagents.

The helperObjectsResolver is called with the type of every agent that the system builds, 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 helperObjectsResolver 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.

The subAgentResolver is called with each type listed among the subagents of a declarative agent, and returns the subagent to use for it, or null to let the system build it from that type. When it returns an instance of a class with an @Agent or @HumanInTheLoop method, a non-AI agent or a human-in-the-loop object, the helper objects of the subagent are that instance followed by the ones that the helperObjectsResolver returns for the listed type, not for the class of the instance, which can be the proxy of a container bean. Helper objects are compared by identity: an object returned by both resolvers, like the bean of a container, counts once, while another instance of the same class is a second helper object, whose helpers conflict with the ones of the first. When the subAgentResolver returns an agent that is built already, like one created by an agent builder or by AgenticServices.humanInTheLoopBuilder(), the helperObjectsResolver is not called for it: its helpers belong to whoever built it.

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