use std::{collections::HashSet, path::Path};
use getset::Getters;
use typed_builder::TypedBuilder;
use crate::domain::{
agent_session::{AgentSession, AgentSessionId},
value::CommandLine,
};
#[derive(Getters, TypedBuilder)]
#[getset(get = "pub")]
pub struct SessionRestore {
session: AgentSession,
#[builder(default)]
command: Option<CommandLine>,
}
pub struct SessionRestorer;
impl SessionRestorer {
pub fn for_project(
sessions: impl IntoIterator<Item = AgentSession>,
project: &Path,
existing: &HashSet<AgentSessionId>,
owns_project: impl Fn(&Path, &Path) -> bool,
owner_is_live: impl Fn(&AgentSession) -> bool,
) -> Vec<SessionRestore> {
sessions
.into_iter()
.filter(|session| {
matches!(
session.state(),
crate::domain::agent_session::AgentSessionState::Pending
| crate::domain::agent_session::AgentSessionState::Open
) && owns_project(session.project(), project)
&& !existing.contains(session.id())
&& !owner_is_live(session)
})
.map(|session| {
let command = session.restore_command();
SessionRestore::builder()
.session(session)
.command(command)
.build()
})
.collect()
}
}