From 1a8b4f1fffe5f7168b079a7d962f2e0d0f4f3dee Mon Sep 17 00:00:00 2001 From: leach Date: Sun, 24 Aug 2025 23:01:35 -0400 Subject: [PATCH] i forget --- src/cli.rs | 15 +++++++++++++++ src/config.rs | 12 ++++++++++++ src/main.rs | 13 ++++++++----- src/utils/input.rs | 10 ++++++++-- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 615400d..8c45153 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -372,6 +372,21 @@ impl ChatCLI { } } } + SessionAction::SetAsDefault(session_name) => { + let mut config = crate::config::Config::load().unwrap_or_default(); + match config.set_default_session(session_name.clone()) { + Ok(()) => { + self.display.print_command_result(&format!( + "Session '{}' is now the default session", + session_name + )); + } + Err(e) => { + self.display.print_error(&format!("Failed to set default session: {}", e)); + } + } + // Continue to show session list + } SessionAction::Cancel => { return Ok(()); } diff --git a/src/config.rs b/src/config.rs index 3fb11ff..6a27a30 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,6 +4,10 @@ use std::env; use std::fs; use std::path::PathBuf; +fn default_session_name() -> String { + "default".to_string() +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { pub api: ApiConfig, @@ -27,6 +31,8 @@ pub struct DefaultsConfig { pub reasoning_effort: String, pub enable_web_search: bool, pub enable_reasoning_summary: bool, + #[serde(default = "default_session_name")] + pub default_session: String, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -72,6 +78,7 @@ impl Default for DefaultsConfig { reasoning_effort: "medium".to_string(), enable_web_search: true, enable_reasoning_summary: false, + default_session: "default".to_string(), } } } @@ -232,4 +239,9 @@ impl Config { println!(" Request timeout: {}s", self.api.request_timeout_seconds); println!(" Max conversation history: {}", self.limits.max_conversation_history); } + + pub fn set_default_session(&mut self, session_name: String) -> Result<()> { + self.defaults.default_session = session_name; + self.save() + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 46b4f64..25720a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,8 @@ use crate::utils::Display; #[command(about = "A lightweight command-line interface for chatting with AI models")] #[command(version)] struct Args { - #[arg(short, long, default_value = "default", help = "Session name")] - session: String, + #[arg(short, long, help = "Session name (defaults to configured default session)")] + session: Option, #[arg(short, long, help = "Model name to use (overrides saved value)")] model: Option, @@ -44,7 +44,10 @@ async fn main() -> Result<()> { let env_vars = Config::validate_env_variables().context("Environment validation failed")?; // Load or create session - let session = match Session::load(&args.session) { + // Use configured default session if none specified + let session_name = args.session.unwrap_or_else(|| config.defaults.default_session.clone()); + + let session = match Session::load(&session_name) { Ok(mut session) => { if let Some(model) = args.model { if !is_model_supported(&model) { @@ -69,9 +72,9 @@ async fn main() -> Result<()> { "Model '{}' is not supported. Falling back to '{}'", default_model, config.defaults.model )); - Session::new(args.session, config.defaults.model.clone()) + Session::new(session_name.clone(), config.defaults.model.clone()) } else { - Session::new(args.session, default_model) + Session::new(session_name, default_model) } } }; diff --git a/src/utils/input.rs b/src/utils/input.rs index eebbf3b..6dab38f 100644 --- a/src/utils/input.rs +++ b/src/utils/input.rs @@ -184,7 +184,7 @@ impl InputHandler { // If it's the current session, show options if Some(selected_session.to_string().as_str()) == current_session { - let options = vec!["Delete this session", "Cancel"]; + let options = vec!["Delete this session", "Set as default session", "Cancel"]; let action_result = match Select::with_theme(&theme) .with_prompt("This is your current session. What would you like to do?") .items(&options) @@ -197,11 +197,14 @@ impl InputHandler { match action_result { Some(0) => { - if self.confirm(&format!("Delete current session '{}'? You will need to create or switch to another session after deletion.", selected_session.to_string()))? { + if self.confirm(&format!("Delete current session '{}'? You will need to create or switch to another sessions after deletion.", selected_session.to_string()))? { return Ok(SessionAction::Delete(selected_session)); } return Ok(SessionAction::Cancel); } + Some(1) => { + return Ok(SessionAction::SetAsDefault(selected_session)); + } _ => return Ok(SessionAction::Cancel), } } else { @@ -209,6 +212,7 @@ impl InputHandler { let options = vec![ format!("Switch to '{}'", selected_session.to_string()), format!("Delete '{}'", selected_session.to_string()), + format!("Set '{}' as default session", selected_session.to_string()), "Cancel".to_string() ]; @@ -230,6 +234,7 @@ impl InputHandler { } return Ok(SessionAction::Cancel); } + Some(2) => return Ok(SessionAction::SetAsDefault(selected_session)), _ => return Ok(SessionAction::Cancel), } } @@ -243,6 +248,7 @@ impl InputHandler { pub enum SessionAction { Switch(T), Delete(T), + SetAsDefault(T), Cancel, }