Interface CancellableAsyncToolCallback
- All Superinterfaces:
AsyncToolCallback,StateAwareToolCallback,org.springframework.ai.tool.ToolCallback
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 TypeMethodDescriptiondefault CompletableFuture<String>Default implementation that delegates to the cancellable version withCancellationToken.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, isAsyncMethods 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()orCancellationToken.throwIfCancelled()and stop gracefully when cancellation is requested.- Parameters:
arguments- the tool arguments as JSON stringcontext- the tool contextcancellationToken- 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 withCancellationToken.NONE.- Specified by:
callAsyncin interfaceAsyncToolCallback- Parameters:
arguments- the tool arguments as JSON stringcontext- the tool context- Returns:
- a CompletableFuture with the result
-