feat: add env meta plugin for environment variables
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
use crate::meta_plugin::{MetaPlugin, MetaPluginType, process_metadata_outputs};
|
||||
use log::debug;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EnvMetaPlugin {
|
||||
is_finalized: bool,
|
||||
outputs: std::collections::HashMap<String, serde_yaml::Value>,
|
||||
env_vars: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
impl EnvMetaPlugin {
|
||||
pub fn new(
|
||||
_options: Option<std::collections::HashMap<String, serde_yaml::Value>>,
|
||||
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
|
||||
) -> Self {
|
||||
// Collect environment variables starting with KEEP_META_
|
||||
let mut env_vars = Vec::new();
|
||||
let mut outputs_map = std::collections::HashMap::new();
|
||||
|
||||
for (key, value) in std::env::vars() {
|
||||
if let Some(stripped_key) = key.strip_prefix("KEEP_META_") {
|
||||
// Add to env_vars to process later
|
||||
env_vars.push((stripped_key.to_string(), value));
|
||||
// Add to outputs with default mapping to the stripped name
|
||||
outputs_map.insert(
|
||||
stripped_key.to_string(),
|
||||
serde_yaml::Value::String(stripped_key.to_string())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Override with provided outputs
|
||||
if let Some(provided_outputs) = outputs {
|
||||
for (key, value) in provided_outputs {
|
||||
outputs_map.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
EnvMetaPlugin {
|
||||
is_finalized: false,
|
||||
outputs: outputs_map,
|
||||
env_vars,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MetaPlugin for EnvMetaPlugin {
|
||||
fn meta_type(&self) -> MetaPluginType {
|
||||
MetaPluginType::Env
|
||||
}
|
||||
|
||||
fn is_finalized(&self) -> bool {
|
||||
self.is_finalized
|
||||
}
|
||||
|
||||
fn set_finalized(&mut self, finalized: bool) {
|
||||
self.is_finalized = finalized;
|
||||
}
|
||||
|
||||
fn initialize(&mut self) -> crate::meta_plugin::MetaPluginResponse {
|
||||
// If already finalized, don't process again
|
||||
if self.is_finalized {
|
||||
return crate::meta_plugin::MetaPluginResponse {
|
||||
metadata: Vec::new(),
|
||||
is_finalized: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Process all collected environment variables
|
||||
let mut metadata = Vec::new();
|
||||
for (name, value) in &self.env_vars {
|
||||
if let Some(meta_data) = process_metadata_outputs(
|
||||
name,
|
||||
serde_yaml::Value::String(value.clone()),
|
||||
&self.outputs
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
}
|
||||
|
||||
// Mark as finalized since this plugin only needs to run once
|
||||
self.is_finalized = true;
|
||||
|
||||
crate::meta_plugin::MetaPluginResponse {
|
||||
metadata,
|
||||
is_finalized: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, _data: &[u8]) -> crate::meta_plugin::MetaPluginResponse {
|
||||
// If already finalized, don't process more data
|
||||
if self.is_finalized {
|
||||
return crate::meta_plugin::MetaPluginResponse {
|
||||
metadata: Vec::new(),
|
||||
is_finalized: true,
|
||||
};
|
||||
}
|
||||
|
||||
crate::meta_plugin::MetaPluginResponse {
|
||||
metadata: Vec::new(),
|
||||
is_finalized: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn finalize(&mut self) -> crate::meta_plugin::MetaPluginResponse {
|
||||
// If not already finalized, we can call initialize
|
||||
if !self.is_finalized {
|
||||
return self.initialize();
|
||||
}
|
||||
|
||||
crate::meta_plugin::MetaPluginResponse {
|
||||
metadata: Vec::new(),
|
||||
is_finalized: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||
&self.outputs
|
||||
}
|
||||
|
||||
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
||||
&mut self.outputs
|
||||
}
|
||||
|
||||
fn default_outputs(&self) -> Vec<String> {
|
||||
self.env_vars.iter()
|
||||
.map(|(name, _)| name.clone())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||
&std::collections::HashMap::new()
|
||||
}
|
||||
|
||||
fn options_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
||||
panic!("options_mut() not implemented for EnvMetaPlugin")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user