summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-11-07 19:14:58 +0800
committerGitHub <noreply@github.com>2023-11-07 19:14:58 +0800
commit6bb2ec05be1a502c70e95799af167ab4f6662cd5 (patch)
tree9d2ee1e9aabbcfbaefcc0d79c956fc39c102e866
parent534733b77cb1baef1459faa16a45d75c5f6275e3 (diff)
downloadaichat-6bb2ec05be1a502c70e95799af167ab4f6662cd5.tar.gz
feat: add `config.prelude` to allow setting default role/session (#224)
-rw-r--r--README.md3
-rw-r--r--config.example.yaml3
-rw-r--r--src/config/mod.rs29
-rw-r--r--src/main.rs1
-rw-r--r--src/render/repl.rs2
-rw-r--r--src/repl/mod.rs2
6 files changed, 34 insertions, 6 deletions
diff --git a/README.md b/README.md
index a061bf2..df4ed18 100644
--- a/README.md
+++ b/README.md
@@ -68,6 +68,7 @@ wrap: no # Specify the text-wrapping mode (no, auto, <ma
wrap_code: false # Whether wrap code block
auto_copy: false # Automatically copy the last output to the clipboard
keybindings: emacs # REPL keybindings. values: emacs, vi
+prelude: '' # Set a default role or session (role:<name>, session:<name>)
clients:
- type: openai
@@ -141,7 +142,7 @@ The Chat REPL supports:
.info Print system info
.edit Multi-line editing (CTRL+S to finish)
.model Switch LLM model
-.role Use role
+.role Use a role
.info role Show role info
.exit role Leave current role
.session Start a context-aware chat session
diff --git a/config.example.yaml b/config.example.yaml
index 30e00c9..8eae76b 100644
--- a/config.example.yaml
+++ b/config.example.yaml
@@ -6,7 +6,8 @@ light_theme: false # Whether to use a light theme
wrap: no # Specify the text-wrapping mode (no, auto, <max-width>)
wrap_code: false # Whether wrap code block
auto_copy: false # Automatically copy the last output to the clipboard
-keybindings: emacs # REPL keybindings. values: emacs, vi
+keybindings: emacs # REPL keybindings. (emacs, vi)
+prelude: '' # Set a default role or session (role:<name>, session:<name>)
clients:
# All clients have the following configuration:
diff --git a/src/config/mod.rs b/src/config/mod.rs
index e6fef5f..22c58bb 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -60,9 +60,11 @@ pub struct Config {
pub wrap_code: bool,
/// Automatically copy the last output to the clipboard
pub auto_copy: bool,
- /// REPL keybindings. values: emacs, vi
+ /// REPL keybindings. (emacs, vi)
pub keybindings: Keybindings,
- /// Setup AIs
+ /// Set a default role or session (role:<name>, session:<name>)
+ pub prelude: String,
+ /// Setup clients
pub clients: Vec<ClientConfig>,
/// Predefined roles
#[serde(skip)]
@@ -94,6 +96,7 @@ impl Default for Config {
wrap_code: false,
auto_copy: false,
keybindings: Default::default(),
+ prelude: String::new(),
clients: vec![ClientConfig::default()],
roles: vec![],
role: None,
@@ -145,6 +148,28 @@ impl Config {
Ok(config)
}
+ pub fn onstart(&mut self) -> Result<()> {
+ let prelude = self.prelude.clone();
+ let err_msg = || format!("Invalid prelude '{}", prelude);
+ match prelude.split_once(':') {
+ Some(("role", name)) => {
+ if self.role.is_none() && self.session.is_none() {
+ self.set_role(name).with_context(err_msg)?;
+ }
+ }
+ Some(("session", name)) => {
+ if self.session.is_none() {
+ self.start_session(Some(name)).with_context(err_msg)?;
+ }
+ }
+ Some(_) => {
+ bail!("{}", err_msg())
+ }
+ None => {}
+ }
+ Ok(())
+ }
+
pub fn retrieve_role(&self, name: &str) -> Result<Role> {
self.roles
.iter()
diff --git a/src/main.rs b/src/main.rs
index a40745f..368779e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -77,6 +77,7 @@ fn main() -> Result<()> {
println!("{}", info);
exit(0);
}
+ config.write().onstart()?;
let no_stream = cli.no_stream;
let client = init_client(&config)?;
if stdin().is_terminal() {
diff --git a/src/render/repl.rs b/src/render/repl.rs
index 34212c3..ca11e17 100644
--- a/src/render/repl.rs
+++ b/src/render/repl.rs
@@ -83,7 +83,7 @@ fn repl_render_stream_inner(
queue!(writer, style::Print(&buffer),)?;
// No guarantee the buffer width of the buffer will not exceed the number of columns.
- // So we calcuate the number of rows needed, rather than setting it directly to 1.
+ // So we calculate the number of rows needed, rather than setting it directly to 1.
buffer_rows = need_rows(&buffer, columns);
} else {
buffer = format!("{buffer}{text}");
diff --git a/src/repl/mod.rs b/src/repl/mod.rs
index 3962229..a4f397c 100644
--- a/src/repl/mod.rs
+++ b/src/repl/mod.rs
@@ -32,7 +32,7 @@ const REPL_COMMANDS: [(&str, &str); 14] = [
(".info", "Print system info"),
(".edit", "Multi-line editing (CTRL+S to finish)"),
(".model", "Switch LLM model"),
- (".role", "Use role"),
+ (".role", "Use a role"),
(".info role", "Show role info"),
(".exit role", "Leave current role"),
(".session", "Start a context-aware chat session"),