summaryrefslogtreecommitdiffstats
path: root/src/repl
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-03-09 10:39:28 +0800
committerGitHub <noreply@github.com>2023-03-09 10:39:28 +0800
commita62e461e38482ade15c6826e656393d5f867488a (patch)
treebb8c0d76e6863e761f273059744bee722c0ab260 /src/repl
parenta7f2da156c0692121e35dbe20bdacc76baadd5c4 (diff)
downloadaichat-a62e461e38482ade15c6826e656393d5f867488a.tar.gz
feat: support conversation (#48)
Diffstat (limited to 'src/repl')
-rw-r--r--src/repl/handler.rs28
-rw-r--r--src/repl/init.rs8
-rw-r--r--src/repl/mod.rs14
3 files changed, 33 insertions, 17 deletions
diff --git a/src/repl/handler.rs b/src/repl/handler.rs
index 1b2eb20..979fc5e 100644
--- a/src/repl/handler.rs
+++ b/src/repl/handler.rs
@@ -16,7 +16,9 @@ pub enum ReplCmd {
UpdateConfig(String),
Prompt(String),
ClearRole,
- Info,
+ ViewInfo,
+ StartConversation,
+ EndConversatoin,
}
pub struct ReplCmdHandler {
@@ -61,10 +63,11 @@ impl ReplCmdHandler {
wg.wait();
let buffer = ret?;
self.config.lock().save_message(&input, &buffer)?;
+ self.config.lock().record_conversation(&input, &buffer)?;
*self.reply.borrow_mut() = buffer;
}
ReplCmd::SetRole(name) => {
- let output = self.config.lock().change_role(&name);
+ let output = self.config.lock().change_role(&name)?;
print_now!("{}\n\n", output.trim_end());
}
ReplCmd::ClearRole => {
@@ -72,21 +75,24 @@ impl ReplCmdHandler {
print_now!("\n");
}
ReplCmd::Prompt(prompt) => {
- self.config.lock().create_temp_role(&prompt);
+ self.config.lock().create_temp_role(&prompt)?;
print_now!("\n");
}
- ReplCmd::Info => {
+ ReplCmd::ViewInfo => {
let output = self.config.lock().info()?;
print_now!("{}\n\n", output.trim_end());
}
ReplCmd::UpdateConfig(input) => {
- let output = self.config.lock().update(&input)?;
- let output = output.trim();
- if output.is_empty() {
- print_now!("\n");
- } else {
- print_now!("{}\n\n", output);
- }
+ self.config.lock().update(&input)?;
+ print_now!("\n");
+ }
+ ReplCmd::StartConversation => {
+ self.config.lock().start_conversation()?;
+ print_now!("\n");
+ }
+ ReplCmd::EndConversatoin => {
+ self.config.lock().end_conversation();
+ print_now!("\n");
}
}
Ok(())
diff --git a/src/repl/init.rs b/src/repl/init.rs
index 998b9d0..a14265c 100644
--- a/src/repl/init.rs
+++ b/src/repl/init.rs
@@ -11,7 +11,6 @@ use reedline::{
use std::borrow::Cow;
const MENU_NAME: &str = "completion_menu";
-const DEFAULT_PROMPT_INDICATOR: &str = "〉";
const DEFAULT_MULTILINE_INDICATOR: &str = "::: ";
pub struct Repl {
@@ -140,7 +139,12 @@ impl Prompt for ReplPrompt {
}
fn render_prompt_indicator(&self, _prompt_mode: reedline::PromptEditMode) -> Cow<str> {
- Cow::Borrowed(DEFAULT_PROMPT_INDICATOR)
+ let config = self.0.lock();
+ if config.conversation.is_some() {
+ Cow::Borrowed("$")
+ } else {
+ Cow::Borrowed("〉")
+ }
}
fn render_prompt_multiline_indicator(&self) -> Cow<str> {
diff --git a/src/repl/mod.rs b/src/repl/mod.rs
index e9a0b51..9407bcf 100644
--- a/src/repl/mod.rs
+++ b/src/repl/mod.rs
@@ -15,12 +15,14 @@ use anyhow::{Context, Result};
use reedline::Signal;
use std::sync::Arc;
-pub const REPL_COMMANDS: [(&str, &str, bool); 10] = [
+pub const REPL_COMMANDS: [(&str, &str, bool); 12] = [
(".info", "Print the information", false),
(".set", "Modify the configuration temporarily", false),
(".prompt", "Add a GPT prompt", true),
(".role", "Select a role", false),
(".clear role", "Clear the currently selected role", false),
+ (".conversation", "Start a conversation.", false),
+ (".clear conversation", "End the conversation.", false),
(".history", "Print the history", false),
(".clear history", "Clear the history", false),
(".editor", "Enter editor mode for multiline input", true),
@@ -102,6 +104,7 @@ impl Repl {
print_now!("\n");
}
Some("role") => handler.handle(ReplCmd::ClearRole)?,
+ Some("conversation") => handler.handle(ReplCmd::EndConversatoin)?,
_ => dump_unknown_command(),
},
".history" => {
@@ -113,7 +116,7 @@ impl Repl {
None => print_now!("Usage: .role <name>\n\n"),
},
".info" => {
- handler.handle(ReplCmd::Info)?;
+ handler.handle(ReplCmd::ViewInfo)?;
}
".editor" => {
let mut text = args.unwrap_or_default().to_string();
@@ -140,6 +143,9 @@ impl Repl {
handler.handle(ReplCmd::Prompt(text))?;
}
}
+ ".conversation" => {
+ handler.handle(ReplCmd::StartConversation)?;
+ }
_ => dump_unknown_command(),
}
} else {
@@ -157,11 +163,11 @@ fn dump_unknown_command() {
fn dump_repl_help() {
let head = REPL_COMMANDS
.iter()
- .map(|(name, desc, _)| format!("{name:<15} {desc}"))
+ .map(|(name, desc, _)| format!("{name:<24} {desc}"))
.collect::<Vec<String>>()
.join("\n");
print_now!(
- "{}\n\nPress Ctrl+C to abort session, Ctrl+D to exit the REPL\n\n",
+ "{}\n\nPress Ctrl+C to abort conversation, Ctrl+D to exit the REPL\n\n",
head,
);
}