Class: AgentC::Schema::AnyOneOf

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_c/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*schemas) ⇒ AnyOneOf

Returns a new instance of AnyOneOf.



9
10
11
# File 'lib/agent_c/schema.rb', line 9

def initialize(*schemas)
  @schemas = schemas
end

Instance Attribute Details

#schemasObject (readonly)

Returns the value of attribute schemas.



8
9
10
# File 'lib/agent_c/schema.rb', line 8

def schemas
  @schemas
end

Instance Method Details

#schema_jsonsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/agent_c/schema.rb', line 17

def schema_jsons
  @schema_jsons ||= (
    schemas.flat_map do |schema|
      if schema.is_a?(AnyOneOf)
        to_nested_schema
      elsif schema.is_a?(Hash)
        schema
      elsif schema.ancestors.include?(RubyLLM::Schema)
        schema.new.to_json_schema.fetch(:schema)
      else
        raise ArgumentError, "Invalid schema class: #{schema}"
      end
    end
  )
end

#to_json_schemaObject



33
34
35
36
37
38
39
# File 'lib/agent_c/schema.rb', line 33

def to_json_schema
  {
    schema: {
      oneOf: schema_jsons
    }
  }
end

#to_nested_schemaObject



13
14
15
# File 'lib/agent_c/schema.rb', line 13

def to_nested_schema
  to_json_schema.fetch(:schema).fetch(:oneOf)
end