Class: RubyCode::Client::ResponseHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycode/client/response_handler.rb

Overview

Handles response scenarios (max iterations, empty tool calls, etc.)

Constant Summary collapse

MAX_ITERATIONS =
25
MAX_TOOL_CALLS =
50

Instance Method Summary collapse

Constructor Details

#initialize(memory:, config:) ⇒ ResponseHandler

Returns a new instance of ResponseHandler.



10
11
12
13
# File 'lib/rubycode/client/response_handler.rb', line 10

def initialize(memory:, config:)
  @memory = memory
  @config = config
end

Instance Method Details

#finalize_response(done_result, iteration, total_tool_calls) ⇒ Object



37
38
39
40
41
# File 'lib/rubycode/client/response_handler.rb', line 37

def finalize_response(done_result, iteration, total_tool_calls)
  puts Views::ResponseHandler::AgentFinished.build(iteration: iteration,
                                                   total_tool_calls: total_tool_calls + 1)
  done_result
end

#handle_empty_tool_calls(content, iteration, total_tool_calls) ⇒ Object



23
24
25
26
27
# File 'lib/rubycode/client/response_handler.rb', line 23

def handle_empty_tool_calls(content, iteration, total_tool_calls)
  puts Views::ResponseHandler::CompleteMessage.build(iteration: iteration,
                                                     total_tool_calls: total_tool_calls)
  content
end

#handle_max_iterations(_iteration) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rubycode/client/response_handler.rb', line 15

def handle_max_iterations(_iteration)
  puts Views::ResponseHandler::MaxIterations.build(max_iterations: MAX_ITERATIONS)

  error_msg = I18n.t("rubycode.errors.max_iterations_reached")
  @memory.add_message(role: "assistant", content: error_msg)
  error_msg
end

#handle_max_tool_calls(content, _total_tool_calls) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/rubycode/client/response_handler.rb', line 29

def handle_max_tool_calls(content, _total_tool_calls)
  puts Views::ResponseHandler::MaxToolCalls.build(max_tool_calls: MAX_TOOL_CALLS)

  error_msg = I18n.t("rubycode.errors.max_tool_calls_reached")
  @memory.add_message(role: "assistant", content: error_msg)
  content.empty? ? error_msg : content
end