Skip to main content

steer_cli/commands/session/
delete.rs

1use async_trait::async_trait;
2use eyre::{Result, eyre};
3use std::io::{self, Write};
4
5use super::super::Command;
6use steer_core::api::Model;
7use steer_core::session::{SessionManager, SessionManagerConfig};
8use steer_core::utils::session::{create_session_store_with_config, resolve_session_store_config};
9
10pub struct DeleteSessionCommand {
11    pub session_id: String,
12    pub force: bool,
13    pub remote: Option<String>,
14    pub session_db: Option<std::path::PathBuf>,
15}
16
17#[async_trait]
18impl Command for DeleteSessionCommand {
19    async fn execute(&self) -> Result<()> {
20        // If remote is specified, handle via gRPC
21        if let Some(remote_addr) = &self.remote {
22            return self.handle_remote(remote_addr).await;
23        }
24
25        // Local session handling
26        if !self.force {
27            print!(
28                "Are you sure you want to delete session {}? (y/N): ",
29                self.session_id
30            );
31            io::stdout().flush()?;
32
33            let mut input = String::new();
34            io::stdin().read_line(&mut input)?;
35
36            if !input.trim().to_lowercase().starts_with('y') {
37                println!("Deletion cancelled.");
38                return Ok(());
39            }
40        }
41
42        let store_config = resolve_session_store_config(self.session_db.clone())?;
43        let session_store = create_session_store_with_config(store_config).await?;
44        let session_manager_config = SessionManagerConfig {
45            max_concurrent_sessions: 10,
46            default_model: Model::default(),
47            auto_persist: true,
48        };
49
50        let session_manager = SessionManager::new(session_store, session_manager_config);
51
52        let deleted = session_manager
53            .delete_session(&self.session_id)
54            .await
55            .map_err(|e| eyre!("Failed to delete session: {}", e))?;
56
57        if deleted {
58            println!("Session {} deleted.", self.session_id);
59        } else {
60            return Err(eyre!("Session not found: {}", self.session_id));
61        }
62
63        Ok(())
64    }
65}
66
67impl DeleteSessionCommand {
68    async fn handle_remote(&self, remote_addr: &str) -> Result<()> {
69        use steer_grpc::GrpcClientAdapter;
70
71        // Connect to the gRPC server
72        let client = GrpcClientAdapter::connect(remote_addr).await.map_err(|e| {
73            eyre!(
74                "Failed to connect to remote server at {}: {}",
75                remote_addr,
76                e
77            )
78        })?;
79
80        if !self.force {
81            print!(
82                "Are you sure you want to delete remote session {}? (y/N): ",
83                self.session_id
84            );
85            io::stdout().flush()?;
86
87            let mut input = String::new();
88            io::stdin().read_line(&mut input)?;
89
90            if !input.trim().to_lowercase().starts_with('y') {
91                println!("Deletion cancelled.");
92                return Ok(());
93            }
94        }
95
96        let deleted = client
97            .delete_session(&self.session_id)
98            .await
99            .map_err(|e| eyre!("Failed to delete remote session: {}", e))?;
100
101        if deleted {
102            println!("Remote session {} deleted.", self.session_id);
103        } else {
104            return Err(eyre!("Remote session not found: {}", self.session_id));
105        }
106
107        Ok(())
108    }
109}