Module: Legate::ToolCodeGenerator

Defined in:
lib/legate/tool_code_generator.rb

Overview

Generates Ruby source code from tool metadata. Used to create downloadable .rb files for native tools from the web UI.

Class Method Summary collapse

Class Method Details

.generate(tool_name) ⇒ String?

Generate Ruby code from a tool’s metadata.

Parameters:

  • tool_name (Symbol, String)

    The tool name registered with GlobalToolManager.

Returns:

  • (String, nil)

    Valid Ruby source code or nil if tool not found.



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/legate/tool_code_generator.rb', line 11

def self.generate(tool_name)
  tool_name_sym = tool_name.to_sym
  tool_class = Legate::GlobalToolManager.find_class(tool_name_sym)
  return nil unless tool_class

   = tool_class.
  return nil unless 

  name = [:name] || tool_name_sym
  description = [:description] || 'No description provided'
  parameters = [:parameters] || {}

  # Generate class name from tool name (snake_case to PascalCase)
  class_name = name.to_s.split('_').map(&:capitalize).join

  code = <<~RUBY
    # frozen_string_literal: true

    # Tool: #{name}
    # Generated from Legate Web UI on #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}

    require 'legate/tool'

    module Legate
      module Tools
        class #{class_name} < Tool
          tool_description #{ruby_string(description)}

  RUBY

  # Add parameter declarations
  parameters.each do |param_name, param_opts|
    code += generate_parameter(param_name, param_opts)
  end

  code += <<~RUBY

    private

    # @param params [Hash] The validated input parameters.
    # @param context [Legate::ToolContext] The execution context.
    # @return [Hash] Result with :status and :result or :error_message.
    def perform_execution(params, context)
      # TODO: Implement your tool logic here
      #
      # Available context methods:
      #   context.state_get(:key)  - Read from session state
      #   context.state_set(:key, value) - Write to session state
      #   context.session_id - Current session ID
      #
  RUBY

  # Add parameter access examples
  parameters.each do |param_name, _param_opts|
    code += "            # #{param_name} = params[:#{param_name}]\n"
  end

  code += <<~RUBY

            # Return success result
            { status: :success, result: 'Tool executed successfully' }

          rescue StandardError => e
            { status: :error, error_message: e.message }
          end
        end
      end
    end

    # Register the tool so agents can use it
    Legate::GlobalToolManager.register_tool(Legate::Tools::#{class_name})
  RUBY

  code
end