Class: Legion::Extensions::Agentic::Executive::Chunking::Helpers::ChunkingEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Executive::Chunking::Helpers::ChunkingEngine
show all
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb
Constant Summary
Constants included
from Constants
Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::CAPACITY_LABELS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::CAPACITY_VARIANCE, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::CHUNK_SIZE_LABELS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::COHERENCE_BOOST, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::COHERENCE_DECAY, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::COHERENCE_LABELS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::DEFAULT_COHERENCE, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::MAX_CHUNKS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::MAX_ITEMS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::RECALL_BOOST, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::RECALL_DECAY, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::RECALL_LABELS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::WORKING_MEMORY_CAPACITY
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ChunkingEngine.
14
15
16
17
18
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 14
def initialize
@items = {}
@chunks = {}
@working_memory = []
end
|
Instance Attribute Details
#chunks ⇒ Object
Returns the value of attribute chunks.
12
13
14
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 12
def chunks
@chunks
end
|
#items ⇒ Object
Returns the value of attribute items.
12
13
14
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 12
def items
@items
end
|
#working_memory ⇒ Object
Returns the value of attribute working_memory.
12
13
14
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 12
def working_memory
@working_memory
end
|
Instance Method Details
#add_item(content:, domain: :general) ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 20
def add_item(content:, domain: :general)
return { success: false, error: :capacity_exceeded } if @items.size >= MAX_ITEMS
item = InformationItem.new(content: content, domain: domain)
@items[item.id] = item
{ success: true, item_id: item.id, item: item.to_h }
end
|
#chunking_efficiency ⇒ Object
107
108
109
110
111
112
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 107
def chunking_efficiency
return 0.0 if @items.empty?
chunked_count = @items.values.count(&:chunked?)
(chunked_count.to_f / @items.size).round(10)
end
|
#chunking_report ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 114
def chunking_report
wm_load = working_memory_load
capacity_label = CAPACITY_LABELS.find { |range, _| range.cover?(wm_load) }&.last || :empty
{
total_items: @items.size,
total_chunks: @chunks.size,
unchunked_items: unchunked_items.size,
chunking_efficiency: chunking_efficiency,
working_memory: {
current: @working_memory.size,
capacity: WORKING_MEMORY_CAPACITY,
load: wm_load,
label: capacity_label,
chunk_ids: @working_memory.dup
},
strongest_chunks: strongest_chunks(limit: 5)
}
end
|
#create_chunk(label:, item_ids:) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 28
def create_chunk(label:, item_ids:)
return { success: false, error: :capacity_exceeded } if @chunks.size >= MAX_CHUNKS
return { success: false, error: :empty_item_ids } if item_ids.empty?
valid_ids = item_ids.select { |id| @items.key?(id) }
return { success: false, error: :no_valid_items } if valid_ids.empty?
chunk = Chunk.new(label: label, item_ids: valid_ids)
@chunks[chunk.id] = chunk
valid_ids.each { |id| @items[id]&.assign_to_chunk!(chunk_id: chunk.id) }
{ success: true, chunk_id: chunk.id, chunk: chunk.to_h }
end
|
#decay_all! ⇒ Object
84
85
86
87
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 84
def decay_all!
@chunks.each_value(&:decay!)
{ success: true, chunks_decayed: @chunks.size }
end
|
#load_to_working_memory(chunk_id:) ⇒ Object
57
58
59
60
61
62
63
64
65
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 57
def load_to_working_memory(chunk_id:)
return { success: false, error: :chunk_not_found } unless @chunks.key?(chunk_id)
return { success: false, error: :already_loaded } if @working_memory.include?(chunk_id)
return { success: false, error: :capacity_exceeded } if @working_memory.size >= WORKING_MEMORY_CAPACITY
@working_memory << chunk_id
@chunks[chunk_id].reinforce!
{ success: true, chunk_id: chunk_id, working_memory_size: @working_memory.size }
end
|
#merge_chunks(chunk_ids:, label:) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 42
def merge_chunks(chunk_ids:, label:)
return { success: false, error: :capacity_exceeded } if @chunks.size >= MAX_CHUNKS
return { success: false, error: :insufficient_chunks } if chunk_ids.size < 2
valid_chunk_ids = chunk_ids.select { |id| @chunks.key?(id) }
return { success: false, error: :no_valid_chunks } if valid_chunk_ids.size < 2
merged_item_ids = valid_chunk_ids.flat_map { |id| @chunks[id].item_ids }.uniq
parent = Chunk.new(label: label, item_ids: merged_item_ids)
valid_chunk_ids.each { |id| parent.add_sub_chunk!(chunk_id: id) }
@chunks[parent.id] = parent
{ success: true, chunk_id: parent.id, chunk: parent.to_h, merged_from: valid_chunk_ids }
end
|
#reinforce_chunk(chunk_id:) ⇒ Object
89
90
91
92
93
94
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 89
def reinforce_chunk(chunk_id:)
return { success: false, error: :chunk_not_found } unless @chunks.key?(chunk_id)
@chunks[chunk_id].reinforce!
{ success: true, chunk_id: chunk_id, chunk: @chunks[chunk_id].to_h }
end
|
#strongest_chunks(limit: 10) ⇒ Object
96
97
98
99
100
101
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 96
def strongest_chunks(limit: 10)
@chunks.values
.sort_by { |c| -c.recall_strength }
.first(limit)
.map(&:to_h)
end
|
#to_h ⇒ Object
134
135
136
137
138
139
140
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 134
def to_h
{
items: @items.transform_values(&:to_h),
chunks: @chunks.transform_values(&:to_h),
working_memory: @working_memory.dup
}
end
|
#unchunked_items ⇒ Object
103
104
105
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 103
def unchunked_items
@items.values.reject(&:chunked?).map(&:to_h)
end
|
#unload_from_working_memory(chunk_id:) ⇒ Object
67
68
69
70
71
72
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 67
def unload_from_working_memory(chunk_id:)
return { success: false, error: :not_in_working_memory } unless @working_memory.include?(chunk_id)
@working_memory.delete(chunk_id)
{ success: true, chunk_id: chunk_id, working_memory_size: @working_memory.size }
end
|
#working_memory_load ⇒ Object
74
75
76
77
78
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 74
def working_memory_load
return 0.0 if WORKING_MEMORY_CAPACITY.zero?
(@working_memory.size.to_f / WORKING_MEMORY_CAPACITY).round(10)
end
|
#working_memory_overloaded? ⇒ Boolean
80
81
82
|
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunking_engine.rb', line 80
def working_memory_overloaded?
@working_memory.size > WORKING_MEMORY_CAPACITY
end
|