Class: Legion::Extensions::Agentic::Memory::CommunicationPattern::Helpers::PatternAnalyzer
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Memory::CommunicationPattern::Helpers::PatternAnalyzer
- Defined in:
- lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb
Instance Attribute Summary collapse
-
#agent_id ⇒ Object
readonly
Returns the value of attribute agent_id.
-
#trace_count ⇒ Object
readonly
Returns the value of attribute trace_count.
Instance Method Summary collapse
- #channel_preference ⇒ Object
- #consistency ⇒ Object
- #day_of_week_distribution ⇒ Object
- #direct_address_frequency ⇒ Object
- #dirty? ⇒ Boolean
- #from_apollo(store:) ⇒ Object
-
#initialize(agent_id:) ⇒ PatternAnalyzer
constructor
A new instance of PatternAnalyzer.
- #mark_clean! ⇒ Object
- #time_of_day_distribution ⇒ Object
- #to_apollo_entries ⇒ Object
- #topic_clustering ⇒ Object
- #update_from_traces(traces) ⇒ Object
Constructor Details
#initialize(agent_id:) ⇒ PatternAnalyzer
Returns a new instance of PatternAnalyzer.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 12 def initialize(agent_id:) @agent_id = agent_id @tod_histogram = Array.new(Constants::HOURS_IN_DAY, 0) @dow_histogram = Array.new(Constants::DAYS_IN_WEEK, 0) @channel_counts = Hash.new(0) @direct_count = 0 @topic_counts = Hash.new(0) @trace_count = 0 @dirty = false end |
Instance Attribute Details
#agent_id ⇒ Object (readonly)
Returns the value of attribute agent_id.
10 11 12 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 10 def agent_id @agent_id end |
#trace_count ⇒ Object (readonly)
Returns the value of attribute trace_count.
10 11 12 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 10 def trace_count @trace_count end |
Instance Method Details
#channel_preference ⇒ Object
36 37 38 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 36 def channel_preference @channel_counts.sort_by { |_k, v| -v }.map(&:first) end |
#consistency ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 50 def consistency return 0.0 if @trace_count < Constants::MIN_TRACES_FOR_PATTERN channel_entropy = normalized_entropy(@channel_counts.values) tod_entropy = normalized_entropy(@tod_histogram) (1.0 - ((channel_entropy + tod_entropy) / 2.0)).clamp(0.0, 1.0) end |
#day_of_week_distribution ⇒ Object
32 33 34 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 32 def day_of_week_distribution @dow_histogram.dup end |
#direct_address_frequency ⇒ Object
40 41 42 43 44 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 40 def direct_address_frequency return 0.0 if @trace_count.zero? @direct_count.to_f / @trace_count end |
#dirty? ⇒ Boolean
58 59 60 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 58 def dirty? @dirty end |
#from_apollo(store:) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 72 def from_apollo(store:) result = store.query(text: 'communication_pattern', tags: Constants::TAG_PREFIX + [@agent_id]) return false unless result[:success] && result[:results]&.any? parsed = deserialize(result[:results].first[:content]) return false unless parsed restore_state(parsed) true rescue StandardError => e warn "[pattern_analyzer] from_apollo error: #{e.}" false end |
#mark_clean! ⇒ Object
62 63 64 65 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 62 def mark_clean! @dirty = false self end |
#time_of_day_distribution ⇒ Object
28 29 30 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 28 def time_of_day_distribution @tod_histogram.dup end |
#to_apollo_entries ⇒ Object
67 68 69 70 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 67 def to_apollo_entries = Constants::TAG_PREFIX.dup + [@agent_id] [{ content: serialize(state_hash), tags: }] end |
#topic_clustering ⇒ Object
46 47 48 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 46 def topic_clustering @topic_counts.dup end |
#update_from_traces(traces) ⇒ Object
23 24 25 26 |
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 23 def update_from_traces(traces) traces.each { |t| process_trace(t) } @dirty = true end |