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

Returns a new instance of Optimizer.



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

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
  @scorer = nil
  @composer = nil
  @builder = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



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

def project_dir
  @project_dir
end

#statsObject (readonly)

Returns the value of attribute stats.



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

def stats
  @stats
end

Instance Method Details

#clear_cacheObject

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



99
100
101
102
103
104
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 99

def clear_cache
  @style_guide_indexer = nil
  @template_indexer = nil
  @fragmenter = 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

Parameters:

  • task_type (Symbol) (defaults to: nil)

    Type of task (:feature, :bugfix, etc.)

  • description (String) (defaults to: nil)

    Task description

  • affected_files (Array<String>) (defaults to: [])

    Files being modified

  • step_name (String) (defaults to: nil)

    Current work loop step

  • tags (Array<String>) (defaults to: [])

    Additional context tags

  • options (Hash) (defaults to: {})

    Additional options

Options Hash (options:):

  • :include_metadata (Boolean)

    Include optimization metadata

  • :max_tokens (Integer)

    Override default token budget

Returns:



57
58
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
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 57

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

Returns:

  • (Hash)

    Statistics about optimization runs



109
110
111
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 109

def statistics
  @stats.summary
end