Class: OmniAgent::Tool::SchemaBuilder

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

Defined Under Namespace

Classes: PolymorphicFieldBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchemaBuilder

Returns a new instance of SchemaBuilder.



6
7
8
9
10
11
# File 'lib/omni_agent/tool/schema_builder.rb', line 6

def initialize
  @properties = {}
  @required_fields = []
  @validators = {}
  @polymorphics = {}
end

Instance Attribute Details

#polymorphicsObject (readonly)

Returns the value of attribute polymorphics.



4
5
6
# File 'lib/omni_agent/tool/schema_builder.rb', line 4

def polymorphics
  @polymorphics
end

#propertiesObject (readonly)

Returns the value of attribute properties.



4
5
6
# File 'lib/omni_agent/tool/schema_builder.rb', line 4

def properties
  @properties
end

#required_fieldsObject (readonly)

Returns the value of attribute required_fields.



4
5
6
# File 'lib/omni_agent/tool/schema_builder.rb', line 4

def required_fields
  @required_fields
end

#validatorsObject (readonly)

Returns the value of attribute validators.



4
5
6
# File 'lib/omni_agent/tool/schema_builder.rb', line 4

def validators
  @validators
end

Instance Method Details

#array(name, items_type: nil, description: nil, required: true, min_items: nil, max_items: nil, validate: nil, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/omni_agent/tool/schema_builder.rb', line 43

def array(name, items_type: nil, description: nil, required: true, min_items: nil, max_items: nil, validate: nil, &block)
  property = { type: "array" }
  property[:description] = description if description

  if block_given?
    nested_builder = SchemaBuilder.new
    nested_builder.instance_eval(&block)

    property[:items] = {
      type: "object",
      properties: nested_builder.properties,
      required: nested_builder.required_fields,
      additionalProperties: false
    }
  else
    property[:items] = { type: items_type || "string" }
  end

  property[:minItems] = min_items if min_items
  property[:maxItems] = max_items if max_items

  @properties[name] = property
  @required_fields << name.to_s if required
  @validators[name] = validate if validate
end

#boolean(name, description: nil, required: true, validate: nil) ⇒ Object



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

def boolean(name, description: nil, required: true, validate: nil)
  add_property(name, type: "boolean", description: description, required: required, validate: validate)
end

#enum(name, values:, description: nil, required: true, validate: nil) ⇒ Object

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/omni_agent/tool/schema_builder.rb', line 89

def enum(name, values:, description: nil, required: true, validate: nil)
  raise ArgumentError, "enum requires at least one value" if values.empty?

  normalized_values = values.map { |v| v.is_a?(Symbol) ? v.to_s : v }
  value_data_type = enum_data_type(normalized_values)

  property = { type: value_data_type, enum: normalized_values }
  property[:description] = description if description
  @properties[name] = property
  @required_fields << name.to_s if required
  @validators[name] = validate if validate
end

#hash(name, description: nil, required: true, validate: nil, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/omni_agent/tool/schema_builder.rb', line 69

def hash(name, description: nil, required: true, validate: nil, &block)
  property = { type: "object" }
  property[:description] = description if description

  if block_given?
    nested_builder = SchemaBuilder.new
    nested_builder.instance_eval(&block)

    property[:properties] = nested_builder.properties
    property[:required] = nested_builder.required_fields
    property[:additionalProperties] = false
  else
    property[:additionalProperties] = true
  end

  @properties[name] = property
  @required_fields << name.to_s if required
  @validators[name] = validate if validate
end

#integer(name, description: nil, required: true, min: nil, max: nil, validate: nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/omni_agent/tool/schema_builder.rb', line 23

def integer(name, description: nil, required: true, min: nil, max: nil, validate: nil)
  constraints = {}
  constraints[:minimum] = min if min
  constraints[:maximum] = max if max

  add_property(name, type: "integer", description: description, required: required, constraints: constraints, validate: validate)
end

#number(name, description: nil, required: true, min: nil, max: nil, validate: nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/omni_agent/tool/schema_builder.rb', line 31

def number(name, description: nil, required: true, min: nil, max: nil, validate: nil)
  constraints = {}
  constraints[:minimum] = min if min
  constraints[:maximum] = max if max

  add_property(name, type: "number", description: description, required: required, constraints: constraints, validate: validate)
end

#polymorphic(name, types: nil, description: nil, id_type: "string", required: true, resolve: false, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/omni_agent/tool/schema_builder.rb', line 102

def polymorphic(name, types: nil, description: nil, id_type: "string", required: true, resolve: false, &block)
  if resolve && (types.nil? || types.empty?)
    raise ArgumentError, "polymorphic resolve: true requires types:"
  end

  type_values = types
  type_description = description ? "#{description} (type)" : nil
  id_type_value = id_type
  id_description = description ? "#{description} (id)" : nil

  if block_given?
    field_builder = PolymorphicFieldBuilder.new
    field_builder.instance_eval(&block)

    type_values = field_builder.type_values || type_values
    type_description = field_builder.type_description || type_description
    id_type_value = field_builder.id_type || id_type_value
    id_description = field_builder.id_description || id_description
  end

  type_field = :"#{name}_type"
  id_field = :"#{name}_id"
  id_json_type = id_type_value.to_s == "integer" ? "integer" : "string"

  if type_values && !type_values.empty?
    enum(type_field, values: type_values, description: type_description, required: required)
  else
    string(type_field, description: type_description, required: required)
  end

  if id_json_type == "integer"
    integer(id_field, description: id_description, required: required)
  else
    string(id_field, description: id_description, required: required)
  end

  @polymorphics[name] = {
    type_field: type_field,
    id_field: id_field,
    types: type_values,
    id_type: id_json_type,
    resolve: resolve
  }
end

#string(name, description: nil, required: true, min_length: nil, max_length: nil, pattern: nil, format: nil, validate: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/omni_agent/tool/schema_builder.rb', line 13

def string(name, description: nil, required: true, min_length: nil, max_length: nil, pattern: nil, format: nil, validate: nil)
  constraints = {}
  constraints[:minLength] = min_length if min_length
  constraints[:maxLength] = max_length if max_length
  constraints[:pattern] = pattern.is_a?(Regexp) ? pattern.source : pattern if pattern
  constraints[:format] = format if format

  add_property(name, type: "string", description: description, required: required, constraints: constraints, validate: validate)
end