Class: OpenRouter::Tool::ItemsBuilder

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

Overview

Internal class for building array items

Instance Method Summary collapse

Constructor Details

#initializeItemsBuilder

Returns a new instance of ItemsBuilder.



167
168
169
# File 'lib/open_router/tool.rb', line 167

def initialize
  @items = {}
end

Instance Method Details

#boolean(description: nil, **options) ⇒ Object



183
184
185
# File 'lib/open_router/tool.rb', line 183

def boolean(description: nil, **options)
  @items = { type: "boolean", description: }.merge(options).compact
end

#integer(description: nil, **options) ⇒ Object



175
176
177
# File 'lib/open_router/tool.rb', line 175

def integer(description: nil, **options)
  @items = { type: "integer", description: }.merge(options).compact
end

#items(schema = nil, &block) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/open_router/tool.rb', line 201

def items(schema = nil, &block)
  # This method allows for `array { items { object { ... }}}` or `items(hash)`
  if block_given?
    # Block defines what each item in the array looks like
    nested_builder = ItemsBuilder.new
    nested_builder.instance_eval(&block)
    @items = nested_builder.to_h
  elsif schema.is_a?(Hash)
    # Direct hash schema assignment
    @items = schema
  else
    raise ArgumentError, "items must be called with either a hash or a block"
  end
end

#number(description: nil, **options) ⇒ Object



179
180
181
# File 'lib/open_router/tool.rb', line 179

def number(description: nil, **options)
  @items = { type: "number", description: }.merge(options).compact
end

#object(&block) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/open_router/tool.rb', line 187

def object(&block)
  @items = {
    type: "object",
    properties: {},
    required: [],
    additionalProperties: false
  }

  return unless block_given?

  nested = Tool::ParametersBuilder.new(@items)
  nested.instance_eval(&block)
end

#string(description: nil, **options) ⇒ Object



171
172
173
# File 'lib/open_router/tool.rb', line 171

def string(description: nil, **options)
  @items = { type: "string", description: }.merge(options).compact
end

#to_hObject



216
217
218
# File 'lib/open_router/tool.rb', line 216

def to_h
  @items
end