adk_session/service.rs
1use crate::{Event, Session};
2use adk_core::Result;
3use adk_core::identity::{AdkIdentity, AppName, SessionId, UserId};
4use async_trait::async_trait;
5use chrono::{DateTime, Utc};
6use serde_json::Value;
7use std::collections::HashMap;
8
9/// Request to create a new session.
10#[derive(Debug, Clone)]
11pub struct CreateRequest {
12 /// Application name that owns the session.
13 pub app_name: String,
14 /// User identifier for the session owner.
15 pub user_id: String,
16 /// Optional session ID; generated if not provided.
17 pub session_id: Option<String>,
18 /// Initial state key-value pairs for the session.
19 pub state: HashMap<String, Value>,
20}
21
22impl CreateRequest {
23 /// Returns the application name as a typed [`AppName`].
24 ///
25 /// # Errors
26 ///
27 /// Returns an error if the raw string fails identity validation.
28 pub fn try_app_name(&self) -> Result<AppName> {
29 Ok(AppName::try_from(self.app_name.as_str())?)
30 }
31
32 /// Returns the user identifier as a typed [`UserId`].
33 ///
34 /// # Errors
35 ///
36 /// Returns an error if the raw string fails identity validation.
37 pub fn try_user_id(&self) -> Result<UserId> {
38 Ok(UserId::try_from(self.user_id.as_str())?)
39 }
40
41 /// Returns the session identifier as a typed [`SessionId`], if one was
42 /// provided.
43 ///
44 /// Returns `Ok(None)` when `session_id` is `None` (the service will
45 /// generate one). Returns an error only when a non-`None` value fails
46 /// identity validation.
47 ///
48 /// # Errors
49 ///
50 /// Returns an error if the provided session ID string fails validation.
51 pub fn try_session_id(&self) -> Result<Option<SessionId>> {
52 self.session_id.as_deref().map(SessionId::try_from).transpose().map_err(Into::into)
53 }
54
55 /// Returns the stable session-scoped [`AdkIdentity`] triple, if a session
56 /// ID was provided.
57 ///
58 /// Because `CreateRequest` allows `session_id` to be `None` (the backend
59 /// generates one), this returns `Ok(None)` when no session ID is present.
60 ///
61 /// # Errors
62 ///
63 /// Returns an error if any of the constituent identifiers fail validation.
64 pub fn try_identity(&self) -> Result<Option<AdkIdentity>> {
65 let Some(sid) = self.try_session_id()? else {
66 return Ok(None);
67 };
68 Ok(Some(AdkIdentity {
69 app_name: self.try_app_name()?,
70 user_id: self.try_user_id()?,
71 session_id: sid,
72 }))
73 }
74}
75
76/// Request to retrieve an existing session.
77#[derive(Debug, Clone)]
78pub struct GetRequest {
79 /// Application name that owns the session.
80 pub app_name: String,
81 /// User identifier for the session owner.
82 pub user_id: String,
83 /// Session identifier to retrieve.
84 pub session_id: String,
85 /// If set, only return the N most recent events.
86 pub num_recent_events: Option<usize>,
87 /// If set, only return events after this timestamp.
88 pub after: Option<DateTime<Utc>>,
89}
90
91impl GetRequest {
92 /// Returns the stable session-scoped [`AdkIdentity`] triple.
93 ///
94 /// Parses `app_name`, `user_id`, and `session_id` into their typed
95 /// equivalents and combines them into an [`AdkIdentity`].
96 ///
97 /// # Errors
98 ///
99 /// Returns an error if any of the three identifiers fail validation.
100 pub fn try_identity(&self) -> Result<AdkIdentity> {
101 Ok(AdkIdentity {
102 app_name: AppName::try_from(self.app_name.as_str())?,
103 user_id: UserId::try_from(self.user_id.as_str())?,
104 session_id: SessionId::try_from(self.session_id.as_str())?,
105 })
106 }
107}
108
109/// Request to list sessions for a given app and user.
110#[derive(Debug, Clone)]
111pub struct ListRequest {
112 /// Application name to filter sessions by.
113 pub app_name: String,
114 /// User identifier to filter sessions by.
115 pub user_id: String,
116 /// Maximum number of sessions to return. `None` means no limit.
117 pub limit: Option<usize>,
118 /// Number of sessions to skip for pagination. `None` means start from the beginning.
119 pub offset: Option<usize>,
120}
121
122impl ListRequest {
123 /// Returns the application name as a typed [`AppName`].
124 ///
125 /// # Errors
126 ///
127 /// Returns an error if the raw string fails identity validation.
128 pub fn try_app_name(&self) -> Result<AppName> {
129 Ok(AppName::try_from(self.app_name.as_str())?)
130 }
131
132 /// Returns the user identifier as a typed [`UserId`].
133 ///
134 /// # Errors
135 ///
136 /// Returns an error if the raw string fails identity validation.
137 pub fn try_user_id(&self) -> Result<UserId> {
138 Ok(UserId::try_from(self.user_id.as_str())?)
139 }
140}
141
142/// Request to append an event to a session using typed [`AdkIdentity`] addressing.
143///
144/// This is the preferred way to append events in new code because it uses the
145/// full `(app_name, user_id, session_id)` triple, eliminating ambiguity that
146/// can arise when a bare `session_id` string is not globally unique.
147///
148/// # Example
149///
150/// ```rust
151/// use adk_core::identity::{AdkIdentity, AppName, SessionId, UserId};
152/// use adk_session::AppendEventRequest;
153/// use adk_session::Event;
154///
155/// let identity = AdkIdentity::new(
156/// AppName::try_from("weather-app").unwrap(),
157/// UserId::try_from("user-123").unwrap(),
158/// SessionId::try_from("session-456").unwrap(),
159/// );
160///
161/// let event = Event::new("inv-001");
162/// let req = AppendEventRequest { identity, event };
163/// ```
164#[derive(Debug, Clone)]
165pub struct AppendEventRequest {
166 /// The typed session-scoped identity triple.
167 pub identity: AdkIdentity,
168 /// The event to append.
169 pub event: Event,
170}
171
172/// Request to delete a session.
173#[derive(Debug, Clone)]
174pub struct DeleteRequest {
175 /// Application name that owns the session.
176 pub app_name: String,
177 /// User identifier for the session owner.
178 pub user_id: String,
179 /// Session identifier to delete.
180 pub session_id: String,
181}
182
183impl DeleteRequest {
184 /// Returns the stable session-scoped [`AdkIdentity`] triple.
185 ///
186 /// Parses `app_name`, `user_id`, and `session_id` into their typed
187 /// equivalents and combines them into an [`AdkIdentity`].
188 ///
189 /// # Errors
190 ///
191 /// Returns an error if any of the three identifiers fail validation.
192 pub fn try_identity(&self) -> Result<AdkIdentity> {
193 Ok(AdkIdentity {
194 app_name: AppName::try_from(self.app_name.as_str())?,
195 user_id: UserId::try_from(self.user_id.as_str())?,
196 session_id: SessionId::try_from(self.session_id.as_str())?,
197 })
198 }
199}
200
201/// Trait for session persistence backends.
202///
203/// Implementations manage the full lifecycle of sessions: creation, retrieval,
204/// listing, deletion, and event appending.
205#[async_trait]
206pub trait SessionService: Send + Sync {
207 /// Create a new session and return it.
208 async fn create(&self, req: CreateRequest) -> Result<Box<dyn Session>>;
209 /// Retrieve an existing session by its identifiers.
210 async fn get(&self, req: GetRequest) -> Result<Box<dyn Session>>;
211 /// List sessions for a given app and user.
212 async fn list(&self, req: ListRequest) -> Result<Vec<Box<dyn Session>>>;
213 /// Delete a session by its identifiers.
214 async fn delete(&self, req: DeleteRequest) -> Result<()>;
215 /// Append an event to a session identified by its session ID string.
216 async fn append_event(&self, session_id: &str, event: Event) -> Result<()>;
217
218 /// Get a session using typed [`AdkIdentity`] addressing.
219 ///
220 /// This is the preferred path for new code. It constructs a [`GetRequest`]
221 /// from the full `(app_name, user_id, session_id)` triple so that session
222 /// lookup is unambiguous.
223 ///
224 /// The default implementation delegates to
225 /// [`get`](SessionService::get) with a freshly built [`GetRequest`].
226 ///
227 /// # Errors
228 ///
229 /// Returns an error if the session cannot be retrieved.
230 async fn get_for_identity(&self, identity: &AdkIdentity) -> Result<Box<dyn Session>> {
231 self.get(GetRequest {
232 app_name: identity.app_name.as_ref().to_string(),
233 user_id: identity.user_id.as_ref().to_string(),
234 session_id: identity.session_id.as_ref().to_string(),
235 num_recent_events: None,
236 after: None,
237 })
238 .await
239 }
240
241 /// Delete a session using typed [`AdkIdentity`] addressing.
242 ///
243 /// This is the preferred path for new code. It constructs a
244 /// [`DeleteRequest`] from the full `(app_name, user_id, session_id)` triple
245 /// so that session deletion is unambiguous.
246 ///
247 /// The default implementation delegates to
248 /// [`delete`](SessionService::delete) with a freshly built
249 /// [`DeleteRequest`].
250 ///
251 /// # Errors
252 ///
253 /// Returns an error if the session cannot be deleted.
254 async fn delete_for_identity(&self, identity: &AdkIdentity) -> Result<()> {
255 self.delete(DeleteRequest {
256 app_name: identity.app_name.as_ref().to_string(),
257 user_id: identity.user_id.as_ref().to_string(),
258 session_id: identity.session_id.as_ref().to_string(),
259 })
260 .await
261 }
262
263 /// Append an event to a session using typed [`AdkIdentity`] addressing.
264 ///
265 /// This is the preferred path for new code. It uses the full
266 /// `(app_name, user_id, session_id)` triple so that session lookup is
267 /// unambiguous even when the same `session_id` string appears under
268 /// different apps or users.
269 ///
270 /// The default implementation delegates to the legacy
271 /// [`append_event`](SessionService::append_event) method using only the
272 /// `session_id` component. Backends that support composite-key addressing
273 /// should override this method to use all three identity fields.
274 ///
275 /// # Errors
276 ///
277 /// Returns an error if the event cannot be appended.
278 async fn append_event_for_identity(&self, req: AppendEventRequest) -> Result<()> {
279 self.append_event(req.identity.session_id.as_ref(), req.event).await
280 }
281
282 /// Delete all sessions for a given app and user.
283 ///
284 /// Removes all sessions and their associated events. Useful for
285 /// bulk cleanup and GDPR right-to-erasure compliance.
286 /// The default implementation returns an error.
287 async fn delete_all_sessions(&self, app_name: &str, user_id: &str) -> Result<()> {
288 let _ = (app_name, user_id);
289 Err(adk_core::AdkError::session("delete_all_sessions not implemented"))
290 }
291
292 /// Verify backend connectivity.
293 ///
294 /// Returns `Ok(())` if the backend is reachable and responsive.
295 /// Use this for Kubernetes readiness probes and `/healthz` endpoints.
296 /// The default implementation always succeeds (suitable for in-memory).
297 async fn health_check(&self) -> Result<()> {
298 Ok(())
299 }
300}