use trusty_agents_common::agent_content::{
AgentContentError, AgentRoster, ContentError, DevOverride, ResolvedContent, describe_source,
resolve_content,
};
use super::{
DEFAULT_AGENTS, DEFAULT_SKILL_NAMES, EmbeddedAgent, LOCAL_TM_AGENT_FORKS, SHARED_TM_AGENT_FILES,
};
pub const TCODE_AGENTS_DIR: &str = "agents/trusty-code";
pub const TCODE_SKILLS_DIR: &str = "instructions/trusty-code/skills";
pub fn resolve_for_process() -> Result<ResolvedContent, AgentContentError> {
let dev = std::env::current_dir().map_or(DevOverride::Off, DevOverride::DetectFrom);
resolve_content(dev)
}
pub fn require_file(content: &ResolvedContent, path: &str) -> Result<String, AgentContentError> {
let origin = || describe_source(content.source());
match content.read_to_string(path) {
Ok(body) if body.trim().is_empty() => Err(AgentContentError::Invalid {
origin: origin(),
path: path.to_owned(),
reason: "the file is empty".to_owned(),
}),
Ok(body) => Ok(body),
Err(ContentError::NotFound { .. }) => Err(AgentContentError::Missing {
origin: origin(),
path: path.to_owned(),
}),
Err(e) => Err(e.into()),
}
}
pub fn own_agent_path(file: &str) -> String {
format!("{TCODE_AGENTS_DIR}/{file}")
}
pub fn catalog_from(content: &ResolvedContent) -> Result<Vec<(String, String)>, AgentContentError> {
let roster = AgentRoster::load(content)?;
let mut out = Vec::with_capacity(SHARED_TM_AGENT_FILES.len() + LOCAL_TM_AGENT_FORKS.len());
for file in SHARED_TM_AGENT_FILES {
out.push(((*file).to_owned(), roster.require(file)?.to_owned()));
}
for file in LOCAL_TM_AGENT_FORKS {
out.push((
(*file).to_owned(),
require_file(content, &own_agent_path(file))?,
));
}
Ok(out)
}
pub fn direct_agent_from(
content: &ResolvedContent,
name: &str,
) -> Result<Option<String>, AgentContentError> {
if !is_direct(name) {
return Ok(None);
}
require_file(content, &own_agent_path(&format!("{name}.md"))).map(Some)
}
pub fn load_direct_agent(name: &str) -> Result<Option<String>, AgentContentError> {
if !is_direct(name) {
return Ok(None);
}
direct_agent_from(&resolve_for_process()?, name)
}
pub fn is_direct(name: &str) -> bool {
DEFAULT_AGENTS
.iter()
.any(|a| matches!(a, EmbeddedAgent::Direct { name: n } if *n == name))
}
#[derive(Debug, Clone)]
pub struct AgentSources {
catalog: Vec<(String, String)>,
direct: Vec<(&'static str, String)>,
}
impl AgentSources {
pub fn from_content(content: &ResolvedContent) -> Result<Self, AgentContentError> {
let catalog = catalog_from(content)?;
let mut direct = Vec::new();
for agent in DEFAULT_AGENTS {
if let EmbeddedAgent::Direct { name } = agent {
let md = require_file(content, &own_agent_path(&format!("{name}.md")))?;
direct.push((*name, md));
}
}
Ok(Self { catalog, direct })
}
pub fn load() -> Result<Self, AgentContentError> {
Self::from_content(&resolve_for_process()?)
}
pub fn catalog(&self) -> &[(String, String)] {
&self.catalog
}
pub fn direct(&self, name: &str) -> Option<&str> {
self.direct
.iter()
.find(|(n, _)| *n == name)
.map(|(_, md)| md.as_str())
}
pub fn directs(&self) -> impl Iterator<Item = (&'static str, &str)> + '_ {
self.direct.iter().map(|(n, md)| (*n, md.as_str()))
}
}
#[derive(Debug, Clone)]
pub struct DefaultSkill {
pub name: &'static str,
pub skill_md: String,
}
pub fn skill_path(name: &str) -> String {
format!("{TCODE_SKILLS_DIR}/{name}/SKILL.md")
}
pub fn default_skills_from(
content: &ResolvedContent,
) -> Result<Vec<DefaultSkill>, AgentContentError> {
DEFAULT_SKILL_NAMES
.iter()
.map(|name| {
Ok(DefaultSkill {
name,
skill_md: require_file(content, &skill_path(name))?,
})
})
.collect()
}
pub fn load_default_skills() -> Result<Vec<DefaultSkill>, AgentContentError> {
default_skills_from(&resolve_for_process()?)
}
pub fn default_skill_from(
content: &ResolvedContent,
name: &str,
) -> Result<Option<String>, AgentContentError> {
if !DEFAULT_SKILL_NAMES.contains(&name) {
return Ok(None);
}
require_file(content, &skill_path(name)).map(Some)
}
pub fn load_default_skill(name: &str) -> Result<Option<String>, AgentContentError> {
if !DEFAULT_SKILL_NAMES.contains(&name) {
return Ok(None);
}
default_skill_from(&resolve_for_process()?, name)
}
#[cfg(test)]
#[path = "content_tests.rs"]
mod tests;