Package com.google.genkit.ai.session
Class Session<S>
java.lang.Object
com.google.genkit.ai.session.Session<S>
- Type Parameters:
S- the type of the custom session state
Session represents a stateful chat session that persists conversation history
and custom state across multiple interactions.
Sessions provide:
- Persistent conversation threads
- Custom state management
- Multiple named chat threads within a session
- Automatic history management
Example usage:
// Create a session with initial state
Session<MyState> session = genkit
.createSession(SessionOptions.<MyState>builder().initialState(new MyState("John")).build());
// Create a chat and interact
Chat chat = session.chat();
ModelResponse response = chat.send("Hello!");
// Access session state
MyState state = session.getState();
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionchat()Creates a new Chat instance for the default thread.chat(ChatOptions<S> options) Creates a new Chat instance with options.Creates a new Chat instance for a specific thread.chat(String threadName, ChatOptions<S> options) Creates a new Chat instance for a specific thread with options.static <S> Session<S> create(Registry registry, SessionOptions<S> options) Creates a new Session with a generated ID.static <S> Session<S> Creates a new Session with a generated ID and agent registry.Gets the agent registry for multi-agent handoffs.getId()Gets the session ID.Gets the message history for the default thread.getMessages(String threadName) Gets the message history for a thread.Gets the registry.Gets the session data.getState()Gets the current session state.getStore()Gets the session store.static <S> CompletableFuture<Session<S>> load(Registry registry, String sessionId, SessionOptions<S> options) Loads an existing session from a store.static <S> CompletableFuture<Session<S>> load(Registry registry, String sessionId, SessionOptions<S> options, Map<String, Agent> agentRegistry) Loads an existing session from a store with agent registry.toJSON()Serializes the session to JSON-compatible data.updateMessages(String threadName, List<Message> messages) Updates the messages for a thread and persists them.updateState(S state) Updates the session state and persists it.
-
Field Details
-
DEFAULT_THREAD
Default thread name for chat conversations.- See Also:
-
-
Method Details
-
getId
Gets the session ID.- Returns:
- the unique session identifier
-
getState
Gets the current session state.- Returns:
- the session state, or null if not set
-
updateState
Updates the session state and persists it.- Parameters:
state- the new state- Returns:
- a CompletableFuture that completes when the state is saved
-
getMessages
Gets the message history for a thread.- Parameters:
threadName- the thread name- Returns:
- the list of messages in the thread
-
getMessages
Gets the message history for the default thread.- Returns:
- the list of messages
-
updateMessages
Updates the messages for a thread and persists them.- Parameters:
threadName- the thread namemessages- the messages to save- Returns:
- a CompletableFuture that completes when saved
-
chat
Creates a new Chat instance for the default thread.- Returns:
- a new Chat instance
-
chat
Creates a new Chat instance with options.- Parameters:
options- the chat options- Returns:
- a new Chat instance
-
chat
Creates a new Chat instance for a specific thread.- Parameters:
threadName- the thread name- Returns:
- a new Chat instance
-
chat
Creates a new Chat instance for a specific thread with options.- Parameters:
threadName- the thread nameoptions- the chat options- Returns:
- a new Chat instance
-
getStore
Gets the session store.- Returns:
- the session store
-
getRegistry
Gets the registry.- Returns:
- the registry
-
getAgentRegistry
Gets the agent registry for multi-agent handoffs.- Returns:
- the agent registry, or null if not set
-
getSessionData
Gets the session data.- Returns:
- the session data
-
toJSON
Serializes the session to JSON-compatible data.- Returns:
- the session data
-
create
Creates a new Session with a generated ID.- Type Parameters:
S- the state type- Parameters:
registry- the Genkit registryoptions- the session options- Returns:
- a new Session
-
create
public static <S> Session<S> create(Registry registry, SessionOptions<S> options, Map<String, Agent> agentRegistry) Creates a new Session with a generated ID and agent registry.- Type Parameters:
S- the state type- Parameters:
registry- the Genkit registryoptions- the session optionsagentRegistry- the agent registry for multi-agent handoffs (may be null)- Returns:
- a new Session
-
load
public static <S> CompletableFuture<Session<S>> load(Registry registry, String sessionId, SessionOptions<S> options) Loads an existing session from a store.- Type Parameters:
S- the state type- Parameters:
registry- the Genkit registrysessionId- the session ID to loadoptions- the session options (must include store)- Returns:
- a CompletableFuture containing the loaded session, or null if not found
-
load
public static <S> CompletableFuture<Session<S>> load(Registry registry, String sessionId, SessionOptions<S> options, Map<String, Agent> agentRegistry) Loads an existing session from a store with agent registry.- Type Parameters:
S- the state type- Parameters:
registry- the Genkit registrysessionId- the session ID to loadoptions- the session options (must include store)agentRegistry- the agent registry for multi-agent handoffs (may be null)- Returns:
- a CompletableFuture containing the loaded session, or null if not found
-