lib/please/cli.rb in openai-please-0.1.0 vs lib/please/cli.rb in openai-please-0.1.1

- old
+ new

@@ -1,67 +1,100 @@ # frozen_string_literal: true +require 'English' require 'tty-prompt' require 'optparse' require 'please' require 'tempfile' -tty_prompt = TTY::Prompt.new +begin + tty_prompt = TTY::Prompt.new -options = {} + options = { + show_prompt: false, + send_pwd: true, + send_uname: true, + send_ls: true, + } -USAGE = 'Usage: please [options] <instruction>' + USAGE = 'Usage: please [options] <instruction>' -OptionParser.new do |opts| - opts.banner = USAGE -end.parse! + OptionParser.new do |opts| + opts.banner = USAGE -access_token = ENV.fetch('OPENAI_ACCESS_TOKEN') do - tty_prompt.error 'Ensure the OPENAI_ACCESS_TOKEN environment variable is set' - exit 1 -end + opts.on('--show-prompt', 'Output the prompt that would ordinarily be sent to OpenAI Codex, and then exit') do |v| + options[:show_prompt] = v + end -codex_service = Please::OpenAI::CodexService.new(access_token: access_token) + opts.on('--[no-]send-pwd', 'Send the result of `pwd` as part of the prompt') do |v| + options[:send_pwd] = v + end -instruction = ARGV.join(' ') + opts.on('--[no-]send-uname', 'Send the result of `uname -a` as part of the prompt') do |v| + options[:send_uname] = v + end -if instruction.empty? - tty_prompt.error USAGE - exit 1 -end + opts.on('--[no-]send-ls', 'Send the result of `ls -a` as part of the prompt') do |v| + options[:send_ls] = v + end + end.parse! -request = Please::Request.new( - options: options, - instruction: instruction, - codex_service: codex_service, -) + access_token = ENV.fetch('OPENAI_ACCESS_TOKEN') do + tty_prompt.error 'Ensure the OPENAI_ACCESS_TOKEN environment variable is set' + exit 1 + end -command = request.send + codex_service = Please::OpenAI::CodexService.new(access_token: access_token) -loop do - print '$ ' - tty_prompt.ok command + instruction = ARGV.join(' ') - action = tty_prompt.expand('Run the command?') do |q| - q.choice key: 'y', name: 'Yes', value: :run - q.choice key: 'n', name: 'No', value: :abort - q.choice key: 'e', name: 'Edit command before running', value: :edit + if instruction.empty? + tty_prompt.error USAGE + exit 1 end - case action - when :run - Process.wait spawn(command) - exit $?.exitstatus - when :abort - break - when :edit - Tempfile.open('command') do |file| - file << command - file.flush + context = Please::Context.new(options) - Process.wait spawn("${EDITOR:-vi} #{file.path}") + request = Please::Request.new( + instruction: instruction, + context: context, + codex_service: codex_service, + ) - file.rewind - command = file.read.chomp + if options[:show_prompt] + tty_prompt.say request.prompt + exit + end + + command = request.send + + loop do + print '$ ' + tty_prompt.ok command + + action = tty_prompt.expand('Run the command?') do |q| + q.choice key: 'y', name: 'Yes', value: :run + q.choice key: 'n', name: 'No', value: :abort + q.choice key: 'e', name: 'Edit command before running', value: :edit end + + case action + when :run + Process.wait spawn(command) + exit $CHILD_STATUS.exitstatus + when :abort + break + when :edit + Tempfile.open('command') do |file| + file << command + file.flush + + Process.wait spawn("${EDITOR:-vi} #{file.path}") + + file.rewind + command = file.read.chomp + end + end end +rescue Interrupt + exit 130 end