Class: PromptObjects::CLI::ReplCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/cli/repl_command.rb

Overview

repl command: interactive session with a prompt object.

Instance Method Summary collapse

Instance Method Details

#helpObject



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/prompt_objects/cli/repl_command.rb', line 345

def help
  prog = Helpers.program_name
  puts <<~HELP
    Usage: #{prog} repl [options] [name] [objects_dir]

    Arguments:
      name         Prompt object to load (default: greeter)
      objects_dir  Directory containing .md files (default: objects)

    Options:
      --sandbox, -s   Run in sandbox mode (isolates changes from main project)
      --help, -h      Show this help message

    Sandbox Mode:
      When running with --sandbox, all new capabilities and prompt objects
      created during the session are stored in a temporary directory.
      On exit, you'll be prompted to copy changes back to the main project.
  HELP
end

#run(args) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/prompt_objects/cli/repl_command.rb', line 293

def run(args)
  options = {
    name: "greeter",
    objects_dir: "objects",
    sandbox: false
  }

  positional = []
  args.each do |arg|
    case arg
    when "--sandbox", "-s"
      options[:sandbox] = true
    when "--help", "-h"
      help
      exit 0
    else
      positional << arg
    end
  end

  options[:name] = positional[0] if positional[0]
  options[:objects_dir] = positional[1] if positional[1]

  sandbox = nil
  primitives_dir = nil

  if options[:sandbox]
    sandbox = Sandbox.new(options[:objects_dir])
    objects_dir = sandbox.objects_dir
    primitives_dir = sandbox.primitives_dir
  else
    objects_dir = options[:objects_dir]
  end

  env = PromptObjects::Runtime.new(objects_dir: objects_dir, primitives_dir: primitives_dir)

  begin
    po = env.load_by_name(options[:name])
    env.load_dependencies(po)
  rescue PromptObjects::Error => e
    puts "Error loading '#{options[:name]}': #{e.message}"
    puts "\nAvailable objects in '#{objects_dir}/':"
    Dir.glob(File.join(objects_dir, "*.md")).each do |path|
      puts "  - #{File.basename(path, '.md')}"
    end
    sandbox&.cleanup(keep_changes: false)
    exit 1
  end

  REPL.new(po, env, sandbox: sandbox).run
end