Add session-memory module: Spring AI 2.1 Session API (event log, turn-aware windows, compaction with summarizer, SessionAdvisors, JDBC on PostgreSQL 16) with 14 transcripts and the 2.0 ChatMemory deprecation warnings from javac. Spring Boot 4.1.1, Spring AI 2.1.0, Java 25.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6jDW7zqoKDMTRrCyq
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
# A session is an append-only log; the active view is a projection of it
|
||||
|
||||
After 5 turns, the log holds 10 events and the model would see 10 messages:
|
||||
1 MESSAGE U:u1
|
||||
2 MESSAGE A:a1
|
||||
3 MESSAGE U:u2
|
||||
4 MESSAGE A:a2
|
||||
5 MESSAGE U:u3
|
||||
6 MESSAGE A:a3
|
||||
7 MESSAGE U:u4
|
||||
8 MESSAGE A:a4
|
||||
9 MESSAGE U:u5
|
||||
10 MESSAGE A:a5
|
||||
|
||||
compact(trigger: more than 3 turns, strategy: keep last 2 turns):
|
||||
unchanged=false covered=6 preserved=4 pinned=0
|
||||
|
||||
The log now holds 11 events (nothing was removed, one COMPACTION boundary was appended):
|
||||
1 MESSAGE U:u1
|
||||
2 MESSAGE A:a1
|
||||
3 MESSAGE U:u2
|
||||
4 MESSAGE A:a2
|
||||
5 MESSAGE U:u3
|
||||
6 MESSAGE A:a3
|
||||
7 MESSAGE U:u4
|
||||
8 MESSAGE A:a4
|
||||
9 MESSAGE U:u5
|
||||
10 MESSAGE A:a5
|
||||
11 COMPACTION payload=[coveredEventCount, pinnedEventIds, preservedEventIds, strategy, summaryEventIds, tokensEstimatedSaved]
|
||||
|
||||
The active view (what the next prompt starts from): U:u4 | A:a4 | U:u5 | A:a5
|
||||
@@ -0,0 +1,8 @@
|
||||
# EventFilter.lastN extends back to the start of its turn
|
||||
|
||||
Log: U:u1 | A:a1 | U:weather in Pune? | A:[tool call weather] | T:[tool result 31C] | A:31C in Pune. | U:u2 | A:a2
|
||||
lastN(1) -> 2 messages: U:u2 | A:a2
|
||||
lastN(2) -> 2 messages: U:u2 | A:a2
|
||||
lastN(3) -> 6 messages: U:weather in Pune? | A:[tool call weather] | T:[tool result 31C] | A:31C in Pune. | U:u2 | A:a2
|
||||
lastN(4) -> 6 messages: U:weather in Pune? | A:[tool call weather] | T:[tool result 31C] | A:31C in Pune. | U:u2 | A:a2
|
||||
lastN(5) -> 6 messages: U:weather in Pune? | A:[tool call weather] | T:[tool result 31C] | A:31C in Pune. | U:u2 | A:a2
|
||||
@@ -0,0 +1,6 @@
|
||||
# Compaction cuts between turns, so a tool call stays with its result
|
||||
|
||||
Before: U:u1 | A:a1 | U:u2 | A:a2 | U:weather in Pune? | A:[tool call weather] | T:[tool result 31C] | A:31C in Pune. | U:u3 | A:a3
|
||||
compact(keep last 3 EVENTS): covered=8 preserved=2
|
||||
After: U:u3 | A:a3
|
||||
The last 3 events would have started at the assistant reply "31C in Pune.", which is mid-turn, so the cut moved forward to the next turn start (u3).
|
||||
@@ -0,0 +1,15 @@
|
||||
# WindowCompactionStrategy with a ChatModelSummarizer
|
||||
|
||||
covered=6 preserved=4 summary messages=2
|
||||
|
||||
Active view after compaction:
|
||||
1 MESSAGE U:Summarize the conversation we had so far. (synthetic)
|
||||
2 MESSAGE A:SUMMARY: user is Asha, asked about turns 1-3. (synthetic)
|
||||
3 MESSAGE U:u4
|
||||
4 MESSAGE A:a4
|
||||
5 MESSAGE U:u5
|
||||
6 MESSAGE A:a5
|
||||
|
||||
Messages the summarizer model was sent (1 call):
|
||||
S:You are a conversation summarizer. Your task is to create a concise summary of the conve ...
|
||||
U:=== CONVERSATION TO SUMMARIZE === / User: u1 / Assistant: a1 / User: u2 / Assistant: a2 ...
|
||||
@@ -0,0 +1,15 @@
|
||||
# SessionAdvisors on a ChatClient, scripted model
|
||||
|
||||
alice> My name is Asha
|
||||
model< Nice to meet you, Asha.
|
||||
bob> My name is Bala
|
||||
model< Nice to meet you, Bala.
|
||||
alice> What is my name?
|
||||
model< Your name is Asha.
|
||||
bob> What is my name?
|
||||
model< Your name is Bala.
|
||||
|
||||
Messages sent to the model on its 3rd call (alice's second question):
|
||||
U:My name is Asha | A:Nice to meet you, Asha. | U:What is my name?
|
||||
|
||||
Alice's stored log: U:My name is Asha | A:Nice to meet you, Asha. | U:What is my name? | A:Your name is Asha.
|
||||
@@ -0,0 +1,11 @@
|
||||
# TurnCountTrigger(3) + lastTurns(2) through the advisors
|
||||
|
||||
after turn 1: log= 2 events, model would see 2 messages
|
||||
after turn 2: log= 4 events, model would see 4 messages
|
||||
after turn 3: log= 6 events, model would see 6 messages
|
||||
after turn 4: log= 9 events, model would see 4 messages
|
||||
after turn 5: log=11 events, model would see 6 messages
|
||||
after turn 6: log=14 events, model would see 4 messages
|
||||
|
||||
Prompt sent on turn 6: U:question 3 | A:Noted. | U:question 4 | A:Noted. | U:question 5 | A:Noted. | U:question 6
|
||||
Active view after turn 6: U:question 5 | A:Noted. | U:question 6 | A:Noted.
|
||||
@@ -0,0 +1,4 @@
|
||||
# No session id on the request
|
||||
|
||||
IllegalStateException: No session ID found in advisor context. Set SESSION_ID_CONTEXT_KEY on every request: .advisors(a -> a.param(SessionMemoryAdvisor.SESSION_ID_CONTEXT_KEY, sessionId))
|
||||
Model calls made: 0
|
||||
@@ -0,0 +1,6 @@
|
||||
# SystemMessage in a session
|
||||
|
||||
allowSystemMessages(false) [default]: IllegalArgumentException: Storing a SystemMessage in session 'x' is disabled. System prompts are configuration: supply them on every request (e.g. ChatClient defaultSystem/.system, or your own prompt builder) and keep per-session settings in Session.metadata. To store system messages anyway, use DefaultSessionService.builder().allowSystemMessages(true) or set spring.ai.session.allow-system-messages=true. See the "System Messages" reference page.
|
||||
|
||||
allowSystemMessages(true), 4 turns, then compact(lastTurns(1)):
|
||||
active view: S:You are terse. | U:u4 | A:a4
|
||||
@@ -0,0 +1,8 @@
|
||||
# Retries and compare-and-swap appends
|
||||
|
||||
version before append: 0, after append: 1, after retrying the SAME event: 1
|
||||
events in log: 1
|
||||
|
||||
appendEvents(expectedVersion = 0, which is stale): false
|
||||
appendEvents(expectedVersion = 1, which is current): true
|
||||
events in log: 2
|
||||
@@ -0,0 +1,22 @@
|
||||
# JdbcSessionRepository on PostgreSQL 16
|
||||
|
||||
service: DefaultSessionService, repository: JdbcSessionRepository
|
||||
tables: [ai_session, ai_session_event, ai_session_index]
|
||||
|
||||
select seq order, event_type, message_type, message_content, synthetic from ai_session_event:
|
||||
1 MESSAGE USER u1 false
|
||||
2 MESSAGE ASSISTANT a1 false
|
||||
3 MESSAGE USER u2 false
|
||||
4 MESSAGE ASSISTANT a2 false
|
||||
5 MESSAGE USER u3 false
|
||||
6 MESSAGE ASSISTANT a3 false
|
||||
7 MESSAGE USER u4 false
|
||||
8 MESSAGE ASSISTANT a4 false
|
||||
9 MESSAGE USER u5 false
|
||||
10 MESSAGE ASSISTANT a5 false
|
||||
11 COMPACTION null null false
|
||||
|
||||
A second SessionService on the same repository (a stand-in for a restart):
|
||||
active view: U:u4 | A:a4 | U:u5 | A:a5
|
||||
whole log: 11 events
|
||||
findByMetadata(principalName, asha): [jdbc-1]
|
||||
@@ -0,0 +1,4 @@
|
||||
# What the autoconfiguration gives you with no properties
|
||||
|
||||
default time-to-live: 60 days
|
||||
SystemMessage append: IllegalArgumentException
|
||||
@@ -0,0 +1,19 @@
|
||||
# javac -Xlint:deprecation on the Spring AI 2.0 ChatMemory setup, compiled against Spring AI 2.1.0
|
||||
|
||||
$ javac -Xlint:deprecation -cp <2.1.0 classpath> src/broken/LegacyChatMemory.java
|
||||
LegacyChatMemory.java:12: warning: [removal] ChatMemory in org.springframework.ai.chat.memory has been deprecated and marked for removal
|
||||
ChatMemory memory = MessageWindowChatMemory.builder()
|
||||
^
|
||||
LegacyChatMemory.java:13: warning: [removal] InMemoryChatMemoryRepository in org.springframework.ai.chat.memory has been deprecated and marked for removal
|
||||
.chatMemoryRepository(new InMemoryChatMemoryRepository())
|
||||
^
|
||||
LegacyChatMemory.java:12: warning: [removal] MessageWindowChatMemory in org.springframework.ai.chat.memory has been deprecated and marked for removal
|
||||
ChatMemory memory = MessageWindowChatMemory.builder()
|
||||
^
|
||||
LegacyChatMemory.java:17: warning: [removal] MessageChatMemoryAdvisor in org.springframework.ai.chat.client.advisor has been deprecated and marked for removal
|
||||
.defaultAdvisors(MessageChatMemoryAdvisor.builder(memory).build())
|
||||
^
|
||||
LegacyChatMemory.java:20: warning: [removal] ChatMemory in org.springframework.ai.chat.memory has been deprecated and marked for removal
|
||||
.advisors(a -> a.param(ChatMemory.CONVERSATION_ID, conversationId))
|
||||
^
|
||||
5 warnings
|
||||
@@ -0,0 +1,9 @@
|
||||
# ChatMemory.CONVERSATION_ID against SessionMemoryAdvisor.SESSION_ID_CONTEXT_KEY
|
||||
|
||||
ChatMemory.CONVERSATION_ID = chat_memory_conversation_id
|
||||
MemoryAdvisor.CONVERSATION_ID = chat_memory_conversation_id
|
||||
SessionMemoryAdvisor.SESSION_ID_CONTEXT_KEY = chat_memory_conversation_id
|
||||
|
||||
Two calls with the 2.0 parameter name, SessionAdvisors on the client:
|
||||
model< Your name is Asha.
|
||||
session 'legacy-key' holds: U:My name is Asha | A:Nice to meet you, Asha. | U:What is my name? | A:Your name is Asha.
|
||||
@@ -0,0 +1,6 @@
|
||||
# timeToLive and deleteExpiredSessions
|
||||
|
||||
Two hours from now, 'short' (ttl 1h) is past its expiresAt; 'long' (ttl 30d) is not.
|
||||
Before the cleaner: findById(short) = present, events = 2
|
||||
deleteExpiredSessions(now + 2h) removed 1 session(s)
|
||||
After: findById(short) = null, findById(long) = present
|
||||
Reference in New Issue
Block a user