A context generator for Rust repositories that creates a structured markdown file with relevant information for LLMs and AI agents.
Choose your preferred way to use AI Context Generator:
| Usage Mode | When to Use | Quick Start |
|---|---|---|
| π§ CLI Tool | Interactive use, one-time analysis, scripts | ai-context-gen --path ./my-project |
| π Rust Library | Integrate into Rust apps, custom workflows | cargo add ai-context-gen |
# Install globally
git clone https://github.com/brbtavares/ai-context-gen
cd ai-context-gen && make install
# Use anywhere
ai-context-gen --path /path/to/project# Cargo.toml
[dependencies]
ai-context-gen = "0.1.2"use ai_context_gen::generate_context;
use std::path::PathBuf;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
generate_context(PathBuf::from("."), "context.md".to_string()).await?;
Ok(())
}- π Complete Scanning: Analyzes all
.rsand.mdfiles in the repository - π³ Abstract Syntax Tree: Extracts and documents structures, functions, enums and implementations
- π Token Control: Respects token limits and prioritizes important content
- π Project Structure: Generates file tree visualization
- π Documentation: Includes markdown files like README, documentation, etc.
- β‘ Performance: Asynchronous and optimized processing
The AI Context Generator CLI is perfect for interactive use, one-time analysis, and shell scripts.
- Rust 1.76 or higher
- Linux system (tested on Ubuntu/Debian)
# Clone the repository
git clone https://github.com/brbtavares/ai-context-gen
cd ai-context-gen
# Build and install globally (recommended)
make install
# Alternative: step by step
make build
sudo cp target/release/ai-context-gen /usr/local/bin/# Check if installed correctly
ai-context-gen --version
ai-context-gen --help
# Should work from any directory
cd /tmp && ai-context-gen --path ~/my-project# Development & Testing
make dev # Build and run in development mode
make demo # Run demo with current directory
make test # Run tests
make check # Run format, lint and tests
# Build & Installation
make build # Build using script (recommended)
make install # Install on system
make uninstall # Remove from system
# Utilities
make clean # Clean build artifacts
make help-make # Show all make commands# Analyze current directory (interactive mode)
ai-context-gen
# Analyze specific directory
ai-context-gen --path /path/to/project
# Custom output file
ai-context-gen --output my_context.md
# High token limit for large projects
ai-context-gen --max-tokens 100000ai-context-gen [OPTIONS]
Options:
-p, --path <PATH> Path to repository (default: current directory)
-m, --max-tokens <MAX_TOKENS> Maximum number of tokens (default: 50000)
-o, --output <OUTPUT> Output file name (default: repo_context.md)
--include-hidden Include hidden files and directories
--include-deps Include external dependencies analysis
-h, --help Print help
-V, --version Print version# Complete analysis with all options
ai-context-gen --path ~/my-rust-project --max-tokens 200000 --output complete_analysis.md --include-hidden
# Quick summary
ai-context-gen --max-tokens 10000 --output summary.md
# Analyze remote/different project
ai-context-gen --path /opt/some-project --output /tmp/analysis.mdThe AI Context Generator library is perfect for integrating context generation into your Rust applications.
Add to your Cargo.toml:
[dependencies]
ai-context-gen = "0.1.2"use ai_context_gen::generate_context;
use std::path::PathBuf;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Generate context for current directory
generate_context(PathBuf::from("."), "context.md".to_string()).await?;
println!("Context generated in context.md");
Ok(())
}use ai_context_gen::{Config, ContextGenerator, RepositoryScanner};
use std::path::PathBuf;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Custom configuration
let config = Config {
repo_path: PathBuf::from("./my-project"),
max_tokens: 100000,
output_file: "detailed_context.md".to_string(),
include_hidden: true,
include_deps: true,
};
// Two-step process for more control
let scanner = RepositoryScanner::new(config.clone());
let scan_result = scanner.scan().await?;
println!("Files found: {}", scan_result.files.len());
let generator = ContextGenerator::new(config);
generator.generate_context(scan_result).await?;
println!("Context generated successfully!");
Ok(())
}use ai_context_gen::{Config, generate_context_with_config};
use std::path::PathBuf;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = Config {
repo_path: PathBuf::from("/path/to/analyze"),
max_tokens: 75000,
output_file: "custom_context.md".to_string(),
include_hidden: false,
include_deps: true,
};
generate_context_with_config(config).await?;
Ok(())
}generate_context(path, output): Simple function for basic casesgenerate_context_with_config(config): Function with custom configurationConfig: Configuration structureRepositoryScanner: File scanning and analysisContextGenerator: Context generation with prioritiesRustParser: Rust code AST parser
The generated file contains the following sections (in priority order):
- Project name and description
- Rust version
- Main dependencies
- Project statistics
- File tree
- Directory organization
- README.md
- Other .md files found
- Project documentation
- Structures (structs)
- Enumerations (enums)
- Functions
- Implementations (impls)
- Modules
- Code documentation
- Complete content of .rs files
- Syntax highlighting for markdown
The system uses an intelligent prioritization algorithm:
- High Priority (9): Metadata, structure and documentation
- Medium Priority (5): AST analysis and code architecture
- Low Priority (1): Complete source code
When the token limit is reached, the system:
- Includes high priority sections first
- Truncates low priority sections if necessary
- Reports which sections were truncated
The system automatically ignores:
Directories:
target/node_modules/.git/.vscode/.idea/
Files:
Cargo.lock.gitignore.DS_Store
Uses the GPT-4 tokenizer for precise token counting, ensuring compatibility with:
- OpenAI GPT-4
- Claude
- Other models based on similar tokens
- Automatic project documentation
- Onboarding new team members
- Code architecture analysis
- Structured context for code assistants
- Analysis of existing projects
- Documentation generation
- Automated code review
- Project wiki generation
- Architecture reports
- Technical documentation
- Supports only Rust projects
- Analyzes only
.rsand.mdfiles - Requires Linux system for execution
- Token limit may truncate content
Contributions are welcome! Please:
- Fork the project
- Create a branch for your feature
- Implement your changes
- Add tests if necessary
- Open a Pull Request
git clone https://github.com/brbtavares/ai-context-gen
cd ai-context-gen
cargo build
cargo testFor maintainers, releases are automated. See RELEASE.md for details.
# Update version and changelog, then:
git tag v0.1.2
git push origin v0.1.2
# GitHub Actions handles the rest!This project is licensed under the MIT license. See the LICENSE file for details.
- Web interface
- Git integration
- Commit history analysis
- Support for other output formats (JSON, YAML)
- Cache for better performance
- π§ MSRV Compatibility: Updated MSRV to Rust 1.76 to support Cargo lock file version 4
- π Workflow Improvements: Modernized GitHub Actions workflows with updated actions
- π οΈ CI Fixes: Fixed MSRV check to handle newer lock file formats correctly
- π¦ Release Automation: Enhanced release workflow with better asset handling
- π Better Error Handling: Improved release workflow with proper secret handling
- β¨ Documentation Improvements: Completely restructured README.md and lib.rs documentation
- π§ Clear CLI/Library Separation: Added clear distinction between CLI and library usage
- π Enhanced Library Examples: Added multiple usage patterns and integration examples
- π― Quick Start Guide: Added comparison table and clear guidance on when to use each mode
- π οΈ Better Error Handling: Improved CLI output messages and error reporting
- π API Documentation: Enhanced rustdoc comments with comprehensive examples
- π§Ή Code Organization: Improved module structure and exports
- Initial implementation
- Support for Rust AST analysis
- Content prioritization system
- Token counting with tiktoken
- Structured markdown file generation
# Check if /usr/local/bin is in your PATH
echo $PATH | grep -o '/usr/local/bin'
# If not found, add to your shell profile
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
# Verify installation
which ai-context-gen
ai-context-gen --version# Make sure you have sudo privileges
sudo make install
# Or install manually
make build
sudo cp target/release/ai-context-gen /usr/local/bin/
sudo chmod +x /usr/local/bin/ai-context-gen# Remove old installations
rm -f ~/.local/bin/ai-context-gen
sudo rm -f /usr/local/bin/ai-context-gen
# Reinstall fresh
make clean
make installMake sure you're using #[tokio::main] or initializing a runtime:
// Option 1: Use tokio::main
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// your code here
}
// Option 2: Manual runtime
fn main() -> anyhow::Result<()> {
let rt = tokio::runtime::Runtime::new()?;
rt.block_on(async {
// your async code here
})
}use ai_context_gen::{Config, generate_context_with_config};
use std::path::PathBuf;
// Make sure output directory is writable
let config = Config {
repo_path: PathBuf::from("./my-project"),
output_file: "/tmp/context.md".to_string(), // Use temp dir if needed
// ... other config
};# Use higher token limits for large projects
ai-context-gen --path ./large-project --max-tokens 200000
# Or focus on specific parts
ai-context-gen --path ./large-project/src --max-tokens 50000# Include hidden files
ai-context-gen --include-hidden
# For library usage, modify Config
let config = Config {
include_hidden: true,
include_deps: true,
// ...
};