Class: Legion::Extensions::Agentic::Self::SelfTalk::Helpers::SelfTalkEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Self::SelfTalk::Helpers::SelfTalkEngine
- Defined in:
- lib/legion/extensions/agentic/self/self_talk/helpers/self_talk_engine.rb
Instance Attribute Summary collapse
-
#dialogues ⇒ Object
readonly
Returns the value of attribute dialogues.
-
#voices ⇒ Object
readonly
Returns the value of attribute voices.
Instance Method Summary collapse
- #active_dialogues ⇒ Object
- #add_turn(dialogue_id:, voice_id:, content:, position: :clarify, strength: 0.5) ⇒ Object
- #amplify_voice(voice_id:, amount: Constants::VOLUME_BOOST) ⇒ Object
- #conclude_dialogue(dialogue_id:, summary:) ⇒ Object
- #concluded_dialogues ⇒ Object
- #dampen_voice(voice_id:, amount: Constants::VOLUME_DECAY) ⇒ Object
- #deadlock_dialogue(dialogue_id:) ⇒ Object
- #dialogue_report(dialogue_id:) ⇒ Object
- #dominant_voice ⇒ Object
-
#initialize ⇒ SelfTalkEngine
constructor
A new instance of SelfTalkEngine.
- #quietest_voice ⇒ Object
- #register_voice(name:, voice_type:, volume: Constants::DEFAULT_VOLUME, bias_direction: nil) ⇒ Object
- #start_dialogue(topic:) ⇒ Object
- #to_h ⇒ Object
- #voice_balance ⇒ Object
Constructor Details
#initialize ⇒ SelfTalkEngine
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
#dialogues ⇒ Object (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 |
#voices ⇒ Object (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_dialogues ⇒ Object
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_dialogues ⇒ Object
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_voice ⇒ Object
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_voice ⇒ Object
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_h ⇒ Object
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_balance ⇒ Object
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 |