Class: Legion::Extensions::Agentic::Learning::Chrysalis::Helpers::Chrysalis

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chrysalis_type:, content:) ⇒ Chrysalis

Returns a new instance of Chrysalis.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 15

def initialize(chrysalis_type:, content:, **)
  @id                     = SecureRandom.uuid
  @chrysalis_type         = chrysalis_type.to_sym
  @content                = content.to_s
  @stage                  = :larva
  @transformation_progress = 0.0
  @protection             = 0.8
  @beauty                 = 0.0
  @created_at             = Time.now.utc
  @premature              = false
end

Instance Attribute Details

#beautyObject (readonly)

Returns the value of attribute beauty.



12
13
14
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 12

def beauty
  @beauty
end

#chrysalis_typeObject (readonly)

Returns the value of attribute chrysalis_type.



12
13
14
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 12

def chrysalis_type
  @chrysalis_type
end

#contentObject (readonly)

Returns the value of attribute content.



12
13
14
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 12

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 12

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 12

def id
  @id
end

#protectionObject (readonly)

Returns the value of attribute protection.



12
13
14
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 12

def protection
  @protection
end

#stageObject (readonly)

Returns the value of attribute stage.



12
13
14
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 12

def stage
  @stage
end

#transformation_progressObject (readonly)

Returns the value of attribute transformation_progress.



12
13
14
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 12

def transformation_progress
  @transformation_progress
end

Instance Method Details

#beauty_labelObject



93
94
95
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 93

def beauty_label
  Helpers::Constants.label_for(Helpers::Constants::BEAUTY_LABELS, @beauty)
end

#butterfly?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 73

def butterfly?
  @stage == :butterfly
end

#cocoon!Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 34

def cocoon!
  raise ArgumentError, "must be :spinning to cocoon, currently #{@stage}" unless @stage == :spinning

  @stage = :cocooned
  true
end

#cocooned?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 77

def cocooned?
  @stage == :cocooned
end

#decay_protection!Object



64
65
66
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 64

def decay_protection!
  @protection = (@protection - Helpers::Constants::PROTECTION_DECAY).clamp(0.0, 1.0).round(10)
end

#disturb!(force) ⇒ Object



68
69
70
71
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 68

def disturb!(force)
  @protection = (@protection - force.to_f).clamp(0.0, 1.0).round(10)
  emerge!(force: true) if @protection <= 0 && %i[cocooned transforming].include?(@stage)
end

#emerge!(force: false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 50

def emerge!(force: false)
  if @transformation_progress >= Helpers::Constants::EMERGENCE_THRESHOLD
    @stage  = :butterfly
    @beauty = 1.0
  elsif force
    @premature = true
    @stage     = :butterfly
    @beauty    = (@beauty - Helpers::Constants::PREMATURE_PENALTY).clamp(0.0, 1.0).round(10)
  else
    return false
  end
  true
end

#premature?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 85

def premature?
  butterfly? && @beauty < 0.5
end

#spin!Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 27

def spin!
  raise ArgumentError, "must be :larva to spin, currently #{@stage}" unless @stage == :larva

  @stage = :spinning
  true
end

#stage_labelObject



89
90
91
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 89

def stage_label
  Helpers::Constants.label_for(Helpers::Constants::STAGE_LABELS, @transformation_progress)
end

#to_hObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 97

def to_h
  {
    id:                      @id,
    chrysalis_type:          @chrysalis_type,
    content:                 @content,
    stage:                   @stage,
    transformation_progress: @transformation_progress,
    protection:              @protection,
    beauty:                  @beauty,
    stage_label:             stage_label,
    beauty_label:            beauty_label,
    premature:               @premature,
    created_at:              @created_at
  }
end

#transform!(rate = Helpers::Constants::TRANSFORMATION_RATE) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 41

def transform!(rate = Helpers::Constants::TRANSFORMATION_RATE)
  return false if @stage == :butterfly

  @transformation_progress = (@transformation_progress + rate).clamp(0.0, 1.0).round(10)
  @beauty = (@transformation_progress * 0.9).clamp(0.0, 1.0).round(10)
  update_stage_from_progress!
  true
end

#transforming?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/legion/extensions/agentic/learning/chrysalis/helpers/chrysalis.rb', line 81

def transforming?
  @stage == :transforming
end