Package com.google.genkit.ai.agent
Class Session<S>
java.lang.Object
com.google.genkit.ai.agent.Session<S>
- Type Parameters:
S- the type of the custom state object
- All Implemented Interfaces:
ArtifactStore
Session is the per-invocation in-memory state holder managed by the agent runtime. It stores
messages, custom state, and artifacts; provides mutation methods that increment a version counter
and fire listener callbacks; and implements
ArtifactStore for middleware/tools that do
not need to know the custom state type.
Thread-safety: the runtime drives a session from one turn at a time. Mutations are
synchronized to guard against concurrent reads in edge cases, but over-engineering with
lock-free structures is intentionally avoided.
-
Constructor Summary
ConstructorsConstructorDescriptionSession(SessionState<S> initialState) Constructs a new Session from the given initial state. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddArtifacts(Artifact... arts) Adds artifacts with deduplication by name.voidaddArtifacts(List<Artifact> arts) Adds artifacts with deduplication by name (list overload).voidaddMessages(Message... msgs) Appends the given messages to the session.voidaddMessages(List<Message> msgs) Appends the given messages to the session.Returns a copy of the current artifacts list.Returns a deep copy of the current custom state.Returns a copy of the current messages list.getState()Returns a deep copy of the current session state.longReturns the current mutation version counter.Returns the session ID.voidsetMessages(List<Message> msgs) Replaces the session's message list with a copy of the given list.voidSets the callback to be invoked for each artifact added or updated viaaddArtifacts(com.google.genkit.ai.agent.Artifact...).voidSets the callback to be invoked afterupdateCustom(java.util.function.UnaryOperator<S>)completes.voidAppliesfnto the current artifacts list and stores the result.voidupdateCustom(UnaryOperator<S> fn) Appliesfnto the current custom state, stores the result, increments the version, and fires theonCustomChangedlistener.voidAppliesfnto the current messages list and stores the result.
-
Constructor Details
-
Session
Constructs a new Session from the given initial state. Deep-copiesinitialStateso that subsequent external mutations to the original object cannot affect this session. IfinitialState.sessionIdis null or empty, a new UUID is minted.- Parameters:
initialState- the initial session state (must not be null)
-
-
Method Details
-
sessionId
Returns the session ID.- Returns:
- the session ID (never null)
-
getVersion
public long getVersion()Returns the current mutation version counter. Increments on every mutating operation.- Returns:
- the version
-
getState
Returns a deep copy of the current session state. Callers must not mutate the returned object to avoid leaking changes back into the session.- Returns:
- a deep copy of the current
SessionState
-
getMessages
Returns a copy of the current messages list. Mutating the returned list does not affect the session.- Returns:
- a copy of the messages (never null)
-
addMessages
Appends the given messages to the session.- Parameters:
msgs- messages to add
-
addMessages
Appends the given messages to the session.- Parameters:
msgs- messages to add (must not be null)
-
setMessages
Replaces the session's message list with a copy of the given list.- Parameters:
msgs- the new messages (must not be null)
-
updateMessages
Appliesfnto the current messages list and stores the result.- Parameters:
fn- the update function
-
getCustom
Returns a deep copy of the current custom state. Mutating the returned value does not affect the session — preferupdateCustom(java.util.function.UnaryOperator<S>)for modifications.- Returns:
- a deep copy of the current custom state, or null if not set
-
updateCustom
Appliesfnto the current custom state, stores the result, increments the version, and fires theonCustomChangedlistener.- Parameters:
fn- the update function
-
getArtifacts
Returns a copy of the current artifacts list. Mutating the returned list does not affect the session.- Specified by:
getArtifactsin interfaceArtifactStore- Returns:
- a copy of the artifacts (never null)
-
addArtifacts
Adds artifacts with deduplication by name. If an artifact with the same non-null name already exists, it is replaced in place. Artifacts with null names are always appended. Fires theonArtifactChangedlistener for each artifact added or updated.- Specified by:
addArtifactsin interfaceArtifactStore- Parameters:
arts- artifacts to add
-
addArtifacts
Adds artifacts with deduplication by name (list overload).- Parameters:
arts- artifacts to add (must not be null)
-
updateArtifacts
Appliesfnto the current artifacts list and stores the result.- Parameters:
fn- the update function
-
setOnCustomChanged
Sets the callback to be invoked afterupdateCustom(java.util.function.UnaryOperator<S>)completes.- Parameters:
cb- the callback (may be null to clear)
-
setOnArtifactChanged
Sets the callback to be invoked for each artifact added or updated viaaddArtifacts(com.google.genkit.ai.agent.Artifact...).- Parameters:
cb- the callback (may be null to clear)
-