Class: Legion::Extensions::Agentic::Memory::Nostalgia::Helpers::NostalgiaEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNostalgiaEngine

Returns a new instance of NostalgiaEngine.



12
13
14
15
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 12

def initialize
  @memories = []
  @events   = []
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



10
11
12
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 10

def events
  @events
end

#memoriesObject (readonly)

Returns the value of attribute memories.



10
11
12
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 10

def memories
  @memories
end

Instance Method Details

#age_all!Object



53
54
55
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 53

def age_all!
  @memories.each(&:age!)
end

#bittersweet_memoriesObject



92
93
94
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 92

def bittersweet_memories
  @memories.select(&:bittersweet?).map(&:to_h)
end

#create_memory(content:, domain: :unknown, warmth: Constants::DEFAULT_WARMTH, original_valence: 0.5) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 17

def create_memory(content:, domain: :unknown, warmth: Constants::DEFAULT_WARMTH,
                  original_valence: 0.5)
  memory = NostalgicMemory.new(
    content:          content,
    domain:           domain,
    warmth:           warmth,
    original_valence: original_valence
  )
  @memories << memory
  @memories.shift while @memories.size > Constants::MAX_MEMORIES
  memory
end

#most_nostalgic_domainsObject



86
87
88
89
90
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 86

def most_nostalgic_domains
  warmth_by_domain.sort_by { |_, v| -v }.map do |domain, avg_warmth|
    { domain: domain, avg_warmth: avg_warmth.round(10) }
  end
end

#nostalgia_pronenessObject



78
79
80
81
82
83
84
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 78

def nostalgia_proneness
  return 0.0 if @memories.empty?

  avg_warmth = @memories.sum(&:warmth) / @memories.size
  event_density = [@events.size.to_f / Constants::MAX_EVENTS, 1.0].min
  ((avg_warmth * 0.7) + (event_density * 0.3)).clamp(0.0, 1.0).round(10)
end

#nostalgia_reportObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 96

def nostalgia_report
  index = rosy_retrospection_index
  proneness = nostalgia_proneness
  {
    total_memories:           @memories.size,
    total_events:             @events.size,
    rosy_retrospection_index: index,
    retrospection_label:      Constants.label_for(Constants::RETROSPECTION_LABELS, index),
    nostalgia_proneness:      proneness,
    nostalgia_label:          Constants.label_for(Constants::NOSTALGIA_LABELS, proneness),
    most_nostalgic_domains:   most_nostalgic_domains,
    bittersweet_count:        bittersweet_memories.size,
    rosy_count:               @memories.count(&:rosy?)
  }
end

#rosy_retrospection_indexObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 65

def rosy_retrospection_index
  return 0.0 if @memories.empty?

  rosy_count = @memories.count(&:rosy?)
  inflation_sum = @memories.select(&:rosy?).sum do |m|
    m.warmth - m.original_valence
  end

  base = rosy_count.to_f / @memories.size
  avg_inflation = rosy_count.positive? ? inflation_sum / rosy_count : 0.0
  (base * avg_inflation).clamp(0.0, 1.0).round(10)
end

#trigger_nostalgia(trigger:, domain: nil, intensity_hint: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 30

def trigger_nostalgia(trigger:, domain: nil, intensity_hint: nil)
  candidates = find_candidates(domain, trigger)
  return [] if candidates.empty?

  new_events = candidates.filter_map do |memory|
    intensity = compute_intensity(memory, intensity_hint)
    next unless intensity >= Constants::TRIGGER_SENSITIVITY

    event = NostalgiaEvent.new(
      memory_id:      memory.id,
      trigger:        trigger,
      intensity:      intensity,
      effect_on_mood: compute_mood_effect(memory, intensity)
    )
    memory.warm!(intensity * 0.1)
    @events << event
    event
  end

  @events.shift while @events.size > Constants::MAX_EVENTS
  new_events
end

#warmth_by_domainObject



57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/memory/nostalgia/helpers/nostalgia_engine.rb', line 57

def warmth_by_domain
  grouped = @memories.group_by(&:domain)
  grouped.transform_values do |mems|
    total = mems.sum(&:warmth)
    (total / mems.size).round(10)
  end
end