ask_chatgpt 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81b5ff785019505f049a92996ae9a8f2afbb6bda16375df14a9b101d110a4e3f
4
- data.tar.gz: 0c4c037173a02db028500cf6dbafe67331b3f3b90b2bcd80327f3e0336e678e6
3
+ metadata.gz: 86f36d546bd015947fcac7a2bb9366530ba0ff8a9012f21f2151ae0484ecf647
4
+ data.tar.gz: 136c817bc54317c77325ca0145857262d48680b8b25072e3364dcd0fe526e501
5
5
  SHA512:
6
- metadata.gz: e7d950e4d0cd28e23a37a1987ae6600d8627eea1974360ae138dadf556a9976ced78423afa69b6da94d5f7a6d7d6fac9c41d057d5f9c78b40419cc779a003e93
7
- data.tar.gz: 3062aee654d3d3505af33762a2c781d02a02b6b50edd1539a6ab248914ffaac31169899c1eda184782683c14fbeacad361859ae4b2930f739d90055d71597ee6
6
+ metadata.gz: 7b3112557fcb6e3fa11856693e26dc37df80dedafde058f6f181d303a336f75cc4ab21f9c1daf89dd61d253eea150b05d5f664408ab5299c07210f37d8f9d87c
7
+ data.tar.gz: 4c5a22ca8157813e15fa5e2f639eefd89967ba9bbd300d6c0929bfe18cd3b2ff4e600197d59411520da05626c77ec9ca1786e2b6f3bfd7b51a2d5403f4528098
data/bin/ask_chatgpt CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'optparse'
4
4
  require 'active_support'
5
+ require 'irb'
5
6
 
6
7
  require_relative "../lib/ask_chatgpt"
7
8
 
@@ -24,6 +25,8 @@ parser = OptionParser.new do |opts|
24
25
  ask_chatgpt -f app/models/user.rb -q "create RSpec spec for this model"
25
26
  ask_chatgpt -f test/dummy/Gemfile -q "sort Ruby gems alphabetically"
26
27
 
28
+ Version: #{AskChatGPT::VERSION}
29
+
27
30
  USAGE
28
31
 
29
32
  opts.on("-q", "--question \"Your Prompt\"", String, "Specify your prompt with full context, language, etc.") do |prompt|
@@ -1,7 +1,7 @@
1
1
  module AskChatgpt
2
2
  module Console
3
3
  def gpt
4
- AskChatGPT::Core.call
4
+ AskChatGPT::Executor.call
5
5
  end
6
6
 
7
7
  alias :chatgpt :gpt
@@ -18,6 +18,11 @@ module AskChatgpt
18
18
 
19
19
  attr_reader :scope, :client, :spinner, :cursor
20
20
 
21
+ def self.call
22
+ client = OpenAI::Client.new(access_token: AskChatGPT.access_token)
23
+ AskChatgpt::Executor.new(client)
24
+ end
25
+
21
26
  def initialize(client)
22
27
  @scope = AskChatGPT.included_prompts.dup
23
28
  @client = client
@@ -26,6 +31,9 @@ module AskChatgpt
26
31
  end
27
32
 
28
33
  def inspect
34
+ return if @executed
35
+ @executed = true
36
+
29
37
  pp(executor_parameters) if AskChatGPT.debug
30
38
  call_with_validations do
31
39
  case AskChatGPT.mode
@@ -35,11 +43,14 @@ module AskChatgpt
35
43
  call_sync
36
44
  end
37
45
  end
38
- rescue InputError => e
46
+ nil
47
+ rescue SystemExit, SignalException, InputError, NoMethodError => e
39
48
  puts e.message
40
49
  rescue StandardError => e
41
50
  puts e.message
42
51
  puts e.backtrace.take(5).join("\n")
52
+ rescue Exception => e
53
+ puts e.message
43
54
  ensure
44
55
  nil
45
56
  end
@@ -68,18 +79,22 @@ module AskChatgpt
68
79
  print content_part
69
80
  end
70
81
  }))
82
+ spinner&.stop if spinner&.spinning?
71
83
  if AskChatGPT.markdown
84
+ result = content.compact.join
85
+ shift = result.split("\n").size == 1 ? 1 : result.split("\n").size + 1
72
86
  # re-draw the screen
73
87
  # go back to the top by the number of new lines previously printed
74
- print cursor.clear_lines(content.compact.join.split("\n").size + 1, :up)
88
+ print cursor.clear_lines(shift, :up)
75
89
  # print cursor.restore
76
90
  # print cursor.down
77
91
  # print cursor.clear_screen_down
78
92
  # $content = content.compact.join
79
- puts(TTY::Markdown.parse(content.compact.join))
93
+ puts(TTY::Markdown.parse(result))
80
94
  else
81
95
  # nothing, content is already printed in the stream
82
96
  end
97
+ nil
83
98
  end
84
99
 
85
100
  # wait for the whole response and print it at once
@@ -98,6 +113,7 @@ module AskChatgpt
98
113
  puts(content)
99
114
  end
100
115
  end
116
+ nil
101
117
  end
102
118
 
103
119
  def executor_parameters
@@ -7,6 +7,7 @@ module AskChatgpt
7
7
  method_or_class_or_str.source
8
8
  when Module, Class, String
9
9
  str = capture_io do
10
+ init_irb unless IRB.CurrentContext
10
11
  IRB.CurrentContext.main.irb_show_source(method_or_class_or_str.to_s)
11
12
  end.join("\n")
12
13
  # check if source was extracted
@@ -22,6 +23,48 @@ module AskChatgpt
22
23
 
23
24
  private
24
25
 
26
+ class DummyInputMethod < ::IRB::InputMethod
27
+ attr_reader :list, :line_no
28
+
29
+ def initialize(list = [])
30
+ super("test")
31
+ @line_no = 0
32
+ @list = list
33
+ end
34
+
35
+ def gets
36
+ @list[@line_no]&.tap {@line_no += 1}
37
+ end
38
+
39
+ def eof?
40
+ @line_no >= @list.size
41
+ end
42
+
43
+ def encoding
44
+ Encoding.default_external
45
+ end
46
+
47
+ def reset
48
+ @line_no = 0
49
+ end
50
+ end
51
+
52
+ # https://github.com/ruby/irb/blob/95782f2cf93b4b32e51104a21a860aa4491a04bc/test/irb/test_cmd.rb#L36
53
+ def init_irb
54
+ conf = {}
55
+ main = :self
56
+ irb_path = nil
57
+ IRB.init_config(nil)
58
+ IRB.conf[:VERBOSE] = false
59
+ IRB.conf[:PROMPT_MODE] = :SIMPLE
60
+ IRB.conf.merge!(conf)
61
+ input = DummyInputMethod.new("TestInputMethod#gets")
62
+ irb = IRB::Irb.new(IRB::WorkSpace.new(main), input)
63
+ irb.context.return_format = "=> %s\n"
64
+ irb.context.irb_path = irb_path if irb_path
65
+ IRB.conf[:MAIN_CONTEXT] = irb.context
66
+ end
67
+
25
68
  # from https://github.com/minitest/minitest/blob/master/lib/minitest/assertions.rb#L542
26
69
  def capture_io
27
70
  _synchronize do
@@ -1,3 +1,3 @@
1
1
  module AskChatgpt
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/lib/ask_chatgpt.rb CHANGED
@@ -9,11 +9,11 @@ require "openai"
9
9
  require "tty-markdown"
10
10
  require "tty-spinner"
11
11
  require "tty-cursor"
12
+ require "irb"
12
13
 
13
14
  require_relative "ask_chatgpt/console"
14
15
  require_relative "ask_chatgpt/executor"
15
16
  require_relative "ask_chatgpt/helpers"
16
- require_relative "ask_chatgpt/core"
17
17
 
18
18
  module AskChatgpt
19
19
  ::AskChatGPT = AskChatgpt
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ask_chatgpt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-04-29 00:00:00.000000000 Z
12
+ date: 2023-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -138,7 +138,6 @@ files:
138
138
  - bin/ask_chatgpt
139
139
  - lib/ask_chatgpt.rb
140
140
  - lib/ask_chatgpt/console.rb
141
- - lib/ask_chatgpt/core.rb
142
141
  - lib/ask_chatgpt/default_behavior.rb
143
142
  - lib/ask_chatgpt/executor.rb
144
143
  - lib/ask_chatgpt/helpers.rb
@@ -161,11 +160,11 @@ files:
161
160
  - lib/generators/ask_chatgpt/ask_chatgpt_generator.rb
162
161
  - lib/generators/ask_chatgpt/templates/template.rb
163
162
  - lib/tasks/ask_chatgpt_tasks.rake
164
- homepage: https://github.com/railsjazz.com/ask_chatgpt
163
+ homepage: https://github.com/railsjazz/ask_chatgpt
165
164
  licenses:
166
165
  - MIT
167
166
  metadata:
168
- homepage_uri: https://github.com/railsjazz.com/ask_chatgpt
167
+ homepage_uri: https://github.com/railsjazz/ask_chatgpt
169
168
  post_install_message:
170
169
  rdoc_options: []
171
170
  require_paths:
@@ -1,8 +0,0 @@
1
- module AskChatgpt
2
- class Core
3
- def self.call
4
- client = OpenAI::Client.new(access_token: AskChatGPT.access_token)
5
- AskChatgpt::Executor.new(client)
6
- end
7
- end
8
- end