summaryrefslogtreecommitdiffstats
path: root/src/repl
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-10-28 21:39:17 +0800
committerGitHub <noreply@github.com>2023-10-28 21:39:17 +0800
commitbc44026ff8828f532e9eba138e2ba35f7c247271 (patch)
tree98c55ad31a543ff3cd56e297751d32ee9ca960a7 /src/repl
parent1575d441724a27854c8f9fe1510a47a3050ae28d (diff)
downloadaichat-bc44026ff8828f532e9eba138e2ba35f7c247271.tar.gz
feat: enhance session/conversation (#162)
* feat: enhance session/conversation * updates * updates * cut version v0.9.0-rc2 * add .session name completion
Diffstat (limited to 'src/repl')
-rw-r--r--src/repl/handler.rs29
-rw-r--r--src/repl/mod.rs13
-rw-r--r--src/repl/prompt.rs16
3 files changed, 31 insertions, 27 deletions
diff --git a/src/repl/handler.rs b/src/repl/handler.rs
index 1952ebe..fd157dc 100644
--- a/src/repl/handler.rs
+++ b/src/repl/handler.rs
@@ -21,8 +21,8 @@ pub enum ReplCmd {
Prompt(String),
ClearRole,
ViewInfo,
- StartConversation,
- EndConversatoin,
+ StartSession(Option<String>),
+ EndSession,
Copy,
ReadFile(String),
}
@@ -30,7 +30,6 @@ pub enum ReplCmd {
#[allow(clippy::module_name_repetitions)]
pub struct ReplCmdHandler {
config: SharedConfig,
- reply: RefCell<String>,
abort: SharedAbortSignal,
clipboard: std::result::Result<RefCell<Clipboard>, arboard::Error>,
}
@@ -38,11 +37,9 @@ pub struct ReplCmdHandler {
impl ReplCmdHandler {
#[allow(clippy::unnecessary_wraps)]
pub fn init(config: SharedConfig, abort: SharedAbortSignal) -> Result<Self> {
- let reply = RefCell::new(String::new());
let clipboard = Clipboard::new().map(RefCell::new);
Ok(Self {
config,
- reply,
abort,
clipboard,
})
@@ -52,7 +49,6 @@ impl ReplCmdHandler {
match cmd {
ReplCmd::Submit(input) => {
if input.is_empty() {
- self.reply.borrow_mut().clear();
return Ok(());
}
self.config.read().maybe_print_send_tokens(&input);
@@ -68,12 +64,10 @@ impl ReplCmdHandler {
);
wg.wait();
let buffer = ret?;
- self.config.read().save_message(&input, &buffer)?;
+ self.config.write().save_message(&input, &buffer)?;
if self.config.read().auto_copy {
let _ = self.copy(&buffer);
}
- self.config.write().save_conversation(&input, &buffer)?;
- *self.reply.borrow_mut() = buffer;
}
ReplCmd::SetModel(name) => {
self.config.write().set_model(&name)?;
@@ -99,16 +93,23 @@ impl ReplCmdHandler {
self.config.write().update(&input)?;
print_now!("\n");
}
- ReplCmd::StartConversation => {
- self.config.write().start_conversation()?;
+ ReplCmd::StartSession(name) => {
+ self.config.write().start_session(&name)?;
print_now!("\n");
}
- ReplCmd::EndConversatoin => {
- self.config.write().end_conversation();
+ ReplCmd::EndSession => {
+ self.config.write().end_session()?;
print_now!("\n");
}
ReplCmd::Copy => {
- self.copy(&self.reply.borrow())
+ let reply = self
+ .config
+ .read()
+ .last_message
+ .as_ref()
+ .map(|v| v.1.clone())
+ .unwrap_or_default();
+ self.copy(&reply)
.with_context(|| "Failed to copy the last output")?;
print_now!("\n");
}
diff --git a/src/repl/mod.rs b/src/repl/mod.rs
index f498ab4..797519b 100644
--- a/src/repl/mod.rs
+++ b/src/repl/mod.rs
@@ -26,10 +26,10 @@ pub const REPL_COMMANDS: [(&str, &str); 15] = [
(".prompt", "Add a GPT prompt"),
(".role", "Select a role"),
(".clear role", "Clear the currently selected role"),
- (".conversation", "Start a conversation."),
- (".clear conversation", "End current conversation."),
+ (".session", "Start a session"),
+ (".clear session", "End current session"),
(".copy", "Copy the last output to the clipboard"),
- (".read", "Read the contents of a file into the prompt"),
+ (".read", "Read the contents of a file and submit"),
(".edit", "Multi-line editing (CTRL+S to finish)"),
(".history", "Print the history"),
(".clear history", "Clear the history"),
@@ -89,6 +89,7 @@ impl Repl {
_ => {}
}
}
+ handler.handle(ReplCmd::EndSession)?;
Ok(())
}
@@ -111,7 +112,7 @@ impl Repl {
print_now!("\n");
}
Some("role") => handler.handle(ReplCmd::ClearRole)?,
- Some("conversation") => handler.handle(ReplCmd::EndConversatoin)?,
+ Some("session") => handler.handle(ReplCmd::EndSession)?,
_ => dump_unknown_command(),
},
".history" => {
@@ -141,8 +142,8 @@ impl Repl {
handler.handle(ReplCmd::Prompt(text))?;
}
}
- ".conversation" => {
- handler.handle(ReplCmd::StartConversation)?;
+ ".session" => {
+ handler.handle(ReplCmd::StartSession(args.map(|v| v.to_string())))?;
}
".copy" => {
handler.handle(ReplCmd::Copy)?;
diff --git a/src/repl/prompt.rs b/src/repl/prompt.rs
index 1d92407..be3763b 100644
--- a/src/repl/prompt.rs
+++ b/src/repl/prompt.rs
@@ -69,15 +69,17 @@ impl ReplPrompt {
impl Prompt for ReplPrompt {
fn render_prompt_left(&self) -> Cow<str> {
- self.config
- .read()
- .role
- .as_ref()
- .map_or(Cow::Borrowed(""), |role| Cow::Owned(role.name.clone()))
+ if let Some(session) = &self.config.read().session {
+ Cow::Owned(session.name.clone())
+ } else if let Some(role) = &self.config.read().role {
+ Cow::Owned(role.name.clone())
+ } else {
+ Cow::Borrowed("")
+ }
}
fn render_prompt_right(&self) -> Cow<str> {
- if self.config.read().conversation.is_none() {
+ if self.config.read().session.is_none() {
Cow::Borrowed("")
} else {
self.config.read().get_reamind_tokens().to_string().into()
@@ -85,7 +87,7 @@ impl Prompt for ReplPrompt {
}
fn render_prompt_indicator(&self, _prompt_mode: reedline::PromptEditMode) -> Cow<str> {
- if self.config.read().conversation.is_some() {
+ if self.config.read().session.is_some() {
Cow::Borrowed(")")
} else {
Cow::Borrowed("〉")