diff options
| author | sigoden <sigoden@gmail.com> | 2023-03-10 07:13:22 +0800 |
|---|---|---|
| committer | sigoden <sigoden@gmail.com> | 2023-03-10 07:13:22 +0800 |
| commit | 899c4af9ea4dedd295a909f6ddc968b20157a505 (patch) | |
| tree | e58cf09678b12c99d87778420ebb3b591d9ee89c | |
| parent | ff00426c2c9d7d16de9221644e4f276f7e24d74a (diff) | |
| download | aichat-899c4af9ea4dedd295a909f6ddc968b20157a505.tar.gz | |
fix: forbid .clear role in middle of conversation
| -rw-r--r-- | src/config/conversation.rs | 7 | ||||
| -rw-r--r-- | src/config/mod.rs | 8 | ||||
| -rw-r--r-- | src/repl/handler.rs | 2 |
3 files changed, 16 insertions, 1 deletions
diff --git a/src/config/conversation.rs b/src/config/conversation.rs index 7fe843d..93382a3 100644 --- a/src/config/conversation.rs +++ b/src/config/conversation.rs @@ -33,6 +33,13 @@ impl Conversation { Ok(()) } + pub fn can_clear_role(&self) -> Result<()> { + if self.messages.is_empty() { + return Ok(()); + } + bail!("Error: Cannot perform this action in the middle of conversation") + } + pub fn update_tokens(&mut self) { self.tokens = num_tokens_from_messages(&self.build_emssages("")); } diff --git a/src/config/mod.rs b/src/config/mod.rs index ebbd909..ff7acae 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -186,6 +186,14 @@ impl Config { } } + pub fn clear_role(&mut self) -> Result<()> { + if let Some(conversation) = self.conversation.as_ref() { + conversation.can_clear_role()?; + } + self.role = None; + Ok(()) + } + pub fn create_temp_role(&mut self, prompt: &str) -> Result<()> { let role = Role::new(prompt, self.temperature); if let Some(conversation) = self.conversation.as_mut() { diff --git a/src/repl/handler.rs b/src/repl/handler.rs index 30a75c5..a0b1e5c 100644 --- a/src/repl/handler.rs +++ b/src/repl/handler.rs @@ -71,7 +71,7 @@ impl ReplCmdHandler { print_now!("{}\n\n", output.trim_end()); } ReplCmd::ClearRole => { - self.config.lock().role = None; + self.config.lock().clear_role()?; print_now!("\n"); } ReplCmd::Prompt(prompt) => { |
