Class: Aidp::PromptOptimization::Fragment

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

Overview

Represents a fragment of the style guide

Each fragment corresponds to a section or subsection and can be independently selected for inclusion in prompts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, heading:, level:, content:, tags: []) ⇒ Fragment

Returns a new instance of Fragment.



188
189
190
191
192
193
194
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 188

def initialize(id:, heading:, level:, content:, tags: [])
  @id = id
  @heading = heading
  @level = level
  @content = content
  @tags = tags
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



186
187
188
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 186

def content
  @content
end

#headingObject (readonly)

Returns the value of attribute heading.



186
187
188
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 186

def heading
  @heading
end

#idObject (readonly)

Returns the value of attribute id.



186
187
188
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 186

def id
  @id
end

#levelObject (readonly)

Returns the value of attribute level.



186
187
188
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 186

def level
  @level
end

#tagsObject (readonly)

Returns the value of attribute tags.



186
187
188
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 186

def tags
  @tags
end

Instance Method Details

#estimated_tokensInteger

Estimate token count (rough approximation: 1 token ≈ 4 chars)

Returns:

  • (Integer)

    Estimated token count



215
216
217
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 215

def estimated_tokens
  (size / 4.0).ceil
end

#inspectObject



237
238
239
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 237

def inspect
  "#<Fragment id=#{@id} heading=\"#{@heading}\" level=#{@level} tags=#{@tags}>"
end

#matches_any_tag?(query_tags) ⇒ Boolean

Check if fragment matches any of the given tags

Parameters:

  • query_tags (Array<String>)

    Tags to match against

Returns:

  • (Boolean)

    True if any tag matches



200
201
202
203
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 200

def matches_any_tag?(query_tags)
  query_tags = query_tags.map(&:downcase)
  @tags.any? { |tag| query_tags.include?(tag.downcase) }
end

#sizeInteger

Get the size of the fragment in characters

Returns:

  • (Integer)

    Character count



208
209
210
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 208

def size
  @content.length
end

#summaryHash

Get a summary of the fragment

Returns:

  • (Hash)

    Fragment summary



222
223
224
225
226
227
228
229
230
231
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 222

def summary
  {
    id: @id,
    heading: @heading,
    level: @level,
    tags: @tags,
    size: size,
    estimated_tokens: estimated_tokens
  }
end

#to_sObject



233
234
235
# File 'lib/aidp/prompt_optimization/style_guide_indexer.rb', line 233

def to_s
  "Fragment<#{@id}>"
end