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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(topic:) ⇒ Dialogue

Returns a new instance of Dialogue.



14
15
16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 14

def initialize(topic:)
  @id         = SecureRandom.uuid
  @topic      = topic
  @turns      = []
  @status     = :open
  @conclusion = nil
  @created_at = Time.now.utc
end

Instance Attribute Details

#conclusionObject (readonly)

Returns the value of attribute conclusion.



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

def conclusion
  @conclusion
end

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#topicObject (readonly)

Returns the value of attribute topic.



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

def topic
  @topic
end

#turnsObject (readonly)

Returns the value of attribute turns.



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

def turns
  @turns
end

Instance Method Details

#abandon!Object



53
54
55
56
57
58
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 53

def abandon!
  return false unless active?

  @status = :abandoned
  true
end

#active?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 60

def active?
  @status == :open
end

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 23

def add_turn!(voice_id:, content:, position: :clarify, strength: 0.5)
  return false if @turns.size >= Constants::MAX_TURNS_PER_DIALOGUE
  return false unless active?

  turn = DialogueTurn.new(
    dialogue_id: @id,
    voice_id:    voice_id,
    content:     content,
    position:    position,
    strength:    strength
  )
  @turns << turn
  turn
end

#conclude!(summary) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 38

def conclude!(summary)
  return false unless active?

  @conclusion = summary
  @status     = :concluded
  true
end

#concluded?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 64

def concluded?
  @status == :concluded
end

#consensus_labelObject



91
92
93
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 91

def consensus_label
  Constants.consensus_label(consensus_score)
end

#consensus_scoreObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 79

def consensus_score
  return 1.0 if @turns.empty?

  support_strength = @turns.select { |t| t.position == :support }.sum(&:strength)
  oppose_strength  = @turns.select { |t| t.position == :oppose }.sum(&:strength)
  total            = support_strength + oppose_strength
  return 0.5 if total.zero?

  stronger = [support_strength, oppose_strength].max
  stronger / total
end

#deadlock!Object



46
47
48
49
50
51
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 46

def deadlock!
  return false unless active?

  @status = :deadlocked
  true
end

#to_hObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 95

def to_h
  {
    id:              @id,
    topic:           @topic,
    status:          @status,
    conclusion:      @conclusion,
    turn_count:      turn_count,
    consensus_score: consensus_score.round(10),
    consensus_label: consensus_label,
    created_at:      @created_at,
    turns:           @turns.map(&:to_h)
  }
end

#turn_countObject



68
69
70
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 68

def turn_count
  @turns.size
end

#voice_positionsObject



72
73
74
75
76
77
# File 'lib/legion/extensions/agentic/self/self_talk/helpers/dialogue.rb', line 72

def voice_positions
  grouped = @turns.group_by(&:voice_id)
  grouped.transform_values do |voice_turns|
    voice_turns.sum(&:strength) / voice_turns.size.to_f
  end
end