Class: Cli::Actions
- Inherits:
-
Object
- Object
- Cli::Actions
- Defined in:
- lib/cli/actions.rb
Class Method Summary collapse
- .check_cli_version ⇒ Object
- .create_and_run_recall(options) ⇒ Object
- .create_and_run_recipe(options) ⇒ Object
- .create_and_run_remember(options) ⇒ Object
-
.create_coder(options) ⇒ Object
CODER.
- .help ⇒ Object
- .init ⇒ Object
- .list_coders(options) ⇒ Object
- .report_exception(command, e) ⇒ Object
- .run_coder(options) ⇒ Object
Class Method Details
.check_cli_version ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cli/actions.rb', line 36 def self.check_cli_version versions = PairProgrammer::Api::System.versions if versions["cli"] != Cli::Version::VERSION Cli::Display.("A new version of the CLI is available, installing update") gem = PairProgrammer::Configuration.development? ? "pear-programmer-0.1.gem" : "pear-programmer" Cli::Display.("Running gem update #{gem}") system("gem update #{gem}") Cli::Display.("Update complete") end end |
.create_and_run_recall(options) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/cli/actions.rb', line 77 def self.create_and_run_recall() requirements = Cli::Display.get_input("what would you like to recall?: ") config = Cli::Configuration.new context = config.default_context id = PairProgrammer::Api::Coder.create([], context, requirements, "recall") run_coder({ id: id }) end |
.create_and_run_recipe(options) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/cli/actions.rb', line 86 def self.create_and_run_recipe() recipe = [:recipe] raise "Recipe cannot be blank" if recipe.nil? || recipe.empty? requirements = Cli::Display.get_input("requirements: ") config = Cli::Configuration.new context = config.default_context id = PairProgrammer::Api::Coder.create([], context, requirements, recipe) run_coder({ id: id }) end |
.create_and_run_remember(options) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/cli/actions.rb', line 68 def self.create_and_run_remember() requirements = Cli::Display.get_input("what would you like to remember?: ") config = Cli::Configuration.new context = config.default_context id = PairProgrammer::Api::Coder.create([], context, requirements, "remember") run_coder({ id: id }) end |
.create_coder(options) ⇒ Object
CODER
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/cli/actions.rb', line 99 def self.create_coder() config = Cli::Configuration.new # context = Cli::Display.get_input("context (press enter to use context from your #{Cli::Configuration::FILE_NAME} file): ") # if context.empty? Cli::Display.("using context from your #{Cli::Configuration::FILE_NAME} file:") Cli::Display.(config.default_context) context = config.default_context # end requirements = Cli::Display.get_input("requirements: ") tasks = [] while true do task = Cli::Display.get_input("task (press enter to complete): ") if task.empty? break else tasks << task end end id = PairProgrammer::Api::Coder.create(tasks, context, requirements, nil) Cli::Display.("Created and run starting") run_coder({ id: id }) end |
.help ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cli/actions.rb', line 54 def self.help Cli::Display. "Available Commands:" Cli::Display. " init - initialize pear-programmer from the root of your project" Cli::Display. " help" Cli::Display. " coding (new|start|list)" Cli::Display. "Usage examples:" Cli::Display. " pear-on init" Cli::Display. " pear-on help" Cli::Display. " pear-on coding new (define a new set of requirements and tasks)" Cli::Display. " pear-on coding start (-a, --auto | will prompt you only when required)" Cli::Display. " pear-on coding list (list all requirements)" end |
.init ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cli/actions.rb', line 9 def self.init Cli::Display.("Welcome to Pear Programmer!") confirmation = Cli::Display.confirmation("Are you in the root directory of your project?") if !confirmation Cli::Display.("Please change directory into the root of your project and try again.") return end Cli::Display.("creating #{Cli::Configuration::FILE_NAME} in #{Dir.pwd}") Cli::Display.("Context is any information about your project that you want the large language model to know about. A few sentences should suffice.") Cli::Display.("Include information about your framework (ie Ruby on Rails), your database, how you handle assets, authentication, etc.") Cli::Display.("The more detailed you are the better the LLM will perform.") context = Cli::Display.get_input("context: ") Cli::Display.("If you haven't already, sign up for an API key at https://pairprogrammer.io") Cli::Display.("To skip for now and update later press enter") api_key = Cli::Display.get_input("api_key: ") Cli::Display.("If you are running a python app, which python binary are you using? (python, python2, python3)") Cli::Display.("If you are not using python, press enter to skip") python_command = Cli::Display.select("python command: (choose none if you are not using python)", {"python" => "python", "python2" => "python2", "python3" => "python3", "none" => ""}) Cli::Configuration.create(context, api_key, python_command) Cli::Display.("successfully created #{Cli::Configuration::FILE_NAME} - you can update this file at any time") Cli::Display.("Please add it to your .gitignore file") end |
.list_coders(options) ⇒ Object
260 261 262 263 |
# File 'lib/cli/actions.rb', line 260 def self.list_coders() coders = PairProgrammer::Api::Coder.list Cli::Display.table(coders, ["id", "tasks", "requirements", "created_at"]) end |
.report_exception(command, e) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/cli/actions.rb', line 47 def self.report_exception(command, e) version = Cli::Version::VERSION PairProgrammer::Api::System.client_exception(command, e, version) Cli::Display.("An error occurred") Cli::Display.(e.) end |
.run_coder(options) ⇒ Object
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 232 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 |
# File 'lib/cli/actions.rb', line 124 def self.run_coder() config = Cli::Configuration.new auto = true #!!options[:auto] if [:id] id = [:id] else coders = PairProgrammer::Api::Coder.list if coders.empty? Cli::Display.("you have not created any requirements yet") return end id = Cli::Display.select("Select which requirements you would like to work on", coders.inject({}) { |hash, coder| hash[coder["requirements"]] = coder["id"]; hash }) end while true do # RETRY when Net::ReadTimeout spinner = Cli::Display.spinner spinner.auto_spin begin response = PairProgrammer::Api::Coder.run(id) rescue Net::ReadTimeout Cli::Display.("connection timed out but coder is still running. reconnecting...") next ensure spinner.stop() end if response["running"] Cli::Display.("assistant is still running, will try again in 20 seconds") sleep(20) next elsif response["reached_max_length"] Cli::Display.("conversation has reached its context length due to limitations with LLMs") Cli::Display.("please create a new set of requirements") return elsif response["error"] Cli::Display.("there was an error processing the assistant's response") Cli::Display.("retrying in 5 seconds") sleep(5) next end if response["available_tokens"] && response["available_tokens"] < 500 Cli::Display.("conversation is getting long and approaching context length limit") Cli::Display.("this conversation has #{response["available_tokens"]} tokens left") end = response["system_message"] if .nil? next end response_required = true # TODO if there is explanation but no command then response is required if ["explanation"] && !["explanation"].empty? Cli::Display.("assistant", ["explanation"]) end if ["command"] skip_command = false command_display = PairProgrammer::Command.display_command(["command"], ["arguments"]) Cli::Display.(command_display) if command_display response_required = false # command overwrites if ["command"] == "comment" Cli::Display.("assistant", ["arguments"]["comment"]) response_required = true elsif ["command"] == "ask_question" Cli::Display.("assistant", ["arguments"]["question"]) response_required = true else if ["command"] == "write_file" # this fails if file doesn't exist file_path = PairProgrammer::Configuration.absolute_path(["arguments"]["file_path"]) begin original_content = File.read(file_path) rescue Errno::ENOENT => e Cli::Display.("there was an error running the command, notifying assistant of error.") PairProgrammer::Api::Coder.append_exception(id, e) Cli::Display.("retrying...") next end Cli::Display.dispaly_diff(original_content, ["arguments"]["content"]) confirmation = Cli::Display.confirmation("Accept changes?") if !confirmation Cli::Display.("changes rejected") # PairProgrammer::Api::Coder.append_output(id, "COMMAND REJECTED") skip_command = true response_required = true else Cli::Display.("changes accepted") end end if !skip_command output = nil begin output = PairProgrammer::Command.run(["command"], ["arguments"]) rescue => e Cli::Display.("there was an error running the command, notifying assistant of error.") PairProgrammer::Api::Coder.append_exception(id, e) Cli::Display.("retrying...") next end PairProgrammer::Api::Coder.append_output(id, output, ["command"]) end end end while true do # user does not have to respond if auto is true if !response_required && auto break end display = response_required ? "required" : "optional" = Cli::Display.get_input("response (#{display}): ") if .empty? if response_required Cli::Display.("response required") next else break end else PairProgrammer::Api::Coder.(id, ) response_required = false end end end end |