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.



234
235
236
237
238
239
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 234

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.



232
233
234
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 232

def composition_result
  @composition_result
end

#contentObject (readonly)

Returns the value of attribute content.



232
233
234
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 232

def content
  @content
end

#metadataObject (readonly)

Returns the value of attribute metadata.



232
233
234
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 232

def 
  @metadata
end

#task_contextObject (readonly)

Returns the value of attribute task_context.



232
233
234
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 232

def task_context
  @task_context
end

Instance Method Details

#estimated_tokensInteger

Estimate token count for the prompt

Returns:

  • (Integer)

    Estimated tokens



251
252
253
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 251

def estimated_tokens
  (size / 4.0).ceil
end

#selection_reportString

Get fragment selection report

Returns:

  • (String)

    Human-readable report



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 266

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



244
245
246
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 244

def size
  @content.length
end

#to_sObject



304
305
306
# File 'lib/aidp/prompt_optimization/prompt_builder.rb', line 304

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



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

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