Class: ActiveAgent::SchemaGenerator::Builder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/active_agent/schema_generator.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Internal builder class for constructing JSON Schemas from model classes.

This class handles the actual schema generation logic, supporting both ActiveRecord and ActiveModel classes with various options and configurations.

Class Method Summary collapse

Class Method Details

.json_schema_from_model(model_class, options = {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a JSON Schema from a model class.

Parameters:

  • model_class (Class)

    The ActiveRecord or ActiveModel class

  • options (Hash) (defaults to: {})

    Schema generation options

Returns:

  • (Hash)

    The generated JSON Schema

Raises:

  • (ArgumentError)

    If model_class is not an ActiveRecord or ActiveModel class



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/active_agent/schema_generator.rb', line 124

def self.json_schema_from_model(model_class, options = {})
  schema = {
    type: "object",
    properties: {},
    required: [],
    additional_properties: options.fetch(:additional_properties, false)
  }

  if defined?(ActiveRecord::Base) && model_class < ActiveRecord::Base
    schema = build_activerecord_schema(schema, model_class, options)
  elsif defined?(ActiveModel::Model) && model_class.include?(ActiveModel::Model)
    schema = build_activemodel_schema(schema, model_class, options)
  else
    raise ArgumentError, "Model must be an ActiveRecord or ActiveModel class"
  end

  # OpenAI strict mode requires all properties to be in the required array
  # So we add all properties to required if strict mode is enabled
  schema[:required] = schema[:properties].keys.sort if options[:strict]

  {
    name: options[:name] || model_class.name.underscore,
    schema: schema,
    strict: options[:strict]
  }.compact
end