muster/application/
session_restore.rs1use std::{collections::HashSet, path::Path};
2
3use getset::Getters;
4use typed_builder::TypedBuilder;
5
6use crate::domain::{
7 agent_session::{AgentSession, AgentSessionId},
8 value::CommandLine,
9};
10
11#[derive(Getters, TypedBuilder)]
13#[getset(get = "pub")]
14pub struct SessionRestore {
15 session: AgentSession,
17 #[builder(default)]
19 command: Option<CommandLine>,
20}
21
22pub struct SessionRestorer;
24
25impl SessionRestorer {
26 pub fn for_project(
28 sessions: impl IntoIterator<Item = AgentSession>,
29 project: &Path,
30 existing: &HashSet<AgentSessionId>,
31 owns_project: impl Fn(&Path, &Path) -> bool,
32 owner_is_live: impl Fn(&AgentSession) -> bool,
33 ) -> Vec<SessionRestore> {
34 sessions
35 .into_iter()
36 .filter(|session| {
37 matches!(
38 session.state(),
39 crate::domain::agent_session::AgentSessionState::Pending
40 | crate::domain::agent_session::AgentSessionState::Open
41 ) && owns_project(session.project(), project)
42 && !existing.contains(session.id())
43 && !owner_is_live(session)
44 })
45 .map(|session| {
46 let command = session.restore_command();
47 SessionRestore::builder()
48 .session(session)
49 .command(command)
50 .build()
51 })
52 .collect()
53 }
54}