Class: Brute::Middleware::ToolResultLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/brute/middleware/003_tool_result_loop.rb

Overview

Re-invokes the inner stack whenever the last message is a :tool result.

After the inner pipeline runs (LLMCall responds, ToolCall executes tools and appends :tool messages), this middleware checks if tool results are pending. If so, it increments the iteration counter and loops — sending the tool results back through MaxIterations → ToolCall → LLMCall so the LLM can see them.

The loop breaks when:

- The LLM responds with text only (no tool calls) — last message is :assistant
- env[:should_exit] is set (e.g. by MaxIterations)

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ToolResultLoop

Returns a new instance of ToolResultLoop.



21
22
23
# File 'lib/brute/middleware/003_tool_result_loop.rb', line 21

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/brute/middleware/003_tool_result_loop.rb', line 25

def call(env)
  loop do
    @app.call(env)

    break if env[:should_exit]
    break unless env[:messages].last&.role == :tool

    env[:current_iteration] += 1
  end

  env
end