Class: Legion::Extensions::Agentic::Self::SelfTalk::Helpers::SelfTalkEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSelfTalkEngine

Returns a new instance of SelfTalkEngine.



12
13
14
15
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 12

def initialize
  @voices    = {}
  @dialogues = {}
end

Instance Attribute Details

#dialoguesObject (readonly)

Returns the value of attribute dialogues.



10
11
12
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 10

def dialogues
  @dialogues
end

#voicesObject (readonly)

Returns the value of attribute voices.



10
11
12
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 10

def voices
  @voices
end

Instance Method Details

#active_dialoguesObject



73
74
75
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 73

def active_dialogues
  @dialogues.values.select(&:active?)
end

#add_turn(dialogue_id:, voice_id:, content:, position: :clarify, strength: 0.5) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 38

def add_turn(dialogue_id:, voice_id:, content:, position: :clarify, strength: 0.5)
  dialogue = @dialogues[dialogue_id]
  return { added: false, reason: :dialogue_not_found } unless dialogue

  voice = @voices[voice_id]
  return { added: false, reason: :voice_not_found } unless voice
  return { added: false, reason: :voice_inactive } unless voice.active

  turn = dialogue.add_turn!(
    voice_id: voice_id,
    content:  content,
    position: position,
    strength: strength
  )
  return { added: false, reason: :limit_reached_or_closed } unless turn

  { added: true, turn: turn.to_h }
end

#amplify_voice(voice_id:, amount: Constants::VOLUME_BOOST) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 104

def amplify_voice(voice_id:, amount: Constants::VOLUME_BOOST)
  voice = @voices[voice_id]
  return { amplified: false, reason: :not_found } unless voice

  voice.amplify!(amount)
  { amplified: true, voice_id: voice_id, volume: voice.volume.round(10) }
end

#conclude_dialogue(dialogue_id:, summary:) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 57

def conclude_dialogue(dialogue_id:, summary:)
  dialogue = @dialogues[dialogue_id]
  return { concluded: false, reason: :not_found } unless dialogue

  result = dialogue.conclude!(summary)
  { concluded: result, dialogue_id: dialogue_id }
end

#concluded_dialoguesObject



77
78
79
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 77

def concluded_dialogues
  @dialogues.values.select(&:concluded?)
end

#dampen_voice(voice_id:, amount: Constants::VOLUME_DECAY) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 112

def dampen_voice(voice_id:, amount: Constants::VOLUME_DECAY)
  voice = @voices[voice_id]
  return { dampened: false, reason: :not_found } unless voice

  voice.dampen!(amount)
  { dampened: true, voice_id: voice_id, volume: voice.volume.round(10) }
end

#deadlock_dialogue(dialogue_id:) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 65

def deadlock_dialogue(dialogue_id:)
  dialogue = @dialogues[dialogue_id]
  return { deadlocked: false, reason: :not_found } unless dialogue

  result = dialogue.deadlock!
  { deadlocked: result, dialogue_id: dialogue_id }
end

#dialogue_report(dialogue_id:) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 120

def dialogue_report(dialogue_id:)
  dialogue = @dialogues[dialogue_id]
  return { found: false } unless dialogue

  positions = dialogue.voice_positions.transform_keys do |vid|
    @voices[vid]&.name || vid
  end

  {
    found:           true,
    dialogue:        dialogue.to_h,
    voice_positions: positions
  }
end

#dominant_voiceObject



81
82
83
84
85
86
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 81

def dominant_voice
  active = @voices.values.select(&:active)
  return nil if active.empty?

  active.max_by(&:volume)
end

#quietest_voiceObject



88
89
90
91
92
93
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 88

def quietest_voice
  active = @voices.values.select(&:active)
  return nil if active.empty?

  active.min_by(&:volume)
end

#register_voice(name:, voice_type:, volume: Constants::DEFAULT_VOLUME, bias_direction: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 17

def register_voice(name:, voice_type:, volume: Constants::DEFAULT_VOLUME, bias_direction: nil)
  return { registered: false, reason: :max_voices } if @voices.size >= Constants::MAX_VOICES
  return { registered: false, reason: :unknown_type } unless Constants::VOICE_TYPES.include?(voice_type)

  voice = InnerVoice.new(
    name:           name,
    voice_type:     voice_type,
    volume:         volume,
    bias_direction: bias_direction
  )
  @voices[voice.id] = voice
  { registered: true, voice: voice.to_h }
end

#start_dialogue(topic:) ⇒ Object



31
32
33
34
35
36
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 31

def start_dialogue(topic:)
  prune_dialogues_if_needed
  dialogue = Dialogue.new(topic: topic)
  @dialogues[dialogue.id] = dialogue
  { started: true, dialogue: dialogue.to_h }
end

#to_hObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 135

def to_h
  {
    voice_count:           @voices.size,
    dialogue_count:        @dialogues.size,
    active_dialogue_count: active_dialogues.size,
    dominant_voice:        dominant_voice&.to_h,
    quietest_voice:        quietest_voice&.to_h,
    voice_balance:         voice_balance
  }
end

#voice_balanceObject



95
96
97
98
99
100
101
102
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb', line 95

def voice_balance
  return {} if @voices.empty?

  total = @voices.values.sum(&:volume)
  @voices.transform_values do |v|
    total.positive? ? (v.volume / total).round(10) : 0.0
  end
end