codetether_agent/session/
listing.rs1use super::Session;
2use anyhow::Result;
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6use tokio::fs;
7
8#[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 #[serde(default)]
19 pub directory: Option<PathBuf>,
20}
21
22pub 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
57pub 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
75pub 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}