ag_session/project.rs
1use std::cmp::Reverse;
2use std::path::{Path, PathBuf};
3
4/// Persisted project metadata used for multi-project management.
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub struct Project {
7 /// Creation timestamp in Unix seconds.
8 pub created_at: i64,
9 /// Optional user-defined display name.
10 pub display_name: Option<String>,
11 /// Last observed branch for the project checkout.
12 pub git_branch: Option<String>,
13 /// Stable database identifier.
14 pub id: i64,
15 /// Whether the project is pinned ahead of other projects.
16 pub is_favorite: bool,
17 /// Most recent project-open timestamp in Unix seconds.
18 pub last_opened_at: Option<i64>,
19 /// Absolute path to the project checkout.
20 pub path: PathBuf,
21 /// Last metadata update timestamp in Unix seconds.
22 pub updated_at: i64,
23}
24
25impl Project {
26 /// Returns the preferred project label for UI rendering.
27 #[must_use]
28 pub fn display_label(&self) -> String {
29 if let Some(display_name) = self.display_name.as_deref()
30 && !display_name.trim().is_empty()
31 {
32 return display_name.to_string();
33 }
34
35 project_name_from_path(self.path.as_path())
36 }
37}
38
39/// Aggregated project snapshot for list and switcher views.
40#[derive(Clone, Debug, Eq, PartialEq)]
41pub struct ProjectListItem {
42 /// Number of sessions still in an active lifecycle state.
43 pub active_session_count: u32,
44 /// Total input tokens accumulated by sessions in this project.
45 pub input_tokens: u64,
46 /// Most recent session update timestamp for this project.
47 pub last_session_updated_at: Option<i64>,
48 /// Total output tokens accumulated by sessions in this project.
49 pub output_tokens: u64,
50 /// Persisted project metadata for display and selection.
51 pub project: Project,
52 /// Total number of sessions belonging to this project.
53 pub session_count: u32,
54}
55
56/// Returns indices into `project_items` ordered most-recently-opened first.
57///
58/// Projects that were never opened sort after all opened projects; ties are
59/// broken by display label so the switcher order stays deterministic. Callers
60/// cache the returned order alongside the rows it was derived from and rebuild
61/// it only when the rows change, keeping the sort off the render path.
62#[must_use]
63pub fn mru_project_order(project_items: &[ProjectListItem]) -> Vec<usize> {
64 let mut ordered_indices: Vec<usize> = (0..project_items.len()).collect();
65 ordered_indices.sort_by_cached_key(|&index| {
66 let project = &project_items[index].project;
67
68 (Reverse(project.last_opened_at), project.display_label())
69 });
70
71 ordered_indices
72}
73
74/// Returns the project rows named by `project_order`, skipping stale indices.
75#[must_use]
76pub fn ordered_project_items<'a>(
77 project_items: &'a [ProjectListItem],
78 project_order: &[usize],
79) -> Vec<&'a ProjectListItem> {
80 project_order
81 .iter()
82 .filter_map(|&index| project_items.get(index))
83 .collect()
84}
85
86/// Derives a readable project name from its filesystem path.
87#[must_use]
88pub fn project_name_from_path(path: &Path) -> String {
89 path.file_name()
90 .and_then(std::ffi::OsStr::to_str)
91 .unwrap_or_default()
92 .to_string()
93}
94
95#[cfg(test)]
96#[path = "project_test.rs"]
97mod tests;