From ec94f4c7bf8a93c971bb00fd8442e448c0c37caf Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 6 Sep 2024 06:53:52 +0800 Subject: [PATCH 1/2] feat: add config `serve_addr` & env $SERVE_ADDR for specifying serve addr --- src/config/mod.rs | 14 ++++++++++++++ src/serve.rs | 3 +-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 3ef68c6d..36a33498 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -57,6 +57,8 @@ pub const TEMP_SESSION_NAME: &str = "temp"; const CLIENTS_FIELD: &str = "clients"; +const SERVE_ADDR: &str = "127.0.0.1:8000"; + const SUMMARIZE_PROMPT: &str = "Summarize the discussion briefly in 200 words or less to use as a prompt for future context."; const SUMMARY_PROMPT: &str = "This is a summary of the chat history as a recap: "; @@ -126,6 +128,8 @@ pub struct Config { pub left_prompt: Option, pub right_prompt: Option, + pub serve_addr: Option, + pub clients: Vec, #[serde(skip)] @@ -191,6 +195,8 @@ impl Default for Config { left_prompt: None, right_prompt: None, + serve_addr: None, + clients: vec![], role: None, @@ -392,6 +398,10 @@ impl Config { flags } + pub fn serve_addr(&self) -> String { + self.serve_addr.clone().unwrap_or_else(|| SERVE_ADDR.into()) + } + pub fn log(is_serve: bool) -> Result<(LevelFilter, Option)> { let log_level = env::var(get_env_name("log_level")) .ok() @@ -1848,6 +1858,10 @@ impl Config { if let Some(v) = read_env_value::("right_prompt") { self.right_prompt = v; } + + if let Some(v) = read_env_value::("serve_addr") { + self.serve_addr = v; + } } fn load_functions(&mut self) -> Result<()> { diff --git a/src/serve.rs b/src/serve.rs index dc16d845..59507a4d 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -32,7 +32,6 @@ use tokio::{ use tokio_graceful::Shutdown; use tokio_stream::wrappers::UnboundedReceiverStream; -const DEFAULT_ADDRESS: &str = "127.0.0.1:8000"; const DEFAULT_MODEL_NAME: &str = "default"; const PLAYGROUND_HTML: &[u8] = include_bytes!("../assets/playground.html"); const ARENA_HTML: &[u8] = include_bytes!("../assets/arena.html"); @@ -50,7 +49,7 @@ pub async fn run(config: GlobalConfig, addr: Option) -> Result<()> { addr } } - None => DEFAULT_ADDRESS.to_string(), + None => config.read().serve_addr(), }; let server = Arc::new(Server::new(&config)); let listener = TcpListener::bind(&addr).await?; From f8d6cdda71f32f1389a79dfd53693d2ef5d0245d Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 6 Sep 2024 07:01:51 +0800 Subject: [PATCH 2/2] clippy --- src/repl/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 0d9353b5..eec43e84 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -262,7 +262,7 @@ impl Repl { None => println!("Usage: .prompt ..."), }, ".role" => match args { - Some(args) => match args.split_once(|c| c == '\n' || c == ' ') { + Some(args) => match args.split_once(['\n', ' ']) { Some((name, text)) => { let role = self.config.read().retrieve_role(name.trim())?; let input = Input::from_str(&self.config, text.trim(), Some(role));