Class: AIA::DebateHandler

Inherits:
Object
  • Object
show all
Includes:
ContentExtractor, HandlerProtocol
Defined in:
lib/aia/debate_handler.rb

Defined Under Namespace

Classes: FailedResponse

Constant Summary collapse

MAX_ROUNDS =
5
MIN_ROUNDS =
2
SIMILARITY_THRESHOLD =
0.85
CONVERGENCE_SIGNAL =
"CONVERGED"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContentExtractor

#extract_content, #extract_network_content, #format_duration, #output_to_file, #present_result, #store_results_in_memory

Constructor Details

#initialize(robot:, ui_presenter:, tracker:) ⇒ DebateHandler

Returns a new instance of DebateHandler.



27
28
29
30
31
# File 'lib/aia/debate_handler.rb', line 27

def initialize(robot:, ui_presenter:, tracker:)
  @robot        = robot
  @ui_presenter = ui_presenter
  @tracker      = tracker
end

Instance Attribute Details

#robot=(value) ⇒ Object (writeonly)

Sets the attribute robot

Parameters:

  • value

    the value to set the attribute robot to.



33
34
35
# File 'lib/aia/debate_handler.rb', line 33

def robot=(value)
  @robot = value
end

Instance Method Details

#handle(context) ⇒ String?

Run a debate between robots in the network.

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Parameters:

Returns:

  • (String, nil)

    formatted debate results, or nil if not applicable



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/aia/debate_handler.rb', line 40

def handle(context)
  prompt = context.prompt
  return nil unless @robot.network?

  robots = @robot.crew
  return nil if robots.size < 2

  # Ensure all robots share a bus
  bus = TypedBus::MessageBus.new
  robots.each { |r| r.with_bus(bus) }

  @ui_presenter.display_info(
    "Debate between #{robots.map(&:name).join(', ')}..."
  )

  rounds = []

  MAX_ROUNDS.times do |round|
    round_context = build_round_context(prompt, rounds)

    round_results = Sync do
      barrier = Async::Barrier.new
      tasks = robots.map do |robot|
        barrier.async do
          @ui_presenter.display_info("  Round #{round + 1}: #{robot.name}...")
          result  = robot.run(round_context, mcp: :inherit, tools: :inherit)
          content = extract_content(result)
          write_to_memory(round, robot.name, content)
          { robot: robot.name, content: content }
        rescue => e
          FailedResponse.new(robot_name: robot.name, error_message: e.message)
        end
      end
      barrier.wait
      tasks.map(&:wait)
    end

    raise DebateError, "All robots failed in round #{round + 1}" if
      round_results.all?(FailedResponse)

    previous = rounds.last
    rounds << round_results

    if converged?(round, round_results, previous)
      @ui_presenter.display_info("  Converged in round #{round + 1}.")
      break
    end
  end

  @tracker.record_turn(
    model: AIA.config.models.first.name,
    input: prompt,
    result: rounds
  )

  format_rounds(rounds)
end