Class: GenerativeUI::Tool
- Inherits:
-
RubyLLM::Tool
- Object
- RubyLLM::Tool
- GenerativeUI::Tool
- Defined in:
- lib/generative_ui/tool.rb
Instance Attribute Summary collapse
-
#catalog ⇒ Object
readonly
Returns the value of attribute catalog.
Instance Method Summary collapse
- #description ⇒ Object
- #execute(**args) ⇒ Object
-
#initialize(catalog: :default) ⇒ Tool
constructor
A new instance of Tool.
- #name ⇒ Object
- #params_schema ⇒ Object
Constructor Details
Instance Attribute Details
#catalog ⇒ Object (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
#description ⇒ Object
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 |
#name ⇒ Object
14 15 16 |
# File 'lib/generative_ui/tool.rb', line 14 def name 'generate_ui' end |
#params_schema ⇒ Object
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 |