diff --git a/bin/pudoxd/src/session.rs b/bin/pudoxd/src/session.rs index 8c09c7b..7626a18 100644 --- a/bin/pudoxd/src/session.rs +++ b/bin/pudoxd/src/session.rs @@ -18,16 +18,35 @@ enum SessionStage { Running(RunningState), } +impl SessionStage { + async fn run( + self, + rx: &mut mpsc::Receiver, + session_id: &str, + ) -> Option { + match self { + SessionStage::Embryo(s) => s.run(rx, session_id).await, + SessionStage::Running(s) => s.run(rx, session_id).await, + } + } +} + struct EmbryoState; impl EmbryoState { - async fn handle(self, _msg: SessionMessage, session_id: &str) -> SessionStage { + async fn run( + self, + rx: &mut mpsc::Receiver, + session_id: &str, + ) -> Option { + let _msg = rx.recv().await?; + tracing::info!("Processing event for session {}", session_id); - let _repository = "user/repo"; + let repository = "user/repo"; tracing::info!( "Resolved repository {} for session {}", - _repository, + repository, session_id ); @@ -35,7 +54,7 @@ impl EmbryoState { sandbox.prepare(session_id).await; tracing::info!("Transitioning to Running for session {}", session_id); - SessionStage::Running(RunningState { sandbox }) + Some(SessionStage::Running(RunningState { sandbox })) } } @@ -44,11 +63,19 @@ struct RunningState { } impl RunningState { - async fn handle(self, _msg: SessionMessage, session_id: &str) -> SessionStage { - tracing::info!("Agent active for session {}", session_id); + async fn run( + self, + rx: &mut mpsc::Receiver, + session_id: &str, + ) -> Option { self.sandbox.spawn_agent(session_id).await; - std::future::pending::<()>().await; - SessionStage::Running(self) + + while let Some(_msg) = rx.recv().await { + tracing::info!("Running: forwarding message for session {}", session_id); + // TODO: forward to pudox-run + } + + None } } @@ -57,13 +84,9 @@ pub async fn run_session( mut rx: mpsc::Receiver, session_id: String, ) { - let mut stage = SessionStage::Embryo(EmbryoState); - - while let Some(msg) = rx.recv().await { - stage = match stage { - SessionStage::Embryo(s) => s.handle(msg, &session_id).await, - SessionStage::Running(s) => s.handle(msg, &session_id).await, - }; + let mut stage: Option = Some(SessionStage::Embryo(EmbryoState)); + while let Some(s) = stage { + stage = s.run(&mut rx, &session_id).await; } sessions.write().await.remove(&session_id);