yek

repository·main·Indexed 25 days ago

https://github.com/mohsen1/yek

A high-performance tool (v0.25.5) designed to serialize repository file contents into text chunks, optimized for LLM context preparation. It features intelligent prioritization using Git history and file categories, size management via byte or token limits, and a modular Rust-based architecture that allows for custom ProcessingStage implementations.

Tokens
8.4K
Snippets
20
Records
49
Agent score
82%

What's inside yek

  1. Understand the Yek Core Architecture

    main

    Yek is designed with a modular architecture that separates concerns into distinct layers. The core components include:

    • Domain Models: Data structures like ProcessedFile and RepositoryInfo that represent the state of files and repositories.
    • Repository Pattern: Abstractions (FileSystem and GitOperations traits) that decouple the logic from the actual file system and Git implementations.
    • Processing Pipeline: A middleware-based system where files pass through various ProcessingStage implementations (e.g., discovery, filtering, and formatting).
    • Configuration Architecture: A split configuration model consisting of InputConfig, OutputConfig, and ProcessingConfig to manage different aspects of the tool's behavior.
  2. Optimize Yek for memory-constrained environments

    main

    If running Yek in environments with limited RAM, use the following strategies:

    1. Enable streaming mode: Avoids loading the entire file set into memory.
    2. Use smaller batch sizes: Reduces the amount of data processed in a single step.
    3. Enable lazy token counting: Prevents unnecessary computation of token counts until they are actually required.
    4. Set memory limits: Explicitly define limits to prevent system instability.
  3. Install yek

    main

    You can install yek using a shell script for Unix-like systems or PowerShell for Windows.

    Unix-like Systems (macOS, Linux)

    curl -fsSL https://azimi.me/yek.sh | bash

    Windows (PowerShell)

    irm https://azimi.me/yek.ps1 | iex

    Build from Source

    If you prefer to build from source using Cargo:

    git clone https://github.com/mohsen1/yek
    cd yek
    cargo install --path .
    curl -fsSL https://azimi.me/yek.sh | bash
  4. Optimize Yek for large repositories

    main

    When processing very large repositories, follow these performance guidelines to ensure efficiency and stability:

    1. Use token mode: Provides more accurate size management for LLM contexts.
    2. Enable parallel processing: Maximizes CPU utilization.
    3. Set memory limits: Use memory_limit_mb to prevent Out-Of-Memory (OOM) errors.
    4. Use streaming output: Reduces memory pressure for very large outputs.
    5. Optimize ignore patterns: Reduces the overhead of file system traversal.
  5. Integrate Yek as a library

    main

    To use Yek within your own Rust application, you can manually construct a ProcessingContext and run it through a ProcessingPipeline. This gives you programmatic control over the input, output, and processing configurations.

    use yek::{models::ProcessedFile, pipeline::ProcessingPipeline, error::YekResult};
    
    // Create processing context
    let context = ProcessingContext::new(
        input_config,
        output_config,
        processing_config,
        repository_info,
        Arc::new(RealFileSystem),
        git_operations,
    );
    
    // Use the pipeline
    let pipeline = ProcessingPipeline::new(context);
    let files = pipeline.process()?;
  6. Enable debug mode for detailed logging

    main

    To get detailed information about the file discovery process, processing stage timing, memory usage statistics, cache hit rates, and error details, run Yek with the --debug flag.

    yek --debug --input src/ --max-size 10MB
  7. Use yek to serialize files for LLM consumption

    main

    yek serializes text-based files in a directory into a single output format optimized for LLMs. By default, it respects .gitignore, uses Git history to prioritize important files (placing them later in the output so LLMs pay more attention), and automatically detects if output is being piped to a stream.

    Basic Usage

    Run yek in a directory to serialize the entire repository into a temporary file. The path to the file will be printed to the console.

    yek

    Selecting Specific Files or Directories

    You can pass specific paths or use glob patterns (ensure you quote glob patterns to prevent shell expansion).

    # Process specific directories
    yek src/ tests/
    
    # Process specific files
    yek file1.txt file2.txt
    
    # Use glob patterns
    yek "src/**/*.ts"
    yek "src/main.rs" "tests/*.rs"

    Controlling Output Size

    You can cap the output size using bytes or tokens. If the limit is reached, yek will remove files, prioritizing keeping the most important ones.

    # Cap by tokens
    yek --tokens 128k
    
    # Cap by size and specify output directory
    yek --max-size 100KB --output-dir /tmp/yek src/
    yek
    
    yek src/ | pbcopy
    
    yek --tokens 128k
    
    yek --max-size 100KB --output-dir /tmp/yek src/
    
    yek "src/**/*.ts"
  8. How file categorization works

    main

    The categorize_file function uses heuristics based on file paths and extensions to assign a FileCategory. The categorization follows a specific order of precedence to ensure accuracy:

    1. Test Files: Checked first via directory patterns (e.g., /tests/, /spec/) or naming conventions (e.g., .test.js, test_*.py).
    2. Configuration Files: Checked via extensions (e.g., .toml, .yaml, .json) or specific filenames (e.g., package.json, Cargo.toml, .gitignore).
    3. Documentation Files: Checked via extensions (e.g., .md, .rst, .txt) or common names (e.g., README, CHANGELOG, LICENSE).
    4. Source Files: Checked via common programming language extensions (e.g., .rs, .py, .js, .cpp) or directory patterns (e.g., /src/, /lib/).
    5. Other: If no patterns match, the file is categorized as Other.
  9. Understand FilePriority calculation

    main

    File priority is determined by combining rule-based priority and Git-based boosts. The FilePriority struct calculates a combined score which is used to order files.

    • rule_priority: The base priority assigned by user-defined rules.
    • git_boost: A boost value derived from Git history (e.g., recency of commits).
    • combined: The sum of rule_priority and git_boost.
  10. Understand File Categories and Default Priorities

    main

    yek categorizes files into five distinct types to determine their importance during processing. Each category has a default priority offset used for sorting. Higher offsets indicate higher priority.

    Categories and Default Offsets:

    • Source: Main application or library source code (Offset: 20)
    • Documentation: Documentation files like markdown or rst (Offset: 15)
    • Test: Test files and testing related code (Offset: 10)
    • Configuration: Configuration files like yaml, toml, or json (Offset: 5)
    • Other: Files that do not fit into the above categories (Offset: 1)
  11. Configure yek using a configuration file

    main

    You can use a yek.yaml, yek.toml, or yek.json file at your project root to persist settings. This is useful for defining custom ignore patterns, priority rules, and output templates.

    Configurable Options

    File Processing:

    • max_size: Size limit (e.g., "10MB", "128K")
    • tokens: Token count limit (e.g., "128k", "100")
    • ignore_patterns: Additional patterns to ignore
    • unignore_patterns: Override built-in ignores
    • binary_extensions: Additional binary file extensions to ignore
    • priority_rules: Custom rules for processing order
    • git_boost_max: Max score boost from Git history

    Output Configuration:

    • json: Boolean for JSON output
    • debug: Boolean for debug mode
    • line_numbers: Boolean to include line numbers
    • output_dir: Directory for output files
    • output_name: Filename for output
    • output_template: Template string using FILE_PATH and FILE_CONTENT placeholders
    • tree_header: Boolean to include directory tree header
    • tree_only: Boolean to show only the directory tree

    Example yek.yaml

    ignore_patterns:
      - "ai-prompts/**"
      - "__generated__/**"
    
    git_boost_max: 50
    
    priority_rules:
      - score: 100
        pattern: "^src/lib/"
      - score: 90
        pattern: "^src/"
    
    binary_extensions:
      - ".blend"
      - ".fbx"
    
    max_size: "128K"
    json: false
    debug: false
    line_numbers: false
    tree_header: false
    output_dir: /tmp/yek
    output_name: yek-output.txt
    output_template: "FILE_PATH\n\nFILE_CONTENT"
    ignore_patterns:
      - "ai-prompts/**"
      - "__generated__/**"
    
    git_boost_max: 50
    
    priority_rules:
      - score: 100
        pattern: "^src/lib/"
      - score: 90
        pattern: "^src/"
      - score: 80
        pattern: "^docs/"
    
    binary_extensions:
      - ".blend"
      - ".fbx"
      - ".max"
      - ".psd"
    
    max_size: "128K"
    json: false
    debug: false
    line_numbers: false
    tree_header: false
    output_dir: /tmp/yek
    output_name: yek-output.txt
    output_template: "FILE_PATH\n\nFILE_CONTENT"
  12. Run yek to serialize a repository

    main

    The yek CLI tool serializes a repository into a single output file (either .txt or .json). It can operate in two modes:

    1. Streaming Mode: Skips checksum calculation and processes files sequentially. Use this when stream is enabled in your configuration.
    2. Standard Mode: Runs repository serialization and checksum calculation in parallel. This mode requires an output_dir to be specified if an output_name is not provided, as it generates a filename based on the content checksum (e.g., yek-output-<checksum>.json).

    When an output_name is provided, the tool writes the output to that specific name (optionally within an output_dir). If no output_name is provided in standard mode, the tool automatically generates a unique filename using a checksum of the input paths.