Class: Legion::Extensions::Agentic::Attention::RelevanceTheory::Helpers::RelevanceEngine

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

Constant Summary

Constants included from Constants

Constants::ATTENTION_THRESHOLD, Constants::DEFAULT_EFFECT, Constants::DEFAULT_EFFORT, Constants::EFFECT_CEILING, Constants::EFFECT_DECAY, Constants::EFFECT_FLOOR, Constants::EFFECT_TYPES, Constants::EFFORT_CEILING, Constants::EFFORT_FLOOR, Constants::EFFORT_INFLATION, Constants::INPUT_TYPES, Constants::MAX_CONTEXTS, Constants::MAX_HISTORY, Constants::MAX_INPUTS, Constants::RELEVANCE_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRelevanceEngine

Returns a new instance of RelevanceEngine.



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

def initialize
  @inputs  = {}
  @history = []
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



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

def history
  @history
end

Instance Method Details

#assess_relevance(input_id:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 41

def assess_relevance(input_id:)
  input = @inputs[input_id]
  return { success: false, reason: :not_found } unless input

  {
    success:          true,
    relevance:        input.relevance.round(3),
    normalized:       input.normalized_relevance.round(3),
    label:            input.relevance_label,
    worth_processing: input.worth_processing?,
    effect:           input.cognitive_effect,
    effort:           input.processing_effort
  }
end

#attention_budgetObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 101

def attention_budget
  processable = worth_processing
  total = @inputs.size.to_f
  {
    total_inputs:     @inputs.size,
    worth_processing: processable.size,
    filtered_out:     @inputs.size - processable.size,
    processing_ratio: total.zero? ? 0.0 : (processable.size / total).round(3),
    avg_relevance:    avg_relevance.round(3)
  }
end

#by_context(context:) ⇒ Object



86
87
88
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 86

def by_context(context:)
  @inputs.values.select { |inp| inp.context == context }
end

#by_effect_type(effect_type:) ⇒ Object



90
91
92
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 90

def by_effect_type(effect_type:)
  @inputs.values.select { |inp| inp.effect_type == effect_type }
end

#context_relevance(context:) ⇒ Object



94
95
96
97
98
99
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 94

def context_relevance(context:)
  ctx_inputs = by_context(context: context)
  return 0.0 if ctx_inputs.empty?

  ctx_inputs.sum(&:normalized_relevance) / ctx_inputs.size
end

#decay_allObject



113
114
115
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 113

def decay_all
  @inputs.each_value(&:decay!)
end

#irrelevant_inputsObject



78
79
80
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 78

def irrelevant_inputs
  @inputs.values.reject(&:worth_processing?)
end

#most_relevant(limit: 5) ⇒ Object



82
83
84
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 82

def most_relevant(limit: 5)
  @inputs.values.sort_by { |inp| -inp.normalized_relevance }.first(limit)
end

#prune_irrelevantObject



117
118
119
120
121
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 117

def prune_irrelevant
  ids = @inputs.select { |_id, inp| inp.normalized_relevance <= 0.05 }.keys
  ids.each { |input_id| @inputs.delete(input_id) }
  ids.size
end

#strengthen_input(input_id:, amount: 0.1) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 56

def strengthen_input(input_id:, amount: 0.1)
  input = @inputs[input_id]
  return { success: false, reason: :not_found } unless input

  input.strengthen!(amount: amount)
  record_history(:strengthened, input_id)
  { success: true, relevance: input.normalized_relevance.round(3) }
end

#submit_input(content:, input_type:, context:, cognitive_effect: DEFAULT_EFFECT, processing_effort: DEFAULT_EFFORT, effect_type: :new_implication, source_id: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 19

def submit_input(content:, input_type:, context:, cognitive_effect: DEFAULT_EFFECT,
                 processing_effort: DEFAULT_EFFORT, effect_type: :new_implication,
                 source_id: nil)
  return nil unless INPUT_TYPES.include?(input_type.to_sym)
  return nil unless EFFECT_TYPES.include?(effect_type.to_sym)

  evict_oldest if @inputs.size >= MAX_INPUTS

  input = CognitiveInput.new(
    content:           content,
    input_type:        input_type,
    context:           context,
    cognitive_effect:  cognitive_effect,
    processing_effort: processing_effort,
    effect_type:       effect_type,
    source_id:         source_id
  )
  @inputs[input.id] = input
  record_history(:submitted, input.id)
  input
end

#to_hObject



123
124
125
126
127
128
129
130
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 123

def to_h
  {
    total_inputs:  @inputs.size,
    processable:   worth_processing.size,
    avg_relevance: avg_relevance.round(3),
    history_count: @history.size
  }
end

#weaken_input(input_id:, amount: 0.1) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 65

def weaken_input(input_id:, amount: 0.1)
  input = @inputs[input_id]
  return { success: false, reason: :not_found } unless input

  input.weaken!(amount: amount)
  record_history(:weakened, input_id)
  { success: true, relevance: input.normalized_relevance.round(3) }
end

#worth_processingObject



74
75
76
# File 'lib/legion/extensions/agentic/attention/relevance_theory/helpers/relevance_engine.rb', line 74

def worth_processing
  @inputs.values.select(&:worth_processing?)
end