BlueGit

ict rusty-teams

Files

rusty-teams / crates / agents / src / desktop_sessions.rs

desktop_sessions.rs

desktop_sessions.rs 4.4 KiB Download Raw
1use std::path::Path;
2
3use serde::Deserialize;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub struct DesktopSession {
7 pub session_id: String,
8 pub working_dir: String,
9 pub title: String,
10 pub last_activity_millis: i64,
11}
12
13#[derive(Deserialize)]
14struct DesktopConfig {
15 #[serde(rename = "lastKnownAccountUuid")]
16 account_uuid: Option<String>,
17}
18
19#[derive(Deserialize)]
20struct DesktopSessionFile {
21 #[serde(rename = "cliSessionId")]
22 cli_session_id: Option<String>,
23 cwd: Option<String>,
24 title: Option<String>,
25 #[serde(rename = "isArchived", default)]
26 archived: bool,
27 #[serde(rename = "lastActivityAt", default)]
28 last_activity_millis: i64,
29}
30
31fn is_plain_account_uuid(text: &str) -> bool {
32 !text.is_empty()
33 && text
34 .chars()
35 .all(|character| character.is_ascii_alphanumeric() || character == '-')
36}
37
38pub fn desktop_sessions(desktop_dir: &Path) -> Vec<DesktopSession> {
39 let Some(account_uuid) = std::fs::read_to_string(desktop_dir.join("config.json"))
40 .ok()
41 .and_then(|text| serde_json::from_str::<DesktopConfig>(&text).ok())
42 .and_then(|config| config.account_uuid)
43 .filter(|uuid| is_plain_account_uuid(uuid))
44 else {
45 return Vec::new();
46 };
47 let Ok(organizations) =
48 std::fs::read_dir(desktop_dir.join("claude-code-sessions").join(account_uuid))
49 else {
50 return Vec::new();
51 };
52 organizations
53 .flatten()
54 .filter_map(|organization| std::fs::read_dir(organization.path()).ok())
55 .flatten()
56 .flatten()
57 .filter(|entry| {
58 let name = entry.file_name();
59 let name = name.to_string_lossy();
60 name.starts_with("local_") && name.ends_with(".json")
61 })
62 .filter_map(|entry| {
63 let file: DesktopSessionFile =
64 serde_json::from_str(&std::fs::read_to_string(entry.path()).ok()?).ok()?;
65 if file.archived {
66 return None;
67 }
68 Some(DesktopSession {
69 session_id: file.cli_session_id.filter(|id| !id.is_empty())?,
70 working_dir: file.cwd.unwrap_or_default(),
71 title: file.title.unwrap_or_default(),
72 last_activity_millis: file.last_activity_millis,
73 })
74 })
75 .collect()
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 fn write(path: &Path, body: &str) {
83 std::fs::create_dir_all(path.parent().unwrap()).unwrap();
84 std::fs::write(path, body).unwrap();
85 }
86
87 #[test]
88 fn only_active_sessions_of_the_signed_in_account_with_a_cli_id_are_listed() {
89 let root = tempfile::tempdir().unwrap();
90 write(
91 &root.path().join("config.json"),
92 r#"{"lastKnownAccountUuid":"acc-1","oauth:tokenCache":"secret"}"#,
93 );
94 let sessions = root.path().join("claude-code-sessions");
95 write(
96 &sessions.join("acc-1/org-1/local_a.json"),
97 r#"{"cliSessionId":"cli-a","cwd":"/srv/app","title":"Fix build","isArchived":false,"lastActivityAt":1700000000000,"wslConfig":{"distro":"Ubuntu"}}"#,
98 );
99 write(
100 &sessions.join("acc-1/org-2/local_b.json"),
101 r#"{"cliSessionId":"cli-b","cwd":"/srv/b","title":"Archived","isArchived":true,"lastActivityAt":1}"#,
102 );
103 write(
104 &sessions.join("acc-1/org-1/local_c.json"),
105 r#"{"cwd":"/srv/c","title":"No cli id","isArchived":false}"#,
106 );
107 write(
108 &sessions.join("acc-2/org-1/local_d.json"),
109 r#"{"cliSessionId":"cli-d","cwd":"/srv/d","title":"Other account","isArchived":false}"#,
110 );
111 write(&sessions.join("acc-1/org-1/notes.json"), "{}");
112 assert_eq!(
113 desktop_sessions(root.path()),
114 [DesktopSession {
115 session_id: "cli-a".to_owned(),
116 working_dir: "/srv/app".to_owned(),
117 title: "Fix build".to_owned(),
118 last_activity_millis: 1_700_000_000_000,
119 }]
120 );
121 }
122
123 #[test]
124 fn a_missing_config_or_an_unsafe_account_id_lists_nothing() {
125 let root = tempfile::tempdir().unwrap();
126 assert!(desktop_sessions(root.path()).is_empty());
127 write(
128 &root.path().join("config.json"),
129 r#"{"lastKnownAccountUuid":"../x"}"#,
130 );
131 assert!(desktop_sessions(root.path()).is_empty());
132 }
133}