Class: Legion::Extensions::Agentic::Self::DefaultModeNetwork::Helpers::DmnEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb

Constant Summary

Constants included from Constants

Constants::ACTIVITY_LABELS, Constants::DEEP_IDLE_THRESHOLD, Constants::DEFAULT_SALIENCE, Constants::IDLE_THRESHOLD, Constants::MAX_ASSOCIATION_CHAIN, Constants::MAX_THOUGHT_HISTORY, Constants::MAX_WANDERING_THOUGHTS, Constants::PLANNING_PROBABILITY, Constants::SALIENCE_ALPHA, Constants::SALIENCE_LABELS, Constants::SELF_REFERENTIAL_PROBABILITY, Constants::SOCIAL_REPLAY_PROBABILITY, Constants::THOUGHT_DECAY, Constants::THOUGHT_SALIENCE_FLOOR, Constants::WANDERING_PROBABILITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDmnEngine

Returns a new instance of DmnEngine.



14
15
16
17
18
19
20
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 14

def initialize
  @mode              = :active
  @last_stimulus_at  = Time.now.utc
  @thoughts          = []
  @thought_history   = []
  @wandering_seeds   = []
end

Instance Attribute Details

#last_stimulus_atObject (readonly)

Returns the value of attribute last_stimulus_at.



12
13
14
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 12

def last_stimulus_at
  @last_stimulus_at
end

#modeObject (readonly)

Returns the value of attribute mode.



12
13
14
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 12

def mode
  @mode
end

#thought_historyObject (readonly)

Returns the value of attribute thought_history.



12
13
14
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 12

def thought_history
  @thought_history
end

#thoughtsObject (readonly)

Returns the value of attribute thoughts.



12
13
14
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 12

def thoughts
  @thoughts
end

#wandering_seedsObject (readonly)

Returns the value of attribute wandering_seeds.



12
13
14
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 12

def wandering_seeds
  @wandering_seeds
end

Instance Method Details

#decay_allObject

— Lifecycle —



136
137
138
139
140
141
142
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 136

def decay_all
  @thoughts.each(&:decay)
  faded = @thoughts.select(&:faded?)
  faded.each { |t| archive_thought(t) }
  @thoughts.reject!(&:faded?)
  faded.size
end

#generate_thoughtObject

— Thought Generation —



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 54

def generate_thought
  roll = rand
  thought = if roll < Constants::SELF_REFERENTIAL_PROBABILITY
              self_reflect
            elsif roll < Constants::SELF_REFERENTIAL_PROBABILITY + Constants::SOCIAL_REPLAY_PROBABILITY
              social_replay(interaction: random_seed(:social))
            elsif roll < Constants::SELF_REFERENTIAL_PROBABILITY +
                         Constants::SOCIAL_REPLAY_PROBABILITY +
                         Constants::PLANNING_PROBABILITY
              plan_spontaneously(goal: random_seed(:goal))
            else
              wander(seed: random_seed(:concept))
            end
  store_thought(thought)
  thought
end

#idle_durationObject



48
49
50
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 48

def idle_duration
  Time.now.utc - @last_stimulus_at
end

#plan_spontaneously(goal: nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 98

def plan_spontaneously(goal: nil)
  seed  = goal || :unresolved_objective
  chain = build_association_chain(seed, Constants::MAX_ASSOCIATION_CHAIN)

  WanderingThought.new(
    seed:              seed,
    association_chain: chain,
    domain:            :planning,
    thought_type:      :spontaneous_plan,
    salience:          rand(0.3..0.8)
  )
end

#register_stimulus(source: nil) ⇒ Object

— Stimulus / Mode —



24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 24

def register_stimulus(source: nil)
  @last_stimulus_at = Time.now.utc
  previous          = @mode
  @mode             = :active
  { previous_mode: previous, current_mode: @mode, source: source, at: @last_stimulus_at }
end

#salient_thoughts(count: 5) ⇒ Object

— Retrieval —



126
127
128
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 126

def salient_thoughts(count: 5)
  @thoughts.sort_by { |t| -t.salience }.first(count)
end

#self_reflect(topic: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 71

def self_reflect(topic: nil)
  topics        = %i[identity values goals capabilities limitations purpose growth]
  chosen_topic  = topic || topics.sample
  chain         = build_association_chain(chosen_topic, Constants::MAX_ASSOCIATION_CHAIN)

  WanderingThought.new(
    seed:              chosen_topic,
    association_chain: chain,
    domain:            :self,
    thought_type:      :self_referential,
    salience:          rand(0.3..0.7)
  )
end

#social_replay(interaction: nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 85

def social_replay(interaction: nil)
  seed  = interaction || :recent_interaction
  chain = build_association_chain(seed, Constants::MAX_ASSOCIATION_CHAIN)

  WanderingThought.new(
    seed:              seed,
    association_chain: chain,
    domain:            :social,
    thought_type:      :social_replay,
    salience:          rand(0.2..0.6)
  )
end

#thought_countObject



144
145
146
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 144

def thought_count
  @thoughts.size
end

#thoughts_of_type(type:) ⇒ Object



130
131
132
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 130

def thoughts_of_type(type:)
  @thoughts.select { |t| t.thought_type == type.to_sym }
end

#tick_modeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 31

def tick_mode
  elapsed = idle_duration
  previous = @mode

  @mode = if elapsed >= Constants::DEEP_IDLE_THRESHOLD
            :deep_idle
          elsif elapsed >= Constants::IDLE_THRESHOLD
            :idle
          elsif @mode == :active && elapsed.positive?
            :transitioning
          else
            @mode
          end

  { previous_mode: previous, current_mode: @mode, idle_duration: elapsed.round(2) }
end

#to_hObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 148

def to_h
  {
    mode:             @mode,
    mode_label:       Constants::ACTIVITY_LABELS[@mode],
    idle_duration:    idle_duration.round(2),
    thought_count:    @thoughts.size,
    history_count:    @thought_history.size,
    last_stimulus_at: @last_stimulus_at
  }
end

#wander(seed: nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/legion/extensions/agentic/self/default_mode_network/helpers/dmn_engine.rb', line 111

def wander(seed: nil)
  anchor = seed || random_seed(:concept)
  chain  = build_association_chain(anchor, Constants::MAX_ASSOCIATION_CHAIN)

  WanderingThought.new(
    seed:              anchor,
    association_chain: chain,
    domain:            :associative,
    thought_type:      :wandering,
    salience:          rand(0.1..0.5)
  )
end