Class: Legion::Extensions::Agentic::Social::Mirror::Helpers::MirrorEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Social::Mirror::Helpers::MirrorEngine
- Defined in:
- lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb
Constant Summary collapse
- INTENT_MAP =
{ communication: :inform_or_persuade, decision: :resolve_uncertainty, emotional_expression: :signal_internal_state, creative_act: :generate_novelty, analytical_task: :reduce_uncertainty, social_interaction: :build_relationship, movement: :change_position }.freeze
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#resonance_map ⇒ Object
readonly
Returns the value of attribute resonance_map.
-
#simulations ⇒ Object
readonly
Returns the value of attribute simulations.
Instance Method Summary collapse
- #boost_resonance(agent_id) ⇒ Object
- #decay_all_resonances ⇒ Object
- #decay_resonance(agent_id) ⇒ Object
- #empathic_resonance(agent_id) ⇒ Object
- #events_for(agent_id) ⇒ Object
-
#initialize ⇒ MirrorEngine
constructor
A new instance of MirrorEngine.
- #known_agents ⇒ Object
- #observe(agent_id:, action_type:, context: {}, emotional_valence: 0.0) ⇒ Object
- #record_accuracy(simulation_id, accuracy) ⇒ Object
- #simulate(event, confidence: Constants::SIMULATION_CONFIDENCE_DEFAULT) ⇒ Object
- #simulation_accuracy_for(agent_id) ⇒ Object
- #simulation_history(limit: 20) ⇒ Object
- #simulations_for(agent_id) ⇒ Object
Constructor Details
#initialize ⇒ MirrorEngine
Returns a new instance of MirrorEngine.
22 23 24 25 26 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 22 def initialize @events = [] @simulations = [] @resonance_map = Hash.new(Constants::DEFAULT_RESONANCE) end |
Instance Attribute Details
#events ⇒ Object (readonly)
Returns the value of attribute events.
20 21 22 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 20 def events @events end |
#resonance_map ⇒ Object (readonly)
Returns the value of attribute resonance_map.
20 21 22 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 20 def resonance_map @resonance_map end |
#simulations ⇒ Object (readonly)
Returns the value of attribute simulations.
20 21 22 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 20 def simulations @simulations end |
Instance Method Details
#boost_resonance(agent_id) ⇒ Object
67 68 69 70 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 67 def boost_resonance(agent_id) current = @resonance_map[agent_id] @resonance_map[agent_id] = (current + Constants::RESONANCE_BOOST).round(10).clamp(0.0, 1.0) end |
#decay_all_resonances ⇒ Object
77 78 79 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 77 def decay_all_resonances @resonance_map.each_key { |agent_id| decay_resonance(agent_id) } end |
#decay_resonance(agent_id) ⇒ Object
72 73 74 75 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 72 def decay_resonance(agent_id) current = @resonance_map[agent_id] @resonance_map[agent_id] = (current - Constants::RESONANCE_DECAY).round(10).clamp(0.0, 1.0) end |
#empathic_resonance(agent_id) ⇒ Object
63 64 65 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 63 def empathic_resonance(agent_id) @resonance_map[agent_id].clamp(0.0, 1.0) end |
#events_for(agent_id) ⇒ Object
93 94 95 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 93 def events_for(agent_id) @events.select { |e| e.agent_id == agent_id } end |
#known_agents ⇒ Object
101 102 103 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 101 def known_agents @resonance_map.keys end |
#observe(agent_id:, action_type:, context: {}, emotional_valence: 0.0) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 28 def observe(agent_id:, action_type:, context: {}, emotional_valence: 0.0) event = MirrorEvent.new( agent_id: agent_id, action_type: action_type, context: context, emotional_valence: emotional_valence ) @events << event @events.shift while @events.size > Constants::MAX_EVENTS event end |
#record_accuracy(simulation_id, accuracy) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 55 def record_accuracy(simulation_id, accuracy) sim = @simulations.find { |s| s.id == simulation_id } return false unless sim sim.accuracy_score = accuracy.to_f.clamp(0.0, 1.0) true end |
#simulate(event, confidence: Constants::SIMULATION_CONFIDENCE_DEFAULT) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 40 def simulate(event, confidence: Constants::SIMULATION_CONFIDENCE_DEFAULT) resonance = compute_resonance_for(event) outcome = derive_outcome(event) sim = Simulation.new( event_id: event.id, simulated_outcome: outcome, confidence: confidence, emotional_resonance: resonance ) @simulations << sim @simulations.shift while @simulations.size > Constants::MAX_SIMULATIONS sim end |
#simulation_accuracy_for(agent_id) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 81 def simulation_accuracy_for(agent_id) scored = simulations_for(agent_id).reject { |s| s.accuracy_score.nil? } return nil if scored.empty? scored.sum(&:accuracy_score) / scored.size.to_f end |
#simulation_history(limit: 20) ⇒ Object
97 98 99 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 97 def simulation_history(limit: 20) @simulations.last(limit).map(&:to_h) end |
#simulations_for(agent_id) ⇒ Object
88 89 90 91 |
# File 'lib/legion/extensions/agentic/social/mirror/helpers/mirror_engine.rb', line 88 def simulations_for(agent_id) event_ids = events_for(agent_id).map(&:id) @simulations.select { |s| event_ids.include?(s.event_id) } end |