Class ToolResponseBuilder

java.lang.Object
org.bsc.langgraph4j.agent.ToolResponseBuilder
Direct Known Subclasses:
LC4jToolResponseBuilder, SpringAIToolResponseBuilder

public class ToolResponseBuilder extends Object
A builder class to construct a Command and update the execution context.

This builder is used within a tool/action to specify the next node to execute (gotoNode(String)) and any state updates (update(Map)). The resulting Command is then placed into the shared context, typically within an AtomicReference, for the graph execution engine to process.

  • Field Details

  • Constructor Details

    • ToolResponseBuilder

      public ToolResponseBuilder(Map<String,Object> context)
      Constructs a new ToolResponseBuilder.
      Parameters:
      context - the shared execution context map
  • Method Details

    • gotoNode

      public final ToolResponseBuilder gotoNode(String gotoNode)
      Specifies the name of the next node to transition to in the graph.
      Parameters:
      gotoNode - the name of the target node
      Returns:
      this builder instance for chaining
    • update

      public final ToolResponseBuilder update(Map<String,Object> update)
      Provides a map of key-value pairs to update the shared state.
      Parameters:
      update - a map containing state updates
      Returns:
      this builder instance for chaining
    • build

      @Deprecated(forRemoval=true) public final <T> T build(T result)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use buildAndReturn(Object) instead. This method will be removed in a future version.
      Builds the Command, updates the context, and returns the provided result.
      Type Parameters:
      T - the type of the result
      Parameters:
      result - the result to return from the tool/action method
      Returns:
      the provided result, allowing for a fluent return from the calling method
    • buildAndReturn

      public final <T> T buildAndReturn(T result)
      Builds the Command, updates the context, and returns the provided result. This is useful for returning a value from a tool execution while also setting the command.
      Type Parameters:
      T - the type of the result
      Parameters:
      result - the result to return from the tool/action method
      Returns:
      the provided result, allowing for a fluent return from the calling method
    • buildAndSet

      public final void buildAndSet()
      Builds the Command and sets it in the context. This method finds an AtomicReference<Command> in the context using the commandResultKey, creates a new Command from the builder's state, and sets it on the reference.
      Throws:
      IllegalStateException - if the object at commandResultKey in the context is not of the expected type AtomicReference<Command>.