gadk-session 0.4.5

Sessions, events and the in-memory session service for gadk
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2026 Chris Olson
// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! An in-memory [`SessionService`].

use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use gadk_platform::{Platform, go_quote};
use parking_lot::RwLock;
use serde_json::{Map, Value};

use crate::error::SessionError;
use crate::event::{Event, zero_time};
use crate::service::{
    CreateRequest, DeleteRequest, GetRequest, KEY_PREFIX_APP, KEY_PREFIX_USER, ListRequest,
    Session, SessionService, State, compare_by_last_update,
};
use crate::state_util::{
    check_requested_auth_configs, extract_state_deltas, merge_states, require,
    trim_temp_delta_state,
};

type StateMap = Map<String, Value>;
/// `(app_name, user_id, session_id)`.
type SessionKey = (String, String, String);

/// An in-memory [`SessionService`]. Thread-safe.
///
/// State keys are routed by prefix: `app:` keys are shared by every session of the app,
/// `user:` keys by every session of the user, and the rest belong to the session. `temp:`
/// keys in appended event deltas are never persisted; `temp:` keys in the initial state given
/// to `create` are kept in session state. Sessions handed out are copies of the service's
/// record.
///
/// An event appended with an empty ID is given one:
///
/// ```
/// # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
/// use gadk_session::*;
/// use std::sync::Arc;
/// let svc: Arc<dyn SessionService> = Arc::new(InMemorySessionService::new());
/// let s = svc.create(CreateRequest { app_name: "app".into(), user_id: "u".into(), ..Default::default() }).await.unwrap();
/// let mut ev = Event::new(&gadk_platform::Platform::default(), "inv");
/// ev.id.clear();
/// svc.append_event(s.as_ref(), &mut ev).await.unwrap();
/// assert!(!ev.id.is_empty());
/// # });
/// ```
pub struct InMemorySessionService {
    platform: Platform,
    inner: RwLock<Inner>,
}

#[derive(Default)]
struct Inner {
    sessions: BTreeMap<SessionKey, StoredSession>,
    app_state: HashMap<String, StateMap>,
    user_state: HashMap<(String, String), StateMap>,
}

/// The service's canonical record of a session.
struct StoredSession {
    state: StateMap,
    events: Vec<Event>,
    updated_at: DateTime<Utc>,
}

impl InMemorySessionService {
    /// A new, empty service using [`Platform::default`].
    pub fn new() -> Self {
        Self::with_platform(Platform::default())
    }

    /// A new, empty service taking time and session/event IDs from `platform`.
    pub fn with_platform(platform: Platform) -> Self {
        Self {
            platform,
            inner: RwLock::new(Inner::default()),
        }
    }
}

impl Default for InMemorySessionService {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for InMemorySessionService {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("InMemorySessionService")
            .finish_non_exhaustive()
    }
}

impl Inner {
    /// Session state plus the app and user state, prefixed back.
    fn merged_state(&self, state: &StateMap, app_name: &str, user_id: &str) -> StateMap {
        merge_states(
            self.app_state.get(app_name),
            self.user_state
                .get(&(app_name.to_string(), user_id.to_string())),
            state,
        )
    }

    /// Applies a state delta to the app, user and session maps: the delta is split by key
    /// prefix and each part is merged into its map.
    fn apply_delta(
        app_state: &mut HashMap<String, StateMap>,
        user_state: &mut HashMap<(String, String), StateMap>,
        session_state: &mut StateMap,
        delta: &StateMap,
        app_name: &str,
        user_id: &str,
    ) {
        let (app, user, session) = extract_state_deltas(delta);
        app_state
            .entry(app_name.to_string())
            .or_default()
            .extend(app);
        user_state
            .entry((app_name.to_string(), user_id.to_string()))
            .or_default()
            .extend(user);
        session_state.extend(session);
    }
}

#[async_trait]
impl SessionService for InMemorySessionService {
    async fn create(&self, req: CreateRequest) -> Result<Arc<dyn Session>, SessionError> {
        if req.app_name.is_empty() || req.user_id.is_empty() {
            return Err(SessionError::InvalidRequest(format!(
                "app_name and user_id are required, got app_name: {}, user_id: {}",
                go_quote(&req.app_name),
                go_quote(&req.user_id)
            )));
        }
        let session_id = match req.session_id {
            Some(id) if !id.is_empty() => id,
            _ => self.platform.new_uuid(),
        };
        let key = (req.app_name, req.user_id, session_id);

        let mut inner = self.inner.write();
        if inner.sessions.contains_key(&key) {
            return Err(SessionError::InvalidRequest(format!(
                "session {} already exists",
                key.2
            )));
        }

        let Inner {
            sessions,
            app_state,
            user_state,
        } = &mut *inner;
        // Stores the raw request state: app: and user: keys go to the shared maps (and are
        // merged back on read), and every other key, temp: included, stays in session state.
        // Only appended deltas are trimmed.
        let mut state = StateMap::new();
        if let Some(initial) = &req.state {
            let (app, user, _) = extract_state_deltas(initial);
            app_state.entry(key.0.clone()).or_default().extend(app);
            user_state
                .entry((key.0.clone(), key.1.clone()))
                .or_default()
                .extend(user);
            state.extend(
                initial
                    .iter()
                    .filter(|(k, _)| {
                        !k.starts_with(KEY_PREFIX_APP) && !k.starts_with(KEY_PREFIX_USER)
                    })
                    .map(|(k, v)| (k.clone(), v.clone())),
            );
        }
        let updated_at = self.platform.now();
        let merged = merge_states(
            app_state.get(&key.0),
            user_state.get(&(key.0.clone(), key.1.clone())),
            &state,
        );
        sessions.insert(
            key.clone(),
            StoredSession {
                state,
                events: Vec::new(),
                updated_at,
            },
        );
        drop(inner);

        Ok(Arc::new(InMemorySession::new(
            key,
            merged,
            Vec::new(),
            updated_at,
        )))
    }

    async fn get(&self, req: GetRequest) -> Result<Arc<dyn Session>, SessionError> {
        require(&[
            ("app_name", &req.app_name),
            ("user_id", &req.user_id),
            ("session_id", &req.session_id),
        ])?;
        let key = (req.app_name, req.user_id, req.session_id);

        let inner = self.inner.read();
        let stored = inner.sessions.get(&key).ok_or(SessionError::NotFound)?;
        let state = inner.merged_state(&stored.state, &key.0, &key.1);

        // The recent-events limit applies first, then the timestamp filter.
        let mut events: &[Event] = &stored.events;
        if req.num_recent_events > 0 {
            let start = events.len().saturating_sub(req.num_recent_events);
            events = &events[start..];
        }
        // Test every event rather than searching for a cut point: events are kept in append
        // order, which need not be timestamp order.
        let after = req.after.filter(|t| *t != zero_time());
        let events: Vec<Event> = events
            .iter()
            .filter(|e| after.is_none_or(|after| e.timestamp >= after))
            .cloned()
            .collect();
        let updated_at = stored.updated_at;
        drop(inner);

        Ok(Arc::new(InMemorySession::new(
            key, state, events, updated_at,
        )))
    }

    async fn list(&self, req: ListRequest) -> Result<Vec<Arc<dyn Session>>, SessionError> {
        require(&[("app_name", &req.app_name)])?;

        let inner = self.inner.read();
        let mut sessions: Vec<Arc<dyn Session>> = inner
            .sessions
            .iter()
            .filter(|((app, user, _), _)| {
                *app == req.app_name && (req.user_id.is_empty() || *user == req.user_id)
            })
            .map(|(key, stored)| {
                let state = inner.merged_state(&stored.state, &key.0, &key.1);
                Arc::new(InMemorySession::new(
                    key.clone(),
                    state,
                    Vec::new(),
                    stored.updated_at,
                )) as Arc<dyn Session>
            })
            .collect();
        drop(inner);
        sessions.sort_by(|a, b| compare_by_last_update(a.as_ref(), b.as_ref()));
        Ok(sessions)
    }

    async fn delete(&self, req: DeleteRequest) -> Result<(), SessionError> {
        require(&[
            ("app_name", &req.app_name),
            ("user_id", &req.user_id),
            ("session_id", &req.session_id),
        ])?;
        let key = (req.app_name, req.user_id, req.session_id);
        self.inner.write().sessions.remove(&key);
        Ok(())
    }

    async fn append_event(
        &self,
        session: &dyn Session,
        event: &mut Event,
    ) -> Result<(), SessionError> {
        // Partial events are not stored.
        if event.response.partial {
            return Ok(());
        }
        // A backend that persists events could not read this request back, so this one
        // refuses it too.
        check_requested_auth_configs(&event.actions.requested_auth_configs)
            .map_err(|e| SessionError::Backend(format!("cannot append event: {e}").into()))?;
        // Give the event an identity if it arrived without one, the same way a missing
        // session ID is filled in on create.
        if event.id.is_empty() {
            event.id = self.platform.new_uuid();
        }

        let live = session
            .as_any()
            .downcast_ref::<InMemorySession>()
            .ok_or_else(|| {
                SessionError::InvalidRequest(
                    "session not created by InMemorySessionService".to_string(),
                )
            })?;

        let mut inner = self.inner.write();
        let Inner {
            sessions,
            app_state,
            user_state,
        } = &mut *inner;
        let stored = sessions.get_mut(&live.key).ok_or(SessionError::NotFound)?;

        let trimmed = trim_temp_delta_state(event);

        // Update the caller's session: the whole delta (temp: keys included) goes into its
        // state and the trimmed event is recorded.
        {
            let mut data = live.data.write();
            if let Some(delta) = &event.actions.state_delta {
                data.state
                    .extend(delta.iter().map(|(k, v)| (k.clone(), v.clone())));
            }
            data.events.push(trimmed.clone());
            data.updated_at = event.timestamp;
        }

        // Update the canonical record.
        stored.events.push(trimmed);
        stored.updated_at = event.timestamp;
        if let Some(delta) = event.actions.state_delta.as_ref().filter(|d| !d.is_empty()) {
            Inner::apply_delta(
                app_state,
                user_state,
                &mut stored.state,
                delta,
                &live.key.0,
                &live.key.1,
            );
        }
        Ok(())
    }
}

/// A session handed out by [`InMemorySessionService`]: a copy of the service's record.
struct InMemorySession {
    key: SessionKey,
    data: LiveState,
}

/// The mutable part of an [`InMemorySession`], behind one lock.
struct LiveState(RwLock<LiveData>);

struct LiveData {
    state: StateMap,
    events: Vec<Event>,
    updated_at: DateTime<Utc>,
}

impl LiveState {
    fn write(&self) -> parking_lot::RwLockWriteGuard<'_, LiveData> {
        self.0.write()
    }
}

impl InMemorySession {
    fn new(
        key: SessionKey,
        state: StateMap,
        events: Vec<Event>,
        updated_at: DateTime<Utc>,
    ) -> Self {
        Self {
            key,
            data: LiveState(RwLock::new(LiveData {
                state,
                events,
                updated_at,
            })),
        }
    }
}

impl Session for InMemorySession {
    fn id(&self) -> &str {
        &self.key.2
    }

    fn app_name(&self) -> &str {
        &self.key.0
    }

    fn user_id(&self) -> &str {
        &self.key.1
    }

    fn state(&self) -> &dyn State {
        &self.data
    }

    fn events(&self) -> Vec<Event> {
        self.data.0.read().events.clone()
    }

    fn last_update_time(&self) -> DateTime<Utc> {
        self.data.0.read().updated_at
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl State for LiveState {
    fn get(&self, key: &str) -> Result<Value, SessionError> {
        self.0
            .read()
            .state
            .get(key)
            .cloned()
            .ok_or(SessionError::StateKeyNotExist)
    }

    fn set(&self, key: &str, value: Value) -> Result<(), SessionError> {
        self.0.write().state.insert(key.to_string(), value);
        Ok(())
    }

    fn all(&self) -> Vec<(String, Value)> {
        // A snapshot taken under the lock, so iterating it never races a writer.
        self.0
            .read()
            .state
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    }
}