Package org.springframework.ai.session
Interface SessionService
- All Known Implementing Classes:
DefaultSessionService
public interface SessionService
The sessions of an application and their conversations: creating and finding sessions,
appending and reading their events, compacting them.
appendEvent(org.springframework.ai.session.SessionEvent) appends one
event. appendEvents(String, List, long) appends a batch only if the log has
not changed since the caller read its version. Compaction writes this way, and so can
any other code that builds events from the log. DefaultSessionService
implements it on a SessionRepository.- Since:
- 2.1.0
- Author:
- Christian Tzolov
-
Method Summary
Modifier and TypeMethodDescriptionvoidappendEvent(SessionEvent event) Appends an event to the session named bySessionEvent.getSessionId().booleanappendEvents(String sessionId, List<SessionEvent> events, long expectedVersion) Appends one or more events in order, atomically, only if the log's version still equalsexpectedVersion.default voidappendMessage(String sessionId, Message message) Wraps the message in aSessionEventand appends it; seeappendEvent(SessionEvent).compact(String sessionId, CompactionTrigger trigger, CompactionStrategy strategy) Evaluates the trigger on the active view and, when it fires, runs the strategy and records its result: oneEventType.COMPACTIONboundary event followed by the summary events, in one version-checkedappendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long).create(CreateSessionRequest request) Creates a session with the request's id, a random UUID when it has none, its time to live and its metadata.voidDeletes the session and its events.intdeleteExpiredSessions(Instant before) Deletes every session that expired before the given instant, with their events, and returns how many.@Nullable SessionfindByMetadata(String name, String value) The sessions whose metadata holdsvalueundername, oldest first, for the keys the store indexes; seeSessionRepository.findByMetadata(java.lang.String, java.lang.String).default SessionfindOrCreate(CreateSessionRequest request) The session with the request's id, created from the request when there is none.getActiveMessages(String sessionId) The messages of the active view, the context window to send to the model:getEvents(sessionId, EventFilter.active())as messages.default List<SessionEvent>Every event of the log, oldest first.getEvents(String sessionId, EventFilter filter) The events the filter selects, oldest first; seeEventFilter.longgetEventVersion(String sessionId) The version of the session's log.getMessages(String sessionId) The messages of the whole log, the events a compaction covered and the summaries included: the full history, not a prompt.
-
Method Details
-
create
Creates a session with the request's id, a random UUID when it has none, its time to live and its metadata.- Throws:
SessionAlreadyExistsException- if a session with the requested id exists
-
findOrCreate
The session with the request's id, created from the request when there is none. Safe under concurrent first use: when another caller creates it first, the stored session is returned, not this request's. The default finds, creates, and on aSessionAlreadyExistsExceptionfinds again, through this service's ownfindById(java.lang.String)andcreate(org.springframework.ai.session.CreateSessionRequest).- Throws:
IllegalArgumentException- if the request has no id, null or blank
-
findById
-
findByMetadata
The sessions whose metadata holdsvalueundername, oldest first, for the keys the store indexes; seeSessionRepository.findByMetadata(java.lang.String, java.lang.String). The sessions of a principal arefindByMetadata(SessionAttributes.PRINCIPAL_NAME, name). -
delete
Deletes the session and its events. -
deleteExpiredSessions
Deletes every session that expired before the given instant, with their events, and returns how many. Nothing calls this on its own: call it from a scheduler, every hour for example. -
appendEvent
Appends an event to the session named bySessionEvent.getSessionId().- Throws:
IllegalArgumentException- if the event wraps aSystemMessageand the service does not allow storing system messages; seeDefaultSessionService.Builder.allowSystemMessages(boolean)
-
appendMessage
Wraps the message in aSessionEventand appends it; seeappendEvent(SessionEvent). -
appendEvents
Appends one or more events in order, atomically, only if the log's version still equalsexpectedVersion. Otherwise nothing is written andfalseis returned: another writer appended in between. On success the version advances once, whatever the number of events. This is the compare-and-set of the log, asSessionRepository.appendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long)defines it.Read
getEventVersion(java.lang.String), then read what the new events depend on, then call this method. Onfalse, read again and rebuild the events. Compaction writes its boundary and summaries this way.- Parameters:
sessionId- the session whose log is appended to; every event must belong to itevents- the events to append, oldest first; at least one, with ids that are new to the logexpectedVersion- the version read before the events were prepared- Returns:
truewhen the events were appended,falsewhen the log moved on and nothing was written- Throws:
IllegalArgumentException- if the session does not exist, the batch is empty, an event belongs to another session, an id is already in the log or appears twice, or an event holds a system message thatappendEvent(org.springframework.ai.session.SessionEvent)would reject. The system message check runs before the version check. The other checks may run only when the version matches, so a stale version can returnfalsefor a batch that would otherwise throw.
-
getEventVersion
The version of the session's log. It advances on every append of a new event and once per successfulappendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long). AnappendEvent(org.springframework.ai.session.SessionEvent)of an id the log already holds is a no-op and does not advance it.0for a session without events, or one that does not exist. Read it before the events that a version-checkedappendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long)depends on. -
getEvents
The events the filter selects, oldest first; seeEventFilter. -
getEvents
Every event of the log, oldest first. -
getMessages
The messages of the whole log, the events a compaction covered and the summaries included: the full history, not a prompt. UsegetActiveMessages(String)for the prompt. -
getActiveMessages
The messages of the active view, the context window to send to the model:getEvents(sessionId, EventFilter.active())as messages. -
compact
Evaluates the trigger on the active view and, when it fires, runs the strategy and records its result: oneEventType.COMPACTIONboundary event followed by the summary events, in one version-checkedappendEvents(java.lang.String, java.util.List<org.springframework.ai.session.SessionEvent>, long). The log is never changed; the boundary defines the view from then on. An append that lands in between does not waste the summary: the new events join the view verbatim and the write is retried, a bounded number of times. Passrequest -> trueto compact unconditionally.- Returns:
- the result as written, the events appended meanwhile added to its preserved
tail;
CompactionResult.isUnchanged()when nothing was written: the trigger did not fire, the strategy covered nothing, another compaction landed first, or appends kept overtaking the write
-