Class PatchToolCallsInterceptor

java.lang.Object
com.alibaba.cloud.ai.graph.agent.interceptor.ModelInterceptor
com.alibaba.cloud.ai.graph.agent.extension.interceptor.PatchToolCallsInterceptor
All Implemented Interfaces:
Interceptor

public class PatchToolCallsInterceptor extends ModelInterceptor
Middleware to patch dangling tool calls in the messages history.

This interceptor handles situations where an AI message contains tool calls that don't have corresponding ToolResponseMessages in the conversation history. This can happen when tool execution is interrupted or when new messages arrive before tool responses can be added.

The interceptor automatically adds cancellation messages for any dangling tool calls, preventing errors when the conversation is sent to the model.

Example usage:

 PatchToolCallsInterceptor interceptor = PatchToolCallsInterceptor.builder().build();
 agent.addInterceptor(interceptor);
 
  • Method Details

    • builder

      public static PatchToolCallsInterceptor.Builder builder()
    • getName

      public String getName()
    • interceptModel

      public ModelResponse interceptModel(ModelRequest request, ModelCallHandler handler)
      Description copied from class: ModelInterceptor
      Wrap a model call with custom logic. Implementations can: - Modify the request before calling the handler - Call the handler multiple times (retry logic) - Modify the response after handler returns - Handle exceptions and provide fallbacks
      Specified by:
      interceptModel in class ModelInterceptor
      Parameters:
      request - The model request
      handler - The next handler in the chain (or base handler)
      Returns:
      The model response