Class: Brute::Tool

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

Overview

Base class for Brute's built-in tools — a tiny, framework-agnostic tool DSL. It intentionally mirrors the common tool-library shape (description + params) so tools read the same as they would in any LLM library, without depending on one:

class Shell < Brute::Tool
description "Execute a shell command"
param :command, type: 'string', desc: "The command", required: true

def name; "shell"; end

def execute(command:)
  ...
end
end

For tools whose arguments don't fit the flat param list, pass a raw JSON schema instead:

params({ type: 'object', properties: { ... }, required: [...] })

Instances expose the neutral interface Brute::Tools::Adapter understands: #name, #description, #params, #params_schema, #call.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.description(text = nil) ⇒ Object



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

def description(text = nil)
  if text
    @description = text
  else
    @description
  end
end

.param(name, type: "string", desc: nil, required: true, **opts) ⇒ Object

Declare one parameter: param :key, type:, desc:, required:



41
42
43
# File 'lib/brute/tool.rb', line 41

def param(name, type: "string", desc: nil, required: true, **opts)
  param_definitions[name.to_sym] = { type: type, desc: desc, required: required, **opts }.compact
end

.param_definitionsObject



45
46
47
# File 'lib/brute/tool.rb', line 45

def param_definitions
  @param_definitions ||= {}
end

.params(schema = nil) ⇒ Object

Raw JSON-schema override for complex argument shapes.



50
51
52
53
54
55
56
# File 'lib/brute/tool.rb', line 50

def params(schema = nil)
  if schema
    @params_schema = schema.deep_symbolize_keys
  else
    @params_schema
  end
end

Instance Method Details

#call(arguments = {}) ⇒ Object

Execute with a string- or symbol-keyed argument hash, as delivered by LLM providers.



80
81
82
# File 'lib/brute/tool.rb', line 80

def call(arguments = {})
  execute(**arguments.to_h.transform_keys(&:to_sym))
end

#descriptionObject



64
65
66
# File 'lib/brute/tool.rb', line 64

def description
  self.class.description.to_s
end

#executeObject

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/brute/tool.rb', line 84

def execute(**)
  raise NotImplementedError, "#{self.class} must implement #execute"
end

#nameObject

Tool name; subclasses usually override with an explicit short name.



60
61
62
# File 'lib/brute/tool.rb', line 60

def name
  self.class.name.demodulize.underscore
end

#paramsObject

{ key => { type:, desc:, required: } }



69
70
71
# File 'lib/brute/tool.rb', line 69

def params
  self.class.param_definitions
end

#params_schemaObject

The raw JSON schema, when declared via params(...).



74
75
76
# File 'lib/brute/tool.rb', line 74

def params_schema
  self.class.params
end