Module: Anthropic::Helpers::InputSchema::SupportedSchemas

Defined in:
lib/anthropic/helpers/input_schema/supported_schemas.rb

Class Method Summary collapse

Class Method Details

.transform_schema!(schema) ⇒ Hash{Symbol=>Object}

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/anthropic/helpers/input_schema/supported_schemas.rb', line 57

def transform_schema!(schema)
  defs = schema[:$defs].to_h

  # rubocop:disable Metrics/BlockLength
  xform = ->(s) do
    case s
    in {type: "string" | ["string", "null"], format: "date-time" | "time" | "date" | "duration" | "email" | "hostname" | "uri" | "ipv4" | "ipv6" | "uuid"} | {type: "array", minItems: 0 | 1}
      # these are the currently supported cases
      next

    in {oneOf: Array => schemas, **rest}
      {anyOf: schemas, **rest}

    in {allOf: Array => schemas}
      derefed = schemas.lazy.grep(Hash).map do
        ref = _1[:$ref].to_s.delete_prefix("#/$defs/")
        defs.fetch(ref, _1)
      end
      merged = {}.merge!(*derefed)
      if (doc = xform.call(merged))
        merged.store(:$ref, true)

        schemas.reject! do |s|
          next unless s.is_a?(Hash)
          s.delete(:description)
          s.select! { merged.key?(_1) }
          s.empty?
        end
        schemas << {description: doc}
      end
    in {type: "integer" | "number" | "string" | ["integer", "null"] | ["number", "null"] | ["string", "null"], **unsupported}
      describe!(s, unsupported: unsupported.except(:enum))
    in {type: "array",  **unsupported}
      describe!(s, unsupported: unsupported.except(:items))
    in {type: "object", **unsupported}
      unsupported.delete(:additionalProperties) if unsupported[:additionalProperties] == false
      describe!(s, unsupported: unsupported.except(:required, :properties))
    else
    end
  end
  # rubocop:enable Metrics/BlockLength

  walk_once(schema, &xform)
  schema
end