use agent_framework_core::error::{Error, Result};
pub const DEFAULT_PORT: u16 = 8088;
pub const DEFAULT_SSE_KEEPALIVE_SECONDS: u64 = 15;
pub mod env {
pub const FOUNDRY_AGENT_NAME: &str = "FOUNDRY_AGENT_NAME";
pub const FOUNDRY_AGENT_ID: &str = "FOUNDRY_AGENT_ID";
pub const FOUNDRY_AGENT_VERSION: &str = "FOUNDRY_AGENT_VERSION";
pub const FOUNDRY_HOSTING_ENVIRONMENT: &str = "FOUNDRY_HOSTING_ENVIRONMENT";
pub const FOUNDRY_PROJECT_ENDPOINT: &str = "FOUNDRY_PROJECT_ENDPOINT";
pub const FOUNDRY_PROJECT_ARM_ID: &str = "FOUNDRY_PROJECT_ARM_ID";
pub const FOUNDRY_AGENT_SESSION_ID: &str = "FOUNDRY_AGENT_SESSION_ID";
pub const FOUNDRY_AGENT_SESSION_GUID: &str = "FOUNDRY_AGENT_SESSION_GUID";
pub const PORT: &str = "PORT";
pub const APPLICATIONINSIGHTS_CONNECTION_STRING: &str = "APPLICATIONINSIGHTS_CONNECTION_STRING";
pub const OTEL_EXPORTER_OTLP_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_ENDPOINT";
pub const SSE_KEEPALIVE_INTERVAL: &str = "SSE_KEEPALIVE_INTERVAL";
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct FoundryHostConfig {
pub agent_name: String,
pub agent_version: String,
pub agent_id: String,
pub agent_guid: String,
pub is_hosted: bool,
pub project_endpoint: String,
pub project_id: String,
pub session_id: String,
pub session_guid: String,
pub port: u16,
pub appinsights_connection_string: String,
pub otlp_endpoint: String,
pub sse_keepalive_interval: u64,
}
impl FoundryHostConfig {
pub fn local() -> Self {
Self {
port: DEFAULT_PORT,
sse_keepalive_interval: DEFAULT_SSE_KEEPALIVE_SECONDS,
..Default::default()
}
}
pub fn from_env() -> Result<Self> {
Self::from_lookup(|name| std::env::var(name).ok())
}
pub fn from_lookup(lookup: impl Fn(&str) -> Option<String>) -> Result<Self> {
let get = |name: &str| lookup(name).unwrap_or_default();
let agent_name = get(env::FOUNDRY_AGENT_NAME);
let agent_version = get(env::FOUNDRY_AGENT_VERSION);
let agent_id = match (agent_name.is_empty(), agent_version.is_empty()) {
(false, false) => format!("{agent_name}:{agent_version}"),
(false, true) => agent_name.clone(),
_ => String::new(),
};
let is_hosted = !get(env::FOUNDRY_HOSTING_ENVIRONMENT).is_empty();
let session_guid = get(env::FOUNDRY_AGENT_SESSION_GUID);
if is_hosted
&& !session_guid.is_empty()
&& !(session_guid.len() == 32
&& session_guid
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)))
{
return Err(Error::Configuration(
"FOUNDRY_AGENT_SESSION_GUID must be a 32-character lowercase hexadecimal GUID"
.into(),
));
}
let int = |name: &str, default: u64| -> Result<u64> {
match lookup(name) {
None => Ok(default),
Some(raw) => raw.trim().parse::<u64>().map_err(|_| {
Error::Configuration(format!(
"Invalid value for {name}: {raw:?} (expected a non-negative integer)"
))
}),
}
};
let port = int(env::PORT, u64::from(DEFAULT_PORT))?;
let port = u16::try_from(port)
.ok()
.filter(|p| *p > 0)
.ok_or_else(|| Error::Configuration(format!("Invalid value for PORT: {port}")))?;
Ok(Self {
agent_name,
agent_version,
agent_id,
agent_guid: get(env::FOUNDRY_AGENT_ID),
is_hosted,
project_endpoint: get(env::FOUNDRY_PROJECT_ENDPOINT),
project_id: get(env::FOUNDRY_PROJECT_ARM_ID),
session_id: get(env::FOUNDRY_AGENT_SESSION_ID),
session_guid,
port,
appinsights_connection_string: get(env::APPLICATIONINSIGHTS_CONNECTION_STRING),
otlp_endpoint: get(env::OTEL_EXPORTER_OTLP_ENDPOINT),
sse_keepalive_interval: int(
env::SSE_KEEPALIVE_INTERVAL,
DEFAULT_SSE_KEEPALIVE_SECONDS,
)?,
})
}
}