Class: Legion::Extensions::Agentic::Language::InnerSpeech::Helpers::SpeechStream

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb

Constant Summary

Constants included from Constants

Constants::AUTOMATIC_SPEED, Constants::CONDENSATION_RATIO, Constants::CONTROLLED_SPEED, Constants::EGOCENTRIC_SPEED, Constants::MAX_HISTORY, Constants::MAX_STREAM_LENGTH, Constants::MAX_UTTERANCES, Constants::RUMINATION_THRESHOLD, Constants::SALIENCE_DECAY, Constants::SALIENCE_FLOOR, Constants::SPEECH_MODES, Constants::URGENCY_LABELS, Constants::VOICE_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpeechStream

Returns a new instance of SpeechStream.



14
15
16
17
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 14

def initialize
  @utterances = []
  @counter    = 0
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



12
13
14
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 12

def counter
  @counter
end

#utterancesObject (readonly)

Returns the value of attribute utterances.



12
13
14
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 12

def utterances
  @utterances
end

Instance Method Details

#append(content:, mode: :narrating) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 19

def append(content:, mode: :narrating, **)
  return nil if @utterances.size >= MAX_STREAM_LENGTH

  @counter += 1
  utterance = Utterance.new(
    id:      :"utt_#{@counter}",
    content: content,
    mode:    mode,
    **
  )
  @utterances << utterance
  utterance
end

#by_mode(mode:) ⇒ Object



41
42
43
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 41

def by_mode(mode:)
  @utterances.select { |u| u.mode == mode }.map(&:to_h)
end

#by_topic(topic:) ⇒ Object



49
50
51
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 49

def by_topic(topic:)
  @utterances.select { |u| u.topic == topic }.map(&:to_h)
end

#by_voice(voice:) ⇒ Object



45
46
47
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 45

def by_voice(voice:)
  @utterances.select { |u| u.voice == voice }.map(&:to_h)
end

#clearObject



91
92
93
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 91

def clear
  @utterances.clear
end

#condensed_streamObject



83
84
85
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 83

def condensed_stream
  @utterances.select(&:salient?).map(&:condensed_content)
end

#currentObject



33
34
35
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 33

def current
  @utterances.last
end

#decay_allObject



74
75
76
77
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 74

def decay_all
  @utterances.each(&:decay_salience!)
  prune_stale
end

#interrupt(content:, mode: :warning) ⇒ Object



79
80
81
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 79

def interrupt(content:, mode: :warning, **)
  append(content: content, mode: mode, urgency: 0.9, salience: 0.9, **)
end

#narrativeObject



87
88
89
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 87

def narrative
  @utterances.map(&:content).join(' ')
end

#recent(count: 5) ⇒ Object



37
38
39
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 37

def recent(count: 5)
  @utterances.last(count).map(&:to_h)
end

#ruminating?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 61

def ruminating?
  return false if @utterances.size < RUMINATION_THRESHOLD

  recent_topics = @utterances.last(RUMINATION_THRESHOLD).map(&:topic)
  recent_topics.uniq.size == 1
end

#rumination_topicObject



68
69
70
71
72
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 68

def rumination_topic
  return nil unless ruminating?

  @utterances.last&.topic
end

#salientObject



53
54
55
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 53

def salient
  @utterances.select(&:salient?).map(&:to_h)
end

#sizeObject



95
96
97
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 95

def size
  @utterances.size
end

#to_hObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 99

def to_h
  {
    size:              @utterances.size,
    total_generated:   @counter,
    ruminating:        ruminating?,
    rumination_topic:  rumination_topic,
    salient_count:     @utterances.count(&:salient?),
    urgent_count:      @utterances.count(&:urgent?),
    mode_distribution: mode_distribution
  }
end

#urgentObject



57
58
59
# File 'lib/legion/extensions/agentic/language/inner_speech/helpers/speech_stream.rb', line 57

def urgent
  @utterances.select(&:urgent?).map(&:to_h)
end