Class: Legion::Extensions::Agentic::Affect::CognitiveEmpathy::Helpers::EmpathyEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb

Constant Summary

Constants included from Constants

Constants::ACCURACY_ALPHA, Constants::ACCURACY_CEILING, Constants::ACCURACY_FLOOR, Constants::ACCURACY_LABELS, Constants::CONTAGION_DECAY, Constants::CONTAGION_RATE, Constants::DEFAULT_ACCURACY, Constants::EMPATHIC_STATES, Constants::MAX_HISTORY, Constants::MAX_INTERACTIONS, Constants::MAX_PERSPECTIVES, Constants::PERSPECTIVE_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEmpathyEngine

Returns a new instance of EmpathyEngine.



14
15
16
17
18
19
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 14

def initialize
  @perspectives     = {}
  @contagion_level  = 0.0
  @counter          = 0
  @history          = []
end

Instance Attribute Details

#contagion_levelObject (readonly)

Returns the value of attribute contagion_level.



12
13
14
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 12

def contagion_level
  @contagion_level
end

#historyObject (readonly)

Returns the value of attribute history.



12
13
14
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 12

def history
  @history
end

#perspectivesObject (readonly)

Returns the value of attribute perspectives.



12
13
14
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 12

def perspectives
  @perspectives
end

Instance Method Details

#contagion_decayObject



72
73
74
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 72

def contagion_decay
  @contagion_level = [@contagion_level - CONTAGION_DECAY, 0.0].max
end

#emotional_contagion(emotion_valence:, intensity:) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 63

def emotional_contagion(emotion_valence:, intensity:)
  intensity_f = intensity.to_f.clamp(0.0, 1.0)
  absorption  = CONTAGION_RATE * intensity_f
  @contagion_level = (@contagion_level + absorption).clamp(0.0, 1.0)
  record_event(:contagion, valence: emotion_valence, intensity: intensity_f,
                           level: @contagion_level)
  @contagion_level
end

#empathic_accuracy(agent_id:) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 48

def empathic_accuracy(agent_id:)
  agent_perspectives = perspectives_for(agent_id: agent_id)
  resolved = agent_perspectives.select(&:resolved?)
  return DEFAULT_ACCURACY if resolved.empty?

  resolved.sum(&:accuracy) / resolved.size
end

#empathic_stateObject



76
77
78
79
80
81
82
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 76

def empathic_state
  return :immersed   if @contagion_level >= 0.75
  return :resonating if @contagion_level >= 0.45
  return :observing  if @contagion_level >= 0.15

  :detached
end

#least_accurate_agentObject



95
96
97
98
99
100
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 95

def least_accurate_agent
  agent_accuracies = build_agent_accuracies
  return nil if agent_accuracies.empty?

  agent_accuracies.min_by { |_, acc| acc }&.first
end

#most_accurate_agentObject



88
89
90
91
92
93
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 88

def most_accurate_agent
  agent_accuracies = build_agent_accuracies
  return nil if agent_accuracies.empty?

  agent_accuracies.max_by { |_, acc| acc }&.first
end

#overall_accuracyObject



56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 56

def overall_accuracy
  resolved = @perspectives.values.select(&:resolved?)
  return DEFAULT_ACCURACY if resolved.empty?

  resolved.sum(&:accuracy) / resolved.size
end

#perspectives_for(agent_id:) ⇒ Object



84
85
86
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 84

def perspectives_for(agent_id:)
  @perspectives.values.select { |p| p.agent_id == agent_id }
end

#record_outcome(perspective_id:, actual_state:) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 39

def record_outcome(perspective_id:, actual_state:)
  perspective = @perspectives[perspective_id]
  return nil unless perspective

  perspective.record_actual(actual_state: actual_state)
  record_event(:outcome_recorded, id: perspective_id, accuracy: perspective.accuracy)
  perspective
end

#take_perspective(agent_id:, perspective_type:, predicted_state:, confidence:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 21

def take_perspective(agent_id:, perspective_type:, predicted_state:, confidence:)
  return nil if @perspectives.size >= MAX_PERSPECTIVES
  return nil unless PERSPECTIVE_TYPES.include?(perspective_type)

  @counter += 1
  id = :"perspective_#{@counter}"
  perspective = Perspective.new(
    id:               id,
    agent_id:         agent_id,
    perspective_type: perspective_type,
    predicted_state:  predicted_state,
    confidence:       confidence
  )
  @perspectives[id] = perspective
  record_event(:perspective_taken, id: id, agent_id: agent_id)
  perspective
end

#tickObject



102
103
104
105
106
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 102

def tick
  contagion_decay
  prune_old_perspectives
  self
end

#to_hObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/helpers/empathy_engine.rb', line 108

def to_h
  {
    perspective_count: @perspectives.size,
    resolved_count:    @perspectives.values.count(&:resolved?),
    overall_accuracy:  overall_accuracy.round(4),
    contagion_level:   @contagion_level.round(4),
    empathic_state:    empathic_state,
    history_size:      @history.size
  }
end