Class: Richat::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/richat/shell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @chat_client = options[:chat_client]
  @logger = options[: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.expand_path(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_roleObject (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_clientObject (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_pathObject (readonly)

Returns the value of attribute history_path.



5
6
7
# File 'lib/richat/shell.rb', line 5

def history_path
  @history_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/richat/shell.rb', line 5

def logger
  @logger
end

#system_roleObject (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_colorObject (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_roleObject (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

#callObject



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
  enable_context_message = 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?
    context_messages = []
  else
    context_messages = [{ 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, enable_context_message), 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
          context_messages = [{ 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
          enable_context_message = !enable_context_message
          unless enable_context_message
            context_messages = Command.prompt_id ? [{ role: 'system', content: Command.prompt }] : []
          end
          Command.print_info("chat context mode " + (enable_context_message ? "enabled." : "disabled."))
          next
        elsif code == Command::RESET_CONTEXT_CODE
          context_messages = 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 = ''

      context_messages << { role: 'user', content: user_content }

      chat_client.call(context_messages) 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 enable_context_message
        context_messages << { role: 'assistant', content: response }
      else
        context_messages.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.message
    if sys_cmd_mode
      retry
    else
      Command.handle_exit
    end
  rescue Exception => e
    puts e.message
    Command.handle_exit
  end
end