Class: Aidp::PromptOptimization::StyleGuideIndexer

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/prompt_optimization/style_guide_indexer.rb

Overview

Indexes LLM_STYLE_GUIDE.md into retrievable fragments

Parses the style guide markdown and creates searchable fragments based on headings and sections. Each fragment can be independently included or excluded from prompts based on relevance.

Examples:

Basic usage

indexer = StyleGuideIndexer.new(project_dir: "/path/to/project")
indexer.index!
fragments = indexer.find_fragments(tags: ["testing", "naming"])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_dir:) ⇒ StyleGuideIndexer

Returns a new instance of StyleGuideIndexer.



18
19
20
21
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 18

def initialize(project_dir:)
  @project_dir = project_dir
  @fragments = []
end

Instance Attribute Details

#fragmentsObject (readonly)

Returns the value of attribute fragments.



16
17
18
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 16

def fragments
  @fragments
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



16
17
18
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 16

def project_dir
  @project_dir
end

Instance Method Details

#all_tagsArray<String>

Get all unique tags from indexed fragments

Returns:

  • (Array<String>)

    List of all tags



64
65
66
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 64

def all_tags
  @fragments.flat_map(&:tags).uniq.sort
end

#find_by_id(id) ⇒ Fragment?

Get fragment by ID

Parameters:

  • id (String)

    Fragment ID (e.g., “naming-structure”)

Returns:

  • (Fragment, nil)

    The fragment or nil if not found



72
73
74
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 72

def find_by_id(id)
  @fragments.find { |f| f.id == id }
end

#find_fragments(tags: nil, heading: nil, min_level: 1, max_level: 6) ⇒ Array<Fragment>

Find fragments matching given criteria

Parameters:

  • tags (Array<String>) (defaults to: nil)

    Tags to match (e.g., [“testing”, “naming”])

  • heading (String) (defaults to: nil)

    Heading pattern to match

  • min_level (Integer) (defaults to: 1)

    Minimum heading level (1-6)

  • max_level (Integer) (defaults to: 6)

    Maximum heading level (1-6)

Returns:

  • (Array<Fragment>)

    Matching fragments



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 46

def find_fragments(tags: nil, heading: nil, min_level: 1, max_level: 6)
  results = @fragments

  if tags && !tags.empty?
    results = results.select { |f| f.matches_any_tag?(tags) }
  end

  if heading
    pattern = Regexp.new(heading, Regexp::IGNORECASE)
    results = results.select { |f| f.heading =~ pattern }
  end

  results.select { |f| f.level.between?(min_level, max_level) }
end

#index!Array<Fragment>

Index the style guide file

Parses the LLM_STYLE_GUIDE.md and extracts fragments organized by sections and headings

Returns:

  • (Array<Fragment>)

    List of indexed fragments



29
30
31
32
33
34
35
36
37
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 29

def index!
  @fragments = []
  content = read_style_guide

  return @fragments if content.nil? || content.empty?

  parse_fragments(content)
  @fragments
end