{"id":13878195,"url":"https://github.com/stevegeek/ai_refactor","last_synced_at":"2025-04-07T17:11:39.119Z","repository":{"id":168553700,"uuid":"642714960","full_name":"stevegeek/ai_refactor","owner":"stevegeek","description":"AI Refactor is an experimental tool to see how AI can be applied to bulk refactor code.","archived":false,"fork":false,"pushed_at":"2025-03-06T09:14:38.000Z","size":112,"stargazers_count":38,"open_issues_count":6,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-02T04:50:28.338Z","etag":null,"topics":["refactoring","refactoring-tools","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stevegeek.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-19T07:27:28.000Z","updated_at":"2025-03-06T09:14:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"43f4d7e7-7c42-4ba2-8042-0dbdf0709f20","html_url":"https://github.com/stevegeek/ai_refactor","commit_stats":{"total_commits":79,"total_committers":1,"mean_commits":79.0,"dds":0.0,"last_synced_commit":"c8cd9267a9431edb9306fa30adf9d6f852ddc0db"},"previous_names":["stevegeek/ai_refactor"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegeek%2Fai_refactor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegeek%2Fai_refactor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegeek%2Fai_refactor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegeek%2Fai_refactor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stevegeek","download_url":"https://codeload.github.com/stevegeek/ai_refactor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247694877,"owners_count":20980733,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["refactoring","refactoring-tools","ruby"],"created_at":"2024-08-06T08:01:42.428Z","updated_at":"2025-04-07T17:11:39.101Z","avatar_url":"https://github.com/stevegeek.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Deprecated\n\n[Claude code](https://docs.anthropic.com/en/docs/agents-and-tools/claude-code) now fulfils the main goals of `ai_refactor`, and is a more complete tool. \n\nClaude code \"custom slash commands\" take on from the idea of `ai_refactor`'s command files. I will likely port the command files to Claude code slash commands and share them in the future.\n\nI will not be updating this tool further.\n\n# AIRefactor for Ruby\n\n__The goal for AIRefactor is to use LLMs to apply repetitive refactoring tasks to code.__\n\n## The workflow\n\n1) the human decides what refactoring is needed\n2) the human selects an existing built-in refactoring command, and/or builds up a prompt to describe the task\n3) the human selects some source files to act as context (eg examples of the code post-refactor, or related classes etc)\n4) the human runs the tool with the command, source files and context files\n5) the AI generates the refactored code and outputs it either to a file or stdout.\n6) In some cases, the tool can then check the generated code by running tests and comparing test outputs.\n\nAIRefactor can apply the refactoring to multiple files, allowing batch processing.\n\n#### Notes\n\nAI Refactor is an experimental tool and under active development as I explore the idea myself. It may not work as expected, or\nchange in ways that break existing functionality.\n\nThe focus of the tool is work with the **Ruby programming language ecosystem**, but it can be used with any language.\n\nAI Refactor currently uses [OpenAI's ChatGPT](https://platform.openai.com/) or [Anthropic Claude](https://docs.anthropic.com/en/docs/about-claude/models) to generate code.\n\n## Examples\n\nSee the [examples](examples/) directory for some examples of using the tool.\n\nYou can run the command files to run the example.\n\nFor example, the first example can be run with: (you can add options if desired, eg `-v` for verbose output and `-d` for debug output)\n\n```shell\n./exe/ai_refactor examples/ex1_convert_a_rspec_test_to_minitest.yml\n```\n\nYou should see:\n    \n```\n$ ./exe/ai_refactor examples/ex1_convert_a_rspec_test_to_minitest.yml\nLoading refactor command file 'examples/ex1_convert_a_rspec_test_to_minitest.yml'...\nAI Refactor 1 files(s)/dir(s) '[\"examples/ex1_input_spec.rb\"]' with rails/minitest/rspec_to_minitest refactor\n====================\nProcessing examples/ex1_input_spec.rb...\n\nNo differences found! Conversion worked!\nRefactor succeeded on examples/ex1_input_spec.rb\n\nAll files processed successfully!\nDone processing all files!\n```\n\nAnd find the file `examples/ex1_input_test.rb` has been created. Note the process above also ran the generated test file and compared the output to the original test file.\n\nIf you see an error, then try to run it again, or use a different GPT model.\n\n## Available refactors \u0026 commands\n\nWrite your own prompt:\n\n- `ruby/write_ruby`: provide your own prompt for the AI and expect to output Ruby code (no input files required)\n- `ruby/refactor_ruby`: provide your own refactoring prompt for the AI and expect to output Ruby code\n- `custom`: provide your own prompt for the AI and run against the input files. There is no expectation of the output.\n\nUse a pre-built prompt:\n\n- `minitest/write_test_for_class`: write a minitest test for a given class\n- `rails/minitest/rspec_to_minitest`: convert RSpec specs to minitest tests in Rails apps\n\n### User supplied prompts, eg `custom`, `ruby/write_ruby` and `ruby/refactor_ruby`\n\nYou can use these commands in conjunction with a user supplied prompt. \n\nYou must supply a prompt file with the `-p` option.\n\nThe output is written to `stdout`, or to a file with the `--output` option.\n\nUser supplied prompts are best configured using a command file, see below.\n\n### `rails/minitest/rspec_to_minitest`\n\nConverts RSpec tests to minitest tests for Rails test suites (ie generated minitest tests are actually `ActiveSupport::TestCase`s).\n\nThe tool first runs the original RSpec spec file and then runs the generated minitest test file, and compares the output of both.\n\nThe comparison is simply the count of successful and failed tests but this is probably enough to determine if the conversion worked.\n\n```shellq\nstephen$ OPENAI_API_KEY=my-key ai_refactor rails/minitest/rspec_to_minitest spec/models/my_thing_spec.rb\nAI Refactor 1 files(s)/dir(s) '[\"spec/models/my_thing_spec.rb\"]' with rails/minitest/rspec_to_minitest refactor\n====================\nProcessing spec/models/my_thing_spec.rb...\n\nOriginal test run results:\n\u003e\u003e Examples: 41, Failures: 0, Pendings: 0\n\nTranslated test file results:\n\u003e\u003e Runs: 41, Failures: 0, Skips: 0\n\nNo differences found! Conversion worked!\nRefactor succeeded on spec/models/my_thing_spec.rb\n\nDone processing all files!\n```\n\n### `minitest/write_test_for_class`\n\nWrites a minitest test for a given class. The output will, by default, be put into a directory named `test` in the current directory,\nin a path that matches the input file path, with a `_test.rb` suffix.\n\nFor example, if the input file is `app/stuff/my_thing.rb` the output will be written to `test/app/stuff/my_thing_test.rb`.\n\nThis refactor can benefit from being passed related files as context, for example, if the class under test inherits from another class,\nthen context can be used to provide the parent class.\n\n### `quickdraw/0.1.0/convert_minitest`\n\nConvert Minitest or Test::Unit test suite files to [Quickdraw](https://github.com/joeldrapper/quickdraw) test suite files.\n\nFiles, by default, are output to the same directory as the input file but with .test.rb extension (and _test removed).\n\nNote: Quickdraw is still missing some features, so some minitest methods are not converted, for example, Quickdraw does not support setup/teardown just yet.\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n\n    $ bundle add ai_refactor\n\nIf bundler is not being used to manage dependencies, install the gem by executing:\n\n    $ gem install ai_refactor\n\n## Usage\n\nSee `ai_refactor --help` for more information.\n\n```\nUsage: ai_refactor REFACTOR_TYPE_OR_COMMAND_FILE INPUT_FILE_OR_DIR [options]\n\nWhere REFACTOR_TYPE_OR_COMMAND_FILE is either the path to a command YML file, or one of the refactor types: [\"custom\" ... (run ai_refactor --help for full list of refactor types)]\n\n    -o, --output [FILE]              Write output to given file instead of stdout. If no path provided will overwrite input file (will prompt to overwrite existing files). Some refactor tasks will write out to a new file by default. This option will override the tasks default behaviour.\n    -O, --output-template TEMPLATE   Write outputs to files instead of stdout. The template is used to create the output name, where the it can have substitutions, '[FILE]', '[NAME]', '[DIR]', '[REFACTOR]' \u0026 '[EXT]'. Eg `[DIR]/[NAME]_[REFACTOR][EXT]` (will prompt to overwrite existing files)\n    -c, --context CONTEXT_FILES      Specify one or more files to use as context for the AI. The contents of these files will be prepended to the prompt sent to the AI.\n    -x, --extra CONTEXT_TEXT         Specify some text to be prepended to the prompt sent to the AI as extra information of note.\n    -r, --review-prompt              Show the prompt that will be sent to ChatGPT but do not actually call ChatGPT or make changes to files.\n    -p, --prompt PROMPT_FILE         Specify path to a text file that contains the ChatGPT 'system' prompt.\n    -f, --diffs                      Request AI generate diffs of changes rather than writing out the whole file.\n    -C, --continue [MAX_MESSAGES]    If ChatGPT stops generating due to the maximum token count being reached, continue to generate more messages, until a stop condition or MAX_MESSAGES. MAX_MESSAGES defaults to 3\n    -m, --model MODEL_NAME           Specify a ChatGPT model to use (default gpt-4-turbo).\n        --temperature TEMP           Specify the temperature parameter for ChatGPT (default 0.7).\n        --max-tokens MAX_TOKENS      Specify the max number of tokens of output ChatGPT can generate. Max will depend on the size of the prompt (default 1500)\n    -t, --timeout SECONDS            Specify the max wait time for ChatGPT response.\n        --overwrite ANSWER           Always overwrite existing output files, 'y' for yes, 'n' for no, or 'a' for ask. Default to ask.\n    -N, --no                         Never overwrite existing output files, same as --overwrite=n.\n    -v, --verbose                    Show extra output and progress info\n    -d, --debug                      Show debugging output to help diagnose issues\n    -h, --help                       Prints this help\n```\n\n### Interactive mode\n\nA basic interactive mode exists too, where you are prompted for options. \n\nStart interactive mode by not specifying anything for `REFACTOR_TYPE_OR_COMMAND_FILE` (ie no refactor type or command file)\n\n### Command files and Custom prompts\n\nApart from invoking the tool with CLI options, the tool can also be invoked with a command file.\n\nThis makes it easier to build custom refactor prompts for projects, and run that custom refactor multiple times.\n\nThe command file is a YAML file that contains configuration options to pass to the tool.\n\nThe format of the YAML file is:\n\n```yaml\n# Required options:\nrefactor: refactor type name, eg 'ruby/write_ruby'\n# Optional options:\ninput_file_paths: \n  - input files or directories\noutput_file_path: output file or directory\noutput_template_path: output file template (see docs)\nprompt_file_path: path\nprompt: |\n  A custom prompt to send to AI if the command needs it (otherwise read from file)\ncontext_file_paths: \n    - file1.rb\n    - file2.rb\ncontext_file_paths_from_gems:\n  gem_name: \n    - path/from/gem_root/file1.rb\n    - lib/gem_name/file2.rb\n  gem_name2: \n    - lib/gem_name2/file1.rb\n    - app/controllers/file2.rb\n# Other configuration options:\ncontext_text: |\n    Some extra info to prepend to the prompt\ndiff: true/false (default false)\nai_max_attempts: max times to generate more if AI does not complete generating (default 3)\nai_model: AI model name, OpenAI GPT or Anthropic Claude (default gpt-4-turbo)\nai_temperature: AI temperature (default 0.7)\nai_max_tokens: AI max tokens (default 1500)\nai_timeout: AI timeout (default 60)\noverwrite: y/n/a (default a)\nverbose: true/false (default false)\ndebug: true/false (default false)\n```\n\nThe command file can be invoked by passing it as the first argument to the tool:\n\n```shell\nai_refactor my_command_file.yml\n```\n\nOther options can be passed on the command line and will override the options in the command file.\n\nFor example, if the command file contains:\n\n```shell\nai_refactor my_command_file.yml my_input.rb -d --output foo.rb\n```\n\n### Prompt template substitutions\n\nPrompt text can contain the following substitutions:\n\n* `__{{input_file_path}}__`: the path to the input file\n* `__{{output_file_path}}__`: the path to the output file\n* `__{{prompt_header}}__`: the place the pre-build prompt will be injected, if used\n* `__{{prompt_footer}}__`: prompt text that will be inserted after the prompt, eg the \"make diffs\" prompt if `--diffs` is used\n* `__{{context}}__`: the contents of the context files, if any\n* `__{{content}}__`: the contents of input file, if any\n\n## Outputs\n\nSome refactor tasks will write out to a new file by default, others to stdout.\n\nThe `--output` lets you specify a file to write to instead of the Refactors default behaviour.\n\nIf `--output` is used without a value it overwrites the input with a prompt to overwrite existing files.\n\nYou can also output to a file using a template, `--output-template` to determine the output file name given a template string:\n\nThe template is used to create the output name, where the it can have substitutions, '[FILE]', '[NAME]', '[DIR]', '[REFACTOR]' \u0026 '[EXT]'.\n\nEg `--output-template \"[DIR]/[NAME]_[REFACTOR][EXT]\"`\n\neg for the input `my_dir/my_class.rb`\n- `[FILE]`: `my_class.rb`\n- `[NAME]`: `my_class`\n- `[DIR]`: `my_dir`\n- `[REFACTOR]`: `generic`\n- `[EXT]`: `.rb`\n\n## Configuration\n\n### `.ai_refactor` file\n\nThe tool can be configured using a `.ai_refactor` file in the current directory or in the user's home directory.\n\nThis file provides default CLI switches to add to any `ai_refactor` command.\n\n## Command history\n\nThe tool keeps a history of commands run in the `.ai_refactor_history` file in the current working directory.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/stevegeek/ai_refactor.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevegeek%2Fai_refactor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstevegeek%2Fai_refactor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevegeek%2Fai_refactor/lists"}