Class: Brute::Tool

Inherits:
Pipeline show all
Defined in:
lib/brute/tool.rb

Overview

A Tool is a Pipeline configured for tool execution. The terminal app does the work; middleware wraps it with concerns like file mutation queueing, snapshotting, validation, logging.

Coexists with Brute::Tools::* (which inherit from RubyLLM::Tool). Use Brute::Tool when you want middleware. Use RubyLLM::Tool subclasses for simple cases.

Usage:

read = Brute::Tool.new(
  name:        "read",
  description: "Read a file's contents",
  params:      { file_path: { type: "string", required: true } },
) do
  use Brute::Middleware::Tool::ValidateParams
  run ->(env) {
    env[:result] = File.read(File.expand_path(env[:arguments][:file_path]))
  }
end

read.call(file_path: "lib/brute.rb")

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Pipeline

#build, #run, #use

Constructor Details

#initialize(name:, description:, params: {}, &block) ⇒ Tool

Returns a new instance of Tool.



34
35
36
37
38
39
# File 'lib/brute/tool.rb', line 34

def initialize(name:, description:, params: {}, &block)
  @name        = name.to_s
  @description = description
  @params      = params
  super(&block)
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



32
33
34
# File 'lib/brute/tool.rb', line 32

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/brute/tool.rb', line 32

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



32
33
34
# File 'lib/brute/tool.rb', line 32

def params
  @params
end

Instance Method Details

#call(events: NullSink.new, **arguments) ⇒ Object

Execute the tool. Arguments come in as kwargs; result is whatever the terminal app puts into env.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/brute/tool.rb', line 43

def call(events: NullSink.new, **arguments)
  env = {
    name:      @name,
    arguments: arguments,
    result:    nil,
    events:    events,
    metadata:  {},
  }
  super(env)
  env[:result]
end

#to_ruby_llmObject

Adapter so the LLM can call this tool through ruby_llm. ToolCall middleware checks for to_ruby_llm and uses it if present.



57
58
59
60
61
62
63
64
65
# File 'lib/brute/tool.rb', line 57

def to_ruby_llm
  tool = self
  Class.new(RubyLLM::Tool) do
    description tool.description
    tool.params.each { |k, opts| param k, **opts }
    define_method(:name) { tool.name }
    define_method(:execute) { |**args| tool.call(**args) }
  end.new
end