Class: Sublayer::Commands::Agent

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/sublayer/cli/commands/agent.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



12
13
14
# File 'lib/sublayer/cli/commands/agent.rb', line 12

def self.banner
  "sublayer generate:agent"
end

Instance Method Details

#ask_for_agent_detailsObject



30
31
32
33
34
35
36
37
38
# File 'lib/sublayer/cli/commands/agent.rb', line 30

def ask_for_agent_details
  @ai_provider = options[:provider] || ask("Select an AI provider:", default: "OpenAI", limited_to: @available_providers)
  @ai_model = options[:model] || select_ai_model
  @description = options[:description] || ask("Enter a description for the Sublayer Agent you'd like to create:")
  @trigger = ask("What should trigger this agent to start acting?")
  @goal = ask("What is the agent's goal condition?")
  @check_status = ask("How should the agent check its status toward the goal?")
  @step = ask("How does the agent take a step toward its goal?")
end

#confirm_usage_of_ai_apiObject



16
17
18
19
20
# File 'lib/sublayer/cli/commands/agent.rb', line 16

def confirm_usage_of_ai_api
  puts "You are about to generate a new agent that uses an AI API to generate content."
  puts "Please ensure you have the necessary API keys and that you are aware of the costs associated with using the API."
  exit unless yes?("Do you want to continue?")
end

#determine_available_providersObject



22
23
24
25
26
27
28
# File 'lib/sublayer/cli/commands/agent.rb', line 22

def determine_available_providers
  @available_providers = []

  @available_providers << "OpenAI" if ENV["OPENAI_API_KEY"]
  @available_providers << "Claude" if ENV["ANTHROPIC_API_KEY"]
  @available_providers << "Gemini" if ENV["GEMINI_API_KEY"]
end

#determine_destination_folderObject



55
56
57
58
59
60
61
62
63
# File 'lib/sublayer/cli/commands/agent.rb', line 55

def determine_destination_folder
  @destination_folder = if File.directory?("./agents")
                          "./agents"
                        elsif Dir.glob("./lib/**/agents").any?
                          Dir.glob("./lib/**/agents").first
                        else
                          "./"
                        end
end

#generate_agentObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sublayer/cli/commands/agent.rb', line 40

def generate_agent
  Sublayer.configuration.ai_provider = Object.const_get("Sublayer::Providers::#{@ai_provider}")
  Sublayer.configuration.ai_model = @ai_model

  say "Generating Sublayer Agent..."

  @results = SublayerAgentGenerator.new(
    description: @description,
    trigger: @trigger_explanation,
    goal: @goal,
    check_status: @check_status,
    step: @step
  ).generate
end

#save_agent_to_destination_folderObject



65
66
67
# File 'lib/sublayer/cli/commands/agent.rb', line 65

def save_agent_to_destination_folder
  create_file File.join(@destination_folder, @results.filename), @results.code
end