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
# File 'lib/brute/tool.rb', line 32

def description(text = nil)
  return @description unless text

  @description = text
end

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

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



39
40
41
# File 'lib/brute/tool.rb', line 39

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



43
44
45
# File 'lib/brute/tool.rb', line 43

def param_definitions
  @param_definitions ||= {}
end

.params(schema = nil) ⇒ Object

Raw JSON-schema override for complex argument shapes.



48
49
50
51
52
# File 'lib/brute/tool.rb', line 48

def params(schema = nil)
  return @params_schema unless schema

  @params_schema = schema.deep_symbolize_keys
end

Instance Method Details

#call(arguments = {}) ⇒ Object

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



76
77
78
# File 'lib/brute/tool.rb', line 76

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

#descriptionObject



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

def description
  self.class.description.to_s
end

#executeObject

Raises:

  • (NotImplementedError)


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

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

#nameObject

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



56
57
58
# File 'lib/brute/tool.rb', line 56

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

#paramsObject

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



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

def params
  self.class.param_definitions
end

#params_schemaObject

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



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

def params_schema
  self.class.params
end