Class: Legate::Generators::AgentGenerator

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

Overview

AI-powered agent 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; passed to Legate::LLM.build_adapter.

'gemini-2.5-pro'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(description:) ⇒ Hash

Generate agent definition code from a natural language description

Parameters:

  • description (String)

    Natural language description of the agent to generate

Returns:

  • (Hash)

    { code: String, suggested_name: String }

Raises:



24
25
26
# File 'lib/legate/generators/agent_generator.rb', line 24

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

.generate_definition(description:) ⇒ Hash

Generate a STRUCTURED agent definition (the same fields the “Create Agent” form accepts) from a description. No Ruby is generated or executed — the result is plain data that can be registered live via POST /agents. Tools are filtered to those actually installed (hallucinated ones are dropped).

Returns:

  • (Hash)

    { name:, description:, instruction:, model:, agent_type:, tools: [String], output_key:, dropped_tools: [String] }



53
54
55
# File 'lib/legate/generators/agent_generator.rb', line 53

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

Instance Method Details

#generate(description:) ⇒ Object



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

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?

  available_tools = format_available_tools
  system_prompt = build_prompt(available_tools)
  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_agent_name(clean_code)

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

#generate_definition(description:) ⇒ Object

Raises:



57
58
59
60
61
62
63
64
65
66
# File 'lib/legate/generators/agent_generator.rb', line 57

def generate_definition(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_definition_prompt(format_available_tools)
  user_prompt = build_definition_user_prompt(description)
  raw = call_llm(adapter, system_prompt, user_prompt)
  normalize_definition_fields(parse_definition_json(raw), fallback_description: description)
end