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.
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().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-checked append.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.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). -
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-checked append. 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
-