Class: Legion::Extensions::Agentic::Executive::WorkingMemory::Helpers::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuffer

Returns a new instance of Buffer.



12
13
14
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 12

def initialize
  @items = []
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



10
11
12
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 10

def items
  @items
end

Instance Method Details

#available_slotsObject



74
75
76
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 74

def available_slots
  [capacity - @items.size, 0].max
end

#capacityObject



70
71
72
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 70

def capacity
  Constants::CAPACITY + chunk_bonus
end

#clearObject



82
83
84
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 82

def clear
  @items.clear
end

#consolidation_candidatesObject



52
53
54
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 52

def consolidation_candidates
  @items.select(&:consolidation_ready?)
end

#current_loadObject



56
57
58
59
60
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 56

def current_load
  return 0.0 if capacity.zero?

  (@items.size.to_f / capacity).clamp(0.0, 1.0)
end

#full?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 78

def full?
  @items.size >= capacity
end

#load_levelObject



62
63
64
65
66
67
68
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 62

def load_level
  load = current_load
  Constants::LOAD_LEVELS.each do |level, threshold|
    return level if load >= threshold
  end
  :idle
end

#rehearse(id) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 35

def rehearse(id)
  item = retrieve(id)
  return nil unless item

  item.rehearse
  item
end

#remove(id) ⇒ Object



43
44
45
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 43

def remove(id)
  @items.reject! { |i| i.id == id }
end

#retrieve(id) ⇒ Object



23
24
25
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 23

def retrieve(id)
  @items.find { |i| i.id == id }
end

#retrieve_by_tag(tag) ⇒ Object



27
28
29
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 27

def retrieve_by_tag(tag)
  @items.select { |i| i.tags.include?(tag) }
end

#retrieve_by_type(buffer_type) ⇒ Object



31
32
33
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 31

def retrieve_by_type(buffer_type)
  @items.select { |i| i.buffer_type == buffer_type }
end

#sizeObject



86
87
88
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 86

def size
  @items.size
end

#store(content:, buffer_type: :episodic, priority: :normal, tags: []) ⇒ Object



16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 16

def store(content:, buffer_type: :episodic, priority: :normal, tags: [])
  item = BufferItem.new(content: content, buffer_type: buffer_type, priority: priority, tags: tags)
  @items << item
  evict_if_over_capacity
  item
end

#tick_decayObject



47
48
49
50
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 47

def tick_decay
  @items.each(&:decay)
  @items.reject!(&:expired?)
end

#to_hObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/legion/extensions/agentic/executive/working_memory/helpers/buffer.rb', line 90

def to_h
  {
    size:       @items.size,
    capacity:   capacity,
    load:       current_load.round(4),
    load_level: load_level,
    by_type:    items_by_type,
    available:  available_slots
  }
end