Skip to main content

ag_session/
project.rs

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