Class: Aidp::PromptOptimization::PromptOutput

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

Overview

Output of prompt building

Contains the built prompt content along with metadata about the composition and selection process

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, composition_result:, task_context:, metadata:) ⇒ PromptOutput

Returns a new instance of PromptOutput.



256
257
258
259
260
261
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 256

def initialize(content:, composition_result:, task_context:, metadata:)
  @content = content
  @composition_result = composition_result
  @task_context = task_context
  @metadata = 
end

Instance Attribute Details

#composition_resultObject (readonly)

Returns the value of attribute composition_result.



254
255
256
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 254

def composition_result
  @composition_result
end

#contentObject (readonly)

Returns the value of attribute content.



254
255
256
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 254

def content
  @content
end

#metadataObject (readonly)

Returns the value of attribute metadata.



254
255
256
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 254

def 
  @metadata
end

#task_contextObject (readonly)

Returns the value of attribute task_context.



254
255
256
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 254

def task_context
  @task_context
end

Instance Method Details

#estimated_tokensInteger

Estimate token count for the prompt

Returns:

  • (Integer)

    Estimated tokens



273
274
275
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 273

def estimated_tokens
  (size / 4.0).ceil
end

#selection_reportString

Get fragment selection report

Returns:

  • (String)

    Human-readable report



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 288

def selection_report
  lines = ["# Prompt Optimization Report"]
  lines << ""
  lines << "Generated at: #{@metadata[:timestamp]}"
  lines << ""

  lines << "## Task Context"
  lines << "- Type: #{@task_context.task_type || "N/A"}"
  lines << "- Step: #{@task_context.step_name || "N/A"}"
  if @task_context.affected_files && !@task_context.affected_files.empty?
    lines << "- Affected Files: #{@task_context.affected_files.join(", ")}"
  end
  lines << ""

  lines << "## Composition Statistics"
  lines << "- Selected: #{@metadata[:selected_count]} fragments"
  lines << "- Excluded: #{@metadata[:excluded_count]} fragments"
  lines << "- Tokens: #{@metadata[:total_tokens]} / #{@metadata[:budget]} (#{@metadata[:utilization]}%)"
  lines << "- Avg Score: #{(@metadata[:average_score] * 100).round}%"
  lines << ""

  lines << "## Selected Fragments"
  @composition_result.selected_fragments.each do |item|
    fragment = item[:fragment]
    score = item[:score]

    if fragment.respond_to?(:heading)
      lines << "- #{fragment.heading} (#{(score * 100).round}%)"
    elsif fragment.respond_to?(:name)
      lines << "- #{fragment.name} (#{(score * 100).round}%)"
    elsif fragment.respond_to?(:id)
      lines << "- #{fragment.id} (#{(score * 100).round}%)"
    end
  end

  lines.join("\n")
end

#sizeInteger

Get content length in characters

Returns:

  • (Integer)

    Character count



266
267
268
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 266

def size
  @content.length
end

#to_sObject



326
327
328
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 326

def to_s
  "PromptOutput<#{estimated_tokens} tokens, #{@composition_result.selected_count} fragments>"
end

#write_to_file(file_path) ⇒ Object

Write prompt to file

Parameters:

  • file_path (String)

    Path to write prompt



280
281
282
283
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 280

def write_to_file(file_path)
  File.write(file_path, @content)
  Aidp.log_info("prompt_builder", "Wrote optimized prompt", path: file_path, tokens: estimated_tokens)
end