From b965c63be05c726c996ece298d6f5f2514a19316 Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 17 Jun 2024 19:54:24 +0800 Subject: [PATCH] refactor: minor improvement (#611) --- README.md | 2 +- scripts/completions/aichat.fish | 2 +- scripts/completions/aichat.nu | 2 +- scripts/completions/aichat.ps1 | 2 +- scripts/completions/aichat.zsh | 2 +- src/cli.rs | 2 +- src/config/mod.rs | 16 +++++++++++----- src/config/session.rs | 8 +++++--- src/repl/mod.rs | 2 +- 9 files changed, 23 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 977d7d89..0d0adcff 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ Options: --light-theme Use light theme --dry-run Display the message without sending it --info Display information - --list-models List all available models + --list-models List all available chat models --list-roles List all available roles --list-sessions List all available sessions -h, --help Print help diff --git a/scripts/completions/aichat.fish b/scripts/completions/aichat.fish index d05b4e6e..8761b1dc 100644 --- a/scripts/completions/aichat.fish +++ b/scripts/completions/aichat.fish @@ -15,7 +15,7 @@ complete -c aichat -s S -l no-stream -d 'Turns off stream mode' complete -c aichat -l light-theme -d 'Use light theme' complete -c aichat -l dry-run -d 'Display the message without sending it' complete -c aichat -l info -d 'Display information' -complete -c aichat -l list-models -d 'List all available models' +complete -c aichat -l list-models -d 'List all available chat models' complete -c aichat -l list-roles -d 'List all roles' complete -c aichat -l list-sessions -d 'List all sessions' complete -c aichat -l list-bots -d 'List all bots' diff --git a/scripts/completions/aichat.nu b/scripts/completions/aichat.nu index ec4b0838..c09626b7 100644 --- a/scripts/completions/aichat.nu +++ b/scripts/completions/aichat.nu @@ -53,7 +53,7 @@ module completions { --light-theme # Use light theme --dry-run # Display the message without sending it --info # Display information - --list-models # List all available models + --list-models # List all available chat models --list-roles # List all roles --list-sessions # List all sessions --list-bots # List all bots diff --git a/scripts/completions/aichat.ps1 b/scripts/completions/aichat.ps1 index 093f53f3..cf6cdd49 100644 --- a/scripts/completions/aichat.ps1 +++ b/scripts/completions/aichat.ps1 @@ -47,7 +47,7 @@ Register-ArgumentCompleter -Native -CommandName 'aichat' -ScriptBlock { [CompletionResult]::new('--light-theme', '--light-theme', [CompletionResultType]::ParameterName, 'Use light theme') [CompletionResult]::new('--dry-run', '--dry-run', [CompletionResultType]::ParameterName, 'Display the message without sending it') [CompletionResult]::new('--info', '--info', [CompletionResultType]::ParameterName, 'Display information') - [CompletionResult]::new('--list-models', '--list-models', [CompletionResultType]::ParameterName, 'List all available models') + [CompletionResult]::new('--list-models', '--list-models', [CompletionResultType]::ParameterName, 'List all available chat models') [CompletionResult]::new('--list-roles', '--list-roles', [CompletionResultType]::ParameterName, 'List all roles') [CompletionResult]::new('--list-sessions', '--list-sessions', [CompletionResultType]::ParameterName, 'List all sessions') [CompletionResult]::new('--list-bots', '--list-bots', [CompletionResultType]::ParameterName, 'List all bots') diff --git a/scripts/completions/aichat.zsh b/scripts/completions/aichat.zsh index ae39481c..b57b5753 100644 --- a/scripts/completions/aichat.zsh +++ b/scripts/completions/aichat.zsh @@ -42,7 +42,7 @@ _aichat() { '--light-theme[Use light theme]' \ '--dry-run[Display the message without sending it]' \ '--info[Display information]' \ -'--list-models[List all available models]' \ +'--list-models[List all available chat models]' \ '--list-roles[List all roles]' \ '--list-sessions[List all sessions]' \ '--list-bots[List all bots]' \ diff --git a/src/cli.rs b/src/cli.rs index 651d23a6..e9e0323c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -54,7 +54,7 @@ pub struct Cli { /// Display information #[clap(long)] pub info: bool, - /// List all available models + /// List all available chat models #[clap(long)] pub list_models: bool, /// List all roles diff --git a/src/config/mod.rs b/src/config/mod.rs index cb510eff..41e449ad 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -583,7 +583,8 @@ impl Config { } pub fn use_prompt(&mut self, prompt: &str) -> Result<()> { - let role = Role::new(TEMP_ROLE_NAME, prompt); + let mut role = Role::new(TEMP_ROLE_NAME, prompt); + role.set_model(&self.model); self.use_role_obj(role) } @@ -705,9 +706,8 @@ impl Config { pub fn exit_session(&mut self) -> Result<()> { if let Some(mut session) = self.session.take() { - let is_repl = self.working_mode == WorkingMode::Repl; let sessions_dir = self.sessions_dir()?; - session.exit(&sessions_dir, is_repl)?; + session.exit(&sessions_dir, self.working_mode.is_repl())?; self.last_message = None; } Ok(()) @@ -723,7 +723,7 @@ impl Config { }; let session_path = self.session_file(&name)?; if let Some(session) = self.session.as_mut() { - session.save(&session_path)?; + session.save(&session_path, self.working_mode.is_repl())?; } Ok(()) } @@ -933,8 +933,8 @@ impl Config { } pub fn exit_bot(&mut self) -> Result<()> { + self.exit_session()?; if self.bot.take().is_some() { - self.exit_session()?; self.rag.take(); self.last_message = None; } @@ -1420,6 +1420,12 @@ pub enum WorkingMode { Serve, } +impl WorkingMode { + pub fn is_repl(&self) -> bool { + *self == WorkingMode::Repl + } +} + bitflags::bitflags! { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct StateFlags: u32 { diff --git a/src/config/session.rs b/src/config/session.rs index 2149ba5d..726249c7 100644 --- a/src/config/session.rs +++ b/src/config/session.rs @@ -312,12 +312,12 @@ impl Session { } } let session_path = session_dir.join(format!("{}.yaml", self.name())); - self.save(&session_path)?; + self.save(&session_path, is_repl)?; } Ok(()) } - pub fn save(&mut self, session_path: &Path) -> Result<()> { + pub fn save(&mut self, session_path: &Path, is_repl: bool) -> Result<()> { if let Some(sessions_dir) = session_path.parent() { if !sessions_dir.exists() { create_dir_all(sessions_dir).with_context(|| { @@ -338,7 +338,9 @@ impl Session { ) })?; - println!("✨ Saved session to '{}'", session_path.display()); + if is_repl { + println!("✨ Saved session to '{}'", session_path.display()); + } self.dirty = false; diff --git a/src/repl/mod.rs b/src/repl/mod.rs index ca718b5f..1214fb5f 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -350,7 +350,7 @@ Tips: use to autocomplete conversation starter text. ".copy" => { let config = self.config.read(); self.copy(config.last_reply()) - .with_context(|| "Failed to copy the last output")?; + .with_context(|| "Failed to copy the last response")?; } ".exit" => match args { Some("role") => {