1use stakpak_agent_core::AgentCommand;
2use tokio::sync::mpsc;
3use tokio_util::sync::CancellationToken;
4use uuid::Uuid;
5
6#[derive(Clone)]
7pub struct SessionHandle {
8 pub command_tx: mpsc::Sender<AgentCommand>,
9 pub cancel: CancellationToken,
10}
11
12impl std::fmt::Debug for SessionHandle {
13 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14 f.debug_struct("SessionHandle").finish_non_exhaustive()
15 }
16}
17
18impl SessionHandle {
19 pub fn new(command_tx: mpsc::Sender<AgentCommand>, cancel: CancellationToken) -> Self {
20 Self { command_tx, cancel }
21 }
22}
23
24#[derive(Debug, Clone, Default)]
25pub enum SessionRuntimeState {
26 #[default]
27 Idle,
28 Starting {
29 run_id: Uuid,
30 },
31 Running {
32 run_id: Uuid,
33 handle: SessionHandle,
34 },
35 Failed {
36 last_error: String,
37 },
38}
39
40impl SessionRuntimeState {
41 pub fn run_id(&self) -> Option<Uuid> {
42 match self {
43 SessionRuntimeState::Starting { run_id }
44 | SessionRuntimeState::Running { run_id, .. } => Some(*run_id),
45 SessionRuntimeState::Idle | SessionRuntimeState::Failed { .. } => None,
46 }
47 }
48
49 pub fn is_active(&self) -> bool {
50 matches!(
51 self,
52 SessionRuntimeState::Starting { .. } | SessionRuntimeState::Running { .. }
53 )
54 }
55}