Class: Aidp::PromptOptimization::TemplateIndexer

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

Overview

Indexes step templates into retrievable fragments

Parses template markdown files from the templates/ directory and creates searchable fragments based on template category, content, and keywords.

Examples:

Basic usage

indexer = TemplateIndexer.new(project_dir: "/path/to/project")
indexer.index!
fragments = indexer.find_templates(category: "analysis", tags: ["testing"])

Constant Summary collapse

CATEGORIES =

Template categories based on directory structure

%w[analysis planning implementation].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_dir:) ⇒ TemplateIndexer

Returns a new instance of TemplateIndexer.



21
22
23
24
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 21

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

Instance Attribute Details

#project_dirObject (readonly)

Returns the value of attribute project_dir.



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

def project_dir
  @project_dir
end

#templatesObject (readonly)

Returns the value of attribute templates.



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

def templates
  @templates
end

Instance Method Details

#all_tagsArray<String>

Get all unique tags from indexed templates

Returns:

  • (Array<String>)

    List of all tags



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

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

#categoriesArray<String>

Get all categories

Returns:

  • (Array<String>)

    List of categories



79
80
81
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 79

def categories
  @templates.map(&:category).uniq.sort
end

#find_by_id(id) ⇒ TemplateFragment?

Get template by ID

Parameters:

  • id (String)

    Template ID

Returns:



87
88
89
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 87

def find_by_id(id)
  @templates.find { |t| t.id == id }
end

#find_templates(category: nil, tags: nil, name: nil) ⇒ Array<TemplateFragment>

Find templates matching given criteria

Parameters:

  • category (String, nil) (defaults to: nil)

    Category to filter by (e.g., “analysis”, “planning”)

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

    Tags to match

  • name (String, nil) (defaults to: nil)

    Template name pattern to match

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 50

def find_templates(category: nil, tags: nil, name: nil)
  results = @templates

  if category
    results = results.select { |t| t.category == category }
  end

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

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

  results
end

#index!Array<TemplateFragment>

Index all template files

Scans the templates/ directory and indexes all markdown templates

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 31

def index!
  @templates = []

  CATEGORIES.each do |category|
    category_dir = File.join(@project_dir, "templates", category)
    next unless Dir.exist?(category_dir)

    index_category(category, category_dir)
  end

  @templates
end