Class: Legion::Extensions::Llm::Tool::SchemaDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/tool.rb

Overview

Wraps schema handling for tool parameters, supporting JSON Schema hashes, Legion::Extensions::Llm::Schema instances/classes, and DSL blocks.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema: nil, block: nil) ⇒ SchemaDefinition

Returns a new instance of SchemaDefinition.



211
212
213
214
# File 'lib/legion/extensions/llm/tool.rb', line 211

def initialize(schema: nil, block: nil)
  @schema = schema
  @block = block
end

Class Method Details

.default_items_schemaObject



207
208
209
# File 'lib/legion/extensions/llm/tool.rb', line 207

def self.default_items_schema
  { type: 'string' }
end

.from_parameters(parameters) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/legion/extensions/llm/tool.rb', line 168

def self.from_parameters(parameters)
  return nil if parameters.nil? || parameters.empty?

  properties = parameters.to_h do |name, param|
    schema = {
      type: map_type(param.type),
      description: param.description
    }.compact

    schema[:items] = default_items_schema if schema[:type] == 'array'

    [name.to_s, schema]
  end

  required = parameters.select { |_, param| param.required }.keys.map(&:to_s)

  json_schema = {
    type: 'object',
    properties: properties,
    required: required,
    additionalProperties: false,
    strict: true
  }

  new(schema: json_schema)
end

.map_type(type) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/legion/extensions/llm/tool.rb', line 195

def self.map_type(type)
  case type.to_s
  when 'integer', 'int' then 'integer'
  when 'number', 'float', 'double' then 'number'
  when 'boolean' then 'boolean'
  when 'array' then 'array'
  when 'object' then 'object'
  else
    'string'
  end
end

Instance Method Details

#json_schemaObject



220
221
222
# File 'lib/legion/extensions/llm/tool.rb', line 220

def json_schema
  @json_schema ||= Legion::Extensions::Llm::Utils.deep_stringify_keys(resolve_schema)
end

#present?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/legion/extensions/llm/tool.rb', line 216

def present?
  @schema || @block
end