Class: Aidp::PromptOptimization::Optimizer

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

Overview

Main coordinator for prompt optimization

Orchestrates all components to produce an optimized prompt:

  1. Index style guide, templates, and source code
  2. Score fragments based on task context
  3. Select optimal fragments within token budget
  4. Build final prompt markdown

Examples:

Basic usage

optimizer = Optimizer.new(project_dir: "/project", config: config)
result = optimizer.optimize_prompt(
  task_type: :feature,
  description: "Add user auth",
  affected_files: ["lib/user.rb"],
  step_name: "implementation"
)
result.write_to_file("PROMPT.md")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_dir:, config: nil) ⇒ Optimizer



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 33

def initialize(project_dir:, config: nil)
  @project_dir = project_dir
  @config = config || default_config
  @stats = OptimizerStats.new

  # Initialize indexers (will cache results)
  @style_guide_indexer = nil
  @template_indexer = nil
  @fragmenter = nil
  @project_knowledge_indexer = nil
  @scorer = nil
  @composer = nil
  @builder = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



31
32
33
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 31

def config
  @config
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



31
32
33
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 31

def project_dir
  @project_dir
end

#statsObject (readonly)

Returns the value of attribute stats.



31
32
33
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 31

def stats
  @stats
end

Instance Method Details

#clear_cacheObject

Clear cached indexes (useful for testing or when files change)



101
102
103
104
105
106
107
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 101

def clear_cache
  @style_guide_indexer = nil
  @template_indexer = nil
  @fragmenter = nil
  @project_knowledge_indexer = nil
  @stats.reset!
end

#optimize_prompt(task_type: nil, description: nil, affected_files: [], step_name: nil, tags: [], options: {}) ⇒ PromptOutput

Optimize prompt for given task context

Options Hash (options:):

  • :include_metadata (Boolean)

    Include optimization metadata

  • :max_tokens (Integer)

    Override default token budget



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 59

def optimize_prompt(task_type: nil, description: nil, affected_files: [], step_name: nil, tags: [], options: {})
  start_time = Time.now

  # Build task context
  task_context = TaskContext.new(
    task_type: task_type,
    description: description,
    affected_files: affected_files,
    step_name: step_name,
    tags: tags
  )

  # Index all fragments
  all_fragments = index_all_fragments(affected_files)
  @stats.record_fragments_indexed(all_fragments.count)

  # Score fragments
  scored_fragments = score_fragments(all_fragments, task_context)
  @stats.record_fragments_scored(scored_fragments.count)

  # Select fragments within budget
  max_tokens = options[:max_tokens] || @config[:max_tokens]
  thresholds = @config[:include_threshold]
  composition_result = compose_context(scored_fragments, max_tokens, thresholds)

  @stats.record_fragments_selected(composition_result.selected_count)
  @stats.record_fragments_excluded(composition_result.excluded_count)
  @stats.record_tokens_used(composition_result.total_tokens)
  @stats.record_budget_utilization(composition_result.budget_utilization)

  # Build final prompt
  prompt_output = build_prompt(task_context, composition_result, options)

  elapsed = Time.now - start_time
  @stats.record_optimization_time(elapsed)

  log_optimization_result(prompt_output) if @config[:log_selected_fragments]

  prompt_output
end

#statisticsHash

Get optimization statistics



112
113
114
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 112

def statistics
  @stats.summary
end