Class: Legate::Generators::ToolGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/legate/generators/tool_generator.rb

Overview

AI-powered tool code generator. Uses the configured LLM adapter (Gemini by default) via Legate::LLM.

Defined Under Namespace

Classes: ApiError, ApiKeyMissingError, GenerationError

Constant Summary collapse

GENERATION_MODEL =

Model used for code generation (a capable model is worth it here). Passed to Legate::LLM.build_adapter; a configured non-Gemini factory may remap it.

'gemini-2.5-pro'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(description:) ⇒ Hash

Generate tool class code from a natural language description

Parameters:

  • description (String)

    Natural language description of the tool to generate

Returns:

  • (Hash)

    { code: String, suggested_name: String, tool_type: String }

Raises:



25
26
27
# File 'lib/legate/generators/tool_generator.rb', line 25

def self.generate(description:)
  new.generate(description: description)
end

Instance Method Details

#generate(description:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/legate/generators/tool_generator.rb', line 29

def generate(description:)
  validate_description!(description)
  adapter = Legate::LLM.build_adapter(model: GENERATION_MODEL)
  raise ApiKeyMissingError, 'GOOGLE_API_KEY not configured. AI generation requires a Gemini API key.' unless adapter.available?

  system_prompt = build_prompt
  user_prompt = build_user_prompt(description)

  generated_code = call_llm(adapter, system_prompt, user_prompt)
  clean_code = clean_generated_code(generated_code)
  CodeValidator.validate!(clean_code)
  suggested_name = extract_tool_name(clean_code)
  tool_type = detect_tool_type(clean_code)

  { code: clean_code, suggested_name: suggested_name, tool_type: tool_type }
rescue CodeValidator::UnsafeCodeError => e
  raise GenerationError, e.message
end