From 14bfd4e97d0ea7d633e54595e17a54eb66529232 Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 29 Jul 2024 17:12:30 +0800 Subject: feat: add builtin variables for agent (#762) --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/config/agent.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 8597201..f5bd2c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,6 +87,7 @@ dependencies = [ "shell-words", "simplelog", "syntect", + "sys-locale", "textwrap", "time", "tokio", @@ -2727,6 +2728,15 @@ dependencies = [ "walkdir", ] +[[package]] +name = "sys-locale" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e801cf239ecd6ccd71f03d270d67dd53d13e90aab208bf4b8fe4ad957ea949b0" +dependencies = [ + "libc", +] + [[package]] name = "sysctl" version = "0.5.5" diff --git a/Cargo.toml b/Cargo.toml index 97dff85..462721c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ hnsw_rs = "0.3.0" rayon = "1.10.0" uuid = { version = "1.9.1", features = ["v4"] } html2text = "0.12.5" +sys-locale = "0.3.1" [dependencies.reqwest] version = "0.12.0" diff --git a/src/config/agent.rs b/src/config/agent.rs index c09c8a9..2b319ed 100644 --- a/src/config/agent.rs +++ b/src/config/agent.rs @@ -279,6 +279,9 @@ impl AgentDefinition { for variable in &self.variables { output = output.replace(&format!("{{{{{}}}}}", variable.name), &variable.value) } + for (key, value) in builtin_variables() { + output = output.replace(&format!("{{{{{}}}}}", key), &value); + } output } @@ -395,3 +398,27 @@ fn save_variables(variables_path: &Path, variables: &[AgentVariable]) -> Result< .with_context(|| format!("Failed to save variables to '{}'", variables_path.display()))?; Ok(()) } + +fn builtin_variables() -> Vec<(&'static str, String)> { + vec![ + ("__os__", env::consts::OS.to_string()), + ("__os_family__", env::consts::FAMILY.to_string()), + ("__arch__", env::consts::ARCH.to_string()), + ("__shell__", detect_shell().name), + ("__locale__", sys_locale::get_locale().unwrap_or_default()), + ( + "__now__", + time::OffsetDateTime::now_utc() + .format(simplelog::format_description!( + "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z" + )) + .unwrap_or_default(), + ), + ( + "__cwd__", + env::current_dir() + .map(|v| v.display().to_string()) + .unwrap_or_default(), + ), + ] +} -- cgit v1.2.3