Class: RailsConsoleAi::AgentLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_ai/agent_loader.rb

Constant Summary collapse

AGENTS_DIR =
'agents'
BUILTIN_DIR =
File.expand_path('../agents', __FILE__)

Instance Method Summary collapse

Constructor Details

#initialize(storage = nil) ⇒ AgentLoader

Returns a new instance of AgentLoader.



8
9
10
# File 'lib/rails_console_ai/agent_loader.rb', line 8

def initialize(storage = nil)
  @storage = storage || RailsConsoleAi.storage
end

Instance Method Details

#agent_summariesObject



25
26
27
28
29
30
31
32
# File 'lib/rails_console_ai/agent_loader.rb', line 25

def agent_summaries
  agents = load_all_agents
  return nil if agents.empty?

  agents.map { |a|
    "- **#{a['name']}**: #{a['description']}"
  }
end

#delete_agent(name:) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rails_console_ai/agent_loader.rb', line 64

def delete_agent(name:)
  key = agent_key(name)
  unless @storage.exists?(key)
    found = load_all_agents.find { |a| a['name'].to_s.downcase == name.to_s.downcase }
    return "No agent found: \"#{name}\"" unless found
    key = agent_key(found['name'])
  end

  agent = load_agent(key)
  @storage.delete(key)
  "Agent deleted: \"#{agent ? agent['name'] : name}\""
rescue Storage::StorageError => e
  "FAILED to delete agent (#{e.message})."
end

#find_agent(name) ⇒ Object



34
35
36
37
# File 'lib/rails_console_ai/agent_loader.rb', line 34

def find_agent(name)
  agents = load_all_agents
  agents.find { |a| a['name'].to_s.downcase == name.to_s.downcase }
end

#load_all_agentsObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rails_console_ai/agent_loader.rb', line 12

def load_all_agents
  agents = load_builtin_agents
  app_agents = load_app_agents
  # App-specific agents override built-ins with the same name
  app_names = app_agents.map { |a| a['name'].to_s.downcase }.to_set
  agents.reject! { |a| app_names.include?(a['name'].to_s.downcase) }
  agents.concat(app_agents)
  agents
rescue => e
  RailsConsoleAi.logger.warn("RailsConsoleAi: failed to load agents: #{e.message}")
  []
end

#save_agent(name:, description:, body:, max_rounds: nil, model: nil, tools: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rails_console_ai/agent_loader.rb', line 39

def save_agent(name:, description:, body:, max_rounds: nil, model: nil, tools: nil)
  key = agent_key(name)
  existing = find_agent(name)

  frontmatter = {
    'name' => name,
    'description' => description
  }
  frontmatter['max_rounds'] = max_rounds if max_rounds
  frontmatter['model'] = model if model
  frontmatter['tools'] = Array(tools) if tools && !tools.empty?

  content = "---\n#{YAML.dump(frontmatter).sub("---\n", '').strip}\n---\n\n#{body}\n"
  @storage.write(key, content)

  path = @storage.respond_to?(:root_path) ? File.join(@storage.root_path, key) : key
  if existing
    "Agent updated: \"#{name}\" (#{path})"
  else
    "Agent created: \"#{name}\" (#{path})"
  end
rescue Storage::StorageError => e
  "FAILED to save agent (#{e.message})."
end