Class: Aidp::PromptOptimization::ContextComposer

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

Overview

Composes optimal context from scored fragments within token budget

Selects the best combination of fragments that fits within the token budget while maximizing relevance and coverage.

Algorithm:

  1. Sort fragments by relevance score

  2. Always include critical fragments (score > 0.9)

  3. Fill remaining budget with highest-scoring fragments

  4. Deduplicate overlapping content

  5. Return selected fragments with statistics

Examples:

Basic usage

composer = ContextComposer.new(max_tokens: 8000)
selection = composer.compose(scored_fragments, thresholds: {...})

Constant Summary collapse

CRITICAL_SCORE_THRESHOLD =

Thresholds for different fragment types

0.9
MINIMUM_SCORE_THRESHOLD =
0.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_tokens: 16000) ⇒ ContextComposer

Returns a new instance of ContextComposer.



27
28
29
# File 'lib/aidp/prompt_optimization/context_composer.rb', line 27

def initialize(max_tokens: 16000)
  @max_tokens = max_tokens
end

Instance Attribute Details

#max_tokensObject (readonly)

Returns the value of attribute max_tokens.



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

def max_tokens
  @max_tokens
end

Instance Method Details

#compose(scored_fragments, thresholds: {}, reserved_tokens: 2000) ⇒ CompositionResult

Compose optimal context from scored fragments

Parameters:

  • scored_fragments (Array<Hash>)

    List of score:, breakdown:

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

    Type-specific thresholds :templates, :source

  • reserved_tokens (Integer) (defaults to: 2000)

    Tokens to reserve for task description, etc.

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aidp/prompt_optimization/context_composer.rb', line 37

def compose(scored_fragments, thresholds: {}, reserved_tokens: 2000)
  available_budget = @max_tokens - reserved_tokens

  # Separate fragments by type for threshold checking
  categorized = categorize_fragments(scored_fragments, thresholds)

  # Start with critical fragments (always included)
  selected = select_critical_fragments(categorized[:critical])
  used_tokens = calculate_total_tokens(selected)

  # Add high-priority fragments within budget
  selected, used_tokens = add_fragments_within_budget(
    selected,
    categorized[:high_priority],
    available_budget - used_tokens
  )

  # Fill remaining budget with other fragments if space allows
  selected, used_tokens = add_fragments_within_budget(
    selected,
    categorized[:medium_priority],
    available_budget - used_tokens
  )

  # Deduplicate if requested
  selected = deduplicate_fragments(selected) if categorized[:needs_dedup]

  CompositionResult.new(
    selected_fragments: selected,
    total_tokens: used_tokens,
    budget: available_budget,
    excluded_count: scored_fragments.length - selected.length,
    average_score: calculate_average_score(selected)
  )
end