EventPublishingConversation

class EventPublishingConversation(delegate: Conversation, eventPublisher: <Error class: unknown class>, fromUserId: String? = null, toUserId: String? = null) : Conversation

A decorator that wraps any Conversation to publish MessageEvents when messages are added.

This enables event-driven patterns for any conversation implementation, including InMemoryConversation.

Usage

val conversation = InMemoryConversation(id = "session-1")
val eventPublishing = EventPublishingConversation(conversation, applicationEventPublisher)

// Now fires MessageEvent(status=ADDED) on every addMessage
eventPublishing.addMessage(UserMessage("Hello!"))

Event Flow

This decorator fires MessageEvent with status MessageStatus.ADDED synchronously after the delegate adds the message.

For persistent conversations (like StoredConversation), additional events (MessageStatus.PERSISTED or MessageStatus.PERSISTENCE_FAILED) may be fired asynchronously by the underlying implementation.

Parameters

delegate

the underlying Conversation implementation

eventPublisher

Spring's event publisher

fromUserId

optional default sender ID for published events

toUserId

optional default recipient ID for published events

Constructors

Link copied to clipboard
constructor(delegate: Conversation, eventPublisher: <Error class: unknown class>, fromUserId: String? = null, toUserId: String? = null)

Properties

Link copied to clipboard
open override val assets: List<Asset>

All assets visible in this conversation.

Link copied to clipboard
open override val assetTracker: AssetTracker

Assets tracked in the conversation.

Link copied to clipboard
open override val messages: List<Message>

Messages in the conversation in chronological order. Visible to user.

Functions

Link copied to clipboard
open override fun addMessage(message: Message): Message

Add a message and publish a MessageEvent with status ADDED.

Link copied to clipboard
open override fun addMessageFrom(message: Message, author: User?): Message

Add a message with explicit author attribution. Useful for group chats or when the author differs per message.

Link copied to clipboard
open override fun addMessageFromTo(message: Message, from: User?, to: User?): Message

Add a message with explicit author and recipient. Use this for multi-party chats where both sender and receiver need to be specified.

Link copied to clipboard
open override fun infoString(verbose: Boolean?, indent: Int): String
Link copied to clipboard
open infix override fun last(n: Int): Conversation

Create a nonpersistent conversation with the last n messages from this conversation.

Link copied to clipboard
open override fun lastMessageIfBeFromUser(): UserMessage?

Non-null if the conversation has messages and the last message is from the user.

Link copied to clipboard
open override fun mostRecent(n: Int): AssetView

The most recently timestamped assets

Link copied to clipboard
open override fun mostRecentlyAdded(n: Int): AssetView

The most recently added assets

Link copied to clipboard
open override fun promptContributor(conversationFormatter: ConversationFormatter): <Error class: unknown class>

Prompt contributor that represents the conversation so far. Usually we will want to add messages from the conversation to a prompt instead of formatting the conversation

Link copied to clipboard
open override fun references(): List<LlmReference>

Convenience method to return references. References will be converted to matryoshka tools

Link copied to clipboard
open override fun since(instant: Instant): AssetView

Assets timestamped since the given instant