Skip to main content

lago_core/
session.rs

1use crate::id::{BranchId, SessionId};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5/// Represents an agent session — the top-level unit of work.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Session {
8    pub session_id: SessionId,
9    pub config: SessionConfig,
10    pub created_at: u64,
11    pub branches: Vec<BranchId>,
12}
13
14/// Configuration for a session.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct SessionConfig {
17    pub name: String,
18    #[serde(default)]
19    pub model: String,
20    #[serde(default)]
21    pub params: HashMap<String, String>,
22}
23
24impl SessionConfig {
25    pub fn new(name: impl Into<String>) -> Self {
26        Self {
27            name: name.into(),
28            model: String::new(),
29            params: HashMap::new(),
30        }
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn session_config_new() {
40        let config = SessionConfig::new("test-session");
41        assert_eq!(config.name, "test-session");
42        assert_eq!(config.model, "");
43        assert!(config.params.is_empty());
44    }
45
46    #[test]
47    fn session_serde_roundtrip() {
48        let session = Session {
49            session_id: SessionId::from_string("SESS001"),
50            config: SessionConfig {
51                name: "my-session".to_string(),
52                model: "gpt-4".to_string(),
53                params: HashMap::from([("temp".to_string(), "0.7".to_string())]),
54            },
55            created_at: 1700000000,
56            branches: vec![BranchId::from_string("main"), BranchId::from_string("dev")],
57        };
58        let json = serde_json::to_string(&session).unwrap();
59        let back: Session = serde_json::from_str(&json).unwrap();
60        assert_eq!(back.session_id.as_str(), "SESS001");
61        assert_eq!(back.config.name, "my-session");
62        assert_eq!(back.config.model, "gpt-4");
63        assert_eq!(back.config.params["temp"], "0.7");
64        assert_eq!(back.branches.len(), 2);
65        assert_eq!(back.created_at, 1700000000);
66    }
67}