use std::sync::OnceLock;
use indexmap::IndexMap;
use objectiveai_sdk::identity::Identity;
pub fn identity() -> &'static Identity {
static IDENTITY: OnceLock<Identity> = OnceLock::new();
IDENTITY.get_or_init(|| read_identity(env))
}
pub fn arguments() -> &'static IndexMap<String, serde_json::Value> {
static ARGUMENTS: OnceLock<IndexMap<String, serde_json::Value>> = OnceLock::new();
ARGUMENTS.get_or_init(|| read_arguments(env))
}
pub(crate) fn postgres_url() -> Option<&'static str> {
static POSTGRES_URL: OnceLock<Option<String>> = OnceLock::new();
POSTGRES_URL
.get_or_init(|| env("OBJECTIVEAI_POSTGRES_URL"))
.as_deref()
}
fn env(name: &str) -> Option<String> {
std::env::var(name).ok().filter(|v| !v.is_empty())
}
fn read_identity(lookup: impl Fn(&str) -> Option<String>) -> Identity {
Identity {
agent_instance_hierarchy: lookup("OBJECTIVEAI_AGENT_INSTANCE_HIERARCHY"),
agent_id: lookup("OBJECTIVEAI_AGENT_ID"),
agent_full_id: lookup("OBJECTIVEAI_AGENT_FULL_ID"),
agent_remote: lookup("OBJECTIVEAI_AGENT_REMOTE"),
response_id: lookup("OBJECTIVEAI_RESPONSE_ID"),
response_ids: lookup("OBJECTIVEAI_RESPONSE_IDS"),
plugin_owner: lookup("OBJECTIVEAI_PLUGIN_OWNER"),
plugin_name: lookup("OBJECTIVEAI_PLUGIN_NAME"),
plugin_version: lookup("OBJECTIVEAI_PLUGIN_VERSION"),
task: lookup("OBJECTIVEAI_TASK").as_deref() == Some("true"),
}
}
fn read_arguments(
lookup: impl Fn(&str) -> Option<String>,
) -> IndexMap<String, serde_json::Value> {
lookup("OBJECTIVEAI_ARGUMENTS")
.and_then(|raw| serde_json::from_str(&raw).ok())
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
fn lookup(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> {
let pairs: Vec<(String, String)> = pairs
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
move |name: &str| {
pairs
.iter()
.find(|(k, _)| k == name)
.map(|(_, v)| v.clone())
.filter(|v| !v.is_empty())
}
}
#[test]
fn empty_environment_yields_nothing_set() {
assert_eq!(read_identity(lookup(&[])), Identity::default());
assert!(read_arguments(lookup(&[])).is_empty());
}
#[test]
fn identity_env_round_trips() {
let written = Identity {
agent_id: Some("agent".into()),
plugin_owner: Some("exampleorg".into()),
plugin_name: Some("hello".into()),
plugin_version: Some("v0.1.0".into()),
task: true,
..Default::default()
};
let pairs = written.identity_env();
let read = read_identity(|name| {
pairs.iter().find(|(k, _)| k == name).map(|(_, v)| v.clone())
});
assert_eq!(read, written);
}
#[test]
fn arguments_keep_canonical_key_order() {
let arguments = read_arguments(lookup(&[(
"OBJECTIVEAI_ARGUMENTS",
r#"{"apple":"1","mango":null,"zebra":[1,2]}"#,
)]));
let keys: Vec<&str> = arguments.keys().map(String::as_str).collect();
assert_eq!(keys, ["apple", "mango", "zebra"]);
}
#[test]
fn arguments_carry_arbitrary_json() {
let arguments = read_arguments(lookup(&[(
"OBJECTIVEAI_ARGUMENTS",
r#"{"level":"debug","retries":3,"hosts":["a"],"verbose":null}"#,
)]));
assert_eq!(arguments.get("level"), Some(&serde_json::json!("debug")));
assert_eq!(arguments.get("retries"), Some(&serde_json::json!(3)));
assert_eq!(arguments.get("hosts"), Some(&serde_json::json!(["a"])));
assert_eq!(arguments.get("verbose"), Some(&serde_json::Value::Null));
assert_eq!(arguments.get("absent"), None);
}
#[test]
fn empty_values_read_as_absent() {
let identity = read_identity(lookup(&[
("OBJECTIVEAI_AGENT_ID", ""),
("OBJECTIVEAI_TASK", ""),
]));
assert_eq!(identity.agent_id, None);
assert!(!identity.task);
}
#[test]
fn unparseable_arguments_do_not_panic() {
let arguments = read_arguments(lookup(&[("OBJECTIVEAI_ARGUMENTS", "not json")]));
assert!(arguments.is_empty());
}
}