use super::*;
pub(super) const MAX_SESSIONS: usize = 50;
pub(super) const TITLE_LINES: usize = 400;
#[derive(Debug, PartialEq)]
pub struct SessionInfo {
pub id: String,
pub title: String,
pub modified: SystemTime,
}
pub fn sessions_dir(home: &Path, root: &Path) -> PathBuf {
let name: String = root
.to_string_lossy()
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect();
home.join(".claude").join("projects").join(name)
}
pub(super) fn transcript_line(line: &str) -> Vec<(Who, String)> {
let Ok(message) = serde_json::from_str::<Value>(line) else {
return Vec::new();
};
if message["isMeta"].as_bool() == Some(true) || message["isSidechain"].as_bool() == Some(true) {
return Vec::new();
}
let content = &message["message"]["content"];
let mut entries = Vec::new();
let mut said = |who: Who, text: &str| {
let text = text.trim();
if !text.is_empty() && !text.starts_with('<') {
entries.push((who, text.to_string()));
}
};
match message["type"].as_str() {
Some("user") => match content.as_str() {
Some(text) => said(Who::You, text),
None => {
for block in content.as_array().into_iter().flatten() {
if block["type"].as_str() == Some("text") {
said(Who::You, block["text"].as_str().unwrap_or_default());
}
}
}
},
Some("assistant") => {
for block in content.as_array().into_iter().flatten() {
match block["type"].as_str() {
Some("text") => said(Who::Claude, block["text"].as_str().unwrap_or_default()),
Some("tool_use") => said(Who::Note, &step(block)),
_ => {}
}
}
}
_ => {}
}
entries
}
pub fn list_sessions(dir: &Path) -> Vec<SessionInfo> {
let mut sessions: Vec<SessionInfo> = std::fs::read_dir(dir)
.into_iter()
.flatten()
.flatten()
.filter_map(|item| {
let path = item.path();
let id = path.file_stem()?.to_str()?.to_string();
if path.extension()?.to_str()? != "jsonl" {
return None;
}
let modified = item.metadata().ok()?.modified().ok()?;
let lines = BufReader::new(std::fs::File::open(&path).ok()?).lines();
let title = lines
.map_while(Result::ok)
.take(TITLE_LINES)
.flat_map(|line| transcript_line(&line))
.find(|(who, _)| *who == Who::You)?
.1;
let title = title.lines().next().unwrap_or_default().to_string();
Some(SessionInfo { id, title, modified })
})
.collect();
sessions.sort_by_key(|session| std::cmp::Reverse(session.modified));
sessions.truncate(MAX_SESSIONS);
sessions
}
pub fn load_session(dir: &Path, id: &str) -> Vec<(Who, String)> {
let Ok(file) = std::fs::File::open(dir.join(format!("{id}.jsonl"))) else {
return Vec::new();
};
BufReader::new(file)
.lines()
.map_while(Result::ok)
.flat_map(|line| transcript_line(&line))
.collect()
}
pub fn age(then: SystemTime, now: SystemTime) -> String {
let minutes = now.duration_since(then).map_or(0, |passed| passed.as_secs() / 60);
match minutes {
0 => "now".into(),
1..60 => format!("{minutes}m"),
60..1440 => format!("{}h", minutes / 60),
_ => format!("{}d", minutes / 1440),
}
}
pub fn own_commands(home: &Path, root: &Path) -> Vec<String> {
let mut names = Vec::new();
for base in [root.join(".claude"), home.join(".claude")] {
for (folder, wants_dir) in [("commands", false), ("skills", true)] {
for item in std::fs::read_dir(base.join(folder)).into_iter().flatten().flatten() {
let path = item.path();
let is_command = !wants_dir && path.extension().is_some_and(|ext| ext == "md");
let is_skill = wants_dir && path.is_dir();
if let (true, Some(name)) = (is_command || is_skill, path.file_stem()) {
names.push(name.to_string_lossy().into_owned());
}
}
}
}
names.sort();
names.dedup();
names
}