Class SessionEvent

java.lang.Object
org.springframework.ai.session.SessionEvent

public final class SessionEvent extends Object
An entry of a session's log. An event has an EventType: a EventType.MESSAGE event wraps a Spring AI Message and adds what the message intentionally lacks, identity, ordering and the session it belongs to; every other type records runtime state, a compaction or an application's own event, in getPayload() instead of a message, and getMessage() throws for it. Message events are the conversation: they alone take part in prompt assembly and compaction.

An event is never changed once appended. A compaction records what left the active view in a EventType.COMPACTION event of its own, and a covered event stays in the log, readable through EventFilter.all(). A synthetic event is one a compaction wrote: the user message that opens a summary turn and the assistant message that closes it.

Since:
2.1.0
Author:
Christian Tzolov
  • Field Details

  • Method Details

    • getId

      public String getId()
      The identity of the event, unique across sessions.
    • getSessionId

      public String getSessionId()
      The session the event belongs to.
    • getTimestamp

      public Instant getTimestamp()
      When the event was recorded. A log is ordered by insertion, not by this.
    • getType

      public EventType getType()
      What the event records; EventType.MESSAGE for a conversation message.
    • hasMessage

      public boolean hasMessage()
      Whether the event carries a message, so that getMessage() can be called: a EventType.MESSAGE event does; see EventType.canCarryMessage().
    • getMessage

      public Message getMessage()
      The wrapped Spring AI message.
      Throws:
      IllegalStateException - if the event carries no message; see hasMessage()
    • getPayload

      public Map<String,Object> getPayload()
      The data of an event that is not a message, the ids a EventType.COMPACTION boundary names for example; empty for a message event. Values must be JSON-serializable.
    • getMetadata

      public Map<String,Object> getMetadata()
      Framework flags such as METADATA_SYNTHETIC and METADATA_COMPACTION_SOURCE, and whatever the application adds.
    • getMessageType

      public MessageType getMessageType()
      The type of the wrapped message.
      Throws:
      IllegalStateException - if the event carries no message; see hasMessage()
    • isSynthetic

      public boolean isSynthetic()
      Whether a compaction wrote this event, a summary rather than something a participant said.
    • isTurnStart

      public boolean isTurnStart()
      Whether the event starts a turn: a MessageType.USER message event. A turn is a user message plus everything up to the next one, the assistant replies, the tool calls and their results, so it is the unit no read or compaction cut may split. A synthetic shadow prompt is a user message too, so a summary turn is never separated from its summary. An event that is not a EventType.MESSAGE never starts a turn.
    • hasToolCalls

      public boolean hasToolCalls()
      Whether the event is an assistant message with tool calls; false for an event without a message.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • builder

      public static SessionEvent.Builder builder()
      Returns a new SessionEvent.Builder.