-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutilities.rb
More file actions
47 lines (40 loc) · 1.25 KB
/
utilities.rb
File metadata and controls
47 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
module RubyLLM
class Schema
module DSL
module Utilities
# Schema definition and reference methods
def define(name, description: nil, &block)
sub_schema = Class.new(Schema)
sub_schema.class_eval(&block)
sub_schema.description = description if description
definitions[name] = {
type: "object",
description: sub_schema.description,
properties: sub_schema.properties,
required: sub_schema.required_properties,
additionalProperties: sub_schema.additional_properties
}.compact
end
def reference(schema_name)
if schema_name == :root
{"$ref" => "#"}
else
{"$ref" => "#/$defs/#{schema_name}"}
end
end
private
def add_property(name, definition, required:)
properties[name.to_sym] = definition
required_properties << name.to_sym if required
end
def primitive_type?(type)
type.is_a?(Symbol) && PRIMITIVE_TYPES.include?(type)
end
def schema_class?(type)
(type.is_a?(Class) && type < Schema) || type.is_a?(Schema)
end
end
end
end
end