Class: Legion::Extensions::Agentic::Memory::Episodic::Helpers::Episode

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb

Constant Summary

Constants included from Constants

Constants::ATTENTION_BOOST, Constants::BINDING_DECAY, Constants::BINDING_STRENGTH_FLOOR, Constants::COHERENCE_LABELS, Constants::DEFAULT_BINDING_STRENGTH, Constants::EPISODE_TTL, Constants::INTEGRATION_THRESHOLD, Constants::MAX_BINDINGS_PER_EPISODE, Constants::MAX_EPISODES, Constants::MAX_HISTORY, Constants::MODALITIES, Constants::RECENTLY_ACCESSED_WINDOW, Constants::REHEARSAL_BOOST

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEpisode

Returns a new instance of Episode.



16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 16

def initialize
  @id            = SecureRandom.uuid
  @bindings      = {}
  @created_at    = Time.now
  @last_accessed = Time.now
end

Instance Attribute Details

#bindingsObject (readonly)

Returns the value of attribute bindings.



14
15
16
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 14

def bindings
  @bindings
end

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 14

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 14

def id
  @id
end

#last_accessedObject (readonly)

Returns the value of attribute last_accessed.



14
15
16
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 14

def last_accessed
  @last_accessed
end

Instance Method Details

#add_binding(modality:, content:, source:, strength: DEFAULT_BINDING_STRENGTH) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 23

def add_binding(modality:, content:, source:, strength: DEFAULT_BINDING_STRENGTH)
  return { added: false, reason: :capacity_full } if @bindings.size >= MAX_BINDINGS_PER_EPISODE

  binding = EpisodicBinding.new(modality: modality, content: content, source: source, strength: strength)
  @bindings[binding.id] = binding
  { added: true, binding_id: binding.id }
end

#attendObject



36
37
38
39
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 36

def attend
  @last_accessed = Time.now
  @bindings.each_value { |b| b.strengthen(ATTENTION_BOOST) }
end

#coherenceObject



50
51
52
53
54
55
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 50

def coherence
  integrated = @bindings.values.select(&:integrated?)
  return 0.0 if integrated.empty?

  integrated.sum(&:strength) / integrated.size
end

#coherence_labelObject



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

def coherence_label
  score = coherence
  COHERENCE_LABELS.each do |range, label|
    return label if range.cover?(score)
  end
  :fragmented
end

#decay_bindingsObject



74
75
76
77
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 74

def decay_bindings
  @bindings.each_value(&:decay)
  @bindings.delete_if { |_, b| b.faded? }
end

#expired?Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 69

def expired?
  age = Time.now - @created_at
  age > EPISODE_TTL && !recently_accessed?
end

#modalities_presentObject



46
47
48
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 46

def modalities_present
  @bindings.values.map(&:modality).uniq
end

#multimodal?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 65

def multimodal?
  modalities_present.size >= 2
end

#rehearseObject



41
42
43
44
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 41

def rehearse
  @last_accessed = Time.now
  @bindings.each_value { |b| b.strengthen(REHEARSAL_BOOST) }
end

#remove_binding(binding_id:) ⇒ Object



31
32
33
34
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 31

def remove_binding(binding_id:)
  removed = !@bindings.delete(binding_id).nil?
  { removed: removed, binding_id: binding_id }
end

#to_hObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/legion/extensions/agentic/memory/episodic/helpers/episode.rb', line 79

def to_h
  {
    id:              @id,
    bindings:        @bindings.transform_values(&:to_h),
    created_at:      @created_at,
    last_accessed:   @last_accessed,
    modalities:      modalities_present,
    coherence:       coherence,
    coherence_label: coherence_label,
    multimodal:      multimodal?,
    binding_count:   @bindings.size
  }
end