ricecoder_storage/industry/
agents.rs

1//! Generic agent configuration adapter
2//!
3//! Reads and converts generic agent configuration files (AGENTS.md)
4//! into RiceCoder's internal configuration format.
5
6use crate::config::{Config, SteeringRule};
7use crate::error::StorageResult;
8use crate::types::DocumentFormat;
9use std::path::Path;
10use tracing::debug;
11
12use super::adapter::IndustryFileAdapter;
13
14/// Generic agents adapter
15pub struct AgentsAdapter;
16
17impl AgentsAdapter {
18    /// Create a new agents adapter
19    pub fn new() -> Self {
20        AgentsAdapter
21    }
22
23    /// Read AGENTS.md file
24    fn read_agents_md(&self, project_root: &Path) -> StorageResult<Option<String>> {
25        let agents_path = project_root.join("AGENTS.md");
26
27        if !agents_path.exists() {
28            debug!("No AGENTS.md file found at {:?}", agents_path);
29            return Ok(None);
30        }
31
32        debug!("Reading AGENTS.md from {:?}", agents_path);
33        let content = std::fs::read_to_string(&agents_path).map_err(|e| {
34            crate::error::StorageError::io_error(
35                agents_path.clone(),
36                crate::error::IoOperation::Read,
37                e,
38            )
39        })?;
40
41        Ok(Some(content))
42    }
43}
44
45impl Default for AgentsAdapter {
46    fn default() -> Self {
47        Self::new()
48    }
49}
50
51impl IndustryFileAdapter for AgentsAdapter {
52    fn name(&self) -> &'static str {
53        "agents"
54    }
55
56    fn can_handle(&self, project_root: &Path) -> bool {
57        project_root.join("AGENTS.md").exists()
58    }
59
60    fn read_config(&self, project_root: &Path) -> StorageResult<Config> {
61        let mut config = Config::default();
62
63        if let Ok(Some(agents_content)) = self.read_agents_md(project_root) {
64            debug!("Adding agent instructions as steering rule");
65            config.steering.push(SteeringRule {
66                name: "agent-instructions".to_string(),
67                content: agents_content,
68                format: DocumentFormat::Markdown,
69            });
70        }
71
72        Ok(config)
73    }
74
75    fn priority(&self) -> u32 {
76        // Generic agents have lower priority than specific tools
77        40
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84    use std::fs;
85    use tempfile::TempDir;
86
87    #[test]
88    fn test_agents_adapter_detects_agents_md() {
89        let temp_dir = TempDir::new().unwrap();
90        let agents_path = temp_dir.path().join("AGENTS.md");
91        fs::write(&agents_path, "# Agent Instructions").unwrap();
92
93        let adapter = AgentsAdapter::new();
94        assert!(adapter.can_handle(temp_dir.path()));
95    }
96
97    #[test]
98    fn test_agents_adapter_no_file() {
99        let temp_dir = TempDir::new().unwrap();
100
101        let adapter = AgentsAdapter::new();
102        assert!(!adapter.can_handle(temp_dir.path()));
103    }
104
105    #[test]
106    fn test_agents_adapter_reads_agents_md() {
107        let temp_dir = TempDir::new().unwrap();
108        let agents_path = temp_dir.path().join("AGENTS.md");
109        let instructions = "# Agent Instructions\nGeneric agent guidelines";
110        fs::write(&agents_path, instructions).unwrap();
111
112        let adapter = AgentsAdapter::new();
113        let config = adapter.read_config(temp_dir.path()).unwrap();
114
115        assert_eq!(config.steering.len(), 1);
116        assert_eq!(config.steering[0].name, "agent-instructions");
117        assert_eq!(config.steering[0].content, instructions);
118        assert_eq!(config.steering[0].format, DocumentFormat::Markdown);
119    }
120
121    #[test]
122    fn test_agents_adapter_priority() {
123        let adapter = AgentsAdapter::new();
124        assert_eq!(adapter.priority(), 40);
125    }
126
127    #[test]
128    fn test_agents_adapter_name() {
129        let adapter = AgentsAdapter::new();
130        assert_eq!(adapter.name(), "agents");
131    }
132}