ricecoder_storage/markdown_config/
mod.rs

1//! Markdown Configuration Module
2//!
3//! Provides markdown-based definition of agents, modes, and commands with YAML frontmatter.
4//! Enables users to create custom configurations using familiar markdown syntax.
5//!
6//! # Overview
7//!
8//! The markdown configuration module enables users to define custom agents, modes, and commands
9//! using markdown files with YAML frontmatter. This approach combines the readability of markdown
10//! with the structure of YAML, making configuration files both human-friendly and machine-parseable.
11//!
12//! # Configuration File Format
13//!
14//! All markdown configuration files follow this structure:
15//!
16//! ```markdown
17//! ---
18//! name: example-agent
19//! description: Example agent configuration
20//! model: gpt-4
21//! temperature: 0.7
22//! max_tokens: 2000
23//! ---
24//!
25//! # Example Agent
26//!
27//! This is the markdown content that serves as documentation or prompt.
28//! ```
29//!
30//! # Configuration Types
31//!
32//! ## Agent Configuration
33//!
34//! Agents are AI-powered components with specific capabilities and parameters.
35//!
36//! **File Pattern**: `*.agent.md`
37//!
38//! **Fields**:
39//! - `name` (required): Unique identifier for the agent
40//! - `description` (optional): Human-readable description
41//! - `model` (optional): LLM model to use (e.g., "gpt-4")
42//! - `temperature` (optional): Model temperature (0.0-2.0)
43//! - `max_tokens` (optional): Maximum response tokens
44//! - `tools` (optional): List of available tools
45//!
46//! ## Mode Configuration
47//!
48//! Modes define editor behaviors and keybindings for different workflows.
49//!
50//! **File Pattern**: `*.mode.md`
51//!
52//! **Fields**:
53//! - `name` (required): Unique identifier for the mode
54//! - `description` (optional): Human-readable description
55//! - `keybinding` (optional): Keyboard shortcut to activate
56//! - `enabled` (optional): Whether the mode is enabled (default: true)
57//!
58//! ## Command Configuration
59//!
60//! Commands define custom operations with parameters and templates.
61//!
62//! **File Pattern**: `*.command.md`
63//!
64//! **Fields**:
65//! - `name` (required): Unique identifier for the command
66//! - `description` (optional): Human-readable description
67//! - `template` (required): Command template with parameter placeholders
68//! - `parameters` (optional): List of parameter definitions
69//! - `keybinding` (optional): Keyboard shortcut to invoke
70//!
71//! # Discovery and Loading
72//!
73//! Configuration files are discovered in the following locations (in priority order):
74//!
75//! 1. **Project-level**: `projects/ricecoder/.agent/`
76//! 2. **User-level**: `~/.ricecoder/agents/`, `~/.ricecoder/modes/`, `~/.ricecoder/commands/`
77//! 3. **System-level**: `/etc/ricecoder/agents/` (Linux/macOS)
78//!
79//! The [`ConfigurationLoader`] automatically discovers and loads all configuration files
80//! from these locations, validating each file and registering valid configurations with
81//! the [`ConfigRegistry`].
82//!
83//! # Hot-Reload Support
84//!
85//! The [`FileWatcher`] monitors configuration directories for changes. When a file is modified:
86//!
87//! 1. File is detected within 5 seconds
88//! 2. Configuration is re-parsed and validated
89//! 3. If valid, configuration is updated in the registry
90//! 4. If invalid, error is logged and previous configuration is retained
91//!
92//! This enables runtime configuration updates without requiring application restart.
93//!
94//! # Usage Examples
95//!
96//! ## Loading Configurations
97//!
98//! ```ignore
99//! use ricecoder_storage::markdown_config::{ConfigurationLoader, ConfigRegistry};
100//! use std::sync::Arc;
101//!
102//! #[tokio::main]
103//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
104//!     let registry = Arc::new(ConfigRegistry::new());
105//!     let loader = ConfigurationLoader::new(registry.clone());
106//!
107//!     // Discover and load configurations
108//!     let paths = vec![
109//!         std::path::PathBuf::from("~/.ricecoder/agents"),
110//!         std::path::PathBuf::from("projects/ricecoder/.agent"),
111//!     ];
112//!
113//!     loader.load_all(&paths).await?;
114//!
115//!     // Query loaded configurations
116//!     if let Some(agent) = registry.get_agent("code-review") {
117//!         println!("Found agent: {}", agent.name);
118//!     }
119//!
120//!     Ok(())
121//! }
122//! ```
123//!
124//! ## Parsing Markdown Configuration
125//!
126//! ```ignore
127//! use ricecoder_storage::markdown_config::{MarkdownParser, YamlParser};
128//!
129//! let markdown_content = r#"---
130//! name: example-agent
131//! description: Example agent
132//! model: gpt-4
133//! ---
134//!
135//! # Example Agent
136//! This is the documentation.
137//! "#;
138//!
139//! let parser = MarkdownParser::new();
140//! let parsed = parser.parse(markdown_content)?;
141//!
142//! println!("Frontmatter: {:?}", parsed.frontmatter);
143//! println!("Content: {}", parsed.content);
144//! ```
145//!
146//! ## Validating Configuration
147//!
148//! ```ignore
149//! use ricecoder_storage::markdown_config::{AgentConfig, validate_agent_config};
150//!
151//! let config = AgentConfig {
152//!     name: "code-review".to_string(),
153//!     description: Some("Code review agent".to_string()),
154//!     model: Some("gpt-4".to_string()),
155//!     temperature: Some(0.7),
156//!     max_tokens: Some(2000),
157//!     tools: vec!["syntax-analyzer".to_string()],
158//! };
159//!
160//! validate_agent_config(&config)?;
161//! println!("Configuration is valid!");
162//! ```
163//!
164//! # Error Handling
165//!
166//! The module provides detailed error information for debugging:
167//!
168//! - **Parsing Errors**: Include file path and line numbers
169//! - **Validation Errors**: List all validation failures with field paths
170//! - **Loading Errors**: Indicate which files failed and why
171//!
172//! Invalid configurations are logged but don't prevent loading of other files,
173//! enabling graceful degradation when some configurations are malformed.
174//!
175//! # Configuration File Locations
176//!
177//! ## Project-Level Configuration
178//!
179//! Place configuration files in `projects/ricecoder/.agent/`:
180//!
181//! ```text
182//! projects/ricecoder/
183//! └── .agent/
184//!     ├── examples/
185//!     │   ├── code-review.agent.md
186//!     │   ├── focus.mode.md
187//!     │   └── test.command.md
188//!     ├── custom-agent.agent.md
189//!     └── custom-mode.mode.md
190//! ```
191//!
192//! ## User-Level Configuration
193//!
194//! Place configuration files in `~/.ricecoder/`:
195//!
196//! ```text
197//! ~/.ricecoder/
198//! ├── agents/
199//! │   ├── my-agent.agent.md
200//! │   └── code-review.agent.md
201//! ├── modes/
202//! │   ├── focus.mode.md
203//! │   └── debug.mode.md
204//! └── commands/
205//!     ├── test.command.md
206//!     └── build.command.md
207//! ```
208//!
209//! # Best Practices
210//!
211//! 1. **Use Descriptive Names**: Choose names that clearly indicate purpose
212//! 2. **Document Configurations**: Include markdown documentation explaining usage
213//! 3. **Validate Early**: Use the validation functions to catch errors early
214//! 4. **Organize Files**: Group related configurations in directories
215//! 5. **Version Control**: Commit project-level configurations to version control
216//! 6. **Test Configurations**: Verify configurations work as expected before deploying
217//!
218//! # See Also
219//!
220//! - [`ConfigurationLoader`]: Discovers and loads configuration files
221//! - [`ConfigRegistry`]: Central registry for loaded configurations
222//! - [`FileWatcher`]: Monitors configuration files for changes
223//! - [`MarkdownParser`]: Parses markdown files with YAML frontmatter
224//! - [`YamlParser`]: Parses and validates YAML frontmatter
225
226pub mod error;
227pub mod integration;
228pub mod loader;
229pub mod parser;
230pub mod registry;
231pub mod types;
232pub mod validation;
233pub mod watcher;
234pub mod yaml_parser;
235
236// Re-export commonly used types
237pub use error::MarkdownConfigError;
238pub use integration::{AgentConfigIntegration, CommandConfigIntegration, ModeConfigIntegration};
239pub use loader::{ConfigFile, ConfigFileType, ConfigurationLoader, LoadedConfig};
240pub use parser::MarkdownParser;
241pub use registry::ConfigRegistry;
242pub use types::{AgentConfig, CommandConfig, ModeConfig, ParsedMarkdown, Parameter};
243pub use validation::{validate_agent_config, validate_command_config, validate_mode_config};
244pub use watcher::FileWatcher;
245pub use yaml_parser::YamlParser;