Class: ToolSchema::Builder

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/lib/tool_schema/builder.rb

Constant Summary collapse

SchemaAst =
T.type_alias do
  T::Hash[Symbol, T.untyped]
end

Class Method Summary collapse

Class Method Details

.build(service_class) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/lib/tool_schema/builder.rb', line 17

def self.build(service_class)
   = T.let(service_class., T::Hash[Symbol, T.untyped])
  entrypoint = [:entrypoint]&.to_sym || :call
  method = service_class.instance_method(entrypoint)
  type_info = SorbetTypeMapper.map_signature(method)

  params_ast = type_info[:params].map do |param_ast|
    merge_param(param_ast, [:params])
  end

  {
    name: [:name],
    description: [:description],
    params: params_ast,
    return_type: type_info[:return_type],
    entrypoint: entrypoint,
    service_class: service_class
  }
end

.merge_children(children, metadata_params) ⇒ Object



63
64
65
66
67
68
69
# File 'app/lib/tool_schema/builder.rb', line 63

def self.merge_children(children, )
  return [] unless children.is_a?(Array)

  children.map do |child|
    merge_param(child, )
  end
end

.merge_param(param_ast, metadata_params) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/lib/tool_schema/builder.rb', line 43

def self.merge_param(param_ast, )
  meta = &.find { |p| p[:name].to_sym == param_ast[:name].to_sym }
  description = meta ? meta[:description] : nil
  example = meta ? meta[:example] : nil
  enum = meta ? meta[:enum] : nil
  required_from_meta = meta.nil? ? true : (meta[:required].nil? ? true : meta[:required])
  required = param_ast[:required] && required_from_meta

  param_ast.merge(
    description: description,
    example: example,
    enum: enum,
    required: required,
    children: merge_children(param_ast[:children], )
  )
end