agent_base/types/
session.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
4pub struct SessionId {
5 pub id: u64,
6 pub external_id: Option<String>,
7}
8
9impl SessionId {
10 pub fn new(id: u64) -> Self {
11 Self {
12 id,
13 external_id: None,
14 }
15 }
16
17 pub fn with_external_id(id: u64, external_id: impl Into<String>) -> Self {
18 Self {
19 id,
20 external_id: Some(external_id.into()),
21 }
22 }
23}
24
25impl std::fmt::Display for SessionId {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 if let Some(ref ext) = self.external_id {
28 write!(f, "{}({})", self.id, ext)
29 } else {
30 write!(f, "{}", self.id)
31 }
32 }
33}
34
35pub trait SessionIdGenerator: Send + Sync {
36 fn generate(&self) -> SessionId;
37}
38
39pub struct AtomicU64SessionIdGenerator {
40 counter: std::sync::atomic::AtomicU64,
41}
42
43impl AtomicU64SessionIdGenerator {
44 pub fn new(start: u64) -> Self {
45 Self {
46 counter: std::sync::atomic::AtomicU64::new(start),
47 }
48 }
49}
50
51impl Default for AtomicU64SessionIdGenerator {
52 fn default() -> Self {
53 Self::new(1)
54 }
55}
56
57impl SessionIdGenerator for AtomicU64SessionIdGenerator {
58 fn generate(&self) -> SessionId {
59 SessionId::new(
60 self.counter
61 .fetch_add(1, std::sync::atomic::Ordering::Relaxed),
62 )
63 }
64}
65
66pub struct UuidSessionIdGenerator;
67
68impl SessionIdGenerator for UuidSessionIdGenerator {
69 fn generate(&self) -> SessionId {
70 SessionId::with_external_id(0, uuid::Uuid::new_v4().to_string())
71 }
72}