Package com.tnsai.events
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:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA subscription handle for cancelling event subscriptions. -
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves all subscriptions for a session and cleans up resources.voidSignals completion for a session (no more events will be published).Creates a new session and returns its ID.voidSignals an error for a session.getPublisher(String sessionId) Gets a Flow.Publisher for the specified session.booleanhasSession(String sessionId) Checks if a session exists.voidpublish(String sessionId, TnsAIEvent event) Publishes an event to all subscribers of the specified session.voidsubscribe(String sessionId, Flow.Subscriber<TnsAIEvent> subscriber) Subscribes to events for a specific session using a Flow.Subscriber.subscribe(String sessionId, Consumer<TnsAIEvent> consumer) Subscribes to events for a specific session using a simple consumer.
-
Method Details
-
publish
Publishes an event to all subscribers of the specified session.- Parameters:
sessionId- the session identifierevent- 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 identifierconsumer- the event consumer- Returns:
- a Subscription that can be used to cancel
-
subscribe
Subscribes to events for a specific session using a Flow.Subscriber.This provides full reactive streams control including backpressure.
- Parameters:
sessionId- the session identifiersubscriber- the event subscriber
-
getPublisher
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
Signals completion for a session (no more events will be published).This triggers onComplete() for all subscribers.
- Parameters:
sessionId- the session identifier
-
error
Signals an error for a session.This triggers onError() for all subscribers.
- Parameters:
sessionId- the session identifiererror- the error that occurred
-
close
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
Checks if a session exists.- Parameters:
sessionId- the session identifier- Returns:
- true if the session exists
-