use std::sync::Arc;
use crate::security::capability::types::CSpace;
use uuid::Uuid;
#[derive(Debug, Clone)]
pub struct AgentContext {
pub agent_id: Uuid,
pub agent_name: String,
pub cspace: Arc<CSpace>,
}
impl AgentContext {
pub fn new(agent_name: String, cspace: CSpace) -> Self {
let agent_id = cspace.agent_id;
Self {
agent_id,
agent_name,
cspace: Arc::new(cspace),
}
}
pub fn from_template(agent_name: &str, template_name: &str) -> Self {
let id = Uuid::new_v4();
let cspace = crate::security::capability::resolve::resolve_cspace(
Some(template_name),
None,
None,
id,
);
Self {
agent_id: id,
agent_name: agent_name.to_string(),
cspace: Arc::new(cspace),
}
}
}
impl std::fmt::Display for AgentContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "agent:{}:{}", self.agent_name, self.agent_id)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::security::capability::types::{ResourceRef, Rights};
#[test]
fn test_from_template() {
let ctx = AgentContext::from_template("test", "standard");
assert_eq!(ctx.agent_name, "test");
assert!(!ctx.agent_id.is_nil());
assert!(ctx.cspace.can(
&ResourceRef::KernelDomain {
domain: "memory".into()
},
Rights::READ
));
}
#[test]
fn test_display() {
let ctx = AgentContext::from_template("my-agent", "worker");
let s = format!("{}", ctx);
assert!(s.starts_with("agent:my-agent:"));
}
}