Class: GenerativeUI::Tool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/generative_ui/tool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalog: :default) ⇒ Tool

Returns a new instance of Tool.

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/generative_ui/tool.rb', line 9

def initialize(catalog: :default)
  @catalog = Catalog.coerce(catalog)
  raise ArgumentError, 'generative UI catalog is empty' if @catalog.empty?
end

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



7
8
9
# File 'lib/generative_ui/tool.rb', line 7

def catalog
  @catalog
end

Instance Method Details

#descriptionObject



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
# File 'lib/generative_ui/tool.rb', line 18

def description
  <<~TEXT.strip
    Render inline UI from the available component catalog.

    Call this tool at most once per assistant turn. After it returns
    {"status":"ok"}, the UI is shown to the user — do not call it again.

    Arguments:
    - components is a flat array of component instances.
    - Each component is an object: { "id": "...", "component": "<Name>", ...attributes }
      where "component" is the literal component name as a string.
    - One component must have id="root".
    - ComponentId fields reference one component id.
    - ComponentIdList fields reference ordered arrays of component ids.
    - The accepted payload must form one rooted tree.

    Example payload (component names below must be from the catalog;
    ids are arbitrary labels — suffix them with numbers so they are
    clearly distinct from component names):
    {
      "components": [
        { "id": "root",    "component": "<CatalogComponent>", ...attributes },
        { "id": "title-1", "component": "<CatalogComponent>", ...attributes },
        { "id": "body-1",  "component": "<CatalogComponent>", ...attributes }
      ]
    }

    #{catalog.to_prompt}
  TEXT
end

#execute(**args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/generative_ui/tool.rb', line 53

def execute(**args)
  unknown = args.keys.map(&:to_s) - %w[components]
  return invalid_arguments("unknown arguments: #{unknown.join(', ')}") if unknown.any?

  components = args[:components]
  return invalid_arguments('components must be an array') unless components.is_a?(Array)

  set = ComponentSet.from_args(components)
  validation = ComponentTreeValidator.call(set, catalog:)

  if validation.valid?
    { status: 'ok' }.to_json
  else
    { status: 'invalid', errors: validation.errors }.to_json
  end
end

#nameObject



14
15
16
# File 'lib/generative_ui/tool.rb', line 14

def name
  'generate_ui'
end

#params_schemaObject



49
50
51
# File 'lib/generative_ui/tool.rb', line 49

def params_schema
  @params_schema ||= JSON.parse(catalog.tool_arguments_schema.to_json)
end