senior 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 39730d334959dd50f95ebdf665ac8731d61fc15fda52692e61c6a521d1228b4b
4
+ data.tar.gz: e2d2bb32557f3d3c7c780ec7f4293f0c56f68d885e0ed792ce2e0fce69e00b2a
5
+ SHA512:
6
+ metadata.gz: 30cabd4898ce92778b12e1bac480d6e6de51bf57af8a0f1356ab24f29626cbe1c778c89b89f073880bcb099e871ab45b65cbb0f162555b1a85db9014239f1342
7
+ data.tar.gz: 5654b395cf96a528179988edc566ca0f4eebaa2a8f521859ba64b56f9ba1ffe60ef8a457c71f7d5da3a9ed2920b96568245fe676b2a9711b2cd9ca105cb47c1f
data/.editorconfig ADDED
@@ -0,0 +1,11 @@
1
+ # Unix-style newlines with a newline ending every file
2
+ [*]
3
+ end_of_line = lf
4
+ insert_final_newline = true
5
+ max_line_length = 120
6
+ trim_trailing_whitespace = true
7
+
8
+ # 2 space indentation
9
+ [*.rb]
10
+ indent_style = space
11
+ indent_size = 2
data/.env.example ADDED
@@ -0,0 +1,5 @@
1
+ # Get this from https://platform.openai.com/account/api-keys
2
+ OPEN_AI_ACCESS_TOKEN=
3
+
4
+ # If you have an organization, get this form https://platform.openai.com/account/org-settings
5
+ OPEN_AI_ORGANIZATION_ID=
data/.overcommit.yml ADDED
@@ -0,0 +1,33 @@
1
+ # Use this file to configure the Overcommit hooks you wish to use. This will
2
+ # extend the default configuration defined in:
3
+ # https://github.com/sds/overcommit/blob/master/config/default.yml
4
+ #
5
+ # At the topmost level of this YAML file is a key representing type of hook
6
+ # being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
7
+ # customize each hook, such as whether to only run it on certain files (via
8
+ # `include`), whether to only display output if it fails (via `quiet`), etc.
9
+ #
10
+ # For a complete list of hooks, see:
11
+ # https://github.com/sds/overcommit/tree/master/lib/overcommit/hook
12
+ #
13
+ # For a complete list of options that you can use to customize hooks, see:
14
+ # https://github.com/sds/overcommit#configuration
15
+ #
16
+ # Uncomment the following lines to make the configuration take effect.
17
+
18
+ PreCommit:
19
+ RuboCop:
20
+ enabled: true
21
+ on_warn: fail # Treat all warnings as failures
22
+
23
+ TrailingWhitespace:
24
+ enabled: true
25
+ # exclude:
26
+ # - '**/db/structure.sql' # Ignore trailing whitespace in generated files
27
+ #
28
+ #PostCheckout:
29
+ # ALL: # Special hook name that customizes all hooks of this type
30
+ # quiet: true # Change all post-checkout hooks to only display output on failure
31
+ #
32
+ # IndexTags:
33
+ # enabled: true # Generate a tags file with `ctags` each time HEAD changes
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.2
7
+ DisplayCopNames: true
8
+ NewCops: enable
9
+ Exclude:
10
+ - spec/support/broken_code.rb
11
+
12
+ # --------------------- Security ----------------------
13
+ Security/Eval:
14
+ Exclude:
15
+ - lib/senior/agent.rb
16
+
17
+ # ----------------------- Style -----------------------
18
+
19
+ Style/StringLiterals:
20
+ Enabled: true
21
+ EnforcedStyle: single_quotes
22
+
23
+ Style/StringLiteralsInInterpolation:
24
+ Enabled: true
25
+ EnforcedStyle: double_quotes
26
+
27
+ # ----------------------- Layout ----------------------
28
+
29
+ Layout/LineLength:
30
+ Max: 120
31
+
32
+ # ---------------------- Metrics ----------------------
33
+
34
+ Metrics/BlockLength:
35
+ Exclude:
36
+ - '**/*_spec.rb'
37
+ - senior.gemspec
38
+
39
+ Metrics/MethodLength:
40
+ Exclude:
41
+ - lib/senior/brains/open_ai.rb
42
+
43
+ # ----------------------- RSpec -----------------------
44
+
45
+ RSpec/ExampleLength:
46
+ Enabled: false
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.2.2
data/.yardstick.yml ADDED
@@ -0,0 +1,22 @@
1
+ threshold: 100
2
+ rules:
3
+ ApiTag::Presence:
4
+ enabled: true
5
+ ApiTag::Inclusion:
6
+ enabled: true
7
+ ApiTag::ProtectedMethod:
8
+ enabled: true
9
+ ApiTag::PrivateMethod:
10
+ enabled: true
11
+ ExampleTag:
12
+ enabled: true
13
+ ReturnTag:
14
+ enabled: true
15
+ Summary::Presence:
16
+ enabled: true
17
+ Summary::Length:
18
+ enabled: false
19
+ Summary::Delimiter:
20
+ enabled: true
21
+ Summary::SingleLine:
22
+ enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ ## [Unreleased]
2
+
3
+ - Ability to wrap a method invocation. For example, `Senior.suggest_fix { my_broken_method(2) }`
4
+ - Test generation `Senior.write_tests_for {}`
5
+ - Configure API keys for the OpenAI
6
+ - Use different OpenAI models
7
+ - Parametrize the calls to OpenAI
8
+ - See price per call
9
+ - Set API calling and pricing limits
10
+ - Use different AI APIs, not just OpenAI
11
+ - Global convenience methods such as `auto_debug { }`
12
+ - Guard plugin
13
+ - RBS Signatures
14
+ - Domain exceptions
15
+
16
+ ## [0.1.0] - 2023-04-10
17
+
18
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at wilson.dsigns@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in senior.gemspec
6
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :bundler do
4
+ watch('senior.gemspec')
5
+ end
6
+
7
+ guard :bundler_audit, run_on_start: true do
8
+ watch('Gemfile.lock')
9
+ end
10
+
11
+ group :tests do
12
+ guard :rspec, all_on_start: true, cmd: 'COVERAGE=false bundle exec rspec --format progress' do
13
+ watch(%r{^spec/.+_spec\.rb$})
14
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
15
+ watch('spec/spec_helper.rb') { 'spec' }
16
+ end
17
+ end
18
+
19
+ guard :rubocop do
20
+ watch(/.+\.rb$/)
21
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
22
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Wilson Silva
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Senior
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/senior.svg)](https://badge.fury.io/rb/senior)
4
+ [![Tests](https://github.com/wilsonsilva/senior/actions/workflows/main.yml/badge.svg)](https://github.com/wilsonsilva/senior/actions/workflows/main.yml)
5
+
6
+ Provides AI-powered debugging and automatic suggestion of code fixes. It makes use of OpenAI's language model to analyze
7
+ and modify the source code of broken methods, allowing them to be fixed automatically.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add senior
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install senior
18
+
19
+ ## Usage
20
+
21
+ Before using Senior, ensure that the environment variables `OPEN_AI_ACCESS_TOKEN` and `OPEN_AI_ORGANIZATION_ID` are
22
+ defined. These variables are used by the gem to authenticate and access OpenAI's language model.
23
+
24
+ ### Auto-debugging a broken method
25
+ To debug a broken method, call Senior.auto_debug and pass in the broken method, its arguments, and optionally its
26
+ source code. The method will be called repeatedly, with modifications made to its source code each time, until it no
27
+ longer raises exceptions.
28
+
29
+ ```ruby
30
+ def square(n) = n * y
31
+
32
+ result = Senior.auto_debug(method(:square), 2)
33
+ puts result # => 4
34
+ ```
35
+
36
+ ### Suggesting a fix for a broken method
37
+ To suggest a fix for a broken method, call Senior.suggest_fix and pass in the broken method and its arguments.
38
+ The method will be analyzed and a fix will be suggested in the form of modified source code.
39
+
40
+ ```ruby
41
+ def square(n) = n * y
42
+
43
+ suggestion = Senior.suggest_fix(method(:square), 2)
44
+ puts suggestion # => "def square(n) = n * n"
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`.
52
+
53
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
56
+ which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file
57
+ to [rubygems.org](https://rubygems.org).
58
+
59
+ The health and maintainability of the codebase is ensured through a set of
60
+ Rake tasks to test, lint and audit the gem for security vulnerabilities and documentation:
61
+
62
+ ```
63
+ rake build # Build senior.gem into the pkg directory
64
+ rake build:checksum # Generate SHA512 checksum if senior.gem into the checksums directory
65
+ rake bundle:audit:check # Checks the Gemfile.lock for insecure dependencies
66
+ rake bundle:audit:update # Updates the bundler-audit vulnerability database
67
+ rake clean # Remove any temporary products
68
+ rake clobber # Remove any generated files
69
+ rake coverage # Run spec with coverage
70
+ rake install # Build and install senior.gem into system gems
71
+ rake install:local # Build and install senior.gem into system gems without network access
72
+ rake qa # Test, lint and perform security and documentation audits
73
+ rake release[remote] # Create a tag, build and push senior.gem to rubygems.org
74
+ rake rubocop # Run RuboCop
75
+ rake rubocop:autocorrect # Autocorrect RuboCop offenses (only when it's safe)
76
+ rake rubocop:autocorrect_all # Autocorrect RuboCop offenses (safe and unsafe)
77
+ rake spec # Run RSpec code examples
78
+ rake verify_measurements # Verify that yardstick coverage is at least 100%
79
+ rake yard # Generate YARD Documentation
80
+ rake yard:junk # Check the junk in your YARD Documentation
81
+ rake yardstick_measure # Measure docs in lib/**/*.rb with yardstick
82
+ ```
83
+
84
+ ### Type checking
85
+
86
+ This gem leverages [RBS](https://github.com/ruby/rbs), a language to describe the structure of Ruby programs. It is
87
+ used to provide type checking and autocompletion in your editor. Run `bundle exec typeprof FILENAME` to generate
88
+ an RBS definition for the given Ruby file. And validate all definitions using [Steep](https://github.com/soutaro/steep)
89
+ with the command `bundle exec steep check`.
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wilsonsilva/senior. This project is intended
94
+ to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the
95
+ [code of conduct](https://github.com/wilsonsilva/senior/blob/main/CODE_OF_CONDUCT.md).
96
+
97
+ ## License
98
+
99
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
100
+
101
+ ## Code of Conduct
102
+
103
+ Everyone interacting in the Senior project's codebases, issue trackers, chat rooms and mailing lists is expected to
104
+ follow the [code of conduct](https://github.com/wilsonsilva/senior/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/audit/task'
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+ require 'rubocop/rake_task'
7
+ require 'yaml'
8
+ require 'yard/rake/yardoc_task'
9
+ require 'yard-junk/rake'
10
+ require 'yardstick/rake/measurement'
11
+ require 'yardstick/rake/verify'
12
+
13
+ yardstick_options = YAML.load_file('.yardstick.yml')
14
+
15
+ Bundler::Audit::Task.new
16
+ RSpec::Core::RakeTask.new(:spec)
17
+ RuboCop::RakeTask.new
18
+ YARD::Rake::YardocTask.new
19
+ YardJunk::Rake.define_task
20
+ Yardstick::Rake::Measurement.new(:yardstick_measure, yardstick_options)
21
+ Yardstick::Rake::Verify.new
22
+
23
+ task default: %i[spec rubocop]
24
+
25
+ # Remove the report on rake clobber
26
+ CLEAN.include('measurements', 'doc', '.yardoc', 'tmp')
27
+
28
+ # Delete these files and folders when running rake clobber.
29
+ CLOBBER.include('coverage', '.rspec_status')
30
+
31
+ desc 'Run spec with coverage'
32
+ task :coverage do
33
+ ENV['COVERAGE'] = 'true'
34
+ Rake::Task['spec'].execute
35
+ `open coverage/index.html`
36
+ end
37
+
38
+ desc 'Test, lint and perform security and documentation audits'
39
+ task qa: %w[spec rubocop yard:junk verify_measurements bundle:audit]
data/Steepfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ target :lib do
4
+ signature 'sig'
5
+
6
+ check 'lib'
7
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Senior
4
+ # Suggests code fixes
5
+ #
6
+ # @api private
7
+ #
8
+ class Agent
9
+ # Instantiates a new Agent
10
+ #
11
+ # @api private
12
+ #
13
+ def initialize(brain = Brains::OpenAI.new)
14
+ @brain = brain
15
+ end
16
+
17
+ # Calls the given method continuously, using AI to attempt to fix it until it is no longer raises exceptions
18
+ #
19
+ # @api private
20
+ #
21
+ # @param broken_method [Method] A broken method to be fixed
22
+ # @param args [Object] Arguments given to a broken method
23
+ # @param broken_method_source [String|nil] Source code of the broken method
24
+ #
25
+ # @return [Object] The return value of the previously broken but now fixed method
26
+ #
27
+ def auto_debug(broken_method, args, broken_method_source = nil)
28
+ broken_method.call(*args)
29
+ rescue StandardError => e
30
+ puts "The invocation #{broken_method.name}(#{args}) failed. Debugging..."
31
+
32
+ suggested_fix_method_source = brain.suggest_fix(
33
+ erroneous_source: broken_method_source || broken_method.source,
34
+ exception_backtrace: e.backtrace&.first.to_s
35
+ )
36
+
37
+ puts "\nSuggested fix:\n#{suggested_fix_method_source}\n\n"
38
+
39
+ suggested_fix_method_name = eval(suggested_fix_method_source)
40
+
41
+ auto_debug(method(suggested_fix_method_name), args, suggested_fix_method_source)
42
+ end
43
+
44
+ # Suggests a fix for a broken method
45
+ #
46
+ # @api private
47
+ #
48
+ # @param broken_method [Method] A broken method to be fixed
49
+ # @param args [Object] Arguments given to a broken method
50
+ #
51
+ # @return [String] The suggested fix
52
+ #
53
+ def suggest_fix(broken_method, args)
54
+ broken_method.call(*args)
55
+ rescue StandardError => e
56
+ brain.suggest_fix(
57
+ erroneous_source: broken_method.source,
58
+ exception_backtrace: e.backtrace&.first.to_s
59
+ )
60
+ end
61
+
62
+ private
63
+
64
+ # The interface to an AI's API
65
+ #
66
+ # @api private
67
+ #
68
+ # @return [Object]
69
+ #
70
+ attr_reader :brain
71
+ end
72
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openai'
4
+
5
+ module Senior
6
+ # Interface with the different AI API's
7
+ module Brains
8
+ # Interface to OpenAI's API
9
+ #
10
+ # @api private
11
+ #
12
+ class OpenAI
13
+ # Suggests a fix for a broken method
14
+ #
15
+ # @api private
16
+ #
17
+ # @param erroneous_source [String] Source code of a broken method
18
+ # @param exception_backtrace [String] First line of a broken method's backtrace
19
+ #
20
+ # @return [String] The suggested fix
21
+ #
22
+ def suggest_fix(erroneous_source:, exception_backtrace:)
23
+ prompt = <<~PROMPT
24
+ This is a method's source and the error. Fix the method:
25
+
26
+ ## Source:
27
+ #{erroneous_source}
28
+
29
+ ## Error:
30
+ #{exception_backtrace}
31
+
32
+ ## Updated source:
33
+ PROMPT
34
+
35
+ request_completion(prompt)
36
+ end
37
+
38
+ private
39
+
40
+ # Creates an instance of OpenAI::Client
41
+ #
42
+ # @api private
43
+ #
44
+ # @return [OpenAI::Client] A client to communicate with OpenAI's API
45
+ #
46
+ def open_ai_client
47
+ @open_ai_client ||= ::OpenAI::Client.new(
48
+ access_token: ENV.fetch('OPEN_AI_ACCESS_TOKEN'),
49
+ organization_id: ENV.fetch('OPEN_AI_ORGANIZATION_ID')
50
+ )
51
+ end
52
+
53
+ # Creates a completion in OpenAI's API
54
+ #
55
+ # @api private
56
+ #
57
+ # @param prompt [String] The prompt for which to generate a completion
58
+ # @param max_tokens [Integer] The maximum number of tokens to generate in the completion. Default value is 1024
59
+ #
60
+ # @return [String] The create completion
61
+ #
62
+ def request_completion(prompt, max_tokens = 1024)
63
+ response = open_ai_client.completions(
64
+ parameters: {
65
+ model: 'text-davinci-003',
66
+ prompt:,
67
+ max_tokens:,
68
+ n: 1,
69
+ stop: nil,
70
+ temperature: 0.7
71
+ }
72
+ )
73
+
74
+ raise 'No completion found' unless response['choices'].any?
75
+
76
+ response.dig('choices', 0, 'text').strip
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Senior
4
+ VERSION = '0.1.0'
5
+ end
data/lib/senior.rb ADDED
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'senior/brains/open_ai'
4
+ require_relative 'senior/agent'
5
+ require_relative 'senior/version'
6
+
7
+ require 'method_source'
8
+
9
+ # Encapsulates all the gem's logic
10
+ module Senior
11
+ # Calls the given method continuously, using AI to attempt to fix it until it is no longer raises exceptions
12
+ #
13
+ # @api public
14
+ #
15
+ # @example Debugging a broken method
16
+ # def square(n) = n * y
17
+ #
18
+ # result = Senior.auto_debug(method(:square), 2)
19
+ # result # => 4
20
+ #
21
+ # @param broken_method [Method] A broken method to be fixed
22
+ # @param args [Object] Arguments given to a broken method
23
+ # @param broken_method_source [String|nil] Source code of the broken method
24
+ #
25
+ # @return [Object] The return value of the previously broken but now fixed method
26
+ #
27
+ def self.auto_debug(broken_method, args, broken_method_source = nil)
28
+ agent.auto_debug(broken_method, args, broken_method_source)
29
+ end
30
+
31
+ # Suggests a fix for a broken method
32
+ #
33
+ # @api public
34
+ #
35
+ # @example Suggesting a fix for a broken method
36
+ # def square(n) = n * y
37
+ #
38
+ # suggestion = Senior.suggest_fix(method(:square), 2)
39
+ # suggestion # => "def square(n) = n * n"
40
+ #
41
+ # @param broken_method [Method] A broken method to be fixed
42
+ # @param args [Object] Arguments given to a broken method
43
+ #
44
+ # @return [String] The suggested fix
45
+ #
46
+ def self.suggest_fix(broken_method, args)
47
+ agent.suggest_fix(broken_method, args)
48
+ end
49
+
50
+ # Returns an instance of the agent
51
+ #
52
+ # @api private
53
+ #
54
+ # @return [Agent] An instance of the agent
55
+ #
56
+ def self.agent
57
+ @agent ||= Agent.new
58
+ end
59
+ end
@@ -0,0 +1,4 @@
1
+ # Monkey patched by the gem method_source
2
+ class Method
3
+ def source: () -> String
4
+ end
@@ -0,0 +1,29 @@
1
+ module OpenAI
2
+ class Client
3
+ @files: untyped
4
+ @finetunes: untyped
5
+ @images: untyped
6
+ @models: untyped
7
+
8
+ def initialize: (?access_token: String|nil, ?organization_id: String|nil, ?uri_base: nil, ?request_timeout: nil) -> void
9
+ def chat: (?parameters: Hash[untyped, untyped]) -> untyped
10
+ def completions: (?parameters: Hash[untyped, untyped]) -> untyped
11
+ def edits: (?parameters: Hash[untyped, untyped]) -> untyped
12
+ def embeddings: (?parameters: Hash[untyped, untyped]) -> untyped
13
+ def files: -> untyped
14
+ def finetunes: -> untyped
15
+ def images: -> untyped
16
+ def models: -> untyped
17
+ def moderations: (?parameters: Hash[untyped, untyped]) -> untyped
18
+ def transcribe: (?parameters: Hash[untyped, untyped]) -> untyped
19
+ def translate: (?parameters: Hash[untyped, untyped]) -> untyped
20
+
21
+ def self.get: (path: untyped) -> untyped
22
+ def self.json_post: (path: String, parameters: Hash[untyped, untyped]) -> untyped
23
+ def self.multipart_post: (path: String, ?parameters: Hash[untyped, untyped]?) -> untyped
24
+ def self.delete: (path: untyped) -> untyped
25
+ def self.uri: (path: String) -> untyped
26
+ def self.headers: -> Hash[String, String]
27
+ def self.request_timeout: -> untyped
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ module Senior
2
+ class Agent
3
+ def initialize: (?_IBrain brain) -> void
4
+ def auto_debug: (Method broken_method, untyped args, String? | nil broken_method_source) -> untyped
5
+ def suggest_fix: (Method broken_method, untyped args) -> String
6
+
7
+ private
8
+
9
+ attr_reader brain: _IBrain
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ interface _IBrain
2
+ def suggest_fix: (erroneous_source: String, exception_backtrace: String) -> String
3
+ end
@@ -0,0 +1,14 @@
1
+ module Senior
2
+ module Brains
3
+ class OpenAI
4
+ @open_ai_client: untyped
5
+
6
+ def suggest_fix: (erroneous_source: String, exception_backtrace: String) -> String
7
+
8
+ private
9
+
10
+ def open_ai_client: -> untyped
11
+ def request_completion: (String prompt, ?Integer max_tokens) -> String
12
+ end
13
+ end
14
+ end
data/sig/senior.rbs ADDED
@@ -0,0 +1,12 @@
1
+ module Senior
2
+ VERSION: String
3
+ self.@agent: Agent
4
+
5
+ def self.auto_debug: (Method broken_method, untyped args, String? | nil broken_method_source) -> untyped
6
+
7
+ def self.suggest_fix: (Method broken_method, untyped args) -> String
8
+
9
+ private
10
+
11
+ def self.agent: -> Agent
12
+ end
metadata ADDED
@@ -0,0 +1,425 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: senior
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wilson Silva
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: method_source
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-openai
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler-audit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dotenv
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.18'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.18'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-bundler-audit
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.7'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.7'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.5'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.5'
139
+ - !ruby/object:Gem::Dependency
140
+ name: overcommit
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.60'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.60'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rake
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '13.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '13.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rbs
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '2.8'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '2.8'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rspec
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '3.12'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '3.12'
195
+ - !ruby/object:Gem::Dependency
196
+ name: rubocop
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: '1.49'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: '1.49'
209
+ - !ruby/object:Gem::Dependency
210
+ name: rubocop-rake
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '0.6'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '0.6'
223
+ - !ruby/object:Gem::Dependency
224
+ name: rubocop-rspec
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - '='
228
+ - !ruby/object:Gem::Version
229
+ version: '2.19'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - '='
235
+ - !ruby/object:Gem::Version
236
+ version: '2.19'
237
+ - !ruby/object:Gem::Dependency
238
+ name: simplecov
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - '='
242
+ - !ruby/object:Gem::Version
243
+ version: '0.17'
244
+ type: :development
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - '='
249
+ - !ruby/object:Gem::Version
250
+ version: '0.17'
251
+ - !ruby/object:Gem::Dependency
252
+ name: simplecov-console
253
+ requirement: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - "~>"
256
+ - !ruby/object:Gem::Version
257
+ version: '0.9'
258
+ type: :development
259
+ prerelease: false
260
+ version_requirements: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - "~>"
263
+ - !ruby/object:Gem::Version
264
+ version: '0.9'
265
+ - !ruby/object:Gem::Dependency
266
+ name: steep
267
+ requirement: !ruby/object:Gem::Requirement
268
+ requirements:
269
+ - - "~>"
270
+ - !ruby/object:Gem::Version
271
+ version: '1.3'
272
+ type: :development
273
+ prerelease: false
274
+ version_requirements: !ruby/object:Gem::Requirement
275
+ requirements:
276
+ - - "~>"
277
+ - !ruby/object:Gem::Version
278
+ version: '1.3'
279
+ - !ruby/object:Gem::Dependency
280
+ name: typeprof
281
+ requirement: !ruby/object:Gem::Requirement
282
+ requirements:
283
+ - - "~>"
284
+ - !ruby/object:Gem::Version
285
+ version: '0.21'
286
+ type: :development
287
+ prerelease: false
288
+ version_requirements: !ruby/object:Gem::Requirement
289
+ requirements:
290
+ - - "~>"
291
+ - !ruby/object:Gem::Version
292
+ version: '0.21'
293
+ - !ruby/object:Gem::Dependency
294
+ name: vcr
295
+ requirement: !ruby/object:Gem::Requirement
296
+ requirements:
297
+ - - "~>"
298
+ - !ruby/object:Gem::Version
299
+ version: '6.1'
300
+ type: :development
301
+ prerelease: false
302
+ version_requirements: !ruby/object:Gem::Requirement
303
+ requirements:
304
+ - - "~>"
305
+ - !ruby/object:Gem::Version
306
+ version: '6.1'
307
+ - !ruby/object:Gem::Dependency
308
+ name: webmock
309
+ requirement: !ruby/object:Gem::Requirement
310
+ requirements:
311
+ - - "~>"
312
+ - !ruby/object:Gem::Version
313
+ version: '3.18'
314
+ type: :development
315
+ prerelease: false
316
+ version_requirements: !ruby/object:Gem::Requirement
317
+ requirements:
318
+ - - "~>"
319
+ - !ruby/object:Gem::Version
320
+ version: '3.18'
321
+ - !ruby/object:Gem::Dependency
322
+ name: yard
323
+ requirement: !ruby/object:Gem::Requirement
324
+ requirements:
325
+ - - "~>"
326
+ - !ruby/object:Gem::Version
327
+ version: '0.9'
328
+ type: :development
329
+ prerelease: false
330
+ version_requirements: !ruby/object:Gem::Requirement
331
+ requirements:
332
+ - - "~>"
333
+ - !ruby/object:Gem::Version
334
+ version: '0.9'
335
+ - !ruby/object:Gem::Dependency
336
+ name: yard-junk
337
+ requirement: !ruby/object:Gem::Requirement
338
+ requirements:
339
+ - - "~>"
340
+ - !ruby/object:Gem::Version
341
+ version: 0.0.9
342
+ type: :development
343
+ prerelease: false
344
+ version_requirements: !ruby/object:Gem::Requirement
345
+ requirements:
346
+ - - "~>"
347
+ - !ruby/object:Gem::Version
348
+ version: 0.0.9
349
+ - !ruby/object:Gem::Dependency
350
+ name: yardstick
351
+ requirement: !ruby/object:Gem::Requirement
352
+ requirements:
353
+ - - "~>"
354
+ - !ruby/object:Gem::Version
355
+ version: '0.9'
356
+ type: :development
357
+ prerelease: false
358
+ version_requirements: !ruby/object:Gem::Requirement
359
+ requirements:
360
+ - - "~>"
361
+ - !ruby/object:Gem::Version
362
+ version: '0.9'
363
+ description: |-
364
+ This gem provides a simple interface to OpenAI's GPT4 API for code repair. Given a piece of broken
365
+ code, the gem generates a corrected version.
366
+ email:
367
+ - wilson.dsigns@gmail.com
368
+ executables: []
369
+ extensions: []
370
+ extra_rdoc_files: []
371
+ files:
372
+ - ".editorconfig"
373
+ - ".env.example"
374
+ - ".overcommit.yml"
375
+ - ".rspec"
376
+ - ".rubocop.yml"
377
+ - ".tool-versions"
378
+ - ".yardstick.yml"
379
+ - CHANGELOG.md
380
+ - CODE_OF_CONDUCT.md
381
+ - Gemfile
382
+ - Guardfile
383
+ - LICENSE.txt
384
+ - README.md
385
+ - Rakefile
386
+ - Steepfile
387
+ - lib/senior.rb
388
+ - lib/senior/agent.rb
389
+ - lib/senior/brains/open_ai.rb
390
+ - lib/senior/version.rb
391
+ - sig/gems/method_source/method.rbs
392
+ - sig/gems/ruby-openai/client.rbs
393
+ - sig/senior.rbs
394
+ - sig/senior/agent.rbs
395
+ - sig/senior/brains/i_brain.rbs
396
+ - sig/senior/brains/open_ai.rbs
397
+ homepage: https://github.com/wilsonsilva/senior
398
+ licenses:
399
+ - MIT
400
+ metadata:
401
+ allowed_push_host: https://rubygems.org
402
+ rubygems_mfa_required: 'true'
403
+ homepage_uri: https://github.com/wilsonsilva/senior
404
+ source_code_uri: https://github.com/wilsonsilva/senior
405
+ changelog_uri: https://github.com/wilsonsilva/senior/blob/main/CHANGELOG.md
406
+ post_install_message:
407
+ rdoc_options: []
408
+ require_paths:
409
+ - lib
410
+ required_ruby_version: !ruby/object:Gem::Requirement
411
+ requirements:
412
+ - - ">="
413
+ - !ruby/object:Gem::Version
414
+ version: 3.2.0
415
+ required_rubygems_version: !ruby/object:Gem::Requirement
416
+ requirements:
417
+ - - ">="
418
+ - !ruby/object:Gem::Version
419
+ version: '0'
420
+ requirements: []
421
+ rubygems_version: 3.4.10
422
+ signing_key:
423
+ specification_version: 4
424
+ summary: AI coding companion
425
+ test_files: []