use std::{future::Future, pin::Pin};
use crate::{
app::{
peer_sessions::{PeerBatch, PeerInbox},
pull_requests::{NewsBatch, PullRequestInbox},
subagent_manager::{SubagentManager, SubagentNotification},
},
tools::{
process::{ProcessManager, ProcessNotification},
ui::{SurfaceAction, SurfaceHost},
workflow_tracker::{WorkflowNotification, WorkflowRunTracker},
},
};
pub(crate) type Arrival = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;
#[derive(Clone)]
pub(crate) enum BoundarySource {
Subagents {
manager: Box<SubagentManager>,
session: String,
},
Workflows {
tracker: WorkflowRunTracker,
session: String,
},
Processes(ProcessManager),
Surfaces(SurfaceHost),
PullRequests(PullRequestInbox),
Peers(PeerInbox),
}
impl BoundarySource {
pub(crate) fn arrived(&self) -> Arrival {
match self {
Self::Subagents { manager, .. } => Box::pin(manager.parent_wake()),
Self::Workflows { tracker, .. } => Box::pin(tracker.notified_owned()),
Self::Processes(processes) => Box::pin(processes.notified_owned()),
Self::Surfaces(surfaces) => Box::pin(surfaces.acted_owned()),
Self::PullRequests(inbox) => Box::pin(inbox.news_owned()),
Self::Peers(inbox) => Box::pin(inbox.arrived_owned()),
}
}
pub(crate) fn reserve(&self) -> Reserved {
match self {
Self::Subagents { manager, session } => {
Reserved::Subagents(manager.clone(), manager.take_notifications(session))
}
Self::Workflows { tracker, session } => {
Reserved::Workflows(tracker.clone(), tracker.take_notifications(session))
}
Self::Processes(processes) => {
Reserved::Processes(processes.clone(), processes.take_notifications())
}
Self::Surfaces(surfaces) => {
Reserved::Surfaces(surfaces.clone(), surfaces.take_actions())
}
Self::PullRequests(inbox) => Reserved::PullRequests(inbox.clone(), inbox.take_news()),
Self::Peers(inbox) => Reserved::Peers(inbox.clone(), inbox.take()),
}
}
}
pub(crate) fn any_arrived(sources: impl IntoIterator<Item = Arrival>) -> Arrival {
let arrivals: Vec<Arrival> = sources.into_iter().collect();
if arrivals.is_empty() {
return Box::pin(std::future::pending());
}
Box::pin(async move {
futures_util::future::select_all(arrivals).await;
})
}
pub(crate) enum Reserved {
Subagents(Box<SubagentManager>, Vec<SubagentNotification>),
Workflows(WorkflowRunTracker, Vec<WorkflowNotification>),
Processes(ProcessManager, Vec<ProcessNotification>),
Surfaces(SurfaceHost, Vec<SurfaceAction>),
PullRequests(PullRequestInbox, NewsBatch),
Peers(PeerInbox, PeerBatch),
}
impl Reserved {
pub(crate) fn is_empty(&self) -> bool {
match self {
Self::Subagents(_, results) => results.is_empty(),
Self::Workflows(_, results) => results.is_empty(),
Self::Processes(_, results) => results.is_empty(),
Self::Surfaces(_, actions) => actions.is_empty(),
Self::PullRequests(_, news) => news.is_empty(),
Self::Peers(_, notices) => notices.is_empty(),
}
}
pub(crate) fn commit(&mut self) {
match self {
Self::Subagents(_, results) => results.clear(),
Self::Workflows(_, results) => results.clear(),
Self::Processes(_, results) => results.clear(),
Self::Surfaces(_, actions) => actions.clear(),
Self::PullRequests(inbox, news) => {
inbox.acknowledge(news);
*news = NewsBatch::default();
}
Self::Peers(_, notices) => *notices = PeerBatch::default(),
}
}
}
impl Drop for Reserved {
fn drop(&mut self) {
match self {
Self::Subagents(manager, results) => manager.restore_notifications(results),
Self::Workflows(tracker, results) => tracker.restore_notifications(results),
Self::Processes(processes, results) => processes.restore_notifications(results),
Self::Surfaces(surfaces, actions) => surfaces.restore_actions(actions),
Self::PullRequests(inbox, news) => inbox.restore_news(news),
Self::Peers(inbox, notices) => inbox.restore(notices),
}
}
}