Crate ai_context_gen

Source
Expand description

§AI Context Generator - Rust Library

A Rust library for generating structured context from code repositories, specifically designed for LLMs and AI agents. This library provides both simple convenience functions and advanced APIs for fine-grained control.

§When to Use This Library

  • Integrate context generation into your Rust applications
  • Build custom analysis workflows
  • Create automated documentation systems
  • Develop AI-powered developer tools

For standalone command-line usage, consider using the CLI tool instead.

§Features

  • 🔍 Complete Scanning: Analyzes all .rs and .md files in repositories
  • 🌳 AST Analysis: Extracts structures, functions, enums and implementations
  • 📊 Token Control: Respects limits and prioritizes important content
  • 📁 Project Structure: Generates file tree visualizations
  • 📖 Documentation: Includes markdown files and code documentation
  • Async Processing: Non-blocking, high-performance analysis

§Quick Start

§Simple Usage

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 successfully!");
    Ok(())
}

§Advanced Usage with Configuration

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,
    };

    // Step-by-step process for more control
    let scanner = RepositoryScanner::new(config.clone());
    let scan_result = scanner.scan().await?;
     
    println!("Found {} files", scan_result.files.len());
     
    let generator = ContextGenerator::new(config);
    generator.generate_context(scan_result).await?;
     
    Ok(())
}

§Using the Configuration Function

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: "analysis.md".to_string(),
        include_hidden: false,
        include_deps: true,
    };

    generate_context_with_config(config).await?;
    Ok(())
}

§API Overview

§Integration Patterns

§Web Applications

use ai_context_gen::{Config, generate_context_with_config};
use std::path::PathBuf;

async fn analyze_repo_endpoint(repo_path: String) -> Result<String, Box<dyn std::error::Error>> {
    let config = Config {
        repo_path: PathBuf::from(repo_path),
        max_tokens: 50000,
        output_file: format!("/tmp/analysis_{}.md", chrono::Utc::now().timestamp()),
        include_hidden: false,
        include_deps: false,
    };
     
    generate_context_with_config(config.clone()).await?;
    Ok(config.output_file)
}

§Custom Workflows

use ai_context_gen::{Config, RepositoryScanner, ContextGenerator};
use std::path::PathBuf;

async fn custom_analysis_workflow(repo_path: PathBuf) -> anyhow::Result<()> {
    let config = Config {
        repo_path: repo_path.clone(),
        max_tokens: 100000,
        output_file: "temp_analysis.md".to_string(),
        include_hidden: true,
        include_deps: true,
    };

    // Scan first
    let scanner = RepositoryScanner::new(config.clone());
    let scan_result = scanner.scan().await?;
     
    // Custom filtering or processing here
    println!("Found {} Rust files", scan_result.files.iter()
        .filter(|f| matches!(f.file_type, ai_context_gen::FileType::Rust))
        .count());
     
    // Generate context
    let generator = ContextGenerator::new(config);
    generator.generate_context(scan_result).await?;
     
    Ok(())
}

Re-exports§

pub use config::Config;
pub use generator::ContextGenerator;
pub use parser::EnumInfo;
pub use parser::FunctionInfo;
pub use parser::ImplInfo;
pub use parser::RustAnalysis;
pub use parser::RustParser;
pub use parser::StructInfo;
pub use scanner::FileInfo;
pub use scanner::FileType;
pub use scanner::RepositoryScanner;
pub use scanner::ScanResult;
pub use token_counter::ContentPrioritizer;
pub use token_counter::ContentSection;
pub use token_counter::TokenCounter;

Modules§

config
Configuration module for the AI Context Generator.
generator
Context generation module for the AI Context Generator.
parser
Rust AST parsing module for the AI Context Generator.
scanner
Repository scanning module for the AI Context Generator.
token_counter
Token counting and content prioritization module.

Functions§

generate_context
Generates repository context with default configuration
generate_context_with_config
Generates repository context with custom configuration

Type Aliases§

Result
Default Result type used by the library