Class: Legion::Extensions::Agentic::Attention::Lens::Helpers::LensEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb

Constant Summary

Constants included from Constants

Constants::CLARITY_BLURRY_THRESHOLD, Constants::CLARITY_LABELS, Constants::CLARITY_SHARP_THRESHOLD, Constants::CLEAN_BOOST_DEFAULT, Constants::DISTORTION_TYPES, Constants::LENS_DEFAULTS, Constants::LENS_TYPES, Constants::MAGNIFICATION_LABELS, Constants::MAX_LENSES, Constants::SMUDGE_RATE_DEFAULT, Constants::STACK_CLARITY_DECAY, Constants::STACK_DISTORTION_BLEND, Constants::STACK_MAGNIFICATION_EXPONENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLensEngine

Returns a new instance of LensEngine.



14
15
16
17
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 14

def initialize
  @lenses = {}
  @stacks = {}
end

Instance Attribute Details

#lensesObject (readonly)

Returns the value of attribute lenses.



12
13
14
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 12

def lenses
  @lenses
end

#stacksObject (readonly)

Returns the value of attribute stacks.



12
13
14
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 12

def stacks
  @stacks
end

Instance Method Details

#clearest_lenses(limit: 3) ⇒ Object



66
67
68
69
70
71
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 66

def clearest_lenses(limit: 3)
  @lenses.values
         .sort_by { |l| -l.clarity }
         .first(limit)
         .map(&:to_h)
end

#create_lens(lens_type:, magnification: nil, clarity: 1.0, distortion: nil, aperture: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 19

def create_lens(lens_type:, magnification: nil, clarity: 1.0, distortion: nil, aperture: nil, **)
  lens = Lens.new(
    lens_type:     lens_type,
    magnification: magnification,
    clarity:       clarity,
    distortion:    distortion,
    aperture:      aperture
  )
  @lenses[lens.id] = lens
  lens
end

#degrade_all!(rate: Constants::SMUDGE_RATE_DEFAULT) ⇒ Object



61
62
63
64
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 61

def degrade_all!(rate: Constants::SMUDGE_RATE_DEFAULT)
  @lenses.each_value { |l| l.smudge!(rate) }
  { degraded: @lenses.size, rate: rate }
end

#lens_reportObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 80

def lens_report
  return { lens_count: 0, stack_count: 0, lenses: [], stacks: [] } if @lenses.empty?

  avg_clarity    = (@lenses.values.sum(&:clarity) / @lenses.size).round(10)
  avg_distortion = (@lenses.values.sum(&:distortion) / @lenses.size).round(10)

  {
    lens_count:     @lenses.size,
    stack_count:    @stacks.size,
    avg_clarity:    avg_clarity,
    avg_distortion: avg_distortion,
    sharp_count:    @lenses.values.count(&:sharp?),
    blurry_count:   @lenses.values.count(&:blurry?),
    lenses:         @lenses.values.map(&:to_h),
    stacks:         @stacks.transform_values(&:to_h)
  }
end

#most_distorted(limit: 3) ⇒ Object



73
74
75
76
77
78
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 73

def most_distorted(limit: 3)
  @lenses.values
         .sort_by { |l| -l.distortion }
         .first(limit)
         .map(&:to_h)
end

#stack_lenses(lens_ids:, stack_id: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 31

def stack_lenses(lens_ids:, stack_id: nil, **)
  stack_id ||= SecureRandom.uuid
  stack = LensStack.new

  lens_ids.each do |lid|
    lens = @lenses[lid]
    raise ArgumentError, "lens not found: #{lid}" unless lens

    stack.push_lens(lens)
  end

  @stacks[stack_id] = stack
  { stack_id: stack_id, stack: stack }
end

#view_through_stack(stack_id:, content:) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/legion/extensions/agentic/attention/lens/helpers/lens_engine.rb', line 46

def view_through_stack(stack_id:, content:, **)
  stack = @stacks[stack_id]
  raise ArgumentError, "stack not found: #{stack_id}" unless stack

  result = stack.view_through(content)
  # rubocop:disable Legion/HelperMigration/DirectLogging
  Legion::Logging.debug("[cognitive_lens] view_through stack=#{stack_id} " \
                        "perceived=#{result[:perceived].round(4)} " \
                        "magnification=#{result[:combined_magnification].round(2)} " \
                        "distortion=#{result[:combined_distortion].round(2)} " \
                        "clarity=#{result[:stack_clarity].round(2)}")
  # rubocop:enable Legion/HelperMigration/DirectLogging
  result
end