Skip to main content

adk_session/
lib.rs

1//! # adk-session
2//!
3//! Session management and state persistence for ADK agents.
4//!
5//! ## Overview
6//!
7//! This crate provides session and state management:
8//!
9//! - [`InMemorySessionService`] - Simple in-memory session storage
10//! - `SqliteSessionService`, `PostgresSessionService`, `RedisSessionService`,
11//!   `MongoSessionService`, `Neo4jSessionService`, `FirestoreSessionService`, and
12//!   `VertexAiSessionService` - database backends, each behind its feature
13//! - `EncryptedSession` - AES-256-GCM encryption over any backend (`encrypted-session` feature)
14//! - Durable stores for every backend: spend ledgers, action ledgers, approval stores, audit log
15//!   stores, the lease, run, schedule, and governance state stores of the durable runtime, and
16//!   the subject registries and consent stores of [`adk_core::subject`]
17//! - [`SessionSubjectData`] - A subject's sessions, exported and erased through any backend
18//! - [`archive`] - Archive, unarchive and export sessions on every backend
19//! - [`Session`] - Conversation session with state and events
20//! - [`State`] - Key-value state with typed prefixes
21//! - [`SessionService`] - Trait for custom session backends
22//!
23//! ## Quick Start
24//!
25//! ```rust
26//! use adk_session::{CreateRequest, InMemorySessionService, SessionService};
27//!
28//! # #[tokio::main(flavor = "current_thread")]
29//! # async fn main() -> adk_core::Result<()> {
30//! let service = InMemorySessionService::new();
31//!
32//! // Create the session before a Runner runs against it.
33//! let session = service
34//!     .create(CreateRequest {
35//!         app_name: "my_app".into(),
36//!         user_id: "user".into(),
37//!         session_id: Some("session".into()),
38//!         state: Default::default(),
39//!     })
40//!     .await?;
41//! assert_eq!(session.id(), "session");
42//! # Ok(())
43//! # }
44//! ```
45//!
46//! ## State Prefixes
47//!
48//! ADK uses prefixes to organize state:
49//!
50//! | Prefix | Constant | Purpose |
51//! |--------|----------|---------|
52//! | `user:` | [`KEY_PREFIX_USER`] | Shared by every session of the user |
53//! | `app:` | [`KEY_PREFIX_APP`] | Shared by every session of the app |
54//! | `temp:` | [`KEY_PREFIX_TEMP`] | Not persisted |
55//!
56//! ## Feature flags
57//!
58//! No feature is on by default; [`InMemorySessionService`] needs none.
59//!
60//! | Feature | Enables |
61//! |---------|---------|
62//! | `sqlite` | `SqliteSessionService` |
63//! | `database` | Alias for `sqlite` |
64//! | `postgres` | `PostgresSessionService`, with TLS |
65//! | `redis` | `RedisSessionService` on a standalone server or Redis Cluster |
66//! | `mongodb` | `MongoSessionService` |
67//! | `neo4j` | `Neo4jSessionService` |
68//! | `firestore` | `FirestoreSessionService` |
69//! | `vertex-session` | `VertexAiSessionService` over the Vertex AI Session API |
70//! | `encrypted-session` | `EncryptedSession`: AES-256-GCM encryption at rest over any backend, with key rotation |
71#![allow(clippy::result_large_err)]
72#![deny(missing_docs)]
73
74#[cfg(any(feature = "sqlite", feature = "postgres"))]
75/// SQL-backed approval stores for calls held for confirmation.
76pub mod approval;
77pub mod archive;
78#[cfg(any(feature = "sqlite", feature = "postgres"))]
79/// SQL-backed audit log stores for signed, Merkle-hashed audit logs.
80pub mod audit_log;
81/// A session service that writes nothing lasting for a subject without its consent.
82pub mod consent_gate;
83#[cfg(any(feature = "sqlite", feature = "postgres"))]
84mod durable_sql;
85/// Event types and the [`Events`] trait for accessing session event history.
86pub mod event;
87#[cfg(any(feature = "sqlite", feature = "postgres"))]
88/// SQL-backed governance state stores for a kill switch shared by every replica.
89pub mod governance_state;
90pub mod history;
91/// In-memory session backend for testing and lightweight use cases.
92pub mod inmemory;
93#[cfg(any(feature = "sqlite", feature = "postgres"))]
94/// SQL-backed lease stores with fencing epochs.
95pub mod lease;
96/// Schema migration utilities for database-backed session stores.
97pub mod migration;
98#[cfg(any(feature = "sqlite", feature = "postgres"))]
99/// SQL-backed durable run queues.
100pub mod run_store;
101#[cfg(any(feature = "sqlite", feature = "postgres"))]
102/// SQL-backed schedule stores.
103pub mod schedule;
104/// Session service trait and request/response types.
105pub mod service;
106/// The [`Session`] trait and state key prefix constants.
107pub mod session;
108/// State access traits ([`State`] and [`ReadonlyState`]).
109pub mod state;
110/// Shared utilities for extracting and merging state deltas across backends.
111pub mod state_utils;
112pub mod subject;
113#[cfg(any(feature = "sqlite", feature = "postgres"))]
114/// SQL-backed subject registries and consent stores.
115pub mod subject_sql;
116
117#[cfg(any(feature = "redis", feature = "mongodb", feature = "neo4j", feature = "firestore"))]
118mod durable_common;
119#[cfg(any(feature = "mongodb", feature = "neo4j", feature = "firestore"))]
120mod durable_transitions;
121#[cfg(any(feature = "redis", feature = "mongodb", feature = "neo4j", feature = "firestore"))]
122mod subject_common;
123
124#[cfg(feature = "encrypted-session")]
125/// AES-256-GCM encrypted session wrapper with key rotation.
126pub mod encrypted;
127#[cfg(feature = "encrypted-session")]
128/// Approval arguments and decisions sealed at rest (spec 3.1 §7.12, §10 S5).
129pub mod encrypted_approval;
130#[cfg(feature = "encrypted-session")]
131/// Encryption key management for encrypted sessions.
132pub mod encryption_key;
133#[cfg(feature = "encrypted-session")]
134/// The `adk-enc:v1:` envelope other stores wrap their payloads with (spec 3.1 §10 S5).
135pub mod envelope;
136#[cfg(feature = "firestore")]
137/// Google Cloud Firestore session backend.
138pub mod firestore;
139#[cfg(feature = "firestore")]
140/// Firestore-backed audit log store for signed, Merkle-hashed audit logs.
141pub mod firestore_audit_log;
142#[cfg(feature = "firestore")]
143/// Firestore-backed lease, run, schedule, and governance state stores.
144pub mod firestore_runtime;
145#[cfg(feature = "firestore")]
146/// Firestore-backed spend ledger, action ledger, and approval store.
147pub mod firestore_stores;
148#[cfg(feature = "mongodb")]
149/// MongoDB session backend.
150pub mod mongodb;
151#[cfg(feature = "mongodb")]
152/// MongoDB-backed audit log store for signed, Merkle-hashed audit logs.
153pub mod mongodb_audit_log;
154#[cfg(feature = "mongodb")]
155/// MongoDB-backed lease, run, schedule, and governance state stores.
156pub mod mongodb_runtime;
157#[cfg(feature = "mongodb")]
158/// MongoDB-backed spend ledger, action ledger, and approval store.
159pub mod mongodb_stores;
160#[cfg(feature = "neo4j")]
161/// Neo4j graph database session backend.
162pub mod neo4j;
163#[cfg(feature = "neo4j")]
164/// Neo4j-backed audit log store for signed, Merkle-hashed audit logs.
165pub mod neo4j_audit_log;
166#[cfg(feature = "neo4j")]
167/// Neo4j-backed lease, run, schedule, and governance state stores.
168pub mod neo4j_runtime;
169#[cfg(feature = "neo4j")]
170/// Neo4j-backed spend ledger, action ledger, and approval store.
171pub mod neo4j_stores;
172#[cfg(feature = "postgres")]
173/// PostgreSQL session backend.
174pub mod postgres;
175#[cfg(feature = "postgres")]
176/// PostgreSQL-backed action ledger for non-idempotent tool calls.
177pub mod postgres_action_ledger;
178#[cfg(feature = "redis")]
179/// Redis session backend with TTL support.
180pub mod redis;
181#[cfg(feature = "redis")]
182/// Redis-backed audit log store for signed, Merkle-hashed audit logs.
183pub mod redis_audit_log;
184#[cfg(feature = "redis")]
185/// Redis-backed lease, run, schedule, and governance state stores.
186pub mod redis_runtime;
187#[cfg(feature = "redis")]
188/// Redis-backed spend ledger, action ledger, and approval store.
189pub mod redis_stores;
190#[cfg(any(feature = "sqlite", feature = "postgres"))]
191/// SQL-backed spend ledgers shared by model calls and payments.
192pub mod spend;
193#[cfg(feature = "sqlite")]
194/// SQLite session backend.
195pub mod sqlite;
196#[cfg(feature = "sqlite")]
197/// SQLite-backed action ledger for non-idempotent tool calls.
198pub mod sqlite_action_ledger;
199#[cfg(feature = "firestore")]
200/// Firestore-backed subject registry and consent store.
201pub mod subject_firestore;
202#[cfg(feature = "mongodb")]
203/// MongoDB-backed subject registry and consent store.
204pub mod subject_mongodb;
205#[cfg(feature = "neo4j")]
206/// Neo4j-backed subject registry and consent store.
207pub mod subject_neo4j;
208#[cfg(feature = "redis")]
209/// Redis-backed subject registry and consent store.
210pub mod subject_redis;
211#[cfg(feature = "vertex-session")]
212/// Vertex AI Session API backend.
213pub mod vertex;
214
215pub use archive::{
216    ARCHIVE_AUTHOR, ARCHIVE_KEY, ArchiveMark, SESSION_BUNDLE_FORMAT, SessionBundle, ensure_writable,
217};
218pub use consent_gate::ConsentGatedSessionService;
219pub use event::{Event, EventActions, Events};
220pub use history::{
221    FORK_ORIGIN_KEY, ForkOrigin, ForkRequest, HistoryChange, HistoryEdit, HistoryOperation,
222    HistoryOutcome, HistoryRewrite, ImportSessionRequest, RewriteHistoryRequest, RewrittenHistory,
223    SessionHistory,
224};
225pub use inmemory::InMemorySessionService;
226pub use service::{
227    AppendEventRequest, CreateRequest, DeleteRequest, GetRequest, ListRequest, SessionService,
228};
229pub use session::{KEY_PREFIX_APP, KEY_PREFIX_TEMP, KEY_PREFIX_USER, Session};
230pub use state::{ReadonlyState, State};
231pub use state_utils::{extract_state_deltas, merge_states};
232pub use subject::SessionSubjectData;
233#[cfg(feature = "firestore")]
234pub use subject_firestore::{FirestoreConsentStore, FirestoreSubjectRegistry};
235#[cfg(feature = "mongodb")]
236pub use subject_mongodb::{MongoConsentStore, MongoSubjectRegistry};
237#[cfg(feature = "neo4j")]
238pub use subject_neo4j::{Neo4jConsentStore, Neo4jSubjectRegistry};
239#[cfg(feature = "redis")]
240pub use subject_redis::{RedisConsentStore, RedisSubjectRegistry};
241#[cfg(feature = "postgres")]
242pub use subject_sql::{PostgresConsentStore, PostgresSubjectRegistry};
243#[cfg(feature = "sqlite")]
244pub use subject_sql::{SqliteConsentStore, SqliteSubjectRegistry};
245
246#[cfg(feature = "postgres")]
247pub use approval::PostgresApprovalStore;
248#[cfg(feature = "sqlite")]
249pub use approval::SqliteApprovalStore;
250#[cfg(feature = "postgres")]
251pub use audit_log::PostgresAuditLogStore;
252#[cfg(feature = "sqlite")]
253pub use audit_log::SqliteAuditLogStore;
254#[cfg(feature = "firestore")]
255pub use firestore_audit_log::FirestoreAuditLogStore;
256#[cfg(feature = "firestore")]
257pub use firestore_runtime::{
258    FirestoreGovernanceStateStore, FirestoreLeaseStore, FirestoreRunStore, FirestoreScheduleStore,
259};
260#[cfg(feature = "firestore")]
261pub use firestore_stores::{FirestoreActionLedger, FirestoreApprovalStore, FirestoreSpendLedger};
262#[cfg(feature = "postgres")]
263pub use governance_state::PostgresGovernanceStateStore;
264#[cfg(feature = "sqlite")]
265pub use governance_state::SqliteGovernanceStateStore;
266#[cfg(feature = "postgres")]
267pub use lease::PostgresLeaseStore;
268#[cfg(feature = "sqlite")]
269pub use lease::SqliteLeaseStore;
270#[cfg(feature = "mongodb")]
271pub use mongodb_audit_log::MongoAuditLogStore;
272#[cfg(feature = "mongodb")]
273pub use mongodb_runtime::{
274    MongoGovernanceStateStore, MongoLeaseStore, MongoRunStore, MongoScheduleStore,
275};
276#[cfg(feature = "mongodb")]
277pub use mongodb_stores::{MongoActionLedger, MongoApprovalStore, MongoSpendLedger};
278#[cfg(feature = "neo4j")]
279pub use neo4j_audit_log::Neo4jAuditLogStore;
280#[cfg(feature = "neo4j")]
281pub use neo4j_runtime::{
282    Neo4jGovernanceStateStore, Neo4jLeaseStore, Neo4jRunStore, Neo4jScheduleStore,
283};
284#[cfg(feature = "neo4j")]
285pub use neo4j_stores::{Neo4jActionLedger, Neo4jApprovalStore, Neo4jSpendLedger};
286#[cfg(feature = "postgres")]
287pub use postgres_action_ledger::PostgresActionLedger;
288#[cfg(feature = "redis")]
289pub use redis_audit_log::RedisAuditLogStore;
290#[cfg(feature = "redis")]
291pub use redis_runtime::{
292    RedisGovernanceStateStore, RedisLeaseStore, RedisRunStore, RedisScheduleStore,
293};
294#[cfg(feature = "redis")]
295pub use redis_stores::{RedisActionLedger, RedisApprovalStore, RedisSpendLedger};
296#[cfg(feature = "postgres")]
297pub use run_store::PostgresRunStore;
298#[cfg(feature = "sqlite")]
299pub use run_store::SqliteRunStore;
300#[cfg(feature = "postgres")]
301pub use schedule::PostgresScheduleStore;
302#[cfg(feature = "sqlite")]
303pub use schedule::SqliteScheduleStore;
304#[cfg(feature = "postgres")]
305pub use spend::PostgresSpendLedger;
306#[cfg(feature = "sqlite")]
307pub use spend::SqliteSpendLedger;
308#[cfg(feature = "sqlite")]
309pub use sqlite::SqliteSessionService;
310#[cfg(feature = "sqlite")]
311pub use sqlite_action_ledger::SqliteActionLedger;
312
313#[cfg(feature = "encrypted-session")]
314pub use encrypted::EncryptedSession;
315#[cfg(feature = "encrypted-session")]
316pub use encrypted_approval::EncryptedApprovalStore;
317#[cfg(feature = "encrypted-session")]
318pub use encryption_key::EncryptionKey;
319#[cfg(feature = "firestore")]
320pub use firestore::{
321    FirestoreSessionConfig, FirestoreSessionService, app_state_path as firestore_app_state_path,
322    event_path as firestore_event_path, session_path as firestore_session_path,
323    user_state_path as firestore_user_state_path,
324};
325#[cfg(feature = "mongodb")]
326pub use mongodb::MongoSessionService;
327#[cfg(feature = "neo4j")]
328pub use neo4j::Neo4jSessionService;
329#[cfg(feature = "postgres")]
330pub use postgres::PostgresSessionService;
331#[cfg(feature = "redis")]
332pub use redis::{
333    RedisSessionConfig, RedisSessionService, app_state_key, events_key, index_key, session_key,
334    updated_index_key, user_state_key,
335};
336#[cfg(feature = "vertex-session")]
337pub use vertex::{VertexAiSessionConfig, VertexAiSessionService};