Class: Legion::Extensions::Agentic::Social::TheoryOfMind::Helpers::AgentModel

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_id) ⇒ AgentModel

Returns a new instance of AgentModel.



13
14
15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 13

def initialize(agent_id)
  @agent_id            = agent_id
  @beliefs             = {}
  @desires             = []
  @intentions          = []
  @prediction_accuracy = 0.5
  @interaction_count   = 0
  @created_at          = Time.now.utc
  @updated_at          = Time.now.utc
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



10
11
12
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 10

def agent_id
  @agent_id
end

#beliefsObject (readonly)

Returns the value of attribute beliefs.



10
11
12
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 10

def beliefs
  @beliefs
end

#created_atObject (readonly)

Returns the value of attribute created_at.



10
11
12
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 10

def created_at
  @created_at
end

#desiresObject (readonly)

Returns the value of attribute desires.



10
11
12
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 10

def desires
  @desires
end

#intentionsObject (readonly)

Returns the value of attribute intentions.



10
11
12
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 10

def intentions
  @intentions
end

#interaction_countObject (readonly)

Returns the value of attribute interaction_count.



10
11
12
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 10

def interaction_count
  @interaction_count
end

#prediction_accuracyObject (readonly)

Returns the value of attribute prediction_accuracy.



10
11
12
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 10

def prediction_accuracy
  @prediction_accuracy
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



10
11
12
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 10

def updated_at
  @updated_at
end

Instance Method Details

#belief_for(domain) ⇒ Object



59
60
61
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 59

def belief_for(domain)
  @beliefs[domain]
end

#decay_beliefsObject



98
99
100
101
102
103
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 98

def decay_beliefs
  @beliefs.each do |domain, belief|
    belief[:confidence] -= Constants::BELIEF_DECAY_RATE
    @beliefs.delete(domain) if belief[:confidence] < Constants::CONFIDENCE_THRESHOLD
  end
end

#false_beliefs(known_truths) ⇒ Object



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

def false_beliefs(known_truths)
  @beliefs.each_with_object({}) do |(domain, belief), result|
    truth = known_truths[domain]
    next unless truth
    next if truth == belief[:content]

    result[domain] = {
      agent_believes: belief[:content],
      actual_truth:   truth,
      confidence:     belief[:confidence]
    }
  end
end

#most_likely_intentionObject



67
68
69
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 67

def most_likely_intention
  @intentions.max_by { |i| Constants::INTENTION_CONFIDENCE_LEVELS[i[:confidence]] || 0 }
end

#perspectiveObject



105
106
107
108
109
110
111
112
113
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 105

def perspective
  {
    knowledge:       @beliefs.transform_values { |b| b[:content] },
    goals:           @desires.map { |d| d[:goal] },
    emotional_state: infer_emotional_state,
    constraints:     infer_constraints,
    recent_actions:  @intentions.last(5).map { |i| i[:action] }
  }
end

#strongest_desireObject



63
64
65
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 63

def strongest_desire
  @desires.max_by { |d| Constants::DESIRE_PRIORITIES[d[:priority]] || 0 }
end

#to_hObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 115

def to_h
  {
    agent_id:            @agent_id,
    belief_count:        @beliefs.size,
    desire_count:        @desires.size,
    intention_count:     @intentions.size,
    prediction_accuracy: @prediction_accuracy.round(4),
    interaction_count:   @interaction_count,
    strongest_desire:    strongest_desire&.dig(:goal),
    likely_action:       most_likely_intention&.dig(:action)
  }
end

#update_belief(domain:, content:, confidence:, source: :inference) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 24

def update_belief(domain:, content:, confidence:, source: :inference)
  @beliefs[domain] = {
    content:    content,
    confidence: confidence.clamp(0.0, 1.0),
    source:     source,
    updated_at: Time.now.utc
  }
  trim_beliefs
  touch
end

#update_desire(goal:, priority: :medium) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 35

def update_desire(goal:, priority: :medium)
  existing = @desires.find { |d| d[:goal] == goal }
  if existing
    existing[:priority]    = priority
    existing[:observed_at] = Time.now.utc
  else
    @desires << { goal: goal, priority: priority, observed_at: Time.now.utc }
    trim_desires
  end
  touch
end

#update_intention(action:, confidence: :possible) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/agentic/social/theory_of_mind/helpers/agent_model.rb', line 47

def update_intention(action:, confidence: :possible)
  existing = @intentions.find { |i| i[:action] == action }
  if existing
    existing[:confidence]    = confidence
    existing[:estimated_at]  = Time.now.utc
  else
    @intentions << { action: action, confidence: confidence, estimated_at: Time.now.utc }
    trim_intentions
  end
  touch
end

#update_prediction_accuracy(outcome) ⇒ Object



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

def update_prediction_accuracy(outcome)
  score = case outcome
          when :correct           then 1.0
          when :partially_correct then 0.5
          when :incorrect         then 0.0
          else return
          end
  alpha = Constants::PREDICTION_ALPHA
  @prediction_accuracy = (@prediction_accuracy * (1.0 - alpha)) + (score * alpha)
  @interaction_count += 1
  touch
end