Interface TnsAIEventPublisher

All Known Implementing Classes:
InMemoryEventPublisher

public interface TnsAIEventPublisher
Interface for publishing and subscribing to TnsAI events.

The TnsAIEventPublisher provides a reactive event distribution mechanism using Java's built-in Flow API (Reactive Streams implementation).

Usage patterns:

1. Simple Consumer Subscription


 publisher.subscribe("session-1", event -> {
     System.out.println("Received: " + event);
 });

 publisher.publish("session-1", TextDeltaEvent.of("msg-1", "Hello"));
 

2. Flow.Subscriber for Advanced Control


 publisher.subscribe("session-1", new Flow.Subscriber<TnsAIEvent>() {
     private Flow.Subscription subscription;

     public void onSubscribe(Flow.Subscription s) {
         this.subscription = s;
         s.request(Long.MAX_VALUE);
     }

     public void onNext(TnsAIEvent event) {
         // Handle event
     }

     public void onError(Throwable t) {
         // Handle error
     }

     public void onComplete() {
         // Handle completion
     }
 });
 

3. Integration with AG-UI


 // In TnsAIAgentAdapter
 publisher.getPublisher(sessionId)
     .ifPresent(pub -> {
         // Convert Flow.Publisher to Reactor Flux
         Flux.from(JdkFlowAdapter.flowPublisherToFlux(pub))
             .map(this::mapToAGUIEvent)
             .subscribe(sink::next);
     });
 
Author:
TnsAI Team
See Also:
  • Method Details

    • publish

      void publish(String sessionId, TnsAIEvent event)
      Publishes an event to all subscribers of the specified session.
      Parameters:
      sessionId - the session identifier
      event - the event to publish
    • subscribe

      Subscribes to events for a specific session using a simple consumer.

      This is a convenience method that wraps the consumer in a Flow.Subscriber.

      Parameters:
      sessionId - the session identifier
      consumer - the event consumer
      Returns:
      a Subscription that can be used to cancel
    • subscribe

      void subscribe(String sessionId, Flow.Subscriber<TnsAIEvent> subscriber)
      Subscribes to events for a specific session using a Flow.Subscriber.

      This provides full reactive streams control including backpressure.

      Parameters:
      sessionId - the session identifier
      subscriber - the event subscriber
    • getPublisher

      Optional<Flow.Publisher<TnsAIEvent>> getPublisher(String sessionId)
      Gets a Flow.Publisher for the specified session.

      This is useful for integration with reactive frameworks:

      • Project Reactor: Use JdkFlowAdapter to convert to Flux
      • RxJava: Use FlowAdapters
      • Spring WebFlux: Direct Flow.Publisher support
      Parameters:
      sessionId - the session identifier
      Returns:
      the Flow.Publisher, or empty if session doesn't exist
    • complete

      void complete(String sessionId)
      Signals completion for a session (no more events will be published).

      This triggers onComplete() for all subscribers.

      Parameters:
      sessionId - the session identifier
    • error

      void error(String sessionId, Throwable error)
      Signals an error for a session.

      This triggers onError() for all subscribers.

      Parameters:
      sessionId - the session identifier
      error - the error that occurred
    • close

      void close(String sessionId)
      Removes all subscriptions for a session and cleans up resources.
      Parameters:
      sessionId - the session identifier
    • createSession

      String createSession()
      Creates a new session and returns its ID.

      Sessions are automatically created on first publish/subscribe, but this method can be used for explicit session management.

      Returns:
      the new session identifier
    • hasSession

      boolean hasSession(String sessionId)
      Checks if a session exists.
      Parameters:
      sessionId - the session identifier
      Returns:
      true if the session exists