Skip to main content

codetether_agent/session/
listing.rs

1use super::Session;
2use anyhow::Result;
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6use tokio::fs;
7
8/// Summary of a session for listing
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct SessionSummary {
11    pub id: String,
12    pub title: Option<String>,
13    pub created_at: DateTime<Utc>,
14    pub updated_at: DateTime<Utc>,
15    pub message_count: usize,
16    pub agent: String,
17    /// The working directory this session was created in
18    #[serde(default)]
19    pub directory: Option<PathBuf>,
20}
21
22/// List all sessions
23pub async fn list_sessions() -> Result<Vec<SessionSummary>> {
24    let sessions_dir = crate::config::Config::data_dir()
25        .map(|d| d.join("sessions"))
26        .ok_or_else(|| anyhow::anyhow!("Could not determine data directory"))?;
27
28    if !sessions_dir.exists() {
29        return Ok(Vec::new());
30    }
31
32    let mut summaries = Vec::new();
33    let mut entries = fs::read_dir(&sessions_dir).await?;
34
35    while let Some(entry) = entries.next_entry().await? {
36        let path = entry.path();
37        if path.extension().map(|e| e == "json").unwrap_or(false)
38            && let Ok(content) = fs::read_to_string(&path).await
39            && let Ok(session) = serde_json::from_str::<Session>(&content)
40        {
41            summaries.push(SessionSummary {
42                id: session.id,
43                title: session.title,
44                created_at: session.created_at,
45                updated_at: session.updated_at,
46                message_count: session.messages.len(),
47                agent: session.agent,
48                directory: session.metadata.directory,
49            });
50        }
51    }
52
53    summaries.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
54    Ok(summaries)
55}
56
57/// List sessions scoped to a specific directory (workspace)
58///
59/// Only returns sessions whose `metadata.directory` matches the given path.
60/// This prevents sessions from other workspaces "leaking" into the TUI.
61pub async fn list_sessions_for_directory(dir: &std::path::Path) -> Result<Vec<SessionSummary>> {
62    let all = list_sessions().await?;
63    let canonical = dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf());
64    Ok(all
65        .into_iter()
66        .filter(|s| {
67            s.directory
68                .as_ref()
69                .map(|d| d.canonicalize().unwrap_or_else(|_| d.clone()) == canonical)
70                .unwrap_or(false)
71        })
72        .collect())
73}
74
75/// List sessions for a directory with pagination.
76///
77/// - `limit`: Maximum number of sessions to return (default: 100)
78/// - `offset`: Number of sessions to skip (default: 0)
79pub async fn list_sessions_paged(
80    dir: &std::path::Path,
81    limit: usize,
82    offset: usize,
83) -> Result<Vec<SessionSummary>> {
84    let mut sessions = list_sessions_for_directory(dir).await?;
85    sessions.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
86    Ok(sessions.into_iter().skip(offset).take(limit).collect())
87}