Class: Legion::Extensions::Agentic::Memory::Offloading::Helpers::OffloadingEngine

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

Constant Summary

Constants included from Constants

Constants::DEFAULT_STORE_TRUST, Constants::IMPORTANCE_LABELS, Constants::ITEM_TYPES, Constants::MAX_ITEMS, Constants::MAX_STORES, Constants::OFFLOAD_LABELS, Constants::RETRIEVAL_SUCCESS_THRESHOLD, Constants::STORE_TYPES, Constants::TRUST_BOOST, Constants::TRUST_DECAY, Constants::TRUST_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOffloadingEngine

Returns a new instance of OffloadingEngine.



18
19
20
21
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 18

def initialize
  @items  = {}
  @stores = {}
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



16
17
18
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 16

def items
  @items
end

#storesObject (readonly)

Returns the value of attribute stores.



16
17
18
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 16

def stores
  @stores
end

Instance Method Details

#items_by_type(item_type:) ⇒ Object



69
70
71
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 69

def items_by_type(item_type:)
  @items.values.select { |i| i.item_type == item_type }
end

#items_in_store(store_id:) ⇒ Object



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

def items_in_store(store_id:)
  @items.values.select { |i| i.store_id == store_id }
end

#least_trusted_storeObject



96
97
98
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 96

def least_trusted_store
  @stores.values.min_by(&:trust)
end

#most_important_offloaded(limit: 10) ⇒ Object



73
74
75
76
77
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 73

def most_important_offloaded(limit: 10)
  @items.values
        .sort_by { |i| -i.importance }
        .first(limit)
end

#most_trusted_storeObject



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

def most_trusted_store
  @stores.values.max_by(&:trust)
end

#offload(content:, item_type:, importance:, store_id:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 31

def offload(content:, item_type:, importance:, store_id:)
  return nil if @items.size >= MAX_ITEMS
  return nil unless @stores.key?(store_id)

  item = OffloadedItem.new(
    content:    content,
    item_type:  item_type,
    importance: importance,
    store_id:   store_id
  )
  @items[item.id] = item
  @stores[store_id].increment_items!
  item
end

#offloading_ratioObject



79
80
81
82
83
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 79

def offloading_ratio
  return 0.0 if MAX_ITEMS.zero?

  (@items.size.to_f / MAX_ITEMS).round(10)
end

#offloading_reportObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 100

def offloading_report
  {
    total_items:         @items.size,
    total_stores:        @stores.size,
    offloading_ratio:    offloading_ratio,
    offloading_label:    offloading_label,
    overall_store_trust: overall_store_trust,
    most_trusted_store:  most_trusted_store&.to_h,
    least_trusted_store: least_trusted_store&.to_h,
    items_by_type:       items_type_summary,
    stores_summary:      @stores.values.map(&:to_h)
  }
end

#overall_store_trustObject



85
86
87
88
89
90
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 85

def overall_store_trust
  return 0.0 if @stores.empty?

  total = @stores.values.sum(&:trust)
  (total / @stores.size).round(10)
end

#register_store(name:, store_type:) ⇒ Object



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

def register_store(name:, store_type:)
  return nil if @stores.size >= MAX_STORES

  store = ExternalStore.new(name: name, store_type: store_type)
  @stores[store.id] = store
  store
end

#retrieve(item_id:) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 46

def retrieve(item_id:)
  item = @items[item_id]
  return nil unless item

  store = @stores[item.store_id]
  store&.record_success!
  item.retrieve!
  item
end

#retrieve_failed(item_id:) ⇒ Object



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

def retrieve_failed(item_id:)
  item = @items[item_id]
  return nil unless item

  store = @stores[item.store_id]
  store&.record_failure!
  item
end

#to_hObject



114
115
116
117
118
119
# File 'lib/legion/extensions/agentic/memory/offloading/helpers/offloading_engine.rb', line 114

def to_h
  {
    items:  @items.transform_values(&:to_h),
    stores: @stores.transform_values(&:to_h)
  }
end