Skip to main content

SessionService

Trait SessionService 

Source
pub trait SessionService: Send + Sync {
Show 21 methods // Required methods fn create<'life0, 'async_trait>( &'life0 self, req: CreateRequest, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn get<'life0, 'async_trait>( &'life0 self, req: GetRequest, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn list<'life0, 'async_trait>( &'life0 self, req: ListRequest, ) -> Pin<Box<dyn Future<Output = Result<Vec<Box<dyn Session>>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn delete<'life0, 'async_trait>( &'life0 self, req: DeleteRequest, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn append_event<'life0, 'life1, 'async_trait>( &'life0 self, session_id: &'life1 str, event: Event, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait; // Provided methods fn get_for_identity<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... } fn delete_for_identity<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... } fn append_event_for_identity<'life0, 'async_trait>( &'life0 self, req: AppendEventRequest, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn delete_all_sessions<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, app_name: &'life1 str, user_id: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait { ... } fn history<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<SessionHistory, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... } fn rewrite_history<'life0, 'async_trait>( &'life0 self, _req: RewriteHistoryRequest, ) -> Pin<Box<dyn Future<Output = Result<HistoryOutcome, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn import_session<'life0, 'async_trait>( &'life0 self, _req: ImportSessionRequest, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn fork<'life0, 'async_trait>( &'life0 self, req: ForkRequest, ) -> Pin<Box<dyn Future<Output = Result<HistoryOutcome, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn rewind<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, target_event_id: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait { ... } fn rewind_steps<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, steps: usize, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... } fn health_check<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn archive<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, reason: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<ArchiveMark, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... } fn unarchive<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... } fn export_session<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<SessionBundle, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait { ... } fn list_active<'life0, 'async_trait>( &'life0 self, req: ListRequest, ) -> Pin<Box<dyn Future<Output = Result<Vec<Box<dyn Session>>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... } fn list_archived<'life0, 'async_trait>( &'life0 self, req: ListRequest, ) -> Pin<Box<dyn Future<Output = Result<Vec<Box<dyn Session>>, AdkError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait { ... }
}
Available on crate feature sessions only.
Expand description

Trait for session persistence backends.

Implementations manage the full lifecycle of sessions: creation, retrieval, listing, deletion, and event appending.

Every method validates the app names, user IDs, and session IDs it receives against the AppName, UserId, and SessionId rules (non-empty, at most MAX_ID_LEN bytes, no NUL) before it reads or writes storage, and rejects an invalid one with an InvalidInput error. The built-in backends all follow this rule.

Required Methods§

Source

fn create<'life0, 'async_trait>( &'life0 self, req: CreateRequest, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Creates a new session and returns it.

§Errors

Returns an invalid-input error when an identifier fails validation, and an AdkError with code session.already_exists and a Conflict category when a session with the same (app_name, user_id, session_id) already exists. A conflicting create changes nothing: the existing session keeps its events and state, and the app and user tiers are not written. Callers that get-or-create read the session again on a conflict.

Source

fn get<'life0, 'async_trait>( &'life0 self, req: GetRequest, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Retrieves an existing session by its complete identity.

Implementations address sessions by the full (app_name, user_id, session_id) tuple. When that valid identity has no matching record, implementations return an AdkError with code session.not_found and a NotFound category. Backend and transport failures must not be reported as not-found errors.

§Errors

Returns an invalid-input error when an identifier fails validation, session.not_found when no matching session exists, or a backend-specific error when retrieval fails.

Source

fn list<'life0, 'async_trait>( &'life0 self, req: ListRequest, ) -> Pin<Box<dyn Future<Output = Result<Vec<Box<dyn Session>>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Lists the sessions of an app and user, most recently updated first.

Sessions updated at the same instant are ordered by session ID, descending, so offset/limit pages are stable. Archived sessions are listed too, with their ArchiveMark in their state; list_active leaves them out.

Source

fn delete<'life0, 'async_trait>( &'life0 self, req: DeleteRequest, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Delete a session by its identifiers.

Source

fn append_event<'life0, 'life1, 'async_trait>( &'life0 self, session_id: &'life1 str, event: Event, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Appends an event to the session with this ID.

Events read back in timestamp order; events with equal timestamps keep the order they were appended in.

§Errors

Returns session.not_found (NotFound) when no session has this ID. An ID alone does not name one session when more than one app or user holds it, so prefer append_event_for_identity.

Provided Methods§

Source

fn get_for_identity<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Get a session using typed AdkIdentity addressing.

This is the preferred path for new code. It constructs a GetRequest from the full (app_name, user_id, session_id) triple so that session lookup is unambiguous.

The default implementation delegates to get with a freshly built GetRequest.

§Errors

Returns an error if the session cannot be retrieved.

Source

fn delete_for_identity<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Delete a session using typed AdkIdentity addressing.

This is the preferred path for new code. It constructs a DeleteRequest from the full (app_name, user_id, session_id) triple so that session deletion is unambiguous.

The default implementation delegates to delete with a freshly built DeleteRequest.

§Errors

Returns an error if the session cannot be deleted.

Source

fn append_event_for_identity<'life0, 'async_trait>( &'life0 self, req: AppendEventRequest, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Append an event to a session using typed AdkIdentity addressing.

This is the preferred path for new code. It uses the full (app_name, user_id, session_id) triple so that session lookup is unambiguous even when the same session_id string appears under different apps or users.

The default implementation delegates to the legacy append_event method using only the session_id component. Backends that support composite-key addressing should override this method to use all three identity fields.

§Errors

Returns an error if the event cannot be appended.

Source

fn delete_all_sessions<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, app_name: &'life1 str, user_id: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

Erases what the user user_id holds in the app app_name: every session with its events, and the user’s user: state tier.

The app: tier, the user’s sessions and state in other apps, and other users’ sessions and state are untouched. No other method removes the user: tier, so this is the call for bulk cleanup and for a request to erase a user’s session data. The tier is erased when the user has no sessions left, and a repeated call succeeds.

The default implementation validates the identifiers, then returns session.delete_all.unsupported.

§Errors

Returns an invalid-input error when an identifier fails validation, session.delete_all.unsupported (Unsupported) from a backend that does not implement it, or a backend error. A backend that cannot erase in one transaction may have removed some sessions before the error; calling again finishes the erasure.

Source

fn history<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<SessionHistory, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Reads the stored history of the session identity: its events, its own state, and the state it was created with.

The default reads the session with get_for_identity and reports no recorded creation state; every adk-session backend that records one returns it.

§Errors

Returns session.not_found when the session does not exist, or a backend error.

Source

fn rewrite_history<'life0, 'async_trait>( &'life0 self, _req: RewriteHistoryRequest, ) -> Pin<Box<dyn Future<Output = Result<HistoryOutcome, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Rewrites the history of a session in place: a rewind or an edit.

The backend reads the session, applies SessionHistory::rewrite to it, and writes the result atomically, so a concurrent append either lands before the read or after the write. The session’s own state is rebuilt from the state it was created with plus the state deltas of the events it keeps; the app: and user: tiers do not change. Only the session that identity names changes, even when other apps or users hold a session with the same ID. The rules every history operation follows are in crate::history.

§Errors

Returns session.history.unsupported (Unsupported) when the backend cannot delete events, session.not_found when the session does not exist, and the errors of SessionHistory::rewrite.

Source

fn import_session<'life0, 'async_trait>( &'life0 self, _req: ImportSessionRequest, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Creates a session that already holds events, without applying their state deltas to the app: and user: tiers.

§Errors

Returns session.history.unsupported when the backend does not implement it, session.already_exists (Conflict) when the session exists, and an invalid-input error when an identifier fails validation.

Source

fn fork<'life0, 'async_trait>( &'life0 self, req: ForkRequest, ) -> Pin<Box<dyn Future<Output = Result<HistoryOutcome, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Forks a session: creates a session of the same app and user that holds the source’s events up to ForkRequest::at_event_id, with the state the source had there.

The source does not change. The fork’s events keep their IDs and timestamps, its state is the source’s creation state plus the deltas of the copied events, and it records its ForkOrigin under FORK_ORIGIN_KEY. The default reads the source with history and creates the fork with import_session.

§Example
use adk_session::{CreateRequest, Event, ForkRequest, InMemorySessionService, SessionService};

let service = InMemorySessionService::new();
let source = service
    .create(CreateRequest {
        app_name: "app".into(),
        user_id: "alice".into(),
        session_id: Some("main".into()),
        state: Default::default(),
    })
    .await?;
let identity = source.try_identity()?;
let first = Event::new("inv-1");
let first_id = first.id.clone();
for event in [first, Event::new("inv-2")] {
    service
        .append_event_for_identity(adk_session::AppendEventRequest {
            identity: identity.clone(),
            event,
        })
        .await?;
}

let fork = service.fork(ForkRequest::new(identity).at_event(first_id)).await?;
assert_eq!(fork.session.events().len(), 1);
§Errors

Returns session.not_found when the source does not exist, session.already_exists when the fork’s session ID is taken, session.history.unsupported from a backend that cannot import, and the errors of SessionHistory::fork.

Source

fn rewind<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, target_event_id: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

Rewinds the session identity to target_event_id, removing every event after it.

A rewrite_history with HistoryRewrite::RewindTo.

§Errors

Returns session.event_not_found when the target is not an event of the session, and the errors of rewrite_history.

Source

fn rewind_steps<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, steps: usize, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Session>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Rewinds the session identity by steps events from the end.

steps == 0 returns the session unchanged, and steps equal to the event count removes every event and leaves the state the session was created with.

§Errors

Returns session.rewind.too_many_steps (InvalidInput) when steps exceeds the event count, and the errors of rewrite_history.

Source

fn health_check<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Verify backend connectivity.

Returns Ok(()) if the backend is reachable and responsive. Use this for Kubernetes readiness probes and /healthz endpoints. The default implementation always succeeds (suitable for in-memory).

Source

fn archive<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, reason: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<ArchiveMark, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Archives the session identity: it keeps its events and state, is left out of list_active, and takes no new turns until unarchive (see crate::archive).

The mark is written to the session’s own state under ARCHIVE_KEY by a content-less marker event, through append_event_for_identity, so it works on every backend. Archiving an archived session changes nothing and returns its mark.

§Example
use adk_session::{CreateRequest, InMemorySessionService, SessionService};

let service = InMemorySessionService::new();
let session = service
    .create(CreateRequest {
        app_name: "app".into(),
        user_id: "ana".into(),
        session_id: Some("s".into()),
        state: Default::default(),
    })
    .await?;
let identity = session.try_identity()?;
let first = service.archive(&identity, Some("done".into())).await?;
let again = service.archive(&identity, None).await?;
assert_eq!(first, again);
§Errors

Returns session.not_found when the session does not exist, or a backend error.

Source

fn unarchive<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<(), AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Unarchives the session identity: it lists and takes turns again. Unarchiving a session that is not archived changes nothing.

The mark is cleared by a content-less marker event that sets ARCHIVE_KEY to null.

§Errors

Returns session.not_found when the session does not exist, or a backend error.

Source

fn export_session<'life0, 'life1, 'async_trait>( &'life0 self, identity: &'life1 AdkIdentity, ) -> Pin<Box<dyn Future<Output = Result<SessionBundle, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Writes the session identity out as a portable SessionBundle: its identity, its own state, every event and its archive mark.

§Errors

Returns session.not_found when the session does not exist, or a backend error.

Source

fn list_active<'life0, 'async_trait>( &'life0 self, req: ListRequest, ) -> Pin<Box<dyn Future<Output = Result<Vec<Box<dyn Session>>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Lists the sessions of an app and user that are not archived, in list’s order; offset and limit count only these.

This is the listing a person sees: adk-rust sessions list and the REST API’s session list use it. The default reads list a page at a time and leaves archived sessions out.

§Errors

Returns what list returns.

Source

fn list_archived<'life0, 'async_trait>( &'life0 self, req: ListRequest, ) -> Pin<Box<dyn Future<Output = Result<Vec<Box<dyn Session>>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Lists the archived sessions of an app and user, in list’s order; offset and limit count only these.

§Errors

Returns what list returns.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§