From 632d7cdfa32534f55a48aa31fb2512a221040e60 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 13 Sep 2024 18:23:44 +0800 Subject: refactor: improve repl completeion (#865) --- src/config/mod.rs | 72 ++++++++++++++++++++++++--------------------------- src/repl/completer.rs | 2 +- src/repl/mod.rs | 2 +- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index d58f4f1..3fdba3c 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1441,27 +1441,16 @@ impl Config { let mut filter = ""; if args.len() == 1 { values = match cmd { - ".role" => Self::list_roles(true) - .into_iter() - .map(|v| (v, None)) - .collect(), + ".role" => map_completion_values(Self::list_roles(true)), ".model" => list_chat_models(self) .into_iter() .map(|v| (v.id(), Some(v.description()))) .collect(), - ".session" => self - .list_sessions() - .into_iter() - .map(|v| (v, None)) - .collect(), - ".rag" => Self::list_rags().into_iter().map(|v| (v, None)).collect(), - ".agent" => list_agents().into_iter().map(|v| (v, None)).collect(), + ".session" => map_completion_values(self.list_sessions()), + ".rag" => map_completion_values(Self::list_rags()), + ".agent" => map_completion_values(list_agents()), ".starter" => match &self.agent { - Some(agent) => agent - .conversation_staters() - .iter() - .map(|v| (v.clone(), None)) - .collect(), + Some(agent) => map_completion_values(agent.conversation_staters().to_vec()), None => vec![], }, ".variable" => match &self.agent { @@ -1472,28 +1461,31 @@ impl Config { .collect(), None => vec![], }, - ".set" => vec![ - "max_output_tokens", - "temperature", - "top_p", - "dry_run", - "stream", - "save", - "save_session", - "compress_threshold", - "function_calling", - "use_tools", - "rag_reranker_model", - "rag_top_k", - "highlight", - ] - .into_iter() - .map(|v| (format!("{v} "), None)) - .collect(), - ".delete" => vec!["roles", "sessions", "rags", "agents-config"] - .into_iter() - .map(|v| (v.to_string(), None)) - .collect(), + ".set" => { + let mut values = vec![ + "max_output_tokens", + "temperature", + "top_p", + "dry_run", + "stream", + "save", + "save_session", + "compress_threshold", + "function_calling", + "use_tools", + "rag_reranker_model", + "rag_top_k", + "highlight", + ]; + values.sort_unstable(); + values + .into_iter() + .map(|v| (format!("{v} "), None)) + .collect() + } + ".delete" => { + map_completion_values(vec!["roles", "sessions", "rags", "agents-config"]) + } _ => vec![], }; filter = args[0] @@ -2129,6 +2121,10 @@ fn complete_option_bool(value: Option) -> Vec { } } +fn map_completion_values(value: Vec) -> Vec<(String, Option)> { + value.into_iter().map(|v| (v.to_string(), None)).collect() +} + fn update_rag(config: &GlobalConfig, f: F) -> Result<()> where F: FnOnce(&mut Rag) -> Result<()>, diff --git a/src/repl/completer.rs b/src/repl/completer.rs index 7f325bd..0771001 100644 --- a/src/repl/completer.rs +++ b/src/repl/completer.rs @@ -42,7 +42,7 @@ impl Completer for ReplCompleter { .map(|(v, _)| *v) .collect::>() .join(" "); - cmd.name.starts_with(&line) + cmd.name.starts_with(&line) && cmd.name != ".set" }) .collect(); diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 13e147b..2f6c76f 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -147,7 +147,7 @@ lazy_static::lazy_static! { "Regenerate the last response", AssertState::pass() ), - ReplCommand::new(".set", "Adjust settings", AssertState::pass()), + ReplCommand::new(".set", "Adjust runtime configuration", AssertState::pass()), ReplCommand::new(".delete", "Delete roles/sessions/RAGs/agents-config", AssertState::pass()), ReplCommand::new(".copy", "Copy the last response", AssertState::pass()), ReplCommand::new(".exit", "Exit the REPL", AssertState::pass()), -- cgit v1.2.3