ghostest 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.idea/misc.xml +4 -0
  3. data/.idea/modules.xml +8 -0
  4. data/.idea/vcs.xml +6 -0
  5. data/.rubocop.yml +236 -0
  6. data/.ruby-version +1 -0
  7. data/README.md +31 -0
  8. data/Rakefile +4 -0
  9. data/exe/ghostest +74 -0
  10. data/ghostest.gemspec +48 -0
  11. data/lib/ghostest/attr_reader.rb +11 -0
  12. data/lib/ghostest/config/agent.rb +30 -0
  13. data/lib/ghostest/config.rb +65 -0
  14. data/lib/ghostest/config_error.rb +5 -0
  15. data/lib/ghostest/error.rb +5 -0
  16. data/lib/ghostest/languages/ruby.rb +21 -0
  17. data/lib/ghostest/logger.rb +32 -0
  18. data/lib/ghostest/manager.rb +72 -0
  19. data/lib/ghostest/test_condition.rb +24 -0
  20. data/lib/ghostest/version.rb +3 -0
  21. data/lib/ghostest.rb +58 -0
  22. data/lib/google_custom_search.rb +30 -0
  23. data/lib/i18n_translator.rb +66 -0
  24. data/lib/initializers/i18n.rb +9 -0
  25. data/lib/llm/agents/base.rb +31 -0
  26. data/lib/llm/agents/reviewer.rb +50 -0
  27. data/lib/llm/agents/test_designer.rb +43 -0
  28. data/lib/llm/agents/test_programmer.rb +45 -0
  29. data/lib/llm/clients/azure_open_ai.rb +15 -0
  30. data/lib/llm/clients/base.rb +88 -0
  31. data/lib/llm/clients/open_ai.rb +14 -0
  32. data/lib/llm/functions/add_to_memory.rb +41 -0
  33. data/lib/llm/functions/base.rb +13 -0
  34. data/lib/llm/functions/exec_rspec_test.rb +39 -0
  35. data/lib/llm/functions/fix_one_rspec_test.rb +55 -0
  36. data/lib/llm/functions/get_files_list.rb +29 -0
  37. data/lib/llm/functions/get_gem_files_list.rb +43 -0
  38. data/lib/llm/functions/make_new_file.rb +43 -0
  39. data/lib/llm/functions/overwrite_file.rb +42 -0
  40. data/lib/llm/functions/read_file.rb +43 -0
  41. data/lib/llm/functions/record_lgtm.rb +48 -0
  42. data/lib/llm/functions/report_bug.rb +34 -0
  43. data/lib/llm/functions/switch_assignee.rb +74 -0
  44. data/lib/llm/message_container.rb +63 -0
  45. data/sig/ghostest.rbs +4 -0
  46. metadata +245 -0
@@ -0,0 +1,43 @@
1
+ module Llm
2
+ module Functions
3
+ class GetGemFilesList < Base
4
+ def function_name
5
+ :get_gem_files_list
6
+ end
7
+
8
+ def self.gem_name_enums
9
+ @gem_name_enums ||= ::Bundler.locked_gems.specs.map(&:full_name)
10
+ end
11
+
12
+ def definition
13
+ return @definition unless @definition.nil?
14
+
15
+ @definition = {
16
+ name: self.function_name,
17
+ description: I18n.t("ghostest.functions.#{self.function_name}.description"),
18
+ parameters: {
19
+ type: :object,
20
+ properties: {
21
+ gem_name: {
22
+ type: :string,
23
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.gem_name"),
24
+ enum: self.class.gem_name_enums,
25
+ },
26
+ },
27
+ required: [:gem_name],
28
+ },
29
+ }
30
+ @definition
31
+ end
32
+
33
+ def execute_and_generate_message(args)
34
+ unless self.class.gem_name_enums.include?(args['gem_name'])
35
+ return { message: 'invalid gem_name. gem_name is below' + self.class.gem_name_enums.join("\n") }
36
+ end
37
+ files_list = Dir.glob("#{Gem.paths.home}/gems/#{args['gem_name']}/**/*.{rb,yml}")
38
+
39
+ { files_list: }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ module Llm
2
+ module Functions
3
+ class MakeNewFile < Base
4
+ def function_name
5
+ :make_new_file
6
+ end
7
+
8
+ def definition
9
+ return @definition unless @definition.nil?
10
+
11
+ @definition = {
12
+ name: self.function_name,
13
+ description: I18n.t("ghostest.functions.#{self.function_name}.description"),
14
+ parameters: {
15
+ type: :object,
16
+ properties: {
17
+ filepath: {
18
+ type: :string,
19
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.filepath"),
20
+ },
21
+ file_contents: {
22
+ type: :string,
23
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.file_contents"),
24
+ },
25
+ },
26
+ required: [:filepath, :file_contents],
27
+ },
28
+ }
29
+ @definition
30
+ end
31
+
32
+ def execute_and_generate_message(args)
33
+ dirname = File.dirname(args[:filepath])
34
+ unless File.directory?(dirname)
35
+ FileUtils.mkdir_p(dirname)
36
+ end
37
+ File.write(args[:filepath], args[:file_contents])
38
+
39
+ { result: "success" }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,42 @@
1
+ module Llm
2
+ module Functions
3
+ class OverwriteFile < Base
4
+ def function_name
5
+ :overwrite_file
6
+ end
7
+
8
+ def definition
9
+ return @definition unless @definition.nil?
10
+
11
+ @definition = {
12
+ name: self.function_name,
13
+ description: I18n.t("ghostest.functions.#{self.function_name}.description"),
14
+ parameters: {
15
+ type: :object,
16
+ properties: {
17
+ filepath: {
18
+ type: :string,
19
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.filepath"),
20
+ },
21
+ new_text: {
22
+ type: :string,
23
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.new_text"),
24
+ },
25
+ },
26
+ required: [:filepath, :new_text],
27
+ },
28
+ }
29
+ @definition
30
+ end
31
+
32
+ def execute_and_generate_message(args)
33
+ unless File.exist?(args[:filepath])
34
+ raise Ghostest::Error.new("File not found: #{args[:filepath]}")
35
+ end
36
+ File.write(args[:filepath], args[:new_text])
37
+
38
+ { result: :success }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ module Llm
2
+ module Functions
3
+ class ReadFile < Base
4
+ def function_name
5
+ :read_file
6
+ end
7
+
8
+ def definition
9
+ return @definition unless @definition.nil?
10
+
11
+ @definition = {
12
+ name: self.function_name,
13
+ description: I18n.t("ghostest.functions.#{self.function_name}.description"),
14
+ parameters: {
15
+ type: :object,
16
+ properties: {
17
+ filepath: {
18
+ type: :string,
19
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.filepath"),
20
+ },
21
+ },
22
+ required: [:filepath],
23
+ },
24
+ }
25
+ @definition
26
+ end
27
+
28
+ def execute_and_generate_message(args)
29
+ if File.exist?(args[:filepath])
30
+ {
31
+ filepath: args[:filepath],
32
+ file_contents: File.read(args[:filepath]),
33
+ }
34
+ else
35
+ {
36
+ filepath: args[:filepath],
37
+ error: "File not found.",
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,48 @@
1
+ module Llm
2
+ module Functions
3
+ class RecordLgtm < Base
4
+ def function_name
5
+ :record_lgtm
6
+ end
7
+
8
+ def initialize
9
+ @lgtm = false
10
+ end
11
+
12
+ def stop_llm_call?
13
+ # stop always
14
+ true
15
+ end
16
+
17
+ def lgtm?
18
+ !!@lgtm
19
+ end
20
+
21
+ def definition
22
+ return @definition unless @definition.nil?
23
+
24
+ @definition = {
25
+ name: self.function_name,
26
+ description: I18n.t("ghostest.functions.#{self.function_name}.description"),
27
+ parameters: {
28
+ type: :object,
29
+ properties: {
30
+ message: {
31
+ type: :string,
32
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.message"),
33
+ },
34
+ },
35
+ required: [:message],
36
+ },
37
+ }
38
+ @definition
39
+ end
40
+
41
+ def execute_and_generate_message(args)
42
+ @lgtm = true
43
+
44
+ { message: args[:message] }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,34 @@
1
+ module Llm
2
+ module Functions
3
+ class ReportBug < Base
4
+ def function_name
5
+ :report_bug
6
+ end
7
+
8
+ def definition
9
+ return @definition unless @definition.nil?
10
+
11
+ @definition = {
12
+ name: self.function_name,
13
+ description: I18n.t("ghostest.functions.#{self.function_name}.description"),
14
+ parameters: {
15
+ type: :object,
16
+ properties: {
17
+ message: {
18
+ type: :string,
19
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.message"),
20
+ },
21
+ },
22
+ required: [:message],
23
+ },
24
+ }
25
+ @definition
26
+ end
27
+
28
+ def execute_and_generate_message(args)
29
+ puts(args['message'])
30
+ exit 0
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,74 @@
1
+ module Llm
2
+ module Functions
3
+ class SwitchAssignee < Base
4
+ include AttrReader
5
+ attr_reader :assignee, :messages
6
+
7
+ def function_name
8
+ :switch_assignee
9
+ end
10
+
11
+ def initialize(assignee, agents)
12
+ @messages = []
13
+ @assignee = assignee
14
+ @agents = agents
15
+ @valid_agents = agents.map(&:name_with_type)
16
+ end
17
+
18
+ def stop_llm_call?
19
+ # stop always
20
+ true
21
+ end
22
+
23
+ def last_message
24
+ @messages.last[:message]
25
+ end
26
+
27
+ def last_assignee
28
+ @messages.last[:name]
29
+ end
30
+
31
+ def definition
32
+ # @assigneeに応じて毎回作り直す
33
+ definition = {
34
+ name: self.function_name,
35
+ description: I18n.t("ghostest.functions.#{self.function_name}.description"),
36
+ parameters: {
37
+ type: :object,
38
+ properties: {
39
+ next_assignee: {
40
+ type: :string,
41
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.next_assignee"),
42
+ enum: @valid_agents - [@assignee.name_with_type],
43
+ },
44
+ message: {
45
+ type: :string,
46
+ description: I18n.t("ghostest.functions.#{self.function_name}.parameters.message"),
47
+ },
48
+ },
49
+ required: [:assignee, :message],
50
+ },
51
+ }
52
+ definition
53
+ end
54
+
55
+ def execute_and_generate_message(args)
56
+ if args[:next_assignee].nil? || args[:next_assignee].empty?
57
+ raise Ghostest::Error.new("next_assignee is required")
58
+ end
59
+ if args[:message].nil? || args[:message].empty?
60
+ raise Ghostest::Error.new("message is required")
61
+ end
62
+ next_assignee = @agents.find { |agent| agent.name_with_type == args[:next_assignee] }
63
+ message = {
64
+ name: @assignee.name_with_type,
65
+ next_assignee: next_assignee.name_with_type,
66
+ message: args[:message],
67
+ }
68
+ @assignee = next_assignee
69
+ @messages << message
70
+ message
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,63 @@
1
+ module Llm
2
+ class MessageContainer
3
+ attr_reader :messages
4
+ def initialize
5
+ @messages = []
6
+ @metas = []
7
+ @token_encoder = Tiktoken.get_encoding("cl100k_base")
8
+
9
+ self.add_default_system_message!
10
+ end
11
+
12
+ def add_system_message(content)
13
+ @metas << { index: @metas.size, token: @token_encoder.encode(content.to_s).size, role: :system }
14
+ @messages << { role: :system, content: }
15
+ end
16
+
17
+ def add_user_message(content)
18
+ @metas << { index: @metas.size, token: @token_encoder.encode(content.to_s).size, role: :user }
19
+ @messages << { role: :user, content: }
20
+ end
21
+
22
+ def add_raw_message(message)
23
+ message = message.with_indifferent_access
24
+ @metas << { index: @metas.size, token: @token_encoder.encode(message[:content].to_s).size, role: message[:role] }
25
+ @messages << message
26
+ end
27
+
28
+ def total_token
29
+ @metas.map { |meta| meta[:token] }.sum
30
+ end
31
+
32
+ def add_raw_messages(messages)
33
+ messages.each do |message|
34
+ self.add_raw_message(message)
35
+ end
36
+ end
37
+
38
+ def to_capped_messages(token_limit: 28_000)
39
+ return @messages if self.total_token <= token_limit
40
+
41
+ # システムメッセージは消さない
42
+ system_metas, not_system_metas = @metas.partition { |message| message[:role] == :system }
43
+ current_token = system_metas.map { |meta| meta[:token] }.sum
44
+ filtered_indexes = system_metas.map { |meta| meta[:index] }
45
+
46
+ # 新しいメッセージを優先的に残す
47
+ not_system_metas.reverse.each do |meta|
48
+ break if token_limit < current_token + meta[:token]
49
+
50
+ current_token += meta[:token]
51
+ filtered_indexes << meta[:index]
52
+ end
53
+
54
+ # システムメッセージが挟み込まれている場合も、並び順を維持すること
55
+ @messages.select.with_index { |message, i| filtered_indexes.include?(i) }
56
+ end
57
+
58
+ def add_default_system_message!
59
+ self.add_system_message("Current time is #{Time.now}")
60
+ end
61
+ end
62
+
63
+ end
data/sig/ghostest.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ghostest
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,245 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ghostest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ryooo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby-openai
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: html2markdown
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: addressable
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: baran
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: tiktoken_ruby
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: google-apis-customsearch_v1
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: colorize
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: i18n
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: indifference
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: Output test code using LLM agents.
168
+ email:
169
+ - ryooo.321@gmail.com
170
+ executables:
171
+ - ghostest
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".idea/misc.xml"
176
+ - ".idea/modules.xml"
177
+ - ".idea/vcs.xml"
178
+ - ".rubocop.yml"
179
+ - ".ruby-version"
180
+ - README.md
181
+ - Rakefile
182
+ - exe/ghostest
183
+ - ghostest.gemspec
184
+ - lib/ghostest.rb
185
+ - lib/ghostest/attr_reader.rb
186
+ - lib/ghostest/config.rb
187
+ - lib/ghostest/config/agent.rb
188
+ - lib/ghostest/config_error.rb
189
+ - lib/ghostest/error.rb
190
+ - lib/ghostest/languages/ruby.rb
191
+ - lib/ghostest/logger.rb
192
+ - lib/ghostest/manager.rb
193
+ - lib/ghostest/test_condition.rb
194
+ - lib/ghostest/version.rb
195
+ - lib/google_custom_search.rb
196
+ - lib/i18n_translator.rb
197
+ - lib/initializers/i18n.rb
198
+ - lib/llm/agents/base.rb
199
+ - lib/llm/agents/reviewer.rb
200
+ - lib/llm/agents/test_designer.rb
201
+ - lib/llm/agents/test_programmer.rb
202
+ - lib/llm/clients/azure_open_ai.rb
203
+ - lib/llm/clients/base.rb
204
+ - lib/llm/clients/open_ai.rb
205
+ - lib/llm/functions/add_to_memory.rb
206
+ - lib/llm/functions/base.rb
207
+ - lib/llm/functions/exec_rspec_test.rb
208
+ - lib/llm/functions/fix_one_rspec_test.rb
209
+ - lib/llm/functions/get_files_list.rb
210
+ - lib/llm/functions/get_gem_files_list.rb
211
+ - lib/llm/functions/make_new_file.rb
212
+ - lib/llm/functions/overwrite_file.rb
213
+ - lib/llm/functions/read_file.rb
214
+ - lib/llm/functions/record_lgtm.rb
215
+ - lib/llm/functions/report_bug.rb
216
+ - lib/llm/functions/switch_assignee.rb
217
+ - lib/llm/message_container.rb
218
+ - sig/ghostest.rbs
219
+ homepage: https://github.com/ryooo/ghostest
220
+ licenses: []
221
+ metadata:
222
+ allowed_push_host: https://rubygems.org
223
+ homepage_uri: https://github.com/ryooo/ghostest
224
+ source_code_uri: https://github.com/ryooo/ghostest
225
+ changelog_uri: https://github.com/ryooo/ghostest
226
+ post_install_message:
227
+ rdoc_options: []
228
+ require_paths:
229
+ - lib
230
+ required_ruby_version: !ruby/object:Gem::Requirement
231
+ requirements:
232
+ - - ">="
233
+ - !ruby/object:Gem::Version
234
+ version: '0'
235
+ required_rubygems_version: !ruby/object:Gem::Requirement
236
+ requirements:
237
+ - - ">="
238
+ - !ruby/object:Gem::Version
239
+ version: '0'
240
+ requirements: []
241
+ rubygems_version: 3.4.6
242
+ signing_key:
243
+ specification_version: 4
244
+ summary: Test code generator by llm
245
+ test_files: []