Class FirestoreSessionStore<S>
- Type Parameters:
S- the type of custom session state
- All Implemented Interfaces:
SessionStore<S>,SnapshotReader<S>,SnapshotSubscriber,SnapshotWriter<S>
SessionStore and SnapshotSubscriber.
Persists session snapshots with a sharded checkpoint + diff + pointer layout, mirroring the
upstream Go (go/plugins/firebase/exp/firestore_session_store.go) and JS (
js/plugins/google-cloud/src/session-store/firestore.ts) implementations.
Storage layout
For a configured collection and per-tenant prefix (default "global"):
<collection>/<prefix>/snapshots/<snapshotId>— one metadata document per snapshot.kindis"checkpoint"(full state stored out-of-band in shards) or"diff"(RFC-6902statePatchfrom its parent). Each document carriescheckpointId(nearest checkpoint ancestor),checkpointShardCount,segmentPath(ordered diff ids from the checkpoint exclusive → this doc inclusive) and the snapshot metadata fields.statePatchanderrorare stored as opaque JSON strings because Firestore disallows nested arrays.<collection>-shards/<prefix>/shards/<checkpointId>_<index>— checkpoint state JSON (UTF-8) split intoshardSize-byte chunks.<collection>-pointers/<prefix>/pointers/<sessionId>— the current leaf pointer for a session (currentSnapshotId,checkpointId,checkpointShardCount,segmentPath,currentCreatedAt,updatedAt). Carries no state.
Concurrency
saveSnapshot(java.lang.String, com.google.genkit.ai.agent.SnapshotMutator<S>, com.google.genkit.ai.agent.SessionStoreOptions) runs inside a Firestore transaction (read existing → apply mutator →
write). The mutator is treated as pure and may be re-invoked on transaction retry.
-
Constructor Summary
ConstructorsConstructorDescriptionFirestoreSessionStore(com.google.cloud.firestore.Firestore db) Creates a store with default options.FirestoreSessionStore(com.google.cloud.firestore.Firestore db, FirestoreSessionStoreOptions options) Creates a store. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves a session snapshot.onSnapshotStateChange(String snapshotId, Consumer<SessionSnapshot<?>> cb, SessionStoreOptions options) Subscribes to status changes for the snapshot identified bysnapshotId.saveSnapshot(String snapshotId, SnapshotMutator<S> mutator, SessionStoreOptions options) Atomically reads the snapshot identified bysnapshotId(or creates a new one), appliesmutator, and persists the result.
-
Constructor Details
-
FirestoreSessionStore
public FirestoreSessionStore(com.google.cloud.firestore.Firestore db) Creates a store with default options.- Parameters:
db- the Firestore client
-
FirestoreSessionStore
public FirestoreSessionStore(com.google.cloud.firestore.Firestore db, FirestoreSessionStoreOptions options) Creates a store.- Parameters:
db- the Firestore clientoptions- the store options
-
-
Method Details
-
saveSnapshot
public String saveSnapshot(String snapshotId, SnapshotMutator<S> mutator, SessionStoreOptions options) Atomically reads the snapshot identified bysnapshotId(or creates a new one), appliesmutator, and persists the result.Runs a Firestore transaction implementing the same identity/sessionId/status defaulting contract as the in-memory reference store:
- Read the existing snapshot doc; reconstruct its full state when present.
- Apply the (pure) mutator. If it returns
null, no write occurs andnullis returned. - Mint a UUID id when none supplied; preserve
sessionIdfrom the existing row; reject emptysessionIdwithINVALID_ARGUMENT; defaultnullstatus toCOMPLETED. - Decide checkpoint vs diff and write the snapshot doc (+ shards for a checkpoint).
- Advance the session pointer to the new leaf (never backward).
- Specified by:
saveSnapshotin interfaceSnapshotWriter<S>- Parameters:
snapshotId- the ID of the snapshot to write; ifnullthe store mints a UUIDmutator- the pure read-modify-write function; receives the existing snapshot (ornull) and returns the snapshot to persist, ornullto declineoptions- store options- Returns:
- the snapshot ID that was written, or
nullif the mutator declined
-
getSnapshot
Retrieves a session snapshot.When
GetSnapshotOptions.getSnapshotId()is set the store returns that specific snapshot. WhenGetSnapshotOptions.getSessionId()is set the store resolves and returns the latest leaf snapshot for that session. Returnsnullif no matching snapshot exists.By
snapshotId: loads the snapshot doc and reconstructs its full state from the nearest checkpoint's shards plus the orderedsegmentPathdiffs. BysessionId: reads the session pointer, then loads and reconstructs the pointed snapshot.- Specified by:
getSnapshotin interfaceSnapshotReader<S>- Parameters:
opts- options specifying which snapshot to retrieve; exactly one ofsnapshotId/sessionIdmust be set- Returns:
- the matching snapshot, or
nullif none exists
-
onSnapshotStateChange
public AutoCloseable onSnapshotStateChange(String snapshotId, Consumer<SessionSnapshot<?>> cb, SessionStoreOptions options) Subscribes to status changes for the snapshot identified bysnapshotId.Invokes
cbimmediately with the current snapshot and again on every subsequent change. The returnedAutoCloseableunsubscribes when closed.Registers a Firestore realtime listener on the snapshot document. On every event (including the initial snapshot when the document exists) the snapshot is reconstructed off-band (via a read transaction) and passed to
cb. The returnedAutoCloseableremoves the listener.- Specified by:
onSnapshotStateChangein interfaceSnapshotSubscriber- Parameters:
snapshotId- the snapshot to observecb- callback invoked with the updated snapshot on each changeoptions- store options- Returns:
- a handle that cancels the subscription when
AutoCloseable.close()is called
-