Module: Brute::Loop::AgentTurn

Defined in:
lib/brute/loop/agent_turn.rb

Overview

Factory + namespace for provider-specific agent turns.

An agent turn sends a message to the LLM, iterates over tool calls until there are none left, and returns the response. Each turn has its own job queue for tool execution (ParallelQueue of ToolCallSteps).

Usage:

step = AgentTurn.perform(agent:, session:, pipeline:, input:)

AgentTurn.perform detects the provider from the agent and returns the appropriate provider-specific Step subclass, already executed. The returned step has .state, .result, .error, etc.

Provider-specific subclasses live under AgentTurn

and override

supported_messages to filter the session’s message history per provider capability.

Defined Under Namespace

Classes: Anthropic, Base, Google, OpenAI

Class Method Summary collapse

Class Method Details

.detect(provider) ⇒ Object

Detect the right subclass from the provider.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/brute/loop/agent_turn.rb', line 44

def self.detect(provider)
  if provider
    provider.class.name.to_s.downcase.then do |class_name|
      if class_name.include?("anthropic")
        Anthropic
      elsif class_name.include?("openai")
        OpenAI
      elsif class_name.include?("google") || class_name.include?("gemini")
        Google
      else
        Base
      end
    end
  else
    Base
  end
end

.new(agent:, session:, pipeline:, input: nil, callbacks: {}, **rest) ⇒ Object

Build and return the right AgentTurn step for this agent’s provider. Does NOT execute it — call step.call(task) yourself, or enqueue it.



29
30
31
32
# File 'lib/brute/loop/agent_turn.rb', line 29

def self.new(agent:, session:, pipeline:, input: nil, callbacks: {}, **rest)
  klass = detect(agent.provider)
  klass.new(agent: agent, session: session, pipeline: pipeline, input: input, callbacks: callbacks, **rest)
end

.perform(agent:, session:, pipeline:, input: nil, callbacks: {}, **rest) ⇒ Object

Build, execute inside a Sync block, return the finished step.



35
36
37
38
39
40
41
# File 'lib/brute/loop/agent_turn.rb', line 35

def self.perform(agent:, session:, pipeline:, input: nil, callbacks: {}, **rest)
  step = self.new(agent: agent, session: session, pipeline: pipeline, input: input, callbacks: callbacks, **rest)
  Sync do
    step.call(Async::Task.current)
  end
  step
end