Class: Legion::Extensions::Agentic::Affect::Empathy::Helpers::MentalModel

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_id:, bond_role: :unknown, channel: nil, confidence: nil) ⇒ MentalModel

Returns a new instance of MentalModel.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 14

def initialize(agent_id:, bond_role: :unknown, channel: nil, confidence: nil)
  @agent_id = agent_id
  @bond_role = bond_role
  @channel = channel
  @believed_goal = nil
  @emotional_state = :unknown
  @attention_focus = nil
  @confidence_level = confidence || partner_default_confidence(bond_role)
  @cooperation_stance = :unknown
  @interaction_history = []
  @predictions = []
  @prediction_outcomes = []
  @created_at = Time.now.utc
  @updated_at = @created_at
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def agent_id
  @agent_id
end

#attention_focusObject (readonly)

Returns the value of attribute attention_focus.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def attention_focus
  @attention_focus
end

#believed_goalObject (readonly)

Returns the value of attribute believed_goal.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def believed_goal
  @believed_goal
end

#bond_roleObject (readonly)

Returns the value of attribute bond_role.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def bond_role
  @bond_role
end

#channelObject (readonly)

Returns the value of attribute channel.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def channel
  @channel
end

#confidence_levelObject (readonly)

Returns the value of attribute confidence_level.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def confidence_level
  @confidence_level
end

#cooperation_stanceObject (readonly)

Returns the value of attribute cooperation_stance.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def cooperation_stance
  @cooperation_stance
end

#created_atObject (readonly)

Returns the value of attribute created_at.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def created_at
  @created_at
end

#emotional_stateObject (readonly)

Returns the value of attribute emotional_state.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def emotional_state
  @emotional_state
end

#interaction_historyObject (readonly)

Returns the value of attribute interaction_history.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def interaction_history
  @interaction_history
end

#predictionsObject (readonly)

Returns the value of attribute predictions.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def predictions
  @predictions
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



10
11
12
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 10

def updated_at
  @updated_at
end

Instance Method Details

#decayObject



81
82
83
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 81

def decay
  @confidence_level = [(@confidence_level - Constants::MODEL_DECAY_RATE), 0.1].max
end

#predict_reaction(scenario) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 42

def predict_reaction(scenario)
  prediction = {
    prediction_id:     SecureRandom.uuid,
    scenario:          scenario,
    predicted_at:      Time.now.utc,
    likely_response:   infer_response(scenario),
    emotional_shift:   infer_emotional_shift(scenario),
    cooperation_shift: infer_cooperation_shift(scenario),
    confidence:        prediction_confidence
  }

  @predictions << prediction
  @predictions = @predictions.last(Constants::MAX_INTERACTION_HISTORY)
  prediction
end

#prediction_accuracyObject



70
71
72
73
74
75
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 70

def prediction_accuracy
  return nil if @prediction_outcomes.empty?

  correct = @prediction_outcomes.count { |o| o[:accurate] }
  correct.to_f / @prediction_outcomes.size
end

#record_prediction_outcome(prediction_id:, actual_response:, accurate:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 58

def record_prediction_outcome(prediction_id:, actual_response:, accurate:)
  pred = @predictions.find { |p| p[:prediction_id] == prediction_id }
  return nil unless pred

  pred[:actual_response] = actual_response
  pred[:accurate] = accurate

  @prediction_outcomes << { prediction_id: prediction_id, accurate: accurate, at: Time.now.utc }
  @prediction_outcomes = @prediction_outcomes.last(Constants::ACCURACY_WINDOW)
  accurate
end

#stale?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 77

def stale?
  (Time.now.utc - @updated_at) > Constants::MODEL_STALENESS_THRESHOLD
end

#to_hObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 85

def to_h
  {
    agent_id:            @agent_id,
    bond_role:           @bond_role,
    channel:             @channel,
    believed_goal:       @believed_goal,
    emotional_state:     @emotional_state,
    attention_focus:     @attention_focus,
    confidence_level:    @confidence_level,
    cooperation_stance:  @cooperation_stance,
    interactions:        @interaction_history.size,
    predictions_made:    @predictions.size,
    prediction_accuracy: prediction_accuracy,
    stale:               stale?,
    created_at:          @created_at,
    updated_at:          @updated_at
  }
end

#update_from_observation(observation) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/mental_model.rb', line 30

def update_from_observation(observation)
  @updated_at = Time.now.utc

  update_believed_goal(observation[:goal]) if observation[:goal]
  update_emotional_state(observation[:emotion]) if observation[:emotion]
  update_attention(observation[:attention]) if observation[:attention]
  update_cooperation(observation[:cooperation]) if observation[:cooperation]
  update_confidence(observation)

  record_interaction(observation)
end