Module: Ask::Agent::CLI

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

Class Method Summary collapse

Class Method Details

.camelize(str) ⇒ Object



294
295
296
# File 'lib/ask/agent/cli.rb', line 294

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

.cmd_helpObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/ask/agent/cli.rb', line 262

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
      skills list            List all discovered skills
      skills show <name>     Show skill details and instructions
      skills search <query>  Search skills by name, description, or tags
      help                   Show this help

    Examples:
      askr list
      askr run health_check
      askr schedule
      askr skills list
      askr skills show rails_debug
      askr skills search deploy

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

    Skills are discovered from:
      ./agents/shared/skills/<name>/SKILL.md
      ./app/agents/shared/skills/<name>/SKILL.md
  HELP
end

.cmd_listObject



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

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



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

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



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

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



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

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

.cmd_skills(args) ⇒ Object



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

def cmd_skills(args)
  sub = args.first

  case sub
  when "list"
    cmd_skills_list
  when "show"
    cmd_skills_show(args[1])
  when "search"
    cmd_skills_search(args[1])
  else
    puts "Usage: askr skills <list|show|search>"
    puts ""
    puts "Commands:"
    puts "  list                  List all discovered skills"
    puts "  show <name>           Show skill details and sibling files"
    puts "  search <query>        Search skills by name, description, or tags"
  end
end

.cmd_skills_listObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ask/agent/cli.rb', line 175

def cmd_skills_list
  require "ask/skills"
  registry = Ask::Skills.discover

  if registry.names.empty?
    puts "No skills found."
    return
  end

  puts "Discovered skills:"
  puts ""
  registry.names.sort.each do |name|
    skill = registry[name]
    puts "  #{skill.name}"
    puts "    description: #{skill.description}"
    puts "    tags:        #{skill.tags.join(", ")}" if skill.tags.any?
    siblings = skill.siblings
    if siblings.any?
      summaries = siblings.map { |cat, files| "#{files.length} #{cat}" }.join(", ")
      puts "    files:       #{summaries}"
    end
  end
end

.cmd_skills_search(query) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ask/agent/cli.rb', line 233

def cmd_skills_search(query)
  unless query
    puts "Usage: askr skills search <query>"
    exit 1
  end

  require "ask/skills"
  registry = Ask::Skills.discover
  query_down = query.downcase

  matches = registry.names.select do |name|
    skill = registry[name]
    name.downcase.include?(query_down) ||
      skill.description.downcase.include?(query_down) ||
      skill.tags.any? { |t| t.downcase.include?(query_down) }
  end

  if matches.empty?
    puts "No skills matching \"#{query}\"."
    return
  end

  puts "Skills matching \"#{query}\":"
  matches.sort.each do |name|
    skill = registry[name]
    puts "  #{skill.name}#{skill.description}"
  end
end

.cmd_skills_show(name) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ask/agent/cli.rb', line 199

def cmd_skills_show(name)
  unless name
    puts "Usage: askr skills show <name>"
    exit 1
  end

  require "ask/skills"
  registry = Ask::Skills.discover
  skill = registry[name]

  unless skill
    puts "Skill not found: #{name}"
    exit 1
  end

  puts "Name:        #{skill.name}"
  puts "Description: #{skill.description}"
  puts "Source:      #{skill.source}"
  puts "Tags:        #{skill.tags.join(", ")}" if skill.tags.any?

  if skill.siblings.any?
    puts ""
    puts "Sibling files:"
    skill.siblings.each do |category, files|
      puts "  #{category}/"
      files.each { |f| puts "    #{f}" }
    end
  end

  puts ""
  puts "--- Instructions ---"
  puts skill.instructions
end

.run(argv) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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 "skills"
    cmd_skills(argv[1..])
  when "help", "--help", "-h", nil
    cmd_help
  else
    puts "Unknown command: #{argv.first}"
    cmd_help
    exit 1
  end
end