Package com.tnsai.agents.async
Interface AsyncAgent
public interface AsyncAgent
Interface for asynchronous agent execution.
Provides non-blocking chat operations with support for:
- CompletableFuture-based async execution
- Reactive streaming with Flow API
- Typed event streams
- Cancellation support
Usage
AsyncAgent agent = new MyAsyncAgent(principal, scope); // passes a wired ActionExecutor, see com.tnsai.agents
// Simple async
agent.chatAsync("Tell me about Java")
.thenAccept(response -> System.out.println(response))
.exceptionally(e -> { e.printStackTrace(); return null; });
// Streaming
agent.chatStream("Tell me a story")
.forEach(token -> System.out.print(token));
// Typed events
agent.chatEventStream("Complex task")
.forEach(event -> {
if (event instanceof ChatEvent.Token t) {
System.out.print(t.content());
} else if (event instanceof ChatEvent.ToolCall tc) {
System.out.println("Calling tool: " + tc.toolName());
}
});
-
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Cancels any ongoing async operation.Sends a chat message asynchronously.chatAsync(String message, ChatOptions options) Sends a chat message with options asynchronously.chatEventStream(String message) Sends a chat message and streams typed events.chatPublisher(String message) Creates a reactive publisher for chat events.chatStream(String message) Sends a chat message and streams the response.doubleGets the current async execution progress (0.0 - 1.0).booleanChecks if an async operation is in progress.
-
Method Details
-
chatAsync
Sends a chat message asynchronously.- Parameters:
message- The user message- Returns:
- A future that completes with the response
-
chatAsync
Sends a chat message with options asynchronously.- Parameters:
message- The user messageoptions- Chat options- Returns:
- A future that completes with the response
-
chatStream
Sends a chat message and streams the response.- Parameters:
message- The user message- Returns:
- A stream of response tokens
-
chatEventStream
Sends a chat message and streams typed events.- Parameters:
message- The user message- Returns:
- A stream of typed chat events
-
chatPublisher
Creates a reactive publisher for chat events.- Parameters:
message- The user message- Returns:
- A Flow.Publisher of chat events
-
cancel
void cancel()Cancels any ongoing async operation. -
isProcessing
boolean isProcessing()Checks if an async operation is in progress. -
getProgress
double getProgress()Gets the current async execution progress (0.0 - 1.0).
-