agent_base/types/
session.rs1pub use agent_types::SessionId;
2
3pub trait SessionIdGenerator: Send + Sync {
4 fn generate(&self) -> SessionId;
5}
6
7pub struct AtomicU64SessionIdGenerator {
8 counter: std::sync::atomic::AtomicU64,
9}
10
11impl AtomicU64SessionIdGenerator {
12 pub fn new(start: u64) -> Self {
13 Self {
14 counter: std::sync::atomic::AtomicU64::new(start),
15 }
16 }
17}
18
19impl Default for AtomicU64SessionIdGenerator {
20 fn default() -> Self {
21 Self::new(1)
22 }
23}
24
25impl SessionIdGenerator for AtomicU64SessionIdGenerator {
26 fn generate(&self) -> SessionId {
27 SessionId::new(
28 self.counter
29 .fetch_add(1, std::sync::atomic::Ordering::Relaxed),
30 )
31 }
32}
33
34pub struct UuidSessionIdGenerator;
35
36impl SessionIdGenerator for UuidSessionIdGenerator {
37 fn generate(&self) -> SessionId {
38 SessionId::with_external_id(0, uuid::Uuid::new_v4().to_string())
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn session_id_display_with_and_without_external_id() {
48 assert_eq!(SessionId::new(42).to_string(), "42");
49 assert_eq!(
50 SessionId::with_external_id(42, "ext").to_string(),
51 "42(ext)"
52 );
53 }
54
55 #[test]
56 fn atomic_generator_increments() {
57 let g = AtomicU64SessionIdGenerator::new(10);
58 assert_eq!(g.generate().id, 10);
59 assert_eq!(g.generate().id, 11);
60
61 let g = AtomicU64SessionIdGenerator::default();
62 assert_eq!(g.generate().id, 1);
63 }
64
65 #[test]
66 fn uuid_generator_sets_external_id() {
67 let sid = UuidSessionIdGenerator.generate();
68 assert_eq!(sid.id, 0);
69 assert!(sid.external_id.is_some());
70 }
71}