Class: Aidp::PromptOptimization::TemplateFragment

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

Overview

Represents a template fragment

Each template is a complete markdown file that can be included or excluded from prompts based on relevance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, category:, file_path:, content:, tags: []) ⇒ TemplateFragment

Returns a new instance of TemplateFragment.



195
196
197
198
199
200
201
202
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 195

def initialize(id:, name:, category:, file_path:, content:, tags: [])
  @id = id
  @name = name
  @category = category
  @file_path = file_path
  @content = content
  @tags = tags
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



193
194
195
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 193

def category
  @category
end

#contentObject (readonly)

Returns the value of attribute content.



193
194
195
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 193

def content
  @content
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



193
194
195
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 193

def file_path
  @file_path
end

#idObject (readonly)

Returns the value of attribute id.



193
194
195
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 193

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



193
194
195
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 193

def name
  @name
end

#tagsObject (readonly)

Returns the value of attribute tags.



193
194
195
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 193

def tags
  @tags
end

Instance Method Details

#estimated_tokensInteger

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

Returns:

  • (Integer)

    Estimated token count



223
224
225
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 223

def estimated_tokens
  (size / 4.0).ceil
end

#inspectObject



245
246
247
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 245

def inspect
  "#<TemplateFragment id=#{@id} name=\"#{@name}\" category=#{@category} tags=#{@tags}>"
end

#matches_any_tag?(query_tags) ⇒ Boolean

Check if template matches any of the given tags

Parameters:

  • query_tags (Array<String>)

    Tags to match against

Returns:

  • (Boolean)

    True if any tag matches



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

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 template in characters

Returns:

  • (Integer)

    Character count



216
217
218
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 216

def size
  @content.length
end

#summaryHash

Get a summary of the template

Returns:

  • (Hash)

    Template summary



230
231
232
233
234
235
236
237
238
239
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 230

def summary
  {
    id: @id,
    name: @name,
    category: @category,
    tags: @tags,
    size: size,
    estimated_tokens: estimated_tokens
  }
end

#to_sObject



241
242
243
# File 'lib/aidp/prompt_optimization/template_indexer.rb', line 241

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