#[derive(
Debug,
Clone,
Default,
PartialEq,
Eq,
serde::Serialize,
serde::Deserialize,
schemars::JsonSchema,
)]
#[schemars(rename = "Identity")]
pub struct Identity {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub agent_instance_hierarchy: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub agent_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub agent_full_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub agent_remote: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub response_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub response_ids: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub plugin_owner: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub plugin_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub plugin_version: Option<String>,
#[serde(default)]
pub task: bool,
}
impl Identity {
pub fn from_transient_headers(
headers: &indexmap::IndexMap<String, String>,
) -> Self {
let get = |name: &str| {
headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case(name))
.map(|(_, v)| v.clone())
};
Self {
agent_instance_hierarchy: get(
"X-OBJECTIVEAI-AGENT-INSTANCE-HIERARCHY",
),
agent_id: get("X-OBJECTIVEAI-AGENT-ID"),
agent_full_id: get("X-OBJECTIVEAI-AGENT-FULL-ID"),
agent_remote: get("X-OBJECTIVEAI-AGENT-REMOTE"),
response_id: get("X-OBJECTIVEAI-RESPONSE-ID"),
response_ids: get("X-OBJECTIVEAI-RESPONSE-IDS"),
plugin_owner: None,
plugin_name: None,
plugin_version: None,
task: false,
}
}
pub fn identity_env(&self) -> Vec<(String, String)> {
[
(
"OBJECTIVEAI_AGENT_INSTANCE_HIERARCHY",
&self.agent_instance_hierarchy,
),
("OBJECTIVEAI_AGENT_ID", &self.agent_id),
("OBJECTIVEAI_AGENT_FULL_ID", &self.agent_full_id),
("OBJECTIVEAI_AGENT_REMOTE", &self.agent_remote),
("OBJECTIVEAI_RESPONSE_ID", &self.response_id),
("OBJECTIVEAI_RESPONSE_IDS", &self.response_ids),
("OBJECTIVEAI_PLUGIN_OWNER", &self.plugin_owner),
("OBJECTIVEAI_PLUGIN_NAME", &self.plugin_name),
("OBJECTIVEAI_PLUGIN_VERSION", &self.plugin_version),
]
.into_iter()
.filter_map(|(name, value)| {
value.as_ref().map(|v| (name.to_string(), v.clone()))
})
.chain(
self.task
.then(|| ("OBJECTIVEAI_TASK".to_string(), "true".to_string())),
)
.collect()
}
#[cfg(feature = "cli-executor")]
pub fn apply_to_command(&self, command: &mut tokio::process::Command) {
let pairs: [(&str, &Option<String>); 9] = [
(
"OBJECTIVEAI_AGENT_INSTANCE_HIERARCHY",
&self.agent_instance_hierarchy,
),
("OBJECTIVEAI_AGENT_ID", &self.agent_id),
("OBJECTIVEAI_AGENT_FULL_ID", &self.agent_full_id),
("OBJECTIVEAI_AGENT_REMOTE", &self.agent_remote),
("OBJECTIVEAI_RESPONSE_ID", &self.response_id),
("OBJECTIVEAI_RESPONSE_IDS", &self.response_ids),
("OBJECTIVEAI_PLUGIN_OWNER", &self.plugin_owner),
("OBJECTIVEAI_PLUGIN_NAME", &self.plugin_name),
("OBJECTIVEAI_PLUGIN_VERSION", &self.plugin_version),
];
for (name, value) in pairs {
match value {
Some(v) => {
command.env(name, v);
}
None => {
command.env_remove(name);
}
}
}
if self.task {
command.env("OBJECTIVEAI_TASK", "true");
} else {
command.env_remove("OBJECTIVEAI_TASK");
}
}
}