Class: Legion::Extensions::Agentic::Attention::Economy::Helpers::Demand

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/attention/economy/helpers/demand.rb

Constant Summary

Constants included from Constants

Constants::BUDGET_RECOVERY_RATE, Constants::DEFAULT_BUDGET, Constants::DEMAND_TYPES, Constants::EFFICIENCY_LABELS, Constants::MAX_DEMANDS, Constants::MIN_ALLOCATION, Constants::PRIORITY_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, demand_type:, priority:, cost:, roi: 0.5) ⇒ Demand

Returns a new instance of Demand.



18
19
20
21
22
23
24
25
26
27
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 18

def initialize(label:, demand_type:, priority:, cost:, roi: 0.5)
  @id          = SecureRandom.uuid
  @label       = label
  @demand_type = demand_type
  @priority    = priority.clamp(0.0, 1.0)
  @cost        = cost.clamp(0.0, 1.0)
  @roi         = roi.clamp(0.0, 1.0)
  @allocated   = 0.0
  @created_at  = Time.now.utc
end

Instance Attribute Details

#allocatedObject

Returns the value of attribute allocated.



16
17
18
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 16

def allocated
  @allocated
end

#costObject (readonly)

Returns the value of attribute cost.



15
16
17
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 15

def cost
  @cost
end

#created_atObject (readonly)

Returns the value of attribute created_at.



15
16
17
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 15

def created_at
  @created_at
end

#demand_typeObject (readonly)

Returns the value of attribute demand_type.



15
16
17
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 15

def demand_type
  @demand_type
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 15

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



15
16
17
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 15

def label
  @label
end

#priorityObject (readonly)

Returns the value of attribute priority.



15
16
17
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 15

def priority
  @priority
end

#roiObject (readonly)

Returns the value of attribute roi.



15
16
17
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 15

def roi
  @roi
end

Instance Method Details

#allocate!(amount:) ⇒ Object



29
30
31
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 29

def allocate!(amount:)
  @allocated = amount.clamp(0.0, 1.0).round(10)
end

#deallocate!Object



33
34
35
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 33

def deallocate!
  @allocated = 0.0
end

#efficiencyObject



37
38
39
40
41
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 37

def efficiency
  return 0.0 if cost.zero?

  (roi / cost).clamp(0.0, 1.0).round(10)
end

#efficiency_labelObject



43
44
45
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 43

def efficiency_label
  Constants::EFFICIENCY_LABELS.find { |range, _| range.cover?(efficiency) }&.last || :wasted
end

#priority_labelObject



47
48
49
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 47

def priority_label
  Constants::PRIORITY_LABELS.find { |range, _| range.cover?(priority) }&.last || :background
end

#to_hObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/attention/economy/helpers/demand.rb', line 51

def to_h
  {
    id:               id,
    label:            label,
    demand_type:      demand_type,
    priority:         priority,
    priority_label:   priority_label,
    cost:             cost,
    roi:              roi,
    allocated:        allocated,
    efficiency:       efficiency,
    efficiency_label: efficiency_label,
    created_at:       created_at
  }
end