Module: Raif::ModelManifest::Dsl

Defined in:
lib/raif/model_manifest/dsl.rb

Defined Under Namespace

Classes: Context, EmbeddingModelDeclaration, EmbeddingProviderBuilder, EmbeddingProviderDeclaration, EmbeddingRegistryBuilder, ModelDeclaration, ProviderBuilder, ProviderDeclaration, UnknownDeclaration

Class Method Summary collapse

Class Method Details

.deep_freeze(value) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/raif/model_manifest/dsl.rb', line 87

def deep_freeze(value)
  case value
  when Hash then value.to_h { |key, nested| [deep_freeze(key), deep_freeze(nested)] }.freeze
  when Array then value.map { |nested| deep_freeze(nested) }.freeze
  else value.freeze
  end
end

.normalize_capabilities(capabilities) ⇒ Object

Manifest files are Ruby, so these hashes normally arrive symbol-keyed already. Normalizing anyway means the entry shape consumers rely on is guaranteed by the loader rather than by author discipline.



47
48
49
50
51
52
53
54
# File 'lib/raif/model_manifest/dsl.rb', line 47

def normalize_capabilities(capabilities)
  normalized = capabilities.transform_keys(&:to_sym)
  if normalized.key?(:provider_managed_tools)
    normalized[:provider_managed_tools] = Array(normalized[:provider_managed_tools]).map(&:to_sym)
  end

  deep_freeze(normalized)
end

.normalize_endpoints(endpoints, source_path:) ⇒ Object

Endpoint names become strings because that is what the adapter and key-prefix maps in ModelManifest are keyed by. Capabilities are the only per-endpoint attribute; everything else is model level.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/raif/model_manifest/dsl.rb', line 75

def normalize_endpoints(endpoints, source_path:)
  endpoints.to_h do |endpoint, declaration|
    attributes = declaration.transform_keys(&:to_sym)
    unknown = attributes.keys - [:capabilities]
    unless unknown.empty?
      raise ArgumentError, "#{source_path}: endpoint #{endpoint.inspect} declares #{unknown.join(", ")}; only capabilities: is supported"
    end

    [endpoint.to_s, normalize_capabilities(attributes.fetch(:capabilities))]
  end.freeze
end

.normalize_lifecycle(lifecycle) ⇒ Object

Every lifecycle field is always present, defaulting to nil, so callers can read retirement_date or replacement_key off any entry without first asking whether the model bothered to declare them. replacement_key is symbolized alongside status because callers match it against entry keys, which are symbols.



61
62
63
64
65
66
# File 'lib/raif/model_manifest/dsl.rb', line 61

def normalize_lifecycle(lifecycle)
  normalized = LIFECYCLE_KEYS.to_h { |key| [key, nil] }.merge(lifecycle.transform_keys(&:to_sym))
  normalized[:status] &&= normalized[:status].to_sym
  normalized[:replacement_key] &&= normalized[:replacement_key].to_sym
  deep_freeze(normalized)
end

.normalize_pricing(pricing) ⇒ Object



68
69
70
# File 'lib/raif/model_manifest/dsl.rb', line 68

def normalize_pricing(pricing)
  deep_freeze(pricing.transform_keys(&:to_sym))
end