Class: Richat::Shell
- Inherits:
-
Object
- Object
- Richat::Shell
- Defined in:
- lib/richat/shell.rb
Instance Attribute Summary collapse
-
#ai_role ⇒ Object
readonly
Returns the value of attribute ai_role.
-
#chat_client ⇒ Object
readonly
Returns the value of attribute chat_client.
-
#history_path ⇒ Object
readonly
Returns the value of attribute history_path.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#system_role ⇒ Object
readonly
Returns the value of attribute system_role.
-
#user_color ⇒ Object
readonly
Returns the value of attribute user_color.
-
#user_role ⇒ Object
readonly
Returns the value of attribute user_role.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(options = {}) ⇒ Shell
constructor
A new instance of Shell.
Constructor Details
#initialize(options = {}) ⇒ Shell
Returns a new instance of Shell.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/richat/shell.rb', line 7 def initialize( = {}) @chat_client = [:chat_client] @logger = [:logger] @user_role = Config.get("log", "user_role") @ai_role = Config.get("log", "ai_role") @system_role = Config.get("log", "system_role") @history_path = File.(Config.get("shell", "shell_history_file")) File.open(@history_path, 'w') {} unless File.exist?(@history_path) @user_color = ColorCode.get_code(Config.get("shell", "user_content_color")) end |
Instance Attribute Details
#ai_role ⇒ Object (readonly)
Returns the value of attribute ai_role.
5 6 7 |
# File 'lib/richat/shell.rb', line 5 def ai_role @ai_role end |
#chat_client ⇒ Object (readonly)
Returns the value of attribute chat_client.
5 6 7 |
# File 'lib/richat/shell.rb', line 5 def chat_client @chat_client end |
#history_path ⇒ Object (readonly)
Returns the value of attribute history_path.
5 6 7 |
# File 'lib/richat/shell.rb', line 5 def history_path @history_path end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
5 6 7 |
# File 'lib/richat/shell.rb', line 5 def logger @logger end |
#system_role ⇒ Object (readonly)
Returns the value of attribute system_role.
5 6 7 |
# File 'lib/richat/shell.rb', line 5 def system_role @system_role end |
#user_color ⇒ Object (readonly)
Returns the value of attribute user_color.
5 6 7 |
# File 'lib/richat/shell.rb', line 5 def user_color @user_color end |
#user_role ⇒ Object (readonly)
Returns the value of attribute user_role.
5 6 7 |
# File 'lib/richat/shell.rb', line 5 def user_role @user_role end |
Instance Method Details
#call ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/richat/shell.rb', line 18 def call = Config.get("shell", "enable_chat_context") enable_full_completion = Config.get("shell", "save_shell_history") if enable_full_completion Readline::HISTORY.push(*File.readlines(history_path).last(1000).map(&:chomp)) end Readline.completion_proc = proc { |s| Readline::HISTORY&.grep(/^#{Regexp.escape(s)}/) } Readline.completion_append_character = "" default_prompt = Command.load_default_prompt if default_prompt.nil? || default_prompt.empty? = [] else = [{ role: 'system', content: default_prompt }] end Command.print_welcome if Config.get("shell", "show_welcome_info") sys_cmd_mode = false begin while (user_content = Readline.readline(shell_prompt(sys_cmd_mode, ), true)) if user_content.empty? Readline::HISTORY&.pop next end File.open(history_path, 'a') { |f| f.puts(user_content) } if enable_full_completion if (code = Command.call(user_content, sys_cmd_mode)) if code == Command::NEXT_CODE next elsif code == Command::EXIT_CODE break elsif code == Command::PROMPT_CHANGED_CODE = [{ role: 'system', content: Command.prompt }] next elsif code == Command::SYS_CMD_CODE sys_cmd_mode = true next elsif code == Command::SYS_CHAT_CODE sys_cmd_mode = false next elsif code == Command::TOGGLE_CONTEXT_CODE = ! unless = Command.prompt_id ? [{ role: 'system', content: Command.prompt }] : [] end Command.print_info("chat context mode " + ( ? "enabled." : "disabled.")) next elsif code == Command::RESET_CONTEXT_CODE = Command.prompt_id ? [{ role: 'system', content: Command.prompt }] : [] Command.print_info("chat context reset.") next end end print "\e[0m" logger.call(role: user_role, content: user_content) response = '' << { role: 'user', content: user_content } chat_client.call() do |chunk| response += chunk print chunk end if response.empty? Command.print_exception("Empty response from ChatGPT.") break end logger.call(role: ai_role, content: response) if << { role: 'assistant', content: response } else .pop end puts "\n\n" end rescue Interrupt if sys_cmd_mode Command.kill_process retry else Command.handle_exit end rescue SystemCallError => e puts e. if sys_cmd_mode retry else Command.handle_exit end rescue Exception => e puts e. Command.handle_exit end end |