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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModelStore

Returns a new instance of ModelStore.



12
13
14
15
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 12

def initialize
  @models = {}
  @dirty = false
end

Instance Attribute Details

#modelsObject (readonly)

Returns the value of attribute models.



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

def models
  @models
end

Instance Method Details

#all_modelsObject



115
116
117
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 115

def all_models
  @models.values
end

#by_cooperation(stance) ⇒ Object



119
120
121
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 119

def by_cooperation(stance)
  @models.values.select { |m| m.cooperation_stance == stance }
end

#by_emotion(emotion) ⇒ Object



123
124
125
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 123

def by_emotion(emotion)
  @models.values.select { |m| m.emotional_state == emotion }
end

#clearObject



131
132
133
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 131

def clear
  @models.clear
end

#decay_allObject



100
101
102
103
104
105
106
107
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 100

def decay_all
  count = 0
  @models.each_value do |model|
    model.decay
    count += 1
  end
  count
end

#dirty?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 52

def dirty?
  @dirty
end

#from_apollo(store:) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 73

def from_apollo(store:)
  entries = store.query(tags: %w[empathy mental_model])
  entries.each do |entry|
    data = ::JSON.parse(entry[:content])
    agent_id = data['agent_id']
    next unless agent_id

    bond_role  = data['bond_role']&.to_sym || :unknown
    channel    = data['channel']&.to_sym
    confidence = data['confidence_level']

    model = MentalModel.new(agent_id: agent_id, bond_role: bond_role,
                            channel: channel, confidence: confidence)
    @models[agent_id] = model
  rescue ::JSON::ParserError => e
    warn "[empathy] from_apollo: skipping invalid entry: #{e.message}"
    next
  end
end

#get(agent_id) ⇒ Object



17
18
19
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 17

def get(agent_id)
  @models[agent_id.to_s]
end

#get_or_create(agent_id) ⇒ Object



21
22
23
24
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 21

def get_or_create(agent_id)
  key = agent_id.to_s
  @models[key] ||= MentalModel.new(agent_id: key)
end

#mark_clean!Object



56
57
58
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 56

def mark_clean!
  @dirty = false
end

#predict(agent_id, scenario) ⇒ Object



93
94
95
96
97
98
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 93

def predict(agent_id, scenario)
  model = get(agent_id)
  return nil unless model

  model.predict_reaction(scenario)
end

#remove_staleObject



109
110
111
112
113
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 109

def remove_stale
  stale_keys = @models.select { |_, m| m.stale? && m.interaction_history.empty? }.keys
  stale_keys.each { |k| @models.delete(k) }
  stale_keys.size
end

#sizeObject



127
128
129
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 127

def size
  @models.size
end

#to_apollo_entriesObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 60

def to_apollo_entries
  @models.values.map do |model|
    data = model.to_h.merge(
      created_at: model.created_at.iso8601,
      updated_at: model.updated_at.iso8601
    )
    {
      content: ::JSON.generate(data.transform_keys(&:to_s)),
      tags:    ['empathy', 'mental_model', model.agent_id]
    }
  end
end

#update(agent_id, observation) ⇒ Object



26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 26

def update(agent_id, observation)
  model = get_or_create(agent_id)
  model.update_from_observation(observation)
  evict_if_needed
  model
end

#update_from_human_observation(observation) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/legion/extensions/agentic/affect/empathy/helpers/model_store.rb', line 33

def update_from_human_observation(observation)
  identity   = observation[:identity].to_s
  bond_role  = observation[:bond_role] || :unknown
  channel    = observation[:channel]

  key = identity
  model = @models[key] ||= MentalModel.new(agent_id: key, bond_role: bond_role, channel: channel)
  evidence = bond_role == :partner ? 0.8 : 0.5
  model.update_from_observation(
    interaction_type:  :human_observation,
    evidence_strength: evidence,
    summary:           "channel=#{channel} content_type=#{observation[:content_type]} " \
                       "length=#{observation[:content_length]}"
  )
  evict_if_needed
  @dirty = true
  model
end