From dd8f3e4528d45b7602df7ea6380b92a87dc4f711 Mon Sep 17 00:00:00 2001 From: mtul0729 <52401682+mtul0729@users.noreply.github.com> Date: Wed, 26 Mar 2025 08:34:00 +0800 Subject: refactor: replace `lazy_static` with `std::sync::LazyLock` (#1246) --- src/repl/mod.rs | 67 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 27 deletions(-) (limited to 'src/repl') diff --git a/src/repl/mod.rs b/src/repl/mod.rs index d7ddb96..97d8f84 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -24,25 +24,30 @@ use reedline::{ ReedlineEvent, ReedlineMenu, ValidationResult, Validator, Vi, }; use reedline::{MenuBuilder, Signal}; +use std::sync::LazyLock; use std::{env, process}; const MENU_NAME: &str = "completion_menu"; -lazy_static::lazy_static! { - static ref REPL_COMMANDS: [ReplCommand; 36] = [ +static REPL_COMMANDS: LazyLock<[ReplCommand; 36]> = LazyLock::new(|| { + [ ReplCommand::new(".help", "Show this help guide", AssertState::pass()), ReplCommand::new(".info", "Show system info", AssertState::pass()), - ReplCommand::new(".edit config", "Modify configuration file", AssertState::False(StateFlags::AGENT)), + ReplCommand::new( + ".edit config", + "Modify configuration file", + AssertState::False(StateFlags::AGENT), + ), ReplCommand::new(".model", "Switch LLM model", AssertState::pass()), ReplCommand::new( ".prompt", "Set a temporary role using a prompt", - AssertState::False(StateFlags::SESSION | StateFlags::AGENT) + AssertState::False(StateFlags::SESSION | StateFlags::AGENT), ), ReplCommand::new( ".role", "Create or switch to a role", - AssertState::False(StateFlags::SESSION | StateFlags::AGENT) + AssertState::False(StateFlags::SESSION | StateFlags::AGENT), ), ReplCommand::new( ".info role", @@ -57,7 +62,10 @@ lazy_static::lazy_static! { ReplCommand::new( ".save role", "Save current role to file", - AssertState::TrueFalse(StateFlags::ROLE, StateFlags::SESSION_EMPTY | StateFlags::SESSION), + AssertState::TrueFalse( + StateFlags::ROLE, + StateFlags::SESSION_EMPTY | StateFlags::SESSION, + ), ), ReplCommand::new( ".exit role", @@ -72,12 +80,12 @@ lazy_static::lazy_static! { ReplCommand::new( ".empty session", "Clear session messages", - AssertState::True(StateFlags::SESSION) + AssertState::True(StateFlags::SESSION), ), ReplCommand::new( ".compress session", "Compress session messages", - AssertState::True(StateFlags::SESSION) + AssertState::True(StateFlags::SESSION), ), ReplCommand::new( ".info session", @@ -87,23 +95,23 @@ lazy_static::lazy_static! { ReplCommand::new( ".edit session", "Modify current session", - AssertState::True(StateFlags::SESSION_EMPTY | StateFlags::SESSION) + AssertState::True(StateFlags::SESSION_EMPTY | StateFlags::SESSION), ), ReplCommand::new( ".save session", "Save current session to file", - AssertState::True(StateFlags::SESSION_EMPTY | StateFlags::SESSION) + AssertState::True(StateFlags::SESSION_EMPTY | StateFlags::SESSION), ), ReplCommand::new( ".exit session", "Exit active session", - AssertState::True(StateFlags::SESSION_EMPTY | StateFlags::SESSION) + AssertState::True(StateFlags::SESSION_EMPTY | StateFlags::SESSION), ), ReplCommand::new(".agent", "Use an agent", AssertState::bare()), ReplCommand::new( ".starter", "Use a conversation starter", - AssertState::True(StateFlags::AGENT) + AssertState::True(StateFlags::AGENT), ), ReplCommand::new( ".edit agent-config", @@ -118,12 +126,12 @@ lazy_static::lazy_static! { ReplCommand::new( ".exit agent", "Leave agent", - AssertState::True(StateFlags::AGENT) + AssertState::True(StateFlags::AGENT), ), ReplCommand::new( ".rag", "Initialize or access RAG", - AssertState::False(StateFlags::AGENT) + AssertState::False(StateFlags::AGENT), ), ReplCommand::new( ".edit rag-docs", @@ -150,30 +158,35 @@ lazy_static::lazy_static! { "Leave RAG", AssertState::TrueFalse(StateFlags::RAG, StateFlags::AGENT), ), - ReplCommand::new( - ".macro", - "Execute a macro", - AssertState::pass() - ), + ReplCommand::new(".macro", "Execute a macro", AssertState::pass()), ReplCommand::new( ".file", "Include files, directories, URLs or commands", - AssertState::pass() + AssertState::pass(), + ), + ReplCommand::new( + ".continue", + "Continue previous response", + AssertState::pass(), ), - ReplCommand::new(".continue", "Continue previous response", AssertState::pass()), ReplCommand::new( ".regenerate", "Regenerate last response", - AssertState::pass() + AssertState::pass(), ), ReplCommand::new(".copy", "Copy last response", AssertState::pass()), ReplCommand::new(".set", "Modify runtime settings", AssertState::pass()), - ReplCommand::new(".delete", "Delete roles, sessions, RAGs, or agents", AssertState::pass()), + ReplCommand::new( + ".delete", + "Delete roles, sessions, RAGs, or agents", + AssertState::pass(), + ), ReplCommand::new(".exit", "Exit REPL", AssertState::pass()), - ]; - static ref COMMAND_RE: Regex = Regex::new(r"^\s*(\.\S*)\s*").unwrap(); - static ref MULTILINE_RE: Regex = Regex::new(r"(?s)^\s*:::\s*(.*)\s*:::\s*$").unwrap(); -} + ] +}); +static COMMAND_RE: LazyLock = LazyLock::new(|| Regex::new(r"^\s*(\.\S*)\s*").unwrap()); +static MULTILINE_RE: LazyLock = + LazyLock::new(|| Regex::new(r"(?s)^\s*:::\s*(.*)\s*:::\s*$").unwrap()); pub struct Repl { config: GlobalConfig, -- cgit v1.2.3