Class: SolidAgent::HasTools::ToolBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_agent/has_tools.rb

Overview

DSL builder for inline tool definitions.

Provides a clean API for defining tool schemas programmatically:

tool :my_tool do
description "Does something useful"
parameter :input, type: :string, required: true
end

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ToolBuilder

Returns a new instance of ToolBuilder.



193
194
195
196
197
198
# File 'lib/solid_agent/has_tools.rb', line 193

def initialize(name)
  @name = name.to_s
  @description = ""
  @parameters = {}
  @required = []
end

Instance Method Details

#description(text) ⇒ void

This method returns an undefined value.

Sets the tool description.

Parameters:

  • text (String)

    human-readable description of what the tool does



204
205
206
# File 'lib/solid_agent/has_tools.rb', line 204

def description(text)
  @description = text
end

#parameter(name, type:, required: false, description: nil, enum: nil, items: nil, properties: nil, default: nil) ⇒ void

This method returns an undefined value.

Defines a parameter for the tool.

Examples:

Simple string parameter

parameter :query, type: :string, required: true

Enum parameter

parameter :format, type: :string, enum: %w[json xml csv]

Array parameter

parameter :tags, type: :array, items: { type: :string }

Parameters:

  • name (Symbol, String)

    parameter name

  • type (Symbol, String)

    JSON Schema type (:string, :integer, :boolean, :array, :object)

  • required (Boolean) (defaults to: false)

    whether this parameter is required (default: false)

  • description (String) (defaults to: nil)

    parameter description

  • enum (Array) (defaults to: nil)

    allowed values (for string type)

  • items (Hash) (defaults to: nil)

    item schema (for array type)

  • properties (Hash) (defaults to: nil)

    nested properties (for object type)

  • default (Object) (defaults to: nil)

    default value



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/solid_agent/has_tools.rb', line 228

def parameter(name, type:, required: false, description: nil, enum: nil, items: nil, properties: nil, default: nil)
  param_schema = { type: type.to_s }
  param_schema[:description] = description if description
  param_schema[:enum] = enum if enum
  param_schema[:items] = items if items
  param_schema[:properties] = properties if properties
  param_schema[:default] = default if default

  @parameters[name.to_s] = param_schema
  @required << name.to_s if required
end

#to_schemaHash

Converts the builder state to an OpenAI-compatible tool schema.

Returns:

  • (Hash)

    tool schema



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/solid_agent/has_tools.rb', line 243

def to_schema
  {
    type: "function",
    name: @name,
    description: @description,
    parameters: {
      type: "object",
      properties: @parameters,
      required: @required
    }.compact
  }
end