Interface CancellableAsyncToolCallback

All Superinterfaces:
AsyncToolCallback, StateAwareToolCallback, org.springframework.ai.tool.ToolCallback

public interface CancellableAsyncToolCallback extends AsyncToolCallback
Interface for cancellable asynchronous tool callbacks. Tools implementing this interface can respond to cancellation requests.

This interface extends AsyncToolCallback to add support for cooperative cancellation. Tools should periodically check the CancellationToken and stop gracefully when cancellation is requested.

Example usage:


 public class CancellableTool implements CancellableAsyncToolCallback {

     @Override
     public ToolDefinition getToolDefinition() {
         return ToolDefinition.builder()
             .name("cancellableTool")
             .description("A tool that supports cancellation")
             .build();
     }

     @Override
     public CompletableFuture<String> callAsync(String arguments, ToolContext context,
             CancellationToken cancellationToken) {
         return CompletableFuture.supplyAsync(() -> {
             for (int i = 0; i < 100; i++) {
                 // Check for cancellation periodically
                 cancellationToken.throwIfCancelled();

                 // Do some work
                 processItem(i);
             }
             return "completed";
         });
     }
 }
 
Since:
1.0.0
Author:
disaster
See Also:
  • Field Summary

    Fields inherited from interface org.springframework.ai.tool.ToolCallback

    logger
  • Method Summary

    Modifier and Type
    Method
    Description
    callAsync(String arguments, org.springframework.ai.chat.model.ToolContext context)
    Default implementation that delegates to the cancellable version with CancellationToken.NONE.
    callAsync(String arguments, org.springframework.ai.chat.model.ToolContext context, CancellationToken cancellationToken)
    Asynchronously executes the tool with cancellation support.

    Methods inherited from interface com.alibaba.cloud.ai.graph.agent.tool.AsyncToolCallback

    call, getTimeout, isAsync

    Methods inherited from interface org.springframework.ai.tool.ToolCallback

    call, getToolDefinition, getToolMetadata
  • Method Details

    • callAsync

      CompletableFuture<String> callAsync(String arguments, org.springframework.ai.chat.model.ToolContext context, CancellationToken cancellationToken)
      Asynchronously executes the tool with cancellation support.

      Implementations should periodically check the cancellation token using CancellationToken.isCancelled() or CancellationToken.throwIfCancelled() and stop gracefully when cancellation is requested.

      Parameters:
      arguments - the tool arguments as JSON string
      context - the tool context
      cancellationToken - token to check for cancellation
      Returns:
      a CompletableFuture with the result
    • callAsync

      default CompletableFuture<String> callAsync(String arguments, org.springframework.ai.chat.model.ToolContext context)
      Default implementation that delegates to the cancellable version with CancellationToken.NONE.
      Specified by:
      callAsync in interface AsyncToolCallback
      Parameters:
      arguments - the tool arguments as JSON string
      context - the tool context
      Returns:
      a CompletableFuture with the result