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 Details

    • chatAsync

      CompletableFuture<String> chatAsync(String message)
      Sends a chat message asynchronously.
      Parameters:
      message - The user message
      Returns:
      A future that completes with the response
    • chatAsync

      CompletableFuture<String> chatAsync(String message, ChatOptions options)
      Sends a chat message with options asynchronously.
      Parameters:
      message - The user message
      options - Chat options
      Returns:
      A future that completes with the response
    • chatStream

      Stream<String> chatStream(String message)
      Sends a chat message and streams the response.
      Parameters:
      message - The user message
      Returns:
      A stream of response tokens
    • chatEventStream

      Stream<ChatEvent> chatEventStream(String message)
      Sends a chat message and streams typed events.
      Parameters:
      message - The user message
      Returns:
      A stream of typed chat events
    • chatPublisher

      Flow.Publisher<ChatEvent> chatPublisher(String message)
      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).