use std::ffi::OsString;
const UNAVAILABLE: &str = "unavailable";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Agent {
pub id: Option<String>,
pub version: Option<String>,
pub model: Option<String>,
}
impl Agent {
pub fn from_env() -> Self {
Self::from_lookup(|name| std::env::var_os(name))
}
pub(crate) fn from_lookup(env: impl Fn(&str) -> Option<OsString>) -> Self {
let text = |name| {
env(name)
.map(|v| v.to_string_lossy().trim().to_string())
.filter(|v| !v.is_empty())
};
let raw = text("AI_AGENT");
let (named, version) = raw.as_deref().map_or((None, None), parse_ai_agent);
let id = match mise_agent_env::detect_with_env(&env).map(|d| d.agent) {
Some(mise_agent_env::Agent::Other) | None => named,
Some(agent) => Some(agent.as_str().to_string()),
};
Self {
id,
version,
model: text("JOUET_AI_MODEL"),
}
}
pub fn tool(&self) -> Option<String> {
let id = self.id.as_deref()?;
Some(
match id {
"claude-code" => "Claude Code",
"claude-cowork" => "Claude Cowork",
"codex" => "Codex",
"cursor" => "Cursor",
"gemini-cli" => "Gemini CLI",
"github-copilot" => "GitHub Copilot",
"opencode" => "OpenCode",
other => other,
}
.to_string(),
)
}
pub fn default_trailers(&self) -> Vec<String> {
let Some(id) = self.id.as_deref() else {
return Vec::new();
};
let mut out = Vec::new();
if matches!(id, "claude-code" | "claude-cowork") {
out.push("Co-Authored-By: Claude <noreply@anthropic.com>".to_string());
}
out.push("AI-Assisted: Tool: {tool}; model: {model}; version: {version}".to_string());
out
}
pub fn expand(&self, template: &str) -> String {
let or = |v: Option<String>| v.unwrap_or_else(|| UNAVAILABLE.into());
template
.replace("{tool}", &or(self.tool()))
.replace("{model}", &or(self.model.clone()))
.replace("{version}", &or(self.version.clone()))
}
}
fn parse_ai_agent(raw: &str) -> (Option<String>, Option<String>) {
let (name, at_version) = match raw.split_once('@') {
Some((name, version)) => (name, Some(version)),
None => (raw, None),
};
let mut words = name.split('_');
let id = words
.next()
.map(|w| w.trim().to_ascii_lowercase())
.filter(|w| !w.is_empty() && !matches!(w.as_str(), "1" | "true" | "yes"));
let is_version = |w: &&str| {
w.starts_with(|c: char| c.is_ascii_digit())
&& w.chars()
.all(|c| c.is_ascii_digit() || c == '-' || c == '.')
};
let version = at_version
.into_iter()
.chain(words)
.find(is_version)
.map(|w| w.replace('-', "."));
(id, version)
}
#[cfg(test)]
mod tests {
use super::*;
fn agent(vars: &[(&str, &str)]) -> Agent {
let vars: Vec<(String, String)> = vars
.iter()
.map(|(k, v)| ((*k).to_string(), (*v).to_string()))
.collect();
Agent::from_lookup(|name| {
vars.iter()
.find(|(k, _)| k == name)
.map(|(_, v)| OsString::from(v))
})
}
#[test]
fn ai_agent_names_the_tool_and_version() {
let a = agent(&[("AI_AGENT", "claude-code_2-1-309_agent")]);
assert_eq!(a.id.as_deref(), Some("claude-code"));
assert_eq!(a.version.as_deref(), Some("2.1.309"));
assert_eq!(a.tool().as_deref(), Some("Claude Code"));
assert_eq!(a.model, None);
assert_eq!(
a.expand("Tool: {tool}; model: {model}; version: {version}"),
"Tool: Claude Code; model: unavailable; version: 2.1.309"
);
}
#[test]
fn a_vendor_marker_names_the_tool_without_a_version() {
let a = agent(&[("CODEX_SANDBOX", "seatbelt")]);
assert_eq!(a.id.as_deref(), Some("codex"));
assert_eq!(a.version, None);
let a = agent(&[("AI_AGENT", "codex"), ("JOUET_AI_MODEL", "openai/gpt-5")]);
assert_eq!(a.tool().as_deref(), Some("Codex"));
assert_eq!(a.model.as_deref(), Some("openai/gpt-5"));
}
#[test]
fn defaults_follow_the_agent() {
assert!(agent(&[]).default_trailers().is_empty());
let claude = agent(&[("AI_AGENT", "claude-code_2-1-309_agent")]);
assert_eq!(
claude.default_trailers(),
[
"Co-Authored-By: Claude <noreply@anthropic.com>",
"AI-Assisted: Tool: {tool}; model: {model}; version: {version}",
]
);
let codex = agent(&[("AI_AGENT", "codex")]);
assert_eq!(codex.default_trailers().len(), 1);
}
#[test]
fn flag_like_values_name_no_tool() {
assert_eq!(parse_ai_agent("1"), (None, None));
assert_eq!(parse_ai_agent("opencode"), (Some("opencode".into()), None));
assert_eq!(
parse_ai_agent("some-agent_v2_x"),
(Some("some-agent".into()), None)
);
assert_eq!(
parse_ai_agent("pi@0.4.1"),
(Some("pi".into()), Some("0.4.1".into()))
);
let copilot = agent(&[("AI_AGENT", "github_copilot_vscode_agent")]);
assert_eq!(copilot.tool().as_deref(), Some("GitHub Copilot"));
}
}