mod context_files;
mod frontmatter;
mod prompts;
mod skills;
use std::path::{Path, PathBuf};
use tea_context::{SkillMetadata, WorkspaceInstruction};
pub use prompts::PromptTemplate;
pub use skills::{DiscoveredSkill, LoadedSkill};
use crate::{CodingError, ProjectAccess};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResourceDiagnostic {
code: &'static str,
subject: String,
}
impl ResourceDiagnostic {
pub(crate) fn new(code: &'static str, subject: &str) -> Self {
Self {
code,
subject: subject.to_owned(),
}
}
#[must_use]
pub const fn code(&self) -> &'static str {
self.code
}
#[must_use]
pub fn subject(&self) -> &str {
&self.subject
}
}
#[derive(Debug, Clone)]
pub struct ResourceCatalog {
context: Vec<WorkspaceInstruction>,
skills: Vec<DiscoveredSkill>,
prompts: Vec<PromptTemplate>,
diagnostics: Vec<ResourceDiagnostic>,
}
impl ResourceCatalog {
#[allow(clippy::too_many_arguments)]
pub fn discover(
boundary: &Path,
workspace: &Path,
access: ProjectAccess,
global_skill_roots: &[PathBuf],
project_skill_roots: &[PathBuf],
global_prompt_root: Option<&Path>,
project_prompt_root: Option<&Path>,
) -> Result<Self, CodingError> {
let (context, diagnostics) = context_files::discover(boundary, workspace, access)?;
let mut skill_roots = global_skill_roots.to_vec();
if access == ProjectAccess::Trusted {
skill_roots.extend_from_slice(project_skill_roots);
}
let skills = skills::discover(&skill_roots)?;
let prompts = prompts::discover(
global_prompt_root,
(access == ProjectAccess::Trusted)
.then_some(project_prompt_root)
.flatten(),
)?;
Ok(Self {
context,
skills,
prompts,
diagnostics,
})
}
pub fn add_explicit_context_files(
&mut self,
workspace: &tea_coding_tools::WorkspaceRoot,
paths: &[String],
) -> Result<(), CodingError> {
context_files::add_explicit(workspace, paths, &mut self.context)
}
pub fn apply_settings(&mut self, context_files: bool, prompt_templates: bool) {
if !context_files {
self.context.clear();
}
if !prompt_templates {
self.prompts.clear();
}
}
#[must_use]
pub fn context(&self) -> &[WorkspaceInstruction] {
&self.context
}
#[must_use]
pub fn skills(&self) -> &[DiscoveredSkill] {
&self.skills
}
#[must_use]
pub fn prompts(&self) -> &[PromptTemplate] {
&self.prompts
}
#[must_use]
pub fn diagnostics(&self) -> &[ResourceDiagnostic] {
&self.diagnostics
}
#[must_use]
pub fn skill_metadata(&self) -> Vec<SkillMetadata> {
self.skills
.iter()
.map(|skill| skill.metadata().clone())
.collect()
}
pub fn invoke_skill(&self, invocation: &str) -> Result<LoadedSkill, CodingError> {
let name = invocation
.strip_prefix("/skill:")
.map(|remaining| {
remaining
.split_once(' ')
.map_or(remaining, |(name, _)| name)
})
.filter(|name| !name.is_empty())
.ok_or_else(|| {
crate::CodingError::new(
crate::CodingErrorCode::InvalidInput,
"skill invocation is invalid",
)
})?;
self.skills
.iter()
.find(|skill| skill.metadata().id().as_str() == name)
.ok_or_else(|| {
crate::CodingError::new(crate::CodingErrorCode::NotFound, "skill is not registered")
})?
.invoke(invocation)
}
}