Class InstructionAgentHook

java.lang.Object
com.alibaba.cloud.ai.graph.agent.hook.messages.MessagesAgentHook
com.alibaba.cloud.ai.graph.agent.hook.InstructionAgentHook
All Implemented Interfaces:
Hook, Prioritized

public class InstructionAgentHook extends MessagesAgentHook
MessagesAgentHook that injects the ReactAgent's instruction into messages before each agent run.

When this hook is active, it runs at HookPosition.BEFORE_AGENT and reads ReactAgent.instruction() from getAgent(). If the instruction is non-empty, it prepends an AgentInstructionMessage to the given messages and returns an AgentCommand with UpdatePolicy.REPLACE. This allows ReactAgent to avoid adding instruction again in the subgraph adapter when used as a subgraph node.

This hook is added by default in ReactAgent when no other hook is an InstructionAgentHook. It runs first among beforeAgent hooks (lowest order).

  • Constructor Details

    • InstructionAgentHook

      public InstructionAgentHook()
  • Method Details

    • beforeAgent

      public AgentCommand beforeAgent(List<org.springframework.ai.chat.messages.Message> previousMessages, RunnableConfig config)
      Overrides:
      beforeAgent in class MessagesAgentHook
    • getName

      public String getName()
      Description copied from interface: Hook
      Gets the unique name of this hook. The name is used to identify the hook in the execution graph.
      Returns:
      the hook name, must not be null
    • getOrder

      public int getOrder()
      Description copied from interface: Prioritized
      Returns the order value for this object.

      The order value determines the priority of this object when sorted with other Prioritized objects. Objects with smaller order values will be sorted before objects with larger order values (ascending order).

      For example:

      • An object with order 1 will be placed before an object with order 5
      • An object with order 10 will be placed before an object with order 20

      Returns:
      the order value (smaller values indicate higher priority)
    • getAgent

      public ReactAgent getAgent()
      Description copied from interface: Hook
      Gets the ReactAgent instance that owns this hook. This allows hooks to access the agent's state and configuration.
      Returns:
      the ReactAgent instance, or null if not set
    • setAgent

      public void setAgent(ReactAgent agent)
      Description copied from interface: Hook
      Sets the ReactAgent instance that owns this hook. This is typically called by the framework during agent initialization.
      Parameters:
      agent - the ReactAgent instance, must not be null
    • create

      public static InstructionAgentHook create()
      Create the default InstructionAgentHook instance (used when no other hook handles instruction).
      Returns:
      a new InstructionAgentHook