Class SessionTurnAdvisor

java.lang.Object
org.springframework.ai.session.advisor.SessionTurnAdvisor
All Implemented Interfaces:
Advisor, CallAdvisor, StreamAdvisor, org.springframework.core.Ordered

public final class SessionTurnAdvisor extends Object implements CallAdvisor, StreamAdvisor
Runs each ChatClient call as one turn of the session. A turn is one user message and everything up to the next: the tool rounds and the reply, the unit the chat memory's turn-boundary eviction works with too. The advisor sits outside the tool-calling loop (order DEFAULT_ORDER), so it sees the request before the first round and the turn's final response after the last one, however the loop ended; what happens once per turn is its, what happens once per model round is the SessionMemoryAdvisor's, inside the loop.
  1. Finds the session or creates it on first use, with the metadata from SessionMemoryAdvisor.SESSION_METADATA_CONTEXT_KEY, such as the agent attributes.
  2. After a loop that ended on a returnDirect tool or a breached tool call limit, stores the reply's tool results, which no model round stored.
  3. Compacts the session once the call has returned, if a trigger fires: before the reply is returned, or after it on the scheduler with SessionTurnAdvisor.Builder.asyncCompaction(boolean). Compaction is best-effort: a failure is logged and the reply returned, and it is retried after the next turn. It appends a boundary with an optimistic compare-and-swap, so of two concurrent compactions only the first lands; a compaction that a new turn's writes overtake keeps them in the view and lands with the same summary.
On its own the advisor stores no conversation: it needs the memory advisor, and SessionAdvisors builds the two together at their orders. The memory advisor alone keeps the history, without compaction.
Since:
2.1.0
Author:
Christian Tzolov