Module: Ask::Agent::CLI

Defined in:
lib/ask/agent/cli.rb

Class Method Summary collapse

Class Method Details

.camelize(str) ⇒ Object



177
178
179
# File 'lib/ask/agent/cli.rb', line 177

def camelize(str)
  str.split(/[_-]/).map(&:capitalize).join
end

.cmd_helpObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ask/agent/cli.rb', line 153

def cmd_help
  puts <<~HELP
    Usage: askr <command> [options]

    Commands:
      run <name> [prompt]    Run an agent with an optional prompt
      list                   List all discovered agents
      schedule               Start the scheduler for all scheduled agents
      new <name>             Scaffold a new agent directory
      help                   Show this help

    Examples:
      askr list
      askr run health_check
      askr run health_check "Check the server status"
      askr new deploy_bot
      askr schedule

    Agent directories are discovered from:
      ./agents/<name>/agent.rb
      ./app/agents/<name>/agent.rb
  HELP
end

.cmd_listObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ask/agent/cli.rb', line 60

def cmd_list
  defs = Ask::Agent.definitions
  if defs.empty?
    puts "No agents found. Define one in agents/<name>/agent.rb or app/agents/<name>/agent.rb."
    return
  end

  puts "Discovered agents:"
  defs.each do |name, (klass, dir)|
    config = klass._config
    model = config[:model] || "(default)"
    schedule = config[:schedule]
    has_instructions = klass.instructions_path ? "yes" : "no"
    tools = config[:tools].any? ? config[:tools].join(", ") : "(none)"

    puts "  #{name}"
    puts "    model:       #{model}"
    puts "    tools:       #{tools}"
    puts "    instructions: #{has_instructions}"
    puts "    schedule:    #{schedule || "(none)"}" if schedule
    puts "    directory:   #{dir}"
  end
end

.cmd_new(args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ask/agent/cli.rb', line 112

def cmd_new(args)
  name = args.first
  unless name
    puts "Usage: askr new <agent-name>"
    exit 1
  end

  dir = File.join(Dir.pwd, "agents", name)
  if File.exist?(dir)
    puts "Directory already exists: #{dir}"
    exit 1
  end

  FileUtils.mkdir_p(dir)

  File.write(File.join(dir, "agent.rb"), <<~RUBY)
    # frozen_string_literal: true

    class #{camelize(name)} < Ask::Agent::Definition
      model "gpt-4o"
      tools :bash, :read, :grep
    end
  RUBY

  File.write(File.join(dir, "instructions.md"), <<~MARKDOWN)
    # #{name}

    You are a helpful AI agent. Your goal is to assist the user with their tasks.

    ## Guidelines

    - Be concise and accurate
    - Use the available tools when needed
    - Ask for clarification if instructions are ambiguous
  MARKDOWN

  puts "Created agent: #{dir}"
  puts ""
  puts "Run it:  askr run #{name}"
end

.cmd_run(args) ⇒ Object



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
# File 'lib/ask/agent/cli.rb', line 27

def cmd_run(args)
  name = args.first
  unless name
    puts "Usage: askr run <agent-name> [prompt]"
    exit 1
  end

  prompt = args[1..]&.join(" ") || ""

  begin
    agent = Ask::Agent.new(name)

    if prompt.empty?
      puts "Starting interactive session with #{name}..."
      puts "Type your message (Ctrl+D to exit):"
      while (input = $stdin.gets)
        response = agent.run(input.strip)
        puts response
        puts "---"
      end
    else
      response = agent.run(prompt)
      puts response
    end
  rescue Ask::UnknownAgent => e
    puts e.message
    exit 1
  rescue => e
    puts "Error: #{e.message}"
    exit 1
  end
end

.cmd_scheduleObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ask/agent/cli.rb', line 84

def cmd_schedule
  defs = Ask::Agent.definitions
  scheduled = defs.select { |_name, (klass, _dir)| klass._config[:schedule] }

  if scheduled.empty?
    puts "No scheduled agents found."
    return
  end

  scheduled.each do |name, (klass, _dir)|
    schedule = klass._config[:schedule]
    puts "Scheduling #{name} (#{schedule})..."
    Ask::Agent.new(name)
  end

  Ask::Agent::Scheduler.start
  puts "Scheduler started. Running #{scheduled.length} task(s)."
  puts "Press Ctrl+C to stop."

  loop do
    sleep 1
  rescue Interrupt
    puts "\nShutting down..."
    Ask::Agent::Scheduler.stop
    exit 0
  end
end

.run(argv) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ask/agent/cli.rb', line 8

def run(argv)
  case argv.first
  when "run"
    cmd_run(argv[1..])
  when "list"
    cmd_list
  when "schedule"
    cmd_schedule
  when "new"
    cmd_new(argv[1..])
  when "help", "--help", "-h", nil
    cmd_help
  else
    puts "Unknown command: #{argv.first}"
    cmd_help
    exit 1
  end
end