use serde::{Deserialize, Serialize};
pub const ENV_CALLER: &str = "TCODE_CALLER";
pub const ENV_SESSION_ID: &str = "TCODE_SESSION_ID";
pub const ENV_PROJECT_ID: &str = "TCODE_PROJECT_ID";
pub const ENV_AGENT_ID: &str = "TCODE_AGENT_ID";
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase", tag = "kind")]
pub enum CallerIdentity {
Ctrl,
Pm {
session_id: String,
project_id: String,
},
Agent {
session_id: String,
project_id: String,
agent_id: String,
},
}
impl CallerIdentity {
pub fn max_recall_scope(&self) -> RecallCeiling {
match self {
Self::Ctrl => RecallCeiling::All,
Self::Pm { .. } => RecallCeiling::Session,
Self::Agent { .. } => RecallCeiling::Agent,
}
}
pub fn auto_tags(&self) -> Vec<String> {
match self {
Self::Ctrl => vec!["scope/user".into()],
Self::Pm {
session_id,
project_id,
} => vec![
format!("session/{session_id}"),
format!("project/{project_id}"),
],
Self::Agent {
session_id,
project_id,
agent_id,
} => vec![
format!("session/{session_id}"),
format!("project/{project_id}"),
format!("agent/{agent_id}"),
],
}
}
pub fn from_env() -> Option<Self> {
let kind = std::env::var(ENV_CALLER).ok()?;
match kind.as_str() {
"ctrl" => Some(Self::Ctrl),
"pm" => {
let session_id = std::env::var(ENV_SESSION_ID).ok()?;
let project_id = std::env::var(ENV_PROJECT_ID).ok()?;
Some(Self::Pm {
session_id,
project_id,
})
}
"agent" => {
let session_id = std::env::var(ENV_SESSION_ID).ok()?;
let project_id = std::env::var(ENV_PROJECT_ID).ok()?;
let agent_id = std::env::var(ENV_AGENT_ID).ok()?;
Some(Self::Agent {
session_id,
project_id,
agent_id,
})
}
_ => None,
}
}
pub fn session_id(&self) -> Option<&str> {
match self {
Self::Ctrl => None,
Self::Pm { session_id, .. } => Some(session_id.as_str()),
Self::Agent { session_id, .. } => Some(session_id.as_str()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RecallCeiling {
All,
Session,
Agent,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn max_recall_scope_for_each_variant() {
assert_eq!(CallerIdentity::Ctrl.max_recall_scope(), RecallCeiling::All);
assert_eq!(
CallerIdentity::Pm {
session_id: "s1".into(),
project_id: "p1".into()
}
.max_recall_scope(),
RecallCeiling::Session
);
assert_eq!(
CallerIdentity::Agent {
session_id: "s1".into(),
project_id: "p1".into(),
agent_id: "a1".into()
}
.max_recall_scope(),
RecallCeiling::Agent
);
}
#[test]
fn auto_tags_for_each_variant() {
let ctrl_tags = CallerIdentity::Ctrl.auto_tags();
assert_eq!(ctrl_tags, vec!["scope/user"]);
let pm_tags = CallerIdentity::Pm {
session_id: "s42".into(),
project_id: "p7".into(),
}
.auto_tags();
assert!(pm_tags.contains(&"session/s42".to_string()));
assert!(pm_tags.contains(&"project/p7".to_string()));
assert_eq!(pm_tags.len(), 2);
let agent_tags = CallerIdentity::Agent {
session_id: "s42".into(),
project_id: "p7".into(),
agent_id: "a99".into(),
}
.auto_tags();
assert!(agent_tags.contains(&"agent/a99".to_string()));
assert_eq!(agent_tags.len(), 3);
}
#[test]
fn session_id_for_each_variant() {
assert_eq!(CallerIdentity::Ctrl.session_id(), None);
assert_eq!(
CallerIdentity::Pm {
session_id: "sess".into(),
project_id: "proj".into()
}
.session_id(),
Some("sess")
);
assert_eq!(
CallerIdentity::Agent {
session_id: "sess".into(),
project_id: "proj".into(),
agent_id: "ag".into()
}
.session_id(),
Some("sess")
);
}
#[test]
fn caller_identity_roundtrips_json() {
let agent = CallerIdentity::Agent {
session_id: "s1".into(),
project_id: "p1".into(),
agent_id: "engineer".into(),
};
let json = serde_json::to_string(&agent).expect("serialize");
let back: CallerIdentity = serde_json::from_str(&json).expect("deserialize");
assert_eq!(agent, back);
}
}