richat 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/richat/command.rb +25 -3
- data/lib/richat/config.rb +8 -1
- data/lib/richat/shell.rb +23 -2
- data/lib/richat/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b76efa6dcbdfe7a9f2184c7823b6c0292a6ce8b35ed44378679c06b0c847503
|
4
|
+
data.tar.gz: cda10863c00442ecd0a5c24c5758251bcfa1bb04b84f623843c2b6df30d1ead5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3251caa055e0e5e60b9cac6981e81b52c083eebf06c5c5a4d4e2f115171f4445ed79142694ef514c1f067aed13276f0e03230f85896e8fce425804abb2cbbd16
|
7
|
+
data.tar.gz: '04008b119d99774c298f20f3d631a938221be60951df86e8e1c0eb6630060f3762561cfc62798f6cfa65746465c1b704febf63f1834dda82da248679d5adc6ea'
|
data/lib/richat/command.rb
CHANGED
@@ -3,19 +3,21 @@ module Richat
|
|
3
3
|
EXIT_CODE = 0
|
4
4
|
NEXT_CODE = 1
|
5
5
|
PROMPT_CHANGED_CODE = 2
|
6
|
+
SYS_CMD_CODE = 3
|
7
|
+
SYS_CHAT_CODE = 4
|
6
8
|
|
7
9
|
class << self
|
8
10
|
attr_reader :prompt, :prompt_id
|
9
11
|
|
10
|
-
def call(user_input)
|
12
|
+
def call(user_input, sys_cmd_mode=false)
|
11
13
|
user_input = user_input.strip
|
14
|
+
return handle_system_command(user_input, sys_cmd_mode) if user_input.start_with?(*Config.get("sys_cmd", "activate_keywords"))
|
15
|
+
return handle_exit if Config.get("shell", "exit_keywords").include?(user_input)
|
12
16
|
return unless user_input.start_with?("/")
|
13
17
|
if user_input == "/help"
|
14
18
|
handle_help
|
15
19
|
elsif user_input == "/config"
|
16
20
|
handle_config
|
17
|
-
elsif user_input == "/exit"
|
18
|
-
handle_exit
|
19
21
|
elsif user_input =~ /^\/prompt\s*/
|
20
22
|
if user_input == "/prompt"
|
21
23
|
handle_prompt
|
@@ -25,6 +27,25 @@ module Richat
|
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
30
|
+
def handle_system_command(user_input, sys_cmd_mode)
|
31
|
+
Config.get("sys_cmd", "activate_keywords").each do |sub_str|
|
32
|
+
if user_input.start_with?(sub_str)
|
33
|
+
cmd = user_input[sub_str.length..-1].strip
|
34
|
+
return SYS_CHAT_CODE if Config.get("sys_cmd", "deactivate_keywords").include?(cmd) && sys_cmd_mode
|
35
|
+
if (match = /^cd\s?(.*)$/.match(cmd))
|
36
|
+
Dir.chdir(File.expand_path(match[1].empty? ? "~" : match[1]))
|
37
|
+
else
|
38
|
+
system(cmd)
|
39
|
+
end
|
40
|
+
if sys_cmd_mode
|
41
|
+
return NEXT_CODE
|
42
|
+
else
|
43
|
+
return SYS_CMD_CODE, sub_str
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
28
49
|
def handle_exit
|
29
50
|
puts "Bye"
|
30
51
|
EXIT_CODE
|
@@ -37,6 +58,7 @@ module Richat
|
|
37
58
|
end
|
38
59
|
|
39
60
|
def handle_help
|
61
|
+
puts "Version #{VERSION}"
|
40
62
|
puts "\e[32m/exit\e[0m exit Richat"
|
41
63
|
puts "\e[32m/config\e[0m show configuration"
|
42
64
|
puts "\e[32m/prompt\e[0m show prompt list"
|
data/lib/richat/config.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
1
3
|
module Richat
|
2
4
|
class Config
|
3
5
|
DEFAULT_CONFIG = {
|
@@ -18,7 +20,12 @@ module Richat
|
|
18
20
|
"save_shell_history" => true,
|
19
21
|
"enable_chat_context" => true,
|
20
22
|
"show_welcome_info" => true,
|
21
|
-
"shell_history_file" => "~/.richat/history.txt"
|
23
|
+
"shell_history_file" => "~/.richat/history.txt",
|
24
|
+
"exit_keywords" => ["/exit", "q", "quit", "exit"]
|
25
|
+
},
|
26
|
+
"sys_cmd" => {
|
27
|
+
"activate_keywords" => [">", "!"],
|
28
|
+
"deactivate_keywords" => ["q", "quit", "exit"]
|
22
29
|
},
|
23
30
|
"prompt" => {
|
24
31
|
"prompt_dir" => "~/.richat/prompts",
|
data/lib/richat/shell.rb
CHANGED
@@ -33,17 +33,21 @@ module Richat
|
|
33
33
|
end
|
34
34
|
|
35
35
|
Command.print_welcome if Config.get("shell", "show_welcome_info")
|
36
|
+
sys_cmd_mode = false
|
37
|
+
current_cmd_prefix = nil
|
36
38
|
|
37
39
|
begin
|
38
|
-
while (user_content = Readline.readline(
|
40
|
+
while (user_content = Readline.readline(shell_prompt(sys_cmd_mode, current_cmd_prefix), true))
|
39
41
|
if user_content.empty?
|
40
42
|
Readline::HISTORY&.pop
|
41
43
|
next
|
42
44
|
end
|
43
45
|
|
46
|
+
user_content = current_cmd_prefix + user_content if sys_cmd_mode
|
44
47
|
File.open(history_path, 'a') { |f| f.puts(user_content) } if enable_full_completion
|
45
48
|
|
46
|
-
|
49
|
+
code, _ = Command.call(user_content, sys_cmd_mode)
|
50
|
+
if code
|
47
51
|
if code == Command::NEXT_CODE
|
48
52
|
next
|
49
53
|
elsif code == Command::EXIT_CODE
|
@@ -51,6 +55,13 @@ module Richat
|
|
51
55
|
elsif code == Command::PROMPT_CHANGED_CODE
|
52
56
|
context_messages = [{ role: 'system', content: Command.prompt }]
|
53
57
|
next
|
58
|
+
elsif code == Command::SYS_CMD_CODE
|
59
|
+
sys_cmd_mode = true
|
60
|
+
current_cmd_prefix = _
|
61
|
+
next
|
62
|
+
elsif code == Command::SYS_CHAT_CODE
|
63
|
+
sys_cmd_mode = false
|
64
|
+
next
|
54
65
|
end
|
55
66
|
end
|
56
67
|
|
@@ -84,5 +95,15 @@ module Richat
|
|
84
95
|
Command.handle_exit
|
85
96
|
end
|
86
97
|
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def shell_prompt(sys_cmd_mode, current_cmd_prefix)
|
102
|
+
if sys_cmd_mode
|
103
|
+
"\e[32m#{Command.prompt_id&.+" "}\e[0m\e[33m>>\e[0m \e[32m#{current_cmd_prefix}\e[0m"
|
104
|
+
else
|
105
|
+
"\e[32m#{Command.prompt_id&.+" "}\e[0m\e[33m>> \e[0m"
|
106
|
+
end
|
107
|
+
end
|
87
108
|
end
|
88
109
|
end
|
data/lib/richat/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: richat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fzdp
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-04-
|
11
|
+
date: 2023-04-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -67,8 +67,9 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.12'
|
69
69
|
description: Richat is a command-line ChatGPT tool implemented in Ruby that supports
|
70
|
-
highly customizable configuration. It can save chat
|
71
|
-
on historical inputs, allows for prompt
|
70
|
+
highly customizable configuration. It can save chat contents, performs fuzzy searches
|
71
|
+
on historical inputs, allows for prompt switching at any time and can event act
|
72
|
+
as Linux terminal.
|
72
73
|
email:
|
73
74
|
- fzdp01@gmail.com
|
74
75
|
executables:
|