Module: Legion::Extensions::Agentic::Affect::CognitiveEmpathy::Runners::CognitiveEmpathy

Includes:
Helpers::Constants, Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb

Constant Summary

Constants included from Helpers::Constants

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

Instance Method Summary collapse

Instance Method Details

#apply_emotional_contagion(emotion_valence:, intensity:) ⇒ Object



68
69
70
71
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb', line 68

def apply_emotional_contagion(emotion_valence:, intensity:, **)
  level = engine.emotional_contagion(emotion_valence: emotion_valence, intensity: intensity)
  { success: true, contagion_level: level.round(4), empathic_state: engine.empathic_state }
end

#cognitive_empathy_statsObject



95
96
97
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb', line 95

def cognitive_empathy_stats(**)
  { success: true }.merge(engine.to_h)
end

#current_empathic_stateObject



73
74
75
76
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb', line 73

def current_empathic_state(**)
  { success: true, empathic_state: engine.empathic_state,
    contagion_level: engine.contagion_level.round(4) }
end

#empathic_accuracy_for(agent_id:) ⇒ Object



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

def empathic_accuracy_for(agent_id:, **)
  accuracy = engine.empathic_accuracy(agent_id: agent_id)
  label = accuracy_label(accuracy)
  { success: true, agent_id: agent_id, accuracy: accuracy.round(4), label: label }
end

#empathic_blind_spotsObject



83
84
85
86
87
88
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb', line 83

def empathic_blind_spots(**)
  least = engine.least_accurate_agent
  most  = engine.most_accurate_agent
  { success: true, least_accurate_agent: least, most_accurate_agent: most,
    overall_accuracy: engine.overall_accuracy.round(4) }
end

#overall_empathic_accuracyObject



62
63
64
65
66
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb', line 62

def overall_empathic_accuracy(**)
  accuracy = engine.overall_accuracy
  label = accuracy_label(accuracy)
  { success: true, accuracy: accuracy.round(4), label: label }
end

#perspectives_for_agent(agent_id:) ⇒ Object



78
79
80
81
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb', line 78

def perspectives_for_agent(agent_id:, **)
  list = engine.perspectives_for(agent_id: agent_id).map(&:to_h)
  { success: true, agent_id: agent_id, perspectives: list, count: list.size }
end

#process_human_observations(human_observations: []) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb', line 13

def process_human_observations(human_observations: [], **)
  return { processed: 0 } if human_observations.empty?

  human_observations.each do |obs|
    identity  = obs[:identity].to_s
    bond_role = obs[:bond_role] || :unknown

    take_empathic_perspective(
      agent_id:         identity,
      perspective_type: :affective,
      predicted_state:  { bond_role: bond_role, channel: obs[:channel] },
      confidence:       bond_role == :partner ? 0.7 : 0.4
    )

    virulence = bond_role == :partner ? 0.3 : 0.05
    engine.emotional_contagion(emotion_valence: 0.5, intensity: virulence)
  end

  log.debug("[cognitive_empathy] process_human_observations: count=#{human_observations.size}")
  { processed: human_observations.size }
end

#record_empathic_outcome(perspective_id:, actual_state:) ⇒ Object



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

def record_empathic_outcome(perspective_id:, actual_state:, **)
  perspective = engine.record_outcome(perspective_id: perspective_id, actual_state: actual_state)
  return { success: false, reason: :not_found } unless perspective

  { success: true, perspective_id: perspective_id,
    accuracy: perspective.accuracy.round(4), accurate: perspective.accurate? }
end

#take_empathic_perspective(agent_id:, perspective_type:, predicted_state:, confidence: 0.5) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb', line 35

def take_empathic_perspective(agent_id:, perspective_type:, predicted_state:, confidence: 0.5, **)
  perspective = engine.take_perspective(
    agent_id:         agent_id,
    perspective_type: perspective_type,
    predicted_state:  predicted_state,
    confidence:       confidence
  )
  return { success: false, reason: :limit_or_invalid_type } unless perspective

  { success: true, perspective_id: perspective.id, agent_id: agent_id,
    perspective_type: perspective_type }
end

#update_cognitive_empathyObject



90
91
92
93
# File 'lib/legion/extensions/agentic/affect/cognitive_empathy/runners/cognitive_empathy.rb', line 90

def update_cognitive_empathy(**)
  engine.tick
  { success: true }.merge(engine.to_h)
end