Class SessionEventCodec

java.lang.Object
org.springframework.ai.session.support.SessionEventCodec

public final class SessionEventCodec extends Object
Encodes a SessionEvent into its persisted shape, an SessionEventCodec.EncodedEvent, and back. Shared by every SessionRepository that stores events as columns or fields rather than as Java objects.

An event is stored as four fields: its EventType, the MessageType and text of its message (when it carries one; the text is kept as its own field so a store can search it), and a JSON payload. For an event with a message the payload is the message's ordered MessageParts and its metadata, serialized with the parts' own polymorphic JSON, so media, tool calls, tool results and provider-signed reasoning all survive a round trip; an event type of the application's own that carries a message stores its own payload in the same JSON object. For every other event type the payload is SessionEvent.getPayload(). encode(org.springframework.ai.session.SessionEvent) and decode(org.springframework.ai.session.support.SessionEventCodec.EncodedEvent) convert between the two, and toJson(java.lang.Object) / fromJsonMap(java.lang.String) handle the metadata maps of sessions and events.

Since:
2.1.0
Author:
Christian Tzolov
  • Constructor Details

    • SessionEventCodec

      public SessionEventCodec()
      Creates a codec with a default JsonMapper.
    • SessionEventCodec

      public SessionEventCodec(tools.jackson.databind.json.JsonMapper jsonMapper)
      Creates a codec that serializes with the given mapper.
      Parameters:
      jsonMapper - the mapper used for the payloads and the metadata maps
  • Method Details

    • encode

      Encodes an event into its persisted shape.
      Parameters:
      event - the event to encode
      Returns:
      the fields to store
      Throws:
      IllegalStateException - if the payload of a non-message event cannot be serialized
    • decode

      Rebuilds an event from its persisted shape, as a builder: the codec restores the type, the message or the payload, and the caller adds identity, timestamp and metadata. A payload that cannot be parsed is logged and treated as absent, so a damaged row still yields an event.
      Parameters:
      encoded - the stored fields
      Returns:
      a builder holding the decoded type and content
    • toJson

      public String toJson(Object value)
      Serializes a value, typically a metadata map, to JSON.
      Parameters:
      value - the value
      Returns:
      the JSON text
      Throws:
      IllegalStateException - if the value cannot be serialized
    • fromJsonMap

      public Map<String,Object> fromJsonMap(String json)
      Parses a JSON object into a map. Blank input yields an empty map; input that cannot be parsed is logged and yields an empty map, so a damaged row still loads.
      Parameters:
      json - the JSON text
      Returns:
      the map