From c12ae0275108acbbb124a41ff936b441723ae625 Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 6 Mar 2023 16:35:51 +0800 Subject: [PATCH] chore: improve code by renameing some function --- src/client.rs | 19 +++++++++++-------- src/main.rs | 2 +- src/repl/handler.rs | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/client.rs b/src/client.rs index 3f74aa6..589ad1a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -29,12 +29,15 @@ impl ChatGptClient { Ok(s) } - pub fn acquire(&self, input: &str, prompt: Option) -> Result { - self.runtime - .block_on(async { self.acquire_inner(input, prompt).await }) + pub fn send_message(&self, input: &str, prompt: Option) -> Result { + self.runtime.block_on(async { + self.send_message_inner(input, prompt) + .await + .with_context(|| "Failed to send message") + }) } - pub fn acquire_stream( + pub fn send_message_streaming( &self, input: &str, prompt: Option, @@ -51,9 +54,9 @@ impl ChatGptClient { let ctrlc = handler.get_ctrlc(); self.runtime.block_on(async { tokio::select! { - ret = self.acquire_stream_inner(input, prompt, handler) => { + ret = self.send_message_streaming_inner(input, prompt, handler) => { handler.done(); - ret + ret.with_context(|| "Failed to send message streaming") } _ = watch_ctrlc(ctrlc.clone()) => { handler.done(); @@ -67,7 +70,7 @@ impl ChatGptClient { }) } - async fn acquire_inner(&self, content: &str, prompt: Option) -> Result { + async fn send_message_inner(&self, content: &str, prompt: Option) -> Result { if self.config.borrow().dry_run { return Ok(combine(content, prompt)); } @@ -82,7 +85,7 @@ impl ChatGptClient { Ok(output.to_string()) } - async fn acquire_stream_inner( + async fn send_message_streaming_inner( &self, content: &str, prompt: Option, diff --git a/src/main.rs b/src/main.rs index 884a56f..5cb94f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,7 +65,7 @@ fn main() -> Result<()> { fn start_directive(client: ChatGptClient, config: SharedConfig, input: &str) -> Result<()> { let mut file = config.borrow().open_message_file()?; let prompt = config.borrow().get_prompt(); - let output = client.acquire(input, prompt)?; + let output = client.send_message(input, prompt)?; let output = output.trim(); if config.borrow().highlight && stdout().is_terminal() { let markdown_render = MarkdownRender::init()?; diff --git a/src/repl/handler.rs b/src/repl/handler.rs index 21c49a8..40ce101 100644 --- a/src/repl/handler.rs +++ b/src/repl/handler.rs @@ -76,7 +76,7 @@ impl ReplCmdHandler { ReplyStreamHandler::new(None, self.ctrlc.clone()) }; self.client - .acquire_stream(&input, prompt, &mut stream_handler)?; + .send_message_streaming(&input, prompt, &mut stream_handler)?; let buffer = stream_handler.get_buffer(); self.config.borrow().save_message( self.state.borrow_mut().save_file.as_mut(),